-
Notifications
You must be signed in to change notification settings - Fork 0
/
late.c
670 lines (562 loc) · 15.7 KB
/
late.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2002-2003, Jeffrey Roberson <[email protected]>
* All rights reserved.
* Copyright (c) 2024 The FreeBSD Foundation
*
* Portions of this software were developed by Olivier Certner
* <[email protected]> at Kumacom SARL under sponsorship from the FreeBSD
* Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/resource.h>
#ifdef __FreeBSD__
#include <sys/rtprio.h>
#endif
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#ifndef __FreeBSD__
#include <sched.h>
#endif
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
bool bflag; /* Settle before test */
bool cflag; /* Calibrate ? */
bool iflag; /* Iterate a specific number of times? */
bool nflag; /* Was '-n' specified? */
bool pflag; /* Print priority? */
bool uflag; /* Use SIGUSR1 to start the test after settling */
bool xflag; /* Print stats once a second. */
int niceval; /* Nice setting. */
int rsecs; /* Run for rsecs seconds. */
unsigned int settle_secs; /* Settle before test this many seconds */
/* Calibration. */
unsigned int leeway = 5; /* Percents of error allowed during calibration. */
unsigned int cmiter = 8; /* Max number of attempts during calibration. */
volatile sig_atomic_t start; /* Can we start? */
volatile sig_atomic_t done; /* Should we stop? */
/* Internal multiplicator for memcpy() iterations. */
#define INT_MEMCPY_ITERATIONS 4096
/*
* Interval sets.
*/
struct iset {
struct timeval is_max; /* Maximum timeval for this set. */
struct timeval is_total; /* Total time accumulated. */
int is_count; /* Number of recorded intervals. */
};
void is_init(struct iset *is);
void is_add(struct iset *is, struct timeval *tv);
void is_average(struct iset *is, struct timeval *tv);
void is_max(struct iset *is, struct timeval *tv);
void is_total(struct iset *is, struct timeval *tv);
void is_count(struct iset *is, int *count);
#ifndef timeradd
#define timeradd(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
if ((vvp)->tv_usec >= 1000000) { \
(vvp)->tv_sec++; \
(vvp)->tv_usec -= 1000000; \
} \
} while (0)
#endif
#ifndef timercmp
#define timercmp(tvp, uvp, cmp) \
(((tvp)->tv_sec == (uvp)->tv_sec) ? \
((tvp)->tv_usec cmp (uvp)->tv_usec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec))
#endif
#ifndef timersub
#define timersub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
if ((vvp)->tv_usec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \
} \
} while (0)
#endif
struct iset lat_set;
struct iset lat_cur_set;
struct iset work_set;
struct iset work_cur_set;
struct iset sleep_set;
void test_latency(unsigned int microseconds);
void test_latency_report(struct iset *is);
int test_prio(void);
void work_memcpy(unsigned int count);
void work_memcpy_calibrate(unsigned int microseconds);
void work_memcpy_report(struct iset *is);
void cpu_report(struct timeval *wtime);
void finished(int trash);
void usage(void);
void
is_init(struct iset *is)
{
memset(is, 0, sizeof(*is));
}
void
is_add(struct iset *is, struct timeval *tv)
{
is->is_count++;
/* Add this to the total */
timeradd(&is->is_total, tv, &is->is_total);
/* See if this value exceeds the max. */
if (timercmp(tv, &is->is_max, >))
is->is_max = *tv;
}
void
is_average(struct iset *is, struct timeval *tv)
{
uint64_t total;
if (is->is_count == 0) {
tv->tv_sec = tv->tv_usec = 0;
return;
}
total = is->is_total.tv_usec;
total += is->is_total.tv_sec * 1000000;
total /= is->is_count;
tv->tv_sec = total / 1000000;
total -= tv->tv_sec * 1000000;
tv->tv_usec = total;
}
void
is_max(struct iset *is, struct timeval *tv)
{
*tv = is->is_max;
}
void
is_count(struct iset *is, int *count)
{
*count = is->is_count;
}
void
is_total(struct iset *is, struct timeval *tv)
{
*tv = is->is_total;
}
void
tv_print(char *pre, struct timeval *tv)
{
printf("%s%ld.%06ld\n", pre, tv->tv_sec, tv->tv_usec);
}
void
finished(int trash)
{
done = 1;
}
void
sigalarm(int trash)
{
static int sigcount;
sigcount++;
alarm(1);
if (xflag) {
printf("Stats for second: %d\n", sigcount);
work_memcpy_report(&work_cur_set);
is_init(&work_cur_set);
test_latency_report(&lat_cur_set);
is_init(&lat_cur_set);
}
if (pflag)
printf("Current priority:\t%d\n", test_prio());
printf("\n");
}
void
started(int trash)
{
start = 1;
}
void
usage(void)
{
fprintf(stderr, "usage: late [-pux] [-a max calibration attempts] "
"[-b settle seconds] [-c work us] [-i work loops]\n"
" [-l calibration leeway percents] [-n niceval] "
"[-r run seconds] [-s sleep us] [-w work iterations]\n"
"Options:\n"
"-a: Max calibration attempts (=feedback loop iterations; "
"default: 8).\n"
"-b: Wait before the test to let priority settle.\n"
"-c: Calibrate: Find work iterations to reach the passed duration.\n"
"-i: Number of work + sleep loops (not specified: Infinite).\n"
"-l: Leeway percents for the calibration (default: 5).\n"
"-n: Renice to the passed value (may need privilege)."
"-p: Print the current process' priority every second.\n"
"-r: Stop running (work + sleep) when duration reached.\n"
"-s: Duration of sleep (in us; default: 1s).\n"
"-u: Wait for SIGUSR1 to start (work + sleep) loops.\n"
"-w: Number of iterations forming a unit of work.\n"
"-x: Print work and latency statistics every second.\n");
exit(EXIT_FAILURE);
}
static unsigned int
str_to_u(const char *str)
{
char *endptr;
unsigned long ul;
errno = 0;
ul = strtoul(str, &endptr, 0);
if (errno != 0 || endptr == str || *endptr != 0 || ul > UINT_MAX)
errx(EXIT_FAILURE, "Bad number trying to parse '%s'.", str);
return (ul);
}
int
main(int argc, char **argv)
{
struct timeval wetime; /* Work end time */
struct timeval wstime; /* Work start time */
struct timeval curtime; /* Current time. */
int smicro; /* Microseconds of sleep */
bool wflag = false;
unsigned int wmicro; /* Microseconds of work */
unsigned int wcount; /* work count */
int icount; /* Iteration count. */
int c;
int error;
smicro = 1000000; /* 1 second default */
while ((c = getopt(argc, argv, "a:b:c:i:l:n:pr:s:uw:x")) != -1) {
switch (c) {
case 'a':
cmiter = str_to_u(optarg);
break;
case 'b':
settle_secs = str_to_u(optarg);
break;
case 'c':
cflag = true;
wmicro = str_to_u(optarg);
break;
case 'i':
iflag = true;
icount = atoi(optarg);
break;
case 'l':
leeway = str_to_u(optarg);
if (leeway > 100)
errx(EXIT_FAILURE,
"Leeway must be a number of percents.");
break;
case 'n':
nflag = true;
niceval = atoi(optarg);
break;
case 'p':
pflag = true;
break;
case 'r':
rsecs = atoi(optarg);
break;
case 's':
smicro = atoi(optarg);
break;
case 'u':
uflag = true;
break;
case 'w':
wflag = 1;
wcount = str_to_u(optarg);
break;
case 'x':
xflag = true;
break;
default:
usage();
}
}
if (!cflag && !wflag)
errx(EXIT_FAILURE,
"Expected work iterations (through '-w'; else "
"calibrate with '-c').");
if (cflag) {
#ifdef __FreeBSD__
struct rtprio rtp = { .type = RTP_PRIO_FIFO,
.prio = RTP_PRIO_MAX };
error = rtprio(RTP_SET, 0, &rtp);
if (error != 0)
error = setpriority(PRIO_PROCESS, 0, PRIO_MIN);
#else
error = setpriority(PRIO_PROCESS, 0, PRIO_MIN);
#endif
if (error != 0)
warnx("Could not increase priority, "
"calibration results may be less reliable.");
work_memcpy_calibrate(wmicro);
exit(EXIT_SUCCESS);
}
/*
* Initialize our four interval sets.
*/
is_init(&lat_set);
is_init(&lat_cur_set);
is_init(&work_set);
is_init(&work_cur_set);
if (uflag) {
sigset_t empty_set;
sigset_t usr1_set;
sigset_t cur_set;
sigemptyset(&empty_set);
sigemptyset(&usr1_set);
sigaddset(&usr1_set, SIGUSR1);
sigprocmask(SIG_BLOCK, &usr1_set, &cur_set);
signal(SIGUSR1, started);
while (start == 0)
sigsuspend(&cur_set);
}
signal(SIGINT, finished);
signal(SIGALRM, sigalarm);
/* Record the time that we start, for the total work time */
if (gettimeofday(&wstime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
/* Sleep to let the priority settle before test */
if (settle_secs != 0) {
struct timeval tv;
sleep(settle_secs);
/*
* We want the amount of time that we were denied slices before
* we woke up to be reflected in the wstime. This is why we
* don't just start the timer below.
*/
tv.tv_sec = settle_secs;
tv.tv_usec = 0;
timeradd(&wstime, &tv, &wstime);
}
if (xflag || pflag)
alarm(1);
if (nflag) {
error = setpriority(PRIO_PROCESS, 0, niceval);
if (error != 0)
err(EXIT_FAILURE, "Cannot set the nice value.");
}
while (done == 0 && (!iflag || icount--)) {
if (wflag)
work_memcpy(wcount);
if (done == 0 && smicro)
test_latency(smicro);
if (rsecs) {
gettimeofday(&curtime, NULL);
curtime.tv_sec -= rsecs;
if (timercmp(&wstime, &curtime, <))
break;
}
}
/* Compute the total working time */
if (gettimeofday(&wetime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
timersub(&wetime, &wstime, &wetime);
/* Generate reports */
test_latency_report(&lat_set);
work_memcpy_report(&work_set);
cpu_report(&wetime);
exit(EXIT_SUCCESS);
}
void
work_memcpy_calibrate(unsigned int micro)
{
struct timeval stime; /* Start time */
struct timeval etime; /* End time */
unsigned int rmicro; /* Current run time */
unsigned int count;
unsigned int niter;
rmicro = 0;
count = 10000;
niter = 0;
#define SCALE 128
if (micro * SCALE * 100 <= micro)
errx(EXIT_FAILURE, "Too long duration requested.");
while (rmicro == 0 || rmicro < (micro * (100 - leeway) *
(SCALE - 1) / (SCALE * 100)) ||
rmicro > (micro * (100 + leeway) *
(SCALE + 1) / (SCALE * 100))) {
if (niter++ == cmiter)
errx(EXIT_FAILURE,
"Reached calibration attempts limit (%u). "
"Change with '-a', and/or use '-l'.", cmiter);
if (gettimeofday(&stime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
work_memcpy(count);
if (gettimeofday(&etime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
/* Figure out how long we worked for */
timersub(&etime, &stime, &etime);
rmicro = (etime.tv_sec * 1000000) + etime.tv_usec;
printf("%u iterations took %u microseconds.\n",
count, rmicro);
if (rmicro == 0) {
unsigned int new_count = 2 * count;
if (count >= UINT_MAX / SCALE)
goto too_many_iter;
if (new_count * SCALE <= count)
count = UINT_MAX / SCALE;
else
count = new_count;
} else {
printf("(%u * %u) / %u = %u\n", count, SCALE,
rmicro, (count * SCALE) / rmicro);
count = (((count * SCALE) / rmicro) * micro) / SCALE;
if (count == 0)
errx(EXIT_FAILURE,
"Requested duration too short.");
if (count >= UINT_MAX / SCALE)
goto too_many_iter;
}
};
printf("Calibration succeeded after %u iterations.\n", niter);
printf("Calculated count: %u\n", count);
return;
too_many_iter:
errx(EXIT_FAILURE,
"Calibration failed, too many iterations would be needed.");
}
void
work_memcpy(unsigned int count)
{
struct timeval stime; /* Start time */
struct timeval etime; /* End time */
struct timeval dtime; /* Difference between the above */
char buf0[4096];
char buf1[4096];
if (gettimeofday(&stime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
for (unsigned int i = 0; i != count; ++i)
for (unsigned int j = 0; j != INT_MEMCPY_ITERATIONS; ++j) {
memcpy(buf0, buf1, 4096);
asm volatile ("" ::: "memory");
memcpy(buf1, buf0, 4096);
}
if (gettimeofday(&etime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
/* Figure out how long we ran for */
timersub(&etime, &stime, &dtime);
is_add(&work_set, &dtime);
is_add(&work_cur_set, &dtime);
}
void
work_memcpy_report(struct iset *is)
{
struct timeval tv;
int count;
printf("Time executing work loop:\n");
is_max(is, &tv);
tv_print("\tMax:\t\t", &tv);
is_average(is, &tv);
tv_print("\tAverage:\t", &tv);
is_count(is, &count);
printf("\tWork Count:\t%d\n", count);
}
void
cpu_report(struct timeval *wtime)
{
struct timeval cputime;
struct timeval tv;
struct rusage ru;
uint64_t rmicro;
uint64_t wmicro;
double pct;
if (getrusage(RUSAGE_SELF, &ru) != 0)
err(EXIT_FAILURE, "getrusage");
printf("CPU Stats:\n");
tv_print("\tReal Time:\t", wtime);
timeradd(&ru.ru_utime, &ru.ru_stime, &cputime);
tv_print("\tCPU Time:\t", &cputime);
is_total(&sleep_set, &tv);
tv_print("\tSleep Time:\t", &tv);
rmicro = (cputime.tv_sec * 1000000) + cputime.tv_usec;
wmicro = (wtime->tv_sec * 1000000) + wtime->tv_usec;
pct = ((double)rmicro / (double)wmicro) * 100;
printf("\t%%CPU:\t\t%.0f\n", pct);
printf("\tFinal Priority:\t%d\n", test_prio());
printf("\tNice setting:\t%d\n", niceval);
printf("\tVoluntary Ctx Switch:\t%ld\n", ru.ru_nvcsw);
printf("\tInvoluntary Ctx Switch:\t%ld\n", ru.ru_nivcsw);
}
void
test_latency(unsigned int microseconds)
{
struct timeval stime; /* Start time */
struct timeval etime; /* End time */
struct timeval dtime; /* Difference between the above */
struct timeval utime; /* Represents usleep time */
struct timespec ts;
if (gettimeofday(&stime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
ts.tv_sec = microseconds / 1000000;
ts.tv_nsec = (microseconds - (ts.tv_sec * 1000000)) * 1000;
while (nanosleep(&ts, &ts) != 0);
if (gettimeofday(&etime, NULL) != 0)
err(EXIT_FAILURE, "gettimeofday");
/* Figure out how long we slept for */
timersub(&etime, &stime, &dtime);
/* Add this to the total time spent sleeping. */
is_add(&sleep_set, &dtime);
/* Now subtract how long we should have slept for */
utime.tv_sec = 0;
utime.tv_usec = microseconds;
timersub(&dtime, &utime, &dtime);
/* Add this to the total */
is_add(&lat_set, &dtime);
is_add(&lat_cur_set, &dtime);
}
void
test_latency_report(struct iset *is)
{
struct timeval tv;
int count;
printf("Sleep resumption latency:\n");
is_max(is, &tv);
tv_print("\tMax:\t\t", &tv);
is_average(is, &tv);
tv_print("\tAverage:\t", &tv);
is_count(is, &count);
printf("\tSleep Count:\t%d\n", count);
}
int
test_prio()
{
int priority;
#ifdef __FreeBSD__
struct rtprio rtp;
if (rtprio(RTP_LOOKUP, 0, &rtp) != 0)
err(EXIT_FAILURE, "rtprio");
priority = rtp.prio;
#else
struct sched_param sp;
if (sched_getparam(0, &sp) != 0)
err(EXIT_FAILURE, "sched_getparam");
priority = sp.sched_priority;
#endif
return (priority);
}