Browse Source

Mac specific patch for clock_gettime

pull/111/head
FireMartZ 6 years ago
parent
commit
ee57327641
  1. 15
      src/snark/src/common/profiling.cpp

15
src/snark/src/common/profiling.cpp

@ -26,6 +26,11 @@
#include <proc/readproc.h>
#endif
#ifdef __APPLE__
#include <sys/time.h>
#include <sys/resource.h>
#endif
namespace libsnark {
long long get_nsec_time()
@ -38,10 +43,20 @@ long long get_nsec_time()
long long get_nsec_cpu_time()
{
::timespec ts;
#ifdef __APPLE__
/* from latest Apple Libc: clock_gettime(CLOCK_PROCESS_CPUTIME_ID)
https://opensource.apple.com/source/Libc/Libc-1244.1.7/gen/clock_gettime.c.auto.html */
struct rusage ru;
if (getrusage(RUSAGE_SELF, &ru))
throw ::std::runtime_error("getrusage(RUSAGE_SELF) to get cpu time failed");
timeradd(&ru.ru_utime, &ru.ru_stime, &ru.ru_utime);
TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &ts);
#else
if ( ::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) )
throw ::std::runtime_error("clock_gettime(CLOCK_PROCESS_CPUTIME_ID) failed");
// If we expected this to work, don't silently ignore failures, because that would hide the problem and incur an unnecessarily system-call overhead. So if we ever observe this exception, we should probably add a suitable #ifdef .
//TODO: clock_gettime(CLOCK_PROCESS_CPUTIME_ID) is not supported by native Windows. What about Cygwin? Should we #ifdef on CLOCK_PROCESS_CPUTIME_ID or on __linux__?
#endif
return ts.tv_sec * 1000000000ll + ts.tv_nsec;
}

Loading…
Cancel
Save