diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-04-07 21:47:53 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-04-07 21:47:53 +0200 |
commit | 87ae0fe095686b169ab1aeeb25c8ac3f5be30feb (patch) | |
tree | 9a549a82b1753062157fe7e13121767cbce44cd3 /coreutils/nproc.c | |
parent | 0f7f1ae094f4124303aca58f4efa69da4e2831a6 (diff) | |
download | busybox-w32-87ae0fe095686b169ab1aeeb25c8ac3f5be30feb.tar.gz busybox-w32-87ae0fe095686b169ab1aeeb25c8ac3f5be30feb.tar.bz2 busybox-w32-87ae0fe095686b169ab1aeeb25c8ac3f5be30feb.zip |
nproc: new applet
function old new delta
nproc_main - 98 +98
applet_names 2584 2590 +6
applet_main 1496 1500 +4
applet_install_loc 187 188 +1
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/nproc.c')
-rw-r--r-- | coreutils/nproc.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/coreutils/nproc.c b/coreutils/nproc.c new file mode 100644 index 000000000..d7c6a4ba1 --- /dev/null +++ b/coreutils/nproc.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com> | ||
3 | * | ||
4 | * Licensed under GPLv2, see LICENSE in this source tree | ||
5 | */ | ||
6 | //config:config NPROC | ||
7 | //config: bool "nproc" | ||
8 | //config: default y | ||
9 | //config: help | ||
10 | //config: Print number of CPUs | ||
11 | |||
12 | //applet:IF_NPROC(APPLET(nproc, BB_DIR_USR_BIN, BB_SUID_DROP)) | ||
13 | |||
14 | //kbuild:lib-$(CONFIG_NPROC) += nproc.o | ||
15 | |||
16 | //usage:#define nproc_trivial_usage | ||
17 | //usage: "" | ||
18 | //TODO: "[--all] [--ignore=N]" | ||
19 | //usage:#define nproc_full_usage "\n\n" | ||
20 | //usage: "Print number of CPUs" | ||
21 | |||
22 | #include <sched.h> | ||
23 | #include "libbb.h" | ||
24 | |||
25 | int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
26 | int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | ||
27 | { | ||
28 | unsigned long mask[1024]; | ||
29 | unsigned i, count = 0; | ||
30 | |||
31 | //applet_long_options = ...; | ||
32 | //getopt32(argv, ""); | ||
33 | |||
34 | //if --all, count /sys/devices/system/cpu/cpuN dirs, else: | ||
35 | |||
36 | if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) { | ||
37 | for (i = 0; i < ARRAY_SIZE(mask); i++) { | ||
38 | unsigned long m = mask[i]; | ||
39 | while (m) { | ||
40 | if (m & 1) | ||
41 | count++; | ||
42 | m >>= 1; | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | if (count == 0) | ||
47 | count++; | ||
48 | printf("%u\n", count); | ||
49 | |||
50 | return 0; | ||
51 | } | ||