1*8a272653SPeter Holm /*-
2*8a272653SPeter Holm * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3*8a272653SPeter Holm * All rights reserved.
4*8a272653SPeter Holm *
5*8a272653SPeter Holm * Redistribution and use in source and binary forms, with or without
6*8a272653SPeter Holm * modification, are permitted provided that the following conditions
7*8a272653SPeter Holm * are met:
8*8a272653SPeter Holm * 1. Redistributions of source code must retain the above copyright
9*8a272653SPeter Holm * notice, this list of conditions and the following disclaimer.
10*8a272653SPeter Holm * 2. Redistributions in binary form must reproduce the above copyright
11*8a272653SPeter Holm * notice, this list of conditions and the following disclaimer in the
12*8a272653SPeter Holm * documentation and/or other materials provided with the distribution.
13*8a272653SPeter Holm *
14*8a272653SPeter Holm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*8a272653SPeter Holm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*8a272653SPeter Holm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*8a272653SPeter Holm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*8a272653SPeter Holm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*8a272653SPeter Holm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*8a272653SPeter Holm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*8a272653SPeter Holm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*8a272653SPeter Holm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*8a272653SPeter Holm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*8a272653SPeter Holm * SUCH DAMAGE.
25*8a272653SPeter Holm *
26*8a272653SPeter Holm */
27*8a272653SPeter Holm
28*8a272653SPeter Holm #include <stdio.h>
29*8a272653SPeter Holm #include <stdlib.h>
30*8a272653SPeter Holm #include <unistd.h>
31*8a272653SPeter Holm #include <sysexits.h>
32*8a272653SPeter Holm #include <string.h>
33*8a272653SPeter Holm #include <time.h>
34*8a272653SPeter Holm #include <err.h>
35*8a272653SPeter Holm
36*8a272653SPeter Holm #include "stress.h"
37*8a272653SPeter Holm
38*8a272653SPeter Holm static opt_t opt;
39*8a272653SPeter Holm opt_t *op;
40*8a272653SPeter Holm
41*8a272653SPeter Holm static char path[64];
42*8a272653SPeter Holm
43*8a272653SPeter Holm static void
usage(const char * where)44*8a272653SPeter Holm usage(const char *where)
45*8a272653SPeter Holm {
46*8a272653SPeter Holm const char *help;
47*8a272653SPeter Holm
48*8a272653SPeter Holm if (where != NULL)
49*8a272653SPeter Holm printf("Error in \"%s\"\n", where);
50*8a272653SPeter Holm fprintf(stderr, "Usage: %s [-t | -l | -i | -d | -h | -k | -v]\n", getprogname());
51*8a272653SPeter Holm help = " t <number><s|m|h|d> : time to run test\n"
52*8a272653SPeter Holm " l <pct> : load factor 0 - 100%\n"
53*8a272653SPeter Holm " i <number> : max # of parallel incarnations\n"
54*8a272653SPeter Holm " d <path> : working directory\n"
55*8a272653SPeter Holm " h : hog resources\n"
56*8a272653SPeter Holm " k : terminate with SIGHUP + SIGKILL\n"
57*8a272653SPeter Holm " n : no startup delay\n"
58*8a272653SPeter Holm " v : verbose\n";
59*8a272653SPeter Holm printf("%s", help);
60*8a272653SPeter Holm exit(EX_USAGE);
61*8a272653SPeter Holm }
62*8a272653SPeter Holm
63*8a272653SPeter Holm static int
time2sec(const char * string)64*8a272653SPeter Holm time2sec(const char *string)
65*8a272653SPeter Holm {
66*8a272653SPeter Holm int r, s = 0;
67*8a272653SPeter Holm char modifier;
68*8a272653SPeter Holm r = sscanf(string, "%d%c", &s, &modifier);
69*8a272653SPeter Holm if (r == 2)
70*8a272653SPeter Holm switch(modifier) {
71*8a272653SPeter Holm case 's': break;
72*8a272653SPeter Holm case 'm': s = s * 60; break;
73*8a272653SPeter Holm case 'h': s = s * 60 * 60; break;
74*8a272653SPeter Holm case 'd': s = s * 60 * 60 * 24; break;
75*8a272653SPeter Holm default:
76*8a272653SPeter Holm usage("-t");
77*8a272653SPeter Holm }
78*8a272653SPeter Holm else
79*8a272653SPeter Holm usage("-t");
80*8a272653SPeter Holm return (s);
81*8a272653SPeter Holm }
82*8a272653SPeter Holm
gete(const char * name)83*8a272653SPeter Holm static char *gete(const char *name)
84*8a272653SPeter Holm {
85*8a272653SPeter Holm char *cp;
86*8a272653SPeter Holm char help[128];
87*8a272653SPeter Holm
88*8a272653SPeter Holm snprintf(help, sizeof(help), "%s%s", getprogname(), name);
89*8a272653SPeter Holm cp = getenv(help);
90*8a272653SPeter Holm if (cp == NULL)
91*8a272653SPeter Holm cp = getenv(name);
92*8a272653SPeter Holm return (cp);
93*8a272653SPeter Holm }
94*8a272653SPeter Holm
95*8a272653SPeter Holm static void
environment(void)96*8a272653SPeter Holm environment(void)
97*8a272653SPeter Holm {
98*8a272653SPeter Holm char *cp;
99*8a272653SPeter Holm
100*8a272653SPeter Holm if ((cp = gete("INCARNATIONS")) != NULL) {
101*8a272653SPeter Holm if (sscanf(cp, "%d", &op->incarnations) != 1)
102*8a272653SPeter Holm usage("INCARNATIONS");
103*8a272653SPeter Holm }
104*8a272653SPeter Holm if ((cp = gete("LOAD")) != NULL) {
105*8a272653SPeter Holm if (sscanf(cp, "%d", &op->load) != 1)
106*8a272653SPeter Holm usage("LOAD");
107*8a272653SPeter Holm }
108*8a272653SPeter Holm if ((cp = gete("RUNTIME")) != NULL) {
109*8a272653SPeter Holm op->run_time = time2sec(cp);
110*8a272653SPeter Holm }
111*8a272653SPeter Holm if ((cp = gete("RUNDIR")) != NULL) {
112*8a272653SPeter Holm op->wd = cp;
113*8a272653SPeter Holm }
114*8a272653SPeter Holm if ((cp = gete("CTRLDIR")) != NULL) {
115*8a272653SPeter Holm op->cd = cp;
116*8a272653SPeter Holm }
117*8a272653SPeter Holm if ((cp = gete("HOG")) != NULL) {
118*8a272653SPeter Holm op->hog = 1;
119*8a272653SPeter Holm }
120*8a272653SPeter Holm if ((cp = gete("KILL")) != NULL) {
121*8a272653SPeter Holm op->kill = 1;
122*8a272653SPeter Holm }
123*8a272653SPeter Holm if ((cp = gete("NODELAY")) != NULL) {
124*8a272653SPeter Holm op->nodelay = 1;
125*8a272653SPeter Holm }
126*8a272653SPeter Holm if ((cp = gete("VERBOSE")) != NULL) {
127*8a272653SPeter Holm if (sscanf(cp, "%d", &op->verbose) != 1)
128*8a272653SPeter Holm usage("VERBOSE");
129*8a272653SPeter Holm }
130*8a272653SPeter Holm if ((cp = gete("KBLOCKS")) != NULL) {
131*8a272653SPeter Holm if (sscanf(cp, "%jd", &op->kblocks) != 1)
132*8a272653SPeter Holm usage("KBLOCKS");
133*8a272653SPeter Holm }
134*8a272653SPeter Holm if ((cp = gete("INODES")) != NULL) {
135*8a272653SPeter Holm if (sscanf(cp, "%jd", &op->inodes) != 1)
136*8a272653SPeter Holm usage("INODES");
137*8a272653SPeter Holm }
138*8a272653SPeter Holm }
139*8a272653SPeter Holm
140*8a272653SPeter Holm void
options(int argc,char ** argv)141*8a272653SPeter Holm options(int argc, char **argv)
142*8a272653SPeter Holm {
143*8a272653SPeter Holm int ch;
144*8a272653SPeter Holm
145*8a272653SPeter Holm op = &opt;
146*8a272653SPeter Holm
147*8a272653SPeter Holm op->run_time = 60;
148*8a272653SPeter Holm op->load = 100;
149*8a272653SPeter Holm op->wd = strdup("/tmp/stressX");
150*8a272653SPeter Holm op->cd = strdup("/tmp/stressX.control");
151*8a272653SPeter Holm op->incarnations = 1;
152*8a272653SPeter Holm op->hog = 0;
153*8a272653SPeter Holm op->kill = 0;
154*8a272653SPeter Holm op->nodelay = 0;
155*8a272653SPeter Holm op->verbose = 0;
156*8a272653SPeter Holm op->kblocks = 0;
157*8a272653SPeter Holm op->inodes = 0;
158*8a272653SPeter Holm
159*8a272653SPeter Holm environment();
160*8a272653SPeter Holm
161*8a272653SPeter Holm while ((ch = getopt(argc, argv, "t:l:i:d:hknv")) != -1)
162*8a272653SPeter Holm switch(ch) {
163*8a272653SPeter Holm case 't': /* run time */
164*8a272653SPeter Holm op->run_time = time2sec(optarg);
165*8a272653SPeter Holm break;
166*8a272653SPeter Holm case 'l': /* load factor in pct */
167*8a272653SPeter Holm if (sscanf(optarg, "%d", &op->load) != 1)
168*8a272653SPeter Holm usage("-l");
169*8a272653SPeter Holm break;
170*8a272653SPeter Holm case 'i': /* max incarnations */
171*8a272653SPeter Holm if (sscanf(optarg, "%d", &op->incarnations) != 1)
172*8a272653SPeter Holm usage("-i");
173*8a272653SPeter Holm break;
174*8a272653SPeter Holm case 'd': /* working directory */
175*8a272653SPeter Holm op->wd = strdup(optarg);
176*8a272653SPeter Holm break;
177*8a272653SPeter Holm case 'h': /* hog flag */
178*8a272653SPeter Holm op->hog += 1;
179*8a272653SPeter Holm break;
180*8a272653SPeter Holm case 'k': /* kill flag */
181*8a272653SPeter Holm op->kill = 1;
182*8a272653SPeter Holm break;
183*8a272653SPeter Holm case 'n': /* no delay flag */
184*8a272653SPeter Holm op->nodelay = 1;
185*8a272653SPeter Holm break;
186*8a272653SPeter Holm case 'v': /* verbose flag */
187*8a272653SPeter Holm op->verbose += 1;
188*8a272653SPeter Holm break;
189*8a272653SPeter Holm default:
190*8a272653SPeter Holm usage(NULL);
191*8a272653SPeter Holm }
192*8a272653SPeter Holm op->argc = argc -= optind;
193*8a272653SPeter Holm op->argv = argv += optind;
194*8a272653SPeter Holm
195*8a272653SPeter Holm if (op->incarnations < 1)
196*8a272653SPeter Holm op->incarnations = 1;
197*8a272653SPeter Holm if (op->hog == 0)
198*8a272653SPeter Holm op->incarnations = random_int(1, op->incarnations);
199*8a272653SPeter Holm if (op->run_time < 15)
200*8a272653SPeter Holm op->run_time = 15;
201*8a272653SPeter Holm if (op->load < 0 || op->load > 100)
202*8a272653SPeter Holm op->load = 100;
203*8a272653SPeter Holm }
204*8a272653SPeter Holm
205*8a272653SPeter Holm void
show_status(void)206*8a272653SPeter Holm show_status(void)
207*8a272653SPeter Holm {
208*8a272653SPeter Holm char buf[80], pgname[9];
209*8a272653SPeter Holm int days;
210*8a272653SPeter Holm time_t t;
211*8a272653SPeter Holm
212*8a272653SPeter Holm if (op->verbose > 0) {
213*8a272653SPeter Holm strncpy(pgname, getprogname(), sizeof(pgname));
214*8a272653SPeter Holm pgname[8] = 0;
215*8a272653SPeter Holm t = op->run_time;
216*8a272653SPeter Holm days = t / (60 * 60 * 24);
217*8a272653SPeter Holm t = t % (60 * 60 * 24);
218*8a272653SPeter Holm strftime(buf, sizeof(buf), "%T", gmtime(&t));
219*8a272653SPeter Holm printf("%8s: run time %2d+%s, incarnations %3d, load %3d, "
220*8a272653SPeter Holm "verbose %d\n",
221*8a272653SPeter Holm pgname, days, buf, op->incarnations, op->load,
222*8a272653SPeter Holm op->verbose);
223*8a272653SPeter Holm fflush(stdout);
224*8a272653SPeter Holm }
225*8a272653SPeter Holm }
226*8a272653SPeter Holm
227*8a272653SPeter Holm void
rmval(void)228*8a272653SPeter Holm rmval(void)
229*8a272653SPeter Holm {
230*8a272653SPeter Holm if (snprintf(path, sizeof(path), "%s/%s.conf", op->cd,
231*8a272653SPeter Holm getprogname()) < 0)
232*8a272653SPeter Holm err(1, "snprintf path");
233*8a272653SPeter Holm (void) unlink(path);
234*8a272653SPeter Holm }
235*8a272653SPeter Holm
236*8a272653SPeter Holm void
putval(unsigned long v)237*8a272653SPeter Holm putval(unsigned long v)
238*8a272653SPeter Holm {
239*8a272653SPeter Holm char buf[64];
240*8a272653SPeter Holm
241*8a272653SPeter Holm rmval();
242*8a272653SPeter Holm snprintf(buf, sizeof(buf), "%lu", v);
243*8a272653SPeter Holm if (symlink(buf, path) < 0)
244*8a272653SPeter Holm err(1, "symlink(%s, %s)", path, buf);
245*8a272653SPeter Holm }
246*8a272653SPeter Holm
247*8a272653SPeter Holm unsigned long
getval(void)248*8a272653SPeter Holm getval(void)
249*8a272653SPeter Holm {
250*8a272653SPeter Holm int i, n;
251*8a272653SPeter Holm unsigned long val;
252*8a272653SPeter Holm char buf[64];
253*8a272653SPeter Holm
254*8a272653SPeter Holm if ((n = readlink(path, buf, sizeof(buf) -1)) < 0) {
255*8a272653SPeter Holm for (i = 0; i < 60; i++) {
256*8a272653SPeter Holm sleep(1);
257*8a272653SPeter Holm if ((n = readlink(path, buf, sizeof(buf) -1)) > 0)
258*8a272653SPeter Holm break;
259*8a272653SPeter Holm }
260*8a272653SPeter Holm if (n < 0)
261*8a272653SPeter Holm err(1, "readlink(%s). %s:%d", path, __FILE__,
262*8a272653SPeter Holm __LINE__);
263*8a272653SPeter Holm }
264*8a272653SPeter Holm buf[n] = '\0';
265*8a272653SPeter Holm if (sscanf(buf, "%ld", &val) != 1)
266*8a272653SPeter Holm err(1, "sscanf(%s)", buf);
267*8a272653SPeter Holm return val;
268*8a272653SPeter Holm }
269