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