1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
40 #include <sys/types.h>
51 #define difftime(a, b) \
52 ((double)(a)->tv_sec - (b)->tv_sec + \
53 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
61 void usage(char *name)
63 fprintf(stderr, "usage: %s [opts] <dirname> <seconds> <threads>\n",
65 fprintf(stderr, " -q quiet\n");
66 fprintf(stderr, " -a abort other children on first err\n");
73 struct kid_list_t *next;
76 struct kid_list_t *head = NULL;
78 int push_kid(pid_t kid)
80 struct kid_list_t *new;
81 new = (struct kid_list_t *)malloc(sizeof(struct kid_list_t));
94 kill(head->kid, SIGTERM);
99 static int usr1_received;
100 void usr1_handler(int unused)
106 int wait_for_threads(int live_threads)
110 while (live_threads > 0) {
114 ret = waitpid(0, &status, 0);
120 fprintf(stderr, "%s: error: wait - %s\n",
121 cmdname, strerror(errno));
126 * This is a hack. We _should_ be able to use
127 * WIFEXITED(status) to see if there was an
128 * error, but it appears to be broken and it
129 * always returns 1 (OK). See wait(2).
131 int err = WEXITSTATUS(status);
134 "%s: error: PID %d had rc=%d\n",
136 /* Record first error */
140 /* Give up on first error */
150 printf("%s done, rc = %d\n", cmdname, rc);
154 void print_err(char *op, char *filename, struct timeval *time, int err)
156 fprintf(stderr, "%s: %d.%.06d error: %s(%s): %s\n",
157 cmdname, (int)(time->tv_sec), (int)(time->tv_usec), op,
158 filename, strerror(errno));
161 int run_one_child(char *file, int thread, int seconds)
163 struct timeval start, cur;
167 int fd, rc = 0, rand, maxrand, len;
168 long nfiles = 0, nbytes = 0;
171 printf("%s: running thread #%d\n", cmdname, thread);
174 /* Higher thread numbers will produce bigger random files.
175 Thread 1 will produce only 0-len files. */
176 maxrand = 1; rand = thread;
180 gettimeofday(&start, NULL);
187 gettimeofday(&cur, NULL);
189 if (cur.tv_sec > (start.tv_sec + seconds))
193 sprintf(filename, "%s-%d-%ld", file, thread, nfiles);
195 fd = open(filename, O_RDWR | O_CREAT, 0666);
197 print_err("open", filename, &cur, errno);
202 sprintf(buf, "%s %010ld %.19s.%012d\n", cmdname,
203 nfiles++, ctime(&cur.tv_sec), (int)cur.tv_usec);
206 rand = random() % maxrand;
208 if (write(fd, buf, len) != len) {
209 print_err("write", filename, &cur, errno);
217 print_err("close", filename, &cur, errno);
221 if (unlink(filename) < 0) {
222 print_err("unlink", filename, &cur, errno);
223 if (errno == ENOENT) {
224 printf("Ignoring known bug 6082\n");
232 diff = difftime(&cur, &start);
234 printf("%s: %7ld files, %4ld MB in %.2fs (%7.2f files/s, "
235 "%5.2f MB/s): rc = %d\n",
236 cmdname, nfiles, nbytes >> 20, diff,
237 diff == 0 ? (double)0 : (double)nfiles / diff,
238 diff == 0 ? (double)0 : (double)nbytes/1024/1024 / diff,
244 int main(int argc, char *argv[])
246 unsigned long duration;
252 sprintf(cmdname, "%s", argv[0]);
254 while((i < argc) && (argv[i][0] == '-')) {
255 switch (argv[i][1]) {
270 duration = strtoul(argv[++i], &end, 0);
272 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
277 threads = strtoul(argv[++i], &end, 0);
279 fprintf(stderr, "%s: error: bad thread count '%s'\n",
284 signal(SIGUSR1, usr1_handler);
286 for (i = 1; i <= threads; i++) {
290 fprintf(stderr, "%s: error: #%d - %s\n",
291 cmdname, i, strerror(rc = errno));
296 sprintf(cmdname, "%s-%d", argv[0], i);
297 return (run_one_child(directory, i, duration));
309 printf("%s will run for %ld minutes\n", cmdname, duration/60);
310 return (wait_for_threads(threads));