Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * A mini 'powertop' utility: |
| 4 | * Analyze power consumption on Intel-based laptops. |
| 5 | * Based on powertop 1.11. |
| 6 | * |
| 7 | * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com> |
| 8 | * |
| 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 10 | */ |
| 11 | |
| 12 | //applet:IF_POWERTOP(APPLET(powertop, _BB_DIR_BIN, _BB_SUID_DROP)) |
| 13 | |
| 14 | //kbuild:lib-$(CONFIG_POWERTOP) += powertop.o |
| 15 | |
| 16 | //config:config POWERTOP |
| 17 | //config: bool "powertop" |
| 18 | //config: default y |
| 19 | //config: help |
| 20 | //config: Analyze power consumption on Intel-based laptops |
| 21 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 22 | // XXX This should be configurable |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 23 | #define ENABLE_FEATURE_POWERTOP_PROCIRQ 1 |
| 24 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 25 | #include "libbb.h" |
| 26 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 27 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 28 | //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) |
| 29 | #define debug(fmt, ...) ((void)0) |
| 30 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 31 | |
| 32 | #define BLOATY_HPET_IRQ_NUM_DETECTION 0 |
| 33 | #define MAX_CSTATE_COUNT 8 |
| 34 | #define IRQCOUNT 40 |
| 35 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 36 | |
| 37 | #define DEFAULT_SLEEP 10 |
| 38 | #define DEFAULT_SLEEP_STR "10" |
| 39 | |
| 40 | /* Frequency of the ACPI timer */ |
| 41 | #define FREQ_ACPI 3579.545 |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 42 | #define FREQ_ACPI_1000 3579545 |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 43 | |
| 44 | /* Max filename length of entry in /sys/devices subsystem */ |
| 45 | #define BIG_SYSNAME_LEN 16 |
| 46 | |
| 47 | typedef unsigned long long ullong; |
| 48 | |
| 49 | struct line { |
| 50 | char *string; |
| 51 | int count; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 52 | /*int disk_count;*/ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | #if ENABLE_FEATURE_POWERTOP_PROCIRQ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 56 | struct irqdata { |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 57 | smallint active; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 58 | int number; |
| 59 | ullong count; |
| 60 | char irq_desc[32]; |
| 61 | }; |
| 62 | #endif |
| 63 | |
| 64 | struct globals { |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 65 | struct line *lines; /* the most often used member */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 66 | int lines_cnt; |
| 67 | int lines_cumulative_count; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 68 | int maxcstate; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 69 | unsigned total_cpus; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 70 | smallint cant_enable_timer_stats; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 71 | #if ENABLE_FEATURE_POWERTOP_PROCIRQ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 72 | # if BLOATY_HPET_IRQ_NUM_DETECTION |
| 73 | smallint scanned_timer_list; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 74 | int percpu_hpet_start; |
| 75 | int percpu_hpet_end; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 76 | # endif |
| 77 | int interrupt_0; |
| 78 | int total_interrupt; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 79 | struct irqdata interrupts[IRQCOUNT]; |
| 80 | #endif |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 81 | ullong start_usage[MAX_CSTATE_COUNT]; |
| 82 | ullong last_usage[MAX_CSTATE_COUNT]; |
| 83 | ullong start_duration[MAX_CSTATE_COUNT]; |
| 84 | ullong last_duration[MAX_CSTATE_COUNT]; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 85 | #if ENABLE_FEATURE_USE_TERMIOS |
| 86 | struct termios init_settings; |
| 87 | #endif |
| 88 | }; |
| 89 | #define G (*ptr_to_globals) |
| 90 | #define INIT_G() do { \ |
| 91 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 92 | } while (0) |
| 93 | |
| 94 | #if ENABLE_FEATURE_USE_TERMIOS |
| 95 | static void reset_term(void) |
| 96 | { |
| 97 | tcsetattr_stdin_TCSANOW(&G.init_settings); |
| 98 | } |
| 99 | |
| 100 | static void sig_handler(int signo UNUSED_PARAM) |
| 101 | { |
| 102 | reset_term(); |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 103 | _exit(EXIT_FAILURE); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 104 | } |
| 105 | #endif |
| 106 | |
| 107 | static int write_str_to_file(const char *fname, const char *str) |
| 108 | { |
| 109 | FILE *fp = fopen_for_write(fname); |
| 110 | if (!fp) |
| 111 | return 1; |
| 112 | fputs(str, fp); |
| 113 | fclose(fp); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* Make it more readable */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 118 | #define start_timer() write_str_to_file("/proc/timer_stats", "1\n") |
| 119 | #define stop_timer() write_str_to_file("/proc/timer_stats", "0\n") |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 120 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 121 | static NOINLINE void clear_lines(void) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 122 | { |
| 123 | int i; |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 124 | if (G.lines) { |
| 125 | for (i = 0; i < G.lines_cnt; i++) |
| 126 | free(G.lines[i].string); |
| 127 | free(G.lines); |
| 128 | G.lines_cnt = 0; |
| 129 | G.lines = NULL; |
| 130 | } |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 133 | static void update_lines_cumulative_count(void) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 134 | { |
| 135 | int i; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 136 | for (i = 0; i < G.lines_cnt; i++) |
| 137 | G.lines_cumulative_count += G.lines[i].count; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int line_compare(const void *p1, const void *p2) |
| 141 | { |
| 142 | const struct line *a = p1; |
| 143 | const struct line *b = p2; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 144 | return (b->count /*+ 50 * b->disk_count*/) - (a->count /*+ 50 * a->disk_count*/); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 147 | static void sort_lines(void) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 148 | { |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 149 | qsort(G.lines, G.lines_cnt, sizeof(G.lines[0]), line_compare); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 152 | /* Save C-state usage and duration. Also update maxcstate. */ |
| 153 | static void read_cstate_counts(ullong *usage, ullong *duration) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 154 | { |
| 155 | DIR *dir; |
| 156 | struct dirent *d; |
| 157 | |
| 158 | dir = opendir("/proc/acpi/processor"); |
| 159 | if (!dir) |
| 160 | return; |
| 161 | |
| 162 | while ((d = readdir(dir)) != NULL) { |
| 163 | FILE *fp; |
| 164 | char buf[192]; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 165 | int level; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 166 | int len; |
| 167 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 168 | len = strlen(d->d_name); /* "CPUnn" */ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 169 | if (len < 3 || len > BIG_SYSNAME_LEN) |
| 170 | continue; |
| 171 | |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 172 | sprintf(buf, "%s/%s/power", "/proc/acpi/processor", d->d_name); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 173 | fp = fopen_for_read(buf); |
| 174 | if (!fp) |
| 175 | continue; |
| 176 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 177 | // Example file contents: |
| 178 | // active state: C0 |
| 179 | // max_cstate: C8 |
| 180 | // maximum allowed latency: 2000000000 usec |
| 181 | // states: |
| 182 | // C1: type[C1] promotion[--] demotion[--] latency[001] usage[00006173] duration[00000000000000000000] |
| 183 | // C2: type[C2] promotion[--] demotion[--] latency[001] usage[00085191] duration[00000000000083024907] |
| 184 | // C3: type[C3] promotion[--] demotion[--] latency[017] usage[01017622] duration[00000000017921327182] |
| 185 | level = 0; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 186 | while (fgets(buf, sizeof(buf), fp)) { |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 187 | char *p = strstr(buf, "age["); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 188 | if (!p) |
| 189 | continue; |
| 190 | p += 4; |
| 191 | usage[level] += bb_strtoull(p, NULL, 10) + 1; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 192 | p = strstr(buf, "ation["); |
| 193 | if (!p) |
| 194 | continue; |
| 195 | p += 6; |
| 196 | duration[level] += bb_strtoull(p, NULL, 10); |
| 197 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 198 | if (level >= MAX_CSTATE_COUNT-1) |
| 199 | break; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 200 | level++; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 201 | if (level > G.maxcstate) /* update maxcstate */ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 202 | G.maxcstate = level; |
| 203 | } |
| 204 | fclose(fp); |
| 205 | } |
| 206 | closedir(dir); |
| 207 | } |
| 208 | |
| 209 | /* Add line and/or update count */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 210 | static void save_line(const char *string, int count) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 211 | { |
| 212 | int i; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 213 | for (i = 0; i < G.lines_cnt; i++) { |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 214 | if (strcmp(string, G.lines[i].string) == 0) { |
| 215 | /* It's already there, only update count */ |
| 216 | G.lines[i].count += count; |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 221 | /* Add new line */ |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 222 | G.lines = xrealloc_vector(G.lines, 4, G.lines_cnt); |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 223 | G.lines[G.lines_cnt].string = xstrdup(string); |
| 224 | G.lines[G.lines_cnt].count = count; |
| 225 | /*G.lines[G.lines_cnt].disk_count = 0;*/ |
| 226 | G.lines_cnt++; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | #if ENABLE_FEATURE_POWERTOP_PROCIRQ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 230 | static int is_hpet_irq(const char *name) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 231 | { |
| 232 | char *p; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 233 | # if BLOATY_HPET_IRQ_NUM_DETECTION |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 234 | long hpet_chan; |
| 235 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 236 | /* Learn the range of existing hpet timers. This is done once */ |
| 237 | if (!G.scanned_timer_list) { |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 238 | FILE *fp; |
| 239 | char buf[80]; |
| 240 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 241 | G.scanned_timer_list = true; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 242 | fp = fopen_for_read("/proc/timer_list"); |
| 243 | if (!fp) |
| 244 | return 0; |
| 245 | |
| 246 | while (fgets(buf, sizeof(buf), fp)) { |
| 247 | p = strstr(buf, "Clock Event Device: hpet"); |
| 248 | if (!p) |
| 249 | continue; |
| 250 | p += sizeof("Clock Event Device: hpet")-1; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 251 | if (!isdigit(*p)) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 252 | continue; |
| 253 | hpet_chan = xatoi_positive(p); |
| 254 | if (hpet_chan < G.percpu_hpet_start) |
| 255 | G.percpu_hpet_start = hpet_chan; |
| 256 | if (hpet_chan > G.percpu_hpet_end) |
| 257 | G.percpu_hpet_end = hpet_chan; |
| 258 | } |
| 259 | fclose(fp); |
| 260 | } |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 261 | # endif |
| 262 | //TODO: optimize |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 263 | p = strstr(name, "hpet"); |
| 264 | if (!p) |
| 265 | return 0; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 266 | p += 4; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 267 | if (!isdigit(*p)) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 268 | return 0; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 269 | # if BLOATY_HPET_IRQ_NUM_DETECTION |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 270 | hpet_chan = xatoi_positive(p); |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 271 | if (hpet_chan < G.percpu_hpet_start || hpet_chan > G.percpu_hpet_end) |
| 272 | return 0; |
| 273 | # endif |
| 274 | return 1; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 277 | /* Save new IRQ count, return delta from old one */ |
| 278 | static int save_irq_count(int irq, ullong count) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 279 | { |
| 280 | int unused = IRQCOUNT; |
| 281 | int i; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 282 | for (i = 0; i < IRQCOUNT; i++) { |
| 283 | if (G.interrupts[i].active && G.interrupts[i].number == irq) { |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 284 | ullong old = G.interrupts[i].count; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 285 | G.interrupts[i].count = count; |
| 286 | return count - old; |
| 287 | } |
| 288 | if (!G.interrupts[i].active && unused > i) |
| 289 | unused = i; |
| 290 | } |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 291 | if (unused < IRQCOUNT) { |
| 292 | G.interrupts[unused].active = 1; |
| 293 | G.interrupts[unused].count = count; |
| 294 | G.interrupts[unused].number = irq; |
| 295 | } |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 296 | return count; |
| 297 | } |
| 298 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 299 | /* Read /proc/interrupts, save IRQ counts and IRQ description */ |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 300 | static void process_irq_counts(void) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 301 | { |
| 302 | FILE *fp; |
| 303 | char buf[128]; |
| 304 | |
| 305 | /* Reset values */ |
| 306 | G.interrupt_0 = 0; |
| 307 | G.total_interrupt = 0; |
| 308 | |
| 309 | fp = xfopen_for_read("/proc/interrupts"); |
| 310 | while (fgets(buf, sizeof(buf), fp)) { |
| 311 | char irq_desc[sizeof(" <kernel IPI> : ") + sizeof(buf)]; |
| 312 | char *p; |
| 313 | const char *name; |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 314 | int nr; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 315 | ullong count; |
| 316 | ullong delta; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 317 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 318 | p = strchr(buf, ':'); |
| 319 | if (!p) |
| 320 | continue; |
| 321 | /* 0: 143646045 153901007 IO-APIC-edge timer |
| 322 | * ^ |
| 323 | */ |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 324 | *p = '\0'; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 325 | /* Deal with non-maskable interrupts -- make up fake numbers */ |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 326 | nr = index_in_strings("NMI\0RES\nCAL\0TLB\0TRM\0THR\0SPU\0", buf); |
| 327 | if (nr != -1) { |
| 328 | nr += 20000; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 329 | } else { |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 330 | /* bb_strtou doesn't eat leading spaces, using strtoul */ |
| 331 | nr = strtoul(buf, NULL, 10); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 332 | } |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 333 | *p = ':'; |
| 334 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 335 | if (nr == -1) |
| 336 | continue; |
| 337 | |
| 338 | p++; |
| 339 | /* 0: 143646045 153901007 IO-APIC-edge timer |
| 340 | * ^ |
| 341 | */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 342 | /* Sum counts for this IRQ */ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 343 | count = 0; |
| 344 | while (1) { |
| 345 | char *tmp; |
| 346 | p = skip_whitespace(p); |
| 347 | if (!isdigit(*p)) |
| 348 | break; |
| 349 | count += bb_strtoull(p, &tmp, 10); |
| 350 | p = tmp; |
| 351 | } |
| 352 | /* 0: 143646045 153901007 IO-APIC-edge timer |
| 353 | * NMI: 1 2 Non-maskable interrupts |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 354 | * ^ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 355 | */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 356 | if (nr < 20000) { |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 357 | /* Skip to the interrupt name, e.g. 'timer' */ |
| 358 | p = strchr(p, ' '); |
| 359 | if (!p) |
| 360 | continue; |
| 361 | p = skip_whitespace(p); |
| 362 | } |
| 363 | |
| 364 | name = p; |
| 365 | strchrnul(name, '\n')[0] = '\0'; |
| 366 | /* Save description of the interrupt */ |
Denys Vlasenko | d8b687f | 2010-10-26 12:42:53 +0200 | [diff] [blame] | 367 | if (nr >= 20000) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 368 | sprintf(irq_desc, " <kernel IPI> : %s", name); |
| 369 | else |
| 370 | sprintf(irq_desc, " <interrupt> : %s", name); |
| 371 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 372 | delta = save_irq_count(nr, count); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 373 | |
| 374 | /* Skip per CPU timer interrupts */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 375 | if (is_hpet_irq(name)) |
| 376 | continue; |
| 377 | |
| 378 | if (nr != 0 && delta != 0) |
| 379 | save_line(irq_desc, delta); |
| 380 | |
| 381 | if (nr == 0) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 382 | G.interrupt_0 = delta; |
| 383 | else |
| 384 | G.total_interrupt += delta; |
| 385 | } |
| 386 | |
| 387 | fclose(fp); |
| 388 | } |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 389 | #else /* !ENABLE_FEATURE_POWERTOP_PROCIRQ */ |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 390 | # define process_irq_counts() ((void)0) |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 391 | #endif |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 392 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 393 | static NOINLINE int process_timer_stats(void) |
| 394 | { |
| 395 | char buf[128]; |
| 396 | char line[15 + 3 + 128]; |
| 397 | int n; |
| 398 | ullong totalticks; |
| 399 | FILE *fp; |
| 400 | |
| 401 | buf[0] = '\0'; |
| 402 | totalticks = 0; |
| 403 | |
| 404 | fp = NULL; |
| 405 | if (!G.cant_enable_timer_stats) |
| 406 | fp = fopen_for_read("/proc/timer_stats"); |
| 407 | if (fp) { |
| 408 | // Example file contents: |
| 409 | // Timer Stats Version: v0.2 |
| 410 | // Sample period: 1.329 s |
| 411 | // 76, 0 swapper hrtimer_start_range_ns (tick_sched_timer) |
| 412 | // 88, 0 swapper hrtimer_start_range_ns (tick_sched_timer) |
| 413 | // 24, 3787 firefox hrtimer_start_range_ns (hrtimer_wakeup) |
| 414 | // 46D, 1136 kondemand/1 do_dbs_timer (delayed_work_timer_fn) |
| 415 | // ... |
| 416 | // 1, 1656 Xorg hrtimer_start_range_ns (hrtimer_wakeup) |
| 417 | // 1, 2159 udisks-daemon hrtimer_start_range_ns (hrtimer_wakeup) |
| 418 | // 331 total events, 249.059 events/sec |
| 419 | while (fgets(buf, sizeof(buf), fp)) { |
| 420 | const char *count, *process, *func; |
| 421 | char *p; |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 422 | int idx; |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 423 | |
| 424 | count = skip_whitespace(buf); |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 425 | if (strcmp(strchrnul(count, ' '), " total events") == 0) |
| 426 | break; |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 427 | p = strchr(count, ','); |
| 428 | if (!p) |
| 429 | continue; |
| 430 | *p++ = '\0'; |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 431 | if (strchr(count, 'D')) |
| 432 | continue; /* deferred */ |
| 433 | p = skip_whitespace(p); /* points to pid now */ |
| 434 | process = NULL; |
| 435 | get_func_name: |
| 436 | p = strchr(p, ' '); |
| 437 | if (!p) |
| 438 | continue; |
| 439 | *p++ = '\0'; |
| 440 | p = skip_whitespace(p); |
| 441 | if (process == NULL) { |
| 442 | process = p; |
| 443 | goto get_func_name; |
| 444 | } |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 445 | func = p; |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 446 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 447 | //if (strcmp(process, "swapper") == 0 |
| 448 | // && strcmp(func, "hrtimer_start_range_ns (tick_sched_timer)\n") == 0 |
| 449 | //) { |
| 450 | // process = "[kernel scheduler]"; |
| 451 | // func = "Load balancing tick"; |
| 452 | //} |
| 453 | |
| 454 | if (strncmp(func, "tick_nohz_", 10) == 0) |
| 455 | continue; |
| 456 | if (strncmp(func, "tick_setup_sched_timer", 20) == 0) |
| 457 | continue; |
| 458 | //if (strcmp(process, "powertop") == 0) |
| 459 | // continue; |
| 460 | |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 461 | idx = index_in_strings("insmod\0modprobe\0swapper\0", process); |
| 462 | if (idx != -1) { |
| 463 | process = idx < 2 ? "[kernel module]" : "<kernel core>"; |
| 464 | } |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 465 | |
| 466 | strchrnul(p, '\n')[0] = '\0'; |
| 467 | |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 468 | // 46D\01136\0kondemand/1\0do_dbs_timer (delayed_work_timer_fn) |
| 469 | // ^ ^ ^ |
| 470 | // count process func |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 471 | |
| 472 | //if (strchr(process, '[')) |
| 473 | sprintf(line, "%15.15s : %s", process, func); |
| 474 | //else |
| 475 | // sprintf(line, "%s", process); |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 476 | save_line(line, bb_strtoull(count, NULL, 10)); |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 477 | } |
| 478 | fclose(fp); |
| 479 | } |
| 480 | |
| 481 | n = 0; |
| 482 | #if ENABLE_FEATURE_POWERTOP_PROCIRQ |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 483 | if (strstr(buf, " total events")) { |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 484 | n = bb_strtoull(buf, NULL, 10) / G.total_cpus; |
| 485 | if (n > 0 && n < G.interrupt_0) { |
| 486 | sprintf(line, " <interrupt> : %s", "extra timer interrupt"); |
| 487 | save_line(line, G.interrupt_0 - n); |
| 488 | } |
| 489 | } |
| 490 | #endif |
| 491 | return n; |
| 492 | } |
| 493 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 494 | #ifdef __i386__ |
| 495 | /* |
| 496 | * Get information about CPU using CPUID opcode. |
| 497 | */ |
| 498 | static void cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, |
| 499 | unsigned int *edx) |
| 500 | { |
| 501 | /* EAX value specifies what information to return */ |
| 502 | __asm__( |
| 503 | " pushl %%ebx\n" /* Save EBX */ |
| 504 | " cpuid\n" |
| 505 | " movl %%ebx, %1\n" /* Save content of EBX */ |
| 506 | " popl %%ebx\n" /* Restore EBX */ |
| 507 | : "=a"(*eax), /* Output */ |
| 508 | "=r"(*ebx), |
| 509 | "=c"(*ecx), |
| 510 | "=d"(*edx) |
| 511 | : "0"(*eax), /* Input */ |
| 512 | "1"(*ebx), |
| 513 | "2"(*ecx), |
| 514 | "3"(*edx) |
| 515 | /* No clobbered registers */ |
| 516 | ); |
| 517 | } |
| 518 | #endif |
| 519 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 520 | #ifdef __i386__ |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 521 | static NOINLINE void print_intel_cstates(void) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 522 | { |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 523 | int bios_table[8] = { 0 }; |
| 524 | int nbios = 0; |
| 525 | DIR *cpudir; |
| 526 | struct dirent *d; |
| 527 | int i; |
| 528 | unsigned eax, ebx, ecx, edx; |
| 529 | |
| 530 | cpudir = opendir("/sys/devices/system/cpu"); |
| 531 | if (!cpudir) |
| 532 | return; |
| 533 | |
| 534 | /* Loop over cpuN entries */ |
| 535 | while ((d = readdir(cpudir)) != NULL) { |
| 536 | DIR *dir; |
| 537 | int len; |
| 538 | char fname[sizeof("/sys/devices/system/cpu//cpuidle//desc") + 2*BIG_SYSNAME_LEN]; |
| 539 | |
| 540 | len = strlen(d->d_name); |
| 541 | if (len < 3 || len > BIG_SYSNAME_LEN) |
| 542 | continue; |
| 543 | |
| 544 | if (!isdigit(d->d_name[3])) |
| 545 | continue; |
| 546 | |
Maksym Kryzhanovskyy | 6052c2b | 2010-11-04 08:41:57 +0100 | [diff] [blame^] | 547 | len = sprintf(fname, "%s/%s/cpuidle", "/sys/devices/system/cpu", d->d_name); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 548 | dir = opendir(fname); |
| 549 | if (!dir) |
| 550 | continue; |
| 551 | |
| 552 | /* |
| 553 | * Every C-state has its own stateN directory, that |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 554 | * contains a 'time' and a 'usage' file. |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 555 | */ |
| 556 | while ((d = readdir(dir)) != NULL) { |
| 557 | FILE *fp; |
| 558 | char buf[64]; |
| 559 | int n; |
| 560 | |
| 561 | n = strlen(d->d_name); |
| 562 | if (n < 3 || n > BIG_SYSNAME_LEN) |
| 563 | continue; |
| 564 | |
| 565 | sprintf(fname + len, "/%s/desc", d->d_name); |
| 566 | fp = fopen_for_read(fname); |
| 567 | if (fp) { |
| 568 | char *p = fgets(buf, sizeof(buf), fp); |
| 569 | fclose(fp); |
| 570 | if (!p) |
| 571 | break; |
| 572 | p = strstr(p, "MWAIT "); |
| 573 | if (p) { |
| 574 | int pos; |
| 575 | p += sizeof("MWAIT ") - 1; |
| 576 | pos = (bb_strtoull(p, NULL, 16) >> 4) + 1; |
| 577 | if (pos >= ARRAY_SIZE(bios_table)) |
| 578 | continue; |
| 579 | bios_table[pos]++; |
| 580 | nbios++; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | closedir(dir); |
| 585 | } |
| 586 | closedir(cpudir); |
| 587 | |
| 588 | if (!nbios) |
| 589 | return; |
| 590 | |
| 591 | eax = 5; |
| 592 | ebx = ecx = edx = 0; |
| 593 | cpuid(&eax, &ebx, &ecx, &edx); |
| 594 | if (!edx || !(ecx & 1)) |
| 595 | return; |
| 596 | |
| 597 | printf("Your CPU supports the following C-states: "); |
| 598 | i = 0; |
| 599 | while (edx) { |
| 600 | if (edx & 7) |
| 601 | printf("C%u ", i); |
| 602 | edx >>= 4; |
| 603 | i++; |
| 604 | } |
| 605 | bb_putchar('\n'); |
| 606 | |
| 607 | /* Print BIOS C-States */ |
| 608 | printf("Your BIOS reports the following C-states: "); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 609 | for (i = 0; i < ARRAY_SIZE(bios_table); i++) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 610 | if (bios_table[i]) |
| 611 | printf("C%u ", i); |
| 612 | |
| 613 | bb_putchar('\n'); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 614 | } |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 615 | #else |
Denys Vlasenko | f29a1c5 | 2010-10-29 16:25:18 +0200 | [diff] [blame] | 616 | # define print_intel_cstates() ((void)0) |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 617 | #endif |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 618 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 619 | static void show_timerstats(void) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 620 | { |
| 621 | unsigned lines; |
| 622 | |
| 623 | /* Get terminal height */ |
| 624 | get_terminal_width_height(STDOUT_FILENO, NULL, &lines); |
| 625 | |
| 626 | /* We don't have whole terminal just for timerstats */ |
| 627 | lines -= 12; |
| 628 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 629 | if (!G.cant_enable_timer_stats) { |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 630 | int i, n = 0; |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 631 | char strbuf6[6]; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 632 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 633 | strbuf6[5] = '\0'; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 634 | puts("\nTop causes for wakeups:"); |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 635 | for (i = 0; i < G.lines_cnt; i++) { |
| 636 | if ((G.lines[i].count > 0 /*|| G.lines[i].disk_count > 0*/) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 637 | && n++ < lines |
| 638 | ) { |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 639 | /* NB: upstream powertop prints "(wakeups/sec)", |
| 640 | * we print just "(wakeup counts)". |
| 641 | */ |
| 642 | /*char c = ' '; |
| 643 | if (G.lines[i].disk_count) |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 644 | c = 'D';*/ |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 645 | smart_ulltoa5(G.lines[i].count, strbuf6, " KMGTPEZY"); |
| 646 | printf(/*" %5.1f%% (%s)%c %s\n"*/ |
| 647 | " %5.1f%% (%s) %s\n", |
| 648 | G.lines[i].count * 100.0 / G.lines_cumulative_count, |
| 649 | strbuf6, /*c,*/ |
| 650 | G.lines[i].string); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | } else { |
| 654 | bb_putchar('\n'); |
| 655 | bb_error_msg("no stats available; run as root or" |
| 656 | " enable the cpufreq_stats module"); |
| 657 | } |
| 658 | } |
| 659 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 660 | // Example display from powertop version 1.11 |
| 661 | // Cn Avg residency P-states (frequencies) |
| 662 | // C0 (cpu running) ( 0.5%) 2.00 Ghz 0.0% |
| 663 | // polling 0.0ms ( 0.0%) 1.67 Ghz 0.0% |
| 664 | // C1 mwait 0.0ms ( 0.0%) 1333 Mhz 0.1% |
| 665 | // C2 mwait 0.1ms ( 0.1%) 1000 Mhz 99.9% |
| 666 | // C3 mwait 12.1ms (99.4%) |
| 667 | // |
| 668 | // Wakeups-from-idle per second : 93.6 interval: 15.0s |
| 669 | // no ACPI power usage estimate available |
| 670 | // |
| 671 | // Top causes for wakeups: |
Denys Vlasenko | 4fa07bd | 2010-10-27 00:04:50 +0200 | [diff] [blame] | 672 | // 32.4% ( 26.7) <interrupt> : extra timer interrupt |
| 673 | // 29.0% ( 23.9) <kernel core> : hrtimer_start_range_ns (tick_sched_timer) |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 674 | // 9.0% ( 7.5) <kernel core> : hrtimer_start (tick_sched_timer) |
| 675 | // 6.5% ( 5.3) <interrupt> : ata_piix |
| 676 | // 5.0% ( 4.1) inetd : hrtimer_start_range_ns (hrtimer_wakeup) |
| 677 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 678 | //usage:#define powertop_trivial_usage |
| 679 | //usage: "" |
| 680 | //usage:#define powertop_full_usage "\n\n" |
| 681 | //usage: "Analyze power consumption on Intel-based laptops\n" |
| 682 | |
| 683 | int powertop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 684 | int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv) |
| 685 | { |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 686 | ullong cur_usage[MAX_CSTATE_COUNT]; |
| 687 | ullong cur_duration[MAX_CSTATE_COUNT]; |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 688 | char cstate_lines[MAX_CSTATE_COUNT + 2][64]; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 689 | #if ENABLE_FEATURE_USE_TERMIOS |
| 690 | struct termios new_settings; |
| 691 | struct pollfd pfd[1]; |
| 692 | |
| 693 | pfd[0].fd = 0; |
| 694 | pfd[0].events = POLLIN; |
| 695 | #endif |
| 696 | |
| 697 | INIT_G(); |
| 698 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 699 | #if ENABLE_FEATURE_POWERTOP_PROCIRQ && BLOATY_HPET_IRQ_NUM_DETECTION |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 700 | G.percpu_hpet_start = INT_MAX; |
| 701 | G.percpu_hpet_end = INT_MIN; |
| 702 | #endif |
| 703 | |
| 704 | /* Print warning when we don't have superuser privileges */ |
| 705 | if (geteuid() != 0) |
| 706 | bb_error_msg("run as root to collect enough information"); |
| 707 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 708 | /* Get number of CPUs */ |
| 709 | G.total_cpus = get_cpu_count(); |
| 710 | |
| 711 | printf("Collecting data for "DEFAULT_SLEEP_STR" seconds\n"); |
| 712 | |
| 713 | #if ENABLE_FEATURE_USE_TERMIOS |
| 714 | tcgetattr(0, (void *)&G.init_settings); |
| 715 | memcpy(&new_settings, &G.init_settings, sizeof(new_settings)); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 716 | /* Turn on unbuffered input, turn off echoing */ |
| 717 | new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 718 | /* So we don't forget to reset term settings */ |
| 719 | atexit(reset_term); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 720 | bb_signals(BB_FATAL_SIGS, sig_handler); |
| 721 | tcsetattr_stdin_TCSANOW(&new_settings); |
| 722 | #endif |
| 723 | |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 724 | /* Collect initial data */ |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 725 | process_irq_counts(); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 726 | |
| 727 | /* Read initial usage and duration */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 728 | read_cstate_counts(G.start_usage, G.start_duration); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 729 | |
| 730 | /* Copy them to "last" */ |
| 731 | memcpy(G.last_usage, G.start_usage, sizeof(G.last_usage)); |
| 732 | memcpy(G.last_duration, G.start_duration, sizeof(G.last_duration)); |
| 733 | |
| 734 | /* Display C-states */ |
| 735 | print_intel_cstates(); |
| 736 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 737 | G.cant_enable_timer_stats |= stop_timer(); /* 1 on error */ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 738 | |
| 739 | /* The main loop */ |
| 740 | for (;;) { |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 741 | //double maxsleep = 0.0; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 742 | ullong totalticks, totalevents; |
| 743 | int i; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 744 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 745 | G.cant_enable_timer_stats |= start_timer(); /* 1 on error */ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 746 | #if !ENABLE_FEATURE_USE_TERMIOS |
| 747 | sleep(DEFAULT_SLEEP); |
| 748 | #else |
| 749 | if (safe_poll(pfd, 1, DEFAULT_SLEEP * 1000) > 0) { |
| 750 | unsigned char c; |
| 751 | if (safe_read(STDIN_FILENO, &c, 1) != 1) |
| 752 | break; /* EOF/error */ |
| 753 | if (c == G.init_settings.c_cc[VINTR]) |
| 754 | break; /* ^C */ |
| 755 | if ((c | 0x20) == 'q') |
| 756 | break; |
| 757 | } |
| 758 | #endif |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 759 | G.cant_enable_timer_stats |= stop_timer(); /* 1 on error */ |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 760 | |
| 761 | clear_lines(); |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 762 | process_irq_counts(); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 763 | |
| 764 | /* Clear the stats */ |
| 765 | memset(cur_duration, 0, sizeof(cur_duration)); |
| 766 | memset(cur_usage, 0, sizeof(cur_usage)); |
| 767 | |
| 768 | /* Read them */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 769 | read_cstate_counts(cur_usage, cur_duration); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 770 | |
| 771 | /* Count totalticks and totalevents */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 772 | totalticks = totalevents = 0; |
| 773 | for (i = 0; i < MAX_CSTATE_COUNT; i++) { |
| 774 | if (cur_usage[i] != 0) { |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 775 | totalticks += cur_duration[i] - G.last_duration[i]; |
| 776 | totalevents += cur_usage[i] - G.last_usage[i]; |
| 777 | } |
| 778 | } |
| 779 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 780 | /* Clear the screen */ |
| 781 | printf("\033[H\033[J"); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 782 | |
| 783 | /* Clear C-state lines */ |
| 784 | memset(&cstate_lines, 0, sizeof(cstate_lines)); |
| 785 | |
| 786 | if (totalevents == 0 && G.maxcstate <= 1) { |
| 787 | /* This should not happen */ |
Denys Vlasenko | a43e969 | 2010-10-26 13:03:31 +0200 | [diff] [blame] | 788 | strcpy(cstate_lines[0], "C-state information is not available\n"); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 789 | } else { |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 790 | double percentage; |
Denys Vlasenko | a43e969 | 2010-10-26 13:03:31 +0200 | [diff] [blame] | 791 | unsigned newticks; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 792 | |
| 793 | newticks = G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000 - totalticks; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 794 | /* Handle rounding errors: do not display negative values */ |
Denys Vlasenko | a43e969 | 2010-10-26 13:03:31 +0200 | [diff] [blame] | 795 | if ((int)newticks < 0) |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 796 | newticks = 0; |
| 797 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 798 | sprintf(cstate_lines[0], "Cn\t\t Avg residency\n"); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 799 | percentage = newticks * 100.0 / (G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 800 | sprintf(cstate_lines[1], "C0 (cpu running) (%4.1f%%)\n", percentage); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 801 | |
| 802 | /* Compute values for individual C-states */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 803 | for (i = 0; i < MAX_CSTATE_COUNT; i++) { |
| 804 | if (cur_usage[i] != 0) { |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 805 | double slept; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 806 | slept = (cur_duration[i] - G.last_duration[i]) |
| 807 | / (cur_usage[i] - G.last_usage[i] + 0.1) / FREQ_ACPI; |
| 808 | percentage = (cur_duration[i] - G.last_duration[i]) * 100 |
| 809 | / (G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000); |
Denys Vlasenko | a43e969 | 2010-10-26 13:03:31 +0200 | [diff] [blame] | 810 | sprintf(cstate_lines[i + 2], "C%u\t\t%5.1fms (%4.1f%%)\n", |
| 811 | i + 1, slept, percentage); |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 812 | //if (maxsleep < slept) |
| 813 | // maxsleep = slept; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 818 | for (i = 0; i < MAX_CSTATE_COUNT + 2; i++) |
| 819 | if (cstate_lines[i][0]) |
Denys Vlasenko | a43e969 | 2010-10-26 13:03:31 +0200 | [diff] [blame] | 820 | fputs(cstate_lines[i], stdout); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 821 | |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 822 | i = process_timer_stats(); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 823 | #if ENABLE_FEATURE_POWERTOP_PROCIRQ |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 824 | if (totalevents == 0) { |
| 825 | /* No C-state info available, use timerstats */ |
| 826 | totalevents = i * G.total_cpus + G.total_interrupt; |
| 827 | if (i < 0) |
| 828 | totalevents += G.interrupt_0 - i; |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 829 | } |
| 830 | #endif |
Denys Vlasenko | c3f1fa1 | 2010-10-26 12:39:36 +0200 | [diff] [blame] | 831 | /* Upstream powertop prints wakeups per sec per CPU, |
| 832 | * we print just raw wakeup counts. |
| 833 | */ |
| 834 | //TODO: show real seconds (think about manual refresh) |
| 835 | printf("\nWakeups-from-idle in %u seconds: %llu\n", |
| 836 | DEFAULT_SLEEP, |
| 837 | totalevents |
| 838 | ); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 839 | |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 840 | update_lines_cumulative_count(); |
| 841 | sort_lines(); |
| 842 | show_timerstats(); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 843 | fflush(stdout); |
| 844 | |
| 845 | /* Clear the stats */ |
| 846 | memset(cur_duration, 0, sizeof(cur_duration)); |
| 847 | memset(cur_usage, 0, sizeof(cur_usage)); |
| 848 | |
| 849 | /* Get new values */ |
Denys Vlasenko | 373789e | 2010-10-26 02:54:13 +0200 | [diff] [blame] | 850 | read_cstate_counts(cur_usage, cur_duration); |
Marek Polacek | b507cc3 | 2010-10-25 03:44:34 +0200 | [diff] [blame] | 851 | |
| 852 | /* Save them */ |
| 853 | memcpy(G.last_usage, cur_usage, sizeof(G.last_usage)); |
| 854 | memcpy(G.last_duration, cur_duration, sizeof(G.last_duration)); |
| 855 | } /* for (;;) */ |
| 856 | |
| 857 | bb_putchar('\n'); |
| 858 | |
| 859 | return EXIT_SUCCESS; |
| 860 | } |