generic: new case to test getcwd(2)
[xfstests-dev.git] / src / dmiperf.c
1 /*
2  * Copyright (c) 2006 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <sys/param.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <dirent.h>
23 #include <malloc.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <attr/attributes.h>
32
33 typedef unsigned int uint_t;
34
35 /*
36  * Loop over directory sizes:
37  *      make m directories
38  *      touch n files in each directory
39  *      write y bytes to all files in each directory
40  *      set DMF attribute on x files in each directory
41  * Change directory sizes by multiplication or addition.
42  * Allow control of starting & stopping sizes, name length, target directory.
43  * Print size and wallclock time (ms per file).
44  * Output can be used to make graphs (gnuplot)
45  */
46
47 static uint_t   addval;
48 static uint_t   dirchars;
49 static char     *directory;
50 static uint_t   firstsize;
51 static uint_t   lastsize;
52 static uint_t   minchars;
53 static double   mulval;
54 static uint_t   nchars;
55 static uint_t   ndirs;
56 static uint_t   pfxchars;
57 static off64_t  fsize;
58 static char     *buffer;
59 static size_t   bsize;
60
61 static int      mkfile(char *, char *);
62 static void     filename(int, int, char *);
63 static int      hexchars(uint_t);
64 static uint_t   nextsize(uint_t);
65 static double   now(void);
66 static void     usage(void);
67
68 /*
69  * Maximum size allowed, this is pretty nuts.
70  * The largest one we've ever built has been about 2 million.
71  */
72 #define MAX_DIR_SIZE    (16 * 1024 * 1024)
73 #define DFL_FIRST_SIZE  1
74 #define DFL_LAST_SIZE   (1024 * 1024)
75 #define MAX_DIR_COUNT   1024
76 #define MIN_DIR_COUNT   1
77
78 #define DMFATTRLEN      22
79 #define DMFATTRNAME     "SGI_DMI_DMFATTR"
80
81 int
82 main(int argc, char **argv)
83 {
84         int             c;
85         uint_t          cursize;
86         int             i;
87         int             j;
88         char            name[NAME_MAX + 1];
89         char            attr[DMFATTRLEN];
90         double          stime;
91
92         while ((c = getopt(argc, argv, "a:b:c:d:f:l:m:n:s:")) != -1) {
93                 switch (c) {
94                 case 'a':
95                         addval = (uint_t)atoi(optarg);
96                         break;
97                 case 'b':
98                         bsize = (size_t)atol(optarg);
99                         break;
100                 case 'c':
101                         nchars = (uint_t)atoi(optarg);
102                         break;
103                 case 'd':
104                         directory = optarg;
105                         break;
106                 case 'f':
107                         firstsize = (uint_t)atoi(optarg);
108                         break;
109                 case 'l':
110                         lastsize = (uint_t)atoi(optarg);
111                         break;
112                 case 'm':
113                         mulval = atof(optarg);
114                         break;
115                 case 'n':
116                         ndirs = (uint_t)atoi(optarg);
117                         break;
118                 case 's':
119                         fsize = (off64_t)atol(optarg);
120                         break;
121                 case '?':
122                 default:
123                         usage();
124                         exit(1);
125                 }
126         }
127         if (!addval && !mulval)
128                 mulval = 2.0;
129         else if ((addval && mulval) || mulval < 0.0) {
130                 usage();
131                 exit(1);
132         }
133         if (!bsize)
134                 bsize = 1024 * 1024;
135         buffer = memalign(getpagesize(), bsize);
136         memset(buffer, 0xfeed, bsize);
137         memset(attr, 0xaaaaaaa, sizeof(attr));
138
139         if (!directory)
140                 directory = ".";
141         else {
142                 if (mkdir(directory, 0777) < 0 && errno != EEXIST) {
143                         perror(directory);
144                         exit(1);
145                 }
146                 if (chdir(directory) < 0) {
147                         perror(directory);
148                         exit(1);
149                 }
150         }
151         if (firstsize == 0)
152                 firstsize = DFL_FIRST_SIZE;
153         else if (firstsize > MAX_DIR_SIZE)
154                 firstsize = MAX_DIR_SIZE;
155         if (lastsize == 0)
156                 lastsize = DFL_LAST_SIZE;
157         else if (lastsize > MAX_DIR_SIZE)
158                 lastsize = MAX_DIR_SIZE;
159         if (lastsize < firstsize)
160                 lastsize = firstsize;
161         minchars = hexchars(lastsize - 1);
162         if (nchars < minchars)
163                 nchars = minchars;
164         else if (nchars >= NAME_MAX + 1)
165                 nchars = NAME_MAX;
166         if (ndirs > MAX_DIR_COUNT)
167                 ndirs = MAX_DIR_COUNT;
168         if (ndirs < MIN_DIR_COUNT)
169                 ndirs = MIN_DIR_COUNT;
170         dirchars = hexchars(ndirs);
171         pfxchars = nchars - minchars;
172         if (pfxchars)
173                 memset(&name[dirchars + 1], 'a', pfxchars);
174
175         cursize = firstsize;
176         for (j = 0; j < ndirs; j++) {
177                 filename(0, j, name);
178                 name[dirchars] = '\0';
179                 mkdir(name, 0777);
180                 stime = now();
181                 for (i = 0; i < cursize; i++) {
182                         filename((i + j) % cursize, j, name);
183                         close(mkfile(name, attr));
184                 }
185                 printf("%d %.3f\n", cursize,
186                         (now() - stime) * 1.0e3 / (cursize * ndirs));
187                 cursize = nextsize(cursize);
188         }
189         return 0;
190 }
191
192 static int
193 mkfile(char *name, char *attr)
194 {
195         int             fd;
196         ssize_t         wrote, wsize;
197         off64_t         bytes = fsize;
198
199         if ((fd = open(name, O_WRONLY | O_CREAT | O_EXCL | O_DIRECT, 0666)) < 0) {
200                 perror("open");
201                 exit(1);
202         }
203         if (attr_setf(fd, DMFATTRNAME, attr, DMFATTRLEN, ATTR_ROOT) < 0) {
204                 perror("attr_setf");
205                 exit(1);
206         }
207         while (bytes > 0) {
208                 wsize = (bsize < bytes) ? bsize : bytes;
209                 if ((wrote = write(fd, buffer, wsize)) < 0) {
210                         perror("write");
211                         exit(1);
212                 }
213                 bytes -= wrote;
214         }
215         return fd;
216 }
217
218 static void
219 filename(int idx, int dir, char *name)
220 {
221         static char     hexc[16] = "0123456789abcdef";
222         int             i;
223
224         for (i = dirchars - 1; i >= 0; i--)
225                 *name++ = hexc[(dir >> (4 * i)) & 0xf];
226         *name++ = '/';
227         name += pfxchars;               /* skip pfx a's */
228         for (i = minchars - 1; i >= 0; i--)
229                 *name++ = hexc[(idx >> (4 * i)) & 0xf];
230         *name = '\0';
231 }
232
233 static int
234 hexchars(uint_t maxval)
235 {
236         if (maxval < 16)
237                 return 1;
238         if (maxval < 16 * 16)
239                 return 2;
240         if (maxval < 16 * 16 * 16)
241                 return 3;
242         if (maxval < 16 * 16 * 16 * 16)
243                 return 4;
244         if (maxval < 16 * 16 * 16 * 16 * 16)
245                 return 5;
246         if (maxval < 16 * 16 * 16 * 16 * 16 * 16)
247                 return 6;
248         if (maxval < 16 * 16 * 16 * 16 * 16 * 16 * 16)
249                 return 7;
250         return 8;
251 }
252
253 static uint_t
254 nextsize(uint_t cursize)
255 {
256         double  n;
257
258         n = cursize;
259         if (addval)
260                 n += addval;
261         else
262                 n *= mulval;
263         if (n > (double)lastsize + 0.5)
264                 return lastsize + 1;    /* i.e. out of bounds */
265         else if ((uint_t)n == cursize)
266                 return cursize + 1;
267         else
268                 return (uint_t)n;
269 }
270
271 static double
272 now(void)
273 {
274         struct timeval  tv;
275
276         gettimeofday(&tv, NULL);
277         return (double)tv.tv_sec + 1.0e-6 * (double)tv.tv_usec;
278 }
279
280 static void
281 usage(void)
282 {
283         fprintf(stderr,
284                 "usage: dirperf [-d dir] [-a addstep | -m mulstep] [-f first] "
285                 "[-l last] [-c nchars] [-n ndirs] [-s size]\n");
286 }