[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH][nproc --all --ignore]
From:       haroon maqsood <maqsood3525 () live ! com>
Date:       2018-06-25 23:11:00
Message-ID: AM4PR04MB210025C4CE30FE355CA33349C84A0 () AM4PR04MB2100 ! eurprd04 ! prod ! outlook ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


________________________________
From: haroon maqsood <maqsood3525@live.com>
Sent: Monday, June 25, 2018 9:08 PM
To: busybox@busybox.net
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


[Attachment #5 (text/html)]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} \
--></style> </head>
<body dir="ltr">
<div id="divtagdefaultwrapper" \
style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" \
dir="ltr"> <div style="color: rgb(0, 0, 0);">
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, \
sans-serif" color="#000000"><b>From:</b> haroon maqsood \
&lt;maqsood3525@live.com&gt;<br> <b>Sent:</b> Monday, June 25, 2018 9:08 PM<br>
<b>To:</b> busybox@busybox.net<br>
<b>Subject:</b> [PATCH][nproc --all --ignore]</font>
<div>&nbsp;</div>
</div>
<meta content="text/html; charset=iso-8859-1">
<div dir="ltr">
<div id="x_divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:#000000; \
font-family:Calibri,Helvetica,sans-serif"> <p style="margin-top:0; \
margin-bottom:0">Hi PFA the my attempt at --all --ignore implementation for \
nproc.</p> <p style="margin-top:0; margin-bottom:0">i read that a good applet would \
make long options configurable, but in this</p> <p style="margin-top:0; \
margin-bottom:0">case i found at least that it would be simpler to just add them \
both.</p> <p style="margin-top:0; margin-bottom:0">please let me know what you \
think.</p> <p style="margin-top:0; margin-bottom:0">i have a question about&nbsp; \
<span>/sys/devices/system/cpu/</span><br> </p>
<p style="margin-top:0; margin-bottom:0">can /proc/cpuinfo be used ?</p>
<p style="margin-top:0; margin-bottom:0">Thanks</p>
<p style="margin-top:0; margin-bottom:0">Haroon<br>
</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
</div>
</div>
</div>
</div>
</body>
</html>


["nproc-all-ignore.patch" (text/x-patch)]

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
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic