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