forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-cpu-online.c
139 lines (126 loc) · 3.43 KB
/
stress-cpu-online.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (C) 2016-2017 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(__linux__)
/*
* stress_cpu_online_set()
* set a specified CPUs online or offline
*/
static int stress_cpu_online_set(
const args_t *args,
const int32_t cpu,
const int setting)
{
char filename[PATH_MAX];
char data[3] = { '0' + setting, '\n', 0 };
ssize_t ret;
(void)snprintf(filename, sizeof(filename),
"/sys/devices/system/cpu/cpu%" PRId32 "/online", cpu);
ret = system_write(filename, data, sizeof data);
if ((ret < 0) &&
((ret != -EAGAIN) && (ret != -EINTR))) {
pr_fail_err("write");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_online
* stress twiddling CPUs online/offline
*/
int stress_cpu_online(const args_t *args)
{
const int32_t cpus = stress_get_processors_configured();
int32_t i, cpu_online_count = 0;
bool *cpu_online;
int rc = EXIT_SUCCESS;
if (geteuid() != 0) {
if (args->instance == 0)
pr_inf("%s: need root privilege to run "
"this stressor\n", args->name);
/* Not strictly a test failure */
return EXIT_SUCCESS;
}
if ((cpus < 1) || (cpus > 65536)) {
pr_inf("%s: too few or too many CPUs (found %" PRId32 ")\n",
args->name, cpus);
return EXIT_FAILURE;
}
cpu_online = calloc(cpus, sizeof(bool));
if (!cpu_online) {
pr_err("%s: out of memory\n", args->name);
return EXIT_FAILURE;
}
/*
* Determine how many CPUs we can online/offline via
* the online sys interface
*/
for (i = 0; i < cpus; i++) {
char filename[PATH_MAX];
int ret;
(void)snprintf(filename, sizeof(filename),
"/sys/devices/system/cpu/cpu%" PRId32 "/online", i);
ret = access(filename, O_RDWR);
if (ret == 0) {
cpu_online[i] = true;
cpu_online_count++;
}
}
if (cpu_online_count == 0) {
pr_inf("%s: no CPUs can be set online/offline\n", args->name);
free(cpu_online);
return EXIT_FAILURE;
}
/*
* Now randomly offline/online them all
*/
do {
unsigned long cpu = mwc32() % cpus;
if (cpu_online[cpu]) {
rc = stress_cpu_online_set(args, cpu, 0);
if (rc != EXIT_SUCCESS)
break;
rc = stress_cpu_online_set(args, cpu, 1);
if (rc != EXIT_SUCCESS)
break;
inc_counter(args);
}
} while (keep_stressing());
/*
* Force CPUs all back online
*/
for (i = 0; i < cpus; i++) {
if (cpu_online[i])
(void)stress_cpu_online_set(args, i, 1);
}
free(cpu_online);
return EXIT_SUCCESS;
}
#else
int stress_cpu_online(const args_t *args)
{
return stress_not_implemented(args);
}
#endif