-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.c
824 lines (756 loc) · 20.7 KB
/
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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*
* clockperf
*
* Copyright (c) 2016-2021, Steven Noonan <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "prefix.h"
#include "clock.h"
#include <assert.h>
#include <limits.h>
struct clockspec tsc_ref_clock = { CPERF_NONE, 0 };
struct clockspec ref_clock = { CPERF_NONE, 0 };
/*
* Choices for ref_clock in order of preference, from best to worst.
*/
static struct clockspec ref_clock_choices[] = {
#ifdef HAVE_CPU_CLOCK
/* Not safe to use TSC as a reference clock, unless we can empirically verify
* it's trustworthy. e.g. are TSCs on all cores synced?
*
* TODO: Add a CPU clock sanity test.
*/
#if 0
{CPERF_TSC, 0},
#endif
#endif
#ifdef TARGET_OS_WINDOWS
#if _WIN32_WINNT >= 0x0602
{CPERF_GETSYSTIMEPRECISE, 0},
#endif
{CPERF_QUERYPERFCOUNTER, 0},
#endif
#ifdef HAVE_CLOCK_GETTIME
#ifdef CLOCK_MONOTONIC
{CPERF_GETTIME, CLOCK_MONOTONIC},
#endif
{CPERF_GETTIME, CLOCK_REALTIME},
#endif
#ifdef HAVE_GETTIMEOFDAY
{CPERF_GTOD, 0},
#endif
{CPERF_NONE, 0}
};
static struct clockspec wall_clock_choices[] = {
#ifdef TARGET_OS_WINDOWS
#if _WIN32_WINNT >= 0x0602
{CPERF_GETSYSTIMEPRECISE, 0},
#endif
#endif
#ifdef HAVE_CLOCK_GETTIME
{CPERF_GETTIME, CLOCK_REALTIME},
#endif
#ifdef HAVE_GETTIMEOFDAY
{CPERF_GTOD, 0},
#endif
{CPERF_NONE, 0}
};
static int choose_ref_clock(struct clockspec *ref, struct clockspec *choices, struct clockspec for_clock)
{
int i;
uint64_t ctr_last = 0, ctr_now = 0;
struct clockspec *spec = choices;
#ifdef _DEBUG
fprintf(stderr, "trying to choose reference clock for %s\n", clock_name(for_clock));
#endif
while (spec && spec->major != CPERF_NONE)
{
if (memcmp(spec, &for_clock, sizeof(struct clockspec)) == 0) {
/* You can't use the same clock as a reference for itself. */
goto fail;
}
#ifdef _DEBUG
fprintf(stderr, "trying %s\n", clock_name(*spec));
#endif
if (clock_read(*spec, &ctr_last) || !ctr_last) {
/* This clock failed. Try the next one. */
#ifdef _DEBUG
fprintf(stderr, " failed initial read\n");
#endif
goto fail;
}
/* Quick sanity check to ensure clock is advancing monotonically. */
for (i = 0; i < 100; i++) {
if (clock_read(*spec, &ctr_now) || !ctr_now) {
/* Clock failed? If this didn't happen on the first attempt,
* then it really shouldn't happen at all. But checking this
* anyway.
*/
#ifdef _DEBUG
fprintf(stderr, " failed read %d\n", i + 1);
#endif
goto fail;
}
if (ctr_now < ctr_last) {
/* Not monotonic, try the next clock. */
#ifdef _DEBUG
fprintf(stderr, " not monotonic at read %d (%" PRIu64 " < %" PRIu64 ")\n", i + 1, ctr_now, ctr_last);
#endif
goto fail;
}
ctr_last = ctr_now;
}
/* This clock seems to be working and sane. */
*ref = *spec;
#ifdef _DEBUG
fprintf(stderr, "chose %s as reference clock\n", clock_name(*ref));
#endif
return 0;
fail:
spec++;
}
fprintf(stderr, "Could not choose a reference clock for %s! Aborting.\n",
clock_name(for_clock));
abort();
return 1;
}
#if defined(TARGET_CPU_X86)
static const char *cpu_clock_name(void)
{
return "tsc";
}
void cpu_clock_init(void)
{
}
static INLINE uint64_t cpu_clock_read(void)
{
uint32_t lo, hi;
#ifdef TARGET_COMPILER_MSVC
__asm {
rdtsc
mov lo, eax
mov hi, edx
}
#else
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
#endif
return ((uint64_t) hi << 32ULL) | lo;
}
#elif defined(TARGET_CPU_X86_64)
static const char *cpu_clock_name(void)
{
return "tsc";
}
void cpu_clock_init(void)
{
}
static INLINE uint64_t cpu_clock_read(void)
{
#ifdef TARGET_COMPILER_MSVC
return __rdtsc();
#else
uint32_t lo, hi;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t) hi << 32ULL) | lo;
#endif
}
#elif defined(TARGET_CPU_ARM) && TARGET_CPU_BITS == 64
#ifdef _MSC_VER
#include <intrin.h>
#pragma intrinsic(_ReadStatusReg)
#ifndef ARM64_CNTVCT
#define ARM64_CNTVCT ARM64_SYSREG(3, 3, 14, 0, 2)
#endif
#ifndef ARM64_CNTFRQ
#define ARM64_CNTFRQ ARM64_SYSREG(3, 3, 14, 0, 0)
#endif
#endif
#ifdef HAVE_KNOWN_TSC_FREQUENCY
static uint64_t cntfrq_el0;
#endif
static const char *cpu_clock_name(void)
{
return "cntvct";
}
void cpu_clock_init(void)
{
#ifdef HAVE_KNOWN_TSC_FREQUENCY
#ifdef _MSC_VER
cntfrq_el0 = _ReadStatusReg(ARM64_CNTFRQ);
#else
unsigned long long cval;
asm volatile("mrs %0, cntfrq_el0"
: "=r"(cval));
cntfrq_el0 = cval;
#endif
assert(cntfrq_el0 != 0);
#endif
}
static INLINE uint64_t cpu_clock_read()
{
#ifdef _MSC_VER
return _ReadStatusReg(ARM64_CNTVCT);
#else
unsigned long long cval;
asm volatile("mrs %0, cntvct_el0"
: "=r"(cval));
return cval;
#endif
}
#elif defined(TARGET_CPU_PPC)
/* Indirect stringification. Doing two levels allows the parameter to be
* a macro itself. For example, compile with -DFOO=bar, __stringify(FOO)
* converts to "bar".
*/
#define __stringify_1(x...) #x
#define __stringify(x...) __stringify_1(x)
#define mfspr(rn) ({ \
uint32_t rval; \
asm volatile("mfspr %0," __stringify(rn) \
: "=r" (rval)); rval; \
})
#define SPRN_TBRL 0x10C /* Time Base Register Lower */
#define SPRN_TBRU 0x10D /* Time Base Register Upper */
#define SPRN_ATBL 0x20E /* Alternate Time Base Lower */
#define SPRN_ATBU 0x20F /* Alternate Time Base Upper */
static int supports_atb = 0;
static INLINE uint64_t cpu_clock_read(void)
{
uint64_t result = 0;
uint32_t upper, lower, tmp;
do {
if (supports_atb) {
upper = mfspr(SPRN_ATBU);
lower = mfspr(SPRN_ATBL);
tmp = mfspr(SPRN_ATBU);
} else {
upper = mfspr(SPRN_TBRU);
lower = mfspr(SPRN_TBRL);
tmp = mfspr(SPRN_TBRU);
}
} while (tmp != upper);
result = upper;
result = result << 32;
result = result | lower;
return result;
}
static void atb_child(void)
{
supports_atb = 1;
cpu_clock_read();
_exit(0);
}
static const char *cpu_clock_name(void)
{
return supports_atb ? "atb" : "tbr";
}
void cpu_clock_init(void)
{
pid_t pid;
pid = fork();
if (!pid)
atb_child();
else if (pid != -1) {
int status;
pid = wait(&status);
if (pid != -1 && WIFEXITED(status))
supports_atb = 1;
}
}
#else
void cpu_clock_init(void)
{
}
#endif
#ifdef HAVE_CPU_CLOCK
static unsigned long long cycles_per_msec;
static unsigned long long cycles_start;
static unsigned long long clock_mult;
static unsigned long long max_cycles_mask;
static unsigned long long nsecs_for_max_cycles;
static unsigned int clock_shift;
static unsigned int max_cycles_shift;
#define MAX_CLOCK_SEC 60*60
#define NR_TIME_ITERS 50
static unsigned long get_cycles_per_msec(void)
{
#if defined(HAVE_KNOWN_TSC_FREQUENCY) && defined(TARGET_CPU_ARM) && TARGET_CPU_BITS == 64
return cntfrq_el0 / 1000;
#else
uint64_t wc_s, wc_e;
uint64_t c_s, c_e;
uint64_t elapsed;
if (clock_read(tsc_ref_clock, &wc_s)) {
fprintf(stderr, "Reference clock '%s' died while measuring TSC frequency\n",
clock_name(tsc_ref_clock));
abort();
}
c_s = cpu_clock_read();
do {
if (clock_read(tsc_ref_clock, &wc_e)) {
fprintf(stderr, "Reference clock '%s' died while measuring TSC frequency\n",
clock_name(tsc_ref_clock));
abort();
}
c_e = cpu_clock_read();
elapsed = wc_e - wc_s;
if (elapsed >= 1280000ULL) {
break;
}
} while (1);
return (c_e - c_s) * 1000000 / elapsed;
#endif
}
#ifndef min
#define min(x,y) ({ \
__typeof__(x) _x = (x); \
__typeof__(y) _y = (y); \
(void) (&_x == &_y); \
_x < _y ? _x : _y; })
#endif
#ifndef max
#define max(x,y) ({ \
__typeof__(x) _x = (x); \
__typeof__(y) _y = (y); \
(void) (&_x == &_y); \
_x > _y ? _x : _y; })
#endif
static void cpu_clock_init_ref(void)
{
struct clockspec for_clock = {CPERF_TSC, 0};
choose_ref_clock(&tsc_ref_clock, ref_clock_choices, for_clock);
}
void cpu_clock_calibrate(void)
{
double delta, mean, S;
uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
int i, samples, sft = 0;
unsigned long long tmp, max_ticks, max_mult;
cpu_clock_init_ref();
cycles[0] = get_cycles_per_msec();
S = delta = mean = 0.0;
for (i = 0; i < NR_TIME_ITERS; i++) {
cycles[i] = get_cycles_per_msec();
delta = cycles[i] - mean;
if (delta) {
mean += delta / (i + 1.0);
S += delta * (cycles[i] - mean);
}
}
/*
* The most common platform clock breakage is returning zero
* indefinitely. Check for that and return failure.
*/
if (!cycles[0] && !cycles[NR_TIME_ITERS - 1]) {
fprintf(stderr, "CPU clock calibration failed!\n");
abort();
}
S = sqrt(S / (NR_TIME_ITERS - 1.0));
minc = ~0ULL;
maxc = samples = avg = 0;
for (i = 0; i < NR_TIME_ITERS; i++) {
double this = (double)(cycles[i]);
minc = min(cycles[i], minc);
maxc = max(cycles[i], maxc);
if ((fmax(this, mean) - fmin(this, mean)) > S)
continue;
samples++;
avg += cycles[i];
}
S /= (double) NR_TIME_ITERS;
avg /= samples;
cycles_per_msec = avg;
max_ticks = MAX_CLOCK_SEC * cycles_per_msec * 1000ULL;
max_mult = ULLONG_MAX / max_ticks;
/*
* Find the largest shift count that will produce
* a multiplier that does not exceed max_mult
*/
tmp = max_mult * cycles_per_msec / 1000000;
while (tmp > 1) {
tmp >>= 1;
sft++;
}
clock_shift = sft;
clock_mult = (1ULL << sft) * 1000000 / cycles_per_msec;
/*
* Find the greatest power of 2 clock ticks that is less than the
* ticks in MAX_CLOCK_SEC_2STAGE
*/
max_cycles_shift = max_cycles_mask = 0;
tmp = MAX_CLOCK_SEC * 1000ULL * cycles_per_msec;
while (tmp > 1) {
tmp >>= 1;
max_cycles_shift++;
}
/*
* if use use (1ULL << max_cycles_shift) * 1000 / cycles_per_msec
* here we will have a discontinuity every
* (1ULL << max_cycles_shift) cycles
*/
nsecs_for_max_cycles = ((1ULL << max_cycles_shift) * clock_mult)
>> clock_shift;
/* Use a bitmask to calculate ticks % (1ULL << max_cycles_shift) */
for (tmp = 0; tmp < max_cycles_shift; tmp++)
max_cycles_mask |= 1ULL << tmp;
cycles_start = cpu_clock_read();
}
#else
void cpu_clock_calibrate(void)
{
}
#endif
/* Read a clock, in nanoseconds. */
int clock_read(struct clockspec spec, uint64_t *output)
{
static uint64_t CLOCK_RATIO = 1000000000ULL / CLOCKS_PER_SEC;
#ifdef TARGET_OS_WINDOWS
static LARGE_INTEGER qpc_freq;
#endif
#ifndef TARGET_COMPILER_MSVC
union {
struct timespec ts;
struct timeval tv;
} u;
#endif
switch(spec.major) {
#ifdef HAVE_CLOCK_GETTIME
case CPERF_GETTIME:
if (clock_gettime(spec.minor, &u.ts) != 0)
return 1;
*output = (u.ts.tv_sec * 1000000000ULL) + u.ts.tv_nsec;
break;
#endif
#ifdef HAVE_GETTIMEOFDAY
case CPERF_GTOD:
gettimeofday(&u.tv, NULL);
*output = (u.tv.tv_sec * 1000000000ULL) + (u.tv.tv_usec * 1000ULL);
break;
#endif
#ifdef HAVE_CPU_CLOCK
case CPERF_TSC:
{
uint64_t nsecs, t, multiples;
t = cpu_clock_read();
multiples = t >> max_cycles_shift;
nsecs = multiples * nsecs_for_max_cycles;
nsecs += ((t & max_cycles_mask) * clock_mult) >> clock_shift;
*output = nsecs;
}
break;
#endif
case CPERF_CLOCK:
*output = clock() * CLOCK_RATIO;
if (*output == 0)
return 1;
break;
#ifdef HAVE_GETRUSAGE
case CPERF_RUSAGE:
{
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage))
return 1;
*output = (usage.ru_utime.tv_sec * 1000000000ULL)
+ (usage.ru_utime.tv_usec * 1000ULL);
*output += (usage.ru_stime.tv_sec * 1000000000ULL)
+ (usage.ru_stime.tv_usec * 1000ULL);
}
break;
#endif
#ifdef HAVE_FTIME
case CPERF_FTIME:
{
struct timeb time;
if (ftime(&time))
return 1;
*output = (time.time * 1000000000ULL) + (time.millitm * 1000000ULL);
}
break;
#endif
#ifdef HAVE_TIME
case CPERF_TIME:
{
*output = (time(NULL) * 1000000000ULL);
}
break;
#endif
#ifdef HAVE_MACH_TIME
case CPERF_MACH_TIME:
{
static mach_timebase_info_data_t tb = {0, 0};
if (!tb.numer)
mach_timebase_info(&tb);
*output = mach_absolute_time() * tb.numer / tb.denom;
}
break;
#endif
#ifdef TARGET_OS_WINDOWS
case CPERF_QUERYPERFCOUNTER:
{
LARGE_INTEGER qpc;
if (!qpc_freq.QuadPart) {
if (!QueryPerformanceFrequency(&qpc_freq))
return 1;
}
if (!QueryPerformanceCounter(&qpc))
return 1;
*output = qpc.QuadPart * 1000000000ULL / qpc_freq.QuadPart;
}
break;
case CPERF_GETTICKCOUNT:
*output = GetTickCount() * 1000000ULL;
break;
case CPERF_GETTICKCOUNT64:
*output = GetTickCount64() * 1000000ULL;
break;
case CPERF_TIMEGETTIME:
*output = timeGetTime() * 1000000ULL;
break;
case CPERF_GETSYSTIME:
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
*output = ((uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32)) * 100ULL;
}
break;
#if _WIN32_WINNT >= 0x0602
case CPERF_GETSYSTIMEPRECISE:
{
FILETIME ft;
GetSystemTimePreciseAsFileTime(&ft);
*output = ((uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32)) * 100ULL;
}
break;
#endif
case CPERF_UNBIASEDINTTIME:
{
ULONGLONG t;
if (!QueryUnbiasedInterruptTime(&t))
return 1;
*output = t * 100ULL;
}
break;
#endif
default:
return 1;
}
return 0;
}
const char *clock_name(struct clockspec spec)
{
switch(spec.major) {
case CPERF_NONE:
return "null";
case CPERF_GETTIME:
switch(spec.minor) {
#ifdef CLOCK_REALTIME
case CLOCK_REALTIME:
return "realtime";
#endif
#ifdef CLOCK_REALTIME_COARSE
case CLOCK_REALTIME_COARSE:
return "realtime_crs";
#endif
#ifdef CLOCK_MONOTONIC
case CLOCK_MONOTONIC:
return "monotonic";
#endif
#ifdef CLOCK_MONOTONIC_COARSE
case CLOCK_MONOTONIC_COARSE:
return "monotonic_crs";
#endif
#ifdef CLOCK_MONOTONIC_RAW
case CLOCK_MONOTONIC_RAW:
return "monotonic_raw";
#endif
#ifdef CLOCK_MONOTONIC_RAW_APPROX // OS X
case CLOCK_MONOTONIC_RAW_APPROX:
return "monotonic_raw_approx";
#endif
#ifdef CLOCK_BOOTTIME
case CLOCK_BOOTTIME:
return "boottime";
#endif
#ifdef CLOCK_UPTIME_RAW // OS X
case CLOCK_UPTIME_RAW:
return "uptime_raw";
#endif
#ifdef CLOCK_UPTIME_RAW_APPROX // OS X
case CLOCK_UPTIME_RAW_APPROX:
return "uptime_raw_approx";
#endif
#ifdef CLOCK_PROCESS_CPUTIME_ID
case CLOCK_PROCESS_CPUTIME_ID:
return "process";
#endif
#ifdef CLOCK_THREAD_CPUTIME_ID
case CLOCK_THREAD_CPUTIME_ID:
return "thread";
#endif
default:
abort();
}
#ifdef HAVE_GETTIMEOFDAY
case CPERF_GTOD:
return "gettimeofday";
#endif
#ifdef HAVE_CPU_CLOCK
case CPERF_TSC:
return cpu_clock_name();
#endif
case CPERF_CLOCK:
return "clock";
#ifdef HAVE_GETRUSAGE
case CPERF_RUSAGE:
return "getrusage";
#endif
#ifdef HAVE_FTIME
case CPERF_FTIME:
return "ftime";
#endif
#ifdef HAVE_TIME
case CPERF_TIME:
return "time";
#endif
#ifdef HAVE_MACH_TIME
case CPERF_MACH_TIME:
return "mach_time";
#endif
#ifdef TARGET_OS_WINDOWS
case CPERF_QUERYPERFCOUNTER:
return "PerfCounter";
case CPERF_GETTICKCOUNT:
return "GetTickCount";
case CPERF_GETTICKCOUNT64:
return "GetTickCount64";
case CPERF_TIMEGETTIME:
return "timeGetTime";
case CPERF_GETSYSTIME:
return "SysTimeAsFile";
case CPERF_GETSYSTIMEPRECISE:
return "SysTimePrecAsFile";
case CPERF_UNBIASEDINTTIME:
return "UnbiasIntTime";
#endif
default:
return "unknown";
}
}
void clock_choose_ref(struct clockspec spec)
{
choose_ref_clock(&ref_clock, ref_clock_choices, spec);
}
void clock_choose_ref_wall(void)
{
struct clockspec nullclock = {0, 0};
choose_ref_clock(&ref_clock, wall_clock_choices, nullclock);
}
void clock_set_ref(struct clockspec spec)
{
ref_clock = spec;
}
/*
* Attempts to get the clock resolution for the specified clock. Resolution is
* returned in Hz.
*
* Returns zero on success, nonzero on failure.
*/
int clock_resolution(const struct clockspec spec, uint64_t *output)
{
uint64_t hz;
switch(spec.major) {
case CPERF_NONE:
return 1;
break;
#ifdef HAVE_CLOCK_GETTIME
case CPERF_GETTIME:
{
struct timespec ts;
if (clock_getres(spec.minor, &ts) != 0)
return 1;
hz = 1000000000ULL / ((ts.tv_sec * 1000000000ULL) + ts.tv_nsec);
}
break;
#endif
#ifdef HAVE_GETTIMEOFDAY
case CPERF_GTOD:
return 1;
break;
#endif
#ifdef HAVE_CPU_CLOCK
case CPERF_TSC:
hz = cycles_per_msec * 1000ULL;
break;
#endif
case CPERF_CLOCK:
hz = CLOCKS_PER_SEC;
break;
#ifdef HAVE_GETRUSAGE
case CPERF_RUSAGE:
/* This clock advances based on userspace CPU utilization, but the
* rate at which it gets updated is implementation-dependent and
* there is no clearly defined way to determine that update
* frequency. Best to just error out and say we can't discover it.
*/
return 1;
break;
#endif
#ifdef HAVE_FTIME
case CPERF_FTIME:
return 1;
break;
#endif
#ifdef HAVE_TIME
case CPERF_TIME:
/* 1 second granularity due to API design */
hz = 1ULL;
break;
#endif
#ifdef HAVE_MACH_TIME
case CPERF_MACH_TIME:
{
mach_timebase_info_data_t tb;
mach_timebase_info(&tb);
hz = 1000000000ULL / (tb.numer / tb.denom);
}
break;
#endif
#ifdef TARGET_OS_WINDOWS
case CPERF_QUERYPERFCOUNTER:
{
LARGE_INTEGER freq;
if (!QueryPerformanceFrequency(&freq))
return 1;
hz = freq.QuadPart;
}
break;
case CPERF_GETTICKCOUNT:
case CPERF_GETTICKCOUNT64:
case CPERF_TIMEGETTIME:
hz = 1000ULL;
break;
case CPERF_UNBIASEDINTTIME:
case CPERF_GETSYSTIME:
case CPERF_GETSYSTIMEPRECISE:
hz = 10000000ULL;
break;
#endif
default:
abort();
}
*output = hz;
return 0;
}
/* vim: set ts=4 sts=4 sw=4 et: */