diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-08-16 02:49:21 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-08-16 02:49:21 +0200 |
commit | c9b9750a0e2a85d3d045cc8d0217d4605f2d7989 (patch) | |
tree | 00168254b5f9e960ce42ea36d6d4a78bd269eae4 | |
parent | a4160e15ec866005f3ad30c967bc4829fbb1c8e3 (diff) | |
download | busybox-w32-c9b9750a0e2a85d3d045cc8d0217d4605f2d7989.tar.gz busybox-w32-c9b9750a0e2a85d3d045cc8d0217d4605f2d7989.tar.bz2 busybox-w32-c9b9750a0e2a85d3d045cc8d0217d4605f2d7989.zip |
libbb: factor out common code from mpstat/iostat
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/Kbuild.src | 3 | ||||
-rw-r--r-- | libbb/get_cpu_count.c | 47 | ||||
-rw-r--r-- | procps/iostat.c | 30 | ||||
-rw-r--r-- | procps/mpstat.c | 40 |
5 files changed, 56 insertions, 66 deletions
diff --git a/include/libbb.h b/include/libbb.h index 03fc5d44b..21e144144 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1479,6 +1479,8 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC; | |||
1479 | void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC; | 1479 | void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC; |
1480 | pid_t *find_pid_by_name(const char* procName) FAST_FUNC; | 1480 | pid_t *find_pid_by_name(const char* procName) FAST_FUNC; |
1481 | pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC; | 1481 | pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC; |
1482 | int starts_with_cpu(const char *str) FAST_FUNC; | ||
1483 | unsigned get_cpu_count(void) FAST_FUNC; | ||
1482 | 1484 | ||
1483 | 1485 | ||
1484 | extern const char bb_uuenc_tbl_base64[]; | 1486 | extern const char bb_uuenc_tbl_base64[]; |
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index 5c567000a..6081ebe4b 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src | |||
@@ -157,6 +157,9 @@ lib-$(CONFIG_MOUNT) += find_mount_point.o | |||
157 | lib-$(CONFIG_HWCLOCK) += rtc.o | 157 | lib-$(CONFIG_HWCLOCK) += rtc.o |
158 | lib-$(CONFIG_RTCWAKE) += rtc.o | 158 | lib-$(CONFIG_RTCWAKE) += rtc.o |
159 | 159 | ||
160 | lib-$(CONFIG_IOSTAT) += get_cpu_count.o | ||
161 | lib-$(CONFIG_MPSTAT) += get_cpu_count.o | ||
162 | |||
160 | # We shouldn't build xregcomp.c if we don't need it - this ensures we don't | 163 | # We shouldn't build xregcomp.c if we don't need it - this ensures we don't |
161 | # require regex.h to be in the include dir even if we don't need it thereby | 164 | # require regex.h to be in the include dir even if we don't need it thereby |
162 | # allowing us to build busybox even if uclibc regex support is disabled. | 165 | # allowing us to build busybox even if uclibc regex support is disabled. |
diff --git a/libbb/get_cpu_count.c b/libbb/get_cpu_count.c new file mode 100644 index 000000000..a0dcb45f5 --- /dev/null +++ b/libbb/get_cpu_count.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Factored out of mpstat/iostat. | ||
4 | * | ||
5 | * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com> | ||
6 | * | ||
7 | * Licensed under GPLv2, see file License in this tarball for details. | ||
8 | */ | ||
9 | #include "libbb.h" | ||
10 | |||
11 | /* Does str start with "cpu"? */ | ||
12 | int FAST_FUNC starts_with_cpu(const char *str) | ||
13 | { | ||
14 | return ((str[0] - 'c') | (str[1] - 'p') | (str[2] - 'u')) == 0; | ||
15 | } | ||
16 | |||
17 | /* | ||
18 | * Get number of processors. Uses /proc/stat. | ||
19 | * Return value 0 means one CPU and non SMP kernel. | ||
20 | * Otherwise N means N processor(s) and SMP kernel. | ||
21 | */ | ||
22 | unsigned FAST_FUNC get_cpu_count(void) | ||
23 | { | ||
24 | FILE *fp; | ||
25 | char line[256]; | ||
26 | int proc_nr = -1; | ||
27 | |||
28 | fp = xfopen_for_read("/proc/stat"); | ||
29 | while (fgets(line, sizeof(line), fp)) { | ||
30 | if (!starts_with_cpu(line)) { | ||
31 | if (proc_nr >= 0) | ||
32 | break; /* we are past "cpuN..." lines */ | ||
33 | continue; | ||
34 | } | ||
35 | if (line[3] != ' ') { /* "cpuN" */ | ||
36 | int num_proc; | ||
37 | if (sscanf(line + 3, "%u", &num_proc) == 1 | ||
38 | && num_proc > proc_nr | ||
39 | ) { | ||
40 | proc_nr = num_proc; | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | fclose(fp); | ||
46 | return proc_nr + 1; | ||
47 | } | ||
diff --git a/procps/iostat.c b/procps/iostat.c index 76c5353cc..e8e321b8d 100644 --- a/procps/iostat.c +++ b/procps/iostat.c | |||
@@ -114,26 +114,6 @@ static void print_header(void) | |||
114 | buf, uts.machine, G.total_cpus); | 114 | buf, uts.machine, G.total_cpus); |
115 | } | 115 | } |
116 | 116 | ||
117 | static int get_number_of_cpus(void) | ||
118 | { | ||
119 | #ifdef _SC_NPROCESSORS_CONF | ||
120 | return sysconf(_SC_NPROCESSORS_CONF); | ||
121 | #else | ||
122 | char buf[128]; | ||
123 | int n = 0; | ||
124 | FILE *fp; | ||
125 | |||
126 | fp = xfopen_for_read("/proc/cpuinfo"); | ||
127 | |||
128 | while (fgets(buf, sizeof(buf), fp)) | ||
129 | if (strncmp(buf, "processor\t:", 11) == 0) | ||
130 | n++; | ||
131 | |||
132 | fclose(fp); | ||
133 | return n; | ||
134 | #endif | ||
135 | } | ||
136 | |||
137 | static void get_localtime(struct tm *ptm) | 117 | static void get_localtime(struct tm *ptm) |
138 | { | 118 | { |
139 | time_t timer; | 119 | time_t timer; |
@@ -148,12 +128,6 @@ static void print_timestamp(void) | |||
148 | printf("%s\n", buf); | 128 | printf("%s\n", buf); |
149 | } | 129 | } |
150 | 130 | ||
151 | /* Does str start with "cpu"? */ | ||
152 | static int starts_with_cpu(const char *str) | ||
153 | { | ||
154 | return ((str[0] - 'c') | (str[1] - 'p') | (str[2] - 'u')) == 0; | ||
155 | } | ||
156 | |||
157 | /* Fetch CPU statistics from /proc/stat */ | 131 | /* Fetch CPU statistics from /proc/stat */ |
158 | static void get_cpu_statistics(struct stats_cpu *sc) | 132 | static void get_cpu_statistics(struct stats_cpu *sc) |
159 | { | 133 | { |
@@ -509,7 +483,9 @@ int iostat_main(int argc, char **argv) | |||
509 | G.clk_tck = get_user_hz(); | 483 | G.clk_tck = get_user_hz(); |
510 | 484 | ||
511 | /* Determine number of CPUs */ | 485 | /* Determine number of CPUs */ |
512 | G.total_cpus = get_number_of_cpus(); | 486 | G.total_cpus = get_cpu_count(); |
487 | if (G.total_cpus == 0) | ||
488 | G.total_cpus = 1; | ||
513 | 489 | ||
514 | /* Parse and process arguments */ | 490 | /* Parse and process arguments */ |
515 | /* -k and -m are mutually exclusive */ | 491 | /* -k and -m are mutually exclusive */ |
diff --git a/procps/mpstat.c b/procps/mpstat.c index 85cbb45db..b4520cb55 100644 --- a/procps/mpstat.c +++ b/procps/mpstat.c | |||
@@ -119,12 +119,6 @@ enum { | |||
119 | }; | 119 | }; |
120 | 120 | ||
121 | 121 | ||
122 | /* Does str start with "cpu"? */ | ||
123 | static int starts_with_cpu(const char *str) | ||
124 | { | ||
125 | return !((str[0] - 'c') | (str[1] - 'p') | (str[2] - 'u')); | ||
126 | } | ||
127 | |||
128 | /* Is option on? */ | 122 | /* Is option on? */ |
129 | static ALWAYS_INLINE int display_opt(int opt) | 123 | static ALWAYS_INLINE int display_opt(int opt) |
130 | { | 124 | { |
@@ -816,38 +810,6 @@ static void print_header(struct tm *t) | |||
816 | } | 810 | } |
817 | 811 | ||
818 | /* | 812 | /* |
819 | * Get number of processors in /proc/stat | ||
820 | * Return value '0' means one CPU and non SMP kernel. | ||
821 | * Otherwise N means N processor(s) and SMP kernel. | ||
822 | */ | ||
823 | static int get_cpu_nr(void) | ||
824 | { | ||
825 | FILE *fp; | ||
826 | char line[256]; | ||
827 | int proc_nr = -1; | ||
828 | |||
829 | fp = xfopen_for_read(PROCFS_STAT); | ||
830 | while (fgets(line, sizeof(line), fp)) { | ||
831 | if (!starts_with_cpu(line)) { | ||
832 | if (proc_nr >= 0) | ||
833 | break; /* we are past "cpuN..." lines */ | ||
834 | continue; | ||
835 | } | ||
836 | if (line[3] != ' ') { /* "cpuN" */ | ||
837 | int num_proc; | ||
838 | if (sscanf(line + 3, "%u", &num_proc) == 1 | ||
839 | && num_proc > proc_nr | ||
840 | ) { | ||
841 | proc_nr = num_proc; | ||
842 | } | ||
843 | } | ||
844 | } | ||
845 | |||
846 | fclose(fp); | ||
847 | return proc_nr + 1; | ||
848 | } | ||
849 | |||
850 | /* | ||
851 | * Get number of interrupts available per processor | 813 | * Get number of interrupts available per processor |
852 | */ | 814 | */ |
853 | static int get_irqcpu_nr(const char *f, int max_irqs) | 815 | static int get_irqcpu_nr(const char *f, int max_irqs) |
@@ -910,7 +872,7 @@ int mpstat_main(int UNUSED_PARAM argc, char **argv) | |||
910 | G.interval = -1; | 872 | G.interval = -1; |
911 | 873 | ||
912 | /* Get number of processors */ | 874 | /* Get number of processors */ |
913 | G.cpu_nr = get_cpu_nr(); | 875 | G.cpu_nr = get_cpu_count(); |
914 | 876 | ||
915 | /* Get number of clock ticks per sec */ | 877 | /* Get number of clock ticks per sec */ |
916 | G.hz = get_hz(); | 878 | G.hz = get_hz(); |