-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathwalk.c
312 lines (278 loc) · 9 KB
/
pathwalk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*****************************************************************************
* Copyright (C) 2012 Lawrence Livermore National Security, LLC.
* Written by Jim Garlick <garlick@llnl.gov> LLNL-CODE-423279
* All Rights Reserved.
*
* This file is part of the Distributed I/O Daemon (diod).
* For details, see http://code.google.com/p/diod/.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see
* http://www.gnu.org/licenses/.
*****************************************************************************/
/* pathwalk.c - time path walk */
#if HAVE_CONFIG_H
#include "config.h"
#else
#define HAVE_GETOPT_H 1
#define HAVE_GETOPT_LONG 1
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <errno.h>
#include <assert.h>
#include <libgen.h>
#include "err.h"
#define OPTIONS "l:f:crtCF"
#if HAVE_GETOPT_LONG
#define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL)
static const struct option longopts[] = {
{"length", required_argument, 0, 'l'},
{"files", required_argument, 0, 'f'},
{"create", no_argument, 0, 'c'},
{"remove", no_argument, 0, 'r'},
{"test", no_argument, 0, 't'},
{"cacheonly", no_argument, 0, 'C'},
{"failsearch", no_argument, 0, 'F'},
{0, 0, 0, 0},
};
#else
#define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt)
#endif
static char **searchpath_create(char *root, int length);
static void searchpath_destroy(char **searchpath, int length);
static void fsobj_create (char **searchpath, int length, int files);
static void fsobj_destroy (char **searchpath, int length, int files);
static int pathwalk (char **searchpath, int length, int files, int target);
char *prog = "unknown";
static void
usage(void)
{
fprintf (stderr,
"Usage: pathwalk root-path [-c|-r|-t] [OPTIONS]\n"
" -l,--length N set number of directories to search dflt: 16)\n"
" -f,--files N set number of files to search for (dflt: 10000)\n"
" -c,--create create files and directories\n"
" -r,--remove remove files and directories\n"
" -t,--test run test\n"
" -C,--primecache run one path search before starting timing\n"
" -F,--failsearch arrange for search to be unsuccessful\n"
);
exit (1);
}
int
main (int argc, char *argv[])
{
char *root = NULL;
int length = 16;
int files = 10000;
int cflag = 0;
int rflag = 0;
int tflag = 0;
int Cflag = 0;
int Fflag = 0;
int c;
char **searchpath;
struct timeval t1, t2, elapsed;
int target, count;
prog = basename (argv[0]);
opterr = 0;
while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) {
switch (c) {
case 'l': /* --length N */
length = strtoul (optarg, NULL, 10);
break;
case 'f': /* --files N */
files = strtoul (optarg, NULL, 10);
break;
case 'c': /* --create */
cflag = 1;
break;
case 'r': /* --remove */
rflag = 1;
break;
case 't': /* --test */
tflag = 1;
break;
case 'C': /* --primecache */
Cflag = 1;
break;
case 'F': /* --failsearch */
Fflag = 1;
break;
default:
usage ();
/*NOTREACHED*/
}
}
if (optind != argc - 1 || (!cflag && !rflag && !tflag))
usage ();
root = argv[optind++];
searchpath = searchpath_create (root, length);
target = Fflag ? -1 : length - 1;
/* Create root + fs objects.
*/
if (cflag) {
if (gettimeofday (&t1, NULL) < 0)
err_exit ("gettimeofday");
if (mkdir (root, 0777) < 0)
err_exit ("mkdir %s", root);
fsobj_create (searchpath, length, files);
if (gettimeofday (&t2, NULL) < 0)
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
msg ("Created %d objects in %d.%.3ds", length * files + 1,
elapsed.tv_sec, elapsed.tv_usec / 1000);
}
/* Test
*/
if (tflag) {
if (Cflag) {
if (gettimeofday (&t1, NULL) < 0)
err_exit ("gettimeofday");
count = pathwalk (searchpath, length, 1, target);
if (gettimeofday (&t2, NULL) < 0)
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
msg ("Found %d/%d files in %d directories in %d.%.3ds",
count, 1, length,
elapsed.tv_sec, elapsed.tv_usec / 1000);
}
if (gettimeofday (&t1, NULL) < 0)
err_exit ("gettimeofday");
count = pathwalk (searchpath, length, files, target);
if (gettimeofday (&t2, NULL) < 0)
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
msg ("Found %d/%d files in %d directories in %d.%.3ds",
count, files, length, elapsed.tv_sec, elapsed.tv_usec / 1000);
}
/* Remove root + fs objects.
*/
if (rflag) {
if (gettimeofday (&t1, NULL) < 0)
err_exit ("gettimeofday");
fsobj_destroy (searchpath, length, files);
if (rmdir (root) < 0)
err_exit ("rmdir %s", root);
if (gettimeofday (&t2, NULL) < 0)
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
msg ("Removed %d objects in %d.%.3ds", length * files + 1,
elapsed.tv_sec, elapsed.tv_usec / 1000);
}
searchpath_destroy (searchpath, length);
exit (0);
}
/* Return index of searchpath in which 'name' was found or -1 if not found.
*/
static int
_search (char **searchpath, int length, char *name)
{
char path[PATH_MAX + 1];
struct stat sb;
int i;
for (i = 0; i < length; i++) {
snprintf (path, sizeof (path), "%s/%s", searchpath[i], name);
if (stat (path, &sb) == 0)
break;
}
return i < length ? i : -1;
}
/* Search for a file that will be found in the last directory of searchpath.
*/
static int
pathwalk (char **searchpath, int length, int files, int target)
{
char name[PATH_MAX + 1];
int i, found;
int count = 0;
for (i = 0; i < files; i++) {
snprintf (name, sizeof (name), "%d.%d", target, i);
found = _search (searchpath, length, name);
assert (found == target);
if (found != -1)
count++;
}
return count;
}
static void
searchpath_destroy(char **searchpath, int length)
{
int i;
for (i = 0; i < length; i++)
free (searchpath[i]);
free (searchpath);
}
static char **
searchpath_create (char *root, int length)
{
char path[PATH_MAX + 1];
char **searchpath;
int i;
if (!(searchpath = malloc (sizeof (char *) * length)))
errn_exit (ENOMEM, "malloc searchpath");
for (i = 0; i < length; i++) {
snprintf (path, sizeof (path), "%s/%d", root, i);
if (!(searchpath[i] = strdup (path)))
errn_exit (ENOMEM, "strdup searchpath");
}
return searchpath;
}
static void
fsobj_create (char **searchpath, int length, int files)
{
char path[PATH_MAX + 1];
int i, j, fd;
for (i = 0; i < length; i++) {
if (mkdir (searchpath[i], 0777) < 0)
err_exit ("mkdir %s", searchpath[i]);
for (j = 0; j < files; j++) {
snprintf (path, sizeof (path), "%s/%d.%d", searchpath[i], i, j);
if ((fd = creat (path, 0644)) < 0)
err_exit ("creat %s", path);
if (close (fd) < 0)
err_exit ("close %s", path);
}
}
}
static void
fsobj_destroy (char **searchpath, int length, int files)
{
char path[PATH_MAX + 1];
int i, j, fd;
for (i = 0; i < length; i++) {
for (j = 0; j < files; j++) {
snprintf (path, sizeof (path), "%s/%d.%d", searchpath[i], i, j);
if ((unlink (path)) < 0)
err_exit ("unlink %s", path);
}
if (rmdir (searchpath[i]) < 0)
err_exit ("mkdir %s", searchpath[i]);
}
}
/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/