Debian Codegraph @

util/bench/nfsstones/nfsstones.c

raw symbols SCIP
1 /* @(#)nfsstones.c 19.1 (ES0-DMD) 02/25/03 14:35:52 */
2 /*===========================================================================
3 Copyright (C) 1995 European Southern Observatory (ESO)
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; either version 2 of
8 the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
18 MA 02139, USA.
19
20 Corresponding concerning ESO-MIDAS should be addressed as follows:
21 Internet e-mail: [email protected]
22 Postal address: European Southern Observatory
23 Data Management Division
24 Karl-Schwarzschild-Strasse 2
25 D 85748 Garching bei Muenchen
26 GERMANY
27 ===========================================================================*/
28
29 #include <sys/param.h>
30 #include <stdio.h>
31 #include <sys/file.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <sys/errno.h>
38 #include <fcntl.h>
39
40 /*
41 * This program takes two arguments. The first one is the directory to
42 * run the tests in. The second one is the lock file name. The program
43 * will set itself up, then try to obtain the lock. This will allow
44 * the benchmark to sync up. When it has the lock, it will release it
45 * right away to allow someone else to get it. This should happen fast
46 * enough that the clients will start within a second or two of each other.
47 *
48 */
49
50 /*
51 * Note: with the below settings, each client will use about 12.2 MB of
52 * disk space on the server. It is important to keep the disk space
53 * per client above the amount of memory per client. Since we are
54 * trying to test the server speed, not the client cache speed, the
55 * blocks per file should be adjusted to keep the cache overflowing.
56 * Note: The above observation does not apply to suns not running
57 * SunOS >= 4.0
58 */
59 #define TOP_DIRS 2
60 #define BOT_DIRS 3
61 #define FILES_PER_DIR 1
62 #define FILE_CREATES 83
63 #define NREAD_LINK 583
64 #define BLOCKS_PER_FILE 250
65 #define FILE_LOOKUPS 4167
66 #define BYTES_PER_BLOCK 8192
67 #define TOT_DIRS TOP_DIRS * BOT_DIRS
68 #define TOT_FILES TOT_DIRS * FILES_PER_DIR
69
70 /*
71 * The number eleven comes from:
72 * 1. write the file
73 * 2. read sequentially
74 * 3. read the file (not sequentially)
75 * 4. read sequentially
76 * 5. read sequentially
77 * 6. read sequentially
78 * 7. read the file (not sequentially)
79 * 8. read sequentially
80 * 9. read sequentially
81 * 10. read sequentially
82 * 11. read the file (not sequentially)
83 */
84 #define TOT_FILEOPS TOT_FILES * 11 * BLOCKS_PER_FILE + \
85 (FILE_LOOKUPS + FILE_CREATES + NREAD_LINK) * TOT_FILES
86
87 /*
88 * The number 2 comes from the create/delete of the directories
89 */
90 #define TOTAL_OPS TOT_DIRS * 2 + TOT_FILES * 2 + TOT_FILEOPS
91
92 /*
93 char *strcpy(), *strcat(), *sprintf();
94 long lseek();
95 */
96
97 int buf[BYTES_PER_BLOCK/sizeof(int)];
98 main(argc, argv)
99 int argc;
100 char *argv[];
101 {
102 char *path;
103 char *lock;
104 int lock_fd;
105 struct timeval start_time, end_time;
106 struct timezone dummy;
107 int i, j, num = 0, pid, wait();
108 char s[BUFSIZ], t[BUFSIZ], u[BUFSIZ];
109 double total_time;
110 union wait status;
111 int error = 0;
112 struct stat stat_buf;
113
114 bzero((char *)buf, BYTES_PER_BLOCK);
115 if (argc != 3) {
116 (void)fprintf(stderr, "Usage: %s path_prefix lock_file_path\n",
117 argv[0]);
118 exit(-1);
119 }
120
121 path = argv[1];
122 lock = argv[2];
123
124 if (stat(path, &stat_buf) != 0) {
125 perror("path_prefix");
126 exit(-1);
127 }
128
129 if ((stat_buf.st_mode & S_IFMT) != S_IFDIR ||
130 access(path, W_OK|X_OK) != 0) {
131 (void)fprintf(stderr,
132 "Path_prefix is not a directory with write permission.\n");
133 exit(-1);
134 }
135
136 if ((lock_fd = open(lock, O_RDWR)) < 0) {
137 perror("lock_file_path");
138 exit(-1);
139 }
140
141 if (chdir(path) != 0) {
142 perror("path_prefix");
143 exit(-1);
144 }
145
146 /*
147 * Now we go get the lock to synchronise with the other clients
148 */
149
150 if (lockf(lock_fd, F_LOCK, 0) != 0) {
151 perror("lockf");
152 exit(-1);
153 }
154
155 /*
156 * Now let someone else have the lock
157 */
158
159 if (lockf(lock_fd, F_ULOCK, 0) != 0) {
160 perror("lockf");
161 exit(-1);
162 }
163
164 /*
165 * Start Timing
166 */
167 if (gettimeofday(&start_time, &dummy) == -1) {
168 perror("gettimeofday");
169 exit(-1);
170 }
171
172 for (i = 0; i < TOP_DIRS; i++) {
173 (void)sprintf(u, "%8d", ++num);
174 if (mkdir(u, 0777) != 0) {
175 perror("mkdir");
176 exit(-1);
177 }
178 for (j = 0; j < BOT_DIRS; j++) {
179 (void)sprintf(t, "%8d", ++num);
180 (void)strcpy(s, u);
181 (void)strcat(s, "/");
182 (void)strcat(s, t);
183 if (mkdir(s, 0777) != 0) {
184 perror("mkdir: subdir");
185 exit(-1);
186 }
187 if ((pid = fork()) == 0)
188 do_child(s);
189 else if (pid == -1) {
190 perror("fork");
191 exit(-1);
192 }
193 }
194 }
195
196 /*
197 * Now we wait for all of the children to get done.
198 */
199
200 while(wait(&status) != -1)
201 if (status.w_status != 0)
202 error = 1;
203
204 num = 0;
205 for (i = 0; i < TOP_DIRS; i++) {
206 (void)sprintf(u, "%8d", ++num);
207 for (j = 0; j < BOT_DIRS; j++) {
208 (void)sprintf(t, "%8d", ++num);
209 (void)strcpy(s, u);
210 (void)strcat(s, "/");
211 (void)strcat(s, t);
212 if (rmdir(s) != 0) {
213 perror("rmdir: subdir");
214 exit(-1);
215 }
216 }
217 if (rmdir(u) != 0) {
218 perror("rmdir");
219 exit(-1);
220 }
221 }
222
223 if (error)
224 exit(-1);
225
226 if (gettimeofday(&end_time, &dummy) == -1) {
227 perror("gettimeofday");
228 exit(-1);
229 }
230
231 total_time = (double)(end_time.tv_sec - start_time.tv_sec) +
232 ((double)(end_time.tv_usec - start_time.tv_usec)/1000000.0);
233
234 (void)printf("Total NFSstones: %d\n", TOTAL_OPS);
235 (void)printf("Total time = %f seconds, or %f NFSstones/second\n", total_time,
236 ((double) TOTAL_OPS)/total_time);
237 exit(0);
238 }
239
240 do_child(dir)
241 char *dir;
242 {
243 int i, fd, cfd;
244 long j;
245 char s[BUFSIZ];
246 char b[BUFSIZ];
247 extern int errno;
248
249 if (chdir(dir) != 0) {
250 perror("Subdirectory");
251 exit(-1);
252 }
253 for (i = 0; i < FILES_PER_DIR; i++) {
254 (void)sprintf(s, "%8d", i);
255 if ((fd = open(s, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
256 perror("open");
257 exit(-1);
258 }
259
260 seq_write(fd);
261 seq_read(fd);
262
263 /*
264 * Now lets through in some readlink calls
265 */
266
267 if (symlink("/aaa/bbb/ccc/ddd/eee/fff", "Test_link") != 0) {
268 perror("symlink");
269 if (errno != EEXIST)
270 exit(-1);
271 }
272
273 for (j = 0; j < NREAD_LINK; j++) {
274 if (readlink("Test_link", b, BUFSIZ) == -1) {
275 perror("readlink");
276 exit(-1);
277 }
278 }
279
280
281 nseq_read(fd);
282
283 /*
284 * We put the unlink out here because a create retransmit
285 * could get us.
286 */
287 (void)unlink("Test_link");
288 /*
289 * Now lets throw in create requests
290 */
291
292 for (j = 0; j < FILE_CREATES; j++) {
293 if ((cfd = open("test_create", O_RDWR|O_CREAT|O_TRUNC,
294 0666)) < 0) {
295 perror("open");
296 exit(-1);
297 }
298 /*
299 * Force the create to get out of cache
300 */
301 (void)fsync(cfd);
302 (void)close(cfd);
303 }
304
305 seq_read(fd);
306 /*
307 * We put the unlink out here because a create retransmit
308 * could get us.
309 */
310
311 (void)unlink("test_create");
312 seq_read(fd);
313
314 /*
315 * Now we look up non existent files
316 */
317 for (j = 0; j < FILE_LOOKUPS; j++) {
318 char sb[BUFSIZ];
319 int pp = getpid();
320
321 (void)sprintf(sb, "xx%d%d%d", j,pp,i);
322 (void)access(sb, F_OK);
323 }
324
325 seq_read(fd);
326 nseq_read(fd);
327 seq_read(fd);
328 seq_read(fd);
329 seq_read(fd);
330 nseq_read(fd);
331
332 (void)close(fd);
333
334 /*
335 * Now we rename the file
336 */
337 if (rename(s, "Test_of_rename") != 0) {
338 perror("rename");
339 exit(-1);
340 }
341
342 /*
343 * Finally get rid of the file
344 */
345 if (unlink("Test_of_rename") != 0) {
346 perror("unlink");
347
348 /*
349 * Busy servers cause the client to retransmit the
350 * request, giving the ENOENT since the file was
351 * removed by the first request, so we ignore this.
352 * (Maybe we should penalize 1 NFS stone/sec ??)
353 */
354 if (errno != ENOENT)
355 exit(-1);
356 }
357 sync();
358 }
359
360 exit(0);
361 }
362
363 /*
364 * Write a file descriptor sequentially
365 */
366 seq_write(fd)
367 int fd;
368 {
369 long j;
370
371 /*
372 * Write the file sequentially
373 */
374
375 if (lseek(fd, 0L, L_SET) == -1) {
376 perror("lseek");
377 exit(-1);
378 }
379
380 for (j = 0; j < BLOCKS_PER_FILE; j++) {
381 buf[0] = j; /* Minimal sanity check */
382 if (write(fd,(char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) {
383 perror("write");
384 exit(-1);
385 }
386 }
387
388 (void)fsync(fd);
389 }
390
391 seq_read(fd)
392 int fd;
393 {
394 long j;
395
396 /*
397 * Read sequentially
398 */
399 if (lseek(fd, 0L, L_SET) == -1) {
400 perror("lseek");
401 exit(-1);
402 }
403
404 for (j = 0; j < BLOCKS_PER_FILE; j++) {
405 if (read(fd, (char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) {
406 perror("read");
407 exit(-1);
408 }
409
410 if (buf[0] != j) {
411 (void)fprintf(stderr, "Data corruption error\n");
412 exit(-1);
413 }
414 }
415 }
416
417 nseq_read(fd)
418 int fd;
419 {
420 long j;
421
422 /*
423 * Read back the file not in order.
424 */
425 for (j = 0; j < BLOCKS_PER_FILE/2; j++) {
426 if (lseek(fd, j * BYTES_PER_BLOCK, L_SET) == -1) {
427 perror("lseek");
428 exit(-1);
429 }
430
431 if (read(fd, (char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) {
432 perror("read");
433 exit(-1);
434 }
435
436 if (buf[0] != j) {
437 (void)fprintf(stderr, "Data corruption error\n");
438 exit(-1);
439 }
440
441 if (lseek(fd, (BLOCKS_PER_FILE - (j + 1)) *
442 BYTES_PER_BLOCK, L_SET) == -1) {
443 perror("lseek");
444 exit(-1);
445 }
446
447 if (read(fd, (char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) {
448 perror("read");
449 exit(-1);
450 }
451
452 if (buf[0] != (BLOCKS_PER_FILE - (j + 1))) {
453 (void)fprintf(stderr, "Data corruption error\n");
454 exit(-1);
455 }
456 }
457 }