4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
28 * Lustre is a trademark of Sun Microsystems, Inc.
34 #include <sys/types.h>
48 void usage(char *name)
50 fprintf(stderr, "usage: %s [opts] <dirname> <seconds> <threads>\n",
52 fprintf(stderr, " -q quiet\n");
53 fprintf(stderr, " -a abort other children on first err\n");
59 struct kid_list_t *next;
62 struct kid_list_t *head;
64 int push_kid(pid_t kid)
66 struct kid_list_t *new;
68 new = (struct kid_list_t *)malloc(sizeof(struct kid_list_t));
81 kill(head->kid, SIGTERM);
86 static int usr1_received;
87 void usr1_handler(int unused)
93 int wait_for_threads(int live_threads)
97 while (live_threads > 0) {
101 ret = waitpid(0, &status, 0);
106 fprintf(stderr, "%s: error: wait - %s\n",
107 cmdname, strerror(errno));
112 * This is a hack. We _should_ be able to use
113 * WIFEXITED(status) to see if there was an
114 * error, but it appears to be broken and it
115 * always returns 1 (OK). See wait(2).
117 int err = WEXITSTATUS(status);
121 "%s: error: PID %d had rc=%d\n",
123 /* Record first error */
127 /* Give up on first error */
137 printf("%s done, rc = %d\n", cmdname, rc);
141 void print_err(char *op, char *filename, struct timeval *time, int err)
143 fprintf(stderr, "%s: %d.%.06d error: %s(%s): %s\n",
144 cmdname, (int)(time->tv_sec), (int)(time->tv_usec), op,
145 filename, strerror(errno));
148 int run_one_child(char *file, int thread, int seconds)
150 struct timeval start, cur;
154 int fd, rc = 0, rand, maxrand, len;
155 long nfiles = 0, nbytes = 0;
158 printf("%s: running thread #%d\n", cmdname, thread);
162 * Higher thread numbers will produce bigger random files.
163 * Thread 1 will produce only 0-len files.
165 maxrand = 1; rand = thread;
169 gettimeofday(&start, NULL);
176 gettimeofday(&cur, NULL);
178 if (cur.tv_sec > (start.tv_sec + seconds))
182 snprintf(filename, sizeof(filename), "%s-%d-%ld",
183 file, thread, nfiles);
185 fd = open(filename, O_RDWR | O_CREAT, 0666);
187 print_err("open", filename, &cur, errno);
192 sprintf(buf, "%s %010ld %.19s.%012d\n", cmdname,
193 nfiles++, ctime(&cur.tv_sec), (int)cur.tv_usec);
196 rand = random() % maxrand;
198 if (write(fd, buf, len) != len) {
199 print_err("write", filename, &cur, errno);
207 print_err("close", filename, &cur, errno);
211 if (unlink(filename) < 0) {
212 print_err("unlink", filename, &cur, errno);
213 if (errno == ENOENT) {
214 printf("Ignoring known bug 6082\n");
222 diff = difftime(cur.tv_sec, start.tv_sec);
224 printf("%s: %7ld files, %4ld MB in %.2fs (%7.2f files/s, %5.2f MB/s): rc = %d\n",
225 cmdname, nfiles, nbytes >> 20, diff,
226 diff == 0 ? (double)0 : (double)nfiles / diff,
227 diff == 0 ? (double)0 : (double)nbytes / 1024 / 1024 /
233 int main(int argc, char *argv[])
235 unsigned long duration;
241 snprintf(cmdname, sizeof(cmdname), "%s", argv[0]);
243 while ((i < argc) && (argv[i][0] == '-')) {
244 switch (argv[i][1]) {
259 duration = strtoul(argv[++i], &end, 0);
261 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
266 threads = strtoul(argv[++i], &end, 0);
268 fprintf(stderr, "%s: error: bad thread count '%s'\n",
273 signal(SIGUSR1, usr1_handler);
275 for (i = 1; i <= threads; i++) {
279 fprintf(stderr, "%s: error: #%d - %s\n",
280 cmdname, i, strerror(rc = errno));
285 snprintf(cmdname, sizeof(cmdname), "%s-%d", argv[0], i);
286 return run_one_child(directory, i, duration);
297 printf("%s will run for %ld minutes\n", cmdname, duration / 60);
298 return wait_for_threads(threads);