about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-08-13 08:40:11 -0400
committerRich Felker <dalias@aerifal.cx>2011-08-13 08:40:11 -0400
commitad5759821ced59bbb70eb36d396df2a787141089 (patch)
tree0c05a0e7344b3a79f4f7da1a70b42e2257cced1c
parent4054a135fc0e6c1b7c33f688dcddecee0b2b22d2 (diff)
downloadmusl-ad5759821ced59bbb70eb36d396df2a787141089.tar.gz
musl-ad5759821ced59bbb70eb36d396df2a787141089.tar.xz
musl-ad5759821ced59bbb70eb36d396df2a787141089.zip
fix clock() function
it previously was returning the pseudo-monotonic-realtime clock
returned by times() rather than process cputime. it also violated C
namespace by pulling in times().

we now use clock_gettime() if available because times() has
ridiculously bad resolution. still provide a fallback for ancient
kernels without clock_gettime.
-rw-r--r--src/time/clock.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/time/clock.c b/src/time/clock.c
index 2feddb36..e6e9e776 100644
--- a/src/time/clock.c
+++ b/src/time/clock.c
@@ -1,9 +1,14 @@
 #include <time.h>
 #include <sys/times.h>
 
-/* this function assumes 100 hz linux and corrects for it */
+int __clock_gettime(clockid_t, struct timespec *);
+
 clock_t clock()
 {
+	struct timespec ts;
 	struct tms tms;
-	return (unsigned long)times(&tms)*10000;
+	if (!__clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts))
+		return ts.tv_sec*1000000 + ts.tv_nsec/1000;
+	__syscall(SYS_times, &tms);
+	return (tms.tms_utime + tms.tms_stime)*100;
 }