Move several applets to different install dir:
[oweals/busybox.git] / procps / powertop.c
index 5e028f074d8cdf96dfcc18caf55b5bd6b258f82f..b4c45edbcdacc8860415bde1669e3f6be99460f4 100644 (file)
@@ -9,7 +9,7 @@
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
-//applet:IF_POWERTOP(APPLET(powertop, _BB_DIR_BIN, _BB_SUID_DROP))
+//applet:IF_POWERTOP(APPLET(powertop, BB_DIR_USR_SBIN, BB_SUID_DROP))
 
 //kbuild:lib-$(CONFIG_POWERTOP) += powertop.o
 
@@ -39,7 +39,7 @@
 
 /* Frequency of the ACPI timer */
 #define FREQ_ACPI          3579.545
-#define FREQ_ACPI_1000    3579545
+#define FREQ_ACPI_1000     3579545
 
 /* Max filename length of entry in /sys/devices subsystem */
 #define BIG_SYSNAME_LEN    16
@@ -115,8 +115,8 @@ static int write_str_to_file(const char *fname, const char *str)
 }
 
 /* Make it more readable */
-#define start_timer()  write_str_to_file("/proc/timer_stats", "1\n")
-#define stop_timer()   write_str_to_file("/proc/timer_stats", "0\n")
+#define start_timer() write_str_to_file("/proc/timer_stats", "1\n")
+#define stop_timer()  write_str_to_file("/proc/timer_stats", "0\n")
 
 static NOINLINE void clear_lines(void)
 {
@@ -169,7 +169,7 @@ static void read_cstate_counts(ullong *usage, ullong *duration)
                if (len < 3 || len > BIG_SYSNAME_LEN)
                        continue;
 
-               sprintf(buf, "/proc/acpi/processor/%s/power", d->d_name);
+               sprintf(buf, "%s/%s/power", "/proc/acpi/processor", d->d_name);
                fp = fopen_for_read(buf);
                if (!fp)
                        continue;
@@ -321,31 +321,18 @@ static void process_irq_counts(void)
                /*  0:  143646045  153901007   IO-APIC-edge      timer
                 *   ^
                 */
+               *p = '\0';
                /* Deal with non-maskable interrupts -- make up fake numbers */
-               nr = -1;
-               if (buf[0] != ' ' && !isdigit(buf[0])) {
-//TODO: optimize
-                       if (strncmp(buf, "NMI:", 4) == 0)
-                               nr = 20000;
-                       if (strncmp(buf, "RES:", 4) == 0)
-                               nr = 20001;
-                       if (strncmp(buf, "CAL:", 4) == 0)
-                               nr = 20002;
-                       if (strncmp(buf, "TLB:", 4) == 0)
-                               nr = 20003;
-                       if (strncmp(buf, "TRM:", 4) == 0)
-                               nr = 20004;
-                       if (strncmp(buf, "THR:", 4) == 0)
-                               nr = 20005;
-                       if (strncmp(buf, "SPU:", 4) == 0)
-                               nr = 20006;
+               nr = index_in_strings("NMI\0RES\0CAL\0TLB\0TRM\0THR\0SPU\0", buf);
+               if (nr >= 0) {
+                       nr += 20000;
                } else {
                        /* bb_strtou doesn't eat leading spaces, using strtoul */
+                       errno = 0;
                        nr = strtoul(buf, NULL, 10);
+                       if (errno)
+                               continue;
                }
-               if (nr == -1)
-                       continue;
-
                p++;
                /*  0:  143646045  153901007   IO-APIC-edge      timer
                 *    ^
@@ -362,7 +349,7 @@ static void process_irq_counts(void)
                }
                /*   0:  143646045  153901007   IO-APIC-edge      timer
                 * NMI:          1          2   Non-maskable interrupts
-                *                              ^
+                *                              ^
                 */
                if (nr < 20000) {
                        /* Skip to the interrupt name, e.g. 'timer' */
@@ -406,12 +393,11 @@ static NOINLINE int process_timer_stats(void)
        char buf[128];
        char line[15 + 3 + 128];
        int n;
-       ullong totalticks;
        FILE *fp;
 
        buf[0] = '\0';
-       totalticks = 0;
 
+       n = 0;
        fp = NULL;
        if (!G.cant_enable_timer_stats)
                fp = fopen_for_read("/proc/timer_stats");
@@ -430,33 +416,41 @@ static NOINLINE int process_timer_stats(void)
                while (fgets(buf, sizeof(buf), fp)) {
                        const char *count, *process, *func;
                        char *p;
-                       int cnt;
+                       int idx;
+                       unsigned cnt;
 
                        count = skip_whitespace(buf);
                        p = strchr(count, ',');
                        if (!p)
                                continue;
                        *p++ = '\0';
-                       if (strcmp(strchrnul(count, ' '), " total events") == 0)
+                       cnt = bb_strtou(count, NULL, 10);
+                       if (strcmp(skip_non_whitespace(count), " total events") == 0) {
+#if ENABLE_FEATURE_POWERTOP_PROCIRQ
+                               n = cnt / G.total_cpus;
+                               if (n > 0 && n < G.interrupt_0) {
+                                       sprintf(line, "    <interrupt> : %s", "extra timer interrupt");
+                                       save_line(line, G.interrupt_0 - n);
+                               }
+#endif
                                break;
-                       p = skip_whitespace(p); /* points to pid */
-
-/* Find char ' ', then eat remaining spaces */
-#define ADVANCE(p) do {           \
-       (p) = strchr((p), ' ');   \
-       if (!(p))                 \
-               continue;         \
-       *(p) = '\0';              \
-       (p)++;                    \
-       (p) = skip_whitespace(p); \
-} while (0)
-                       /* Get process name */
-                       ADVANCE(p);
-                       process = p;
-                       /* Get function */
-                       ADVANCE(p);
+                       }
+                       if (strchr(count, 'D'))
+                               continue; /* deferred */
+                       p = skip_whitespace(p); /* points to pid now */
+                       process = NULL;
+ get_func_name:
+                       p = strchr(p, ' ');
+                       if (!p)
+                               continue;
+                       *p++ = '\0';
+                       p = skip_whitespace(p);
+                       if (process == NULL) {
+                               process = p;
+                               goto get_func_name;
+                       }
                        func = p;
-#undef ADVANCE
+
                        //if (strcmp(process, "swapper") == 0
                        // && strcmp(func, "hrtimer_start_range_ns (tick_sched_timer)\n") == 0
                        //) {
@@ -471,45 +465,26 @@ static NOINLINE int process_timer_stats(void)
                        //if (strcmp(process, "powertop") == 0)
                        //      continue;
 
-                       if (strcmp(process, "insmod") == 0)
-                               process = "[kernel module]";
-                       if (strcmp(process, "modprobe") == 0)
-                               process = "[kernel module]";
-                       if (strcmp(process, "swapper") == 0)
-                               process = "<kernel core>";
+                       idx = index_in_strings("insmod\0modprobe\0swapper\0", process);
+                       if (idx != -1) {
+                               process = idx < 2 ? "[kernel module]" : "<kernel core>";
+                       }
 
                        strchrnul(p, '\n')[0] = '\0';
 
-                       {
-                               char *tmp;
-                               cnt = bb_strtoull(count, &tmp, 10);
-                               p = tmp;
-                       }
-                       while (*p != '\0') {
-                               if (*p++ == 'D') /* deferred */
-                                       goto skip;
-                       }
+                       // 46D\01136\0kondemand/1\0do_dbs_timer (delayed_work_timer_fn)
+                       // ^          ^            ^
+                       // count      process      func
 
                        //if (strchr(process, '['))
                                sprintf(line, "%15.15s : %s", process, func);
                        //else
                        //      sprintf(line, "%s", process);
                        save_line(line, cnt);
- skip: ;
                }
                fclose(fp);
        }
 
-       n = 0;
-#if ENABLE_FEATURE_POWERTOP_PROCIRQ
-       if (strstr(buf, "total events")) {
-               n = bb_strtoull(buf, NULL, 10) / G.total_cpus;
-               if (n > 0 && n < G.interrupt_0) {
-                       sprintf(line, "    <interrupt> : %s", "extra timer interrupt");
-                       save_line(line, G.interrupt_0 - n);
-               }
-       }
-#endif
        return n;
 }
 
