diff options
author | davidxu <davidxu@FreeBSD.org> | 2008-01-18 07:04:42 +0000 |
---|---|---|
committer | davidxu <davidxu@FreeBSD.org> | 2008-01-18 07:04:42 +0000 |
commit | 80ec49a2cfa42274e966bd730533a2befe3290a3 (patch) | |
tree | 8d5530ca045d33f657ca151ad95dc4ef55c87acf /sys/kern/kern_time.c | |
parent | da7596f7454b052e23f50d74051f6ad6f835a42a (diff) | |
download | FreeBSD-src-80ec49a2cfa42274e966bd730533a2befe3290a3.zip FreeBSD-src-80ec49a2cfa42274e966bd730533a2befe3290a3.tar.gz |
Add POSIX clock id CLOCK_THREAD_CPUTIME_ID, this can be used to measure
per-thread runtime in user code.
Diffstat (limited to 'sys/kern/kern_time.c')
-rw-r--r-- | sys/kern/kern_time.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/sys/kern/kern_time.c b/sys/kern/kern_time.c index e220cd6..6df10fa 100644 --- a/sys/kern/kern_time.c +++ b/sys/kern/kern_time.c @@ -201,6 +201,7 @@ kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) { struct timeval sys, user; struct proc *p; + uint64_t runtime, curtime, switchtime; p = td->td_proc; switch (clock_id) { @@ -242,6 +243,15 @@ kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) ats->tv_sec = time_second; ats->tv_nsec = 0; break; + case CLOCK_THREAD_CPUTIME_ID: + critical_enter(); + switchtime = PCPU_GET(switchtime); + curtime = cpu_ticks(); + critical_exit(); + runtime = cputick2usec(td->td_runtime + curtime - switchtime); + ats->tv_sec = runtime / 1000000; + ats->tv_nsec = runtime % 1000000 * 1000; + break; default: return (EINVAL); } @@ -336,6 +346,12 @@ kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) ts->tv_sec = 1; ts->tv_nsec = 0; break; + case CLOCK_THREAD_CPUTIME_ID: + /* sync with cputick2usec */ + ts->tv_nsec = 1000000 / cpu_tickrate(); + if (ts->tv_nsec == 0) + ts->tv_nsec = 1000; + break; default: return (EINVAL); } |