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