@@ -539,9 +514,9 @@ static void cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx,
 }
 #endif
 
+#ifdef __i386__
 static NOINLINE void print_intel_cstates(void)
 {
-#ifdef __i386__
        int bios_table[8] = { 0 };
        int nbios = 0;
        DIR *cpudir;
@@ -566,7 +541,7 @@ static NOINLINE void print_intel_cstates(void)
                if (!isdigit(d->d_name[3]))
                        continue;
 
-               len = sprintf(fname, "/sys/devices/system/cpu/%s/cpuidle", d->d_name);
+               len = sprintf(fname, "%s/%s/cpuidle", "/sys/devices/system/cpu", d->d_name);
                dir = opendir(fname);
                if (!dir)
                        continue;
@@ -628,13 +603,15 @@ static NOINLINE void print_intel_cstates(void)
 
        /* Print BIOS C-States */
        printf("Your BIOS reports the following C-states: ");
-       for (i = 0; i < 8; i++)
+       for (i = 0; i < ARRAY_SIZE(bios_table); i++)
                if (bios_table[i])
                        printf("C%u ", i);
 
        bb_putchar('\n');
-#endif
 }
+#else
+# define print_intel_cstates() ((void)0)
+#endif
 
 static void show_timerstats(void)
 {
@@ -817,7 +794,7 @@ int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
 
                        sprintf(cstate_lines[0], "Cn\t\t  Avg residency\n");
                        percentage = newticks * 100.0 / (G.total_cpus * DEFAULT_SLEEP * FREQ_ACPI_1000);
-                       sprintf(cstate_lines[1], "C0 (cpu running)        (%4.1f%%)\n", percentage);
+                       sprintf(cstate_lines[1], "C0 (cpu running)        (%4.1f%%)\n", percentage);
 
                        /* Compute values for individual C-states */
                        for (i = 0; i < MAX_CSTATE_COUNT; i++) {