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