| 1 |
|
| 2 |
/* |
| 3 |
* Copyright (c) 2018 Chris Jones <[email protected]> |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, and distribute this software for any |
| 6 |
* purpose with or without fee is hereby granted, provided that the above |
| 7 |
* copyright notice and this permission notice appear in all copies. |
| 8 |
* |
| 9 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "time.h" |
| 19 |
|
| 20 |
#if __MP_LEGACY_SUPPORT_GETTIME__ |
| 21 |
|
| 22 |
#include <sys/time.h> |
| 23 |
#include <sys/sysctl.h> |
| 24 |
#include <sys/resource.h> |
| 25 |
|
| 26 |
#include <mach/mach_init.h> |
| 27 |
#include <mach/mach_port.h> |
| 28 |
#include <mach/mach_time.h> |
| 29 |
#include <mach/thread_act.h> |
| 30 |
|
| 31 |
#define BILLION 1000000000L |
| 32 |
#define MILLION 1000000L |
| 33 |
#define THOUSAND 1000L |
| 34 |
|
| 35 |
int clock_gettime( clockid_t clk_id, struct timespec *ts ) |
| 36 |
{ |
| 37 |
int ret = -1; |
| 38 |
if ( ts ) |
| 39 |
{ |
| 40 |
if ( CLOCK_REALTIME == clk_id ) |
| 41 |
{ |
| 42 |
struct timeval tv; |
| 43 |
ret = gettimeofday(&tv, NULL); |
| 44 |
ts->tv_sec = tv.tv_sec; |
| 45 |
ts->tv_nsec = tv.tv_usec * 1000; |
| 46 |
} |
| 47 |
else if ( CLOCK_MONOTONIC == clk_id ) |
| 48 |
{ |
| 49 |
struct timeval boottime; |
| 50 |
size_t boottime_len = sizeof(boottime); |
| 51 |
ret = sysctlbyname("kern.boottime", &boottime, &boottime_len, NULL, 0); |
| 52 |
if (ret != KERN_SUCCESS) { return ret; } |
| 53 |
struct timeval tv; |
| 54 |
ret = gettimeofday(&tv, NULL); |
| 55 |
timersub(&tv, &boottime, &tv); |
| 56 |
ts->tv_sec = tv.tv_sec; |
| 57 |
ts->tv_nsec = tv.tv_usec * 1000; |
| 58 |
ret = 0; |
| 59 |
} |
| 60 |
else if ( CLOCK_PROCESS_CPUTIME_ID == clk_id ) |
| 61 |
{ |
| 62 |
struct rusage ru; |
| 63 |
ret = getrusage(RUSAGE_SELF, &ru); |
| 64 |
timeradd(&ru.ru_utime, &ru.ru_stime, &ru.ru_utime); |
| 65 |
ts->tv_sec = ru.ru_utime.tv_sec; |
| 66 |
ts->tv_nsec = ru.ru_utime.tv_usec * 1000; |
| 67 |
} |
| 68 |
else if ( CLOCK_THREAD_CPUTIME_ID == clk_id ) |
| 69 |
{ |
| 70 |
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
| 71 |
thread_basic_info_data_t info; |
| 72 |
|
| 73 |
thread_port_t thread = mach_thread_self(); |
| 74 |
ret = thread_info(thread, THREAD_BASIC_INFO, (thread_info_t) &info, &count); |
| 75 |
mach_port_deallocate(mach_task_self(), thread); |
| 76 |
|
| 77 |
time_value_add(&info.user_time, &info.system_time); |
| 78 |
ts->tv_sec = info.user_time.seconds; |
| 79 |
ts->tv_nsec = info.user_time.microseconds * 1000; |
| 80 |
} |
| 81 |
else if ( CLOCK_MONOTONIC_RAW == clk_id || |
| 82 |
CLOCK_MONOTONIC_RAW_APPROX == clk_id || |
| 83 |
CLOCK_UPTIME_RAW == clk_id || |
| 84 |
CLOCK_UPTIME_RAW_APPROX == clk_id ) |
| 85 |
{ |
| 86 |
static mach_timebase_info_data_t timebase; |
| 87 |
if ( 0 == timebase.numer || 0 == timebase.denom ) { |
| 88 |
const kern_return_t kr = mach_timebase_info( &timebase ); |
| 89 |
if ( kr != KERN_SUCCESS ) { return kr; } |
| 90 |
} |
| 91 |
uint64_t tdiff = mach_absolute_time() * ( timebase.numer / timebase.denom ); |
| 92 |
ts->tv_sec = tdiff / BILLION; |
| 93 |
ts->tv_nsec = tdiff % BILLION; |
| 94 |
ret = 0; |
| 95 |
} |
| 96 |
} |
| 97 |
return ret; |
| 98 |
} |
| 99 |
|
| 100 |
int clock_getres( clockid_t clk_id, struct timespec *ts ) |
| 101 |
{ |
| 102 |
int ret = -1; |
| 103 |
if ( ts ) |
| 104 |
{ |
| 105 |
if ( CLOCK_REALTIME == clk_id || |
| 106 |
CLOCK_MONOTONIC == clk_id || |
| 107 |
CLOCK_PROCESS_CPUTIME_ID == clk_id || |
| 108 |
CLOCK_THREAD_CPUTIME_ID == clk_id) |
| 109 |
{ |
| 110 |
// return 1us precision |
| 111 |
ts->tv_sec = 0; |
| 112 |
ts->tv_nsec = THOUSAND; |
| 113 |
ret = 0; |
| 114 |
} |
| 115 |
else if ( CLOCK_MONOTONIC_RAW == clk_id || |
| 116 |
CLOCK_MONOTONIC_RAW_APPROX == clk_id || |
| 117 |
CLOCK_UPTIME_RAW == clk_id || |
| 118 |
CLOCK_UPTIME_RAW_APPROX == clk_id ) |
| 119 |
{ |
| 120 |
// return 1ns precision |
| 121 |
ts->tv_sec = 0; |
| 122 |
ts->tv_nsec = 1; |
| 123 |
ret = 0; |
| 124 |
} |
| 125 |
} |
| 126 |
return ret; |
| 127 |
} |
| 128 |
|
| 129 |
#endif |
| 130 |
|
| 131 |
#if __MP_LEGACY_SUPPORT_TIMESPEC_GET__ |
| 132 |
|
| 133 |
int timespec_get(struct timespec *ts, int base) |
| 134 |
{ |
| 135 |
switch (base) { |
| 136 |
case TIME_UTC: |
| 137 |
if (clock_gettime(CLOCK_REALTIME, ts) == -1) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
return base; |
| 141 |
|
| 142 |
default: |
| 143 |
return 0; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#endif |