Whamcloud - gitweb
b=17747
[fs/lustre-release.git] / lustre / tests / createmany.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <time.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <getopt.h>
47
48 static void usage(char *prog)
49 {
50         printf("usage: %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt count\n", prog);
51         printf("       %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt ] -seconds\n", prog);
52         printf("       %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt start count\n", prog);
53         exit(EXIT_FAILURE);
54 }
55
56 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
57 {
58         static char filename[4096];
59         int bytes;
60
61         bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
62                 snprintf(filename, 4095, "%s%ld", fmt, n);
63         if (bytes >= 4095) {
64                 printf("file name too long\n");
65                 exit(EXIT_FAILURE);
66         }
67         return filename;
68 }
69
70 int main(int argc, char ** argv)
71 {
72         long i;
73         int rc = 0, do_open = 0, do_link = 0, do_mkdir = 0;
74         int do_unlink = 0, do_mknod = 0;
75         char *filename;
76         char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
77         long start, last, end = ~0UL >> 1;
78         long begin = 0, count = ~0UL >> 1;
79         int c, has_fmt_spec = 0, unlink_has_fmt_spec = 0;
80
81         /* Handle the last argument in form of "-seconds" */
82         if (argc > 1 && argv[argc - 1][0] == '-') {
83                 char *endp;
84
85                 argc--;
86                 end = strtol(argv[argc] + 1, &endp, 0);
87                 if (end <= 0 || *endp != '\0')
88                         usage(argv[0]);
89                 end = end + time(NULL);
90         }
91
92         while ((c = getopt(argc, argv, "omdl:r:")) != -1) {
93                 switch(c) {
94                 case 'o':
95                         do_open++;
96                         break;
97                 case 'm':
98                         do_mknod++;
99                         break;
100                 case 'd':
101                         do_mkdir++;
102                         break;
103                 case 'l':
104                         do_link++;
105                         tgt = optarg;
106                         break;
107                 case 'r':
108                         do_unlink++;
109                         fmt_unlink = optarg;
110                         break;
111                 case '?':
112                         printf("Unknown option '%c'\n", optopt);
113                         usage(argv[0]);
114                 }
115         }
116
117         if (do_open + do_mkdir + do_link + do_mknod != 1 ||
118             do_unlink > 1)
119                 usage(argv[0]);
120
121         switch (argc - optind) {
122         case 3:
123                 begin = strtol(argv[argc - 2], NULL, 0);
124         case 2:
125                 count = strtol(argv[argc - 1], NULL, 0);
126                 if (end != ~0UL >> 1)
127                         usage(argv[0]);
128         case 1:
129                 fmt = argv[optind];
130                 break;
131         default:
132                 usage(argv[0]);
133         }
134
135         start = last = time(NULL);
136
137         has_fmt_spec = strchr(fmt, '%') != NULL;
138         if (do_unlink)
139                 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
140
141         for (i = 0; i < count && time(NULL) < end; i++, begin++) {
142                 filename = get_file_name(fmt, begin, has_fmt_spec);
143                 if (do_open) {
144                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
145                         if (fd < 0) {
146                                 printf("open(%s) error: %s\n", filename,
147                                        strerror(errno));
148                                 rc = errno;
149                                 break;
150                         }
151                         close(fd);
152                 } else if (do_link) {
153                         rc = link(tgt, filename);
154                         if (rc) {
155                                 printf("link(%s, %s) error: %s\n",
156                                        tgt, filename, strerror(errno));
157                                 rc = errno;
158                                 break;
159                         }
160                 } else if (do_mkdir) {
161                         rc = mkdir(filename, 0755);
162                         if (rc) {
163                                 printf("mkdir(%s) error: %s\n",
164                                        filename, strerror(errno));
165                                 rc = errno;
166                                 break;
167                         }
168                 } else {
169                         rc = mknod(filename, S_IFREG| 0444, 0);
170                         if (rc) {
171                                 printf("mknod(%s) error: %s\n",
172                                        filename, strerror(errno));
173                                 rc = errno;
174                                 break;
175                         }
176                 }
177                 if (do_unlink) {
178                         filename = get_file_name(fmt_unlink, begin,
179                                       unlink_has_fmt_spec);
180                         rc = do_mkdir ? rmdir(filename) : unlink(filename);
181                         if (rc) {
182                                 printf("unlink(%s) error: %s\n",
183                                        filename, strerror(errno));
184                                 rc = errno;
185                                 break;
186                         }
187                 }
188
189                 if ((i % 10000) == 0) {
190                         printf(" - created %ld (time %ld total %ld last %ld)\n",
191                                i, time(0), time(0) - start, time(0) - last);
192                         last = time(NULL);
193                 }
194         }
195         printf("total: %ld creates%s in %ld seconds: %f creates/second\n", i,
196                do_unlink ? "/deletions" : "",
197                time(NULL) - start, ((float)i / (time(0) - start)));
198
199         return rc;
200 }