Denys Vlasenko | 87ae0fe | 2017-04-07 21:47:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com> |
| 3 | * |
| 4 | * Licensed under GPLv2, see LICENSE in this source tree |
| 5 | */ |
| 6 | //config:config NPROC |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 7 | //config: bool "nproc (248 bytes)" |
Denys Vlasenko | 87ae0fe | 2017-04-07 21:47:53 +0200 | [diff] [blame] | 8 | //config: default y |
| 9 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 10 | //config: Print number of CPUs |
Denys Vlasenko | 87ae0fe | 2017-04-07 21:47:53 +0200 | [diff] [blame] | 11 | |
Denys Vlasenko | 819b47a | 2017-08-03 03:29:32 +0200 | [diff] [blame] | 12 | //applet:IF_NPROC(APPLET_NOFORK(nproc, nproc, BB_DIR_USR_BIN, BB_SUID_DROP, nproc)) |
Denys Vlasenko | 87ae0fe | 2017-04-07 21:47:53 +0200 | [diff] [blame] | 13 | |
| 14 | //kbuild:lib-$(CONFIG_NPROC) += nproc.o |
| 15 | |
| 16 | //usage:#define nproc_trivial_usage |
| 17 | //usage: "" |
| 18 | //TODO: "[--all] [--ignore=N]" |
| 19 | //usage:#define nproc_full_usage "\n\n" |
| 20 | //usage: "Print number of CPUs" |
| 21 | |
| 22 | #include <sched.h> |
| 23 | #include "libbb.h" |
| 24 | |
| 25 | int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 26 | int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 27 | { |
| 28 | unsigned long mask[1024]; |
| 29 | unsigned i, count = 0; |
| 30 | |
Denys Vlasenko | 87ae0fe | 2017-04-07 21:47:53 +0200 | [diff] [blame] | 31 | //getopt32(argv, ""); |
| 32 | |
| 33 | //if --all, count /sys/devices/system/cpu/cpuN dirs, else: |
| 34 | |
| 35 | if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) { |
| 36 | for (i = 0; i < ARRAY_SIZE(mask); i++) { |
| 37 | unsigned long m = mask[i]; |
| 38 | while (m) { |
| 39 | if (m & 1) |
| 40 | count++; |
| 41 | m >>= 1; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | if (count == 0) |
| 46 | count++; |
| 47 | printf("%u\n", count); |
| 48 | |
| 49 | return 0; |
| 50 | } |