xref: /titanic_50/usr/src/cmd/fm/fminject/common/inj_cmds.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * The "program" executed by the injector consists of a tree of commands.
32  * Routines in this file build and execute said command tree.
33  */
34 
35 #include <sys/fm/protocol.h>
36 #include <unistd.h>
37 
38 #include <inj.h>
39 #include <inj_event.h>
40 #include <inj_lex.h>
41 #include <inj_err.h>
42 
43 /*
44  * Command tree construction
45  */
46 
47 static inj_list_t inj_cmds;
48 
49 void
50 inj_cmds_add(inj_cmd_t *cmd)
51 {
52 	inj_list_append(&inj_cmds, cmd);
53 }
54 
55 inj_list_t *
56 inj_cmds_get(void)
57 {
58 	return (&inj_cmds);
59 }
60 
61 inj_randelem_t *
62 inj_rand_create(inj_defn_t *ev, uint_t prob)
63 {
64 	inj_randelem_t *re = inj_zalloc(sizeof (inj_randelem_t));
65 
66 	re->re_event = ev;
67 	re->re_prob = prob;
68 
69 	return (re);
70 }
71 
72 inj_randelem_t *
73 inj_rand_add(inj_randelem_t *list, inj_randelem_t *new)
74 {
75 	new->re_next = list;
76 	return (new);
77 }
78 
79 inj_cmd_t *
80 inj_cmd_rand(inj_randelem_t *rlist)
81 {
82 	inj_randelem_t *r;
83 	inj_cmd_t *cmd;
84 	uint_t prob, tmpprob;
85 	int nelems, i;
86 
87 	prob = 0;
88 	for (i = 0, r = rlist; r != NULL; r = r->re_next, i++)
89 		prob += r->re_prob;
90 
91 	if (prob != 100) {
92 		yyerror("probabilities don't sum to 100\n");
93 		return (NULL);
94 	}
95 
96 	nelems = i;
97 
98 	cmd = inj_zalloc(sizeof (inj_cmd_t));
99 	cmd->cmd_type = CMD_RANDOM;
100 	cmd->cmd_num = nelems;
101 	cmd->cmd_rand = inj_alloc(sizeof (inj_randelem_t *) * nelems);
102 
103 	prob = 0;
104 	for (r = rlist, i = 0; i < nelems; i++, r = r->re_next) {
105 		tmpprob = r->re_prob;
106 		r->re_prob = prob;
107 		prob += tmpprob;
108 
109 		cmd->cmd_rand[i] = r;
110 	}
111 
112 	return (cmd);
113 }
114 
115 inj_cmd_t *
116 inj_cmd_repeat(inj_cmd_t *repcmd, uint_t num)
117 {
118 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
119 
120 	cmd->cmd_type = CMD_REPEAT;
121 	cmd->cmd_num = num;
122 	cmd->cmd_subcmd = repcmd;
123 
124 	return (cmd);
125 }
126 
127 inj_cmd_t *
128 inj_cmd_send(inj_defn_t *ev)
129 {
130 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
131 
132 	cmd->cmd_type = CMD_SEND_EVENT;
133 	cmd->cmd_event = ev;
134 
135 	return (cmd);
136 }
137 
138 inj_cmd_t *
139 inj_cmd_sleep(uint_t secs)
140 {
141 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
142 
143 	cmd->cmd_type = CMD_SLEEP;
144 	cmd->cmd_num = secs;
145 
146 	return (cmd);
147 }
148 
149 inj_cmd_t *
150 inj_cmd_addhrt(hrtime_t delta)
151 {
152 	const char *class = "resource.fm.fmd.clock.addhrtime";
153 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
154 	inj_defn_t *ev = inj_zalloc(sizeof (inj_defn_t));
155 
156 	ev->defn_name = class;
157 	ev->defn_lineno = yylineno;
158 
159 	if ((errno = nvlist_alloc(&ev->defn_nvl, NV_UNIQUE_NAME, 0)) != 0)
160 		die("failed to allocate nvl for %s event", class);
161 
162 	if ((errno = nvlist_add_string(ev->defn_nvl, FM_CLASS, class)) != 0 ||
163 	    (errno = nvlist_add_uint8(ev->defn_nvl, FM_VERSION, 1)) != 0 ||
164 	    (errno = nvlist_add_int64(ev->defn_nvl, "delta", delta)) != 0)
165 		die("failed to build nvl for %s event", class);
166 
167 	cmd->cmd_type = CMD_SEND_EVENT;
168 	cmd->cmd_event = ev;
169 
170 	return (cmd);
171 }
172 
173 inj_cmd_t *
174 inj_cmd_endhrt(void)
175 {
176 	return (inj_cmd_addhrt(-1LL)); /* clock underflow causes end of time */
177 }
178 
179 static uint64_t
180 inj_ena(void)
181 {
182 	return (((gethrtime() & ENA_FMT1_TIME_MASK) <<
183 	    ENA_FMT1_TIME_SHFT) | (FM_ENA_FMT1 & ENA_FORMAT_MASK));
184 }
185 
186 static void
187 cmd_run_send(const inj_mode_ops_t *mode, void *hdl, inj_defn_t *ev)
188 {
189 	if (!quiet) {
190 		(void) printf("sending event %s ... ", ev->defn_name);
191 		(void) fflush(stdout);
192 	}
193 
194 	if (ev->defn_decl && (ev->defn_decl->decl_flags & DECL_F_AUTOENA) &&
195 	    (errno = nvlist_add_uint64(ev->defn_nvl, "ena", inj_ena())) != 0)
196 		warn("failed to add ena to %s", ev->defn_name);
197 
198 	if (verbose) {
199 		nvlist_print(stdout, ev->defn_nvl);
200 		(void) printf("\n");
201 	}
202 
203 	mode->mo_send(hdl, ev->defn_nvl);
204 
205 	if (!quiet)
206 		(void) printf("done\n");
207 }
208 
209 static void
210 cmd_run_random(const inj_mode_ops_t *mode, void *hdl, inj_cmd_t *cmd)
211 {
212 	uint_t num = lrand48() % 100;
213 	int i;
214 
215 	for (i = 1; i < cmd->cmd_num; i++) {
216 		if (cmd->cmd_rand[i]->re_prob > num)
217 			break;
218 	}
219 
220 	cmd_run_send(mode, hdl, cmd->cmd_rand[i - 1]->re_event);
221 }
222 
223 static void
224 cmd_run(const inj_mode_ops_t *mode, void *hdl, inj_cmd_t *cmd)
225 {
226 	switch (cmd->cmd_type) {
227 	case CMD_SEND_EVENT:
228 		cmd_run_send(mode, hdl, cmd->cmd_event);
229 		break;
230 
231 	case CMD_SLEEP:
232 		(void) printf("sleeping for %d sec%s ... ",
233 		    cmd->cmd_num, cmd->cmd_num > 1 ? "s" : "");
234 		(void) fflush(stdout);
235 		(void) sleep(cmd->cmd_num);
236 		(void) printf("done\n");
237 		break;
238 
239 	case CMD_RANDOM:
240 		cmd_run_random(mode, hdl, cmd);
241 		break;
242 
243 	default:
244 		warn("ignoring unknown command type: %d\n", cmd->cmd_type);
245 	}
246 }
247 
248 void
249 inj_program_run(inj_list_t *prog, const inj_mode_ops_t *mode, void *mode_arg)
250 {
251 	void *hdl = mode->mo_open(mode_arg);
252 	inj_cmd_t *cmd;
253 	int i;
254 
255 	for (cmd = inj_list_next(prog); cmd != NULL; cmd = inj_list_next(cmd)) {
256 		if (cmd->cmd_type == CMD_REPEAT) {
257 			for (i = 1; i <= cmd->cmd_num; i++) {
258 				if (verbose) {
259 					(void) printf("(repeat %d of %d)\n",
260 					    i, cmd->cmd_num);
261 				}
262 				cmd_run(mode, hdl, cmd->cmd_subcmd);
263 			}
264 		} else
265 			cmd_run(mode, hdl, cmd);
266 	}
267 
268 	mode->mo_close(hdl);
269 }
270