Make timespecadd(3) and friends public

The timespecadd(3) family of macros were imported from NetBSD back in
r35029. However, they were initially guarded by #ifdef _KERNEL. In the
meantime, we have grown at least 28 syscalls that use timespecs in some
way, leading many programs both inside and outside of the base system to
redefine those macros. It's better just to make the definitions public.

Our kernel currently defines two-argument versions of timespecadd and
timespecsub.  NetBSD, OpenBSD, and FreeDesktop.org's libbsd, however, define
three-argument versions.  Solaris also defines a three-argument version, but
only in its kernel.  This revision changes our definition to match the
common three-argument version.

Bump _FreeBSD_version due to the breaking KPI change.

Discussed with:	cem, jilles, ian, bde
Differential Revision:	https://reviews.freebsd.org/D14725
This commit is contained in:
Alan Somers 2018-07-30 15:46:40 +00:00
parent 19fe43f796
commit 6040822c4e
45 changed files with 187 additions and 362 deletions

View File

@ -1,65 +0,0 @@
/* $FreeBSD$ */
/*
* Copyright (c) 1982, 1986, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)time.h 8.5 (Berkeley) 5/4/95
*/
#ifndef _LIBNETBSD_SYS_TIME_H_
#define _LIBNETBSD_SYS_TIME_H_
#include_next <sys/time.h>
/* Operations on timespecs. */
#define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
#define timespeccmp(tsp, usp, cmp) \
(((tsp)->tv_sec == (usp)->tv_sec) ? \
((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
((tsp)->tv_sec cmp (usp)->tv_sec))
#define timespecadd(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
if ((vsp)->tv_nsec >= 1000000000L) { \
(vsp)->tv_sec++; \
(vsp)->tv_nsec -= 1000000000L; \
} \
} while (/* CONSTCOND */ 0)
#define timespecsub(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
if ((vsp)->tv_nsec < 0) { \
(vsp)->tv_sec--; \
(vsp)->tv_nsec += 1000000000L; \
} \
} while (/* CONSTCOND */ 0)
#endif

View File

