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