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