summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordavidxu <davidxu@FreeBSD.org>2008-01-18 07:04:42 +0000
committerdavidxu <davidxu@FreeBSD.org>2008-01-18 07:04:42 +0000
commit80ec49a2cfa42274e966bd730533a2befe3290a3 (patch)
tree8d5530ca045d33f657ca151ad95dc4ef55c87acf
parentda7596f7454b052e23f50d74051f6ad6f835a42a (diff)
downloadFreeBSD-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.
-rw-r--r--sys/kern/kern_time.c16
-rw-r--r--sys/sys/time.h1
2 files changed, 17 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);
}
diff --git a/sys/sys/time.h b/sys/sys/time.h
index bef7787..3ece2c1 100644
--- a/sys/sys/time.h
+++ b/sys/sys/time.h
@@ -246,6 +246,7 @@ struct clockinfo {
#define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */
#define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */
#define CLOCK_SECOND 13 /* FreeBSD-specific. */
+#define CLOCK_THREAD_CPUTIME_ID 14
#endif
#ifndef TIMER_ABSTIME
OpenPOWER on IntegriCloud