1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
18 #define difftime(a, b) \
19 ((double)(a)->tv_sec - (b)->tv_sec + \
20 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
28 void usage(char *name)
30 fprintf(stderr, "usage: %s [opts] <dirname> <seconds> <threads>\n",
32 fprintf(stderr, " -q quiet\n");
33 fprintf(stderr, " -a abort other children on first err\n");
40 struct kid_list_t *next;
43 struct kid_list_t *head = NULL;
45 int push_kid(pid_t kid)
47 struct kid_list_t *new;
48 new = (struct kid_list_t *)malloc(sizeof(struct kid_list_t));
61 kill(head->kid, SIGTERM);
66 static int usr1_received;
67 void usr1_handler(int unused)
73 int wait_for_threads(int live_threads)
77 while (live_threads > 0) {
81 ret = waitpid(0, &status, 0);
87 fprintf(stderr, "%s: error: wait - %s\n",
88 cmdname, strerror(errno));
93 * This is a hack. We _should_ be able to use
94 * WIFEXITED(status) to see if there was an
95 * error, but it appears to be broken and it
96 * always returns 1 (OK). See wait(2).
98 int err = WEXITSTATUS(status);
101 "%s: error: PID %d had rc=%d\n",
103 /* Record first error */
107 /* Give up on first error */
117 printf("%s done, rc = %d\n", cmdname, rc);
121 void print_err(char *op, char *filename, struct timeval *time, int err)
123 fprintf(stderr, "%s: %d.%.06d error: %s(%s): %s\n",
124 cmdname, (int)(time->tv_sec), (int)(time->tv_usec), op,
125 filename, strerror(errno));
128 int run_one_child(char *file, int thread, int seconds)
130 struct timeval start, cur;
134 int fd, rc = 0, rand, maxrand, len;
135 long nfiles = 0, nbytes = 0;
138 printf("%s: running thread #%d\n", cmdname, thread);
141 /* Higher thread numbers will produce bigger random files.
142 Thread 1 will produce only 0-len files. */
143 maxrand = 1; rand = thread;
147 gettimeofday(&start, NULL);
153 gettimeofday(&cur, NULL);
155 if (cur.tv_sec > (start.tv_sec + seconds))
159 sprintf(filename, "%s-%d-%ld", file, thread, nfiles);
161 fd = open(filename, O_RDWR | O_CREAT, 0666);
163 print_err("open", filename, &cur, errno);
168 sprintf(buf, "%s %010ld %.19s.%012d\n", cmdname,
169 nfiles++, ctime(&cur.tv_sec), (int)cur.tv_usec);
172 rand = random() % maxrand;
174 if (write(fd, buf, len) != len) {
175 print_err("write", filename, &cur, errno);
183 print_err("close", filename, &cur, errno);
187 if (unlink(filename) < 0) {
188 print_err("unlink", filename, &cur, errno);
189 if (errno == ENOENT) {
190 printf("Ignoring known bug 6082\n");
198 diff = difftime(&cur, &start);
200 printf("%s: %7ld files, %4ld MB in %.2fs (%7.2f files/s, "
201 "%5.2f MB/s): rc = %d\n",
202 cmdname, nfiles, nbytes >> 20, diff,
203 (double)nfiles / diff, (double)nbytes/1024/1024 / diff,
209 int main(int argc, char *argv[])
211 unsigned long duration;
217 sprintf(cmdname, "%s", argv[0]);
219 while((i < argc) && (argv[i][0] == '-')) {
220 switch (argv[i][1]) {
235 duration = strtoul(argv[++i], &end, 0);
237 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
242 threads = strtoul(argv[++i], &end, 0);
244 fprintf(stderr, "%s: error: bad thread count '%s'\n",
249 signal(SIGUSR1, usr1_handler);
251 for (i = 1; i <= threads; i++) {
255 fprintf(stderr, "%s: error: #%d - %s\n",
256 cmdname, i, strerror(rc = errno));
261 sprintf(cmdname, "%s-%d", argv[0], i);
262 return (run_one_child(directory, i, duration));
274 printf("%s will run for %ld minutes\n", cmdname, duration/60);
275 return (wait_for_threads(threads));