________________________________
From: haroon maqsood <[email protected]>
Sent: Monday, June 25, 2018 9:08 PM
To: [email protected]
Subject: [PATCH][nproc --all --ignore]
Hi PFA the my attempt at --all --ignore implementation for nproc.
i read that a good applet would make long options configurable, but in this
case i found at least that it would be simpler to just add them both.
please let me know what you think.
i have a question about /sys/devices/system/cpu/
can /proc/cpuinfo be used ?
Thanks
Haroon
diff --git a/coreutils/nproc.c b/coreutils/nproc.c
index 336b176ca..6c4dcc291 100644
--- a/coreutils/nproc.c
+++ b/coreutils/nproc.c
@@ -8,6 +8,7 @@
//config: default y
//config: help
//config: Print number of CPUs
+//config:
//applet:IF_NPROC(APPLET_NOFORK(nproc, nproc, BB_DIR_USR_BIN, BB_SUID_DROP, nproc))
@@ -15,24 +16,38 @@
//usage:#define nproc_trivial_usage
//usage: ""
-//TODO: "[--all] [--ignore=N]"
//usage:#define nproc_full_usage "\n\n"
-//usage: "Print number of CPUs"
+//usage: "Print number of CPUs\n"
+//usage: "\n --all print the number of installed processors."
+//usage: "\n --ignore=N if possible, exclude N processing units"
#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)
+int nproc_main(int argc UNUSED_PARAM, char **argv)
+
{
unsigned long mask[1024];
- unsigned i, count = 0;
-
- //getopt32(argv, "");
+ int i, val = 0, count = 0;
- //if --all, count /sys/devices/system/cpu/cpuN dirs, else:
+ uint32_t opts = getopt32long(argv, "ai:+", "all\0""\\n""ignore\0" Required_argument "i", &val);
+ if (opts & (1 << 1))
+ count = (~val) + 1;
- if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) {
+ if (opts & (1 << 0)) {
+ DIR * cpusd = opendir("/sys/devices/system/cpu/");
+ if (NULL != cpusd) {
+ struct dirent * de = NULL;
+ while (NULL != (de = readdir(cpusd))) {
+ char * cpuid = NULL;
+ if ((NULL != (cpuid = strstr(de->d_name, "cpu")))
+ && isdigit(cpuid[strlen(cpuid) - 1]))
+ count++;
+ }
+ closedir(cpusd);
+ }
+ } 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) {
@@ -42,9 +57,11 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
}
}
}
- if (count == 0)
- count++;
- printf("%u\n", count);
+
+ if (count <= 0)
+ count = 1;
+
+ printf("%d\n", count);
return 0;
}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox