Add several aliases for existing clockid_t names to indicate that the

application wishes to request high precision time stamps be returned:

Alias                           Existing

CLOCK_REALTIME_PRECISE          CLOCK_REALTIME
CLOCK_MONOTONIC_PRECISE         CLOCK_MONOTONIC
CLOCK_UPTIME_PRECISE            CLOCK_UPTIME

Add experimental low-precision clockid_t names corresponding to these
clocks, but implemented using cached timestamps in kernel rather than
a full time counter query.  This offers a minimum update rate of 1/HZ,
but in practice will often be more frequent due to the frequency of
time stamping in the kernel:

New clockid_t name              Approximates existing clockid_t

CLOCK_REALTIME_FAST             CLOCK_REALTIME
CLOCK_MONOTONIC_FAST            CLOCK_MONOTONIC
CLOCK_UPTIME_FAST               CLOCK_UPTIME

Add one additional new clockid_t, CLOCK_SECOND, which returns the
current second without performing a full time counter query or cache
lookup overhead to make sure the cached timestamp is stable.  This is
intended to support very low granularity consumers, such as time(3).

The names, visibility, and implementation of the above are subject
to change, and will not be MFC'd any time soon.  The goal is to
expose lower quality time measurement to applications willing to
sacrifice accuracy in performance critical paths, such as when taking
time stamps for the purpose of rescheduling select() and poll()
timeouts.  Future changes might include retrofitting the time counter
infrastructure to allow the "fast" time query mechanisms to use a
different time counter, rather than a cached time counter (i.e.,
TSC).

NOTE: With different underlying time mechanisms exposed, using
different time query mechanisms in the same application may result in
relative non-monoticity or the appearance of clock stalling for a
single clockid_t, as a cached time stamp queried after a precision
time stamp lookup may be "before" the time returned by the earlier
live time counter query.
This commit is contained in:
Robert Watson 2005-11-27 00:55:18 +00:00
parent c20e7ce621
commit 5e758b9561
2 changed files with 34 additions and 3 deletions

View File

@ -207,9 +207,13 @@ kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
p = td->td_proc;
switch (clock_id) {
case CLOCK_REALTIME:
case CLOCK_REALTIME: /* Default to precise. */
case CLOCK_REALTIME_PRECISE:
nanotime(ats);
break;
case CLOCK_REALTIME_FAST:
getnanotime(ats);
break;
case CLOCK_VIRTUAL:
PROC_LOCK(p);
calcru(p, &user, &sys);
@ -223,10 +227,20 @@ kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
timevaladd(&user, &sys);
TIMEVAL_TO_TIMESPEC(&user, ats);
break;
case CLOCK_MONOTONIC:
case CLOCK_MONOTONIC: /* Default to precise. */
case CLOCK_MONOTONIC_PRECISE:
case CLOCK_UPTIME:
case CLOCK_UPTIME_PRECISE:
nanouptime(ats);
break;
case CLOCK_UPTIME_FAST:
case CLOCK_MONOTONIC_FAST:
getnanouptime(ats);
break;
case CLOCK_SECOND:
ats->tv_sec = time_second;
ats->tv_nsec = 0;
break;
default:
return (EINVAL);
}
@ -307,8 +321,14 @@ kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
ts->tv_sec = 0;
switch (clock_id) {
case CLOCK_REALTIME:
case CLOCK_REALTIME_FAST:
case CLOCK_REALTIME_PRECISE:
case CLOCK_MONOTONIC:
case CLOCK_MONOTONIC_FAST:
case CLOCK_MONOTONIC_PRECISE:
case CLOCK_UPTIME:
case CLOCK_UPTIME_FAST:
case CLOCK_UPTIME_PRECISE:
/*
* Round up the result of the division cheaply by adding 1.
* Rounding up is especially important if rounding down
@ -321,6 +341,10 @@ kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
/* Accurately round up here because we can do so cheaply. */
ts->tv_nsec = (1000000000 + hz - 1) / hz;
break;
case CLOCK_SECOND:
ts->tv_sec = 1;
ts->tv_nsec = 0;
break;
default:
return (EINVAL);
}

View File

@ -238,7 +238,14 @@ struct clockinfo {
#define CLOCK_VIRTUAL 1
#define CLOCK_PROF 2
#define CLOCK_MONOTONIC 4
#define CLOCK_UPTIME 5
#define CLOCK_UPTIME 5 /* FreeBSD-specific. */
#define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */
#define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */
#define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */
#define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */
#define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */
#define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */
#define CLOCK_SECOND 13 /* FreeBSD-specific. */
#endif
#ifndef TIMER_ABSTIME