Whamcloud - gitweb
b=19139
[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 <sys/time.h>
41 #include <time.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <getopt.h>
48
49 static void usage(char *prog)
50 {
51         printf("usage: %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt count\n", prog);
52         printf("       %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt ] -seconds\n", prog);
53         printf("       %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt start count\n", prog);
54         exit(EXIT_FAILURE);
55 }
56
57 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
58 {
59         static char filename[4096];
60         int bytes;
61
62         bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
63                 snprintf(filename, 4095, "%s%ld", fmt, n);
64         if (bytes >= 4095) {
65                 printf("file name too long\n");
66                 exit(EXIT_FAILURE);
67         }
68         return filename;
69 }
70
71 double now(void)
72 {
73         struct timeval tv;
74         gettimeofday(&tv, NULL);
75         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
76 }
77
78 int main(int argc, char ** argv)
79 {
80         long i;
81         int rc = 0, do_open = 0, do_link = 0, do_mkdir = 0;
82         int do_unlink = 0, do_mknod = 0;
83         char *filename;
84         char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
85         double start, last;
86         long begin = 0, end = ~0UL >> 1, count = ~0UL >> 1;
87         int c, has_fmt_spec = 0, unlink_has_fmt_spec = 0;
88
89         /* Handle the last argument in form of "-seconds" */
90         if (argc > 1 && argv[argc - 1][0] == '-') {
91                 char *endp;
92
93                 argc--;
94                 end = strtol(argv[argc] + 1, &endp, 0);
95                 if (end <= 0 || *endp != '\0')
96                         usage(argv[0]);
97                 end = end + time(NULL);
98         }
99
100         while ((c = getopt(argc, argv, "omdl:r:")) != -1) {
101                 switch(c) {
102                 case 'o':
103                         do_open++;
104                         break;
105                 case 'm':
106                         do_mknod++;
107                         break;
108                 case 'd':
109                         do_mkdir++;
110                         break;
111                 case 'l':
112                         do_link++;
113                         tgt = optarg;
114                         break;
115                 case 'r':
116                         do_unlink++;
117                         fmt_unlink = optarg;
118                         break;
119                 case '?':
120                         printf("Unknown option '%c'\n", optopt);
121                         usage(argv[0]);
122                 }
123         }
124
125         if (do_open + do_mkdir + do_link + do_mknod != 1 ||
126             do_unlink > 1)
127                 usage(argv[0]);
128
129         switch (argc - optind) {
130         case 3:
131                 begin = strtol(argv[argc - 2], NULL, 0);
132         case 2:
133                 count = strtol(argv[argc - 1], NULL, 0);
134                 if (end != ~0UL >> 1)
135                         usage(argv[0]);
136         case 1:
137                 fmt = argv[optind];
138                 break;
139         default:
140                 usage(argv[0]);
141         }
142
143         start = last = now();
144
145         has_fmt_spec = strchr(fmt, '%') != NULL;
146         if (do_unlink)
147                 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
148
149         for (i = 0; i < count && time(NULL) < end; i++, begin++) {
150                 filename = get_file_name(fmt, begin, has_fmt_spec);
151                 if (do_open) {
152                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
153                         if (fd < 0) {
154                                 printf("open(%s) error: %s\n", filename,
155                                        strerror(errno));
156                                 rc = errno;
157                                 break;
158                         }
159                         close(fd);
160                 } else if (do_link) {
161                         rc = link(tgt, filename);
162                         if (rc) {
163                                 printf("link(%s, %s) error: %s\n",
164                                        tgt, filename, strerror(errno));
165                                 rc = errno;
166                                 break;
167                         }
168                 } else if (do_mkdir) {
169                         rc = mkdir(filename, 0755);
170                         if (rc) {
171                                 printf("mkdir(%s) error: %s\n",
172                                        filename, strerror(errno));
173                                 rc = errno;
174                                 break;
175                         }
176                 } else {
177                         rc = mknod(filename, S_IFREG| 0444, 0);
178                         if (rc) {
179                                 printf("mknod(%s) error: %s\n",
180                                        filename, strerror(errno));
181                                 rc = errno;
182                                 break;
183                         }
184                 }
185                 if (do_unlink) {
186                         filename = get_file_name(fmt_unlink, begin,
187                                       unlink_has_fmt_spec);
188                         rc = do_mkdir ? rmdir(filename) : unlink(filename);
189                         if (rc) {
190                                 printf("unlink(%s) error: %s\n",
191                                        filename, strerror(errno));
192                                 rc = errno;
193                                 break;
194                         }
195                 }
196
197                 if (i && (i % 10000) == 0) {
198                         printf(" - created %ld (time %.2f total %.2f last %.2f)"
199                                "\n", i, now(), now() - start, now() - last);
200                         last = now();
201                 }
202         }
203         printf("total: %ld creates%s in %.2f seconds: %.2f creates/second\n", i,
204                do_unlink ? "/deletions" : "",
205                now() - start, ((double)i / (now() - start)));
206
207         return rc;
208 }