summaryrefslogtreecommitdiff
path: root/coreutils/nproc.c
diff options
context:
space:
mode:
authorDenys Vlasenko2017-04-07 21:47:53 +0200
committerDenys Vlasenko2017-04-07 21:47:53 +0200
commit87ae0fe095686b169ab1aeeb25c8ac3f5be30feb (patch)
tree9a549a82b1753062157fe7e13121767cbce44cd3 /coreutils/nproc.c
parent0f7f1ae094f4124303aca58f4efa69da4e2831a6 (diff)
downloadbusybox-87ae0fe095686b169ab1aeeb25c8ac3f5be30feb.zip
busybox-87ae0fe095686b169ab1aeeb25c8ac3f5be30feb.tar.gz
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.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/coreutils/nproc.c b/coreutils/nproc.c
new file mode 100644
index 0000000..d7c6a4b
--- /dev/null
+++ b/coreutils/nproc.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
+ *
+ * Licensed under GPLv2, see LICENSE in this source tree
+ */
+//config:config NPROC
+//config: bool "nproc"
+//config: default y
+//config: help
+//config: Print number of CPUs
+
+//applet:IF_NPROC(APPLET(nproc, BB_DIR_USR_BIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_NPROC) += nproc.o
+
+//usage:#define nproc_trivial_usage
+//usage: ""
+//TODO: "[--all] [--ignore=N]"
+//usage:#define nproc_full_usage "\n\n"
+//usage: "Print number of CPUs"
+
+#include <sched.h>
+#include "libbb.h"
+
+int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
+{
+ unsigned long mask[1024];
+ unsigned i, count = 0;
+
+ //applet_long_options = ...;
+ //getopt32(argv, "");
+
+ //if --all, count /sys/devices/system/cpu/cpuN dirs, else:
+
+ if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) {
+ for (i = 0; i < ARRAY_SIZE(mask); i++) {
+ unsigned long m = mask[i];
+ while (m) {
+ if (m & 1)
+ count++;
+ m >>= 1;
+ }
+ }
+ }
+ if (count == 0)
+ count++;
+ printf("%u\n", count);
+
+ return 0;
+}