1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
18 #define BAD_VERBOSE (-999999999)
20 #define difftime(a, b) \
21 ((double)(a)->tv_sec - (b)->tv_sec + \
22 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
24 static char *cmdname(char *func)
29 sprintf(buf, "%s-%d", func, thread);
36 static int be_verbose(int verbose, struct timeval *next_time,
37 unsigned long num, unsigned long *next_num, int num_total)
44 if (next_time != NULL)
45 gettimeofday(&now, NULL);
47 /* A positive verbosity means to print every X iterations */
48 if (verbose > 0 && (num >= *next_num || num >= num_total)) {
51 next_time->tv_sec = now.tv_sec - verbose;
52 next_time->tv_usec = now.tv_usec;
57 /* A negative verbosity means to print at most each X seconds */
58 if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0){
59 next_time->tv_sec = now.tv_sec - verbose;
60 next_time->tv_usec = now.tv_usec;
68 static int get_verbose(char *func, const char *arg)
73 if (!arg || arg[0] == 'v')
75 else if (arg[0] == 's' || arg[0] == 'q')
78 verbose = (int)strtoul(arg, &end, 0);
80 fprintf(stderr, "%s: error: bad verbose option '%s'\n",
87 printf("Print status every %d seconds\n", -verbose);
88 else if (verbose == 1)
89 printf("Print status every operation\n");
91 printf("Print status every %d operations\n", verbose);
96 int main(int argc, char *argv[])
100 unsigned long count, i;
105 if (argc < 3 || argc > 5) {
107 "usage: %s <filename> <count> [verbose [threads]]\n",
112 count = strtoul(argv[2], &end, 0);
114 fprintf(stderr, "%s: error: bad iteration count '%s'\n",
119 verbose = get_verbose(argv[0], argv[3]);
120 if (verbose == BAD_VERBOSE)
124 threads = strtoul(argv[4], &end, 0);
126 fprintf(stderr, "%s: error: bad thread count '%s'\n",
132 for (i = 1; i <= threads; i++) {
135 fprintf(stderr, "%s: error: #%ld - %s\n",
136 cmdname(argv[0]), i, strerror(rc = errno));
138 } else if (rc == 0) {
142 printf("%s: thread #%ld (PID %d) started\n",
143 cmdname(argv[0]), i, rc);
147 if (threads && thread == 0) { /* parent process */
148 int live_threads = threads;
150 while (live_threads > 0) {
154 ret = waitpid(0, &status, 0);
160 fprintf(stderr, "%s: error: wait - %s\n",
161 argv[0], strerror(errno));
166 * This is a hack. We _should_ be able to use
167 * WIFEXITED(status) to see if there was an
168 * error, but it appears to be broken and it
169 * always returns 1 (OK). See wait(2).
171 int err = WEXITSTATUS(status);
172 if (err || WIFSIGNALED(status))
174 "%s: error: PID %d had rc=%d\n",
183 struct timeval start, end, next_time;
184 unsigned long next_count;
187 gettimeofday(&start, NULL);
188 next_time.tv_sec = start.tv_sec - verbose;
189 next_time.tv_usec = start.tv_usec;
191 for (i = 0, next_count = verbose; i < count; i++) {
193 sprintf(filename, "%s-%d-%ld",
196 sprintf(filename, "%s-%ld", argv[1], i);
198 rc = mknod(filename, S_IFREG, 0);
200 fprintf(stderr, "%s: error: mknod(%s): %s\n",
201 cmdname(argv[0]), filename,
206 if (unlink(filename) < 0) {
207 fprintf(stderr, "%s: error: unlink(%s): %s\n",
208 cmdname(argv[0]), filename,
213 if (be_verbose(verbose, &next_time,i,&next_count,count))
214 printf("%s: number %ld\n", cmdname(argv[0]), i);
217 gettimeofday(&end, NULL);
218 diff = difftime(&end, &start);
220 printf("%s: %ldx2 files in %.4gs (%.4g ops/s): rc = %d: %s",
221 cmdname(argv[0]), i, diff, (double)i * 2 / diff,
222 rc, ctime(&end.tv_sec));