1 /* vi: set sw=4 ts=4: */
3 * A mini 'powertop' utility:
4 * Analyze power consumption on Intel-based laptops.
5 * Based on powertop 1.11.
7 * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com>
9 * Licensed under GPLv2, see file LICENSE in this source tree.
12 //applet:IF_POWERTOP(APPLET(powertop, BB_DIR_USR_SBIN, BB_SUID_DROP))
14 //kbuild:lib-$(CONFIG_POWERTOP) += powertop.o
16 //config:config POWERTOP
17 //config: bool "powertop"
20 //config: Analyze power consumption on Intel-based laptops
22 // XXX This should be configurable
23 #define ENABLE_FEATURE_POWERTOP_PROCIRQ 1
28 //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
29 #define debug(fmt, ...) ((void)0)
32 #define BLOATY_HPET_IRQ_NUM_DETECTION 0
33 #define MAX_CSTATE_COUNT 8
37 #define DEFAULT_SLEEP 10
38 #define DEFAULT_SLEEP_STR "10"
40 /* Frequency of the ACPI timer */
41 #define FREQ_ACPI 3579.545
42 #define FREQ_ACPI_1000 3579545
44 /* Max filename length of entry in /sys/devices subsystem */
45 #define BIG_SYSNAME_LEN 16
47 typedef unsigned long long ullong;
55 #if ENABLE_FEATURE_POWERTOP_PROCIRQ
65 struct line *lines; /* the most often used member */
67 int lines_cumulative_count;
70 smallint cant_enable_timer_stats;
71 #if ENABLE_FEATURE_POWERTOP_PROCIRQ
72 # if BLOATY_HPET_IRQ_NUM_DETECTION
73 smallint scanned_timer_list;
74 int percpu_hpet_start;
79 struct irqdata interrupts[IRQCOUNT];
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];
85 #if ENABLE_FEATURE_USE_TERMIOS
86 struct termios init_settings;
89 #define G (*ptr_to_globals)
90 #define INIT_G() do { \
91 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
94 #if ENABLE_FEATURE_USE_TERMIOS
95 static void reset_term(void)
97 tcsetattr_stdin_TCSANOW(&G.init_settings);
100 static void sig_handler(int signo UNUSED_PARAM)
107 static int write_str_to_file(const char *fname, const char *str)
109 FILE *fp = fopen_for_write(fname);
117 /* Make it more readable */
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")
121 static NOINLINE void clear_lines(void)
125 for (i = 0; i < G.lines_cnt; i++)
126 free(G.lines[i].string);
133 static void update_lines_cumulative_count(void)
136 for (i = 0; i < G.lines_cnt; i++)
137 G.lines_cumulative_count += G.lines[i].count;
140 static int line_compare(const void *p1, const void *p2)
142 const struct line *a = p1;
143 const struct line *b = p2;
144 return (b->count /*+ 50 * b->disk_count*/) - (a->count /*+ 50 * a->disk_count*/);
147 static void sort_lines(void)
149 qsort(G.lines, G.lines_cnt, sizeof(G.lines[0]), line_compare);
152 /* Save C-state usage and duration. Also update maxcstate. */
153 static void read_cstate_counts(ullong *usage, ullong *duration)
158 dir = opendir("/proc/acpi/processor");
162 while ((d = readdir(dir)) != NULL) {
168 len = strlen(d->d_name); /* "CPUnn" */
169 if (len < 3 || len > BIG_SYSNAME_LEN)
172 sprintf(buf, "%s/%s/power", "/proc/acpi/processor", d->d_name);
173 fp = fopen_for_read(buf);
177 // Example file contents:
180 // maximum allowed latency: 2000000000 usec
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]
186 while (fgets(buf, sizeof(buf), fp)) {
187 char *p = strstr(buf, "age[");
191 usage[level] += bb_strtoull(p, NULL, 10) + 1;
192 p = strstr(buf, "ation[");
196 duration[level] += bb_strtoull(p, NULL, 10);
198 if (level >= MAX_CSTATE_COUNT-1)
201 if (level > G.maxcstate) /* update maxcstate */
209 /* Add line and/or update count */
210 static void save_line(const char *string, int count)
213 for (i = 0; i < G.lines_cnt; i++) {
214 if (strcmp(string, G.lines[i].string) == 0) {
215 /* It's already there, only update count */
216 G.lines[i].count += count;
222 G.lines = xrealloc_vector(G.lines, 4, G.lines_cnt);
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;*/
229 #if ENABLE_FEATURE_POWERTOP_PROCIRQ
230 static int is_hpet_irq(const char *name)
233 # if BLOATY_HPET_IRQ_NUM_DETECTION
236 /* Learn the range of existing hpet timers. This is done once */
237 if (!G.scanned_timer_list) {
241 G.scanned_timer_list = true;
242 fp = fopen_for_read("/proc/timer_list");
246 while (fgets(buf, sizeof(buf), fp)) {
247 p = strstr(buf, "Clock Event Device: hpet");
250 p += sizeof("Clock Event Device: hpet")-1;
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;
263 p = strstr(name, "hpet");
269 # if BLOATY_HPET_IRQ_NUM_DETECTION
270 hpet_chan = xatoi_positive(p);
271 if (hpet_chan < G.percpu_hpet_start || hpet_chan > G.percpu_hpet_end)
277 /* Save new IRQ count, return delta from old one */
278 static int save_irq_count(int irq, ullong count)
280 int unused = IRQCOUNT;
282 for (i = 0; i < IRQCOUNT; i++) {
283 if (G.interrupts[i].active && G.interrupts[i].number == irq) {
284 ullong old = G.interrupts[i].count;
285 G.interrupts[i].count = count;
288 if (!G.interrupts[i].active && unused > i)
291 if (unused < IRQCOUNT) {
292 G.interrupts[unused].active = 1;
293 G.interrupts[unused].count = count;
294 G.interrupts[unused].number = irq;
299 /* Read /proc/interrupts, save IRQ counts and IRQ description */
300 static void process_irq_counts(void)
307 G.total_interrupt = 0;
309 fp = xfopen_for_read("/proc/interrupts");
310 while (fgets(buf, sizeof(buf), fp)) {
311 char irq_desc[sizeof(" <kernel IPI> : ") + sizeof(buf)];
318 p = strchr(buf, ':');
321 /* 0: 143646045 153901007 IO-APIC-edge timer
325 /* Deal with non-maskable interrupts -- make up fake numbers */
326 nr = index_in_strings("NMI\0RES\0CAL\0TLB\0TRM\0THR\0SPU\0", buf);
330 /* bb_strtou doesn't eat leading spaces, using strtoul */
332 nr = strtoul(buf, NULL, 10);
337 /* 0: 143646045 153901007 IO-APIC-edge timer
340 /* Sum counts for this IRQ */
344 p = skip_whitespace(p);
347 count += bb_strtoull(p, &tmp, 10);
350 /* 0: 143646045 153901007 IO-APIC-edge timer
351 * NMI: 1 2 Non-maskable interrupts
355 /* Skip to the interrupt name, e.g. 'timer' */
359 p = skip_whitespace(p);
364 /* Save description of the interrupt */
366 sprintf(irq_desc, " <kernel IPI> : %s", name);
368 sprintf(irq_desc, " <interrupt> : %s", name);
370 delta = save_irq_count(nr, count);
372 /* Skip per CPU timer interrupts */
373 if (is_hpet_irq(name))
376 if (nr != 0 && delta != 0)
377 save_line(irq_desc, delta);
380 G.interrupt_0 = delta;
382 G.total_interrupt += delta;
387 #else /* !ENABLE_FEATURE_POWERTOP_PROCIRQ */
388 # define process_irq_counts() ((void)0)
391 static NOINLINE int process_timer_stats(void)
394 char line[15 + 3 + 128];
402 if (!G.cant_enable_timer_stats)
403 fp = fopen_for_read("/proc/timer_stats");
405 // Example file contents:
406 // Timer Stats Version: v0.2
407 // Sample period: 1.329 s
408 // 76, 0 swapper hrtimer_start_range_ns (tick_sched_timer)
409 // 88, 0 swapper hrtimer_start_range_ns (tick_sched_timer)
410 // 24, 3787 firefox hrtimer_start_range_ns (hrtimer_wakeup)
411 // 46D, 1136 kondemand/1 do_dbs_timer (delayed_work_timer_fn)
413 // 1, 1656 Xorg hrtimer_start_range_ns (hrtimer_wakeup)
414 // 1, 2159 udisks-daemon hrtimer_start_range_ns (hrtimer_wakeup)
415 // 331 total events, 249.059 events/sec
416 while (fgets(buf, sizeof(buf), fp)) {
417 const char *count, *process, *func;
422 count = skip_whitespace(buf);
423 p = strchr(count, ',');
427 cnt = bb_strtou(count, NULL, 10);
428 if (strcmp(skip_non_whitespace(count), " total events") == 0) {
429 #if ENABLE_FEATURE_POWERTOP_PROCIRQ
430 n = cnt / G.total_cpus;
431 if (n > 0 && n < G.interrupt_0) {
432 sprintf(line, " <interrupt> : %s", "extra timer interrupt");
433 save_line(line, G.interrupt_0 - n);
438 if (strchr(count, 'D'))
439 continue; /* deferred */
440 p = skip_whitespace(p); /* points to pid now */
447 p = skip_whitespace(p);
448 if (process == NULL) {
454 //if (strcmp(process, "swapper") == 0
455 // && strcmp(func, "hrtimer_start_range_ns (tick_sched_timer)\n") == 0
457 // process = "[kernel scheduler]";
458 // func = "Load balancing tick";
461 if (is_prefixed_with(func, "tick_nohz_"))
463 if (is_prefixed_with(func, "tick_setup_sched_timer"))
465 //if (strcmp(process, "powertop") == 0)
468 idx = index_in_strings("insmod\0modprobe\0swapper\0", process);
470 process = idx < 2 ? "[kernel module]" : "<kernel core>";
475 // 46D\01136\0kondemand/1\0do_dbs_timer (delayed_work_timer_fn)
477 // count process func
479 //if (strchr(process, '['))
480 sprintf(line, "%15.15s : %s", process, func);
482 // sprintf(line, "%s", process);
483 save_line(line, cnt);
493 * Get information about CPU using CPUID opcode.
495 static void cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx,
498 /* EAX value specifies what information to return */
500 " pushl %%ebx\n" /* Save EBX */
502 " movl %%ebx, %1\n" /* Save content of EBX */
503 " popl %%ebx\n" /* Restore EBX */
504 : "=a"(*eax), /* Output */
508 : "0"(*eax), /* Input */
512 /* No clobbered registers */
518 static NOINLINE void print_intel_cstates(void)
520 int bios_table[8] = { 0 };
525 unsigned eax, ebx, ecx, edx;
527 cpudir = opendir("/sys/devices/system/cpu");
531 /* Loop over cpuN entries */
532 while ((d = readdir(cpudir)) != NULL) {
535 char fname[sizeof("/sys/devices/system/cpu//cpuidle//desc") + 2*BIG_SYSNAME_LEN];
537 len = strlen(d->d_name);
538 if (len < 3 || len > BIG_SYSNAME_LEN)
541 if (!isdigit(d->d_name[3]))
544 len = sprintf(fname, "%s/%s/cpuidle", "/sys/devices/system/cpu", d->d_name);
545 dir = opendir(fname);
550 * Every C-state has its own stateN directory, that
551 * contains a 'time' and a 'usage' file.
553 while ((d = readdir(dir)) != NULL) {
558 n = strlen(d->d_name);
559 if (n < 3 || n > BIG_SYSNAME_LEN)
562 sprintf(fname + len, "/%s/desc", d->d_name);
563 fp = fopen_for_read(fname);
565 char *p = fgets(buf, sizeof(buf), fp);
569 p = strstr(p, "MWAIT ");
572 p += sizeof("MWAIT ") - 1;
573 pos = (bb_strtoull(p, NULL, 16) >> 4) + 1;
574 if (pos >= ARRAY_SIZE(bios_table))
590 cpuid(&eax, &ebx, &ecx, &edx);
591 if (!edx || !(ecx & 1))
594 printf("Your CPU supports the following C-states: ");
604 /* Print BIOS C-States */
605 printf("Your BIOS reports the following C-states: ");
606 for (i = 0; i < ARRAY_SIZE(bios_table); i++)
613 # define print_intel_cstates() ((void)0)
616 static void show_timerstats(void)
620 /* Get terminal height */
621 get_terminal_width_height(STDOUT_FILENO, NULL, &lines);
623 /* We don't have whole terminal just for timerstats */
626 if (!G.cant_enable_timer_stats) {
630 puts("\nTop causes for wakeups:");
631 for (i = 0; i < G.lines_cnt; i++) {
632 if ((G.lines[i].count > 0 /*|| G.lines[i].disk_count > 0*/)
635 /* NB: upstream powertop prints "(wakeups/sec)",
636 * we print just "(wakeup counts)".
639 if (G.lines[i].disk_count)
641 smart_ulltoa5(G.lines[i].count, strbuf6, " KMGTPEZY")[0] = '\0';
642 printf(/*" %5.1f%% (%s)%c %s\n"*/
643 " %5.1f%% (%s) %s\n",
644 G.lines[i].count * 100.0 / G.lines_cumulative_count,
651 bb_error_msg("no stats available; run as root or"
652 " enable the timer_stats module");
656 // Example display from powertop version 1.11
657 // Cn Avg residency P-states (frequencies)
658 // C0 (cpu running) ( 0.5%) 2.00 Ghz 0.0%
659 // polling 0.0ms ( 0.0%) 1.67 Ghz 0.0%
660 // C1 mwait 0.0ms ( 0.0%) 1333 Mhz 0.1%
661 // C2 mwait 0.1ms ( 0.1%) 1000 Mhz 99.9%
662 // C3 mwait 12.1ms (99.4%)
664 // Wakeups-from-idle per second : 93.6 interval: 15.0s
665 // no ACPI power usage estimate available
667 // Top causes for wakeups:
668 // 32.4% ( 26.7) <interrupt> : extra timer interrupt
669 // 29.0% ( 23.9) <kernel core> : hrtimer_start_range_ns (tick_sched_timer)
670 // 9.0% ( 7.5) <kernel core> : hrtimer_start (tick_sched_timer)
671 // 6.5% ( 5.3) <interrupt> : ata_piix
672 // 5.0% ( 4.1) inetd : hrtimer_start_range_ns (hrtimer_wakeup)
674 //usage:#define powertop_trivial_usage
676 //usage:#define powertop_full_usage "\n\n"
677 //usage: "Analyze power consumption on Intel-based laptops"
679 int powertop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
680 int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
682 ullong cur_usage[MAX_CSTATE_COUNT];
683 ullong cur_duration[MAX_CSTATE_COUNT];
684 char cstate_lines[MAX_CSTATE_COUNT + 2][64];
685 #if ENABLE_FEATURE_USE_TERMIOS
686 struct termios new_settings;
687 struct pollfd pfd[1];
690 pfd[0].events = POLLIN;
695 #if ENABLE_FEATURE_POWERTOP_PROCIRQ && BLOATY_HPET_IRQ_NUM_DETECTION
696 G.percpu_hpet_start = INT_MAX;
697 G.percpu_hpet_end = INT_MIN;
700 /* Print warning when we don't have superuser privileges */
702 bb_error_msg("run as root to collect enough information");
704 /* Get number of CPUs */
705 G.total_cpus = get_cpu_count();
707 puts("Collecting data for "DEFAULT_SLEEP_STR" seconds");
709 #if ENABLE_FEATURE_USE_TERMIOS
710 tcgetattr(0, (void *)&G.init_settings);
711 memcpy(&new_settings, &G.init_settings, sizeof(new_settings));
712 /* Turn on unbuffered input, turn off echoing */
713 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
714 /* So we don't forget to reset term settings */
716 bb_signals(BB_FATAL_SIGS, sig_handler);
717 tcsetattr_stdin_TCSANOW(&new_settings);
720 /* Collect initial data */
721 process_irq_counts();
723 /* Read initial usage and duration */
724 read_cstate_counts(G.start_usage, G.start_duration);
726 /* Copy them to "last" */
727 memcpy(G.last_usage, G.start_usage, sizeof(G.last_usage));
728 memcpy(G.last_duration, G.start_duration, sizeof(G.last_duration));
730 /* Display C-states */
731 print_intel_cstates();
733 G.cant_enable_timer_stats |= stop_timer(); /* 1 on error */
737 //double maxsleep = 0.0;
738 ullong totalticks, totalevents;
741 G.cant_enable_timer_stats |= start_timer(); /* 1 on error */
742 #if !ENABLE_FEATURE_USE_TERMIOS
743 sleep(DEFAULT_SLEEP);
745 if (safe_poll(pfd, 1, DEFAULT_SLEEP * 1000) > 0) {
747 if (safe_read(STDIN_FILENO, &c, 1) != 1)
748 break; /* EOF/error */
749 if (c == G.init_settings.c_cc[VINTR])
751 if ((c | 0x20) == 'q')
755 G.cant_enable_timer_stats |= stop_timer(); /* 1 on error */
758 process_irq_counts();
760 /* Clear the stats */
761 memset(cur_duration, 0, sizeof(cur_duration));
762 memset(cur_usage, 0, sizeof(cur_usage));
765 read_cstate_counts(cur_usage, cur_duration);
767 /* Count totalticks and totalevents */
768 totalticks = totalevents = 0;
769 for (i = 0; i < MAX_CSTATE_COUNT; i++) {
770 if (cur_usage[i] != 0) {
771 totalticks += cur_duration[i] - G.last_duration[i];
772 totalevents += cur_usage[i] - G.last_usage[i];
776 /* Clear the screen */
777 printf("\033[H\033[J");
779 /* Clear C-state lines */
780 memset(&cstate_lines, 0, sizeof(cstate_lines));
782 if (totalevents == 0 && G.maxcstate <= 1) {
783 /* This should not happen */
784 strcpy(cstate_lines[0], "C-state information is not available\n");
789 newticks = G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000 - totalticks;
790 /* Handle rounding errors: do not display negative values */
791 if ((int)newticks < 0)
794 sprintf(cstate_lines[0], "Cn\t\t Avg residency\n");
795 percentage = newticks * 100.0 / (G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000);
796 sprintf(cstate_lines[1], "C0 (cpu running) (%4.1f%%)\n", percentage);
798 /* Compute values for individual C-states */
799 for (i = 0; i < MAX_CSTATE_COUNT; i++) {
800 if (cur_usage[i] != 0) {
802 slept = (cur_duration[i] - G.last_duration[i])
803 / (cur_usage[i] - G.last_usage[i] + 0.1) / FREQ_ACPI;
804 percentage = (cur_duration[i] - G.last_duration[i]) * 100
805 / (G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000);
806 sprintf(cstate_lines[i + 2], "C%u\t\t%5.1fms (%4.1f%%)\n",
807 i + 1, slept, percentage);
808 //if (maxsleep < slept)
814 for (i = 0; i < MAX_CSTATE_COUNT + 2; i++)
815 if (cstate_lines[i][0])
816 fputs(cstate_lines[i], stdout);
818 i = process_timer_stats();
819 #if ENABLE_FEATURE_POWERTOP_PROCIRQ
820 if (totalevents == 0) {
821 /* No C-state info available, use timerstats */
822 totalevents = i * G.total_cpus + G.total_interrupt;
824 totalevents += G.interrupt_0 - i;
827 /* Upstream powertop prints wakeups per sec per CPU,
828 * we print just raw wakeup counts.
830 //TODO: show real seconds (think about manual refresh)
831 printf("\nWakeups-from-idle in %u seconds: %llu\n",
836 update_lines_cumulative_count();
841 /* Clear the stats */
842 memset(cur_duration, 0, sizeof(cur_duration));
843 memset(cur_usage, 0, sizeof(cur_usage));
846 read_cstate_counts(cur_usage, cur_duration);
849 memcpy(G.last_usage, cur_usage, sizeof(G.last_usage));
850 memcpy(G.last_duration, cur_duration, sizeof(G.last_duration));