xref: /titanic_50/usr/src/cmd/fm/fminject/common/inj_log.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <strings.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <inj_event.h>
32*7c478bd9Sstevel@tonic-gate #include <inj_err.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <fm/fmd_log.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate typedef struct inj_logfile {
37*7c478bd9Sstevel@tonic-gate 	uint64_t ilf_sec;	/* timestamp seconds from previous record */
38*7c478bd9Sstevel@tonic-gate 	uint64_t ilf_nsec;	/* timestamp nanoseconds from previous record */
39*7c478bd9Sstevel@tonic-gate 	int ilf_index;		/* record index in log file for fake lineno */
40*7c478bd9Sstevel@tonic-gate } inj_logfile_t;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43*7c478bd9Sstevel@tonic-gate static int
inj_logfile_event(fmd_log_t * lp,const fmd_log_record_t * rp,void * data)44*7c478bd9Sstevel@tonic-gate inj_logfile_event(fmd_log_t *lp, const fmd_log_record_t *rp, void *data)
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
47*7c478bd9Sstevel@tonic-gate 	inj_defn_t *ev = inj_zalloc(sizeof (inj_defn_t));
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	hrtime_t rec_sec = rp->rec_sec + rp->rec_nsec / NANOSEC;
50*7c478bd9Sstevel@tonic-gate 	hrtime_t rec_nsec = rp->rec_nsec % NANOSEC;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	inj_logfile_t *ilf = data;
53*7c478bd9Sstevel@tonic-gate 	hrtime_t delta;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (ilf->ilf_index == 1)
56*7c478bd9Sstevel@tonic-gate 		goto add_event; /* do not try to adjust time for first record */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/*
59*7c478bd9Sstevel@tonic-gate 	 * If the current record's time is before that of the previous record,
60*7c478bd9Sstevel@tonic-gate 	 * advance it to the previous record time.  This may occur when delays
61*7c478bd9Sstevel@tonic-gate 	 * between capturing ENA and enqueuing a sysevent are observed.
62*7c478bd9Sstevel@tonic-gate 	 */
63*7c478bd9Sstevel@tonic-gate 	if (rec_sec < ilf->ilf_sec ||
64*7c478bd9Sstevel@tonic-gate 	    (rec_sec == ilf->ilf_sec && rec_nsec < ilf->ilf_nsec)) {
65*7c478bd9Sstevel@tonic-gate 		warn("record [%d] (%s) timestamp is out of order: "
66*7c478bd9Sstevel@tonic-gate 		    "advancing event time to %llx.%llx\n",
67*7c478bd9Sstevel@tonic-gate 		    ilf->ilf_index, rp->rec_class, ilf->ilf_sec, ilf->ilf_nsec);
68*7c478bd9Sstevel@tonic-gate 		rec_sec = ilf->ilf_sec;
69*7c478bd9Sstevel@tonic-gate 		rec_nsec = ilf->ilf_nsec;
70*7c478bd9Sstevel@tonic-gate 	}
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	 * For now, compute the delta between the previous record and this one
74*7c478bd9Sstevel@tonic-gate 	 * as a number of nanoseconds and advance the clock.  If a massively
75*7c478bd9Sstevel@tonic-gate 	 * large delay is observed (>INT64_MAX ns), we abort.  This could be
76*7c478bd9Sstevel@tonic-gate 	 * improved if necessary by sending more than one cmd_addhrt in a loop.
77*7c478bd9Sstevel@tonic-gate 	 */
78*7c478bd9Sstevel@tonic-gate 	delta = (rec_sec - ilf->ilf_sec) * NANOSEC;
79*7c478bd9Sstevel@tonic-gate 	delta += (hrtime_t)rec_nsec - (hrtime_t)ilf->ilf_nsec;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (delta < 0)
82*7c478bd9Sstevel@tonic-gate 		die("record [%d] timestamp delta too large\n", ilf->ilf_index);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (delta > 0)
85*7c478bd9Sstevel@tonic-gate 		inj_cmds_add(inj_cmd_addhrt(delta));
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate add_event:
88*7c478bd9Sstevel@tonic-gate 	ev->defn_name = inj_strdup(rp->rec_class);
89*7c478bd9Sstevel@tonic-gate 	ev->defn_lineno = ilf->ilf_index++;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_dup(rp->rec_nvl, &ev->defn_nvl, 0)) != 0)
92*7c478bd9Sstevel@tonic-gate 		die("failed to allocate nvl for %s event", rp->rec_class);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	cmd->cmd_type = CMD_SEND_EVENT;
95*7c478bd9Sstevel@tonic-gate 	cmd->cmd_event = ev;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	inj_cmds_add(cmd);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	ilf->ilf_sec = rec_sec;
100*7c478bd9Sstevel@tonic-gate 	ilf->ilf_nsec = rec_nsec;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	return (0);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate inj_list_t *
inj_logfile_read(fmd_log_t * lp)106*7c478bd9Sstevel@tonic-gate inj_logfile_read(fmd_log_t *lp)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	const char *label = fmd_log_label(lp);
109*7c478bd9Sstevel@tonic-gate 	inj_logfile_t ilf;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if (strcmp(label, "error") != 0)
112*7c478bd9Sstevel@tonic-gate 		die("cannot use '%s' log as injector input\n", label);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	bzero(&ilf, sizeof (ilf));
115*7c478bd9Sstevel@tonic-gate 	ilf.ilf_index = 1;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if (fmd_log_iter(lp, inj_logfile_event, &ilf) != 0) {
118*7c478bd9Sstevel@tonic-gate 		die("failed to process log: %s\n",
119*7c478bd9Sstevel@tonic-gate 		    fmd_log_errmsg(lp, fmd_log_errno(lp)));
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	return (inj_cmds_get());
123*7c478bd9Sstevel@tonic-gate }
124