forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-itimer.c
146 lines (125 loc) · 3.8 KB
/
stress-itimer.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
/*
* Copyright (C) 2013-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"
static volatile uint64_t itimer_counter = 0;
static double rate_us;
static double start;
/*
* stress_set_itimer_freq()
* set itimer frequency from given option
*/
void stress_set_itimer_freq(const char *opt)
{
uint64_t itimer_freq;
itimer_freq = get_uint64(opt);
check_range("itimer-freq", itimer_freq,
MIN_TIMER_FREQ, MAX_TIMER_FREQ);
set_setting("itimer-freq", TYPE_ID_UINT64, &itimer_freq);
}
/*
* stress_itimer_set()
* set timer, ensure it is never zero
*/
static void stress_itimer_set(struct itimerval *timer)
{
double rate;
if (g_opt_flags & OPT_FLAGS_TIMER_RAND) {
/* Mix in some random variation */
double r = ((double)(mwc32() % 10000) - 5000.0) / 40000.0;
rate = rate_us + (rate_us * r);
} else {
rate = rate_us;
}
timer->it_value.tv_sec = (long long int)rate / 1000000;
timer->it_value.tv_usec = (long long int)rate % 1000000;
if (timer->it_value.tv_sec == 0 &&
timer->it_value.tv_usec < 1)
timer->it_value.tv_usec = 1;
timer->it_interval.tv_sec = timer->it_value.tv_sec;
timer->it_interval.tv_usec = timer->it_value.tv_usec;
}
/*
* stress_itimer_handler()
* catch itimer signal and cancel if no more runs flagged
*/
static void stress_itimer_handler(int sig)
{
struct itimerval timer;
sigset_t mask;
(void)sig;
itimer_counter++;
if (sigpending(&mask) == 0)
if (sigismember(&mask, SIGINT))
goto cancel;
/* High freq timer, check periodically for timeout */
if ((itimer_counter & 65535) == 0)
if ((time_now() - start) > (double)g_opt_timeout)
goto cancel;
if (g_keep_stressing_flag) {
stress_itimer_set(&timer);
return;
}
cancel:
g_keep_stressing_flag = false;
/* Cancel timer if we detect no more runs */
(void)memset(&timer, 0, sizeof(timer));
(void)setitimer(ITIMER_PROF, &timer, NULL);
}
/*
* stress_itimer
* stress itimer
*/
int stress_itimer(const args_t *args)
{
struct itimerval timer;
sigset_t mask;
uint64_t itimer_freq = DEFAULT_TIMER_FREQ;
(void)sigemptyset(&mask);
(void)sigaddset(&mask, SIGINT);
(void)sigprocmask(SIG_SETMASK, &mask, NULL);
start = time_now();
if (!get_setting("itimer-freq", &itimer_freq)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
itimer_freq = MAX_ITIMER_FREQ;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
itimer_freq = MIN_ITIMER_FREQ;
}
rate_us = itimer_freq ? 1000000.0 / itimer_freq : 1000000.0;
if (stress_sighandler(args->name, SIGPROF, stress_itimer_handler, NULL) < 0)
return EXIT_FAILURE;
stress_itimer_set(&timer);
if (setitimer(ITIMER_PROF, &timer, NULL) < 0) {
pr_fail_err("setitimer");
return EXIT_FAILURE;
}
do {
struct itimerval t;
(void)getitimer(ITIMER_PROF, &t);
*args->counter = itimer_counter;
} while (keep_stressing());
(void)memset(&timer, 0, sizeof(timer));
(void)setitimer(ITIMER_PROF, &timer, NULL);
return EXIT_SUCCESS;
}