0
0
mirror of https://github.com/yse/easy_profiler.git synced 2025-01-15 01:30:02 +08:00

Fix mac os build

- Work around lack of clock_gettime() on OS X
This commit is contained in:
Karim Naaji 2017-06-12 11:30:01 -04:00
parent c608665e06
commit 504648d613

View File

@ -60,6 +60,11 @@
#include "event_trace_win.h" #include "event_trace_win.h"
#include "current_time.h" #include "current_time.h"
#ifdef __APPLE__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#if EASY_OPTION_LOG_ENABLED != 0 #if EASY_OPTION_LOG_ENABLED != 0
# include <iostream> # include <iostream>
@ -157,14 +162,26 @@ const decltype(LARGE_INTEGER::QuadPart) CPU_FREQUENCY = ([](){ LARGE_INTEGER fre
int64_t calculate_cpu_frequency() int64_t calculate_cpu_frequency()
{ {
double g_TicksPerNanoSec; double g_TicksPerNanoSec;
struct timespec begints, endts;
uint64_t begin = 0, end = 0; uint64_t begin = 0, end = 0;
#ifdef __APPLE__
clock_serv_t cclock;
mach_timespec_t begints, endts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &begints);
#else
struct timespec begints, endts;
clock_gettime(CLOCK_MONOTONIC, &begints); clock_gettime(CLOCK_MONOTONIC, &begints);
#endif
begin = getCurrentTime(); begin = getCurrentTime();
volatile uint64_t i; volatile uint64_t i;
for (i = 0; i < 100000000; i++); /* must be CPU intensive */ for (i = 0; i < 100000000; i++); /* must be CPU intensive */
end = getCurrentTime(); end = getCurrentTime();
#ifdef __APPLE__
clock_get_time(cclock, &endts);
mach_port_deallocate(mach_task_self(), cclock);
#else
clock_gettime(CLOCK_MONOTONIC, &endts); clock_gettime(CLOCK_MONOTONIC, &endts);
#endif
struct timespec tmpts; struct timespec tmpts;
const int NANO_SECONDS_IN_SEC = 1000000000; const int NANO_SECONDS_IN_SEC = 1000000000;
tmpts.tv_sec = endts.tv_sec - begints.tv_sec; tmpts.tv_sec = endts.tv_sec - begints.tv_sec;