-
Notifications
You must be signed in to change notification settings - Fork 4
/
osxGetTime.h
101 lines (88 loc) · 2.28 KB
/
osxGetTime.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
/*
* File: osxGetTime.h
* Author: Russ Porosky
*
* Created on May 3 2012
*/
#ifndef OSXGETTIME_H
#define OSXGETTIME_H
#ifdef __APPLE__
#pragma weak clock_gettime
#include <sys/time.h>
#include <sys/resource.h>
#include <mach/mach.h>
#include <mach/clock.h>
#include <mach/mach_time.h>
#include <errno.h>
#include <unistd.h>
#include <sched.h>
typedef enum {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID
} clockid_t;
static mach_timebase_info_data_t __clock_gettime_inf;
struct timespec clock_gettime(clockid_t clk_id, struct timespec *tp) {
kern_return_t ret;
clock_serv_t clk;
clock_id_t clk_serv_id;
mach_timespec_t tm;
uint64_t start, end, delta, nano;
task_basic_info_data_t tinfo;
task_thread_times_info_data_t ttinfo;
mach_msg_type_number_t tflag;
int retint = -1;
switch (clk_id) {
case CLOCK_REALTIME:
case CLOCK_MONOTONIC:
clk_serv_id = clk_id == CLOCK_REALTIME ? CALENDAR_CLOCK : SYSTEM_CLOCK;
if (KERN_SUCCESS == (ret = host_get_clock_service(mach_host_self(), clk_serv_id, &clk))) {
if (KERN_SUCCESS == (ret = clock_get_time(clk, &tm))) {
tp->tv_sec = tm.tv_sec;
tp->tv_nsec = tm.tv_nsec;
retint = 0;
}
}
if (KERN_SUCCESS != ret) {
errno = EINVAL;
retint = -1;
}
break;
case CLOCK_PROCESS_CPUTIME_ID:
case CLOCK_THREAD_CPUTIME_ID:
start = mach_absolute_time();
if (clk_id == CLOCK_PROCESS_CPUTIME_ID) {
getpid();
} else {
sched_yield();
}
end = mach_absolute_time();
delta = end - start;
if (0 == __clock_gettime_inf.denom) {
mach_timebase_info(&__clock_gettime_inf);
}
nano = delta * __clock_gettime_inf.numer / __clock_gettime_inf.denom;
tp->tv_sec = nano * 1e-9;
tp->tv_nsec = nano - (tp->tv_sec * 1e9);
retint = 0;
break;
default:
errno = EINVAL;
retint = -1;
}
struct timespec retval;
retval.tv_sec = (int)retint / 1000000000;
retval.tv_nsec = (long)retint - retval.tv_sec;
return retval;
}
/* Identifier for system-wide realtime clock. */
#define CLOCK_REALTIME 0
/* Monotonic system-wide clock. */
#define CLOCK_MONOTONIC 1
/* High-resolution timer from the CPU. */
#define CLOCK_PROCESS_CPUTIME_ID 2
/* Thread-specific CPU-time clock. */
#define CLOCK_THREAD_CPUTIME_ID 3
#endif // __APPLE__
#endif /* OSXGETTIME_H */