-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix_utils.h
49 lines (37 loc) · 1.24 KB
/
unix_utils.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
#ifndef UNIX_UTILS_H_
#define UNIX_UTILS_H_
#ifdef __APPLE__
#include <mach/mach_time.h>
#define timestamp_t uint64_t
#define init_timestamper() mach_timebase_info(&timebase)
#define get_timestamp(t) t = mach_absolute_time()
#define timestamp_diff(t1,t2) mach_diff(&t1, &t2)
#define PRINTF_TIMESTAMP_STR "%.9f"
#define PRINTF_TIMESTAMP_VAL(t) (double)t / 1e9
static mach_timebase_info_data_t timebase = { 0,0 };
static inline void mach_diff(uint64_t *tp1, const uint64_t *tp2)
{
uint64_t diff = *tp2 - *tp1;
*tp1 = diff * timebase.numer / timebase.denom;
}
#else
#include <time.h>
#define timestamp_t struct timespec
#define init_timestamper()
#define get_timestamp(t) clock_gettime(CLOCK_MONOTONIC, &t)
#define timestamp_diff(t1,t2) timespec_diff(&t1, &t2)
#define PRINTF_TIMESTAMP_STR "%d.%09ld"
#define PRINTF_TIMESTAMP_VAL(t) (int)t.tv_sec, t.tv_nsec
#define ONE_BILLION 1000000000
static inline void timespec_diff(struct timespec* tp1, const struct timespec* tp2)
{
tp1->tv_sec = tp2->tv_sec - tp1->tv_sec;
tp1->tv_nsec = tp2->tv_nsec - tp1->tv_nsec;
if (tp1->tv_nsec < 0)
{
tp1->tv_sec--;
tp1->tv_nsec += ONE_BILLION;
}
}
#endif /* __APPLE__ */
#endif /* UNIX_UTILS_H_ */