-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstress-hrtimers.c
212 lines (178 loc) · 5.21 KB
/
stress-hrtimers.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (C) 2013-2018 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 <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(HAVE_LIB_RT) && defined(__linux__)
static volatile uint64_t *timer_counter;
static uint64_t max_ops;
static timer_t timerid;
static double start;
#define PROCS_MAX (32)
/*
* stress_hrtimers_set()
* set timer, ensure it is never zero
*/
static void stress_hrtimers_set(struct itimerspec *timer)
{
timer->it_value.tv_sec = 0;
timer->it_value.tv_nsec = (mwc64() % 50000);
if (timer->it_value.tv_sec == 0 &&
timer->it_value.tv_nsec < 1)
timer->it_value.tv_nsec = 1;
timer->it_interval.tv_sec = timer->it_value.tv_sec;
timer->it_interval.tv_nsec = timer->it_value.tv_nsec;
}
/*
* stress_hrtimers_keep_stressing()
* returns true if we can keep on running a stressor
*/
static bool HOT OPTIMIZE3 stress_hrtimers_keep_stressing(void)
{
return (LIKELY(g_keep_stressing_flag) &&
LIKELY(!max_ops || ((*timer_counter) < max_ops)));
}
/*
* stress_hrtimers_handler()
* catch timer signal and cancel if no more runs flagged
*/
static void MLOCKED stress_hrtimers_handler(int sig)
{
struct itimerspec timer;
sigset_t mask;
(void)sig;
if (!stress_hrtimers_keep_stressing())
goto cancel;
(*timer_counter)++;
if (sigpending(&mask) == 0)
if (sigismember(&mask, SIGINT))
goto cancel;
/* High freq timer, check periodically for timeout */
if (((*timer_counter) & 65535) == 0)
if ((time_now() - start) > (double)g_opt_timeout)
goto cancel;
stress_hrtimers_set(&timer);
return;
cancel:
g_keep_stressing_flag = false;
/* Cancel timer if we detect no more runs */
(void)memset(&timer, 0, sizeof(timer));
(void)timer_settime(timerid, 0, &timer, NULL);
}
/*
* stress_hrtimer_process
* stress timer child process
*/
static int stress_hrtimer_process(const args_t *args, uint64_t *counter)
{
struct sigevent sev;
struct itimerspec timer;
sigset_t mask;
int ret;
timer_counter = counter;
(void)sigemptyset(&mask);
(void)sigaddset(&mask, SIGINT);
(void)sigprocmask(SIG_SETMASK, &mask, NULL);
ret = stress_set_sched(getpid(), SCHED_RR, UNDEFINED, true);
(void)ret;
start = time_now();
if (stress_sighandler(args->name, SIGRTMIN, stress_hrtimers_handler, NULL) < 0)
return EXIT_FAILURE;
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCK_REALTIME, &sev, &timerid) < 0) {
pr_fail_err("timer_create");
return EXIT_FAILURE;
}
stress_hrtimers_set(&timer);
if (timer_settime(timerid, 0, &timer, NULL) < 0) {
pr_fail_err("timer_settime");
return EXIT_FAILURE;
}
do {
/* The sleep will be interrupted on each hrtimer tick */
sleep(1);
} while (stress_hrtimers_keep_stressing());
if (timer_delete(timerid) < 0) {
pr_fail_err("timer_delete");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int stress_hrtimers(const args_t *args)
{
pid_t pids[PROCS_MAX];
const size_t page_size = args->page_size;
const size_t counters_sz = sizeof(uint64_t) * PROCS_MAX;
const size_t sz = (counters_sz + page_size) & ~(page_size - 1);
size_t i;
uint64_t *counters;
max_ops = args->max_ops / PROCS_MAX;
memset(pids, 0, sizeof(pids));
counters = (void *)mmap(NULL, sz, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (counters == MAP_FAILED) {
pr_fail_dbg("mmap");
return EXIT_NO_RESOURCE;
}
for (i = 0; i < PROCS_MAX && keep_stressing(); i++) {
pids[i] = fork();
if (pids[0] < 0)
goto reap;
else if (pids[i] == 0) {
/* Child */
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
set_oom_adjustment(args->name, true);
stress_hrtimer_process(args, &counters[i]);
_exit(EXIT_SUCCESS);
}
}
do {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 10000000;
(void)nanosleep(&req, NULL);
*args->counter = 0;
for (i = 0; i < PROCS_MAX; i++)
*args->counter += counters[i];
} while (keep_stressing());
reap:
for (i = 0; i < PROCS_MAX; i++) {
if (pids[i] > 0) {
int status, ret;
(void)kill(pids[i], SIGALRM);
ret = waitpid(pids[i], &status, 0);
(void)ret;
}
}
(void)munmap(counters, sz);
return EXIT_SUCCESS;
}
#else
int stress_hrtimers(const args_t *args)
{
return stress_not_implemented(args);
}
#endif