Bring in FreeBSD's stress2 stress testing suite.
[dragonfly.git] / test / stress / stress2 / lib / main.c
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 /* Main program for all test programs */
29
30 #include <stdio.h>
31 #include <signal.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <sys/wait.h>
39 #include <sys/stat.h>
40
41 #include "stress.h"
42
43 static char const rcsid[] = "$Name:  $ $FreeBSD$";
44
45 volatile int done_testing;
46 static int cleanupcalled = 0;
47 char *home;
48
49 static  pid_t *r;
50
51 void
52 handler(int i)
53 {
54         int j;
55
56         done_testing = 1;
57         for (j = 0; j < op->incarnations; j++) {
58                 if (op->verbose > 2)
59                         printf("handler: kill -HUP %d\n", r[j]);
60                 if (r[j] != 0 && kill(r[j], SIGHUP) == -1)
61                         if (errno != ESRCH)
62                                 warn("kill(%d, SIGHUP), %s:%d", r[j], __FILE__, __LINE__);
63         }
64         if (op->kill == 1) {
65                 sleep(5);
66                 /* test programs may have blocked for the SIGHUP, so try harder */
67                 for (j = 0; j < op->incarnations; j++) {
68                         if (op->verbose > 2)
69                                 printf("handler: kill -KILL %d\n", r[j]);
70                         if (r[j] != 0)
71                                 (void) kill(r[j], SIGKILL);
72                 }
73         }
74 }
75
76 void
77 run_test_handler(int i)
78 {
79
80         done_testing = 1;
81 }
82
83 void
84 exit_handler(int i)
85 {
86
87         exit(1);
88 }
89
90 void
91 callcleanup(void)
92 {
93         if (cleanupcalled == 0)
94                 cleanup();
95         cleanupcalled = 1;
96 }
97
98 static void
99 run_tests(int i)
100 {
101         time_t start;
102
103         signal(SIGHUP, run_test_handler);
104         signal(SIGINT, exit_handler);
105         atexit(callcleanup);
106         setup(i);
107         if ((strcmp(getprogname(), "run") != 0) && (op->nodelay == 0))
108                 sleep(random_int(1,10));
109         start = time(NULL);
110         while (done_testing == 0 &&
111                         (time(NULL) - start) < op->run_time) {
112                 test();
113         }
114         callcleanup();
115         exit(EXIT_SUCCESS);
116 }
117
118 static void
119 run_incarnations(void)
120 {
121         int i;
122         int s;
123
124         signal(SIGHUP, handler);
125         for (i = 0; i < op->incarnations && done_testing == 0; i++) {
126                 if ((r[i] = fork()) == 0) {
127                         run_tests(i);
128                 }
129                 if (r[i] < 0) {
130                         warn("fork(), %s:%d", __FILE__, __LINE__);
131                         r[i] = 0;
132                         break;
133                 }
134         }
135         for (i = 0; i < op->incarnations; i++)
136                 if (r[i] != 0 && waitpid(r[i], &s, 0) == -1)
137                         warn("waitpid(%d), %s:%d", r[i], __FILE__, __LINE__);
138
139         exit(EXIT_SUCCESS);
140 }
141
142 static int
143 run_test(void)
144 {
145         pid_t p;
146         time_t start;
147         int status = 0;
148
149         if (random_int(1,100) > op->load)
150                 return (status);
151
152         show_status();
153
154         start = time(NULL);
155         done_testing = 0;
156         fflush(stdout);
157         rmval();
158         p = fork();
159         if (p == 0)
160                 run_incarnations();
161         if (p < 0)
162                 err(1, "fork() in %s:%d", __FILE__, __LINE__);
163         while (done_testing != 1 &&
164                         (time(NULL) - start) < op->run_time)
165                 sleep(1);
166         if (kill(p, SIGHUP) == -1)
167                 warn("kill(%d, SIGHUP), %s:%d", p, __FILE__, __LINE__);
168
169         if (waitpid(p, &status, 0) == -1)
170                 err(1, "waitpid(%d), %s:%d", p, __FILE__, __LINE__);
171
172         return (status);
173 }
174
175 int
176 main(int argc, char **argv)
177 {
178         struct stat sb;
179         int status = 0;
180
181         options(argc, argv);
182
183         umask(0);
184         if (stat(op->wd, &sb) == -1) {
185                 if (mkdir(op->wd, 0770) == -1)
186                         if (errno != EEXIST)
187                                 err(1, "mkdir(%s) %s:%d", op->wd, __FILE__, __LINE__);
188         }
189         if (stat(op->cd, &sb) == -1) {
190                 if (mkdir(op->cd, 0770) == -1)
191                         if (errno != EEXIST)
192                                 err(1, "mkdir(%s) %s:%d", op->cd, __FILE__, __LINE__);
193         }
194         if ((home = getcwd(NULL, 0)) == NULL)
195                 err(1, "getcwd(), %s:%d",  __FILE__, __LINE__);
196         if (chdir(op->wd) == -1)
197                 err(1, "chdir(%s) %s:%d", op->wd, __FILE__, __LINE__);
198
199         r = (pid_t *)calloc(1, op->incarnations * sizeof(pid_t));
200
201         status = run_test();
202
203         return (status);
204 }