2 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
4 * Licensed under GPLv2, see LICENSE in this source tree
7 //config: bool "nproc (3.7 kb)"
10 //config: Print number of CPUs
12 //applet:IF_NPROC(APPLET_NOFORK(nproc, nproc, BB_DIR_USR_BIN, BB_SUID_DROP, nproc))
14 //kbuild:lib-$(CONFIG_NPROC) += nproc.o
16 //usage:#define nproc_trivial_usage
17 //usage: ""IF_LONG_OPTS("--all --ignore=N")
18 //usage:#define nproc_full_usage "\n\n"
19 //usage: "Print number of available CPUs"
20 //usage: IF_LONG_OPTS(
22 //usage: "\n --all Number of installed CPUs"
23 //usage: "\n --ignore=N Exclude N CPUs"
29 int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30 int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
32 unsigned long mask[1024];
36 int opts = getopt32long(argv, "\xfe:+",
37 "ignore\0" Required_argument "\xfe"
38 "all\0" No_argument "\xff"
42 if (opts & (1 << 1)) {
43 DIR *cpusd = opendir("/sys/devices/system/cpu");
46 while (NULL != (de = readdir(cpusd))) {
47 char *cpuid = strstr(de->d_name, "cpu");
48 if (cpuid && isdigit(cpuid[strlen(cpuid) - 1]))
55 if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) {
57 for (i = 0; i < ARRAY_SIZE(mask); i++) {
58 unsigned long m = mask[i];
67 IF_LONG_OPTS(count -= ignore;)
71 printf("%u\n", count);