xref: /illumos-gate/usr/src/cmd/fm/modules/common/eversholt/fme.c (revision a07094369b21309434206d9b3601d162693466fc)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * fme.c -- fault management exercise module
27  *
28  * this module provides the simulated fault management exercise.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <ctype.h>
38 #include <alloca.h>
39 #include <libnvpair.h>
40 #include <sys/fm/protocol.h>
41 #include <fm/fmd_api.h>
42 #include "alloc.h"
43 #include "out.h"
44 #include "stats.h"
45 #include "stable.h"
46 #include "literals.h"
47 #include "lut.h"
48 #include "tree.h"
49 #include "ptree.h"
50 #include "itree.h"
51 #include "ipath.h"
52 #include "fme.h"
53 #include "evnv.h"
54 #include "eval.h"
55 #include "config.h"
56 #include "platform.h"
57 
58 /* imported from eft.c... */
59 extern char *Autoclose;
60 extern int Dupclose;
61 extern hrtime_t Hesitate;
62 extern nv_alloc_t Eft_nv_hdl;
63 extern int Max_fme;
64 extern fmd_hdl_t *Hdl;
65 
66 static int Istat_need_save;
67 
68 /* fme under construction is global so we can free it on module abort */
69 static struct fme *Nfmep;
70 
71 static const char *Undiag_reason;
72 
73 static int Nextid = 0;
74 
75 static int Open_fme_count = 0;	/* Count of open FMEs */
76 
77 /* list of fault management exercises underway */
78 static struct fme {
79 	struct fme *next;		/* next exercise */
80 	unsigned long long ull;		/* time when fme was created */
81 	int id;				/* FME id */
82 	struct cfgdata *cfgdata;	/* full configuration data */
83 	struct lut *eventtree;		/* propagation tree for this FME */
84 	/*
85 	 * The initial error report that created this FME is kept in
86 	 * two forms.  e0 points to the instance tree node and is used
87 	 * by fme_eval() as the starting point for the inference
88 	 * algorithm.  e0r is the event handle FMD passed to us when
89 	 * the ereport first arrived and is used when setting timers,
90 	 * which are always relative to the time of this initial
91 	 * report.
92 	 */
93 	struct event *e0;
94 	fmd_event_t *e0r;
95 
96 	id_t    timer;			/* for setting an fmd time-out */
97 	id_t	htid;			/* for setting hesitation timer */
98 
99 	struct event *ecurrent;		/* ereport under consideration */
100 	struct event *suspects;		/* current suspect list */
101 	struct event *psuspects;	/* previous suspect list */
102 	int nsuspects;			/* count of suspects */
103 	int nonfault;			/* zero if all suspects T_FAULT */
104 	int posted_suspects;		/* true if we've posted a diagnosis */
105 	int hesitated;			/* true if we hesitated */
106 	int uniqobs;			/* number of unique events observed */
107 	int peek;			/* just peeking, don't track suspects */
108 	int overflow;			/* true if overflow FME */
109 	enum fme_state {
110 		FME_NOTHING = 5000,	/* not evaluated yet */
111 		FME_WAIT,		/* need to wait for more info */
112 		FME_CREDIBLE,		/* suspect list is credible */
113 		FME_DISPROVED,		/* no valid suspects found */
114 		FME_DEFERRED		/* don't know yet (k-count not met) */
115 	} state;
116 
117 	unsigned long long pull;	/* time passed since created */
118 	unsigned long long wull;	/* wait until this time for re-eval */
119 	struct event *observations;	/* observation list */
120 	struct lut *globals;		/* values of global variables */
121 	/* fmd interfacing */
122 	fmd_hdl_t *hdl;			/* handle for talking with fmd */
123 	fmd_case_t *fmcase;		/* what fmd 'case' we associate with */
124 	/* stats */
125 	struct stats *Rcount;
126 	struct stats *Hcallcount;
127 	struct stats *Rcallcount;
128 	struct stats *Ccallcount;
129 	struct stats *Ecallcount;
130 	struct stats *Tcallcount;
131 	struct stats *Marrowcount;
132 	struct stats *diags;
133 } *FMElist, *EFMElist, *ClosedFMEs;
134 
135 static struct case_list {
136 	fmd_case_t *fmcase;
137 	struct case_list *next;
138 } *Undiagablecaselist;
139 
140 static void fme_eval(struct fme *fmep, fmd_event_t *ffep);
141 static enum fme_state hypothesise(struct fme *fmep, struct event *ep,
142 	unsigned long long at_latest_by, unsigned long long *pdelay);
143 static struct node *eventprop_lookup(struct event *ep, const char *propname);
144 static struct node *pathstring2epnamenp(char *path);
145 static void publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep);
146 static void restore_suspects(struct fme *fmep);
147 static void save_suspects(struct fme *fmep);
148 static void destroy_fme(struct fme *f);
149 static void fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
150     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl);
151 
152 static struct fme *
153 alloc_fme(void)
154 {
155 	struct fme *fmep;
156 
157 	fmep = MALLOC(sizeof (*fmep));
158 	bzero(fmep, sizeof (*fmep));
159 	return (fmep);
160 }
161 
162 /*
163  * fme_ready -- called when all initialization of the FME (except for
164  *	stats) has completed successfully.  Adds the fme to global lists
165  *	and establishes its stats.
166  */
167 static struct fme *
168 fme_ready(struct fme *fmep)
169 {
170 	char nbuf[100];
171 
172 	Nfmep = NULL;	/* don't need to free this on module abort now */
173 
174 	if (EFMElist) {
175 		EFMElist->next = fmep;
176 		EFMElist = fmep;
177 	} else
178 		FMElist = EFMElist = fmep;
179 
180 	(void) sprintf(nbuf, "fme%d.Rcount", fmep->id);
181 	fmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
182 	(void) sprintf(nbuf, "fme%d.Hcall", fmep->id);
183 	fmep->Hcallcount = stats_new_counter(nbuf, "calls to hypothesise()", 1);
184 	(void) sprintf(nbuf, "fme%d.Rcall", fmep->id);
185 	fmep->Rcallcount = stats_new_counter(nbuf,
186 	    "calls to requirements_test()", 1);
187 	(void) sprintf(nbuf, "fme%d.Ccall", fmep->id);
188 	fmep->Ccallcount = stats_new_counter(nbuf, "calls to causes_test()", 1);
189 	(void) sprintf(nbuf, "fme%d.Ecall", fmep->id);
190 	fmep->Ecallcount =
191 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
192 	(void) sprintf(nbuf, "fme%d.Tcall", fmep->id);
193 	fmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
194 	(void) sprintf(nbuf, "fme%d.Marrow", fmep->id);
195 	fmep->Marrowcount = stats_new_counter(nbuf,
196 	    "arrows marked by mark_arrows()", 1);
197 	(void) sprintf(nbuf, "fme%d.diags", fmep->id);
198 	fmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
199 
200 	out(O_ALTFP|O_VERB2, "newfme: config snapshot contains...");
201 	config_print(O_ALTFP|O_VERB2, fmep->cfgdata->cooked);
202 
203 	return (fmep);
204 }
205 
206 static struct fme *
207 newfme(const char *e0class, const struct ipath *e0ipp)
208 {
209 	struct cfgdata *cfgdata;
210 
211 	if ((cfgdata = config_snapshot()) == NULL) {
212 		out(O_ALTFP, "newfme: NULL configuration");
213 		Undiag_reason = UD_NOCONF;
214 		return (NULL);
215 	}
216 
217 	Nfmep = alloc_fme();
218 
219 	Nfmep->id = Nextid++;
220 	Nfmep->cfgdata = cfgdata;
221 	Nfmep->posted_suspects = 0;
222 	Nfmep->uniqobs = 0;
223 	Nfmep->state = FME_NOTHING;
224 	Nfmep->pull = 0ULL;
225 	Nfmep->overflow = 0;
226 
227 	Nfmep->fmcase = NULL;
228 	Nfmep->hdl = NULL;
229 
230 	if ((Nfmep->eventtree = itree_create(cfgdata->cooked)) == NULL) {
231 		out(O_ALTFP, "newfme: NULL instance tree");
232 		Undiag_reason = UD_INSTFAIL;
233 		config_free(cfgdata);
234 		FREE(Nfmep);
235 		Nfmep = NULL;
236 		return (NULL);
237 	}
238 
239 	itree_ptree(O_ALTFP|O_VERB2, Nfmep->eventtree);
240 
241 	if ((Nfmep->e0 =
242 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
243 		out(O_ALTFP, "newfme: e0 not in instance tree");
244 		Undiag_reason = UD_BADEVENTI;
245 		itree_free(Nfmep->eventtree);
246 		config_free(cfgdata);
247 		FREE(Nfmep);
248 		Nfmep = NULL;
249 		return (NULL);
250 	}
251 
252 	return (fme_ready(Nfmep));
253 }
254 
255 void
256 fme_fini(void)
257 {
258 	struct fme *sfp, *fp;
259 	struct case_list *ucasep, *nextcasep;
260 
261 	ucasep = Undiagablecaselist;
262 	while (ucasep != NULL) {
263 		nextcasep = ucasep->next;
264 		FREE(ucasep);
265 		ucasep = nextcasep;
266 	}
267 	Undiagablecaselist = NULL;
268 
269 	/* clean up closed fmes */
270 	fp = ClosedFMEs;
271 	while (fp != NULL) {
272 		sfp = fp->next;
273 		destroy_fme(fp);
274 		fp = sfp;
275 	}
276 	ClosedFMEs = NULL;
277 
278 	fp = FMElist;
279 	while (fp != NULL) {
280 		sfp = fp->next;
281 		destroy_fme(fp);
282 		fp = sfp;
283 	}
284 	FMElist = EFMElist = NULL;
285 
286 	/* if we were in the middle of creating an fme, free it now */
287 	if (Nfmep) {
288 		destroy_fme(Nfmep);
289 		Nfmep = NULL;
290 	}
291 }
292 
293 /*
294  * Allocated space for a buffer name.  20 bytes allows for
295  * a ridiculous 9,999,999 unique observations.
296  */
297 #define	OBBUFNMSZ 20
298 
299 /*
300  *  serialize_observation
301  *
302  *  Create a recoverable version of the current observation
303  *  (f->ecurrent).  We keep a serialized version of each unique
304  *  observation in order that we may resume correctly the fme in the
305  *  correct state if eft or fmd crashes and we're restarted.
306  */
307 static void
308 serialize_observation(struct fme *fp, const char *cls, const struct ipath *ipp)
309 {
310 	size_t pkdlen;
311 	char tmpbuf[OBBUFNMSZ];
312 	char *pkd = NULL;
313 	char *estr;
314 
315 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", fp->uniqobs);
316 	estr = ipath2str(cls, ipp);
317 	fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, strlen(estr) + 1);
318 	fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)estr,
319 	    strlen(estr) + 1);
320 	FREE(estr);
321 
322 	if (fp->ecurrent != NULL && fp->ecurrent->nvp != NULL) {
323 		(void) snprintf(tmpbuf,
324 		    OBBUFNMSZ, "observed%d.nvp", fp->uniqobs);
325 		if (nvlist_xpack(fp->ecurrent->nvp,
326 		    &pkd, &pkdlen, NV_ENCODE_XDR, &Eft_nv_hdl) != 0)
327 			out(O_DIE|O_SYS, "pack of observed nvl failed");
328 		fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, pkdlen);
329 		fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)pkd, pkdlen);
330 		FREE(pkd);
331 	}
332 
333 	fp->uniqobs++;
334 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
335 	    sizeof (fp->uniqobs));
336 }
337 
338 /*
339  *  init_fme_bufs -- We keep several bits of state about an fme for
340  *	use if eft or fmd crashes and we're restarted.
341  */
342 static void
343 init_fme_bufs(struct fme *fp)
344 {
345 	size_t cfglen = fp->cfgdata->nextfree - fp->cfgdata->begin;
346 
347 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_CFGLEN, sizeof (cfglen));
348 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_CFGLEN, (void *)&cfglen,
349 	    sizeof (cfglen));
350 	if (cfglen != 0) {
351 		fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_CFG, cfglen);
352 		fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_CFG,
353 		    fp->cfgdata->begin, cfglen);
354 	}
355 
356 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_PULL, sizeof (fp->pull));
357 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_PULL, (void *)&fp->pull,
358 	    sizeof (fp->pull));
359 
360 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_ID, sizeof (fp->id));
361 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_ID, (void *)&fp->id,
362 	    sizeof (fp->id));
363 
364 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_NOBS, sizeof (fp->uniqobs));
365 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
366 	    sizeof (fp->uniqobs));
367 
368 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_POSTD,
369 	    sizeof (fp->posted_suspects));
370 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_POSTD,
371 	    (void *)&fp->posted_suspects, sizeof (fp->posted_suspects));
372 }
373 
374 static void
375 destroy_fme_bufs(struct fme *fp)
376 {
377 	char tmpbuf[OBBUFNMSZ];
378 	int o;
379 
380 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFGLEN);
381 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFG);
382 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_PULL);
383 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_ID);
384 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_POSTD);
385 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_NOBS);
386 
387 	for (o = 0; o < fp->uniqobs; o++) {
388 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", o);
389 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
390 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", o);
391 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
392 	}
393 }
394 
395 /*
396  * reconstitute_observations -- convert a case's serialized observations
397  *	back into struct events.  Returns zero if all observations are
398  *	successfully reconstituted.
399  */
400 static int
401 reconstitute_observations(struct fme *fmep)
402 {
403 	struct event *ep;
404 	struct node *epnamenp = NULL;
405 	size_t pkdlen;
406 	char *pkd = NULL;
407 	char *tmpbuf = alloca(OBBUFNMSZ);
408 	char *sepptr;
409 	char *estr;
410 	int ocnt;
411 	int elen;
412 
413 	for (ocnt = 0; ocnt < fmep->uniqobs; ocnt++) {
414 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", ocnt);
415 		elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
416 		if (elen == 0) {
417 			out(O_ALTFP,
418 			    "reconstitute_observation: no %s buffer found.",
419 			    tmpbuf);
420 			Undiag_reason = UD_MISSINGOBS;
421 			break;
422 		}
423 
424 		estr = MALLOC(elen);
425 		fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
426 		sepptr = strchr(estr, '@');
427 		if (sepptr == NULL) {
428 			out(O_ALTFP,
429 			    "reconstitute_observation: %s: "
430 			    "missing @ separator in %s.",
431 			    tmpbuf, estr);
432 			Undiag_reason = UD_MISSINGPATH;
433 			FREE(estr);
434 			break;
435 		}
436 
437 		*sepptr = '\0';
438 		if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
439 			out(O_ALTFP,
440 			    "reconstitute_observation: %s: "
441 			    "trouble converting path string \"%s\" "
442 			    "to internal representation.",
443 			    tmpbuf, sepptr + 1);
444 			Undiag_reason = UD_MISSINGPATH;
445 			FREE(estr);
446 			break;
447 		}
448 
449 		/* construct the event */
450 		ep = itree_lookup(fmep->eventtree,
451 		    stable(estr), ipath(epnamenp));
452 		if (ep == NULL) {
453 			out(O_ALTFP,
454 			    "reconstitute_observation: %s: "
455 			    "lookup of  \"%s\" in itree failed.",
456 			    tmpbuf, ipath2str(estr, ipath(epnamenp)));
457 			Undiag_reason = UD_BADOBS;
458 			tree_free(epnamenp);
459 			FREE(estr);
460 			break;
461 		}
462 		tree_free(epnamenp);
463 
464 		/*
465 		 * We may or may not have a saved nvlist for the observation
466 		 */
467 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", ocnt);
468 		pkdlen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
469 		if (pkdlen != 0) {
470 			pkd = MALLOC(pkdlen);
471 			fmd_buf_read(fmep->hdl,
472 			    fmep->fmcase, tmpbuf, pkd, pkdlen);
473 			ASSERT(ep->nvp == NULL);
474 			if (nvlist_xunpack(pkd,
475 			    pkdlen, &ep->nvp, &Eft_nv_hdl) != 0)
476 				out(O_DIE|O_SYS, "pack of observed nvl failed");
477 			FREE(pkd);
478 		}
479 
480 		if (ocnt == 0)
481 			fmep->e0 = ep;
482 
483 		FREE(estr);
484 		fmep->ecurrent = ep;
485 		ep->count++;
486 
487 		/* link it into list of observations seen */
488 		ep->observations = fmep->observations;
489 		fmep->observations = ep;
490 	}
491 
492 	if (ocnt == fmep->uniqobs) {
493 		(void) fme_ready(fmep);
494 		return (0);
495 	}
496 
497 	return (1);
498 }
499 
500 /*
501  * restart_fme -- called during eft initialization.  Reconstitutes
502  *	an in-progress fme.
503  */
504 void
505 fme_restart(fmd_hdl_t *hdl, fmd_case_t *inprogress)
506 {
507 	nvlist_t *defect;
508 	struct case_list *bad;
509 	struct fme *fmep;
510 	struct cfgdata *cfgdata = NULL;
511 	size_t rawsz;
512 
513 	fmep = alloc_fme();
514 	fmep->fmcase = inprogress;
515 	fmep->hdl = hdl;
516 
517 	if (fmd_buf_size(hdl, inprogress, WOBUF_CFGLEN) != sizeof (size_t)) {
518 		out(O_ALTFP, "restart_fme: No config data");
519 		Undiag_reason = UD_MISSINGINFO;
520 		goto badcase;
521 	}
522 	fmd_buf_read(hdl, inprogress, WOBUF_CFGLEN, (void *)&rawsz,
523 	    sizeof (size_t));
524 
525 	if ((fmep->e0r = fmd_case_getprincipal(hdl, inprogress)) == NULL) {
526 		out(O_ALTFP, "restart_fme: No event zero");
527 		Undiag_reason = UD_MISSINGZERO;
528 		goto badcase;
529 	}
530 
531 	cfgdata = MALLOC(sizeof (struct cfgdata));
532 	cfgdata->cooked = NULL;
533 	cfgdata->devcache = NULL;
534 	cfgdata->cpucache = NULL;
535 	cfgdata->refcnt = 1;
536 
537 	if (rawsz > 0) {
538 		if (fmd_buf_size(hdl, inprogress, WOBUF_CFG) != rawsz) {
539 			out(O_ALTFP, "restart_fme: Config data size mismatch");
540 			Undiag_reason = UD_CFGMISMATCH;
541 			goto badcase;
542 		}
543 		cfgdata->begin = MALLOC(rawsz);
544 		cfgdata->end = cfgdata->nextfree = cfgdata->begin + rawsz;
545 		fmd_buf_read(hdl,
546 		    inprogress, WOBUF_CFG, cfgdata->begin, rawsz);
547 	} else {
548 		cfgdata->begin = cfgdata->end = cfgdata->nextfree = NULL;
549 	}
550 	fmep->cfgdata = cfgdata;
551 
552 	config_cook(cfgdata);
553 	if ((fmep->eventtree = itree_create(cfgdata->cooked)) == NULL) {
554 		/* case not properly saved or irretrievable */
555 		out(O_ALTFP, "restart_fme: NULL instance tree");
556 		Undiag_reason = UD_INSTFAIL;
557 		goto badcase;
558 	}
559 
560 	itree_ptree(O_ALTFP|O_VERB2, fmep->eventtree);
561 
562 	if (fmd_buf_size(hdl, inprogress, WOBUF_PULL) == 0) {
563 		out(O_ALTFP, "restart_fme: no saved wait time");
564 		Undiag_reason = UD_MISSINGINFO;
565 		goto badcase;
566 	} else {
567 		fmd_buf_read(hdl, inprogress, WOBUF_PULL, (void *)&fmep->pull,
568 		    sizeof (fmep->pull));
569 	}
570 
571 	if (fmd_buf_size(hdl, inprogress, WOBUF_POSTD) == 0) {
572 		out(O_ALTFP, "restart_fme: no saved posted status");
573 		Undiag_reason = UD_MISSINGINFO;
574 		goto badcase;
575 	} else {
576 		fmd_buf_read(hdl, inprogress, WOBUF_POSTD,
577 		    (void *)&fmep->posted_suspects,
578 		    sizeof (fmep->posted_suspects));
579 	}
580 
581 	if (fmd_buf_size(hdl, inprogress, WOBUF_ID) == 0) {
582 		out(O_ALTFP, "restart_fme: no saved id");
583 		Undiag_reason = UD_MISSINGINFO;
584 		goto badcase;
585 	} else {
586 		fmd_buf_read(hdl, inprogress, WOBUF_ID, (void *)&fmep->id,
587 		    sizeof (fmep->id));
588 	}
589 	if (Nextid <= fmep->id)
590 		Nextid = fmep->id + 1;
591 
592 	if (fmd_buf_size(hdl, inprogress, WOBUF_NOBS) == 0) {
593 		out(O_ALTFP, "restart_fme: no count of observations");
594 		Undiag_reason = UD_MISSINGINFO;
595 		goto badcase;
596 	} else {
597 		fmd_buf_read(hdl, inprogress, WOBUF_NOBS,
598 		    (void *)&fmep->uniqobs, sizeof (fmep->uniqobs));
599 	}
600 
601 	if (reconstitute_observations(fmep) != 0)
602 		goto badcase;
603 
604 	Open_fme_count++;
605 
606 	/* give the diagnosis algorithm a shot at the new FME state */
607 	fme_eval(fmep, NULL);
608 	return;
609 
610 badcase:
611 	if (fmep->eventtree != NULL)
612 		itree_free(fmep->eventtree);
613 	config_free(cfgdata);
614 	destroy_fme_bufs(fmep);
615 	FREE(fmep);
616 
617 	/*
618 	 * Since we're unable to restart the case, add it to the undiagable
619 	 * list and solve and close it as appropriate.
620 	 */
621 	bad = MALLOC(sizeof (struct case_list));
622 	bad->next = NULL;
623 
624 	if (Undiagablecaselist != NULL)
625 		bad->next = Undiagablecaselist;
626 	Undiagablecaselist = bad;
627 	bad->fmcase = inprogress;
628 
629 	out(O_ALTFP, "[case %s (unable to restart), ",
630 	    fmd_case_uuid(hdl, bad->fmcase));
631 
632 	if (fmd_case_solved(hdl, bad->fmcase)) {
633 		out(O_ALTFP, "already solved, ");
634 	} else {
635 		out(O_ALTFP, "solving, ");
636 		defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
637 		    NULL, NULL, NULL);
638 		if (Undiag_reason != NULL)
639 			(void) nvlist_add_string(defect,
640 			    UNDIAG_REASON, Undiag_reason);
641 		fmd_case_add_suspect(hdl, bad->fmcase, defect);
642 		fmd_case_solve(hdl, bad->fmcase);
643 	}
644 
645 	if (fmd_case_closed(hdl, bad->fmcase)) {
646 		out(O_ALTFP, "already closed ]");
647 	} else {
648 		out(O_ALTFP, "closing ]");
649 		fmd_case_close(hdl, bad->fmcase);
650 	}
651 }
652 
653 /*ARGSUSED*/
654 static void
655 globals_destructor(void *left, void *right, void *arg)
656 {
657 	struct evalue *evp = (struct evalue *)right;
658 	if (evp->t == NODEPTR)
659 		tree_free((struct node *)evp->v);
660 	evp->v = NULL;
661 	FREE(evp);
662 }
663 
664 void
665 destroy_fme(struct fme *f)
666 {
667 	stats_delete(f->Rcount);
668 	stats_delete(f->Hcallcount);
669 	stats_delete(f->Rcallcount);
670 	stats_delete(f->Ccallcount);
671 	stats_delete(f->Ecallcount);
672 	stats_delete(f->Tcallcount);
673 	stats_delete(f->Marrowcount);
674 	stats_delete(f->diags);
675 
676 	itree_free(f->eventtree);
677 	config_free(f->cfgdata);
678 	lut_free(f->globals, globals_destructor, NULL);
679 	FREE(f);
680 }
681 
682 static const char *
683 fme_state2str(enum fme_state s)
684 {
685 	switch (s) {
686 	case FME_NOTHING:	return ("NOTHING");
687 	case FME_WAIT:		return ("WAIT");
688 	case FME_CREDIBLE:	return ("CREDIBLE");
689 	case FME_DISPROVED:	return ("DISPROVED");
690 	case FME_DEFERRED:	return ("DEFERRED");
691 	default:		return ("UNKNOWN");
692 	}
693 }
694 
695 static int
696 is_problem(enum nametype t)
697 {
698 	return (t == N_FAULT || t == N_DEFECT || t == N_UPSET);
699 }
700 
701 static int
702 is_fault(enum nametype t)
703 {
704 	return (t == N_FAULT);
705 }
706 
707 static int
708 is_defect(enum nametype t)
709 {
710 	return (t == N_DEFECT);
711 }
712 
713 static int
714 is_upset(enum nametype t)
715 {
716 	return (t == N_UPSET);
717 }
718 
719 static void
720 fme_print(int flags, struct fme *fmep)
721 {
722 	struct event *ep;
723 
724 	out(flags, "Fault Management Exercise %d", fmep->id);
725 	out(flags, "\t       State: %s", fme_state2str(fmep->state));
726 	out(flags|O_NONL, "\t  Start time: ");
727 	ptree_timeval(flags|O_NONL, &fmep->ull);
728 	out(flags, NULL);
729 	if (fmep->wull) {
730 		out(flags|O_NONL, "\t   Wait time: ");
731 		ptree_timeval(flags|O_NONL, &fmep->wull);
732 		out(flags, NULL);
733 	}
734 	out(flags|O_NONL, "\t          E0: ");
735 	if (fmep->e0)
736 		itree_pevent_brief(flags|O_NONL, fmep->e0);
737 	else
738 		out(flags|O_NONL, "NULL");
739 	out(flags, NULL);
740 	out(flags|O_NONL, "\tObservations:");
741 	for (ep = fmep->observations; ep; ep = ep->observations) {
742 		out(flags|O_NONL, " ");
743 		itree_pevent_brief(flags|O_NONL, ep);
744 	}
745 	out(flags, NULL);
746 	out(flags|O_NONL, "\tSuspect list:");
747 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
748 		out(flags|O_NONL, " ");
749 		itree_pevent_brief(flags|O_NONL, ep);
750 	}
751 	out(flags, NULL);
752 	out(flags|O_VERB2, "\t        Tree:");
753 	itree_ptree(flags|O_VERB2, fmep->eventtree);
754 }
755 
756 static struct node *
757 pathstring2epnamenp(char *path)
758 {
759 	char *sep = "/";
760 	struct node *ret;
761 	char *ptr;
762 
763 	if ((ptr = strtok(path, sep)) == NULL)
764 		out(O_DIE, "pathstring2epnamenp: invalid empty class");
765 
766 	ret = tree_iname(stable(ptr), NULL, 0);
767 
768 	while ((ptr = strtok(NULL, sep)) != NULL)
769 		ret = tree_name_append(ret,
770 		    tree_iname(stable(ptr), NULL, 0));
771 
772 	return (ret);
773 }
774 
775 /*
776  * for a given upset sp, increment the corresponding SERD engine.  if the
777  * SERD engine trips, return the ename and ipp of the resulting ereport.
778  * returns true if engine tripped and *enamep and *ippp were filled in.
779  */
780 static int
781 serd_eval(struct fme *fmep, fmd_hdl_t *hdl, fmd_event_t *ffep,
782     fmd_case_t *fmcase, struct event *sp, const char **enamep,
783     const struct ipath **ippp)
784 {
785 	struct node *serdinst;
786 	char *serdname;
787 	struct node *nid;
788 
789 	ASSERT(sp->t == N_UPSET);
790 	ASSERT(ffep != NULL);
791 
792 	/*
793 	 * obtain instanced SERD engine from the upset sp.  from this
794 	 * derive serdname, the string used to identify the SERD engine.
795 	 */
796 	serdinst = eventprop_lookup(sp, L_engine);
797 
798 	if (serdinst == NULL)
799 		return (NULL);
800 
801 	serdname = ipath2str(serdinst->u.stmt.np->u.event.ename->u.name.s,
802 	    ipath(serdinst->u.stmt.np->u.event.epname));
803 
804 	/* handle serd engine "id" property, if there is one */
805 	if ((nid =
806 	    lut_lookup(serdinst->u.stmt.lutp, (void *)L_id, NULL)) != NULL) {
807 		struct evalue *gval;
808 		char suffixbuf[200];
809 		char *suffix;
810 		char *nserdname;
811 		size_t nname;
812 
813 		out(O_ALTFP|O_NONL, "serd \"%s\" id: ", serdname);
814 		ptree_name_iter(O_ALTFP|O_NONL, nid);
815 
816 		ASSERTinfo(nid->t == T_GLOBID, ptree_nodetype2str(nid->t));
817 
818 		if ((gval = lut_lookup(fmep->globals,
819 		    (void *)nid->u.globid.s, NULL)) == NULL) {
820 			out(O_ALTFP, " undefined");
821 		} else if (gval->t == UINT64) {
822 			out(O_ALTFP, " %llu", gval->v);
823 			(void) sprintf(suffixbuf, "%llu", gval->v);
824 			suffix = suffixbuf;
825 		} else {
826 			out(O_ALTFP, " \"%s\"", (char *)gval->v);
827 			suffix = (char *)gval->v;
828 		}
829 
830 		nname = strlen(serdname) + strlen(suffix) + 2;
831 		nserdname = MALLOC(nname);
832 		(void) snprintf(nserdname, nname, "%s:%s", serdname, suffix);
833 		FREE(serdname);
834 		serdname = nserdname;
835 	}
836 
837 	if (!fmd_serd_exists(hdl, serdname)) {
838 		struct node *nN, *nT;
839 
840 		/* no SERD engine yet, so create it */
841 		nN = lut_lookup(serdinst->u.stmt.lutp, (void *)L_N, NULL);
842 		nT = lut_lookup(serdinst->u.stmt.lutp, (void *)L_T, NULL);
843 
844 		ASSERT(nN->t == T_NUM);
845 		ASSERT(nT->t == T_TIMEVAL);
846 
847 		fmd_serd_create(hdl, serdname, (uint_t)nN->u.ull,
848 		    (hrtime_t)nT->u.ull);
849 	}
850 
851 
852 	/*
853 	 * increment SERD engine.  if engine fires, reset serd
854 	 * engine and return trip_strcode
855 	 */
856 	if (fmd_serd_record(hdl, serdname, ffep)) {
857 		struct node *tripinst = lut_lookup(serdinst->u.stmt.lutp,
858 		    (void *)L_trip, NULL);
859 
860 		ASSERT(tripinst != NULL);
861 
862 		*enamep = tripinst->u.event.ename->u.name.s;
863 		*ippp = ipath(tripinst->u.event.epname);
864 
865 		fmd_case_add_serd(hdl, fmcase, serdname);
866 		fmd_serd_reset(hdl, serdname);
867 		out(O_ALTFP|O_NONL, "[engine fired: %s, sending: ", serdname);
868 		ipath_print(O_ALTFP|O_NONL, *enamep, *ippp);
869 		out(O_ALTFP, "]");
870 
871 		FREE(serdname);
872 		return (1);
873 	}
874 
875 	FREE(serdname);
876 	return (0);
877 }
878 
879 /*
880  * search a suspect list for upsets.  feed each upset to serd_eval() and
881  * build up tripped[], an array of ereports produced by the firing of
882  * any SERD engines.  then feed each ereport back into
883  * fme_receive_report().
884  *
885  * returns ntrip, the number of these ereports produced.
886  */
887 static int
888 upsets_eval(struct fme *fmep, fmd_event_t *ffep)
889 {
890 	/* we build an array of tripped ereports that we send ourselves */
891 	struct {
892 		const char *ename;
893 		const struct ipath *ipp;
894 	} *tripped;
895 	struct event *sp;
896 	int ntrip, nupset, i;
897 
898 	/*
899 	 * count the number of upsets to determine the upper limit on
900 	 * expected trip ereport strings.  remember that one upset can
901 	 * lead to at most one ereport.
902 	 */
903 	nupset = 0;
904 	for (sp = fmep->suspects; sp; sp = sp->suspects) {
905 		if (sp->t == N_UPSET)
906 			nupset++;
907 	}
908 
909 	if (nupset == 0)
910 		return (0);
911 
912 	/*
913 	 * get to this point if we have upsets and expect some trip
914 	 * ereports
915 	 */
916 	tripped = alloca(sizeof (*tripped) * nupset);
917 	bzero((void *)tripped, sizeof (*tripped) * nupset);
918 
919 	ntrip = 0;
920 	for (sp = fmep->suspects; sp; sp = sp->suspects)
921 		if (sp->t == N_UPSET &&
922 		    serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, sp,
923 			    &tripped[ntrip].ename, &tripped[ntrip].ipp))
924 			ntrip++;
925 
926 	for (i = 0; i < ntrip; i++)
927 		fme_receive_report(fmep->hdl, ffep,
928 		    tripped[i].ename, tripped[i].ipp, NULL);
929 
930 	return (ntrip);
931 }
932 
933 /*
934  * fme_receive_external_report -- call when an external ereport comes in
935  *
936  * this routine just converts the relevant information from the ereport
937  * into a format used internally and passes it on to fme_receive_report().
938  */
939 void
940 fme_receive_external_report(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
941     const char *eventstring)
942 {
943 	struct node *epnamenp = platform_getpath(nvl);
944 	const struct ipath *ipp;
945 
946 	/*
947 	 * XFILE: If we ended up without a path, it's an X-file.
948 	 * For now, use our undiagnosable interface.
949 	 */
950 	if (epnamenp == NULL) {
951 		out(O_ALTFP, "XFILE: Unable to get path from ereport");
952 		Undiag_reason = UD_NOPATH;
953 		publish_undiagnosable(hdl, ffep);
954 		return;
955 	}
956 
957 	ipp = ipath(epnamenp);
958 	tree_free(epnamenp);
959 	fme_receive_report(hdl, ffep, stable(eventstring), ipp, nvl);
960 }
961 
962 static int mark_arrows(struct fme *fmep, struct event *ep, int mark,
963     unsigned long long at_latest_by, unsigned long long *pdelay);
964 
965 /* ARGSUSED */
966 static void
967 clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
968 {
969 	struct bubble *bp;
970 	struct arrowlist *ap;
971 
972 	ep->cached_state = 0;
973 	for (bp = itree_next_bubble(ep, NULL); bp;
974 	    bp = itree_next_bubble(ep, bp)) {
975 		if (bp->t != B_FROM)
976 			continue;
977 		bp->mark = 0;
978 		for (ap = itree_next_arrow(bp, NULL); ap;
979 		    ap = itree_next_arrow(bp, ap))
980 			ap->arrowp->mark = 0;
981 	}
982 }
983 
984 static void
985 fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
986     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl)
987 {
988 	struct event *ep;
989 	struct fme *fmep = NULL;
990 	struct fme *ofmep = NULL;
991 	struct fme *cfmep, *svfmep;
992 	int matched = 0;
993 	nvlist_t *defect;
994 
995 	out(O_ALTFP|O_NONL, "fme_receive_report: ");
996 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
997 	out(O_ALTFP|O_STAMP, NULL);
998 
999 	/* decide which FME it goes to */
1000 	for (fmep = FMElist; fmep; fmep = fmep->next) {
1001 		int prev_verbose;
1002 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
1003 		enum fme_state state;
1004 		nvlist_t *pre_peek_nvp = NULL;
1005 
1006 		if (fmep->overflow) {
1007 			if (!(fmd_case_closed(fmep->hdl, fmep->fmcase)))
1008 				ofmep = fmep;
1009 
1010 			continue;
1011 		}
1012 
1013 		/* look up event in event tree for this FME */
1014 		if ((ep = itree_lookup(fmep->eventtree,
1015 		    eventstring, ipp)) == NULL)
1016 			continue;
1017 
1018 		/* note observation */
1019 		fmep->ecurrent = ep;
1020 		if (ep->count++ == 0) {
1021 			/* link it into list of observations seen */
1022 			ep->observations = fmep->observations;
1023 			fmep->observations = ep;
1024 			ep->nvp = evnv_dupnvl(nvl);
1025 		} else {
1026 			/* use new payload values for peek */
1027 			pre_peek_nvp = ep->nvp;
1028 			ep->nvp = evnv_dupnvl(nvl);
1029 		}
1030 
1031 		/* tell hypothesise() not to mess with suspect list */
1032 		fmep->peek = 1;
1033 
1034 		/* don't want this to be verbose (unless Debug is set) */
1035 		prev_verbose = Verbose;
1036 		if (Debug == 0)
1037 			Verbose = 0;
1038 
1039 		lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
1040 		state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
1041 
1042 		fmep->peek = 0;
1043 
1044 		/* put verbose flag back */
1045 		Verbose = prev_verbose;
1046 
1047 		if (state != FME_DISPROVED) {
1048 			/* found an FME that explains the ereport */
1049 			matched++;
1050 			out(O_ALTFP|O_NONL, "[");
1051 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1052 			out(O_ALTFP, " explained by FME%d]", fmep->id);
1053 
1054 			if (pre_peek_nvp)
1055 				nvlist_free(pre_peek_nvp);
1056 
1057 			if (ep->count == 1)
1058 				serialize_observation(fmep, eventstring, ipp);
1059 
1060 			if (ffep)
1061 				fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1062 
1063 			stats_counter_bump(fmep->Rcount);
1064 
1065 			/* re-eval FME */
1066 			fme_eval(fmep, ffep);
1067 		} else {
1068 
1069 			/* not a match, undo noting of observation */
1070 			fmep->ecurrent = NULL;
1071 			if (--ep->count == 0) {
1072 				/* unlink it from observations */
1073 				fmep->observations = ep->observations;
1074 				ep->observations = NULL;
1075 				nvlist_free(ep->nvp);
1076 				ep->nvp = NULL;
1077 			} else {
1078 				nvlist_free(ep->nvp);
1079 				ep->nvp = pre_peek_nvp;
1080 			}
1081 		}
1082 	}
1083 
1084 	if (matched)
1085 		return;	/* explained by at least one existing FME */
1086 
1087 	/* clean up closed fmes */
1088 	cfmep = ClosedFMEs;
1089 	while (cfmep != NULL) {
1090 		svfmep = cfmep->next;
1091 		destroy_fme(cfmep);
1092 		cfmep = svfmep;
1093 	}
1094 	ClosedFMEs = NULL;
1095 
1096 	if (ofmep) {
1097 		out(O_ALTFP|O_NONL, "[");
1098 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1099 		out(O_ALTFP, " ADDING TO OVERFLOW FME]");
1100 		if (ffep)
1101 			fmd_case_add_ereport(hdl, ofmep->fmcase, ffep);
1102 
1103 		return;
1104 
1105 	} else if (Max_fme && (Open_fme_count >= Max_fme)) {
1106 		out(O_ALTFP|O_NONL, "[");
1107 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1108 		out(O_ALTFP, " MAX OPEN FME REACHED]");
1109 		/* Create overflow fme */
1110 		if ((fmep = newfme(eventstring, ipp)) == NULL) {
1111 			out(O_ALTFP|O_NONL, "[");
1112 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1113 			out(O_ALTFP, " CANNOT OPEN OVERFLOW FME]");
1114 			publish_undiagnosable(hdl, ffep);
1115 			return;
1116 		}
1117 
1118 		Open_fme_count++;
1119 
1120 		fmep->fmcase = fmd_case_open(hdl, NULL);
1121 		fmep->hdl = hdl;
1122 		init_fme_bufs(fmep);
1123 		fmep->overflow = B_TRUE;
1124 
1125 		if (ffep)
1126 			fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1127 
1128 		defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
1129 		    NULL, NULL, NULL);
1130 		(void) nvlist_add_string(defect, UNDIAG_REASON, UD_MAXFME);
1131 		fmd_case_add_suspect(hdl, fmep->fmcase, defect);
1132 		fmd_case_solve(hdl, fmep->fmcase);
1133 		return;
1134 	}
1135 
1136 	/* start a new FME */
1137 	if ((fmep = newfme(eventstring, ipp)) == NULL) {
1138 		out(O_ALTFP|O_NONL, "[");
1139 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1140 		out(O_ALTFP, " CANNOT DIAGNOSE]");
1141 		publish_undiagnosable(hdl, ffep);
1142 		return;
1143 	}
1144 
1145 	Open_fme_count++;
1146 
1147 	/* open a case */
1148 	fmep->fmcase = fmd_case_open(hdl, NULL);
1149 	fmep->hdl = hdl;
1150 	init_fme_bufs(fmep);
1151 
1152 	out(O_ALTFP|O_NONL, "[");
1153 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1154 	out(O_ALTFP, " created FME%d, case %s]", fmep->id,
1155 	    fmd_case_uuid(hdl, fmep->fmcase));
1156 
1157 	ep = fmep->e0;
1158 	ASSERT(ep != NULL);
1159 
1160 	/* note observation */
1161 	fmep->ecurrent = ep;
1162 	if (ep->count++ == 0) {
1163 		/* link it into list of observations seen */
1164 		ep->observations = fmep->observations;
1165 		fmep->observations = ep;
1166 		ep->nvp = evnv_dupnvl(nvl);
1167 		serialize_observation(fmep, eventstring, ipp);
1168 	} else {
1169 		/* new payload overrides any previous */
1170 		nvlist_free(ep->nvp);
1171 		ep->nvp = evnv_dupnvl(nvl);
1172 	}
1173 
1174 	stats_counter_bump(fmep->Rcount);
1175 
1176 	if (ffep) {
1177 		fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1178 		fmd_case_setprincipal(hdl, fmep->fmcase, ffep);
1179 		fmep->e0r = ffep;
1180 	}
1181 
1182 	/* give the diagnosis algorithm a shot at the new FME state */
1183 	fme_eval(fmep, ffep);
1184 }
1185 
1186 void
1187 fme_status(int flags)
1188 {
1189 	struct fme *fmep;
1190 
1191 	if (FMElist == NULL) {
1192 		out(flags, "No fault management exercises underway.");
1193 		return;
1194 	}
1195 
1196 	for (fmep = FMElist; fmep; fmep = fmep->next)
1197 		fme_print(flags, fmep);
1198 }
1199 
1200 /*
1201  * "indent" routines used mostly for nicely formatted debug output, but also
1202  * for sanity checking for infinite recursion bugs.
1203  */
1204 
1205 #define	MAX_INDENT 1024
1206 static const char *indent_s[MAX_INDENT];
1207 static int current_indent;
1208 
1209 static void
1210 indent_push(const char *s)
1211 {
1212 	if (current_indent < MAX_INDENT)
1213 		indent_s[current_indent++] = s;
1214 	else
1215 		out(O_DIE, "unexpected recursion depth (%d)", current_indent);
1216 }
1217 
1218 static void
1219 indent_set(const char *s)
1220 {
1221 	current_indent = 0;
1222 	indent_push(s);
1223 }
1224 
1225 static void
1226 indent_pop(void)
1227 {
1228 	if (current_indent > 0)
1229 		current_indent--;
1230 	else
1231 		out(O_DIE, "recursion underflow");
1232 }
1233 
1234 static void
1235 indent(void)
1236 {
1237 	int i;
1238 	if (!Verbose)
1239 		return;
1240 	for (i = 0; i < current_indent; i++)
1241 		out(O_ALTFP|O_VERB|O_NONL, indent_s[i]);
1242 }
1243 
1244 static int
1245 suspects_changed(struct fme *fmep)
1246 {
1247 	struct event *suspects = fmep->suspects;
1248 	struct event *psuspects = fmep->psuspects;
1249 
1250 	while (suspects != NULL && psuspects != NULL) {
1251 		if (suspects != psuspects)
1252 			return (1);
1253 		suspects = suspects->suspects;
1254 		psuspects = psuspects->psuspects;
1255 	}
1256 
1257 	return (suspects != psuspects);
1258 }
1259 
1260 #define	SLNEW		1
1261 #define	SLCHANGED	2
1262 #define	SLWAIT		3
1263 #define	SLDISPROVED	4
1264 
1265 static void
1266 print_suspects(int circumstance, struct fme *fmep)
1267 {
1268 	struct event *ep;
1269 
1270 	out(O_ALTFP|O_NONL, "[");
1271 	if (circumstance == SLCHANGED) {
1272 		out(O_ALTFP|O_NONL, "FME%d diagnosis changed. state: %s, "
1273 		    "suspect list:", fmep->id, fme_state2str(fmep->state));
1274 	} else if (circumstance == SLWAIT) {
1275 		out(O_ALTFP|O_NONL, "FME%d set wait timer ", fmep->id);
1276 		ptree_timeval(O_ALTFP|O_NONL, &fmep->wull);
1277 	} else if (circumstance == SLDISPROVED) {
1278 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS UNKNOWN", fmep->id);
1279 	} else {
1280 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS PRODUCED:", fmep->id);
1281 	}
1282 
1283 	if (circumstance == SLWAIT || circumstance == SLDISPROVED) {
1284 		out(O_ALTFP, "]");
1285 		return;
1286 	}
1287 
1288 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
1289 		out(O_ALTFP|O_NONL, " ");
1290 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
1291 	}
1292 	out(O_ALTFP, "]");
1293 }
1294 
1295 static struct node *
1296 eventprop_lookup(struct event *ep, const char *propname)
1297 {
1298 	return (lut_lookup(ep->props, (void *)propname, NULL));
1299 }
1300 
1301 #define	MAXDIGITIDX	23
1302 static char numbuf[MAXDIGITIDX + 1];
1303 
1304 static int
1305 node2uint(struct node *n, uint_t *valp)
1306 {
1307 	struct evalue value;
1308 	struct lut *globals = NULL;
1309 
1310 	if (n == NULL)
1311 		return (1);
1312 
1313 	/*
1314 	 * check value.v since we are being asked to convert an unsigned
1315 	 * long long int to an unsigned int
1316 	 */
1317 	if (! eval_expr(n, NULL, NULL, &globals, NULL, NULL, 0, &value) ||
1318 	    value.t != UINT64 || value.v > (1ULL << 32))
1319 		return (1);
1320 
1321 	*valp = (uint_t)value.v;
1322 
1323 	return (0);
1324 }
1325 
1326 static nvlist_t *
1327 node2fmri(struct node *n)
1328 {
1329 	nvlist_t **pa, *f, *p;
1330 	struct node *nc;
1331 	uint_t depth = 0;
1332 	char *numstr, *nullbyte;
1333 	char *failure;
1334 	int err, i;
1335 
1336 	/* XXX do we need to be able to handle a non-T_NAME node? */
1337 	if (n == NULL || n->t != T_NAME)
1338 		return (NULL);
1339 
1340 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
1341 		if (nc->u.name.child == NULL || nc->u.name.child->t != T_NUM)
1342 			break;
1343 		depth++;
1344 	}
1345 
1346 	if (nc != NULL) {
1347 		/* We bailed early, something went wrong */
1348 		return (NULL);
1349 	}
1350 
1351 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
1352 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
1353 	pa = alloca(depth * sizeof (nvlist_t *));
1354 	for (i = 0; i < depth; i++)
1355 		pa[i] = NULL;
1356 
1357 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
1358 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
1359 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
1360 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
1361 	if (err != 0) {
1362 		failure = "basic construction of FMRI failed";
1363 		goto boom;
1364 	}
1365 
1366 	numbuf[MAXDIGITIDX] = '\0';
1367 	nullbyte = &numbuf[MAXDIGITIDX];
1368 	i = 0;
1369 
1370 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
1371 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
1372 		if (err != 0) {
1373 			failure = "alloc of an hc-pair failed";
1374 			goto boom;
1375 		}
1376 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, nc->u.name.s);
1377 		numstr = ulltostr(nc->u.name.child->u.ull, nullbyte);
1378 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
1379 		if (err != 0) {
1380 			failure = "construction of an hc-pair failed";
1381 			goto boom;
1382 		}
1383 		pa[i++] = p;
1384 	}
1385 
1386 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
1387 	if (err == 0) {
1388 		for (i = 0; i < depth; i++)
1389 			if (pa[i] != NULL)
1390 				nvlist_free(pa[i]);
1391 		return (f);
1392 	}
1393 	failure = "addition of hc-pair array to FMRI failed";
1394 
1395 boom:
1396 	for (i = 0; i < depth; i++)
1397 		if (pa[i] != NULL)
1398 			nvlist_free(pa[i]);
1399 	nvlist_free(f);
1400 	out(O_DIE, "%s", failure);
1401 	/*NOTREACHED*/
1402 }
1403 
1404 static uint_t
1405 avg(uint_t sum, uint_t cnt)
1406 {
1407 	unsigned long long s = sum * 10;
1408 
1409 	return ((s / cnt / 10) + (((s / cnt % 10) >= 5) ? 1 : 0));
1410 }
1411 
1412 static uint8_t
1413 percentof(uint_t part, uint_t whole)
1414 {
1415 	unsigned long long p = part * 1000;
1416 
1417 	return ((p / whole / 10) + (((p / whole % 10) >= 5) ? 1 : 0));
1418 }
1419 
1420 static struct rsl {
1421 	struct event *suspect;
1422 	nvlist_t *asru;
1423 	nvlist_t *fru;
1424 	nvlist_t *rsrc;
1425 };
1426 
1427 /*
1428  *  rslfree -- free internal members of struct rsl not expected to be
1429  *	freed elsewhere.
1430  */
1431 static void
1432 rslfree(struct rsl *freeme)
1433 {
1434 	if (freeme->asru != NULL)
1435 		nvlist_free(freeme->asru);
1436 	if (freeme->fru != NULL)
1437 		nvlist_free(freeme->fru);
1438 	if (freeme->rsrc != NULL && freeme->rsrc != freeme->asru)
1439 		nvlist_free(freeme->rsrc);
1440 }
1441 
1442 /*
1443  *  rslcmp -- compare two rsl structures.  Use the following
1444  *	comparisons to establish cardinality:
1445  *
1446  *	1. Name of the suspect's class. (simple strcmp)
1447  *	2. Name of the suspect's ASRU. (trickier, since nvlist)
1448  *
1449  */
1450 static int
1451 rslcmp(const void *a, const void *b)
1452 {
1453 	struct rsl *r1 = (struct rsl *)a;
1454 	struct rsl *r2 = (struct rsl *)b;
1455 	int rv;
1456 
1457 	rv = strcmp(r1->suspect->enode->u.event.ename->u.name.s,
1458 	    r2->suspect->enode->u.event.ename->u.name.s);
1459 	if (rv != 0)
1460 		return (rv);
1461 
1462 	if (r1->asru == NULL && r2->asru == NULL)
1463 		return (0);
1464 	if (r1->asru == NULL)
1465 		return (-1);
1466 	if (r2->asru == NULL)
1467 		return (1);
1468 	return (evnv_cmpnvl(r1->asru, r2->asru, 0));
1469 }
1470 
1471 /*
1472  *  rsluniq -- given an array of rsl structures, seek out and "remove"
1473  *	any duplicates.  Dups are "remove"d by NULLing the suspect pointer
1474  *	of the array element.  Removal also means updating the number of
1475  *	problems and the number of problems which are not faults.  User
1476  *	provides the first and last element pointers.
1477  */
1478 static void
1479 rsluniq(struct rsl *first, struct rsl *last, int *nprobs, int *nnonf)
1480 {
1481 	struct rsl *cr;
1482 
1483 	if (*nprobs == 1)
1484 		return;
1485 
1486 	/*
1487 	 *  At this point, we only expect duplicate defects.
1488 	 *  Eversholt's diagnosis algorithm prevents duplicate
1489 	 *  suspects, but we rewrite defects in the platform code after
1490 	 *  the diagnosis is made, and that can introduce new
1491 	 *  duplicates.
1492 	 */
1493 	while (first <= last) {
1494 		if (first->suspect == NULL || !is_defect(first->suspect->t)) {
1495 			first++;
1496 			continue;
1497 		}
1498 		cr = first + 1;
1499 		while (cr <= last) {
1500 			if (is_defect(first->suspect->t)) {
1501 				if (rslcmp(first, cr) == 0) {
1502 					cr->suspect = NULL;
1503 					rslfree(cr);
1504 					(*nprobs)--;
1505 					(*nnonf)--;
1506 				}
1507 			}
1508 			/*
1509 			 * assume all defects are in order after our
1510 			 * sort and short circuit here with "else break" ?
1511 			 */
1512 			cr++;
1513 		}
1514 		first++;
1515 	}
1516 }
1517 
1518 /*
1519  * get_resources -- for a given suspect, determine what ASRU, FRU and
1520  *     RSRC nvlists should be advertised in the final suspect list.
1521  */
1522 void
1523 get_resources(struct event *sp, struct rsl *rsrcs, struct config *croot)
1524 {
1525 	struct node *asrudef, *frudef;
1526 	nvlist_t *asru, *fru;
1527 	nvlist_t *rsrc = NULL;
1528 	char *pathstr;
1529 
1530 	/*
1531 	 * First find any ASRU and/or FRU defined in the
1532 	 * initial fault tree.
1533 	 */
1534 	asrudef = eventprop_lookup(sp, L_ASRU);
1535 	frudef = eventprop_lookup(sp, L_FRU);
1536 
1537 	/*
1538 	 * Create FMRIs based on those definitions
1539 	 */
1540 	asru = node2fmri(asrudef);
1541 	fru = node2fmri(frudef);
1542 	pathstr = ipath2str(NULL, sp->ipp);
1543 
1544 	/*
1545 	 * Allow for platform translations of the FMRIs
1546 	 */
1547 	platform_units_translate(is_defect(sp->t), croot, &asru, &fru, &rsrc,
1548 	    pathstr);
1549 
1550 	FREE(pathstr);
1551 	rsrcs->suspect = sp;
1552 	rsrcs->asru = asru;
1553 	rsrcs->fru = fru;
1554 	rsrcs->rsrc = rsrc;
1555 }
1556 
1557 /*
1558  * trim_suspects -- prior to publishing, we may need to remove some
1559  *    suspects from the list.  If we're auto-closing upsets, we don't
1560  *    want any of those in the published list.  If the ASRUs for multiple
1561  *    defects resolve to the same ASRU (driver) we only want to publish
1562  *    that as a single suspect.
1563  */
1564 static void
1565 trim_suspects(struct fme *fmep, boolean_t no_upsets, struct rsl **begin,
1566     struct rsl **end)
1567 {
1568 	struct event *ep;
1569 	struct rsl *rp;
1570 	int rpcnt;
1571 
1572 	/*
1573 	 * First save the suspects in the psuspects, then copy back
1574 	 * only the ones we wish to retain.  This resets nsuspects to
1575 	 * zero.
1576 	 */
1577 	rpcnt = fmep->nsuspects;
1578 	save_suspects(fmep);
1579 
1580 	/*
1581 	 * allocate an array of resource pointers for the suspects.
1582 	 * We may end up using less than the full allocation, but this
1583 	 * is a very short-lived array.  publish_suspects() will free
1584 	 * this array when it's done using it.
1585 	 */
1586 	rp = *begin = MALLOC(rpcnt * sizeof (struct rsl));
1587 	bzero(rp, rpcnt * sizeof (struct rsl));
1588 
1589 	/* first pass, remove any unwanted upsets and populate our array */
1590 	for (ep = fmep->psuspects; ep; ep = ep->psuspects) {
1591 		if (no_upsets && is_upset(ep->t))
1592 			continue;
1593 		get_resources(ep, rp, fmep->cfgdata->cooked);
1594 		rp++;
1595 		fmep->nsuspects++;
1596 		if (!is_fault(ep->t))
1597 			fmep->nonfault++;
1598 	}
1599 
1600 	/* if all we had was unwanted upsets, we're done */
1601 	if (fmep->nsuspects == 0)
1602 		return;
1603 
1604 	*end = rp - 1;
1605 
1606 	/* sort the array */
1607 	qsort(*begin, fmep->nsuspects, sizeof (struct rsl), rslcmp);
1608 	rsluniq(*begin, *end, &fmep->nsuspects, &fmep->nonfault);
1609 }
1610 
1611 /*
1612  * addpayloadprop -- add a payload prop to a problem
1613  */
1614 static void
1615 addpayloadprop(const char *lhs, struct evalue *rhs, nvlist_t *fault)
1616 {
1617 	ASSERT(fault != NULL);
1618 	ASSERT(lhs != NULL);
1619 	ASSERT(rhs != NULL);
1620 
1621 	if (rhs->t == UINT64) {
1622 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=%llu", lhs, rhs->v);
1623 
1624 		if (nvlist_add_uint64(fault, lhs, rhs->v) != 0)
1625 			out(O_DIE,
1626 			    "cannot add payloadprop \"%s\" to fault", lhs);
1627 	} else {
1628 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=\"%s\"",
1629 		    lhs, (char *)rhs->v);
1630 
1631 		if (nvlist_add_string(fault, lhs, (char *)rhs->v) != 0)
1632 			out(O_DIE,
1633 			    "cannot add payloadprop \"%s\" to fault", lhs);
1634 	}
1635 }
1636 
1637 static char *Istatbuf;
1638 static char *Istatbufptr;
1639 static int Istatsz;
1640 
1641 /*
1642  * istataddsize -- calculate size of istat and add it to Istatsz
1643  */
1644 /*ARGSUSED2*/
1645 static void
1646 istataddsize(const struct istat_entry *lhs, struct stats *rhs, void *arg)
1647 {
1648 	int val;
1649 
1650 	ASSERT(lhs != NULL);
1651 	ASSERT(rhs != NULL);
1652 
1653 	if ((val = stats_counter_value(rhs)) == 0)
1654 		return;	/* skip zero-valued stats */
1655 
1656 	/* count up the size of the stat name */
1657 	Istatsz += ipath2strlen(lhs->ename, lhs->ipath);
1658 	Istatsz++;	/* for the trailing NULL byte */
1659 
1660 	/* count up the size of the stat value */
1661 	Istatsz += snprintf(NULL, 0, "%d", val);
1662 	Istatsz++;	/* for the trailing NULL byte */
1663 }
1664 
1665 /*
1666  * istat2str -- serialize an istat, writing result to *Istatbufptr
1667  */
1668 /*ARGSUSED2*/
1669 static void
1670 istat2str(const struct istat_entry *lhs, struct stats *rhs, void *arg)
1671 {
1672 	char *str;
1673 	int len;
1674 	int val;
1675 
1676 	ASSERT(lhs != NULL);
1677 	ASSERT(rhs != NULL);
1678 
1679 	if ((val = stats_counter_value(rhs)) == 0)
1680 		return;	/* skip zero-valued stats */
1681 
1682 	/* serialize the stat name */
1683 	str = ipath2str(lhs->ename, lhs->ipath);
1684 	len = strlen(str);
1685 
1686 	ASSERT(Istatbufptr + len + 1 < &Istatbuf[Istatsz]);
1687 	(void) strlcpy(Istatbufptr, str, &Istatbuf[Istatsz] - Istatbufptr);
1688 	Istatbufptr += len;
1689 	FREE(str);
1690 	*Istatbufptr++ = '\0';
1691 
1692 	/* serialize the stat value */
1693 	Istatbufptr += snprintf(Istatbufptr, &Istatbuf[Istatsz] - Istatbufptr,
1694 	    "%d", val);
1695 	*Istatbufptr++ = '\0';
1696 
1697 	ASSERT(Istatbufptr <= &Istatbuf[Istatsz]);
1698 }
1699 
1700 void
1701 istat_save()
1702 {
1703 	if (Istat_need_save == 0)
1704 		return;
1705 
1706 	/* figure out how big the serialzed info is */
1707 	Istatsz = 0;
1708 	lut_walk(Istats, (lut_cb)istataddsize, NULL);
1709 
1710 	if (Istatsz == 0) {
1711 		/* no stats to save */
1712 		fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
1713 		return;
1714 	}
1715 
1716 	/* create the serialized buffer */
1717 	Istatbufptr = Istatbuf = MALLOC(Istatsz);
1718 	lut_walk(Istats, (lut_cb)istat2str, NULL);
1719 
1720 	/* clear out current saved stats */
1721 	fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
1722 
1723 	/* write out the new version */
1724 	fmd_buf_write(Hdl, NULL, WOBUF_ISTATS, Istatbuf, Istatsz);
1725 	FREE(Istatbuf);
1726 
1727 	Istat_need_save = 0;
1728 }
1729 
1730 int
1731 istat_cmp(struct istat_entry *ent1, struct istat_entry *ent2)
1732 {
1733 	if (ent1->ename != ent2->ename)
1734 		return (ent2->ename - ent1->ename);
1735 	if (ent1->ipath != ent2->ipath)
1736 		return ((char *)ent2->ipath - (char *)ent1->ipath);
1737 
1738 	return (0);
1739 }
1740 
1741 /*
1742  * istat-verify -- verify the component associated with a stat still exists
1743  *
1744  * if the component no longer exists, this routine resets the stat and
1745  * returns 0.  if the component still exists, it returns 1.
1746  */
1747 static int
1748 istat_verify(struct node *snp, struct istat_entry *entp)
1749 {
1750 	struct stats *statp;
1751 	nvlist_t *fmri;
1752 
1753 	fmri = node2fmri(snp->u.event.epname);
1754 	if (platform_path_exists(fmri)) {
1755 		nvlist_free(fmri);
1756 		return (1);
1757 	}
1758 	nvlist_free(fmri);
1759 
1760 	/* component no longer in system.  zero out the associated stats */
1761 	if ((statp = (struct stats *)
1762 	    lut_lookup(Istats, entp, (lut_cmp)istat_cmp)) == NULL ||
1763 	    stats_counter_value(statp) == 0)
1764 		return (0);	/* stat is already reset */
1765 
1766 	Istat_need_save = 1;
1767 	stats_counter_reset(statp);
1768 	return (0);
1769 }
1770 
1771 static void
1772 istat_bump(struct node *snp, int n)
1773 {
1774 	struct stats *statp;
1775 	struct istat_entry ent;
1776 
1777 	ASSERT(snp != NULL);
1778 	ASSERTinfo(snp->t == T_EVENT, ptree_nodetype2str(snp->t));
1779 	ASSERT(snp->u.event.epname != NULL);
1780 
1781 	/* class name should be hoisted into a single stable entry */
1782 	ASSERT(snp->u.event.ename->u.name.next == NULL);
1783 	ent.ename = snp->u.event.ename->u.name.s;
1784 	ent.ipath = ipath(snp->u.event.epname);
1785 
1786 	if (!istat_verify(snp, &ent)) {
1787 		/* component no longer exists in system, nothing to do */
1788 		return;
1789 	}
1790 
1791 	if ((statp = (struct stats *)
1792 	    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL) {
1793 		/* need to create the counter */
1794 		int cnt = 0;
1795 		struct node *np;
1796 		char *sname;
1797 		char *snamep;
1798 		struct istat_entry *newentp;
1799 
1800 		/* count up the size of the stat name */
1801 		np = snp->u.event.ename;
1802 		while (np != NULL) {
1803 			cnt += strlen(np->u.name.s);
1804 			cnt++;	/* for the '.' or '@' */
1805 			np = np->u.name.next;
1806 		}
1807 		np = snp->u.event.epname;
1808 		while (np != NULL) {
1809 			cnt += snprintf(NULL, 0, "%s%llu",
1810 			    np->u.name.s, np->u.name.child->u.ull);
1811 			cnt++;	/* for the '/' or trailing NULL byte */
1812 			np = np->u.name.next;
1813 		}
1814 
1815 		/* build the stat name */
1816 		snamep = sname = alloca(cnt);
1817 		np = snp->u.event.ename;
1818 		while (np != NULL) {
1819 			snamep += snprintf(snamep, &sname[cnt] - snamep,
1820 			    "%s", np->u.name.s);
1821 			np = np->u.name.next;
1822 			if (np)
1823 				*snamep++ = '.';
1824 		}
1825 		*snamep++ = '@';
1826 		np = snp->u.event.epname;
1827 		while (np != NULL) {
1828 			snamep += snprintf(snamep, &sname[cnt] - snamep,
1829 			    "%s%llu", np->u.name.s, np->u.name.child->u.ull);
1830 			np = np->u.name.next;
1831 			if (np)
1832 				*snamep++ = '/';
1833 		}
1834 		*snamep++ = '\0';
1835 
1836 		/* create the new stat & add it to our list */
1837 		newentp = MALLOC(sizeof (*newentp));
1838 		*newentp = ent;
1839 		statp = stats_new_counter(NULL, sname, 0);
1840 		Istats = lut_add(Istats, (void *)newentp, (void *)statp,
1841 		    (lut_cmp)istat_cmp);
1842 	}
1843 
1844 	/* if n is non-zero, set that value instead of bumping */
1845 	if (n) {
1846 		stats_counter_reset(statp);
1847 		stats_counter_add(statp, n);
1848 	} else
1849 		stats_counter_bump(statp);
1850 	Istat_need_save = 1;
1851 }
1852 
1853 /*ARGSUSED*/
1854 static void
1855 istat_destructor(void *left, void *right, void *arg)
1856 {
1857 	struct istat_entry *entp = (struct istat_entry *)left;
1858 	struct stats *statp = (struct stats *)right;
1859 	FREE(entp);
1860 	stats_delete(statp);
1861 }
1862 
1863 void
1864 istat_fini(void)
1865 {
1866 	lut_free(Istats, istat_destructor, NULL);
1867 }
1868 
1869 static void
1870 publish_suspects(struct fme *fmep)
1871 {
1872 	struct event *ep;
1873 	struct rsl *srl = NULL;
1874 	struct rsl *erl;
1875 	struct rsl *rp;
1876 	nvlist_t *fault;
1877 	uint8_t cert;
1878 	uint_t *frs;
1879 	uint_t fravg, frsum, fr;
1880 	uint_t messval;
1881 	struct node *snp;
1882 	int frcnt, fridx;
1883 	boolean_t no_upsets = B_FALSE;
1884 	boolean_t allfaulty = B_TRUE;
1885 
1886 	stats_counter_bump(fmep->diags);
1887 
1888 	/*
1889 	 * The current fmd interfaces don't allow us to solve a case
1890 	 * that's already solved.  If we make a new case, what of the
1891 	 * ereports?  We don't appear to have an interface that allows
1892 	 * us to access the ereports attached to a case (if we wanted
1893 	 * to copy the original case's ereport attachments to the new
1894 	 * case) and it's also a bit unclear if there would be any
1895 	 * problems with having ereports attached to multiple cases
1896 	 * and/or attaching DIAGNOSED ereports to a case.  For now,
1897 	 * we'll just output a message.
1898 	 */
1899 	if (fmep->posted_suspects ||
1900 	    fmd_case_solved(fmep->hdl, fmep->fmcase)) {
1901 		out(O_ALTFP|O_NONL, "Revised diagnosis for case %s: ",
1902 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
1903 		for (ep = fmep->suspects; ep; ep = ep->suspects) {
1904 			out(O_ALTFP|O_NONL, " ");
1905 			itree_pevent_brief(O_ALTFP|O_NONL, ep);
1906 		}
1907 		out(O_ALTFP, NULL);
1908 		return;
1909 	}
1910 
1911 	/*
1912 	 * If we're auto-closing upsets, we don't want to include them
1913 	 * in any produced suspect lists or certainty accounting.
1914 	 */
1915 	if (Autoclose != NULL)
1916 		if (strcmp(Autoclose, "true") == 0 ||
1917 		    strcmp(Autoclose, "all") == 0 ||
1918 		    strcmp(Autoclose, "upsets") == 0)
1919 			no_upsets = B_TRUE;
1920 
1921 	trim_suspects(fmep, no_upsets, &srl, &erl);
1922 
1923 	/*
1924 	 * If the resulting suspect list has no members, we're
1925 	 * done.  Returning here will simply close the case.
1926 	 */
1927 	if (fmep->nsuspects == 0) {
1928 		out(O_ALTFP,
1929 		    "[FME%d, case %s (all suspects are upsets)]",
1930 		    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
1931 		FREE(srl);
1932 		restore_suspects(fmep);
1933 		return;
1934 	}
1935 
1936 	/*
1937 	 * If the suspect list is all faults, then for a given fault,
1938 	 * say X of N, X's certainty is computed via:
1939 	 *
1940 	 * fitrate(X) / (fitrate(1) + ... + fitrate(N)) * 100
1941 	 *
1942 	 * If none of the suspects are faults, and there are N suspects,
1943 	 * the certainty of a given suspect is 100/N.
1944 	 *
1945 	 * If there are are a mixture of faults and other problems in
1946 	 * the suspect list, we take an average of the faults'
1947 	 * FITrates and treat this average as the FITrate for any
1948 	 * non-faults.  The fitrate of any given suspect is then
1949 	 * computed per the first formula above.
1950 	 */
1951 	if (fmep->nonfault == fmep->nsuspects) {
1952 		/* NO faults in the suspect list */
1953 		cert = percentof(1, fmep->nsuspects);
1954 	} else {
1955 		/* sum the fitrates */
1956 		frs = alloca(fmep->nsuspects * sizeof (uint_t));
1957 		fridx = frcnt = frsum = 0;
1958 
1959 		for (rp = srl; rp <= erl; rp++) {
1960 			struct node *n;
1961 
1962 			if (rp->suspect == NULL)
1963 				continue;
1964 			if (!is_fault(rp->suspect->t)) {
1965 				frs[fridx++] = 0;
1966 				continue;
1967 			}
1968 			n = eventprop_lookup(rp->suspect, L_FITrate);
1969 			if (node2uint(n, &fr) != 0) {
1970 				out(O_DEBUG|O_NONL, "event ");
1971 				ipath_print(O_DEBUG|O_NONL,
1972 				    ep->enode->u.event.ename->u.name.s,
1973 				    ep->ipp);
1974 				out(O_DEBUG, " has no FITrate (using 1)");
1975 				fr = 1;
1976 			} else if (fr == 0) {
1977 				out(O_DEBUG|O_NONL, "event ");
1978 				ipath_print(O_DEBUG|O_NONL,
1979 				    ep->enode->u.event.ename->u.name.s,
1980 				    ep->ipp);
1981 				out(O_DEBUG, " has zero FITrate (using 1)");
1982 				fr = 1;
1983 			}
1984 
1985 			frs[fridx++] = fr;
1986 			frsum += fr;
1987 			frcnt++;
1988 		}
1989 		fravg = avg(frsum, frcnt);
1990 		for (fridx = 0; fridx < fmep->nsuspects; fridx++)
1991 			if (frs[fridx] == 0) {
1992 				frs[fridx] = fravg;
1993 				frsum += fravg;
1994 			}
1995 	}
1996 
1997 	/* Add them in reverse order of our sort, as fmd reverses order */
1998 	for (rp = erl; rp >= srl; rp--) {
1999 		if (rp->suspect == NULL)
2000 			continue;
2001 		if (!is_fault(rp->suspect->t))
2002 			allfaulty = B_FALSE;
2003 		if (fmep->nonfault != fmep->nsuspects)
2004 			cert = percentof(frs[--fridx], frsum);
2005 		fault = fmd_nvl_create_fault(fmep->hdl,
2006 		    rp->suspect->enode->u.event.ename->u.name.s,
2007 		    cert,
2008 		    rp->asru,
2009 		    rp->fru,
2010 		    rp->rsrc);
2011 		if (fault == NULL)
2012 			out(O_DIE, "fault creation failed");
2013 		/* if "message" property exists, add it to the fault */
2014 		if (node2uint(eventprop_lookup(rp->suspect, L_message),
2015 		    &messval) == 0) {
2016 
2017 			out(O_ALTFP,
2018 			    "[FME%d, %s adds message=%d to suspect list]",
2019 			    fmep->id,
2020 			    rp->suspect->enode->u.event.ename->u.name.s,
2021 			    messval);
2022 			if (nvlist_add_boolean_value(fault,
2023 			    FM_SUSPECT_MESSAGE,
2024 			    (messval) ? B_TRUE : B_FALSE) != 0) {
2025 				out(O_DIE, "cannot add no-message to fault");
2026 			}
2027 		}
2028 		/* add any payload properties */
2029 		lut_walk(rp->suspect->payloadprops,
2030 		    (lut_cb)addpayloadprop, (void *)fault);
2031 		fmd_case_add_suspect(fmep->hdl, fmep->fmcase, fault);
2032 		rp->suspect->fault = fault;
2033 		rslfree(rp);
2034 		/* if "count" property exists, increment the appropriate stat */
2035 		if ((snp = eventprop_lookup(rp->suspect, L_count)) != NULL) {
2036 			out(O_ALTFP|O_NONL,
2037 			    "[FME%d, %s count ", fmep->id,
2038 			    rp->suspect->enode->u.event.ename->u.name.s);
2039 			ptree_name_iter(O_ALTFP|O_NONL, snp);
2040 			out(O_ALTFP, "]");
2041 			istat_bump(snp, 0);
2042 		}
2043 		/* if "action" property exists, evaluate it */
2044 		if ((snp = eventprop_lookup(rp->suspect, L_action)) != NULL) {
2045 			struct evalue evalue;
2046 
2047 			out(O_ALTFP|O_NONL,
2048 			    "[FME%d, %s action ", fmep->id,
2049 			    rp->suspect->enode->u.event.ename->u.name.s);
2050 			ptree_name_iter(O_ALTFP|O_NONL, snp);
2051 			out(O_ALTFP, "]");
2052 			Action_nvl = fault;
2053 			(void) eval_expr(snp, NULL, NULL, NULL, NULL,
2054 			    NULL, 0, &evalue);
2055 		}
2056 		/*
2057 		 * if "dupclose" tunable is set, check if the asru is
2058 		 * already marked as "faulty".
2059 		 */
2060 		if (Dupclose && allfaulty) {
2061 			nvlist_t *asru;
2062 
2063 			out(O_ALTFP|O_VERB, "FMD%d dupclose check ", fmep->id);
2064 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, rp->suspect);
2065 			out(O_ALTFP|O_VERB|O_NONL, " ");
2066 			if (nvlist_lookup_nvlist(fault,
2067 			    FM_FAULT_ASRU, &asru) != 0) {
2068 				out(O_ALTFP|O_VERB, "NULL asru");
2069 				allfaulty = B_FALSE;
2070 			} else if (fmd_nvl_fmri_faulty(fmep->hdl, asru)) {
2071 				out(O_ALTFP|O_VERB, "faulty");
2072 			} else {
2073 				out(O_ALTFP|O_VERB, "not faulty");
2074 				allfaulty = B_FALSE;
2075 			}
2076 		}
2077 
2078 	}
2079 	if (Dupclose && allfaulty) {
2080 		out(O_ALTFP, "[dupclose FME%d, case %s]", fmep->id,
2081 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
2082 		fmd_case_close(fmep->hdl, fmep->fmcase);
2083 	} else {
2084 		out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
2085 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
2086 		fmd_case_solve(fmep->hdl, fmep->fmcase);
2087 	}
2088 
2089 	istat_save();	/* write out any istat changes */
2090 
2091 	/*
2092 	 * revert to the original suspect list
2093 	 */
2094 	FREE(srl);
2095 	restore_suspects(fmep);
2096 }
2097 
2098 static void
2099 publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep)
2100 {
2101 	struct case_list *newcase;
2102 	nvlist_t *defect;
2103 
2104 	out(O_ALTFP,
2105 	    "[undiagnosable ereport received, "
2106 	    "creating and closing a new case (%s)]",
2107 	    Undiag_reason ? Undiag_reason : "reason not provided");
2108 
2109 	newcase = MALLOC(sizeof (struct case_list));
2110 	newcase->next = NULL;
2111 
2112 	newcase->fmcase = fmd_case_open(hdl, NULL);
2113 	if (Undiagablecaselist != NULL)
2114 		newcase->next = Undiagablecaselist;
2115 	Undiagablecaselist = newcase;
2116 
2117 	if (ffep != NULL)
2118 		fmd_case_add_ereport(hdl, newcase->fmcase, ffep);
2119 
2120 	defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
2121 	    NULL, NULL, NULL);
2122 	if (Undiag_reason != NULL)
2123 		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
2124 	fmd_case_add_suspect(hdl, newcase->fmcase, defect);
2125 
2126 	fmd_case_solve(hdl, newcase->fmcase);
2127 	fmd_case_close(hdl, newcase->fmcase);
2128 }
2129 
2130 static void
2131 fme_undiagnosable(struct fme *f)
2132 {
2133 	nvlist_t *defect;
2134 
2135 	out(O_ALTFP, "[solving/closing FME%d, case %s (%s)]",
2136 	    f->id, fmd_case_uuid(f->hdl, f->fmcase),
2137 	    Undiag_reason ? Undiag_reason : "undiagnosable");
2138 
2139 	defect = fmd_nvl_create_fault(f->hdl, UNDIAGNOSABLE_DEFECT, 100,
2140 	    NULL, NULL, NULL);
2141 	if (Undiag_reason != NULL)
2142 		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
2143 	fmd_case_add_suspect(f->hdl, f->fmcase, defect);
2144 	fmd_case_solve(f->hdl, f->fmcase);
2145 	destroy_fme_bufs(f);
2146 	fmd_case_close(f->hdl, f->fmcase);
2147 }
2148 
2149 /*
2150  * fme_close_case
2151  *
2152  *	Find the requested case amongst our fmes and close it.  Free up
2153  *	the related fme.
2154  */
2155 void
2156 fme_close_case(fmd_hdl_t *hdl, fmd_case_t *fmcase)
2157 {
2158 	struct case_list *ucasep, *prevcasep = NULL;
2159 	struct fme *prev = NULL;
2160 	struct fme *fmep;
2161 
2162 	for (ucasep = Undiagablecaselist; ucasep; ucasep = ucasep->next) {
2163 		if (fmcase != ucasep->fmcase) {
2164 			prevcasep = ucasep;
2165 			continue;
2166 		}
2167 
2168 		if (prevcasep == NULL)
2169 			Undiagablecaselist = Undiagablecaselist->next;
2170 		else
2171 			prevcasep->next = ucasep->next;
2172 
2173 		FREE(ucasep);
2174 		return;
2175 	}
2176 
2177 	for (fmep = FMElist; fmep; fmep = fmep->next) {
2178 		if (fmep->hdl == hdl && fmep->fmcase == fmcase)
2179 			break;
2180 		prev = fmep;
2181 	}
2182 
2183 	if (fmep == NULL) {
2184 		out(O_WARN, "Eft asked to close unrecognized case [%s].",
2185 		    fmd_case_uuid(hdl, fmcase));
2186 		return;
2187 	}
2188 
2189 	if (EFMElist == fmep)
2190 		EFMElist = prev;
2191 
2192 	if (prev == NULL)
2193 		FMElist = FMElist->next;
2194 	else
2195 		prev->next = fmep->next;
2196 
2197 	fmep->next = NULL;
2198 
2199 	/* Get rid of any timer this fme has set */
2200 	if (fmep->wull != 0)
2201 		fmd_timer_remove(fmep->hdl, fmep->timer);
2202 
2203 	if (ClosedFMEs == NULL) {
2204 		ClosedFMEs = fmep;
2205 	} else {
2206 		fmep->next = ClosedFMEs;
2207 		ClosedFMEs = fmep;
2208 	}
2209 
2210 	Open_fme_count--;
2211 
2212 	/* See if we can close the overflow FME */
2213 	if (Open_fme_count <= Max_fme) {
2214 		for (fmep = FMElist; fmep; fmep = fmep->next) {
2215 			if (fmep->overflow && !(fmd_case_closed(fmep->hdl,
2216 			    fmep->fmcase)))
2217 				break;
2218 		}
2219 
2220 		if (fmep != NULL)
2221 			fmd_case_close(fmep->hdl, fmep->fmcase);
2222 	}
2223 }
2224 
2225 /*
2226  * fme_set_timer()
2227  *	If the time we need to wait for the given FME is less than the
2228  *	current timer, kick that old timer out and establish a new one.
2229  */
2230 static int
2231 fme_set_timer(struct fme *fmep, unsigned long long wull)
2232 {
2233 	out(O_ALTFP|O_VERB|O_NONL, " fme_set_timer: request to wait ");
2234 	ptree_timeval(O_ALTFP|O_VERB, &wull);
2235 
2236 	if (wull <= fmep->pull) {
2237 		out(O_ALTFP|O_VERB|O_NONL, "already have waited at least ");
2238 		ptree_timeval(O_ALTFP|O_VERB, &fmep->pull);
2239 		out(O_ALTFP|O_VERB, NULL);
2240 		/* we've waited at least wull already, don't need timer */
2241 		return (0);
2242 	}
2243 
2244 	out(O_ALTFP|O_VERB|O_NONL, " currently ");
2245 	if (fmep->wull != 0) {
2246 		out(O_ALTFP|O_VERB|O_NONL, "waiting ");
2247 		ptree_timeval(O_ALTFP|O_VERB, &fmep->wull);
2248 		out(O_ALTFP|O_VERB, NULL);
2249 	} else {
2250 		out(O_ALTFP|O_VERB|O_NONL, "not waiting");
2251 		out(O_ALTFP|O_VERB, NULL);
2252 	}
2253 
2254 	if (fmep->wull != 0)
2255 		if (wull >= fmep->wull)
2256 			/* New timer would fire later than established timer */
2257 			return (0);
2258 
2259 	if (fmep->wull != 0) {
2260 		fmd_timer_remove(fmep->hdl, fmep->timer);
2261 		if (fmep->timer == fmep->htid) {
2262 			out(O_ALTFP,
2263 			    "[stopped hesitating FME%d, case %s]",
2264 			    fmep->id,
2265 			    fmd_case_uuid(fmep->hdl,
2266 			    fmep->fmcase));
2267 			fmep->htid = 0;
2268 		}
2269 	}
2270 
2271 	fmep->timer = fmd_timer_install(fmep->hdl, (void *)fmep,
2272 	    fmep->e0r, wull);
2273 	out(O_ALTFP|O_VERB, "timer set, id is %ld", fmep->timer);
2274 	fmep->wull = wull;
2275 	return (1);
2276 }
2277 
2278 void
2279 fme_timer_fired(struct fme *fmep, id_t tid)
2280 {
2281 	struct fme *ffmep = NULL;
2282 
2283 	for (ffmep = FMElist; ffmep; ffmep = ffmep->next)
2284 		if (ffmep == fmep)
2285 			break;
2286 
2287 	if (ffmep == NULL) {
2288 		out(O_WARN, "Timer fired for an FME (%p) not in FMEs list.",
2289 		    (void *)fmep);
2290 		return;
2291 	}
2292 
2293 	out(O_ALTFP, "Timer fired %lx %lx", tid, fmep->htid);
2294 	if (tid != fmep->htid) {
2295 		/*
2296 		 * normal timer (not the hesitation timer)
2297 		 */
2298 		fmep->pull = fmep->wull;
2299 		fmep->wull = 0;
2300 		fmd_buf_write(fmep->hdl, fmep->fmcase,
2301 		    WOBUF_PULL, (void *)&fmep->pull, sizeof (fmep->pull));
2302 		/*
2303 		 * no point in heistating if we've already waited.
2304 		 */
2305 		fmep->hesitated = 1;
2306 	} else {
2307 		fmep->hesitated = 1;
2308 	}
2309 	fme_eval(fmep, NULL);
2310 }
2311 
2312 /*
2313  * Preserve the fme's suspect list in its psuspects list, NULLing the
2314  * suspects list in the meantime.
2315  */
2316 static void
2317 save_suspects(struct fme *fmep)
2318 {
2319 	struct event *ep;
2320 	struct event *nextep;
2321 
2322 	/* zero out the previous suspect list */
2323 	for (ep = fmep->psuspects; ep; ep = nextep) {
2324 		nextep = ep->psuspects;
2325 		ep->psuspects = NULL;
2326 	}
2327 	fmep->psuspects = NULL;
2328 
2329 	/* zero out the suspect list, copying it to previous suspect list */
2330 	fmep->psuspects = fmep->suspects;
2331 	for (ep = fmep->suspects; ep; ep = nextep) {
2332 		nextep = ep->suspects;
2333 		ep->psuspects = ep->suspects;
2334 		ep->suspects = NULL;
2335 		ep->is_suspect = 0;
2336 	}
2337 	fmep->suspects = NULL;
2338 	fmep->nsuspects = 0;
2339 	fmep->nonfault = 0;
2340 }
2341 
2342 /*
2343  * Retrieve the fme's suspect list from its psuspects list.
2344  */
2345 static void
2346 restore_suspects(struct fme *fmep)
2347 {
2348 	struct event *ep;
2349 	struct event *nextep;
2350 
2351 	fmep->nsuspects = fmep->nonfault = 0;
2352 	fmep->suspects = fmep->psuspects;
2353 	for (ep = fmep->psuspects; ep; ep = nextep) {
2354 		fmep->nsuspects++;
2355 		if (!is_fault(ep->t))
2356 			fmep->nonfault++;
2357 		nextep = ep->psuspects;
2358 		ep->suspects = ep->psuspects;
2359 	}
2360 }
2361 
2362 /*
2363  * this is what we use to call the Emrys prototype code instead of main()
2364  */
2365 static void
2366 fme_eval(struct fme *fmep, fmd_event_t *ffep)
2367 {
2368 	struct event *ep;
2369 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
2370 
2371 	save_suspects(fmep);
2372 
2373 	out(O_ALTFP|O_VERB, "Evaluate FME %d", fmep->id);
2374 	indent_set("  ");
2375 
2376 	lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
2377 	fmep->state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
2378 
2379 	out(O_ALTFP|O_VERB|O_NONL, "FME%d state: %s, suspect list:", fmep->id,
2380 	    fme_state2str(fmep->state));
2381 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
2382 		out(O_ALTFP|O_VERB|O_NONL, " ");
2383 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2384 	}
2385 	out(O_ALTFP|O_VERB, NULL);
2386 
2387 	if (fmep->posted_suspects) {
2388 		/*
2389 		 * this FME has already posted a diagnosis, so see if
2390 		 * the event changed the diagnosis and print a warning
2391 		 * if it did.
2392 		 *
2393 		 */
2394 		if (suspects_changed(fmep)) {
2395 			print_suspects(SLCHANGED, fmep);
2396 			publish_suspects(fmep);
2397 		}
2398 	} else {
2399 		switch (fmep->state) {
2400 		case FME_CREDIBLE:
2401 			/*
2402 			 * if the suspect list contains any upsets, we
2403 			 * turn off the hesitation logic (by setting
2404 			 * the hesitate flag which normally indicates
2405 			 * we've already done the hesitate logic).
2406 			 * this is done because hesitating with upsets
2407 			 * causes us to explain away additional soft errors
2408 			 * while the upset FME stays open.
2409 			 */
2410 			if (fmep->hesitated == 0) {
2411 				struct event *s;
2412 
2413 				for (s = fmep->suspects; s; s = s->suspects) {
2414 					if (s->t == N_UPSET) {
2415 						fmep->hesitated = 1;
2416 						break;
2417 					}
2418 				}
2419 			}
2420 
2421 			if (Hesitate &&
2422 			    fmep->suspects != NULL &&
2423 			    fmep->suspects->suspects != NULL &&
2424 			    fmep->hesitated == 0) {
2425 				/*
2426 				 * about to publish multi-entry suspect list,
2427 				 * set the hesitation timer if not already set.
2428 				 */
2429 				if (fmep->htid == 0) {
2430 					out(O_ALTFP|O_NONL,
2431 					    "[hesitate FME%d, case %s ",
2432 					    fmep->id,
2433 					    fmd_case_uuid(fmep->hdl,
2434 					    fmep->fmcase));
2435 					ptree_timeval(O_ALTFP|O_NONL,
2436 					    (unsigned long long *)&Hesitate);
2437 					out(O_ALTFP, "]");
2438 					if (fme_set_timer(fmep, Hesitate))
2439 						fmep->htid = fmep->timer;
2440 				} else {
2441 					out(O_ALTFP,
2442 					    "[still hesitating FME%d, case %s]",
2443 					    fmep->id,
2444 					    fmd_case_uuid(fmep->hdl,
2445 					    fmep->fmcase));
2446 				}
2447 			} else {
2448 				print_suspects(SLNEW, fmep);
2449 				(void) upsets_eval(fmep, ffep);
2450 				publish_suspects(fmep);
2451 				fmep->posted_suspects = 1;
2452 				fmd_buf_write(fmep->hdl, fmep->fmcase,
2453 				    WOBUF_POSTD,
2454 				    (void *)&fmep->posted_suspects,
2455 				    sizeof (fmep->posted_suspects));
2456 			}
2457 			break;
2458 
2459 		case FME_WAIT:
2460 			/*
2461 			 * singleton suspect list implies
2462 			 * no point in waiting
2463 			 */
2464 			if (fmep->suspects &&
2465 			    fmep->suspects->suspects == NULL) {
2466 				print_suspects(SLNEW, fmep);
2467 				(void) upsets_eval(fmep, ffep);
2468 				publish_suspects(fmep);
2469 				fmep->posted_suspects = 1;
2470 				fmd_buf_write(fmep->hdl, fmep->fmcase,
2471 				    WOBUF_POSTD,
2472 				    (void *)&fmep->posted_suspects,
2473 				    sizeof (fmep->posted_suspects));
2474 				fmep->state = FME_CREDIBLE;
2475 			} else {
2476 				ASSERT(my_delay > fmep->ull);
2477 				(void) fme_set_timer(fmep, my_delay);
2478 				print_suspects(SLWAIT, fmep);
2479 			}
2480 			break;
2481 
2482 		case FME_DISPROVED:
2483 			print_suspects(SLDISPROVED, fmep);
2484 			Undiag_reason = UD_UNSOLVD;
2485 			fme_undiagnosable(fmep);
2486 			break;
2487 		}
2488 	}
2489 
2490 	if (fmep->posted_suspects == 1 && Autoclose != NULL) {
2491 		int doclose = 0;
2492 
2493 		if (strcmp(Autoclose, "true") == 0 ||
2494 		    strcmp(Autoclose, "all") == 0)
2495 			doclose = 1;
2496 
2497 		if (strcmp(Autoclose, "upsets") == 0) {
2498 			doclose = 1;
2499 			for (ep = fmep->suspects; ep; ep = ep->suspects) {
2500 				if (ep->t != N_UPSET) {
2501 					doclose = 0;
2502 					break;
2503 				}
2504 			}
2505 		}
2506 
2507 		if (doclose) {
2508 			out(O_ALTFP, "[closing FME%d, case %s (autoclose)]",
2509 			    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
2510 
2511 			destroy_fme_bufs(fmep);
2512 			fmd_case_close(fmep->hdl, fmep->fmcase);
2513 		}
2514 	}
2515 }
2516 
2517 static void indent(void);
2518 static int triggered(struct fme *fmep, struct event *ep, int mark);
2519 static enum fme_state effects_test(struct fme *fmep,
2520     struct event *fault_event, unsigned long long at_latest_by,
2521     unsigned long long *pdelay);
2522 static enum fme_state requirements_test(struct fme *fmep, struct event *ep,
2523     unsigned long long at_latest_by, unsigned long long *pdelay);
2524 static enum fme_state causes_test(struct fme *fmep, struct event *ep,
2525     unsigned long long at_latest_by, unsigned long long *pdelay);
2526 
2527 static int
2528 checkconstraints(struct fme *fmep, struct arrow *arrowp)
2529 {
2530 	struct constraintlist *ctp;
2531 	struct evalue value;
2532 
2533 	if (arrowp->forever_false) {
2534 		char *sep = "";
2535 		indent();
2536 		out(O_ALTFP|O_VERB|O_NONL, "  Forever false constraint: ");
2537 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
2538 			out(O_ALTFP|O_VERB|O_NONL, sep);
2539 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
2540 			sep = ", ";
2541 		}
2542 		out(O_ALTFP|O_VERB, NULL);
2543 		return (0);
2544 	}
2545 
2546 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
2547 		if (eval_expr(ctp->cnode, NULL, NULL,
2548 		    &fmep->globals, fmep->cfgdata->cooked,
2549 		    arrowp, 0, &value)) {
2550 			/* evaluation successful */
2551 			if (value.t == UNDEFINED || value.v == 0) {
2552 				/* known false */
2553 				arrowp->forever_false = 1;
2554 				indent();
2555 				out(O_ALTFP|O_VERB|O_NONL,
2556 				    "  False constraint: ");
2557 				ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
2558 				out(O_ALTFP|O_VERB, NULL);
2559 				return (0);
2560 			}
2561 		} else {
2562 			/* evaluation unsuccessful -- unknown value */
2563 			indent();
2564 			out(O_ALTFP|O_VERB|O_NONL,
2565 			    "  Deferred constraint: ");
2566 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
2567 			out(O_ALTFP|O_VERB, NULL);
2568 			return (2);
2569 		}
2570 	}
2571 	/* known true */
2572 	return (1);
2573 }
2574 
2575 static int
2576 triggered(struct fme *fmep, struct event *ep, int mark)
2577 {
2578 	struct bubble *bp;
2579 	struct arrowlist *ap;
2580 	int count = 0;
2581 
2582 	stats_counter_bump(fmep->Tcallcount);
2583 	for (bp = itree_next_bubble(ep, NULL); bp;
2584 	    bp = itree_next_bubble(ep, bp)) {
2585 		if (bp->t != B_TO)
2586 			continue;
2587 		for (ap = itree_next_arrow(bp, NULL); ap;
2588 		    ap = itree_next_arrow(bp, ap)) {
2589 			/* check count of marks against K in the bubble */
2590 			if ((ap->arrowp->mark & mark) &&
2591 			    ++count >= bp->nork)
2592 				return (1);
2593 		}
2594 	}
2595 	return (0);
2596 }
2597 
2598 static int
2599 mark_arrows(struct fme *fmep, struct event *ep, int mark,
2600     unsigned long long at_latest_by, unsigned long long *pdelay)
2601 {
2602 	struct bubble *bp;
2603 	struct arrowlist *ap;
2604 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
2605 	unsigned long long my_delay;
2606 	enum fme_state result;
2607 	int retval = 0;
2608 
2609 	for (bp = itree_next_bubble(ep, NULL); bp;
2610 	    bp = itree_next_bubble(ep, bp)) {
2611 		if (bp->t != B_FROM)
2612 			continue;
2613 		stats_counter_bump(fmep->Marrowcount);
2614 		for (ap = itree_next_arrow(bp, NULL); ap;
2615 		    ap = itree_next_arrow(bp, ap)) {
2616 			struct event *ep2 = ap->arrowp->head->myevent;
2617 			/*
2618 			 * if we're clearing marks, we can avoid doing
2619 			 * all that work evaluating constraints.
2620 			 */
2621 			if (mark == 0) {
2622 				ap->arrowp->mark &= ~EFFECTS_COUNTER;
2623 				ep2->cached_state &=
2624 				    ~(WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT);
2625 				(void) mark_arrows(fmep, ep2, mark, 0, NULL);
2626 				continue;
2627 			}
2628 			if (ep2->cached_state & REQMNTS_DISPROVED) {
2629 				indent();
2630 				out(O_ALTFP|O_VERB|O_NONL,
2631 				    "  ALREADY DISPROVED ");
2632 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2633 				out(O_ALTFP|O_VERB, NULL);
2634 				continue;
2635 			}
2636 			if (ep2->cached_state & WAIT_EFFECT) {
2637 				indent();
2638 				out(O_ALTFP|O_VERB|O_NONL,
2639 				    "  ALREADY EFFECTS WAIT ");
2640 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2641 				out(O_ALTFP|O_VERB, NULL);
2642 				continue;
2643 			}
2644 			if (ep2->cached_state & CREDIBLE_EFFECT) {
2645 				indent();
2646 				out(O_ALTFP|O_VERB|O_NONL,
2647 				    "  ALREADY EFFECTS CREDIBLE ");
2648 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2649 				out(O_ALTFP|O_VERB, NULL);
2650 				continue;
2651 			}
2652 			if ((ep2->cached_state & PARENT_WAIT) &&
2653 			    (mark & PARENT_WAIT)) {
2654 				indent();
2655 				out(O_ALTFP|O_VERB|O_NONL,
2656 				    "  ALREADY PARENT EFFECTS WAIT ");
2657 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2658 				out(O_ALTFP|O_VERB, NULL);
2659 				continue;
2660 			}
2661 			platform_set_payloadnvp(ep2->nvp);
2662 			if (checkconstraints(fmep, ap->arrowp) != 1) {
2663 				platform_set_payloadnvp(NULL);
2664 				indent();
2665 				out(O_ALTFP|O_VERB|O_NONL,
2666 				    "  CONSTRAINTS FAIL ");
2667 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2668 				out(O_ALTFP|O_VERB, NULL);
2669 				continue;
2670 			}
2671 			platform_set_payloadnvp(NULL);
2672 			ap->arrowp->mark |= EFFECTS_COUNTER;
2673 			if (!triggered(fmep, ep2, EFFECTS_COUNTER)) {
2674 				indent();
2675 				out(O_ALTFP|O_VERB|O_NONL,
2676 				    "  K-COUNT NOT YET MET ");
2677 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2678 				out(O_ALTFP|O_VERB, NULL);
2679 				continue;
2680 			}
2681 			ep2->cached_state &= ~PARENT_WAIT;
2682 			result = requirements_test(fmep, ep2, at_latest_by +
2683 			    ap->arrowp->maxdelay,
2684 			    &my_delay);
2685 			if (result == FME_WAIT) {
2686 				retval = WAIT_EFFECT;
2687 				if (overall_delay > my_delay)
2688 					overall_delay = my_delay;
2689 				ep2->cached_state |= WAIT_EFFECT;
2690 				indent();
2691 				out(O_ALTFP|O_VERB|O_NONL, "  EFFECTS WAIT ");
2692 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2693 				out(O_ALTFP|O_VERB, NULL);
2694 				indent_push("  E");
2695 				if (mark_arrows(fmep, ep2, PARENT_WAIT,
2696 				    at_latest_by, &my_delay) == WAIT_EFFECT) {
2697 					retval = WAIT_EFFECT;
2698 					if (overall_delay > my_delay)
2699 						overall_delay = my_delay;
2700 				}
2701 				indent_pop();
2702 			} else if (result == FME_DISPROVED) {
2703 				indent();
2704 				out(O_ALTFP|O_VERB|O_NONL,
2705 				    "  EFFECTS DISPROVED ");
2706 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2707 				out(O_ALTFP|O_VERB, NULL);
2708 			} else {
2709 				ep2->cached_state |= mark;
2710 				indent();
2711 				if (mark == CREDIBLE_EFFECT)
2712 					out(O_ALTFP|O_VERB|O_NONL,
2713 					    "  EFFECTS CREDIBLE ");
2714 				else
2715 					out(O_ALTFP|O_VERB|O_NONL,
2716 					    "  PARENT EFFECTS WAIT ");
2717 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2718 				out(O_ALTFP|O_VERB, NULL);
2719 				indent_push("  E");
2720 				if (mark_arrows(fmep, ep2, mark, at_latest_by,
2721 				    &my_delay) == WAIT_EFFECT) {
2722 					retval = WAIT_EFFECT;
2723 					if (overall_delay > my_delay)
2724 						overall_delay = my_delay;
2725 				}
2726 				indent_pop();
2727 			}
2728 		}
2729 	}
2730 	if (retval == WAIT_EFFECT)
2731 		*pdelay = overall_delay;
2732 	return (retval);
2733 }
2734 
2735 static enum fme_state
2736 effects_test(struct fme *fmep, struct event *fault_event,
2737     unsigned long long at_latest_by, unsigned long long *pdelay)
2738 {
2739 	struct event *error_event;
2740 	enum fme_state return_value = FME_CREDIBLE;
2741 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
2742 	unsigned long long my_delay;
2743 
2744 	stats_counter_bump(fmep->Ecallcount);
2745 	indent_push("  E");
2746 	indent();
2747 	out(O_ALTFP|O_VERB|O_NONL, "->");
2748 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
2749 	out(O_ALTFP|O_VERB, NULL);
2750 
2751 	(void) mark_arrows(fmep, fault_event, CREDIBLE_EFFECT, at_latest_by,
2752 	    &my_delay);
2753 	for (error_event = fmep->observations;
2754 	    error_event; error_event = error_event->observations) {
2755 		indent();
2756 		out(O_ALTFP|O_VERB|O_NONL, " ");
2757 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, error_event);
2758 		if (!(error_event->cached_state & CREDIBLE_EFFECT)) {
2759 			if (error_event->cached_state &
2760 			    (PARENT_WAIT|WAIT_EFFECT)) {
2761 				return_value = FME_WAIT;
2762 				if (overall_delay > my_delay)
2763 					overall_delay = my_delay;
2764 				out(O_ALTFP|O_VERB, " NOT YET triggered");
2765 				continue;
2766 			}
2767 			return_value = FME_DISPROVED;
2768 			out(O_ALTFP|O_VERB, " NOT triggered");
2769 			break;
2770 		} else {
2771 			out(O_ALTFP|O_VERB, " triggered");
2772 		}
2773 	}
2774 	(void) mark_arrows(fmep, fault_event, 0, 0, NULL);
2775 
2776 	indent();
2777 	out(O_ALTFP|O_VERB|O_NONL, "<-EFFECTS %s ",
2778 	    fme_state2str(return_value));
2779 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
2780 	out(O_ALTFP|O_VERB, NULL);
2781 	indent_pop();
2782 	if (return_value == FME_WAIT)
2783 		*pdelay = overall_delay;
2784 	return (return_value);
2785 }
2786 
2787 static enum fme_state
2788 requirements_test(struct fme *fmep, struct event *ep,
2789     unsigned long long at_latest_by, unsigned long long *pdelay)
2790 {
2791 	int waiting_events;
2792 	int credible_events;
2793 	int deferred_events;
2794 	enum fme_state return_value = FME_CREDIBLE;
2795 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
2796 	unsigned long long arrow_delay;
2797 	unsigned long long my_delay;
2798 	struct event *ep2;
2799 	struct bubble *bp;
2800 	struct arrowlist *ap;
2801 
2802 	if (ep->cached_state & REQMNTS_CREDIBLE) {
2803 		indent();
2804 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY CREDIBLE ");
2805 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2806 		out(O_ALTFP|O_VERB, NULL);
2807 		return (FME_CREDIBLE);
2808 	}
2809 	if (ep->cached_state & REQMNTS_DISPROVED) {
2810 		indent();
2811 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY DISPROVED ");
2812 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2813 		out(O_ALTFP|O_VERB, NULL);
2814 		return (FME_DISPROVED);
2815 	}
2816 	if (ep->cached_state & REQMNTS_WAIT) {
2817 		indent();
2818 		*pdelay = ep->cached_delay;
2819 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY WAIT ");
2820 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2821 		out(O_ALTFP|O_VERB|O_NONL, ", wait for: ");
2822 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
2823 		out(O_ALTFP|O_VERB, NULL);
2824 		return (FME_WAIT);
2825 	}
2826 	stats_counter_bump(fmep->Rcallcount);
2827 	indent_push("  R");
2828 	indent();
2829 	out(O_ALTFP|O_VERB|O_NONL, "->");
2830 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2831 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
2832 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
2833 	out(O_ALTFP|O_VERB, NULL);
2834 
2835 	if (ep->t == N_EREPORT) {
2836 		if (ep->count == 0) {
2837 			if (fmep->pull >= at_latest_by) {
2838 				return_value = FME_DISPROVED;
2839 			} else {
2840 				ep->cached_delay = *pdelay = at_latest_by;
2841 				return_value = FME_WAIT;
2842 			}
2843 		}
2844 
2845 		indent();
2846 		switch (return_value) {
2847 		case FME_CREDIBLE:
2848 			ep->cached_state |= REQMNTS_CREDIBLE;
2849 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS CREDIBLE ");
2850 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2851 			break;
2852 		case FME_DISPROVED:
2853 			ep->cached_state |= REQMNTS_DISPROVED;
2854 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
2855 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2856 			break;
2857 		case FME_WAIT:
2858 			ep->cached_state |= REQMNTS_WAIT;
2859 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS WAIT ");
2860 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2861 			out(O_ALTFP|O_VERB|O_NONL, " to ");
2862 			ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
2863 			break;
2864 		default:
2865 			out(O_DIE, "requirements_test: unexpected fme_state");
2866 			break;
2867 		}
2868 		out(O_ALTFP|O_VERB, NULL);
2869 		indent_pop();
2870 
2871 		return (return_value);
2872 	}
2873 
2874 	/* this event is not a report, descend the tree */
2875 	for (bp = itree_next_bubble(ep, NULL); bp;
2876 	    bp = itree_next_bubble(ep, bp)) {
2877 		int n;
2878 
2879 		if (bp->t != B_FROM)
2880 			continue;
2881 
2882 		n = bp->nork;
2883 
2884 		credible_events = 0;
2885 		waiting_events = 0;
2886 		deferred_events = 0;
2887 		arrow_delay = TIMEVAL_EVENTUALLY;
2888 		/*
2889 		 * n is -1 for 'A' so adjust it.
2890 		 * XXX just count up the arrows for now.
2891 		 */
2892 		if (n < 0) {
2893 			n = 0;
2894 			for (ap = itree_next_arrow(bp, NULL); ap;
2895 			    ap = itree_next_arrow(bp, ap))
2896 				n++;
2897 			indent();
2898 			out(O_ALTFP|O_VERB, " Bubble Counted N=%d", n);
2899 		} else {
2900 			indent();
2901 			out(O_ALTFP|O_VERB, " Bubble N=%d", n);
2902 		}
2903 
2904 		if (n == 0)
2905 			continue;
2906 		if (!(bp->mark & (BUBBLE_ELIDED|BUBBLE_OK))) {
2907 			for (ap = itree_next_arrow(bp, NULL); ap;
2908 			    ap = itree_next_arrow(bp, ap)) {
2909 				ep2 = ap->arrowp->head->myevent;
2910 				platform_set_payloadnvp(ep2->nvp);
2911 				if (checkconstraints(fmep, ap->arrowp) == 0) {
2912 					/*
2913 					 * if any arrow is invalidated by the
2914 					 * constraints, then we should elide the
2915 					 * whole bubble to be consistant with
2916 					 * the tree creation time behaviour
2917 					 */
2918 					bp->mark |= BUBBLE_ELIDED;
2919 					platform_set_payloadnvp(NULL);
2920 					break;
2921 				}
2922 				platform_set_payloadnvp(NULL);
2923 			}
2924 		}
2925 		if (bp->mark & BUBBLE_ELIDED)
2926 			continue;
2927 		bp->mark |= BUBBLE_OK;
2928 		for (ap = itree_next_arrow(bp, NULL); ap;
2929 		    ap = itree_next_arrow(bp, ap)) {
2930 			ep2 = ap->arrowp->head->myevent;
2931 			if (n <= credible_events)
2932 				break;
2933 
2934 			ap->arrowp->mark |= REQMNTS_COUNTER;
2935 			if (triggered(fmep, ep2, REQMNTS_COUNTER))
2936 				/* XXX adding max timevals! */
2937 				switch (requirements_test(fmep, ep2,
2938 				    at_latest_by + ap->arrowp->maxdelay,
2939 				    &my_delay)) {
2940 				case FME_DEFERRED:
2941 					deferred_events++;
2942 					break;
2943 				case FME_CREDIBLE:
2944 					credible_events++;
2945 					break;
2946 				case FME_DISPROVED:
2947 					break;
2948 				case FME_WAIT:
2949 					if (my_delay < arrow_delay)
2950 						arrow_delay = my_delay;
2951 					waiting_events++;
2952 					break;
2953 				default:
2954 					out(O_DIE,
2955 					"Bug in requirements_test.");
2956 				}
2957 			else
2958 				deferred_events++;
2959 		}
2960 		indent();
2961 		out(O_ALTFP|O_VERB, " Credible: %d Waiting %d",
2962 		    credible_events + deferred_events, waiting_events);
2963 		if (credible_events + deferred_events + waiting_events < n) {
2964 			/* Can never meet requirements */
2965 			ep->cached_state |= REQMNTS_DISPROVED;
2966 			indent();
2967 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
2968 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2969 			out(O_ALTFP|O_VERB, NULL);
2970 			indent_pop();
2971 			return (FME_DISPROVED);
2972 		}
2973 		if (credible_events + deferred_events < n) {
2974 			/* will have to wait */
2975 			/* wait time is shortest known */
2976 			if (arrow_delay < overall_delay)
2977 				overall_delay = arrow_delay;
2978 			return_value = FME_WAIT;
2979 		} else if (credible_events < n) {
2980 			if (return_value != FME_WAIT)
2981 				return_value = FME_DEFERRED;
2982 		}
2983 	}
2984 
2985 	/*
2986 	 * don't mark as FME_DEFERRED. If this event isn't reached by another
2987 	 * path, then this will be considered FME_CREDIBLE. But if it is
2988 	 * reached by a different path so the K-count is met, then might
2989 	 * get overridden by FME_WAIT or FME_DISPROVED.
2990 	 */
2991 	if (return_value == FME_WAIT) {
2992 		ep->cached_state |= REQMNTS_WAIT;
2993 		ep->cached_delay = *pdelay = overall_delay;
2994 	} else if (return_value == FME_CREDIBLE) {
2995 		ep->cached_state |= REQMNTS_CREDIBLE;
2996 	}
2997 	indent();
2998 	out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS %s ",
2999 	    fme_state2str(return_value));
3000 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3001 	out(O_ALTFP|O_VERB, NULL);
3002 	indent_pop();
3003 	return (return_value);
3004 }
3005 
3006 static enum fme_state
3007 causes_test(struct fme *fmep, struct event *ep,
3008     unsigned long long at_latest_by, unsigned long long *pdelay)
3009 {
3010 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3011 	unsigned long long my_delay;
3012 	int credible_results = 0;
3013 	int waiting_results = 0;
3014 	enum fme_state fstate;
3015 	struct event *tail_event;
3016 	struct bubble *bp;
3017 	struct arrowlist *ap;
3018 	int k = 1;
3019 
3020 	stats_counter_bump(fmep->Ccallcount);
3021 	indent_push("  C");
3022 	indent();
3023 	out(O_ALTFP|O_VERB|O_NONL, "->");
3024 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3025 	out(O_ALTFP|O_VERB, NULL);
3026 
3027 	for (bp = itree_next_bubble(ep, NULL); bp;
3028 	    bp = itree_next_bubble(ep, bp)) {
3029 		if (bp->t != B_TO)
3030 			continue;
3031 		k = bp->nork;	/* remember the K value */
3032 		for (ap = itree_next_arrow(bp, NULL); ap;
3033 		    ap = itree_next_arrow(bp, ap)) {
3034 			int do_not_follow = 0;
3035 
3036 			/*
3037 			 * if we get to the same event multiple times
3038 			 * only worry about the first one.
3039 			 */
3040 			if (ap->arrowp->tail->myevent->cached_state &
3041 			    CAUSES_TESTED) {
3042 				indent();
3043 				out(O_ALTFP|O_VERB|O_NONL,
3044 				    "  causes test already run for ");
3045 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
3046 				    ap->arrowp->tail->myevent);
3047 				out(O_ALTFP|O_VERB, NULL);
3048 				continue;
3049 			}
3050 
3051 			/*
3052 			 * see if false constraint prevents us
3053 			 * from traversing this arrow
3054 			 */
3055 			platform_set_payloadnvp(ep->nvp);
3056 			if (checkconstraints(fmep, ap->arrowp) != 1)
3057 				do_not_follow = 1;
3058 			platform_set_payloadnvp(NULL);
3059 			if (do_not_follow) {
3060 				indent();
3061 				out(O_ALTFP|O_VERB|O_NONL,
3062 				    "  False arrow from ");
3063 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
3064 				    ap->arrowp->tail->myevent);
3065 				out(O_ALTFP|O_VERB, NULL);
3066 				continue;
3067 			}
3068 
3069 			ap->arrowp->tail->myevent->cached_state |=
3070 			    CAUSES_TESTED;
3071 			tail_event = ap->arrowp->tail->myevent;
3072 			fstate = hypothesise(fmep, tail_event, at_latest_by,
3073 			    &my_delay);
3074 
3075 			switch (fstate) {
3076 			case FME_WAIT:
3077 				if (my_delay < overall_delay)
3078 					overall_delay = my_delay;
3079 				waiting_results++;
3080 				break;
3081 			case FME_CREDIBLE:
3082 				credible_results++;
3083 				break;
3084 			case FME_DISPROVED:
3085 				break;
3086 			default:
3087 				out(O_DIE, "Bug in causes_test");
3088 			}
3089 		}
3090 	}
3091 	/* compare against K */
3092 	if (credible_results + waiting_results < k) {
3093 		indent();
3094 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES DISPROVED ");
3095 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3096 		out(O_ALTFP|O_VERB, NULL);
3097 		indent_pop();
3098 		return (FME_DISPROVED);
3099 	}
3100 	if (waiting_results != 0) {
3101 		*pdelay = overall_delay;
3102 		indent();
3103 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES WAIT ");
3104 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3105 		out(O_ALTFP|O_VERB|O_NONL, " to ");
3106 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3107 		out(O_ALTFP|O_VERB, NULL);
3108 		indent_pop();
3109 		return (FME_WAIT);
3110 	}
3111 	indent();
3112 	out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES CREDIBLE ");
3113 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3114 	out(O_ALTFP|O_VERB, NULL);
3115 	indent_pop();
3116 	return (FME_CREDIBLE);
3117 }
3118 
3119 static enum fme_state
3120 hypothesise(struct fme *fmep, struct event *ep,
3121 	unsigned long long at_latest_by, unsigned long long *pdelay)
3122 {
3123 	enum fme_state rtr, otr;
3124 	unsigned long long my_delay;
3125 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3126 
3127 	stats_counter_bump(fmep->Hcallcount);
3128 	indent_push("  H");
3129 	indent();
3130 	out(O_ALTFP|O_VERB|O_NONL, "->");
3131 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3132 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
3133 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3134 	out(O_ALTFP|O_VERB, NULL);
3135 
3136 	rtr = requirements_test(fmep, ep, at_latest_by, &my_delay);
3137 	if ((rtr == FME_WAIT) && (my_delay < overall_delay))
3138 		overall_delay = my_delay;
3139 	if (rtr != FME_DISPROVED) {
3140 		if (is_problem(ep->t)) {
3141 			otr = effects_test(fmep, ep, at_latest_by, &my_delay);
3142 			if (otr != FME_DISPROVED) {
3143 				if (fmep->peek == 0 && ep->is_suspect++ == 0) {
3144 					ep->suspects = fmep->suspects;
3145 					fmep->suspects = ep;
3146 					fmep->nsuspects++;
3147 					if (!is_fault(ep->t))
3148 						fmep->nonfault++;
3149 				}
3150 			}
3151 		} else
3152 			otr = causes_test(fmep, ep, at_latest_by, &my_delay);
3153 		if ((otr == FME_WAIT) && (my_delay < overall_delay))
3154 			overall_delay = my_delay;
3155 		if ((otr != FME_DISPROVED) &&
3156 		    ((rtr == FME_WAIT) || (otr == FME_WAIT)))
3157 			*pdelay = overall_delay;
3158 	}
3159 	if (rtr == FME_DISPROVED) {
3160 		indent();
3161 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3162 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3163 		out(O_ALTFP|O_VERB, " (doesn't meet requirements)");
3164 		indent_pop();
3165 		return (FME_DISPROVED);
3166 	}
3167 	if ((otr == FME_DISPROVED) && is_problem(ep->t)) {
3168 		indent();
3169 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3170 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3171 		out(O_ALTFP|O_VERB, " (doesn't explain all reports)");
3172 		indent_pop();
3173 		return (FME_DISPROVED);
3174 	}
3175 	if (otr == FME_DISPROVED) {
3176 		indent();
3177 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3178 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3179 		out(O_ALTFP|O_VERB, " (causes are not credible)");
3180 		indent_pop();
3181 		return (FME_DISPROVED);
3182 	}
3183 	if ((rtr == FME_WAIT) || (otr == FME_WAIT)) {
3184 		indent();
3185 		out(O_ALTFP|O_VERB|O_NONL, "<-WAIT ");
3186 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3187 		out(O_ALTFP|O_VERB|O_NONL, " to ");
3188 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &overall_delay);
3189 		out(O_ALTFP|O_VERB, NULL);
3190 		indent_pop();
3191 		return (FME_WAIT);
3192 	}
3193 	indent();
3194 	out(O_ALTFP|O_VERB|O_NONL, "<-CREDIBLE ");
3195 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3196 	out(O_ALTFP|O_VERB, NULL);
3197 	indent_pop();
3198 	return (FME_CREDIBLE);
3199 }
3200 
3201 /*
3202  * fme_istat_load -- reconstitute any persistent istats
3203  */
3204 void
3205 fme_istat_load(fmd_hdl_t *hdl)
3206 {
3207 	int sz;
3208 	char *sbuf;
3209 	char *ptr;
3210 
3211 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_ISTATS)) == 0) {
3212 		out(O_ALTFP, "fme_istat_load: No stats");
3213 		return;
3214 	}
3215 
3216 	sbuf = alloca(sz);
3217 
3218 	fmd_buf_read(hdl, NULL, WOBUF_ISTATS, sbuf, sz);
3219 
3220 	/*
3221 	 * pick apart the serialized stats
3222 	 *
3223 	 * format is:
3224 	 *	<class-name>, '@', <path>, '\0', <value>, '\0'
3225 	 * for example:
3226 	 *	"stat.first@stat0/path0\02\0stat.second@stat0/path1\023\0"
3227 	 *
3228 	 * since this is parsing our own serialized data, any parsing issues
3229 	 * are fatal, so we check for them all with ASSERT() below.
3230 	 */
3231 	ptr = sbuf;
3232 	while (ptr < &sbuf[sz]) {
3233 		char *sepptr;
3234 		struct node *np;
3235 		int val;
3236 
3237 		sepptr = strchr(ptr, '@');
3238 		ASSERT(sepptr != NULL);
3239 		*sepptr = '\0';
3240 
3241 		/* construct the event */
3242 		np = newnode(T_EVENT, NULL, 0);
3243 		np->u.event.ename = newnode(T_NAME, NULL, 0);
3244 		np->u.event.ename->u.name.t = N_STAT;
3245 		np->u.event.ename->u.name.s = stable(ptr);
3246 		np->u.event.ename->u.name.it = IT_ENAME;
3247 		np->u.event.ename->u.name.last = np->u.event.ename;
3248 
3249 		ptr = sepptr + 1;
3250 		ASSERT(ptr < &sbuf[sz]);
3251 		ptr += strlen(ptr);
3252 		ptr++;	/* move past the '\0' separating path from value */
3253 		ASSERT(ptr < &sbuf[sz]);
3254 		ASSERT(isdigit(*ptr));
3255 		val = atoi(ptr);
3256 		ASSERT(val > 0);
3257 		ptr += strlen(ptr);
3258 		ptr++;	/* move past the final '\0' for this entry */
3259 
3260 		np->u.event.epname = pathstring2epnamenp(sepptr + 1);
3261 		ASSERT(np->u.event.epname != NULL);
3262 
3263 		istat_bump(np, val);
3264 		tree_free(np);
3265 	}
3266 
3267 	istat_save();
3268 }
3269