1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Cyril Hrubis <[email protected]>
4 */
5
6 #include <sys/prctl.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <limits.h>
10 #include <string.h>
11
12 #define TST_NO_DEFAULT_MAIN
13 #include "tst_test.h"
14 #include "tst_clocks.h"
15 #include "tst_timer_test.h"
16
17 #define MAX_SAMPLES 500
18 #if defined (__arm__) || defined(__aarch64__)
19 #define BASE_THRESHOLD 500
20 #else
21 #define BASE_THRESHOLD 400
22 #endif
23
24 static const char *scall;
25 static void (*setup)(void);
26 static void (*cleanup)(void);
27 static int (*sample)(int clk_id, long long usec);
28 static struct tst_test *test;
29
30 static long long *samples;
31 static unsigned int cur_sample;
32 static unsigned int monotonic_resolution;
33 static unsigned int timerslack;
34 static int virt_env;
35
36 static char *print_frequency_plot;
37 static char *file_name;
38 static char *str_sleep_time;
39 static char *str_sample_cnt;
40 static int sleep_time = -1;
41 static int sample_cnt;
42
print_line(char c,int len)43 static void print_line(char c, int len)
44 {
45 while (len-- > 0)
46 fputc(c, stderr);
47 }
48
ceilu(float f)49 static unsigned int ceilu(float f)
50 {
51 if (f - (int)f > 0)
52 return (unsigned int)f + 1;
53
54 return (unsigned int)f;
55 }
56
flooru(float f)57 static unsigned int flooru(float f)
58 {
59 return (unsigned int)f;
60 }
61
bucket_len(unsigned int bucket,unsigned int max_bucket,unsigned int cols)62 static float bucket_len(unsigned int bucket, unsigned int max_bucket,
63 unsigned int cols)
64 {
65 return 1.00 * bucket * cols / max_bucket;
66 }
67
68 static const char *table_heading = " Time: us ";
69
70 /*
71 * Line Header: '10023 | '
72 */
header_len(long long max_sample)73 static unsigned int header_len(long long max_sample)
74 {
75 size_t l = 1;
76
77 while (max_sample/=10)
78 l++;
79
80 return MAX(strlen(table_heading) + 2, l + 3);
81 }
82
frequency_plot(void)83 static void frequency_plot(void)
84 {
85 unsigned int cols = 80;
86 unsigned int rows = 20;
87 unsigned int i, buckets[rows];
88 long long max_sample = samples[0];
89 long long min_sample = samples[cur_sample-1];
90 unsigned int line_header_len = header_len(max_sample);
91 unsigned int plot_line_len = cols - line_header_len;
92 unsigned int bucket_size;
93
94 memset(buckets, 0, sizeof(buckets));
95
96 /*
97 * We work with discrete data buckets smaller than 1 does not make
98 * sense as well as it's a good idea to keep buckets integer sized
99 * to avoid scaling artifacts.
100 */
101 bucket_size = MAX(1u, ceilu(1.00 * (max_sample - min_sample)/(rows-1)));
102
103 for (i = 0; i < cur_sample; i++) {
104 unsigned int bucket;
105 bucket = flooru(1.00 * (samples[i] - min_sample)/bucket_size);
106 buckets[bucket]++;
107 }
108
109 unsigned int max_bucket = buckets[0];
110 for (i = 1; i < rows; i++)
111 max_bucket = MAX(max_bucket, buckets[i]);
112
113 fprintf(stderr, "\n%*s| Frequency\n", line_header_len - 2, table_heading);
114
115 print_line('-', cols);
116 fputc('\n', stderr);
117
118 unsigned int l, r;
119
120 for (l = 0; l < rows; l++) {
121 if (buckets[l])
122 break;
123 }
124
125 for (r = rows-1; r > l; r--) {
126 if (buckets[r])
127 break;
128 }
129
130 for (i = l; i <= r; i++) {
131 float len = bucket_len(buckets[i], max_bucket, plot_line_len);
132
133 fprintf(stderr, "%*lli | ",
134 line_header_len - 3, min_sample + bucket_size*i);
135 print_line('*', len);
136
137 if ((len - (int)len) >= 0.5)
138 fputc('+', stderr);
139 else if ((len - (int)len) >= 0.25)
140 fputc('-', stderr);
141 else if (len < 0.25 && buckets[i])
142 fputc('.', stderr);
143
144 fputc('\n', stderr);
145 }
146
147 print_line('-', cols);
148 fputc('\n', stderr);
149
150 float scale = 1.00 * plot_line_len / max_bucket;
151
152 fprintf(stderr,
153 "%*uus | 1 sample = %.5f '*', %.5f '+', %.5f '-', non-zero '.'\n",
154 line_header_len - 5, bucket_size, scale, scale * 2, scale * 4);
155
156 fputc('\n', stderr);
157 }
158
tst_timer_sample(void)159 void tst_timer_sample(void)
160 {
161 samples[cur_sample++] = tst_timer_elapsed_us();
162 }
163
cmp(const void * a,const void * b)164 static int cmp(const void *a, const void *b)
165 {
166 const long long *aa = a, *bb = b;
167
168 return (*bb - *aa);
169 }
170
171 /*
172 * The threshold per one syscall is computed as a sum of:
173 *
174 * 400 or 500 us - accomodates for context switches, process
175 * migrations between CPUs on SMP, etc. Increase to
176 * 500 on arm/arm64 to allow for increased little CPU
177 * scheduling
178 * 2*monotonic_resolution - accomodates for granurality of the CLOCK_MONOTONIC
179 * slack_per_scall - max of 0.1% of the sleep capped on 100ms or
180 * current->timer_slack_ns, which is slack allowed
181 * in kernel
182 *
183 * The formula for slack_per_scall applies to select() and *poll*() syscalls,
184 * the futex and *nanosleep() use only the timer_slack_ns, so we are a bit
185 * less strict here that we could be for these two for longer sleep times...
186 *
187 * We also allow for outliners, i.e. add some number to the threshold in case
188 * that the number of iteration is small. For large enoung number of iterations
189 * outliners are discarded and averaged out.
190 */
compute_threshold(long long requested_us,unsigned int nsamples)191 static long long compute_threshold(long long requested_us,
192 unsigned int nsamples)
193 {
194 unsigned int slack_per_scall = MIN(100000LL, requested_us / 1000);
195
196 slack_per_scall = MAX(slack_per_scall, timerslack);
197
198 return (BASE_THRESHOLD + 2 * monotonic_resolution + slack_per_scall) * nsamples
199 + 3000/nsamples;
200 }
201
202 /*
203 * Returns number of samples to discard.
204 *
205 * We set it to either at least 1 if number of samples > 1 or 5%.
206 */
compute_discard(unsigned int nsamples)207 static unsigned int compute_discard(unsigned int nsamples)
208 {
209 if (nsamples == 1)
210 return 0;
211
212 return MAX(1u, nsamples / 20);
213 }
214
write_to_file(void)215 static void write_to_file(void)
216 {
217 unsigned int i;
218 FILE *f;
219
220 if (!file_name)
221 return;
222
223 f = fopen(file_name, "w");
224
225 if (!f) {
226 tst_res(TWARN | TERRNO,
227 "Failed to open '%s'", file_name);
228 return;
229 }
230
231 for (i = 0; i < cur_sample; i++)
232 fprintf(f, "%lli\n", samples[i]);
233
234 if (fclose(f)) {
235 tst_res(TWARN | TERRNO,
236 "Failed to close file '%s'", file_name);
237 }
238 }
239
240
241 /*
242 * Timer testing function.
243 *
244 * What we do here is:
245 *
246 * * Take nsamples measurements of the timer function, the function
247 * to be sampled is defined in the actual test.
248 *
249 * * We sort the array of samples, then:
250 *
251 * - look for outliners which are samples where the sleep time has exceeded
252 * requested sleep time by an order of magnitude and, at the same time, are
253 * greater than clock resolution multiplied by three.
254 *
255 * - check for samples where the call has woken up too early which is a plain
256 * old bug
257 *
258 * - then we compute truncated mean and compare that with the requested sleep
259 * time increased by a threshold
260 */
do_timer_test(long long usec,unsigned int nsamples)261 void do_timer_test(long long usec, unsigned int nsamples)
262 {
263 long long trunc_mean, median;
264 unsigned int discard = compute_discard(nsamples);
265 unsigned int keep_samples = nsamples - discard;
266 long long threshold = compute_threshold(usec, keep_samples);
267 int i;
268 int failed = 0;
269
270 tst_res(TINFO,
271 "%s sleeping for %llius %u iterations, threshold %.2fus",
272 scall, usec, nsamples, 1.00 * threshold / (keep_samples));
273
274 cur_sample = 0;
275 for (i = 0; i < (int)nsamples; i++) {
276 if (sample(CLOCK_MONOTONIC, usec)) {
277 tst_res(TINFO, "sampling function failed, exiting");
278 return;
279 }
280 }
281
282 qsort(samples, nsamples, sizeof(samples[0]), cmp);
283
284 write_to_file();
285
286 for (i = 0; samples[i] > 10 * usec && i < (int)nsamples; i++) {
287 if (samples[i] <= 3 * monotonic_resolution)
288 break;
289 }
290
291 if (i > 0) {
292 tst_res(TINFO, "Found %i outliners in [%lli,%lli] range",
293 i, samples[0], samples[i-1]);
294 }
295
296 for (i = nsamples - 1; samples[i] < usec && i > -1; i--);
297
298 if (i < (int)nsamples - 1) {
299 tst_res(TFAIL, "%s woken up early %u times range: [%lli,%lli]",
300 scall, nsamples - 1 - i,
301 samples[i+1], samples[nsamples-1]);
302 failed = 1;
303 }
304
305 median = samples[nsamples/2];
306
307 trunc_mean = 0;
308
309 for (i = discard; i < (int)nsamples; i++)
310 trunc_mean += samples[i];
311
312 tst_res(TINFO,
313 "min %llius, max %llius, median %llius, trunc mean %.2fus (discarded %u)",
314 samples[nsamples-1], samples[0], median,
315 1.00 * trunc_mean / keep_samples, discard);
316
317 if (virt_env) {
318 tst_res(TINFO,
319 "Virtualisation detected, skipping oversleep checks");
320 } else if (trunc_mean > (nsamples - discard) * usec + threshold) {
321 tst_res(TFAIL, "%s slept for too long", scall);
322
323 if (!print_frequency_plot)
324 frequency_plot();
325
326 failed = 1;
327 }
328
329 if (print_frequency_plot)
330 frequency_plot();
331
332 if (!failed)
333 tst_res(TPASS, "Measured times are within thresholds");
334 }
335
336 static void parse_timer_opts(void);
337
set_latency(void)338 static int set_latency(void)
339 {
340 int fd, latency = 0;
341
342 fd = open("/dev/cpu_dma_latency", O_WRONLY);
343 if (fd < 0)
344 return fd;
345
346 return write(fd, &latency, sizeof(latency));
347 }
348
timer_setup(void)349 static void timer_setup(void)
350 {
351 struct timespec t;
352 int ret;
353
354 if (setup)
355 setup();
356
357 /*
358 * Running tests in VM may cause timing issues, disable upper bound
359 * checks if any hypervisor is detected.
360 */
361 virt_env = tst_is_virt(VIRT_ANY);
362 tst_clock_getres(CLOCK_MONOTONIC, &t);
363
364 tst_res(TINFO, "CLOCK_MONOTONIC resolution %lins", (long)t.tv_nsec);
365
366 monotonic_resolution = t.tv_nsec / 1000;
367 timerslack = 50;
368
369 #ifdef PR_GET_TIMERSLACK
370 ret = prctl(PR_GET_TIMERSLACK);
371 if (ret < 0) {
372 tst_res(TINFO, "prctl(PR_GET_TIMERSLACK) = -1, using %uus",
373 timerslack);
374 } else {
375 timerslack = ret / 1000;
376 tst_res(TINFO, "prctl(PR_GET_TIMERSLACK) = %ius", timerslack);
377 }
378 #else
379 tst_res(TINFO, "PR_GET_TIMERSLACK not defined, using %uus",
380 timerslack);
381 #endif /* PR_GET_TIMERSLACK */
382 parse_timer_opts();
383
384 samples = SAFE_MALLOC(sizeof(long long) * MAX(MAX_SAMPLES, sample_cnt));
385 if (set_latency() < 0)
386 tst_res(TINFO, "Failed to set zero latency constraint: %m");
387 }
388
timer_cleanup(void)389 static void timer_cleanup(void)
390 {
391 free(samples);
392
393 if (cleanup)
394 cleanup();
395 }
396
397 static struct tst_timer_tcase {
398 long long usec;
399 unsigned int samples;
400 } tcases[] = {
401 {1000, 500},
402 {2000, 500},
403 {5000, 300},
404 {10000, 100},
405 {25000, 50},
406 {100000, 10},
407 {1000000, 2},
408 };
409
timer_test_fn(unsigned int n)410 static void timer_test_fn(unsigned int n)
411 {
412 do_timer_test(tcases[n].usec, tcases[n].samples);
413 }
414
single_timer_test(void)415 static void single_timer_test(void)
416 {
417 do_timer_test(sleep_time, sample_cnt);
418 }
419
420 static struct tst_option options[] = {
421 {"p", &print_frequency_plot, "-p Print frequency plot"},
422 {"s:", &str_sleep_time, "-s us Sleep time"},
423 {"n:", &str_sample_cnt, "-n uint Number of samples to take"},
424 {"f:", &file_name, "-f fname Write measured samples into a file"},
425 {NULL, NULL, NULL}
426 };
427
parse_timer_opts(void)428 static void parse_timer_opts(void)
429 {
430 size_t i;
431 long long runtime_us = 0;
432
433 if (str_sleep_time) {
434 if (tst_parse_int(str_sleep_time, &sleep_time, 0, INT_MAX)) {
435 tst_brk(TBROK,
436 "Invalid sleep time '%s'", str_sleep_time);
437 }
438 }
439
440 if (str_sample_cnt) {
441 if (tst_parse_int(str_sample_cnt, &sample_cnt, 1, INT_MAX)) {
442 tst_brk(TBROK,
443 "Invalid sample count '%s'", str_sample_cnt);
444 }
445 }
446
447 if (str_sleep_time || str_sample_cnt) {
448 if (sleep_time < 0)
449 sleep_time = 10000;
450
451 if (!sample_cnt)
452 sample_cnt = 500;
453
454 runtime_us = sleep_time * sample_cnt;
455
456 test->test_all = single_timer_test;
457 test->test = NULL;
458 test->tcnt = 0;
459 } else {
460 for (i = 0; i < ARRAY_SIZE(tcases); i++)
461 runtime_us += tcases[i].usec * tcases[i].samples;
462 }
463
464 tst_set_max_runtime((runtime_us + runtime_us/10)/1000000);
465 }
466
tst_timer_test_setup(struct tst_test * timer_test)467 struct tst_test *tst_timer_test_setup(struct tst_test *timer_test)
468 {
469 setup = timer_test->setup;
470 cleanup = timer_test->cleanup;
471 scall = timer_test->scall;
472 sample = timer_test->sample;
473
474 timer_test->scall = NULL;
475 timer_test->setup = timer_setup;
476 timer_test->cleanup = timer_cleanup;
477 timer_test->test = timer_test_fn;
478 timer_test->tcnt = ARRAY_SIZE(tcases);
479 timer_test->sample = NULL;
480 timer_test->options = options;
481
482 test = timer_test;
483
484 return timer_test;
485 }
486