diff options
author | haroon maqsood <maqsood3525@live.com> | 2018-07-06 16:17:57 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-07-06 16:19:04 +0200 |
commit | 4a85d56c4cda90d0c175f29eb320375d14ead507 (patch) | |
tree | fe8054ff79285c09ddd7dca3f30aa952f1a3e8fa | |
parent | 6f09785b7e4a6cc7a9ede623d51aa8f6dc5e9a03 (diff) | |
download | busybox-w32-4a85d56c4cda90d0c175f29eb320375d14ead507.tar.gz busybox-w32-4a85d56c4cda90d0c175f29eb320375d14ead507.tar.bz2 busybox-w32-4a85d56c4cda90d0c175f29eb320375d14ead507.zip |
nproc: implement --all --ignore=N
function old new delta
nproc_main 98 242 +144
packed_usage 32799 32816 +17
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 161/0) Total: 161 bytes
Signed-off-by: haroon maqsood <maqsood3525@live.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/nproc.c | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/coreutils/nproc.c b/coreutils/nproc.c index 336b176ca..0ea8d1001 100644 --- a/coreutils/nproc.c +++ b/coreutils/nproc.c | |||
@@ -14,10 +14,14 @@ | |||
14 | //kbuild:lib-$(CONFIG_NPROC) += nproc.o | 14 | //kbuild:lib-$(CONFIG_NPROC) += nproc.o |
15 | 15 | ||
16 | //usage:#define nproc_trivial_usage | 16 | //usage:#define nproc_trivial_usage |
17 | //usage: "" | 17 | //usage: ""IF_LONG_OPTS("--all --ignore=N") |
18 | //TODO: "[--all] [--ignore=N]" | ||
19 | //usage:#define nproc_full_usage "\n\n" | 18 | //usage:#define nproc_full_usage "\n\n" |
20 | //usage: "Print number of CPUs" | 19 | //usage: "Print number of available CPUs" |
20 | //usage: IF_LONG_OPTS( | ||
21 | //usage: "\n" | ||
22 | //usage: "\n --all Number of installed CPUs" | ||
23 | //usage: "\n --ignore=N Exclude N CPUs" | ||
24 | //usage: ) | ||
21 | 25 | ||
22 | #include <sched.h> | 26 | #include <sched.h> |
23 | #include "libbb.h" | 27 | #include "libbb.h" |
@@ -26,13 +30,30 @@ int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
26 | int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | 30 | int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
27 | { | 31 | { |
28 | unsigned long mask[1024]; | 32 | unsigned long mask[1024]; |
29 | unsigned i, count = 0; | 33 | int count = 0; |
30 | 34 | #if ENABLE_LONG_OPTS | |
31 | //getopt32(argv, ""); | 35 | int ignore = 0; |
32 | 36 | int opts = getopt32long(argv, "\xfe:+", | |
33 | //if --all, count /sys/devices/system/cpu/cpuN dirs, else: | 37 | "ignore\0" Required_argument "\xfe" |
38 | "all\0" No_argument "\xff" | ||
39 | , &ignore | ||
40 | ); | ||
34 | 41 | ||
42 | if (opts & (1 << 1)) { | ||
43 | DIR *cpusd = opendir("/sys/devices/system/cpu"); | ||
44 | if (cpusd) { | ||
45 | struct dirent *de; | ||
46 | while (NULL != (de = readdir(cpusd))) { | ||
47 | char *cpuid = strstr(de->d_name, "cpu"); | ||
48 | if (cpuid && isdigit(cpuid[strlen(cpuid) - 1])) | ||
49 | count++; | ||
50 | } | ||
51 | closedir(cpusd); | ||
52 | } | ||
53 | } else | ||
54 | #endif | ||
35 | if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) { | 55 | if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) { |
56 | int i; | ||
36 | for (i = 0; i < ARRAY_SIZE(mask); i++) { | 57 | for (i = 0; i < ARRAY_SIZE(mask); i++) { |
37 | unsigned long m = mask[i]; | 58 | unsigned long m = mask[i]; |
38 | while (m) { | 59 | while (m) { |
@@ -42,8 +63,11 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
42 | } | 63 | } |
43 | } | 64 | } |
44 | } | 65 | } |
45 | if (count == 0) | 66 | |
46 | count++; | 67 | IF_LONG_OPTS(count -= ignore;) |
68 | if (count <= 0) | ||
69 | count = 1; | ||
70 | |||
47 | printf("%u\n", count); | 71 | printf("%u\n", count); |
48 | 72 | ||
49 | return 0; | 73 | return 0; |