Commit 3748326f authored by Ben Gamari's avatar Ben Gamari 🐢

rts: Add GetMyThreadCPUTime helper

parent 6b5fd7b0
......@@ -13,6 +13,7 @@
void initializeTimer (void);
Time getProcessCPUTime (void);
Time getMyThreadCPUTime (void);
void getProcessTimes (Time *user, Time *elapsed);
/* Get the current date and time.
......
......@@ -44,11 +44,49 @@ void initializeTimer()
#endif
}
#if defined(HAVE_CLOCK_GETTIME)
static Time getClockTime(clockid_t clock)
{
struct timespec ts;
int res = clock_gettime(clock, &ts);
if (res == 0) {
return SecondsToTime(ts.tv_sec) + NSToTime(ts.tv_nsec);
} else {
sysErrorBelch("clock_gettime");
stg_exit(EXIT_FAILURE);
}
}
#endif
Time getMyThreadCPUTime(void)
{
#if defined(HAVE_CLOCK_GETTIME) && \
defined(CLOCK_PROCESS_CPUTIME_ID) && \
defined(HAVE_SYSCONF)
return getClockTime(CLOCK_THREAD_CPUTIME_ID);
#elif defined(darwin_HOST_OS)
mach_port_t port = pthread_mach_thread_np(GetCurrentThread());
thread_basic_info_data_t info = { 0 };
mach_msg_type_number_t info_count = THREAD_BASIC_INFO_COUNT;
kern_return_t kern_err = thread_info(mach_thread_self(), THREAD_BASIC_INFO,
(thread_info_t) &info, &info_count);
if (kern_err == KERN_SUCCESS) {
return SecondsToTime(info.user_time.seconds) + USToTime(info.user_time.microseconds);
} else {
sysErrorBelch("getThreadCPUTime");
stg_exit(EXIT_FAILURE);
}
#else
// TODO: How to fallback here?
return getProcessCPUTime();
#endif
}
Time getProcessCPUTime(void)
{
#if !defined(BE_CONSERVATIVE) && \
defined(HAVE_CLOCK_GETTIME) && \
defined(_SC_CPUTIME) && \
defined(_SC_CPUTIME) && \
defined(CLOCK_PROCESS_CPUTIME_ID) && \
defined(HAVE_SYSCONF)
static int checked_sysconf = 0;
......@@ -59,15 +97,7 @@ Time getProcessCPUTime(void)
checked_sysconf = 1;
}
if (sysconf_result != -1) {
struct timespec ts;
int res;
res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
if (res == 0) {
return SecondsToTime(ts.tv_sec) + NSToTime(ts.tv_nsec);
} else {
sysErrorBelch("clock_gettime");
stg_exit(EXIT_FAILURE);
}
return getClockTime(CLOCK_PROCESS_CPUTIME_ID);
}
#endif
......@@ -82,16 +112,7 @@ Time getProcessCPUTime(void)
StgWord64 getMonotonicNSec(void)
{
#if defined(HAVE_CLOCK_GETTIME)
struct timespec ts;
int res;
res = clock_gettime(CLOCK_ID, &ts);
if (res != 0) {
sysErrorBelch("clock_gettime");
stg_exit(EXIT_FAILURE);
}
return (StgWord64)ts.tv_sec * 1000000000 +
(StgWord64)ts.tv_nsec;
return getClockTime(CLOCK_ID);
#elif defined(darwin_HOST_OS)
......
......@@ -34,6 +34,19 @@ getProcessTimes(Time *user, Time *elapsed)
*elapsed = getProcessElapsedTime();
}
Time
getMyThreadCPUTime(void)
{
FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
if (!GetThreadTimes(GetCurrentThread(), &creationTime,
&exitTime, &kernelTime, &userTime)) {
return 0;
}
return fileTimeToRtsTime(userTime);
}
Time
getProcessCPUTime(void)
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment