diff options
author | Denys Vlasenko | 2010-08-16 02:49:21 +0200 |
---|---|---|
committer | Denys Vlasenko | 2010-08-16 02:49:21 +0200 |
commit | c9b9750a0e2a85d3d045cc8d0217d4605f2d7989 (patch) | |
tree | 00168254b5f9e960ce42ea36d6d4a78bd269eae4 /procps | |
parent | a4160e15ec866005f3ad30c967bc4829fbb1c8e3 (diff) | |
download | busybox-c9b9750a0e2a85d3d045cc8d0217d4605f2d7989.zip busybox-c9b9750a0e2a85d3d045cc8d0217d4605f2d7989.tar.gz |
libbb: factor out common code from mpstat/iostat
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'procps')
-rw-r--r-- | procps/iostat.c | 30 | ||||
-rw-r--r-- | procps/mpstat.c | 40 |
2 files changed, 4 insertions, 66 deletions
diff --git a/procps/iostat.c b/procps/iostat.c index 76c5353..e8e321b 100644 --- a/procps/iostat.c +++ b/procps/iostat.c @@ -114,26 +114,6 @@ static void print_header(void) buf, uts.machine, G.total_cpus); } -static int get_number_of_cpus(void) -{ -#ifdef _SC_NPROCESSORS_CONF - return sysconf(_SC_NPROCESSORS_CONF); -#else - char buf[128]; - int n = 0; - FILE *fp; - - fp = xfopen_for_read("/proc/cpuinfo"); - - while (fgets(buf, sizeof(buf), fp)) - if (strncmp(buf, "processor\t:", 11) == 0) - n++; - - fclose(fp); - return n; -#endif -} - static void get_localtime(struct tm *ptm) { time_t timer; @@ -148,12 +128,6 @@ static void print_timestamp(void) printf("%s\n", buf); } -/* Does str start with "cpu"? */ -static int starts_with_cpu(const char *str) -{ - return ((str[0] - 'c') | (str[1] - 'p') | (str[2] - 'u')) == 0; -} - /* Fetch CPU statistics from /proc/stat */ static void get_cpu_statistics(struct stats_cpu *sc) { @@ -509,7 +483,9 @@ int iostat_main(int argc, char **argv) G.clk_tck = get_user_hz(); /* Determine number of CPUs */ - G.total_cpus = get_number_of_cpus(); + G.total_cpus = get_cpu_count(); + if (G.total_cpus == 0) + G.total_cpus = 1; /* Parse and process arguments */ /* -k and -m are mutually exclusive */ diff --git a/procps/mpstat.c b/procps/mpstat.c index 85cbb45..b4520cb 100644 --- a/procps/mpstat.c +++ b/procps/mpstat.c @@ -119,12 +119,6 @@ enum { }; -/* Does str start with "cpu"? */ -static int starts_with_cpu(const char *str) -{ - return !((str[0] - 'c') | (str[1] - 'p') | (str[2] - 'u')); -} - /* Is option on? */ static ALWAYS_INLINE int display_opt(int opt) { @@ -816,38 +810,6 @@ static void print_header(struct tm *t) } /* - * Get number of processors in /proc/stat - * Return value '0' means one CPU and non SMP kernel. - * Otherwise N means N processor(s) and SMP kernel. - */ -static int get_cpu_nr(void) -{ - FILE *fp; - char line[256]; - int proc_nr = -1; - - fp = xfopen_for_read(PROCFS_STAT); - while (fgets(line, sizeof(line), fp)) { - if (!starts_with_cpu(line)) { - if (proc_nr >= 0) - break; /* we are past "cpuN..." lines */ - continue; - } - if (line[3] != ' ') { /* "cpuN" */ - int num_proc; - if (sscanf(line + 3, "%u", &num_proc) == 1 - && num_proc > proc_nr - ) { - proc_nr = num_proc; - } - } - } - - fclose(fp); - return proc_nr + 1; -} - -/* * Get number of interrupts available per processor */ static int get_irqcpu_nr(const char *f, int max_irqs) @@ -910,7 +872,7 @@ int mpstat_main(int UNUSED_PARAM argc, char **argv) G.interval = -1; /* Get number of processors */ - G.cpu_nr = get_cpu_nr(); + G.cpu_nr = get_cpu_count(); /* Get number of clock ticks per sec */ G.hz = get_hz(); |