Add POSIX clock id CLOCK_THREAD_CPUTIME_ID, this can be used to measure
per-thread runtime in user code.
This commit is contained in:
parent
91c3a77c57
commit
00d6ac63cd
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user