@ -297,28 +297,6 @@ getdatablk(ufs2_daddr_t blkno, long size, int type)
return (bp); return (bp);
} }
/*
* Timespec operations (from <sys/time.h>).
*/
#define timespecsub(vvp, uvp) \
do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
#define timespecadd(vvp, uvp) \
do { \
(vvp)->tv_sec += (uvp)->tv_sec; \
(vvp)->tv_nsec += (uvp)->tv_nsec; \
if ((vvp)->tv_nsec >= 1000000000) { \
(vvp)->tv_sec++; \
(vvp)->tv_nsec -= 1000000000; \
} \
} while (0)
void void
getblk(struct bufarea *bp, ufs2_daddr_t blk, long size) getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
{ {
@ -337,8 +315,9 @@ getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size); bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
if (debug) { if (debug) {
clock_gettime(CLOCK_REALTIME_PRECISE, &finish); clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
timespecsub(&finish, &start); timespecsub(&finish, &start, &finish);
timespecadd(&readtime[bp->b_type], &finish); timespecadd(&readtime[bp->b_type], &finish,
&readtime[bp->b_type]);
} }
bp->b_bno = dblk; bp->b_bno = dblk;
bp->b_size = size; bp->b_size = size;
@ -509,7 +488,7 @@ IOstats(char *what)
totaldiskreads += diskreads; totaldiskreads += diskreads;
diskreads = 0; diskreads = 0;
for (i = 0; i < BT_NUMBUFTYPES; i++) { for (i = 0; i < BT_NUMBUFTYPES; i++) {
timespecadd(&totalreadtime[i], &readtime[i]); timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
totalreadcnt[i] += readcnt[i]; totalreadcnt[i] += readcnt[i];
readtime[i].tv_sec = readtime[i].tv_nsec = 0; readtime[i].tv_sec = readtime[i].tv_nsec = 0;
readcnt[i] = 0; readcnt[i] = 0;
@ -529,7 +508,7 @@ finalIOstats(void)
diskreads = totaldiskreads; diskreads = totaldiskreads;
startpass = startprog; startpass = startprog;
for (i = 0; i < BT_NUMBUFTYPES; i++) { for (i = 0; i < BT_NUMBUFTYPES; i++) {
timespecadd(&totalreadtime[i], &readtime[i]); timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
totalreadcnt[i] += readcnt[i]; totalreadcnt[i] += readcnt[i];
readtime[i] = totalreadtime[i]; readtime[i] = totalreadtime[i];
readcnt[i] = totalreadcnt[i]; readcnt[i] = totalreadcnt[i];
@ -543,7 +522,7 @@ static void printIOstats(void)
int i; int i;
clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass); clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
timespecsub(&finishpass, &startpass); timespecsub(&finishpass, &startpass, &finishpass);
printf("Running time: %jd.%03ld sec\n", printf("Running time: %jd.%03ld sec\n",
(intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000); (intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
printf("buffer reads by type:\n"); printf("buffer reads by type:\n");

View File

@ -164,7 +164,12 @@ MLINKS+= stdarg.3 va_arg.3 \
MLINKS+= timeradd.3 timerclear.3 \ MLINKS+= timeradd.3 timerclear.3 \
timeradd.3 timercmp.3 \ timeradd.3 timercmp.3 \
timeradd.3 timerisset.3 \ timeradd.3 timerisset.3 \
timeradd.3 timersub.3 timeradd.3 timersub.3 \
timeradd.3 timespecadd.3 \
timeradd.3 timespecsub.3 \
timeradd.3 timespecclear.3 \
timeradd.3 timespecisset.3 \
timeradd.3 timespeccmp.3
MLINKS+= tree.3 RB_EMPTY.3 \ MLINKS+= tree.3 RB_EMPTY.3 \
tree.3 RB_ENTRY.3 \ tree.3 RB_ENTRY.3 \
tree.3 RB_FIND.3 \ tree.3 RB_FIND.3 \

View File

@ -27,7 +27,7 @@
.\" .\"
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd August 11, 1999 .Dd July 30, 2018
.Dt TIMERADD 3 .Dt TIMERADD 3
.Os .Os
.Sh NAME .Sh NAME
@ -35,8 +35,13 @@
.Nm timersub , .Nm timersub ,
.Nm timerclear , .Nm timerclear ,
.Nm timerisset , .Nm timerisset ,
.Nm timercmp .Nm timercmp ,
.Nd operations on timevals .Nm timespecadd ,
.Nm timespecsub ,
.Nm timespecclear ,
.Nm timespecisset ,
.Nm timespeccmp
.Nd operations on timevals and timespecs
.Sh SYNOPSIS .Sh SYNOPSIS
.In sys/time.h .In sys/time.h
.Ft void .Ft void
@ -49,15 +54,31 @@
.Fn timerisset "struct timeval *tvp" .Fn timerisset "struct timeval *tvp"
.Ft int .Ft int
.Fn timercmp "struct timeval *a" "struct timeval *b" CMP .Fn timercmp "struct timeval *a" "struct timeval *b" CMP
.Ft void
.Fn timespecadd "struct timespec *a" "struct timespec *b" "struct timespec *res"
.Ft void
.Fn timespecsub "struct timespec *a" "struct timespec *b" "struct timespec *res"
.Ft void
.Fn timespecclear "struct timespec *ts"
.Ft int
.Fn timespecisset "struct timespec *ts"
.Ft int
.Fn timespeccmp "struct timespec *a" "struct timespec *b" CMP
.Sh DESCRIPTION .Sh DESCRIPTION
These macros are provided for manipulating These macros are provided for manipulating
.Fa timeval .Fa timeval
and
.Fa timespec
structures for use with the structures for use with the
.Xr clock_gettime 2 ,
.Xr clock_settime 2 ,
.Xr gettimeofday 2 .Xr gettimeofday 2
and and
.Xr settimeofday 2 .Xr settimeofday 2
calls. calls.
The structure is defined in The
.Fa timeval
structure is defined in
.In sys/time.h .In sys/time.h
as: as:
.Bd -literal .Bd -literal
@ -66,50 +87,67 @@ struct timeval {
long tv_usec; /* and microseconds */ long tv_usec; /* and microseconds */
}; };
.Ed .Ed
And the
.Fa timespec
structure is defined in
.In time.h
as:
.Bd -literal
struct timespec {
time_t tv_nsec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
.Ed
.Pp .Pp
.Fn timeradd .Fn timeradd
adds the time information stored in and
.Fn timespecadd
add the time information stored in
.Fa a .Fa a
to to
.Fa b .Fa b
and stores the resulting and store the result in
.Vt timeval
in
.Fa res . .Fa res .
The results are simplified such that the value of The results are simplified such that the value of
.Fa res->tv_usec .Fa res->tv_usec
is always less than 1,000,000 (1 second). or
.Fa res->tv_nsec
is always less than 1 second.
.Pp .Pp
.Fn timersub .Fn timersub
subtracts the time information stored in and
.Fn timespecsub
subtract the time information stored in
.Fa b .Fa b
from from
.Fa a .Fa a
and stores the resulting and store the result
.Vt timeval
in in
.Fa res . .Fa res .
.Pp .Pp
.Fn timerclear .Fn timerclear
initializes and
.Fa tvp .Fn timespecclear
to midnight (0 hour) January 1st, 1970 (the Epoch). initialize their argument to midnight (0 hour) January 1st, 1970 (the Epoch).
.Pp .Pp
.Fn timerisset .Fn timerisset
returns true if and
.Fa tvp .Fn timespecisset
is set to any time value other than the Epoch. return true if their argument is set to any time value other than the Epoch.
.Pp .Pp
.Fn timercmp .Fn timercmp
compares and
.Fn timespeccmp
compare
.Fa a .Fa a
to to
.Fa b .Fa b
using the comparison operator given in using the comparison operator given in
.Fa CMP , .Fa CMP ,
and returns the result of that comparison. and return the result of that comparison.
.Sh SEE ALSO .Sh SEE ALSO
.Xr gettimeofday 2 .Xr gettimeofday 2 ,
.Xr clock_gettime 2
.Sh HISTORY .Sh HISTORY
The The
.Fn timeradd .Fn timeradd
@ -117,3 +155,11 @@ family of macros were imported from
.Nx 1.1 , .Nx 1.1 ,
and appeared in and appeared in
.Fx 2.2.6 . .Fx 2.2.6 .
The
.Fn timespecadd
family of macros were imported from
.Nx 1.3
into
.Fx 3.0 ,
though they were not exposed to userland until
.Fx 12.0 .

View File

@ -1183,7 +1183,7 @@ linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
linux_timerfd_clocktime(tfd, &cts); linux_timerfd_clocktime(tfd, &cts);
*ots = tfd->tfd_time; *ots = tfd->tfd_time;
if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) { if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
timespecsub(&ots->it_value, &cts); timespecsub(&ots->it_value, &cts, &ots->it_value);
if (ots->it_value.tv_sec < 0 || if (ots->it_value.tv_sec < 0 ||
(ots->it_value.tv_sec == 0 && (ots->it_value.tv_sec == 0 &&
ots->it_value.tv_nsec == 0)) { ots->it_value.tv_nsec == 0)) {
@ -1265,9 +1265,10 @@ linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args
linux_timerfd_clocktime(tfd, &cts); linux_timerfd_clocktime(tfd, &cts);
ts = nts.it_value; ts = nts.it_value;
if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) { if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
timespecadd(&tfd->tfd_time.it_value, &cts); timespecadd(&tfd->tfd_time.it_value, &cts,
&tfd->tfd_time.it_value);
} else { } else {
timespecsub(&ts, &cts); timespecsub(&ts, &cts, &ts);
} }
TIMESPEC_TO_TIMEVAL(&tv, &ts); TIMESPEC_TO_TIMEVAL(&tv, &ts);
callout_reset(&tfd->tfd_callout, tvtohz(&tv), callout_reset(&tfd->tfd_callout, tvtohz(&tv),
@ -1303,13 +1304,13 @@ linux_timerfd_expire(void *arg)
if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) { if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
if (timespecisset(&tfd->tfd_time.it_interval)) if (timespecisset(&tfd->tfd_time.it_interval))
timespecadd(&tfd->tfd_time.it_value, timespecadd(&tfd->tfd_time.it_value,
&tfd->tfd_time.it_interval); &tfd->tfd_time.it_interval,
&tfd->tfd_time.it_value);
else else
/* single shot timer */ /* single shot timer */
timespecclear(&tfd->tfd_time.it_value); timespecclear(&tfd->tfd_time.it_value);
if (timespecisset(&tfd->tfd_time.it_value)) { if (timespecisset(&tfd->tfd_time.it_value)) {
ts = tfd->tfd_time.it_value; timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
timespecsub(&ts, &cts);
TIMESPEC_TO_TIMEVAL(&tv, &ts); TIMESPEC_TO_TIMEVAL(&tv, &ts);
callout_reset(&tfd->tfd_callout, tvtohz(&tv), callout_reset(&tfd->tfd_callout, tvtohz(&tv),
linux_timerfd_expire, tfd); linux_timerfd_expire, tfd);
@ -1319,8 +1320,7 @@ linux_timerfd_expire(void *arg)
selwakeup(&tfd->tfd_sel); selwakeup(&tfd->tfd_sel);
wakeup(&tfd->tfd_count); wakeup(&tfd->tfd_count);
} else if (timespecisset(&tfd->tfd_time.it_value)) { } else if (timespecisset(&tfd->tfd_time.it_value)) {
ts = tfd->tfd_time.it_value; timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
timespecsub(&ts, &cts);
TIMESPEC_TO_TIMEVAL(&tv, &ts); TIMESPEC_TO_TIMEVAL(&tv, &ts);
callout_reset(&tfd->tfd_callout, tvtohz(&tv), callout_reset(&tfd->tfd_callout, tvtohz(&tv),
linux_timerfd_expire, tfd); linux_timerfd_expire, tfd);

View File

@ -290,10 +290,10 @@ futex_copyin_timeout(int op, struct l_timespec *luts, int clockrt,
return (error); return (error);
if (clockrt) { if (clockrt) {
nanotime(&kts); nanotime(&kts);
timespecsub(ts, &kts); timespecsub(ts, &kts, ts);
} else if (op == LINUX_FUTEX_WAIT_BITSET) { } else if (op == LINUX_FUTEX_WAIT_BITSET) {
nanouptime(&kts); nanouptime(&kts);
timespecsub(ts, &kts); timespecsub(ts, &kts, ts);
} }
return (error); return (error);
} }

View File

@ -2382,8 +2382,8 @@ linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
if (error == 0 && args->tsp != NULL) { if (error == 0 && args->tsp != NULL) {
if (td->td_retval[0]) { if (td->td_retval[0]) {
nanotime(&ts1); nanotime(&ts1);
timespecsub(&ts1, &ts0); timespecsub(&ts1, &ts0, &ts1);
timespecsub(&uts, &ts1); timespecsub(&uts, &ts1, &uts);
if (uts.tv_sec < 0) if (uts.tv_sec < 0)
timespecclear(&uts); timespecclear(&uts);
} else } else

View File

@ -1463,7 +1463,7 @@ linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
if (error != 0) if (error != 0)
return (error); return (error);
getnanotime(&tts); getnanotime(&tts);
timespecadd(&tts, &ts); timespecadd(&tts, &ts, &tts);
} }
msg = PTRIN(args->msg); msg = PTRIN(args->msg);
@ -1492,7 +1492,7 @@ linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
*/ */
if (args->timeout) { if (args->timeout) {
getnanotime(&ts); getnanotime(&ts);
timespecsub(&ts, &tts); timespecsub(&ts, &tts, &ts);
if (!timespecisset(&ts) || ts.tv_sec > 0) if (!timespecisset(&ts) || ts.tv_sec > 0)
break; break;
} }

View File

@ -76,9 +76,7 @@ timespec_sub(struct timespec lhs, struct timespec rhs)
{ {
struct timespec ts; struct timespec ts;
ts.tv_sec = lhs.tv_sec; timespecsub(&lhs, &rhs, &ts);
ts.tv_nsec = lhs.tv_nsec;
timespecsub(&ts, &rhs);
return ts; return ts;
} }

View File

@ -229,7 +229,7 @@ acpi_cmbat_info_expired(struct timespec *lastupdated)
return (TRUE); return (TRUE);
getnanotime(&curtime); getnanotime(&curtime);
timespecsub(&curtime, lastupdated); timespecsub(&curtime, lastupdated, &curtime);
return (curtime.tv_sec < 0 || return (curtime.tv_sec < 0 ||
curtime.tv_sec > acpi_battery_get_info_expire()); curtime.tv_sec > acpi_battery_get_info_expire());
} }

View File

@ -170,7 +170,7 @@ acpi_smbat_info_expired(struct timespec *lastupdated)
return (TRUE); return (TRUE);
getnanotime(&curtime); getnanotime(&curtime);
timespecsub(&curtime, lastupdated); timespecsub(&curtime, lastupdated, &curtime);
return (curtime.tv_sec < 0 || return (curtime.tv_sec < 0 ||
curtime.tv_sec > acpi_battery_get_info_expire()); curtime.tv_sec > acpi_battery_get_info_expire());
} }

View File

@ -535,7 +535,7 @@ acpi_tz_monitor(void *Context)
(newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) { (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
getnanotime(&curtime); getnanotime(&curtime);
timespecsub(&curtime, &sc->tz_cooling_started); timespecsub(&curtime, &sc->tz_cooling_started, &curtime);
if (curtime.tv_sec < acpi_tz_min_runtime) if (curtime.tv_sec < acpi_tz_min_runtime)
newactive = sc->tz_active; newactive = sc->tz_active;
} }

View File

@ -1135,8 +1135,8 @@ static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
#undef EXIT_COND #undef EXIT_COND
if (timeout) { if (timeout) {
timespecsub(&now, &before); timespecsub(&now, &before, &now);
timespecsub(timeout, &now); timespecsub(timeout, &now, timeout);
} }
switch (end) { switch (end) {

View File

@ -3102,8 +3102,7 @@ static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
mtx_assert(&mchdev_lock, MA_OWNED); mtx_assert(&mchdev_lock, MA_OWNED);
nanotime(&now); nanotime(&now);
diff1 = now; timespecsub(&now, &dev_priv->ips.last_time2, &diff1);
timespecsub(&diff1, &dev_priv->ips.last_time2);
/* Don't divide by 0 */ /* Don't divide by 0 */
diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000; diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;

View File

@ -163,7 +163,7 @@ efirtc_settime(device_t dev, struct timespec *ts)
*/ */
ts->tv_sec -= utc_offset(); ts->tv_sec -= utc_offset();
if (!efirtc_zeroes_subseconds) if (!efirtc_zeroes_subseconds)
timespecadd(ts, &efirtc_resadj); timespecadd(ts, &efirtc_resadj, ts);
clock_ts_to_ct(ts, &ct); clock_ts_to_ct(ts, &ct);
clock_dbgprint_ct(dev, CLOCK_DBG_WRITE, &ct); clock_dbgprint_ct(dev, CLOCK_DBG_WRITE, &ct);

View File

@ -4083,8 +4083,9 @@ uint64_t
isp_nanotime_sub(struct timespec *b, struct timespec *a) isp_nanotime_sub(struct timespec *b, struct timespec *a)
{ {
uint64_t elapsed; uint64_t elapsed;
struct timespec x = *b; struct timespec x;
timespecsub(&x, a);
timespecsub(b, a, &x);
elapsed = GET_NANOSEC(&x); elapsed = GET_NANOSEC(&x);
if (elapsed == 0) if (elapsed == 0)
elapsed++; elapsed++;

View File

@ -171,14 +171,14 @@ joyread(struct cdev *dev, struct uio *uio, int flag)
nanotime(&t); nanotime(&t);
end.tv_sec = 0; end.tv_sec = 0;
end.tv_nsec = joy->timeout[joypart(dev)] * 1000; end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
timespecadd(&end, &t); timespecadd(&end, &t, &end);
for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t)) for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t))
; /* nothing */ ; /* nothing */
bus_space_write_1 (bt, port, 0, 0xff); bus_space_write_1 (bt, port, 0, 0xff);
nanotime(&start); nanotime(&start);
end.tv_sec = 0; end.tv_sec = 0;
end.tv_nsec = joy->timeout[joypart(dev)] * 1000; end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
timespecadd(&end, &start); timespecadd(&end, &start, &end);
t = start; t = start;
timespecclear(&x); timespecclear(&x);
timespecclear(&y); timespecclear(&y);
@ -200,12 +200,12 @@ joyread(struct cdev *dev, struct uio *uio, int flag)
enable_intr (); enable_intr ();
#endif #endif
if (timespecisset(&x)) { if (timespecisset(&x)) {
timespecsub(&x, &start); timespecsub(&x, &start, &x);
c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000; c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
} else } else
c.x = 0x80000000; c.x = 0x80000000;
if (timespecisset(&y)) { if (timespecisset(&y)) {
timespecsub(&y, &start); timespecsub(&y, &start, &y);
c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000; c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
} else } else
c.y = 0x80000000; c.y = 0x80000000;

View File

@ -262,7 +262,7 @@ xentimer_gettime(device_t dev, struct timespec *ts)
timespecclear(ts); timespecclear(ts);
xen_fetch_wallclock(ts); xen_fetch_wallclock(ts);
xen_fetch_uptime(&u_ts); xen_fetch_uptime(&u_ts);
timespecadd(ts, &u_ts); timespecadd(ts, &u_ts, ts);
return (0); return (0);
} }

View File

@ -1261,8 +1261,7 @@ kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
timevalid = 1; timevalid = 1;
getnanouptime(&rts); getnanouptime(&rts);
ets = rts; timespecadd(&rts, timeout, &ets);
timespecadd(&ets, timeout);
} }
} }
ksiginfo_init(ksi); ksiginfo_init(ksi);
@ -1302,8 +1301,7 @@ kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
error = EAGAIN; error = EAGAIN;
break; break;
} }
ts = ets; timespecsub(&ets, &rts, &ts);
timespecsub(&ts, &rts);
TIMESPEC_TO_TIMEVAL(&tv, &ts); TIMESPEC_TO_TIMEVAL(&tv, &ts);
timo = tvtohz(&tv); timo = tvtohz(&tv);
} else { } else {

View File

@ -1847,7 +1847,7 @@ pps_event(struct pps_state *pps, int event)
*tsp = ts; *tsp = ts;
if (foff) { if (foff) {
timespecadd(tsp, osp); timespecadd(tsp, osp, tsp);
if (tsp->tv_nsec < 0) { if (tsp->tv_nsec < 0) {
tsp->tv_nsec += 1000000000; tsp->tv_nsec += 1000000000;
tsp->tv_sec -= 1; tsp->tv_sec -= 1;

View File

@ -543,7 +543,7 @@ kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
atomic_load_acq_int(&rtc_generation); atomic_load_acq_int(&rtc_generation);
error = kern_clock_gettime(td, clock_id, &now); error = kern_clock_gettime(td, clock_id, &now);
KASSERT(error == 0, ("kern_clock_gettime: %d", error)); KASSERT(error == 0, ("kern_clock_gettime: %d", error));
timespecsub(&ts, &now); timespecsub(&ts, &now, &ts);
} }
if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) { if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
error = EWOULDBLOCK; error = EWOULDBLOCK;
@ -1520,7 +1520,7 @@ realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
realtimer_clocktime(it->it_clockid, &cts); realtimer_clocktime(it->it_clockid, &cts);
*ovalue = it->it_time; *ovalue = it->it_time;
if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
timespecsub(&ovalue->it_value, &cts); timespecsub(&ovalue->it_value, &cts, &ovalue->it_value);
if (ovalue->it_value.tv_sec < 0 || if (ovalue->it_value.tv_sec < 0 ||
(ovalue->it_value.tv_sec == 0 && (ovalue->it_value.tv_sec == 0 &&
ovalue->it_value.tv_nsec == 0)) { ovalue->it_value.tv_nsec == 0)) {
@ -1561,9 +1561,10 @@ realtimer_settime(struct itimer *it, int flags,
ts = val.it_value; ts = val.it_value;
if ((flags & TIMER_ABSTIME) == 0) { if ((flags & TIMER_ABSTIME) == 0) {
/* Convert to absolute time. */ /* Convert to absolute time. */
timespecadd(&it->it_time.it_value, &cts); timespecadd(&it->it_time.it_value, &cts,
&it->it_time.it_value);
} else { } else {
timespecsub(&ts, &cts); timespecsub(&ts, &cts, &ts);
/* /*
* We don't care if ts is negative, tztohz will * We don't care if ts is negative, tztohz will
* fix it. * fix it.
@ -1631,22 +1632,23 @@ realtimer_expire(void *arg)
if (timespeccmp(&cts, &it->it_time.it_value, >=)) { if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
if (timespecisset(&it->it_time.it_interval)) { if (timespecisset(&it->it_time.it_interval)) {
timespecadd(&it->it_time.it_value, timespecadd(&it->it_time.it_value,
&it->it_time.it_interval); &it->it_time.it_interval,
&it->it_time.it_value);
while (timespeccmp(&cts, &it->it_time.it_value, >=)) { while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
if (it->it_overrun < INT_MAX) if (it->it_overrun < INT_MAX)
it->it_overrun++; it->it_overrun++;
else else
it->it_ksi.ksi_errno = ERANGE; it->it_ksi.ksi_errno = ERANGE;
timespecadd(&it->it_time.it_value, timespecadd(&it->it_time.it_value,
&it->it_time.it_interval); &it->it_time.it_interval,
&it->it_time.it_value);
} }
} else { } else {
/* single shot timer ? */ /* single shot timer ? */
timespecclear(&it->it_time.it_value); timespecclear(&it->it_time.it_value);
} }
if (timespecisset(&it->it_time.it_value)) { if (timespecisset(&it->it_time.it_value)) {
ts = it->it_time.it_value; timespecsub(&it->it_time.it_value, &cts, &ts);
timespecsub(&ts, &cts);
TIMESPEC_TO_TIMEVAL(&tv, &ts); TIMESPEC_TO_TIMEVAL(&tv, &ts);
callout_reset(&it->it_callout, tvtohz(&tv), callout_reset(&it->it_callout, tvtohz(&tv),
realtimer_expire, it); realtimer_expire, it);
@ -1658,7 +1660,7 @@ realtimer_expire(void *arg)
itimer_leave(it); itimer_leave(it);
} else if (timespecisset(&it->it_time.it_value)) { } else if (timespecisset(&it->it_time.it_value)) {
ts = it->it_time.it_value; ts = it->it_time.it_value;
timespecsub(&ts, &cts); timespecsub(&ts, &cts, &ts);
TIMESPEC_TO_TIMEVAL(&tv, &ts); TIMESPEC_TO_TIMEVAL(&tv, &ts);
callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
it); it);

View File

@ -772,8 +772,7 @@ abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
if (!absolute) { if (!absolute) {
timo->is_abs_real = false; timo->is_abs_real = false;
abs_timeout_update(timo); abs_timeout_update(timo);
timo->end = timo->cur; timespecadd(&timo->cur, timeout, &timo->end);
timespecadd(&timo->end, timeout);
} else { } else {
timo->end = *timeout; timo->end = *timeout;
timo->is_abs_real = clockid == CLOCK_REALTIME || timo->is_abs_real = clockid == CLOCK_REALTIME ||
@ -811,8 +810,7 @@ abs_timeout_gethz(struct abs_timeout *timo)
if (timespeccmp(&timo->end, &timo->cur, <=)) if (timespeccmp(&timo->end, &timo->cur, <=))
return (-1); return (-1);
tts = timo->end; timespecsub(&timo->end, &timo->cur, &tts);
timespecsub(&tts, &timo->cur);
return (tstohz(&tts)); return (tstohz(&tts));
} }
@ -3247,8 +3245,8 @@ do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
error = EINTR; error = EINTR;
if (error == EINTR) { if (error == EINTR) {
abs_timeout_update(&timo); abs_timeout_update(&timo);
timeout->_timeout = timo.end; timespecsub(&timo.end, &timo.cur,
timespecsub(&timeout->_timeout, &timo.cur); &timeout->_timeout);
} }
} }
} }

View File

@ -144,7 +144,7 @@ settime_task_func(void *arg, int pending)
getnanotime(&ts); getnanotime(&ts);
if (!(rtc->flags & CLOCKF_SETTIME_NO_ADJ)) { if (!(rtc->flags & CLOCKF_SETTIME_NO_ADJ)) {
ts.tv_sec -= utc_offset(); ts.tv_sec -= utc_offset();
timespecadd(&ts, &rtc->resadj); timespecadd(&ts, &rtc->resadj, &ts);
} }
} else { } else {
ts.tv_sec = 0; ts.tv_sec = 0;
@ -301,7 +301,7 @@ read_clocks(struct timespec *ts, bool debug_read)
continue; continue;
} }
if (!(rtc->flags & CLOCKF_GETTIME_NO_ADJ)) { if (!(rtc->flags & CLOCKF_GETTIME_NO_ADJ)) {
timespecadd(ts, &rtc->resadj); timespecadd(ts, &rtc->resadj, ts);
ts->tv_sec += utc_offset(); ts->tv_sec += utc_offset();
} }
if (!debug_read) { if (!debug_read) {

View File

@ -1735,9 +1735,8 @@ mqueue_send(struct mqueue *mq, const char *msg_ptr,
goto bad; goto bad;
} }
for (;;) { for (;;) {
ts2 = *abs_timeout;
getnanotime(&ts); getnanotime(&ts);
timespecsub(&ts2, &ts); timespecsub(abs_timeout, &ts, &ts2);
if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
error = ETIMEDOUT; error = ETIMEDOUT;
break; break;
@ -1887,9 +1886,8 @@ mqueue_receive(struct mqueue *mq, char *msg_ptr,
} }
for (;;) { for (;;) {
ts2 = *abs_timeout;
getnanotime(&ts); getnanotime(&ts);
timespecsub(&ts2, &ts); timespecsub(abs_timeout, &ts, &ts2);
if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
error = ETIMEDOUT; error = ETIMEDOUT;
return (error); return (error);

View File

@ -843,7 +843,7 @@ kern_sem_wait(struct thread *td, semid_t id, int tryflag,
for (;;) { for (;;) {
ts1 = *abstime; ts1 = *abstime;
getnanotime(&ts2); getnanotime(&ts2);
timespecsub(&ts1, &ts2); timespecsub(&ts1, &ts2, &ts1);
TIMESPEC_TO_TIMEVAL(&tv, &ts1); TIMESPEC_TO_TIMEVAL(&tv, &ts1);
if (tv.tv_sec < 0) { if (tv.tv_sec < 0) {
error = ETIMEDOUT; error = ETIMEDOUT;

View File

@ -248,7 +248,7 @@ jzsmb_transfer_read(device_t dev, struct iic_msg *msg)
SMB_WRITE(sc, SMBDC, SMBDC_CMD); SMB_WRITE(sc, SMBDC, SMBDC_CMD);
for (;;) { for (;;) {
getnanouptime(&diff); getnanouptime(&diff);
timespecsub(&diff, &start); timespecsub(&diff, &start, &diff);
if ((SMB_READ(sc, SMBST) & SMBST_RFNE) != 0) { if ((SMB_READ(sc, SMBST) & SMBST_RFNE) != 0) {
msg->buf[msg->len - resid] = msg->buf[msg->len - resid] =
SMB_READ(sc, SMBDC) & SMBDC_DAT; SMB_READ(sc, SMBDC) & SMBDC_DAT;
@ -293,7 +293,7 @@ jzsmb_transfer_write(device_t dev, struct iic_msg *msg, int stop_hold)
for (resid = msg->len; resid > 0; resid--) { for (resid = msg->len; resid > 0; resid--) {
for (;;) { for (;;) {
getnanouptime(&diff); getnanouptime(&diff);
timespecsub(&diff, &start); timespecsub(&diff, &start, &diff);
if ((SMB_READ(sc, SMBST) & SMBST_TFNF) != 0) { if ((SMB_READ(sc, SMBST) & SMBST_TFNF) != 0) {
SMB_WRITE(sc, SMBDC, SMB_WRITE(sc, SMBDC,
msg->buf[msg->len - resid]); msg->buf[msg->len - resid]);

View File

@ -1193,7 +1193,7 @@ ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
mbuf_tstmp2timespec(m, &ts); mbuf_tstmp2timespec(m, &ts);
getboottimebin(&boottimebin); getboottimebin(&boottimebin);
bintime2timespec(&boottimebin, &ts1); bintime2timespec(&boottimebin, &ts1);
timespecadd(&ts, &ts1); timespecadd(&ts, &ts1, &ts);
} else { } else {
nanotime(&ts); nanotime(&ts);
} }

View File

@ -1267,7 +1267,7 @@ ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
mbuf_tstmp2timespec(m, &t.ts); mbuf_tstmp2timespec(m, &t.ts);
getboottimebin(&boottimebin); getboottimebin(&boottimebin);
bintime2timespec(&boottimebin, &ts1); bintime2timespec(&boottimebin, &ts1);
timespecadd(&t.ts, &ts1); timespecadd(&t.ts, &ts1, &t.ts);
} else { } else {
nanotime(&t.ts); nanotime(&t.ts);
} }

View File

@ -557,9 +557,9 @@ smb_iod_sendall(struct smbiod *iod)
break; break;
case SMBRQ_SENT: case SMBRQ_SENT:
SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout); SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout);
timespecadd(&tstimeout, &tstimeout); timespecadd(&tstimeout, &tstimeout, &tstimeout);
getnanotime(&ts); getnanotime(&ts);
timespecsub(&ts, &tstimeout); timespecsub(&ts, &tstimeout, &ts);
if (timespeccmp(&ts, &rqp->sr_timesent, >)) { if (timespeccmp(&ts, &rqp->sr_timesent, >)) {
smb_iod_rqprocessed(rqp, ETIMEDOUT); smb_iod_rqprocessed(rqp, ETIMEDOUT);
} }
@ -630,7 +630,7 @@ smb_iod_main(struct smbiod *iod)
#if 0 #if 0
if (iod->iod_state == SMBIOD_ST_VCACTIVE) { if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
getnanotime(&tsnow); getnanotime(&tsnow);
timespecsub(&tsnow, &iod->iod_pingtimo); timespecsub(&tsnow, &iod->iod_pingtimo, &tsnow);
if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) { if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) {
smb_smb_echo(vcp, &iod->iod_scred); smb_smb_echo(vcp, &iod->iod_scred);
} }

View File

@ -546,15 +546,14 @@ smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
if (error) if (error)
return error; return error;
getnanotime(&ts2); getnanotime(&ts2);
timespecsub(&ts2, &ts1); timespecsub(&ts2, &ts1, &ts2);
if (ts2.tv_sec == 0) { if (ts2.tv_sec == 0) {
ts2.tv_sec = 1; ts2.tv_sec = 1;
ts2.tv_nsec = 0; ts2.tv_nsec = 0;
} }
nbp->nbp_timo = ts2; timespecadd(&ts2, &ts2, &nbp->nbp_timo);
timespecadd(&nbp->nbp_timo, &ts2); timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
timespecadd(&nbp->nbp_timo, &ts2); timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo); /* * 4 */
timespecadd(&nbp->nbp_timo, &ts2); /* * 4 */
error = nbssn_rq_request(nbp, td); error = nbssn_rq_request(nbp, td);
if (error) if (error)
smb_nbst_disconnect(vcp, td); smb_nbst_disconnect(vcp, td);

View File

@ -1188,7 +1188,7 @@ crypto_tstat(struct cryptotstat *ts, struct bintime *bt)
if (u < delta.frac) if (u < delta.frac)
delta.sec--; delta.sec--;
bintime2timespec(&delta, &t); bintime2timespec(&delta, &t);
timespecadd(&ts->acc, &t); timespecadd(&ts->acc, &t, &ts->acc);
if (timespeccmp(&t, &ts->min, <)) if (timespeccmp(&t, &ts->min, <))
ts->min = t; ts->min = t;
if (timespeccmp(&t, &ts->max, >)) if (timespeccmp(&t, &ts->max, >))

View File

@ -60,7 +60,7 @@
* in the range 5 to 9. * in the range 5 to 9.
*/ */
#undef __FreeBSD_version #undef __FreeBSD_version
#define __FreeBSD_version 1200075 /* Master, propagated to newvers */ #define __FreeBSD_version 1200076 /* Master, propagated to newvers */
/* /*
* __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD, * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,

View File

@ -306,6 +306,7 @@ tvtosbt(struct timeval _tv)
#define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \ #define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \
((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000)) ((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000))
#endif
/* Operations on timespecs */ /* Operations on timespecs */
#define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
#define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
@ -313,25 +314,28 @@ tvtosbt(struct timeval _tv)
(((tvp)->tv_sec == (uvp)->tv_sec) ? \ (((tvp)->tv_sec == (uvp)->tv_sec) ? \
((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec)) ((tvp)->tv_sec cmp (uvp)->tv_sec))
#define timespecadd(vvp, uvp) \
#define timespecadd(tsp, usp, vsp) \
do { \ do { \
(vvp)->tv_sec += (uvp)->tv_sec; \ (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
(vvp)->tv_nsec += (uvp)->tv_nsec; \ (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
if ((vvp)->tv_nsec >= 1000000000) { \ if ((vsp)->tv_nsec >= 1000000000L) { \
(vvp)->tv_sec++; \ (vsp)->tv_sec++; \
(vvp)->tv_nsec -= 1000000000; \ (vsp)->tv_nsec -= 1000000000L; \
} \ } \
} while (0) } while (0)
#define timespecsub(vvp, uvp) \ #define timespecsub(tsp, usp, vsp) \
do { \ do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \ (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \ (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \ if ((vsp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \ (vsp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \ (vsp)->tv_nsec += 1000000000L; \
} \ } \
} while (0) } while (0)
#ifdef _KERNEL
/* Operations on timevals. */ /* Operations on timevals. */
#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)

View File

@ -694,7 +694,7 @@ ffs_snapshot(mp, snapfile)
vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR); vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR);
if (collectsnapstats && starttime.tv_sec > 0) { if (collectsnapstats && starttime.tv_sec > 0) {
nanotime(&endtime); nanotime(&endtime);
timespecsub(&endtime, &starttime); timespecsub(&endtime, &starttime, &endtime);
printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n", printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec, vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
endtime.tv_nsec / 1000000, redo, fs->fs_ncg); endtime.tv_nsec / 1000000, redo, fs->fs_ncg);

View File

@ -524,8 +524,7 @@ extern struct timespec dmar_hw_timeout;
} else { \ } else { \
forever = false; \ forever = false; \
nanouptime(&curr); \ nanouptime(&curr); \
last = curr; \ timespecadd(&curr, &dmar_hw_timeout, &last); \
timespecadd(&last, &dmar_hw_timeout); \
} \ } \
for (;;) { \ for (;;) { \
if (cond) { \ if (cond) { \

View File

@ -55,35 +55,6 @@ __FBSDID("$FreeBSD$");
#include "test.h" #include "test.h"
/* Cut and pasted from kernel header, bah! */
/* Operations on timespecs */
#define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
#define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
#define timespeccmp(tvp, uvp, cmp) \
(((tvp)->tv_sec == (uvp)->tv_sec) ? \
((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec))
#define timespecadd(vvp, uvp) \
do { \
(vvp)->tv_sec += (uvp)->tv_sec; \
(vvp)->tv_nsec += (uvp)->tv_nsec; \
if ((vvp)->tv_nsec >= 1000000000) { \
(vvp)->tv_sec++; \
(vvp)->tv_nsec -= 1000000000; \
} \
} while (0)
#define timespecsub(vvp, uvp) \
do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
#define TEST_PATH "/tmp/posixsem_regression_test" #define TEST_PATH "/tmp/posixsem_regression_test"
#define ELAPSED(elapsed, limit) (abs((elapsed) - (limit)) < 100) #define ELAPSED(elapsed, limit) (abs((elapsed) - (limit)) < 100)
@ -791,7 +762,7 @@ timedwait(semid_t id, u_int msec, u_int *delta, int error)
} }
end.tv_sec = msec / 1000; end.tv_sec = msec / 1000;
end.tv_nsec = msec % 1000 * 1000000; end.tv_nsec = msec % 1000 * 1000000;
timespecadd(&end, &start); timespecadd(&end, &start, &end);
if (ksem_timedwait(id, &end) < 0) { if (ksem_timedwait(id, &end) < 0) {
if (errno != error) { if (errno != error) {
fail_errno("ksem_timedwait"); fail_errno("ksem_timedwait");
@ -805,7 +776,7 @@ timedwait(semid_t id, u_int msec, u_int *delta, int error)
fail_errno("clock_gettime(CLOCK_REALTIME)"); fail_errno("clock_gettime(CLOCK_REALTIME)");
return (-1); return (-1);
} }
timespecsub(&end, &start); timespecsub(&end, &start, &end);
*delta = end.tv_nsec / 1000000; *delta = end.tv_nsec / 1000000;
*delta += end.tv_sec * 1000; *delta += end.tv_sec * 1000;
return (0); return (0);
@ -944,7 +915,7 @@ testwait(semid_t id, u_int *delta)
fail_errno("clock_gettime(CLOCK_REALTIME)"); fail_errno("clock_gettime(CLOCK_REALTIME)");
return (-1); return (-1);
} }
timespecsub(&end, &start); timespecsub(&end, &start, &end);
*delta = end.tv_nsec / 1000000; *delta = end.tv_nsec / 1000000;
*delta += end.tv_sec * 1000; *delta += end.tv_sec * 1000;
return (0); return (0);

View File

@ -106,31 +106,6 @@ struct rtt {
#define NSEC_MAX 1000000000L #define NSEC_MAX 1000000000L
#define NSEC_IN_USEC 1000L #define NSEC_IN_USEC 1000L
#define timespecsub2(r, v, u) \
do { \
SEC(r) = SEC(v) - SEC(u); \
NSEC(r) = NSEC(v) - NSEC(u); \
if (NSEC(r) < 0 && (SEC(r) > 0 || NSEC(r) <= -NSEC_MAX)) { \
SEC(r)--; \
NSEC(r) += NSEC_MAX; \
} \
} while (0);
#define timespecadd2(r, v, u) \
do { \
SEC(r) = SEC(v) + SEC(u); \
NSEC(r) = NSEC(v) + NSEC(u); \
if (NSEC(r) >= NSEC_MAX) { \
SEC(r)++; \
NSEC(r) -= NSEC_MAX; \
} \
} while (0);
#define timespeccmp(t, c, u) \
((SEC(t) == SEC(u)) ? \
(NSEC(t) c NSEC(u)) : \
(SEC(t) c SEC(u)))
#define timeval2timespec(tv, ts) \ #define timeval2timespec(tv, ts) \
do { \ do { \
SEC(ts) = (tv)->tv_sec; \ SEC(ts) = (tv)->tv_sec; \
@ -536,10 +511,10 @@ static void
calc_rtt(struct test_pkt *tpp, struct rtt *rttp) calc_rtt(struct test_pkt *tpp, struct rtt *rttp)
{ {
timespecsub2(&rttp->a2b, &tpp->tss[1].recvd, &tpp->tss[0].sent); timespecsub(&tpp->tss[1].recvd, &tpp->tss[0].sent, &rttp->a2b);
timespecsub2(&rttp->b2a, &tpp->tss[0].recvd, &tpp->tss[1].sent); timespecsub(&tpp->tss[0].recvd, &tpp->tss[1].sent, &rttp->b2a);
timespecadd2(&rttp->a2b_b2a, &rttp->a2b, &rttp->b2a); timespecadd(&rttp->a2b, &rttp->b2a, &rttp->a2b_b2a);
timespecsub2(&rttp->e2e, &tpp->tss[0].recvd, &tpp->tss[0].sent); timespecsub(&tpp->tss[0].recvd, &tpp->tss[0].sent, &rttp->e2e);
} }
static void static void
@ -604,13 +579,13 @@ test_run(int ts_type, int use_ipv6, int use_recvmsg, const char *name)
continue; continue;
} }
calc_rtt(&test_ctx.test_pkts[i], &rtt); calc_rtt(&test_ctx.test_pkts[i], &rtt);
if (!timespeccmp(&rtt.e2e, >, &rtt.a2b_b2a)) if (!timespeccmp(&rtt.e2e, &rtt.a2b_b2a, >))
errx(1, "end-to-end trip time is too small"); errx(1, "end-to-end trip time is too small");
if (!timespeccmp(&rtt.e2e, <, &max_ts)) if (!timespeccmp(&rtt.e2e, &max_ts, <))
errx(1, "end-to-end trip time is too large"); errx(1, "end-to-end trip time is too large");
if (!timespeccmp(&rtt.a2b, >, &zero_ts)) if (!timespeccmp(&rtt.a2b, &zero_ts, >))
errx(1, "A2B trip time is not positive"); errx(1, "A2B trip time is not positive");
if (!timespeccmp(&rtt.b2a, >, &zero_ts)) if (!timespeccmp(&rtt.b2a, &zero_ts, >))
errx(1, "B2A trip time is not positive"); errx(1, "B2A trip time is not positive");
} }
teardown_udp(&test_ctx); teardown_udp(&test_ctx);

View File

@ -35,20 +35,6 @@ __FBSDID("$FreeBSD$");
static const struct timeval max_diff_tv = {.tv_sec = 1, .tv_usec = 0}; static const struct timeval max_diff_tv = {.tv_sec = 1, .tv_usec = 0};
static const struct timespec max_diff_ts = {.tv_sec = 1, .tv_nsec = 0}; static const struct timespec max_diff_ts = {.tv_sec = 1, .tv_nsec = 0};
#define timespeccmp(tvp, uvp, cmp) \
(((tvp)->tv_sec == (uvp)->tv_sec) ? \
((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec))
#define timespecsub(vvp, uvp) \
do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
int int
uc_check_bintime(const struct bintime *mt) uc_check_bintime(const struct bintime *mt)
{ {
@ -79,7 +65,7 @@ uc_check_timespec_real(const struct timespec *bt)
if (clock_gettime(CLOCK_REALTIME, &ct) < 0) if (clock_gettime(CLOCK_REALTIME, &ct) < 0)
return (-1); return (-1);
timespecsub(&ct, bt); timespecsub(&ct, bt, &ct);
if (!timespeccmp(&ct, &max_diff_ts, <)) if (!timespeccmp(&ct, &max_diff_ts, <))
return (-1); return (-1);
@ -93,7 +79,7 @@ uc_check_timespec_mono(const struct timespec *bt)
if (clock_gettime(CLOCK_MONOTONIC, &ct) < 0) if (clock_gettime(CLOCK_MONOTONIC, &ct) < 0)
return (-1); return (-1);
timespecsub(&ct, bt); timespecsub(&ct, bt, &ct);
if (!timespeccmp(&ct, &max_diff_ts, <)) if (!timespeccmp(&ct, &max_diff_ts, <))
return (-1); return (-1);

View File

@ -93,19 +93,6 @@
*/ */
#define PIPELINE_MAX 4 #define PIPELINE_MAX 4
/*
* As in all programs, steal timespecsub() from time.h.
*/
#define timespecsub(vvp, uvp) \
do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
static int static int
udp_create(int *fd1p, int *fd2p) udp_create(int *fd1p, int *fd2p)
{ {
@ -277,7 +264,7 @@ juggle(int fd1, int fd2, int pipeline)
if (clock_gettime(CLOCK_REALTIME, &tfinish) < 0) if (clock_gettime(CLOCK_REALTIME, &tfinish) < 0)
err(-1, "juggle: clock_gettime"); err(-1, "juggle: clock_gettime");
timespecsub(&tfinish, &tstart); timespecsub(&tfinish, &tstart, &tfinish);
return (tfinish); return (tfinish);
} }
@ -373,7 +360,7 @@ thread_juggle(int fd1, int fd2, int pipeline)
if (pthread_join(thread, NULL) != 0) if (pthread_join(thread, NULL) != 0)
err(-1, "thread_juggle: pthread_join"); err(-1, "thread_juggle: pthread_join");
timespecsub(&tfinish, &tstart); timespecsub(&tfinish, &tstart, &tfinish);
return (tfinish); return (tfinish);
} }
@ -458,7 +445,7 @@ process_juggle(int fd1, int fd2, int pipeline)
if (wpid != pid) if (wpid != pid)
errx(-1, "process_juggle: waitpid: pid != wpid"); errx(-1, "process_juggle: waitpid: pid != wpid");
timespecsub(&tfinish, &tstart); timespecsub(&tfinish, &tstart, &tfinish);
return (tfinish); return (tfinish);
} }

View File

@ -57,16 +57,6 @@
#define min(x, y) (x < y ? x : y) #define min(x, y) (x < y ? x : y)
#define timespecsub(vvp, uvp) \
do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
/* /*
* Gist of each client worker: build up to mflag connections at a time, and * Gist of each client worker: build up to mflag connections at a time, and
@ -336,7 +326,7 @@ tcpp_client(void)
if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_finish, &size, NULL, 0) if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_finish, &size, NULL, 0)
< 0) < 0)
err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME); err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
timespecsub(&ts_finish, &ts_start); timespecsub(&ts_finish, &ts_start, &ts_finish);
if (failed) if (failed)
errx(-1, "Too many errors"); errx(-1, "Too many errors");

View File

@ -59,16 +59,6 @@ static struct timespec ts_start, ts_end;
static int alarm_timeout; static int alarm_timeout;
static volatile int alarm_fired; static volatile int alarm_fired;
#define timespecsub(vvp, uvp) \
do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
#define BENCHMARK_FOREACH(I, NUM) for (I = 0; I < NUM && alarm_fired == 0; I++) #define BENCHMARK_FOREACH(I, NUM) for (I = 0; I < NUM && alarm_fired == 0; I++)
static void static void
@ -1112,7 +1102,7 @@ main(int argc, char *argv[])
for (k = 0; k < loops; k++) { for (k = 0; k < loops; k++) {
calls = the_test->t_func(iterations, the_test->t_int, calls = the_test->t_func(iterations, the_test->t_int,
path); path);
timespecsub(&ts_end, &ts_start); timespecsub(&ts_end, &ts_start, &ts_end);
printf("%s\t%ju\t", the_test->t_name, k); printf("%s\t%ju\t", the_test->t_name, k);
printf("%ju.%09ju\t%ju\t", (uintmax_t)ts_end.tv_sec, printf("%ju.%09ju\t%ju\t", (uintmax_t)ts_end.tv_sec,
(uintmax_t)ts_end.tv_nsec, calls); (uintmax_t)ts_end.tv_nsec, calls);

View File

@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$");
#include <sys/ptrace.h> #include <sys/ptrace.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <assert.h> #include <assert.h>
@ -522,12 +523,12 @@ print_line_prefix(struct trussinfo *info)
len += fprintf(info->outfile, ": "); len += fprintf(info->outfile, ": ");
} }
if (info->flags & ABSOLUTETIMESTAMPS) { if (info->flags & ABSOLUTETIMESTAMPS) {
timespecsubt(&t->after, &info->start_time, &timediff); timespecsub(&t->after, &info->start_time, &timediff);
len += fprintf(info->outfile, "%jd.%09ld ", len += fprintf(info->outfile, "%jd.%09ld ",
(intmax_t)timediff.tv_sec, timediff.tv_nsec); (intmax_t)timediff.tv_sec, timediff.tv_nsec);
} }
if (info->flags & RELATIVETIMESTAMPS) { if (info->flags & RELATIVETIMESTAMPS) {
timespecsubt(&t->after, &t->before, &timediff); timespecsub(&t->after, &t->before, &timediff);
len += fprintf(info->outfile, "%jd.%09ld ", len += fprintf(info->outfile, "%jd.%09ld ",
(intmax_t)timediff.tv_sec, timediff.tv_nsec); (intmax_t)timediff.tv_sec, timediff.tv_nsec);
} }

View File

@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
#include <sys/socket.h> #include <sys/socket.h>
#define _WANT_FREEBSD11_STAT #define _WANT_FREEBSD11_STAT
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <sys/un.h> #include <sys/un.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -2639,7 +2640,7 @@ print_syscall_ret(struct trussinfo *trussinfo, int errorp, long *retval)
t = trussinfo->curthread; t = trussinfo->curthread;
sc = t->cs.sc; sc = t->cs.sc;
if (trussinfo->flags & COUNTONLY) { if (trussinfo->flags & COUNTONLY) {
timespecsubt(&t->after, &t->before, &timediff); timespecsub(&t->after, &t->before, &timediff);
timespecadd(&sc->time, &timediff, &sc->time); timespecadd(&sc->time, &timediff, &sc->time);
sc->ncalls++; sc->ncalls++;
if (errorp) if (errorp)

View File

@ -119,23 +119,3 @@ struct trussinfo
LIST_HEAD(, procinfo) proclist; LIST_HEAD(, procinfo) proclist;
}; };
#define timespecsubt(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
#define timespecadd(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
(vvp)->tv_nsec = (tvp)->tv_nsec + (uvp)->tv_nsec; \
if ((vvp)->tv_nsec > 1000000000) { \
(vvp)->tv_sec++; \
(vvp)->tv_nsec -= 1000000000; \
} \
} while (0)

View File

@ -429,23 +429,6 @@ static sig_atomic_t need_status = 0;
#define min(a, b) (a < b) ? a : b #define min(a, b) (a < b) ? a : b
#endif #endif
/*
* XXX KDM private copy of timespecsub(). This is normally defined in
* sys/time.h, but is only enabled in the kernel. If that definition is
* enabled in userland, it breaks the build of libnetbsd.
*/
#ifndef timespecsub
#define timespecsub(vvp, uvp) \
do { \
(vvp)->tv_sec -= (uvp)->tv_sec; \
(vvp)->tv_nsec -= (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
#endif
/* Generically useful offsets into the peripheral private area */ /* Generically useful offsets into the peripheral private area */
#define ppriv_ptr0 periph_priv.entries[0].ptr #define ppriv_ptr0 periph_priv.entries[0].ptr
@ -3069,7 +3052,7 @@ camdd_print_status(struct camdd_dev *camdd_dev, struct camdd_dev *other_dev,
return; return;
} }
timespecsub(&done_time, start_time); timespecsub(&done_time, start_time, &done_time);
total_ns = done_time.tv_nsec + (done_time.tv_sec * 1000000000); total_ns = done_time.tv_nsec + (done_time.tv_sec * 1000000000);
total_sec = total_ns; total_sec = total_ns;