-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.h
674 lines (593 loc) · 18.9 KB
/
timer.h
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
/*
The MIT License (MIT)
Copyright (c) 2018 Christopher Crouzet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef ZERO_TIMER_H
#define ZERO_TIMER_H
#define ZR_TIMER_MAJOR_VERSION 0
#define ZR_TIMER_MINOR_VERSION 1
#define ZR_TIMER_PATCH_VERSION 0
#ifndef ZRP_ARCH_DEFINED
#define ZRP_ARCH_DEFINED
#if defined(__x86_64__) || defined(_M_X64)
#define ZRP_ARCH_X86_64
#elif defined(__i386) || defined(_M_IX86)
#define ZRP_ARCH_X86_32
#elif defined(__itanium__) || defined(_M_IA64)
#define ZRP_ARCH_ITANIUM_64
#elif defined(__powerpc64__) || defined(__ppc64__)
#define ZRP_ARCH_POWERPC_64
#elif defined(__powerpc__) || defined(__ppc__)
#define ZRP_ARCH_POWERPC_32
#elif defined(__aarch64__)
#define ZRP_ARCH_ARM_64
#elif defined(__arm__)
#define ZRP_ARCH_ARM_32
#endif
#endif /* ZRP_ARCH_DEFINED */
/*
The environment macro represents whether the code is to be generated for a
32-bit or 64-bit target platform. Some CPUs, such as the x86-64 processors,
allow running code in 32-bit mode if compiled using the -m32 or -mx32
compiler switches, in which case `ZR_ENVIRONMENT` is set to 32.
*/
#ifndef ZR_ENVIRONMENT
#if (!defined(ZRP_ARCH_X86_64) || defined(__ILP32__)) \
&& !defined(ZRP_ARCH_ITANIUM_64) && !defined(ZRP_ARCH_POWERPC_64) \
&& !defined(ZRP_ARCH_ARM_64)
#define ZR_ENVIRONMENT 32
#else
#define ZR_ENVIRONMENT 64
#endif
#ifdef ZR_DEFINE_IMPLEMENTATION
typedef char zrp_invalid_environment_value
[ZR_ENVIRONMENT == 32 || ZR_ENVIRONMENT == 64 ? 1 : -1];
#endif /* ZR_DEFINE_IMPLEMENTATION */
#endif /* ZR_ENVIRONMENT */
#ifndef ZRP_PLATFORM_DEFINED
#define ZRP_PLATFORM_DEFINED
#if defined(_WIN32)
#define ZRP_PLATFORM_WINDOWS
#elif defined(__unix__) || defined(__APPLE__)
#define ZRP_PLATFORM_UNIX
#if defined(__APPLE__)
#define ZRP_PLATFORM_DARWIN
#if TARGET_OS_IPHONE == 1
#define ZRP_PLATFORM_IOS
#elif TARGET_OS_MAC == 1
#define ZRP_PLATFORM_MACOS
#endif
#elif defined(__linux__)
#define ZRP_PLATFORM_LINUX
#endif
#endif
#endif /* ZRP_PLATFORM_DEFINED */
#ifndef ZRP_FIXED_TYPES_DEFINED
#define ZRP_FIXED_TYPES_DEFINED
#ifdef ZR_USE_STD_FIXED_TYPES
#include <stdint.h>
typedef int8_t ZrInt8;
typedef uint8_t ZrUint8;
typedef int16_t ZrInt16;
typedef uint16_t ZrUint16;
typedef int32_t ZrInt32;
typedef uint32_t ZrUint32;
typedef int64_t ZrInt64;
typedef uint64_t ZrUint64;
#else
/*
The focus here is on the common data models, that is ILP32 (most recent
32-bit systems), LP64 (Unix-like systems), and LLP64 (Windows). All of these
models have the `char` type set to 8 bits, `short` to 16 bits, `int` to
32 bits, and `long long` to 64 bits.
*/
#ifdef ZR_INT8
typedef ZR_INT8 ZrInt8;
#else
typedef char ZrInt8;
#endif
#ifdef ZR_UINT8
typedef ZR_UINT8 ZrUint8;
#else
typedef unsigned char ZrUint8;
#endif
#ifdef ZR_INT16
typedef ZR_INT16 ZrInt16;
#else
typedef short ZrInt16;
#endif
#ifdef ZR_UINT16
typedef ZR_UINT16 ZrUint16;
#else
typedef unsigned short ZrUint16;
#endif
#ifdef ZR_INT32
typedef ZR_INT32 ZrInt32;
#else
typedef int ZrInt32;
#endif
#ifdef ZR_UINT32
typedef ZR_UINT32 ZrUint32;
#else
typedef unsigned int ZrUint32;
#endif
#ifdef ZR_INT64
typedef ZR_INT64 ZrInt64;
#else
typedef long long ZrInt64;
#endif
#ifdef ZR_UINT64
typedef ZR_UINT64 ZrUint64;
#else
typedef unsigned long long ZrUint64;
#endif
#endif /* ZR_USE_STD_FIXED_TYPES */
#ifdef ZR_DEFINE_IMPLEMENTATION
typedef char zrp_invalid_int8_type[sizeof(ZrInt8) == 1 ? 1 : -1];
typedef char zrp_invalid_uint8_type[sizeof(ZrUint8) == 1 ? 1 : -1];
typedef char zrp_invalid_int16_type[sizeof(ZrInt16) == 2 ? 1 : -1];
typedef char zrp_invalid_uint16_type[sizeof(ZrUint16) == 2 ? 1 : -1];
typedef char zrp_invalid_int32_type[sizeof(ZrInt32) == 4 ? 1 : -1];
typedef char zrp_invalid_uint32_type[sizeof(ZrUint32) == 4 ? 1 : -1];
typedef char zrp_invalid_int64_type[sizeof(ZrInt64) == 8 ? 1 : -1];
typedef char zrp_invalid_uint64_type[sizeof(ZrUint64) == 8 ? 1 : -1];
#endif /* ZR_DEFINE_IMPLEMENTATION */
#endif /* ZRP_FIXED_TYPES_DEFINED */
#ifndef ZRP_BASIC_TYPES_DEFINED
#define ZRP_BASIC_TYPES_DEFINED
#ifdef ZR_USE_STD_BASIC_TYPES
#include <stddef.h>
typedef size_t ZrSize;
#else
/*
The C standard provides no guarantees about the size of the type `size_t`,
and some exotic platforms will in fact provide original values, but this
should cover most of the use cases.
*/
#ifdef ZR_SIZE_TYPE
typedef ZR_SIZE_TYPE ZrSize;
#elif ZR_ENVIRONMENT == 32
typedef ZrUint32 ZrSize;
#else
typedef ZrUint64 ZrSize;
#endif
#endif
#ifdef ZR_DEFINE_IMPLEMENTATION
typedef char
zrp_invalid_size_type[sizeof(ZrSize) == sizeof sizeof(void *) ? 1 : -1];
#endif /* ZR_DEFINE_IMPLEMENTATION */
#endif /* ZRP_BASIC_TYPES_DEFINED */
#ifndef ZRP_STATUS_DEFINED
#define ZRP_STATUS_DEFINED
enum ZrStatus {
ZR_SUCCESS = 0,
ZR_ERROR = -1,
ZR_ERROR_ALLOCATION = -2,
ZR_ERROR_MAX_SIZE_EXCEEDED = -3
};
#endif /* ZRP_STATUS_DEFINED */
#if defined(ZR_TIMER_SPECIFY_INTERNAL_LINKAGE) \
|| defined(ZR_SPECIFY_INTERNAL_LINKAGE)
#define ZRP_TIMER_LINKAGE static
#elif defined(__cplusplus)
#define ZRP_TIMER_LINKAGE extern "C"
#else
#define ZRP_TIMER_LINKAGE extern
#endif
#define ZR_TIMER_TICKS_PER_SECOND 1000000000ull
struct ZrCpuTimes {
ZrUint64 user;
ZrUint64 system;
};
ZRP_TIMER_LINKAGE enum ZrStatus
zrGetRealTime(ZrUint64 *pTime);
ZRP_TIMER_LINKAGE enum ZrStatus
zrGetCpuTimes(struct ZrCpuTimes *pTimes);
#endif /* ZERO_TIMER_H */
#ifdef ZR_DEFINE_IMPLEMENTATION
#ifndef ZRP_TIMER_IMPLEMENTATION_DEFINED
#define ZRP_TIMER_IMPLEMENTATION_DEFINED
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#ifndef ZR_ASSERT
#include <assert.h>
#define ZR_ASSERT assert
#endif /* ZR_ASSERT */
#ifndef ZRP_UNUSED_DEFINED
#define ZRP_UNUSED_DEFINED
#ifdef __GNUC__
#define ZRP_MAYBE_UNUSED __attribute__((unused))
#else
#define ZRP_MAYBE_UNUSED
#endif
#endif /* ZRP_UNUSED_DEFINED */
#ifndef ZRP_LOGGING_DEFINED
#define ZRP_LOGGING_DEFINED
#if defined(ZR_SET_LOGGING_LEVEL_DEBUG)
#define ZRP_LOGGING_LEVEL ZR_LOG_LEVEL_DEBUG
#elif defined(ZR_SET_LOGGING_LEVEL_TRACE)
#define ZRP_LOGGING_LEVEL ZR_LOG_LEVEL_TRACE
#elif defined(ZR_SET_LOGGING_LEVEL_INFO)
#define ZRP_LOGGING_LEVEL ZR_LOG_LEVEL_INFO
#elif defined(ZR_SET_LOGGING_LEVEL_WARNING)
#define ZRP_LOGGING_LEVEL ZR_LOG_LEVEL_WARNING
#elif defined(ZR_SET_LOGGING_LEVEL_ERROR)
#define ZRP_LOGGING_LEVEL ZR_LOG_LEVEL_ERROR
#elif defined(ZR_ENABLE_DEBUGGING) \
|| (!defined(ZR_DISABLE_DEBUGGING) \
&& (defined(DEBUG) || !defined(NDEBUG)))
#define ZRP_LOGGING_LEVEL ZR_LOG_LEVEL_DEBUG
#else
#define ZRP_LOGGING_LEVEL ZR_LOG_LEVEL_WARNING
#endif
#ifdef ZR_DISABLE_LOGGING
#define ZRP_LOGGING 0
#else
#define ZRP_LOGGING 1
#endif /* ZR_DISABLE_LOGGING */
#ifndef ZR_LOG
#define ZR_LOG(level, ...) \
do { \
if (ZRP_LOGGING && level <= ZRP_LOGGING_LEVEL) { \
zrpLoggerLog(level, __FILE__, __LINE__, __VA_ARGS__); \
} \
} while (0)
#endif /* ZR_LOG */
#define ZRP_LOG_DEBUG(...) ZR_LOG(ZR_LOG_LEVEL_DEBUG, __VA_ARGS__)
#define ZRP_LOG_TRACE(...) ZR_LOG(ZR_LOG_LEVEL_TRACE, __VA_ARGS__)
#define ZRP_LOG_INFO(...) ZR_LOG(ZR_LOG_LEVEL_INFO, __VA_ARGS__)
#define ZRP_LOG_WARNING(...) ZR_LOG(ZR_LOG_LEVEL_WARNING, __VA_ARGS__)
#define ZRP_LOG_ERROR(...) ZR_LOG(ZR_LOG_LEVEL_ERROR, __VA_ARGS__)
#endif /* ZRP_LOGGING_DEFINED */
#ifndef ZRP_LOGLEVEL_DEFINED
#define ZRP_LOGLEVEL_DEFINED
enum ZrLogLevel {
ZR_LOG_LEVEL_ERROR = 0,
ZR_LOG_LEVEL_WARNING = 1,
ZR_LOG_LEVEL_INFO = 2,
ZR_LOG_LEVEL_TRACE = 3,
ZR_LOG_LEVEL_DEBUG = 4
};
#endif /* ZRP_LOGLEVEL_DEFINED */
#ifndef ZRP_LOGGER_DEFINED
#define ZRP_LOGGER_DEFINED
#if !defined(ZR_DISABLE_LOG_STYLING) && defined(ZRP_PLATFORM_UNIX) \
&& defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1
#include <unistd.h>
#define ZRP_LOGGER_LOG_STYLING 1
#else
#define ZRP_LOGGER_LOG_STYLING 0
#endif
#if ZRP_LOGGER_LOG_STYLING
enum ZrpLoggerStyle {
ZRP_LOGGER_STYLE_RESET = 0,
ZRP_LOGGER_STYLE_BLACK = 1,
ZRP_LOGGER_STYLE_RED = 2,
ZRP_LOGGER_STYLE_GREEN = 3,
ZRP_LOGGER_STYLE_YELLOW = 4,
ZRP_LOGGER_STYLE_BLUE = 5,
ZRP_LOGGER_STYLE_MAGENTA = 6,
ZRP_LOGGER_STYLE_CYAN = 7,
ZRP_LOGGER_STYLE_BRIGHT_BLACK = 8,
ZRP_LOGGER_STYLE_BRIGHT_RED = 9,
ZRP_LOGGER_STYLE_BRIGHT_GREEN = 10,
ZRP_LOGGER_STYLE_BRIGHT_YELLOW = 11,
ZRP_LOGGER_STYLE_BRIGHT_BLUE = 12,
ZRP_LOGGER_STYLE_BRIGHT_MAGENTA = 13,
ZRP_LOGGER_STYLE_BRIGHT_CYAN = 14
};
#endif /* ZRP_LOGGER_LOG_STYLING */
static void
zrpLoggerGetLogLevelName(const char **ppName, enum ZrLogLevel level)
{
ZR_ASSERT(ppName != NULL);
switch (level) {
case ZR_LOG_LEVEL_ERROR:
*ppName = "error";
return;
case ZR_LOG_LEVEL_WARNING:
*ppName = "warning";
return;
case ZR_LOG_LEVEL_INFO:
*ppName = "info";
return;
case ZR_LOG_LEVEL_TRACE:
*ppName = "trace";
return;
case ZR_LOG_LEVEL_DEBUG:
*ppName = "debug";
return;
default:
ZR_ASSERT(0);
}
}
#if ZRP_LOGGER_LOG_STYLING
static void
zrpLoggerGetLogLevelStyle(enum ZrpLoggerStyle *pStyle, enum ZrLogLevel level)
{
ZR_ASSERT(pStyle != NULL);
switch (level) {
case ZR_LOG_LEVEL_ERROR:
*pStyle = ZRP_LOGGER_STYLE_BRIGHT_RED;
return;
case ZR_LOG_LEVEL_WARNING:
*pStyle = ZRP_LOGGER_STYLE_BRIGHT_YELLOW;
return;
case ZR_LOG_LEVEL_INFO:
*pStyle = ZRP_LOGGER_STYLE_BRIGHT_GREEN;
return;
case ZR_LOG_LEVEL_TRACE:
*pStyle = ZRP_LOGGER_STYLE_BRIGHT_CYAN;
return;
case ZR_LOG_LEVEL_DEBUG:
*pStyle = ZRP_LOGGER_STYLE_BRIGHT_MAGENTA;
return;
default:
ZR_ASSERT(0);
};
}
static void
zrpLoggerGetStyleAnsiCode(const char **ppCode, enum ZrpLoggerStyle style)
{
ZR_ASSERT(ppCode != NULL);
switch (style) {
case ZRP_LOGGER_STYLE_RESET:
*ppCode = "\x1b[0m";
return;
case ZRP_LOGGER_STYLE_BLACK:
*ppCode = "\x1b[30m";
return;
case ZRP_LOGGER_STYLE_RED:
*ppCode = "\x1b[31m";
return;
case ZRP_LOGGER_STYLE_GREEN:
*ppCode = "\x1b[32m";
return;
case ZRP_LOGGER_STYLE_YELLOW:
*ppCode = "\x1b[33m";
return;
case ZRP_LOGGER_STYLE_BLUE:
*ppCode = "\x1b[34m";
return;
case ZRP_LOGGER_STYLE_MAGENTA:
*ppCode = "\x1b[35m";
return;
case ZRP_LOGGER_STYLE_CYAN:
*ppCode = "\x1b[36m";
return;
case ZRP_LOGGER_STYLE_BRIGHT_BLACK:
*ppCode = "\x1b[1;30m";
return;
case ZRP_LOGGER_STYLE_BRIGHT_RED:
*ppCode = "\x1b[1;31m";
return;
case ZRP_LOGGER_STYLE_BRIGHT_GREEN:
*ppCode = "\x1b[1;32m";
return;
case ZRP_LOGGER_STYLE_BRIGHT_YELLOW:
*ppCode = "\x1b[1;33m";
return;
case ZRP_LOGGER_STYLE_BRIGHT_BLUE:
*ppCode = "\x1b[1;34m";
return;
case ZRP_LOGGER_STYLE_BRIGHT_MAGENTA:
*ppCode = "\x1b[1;35m";
return;
case ZRP_LOGGER_STYLE_BRIGHT_CYAN:
*ppCode = "\x1b[1;36m";
return;
default:
ZR_ASSERT(0);
}
}
#endif /* ZRP_LOGGER_LOG_STYLING */
ZRP_MAYBE_UNUSED static void
zrpLoggerLogVaList(enum ZrLogLevel level,
const char *pFile,
int line,
const char *pFormat,
va_list args)
{
const char *pLevelName;
const char *pLevelStyleStart;
const char *pLevelStyleEnd;
ZR_ASSERT(pFile != NULL);
ZR_ASSERT(pFormat != NULL);
zrpLoggerGetLogLevelName(&pLevelName, level);
#if ZRP_LOGGER_LOG_STYLING
if (isatty(fileno(stderr))) {
enum ZrpLoggerStyle levelStyle;
zrpLoggerGetLogLevelStyle(&levelStyle, level);
zrpLoggerGetStyleAnsiCode(&pLevelStyleStart, levelStyle);
zrpLoggerGetStyleAnsiCode(&pLevelStyleEnd, ZRP_LOGGER_STYLE_RESET);
} else {
pLevelStyleStart = pLevelStyleEnd = "";
}
#else
pLevelStyleStart = pLevelStyleEnd = "";
#endif /* ZRP_LOGGER_LOG_STYLING */
fprintf(stderr,
"%s:%d: %s%s%s: ",
pFile,
line,
pLevelStyleStart,
pLevelName,
pLevelStyleEnd);
vfprintf(stderr, pFormat, args);
}
ZRP_MAYBE_UNUSED static void
zrpLoggerLog(enum ZrLogLevel level,
const char *pFile,
int line,
const char *pFormat,
...)
{
va_list args;
ZR_ASSERT(pFile != NULL);
ZR_ASSERT(pFormat != NULL);
va_start(args, pFormat);
zrpLoggerLogVaList(level, pFile, line, pFormat, args);
va_end(args);
}
#endif /* ZRP_LOGGER_DEFINED */
#if defined(ZRP_PLATFORM_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(ZRP_PLATFORM_DARWIN)
#include <mach/mach_time.h>
#include <sys/resource.h>
#elif defined(ZRP_PLATFORM_UNIX)
#include <sys/resource.h>
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L
#include <time.h>
#define ZRP_TIMER_USE_CLOCK_GETTIME
#if defined(CLOCK_MONOTONIC_RAW)
#define ZRP_TIMER_CLOCK_ID CLOCK_MONOTONIC_RAW
#elif defined(CLOCK_MONOTONIC)
#define ZRP_TIMER_CLOCK_ID CLOCK_MONOTONIC
#else
#define ZRP_TIMER_CLOCK_ID CLOCK_REALTIME
#endif
#else
#include <sys/time.h>
#endif
#else
typedef char zrp_timer_unsupported_platform[-1];
#endif
/*
In some cases, tick values are casted to 64-bit floating-points in order
to prevent (unlikely) integer overflows during some arithmetic operations.
This effectively reducse the precision of the ticks having a value greater
than 2^53, which corresponds to approximatively 104 days.
*/
ZRP_MAYBE_UNUSED ZRP_TIMER_LINKAGE enum ZrStatus
zrGetRealTime(ZrUint64 *pTime)
{
ZR_ASSERT(pTime != NULL);
#if defined(ZRP_PLATFORM_WINDOWS)
{
static double timeToNano;
LARGE_INTEGER time;
if (timeToNano == 0.0) {
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency)) {
ZRP_LOG_ERROR("failed to retrieve the time's frequency\n");
return ZR_ERROR;
}
timeToNano = (double)ZR_TIMER_TICKS_PER_SECOND / frequency.QuadPart;
}
if (!QueryPerformanceCounter(&time)) {
ZRP_LOG_ERROR("failed to retrieve the current time\n");
return ZR_ERROR;
}
*pTime = time.QuadPart * timeToNano;
return ZR_SUCCESS;
}
#elif defined(ZRP_PLATFORM_DARWIN)
/*
Since Darwin 5.2, `clock_gettime()` can return high resolution times
with the `CLOCK_UPTIME_RAW` clock but it internally only calls
`mach_absolute_time()` with the overhead of converting the result into
the `timespec` format.
*/
{
static double timeToNano;
if (timeToNano == 0.0) {
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) {
ZRP_LOG_ERROR("failed to retrieve the current time\n");
return ZR_ERROR;
}
timeToNano = (double)info.numer / info.denom;
}
*pTime = mach_absolute_time() * timeToNano;
return ZR_SUCCESS;
}
#elif defined(ZRP_PLATFORM_UNIX)
#if defined(ZRP_TIMER_USE_CLOCK_GETTIME)
{
struct timespec time;
if (clock_gettime(ZRP_TIMER_CLOCK_ID, &time) != 0) {
ZRP_LOG_ERROR("failed to retrieve the current time\n");
return ZR_ERROR;
}
*pTime = (ZrUint64)time.tv_sec * 1000000000ull + (ZrUint64)time.tv_nsec;
return ZR_SUCCESS;
}
#else
{
struct timeval time;
if (gettimeofday(&time, NULL) != 0) {
ZRP_LOG_ERROR("failed to retrieve the current time\n");
return ZR_ERROR;
}
*pTime = (ZrUint64)time.tv_sec * 1000000000ull
+ (ZrUint64)time.tv_usec * 1000ull;
return ZR_SUCCESS;
}
#endif
#endif
ZRP_LOG_ERROR("platform not supported\n");
return ZR_ERROR;
}
ZRP_MAYBE_UNUSED ZRP_TIMER_LINKAGE enum ZrStatus
zrGetCpuTimes(struct ZrCpuTimes *pTimes)
{
ZR_ASSERT(pTimes != NULL);
#if defined(ZRP_PLATFORM_WINDOWS)
{
FILETIME creationTime;
FILETIME exitTime;
FILETIME kernelTime;
FILETIME userTime;
ULARGE_INTEGER time;
if (!GetProcessTimes(GetCurrentProcess(),
&creationTime,
&exitTime,
&kernelTime,
&userTime)) {
ZRP_LOG_ERROR("failed to retrieve the current CPU times\n");
return ZR_ERROR;
}
time.LowPart = userTime.dwLowDateTime;
time.HighPart = userTime.dwHighDateTime;
*pTimes->user = time.QuadPart * 100ull;
time.LowPart = kernelTime.dwLowDateTime;
time.HighPart = kernelTime.dwHighDateTime;
*pTimes->system = time.QuadPart * 100ull;
}
#elif defined(ZRP_PLATFORM_UNIX)
{
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage)) {
ZRP_LOG_ERROR("failed to retrieve the current CPU times\n");
return ZR_ERROR;
}
pTimes->user = (ZrUint64)usage.ru_utime.tv_sec * 1000000000ull
+ (ZrUint64)usage.ru_utime.tv_usec * 1000ull;
pTimes->system = (ZrUint64)usage.ru_stime.tv_sec * 1000000000ull
+ (ZrUint64)usage.ru_stime.tv_usec * 1000ull;
return ZR_SUCCESS;
}
#endif
ZRP_LOG_ERROR("platform not supported\n");
return ZR_ERROR;
}
#endif /* ZRP_TIMER_IMPLEMENTATION_DEFINED */
#endif /* ZR_DEFINE_IMPLEMENTATION */