xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_event.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
30*7c478bd9Sstevel@tonic-gate #include <limits.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <fmd_alloc.h>
33*7c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
34*7c478bd9Sstevel@tonic-gate #include <fmd_event.h>
35*7c478bd9Sstevel@tonic-gate #include <fmd_string.h>
36*7c478bd9Sstevel@tonic-gate #include <fmd_case.h>
37*7c478bd9Sstevel@tonic-gate #include <fmd_log.h>
38*7c478bd9Sstevel@tonic-gate #include <fmd_time.h>
39*7c478bd9Sstevel@tonic-gate #include <fmd_ctl.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <fmd.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate fmd_event_t *
44*7c478bd9Sstevel@tonic-gate fmd_event_recreate(uint_t type, const fmd_timeval_t *tp,
45*7c478bd9Sstevel@tonic-gate     nvlist_t *nvl, void *data, fmd_log_t *lp, off64_t off, size_t len)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = fmd_alloc(sizeof (fmd_event_impl_t), FMD_SLEEP);
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	fmd_timeval_t tod;
50*7c478bd9Sstevel@tonic-gate 	hrtime_t hr0;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&ep->ev_lock, NULL);
53*7c478bd9Sstevel@tonic-gate 	ep->ev_refs = 0;
54*7c478bd9Sstevel@tonic-gate 	ASSERT(type < FMD_EVT_NTYPES);
55*7c478bd9Sstevel@tonic-gate 	ep->ev_type = (uint16_t)type;
56*7c478bd9Sstevel@tonic-gate 	ep->ev_state = FMD_EVS_RECEIVED;
57*7c478bd9Sstevel@tonic-gate 	ep->ev_flags = FMD_EVF_REPLAY;
58*7c478bd9Sstevel@tonic-gate 	ep->ev_nvl = nvl;
59*7c478bd9Sstevel@tonic-gate 	ep->ev_data = data;
60*7c478bd9Sstevel@tonic-gate 	ep->ev_time = *tp;
61*7c478bd9Sstevel@tonic-gate 	ep->ev_log = lp;
62*7c478bd9Sstevel@tonic-gate 	ep->ev_off = off;
63*7c478bd9Sstevel@tonic-gate 	ep->ev_len = len;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	/*
66*7c478bd9Sstevel@tonic-gate 	 * If we're not restoring from a log, the event is marked volatile.  If
67*7c478bd9Sstevel@tonic-gate 	 * we are restoring from a log, then hold the log pointer and increment
68*7c478bd9Sstevel@tonic-gate 	 * the pending count.  If we're using a log but no offset and data len
69*7c478bd9Sstevel@tonic-gate 	 * are specified, it's a checkpoint event: don't replay or set pending.
70*7c478bd9Sstevel@tonic-gate 	 */
71*7c478bd9Sstevel@tonic-gate 	if (lp == NULL)
72*7c478bd9Sstevel@tonic-gate 		ep->ev_flags |= FMD_EVF_VOLATILE;
73*7c478bd9Sstevel@tonic-gate 	else if (off != 0 && len != 0)
74*7c478bd9Sstevel@tonic-gate 		fmd_log_hold_pending(lp);
75*7c478bd9Sstevel@tonic-gate 	else {
76*7c478bd9Sstevel@tonic-gate 		ep->ev_flags &= ~FMD_EVF_REPLAY;
77*7c478bd9Sstevel@tonic-gate 		fmd_log_hold(lp);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	/*
81*7c478bd9Sstevel@tonic-gate 	 * Sample a (TOD, hrtime) pair from the current system clocks and then
82*7c478bd9Sstevel@tonic-gate 	 * compute ev_hrt by taking the delta between TOD and the input 'tp'.
83*7c478bd9Sstevel@tonic-gate 	 */
84*7c478bd9Sstevel@tonic-gate 	fmd_time_sync(&tod, &hr0, 1);
85*7c478bd9Sstevel@tonic-gate 	fmd_time_tod2hrt(hr0, &tod, tp, &ep->ev_hrt);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	return ((fmd_event_t *)ep);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate fmd_event_t *
91*7c478bd9Sstevel@tonic-gate fmd_event_create(uint_t type, hrtime_t hrt, nvlist_t *nvl, void *data)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = fmd_alloc(sizeof (fmd_event_impl_t), FMD_SLEEP);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	fmd_timeval_t tod;
96*7c478bd9Sstevel@tonic-gate 	hrtime_t hr0;
97*7c478bd9Sstevel@tonic-gate 	const char *p;
98*7c478bd9Sstevel@tonic-gate 	uint64_t ena;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&ep->ev_lock, NULL);
101*7c478bd9Sstevel@tonic-gate 	ep->ev_refs = 0;
102*7c478bd9Sstevel@tonic-gate 	ASSERT(type < FMD_EVT_NTYPES);
103*7c478bd9Sstevel@tonic-gate 	ep->ev_type = (uint16_t)type;
104*7c478bd9Sstevel@tonic-gate 	ep->ev_state = FMD_EVS_RECEIVED;
105*7c478bd9Sstevel@tonic-gate 	ep->ev_flags = FMD_EVF_VOLATILE | FMD_EVF_REPLAY;
106*7c478bd9Sstevel@tonic-gate 	ep->ev_nvl = nvl;
107*7c478bd9Sstevel@tonic-gate 	ep->ev_data = data;
108*7c478bd9Sstevel@tonic-gate 	ep->ev_log = NULL;
109*7c478bd9Sstevel@tonic-gate 	ep->ev_off = 0;
110*7c478bd9Sstevel@tonic-gate 	ep->ev_len = 0;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	/*
113*7c478bd9Sstevel@tonic-gate 	 * Sample TOD and then set ev_time to the earlier TOD corresponding to
114*7c478bd9Sstevel@tonic-gate 	 * the input hrtime value.  This needs to be improved later: hrestime
115*7c478bd9Sstevel@tonic-gate 	 * should be sampled by the transport and passed as an input parameter.
116*7c478bd9Sstevel@tonic-gate 	 */
117*7c478bd9Sstevel@tonic-gate 	fmd_time_sync(&tod, &hr0, 1);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	if (hrt == FMD_HRT_NOW)
120*7c478bd9Sstevel@tonic-gate 		hrt = hr0; /* use hrtime sampled by fmd_time_sync() */
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	/*
123*7c478bd9Sstevel@tonic-gate 	 * If this is an FMA protocol event of class "ereport.*" that contains
124*7c478bd9Sstevel@tonic-gate 	 * valid ENA, we can compute a more precise bound on the event time.
125*7c478bd9Sstevel@tonic-gate 	 */
126*7c478bd9Sstevel@tonic-gate 	if (type == FMD_EVT_PROTOCOL && (p = strchr(data, '.')) != NULL &&
127*7c478bd9Sstevel@tonic-gate 	    strncmp(data, FM_EREPORT_CLASS, (size_t)(p - (char *)data)) == 0 &&
128*7c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) == 0 &&
129*7c478bd9Sstevel@tonic-gate 	    fmd.d_clockops == &fmd_timeops_native)
130*7c478bd9Sstevel@tonic-gate 		hrt = fmd_time_ena2hrt(hrt, ena);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	fmd_time_hrt2tod(hr0, &tod, hrt, &ep->ev_time);
133*7c478bd9Sstevel@tonic-gate 	ep->ev_hrt = hrt;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	return ((fmd_event_t *)ep);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate void
139*7c478bd9Sstevel@tonic-gate fmd_event_destroy(fmd_event_t *e)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ep->ev_lock));
144*7c478bd9Sstevel@tonic-gate 	ASSERT(ep->ev_refs == 0);
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	/*
147*7c478bd9Sstevel@tonic-gate 	 * If the current state is RECEIVED (i.e. no module has accepted the
148*7c478bd9Sstevel@tonic-gate 	 * event) and the event was logged, then change the state to DISCARDED.
149*7c478bd9Sstevel@tonic-gate 	 */
150*7c478bd9Sstevel@tonic-gate 	if (ep->ev_state == FMD_EVS_RECEIVED)
151*7c478bd9Sstevel@tonic-gate 		ep->ev_state = FMD_EVS_DISCARDED;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/*
154*7c478bd9Sstevel@tonic-gate 	 * If the current state is DISCARDED, ACCEPTED, or DIAGNOSED and the
155*7c478bd9Sstevel@tonic-gate 	 * event has not yet been commited, then attempt to commit it now.
156*7c478bd9Sstevel@tonic-gate 	 */
157*7c478bd9Sstevel@tonic-gate 	if (ep->ev_state != FMD_EVS_RECEIVED && (ep->ev_flags & (
158*7c478bd9Sstevel@tonic-gate 	    FMD_EVF_VOLATILE | FMD_EVF_REPLAY)) == FMD_EVF_REPLAY)
159*7c478bd9Sstevel@tonic-gate 		fmd_log_commit(ep->ev_log, e);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (ep->ev_log != NULL) {
162*7c478bd9Sstevel@tonic-gate 		if (ep->ev_flags & FMD_EVF_REPLAY)
163*7c478bd9Sstevel@tonic-gate 			fmd_log_decommit(ep->ev_log, e);
164*7c478bd9Sstevel@tonic-gate 		fmd_log_rele(ep->ev_log);
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	/*
168*7c478bd9Sstevel@tonic-gate 	 * Perform any event type-specific cleanup activities, and then free
169*7c478bd9Sstevel@tonic-gate 	 * the name-value pair list and underlying event data structure.
170*7c478bd9Sstevel@tonic-gate 	 */
171*7c478bd9Sstevel@tonic-gate 	switch (ep->ev_type) {
172*7c478bd9Sstevel@tonic-gate 	case FMD_EVT_CLOSE:
173*7c478bd9Sstevel@tonic-gate 		fmd_case_rele(ep->ev_data);
174*7c478bd9Sstevel@tonic-gate 		break;
175*7c478bd9Sstevel@tonic-gate 	case FMD_EVT_CTL:
176*7c478bd9Sstevel@tonic-gate 		fmd_ctl_fini(ep->ev_data);
177*7c478bd9Sstevel@tonic-gate 		break;
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (ep->ev_nvl != NULL)
181*7c478bd9Sstevel@tonic-gate 		nvlist_free(ep->ev_nvl);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	fmd_free(ep, sizeof (fmd_event_impl_t));
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate void
187*7c478bd9Sstevel@tonic-gate fmd_event_hold(fmd_event_t *e)
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&ep->ev_lock);
192*7c478bd9Sstevel@tonic-gate 	ep->ev_refs++;
193*7c478bd9Sstevel@tonic-gate 	ASSERT(ep->ev_refs != 0);
194*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&ep->ev_lock);
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	if (ep->ev_type == FMD_EVT_CTL)
197*7c478bd9Sstevel@tonic-gate 		fmd_ctl_hold(ep->ev_data);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate void
201*7c478bd9Sstevel@tonic-gate fmd_event_rele(fmd_event_t *e)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	if (ep->ev_type == FMD_EVT_CTL)
206*7c478bd9Sstevel@tonic-gate 		fmd_ctl_rele(ep->ev_data);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&ep->ev_lock);
209*7c478bd9Sstevel@tonic-gate 	ASSERT(ep->ev_refs != 0);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	if (--ep->ev_refs == 0)
212*7c478bd9Sstevel@tonic-gate 		fmd_event_destroy(e);
213*7c478bd9Sstevel@tonic-gate 	else
214*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&ep->ev_lock);
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate /*
218*7c478bd9Sstevel@tonic-gate  * Transition event from its current state to the specified state.  The states
219*7c478bd9Sstevel@tonic-gate  * for events are defined in fmd_event.h and work according to the diagram:
220*7c478bd9Sstevel@tonic-gate  *
221*7c478bd9Sstevel@tonic-gate  *  -------------     -------------     State      Description
222*7c478bd9Sstevel@tonic-gate  * ( RECEIVED =1 )-->( ACCEPTED =2 )    ---------- ---------------------------
223*7c478bd9Sstevel@tonic-gate  *  -----+-------\    ------+------     DISCARDED  No active references in fmd
224*7c478bd9Sstevel@tonic-gate  *       |        \         |           RECEIVED   Active refs in fmd, no case
225*7c478bd9Sstevel@tonic-gate  *  -----v-------  \  ------v------     ACCEPTED   Active refs, case assigned
226*7c478bd9Sstevel@tonic-gate  * ( DISCARDED=0 )  v( DIAGNOSED=3 )    DIAGNOSED  Active refs, case solved
227*7c478bd9Sstevel@tonic-gate  *  -------------     -------------
228*7c478bd9Sstevel@tonic-gate  *
229*7c478bd9Sstevel@tonic-gate  * Since events are reference counted on behalf of multiple subscribers, any
230*7c478bd9Sstevel@tonic-gate  * attempt to transition an event to an "earlier" or "equal" state (as defined
231*7c478bd9Sstevel@tonic-gate  * by the numeric state values shown in the diagram) is silently ignored.
232*7c478bd9Sstevel@tonic-gate  * An event begins life in the RECEIVED state, so the RECEIVED -> DISCARDED
233*7c478bd9Sstevel@tonic-gate  * transition is handled by fmd_event_destroy() when no references remain.
234*7c478bd9Sstevel@tonic-gate  */
235*7c478bd9Sstevel@tonic-gate void
236*7c478bd9Sstevel@tonic-gate fmd_event_transition(fmd_event_t *e, uint_t state)
237*7c478bd9Sstevel@tonic-gate {
238*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&ep->ev_lock);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	TRACE((FMD_DBG_EVT, "event %p transition %u -> %u",
243*7c478bd9Sstevel@tonic-gate 	    (void *)ep, ep->ev_state, state));
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	if (state <= ep->ev_state) {
246*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&ep->ev_lock);
247*7c478bd9Sstevel@tonic-gate 		return; /* no state change necessary */
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (ep->ev_state < FMD_EVS_RECEIVED || ep->ev_state > FMD_EVS_DIAGNOSED)
251*7c478bd9Sstevel@tonic-gate 		fmd_panic("illegal transition %u -> %u\n", ep->ev_state, state);
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	ep->ev_state = state;
254*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&ep->ev_lock);
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate /*
258*7c478bd9Sstevel@tonic-gate  * If the specified event is DISCARDED, ACCEPTED, OR DIAGNOSED and it has been
259*7c478bd9Sstevel@tonic-gate  * written to a log but is still marked for replay, attempt to commit it to the
260*7c478bd9Sstevel@tonic-gate  * log so that it will not be replayed.  If fmd_log_commit() is successful, it
261*7c478bd9Sstevel@tonic-gate  * will clear the FMD_EVF_REPLAY flag on the event for us.
262*7c478bd9Sstevel@tonic-gate  */
263*7c478bd9Sstevel@tonic-gate void
264*7c478bd9Sstevel@tonic-gate fmd_event_commit(fmd_event_t *e)
265*7c478bd9Sstevel@tonic-gate {
266*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&ep->ev_lock);
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	if (ep->ev_state != FMD_EVS_RECEIVED && (ep->ev_flags & (
271*7c478bd9Sstevel@tonic-gate 	    FMD_EVF_VOLATILE | FMD_EVF_REPLAY)) == FMD_EVF_REPLAY)
272*7c478bd9Sstevel@tonic-gate 		fmd_log_commit(ep->ev_log, e);
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&ep->ev_lock);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate /*
278*7c478bd9Sstevel@tonic-gate  * Compute the delta between events in nanoseconds.  To account for very old
279*7c478bd9Sstevel@tonic-gate  * events which are replayed, we must handle the case where ev_hrt is negative.
280*7c478bd9Sstevel@tonic-gate  * We convert the hrtime_t's to unsigned 64-bit integers and then handle the
281*7c478bd9Sstevel@tonic-gate  * case where 'old' is greater than 'new' (i.e. high-res time has wrapped).
282*7c478bd9Sstevel@tonic-gate  */
283*7c478bd9Sstevel@tonic-gate hrtime_t
284*7c478bd9Sstevel@tonic-gate fmd_event_delta(fmd_event_t *e1, fmd_event_t *e2)
285*7c478bd9Sstevel@tonic-gate {
286*7c478bd9Sstevel@tonic-gate 	uint64_t old = ((fmd_event_impl_t *)e1)->ev_hrt;
287*7c478bd9Sstevel@tonic-gate 	uint64_t new = ((fmd_event_impl_t *)e2)->ev_hrt;
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	return (new >= old ? new - old : (UINT64_MAX - old) + new + 1);
290*7c478bd9Sstevel@tonic-gate }
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate hrtime_t
293*7c478bd9Sstevel@tonic-gate fmd_event_hrtime(fmd_event_t *ep)
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate 	return (((fmd_event_impl_t *)ep)->ev_hrt);
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate int
299*7c478bd9Sstevel@tonic-gate fmd_event_match(fmd_event_t *e, uint_t type, void *data)
300*7c478bd9Sstevel@tonic-gate {
301*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	if (type == FMD_EVT_PROTOCOL)
304*7c478bd9Sstevel@tonic-gate 		return (ep->ev_type == type && fmd_strmatch(ep->ev_data, data));
305*7c478bd9Sstevel@tonic-gate 	else
306*7c478bd9Sstevel@tonic-gate 		return (ep->ev_type == type && ep->ev_data == data);
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate int
310*7c478bd9Sstevel@tonic-gate fmd_event_equal(fmd_event_t *e1, fmd_event_t *e2)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep1 = (fmd_event_impl_t *)e1;
313*7c478bd9Sstevel@tonic-gate 	fmd_event_impl_t *ep2 = (fmd_event_impl_t *)e2;
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	return (ep1->ev_log != NULL &&
316*7c478bd9Sstevel@tonic-gate 	    ep1->ev_log == ep2->ev_log && ep1->ev_off == ep2->ev_off);
317*7c478bd9Sstevel@tonic-gate }
318