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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/fm/protocol.h>
30 #include <fmd_alloc.h>
32 #include <fmd_event.h>
33 #include <fmd_string.h>
34 #include <fmd_module.h>
44 fmd_event_nvwrap(fmd_event_impl_t
*ep
)
46 (void) nvlist_remove_all(ep
->ev_nvl
, FMD_EVN_TTL
);
47 (void) nvlist_remove_all(ep
->ev_nvl
, FMD_EVN_TOD
);
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);
56 fmd_event_nvunwrap(fmd_event_impl_t
*ep
, const fmd_timeval_t
*tp
)
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
;
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
;
72 fmd_time_sync(&ep
->ev_time
, &ep
->ev_hrt
, 1);
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
)
79 fmd_event_impl_t
*ep
= fmd_alloc(sizeof (fmd_event_impl_t
), FMD_SLEEP
);
84 (void) pthread_mutex_init(&ep
->ev_lock
, NULL
);
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
;
96 fmd_event_nvunwrap(ep
, tp
);
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.
105 ep
->ev_flags
|= FMD_EVF_VOLATILE
;
106 else if (off
!= 0 && len
!= 0)
107 fmd_log_hold_pending(lp
);
109 ep
->ev_flags
&= ~FMD_EVF_REPLAY
;
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.
117 fmd_time_sync(&tod
, &hr0
, 1);
118 fmd_time_tod2hrt(hr0
, &tod
, &ep
->ev_time
, &ep
->ev_hrt
);
120 fmd_event_nvwrap(ep
);
121 return ((fmd_event_t
*)ep
);
125 fmd_event_create(uint_t type
, hrtime_t hrt
, nvlist_t
*nvl
, void *data
)
127 fmd_event_impl_t
*ep
= fmd_alloc(sizeof (fmd_event_impl_t
), FMD_SLEEP
);
134 (void) pthread_mutex_init(&ep
->ev_lock
, NULL
);
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
;
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.
152 fmd_time_sync(&tod
, &hr0
, 1);
154 if (hrt
== FMD_HRT_NOW
)
155 hrt
= hr0
; /* use hrtime sampled by fmd_time_sync() */
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.
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
);
167 fmd_time_hrt2tod(hr0
, &tod
, hrt
, &ep
->ev_time
);
170 fmd_event_nvwrap(ep
);
171 return ((fmd_event_t
*)ep
);
175 fmd_event_destroy(fmd_event_t
*e
)
177 fmd_event_impl_t
*ep
= (fmd_event_impl_t
*)e
;
179 ASSERT(MUTEX_HELD(&ep
->ev_lock
));
180 ASSERT(ep
->ev_refs
== 0);
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.
186 if (ep
->ev_state
== FMD_EVS_RECEIVED
)
187 ep
->ev_state
= FMD_EVS_DISCARDED
;
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.
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
);
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
);
204 * Perform any event type-specific cleanup activities, and then free
205 * the name-value pair list and underlying event data structure.
207 switch (ep
->ev_type
) {
208 case FMD_EVT_TIMEOUT
:
209 fmd_free(ep
->ev_data
, sizeof (fmd_modtimer_t
));
212 case FMD_EVT_PUBLISH
:
213 fmd_case_rele(ep
->ev_data
);
216 fmd_ctl_fini(ep
->ev_data
);
219 fmd_topo_rele(ep
->ev_data
);
223 nvlist_free(ep
->ev_nvl
);
225 fmd_free(ep
, sizeof (fmd_event_impl_t
));
229 fmd_event_hold(fmd_event_t
*e
)
231 fmd_event_impl_t
*ep
= (fmd_event_impl_t
*)e
;
233 (void) pthread_mutex_lock(&ep
->ev_lock
);
235 ASSERT(ep
->ev_refs
!= 0);
236 (void) pthread_mutex_unlock(&ep
->ev_lock
);
238 if (ep
->ev_type
== FMD_EVT_CTL
)
239 fmd_ctl_hold(ep
->ev_data
);
243 fmd_event_rele(fmd_event_t
*e
)
245 fmd_event_impl_t
*ep
= (fmd_event_impl_t
*)e
;
247 if (ep
->ev_type
== FMD_EVT_CTL
)
248 fmd_ctl_rele(ep
->ev_data
);
250 (void) pthread_mutex_lock(&ep
->ev_lock
);
251 ASSERT(ep
->ev_refs
!= 0);
253 if (--ep
->ev_refs
== 0)
254 fmd_event_destroy(e
);
256 (void) pthread_mutex_unlock(&ep
->ev_lock
);
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:
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 * ------------- -------------
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.
278 fmd_event_transition(fmd_event_t
*e
, uint_t state
)
280 fmd_event_impl_t
*ep
= (fmd_event_impl_t
*)e
;
282 (void) pthread_mutex_lock(&ep
->ev_lock
);
284 TRACE((FMD_DBG_EVT
, "event %p transition %u -> %u",
285 (void *)ep
, ep
->ev_state
, state
));
287 if (state
<= ep
->ev_state
) {
288 (void) pthread_mutex_unlock(&ep
->ev_lock
);
289 return; /* no state change necessary */
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
);
295 ep
->ev_state
= state
;
296 (void) pthread_mutex_unlock(&ep
->ev_lock
);
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.
306 fmd_event_commit(fmd_event_t
*e
)
308 fmd_event_impl_t
*ep
= (fmd_event_impl_t
*)e
;
310 (void) pthread_mutex_lock(&ep
->ev_lock
);
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
);
316 (void) pthread_mutex_unlock(&ep
->ev_lock
);
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).
326 fmd_event_delta(fmd_event_t
*e1
, fmd_event_t
*e2
)
328 uint64_t old
= ((fmd_event_impl_t
*)e1
)->ev_hrt
;
329 uint64_t new = ((fmd_event_impl_t
*)e2
)->ev_hrt
;
331 return (new >= old
? new - old
: (UINT64_MAX
- old
) + new + 1);
335 fmd_event_hrtime(fmd_event_t
*ep
)
337 return (((fmd_event_impl_t
*)ep
)->ev_hrt
);
341 fmd_event_match(fmd_event_t
*e
, uint_t type
, const void *data
)
343 fmd_event_impl_t
*ep
= (fmd_event_impl_t
*)e
;
345 if (ep
->ev_type
!= type
)
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
);
353 return (ep
->ev_data
== data
);
357 fmd_event_equal(fmd_event_t
*e1
, fmd_event_t
*e2
)
359 fmd_event_impl_t
*ep1
= (fmd_event_impl_t
*)e1
;
360 fmd_event_impl_t
*ep2
= (fmd_event_impl_t
*)e2
;
362 return (ep1
->ev_log
!= NULL
&&
363 ep1
->ev_log
== ep2
->ev_log
&& ep1
->ev_off
== ep2
->ev_off
);