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