FreeBSD/usr.sbin/watchdogd/watchdogd.c
/*
 * Convert a timeval to a number of ticks.
 * Mostly copied from the kernel.
 */
Convert a timeval to a number of ticks.
Mostly copied from the kernel.
Convert a timeval to a number of ticks . Mostly copied from the kernel .
int
tvtohz(struct timeval *tv)
{
	register unsigned long ticks;
	register long sec, usec;
	int hz;
	size_t hzsize;
	int error;
	int tick;
	hzsize = sizeof(hz);
	error = sysctlbyname("kern.hz", &hz, &hzsize, NULL, 0);
	if (error)
		err(1, "sysctlbyname kern.hz");
	tick = 1000000 / hz;
	sec = tv->tv_sec;
	usec = tv->tv_usec;
	if (usec < 0) {
		sec--;
		usec += 1000000;
	}
	if (sec < 0) {
#ifdef DIAGNOSTIC
		if (usec > 0) {
			sec++;
			usec -= 1000000;
		}
		printf("tvotohz: negative time difference %ld sec %ld usec\n",
		    sec, usec);
#endif
		ticks = 1;
	} 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);
}
int tvtohz ( struct timeval * tv ) { register unsigned long ticks ; register long sec , usec ; int hz ; size_t hzsize ; int error ; int tick ; hzsize = sizeof ( hz ) ; error = sysctlbyname ( " " , & hz , & hzsize , NULL , 0 ) ; if ( error ) err ( 1 , " " ) ; tick = 1000000 / hz ; sec = tv -> tv_sec ; usec = tv -> tv_usec ; if ( usec < 0 ) { sec -- ; usec += 1000000 ; } if ( sec < 0 ) { #ifdef DIAGNOSTIC if ( usec > 0 ) { sec ++ ; usec -= 1000000 ; } printf ( " \n " , sec , usec ) ; #endif ticks = 1 ; } 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 ) ; }
Convert a timeval to a number of ticks.
Convert a timeval to a number of ticks .
/*
	 * 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 assumes that hz is integral.
	 *
	 * Otherwise, round the time difference down to the maximum
	 * representable value.
	 *
	 * If ints have 32 bits, then the maximum value for any timeout in
	 * 10ms ticks is 248 days.
	 */