Whamcloud - gitweb
Program to create/destroy many files.
[fs/lustre-release.git] / lustre / tests / createdestroy.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/wait.h>
10
11 int main(int argc, char *argv[])
12 {
13         char filename[1024];
14         unsigned long count, i;
15         int thread = 0;
16         int threads = 0;
17         int rc;
18
19         if (argc < 3 || argc > 4) {
20                 fprintf(stderr, "usage: %s <filename> <iterations> [threads]\n",
21                         argv[0]);
22                 exit(1);
23         }
24
25         count = strtoul(argv[2], NULL, 0);
26         if (argc == 4)
27                 threads = strtoul(argv[3], NULL, 0);
28
29         for (i = 1; i <= threads; i++) {
30                 rc = fork();
31                 if (rc < 0) {
32                         fprintf(stderr, "error: %s: #%ld - %s\n", argv[0], i,
33                                 strerror(rc = errno));
34                         break;
35                 } else if (rc == 0) {
36                         thread = i;
37                         argv[2] = "--device";
38                         break;
39                 } else
40                         printf("%s: thread #%ld (PID %d) started\n",
41                                argv[0], i, rc);
42                 rc = 0;
43         }
44
45         if (threads && thread == 0) {   /* parent process */
46                 int live_threads = threads;
47
48                 while (live_threads > 0) {
49                         int status;
50                         pid_t ret;
51
52                         ret = waitpid(0, &status, 0);
53                         if (ret == 0) {
54                                 continue;
55                         }
56
57                         if (ret < 0) {
58                                 fprintf(stderr, "error: %s: wait - %s\n",
59                                         argv[0], strerror(errno));
60                                 if (!rc)
61                                         rc = errno;
62                         } else {
63                                 /*
64                                  * This is a hack.  We _should_ be able to use
65                                  * WIFEXITED(status) to see if there was an
66                                  * error, but it appears to be broken and it
67                                  * always returns 1 (OK).  See wait(2).
68                                  */
69                                 int err = WEXITSTATUS(status);
70                                 if (err || WIFSIGNALED(status))
71                                         fprintf(stderr,
72                                                 "%s: PID %d had rc=%d\n",
73                                                 argv[0], ret, err);
74                                 if (!rc)
75                                         rc = err;
76
77                                 live_threads--;
78                         }
79                 }
80         } else {
81                 for (i = 0; i < count; i++) {
82                         if (threads)
83                                 sprintf(filename, "%s-%d-%ld",
84                                         argv[1], thread, i);
85                         else
86                                 sprintf(filename, "%s-%ld", argv[1], i);
87
88                         rc = mknod(filename, S_IFREG, 0);
89                         if (rc < 0) {
90                                 fprintf(stderr, "mknod(%s): %s\n",
91                                         filename, strerror(errno));
92                                 rc = errno;
93                                 break;
94                         }
95                         if (unlink(filename) < 0) {
96                                 fprintf(stderr, "unlink(%s): %s\n",
97                                         filename, strerror(errno));
98                                 rc = errno;
99                                 break;
100                         }
101                 }
102                 if (threads)
103                         printf("Thread %d done: rc = %d\n", thread, rc);
104                 else
105                         printf("Done: rc = %d\n", rc);
106         }
107         return rc;
108 }