xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_time.c (revision 7532:bb6372f778bb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7532SSean.Ye@Sun.COM  * Common Development and Distribution License (the "License").
6*7532SSean.Ye@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*7532SSean.Ye@Sun.COM 
220Sstevel@tonic-gate /*
23*7532SSean.Ye@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/fm/protocol.h>
280Sstevel@tonic-gate #include <signal.h>
290Sstevel@tonic-gate #include <limits.h>
300Sstevel@tonic-gate #include <time.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <fmd_time.h>
330Sstevel@tonic-gate #include <fmd_alloc.h>
340Sstevel@tonic-gate #include <fmd_error.h>
350Sstevel@tonic-gate #include <fmd_subr.h>
360Sstevel@tonic-gate #include <fmd.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate void
fmd_time_gettimeofday(struct timeval * tvp)390Sstevel@tonic-gate fmd_time_gettimeofday(struct timeval *tvp)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	if (fmd.d_clockops->fto_gettimeofday(tvp, NULL) != 0)
420Sstevel@tonic-gate 		fmd_panic("failed to read time-of-day clock");
430Sstevel@tonic-gate }
440Sstevel@tonic-gate 
450Sstevel@tonic-gate hrtime_t
fmd_time_gethrtime(void)460Sstevel@tonic-gate fmd_time_gethrtime(void)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	return (fmd.d_clockops->fto_gethrtime());
490Sstevel@tonic-gate }
500Sstevel@tonic-gate 
510Sstevel@tonic-gate void
fmd_time_addhrtime(hrtime_t delta)520Sstevel@tonic-gate fmd_time_addhrtime(hrtime_t delta)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	fmd.d_clockops->fto_addhrtime(delta);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate void
fmd_time_waithrtime(hrtime_t delta)580Sstevel@tonic-gate fmd_time_waithrtime(hrtime_t delta)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	fmd.d_clockops->fto_waithrtime(delta);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate void
fmd_time_waitcancel(pthread_t tid)640Sstevel@tonic-gate fmd_time_waitcancel(pthread_t tid)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	fmd.d_clockops->fto_waitcancel(tid);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * To synchronize TOD with a gethrtime() source, we repeatedly sample TOD in
710Sstevel@tonic-gate  * between two calls to gethrtime(), which places a reasonably tight bound on
720Sstevel@tonic-gate  * the high-resolution time that matches the TOD value we sampled.  We repeat
730Sstevel@tonic-gate  * this process several times and ultimately select the sample where the two
740Sstevel@tonic-gate  * values of gethrtime() were closest.  We then assign the average of those
750Sstevel@tonic-gate  * two high-resolution times to be the gethrtime() associated with that TOD.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate void
fmd_time_sync(fmd_timeval_t * ftv,hrtime_t * hrp,uint_t samples)780Sstevel@tonic-gate fmd_time_sync(fmd_timeval_t *ftv, hrtime_t *hrp, uint_t samples)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	const fmd_timeops_t *ftop = fmd.d_clockops;
810Sstevel@tonic-gate 	hrtime_t hrtbase, hrtmin = INT64_MAX;
820Sstevel@tonic-gate 	struct timeval todbase;
830Sstevel@tonic-gate 	uint_t i;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	for (i = 0; i < samples; i++) {
860Sstevel@tonic-gate 		hrtime_t t0, t1, delta;
870Sstevel@tonic-gate 		struct timeval tod;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 		t0 = ftop->fto_gethrtime();
900Sstevel@tonic-gate 		(void) ftop->fto_gettimeofday(&tod, NULL);
910Sstevel@tonic-gate 		t1 = ftop->fto_gethrtime();
920Sstevel@tonic-gate 		delta = t1 - t0;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		if (delta < hrtmin) {
950Sstevel@tonic-gate 			hrtmin = delta;
960Sstevel@tonic-gate 			hrtbase = t0 + delta / 2;
970Sstevel@tonic-gate 			todbase = tod;
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (ftv != NULL) {
1020Sstevel@tonic-gate 		ftv->ftv_sec = todbase.tv_sec;
1030Sstevel@tonic-gate 		ftv->ftv_nsec = todbase.tv_usec * (NANOSEC / MICROSEC);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (hrp != NULL)
1070Sstevel@tonic-gate 		*hrp = hrtbase;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * Convert a high-resolution timestamp into 64-bit seconds and nanoseconds.
1120Sstevel@tonic-gate  * For efficiency, the multiplication and division are expanded using the
1130Sstevel@tonic-gate  * clever algorithm originally designed for the kernel in hrt2ts().  Refer to
1140Sstevel@tonic-gate  * the comments in uts/common/os/timers.c for an explanation of how it works.
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate static void
fmd_time_hrt2ftv(hrtime_t hrt,fmd_timeval_t * ftv)1170Sstevel@tonic-gate fmd_time_hrt2ftv(hrtime_t hrt, fmd_timeval_t *ftv)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	uint32_t sec, nsec, tmp;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	tmp = (uint32_t)(hrt >> 30);
1220Sstevel@tonic-gate 	sec = tmp - (tmp >> 2);
1230Sstevel@tonic-gate 	sec = tmp - (sec >> 5);
1240Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
1250Sstevel@tonic-gate 	sec = tmp - (sec >> 6) + 7;
1260Sstevel@tonic-gate 	sec = tmp - (sec >> 3);
1270Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
1280Sstevel@tonic-gate 	sec = tmp + (sec >> 3);
1290Sstevel@tonic-gate 	sec = tmp + (sec >> 4);
1300Sstevel@tonic-gate 	tmp = (sec << 7) - sec - sec - sec;
1310Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
1320Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
1330Sstevel@tonic-gate 	nsec = (uint32_t)hrt - (tmp << 9);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	while (nsec >= NANOSEC) {
1360Sstevel@tonic-gate 		nsec -= NANOSEC;
1370Sstevel@tonic-gate 		sec++;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	ftv->ftv_sec = sec;
1410Sstevel@tonic-gate 	ftv->ftv_nsec = nsec;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Convert a high-resolution time from gethrtime() to a TOD (fmd_timeval_t).
1460Sstevel@tonic-gate  * We convert 'tod_base' to nanoseconds, adjust it based on the difference
1470Sstevel@tonic-gate  * between the corresponding 'hrt_base' and the event high-res time 'hrt',
1480Sstevel@tonic-gate  * and then repack the result into ftv_sec and ftv_nsec for our output.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate void
fmd_time_hrt2tod(hrtime_t hrt_base,const fmd_timeval_t * tod_base,hrtime_t hrt,fmd_timeval_t * ftv)1510Sstevel@tonic-gate fmd_time_hrt2tod(hrtime_t hrt_base, const fmd_timeval_t *tod_base,
1520Sstevel@tonic-gate     hrtime_t hrt, fmd_timeval_t *ftv)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	fmd_time_hrt2ftv(tod_base->ftv_sec * NANOSEC +
1550Sstevel@tonic-gate 	    tod_base->ftv_nsec + (hrt - hrt_base), ftv);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Convert a TOD (fmd_timeval_t) to a high-resolution time from gethrtime().
1600Sstevel@tonic-gate  * Note that since TOD occurred in the past, the resulting value may be a
1610Sstevel@tonic-gate  * negative number according the current gethrtime() clock value.
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate void
fmd_time_tod2hrt(hrtime_t hrt_base,const fmd_timeval_t * tod_base,const fmd_timeval_t * ftv,hrtime_t * hrtp)1640Sstevel@tonic-gate fmd_time_tod2hrt(hrtime_t hrt_base, const fmd_timeval_t *tod_base,
1650Sstevel@tonic-gate     const fmd_timeval_t *ftv, hrtime_t *hrtp)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	hrtime_t tod_hrt = tod_base->ftv_sec * NANOSEC + tod_base->ftv_nsec;
1680Sstevel@tonic-gate 	hrtime_t ftv_hrt = ftv->ftv_sec * NANOSEC + ftv->ftv_nsec;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	*hrtp = hrt_base - (tod_hrt - ftv_hrt);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Adjust a high-resolution time based on the low bits of time stored in ENA.
1750Sstevel@tonic-gate  * The assumption here in that ENA won't wrap between the time it is computed
1760Sstevel@tonic-gate  * and the time the error is queued (when we capture a full 64-bits of hrtime).
1770Sstevel@tonic-gate  * We extract the relevant ENA time bits as 't0' and subtract the difference
1780Sstevel@tonic-gate  * between these bits and the corresponding low bits of 'hrt' from 'hrt'.
179*7532SSean.Ye@Sun.COM  *
180*7532SSean.Ye@Sun.COM  * Under xVM dom0, the UE ereport is prepared after panic, therefore
181*7532SSean.Ye@Sun.COM  * the full 64-bit hrtime of 't0' can be bigger than 'hrt'.  In such case,
182*7532SSean.Ye@Sun.COM  * we should just return 'hrt'.
183*7532SSean.Ye@Sun.COM  *
184*7532SSean.Ye@Sun.COM  * 't0' contains only the low bits of 64bit hrtime.  It is tricky to tell
185*7532SSean.Ye@Sun.COM  * whether 'hrt' or 't0' happened first.  We assume there should be short
186*7532SSean.Ye@Sun.COM  * period between 'hrt' and 't0', therefore to check which one came first, we
187*7532SSean.Ye@Sun.COM  * test their subtraction against the highest bit of mask, if the bit is not
188*7532SSean.Ye@Sun.COM  * set, then 't0' is earlier.  This is equivalent to
189*7532SSean.Ye@Sun.COM  * 	((hrt - t0) & mask) < ((mask + 1) / 2)
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate hrtime_t
fmd_time_ena2hrt(hrtime_t hrt,uint64_t ena)1920Sstevel@tonic-gate fmd_time_ena2hrt(hrtime_t hrt, uint64_t ena)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	hrtime_t t0, mask;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
1970Sstevel@tonic-gate 	case FM_ENA_FMT1:
1980Sstevel@tonic-gate 		t0 = (ena & ENA_FMT1_TIME_MASK) >> ENA_FMT1_TIME_SHFT;
1990Sstevel@tonic-gate 		mask = ENA_FMT1_TIME_MASK >> ENA_FMT1_TIME_SHFT;
200*7532SSean.Ye@Sun.COM 		if (((hrt - t0) & ((mask + 1) >> 1)) == 0)
201*7532SSean.Ye@Sun.COM 			hrt -= (hrt - t0) & mask;
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	case FM_ENA_FMT2:
2040Sstevel@tonic-gate 		t0 = (ena & ENA_FMT2_TIME_MASK) >> ENA_FMT2_TIME_SHFT;
2050Sstevel@tonic-gate 		mask = ENA_FMT2_TIME_MASK >> ENA_FMT2_TIME_SHFT;
206*7532SSean.Ye@Sun.COM 		if (((hrt - t0) & ((mask + 1) >> 1)) == 0)
207*7532SSean.Ye@Sun.COM 			hrt -= (hrt - t0) & mask;
2080Sstevel@tonic-gate 		break;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	return (hrt);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * To implement a simulated clock, we keep track of an hrtime_t value which
2160Sstevel@tonic-gate  * starts at zero and is incremented only by fmd_time_addhrtime() (i.e. when
2170Sstevel@tonic-gate  * the driver of the simulation requests that the clock advance).  We sample
2180Sstevel@tonic-gate  * the native time-of-day clock once at the start of the simulation and then
2190Sstevel@tonic-gate  * return subsequent time-of-day values by adjusting TOD using the hrtime_t
2200Sstevel@tonic-gate  * clock setting.  Simulated nanosleep (fmd_time_waithrtime() entry point) is
2210Sstevel@tonic-gate  * implemented by waiting on fts->fts_cv for the hrtime_t to increment.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate static void *
fmd_simulator_init(void)2240Sstevel@tonic-gate fmd_simulator_init(void)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	fmd_timesim_t *fts = fmd_alloc(sizeof (fmd_timesim_t), FMD_SLEEP);
2270Sstevel@tonic-gate 	struct timeval tv;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	(void) pthread_mutex_init(&fts->fts_lock, NULL);
2300Sstevel@tonic-gate 	(void) pthread_cond_init(&fts->fts_cv, NULL);
2310Sstevel@tonic-gate 	(void) gettimeofday(&tv, NULL);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	fts->fts_tod = (hrtime_t)tv.tv_sec * NANOSEC +
2340Sstevel@tonic-gate 	    (hrtime_t)tv.tv_usec * (NANOSEC / MICROSEC);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	fts->fts_hrt = 0;
2370Sstevel@tonic-gate 	fts->fts_cancel = 0;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	fmd_dprintf(FMD_DBG_TMR, "simulator tod base tv_sec=%lx hrt=%llx\n",
2400Sstevel@tonic-gate 	    tv.tv_sec, fts->fts_tod);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	return (fts);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate static void
fmd_simulator_fini(void * fts)2460Sstevel@tonic-gate fmd_simulator_fini(void *fts)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	if (fts != NULL)
2490Sstevel@tonic-gate 		fmd_free(fts, sizeof (fmd_timesim_t));
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*ARGSUSED*/
2530Sstevel@tonic-gate static int
fmd_simulator_tod(struct timeval * tvp,void * tzp)2540Sstevel@tonic-gate fmd_simulator_tod(struct timeval *tvp, void *tzp)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	fmd_timesim_t *fts = fmd.d_clockptr;
2570Sstevel@tonic-gate 	hrtime_t tod, hrt, sec, rem;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fts->fts_lock);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	tod = fts->fts_tod;
2620Sstevel@tonic-gate 	hrt = fts->fts_hrt;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fts->fts_lock);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	sec = tod / NANOSEC + hrt / NANOSEC;
2670Sstevel@tonic-gate 	rem = tod % NANOSEC + hrt % NANOSEC;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	tvp->tv_sec = sec + rem / NANOSEC;
2700Sstevel@tonic-gate 	tvp->tv_usec = (rem % NANOSEC) / (NANOSEC / MICROSEC);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	return (0);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate static hrtime_t
fmd_simulator_hrt(void)2760Sstevel@tonic-gate fmd_simulator_hrt(void)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	fmd_timesim_t *fts = fmd.d_clockptr;
2790Sstevel@tonic-gate 	hrtime_t hrt;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fts->fts_lock);
2820Sstevel@tonic-gate 	hrt = fts->fts_hrt;
2830Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fts->fts_lock);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	return (hrt);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate static void
fmd_simulator_add(hrtime_t delta)2890Sstevel@tonic-gate fmd_simulator_add(hrtime_t delta)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	fmd_timesim_t *fts = fmd.d_clockptr;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fts->fts_lock);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (fts->fts_hrt + delta < fts->fts_hrt)
2960Sstevel@tonic-gate 		fts->fts_hrt = INT64_MAX; /* do not increment past apocalypse */
2970Sstevel@tonic-gate 	else
2980Sstevel@tonic-gate 		fts->fts_hrt += delta;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	TRACE((FMD_DBG_TMR, "hrt clock set %llx", fts->fts_hrt));
3010Sstevel@tonic-gate 	fmd_dprintf(FMD_DBG_TMR, "hrt clock set %llx\n", fts->fts_hrt);
3020Sstevel@tonic-gate 
3031193Smws 	(void) pthread_cond_broadcast(&fts->fts_cv);
3040Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fts->fts_lock);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate static void
fmd_simulator_wait(hrtime_t delta)3080Sstevel@tonic-gate fmd_simulator_wait(hrtime_t delta)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	fmd_timesim_t *fts = fmd.d_clockptr;
3110Sstevel@tonic-gate 	uint64_t hrt;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fts->fts_lock);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * If the delta causes time to wrap because we've reached the simulated
3170Sstevel@tonic-gate 	 * apocalypse, then wait forever.  We make 'hrt' unsigned so that the
3180Sstevel@tonic-gate 	 * while-loop comparison fts_hrt < UINT64_MAX will always return true.
3190Sstevel@tonic-gate 	 */
3200Sstevel@tonic-gate 	if (fts->fts_hrt + delta < fts->fts_hrt)
3210Sstevel@tonic-gate 		hrt = UINT64_MAX;
3220Sstevel@tonic-gate 	else
3230Sstevel@tonic-gate 		hrt = fts->fts_hrt + delta;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	while (fts->fts_hrt < hrt && fts->fts_cancel == 0)
3260Sstevel@tonic-gate 		(void) pthread_cond_wait(&fts->fts_cv, &fts->fts_lock);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (fts->fts_cancel != 0)
3290Sstevel@tonic-gate 		fts->fts_cancel--; /* cancel has been processed */
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fts->fts_lock);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*ARGSUSED*/
3350Sstevel@tonic-gate static void
fmd_simulator_cancel(pthread_t tid)3360Sstevel@tonic-gate fmd_simulator_cancel(pthread_t tid)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	fmd_timesim_t *fts = fmd.d_clockptr;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fts->fts_lock);
3410Sstevel@tonic-gate 	fts->fts_cancel++;
3421193Smws 	(void) pthread_cond_signal(&fts->fts_cv);
3430Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fts->fts_lock);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * Native time is implemented by calls to gethrtime() and gettimeofday(), which
3480Sstevel@tonic-gate  * are stored directly in the native time ops-vector defined below.  To wait on
3490Sstevel@tonic-gate  * the native clock we use nanosleep(), which we can abort using a signal.  The
3500Sstevel@tonic-gate  * implementation assumes that callers will have a SIGALRM handler installed.
3510Sstevel@tonic-gate  */
3520Sstevel@tonic-gate static void
fmd_native_wait(hrtime_t delta)3530Sstevel@tonic-gate fmd_native_wait(hrtime_t delta)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	timespec_t tv;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	tv.tv_sec = delta / NANOSEC;
3580Sstevel@tonic-gate 	tv.tv_nsec = delta % NANOSEC;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	(void) nanosleep(&tv, NULL);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate static void
fmd_native_cancel(pthread_t tid)3640Sstevel@tonic-gate fmd_native_cancel(pthread_t tid)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	(void) pthread_kill(tid, SIGALRM);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static void *
fmd_time_nop(void)3700Sstevel@tonic-gate fmd_time_nop(void)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	return (NULL);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate const fmd_timeops_t fmd_timeops_native = {
3760Sstevel@tonic-gate 	(void *(*)())fmd_time_nop,	/* fto_init */
3770Sstevel@tonic-gate 	(void (*)())fmd_time_nop,	/* fto_fini */
3780Sstevel@tonic-gate 	gettimeofday,			/* fto_gettimeofday */
3790Sstevel@tonic-gate 	gethrtime,			/* fto_gethrtime */
3800Sstevel@tonic-gate 	(void (*)())fmd_time_nop,	/* fto_addhrtime */
3810Sstevel@tonic-gate 	fmd_native_wait,		/* fto_waithrtime */
3820Sstevel@tonic-gate 	fmd_native_cancel,		/* fto_waitcancel */
3830Sstevel@tonic-gate };
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate const fmd_timeops_t fmd_timeops_simulated = {
3860Sstevel@tonic-gate 	fmd_simulator_init,		/* fto_init */
3870Sstevel@tonic-gate 	fmd_simulator_fini,		/* fto_fini */
3880Sstevel@tonic-gate 	fmd_simulator_tod,		/* fto_gettimeofday */
3890Sstevel@tonic-gate 	fmd_simulator_hrt,		/* fto_gethrtime */
3900Sstevel@tonic-gate 	fmd_simulator_add,		/* fto_addhrtime */
3910Sstevel@tonic-gate 	fmd_simulator_wait,		/* fto_waithrtime */
3920Sstevel@tonic-gate 	fmd_simulator_cancel,		/* fto_waitcancel */
3930Sstevel@tonic-gate };
394