2017-04-08 01:17:53 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see LICENSE in this source tree
|
|
|
|
*/
|
|
|
|
//config:config NPROC
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "nproc (248 bytes)"
|
2017-04-08 01:17:53 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Print number of CPUs
|
2017-04-08 01:17:53 +05:30
|
|
|
|
2017-08-03 06:59:32 +05:30
|
|
|
//applet:IF_NPROC(APPLET_NOFORK(nproc, nproc, BB_DIR_USR_BIN, BB_SUID_DROP, nproc))
|
2017-04-08 01:17:53 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_NPROC) += nproc.o
|
|
|
|
|
|
|
|
//usage:#define nproc_trivial_usage
|
2018-07-06 19:47:57 +05:30
|
|
|
//usage: ""IF_LONG_OPTS("--all --ignore=N")
|
2017-04-08 01:17:53 +05:30
|
|
|
//usage:#define nproc_full_usage "\n\n"
|
2018-07-06 19:47:57 +05:30
|
|
|
//usage: "Print number of available CPUs"
|
|
|
|
//usage: IF_LONG_OPTS(
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\n --all Number of installed CPUs"
|
|
|
|
//usage: "\n --ignore=N Exclude N CPUs"
|
|
|
|
//usage: )
|
2017-04-08 01:17:53 +05:30
|
|
|
|
|
|
|
#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];
|
2018-07-06 19:47:57 +05:30
|
|
|
int count = 0;
|
|
|
|
#if ENABLE_LONG_OPTS
|
|
|
|
int ignore = 0;
|
|
|
|
int opts = getopt32long(argv, "\xfe:+",
|
|
|
|
"ignore\0" Required_argument "\xfe"
|
|
|
|
"all\0" No_argument "\xff"
|
|
|
|
, &ignore
|
|
|
|
);
|
2017-04-08 01:17:53 +05:30
|
|
|
|
2018-07-06 19:47:57 +05:30
|
|
|
if (opts & (1 << 1)) {
|
|
|
|
DIR *cpusd = opendir("/sys/devices/system/cpu");
|
|
|
|
if (cpusd) {
|
|
|
|
struct dirent *de;
|
|
|
|
while (NULL != (de = readdir(cpusd))) {
|
|
|
|
char *cpuid = strstr(de->d_name, "cpu");
|
|
|
|
if (cpuid && isdigit(cpuid[strlen(cpuid) - 1]))
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
closedir(cpusd);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
2017-04-08 01:17:53 +05:30
|
|
|
if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) {
|
2018-07-06 19:47:57 +05:30
|
|
|
int i;
|
2017-04-08 01:17:53 +05:30
|
|
|
for (i = 0; i < ARRAY_SIZE(mask); i++) {
|
|
|
|
unsigned long m = mask[i];
|
|
|
|
while (m) {
|
|
|
|
if (m & 1)
|
|
|
|
count++;
|
|
|
|
m >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 19:47:57 +05:30
|
|
|
|
|
|
|
IF_LONG_OPTS(count -= ignore;)
|
|
|
|
if (count <= 0)
|
|
|
|
count = 1;
|
|
|
|
|
2017-04-08 01:17:53 +05:30
|
|
|
printf("%u\n", count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|