Subject: reduce __HAVE_TIMECOUNTER ifdefs
To: None <tech-kern@netbsd.org>
From: YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
List: tech-kern
Date: 12/15/2007 23:55:22
--NextPart-20071215231546-1658500
Content-Type: Text/Plain; charset=us-ascii

hi,

can anyone test the attached patch on !__HAVE_TIMECOUNTER ports?
thanks.

YAMAMOTO Takashi

--NextPart-20071215231546-1658500
Content-Type: Text/Plain; charset=us-ascii
Content-Disposition: attachment; filename="t.diff"

Index: sys/timevar.h
===================================================================
RCS file: /cvsroot/src/sys/sys/timevar.h,v
retrieving revision 1.18
diff -u -p -r1.18 timevar.h
--- sys/timevar.h	25 Nov 2007 00:35:28 -0000	1.18
+++ sys/timevar.h	15 Dec 2007 14:09:53 -0000
@@ -129,7 +129,6 @@ struct	ptimers {
  * 
  */
 
-#ifdef __HAVE_TIMECOUNTER
 void	binuptime(struct bintime *);
 void	nanouptime(struct timespec *);
 void	microuptime(struct timeval *);
@@ -145,19 +144,6 @@ void	getmicrouptime(struct timeval *);
 void	getbintime(struct bintime *);
 void	getnanotime(struct timespec *);
 void	getmicrotime(struct timeval *);
-#else /* !__HAVE_TIMECOUNTER */
-/* timecounter compat functions */
-void	microtime(struct timeval *);
-void	nanotime(struct timespec *);
-
-void	nanouptime(struct timespec *);
-void	getbinuptime(struct bintime *);
-void	getnanouptime(struct timespec *);
-void	getmicrouptime(struct timeval *);
-
-void	getnanotime(struct timespec *);
-void	getmicrotime(struct timeval *);
-#endif /* !__HAVE_TIMECOUNTER */
 
 /* Other functions */
 int	adjtime1(const struct timeval *, struct timeval *, struct proc *);
Index: kern/kern_clock.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_clock.c,v
retrieving revision 1.114
diff -u -p -r1.114 kern_clock.c
--- kern/kern_clock.c	6 Nov 2007 00:42:40 -0000	1.114
+++ kern/kern_clock.c	15 Dec 2007 14:09:53 -0000
@@ -1378,16 +1378,39 @@ hardpps(struct timeval *tvp,		/* time at
 
 /* timecounter compat functions */
 void
-nanotime(struct timespec *ts)
+binuptime(struct bintime *bt)
 {
 	struct timeval tv;
 
-	microtime(&tv);
-	TIMEVAL_TO_TIMESPEC(&tv, ts);
+	microuptime(&tv);
+	timeval2bintime(&tv, bt);
 }
 
 void
-getbinuptime(struct bintime *bt)
+nanouptime(struct timespec *tsp)
+{
+	struct timeval tv;
+
+	microuptime(&tv);
+	TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
+}
+
+void
+microuptime(struct timeval *tv)
+{
+	struct timeval t;
+	int s;
+
+	/* microtime + time - mono_time */
+	microtime(&t);
+	s = splclock();
+	timeradd(&t, &time, &t);
+	timersub(&t, &mono_time, &t);
+	splx(s);
+}
+
+void
+bintime(struct bintime *bt)
 {
 	struct timeval tv;
 
@@ -1396,12 +1419,21 @@ getbinuptime(struct bintime *bt)
 }
 
 void
-nanouptime(struct timespec *tsp)
+nanotime(struct timespec *ts)
+{
+	struct timeval tv;
+
+	microtime(&tv);
+	TIMEVAL_TO_TIMESPEC(&tv, ts);
+}
+
+void
+getbinuptime(struct bintime *bt)
 {
 	int s;
 
 	s = splclock();
-	TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
+	timeval2bintime(__UNVOLATILE(&mono_time), bt);
 	splx(s);
 }
 
@@ -1426,6 +1458,16 @@ getmicrouptime(struct timeval *tvp)
 }
 
 void
+getbintime(struct bintime *bt)
+{
+	int s;
+
+	s = splclock();
+	timeval2bintime(__UNVOLATILE(&time), bt);
+	splx(s);
+}
+
+void
 getnanotime(struct timespec *tsp)
 {
 	int s;
@@ -1450,4 +1492,13 @@ tc_getfrequency(void)
 {
 	return hz;
 }
+
+void
+tc_setclock(struct timespec *ts)
+{
+	struct timeval tv;
+
+	TIMESPEC_TO_TIMEVAL(&tv, ts);
+	time = tv;
+}
 #endif /* !__HAVE_TIMECOUNTER */
Index: kern/kern_time.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_time.c,v
retrieving revision 1.134
diff -u -p -r1.134 kern_time.c
--- kern/kern_time.c	8 Dec 2007 13:31:56 -0000	1.134
+++ kern/kern_time.c	15 Dec 2007 14:09:53 -0000
@@ -79,9 +79,6 @@ __KERNEL_RCSID(0, "$NetBSD: kern_time.c,
 #include <sys/signalvar.h>
 #include <sys/syslog.h>
 #include <sys/timetc.h>
-#ifndef __HAVE_TIMECOUNTER
-#include <sys/timevar.h>
-#endif /* !__HAVE_TIMECOUNTER */
 #include <sys/kauth.h>
 
 #include <sys/mount.h>
@@ -122,10 +119,8 @@ static int
 settime1(struct proc *p, struct timespec *ts, bool check_kauth)
 {
 	struct timeval delta, tv;
-#ifdef __HAVE_TIMECOUNTER
 	struct timeval now;
 	struct timespec ts1;
-#endif /* !__HAVE_TIMECOUNTER */
 	lwp_t *l;
 	int s;
 
@@ -133,12 +128,8 @@ settime1(struct proc *p, struct timespec
 
 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
 	s = splclock();
-#ifdef __HAVE_TIMECOUNTER
 	microtime(&now);
 	timersub(&tv, &now, &delta);
-#else /* !__HAVE_TIMECOUNTER */
-	timersub(&tv, &time, &delta);
-#endif /* !__HAVE_TIMECOUNTER */
 
 	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, ts, &delta,
@@ -154,12 +145,8 @@ settime1(struct proc *p, struct timespec
 	}
 #endif
 
-#ifdef __HAVE_TIMECOUNTER
 	TIMEVAL_TO_TIMESPEC(&tv, &ts1);
 	tc_setclock(&ts1);
-#else /* !__HAVE_TIMECOUNTER */
-	time = tv;
-#endif /* !__HAVE_TIMECOUNTER */
 
 	timeradd(&boottime, &delta, &boottime);
 
@@ -308,7 +296,6 @@ sys_nanosleep(struct lwp *l, void *v, re
 int
 nanosleep1(struct lwp *l, struct timespec *rqt, struct timespec *rmt)
 {
-#ifdef __HAVE_TIMECOUNTER
 	int error, timo;
 
 	if (itimespecfix(rqt))
@@ -342,44 +329,6 @@ nanosleep1(struct lwp *l, struct timespe
 	}
 
 	return error;
-#else /* !__HAVE_TIMECOUNTER */
-	struct timeval atv, utv;
-	int error, s, timo;
-
-	TIMESPEC_TO_TIMEVAL(&atv, rqt);
-	if (itimerfix(&atv))
-		return (EINVAL);
-
-	s = splclock();
-	timeradd(&atv,&time,&atv);
-	timo = hzto(&atv);
-	/*
-	 * Avoid inadvertantly sleeping forever
-	 */
-	if (timo == 0)
-		timo = 1;
-	splx(s);
-
-	error = kpause("nanoslp", true, timo, NULL);
-	if (error == ERESTART)
-		error = EINTR;
-	if (error == EWOULDBLOCK)
-		error = 0;
-
-	if (rmt != NULL) {
-		s = splclock();
-		utv = time;
-		splx(s);
-
-		timersub(&atv, &utv, &utv);
-		if (utv.tv_sec < 0)
-			timerclear(&utv);
-
-		TIMEVAL_TO_TIMESPEC(&utv, rmt);
-	}
-
-	return error;
-#endif /* !__HAVE_TIMECOUNTER */
 }
 
 /* ARGSUSED */
@@ -776,9 +725,7 @@ timer_settime(struct ptimer *pt)
 void
 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
 {
-#ifdef __HAVE_TIMECOUNTER
 	struct timeval now;
-#endif
 	struct ptimer *ptn;
 
 	*aitv = pt->pt_time;
@@ -791,20 +738,12 @@ timer_gettime(struct ptimer *pt, struct 
 		 * off.
 		 */
 		if (timerisset(&aitv->it_value)) {
-#ifdef __HAVE_TIMECOUNTER
 			getmicrotime(&now);
 			if (timercmp(&aitv->it_value, &now, <))
 				timerclear(&aitv->it_value);
 			else
 				timersub(&aitv->it_value, &now,
 				    &aitv->it_value);
-#else /* !__HAVE_TIMECOUNTER */
-			if (timercmp(&aitv->it_value, &time, <))
-				timerclear(&aitv->it_value);
-			else
-				timersub(&aitv->it_value, &time,
-				    &aitv->it_value);
-#endif /* !__HAVE_TIMECOUNTER */
 		}
 	} else if (pt->pt_active) {
 		if (pt->pt_type == CLOCK_VIRTUAL)
@@ -855,9 +794,7 @@ int
 dotimer_settime(int timerid, struct itimerspec *value,
     struct itimerspec *ovalue, int flags, struct proc *p)
 {
-#ifdef __HAVE_TIMECOUNTER
 	struct timeval now;
-#endif
 	struct itimerval val, oval;
 	struct ptimer *pt;
 	int s;
@@ -885,27 +822,16 @@ dotimer_settime(int timerid, struct itim
 	 */
 	if (timerisset(&pt->pt_time.it_value)) {
 		if (pt->pt_type == CLOCK_REALTIME) {
-#ifdef __HAVE_TIMECOUNTER
 			if ((flags & TIMER_ABSTIME) == 0) {
 				getmicrotime(&now);
 				timeradd(&pt->pt_time.it_value, &now,
 				    &pt->pt_time.it_value);
 			}
-#else /* !__HAVE_TIMECOUNTER */
-			if ((flags & TIMER_ABSTIME) == 0)
-				timeradd(&pt->pt_time.it_value, &time,
-				    &pt->pt_time.it_value);
-#endif /* !__HAVE_TIMECOUNTER */
 		} else {
 			if ((flags & TIMER_ABSTIME) != 0) {
-#ifdef __HAVE_TIMECOUNTER
 				getmicrotime(&now);
 				timersub(&pt->pt_time.it_value, &now,
 				    &pt->pt_time.it_value);
-#else /* !__HAVE_TIMECOUNTER */
-				timersub(&pt->pt_time.it_value, &time,
-				    &pt->pt_time.it_value);
-#endif /* !__HAVE_TIMECOUNTER */
 				if (!timerisset(&pt->pt_time.it_value) ||
 				    pt->pt_time.it_value.tv_sec < 0) {
 					pt->pt_time.it_value.tv_sec = 0;
@@ -1004,9 +930,7 @@ sys_timer_getoverrun(struct lwp *l, void
 void
 realtimerexpire(void *arg)
 {
-#ifdef __HAVE_TIMECOUNTER
 	struct timeval now;
-#endif
 	struct ptimer *pt;
 	int s;
 
@@ -1018,7 +942,6 @@ realtimerexpire(void *arg)
 		timerclear(&pt->pt_time.it_value);
 		return;
 	}
-#ifdef __HAVE_TIMECOUNTER
 	for (;;) {
 		s = splclock();	/* XXX need spl now? */
 		timeradd(&pt->pt_time.it_value,
@@ -1037,25 +960,6 @@ realtimerexpire(void *arg)
 		splx(s);
 		pt->pt_overruns++;
 	}
-#else /* !__HAVE_TIMECOUNTER */
-	for (;;) {
-		s = splclock();
-		timeradd(&pt->pt_time.it_value,
-		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
-		if (timercmp(&pt->pt_time.it_value, &time, >)) {
-			/*
-			 * Don't need to check hzto() return value, here.
-			 * callout_reset() does it for us.
-			 */
-			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
-			    realtimerexpire, pt);
-			splx(s);
-			return;
-		}
-		splx(s);
-		pt->pt_overruns++;
-	}
-#endif /* !__HAVE_TIMECOUNTER */
 }
 
 /* BSD routine to get the value of an interval timer. */
@@ -1135,9 +1039,7 @@ sys_setitimer(struct lwp *l, void *v, re
 int
 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
 {
-#ifdef __HAVE_TIMECOUNTER
 	struct timeval now;
-#endif
 	struct ptimer *pt;
 	int s;
 
@@ -1185,13 +1087,9 @@ dosetitimer(struct proc *p, int which, s
 	s = splclock();
 	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
 		/* Convert to absolute time */
-#ifdef __HAVE_TIMECOUNTER
 		/* XXX need to wrap in splclock for timecounters case? */
 		getmicrotime(&now);
 		timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
-#else /* !__HAVE_TIMECOUNTER */
-		timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
-#endif /* !__HAVE_TIMECOUNTER */
 	}
 	timer_settime(pt);
 	splx(s);
@@ -1363,17 +1261,8 @@ ratecheck(struct timeval *lasttime, cons
 {
 	struct timeval tv, delta;
 	int rv = 0;
-#ifndef __HAVE_TIMECOUNTER
-	int s;
-#endif
 
-#ifdef __HAVE_TIMECOUNTER
 	getmicrouptime(&tv);
-#else /* !__HAVE_TIMECOUNTER */
-	s = splclock();
-	tv = mono_time;
-	splx(s);
-#endif /* !__HAVE_TIMECOUNTER */
 	timersub(&tv, lasttime, &delta);
 
 	/*
@@ -1397,17 +1286,8 @@ ppsratecheck(struct timeval *lasttime, i
 {
 	struct timeval tv, delta;
 	int rv;
-#ifndef __HAVE_TIMECOUNTER
-	int s;
-#endif
 
-#ifdef __HAVE_TIMECOUNTER
 	getmicrouptime(&tv);
-#else /* !__HAVE_TIMECOUNTER */
-	s = splclock();
-	tv = mono_time;
-	splx(s);
-#endif /* !__HAVE_TIMECOUNTER */
 	timersub(&tv, lasttime, &delta);
 
 	/*
Index: kern/kern_todr.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_todr.c,v
retrieving revision 1.25
diff -u -p -r1.25 kern_todr.c
--- kern/kern_todr.c	29 Nov 2007 18:04:46 -0000	1.25
+++ kern/kern_todr.c	15 Dec 2007 14:09:53 -0000
@@ -115,9 +115,7 @@ inittodr(time_t base)
 {
 	int badbase = 0, waszero = (base == 0), goodtime = 0, badrtc = 0;
 	int s;
-#ifdef	__HAVE_TIMECOUNTER
 	struct timespec ts;
-#endif
 	struct timeval tv;
 
 	if (base < 5 * SECYR) {
@@ -195,14 +193,10 @@ inittodr(time_t base)
 
 	timeset = 1;
 
-	s = splclock();
-#ifdef	__HAVE_TIMECOUNTER
 	ts.tv_sec = tv.tv_sec;
 	ts.tv_nsec = tv.tv_usec * 1000;
+	s = splclock();
 	tc_setclock(&ts);
-#else
-	time = tv;
-#endif
 	splx(s);
 
 	if (waszero || goodtime)
@@ -221,9 +215,7 @@ inittodr(time_t base)
 void
 resettodr(void)
 {
-#ifdef	__HAVE_TIMECOUNTER
-	struct timeval	time;
-#endif
+	struct timeval tv;
 
 	/*
 	 * We might have been called by boot() due to a crash early
@@ -233,15 +225,13 @@ resettodr(void)
 	if (!timeset)
 		return;
 
-#ifdef	__HAVE_TIMECOUNTER
-	getmicrotime(&time);
-#endif
+	getmicrotime(&tv);
 
-	if (time.tv_sec == 0)
+	if (tv.tv_sec == 0)
 		return;
 
 	if (todr_handle)
-		if (todr_settime(todr_handle, &time) != 0)
+		if (todr_settime(todr_handle, &tv) != 0)
 			printf("Cannot set TOD clock time\n");
 }
 
Index: kern/subr_time.c
===================================================================
RCS file: /cvsroot/src/sys/kern/subr_time.c,v
retrieving revision 1.2
diff -u -p -r1.2 subr_time.c
--- kern/subr_time.c	29 Nov 2007 18:04:46 -0000	1.2
+++ kern/subr_time.c	15 Dec 2007 14:09:53 -0000
@@ -42,7 +42,6 @@ __KERNEL_RCSID(0, "$NetBSD: subr_time.c,
 #include <sys/timetc.h>
 #include <sys/intr.h>
 
-#ifdef __HAVE_TIMECOUNTER
 /*
  * Compute number of hz until specified time.  Used to compute second
  * argument to callout_reset() from an absolute time.
@@ -57,7 +56,6 @@ hzto(struct timeval *tvp)
 	timersub(&tv, &now, &tv);
 	return tvtohz(&tv);
 }
-#endif /* __HAVE_TIMECOUNTER */
 
 /*
  * Compute number of ticks in the specified amount of time.
@@ -118,71 +116,6 @@ tvtohz(struct timeval *tv)
 	return ((int)ticks);
 }
 
-#ifndef __HAVE_TIMECOUNTER
-/*
- * Compute number of hz until specified time.  Used to compute second
- * argument to callout_reset() from an absolute time.
- */
-int
-hzto(struct timeval *tv)
-{
-	unsigned long ticks;
-	long sec, usec;
-	int s;
-
-	/*
-	 * If the number of usecs in the whole seconds part of the time
-	 * difference fits in a long, then the total number of usecs will
-	 * fit in an unsigned long.  Compute the total and convert it to
-	 * ticks, rounding up and adding 1 to allow for the current tick
-	 * to expire.  Rounding also depends on unsigned long arithmetic
-	 * to avoid overflow.
-	 *
-	 * Otherwise, if the number of ticks in the whole seconds part of
-	 * the time difference fits in a long, then convert the parts to
-	 * ticks separately and add, using similar rounding methods and
-	 * overflow avoidance.  This method would work in the previous
-	 * case, but it is slightly slower and assume that hz is integral.
-	 *
-	 * Otherwise, round the time difference down to the maximum
-	 * representable value.
-	 *
-	 * If ints are 32-bit, then the maximum value for any timeout in
-	 * 10ms ticks is 248 days.
-	 */
-	s = splclock();
-	sec = tv->tv_sec - time.tv_sec;
-	usec = tv->tv_usec - time.tv_usec;
-	splx(s);
-
-	if (usec < 0) {
-		sec--;
-		usec += 1000000;
-	}
-
-	if (sec < 0 || (sec == 0 && usec <= 0)) {
-		/*
-		 * Would expire now or in the past.  Return 0 ticks.
-		 * This is different from the legacy hzto() interface,
-		 * and callers need to check for it.
-		 */
-		ticks = 0;
-	} else if (sec <= (LONG_MAX / 1000000))
-		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
-		    / tick) + 1;
-	else if (sec <= (LONG_MAX / hz))
-		ticks = (sec * hz) +
-		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
-	else
-		ticks = LONG_MAX;
-
-	if (ticks > INT_MAX)
-		ticks = INT_MAX;
-
-	return ((int)ticks);
-}
-#endif /* !__HAVE_TIMECOUNTER */
-
 /*
  * Compute number of ticks in the specified amount of time.
  */

--NextPart-20071215231546-1658500--