forked from luceracloud/libfasttime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasttime_test.c
498 lines (414 loc) · 10.8 KB
/
fasttime_test.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
/*
* Copyright 2015 Lucera Financial Infrastructure, LLC
*
* This software may be modified and distributed under the terms of
* the MIT license. See the LICENSE file for details.
*/
/*
* This file contains tests to verify that libfasttime is not doing
* anything horribly wrong. It contains short tests for things that
* can be verified quickly and long-running tests for bugs which take
* physical time to manifest like divergence between the system clock
* and the local libfasttime clock (base_* variables).
*/
#include <assert.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef __sun
#include <sys/processor.h>
#include <sys/procset.h>
#endif
#ifdef __linux
#include <sched.h>
#endif
#ifdef __linux
#define MICROSEC 1000000
#define NANOSEC 1000000000
typedef int processorid_t;
#endif
#define MS_TO_NS(ms) (ms * 1000000)
#define TIMESPEC_TO_NS(ts) (((uint64_t)ts.tv_sec * NANOSEC) + ts.tv_nsec)
#define TIMEVAL_TO_US(tv) (((uint64_t)tv.tv_sec * MICROSEC) + tv.tv_usec);
enum tvh_types {
TVH_SYS = 0,
TVH_FT,
TVH_TYPES
};
#define TVH_HIST_SIZE 10
#define TVH_IDX_INCR(idx) (idx = ((idx + 1) % (TVH_HIST_SIZE + 1)))
/*
* A timeval history structure for tracking the last TVH_HIST_SIZE
* timevals reported. Used for tests that track local clock divergence
* over time. Implemented as a circular array.
*/
typedef struct tv_hist {
int tvh_hidx; /* head index */
int tvh_tidx; /* tail index */
/* Array of timevals, one per history type. */
struct timeval tvh_list[TVH_TYPES][TVH_HIST_SIZE + 1];
} tvh_t;
static tvh_t tvhist;
void add_to_hist(struct timeval sys_tv, struct timeval ft_tv, tvh_t *tvhist);
void print_hist(tvh_t *tvhist);
/*
* Pointers to system functions, loaded by libfasttime.so.
*/
extern int (*_sys_clock_gettime)(clockid_t clock_id, struct timespec *tp);
#ifdef __sun
extern int (*_sys_gettimeofday)(struct timeval *tp, void *tzp);
#elif __linux
extern int (*_sys_gettimeofday)(struct timeval *tp, struct timezone *tz);
#endif
#ifdef __sun
/*
* Get the active CPUs, output via the cpus array. On input size
* represents the size of cpus, on output it represents the number of
* CPUs active. On success 0 is returned.
*/
int
get_cpus(processorid_t **cpus, size_t *size)
{
int num_cpus = sysconf(_SC_CPUID_MAX);
processorid_t i, j;
if ((*cpus = calloc(sizeof (processorid_t), num_cpus)) == NULL) {
perror("failed to calloc()");
}
for (i = 0, j = 0; i < num_cpus; i++) {
if (p_online(i, P_STATUS) != -1)
(*cpus)[j++] = i;
}
*size = j;
return (0);
}
#elif __linux
int
get_cpus(processorid_t **cpus, size_t *size)
{
int num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
processorid_t i;
if ((*cpus = calloc(sizeof (processorid_t), num_cpus)) == NULL) {
perror("failed to calloc()");
}
for (i = 0; i < num_cpus; i++) {
(*cpus)[i] = i;
}
*size = i;
return (0);
}
#endif
/*
* Verify that the system TOD and local TOD are not too far out of
* sync.
*
* max_delta_us
*
* The maximum allowed divergence in microseconds between the
* system and local TOD.
*
* tvhist
*
* A bounded history of previous timevals. Used to log timevals
* that lead up to a failure.
*
* consec_over
*
* The number of consecutive invocations in which the divergence
* was greater than max_delta_us.
*
* Return -1 if a test or runtime failure occurs.
*
*/
int
test_gettimeofday_delta(int64_t max_delta_us, tvh_t *tvhist, int *consec_over)
{
struct timeval ft_tv; /* fasttime time */
struct timeval sys_tv; /* system time */
uint64_t ft_us; /* fasttime micros */
uint64_t sys_us; /* system micros */
int64_t delta_us; /* absolute delta */
if (_sys_gettimeofday(&sys_tv, NULL) == -1) {
perror("failed to call system gettimeofday()\n");
return (-1);
}
if (gettimeofday(&ft_tv, NULL) == -1) {
perror("failed to call libfasttime gettimeofday()\n");
return (-1);
}
if (sys_tv.tv_sec < 0 || sys_tv.tv_usec < 0 ||
sys_tv.tv_usec >= 1000000) {
printf("ERROR: bad timeval from system\n");
printf("tv.sec: %ld tv.usec: %ld\n",
sys_tv.tv_sec, sys_tv.tv_usec);
return (-1);
}
if (ft_tv.tv_sec < 0 || ft_tv.tv_usec < 0 ||
ft_tv.tv_usec >= 1000000) {
printf("ERROR: bad timeval from libfasttime\n");
printf("tv.sec: %ld tv.usec: %ld\n",
ft_tv.tv_sec, ft_tv.tv_usec);
return (-1);
}
add_to_hist(sys_tv, ft_tv, tvhist);
ft_us = TIMEVAL_TO_US(ft_tv);
sys_us = TIMEVAL_TO_US(sys_tv);
delta_us = labs(sys_us - ft_us);
assert(delta_us >= 0);
if (delta_us > max_delta_us) {
*consec_over += 1;
if (*consec_over == 3) {
print_hist(tvhist);
return (-1);
}
return (0);
}
*consec_over = 0;
return (0);
}
void
add_to_hist(struct timeval sys_tv, struct timeval ft_tv, tvh_t *tvhist)
{
tvhist->tvh_list[TVH_SYS][tvhist->tvh_tidx] = sys_tv;
tvhist->tvh_list[TVH_FT][tvhist->tvh_tidx] = ft_tv;
TVH_IDX_INCR(tvhist->tvh_tidx);
if (tvhist->tvh_tidx == tvhist->tvh_hidx) {
TVH_IDX_INCR(tvhist->tvh_hidx);
}
}
void
print_hist(tvh_t *tvhist)
{
struct timeval sys_tv, ft_tv;
while (tvhist->tvh_hidx != tvhist->tvh_tidx) {
sys_tv = tvhist->tvh_list[TVH_SYS][tvhist->tvh_hidx];
ft_tv = tvhist->tvh_list[TVH_FT][tvhist->tvh_hidx];
printf("sys\tsec: %10ld usec: %7ld\n",
sys_tv.tv_sec, sys_tv.tv_usec);
printf("lib\tsec: %10ld usec: %7ld\n",
ft_tv.tv_sec, ft_tv.tv_usec);
printf("delta\tsec: %10ld usec: %7ld\n",
labs(sys_tv.tv_sec - ft_tv.tv_sec),
labs(sys_tv.tv_usec - ft_tv.tv_usec));
printf("\n");
TVH_IDX_INCR(tvhist->tvh_hidx);
}
}
void
test_posix_monotonic(const struct timespec *sleep)
{
struct timespec a_ts, b_ts;
uint64_t a_ns, b_ns;
if (clock_gettime(CLOCK_MONOTONIC, &a_ts) == -1) {
perror("failed to query monotonic clock");
exit(1);
}
if (sleep != NULL)
nanosleep(sleep, NULL);
if (clock_gettime(CLOCK_MONOTONIC, &b_ts) == -1) {
perror("failed to query monotonic clock");
exit(1);
}
a_ns = TIMESPEC_TO_NS(a_ts);
b_ns = TIMESPEC_TO_NS(b_ts);
/*
* If the current time is less than or equal to the previous
* time than monotonicity was not held.
*/
if (b_ns <= a_ns) {
printf("ERROR: test_posix_monotonic() failed\n");
printf("\ta_ns: %" PRIu64 "\n", a_ns);
printf("\tb_ns: %" PRIu64 "\n", b_ns);
exit(1);
}
}
#ifdef __sun
void
test_posix_xcore(processorid_t cpus[], size_t num_cpus)
{
processorid_t a_cpu, b_cpu;
uint64_t a_ns, b_ns;
struct timespec a_ts, b_ts;
a_cpu = getcpuid();
/* XXX this will loop forever on single CPU */
do {
/*
* I realize this is only using the bottom bits and
* therefore skews the distribution but that's quite
* alright for the purposes of this test.
*/
b_cpu = cpus[rand() % num_cpus];
} while (a_cpu == b_cpu);
if (processor_bind(P_PID, P_MYID, a_cpu, NULL) == -1) {
perror("failed to bind process");
exit(1);
}
if (clock_gettime(CLOCK_MONOTONIC, &a_ts) == -1) {
perror("failed to query monotonic clock");
exit(1);
}
if (processor_bind(P_PID, P_MYID, b_cpu, NULL) == -1) {
perror("failed to bind process");
exit(1);
}
if (clock_gettime(CLOCK_MONOTONIC, &b_ts) == -1) {
perror("failed to query monotonic clock");
exit(1);
}
a_ns = TIMESPEC_TO_NS(a_ts);
b_ns = TIMESPEC_TO_NS(b_ts);
/*
* If the current time is less than or equal to the previous
* time than monotonicity was not held.
*/
if (b_ns <= a_ns) {
printf("ERROR: test_posix_monotonic() failed\n");
printf("\ta_ns: %" PRIu64 "\n", a_ns);
printf("\tb_ns: %" PRIu64 "\n", b_ns);
exit(1);
}
}
#elif __linux
void
test_posix_xcore(processorid_t cpus[], size_t __attribute__((unused)) num_cpus)
{
cpu_set_t cpuset;
processorid_t a_cpu, b_cpu;
uint64_t a_ns, b_ns;
struct timespec a_ts, b_ts;
/* XXX sched_getcpu() always returns same value even after binding */
/* if ((a_cpu = sched_getcpu()) == -1) { */
/* perror("failed to get current CPU"); */
/* exit(1); */
/* } */
/* XXX Either there is a bug in LX or something I don't
* understand about Linux. Use only have the online CPUs for
* now. */
a_cpu = cpus[rand() % 31];
/* XXX this will loop forever on single CPU */
do {
/*
* I realize this is only using the bottom bits and
* therefore skews the distribution but that's quite
* alright for the purposes of this test.
*/
b_cpu = cpus[rand() % 31];
} while (a_cpu == b_cpu);
CPU_ZERO(&cpuset);
CPU_SET(a_cpu, &cpuset);
if (sched_setaffinity(0, sizeof (cpu_set_t), &cpuset) == -1) {
perror("failed to bind process");
exit(1);
}
if (clock_gettime(CLOCK_MONOTONIC, &a_ts) == -1) {
perror("failed to query monotonic clock");
exit(1);
}
CPU_ZERO(&cpuset);
CPU_SET(b_cpu, &cpuset);
if (sched_setaffinity(0, sizeof (cpu_set_t), &cpuset) == -1) {
perror("failed to bind process");
exit(1);
}
if (clock_gettime(CLOCK_MONOTONIC, &b_ts) == -1) {
perror("failed to query monotonic clock");
exit(1);
}
a_ns = TIMESPEC_TO_NS(a_ts);
b_ns = TIMESPEC_TO_NS(b_ts);
/*
* If the current time is less than or equal to the previous
* time than monotonicity was not held.
*/
if (b_ns <= a_ns) {
printf("ERROR: test_posix_monotonic() failed\n");
printf("\ta_ns: %" PRIu64 "\n", a_ns);
printf("\tb_ns: %" PRIu64 "\n", b_ns);
exit(1);
}
}
#endif
/*
* Run short tests. Each short tests is called back-to-back in rapid
* succession for the given number of iterations.
*/
void
run_short_tests(unsigned int iters)
{
unsigned int i;
processorid_t *cpus;
size_t cpus_size;
struct timespec ts;
int consec_over = 0;
for (i = 0; i < iters; i++) {
if (test_gettimeofday_delta(10, &tvhist, &consec_over) == -1) {
printf("ERROR: TOD delta too large\n");
exit(1);
}
}
for (i = 0; i < iters; i++) {
test_posix_monotonic(NULL);
}
for (i = 0; i < iters; i++) {
ts.tv_sec = 0;
ts.tv_nsec = MS_TO_NS(0 * i);
test_posix_monotonic(&ts);
}
get_cpus(&cpus, &cpus_size);
for (i = 0; i < iters; i++) {
test_posix_xcore(cpus, cpus_size);
}
}
/*
* Run the long tests which are the same as the short tests but run
* for the period of time specified by mins.
*/
void
run_long_tests(unsigned int mins)
{
unsigned int i;
unsigned int secs = mins * 60;
for (i = 0; i < secs; i++) {
run_short_tests(1000);
sleep(1);
}
}
int
main(int argc, char **argv)
{
int c, i;
unsigned int seed = 0;
unsigned int mins = 0;
struct timespec ts;
while ((c = getopt(argc, argv, ":l:")) != -1) {
switch (c) {
case 'l':
mins = atoi(optarg);
break;
case '?':
fprintf(stderr, "Unknown option: %c\n", c);
exit(1);
break;
case ':':
fprintf(stderr, "Option %c missing argument\n", c);
exit(1);
break;
}
}
/* XXX ability to pass seed as arg */
clock_gettime(CLOCK_REALTIME, &ts);
seed = (seed == 0) ? (unsigned int)ts.tv_nsec : seed;
srand(seed);
if (mins == 0) {
for (i = 0; i < 5; i++) {
run_short_tests(1000);
sleep(1);
}
} else {
run_long_tests(mins);
}
return (0);
}