xref: /titanic_41/usr/src/cmd/fm/modules/common/eversholt/fme.c (revision d89fccd8788afe1e920f842edd883fe192a1b8fe)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 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 *)(uintptr_t)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 *)(uintptr_t)gval->v);
827 			suffix = (char *)(uintptr_t)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 	return (NULL);
1403 }
1404 
1405 static uint_t
1406 avg(uint_t sum, uint_t cnt)
1407 {
1408 	unsigned long long s = sum * 10;
1409 
1410 	return ((s / cnt / 10) + (((s / cnt % 10) >= 5) ? 1 : 0));
1411 }
1412 
1413 static uint8_t
1414 percentof(uint_t part, uint_t whole)
1415 {
1416 	unsigned long long p = part * 1000;
1417 
1418 	return ((p / whole / 10) + (((p / whole % 10) >= 5) ? 1 : 0));
1419 }
1420 
1421 struct rsl {
1422 	struct event *suspect;
1423 	nvlist_t *asru;
1424 	nvlist_t *fru;
1425 	nvlist_t *rsrc;
1426 };
1427 
1428 /*
1429  *  rslfree -- free internal members of struct rsl not expected to be
1430  *	freed elsewhere.
1431  */
1432 static void
1433 rslfree(struct rsl *freeme)
1434 {
1435 	if (freeme->asru != NULL)
1436 		nvlist_free(freeme->asru);
1437 	if (freeme->fru != NULL)
1438 		nvlist_free(freeme->fru);
1439 	if (freeme->rsrc != NULL && freeme->rsrc != freeme->asru)
1440 		nvlist_free(freeme->rsrc);
1441 }
1442 
1443 /*
1444  *  rslcmp -- compare two rsl structures.  Use the following
1445  *	comparisons to establish cardinality:
1446  *
1447  *	1. Name of the suspect's class. (simple strcmp)
1448  *	2. Name of the suspect's ASRU. (trickier, since nvlist)
1449  *
1450  */
1451 static int
1452 rslcmp(const void *a, const void *b)
1453 {
1454 	struct rsl *r1 = (struct rsl *)a;
1455 	struct rsl *r2 = (struct rsl *)b;
1456 	int rv;
1457 
1458 	rv = strcmp(r1->suspect->enode->u.event.ename->u.name.s,
1459 	    r2->suspect->enode->u.event.ename->u.name.s);
1460 	if (rv != 0)
1461 		return (rv);
1462 
1463 	if (r1->asru == NULL && r2->asru == NULL)
1464 		return (0);
1465 	if (r1->asru == NULL)
1466 		return (-1);
1467 	if (r2->asru == NULL)
1468 		return (1);
1469 	return (evnv_cmpnvl(r1->asru, r2->asru, 0));
1470 }
1471 
1472 /*
1473  *  rsluniq -- given an array of rsl structures, seek out and "remove"
1474  *	any duplicates.  Dups are "remove"d by NULLing the suspect pointer
1475  *	of the array element.  Removal also means updating the number of
1476  *	problems and the number of problems which are not faults.  User
1477  *	provides the first and last element pointers.
1478  */
1479 static void
1480 rsluniq(struct rsl *first, struct rsl *last, int *nprobs, int *nnonf)
1481 {
1482 	struct rsl *cr;
1483 
1484 	if (*nprobs == 1)
1485 		return;
1486 
1487 	/*
1488 	 *  At this point, we only expect duplicate defects.
1489 	 *  Eversholt's diagnosis algorithm prevents duplicate
1490 	 *  suspects, but we rewrite defects in the platform code after
1491 	 *  the diagnosis is made, and that can introduce new
1492 	 *  duplicates.
1493 	 */
1494 	while (first <= last) {
1495 		if (first->suspect == NULL || !is_defect(first->suspect->t)) {
1496 			first++;
1497 			continue;
1498 		}
1499 		cr = first + 1;
1500 		while (cr <= last) {
1501 			if (is_defect(first->suspect->t)) {
1502 				if (rslcmp(first, cr) == 0) {
1503 					cr->suspect = NULL;
1504 					rslfree(cr);
1505 					(*nprobs)--;
1506 					(*nnonf)--;
1507 				}
1508 			}
1509 			/*
1510 			 * assume all defects are in order after our
1511 			 * sort and short circuit here with "else break" ?
1512 			 */
1513 			cr++;
1514 		}
1515 		first++;
1516 	}
1517 }
1518 
1519 /*
1520  * get_resources -- for a given suspect, determine what ASRU, FRU and
1521  *     RSRC nvlists should be advertised in the final suspect list.
1522  */
1523 void
1524 get_resources(struct event *sp, struct rsl *rsrcs, struct config *croot)
1525 {
1526 	struct node *asrudef, *frudef;
1527 	nvlist_t *asru, *fru;
1528 	nvlist_t *rsrc = NULL;
1529 	char *pathstr;
1530 
1531 	/*
1532 	 * First find any ASRU and/or FRU defined in the
1533 	 * initial fault tree.
1534 	 */
1535 	asrudef = eventprop_lookup(sp, L_ASRU);
1536 	frudef = eventprop_lookup(sp, L_FRU);
1537 
1538 	/*
1539 	 * Create FMRIs based on those definitions
1540 	 */
1541 	asru = node2fmri(asrudef);
1542 	fru = node2fmri(frudef);
1543 	pathstr = ipath2str(NULL, sp->ipp);
1544 
1545 	/*
1546 	 * Allow for platform translations of the FMRIs
1547 	 */
1548 	platform_units_translate(is_defect(sp->t), croot, &asru, &fru, &rsrc,
1549 	    pathstr);
1550 
1551 	FREE(pathstr);
1552 	rsrcs->suspect = sp;
1553 	rsrcs->asru = asru;
1554 	rsrcs->fru = fru;
1555 	rsrcs->rsrc = rsrc;
1556 }
1557 
1558 /*
1559  * trim_suspects -- prior to publishing, we may need to remove some
1560  *    suspects from the list.  If we're auto-closing upsets, we don't
1561  *    want any of those in the published list.  If the ASRUs for multiple
1562  *    defects resolve to the same ASRU (driver) we only want to publish
1563  *    that as a single suspect.
1564  */
1565 static void
1566 trim_suspects(struct fme *fmep, boolean_t no_upsets, struct rsl **begin,
1567     struct rsl **end)
1568 {
1569 	struct event *ep;
1570 	struct rsl *rp;
1571 	int rpcnt;
1572 
1573 	/*
1574 	 * First save the suspects in the psuspects, then copy back
1575 	 * only the ones we wish to retain.  This resets nsuspects to
1576 	 * zero.
1577 	 */
1578 	rpcnt = fmep->nsuspects;
1579 	save_suspects(fmep);
1580 
1581 	/*
1582 	 * allocate an array of resource pointers for the suspects.
1583 	 * We may end up using less than the full allocation, but this
1584 	 * is a very short-lived array.  publish_suspects() will free
1585 	 * this array when it's done using it.
1586 	 */
1587 	rp = *begin = MALLOC(rpcnt * sizeof (struct rsl));
1588 	bzero(rp, rpcnt * sizeof (struct rsl));
1589 
1590 	/* first pass, remove any unwanted upsets and populate our array */
1591 	for (ep = fmep->psuspects; ep; ep = ep->psuspects) {
1592 		if (no_upsets && is_upset(ep->t))
1593 			continue;
1594 		get_resources(ep, rp, fmep->cfgdata->cooked);
1595 		rp++;
1596 		fmep->nsuspects++;
1597 		if (!is_fault(ep->t))
1598 			fmep->nonfault++;
1599 	}
1600 
1601 	/* if all we had was unwanted upsets, we're done */
1602 	if (fmep->nsuspects == 0)
1603 		return;
1604 
1605 	*end = rp - 1;
1606 
1607 	/* sort the array */
1608 	qsort(*begin, fmep->nsuspects, sizeof (struct rsl), rslcmp);
1609 	rsluniq(*begin, *end, &fmep->nsuspects, &fmep->nonfault);
1610 }
1611 
1612 /*
1613  * addpayloadprop -- add a payload prop to a problem
1614  */
1615 static void
1616 addpayloadprop(const char *lhs, struct evalue *rhs, nvlist_t *fault)
1617 {
1618 	ASSERT(fault != NULL);
1619 	ASSERT(lhs != NULL);
1620 	ASSERT(rhs != NULL);
1621 
1622 	if (rhs->t == UINT64) {
1623 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=%llu", lhs, rhs->v);
1624 
1625 		if (nvlist_add_uint64(fault, lhs, rhs->v) != 0)
1626 			out(O_DIE,
1627 			    "cannot add payloadprop \"%s\" to fault", lhs);
1628 	} else {
1629 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=\"%s\"",
1630 		    lhs, (char *)(uintptr_t)rhs->v);
1631 
1632 		if (nvlist_add_string(fault, lhs, (char *)(uintptr_t)rhs->v) !=
1633 		    0)
1634 			out(O_DIE,
1635 			    "cannot add payloadprop \"%s\" to fault", lhs);
1636 	}
1637 }
1638 
1639 static char *Istatbuf;
1640 static char *Istatbufptr;
1641 static int Istatsz;
1642 
1643 /*
1644  * istataddsize -- calculate size of istat and add it to Istatsz
1645  */
1646 /*ARGSUSED2*/
1647 static void
1648 istataddsize(const struct istat_entry *lhs, struct stats *rhs, void *arg)
1649 {
1650 	int val;
1651 
1652 	ASSERT(lhs != NULL);
1653 	ASSERT(rhs != NULL);
1654 
1655 	if ((val = stats_counter_value(rhs)) == 0)
1656 		return;	/* skip zero-valued stats */
1657 
1658 	/* count up the size of the stat name */
1659 	Istatsz += ipath2strlen(lhs->ename, lhs->ipath);
1660 	Istatsz++;	/* for the trailing NULL byte */
1661 
1662 	/* count up the size of the stat value */
1663 	Istatsz += snprintf(NULL, 0, "%d", val);
1664 	Istatsz++;	/* for the trailing NULL byte */
1665 }
1666 
1667 /*
1668  * istat2str -- serialize an istat, writing result to *Istatbufptr
1669  */
1670 /*ARGSUSED2*/
1671 static void
1672 istat2str(const struct istat_entry *lhs, struct stats *rhs, void *arg)
1673 {
1674 	char *str;
1675 	int len;
1676 	int val;
1677 
1678 	ASSERT(lhs != NULL);
1679 	ASSERT(rhs != NULL);
1680 
1681 	if ((val = stats_counter_value(rhs)) == 0)
1682 		return;	/* skip zero-valued stats */
1683 
1684 	/* serialize the stat name */
1685 	str = ipath2str(lhs->ename, lhs->ipath);
1686 	len = strlen(str);
1687 
1688 	ASSERT(Istatbufptr + len + 1 < &Istatbuf[Istatsz]);
1689 	(void) strlcpy(Istatbufptr, str, &Istatbuf[Istatsz] - Istatbufptr);
1690 	Istatbufptr += len;
1691 	FREE(str);
1692 	*Istatbufptr++ = '\0';
1693 
1694 	/* serialize the stat value */
1695 	Istatbufptr += snprintf(Istatbufptr, &Istatbuf[Istatsz] - Istatbufptr,
1696 	    "%d", val);
1697 	*Istatbufptr++ = '\0';
1698 
1699 	ASSERT(Istatbufptr <= &Istatbuf[Istatsz]);
1700 }
1701 
1702 void
1703 istat_save()
1704 {
1705 	if (Istat_need_save == 0)
1706 		return;
1707 
1708 	/* figure out how big the serialzed info is */
1709 	Istatsz = 0;
1710 	lut_walk(Istats, (lut_cb)istataddsize, NULL);
1711 
1712 	if (Istatsz == 0) {
1713 		/* no stats to save */
1714 		fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
1715 		return;
1716 	}
1717 
1718 	/* create the serialized buffer */
1719 	Istatbufptr = Istatbuf = MALLOC(Istatsz);
1720 	lut_walk(Istats, (lut_cb)istat2str, NULL);
1721 
1722 	/* clear out current saved stats */
1723 	fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
1724 
1725 	/* write out the new version */
1726 	fmd_buf_write(Hdl, NULL, WOBUF_ISTATS, Istatbuf, Istatsz);
1727 	FREE(Istatbuf);
1728 
1729 	Istat_need_save = 0;
1730 }
1731 
1732 int
1733 istat_cmp(struct istat_entry *ent1, struct istat_entry *ent2)
1734 {
1735 	if (ent1->ename != ent2->ename)
1736 		return (ent2->ename - ent1->ename);
1737 	if (ent1->ipath != ent2->ipath)
1738 		return ((char *)ent2->ipath - (char *)ent1->ipath);
1739 
1740 	return (0);
1741 }
1742 
1743 /*
1744  * istat-verify -- verify the component associated with a stat still exists
1745  *
1746  * if the component no longer exists, this routine resets the stat and
1747  * returns 0.  if the component still exists, it returns 1.
1748  */
1749 static int
1750 istat_verify(struct node *snp, struct istat_entry *entp)
1751 {
1752 	struct stats *statp;
1753 	nvlist_t *fmri;
1754 
1755 	fmri = node2fmri(snp->u.event.epname);
1756 	if (platform_path_exists(fmri)) {
1757 		nvlist_free(fmri);
1758 		return (1);
1759 	}
1760 	nvlist_free(fmri);
1761 
1762 	/* component no longer in system.  zero out the associated stats */
1763 	if ((statp = (struct stats *)
1764 	    lut_lookup(Istats, entp, (lut_cmp)istat_cmp)) == NULL ||
1765 	    stats_counter_value(statp) == 0)
1766 		return (0);	/* stat is already reset */
1767 
1768 	Istat_need_save = 1;
1769 	stats_counter_reset(statp);
1770 	return (0);
1771 }
1772 
1773 static void
1774 istat_bump(struct node *snp, int n)
1775 {
1776 	struct stats *statp;
1777 	struct istat_entry ent;
1778 
1779 	ASSERT(snp != NULL);
1780 	ASSERTinfo(snp->t == T_EVENT, ptree_nodetype2str(snp->t));
1781 	ASSERT(snp->u.event.epname != NULL);
1782 
1783 	/* class name should be hoisted into a single stable entry */
1784 	ASSERT(snp->u.event.ename->u.name.next == NULL);
1785 	ent.ename = snp->u.event.ename->u.name.s;
1786 	ent.ipath = ipath(snp->u.event.epname);
1787 
1788 	if (!istat_verify(snp, &ent)) {
1789 		/* component no longer exists in system, nothing to do */
1790 		return;
1791 	}
1792 
1793 	if ((statp = (struct stats *)
1794 	    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL) {
1795 		/* need to create the counter */
1796 		int cnt = 0;
1797 		struct node *np;
1798 		char *sname;
1799 		char *snamep;
1800 		struct istat_entry *newentp;
1801 
1802 		/* count up the size of the stat name */
1803 		np = snp->u.event.ename;
1804 		while (np != NULL) {
1805 			cnt += strlen(np->u.name.s);
1806 			cnt++;	/* for the '.' or '@' */
1807 			np = np->u.name.next;
1808 		}
1809 		np = snp->u.event.epname;
1810 		while (np != NULL) {
1811 			cnt += snprintf(NULL, 0, "%s%llu",
1812 			    np->u.name.s, np->u.name.child->u.ull);
1813 			cnt++;	/* for the '/' or trailing NULL byte */
1814 			np = np->u.name.next;
1815 		}
1816 
1817 		/* build the stat name */
1818 		snamep = sname = alloca(cnt);
1819 		np = snp->u.event.ename;
1820 		while (np != NULL) {
1821 			snamep += snprintf(snamep, &sname[cnt] - snamep,
1822 			    "%s", np->u.name.s);
1823 			np = np->u.name.next;
1824 			if (np)
1825 				*snamep++ = '.';
1826 		}
1827 		*snamep++ = '@';
1828 		np = snp->u.event.epname;
1829 		while (np != NULL) {
1830 			snamep += snprintf(snamep, &sname[cnt] - snamep,
1831 			    "%s%llu", np->u.name.s, np->u.name.child->u.ull);
1832 			np = np->u.name.next;
1833 			if (np)
1834 				*snamep++ = '/';
1835 		}
1836 		*snamep++ = '\0';
1837 
1838 		/* create the new stat & add it to our list */
1839 		newentp = MALLOC(sizeof (*newentp));
1840 		*newentp = ent;
1841 		statp = stats_new_counter(NULL, sname, 0);
1842 		Istats = lut_add(Istats, (void *)newentp, (void *)statp,
1843 		    (lut_cmp)istat_cmp);
1844 	}
1845 
1846 	/* if n is non-zero, set that value instead of bumping */
1847 	if (n) {
1848 		stats_counter_reset(statp);
1849 		stats_counter_add(statp, n);
1850 	} else
1851 		stats_counter_bump(statp);
1852 	Istat_need_save = 1;
1853 }
1854 
1855 /*ARGSUSED*/
1856 static void
1857 istat_destructor(void *left, void *right, void *arg)
1858 {
1859 	struct istat_entry *entp = (struct istat_entry *)left;
1860 	struct stats *statp = (struct stats *)right;
1861 	FREE(entp);
1862 	stats_delete(statp);
1863 }
1864 
1865 void
1866 istat_fini(void)
1867 {
1868 	lut_free(Istats, istat_destructor, NULL);
1869 }
1870 
1871 static void
1872 publish_suspects(struct fme *fmep)
1873 {
1874 	struct event *ep;
1875 	struct rsl *srl = NULL;
1876 	struct rsl *erl;
1877 	struct rsl *rp;
1878 	nvlist_t *fault;
1879 	uint8_t cert;
1880 	uint_t *frs;
1881 	uint_t fravg, frsum, fr;
1882 	uint_t messval;
1883 	struct node *snp;
1884 	int frcnt, fridx;
1885 	boolean_t no_upsets = B_FALSE;
1886 	boolean_t allfaulty = B_TRUE;
1887 
1888 	stats_counter_bump(fmep->diags);
1889 
1890 	/*
1891 	 * The current fmd interfaces don't allow us to solve a case
1892 	 * that's already solved.  If we make a new case, what of the
1893 	 * ereports?  We don't appear to have an interface that allows
1894 	 * us to access the ereports attached to a case (if we wanted
1895 	 * to copy the original case's ereport attachments to the new
1896 	 * case) and it's also a bit unclear if there would be any
1897 	 * problems with having ereports attached to multiple cases
1898 	 * and/or attaching DIAGNOSED ereports to a case.  For now,
1899 	 * we'll just output a message.
1900 	 */
1901 	if (fmep->posted_suspects ||
1902 	    fmd_case_solved(fmep->hdl, fmep->fmcase)) {
1903 		out(O_ALTFP|O_NONL, "Revised diagnosis for case %s: ",
1904 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
1905 		for (ep = fmep->suspects; ep; ep = ep->suspects) {
1906 			out(O_ALTFP|O_NONL, " ");
1907 			itree_pevent_brief(O_ALTFP|O_NONL, ep);
1908 		}
1909 		out(O_ALTFP, NULL);
1910 		return;
1911 	}
1912 
1913 	/*
1914 	 * If we're auto-closing upsets, we don't want to include them
1915 	 * in any produced suspect lists or certainty accounting.
1916 	 */
1917 	if (Autoclose != NULL)
1918 		if (strcmp(Autoclose, "true") == 0 ||
1919 		    strcmp(Autoclose, "all") == 0 ||
1920 		    strcmp(Autoclose, "upsets") == 0)
1921 			no_upsets = B_TRUE;
1922 
1923 	trim_suspects(fmep, no_upsets, &srl, &erl);
1924 
1925 	/*
1926 	 * If the resulting suspect list has no members, we're
1927 	 * done.  Returning here will simply close the case.
1928 	 */
1929 	if (fmep->nsuspects == 0) {
1930 		out(O_ALTFP,
1931 		    "[FME%d, case %s (all suspects are upsets)]",
1932 		    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
1933 		FREE(srl);
1934 		restore_suspects(fmep);
1935 		return;
1936 	}
1937 
1938 	/*
1939 	 * If the suspect list is all faults, then for a given fault,
1940 	 * say X of N, X's certainty is computed via:
1941 	 *
1942 	 * fitrate(X) / (fitrate(1) + ... + fitrate(N)) * 100
1943 	 *
1944 	 * If none of the suspects are faults, and there are N suspects,
1945 	 * the certainty of a given suspect is 100/N.
1946 	 *
1947 	 * If there are are a mixture of faults and other problems in
1948 	 * the suspect list, we take an average of the faults'
1949 	 * FITrates and treat this average as the FITrate for any
1950 	 * non-faults.  The fitrate of any given suspect is then
1951 	 * computed per the first formula above.
1952 	 */
1953 	if (fmep->nonfault == fmep->nsuspects) {
1954 		/* NO faults in the suspect list */
1955 		cert = percentof(1, fmep->nsuspects);
1956 	} else {
1957 		/* sum the fitrates */
1958 		frs = alloca(fmep->nsuspects * sizeof (uint_t));
1959 		fridx = frcnt = frsum = 0;
1960 
1961 		for (rp = srl; rp <= erl; rp++) {
1962 			struct node *n;
1963 
1964 			if (rp->suspect == NULL)
1965 				continue;
1966 			if (!is_fault(rp->suspect->t)) {
1967 				frs[fridx++] = 0;
1968 				continue;
1969 			}
1970 			n = eventprop_lookup(rp->suspect, L_FITrate);
1971 			if (node2uint(n, &fr) != 0) {
1972 				out(O_DEBUG|O_NONL, "event ");
1973 				ipath_print(O_DEBUG|O_NONL,
1974 				    ep->enode->u.event.ename->u.name.s,
1975 				    ep->ipp);
1976 				out(O_DEBUG, " has no FITrate (using 1)");
1977 				fr = 1;
1978 			} else if (fr == 0) {
1979 				out(O_DEBUG|O_NONL, "event ");
1980 				ipath_print(O_DEBUG|O_NONL,
1981 				    ep->enode->u.event.ename->u.name.s,
1982 				    ep->ipp);
1983 				out(O_DEBUG, " has zero FITrate (using 1)");
1984 				fr = 1;
1985 			}
1986 
1987 			frs[fridx++] = fr;
1988 			frsum += fr;
1989 			frcnt++;
1990 		}
1991 		fravg = avg(frsum, frcnt);
1992 		for (fridx = 0; fridx < fmep->nsuspects; fridx++)
1993 			if (frs[fridx] == 0) {
1994 				frs[fridx] = fravg;
1995 				frsum += fravg;
1996 			}
1997 	}
1998 
1999 	/* Add them in reverse order of our sort, as fmd reverses order */
2000 	for (rp = erl; rp >= srl; rp--) {
2001 		if (rp->suspect == NULL)
2002 			continue;
2003 		if (!is_fault(rp->suspect->t))
2004 			allfaulty = B_FALSE;
2005 		if (fmep->nonfault != fmep->nsuspects)
2006 			cert = percentof(frs[--fridx], frsum);
2007 		fault = fmd_nvl_create_fault(fmep->hdl,
2008 		    rp->suspect->enode->u.event.ename->u.name.s,
2009 		    cert,
2010 		    rp->asru,
2011 		    rp->fru,
2012 		    rp->rsrc);
2013 		if (fault == NULL)
2014 			out(O_DIE, "fault creation failed");
2015 		/* if "message" property exists, add it to the fault */
2016 		if (node2uint(eventprop_lookup(rp->suspect, L_message),
2017 		    &messval) == 0) {
2018 
2019 			out(O_ALTFP,
2020 			    "[FME%d, %s adds message=%d to suspect list]",
2021 			    fmep->id,
2022 			    rp->suspect->enode->u.event.ename->u.name.s,
2023 			    messval);
2024 			if (nvlist_add_boolean_value(fault,
2025 			    FM_SUSPECT_MESSAGE,
2026 			    (messval) ? B_TRUE : B_FALSE) != 0) {
2027 				out(O_DIE, "cannot add no-message to fault");
2028 			}
2029 		}
2030 		/* add any payload properties */
2031 		lut_walk(rp->suspect->payloadprops,
2032 		    (lut_cb)addpayloadprop, (void *)fault);
2033 		fmd_case_add_suspect(fmep->hdl, fmep->fmcase, fault);
2034 		rp->suspect->fault = fault;
2035 		rslfree(rp);
2036 
2037 		/*
2038 		 * If "action" property exists, evaluate it;  this must be done
2039 		 * before the dupclose check below since some actions may
2040 		 * modify the asru to be used in fmd_nvl_fmri_faulty.  This
2041 		 * needs to be restructured if any new actions are introduced
2042 		 * that have effects that we do not want to be visible if
2043 		 * we decide not to publish in the dupclose check below.
2044 		 */
2045 		if ((snp = eventprop_lookup(rp->suspect, L_action)) != NULL) {
2046 			struct evalue evalue;
2047 
2048 			out(O_ALTFP|O_NONL,
2049 			    "[FME%d, %s action ", fmep->id,
2050 			    rp->suspect->enode->u.event.ename->u.name.s);
2051 			ptree_name_iter(O_ALTFP|O_NONL, snp);
2052 			out(O_ALTFP, "]");
2053 			Action_nvl = fault;
2054 			(void) eval_expr(snp, NULL, NULL, NULL, NULL,
2055 			    NULL, 0, &evalue);
2056 		}
2057 
2058 		/*
2059 		 * if "dupclose" tunable is set, check if the asru is
2060 		 * already marked as "faulty".
2061 		 */
2062 		if (Dupclose && allfaulty) {
2063 			nvlist_t *asru;
2064 
2065 			out(O_ALTFP|O_VERB, "FMD%d dupclose check ", fmep->id);
2066 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, rp->suspect);
2067 			out(O_ALTFP|O_VERB|O_NONL, " ");
2068 			if (nvlist_lookup_nvlist(fault,
2069 			    FM_FAULT_ASRU, &asru) != 0) {
2070 				out(O_ALTFP|O_VERB, "NULL asru");
2071 				allfaulty = B_FALSE;
2072 			} else if (fmd_nvl_fmri_faulty(fmep->hdl, asru)) {
2073 				out(O_ALTFP|O_VERB, "faulty");
2074 			} else {
2075 				out(O_ALTFP|O_VERB, "not faulty");
2076 				allfaulty = B_FALSE;
2077 			}
2078 		}
2079 
2080 	}
2081 
2082 	/*
2083 	 * Close the case if all asrus are already known to be faulty and if
2084 	 * Dupclose is enabled.  Otherwise we are going to publish so take
2085 	 * any pre-publication actions.
2086 	 */
2087 	if (Dupclose && allfaulty) {
2088 		out(O_ALTFP, "[dupclose FME%d, case %s]", fmep->id,
2089 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
2090 		fmd_case_close(fmep->hdl, fmep->fmcase);
2091 	} else {
2092 		for (rp = erl; rp >= srl; rp--) {
2093 			struct event *suspect = rp->suspect;
2094 
2095 			if (suspect == NULL)
2096 				continue;
2097 
2098 			fault = suspect->fault;
2099 
2100 			/* if "count" exists, increment the appropriate stat */
2101 			if ((snp = eventprop_lookup(suspect,
2102 			    L_count)) != NULL) {
2103 				out(O_ALTFP|O_NONL,
2104 				    "[FME%d, %s count ", fmep->id,
2105 				    suspect->enode->u.event.ename->u.name.s);
2106 				ptree_name_iter(O_ALTFP|O_NONL, snp);
2107 				out(O_ALTFP, "]");
2108 				istat_bump(snp, 0);
2109 
2110 			}
2111 		}
2112 		istat_save();	/* write out any istat changes */
2113 
2114 		out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
2115 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
2116 		fmd_case_solve(fmep->hdl, fmep->fmcase);
2117 	}
2118 
2119 	/*
2120 	 * revert to the original suspect list
2121 	 */
2122 	FREE(srl);
2123 	restore_suspects(fmep);
2124 }
2125 
2126 static void
2127 publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep)
2128 {
2129 	struct case_list *newcase;
2130 	nvlist_t *defect;
2131 
2132 	out(O_ALTFP,
2133 	    "[undiagnosable ereport received, "
2134 	    "creating and closing a new case (%s)]",
2135 	    Undiag_reason ? Undiag_reason : "reason not provided");
2136 
2137 	newcase = MALLOC(sizeof (struct case_list));
2138 	newcase->next = NULL;
2139 
2140 	newcase->fmcase = fmd_case_open(hdl, NULL);
2141 	if (Undiagablecaselist != NULL)
2142 		newcase->next = Undiagablecaselist;
2143 	Undiagablecaselist = newcase;
2144 
2145 	if (ffep != NULL)
2146 		fmd_case_add_ereport(hdl, newcase->fmcase, ffep);
2147 
2148 	defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
2149 	    NULL, NULL, NULL);
2150 	if (Undiag_reason != NULL)
2151 		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
2152 	fmd_case_add_suspect(hdl, newcase->fmcase, defect);
2153 
2154 	fmd_case_solve(hdl, newcase->fmcase);
2155 	fmd_case_close(hdl, newcase->fmcase);
2156 }
2157 
2158 static void
2159 fme_undiagnosable(struct fme *f)
2160 {
2161 	nvlist_t *defect;
2162 
2163 	out(O_ALTFP, "[solving/closing FME%d, case %s (%s)]",
2164 	    f->id, fmd_case_uuid(f->hdl, f->fmcase),
2165 	    Undiag_reason ? Undiag_reason : "undiagnosable");
2166 
2167 	defect = fmd_nvl_create_fault(f->hdl, UNDIAGNOSABLE_DEFECT, 100,
2168 	    NULL, NULL, NULL);
2169 	if (Undiag_reason != NULL)
2170 		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
2171 	fmd_case_add_suspect(f->hdl, f->fmcase, defect);
2172 	fmd_case_solve(f->hdl, f->fmcase);
2173 	destroy_fme_bufs(f);
2174 	fmd_case_close(f->hdl, f->fmcase);
2175 }
2176 
2177 /*
2178  * fme_close_case
2179  *
2180  *	Find the requested case amongst our fmes and close it.  Free up
2181  *	the related fme.
2182  */
2183 void
2184 fme_close_case(fmd_hdl_t *hdl, fmd_case_t *fmcase)
2185 {
2186 	struct case_list *ucasep, *prevcasep = NULL;
2187 	struct fme *prev = NULL;
2188 	struct fme *fmep;
2189 
2190 	for (ucasep = Undiagablecaselist; ucasep; ucasep = ucasep->next) {
2191 		if (fmcase != ucasep->fmcase) {
2192 			prevcasep = ucasep;
2193 			continue;
2194 		}
2195 
2196 		if (prevcasep == NULL)
2197 			Undiagablecaselist = Undiagablecaselist->next;
2198 		else
2199 			prevcasep->next = ucasep->next;
2200 
2201 		FREE(ucasep);
2202 		return;
2203 	}
2204 
2205 	for (fmep = FMElist; fmep; fmep = fmep->next) {
2206 		if (fmep->hdl == hdl && fmep->fmcase == fmcase)
2207 			break;
2208 		prev = fmep;
2209 	}
2210 
2211 	if (fmep == NULL) {
2212 		out(O_WARN, "Eft asked to close unrecognized case [%s].",
2213 		    fmd_case_uuid(hdl, fmcase));
2214 		return;
2215 	}
2216 
2217 	if (EFMElist == fmep)
2218 		EFMElist = prev;
2219 
2220 	if (prev == NULL)
2221 		FMElist = FMElist->next;
2222 	else
2223 		prev->next = fmep->next;
2224 
2225 	fmep->next = NULL;
2226 
2227 	/* Get rid of any timer this fme has set */
2228 	if (fmep->wull != 0)
2229 		fmd_timer_remove(fmep->hdl, fmep->timer);
2230 
2231 	if (ClosedFMEs == NULL) {
2232 		ClosedFMEs = fmep;
2233 	} else {
2234 		fmep->next = ClosedFMEs;
2235 		ClosedFMEs = fmep;
2236 	}
2237 
2238 	Open_fme_count--;
2239 
2240 	/* See if we can close the overflow FME */
2241 	if (Open_fme_count <= Max_fme) {
2242 		for (fmep = FMElist; fmep; fmep = fmep->next) {
2243 			if (fmep->overflow && !(fmd_case_closed(fmep->hdl,
2244 			    fmep->fmcase)))
2245 				break;
2246 		}
2247 
2248 		if (fmep != NULL)
2249 			fmd_case_close(fmep->hdl, fmep->fmcase);
2250 	}
2251 }
2252 
2253 /*
2254  * fme_set_timer()
2255  *	If the time we need to wait for the given FME is less than the
2256  *	current timer, kick that old timer out and establish a new one.
2257  */
2258 static int
2259 fme_set_timer(struct fme *fmep, unsigned long long wull)
2260 {
2261 	out(O_ALTFP|O_VERB|O_NONL, " fme_set_timer: request to wait ");
2262 	ptree_timeval(O_ALTFP|O_VERB, &wull);
2263 
2264 	if (wull <= fmep->pull) {
2265 		out(O_ALTFP|O_VERB|O_NONL, "already have waited at least ");
2266 		ptree_timeval(O_ALTFP|O_VERB, &fmep->pull);
2267 		out(O_ALTFP|O_VERB, NULL);
2268 		/* we've waited at least wull already, don't need timer */
2269 		return (0);
2270 	}
2271 
2272 	out(O_ALTFP|O_VERB|O_NONL, " currently ");
2273 	if (fmep->wull != 0) {
2274 		out(O_ALTFP|O_VERB|O_NONL, "waiting ");
2275 		ptree_timeval(O_ALTFP|O_VERB, &fmep->wull);
2276 		out(O_ALTFP|O_VERB, NULL);
2277 	} else {
2278 		out(O_ALTFP|O_VERB|O_NONL, "not waiting");
2279 		out(O_ALTFP|O_VERB, NULL);
2280 	}
2281 
2282 	if (fmep->wull != 0)
2283 		if (wull >= fmep->wull)
2284 			/* New timer would fire later than established timer */
2285 			return (0);
2286 
2287 	if (fmep->wull != 0) {
2288 		fmd_timer_remove(fmep->hdl, fmep->timer);
2289 		if (fmep->timer == fmep->htid) {
2290 			out(O_ALTFP,
2291 			    "[stopped hesitating FME%d, case %s]",
2292 			    fmep->id,
2293 			    fmd_case_uuid(fmep->hdl,
2294 			    fmep->fmcase));
2295 			fmep->htid = 0;
2296 		}
2297 	}
2298 
2299 	fmep->timer = fmd_timer_install(fmep->hdl, (void *)fmep,
2300 	    fmep->e0r, wull);
2301 	out(O_ALTFP|O_VERB, "timer set, id is %ld", fmep->timer);
2302 	fmep->wull = wull;
2303 	return (1);
2304 }
2305 
2306 void
2307 fme_timer_fired(struct fme *fmep, id_t tid)
2308 {
2309 	struct fme *ffmep = NULL;
2310 
2311 	for (ffmep = FMElist; ffmep; ffmep = ffmep->next)
2312 		if (ffmep == fmep)
2313 			break;
2314 
2315 	if (ffmep == NULL) {
2316 		out(O_WARN, "Timer fired for an FME (%p) not in FMEs list.",
2317 		    (void *)fmep);
2318 		return;
2319 	}
2320 
2321 	out(O_ALTFP, "Timer fired %lx %lx", tid, fmep->htid);
2322 	if (tid != fmep->htid) {
2323 		/*
2324 		 * normal timer (not the hesitation timer)
2325 		 */
2326 		fmep->pull = fmep->wull;
2327 		fmep->wull = 0;
2328 		fmd_buf_write(fmep->hdl, fmep->fmcase,
2329 		    WOBUF_PULL, (void *)&fmep->pull, sizeof (fmep->pull));
2330 		/*
2331 		 * no point in heistating if we've already waited.
2332 		 */
2333 		fmep->hesitated = 1;
2334 	} else {
2335 		fmep->hesitated = 1;
2336 	}
2337 	fme_eval(fmep, NULL);
2338 }
2339 
2340 /*
2341  * Preserve the fme's suspect list in its psuspects list, NULLing the
2342  * suspects list in the meantime.
2343  */
2344 static void
2345 save_suspects(struct fme *fmep)
2346 {
2347 	struct event *ep;
2348 	struct event *nextep;
2349 
2350 	/* zero out the previous suspect list */
2351 	for (ep = fmep->psuspects; ep; ep = nextep) {
2352 		nextep = ep->psuspects;
2353 		ep->psuspects = NULL;
2354 	}
2355 	fmep->psuspects = NULL;
2356 
2357 	/* zero out the suspect list, copying it to previous suspect list */
2358 	fmep->psuspects = fmep->suspects;
2359 	for (ep = fmep->suspects; ep; ep = nextep) {
2360 		nextep = ep->suspects;
2361 		ep->psuspects = ep->suspects;
2362 		ep->suspects = NULL;
2363 		ep->is_suspect = 0;
2364 	}
2365 	fmep->suspects = NULL;
2366 	fmep->nsuspects = 0;
2367 	fmep->nonfault = 0;
2368 }
2369 
2370 /*
2371  * Retrieve the fme's suspect list from its psuspects list.
2372  */
2373 static void
2374 restore_suspects(struct fme *fmep)
2375 {
2376 	struct event *ep;
2377 	struct event *nextep;
2378 
2379 	fmep->nsuspects = fmep->nonfault = 0;
2380 	fmep->suspects = fmep->psuspects;
2381 	for (ep = fmep->psuspects; ep; ep = nextep) {
2382 		fmep->nsuspects++;
2383 		if (!is_fault(ep->t))
2384 			fmep->nonfault++;
2385 		nextep = ep->psuspects;
2386 		ep->suspects = ep->psuspects;
2387 	}
2388 }
2389 
2390 /*
2391  * this is what we use to call the Emrys prototype code instead of main()
2392  */
2393 static void
2394 fme_eval(struct fme *fmep, fmd_event_t *ffep)
2395 {
2396 	struct event *ep;
2397 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
2398 
2399 	save_suspects(fmep);
2400 
2401 	out(O_ALTFP|O_VERB, "Evaluate FME %d", fmep->id);
2402 	indent_set("  ");
2403 
2404 	lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
2405 	fmep->state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
2406 
2407 	out(O_ALTFP|O_VERB|O_NONL, "FME%d state: %s, suspect list:", fmep->id,
2408 	    fme_state2str(fmep->state));
2409 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
2410 		out(O_ALTFP|O_VERB|O_NONL, " ");
2411 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2412 	}
2413 	out(O_ALTFP|O_VERB, NULL);
2414 
2415 	if (fmep->posted_suspects) {
2416 		/*
2417 		 * this FME has already posted a diagnosis, so see if
2418 		 * the event changed the diagnosis and print a warning
2419 		 * if it did.
2420 		 *
2421 		 */
2422 		if (suspects_changed(fmep)) {
2423 			print_suspects(SLCHANGED, fmep);
2424 			publish_suspects(fmep);
2425 		}
2426 	} else {
2427 		switch (fmep->state) {
2428 		case FME_CREDIBLE:
2429 			/*
2430 			 * if the suspect list contains any upsets, we
2431 			 * turn off the hesitation logic (by setting
2432 			 * the hesitate flag which normally indicates
2433 			 * we've already done the hesitate logic).
2434 			 * this is done because hesitating with upsets
2435 			 * causes us to explain away additional soft errors
2436 			 * while the upset FME stays open.
2437 			 */
2438 			if (fmep->hesitated == 0) {
2439 				struct event *s;
2440 
2441 				for (s = fmep->suspects; s; s = s->suspects) {
2442 					if (s->t == N_UPSET) {
2443 						fmep->hesitated = 1;
2444 						break;
2445 					}
2446 				}
2447 			}
2448 
2449 			if (Hesitate &&
2450 			    fmep->suspects != NULL &&
2451 			    fmep->suspects->suspects != NULL &&
2452 			    fmep->hesitated == 0) {
2453 				/*
2454 				 * about to publish multi-entry suspect list,
2455 				 * set the hesitation timer if not already set.
2456 				 */
2457 				if (fmep->htid == 0) {
2458 					out(O_ALTFP|O_NONL,
2459 					    "[hesitate FME%d, case %s ",
2460 					    fmep->id,
2461 					    fmd_case_uuid(fmep->hdl,
2462 					    fmep->fmcase));
2463 					ptree_timeval(O_ALTFP|O_NONL,
2464 					    (unsigned long long *)&Hesitate);
2465 					out(O_ALTFP, "]");
2466 					if (fme_set_timer(fmep, Hesitate))
2467 						fmep->htid = fmep->timer;
2468 				} else {
2469 					out(O_ALTFP,
2470 					    "[still hesitating FME%d, case %s]",
2471 					    fmep->id,
2472 					    fmd_case_uuid(fmep->hdl,
2473 					    fmep->fmcase));
2474 				}
2475 			} else {
2476 				print_suspects(SLNEW, fmep);
2477 				(void) upsets_eval(fmep, ffep);
2478 				publish_suspects(fmep);
2479 				fmep->posted_suspects = 1;
2480 				fmd_buf_write(fmep->hdl, fmep->fmcase,
2481 				    WOBUF_POSTD,
2482 				    (void *)&fmep->posted_suspects,
2483 				    sizeof (fmep->posted_suspects));
2484 			}
2485 			break;
2486 
2487 		case FME_WAIT:
2488 			/*
2489 			 * singleton suspect list implies
2490 			 * no point in waiting
2491 			 */
2492 			if (fmep->suspects &&
2493 			    fmep->suspects->suspects == NULL) {
2494 				print_suspects(SLNEW, fmep);
2495 				(void) upsets_eval(fmep, ffep);
2496 				publish_suspects(fmep);
2497 				fmep->posted_suspects = 1;
2498 				fmd_buf_write(fmep->hdl, fmep->fmcase,
2499 				    WOBUF_POSTD,
2500 				    (void *)&fmep->posted_suspects,
2501 				    sizeof (fmep->posted_suspects));
2502 				fmep->state = FME_CREDIBLE;
2503 			} else {
2504 				ASSERT(my_delay > fmep->ull);
2505 				(void) fme_set_timer(fmep, my_delay);
2506 				print_suspects(SLWAIT, fmep);
2507 			}
2508 			break;
2509 
2510 		case FME_DISPROVED:
2511 			print_suspects(SLDISPROVED, fmep);
2512 			Undiag_reason = UD_UNSOLVD;
2513 			fme_undiagnosable(fmep);
2514 			break;
2515 		}
2516 	}
2517 
2518 	if (fmep->posted_suspects == 1 && Autoclose != NULL) {
2519 		int doclose = 0;
2520 
2521 		if (strcmp(Autoclose, "true") == 0 ||
2522 		    strcmp(Autoclose, "all") == 0)
2523 			doclose = 1;
2524 
2525 		if (strcmp(Autoclose, "upsets") == 0) {
2526 			doclose = 1;
2527 			for (ep = fmep->suspects; ep; ep = ep->suspects) {
2528 				if (ep->t != N_UPSET) {
2529 					doclose = 0;
2530 					break;
2531 				}
2532 			}
2533 		}
2534 
2535 		if (doclose) {
2536 			out(O_ALTFP, "[closing FME%d, case %s (autoclose)]",
2537 			    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
2538 
2539 			destroy_fme_bufs(fmep);
2540 			fmd_case_close(fmep->hdl, fmep->fmcase);
2541 		}
2542 	}
2543 }
2544 
2545 static void indent(void);
2546 static int triggered(struct fme *fmep, struct event *ep, int mark);
2547 static enum fme_state effects_test(struct fme *fmep,
2548     struct event *fault_event, unsigned long long at_latest_by,
2549     unsigned long long *pdelay);
2550 static enum fme_state requirements_test(struct fme *fmep, struct event *ep,
2551     unsigned long long at_latest_by, unsigned long long *pdelay);
2552 static enum fme_state causes_test(struct fme *fmep, struct event *ep,
2553     unsigned long long at_latest_by, unsigned long long *pdelay);
2554 
2555 static int
2556 checkconstraints(struct fme *fmep, struct arrow *arrowp)
2557 {
2558 	struct constraintlist *ctp;
2559 	struct evalue value;
2560 
2561 	if (arrowp->forever_false) {
2562 		char *sep = "";
2563 		indent();
2564 		out(O_ALTFP|O_VERB|O_NONL, "  Forever false constraint: ");
2565 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
2566 			out(O_ALTFP|O_VERB|O_NONL, sep);
2567 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
2568 			sep = ", ";
2569 		}
2570 		out(O_ALTFP|O_VERB, NULL);
2571 		return (0);
2572 	}
2573 
2574 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
2575 		if (eval_expr(ctp->cnode, NULL, NULL,
2576 		    &fmep->globals, fmep->cfgdata->cooked,
2577 		    arrowp, 0, &value)) {
2578 			/* evaluation successful */
2579 			if (value.t == UNDEFINED || value.v == 0) {
2580 				/* known false */
2581 				arrowp->forever_false = 1;
2582 				indent();
2583 				out(O_ALTFP|O_VERB|O_NONL,
2584 				    "  False constraint: ");
2585 				ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
2586 				out(O_ALTFP|O_VERB, NULL);
2587 				return (0);
2588 			}
2589 		} else {
2590 			/* evaluation unsuccessful -- unknown value */
2591 			indent();
2592 			out(O_ALTFP|O_VERB|O_NONL,
2593 			    "  Deferred constraint: ");
2594 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
2595 			out(O_ALTFP|O_VERB, NULL);
2596 			return (2);
2597 		}
2598 	}
2599 	/* known true */
2600 	return (1);
2601 }
2602 
2603 static int
2604 triggered(struct fme *fmep, struct event *ep, int mark)
2605 {
2606 	struct bubble *bp;
2607 	struct arrowlist *ap;
2608 	int count = 0;
2609 
2610 	stats_counter_bump(fmep->Tcallcount);
2611 	for (bp = itree_next_bubble(ep, NULL); bp;
2612 	    bp = itree_next_bubble(ep, bp)) {
2613 		if (bp->t != B_TO)
2614 			continue;
2615 		for (ap = itree_next_arrow(bp, NULL); ap;
2616 		    ap = itree_next_arrow(bp, ap)) {
2617 			/* check count of marks against K in the bubble */
2618 			if ((ap->arrowp->mark & mark) &&
2619 			    ++count >= bp->nork)
2620 				return (1);
2621 		}
2622 	}
2623 	return (0);
2624 }
2625 
2626 static int
2627 mark_arrows(struct fme *fmep, struct event *ep, int mark,
2628     unsigned long long at_latest_by, unsigned long long *pdelay)
2629 {
2630 	struct bubble *bp;
2631 	struct arrowlist *ap;
2632 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
2633 	unsigned long long my_delay;
2634 	enum fme_state result;
2635 	int retval = 0;
2636 
2637 	for (bp = itree_next_bubble(ep, NULL); bp;
2638 	    bp = itree_next_bubble(ep, bp)) {
2639 		if (bp->t != B_FROM)
2640 			continue;
2641 		stats_counter_bump(fmep->Marrowcount);
2642 		for (ap = itree_next_arrow(bp, NULL); ap;
2643 		    ap = itree_next_arrow(bp, ap)) {
2644 			struct event *ep2 = ap->arrowp->head->myevent;
2645 			/*
2646 			 * if we're clearing marks, we can avoid doing
2647 			 * all that work evaluating constraints.
2648 			 */
2649 			if (mark == 0) {
2650 				ap->arrowp->mark &= ~EFFECTS_COUNTER;
2651 				ep2->cached_state &=
2652 				    ~(WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT);
2653 				(void) mark_arrows(fmep, ep2, mark, 0, NULL);
2654 				continue;
2655 			}
2656 			if (ep2->cached_state & REQMNTS_DISPROVED) {
2657 				indent();
2658 				out(O_ALTFP|O_VERB|O_NONL,
2659 				    "  ALREADY DISPROVED ");
2660 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2661 				out(O_ALTFP|O_VERB, NULL);
2662 				continue;
2663 			}
2664 			if (ep2->cached_state & WAIT_EFFECT) {
2665 				indent();
2666 				out(O_ALTFP|O_VERB|O_NONL,
2667 				    "  ALREADY EFFECTS WAIT ");
2668 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2669 				out(O_ALTFP|O_VERB, NULL);
2670 				continue;
2671 			}
2672 			if (ep2->cached_state & CREDIBLE_EFFECT) {
2673 				indent();
2674 				out(O_ALTFP|O_VERB|O_NONL,
2675 				    "  ALREADY EFFECTS CREDIBLE ");
2676 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2677 				out(O_ALTFP|O_VERB, NULL);
2678 				continue;
2679 			}
2680 			if ((ep2->cached_state & PARENT_WAIT) &&
2681 			    (mark & PARENT_WAIT)) {
2682 				indent();
2683 				out(O_ALTFP|O_VERB|O_NONL,
2684 				    "  ALREADY PARENT EFFECTS WAIT ");
2685 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2686 				out(O_ALTFP|O_VERB, NULL);
2687 				continue;
2688 			}
2689 			platform_set_payloadnvp(ep2->nvp);
2690 			if (checkconstraints(fmep, ap->arrowp) != 1) {
2691 				platform_set_payloadnvp(NULL);
2692 				indent();
2693 				out(O_ALTFP|O_VERB|O_NONL,
2694 				    "  CONSTRAINTS FAIL ");
2695 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2696 				out(O_ALTFP|O_VERB, NULL);
2697 				continue;
2698 			}
2699 			platform_set_payloadnvp(NULL);
2700 			ap->arrowp->mark |= EFFECTS_COUNTER;
2701 			if (!triggered(fmep, ep2, EFFECTS_COUNTER)) {
2702 				indent();
2703 				out(O_ALTFP|O_VERB|O_NONL,
2704 				    "  K-COUNT NOT YET MET ");
2705 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2706 				out(O_ALTFP|O_VERB, NULL);
2707 				continue;
2708 			}
2709 			ep2->cached_state &= ~PARENT_WAIT;
2710 			result = requirements_test(fmep, ep2, at_latest_by +
2711 			    ap->arrowp->maxdelay,
2712 			    &my_delay);
2713 			if (result == FME_WAIT) {
2714 				retval = WAIT_EFFECT;
2715 				if (overall_delay > my_delay)
2716 					overall_delay = my_delay;
2717 				ep2->cached_state |= WAIT_EFFECT;
2718 				indent();
2719 				out(O_ALTFP|O_VERB|O_NONL, "  EFFECTS WAIT ");
2720 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2721 				out(O_ALTFP|O_VERB, NULL);
2722 				indent_push("  E");
2723 				if (mark_arrows(fmep, ep2, PARENT_WAIT,
2724 				    at_latest_by, &my_delay) == WAIT_EFFECT) {
2725 					retval = WAIT_EFFECT;
2726 					if (overall_delay > my_delay)
2727 						overall_delay = my_delay;
2728 				}
2729 				indent_pop();
2730 			} else if (result == FME_DISPROVED) {
2731 				indent();
2732 				out(O_ALTFP|O_VERB|O_NONL,
2733 				    "  EFFECTS DISPROVED ");
2734 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2735 				out(O_ALTFP|O_VERB, NULL);
2736 			} else {
2737 				ep2->cached_state |= mark;
2738 				indent();
2739 				if (mark == CREDIBLE_EFFECT)
2740 					out(O_ALTFP|O_VERB|O_NONL,
2741 					    "  EFFECTS CREDIBLE ");
2742 				else
2743 					out(O_ALTFP|O_VERB|O_NONL,
2744 					    "  PARENT EFFECTS WAIT ");
2745 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
2746 				out(O_ALTFP|O_VERB, NULL);
2747 				indent_push("  E");
2748 				if (mark_arrows(fmep, ep2, mark, at_latest_by,
2749 				    &my_delay) == WAIT_EFFECT) {
2750 					retval = WAIT_EFFECT;
2751 					if (overall_delay > my_delay)
2752 						overall_delay = my_delay;
2753 				}
2754 				indent_pop();
2755 			}
2756 		}
2757 	}
2758 	if (retval == WAIT_EFFECT)
2759 		*pdelay = overall_delay;
2760 	return (retval);
2761 }
2762 
2763 static enum fme_state
2764 effects_test(struct fme *fmep, struct event *fault_event,
2765     unsigned long long at_latest_by, unsigned long long *pdelay)
2766 {
2767 	struct event *error_event;
2768 	enum fme_state return_value = FME_CREDIBLE;
2769 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
2770 	unsigned long long my_delay;
2771 
2772 	stats_counter_bump(fmep->Ecallcount);
2773 	indent_push("  E");
2774 	indent();
2775 	out(O_ALTFP|O_VERB|O_NONL, "->");
2776 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
2777 	out(O_ALTFP|O_VERB, NULL);
2778 
2779 	(void) mark_arrows(fmep, fault_event, CREDIBLE_EFFECT, at_latest_by,
2780 	    &my_delay);
2781 	for (error_event = fmep->observations;
2782 	    error_event; error_event = error_event->observations) {
2783 		indent();
2784 		out(O_ALTFP|O_VERB|O_NONL, " ");
2785 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, error_event);
2786 		if (!(error_event->cached_state & CREDIBLE_EFFECT)) {
2787 			if (error_event->cached_state &
2788 			    (PARENT_WAIT|WAIT_EFFECT)) {
2789 				return_value = FME_WAIT;
2790 				if (overall_delay > my_delay)
2791 					overall_delay = my_delay;
2792 				out(O_ALTFP|O_VERB, " NOT YET triggered");
2793 				continue;
2794 			}
2795 			return_value = FME_DISPROVED;
2796 			out(O_ALTFP|O_VERB, " NOT triggered");
2797 			break;
2798 		} else {
2799 			out(O_ALTFP|O_VERB, " triggered");
2800 		}
2801 	}
2802 	(void) mark_arrows(fmep, fault_event, 0, 0, NULL);
2803 
2804 	indent();
2805 	out(O_ALTFP|O_VERB|O_NONL, "<-EFFECTS %s ",
2806 	    fme_state2str(return_value));
2807 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
2808 	out(O_ALTFP|O_VERB, NULL);
2809 	indent_pop();
2810 	if (return_value == FME_WAIT)
2811 		*pdelay = overall_delay;
2812 	return (return_value);
2813 }
2814 
2815 static enum fme_state
2816 requirements_test(struct fme *fmep, struct event *ep,
2817     unsigned long long at_latest_by, unsigned long long *pdelay)
2818 {
2819 	int waiting_events;
2820 	int credible_events;
2821 	int deferred_events;
2822 	enum fme_state return_value = FME_CREDIBLE;
2823 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
2824 	unsigned long long arrow_delay;
2825 	unsigned long long my_delay;
2826 	struct event *ep2;
2827 	struct bubble *bp;
2828 	struct arrowlist *ap;
2829 
2830 	if (ep->cached_state & REQMNTS_CREDIBLE) {
2831 		indent();
2832 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY CREDIBLE ");
2833 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2834 		out(O_ALTFP|O_VERB, NULL);
2835 		return (FME_CREDIBLE);
2836 	}
2837 	if (ep->cached_state & REQMNTS_DISPROVED) {
2838 		indent();
2839 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY DISPROVED ");
2840 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2841 		out(O_ALTFP|O_VERB, NULL);
2842 		return (FME_DISPROVED);
2843 	}
2844 	if (ep->cached_state & REQMNTS_WAIT) {
2845 		indent();
2846 		*pdelay = ep->cached_delay;
2847 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY WAIT ");
2848 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2849 		out(O_ALTFP|O_VERB|O_NONL, ", wait for: ");
2850 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
2851 		out(O_ALTFP|O_VERB, NULL);
2852 		return (FME_WAIT);
2853 	}
2854 	stats_counter_bump(fmep->Rcallcount);
2855 	indent_push("  R");
2856 	indent();
2857 	out(O_ALTFP|O_VERB|O_NONL, "->");
2858 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2859 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
2860 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
2861 	out(O_ALTFP|O_VERB, NULL);
2862 
2863 	if (ep->t == N_EREPORT) {
2864 		if (ep->count == 0) {
2865 			if (fmep->pull >= at_latest_by) {
2866 				return_value = FME_DISPROVED;
2867 			} else {
2868 				ep->cached_delay = *pdelay = at_latest_by;
2869 				return_value = FME_WAIT;
2870 			}
2871 		}
2872 
2873 		indent();
2874 		switch (return_value) {
2875 		case FME_CREDIBLE:
2876 			ep->cached_state |= REQMNTS_CREDIBLE;
2877 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS CREDIBLE ");
2878 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2879 			break;
2880 		case FME_DISPROVED:
2881 			ep->cached_state |= REQMNTS_DISPROVED;
2882 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
2883 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2884 			break;
2885 		case FME_WAIT:
2886 			ep->cached_state |= REQMNTS_WAIT;
2887 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS WAIT ");
2888 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2889 			out(O_ALTFP|O_VERB|O_NONL, " to ");
2890 			ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
2891 			break;
2892 		default:
2893 			out(O_DIE, "requirements_test: unexpected fme_state");
2894 			break;
2895 		}
2896 		out(O_ALTFP|O_VERB, NULL);
2897 		indent_pop();
2898 
2899 		return (return_value);
2900 	}
2901 
2902 	/* this event is not a report, descend the tree */
2903 	for (bp = itree_next_bubble(ep, NULL); bp;
2904 	    bp = itree_next_bubble(ep, bp)) {
2905 		int n;
2906 
2907 		if (bp->t != B_FROM)
2908 			continue;
2909 
2910 		n = bp->nork;
2911 
2912 		credible_events = 0;
2913 		waiting_events = 0;
2914 		deferred_events = 0;
2915 		arrow_delay = TIMEVAL_EVENTUALLY;
2916 		/*
2917 		 * n is -1 for 'A' so adjust it.
2918 		 * XXX just count up the arrows for now.
2919 		 */
2920 		if (n < 0) {
2921 			n = 0;
2922 			for (ap = itree_next_arrow(bp, NULL); ap;
2923 			    ap = itree_next_arrow(bp, ap))
2924 				n++;
2925 			indent();
2926 			out(O_ALTFP|O_VERB, " Bubble Counted N=%d", n);
2927 		} else {
2928 			indent();
2929 			out(O_ALTFP|O_VERB, " Bubble N=%d", n);
2930 		}
2931 
2932 		if (n == 0)
2933 			continue;
2934 		if (!(bp->mark & (BUBBLE_ELIDED|BUBBLE_OK))) {
2935 			for (ap = itree_next_arrow(bp, NULL); ap;
2936 			    ap = itree_next_arrow(bp, ap)) {
2937 				ep2 = ap->arrowp->head->myevent;
2938 				platform_set_payloadnvp(ep2->nvp);
2939 				if (checkconstraints(fmep, ap->arrowp) == 0) {
2940 					/*
2941 					 * if any arrow is invalidated by the
2942 					 * constraints, then we should elide the
2943 					 * whole bubble to be consistant with
2944 					 * the tree creation time behaviour
2945 					 */
2946 					bp->mark |= BUBBLE_ELIDED;
2947 					platform_set_payloadnvp(NULL);
2948 					break;
2949 				}
2950 				platform_set_payloadnvp(NULL);
2951 			}
2952 		}
2953 		if (bp->mark & BUBBLE_ELIDED)
2954 			continue;
2955 		bp->mark |= BUBBLE_OK;
2956 		for (ap = itree_next_arrow(bp, NULL); ap;
2957 		    ap = itree_next_arrow(bp, ap)) {
2958 			ep2 = ap->arrowp->head->myevent;
2959 			if (n <= credible_events)
2960 				break;
2961 
2962 			ap->arrowp->mark |= REQMNTS_COUNTER;
2963 			if (triggered(fmep, ep2, REQMNTS_COUNTER))
2964 				/* XXX adding max timevals! */
2965 				switch (requirements_test(fmep, ep2,
2966 				    at_latest_by + ap->arrowp->maxdelay,
2967 				    &my_delay)) {
2968 				case FME_DEFERRED:
2969 					deferred_events++;
2970 					break;
2971 				case FME_CREDIBLE:
2972 					credible_events++;
2973 					break;
2974 				case FME_DISPROVED:
2975 					break;
2976 				case FME_WAIT:
2977 					if (my_delay < arrow_delay)
2978 						arrow_delay = my_delay;
2979 					waiting_events++;
2980 					break;
2981 				default:
2982 					out(O_DIE,
2983 					"Bug in requirements_test.");
2984 				}
2985 			else
2986 				deferred_events++;
2987 		}
2988 		indent();
2989 		out(O_ALTFP|O_VERB, " Credible: %d Waiting %d",
2990 		    credible_events + deferred_events, waiting_events);
2991 		if (credible_events + deferred_events + waiting_events < n) {
2992 			/* Can never meet requirements */
2993 			ep->cached_state |= REQMNTS_DISPROVED;
2994 			indent();
2995 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
2996 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
2997 			out(O_ALTFP|O_VERB, NULL);
2998 			indent_pop();
2999 			return (FME_DISPROVED);
3000 		}
3001 		if (credible_events + deferred_events < n) {
3002 			/* will have to wait */
3003 			/* wait time is shortest known */
3004 			if (arrow_delay < overall_delay)
3005 				overall_delay = arrow_delay;
3006 			return_value = FME_WAIT;
3007 		} else if (credible_events < n) {
3008 			if (return_value != FME_WAIT)
3009 				return_value = FME_DEFERRED;
3010 		}
3011 	}
3012 
3013 	/*
3014 	 * don't mark as FME_DEFERRED. If this event isn't reached by another
3015 	 * path, then this will be considered FME_CREDIBLE. But if it is
3016 	 * reached by a different path so the K-count is met, then might
3017 	 * get overridden by FME_WAIT or FME_DISPROVED.
3018 	 */
3019 	if (return_value == FME_WAIT) {
3020 		ep->cached_state |= REQMNTS_WAIT;
3021 		ep->cached_delay = *pdelay = overall_delay;
3022 	} else if (return_value == FME_CREDIBLE) {
3023 		ep->cached_state |= REQMNTS_CREDIBLE;
3024 	}
3025 	indent();
3026 	out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS %s ",
3027 	    fme_state2str(return_value));
3028 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3029 	out(O_ALTFP|O_VERB, NULL);
3030 	indent_pop();
3031 	return (return_value);
3032 }
3033 
3034 static enum fme_state
3035 causes_test(struct fme *fmep, struct event *ep,
3036     unsigned long long at_latest_by, unsigned long long *pdelay)
3037 {
3038 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3039 	unsigned long long my_delay;
3040 	int credible_results = 0;
3041 	int waiting_results = 0;
3042 	enum fme_state fstate;
3043 	struct event *tail_event;
3044 	struct bubble *bp;
3045 	struct arrowlist *ap;
3046 	int k = 1;
3047 
3048 	stats_counter_bump(fmep->Ccallcount);
3049 	indent_push("  C");
3050 	indent();
3051 	out(O_ALTFP|O_VERB|O_NONL, "->");
3052 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3053 	out(O_ALTFP|O_VERB, NULL);
3054 
3055 	for (bp = itree_next_bubble(ep, NULL); bp;
3056 	    bp = itree_next_bubble(ep, bp)) {
3057 		if (bp->t != B_TO)
3058 			continue;
3059 		k = bp->nork;	/* remember the K value */
3060 		for (ap = itree_next_arrow(bp, NULL); ap;
3061 		    ap = itree_next_arrow(bp, ap)) {
3062 			int do_not_follow = 0;
3063 
3064 			/*
3065 			 * if we get to the same event multiple times
3066 			 * only worry about the first one.
3067 			 */
3068 			if (ap->arrowp->tail->myevent->cached_state &
3069 			    CAUSES_TESTED) {
3070 				indent();
3071 				out(O_ALTFP|O_VERB|O_NONL,
3072 				    "  causes test already run for ");
3073 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
3074 				    ap->arrowp->tail->myevent);
3075 				out(O_ALTFP|O_VERB, NULL);
3076 				continue;
3077 			}
3078 
3079 			/*
3080 			 * see if false constraint prevents us
3081 			 * from traversing this arrow
3082 			 */
3083 			platform_set_payloadnvp(ep->nvp);
3084 			if (checkconstraints(fmep, ap->arrowp) != 1)
3085 				do_not_follow = 1;
3086 			platform_set_payloadnvp(NULL);
3087 			if (do_not_follow) {
3088 				indent();
3089 				out(O_ALTFP|O_VERB|O_NONL,
3090 				    "  False arrow from ");
3091 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
3092 				    ap->arrowp->tail->myevent);
3093 				out(O_ALTFP|O_VERB, NULL);
3094 				continue;
3095 			}
3096 
3097 			ap->arrowp->tail->myevent->cached_state |=
3098 			    CAUSES_TESTED;
3099 			tail_event = ap->arrowp->tail->myevent;
3100 			fstate = hypothesise(fmep, tail_event, at_latest_by,
3101 			    &my_delay);
3102 
3103 			switch (fstate) {
3104 			case FME_WAIT:
3105 				if (my_delay < overall_delay)
3106 					overall_delay = my_delay;
3107 				waiting_results++;
3108 				break;
3109 			case FME_CREDIBLE:
3110 				credible_results++;
3111 				break;
3112 			case FME_DISPROVED:
3113 				break;
3114 			default:
3115 				out(O_DIE, "Bug in causes_test");
3116 			}
3117 		}
3118 	}
3119 	/* compare against K */
3120 	if (credible_results + waiting_results < k) {
3121 		indent();
3122 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES DISPROVED ");
3123 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3124 		out(O_ALTFP|O_VERB, NULL);
3125 		indent_pop();
3126 		return (FME_DISPROVED);
3127 	}
3128 	if (waiting_results != 0) {
3129 		*pdelay = overall_delay;
3130 		indent();
3131 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES WAIT ");
3132 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3133 		out(O_ALTFP|O_VERB|O_NONL, " to ");
3134 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3135 		out(O_ALTFP|O_VERB, NULL);
3136 		indent_pop();
3137 		return (FME_WAIT);
3138 	}
3139 	indent();
3140 	out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES CREDIBLE ");
3141 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3142 	out(O_ALTFP|O_VERB, NULL);
3143 	indent_pop();
3144 	return (FME_CREDIBLE);
3145 }
3146 
3147 static enum fme_state
3148 hypothesise(struct fme *fmep, struct event *ep,
3149 	unsigned long long at_latest_by, unsigned long long *pdelay)
3150 {
3151 	enum fme_state rtr, otr;
3152 	unsigned long long my_delay;
3153 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3154 
3155 	stats_counter_bump(fmep->Hcallcount);
3156 	indent_push("  H");
3157 	indent();
3158 	out(O_ALTFP|O_VERB|O_NONL, "->");
3159 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3160 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
3161 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3162 	out(O_ALTFP|O_VERB, NULL);
3163 
3164 	rtr = requirements_test(fmep, ep, at_latest_by, &my_delay);
3165 	if ((rtr == FME_WAIT) && (my_delay < overall_delay))
3166 		overall_delay = my_delay;
3167 	if (rtr != FME_DISPROVED) {
3168 		if (is_problem(ep->t)) {
3169 			otr = effects_test(fmep, ep, at_latest_by, &my_delay);
3170 			if (otr != FME_DISPROVED) {
3171 				if (fmep->peek == 0 && ep->is_suspect++ == 0) {
3172 					ep->suspects = fmep->suspects;
3173 					fmep->suspects = ep;
3174 					fmep->nsuspects++;
3175 					if (!is_fault(ep->t))
3176 						fmep->nonfault++;
3177 				}
3178 			}
3179 		} else
3180 			otr = causes_test(fmep, ep, at_latest_by, &my_delay);
3181 		if ((otr == FME_WAIT) && (my_delay < overall_delay))
3182 			overall_delay = my_delay;
3183 		if ((otr != FME_DISPROVED) &&
3184 		    ((rtr == FME_WAIT) || (otr == FME_WAIT)))
3185 			*pdelay = overall_delay;
3186 	}
3187 	if (rtr == FME_DISPROVED) {
3188 		indent();
3189 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3190 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3191 		out(O_ALTFP|O_VERB, " (doesn't meet requirements)");
3192 		indent_pop();
3193 		return (FME_DISPROVED);
3194 	}
3195 	if ((otr == FME_DISPROVED) && is_problem(ep->t)) {
3196 		indent();
3197 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3198 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3199 		out(O_ALTFP|O_VERB, " (doesn't explain all reports)");
3200 		indent_pop();
3201 		return (FME_DISPROVED);
3202 	}
3203 	if (otr == FME_DISPROVED) {
3204 		indent();
3205 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3206 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3207 		out(O_ALTFP|O_VERB, " (causes are not credible)");
3208 		indent_pop();
3209 		return (FME_DISPROVED);
3210 	}
3211 	if ((rtr == FME_WAIT) || (otr == FME_WAIT)) {
3212 		indent();
3213 		out(O_ALTFP|O_VERB|O_NONL, "<-WAIT ");
3214 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3215 		out(O_ALTFP|O_VERB|O_NONL, " to ");
3216 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &overall_delay);
3217 		out(O_ALTFP|O_VERB, NULL);
3218 		indent_pop();
3219 		return (FME_WAIT);
3220 	}
3221 	indent();
3222 	out(O_ALTFP|O_VERB|O_NONL, "<-CREDIBLE ");
3223 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3224 	out(O_ALTFP|O_VERB, NULL);
3225 	indent_pop();
3226 	return (FME_CREDIBLE);
3227 }
3228 
3229 /*
3230  * fme_istat_load -- reconstitute any persistent istats
3231  */
3232 void
3233 fme_istat_load(fmd_hdl_t *hdl)
3234 {
3235 	int sz;
3236 	char *sbuf;
3237 	char *ptr;
3238 
3239 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_ISTATS)) == 0) {
3240 		out(O_ALTFP, "fme_istat_load: No stats");
3241 		return;
3242 	}
3243 
3244 	sbuf = alloca(sz);
3245 
3246 	fmd_buf_read(hdl, NULL, WOBUF_ISTATS, sbuf, sz);
3247 
3248 	/*
3249 	 * pick apart the serialized stats
3250 	 *
3251 	 * format is:
3252 	 *	<class-name>, '@', <path>, '\0', <value>, '\0'
3253 	 * for example:
3254 	 *	"stat.first@stat0/path0\02\0stat.second@stat0/path1\023\0"
3255 	 *
3256 	 * since this is parsing our own serialized data, any parsing issues
3257 	 * are fatal, so we check for them all with ASSERT() below.
3258 	 */
3259 	ptr = sbuf;
3260 	while (ptr < &sbuf[sz]) {
3261 		char *sepptr;
3262 		struct node *np;
3263 		int val;
3264 
3265 		sepptr = strchr(ptr, '@');
3266 		ASSERT(sepptr != NULL);
3267 		*sepptr = '\0';
3268 
3269 		/* construct the event */
3270 		np = newnode(T_EVENT, NULL, 0);
3271 		np->u.event.ename = newnode(T_NAME, NULL, 0);
3272 		np->u.event.ename->u.name.t = N_STAT;
3273 		np->u.event.ename->u.name.s = stable(ptr);
3274 		np->u.event.ename->u.name.it = IT_ENAME;
3275 		np->u.event.ename->u.name.last = np->u.event.ename;
3276 
3277 		ptr = sepptr + 1;
3278 		ASSERT(ptr < &sbuf[sz]);
3279 		ptr += strlen(ptr);
3280 		ptr++;	/* move past the '\0' separating path from value */
3281 		ASSERT(ptr < &sbuf[sz]);
3282 		ASSERT(isdigit(*ptr));
3283 		val = atoi(ptr);
3284 		ASSERT(val > 0);
3285 		ptr += strlen(ptr);
3286 		ptr++;	/* move past the final '\0' for this entry */
3287 
3288 		np->u.event.epname = pathstring2epnamenp(sepptr + 1);
3289 		ASSERT(np->u.event.epname != NULL);
3290 
3291 		istat_bump(np, val);
3292 		tree_free(np);
3293 	}
3294 
3295 	istat_save();
3296 }
3297