forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stress-clock.c
573 lines (506 loc) · 14.7 KB
/
stress-clock.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#include "core-capabilities.h"
#if defined(HAVE_POLL_H)
#include <poll.h>
#endif
#if defined(HAVE_SYS_TIMEX_H)
#include <sys/timex.h>
#endif
static const stress_help_t help[] = {
{ NULL, "clock N", "start N workers thrashing clocks and POSIX timers" },
{ NULL, "clock-ops N", "stop clock workers after N bogo operations" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_SYS_TIMEX_H)
#define shim_timex timex
#else
struct shim_timex {
char data[128];
};
#endif
#if defined(HAVE_LIB_RT) && \
defined(HAVE_CLOCK_GETTIME) && \
defined(HAVE_CLOCK_SETTIME)
typedef struct {
const int id; /* Clock ID */
const char *name; /* Clock name */
} stress_clock_info_t;
#define CLOCK_INFO(x) { x, #x }
static const stress_clock_info_t clocks[] = {
#if defined(CLOCK_REALTIME)
CLOCK_INFO(CLOCK_REALTIME),
#endif
#if defined(CLOCK_REALTIME_COARSE)
CLOCK_INFO(CLOCK_REALTIME_COARSE),
#endif
#if defined(CLOCK_MONOTONIC)
CLOCK_INFO(CLOCK_MONOTONIC),
#endif
#if defined(CLOCK_MONOTONIC_RAW)
CLOCK_INFO(CLOCK_MONOTONIC_RAW),
#endif
#if defined(CLOCK_MONOTONIC_ACTIVE)
CLOCK_INFO(CLOCK_MONOTONIC_ACTIVE),
#endif
#if defined(CLOCK_BOOTTIME)
CLOCK_INFO(CLOCK_BOOTTIME),
#endif
#if defined(CLOCK_PROCESS_CPUTIME_ID)
CLOCK_INFO(CLOCK_PROCESS_CPUTIME_ID),
#endif
#if defined(CLOCK_THREAD_CPUTIME_ID)
CLOCK_INFO(CLOCK_THREAD_CPUTIME_ID),
#endif
#if defined(CLOCK_MONOTONIC_ACTIVE)
CLOCK_INFO(CLOCK_MONOTONIC_ACTIVE),
#endif
#if defined(CLOCK_TAI)
CLOCK_INFO(CLOCK_TAI)
#endif
};
#if defined(HAVE_CLOCK_NANOSLEEP)
static const int clocks_nanosleep[] = {
#if defined(CLOCK_REALTIME)
CLOCK_REALTIME,
#endif
#if defined(CLOCK_MONOTONIC)
CLOCK_MONOTONIC,
#endif
#if defined(CLOCK_THREAD_CPUTIME_ID)
CLOCK_THREAD_CPUTIME_ID
#endif
};
#endif
#if defined(HAVE_TIMER_CREATE) && \
defined(HAVE_TIMER_DELETE) && \
defined(HAVE_TIMER_GETTIME) && \
defined(HAVE_TIMER_GETOVERRUN) && \
defined(HAVE_TIMER_SETTIME)
static const int timers[] = {
#if defined(CLOCK_REALTIME)
CLOCK_REALTIME,
#endif
#if defined(CLOCK_MONOTONIC)
CLOCK_MONOTONIC,
#endif
#if defined(CLOCK_THREAD_CPUTIME_ID)
CLOCK_THREAD_CPUTIME_ID
#endif
};
#endif
#if defined(HAVE_CLOCK_NANOSLEEP) || \
(defined(HAVE_TIMER_CREATE) && \
defined(HAVE_TIMER_DELETE) && \
defined(HAVE_TIMER_GETTIME) && \
defined(HAVE_TIMER_GETOVERRUN) && \
defined(HAVE_TIMER_SETTIME))
/*
* stress_clock_name()
* clock id to name
*/
static const char *stress_clock_name(int id)
{
size_t i;
for (i = 0; i < SIZEOF_ARRAY(clocks); i++) {
if (clocks[i].id == id)
return clocks[i].name;
}
return "(unknown clock)";
}
#endif
#if (defined(__NR_clock_adjtime) && \
defined(HAVE_SYS_TIMEX_H) && \
defined(ADJ_SETOFFSET)) || \
(defined(HAVE_CLOCK_NANOSLEEP) && \
defined(TIMER_ABSTIME))
#define CHECK_INVALID_CLOCK_ID (1)
#endif
#if defined(CHECK_INVALID_CLOCK_ID)
/*
* check_invalid_clock_id()
* function to check if given clock_id is valid
*/
static inline bool check_invalid_clock_id(const clockid_t id) {
struct timespec tp;
(void)memset(&tp, 0, sizeof(tp));
return (clock_gettime(id, &tp) != 0);
}
#endif
#define FD_TO_CLOCKID(fd) ((~(clockid_t)(fd) << 3) | 3)
#if defined(__NR_clock_adjtime) && \
defined(HAVE_SYS_TIMEX_H) && \
defined(CLOCK_THREAD_CPUTIME_ID) && \
defined(ADJ_SETOFFSET)
/*
* shim_clock_adjtime
* wrapper for linux clock_adjtime system call
*/
static int shim_clock_adjtime(clockid_t clk_id, struct shim_timex *tx)
{
return (int)syscall(__NR_clock_adjtime, clk_id, tx);
}
#endif
/*
* stress_clock()
* stress system by rapid clocking system calls
*/
static int stress_clock(const stress_args_t *args)
{
bool test_invalid_timespec = true;
const bool is_root = stress_check_capability(SHIM_CAP_IS_ROOT);
#if defined(CHECK_INVALID_CLOCK_ID)
const bool invalid_clock_id = check_invalid_clock_id(INT_MAX);
#endif
/*
* Use same random number seed for each
* test run to ensure predictable repeatable
* 'random' sleep duration timings
*/
stress_mwc_set_seed(0xf238, 0x1872);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
#if defined(CLOCK_THREAD_CPUTIME_ID) && \
defined(HAVE_CLOCK_GETTIME) && \
defined(HAVE_CLOCK_SETTIME)
{
int ret;
struct timespec t;
/*
* Exercise setting local thread CPU timer
*/
ret = shim_clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EINVAL) && (errno != ENOSYS))
pr_fail("%s: clock_gettime failed for timer 'CLOCK_THREAD_CPUTIME_ID', errno=%d (%s)\n",
args->name, errno, strerror(errno));
/* Exercise clock_settime with illegal clockid */
(void)shim_clock_settime((clockid_t)-1, &t);
/*
* According to clock_settime(2), setting the timer
* CLOCK_THREAD_CPUTIME_ID is not possible on Linux.
* Try to set it and ignore the result because it will
* currently return -EINVAL, but it may not do so in the
* future.
*/
VOID_RET(int, clock_settime(CLOCK_THREAD_CPUTIME_ID, &t));
}
#else
UNEXPECTED
#endif
#if defined(HAVE_CLOCK_GETRES)
{
size_t i;
struct timespec t;
/* Exercise clock_getres with illegal clockid */
(void)shim_clock_getres((clockid_t)-1, &t);
/* Exercise clock_gettime with illegal clockid */
(void)shim_clock_gettime((clockid_t)-1, &t);
/*
* Exercise clock_getres and clock_gettime for each clock
*/
for (i = 0; i < SIZEOF_ARRAY(clocks); i++) {
int ret;
ret = shim_clock_getres(clocks[i].id, &t);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EINVAL) && (errno != ENOSYS))
pr_fail("%s: clock_getres failed for timer '%s', errno=%d (%s)\n",
args->name, clocks[i].name, errno, strerror(errno));
ret = shim_clock_gettime(clocks[i].id, &t);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EINVAL) && (errno != ENOSYS))
pr_fail("%s: clock_gettime failed for timer '%s', errno=%d (%s)\n",
args->name, clocks[i].name, errno, strerror(errno));
}
}
#else
UNEXPECTED
#endif
#if defined(HAVE_CLOCK_GETTIME) && \
defined(HAVE_CLOCK_SETTIME)
if (test_invalid_timespec) {
size_t i;
for (i = 0; i < SIZEOF_ARRAY(clocks); i++) {
struct timespec t, t1;
int ret;
/* Save current time to reset later if required */
ret = shim_clock_gettime(clocks[i].id, &t1);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EINVAL) && (errno != ENOSYS)) {
pr_fail("%s: clock_getres failed for timer '%s', errno=%d (%s)\n",
args->name, clocks[i].name, errno, strerror(errno));
}
if (ret < 0)
continue;
/* Ensuring clock_settime cannot succeed without privilege */
if (!is_root) {
ret = shim_clock_settime(clocks[i].id, &t1);
if (ret != -EPERM) {
/* This is an error, report it! */
pr_fail("%s: clock_settime failed, did not have privilege to "
"set time, expected -EPERM, instead got errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
/*
* Exercise clock_settime with illegal tv sec
* and nsec values
*/
t.tv_sec = -1;
t.tv_nsec = -1;
/*
* Test only if time fields are invalid
* negative values (some systems may
* represent tv_* files as unsigned hence
* this sanity check)
*/
if ((t.tv_sec < 0) && (t.tv_nsec < 0)) { /* cppcheck-suppress knownConditionTrueFalse */
ret = shim_clock_settime(clocks[i].id, &t);
if (ret < 0)
continue;
/* Expected a failure, but it succeeded(!) */
pr_fail("%s: clock_settime was able to set an "
"invalid negative time for timer '%s'\n",
args->name, clocks[i].name);
/* Restore the correct time */
ret = shim_clock_settime(clocks[i].id, &t1);
if ((ret < 0) && (errno != EINVAL) && (errno != ENOSYS)) {
pr_fail("%s: clock_gettime failed for timer '%s', errno=%d (%s)\n",
args->name, clocks[i].name, errno, strerror(errno));
}
/*
* Ensuring invalid clock_settime runs
* only single time to minimize time lag
*/
test_invalid_timespec = false;
}
}
}
#else
UNEXPECTED
#endif
#if defined(HAVE_CLOCK_NANOSLEEP) && \
defined(TIMER_ABSTIME)
{
size_t i;
struct timespec t;
static int n = 0;
if (n++ >= 1024) {
n = 0;
/* Exercise clock_nanosleep on invalid clock id */
if (invalid_clock_id) {
(void)memset(&t, 0, sizeof(t));
VOID_RET(int, clock_nanosleep(INT_MAX, TIMER_ABSTIME, &t, NULL));
}
(void)memset(&t, 0, sizeof(t));
t.tv_sec = -1;
VOID_RET(int, clock_nanosleep(clocks_nanosleep[0], TIMER_ABSTIME, &t, NULL));
(void)memset(&t, 0, sizeof(t));
t.tv_nsec = STRESS_NANOSECOND;
VOID_RET(int, clock_nanosleep(clocks_nanosleep[0], TIMER_ABSTIME, &t, NULL));
}
/*
* Exercise clock_nanosleep for each clock
*/
for (i = 0; i < SIZEOF_ARRAY(clocks_nanosleep); i++) {
int ret;
t.tv_sec = 0;
t.tv_nsec = (stress_mwc32() % 2500) + 1;
/*
* Calling with TIMER_ABSTIME will force
* clock_nanosleep() to return immediately
*/
ret = clock_nanosleep(clocks_nanosleep[i], TIMER_ABSTIME, &t, NULL);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY))
pr_fail("%s: clock_nanosleep failed for timer '%s', errno=%d (%s)\n",
args->name,
stress_clock_name(clocks_nanosleep[i]),
errno, strerror(errno));
}
}
#else
UNEXPECTED
#endif
#if defined(__NR_clock_adjtime) && \
defined(HAVE_SYS_TIMEX_H) && \
defined(CLOCK_THREAD_CPUTIME_ID) && \
defined(ADJ_SETOFFSET)
{
size_t i;
struct shim_timex tx;
/* Exercise clock_adjtime on invalid clock id */
if (invalid_clock_id) {
(void)memset(&tx, 0, sizeof(tx));
VOID_RET(int, shim_clock_adjtime(INT_MAX, &tx));
}
/*
* Exercise clock_adjtime
*/
for (i = 0; i < SIZEOF_ARRAY(clocks); i++) {
int ret;
(void)memset(&tx, 0, sizeof(tx));
tx.modes = ADJ_SETOFFSET;
tx.time.tv_sec = 0;
tx.time.tv_usec = 0;
ret = shim_clock_adjtime(clocks[i].id, &tx);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EINVAL) && (errno != ENOSYS) &&
(errno != EPERM) && (errno != EOPNOTSUPP)) {
pr_fail("%s: clock_adjtime failed for timer '%s', errno=%d (%s)\n",
args->name, clocks[i].name, errno, strerror(errno));
}
}
}
#else
UNEXPECTED
#endif
#if defined(HAVE_TIMER_CREATE) && \
defined(HAVE_TIMER_DELETE) && \
defined(HAVE_TIMER_GETTIME) && \
defined(HAVE_TIMER_GETOVERRUN) && \
defined(HAVE_TIMER_SETTIME)
{
size_t i;
const size_t n = SIZEOF_ARRAY(timers);
bool timer_fail[n];
timer_t timer_id[n];
struct itimerspec its;
int ret;
/*
* Stress the timers
*/
for (i = 0; i < n; i++) {
struct sigevent sevp;
timer_fail[i] = false;
timer_id[i] = (timer_t)-1;
(void)memset(&sevp, 0, sizeof(sevp));
sevp.sigev_notify = SIGEV_NONE;
ret = timer_create(timers[i], &sevp, &timer_id[i]);
if (ret < 0) {
timer_fail[i] = true;
if (g_opt_flags & OPT_FLAGS_VERIFY) {
if ((errno == EINVAL) || (errno == EPERM))
continue;
pr_fail("%s: timer_create failed for timer '%s', errno=%d (%s)\n",
args->name,
stress_clock_name(timers[i]),
errno, strerror(errno));
}
continue;
}
/* One shot mode, for random time 0..5000 ns */
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = (stress_mwc32() % 5000) + 1;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
ret = timer_settime(timer_id[i], 0, &its, NULL);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
pr_fail("%s: timer_settime failed for timer '%s', errno=%d (%s)\n",
args->name,
stress_clock_name(timers[i]),
errno, strerror(errno));
}
}
for (i = 0; i < n; i++) {
if (timer_fail[i] || (timer_id[i] == (timer_t)-1))
continue;
ret = timer_gettime(timer_id[i], &its);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
pr_fail("%s: timer_gettime failed for timer '%s', errno=%d (%s)\n",
args->name,
stress_clock_name(timers[i]),
errno, strerror(errno));
break;
}
ret = timer_getoverrun(timer_id[i]);
(void)ret;
}
for (i = 0; i < n; i++) {
if (timer_fail[i] || (timer_id[i] == (timer_t)-1))
continue;
ret = timer_delete(timer_id[i]);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
pr_fail("%s: timer_delete failed for timer '%s', errno=%d (%s)\n",
args->name,
stress_clock_name(timers[i]),
errno, strerror(errno));
break;
}
}
}
#else
UNEXPECTED
#endif
#if defined(__linux__)
{
int fd;
fd = open("/dev/ptp0", O_RDWR);
if (fd >= 0) {
struct timespec t;
int ret, clkid = FD_TO_CLOCKID(fd);
#if defined(HAVE_POLL_H)
struct pollfd pollfds[1];
pollfds[0].fd = fd;
pollfds[0].events = POLLIN;
pollfds[0].revents = 0;
ret = poll(pollfds, 1, 0);
(void)ret;
#else
UNEXPECTED
#endif
ret = clock_gettime(clkid, &t);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EINVAL) && (errno != ENOSYS)) {
pr_fail("%s: clock_gettime failed for /dev/ptp0, errno=%d (%s)",
args->name, errno, strerror(errno));
}
#if defined(HAVE_CLOCK_GETRES)
ret = shim_clock_getres(clkid, &t);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EINVAL) && (errno != ENOSYS)) {
pr_fail("%s: clock_getres failed for /dev/ptp0, errno=%d (%s)",
args->name, errno, strerror(errno));
}
#else
UNEXPECTED
#endif
(void)close(fd);
}
}
#endif
inc_counter(args);
} while (keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return EXIT_SUCCESS;
}
stressor_info_t stress_clock_info = {
.stressor = stress_clock,
.class = CLASS_INTERRUPT | CLASS_OS,
.verify = VERIFY_OPTIONAL,
.help = help
};
#else
stressor_info_t stress_clock_info = {
.stressor = stress_not_implemented,
.class = CLASS_INTERRUPT | CLASS_OS,
.help = help
};
#endif