Whamcloud - gitweb
corrected an error made in setepall in test-framework.sh, which affects
[fs/lustre-release.git] / lustre / tests / createdestroy.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <sys/wait.h>
13 #include <time.h>
14 #include <sys/time.h>
15
16 int thread;
17
18 #define BAD_VERBOSE (-999999999)
19
20 #define difftime(a, b)                                          \
21         ((double)(a)->tv_sec - (b)->tv_sec +                    \
22          ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
23
24 static char *cmdname(char *func)
25 {
26         static char buf[512];
27
28         if (thread) {
29                 sprintf(buf, "%s-%d", func, thread);
30                 return buf;
31         }
32
33         return func;
34 }
35
36 static int be_verbose(int verbose, struct timeval *next_time,
37                       unsigned long num, unsigned long *next_num, int num_total)
38 {
39         struct timeval now;
40
41         if (!verbose)
42                 return 0;
43
44         if (next_time != NULL)
45                 gettimeofday(&now, NULL);
46
47         /* A positive verbosity means to print every X iterations */
48         if (verbose > 0 && (num >= *next_num || num >= num_total)) {
49                 *next_num += verbose;
50                 if (next_time) {
51                         next_time->tv_sec = now.tv_sec - verbose;
52                         next_time->tv_usec = now.tv_usec;
53                 }
54                 return 1;
55         }
56
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;
61                 *next_num = num;
62                 return 1;
63         }
64
65         return 0;
66 }
67
68 static int get_verbose(char *func, const char *arg)
69 {
70         int verbose;
71         char *end;
72
73         if (!arg || arg[0] == 'v')
74                 verbose = 1;
75         else if (arg[0] == 's' || arg[0] == 'q')
76                 verbose = 0;
77         else {
78                 verbose = (int)strtoul(arg, &end, 0);
79                 if (*end) {
80                         fprintf(stderr, "%s: error: bad verbose option '%s'\n",
81                                 func, arg);
82                         return BAD_VERBOSE;
83                 }
84         }
85
86         if (verbose < 0)
87                 printf("Print status every %d seconds\n", -verbose);
88         else if (verbose == 1)
89                 printf("Print status every operation\n");
90         else if (verbose > 1)
91                 printf("Print status every %d operations\n", verbose);
92
93         return verbose;
94 }
95
96 int main(int argc, char *argv[])
97 {
98         char filename[1024];
99         int verbose = 0;
100         unsigned long count, i;
101         int threads = 0;
102         char *end;
103         int rc = 0;
104
105         if (argc < 3 || argc > 5) {
106                 fprintf(stderr,
107                         "usage: %s <filename> <count> [verbose [threads]]\n",
108                         argv[0]);
109                 exit(1);
110         }
111
112         count = strtoul(argv[2], &end, 0);
113         if (*end) {
114                 fprintf(stderr, "%s: error: bad iteration count '%s'\n",
115                         argv[0], argv[1]);
116                 exit(2);
117         }
118         if (argc == 4) {
119                 verbose = get_verbose(argv[0], argv[3]);
120                 if (verbose == BAD_VERBOSE)
121                         exit(2);
122         }
123         if (argc == 5) {
124                 threads = strtoul(argv[4], &end, 0);
125                 if (*end) {
126                         fprintf(stderr, "%s: error: bad thread count '%s'\n",
127                                 argv[0], argv[1]);
128                         exit(2);
129                 }
130         }
131
132         for (i = 1; i <= threads; i++) {
133                 rc = fork();
134                 if (rc < 0) {
135                         fprintf(stderr, "%s: error: #%ld - %s\n",
136                                 cmdname(argv[0]), i, strerror(rc = errno));
137                         break;
138                 } else if (rc == 0) {
139                         thread = i;
140                         break;
141                 } else
142                         printf("%s: thread #%ld (PID %d) started\n",
143                                cmdname(argv[0]), i, rc);
144                 rc = 0;
145         }
146
147         if (threads && thread == 0) {   /* parent process */
148                 int live_threads = threads;
149
150                 while (live_threads > 0) {
151                         int status;
152                         pid_t ret;
153
154                         ret = waitpid(0, &status, 0);
155                         if (ret == 0) {
156                                 continue;
157                         }
158
159                         if (ret < 0) {
160                                 fprintf(stderr, "%s: error: wait - %s\n",
161                                         argv[0], strerror(errno));
162                                 if (!rc)
163                                         rc = errno;
164                         } else {
165                                 /*
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).
170                                  */
171                                 int err = WEXITSTATUS(status);
172                                 if (err || WIFSIGNALED(status))
173                                         fprintf(stderr,
174                                                 "%s: error: PID %d had rc=%d\n",
175                                                 argv[0], ret, err);
176                                 if (!rc)
177                                         rc = err;
178
179                                 live_threads--;
180                         }
181                 }
182         } else {
183                 struct timeval start, end, next_time;
184                 unsigned long next_count;
185                 double diff;
186
187                 gettimeofday(&start, NULL);
188                 next_time.tv_sec = start.tv_sec - verbose;
189                 next_time.tv_usec = start.tv_usec;
190
191                 for (i = 0, next_count = verbose; i < count; i++) {
192                         if (threads)
193                                 sprintf(filename, "%s-%d-%ld",
194                                         argv[1], thread, i);
195                         else
196                                 sprintf(filename, "%s-%ld", argv[1], i);
197
198                         rc = mknod(filename, S_IFREG, 0);
199                         if (rc < 0) {
200                                 fprintf(stderr, "%s: error: mknod(%s): %s\n",
201                                         cmdname(argv[0]), filename,
202                                         strerror(errno));
203                                 rc = errno;
204                                 break;
205                         }
206                         if (unlink(filename) < 0) {
207                                 fprintf(stderr, "%s: error: unlink(%s): %s\n",
208                                         cmdname(argv[0]), filename,
209                                         strerror(errno));
210                                 rc = errno;
211                                 break;
212                         }
213                         if (be_verbose(verbose, &next_time,i,&next_count,count))
214                                 printf("%s: number %ld\n", cmdname(argv[0]), i);
215                 }
216
217                 gettimeofday(&end, NULL);
218                 diff = difftime(&end, &start);
219
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));
223         }
224         return rc;
225 }