udhcpc: code shrink
[oweals/busybox.git] / procps / mpstat.c
index f1a0b00b8f2bc9f833064d62424ad510f7994b5c..ed678f4562b58ebb16380ef1e27f039be5e6cada 100644 (file)
@@ -6,19 +6,19 @@
  *
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
-
-//applet:IF_MPSTAT(APPLET(mpstat, _BB_DIR_BIN, _BB_SUID_DROP))
-
-//kbuild:lib-$(CONFIG_MPSTAT) += mpstat.o
-
 //config:config MPSTAT
-//config:      bool "mpstat"
+//config:      bool "mpstat (10 kb)"
 //config:      default y
 //config:      help
-//config:        Per-processor statistics
+//config:      Per-processor statistics
+
+//applet:IF_MPSTAT(APPLET(mpstat, BB_DIR_BIN, BB_SUID_DROP))
+/* shouldn't be noexec: "mpstat INTERVAL" runs indefinitely */
+
+//kbuild:lib-$(CONFIG_MPSTAT) += mpstat.o
 
 #include "libbb.h"
-#include <sys/utsname.h>       /* struct utsname */
+#include <sys/utsname.h>  /* struct utsname */
 
 //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
 #define debug(fmt, ...) ((void)0)
  * We are printing headers in the " IRQNAME/s" form, experimentally
  * anything smaller than 10 chars looks ugly for /proc/softirqs stats.
  */
-#define INTRATE_SCRWIDTH   10
+#define INTRATE_SCRWIDTH      10
 #define INTRATE_SCRWIDTH_STR "10"
 
 /* System files */
-#define SYSFS_DEVCPU      "/sys/devices/system/cpu"
 #define PROCFS_STAT       "/proc/stat"
 #define PROCFS_INTERRUPTS "/proc/interrupts"
 #define PROCFS_SOFTIRQS   "/proc/softirqs"
@@ -523,13 +522,11 @@ static void get_irqs_from_stat(struct stats_irq *irq)
        FILE *fp;
        char buf[1024];
 
-       fp = fopen_for_read(PROCFS_STAT);
-       if (!fp)
-               return;
+       fp = xfopen_for_read(PROCFS_STAT);
 
        while (fgets(buf, sizeof(buf), fp)) {
                //bb_error_msg("/proc/stat:'%s'", buf);
-               if (strncmp(buf, "intr ", 5) == 0) {
+               if (is_prefixed_with(buf, "intr ")) {
                        /* Read total number of IRQs since system boot */
                        sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr);
                }
@@ -634,7 +631,7 @@ static void get_irqs_from_interrupts(const char *fname,
        while (irq < irqs_per_cpu) {
                /* Number of interrupts per CPU has changed */
                ic = &per_cpu_stats[current][irq];
-               ic->irq_name[0] = '\0'; /* False interrupt */
+               ic->irq_name[0] = '\0'; /* False interrupt */
                irq++;
        }
 }
@@ -645,9 +642,7 @@ static void get_uptime(data_t *uptime)
        char buf[sizeof(long)*3 * 2 + 4]; /* enough for long.long */
        unsigned long uptime_sec, decimal;
 
-       fp = fopen_for_read(PROCFS_UPTIME);
-       if (!fp)
-               return;
+       fp = xfopen_for_read(PROCFS_UPTIME);
        if (fgets(buf, sizeof(buf), fp)) {
                if (sscanf(buf, "%lu.%lu", &uptime_sec, &decimal) == 2) {
                        *uptime = (data_t)uptime_sec * G.hz + decimal * G.hz / 100;
@@ -776,12 +771,6 @@ static void main_loop(void)
 
 /* Initialization */
 
-/* Get number of clock ticks per sec */
-static ALWAYS_INLINE unsigned get_hz(void)
-{
-       return sysconf(_SC_CLK_TCK);
-}
-
 static void alloc_struct(int cpus)
 {
        int i;
@@ -820,7 +809,7 @@ static int get_irqcpu_nr(const char *f, int max_irqs)
        unsigned irq;
 
        fp = fopen_for_read(f);
-       if (!fp)                /* No interrupts file */
+       if (!fp)  /* No interrupts file */
                return 0;
 
        linelen = INTERRUPTS_LINE + 16 * G.cpu_nr;
@@ -845,23 +834,22 @@ static int get_irqcpu_nr(const char *f, int max_irqs)
 //usage:       "[-A] [-I SUM|CPU|ALL|SCPU] [-u] [-P num|ALL] [INTERVAL [COUNT]]"
 //usage:#define mpstat_full_usage "\n\n"
 //usage:       "Per-processor statistics\n"
-//usage:     "\nOptions:"
 //usage:     "\n       -A                      Same as -I ALL -u -P ALL"
 //usage:     "\n       -I SUM|CPU|ALL|SCPU     Report interrupt statistics"
 //usage:     "\n       -P num|ALL              Processor to monitor"
 //usage:     "\n       -u                      Report CPU utilization"
 
 int mpstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int mpstat_main(int UNUSED_PARAM argc, char **argv)
+int mpstat_main(int argc UNUSED_PARAM, char **argv)
 {
        char *opt_irq_fmt;
        char *opt_set_cpu;
        int i, opt;
        enum {
-               OPT_ALL    = 1 << 0,    /* -A */
-               OPT_INTS   = 1 << 1,    /* -I */
-               OPT_SETCPU = 1 << 2,    /* -P */
-               OPT_UTIL   = 1 << 3,    /* -u */
+               OPT_ALL    = 1 << 0, /* -A */
+               OPT_INTS   = 1 << 1, /* -I */
+               OPT_SETCPU = 1 << 2, /* -P */
+               OPT_UTIL   = 1 << 3, /* -u */
        };
 
        /* Dont buffer data if redirected to a pipe */
@@ -875,7 +863,7 @@ int mpstat_main(int UNUSED_PARAM argc, char **argv)
        G.cpu_nr = get_cpu_count();
 
        /* Get number of clock ticks per sec */
-       G.hz = get_hz();
+       G.hz = bb_clk_tck();
 
        /* Calculate number of interrupts per processor */
        G.irqcpu_nr = get_irqcpu_nr(PROCFS_INTERRUPTS, NR_IRQS) + NR_IRQCPU_PREALLOC;