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 */
49 (next_num == NULL || num >= *next_num || num >= num_total)) {
52 next_time->tv_sec = now.tv_sec - verbose;
53 next_time->tv_usec = now.tv_usec;
58 /* A negative verbosity means to print at most each X seconds */
59 if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0){
60 next_time->tv_sec = now.tv_sec - verbose;
61 next_time->tv_usec = now.tv_usec;
70 static int get_verbose(char *func, const char *arg)
75 if (!arg || arg[0] == 'v')
77 else if (arg[0] == 's' || arg[0] == 'q')
80 verbose = (int)strtoul(arg, &end, 0);
82 fprintf(stderr, "%s: error: bad verbose option '%s'\n",
89 printf("Print status every %d seconds\n", -verbose);
90 else if (verbose == 1)
91 printf("Print status every operation\n");
93 printf("Print status every %d operations\n", verbose);
98 int main(int argc, char *argv[])
102 unsigned long count, i;
107 if (argc < 3 || argc > 5) {
109 "usage: %s <filename> <count> [verbose [threads]]\n",
114 count = strtoul(argv[2], &end, 0);
116 fprintf(stderr, "%s: error: bad iteration count '%s'\n",
121 verbose = get_verbose(argv[0], argv[3]);
122 if (verbose == BAD_VERBOSE)
126 threads = strtoul(argv[4], &end, 0);
128 fprintf(stderr, "%s: error: bad thread count '%s'\n",
134 for (i = 1; i <= threads; i++) {
137 fprintf(stderr, "%s: error: #%ld - %s\n",
138 cmdname(argv[0]), i, strerror(rc = errno));
140 } else if (rc == 0) {
144 printf("%s: thread #%ld (PID %d) started\n",
145 cmdname(argv[0]), i, rc);
149 if (threads && thread == 0) { /* parent process */
150 int live_threads = threads;
152 while (live_threads > 0) {
156 ret = waitpid(0, &status, 0);
162 fprintf(stderr, "%s: error: wait - %s\n",
163 argv[0], strerror(errno));
168 * This is a hack. We _should_ be able to use
169 * WIFEXITED(status) to see if there was an
170 * error, but it appears to be broken and it
171 * always returns 1 (OK). See wait(2).
173 int err = WEXITSTATUS(status);
174 if (err || WIFSIGNALED(status))
176 "%s: error: PID %d had rc=%d\n",
185 struct timeval start, end, next_time;
186 unsigned long next_count;
189 gettimeofday(&start, NULL);
190 next_time.tv_sec = start.tv_sec - verbose;
191 next_time.tv_usec = start.tv_usec;
193 for (i = 0, next_count = verbose; i < count; i++) {
195 sprintf(filename, "%s-%d-%ld",
198 sprintf(filename, "%s-%ld", argv[1], i);
200 rc = mknod(filename, S_IFREG, 0);
202 fprintf(stderr, "%s: error: mknod(%s): %s\n",
203 cmdname(argv[0]), filename,
208 if (unlink(filename) < 0) {
209 fprintf(stderr, "%s: error: unlink(%s): %s\n",
210 cmdname(argv[0]), filename,
215 if (be_verbose(verbose, &next_time,i,&next_count,count))
216 printf("%s: number %ld\n", cmdname(argv[0]), i);
219 gettimeofday(&end, NULL);
220 diff = difftime(&end, &start);
222 printf("%s: %ldx2 files in %.4gs (%.4g ops/s): rc = %d: %s",
223 cmdname(argv[0]), i, diff, (double)i * 2 / diff,
224 rc, ctime(&end.tv_sec));