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 /libbb | |
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>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Kbuild.src | 3 | ||||
-rw-r--r-- | libbb/get_cpu_count.c | 47 |
2 files changed, 50 insertions, 0 deletions
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 | } | ||