summaryrefslogtreecommitdiff
path: root/rts/posix/GetTime.c
diff options
context:
space:
mode:
authorSimon Marlow <marlowsd@gmail.com>2010-09-13 15:38:38 +0000
committerSimon Marlow <marlowsd@gmail.com>2010-09-13 15:38:38 +0000
commit960c61cd95622c39b6e786ad21afa45c3fdb405f (patch)
tree14a8e431642fe906ebf7e1fc81ff8a4aa92e5b27 /rts/posix/GetTime.c
parent4a82c60e35612612921c74b3448e79b0a60de780 (diff)
downloadhaskell-960c61cd95622c39b6e786ad21afa45c3fdb405f.tar.gz
Fix getThreadCPUTime()
ever since the patch "Check with sysconf _POSIX_THREAD_CPUTIME", it has been returning incorrect results, because the sysconf variable to check should have been _SC_THREAD_CPUTIME, not _POSIX_THREAD_CPUTIME.
Diffstat (limited to 'rts/posix/GetTime.c')
-rw-r--r--rts/posix/GetTime.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/rts/posix/GetTime.c b/rts/posix/GetTime.c
index 39465cb664..15643b8319 100644
--- a/rts/posix/GetTime.c
+++ b/rts/posix/GetTime.c
@@ -156,16 +156,25 @@ Ticks getThreadCPUTime(void)
}
return ((usec * TICKS_PER_SECOND) / 1000000);
-#elif !defined(BE_CONSERVATIVE) && defined(HAVE_CLOCK_GETTIME) && defined (_POSIX_THREAD_CPUTIME) && defined(CLOCK_THREAD_CPUTIME_ID) && defined(HAVE_SYSCONF)
- if (sysconf(_POSIX_THREAD_CPUTIME) != -1) {
- // clock_gettime() gives us per-thread CPU time. It isn't
- // reliable on Linux, but it's the best we have.
- struct timespec ts;
- int res;
- res = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
- if (res == 0) {
- return ((Ticks)ts.tv_sec * TICKS_PER_SECOND +
- ((Ticks)ts.tv_nsec * TICKS_PER_SECOND) / 1000000000);
+#elif !defined(BE_CONSERVATIVE) && defined(HAVE_CLOCK_GETTIME) && defined (_SC_THREAD_CPUTIME) && defined(CLOCK_THREAD_CPUTIME_ID) && defined(HAVE_SYSCONF)
+ {
+ static int checked_sysconf = 0;
+ static int sysconf_result = 0;
+
+ if (!checked_sysconf) {
+ sysconf_result = sysconf(_SC_THREAD_CPUTIME);
+ checked_sysconf = 1;
+ }
+ if (sysconf_result != -1) {
+ // clock_gettime() gives us per-thread CPU time. It isn't
+ // reliable on Linux, but it's the best we have.
+ struct timespec ts;
+ int res;
+ res = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
+ if (res == 0) {
+ return ((Ticks)ts.tv_sec * TICKS_PER_SECOND +
+ ((Ticks)ts.tv_nsec * TICKS_PER_SECOND) / 1000000000);
+ }
}
}
#endif