1 /*
2 * Copyright (c) 2012 Fujitsu Ltd.
3 * Author: Wanlong Gao <gaowanlong@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "lapi/cpuset.h"
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include "test.h"
25 #include "safe_macros.h"
26
tst_ncpus(void)27 long tst_ncpus(void)
28 {
29 long ncpus = -1;
30 #ifdef _SC_NPROCESSORS_ONLN
31 ncpus = SAFE_SYSCONF(NULL, _SC_NPROCESSORS_ONLN);
32 #else
33 tst_brkm(TBROK, NULL, "could not determine number of CPUs online");
34 #endif
35 return ncpus;
36 }
37
tst_ncpus_conf(void)38 long tst_ncpus_conf(void)
39 {
40 long ncpus_conf = -1;
41 #ifdef _SC_NPROCESSORS_CONF
42 ncpus_conf = SAFE_SYSCONF(NULL, _SC_NPROCESSORS_CONF);
43 #else
44 tst_brkm(TBROK, NULL, "could not determine number of CPUs configured");
45 #endif
46 return ncpus_conf;
47 }
48
49 #define KERNEL_MAX "/sys/devices/system/cpu/kernel_max"
50
tst_ncpus_max(void)51 long tst_ncpus_max(void)
52 {
53 long ncpus_max = -1;
54 struct stat buf;
55
56 /* sched_getaffinity() and sched_setaffinity() cares about number of
57 * possible CPUs the OS or hardware can support, which can be larger
58 * than what sysconf(_SC_NPROCESSORS_CONF) currently provides
59 * (by enumarating /sys/devices/system/cpu/cpu* entries).
60 *
61 * Use /sys/devices/system/cpu/kernel_max, if available. This
62 * represents NR_CPUS-1, a compile time option which specifies
63 * "maximum number of CPUs which this kernel will support".
64 * This should provide cpu mask size large enough for any purposes. */
65 if (stat(KERNEL_MAX, &buf) == 0) {
66 SAFE_FILE_SCANF(NULL, KERNEL_MAX, "%ld", &ncpus_max);
67 /* this is maximum CPU index allowed by the kernel
68 * configuration, so # of cpus allowed by config is +1 */
69 ncpus_max++;
70 } else {
71 /* fall back to _SC_NPROCESSORS_CONF */
72 ncpus_max = tst_ncpus_conf();
73 }
74 return ncpus_max;
75 }
76
tst_ncpus_available(void)77 long tst_ncpus_available(void)
78 {
79 #ifdef CPU_COUNT_S
80 long ncpus = tst_ncpus_max();
81 size_t cpusz = CPU_ALLOC_SIZE(ncpus);
82 cpu_set_t *cpus = CPU_ALLOC(ncpus);
83
84 if (!cpus)
85 tst_brkm(TBROK | TERRNO, NULL, "CPU_ALLOC(%zu)", cpusz);
86
87 if (sched_getaffinity(0, cpusz, cpus)) {
88 tst_resm(TWARN | TERRNO, "sched_getaffinity(0, %zu, %zx)",
89 cpusz, (size_t)cpus);
90 } else {
91 ncpus = CPU_COUNT_S(cpusz, cpus);
92 }
93 CPU_FREE(cpus);
94
95 return ncpus;
96 #else
97 return tst_ncpus();
98 #endif
99 }
100