Home | History | Annotate | Download | only in fs-bench
      1 /* random-access.c (GPL)*/
      2 /* Hironobu SUZUKI <hironobu (at) h2np.net> */
      3 #include <stdio.h>
      4 #include <errno.h>
      5 #include <sys/stat.h>
      6 #include <sys/types.h>
      7 #include <fcntl.h>
      8 #include <unistd.h>
      9 #include <time.h>
     10 #include <stdlib.h>
     11 #define FAIL 0
     12 #define SUCCESS 1
     13 
     14 int openlog[2] = { 0, 0 };
     15 
     16 #define MAXNUM 0x100000
     17 
     18 void open_read_close(char *fname);
     19 
     20 int nullfd;
     21 
     22 int main(int ac, char **av)
     23 {
     24 	int r;
     25 	char fname[1024];
     26 	time_t t;
     27 	int i;
     28 	int m;
     29 
     30 	if (ac != 2) {
     31 		printf("%s hex-style-filename \n", av[0]);
     32 		printf("ex) %s 00022300\n", av[0]);
     33 		exit(1);
     34 	}
     35 	sscanf(av[1], "%x", &m);
     36 	if (m < 1 || m > MAXNUM) {
     37 		printf("out of size %d\n", m);
     38 		exit(1);
     39 	}
     40 
     41 	time(&t);
     42 	srandom((unsigned int)getpid() ^
     43 		(((unsigned int)t << 16) | (unsigned int)t >> 16));
     44 
     45 	if ((nullfd = open("/dev/null", O_WRONLY)) < 0) {
     46 		perror("/dev/null");
     47 		exit(1);
     48 	}
     49 
     50 	/* 00/00/00/00 */
     51 	for (i = 0; i < m; i++) {
     52 		r = random() % m;
     53 		sprintf(fname, "00/%2.2x/%2.2x/00%2.2x%2.2x%2.2x",
     54 			((r >> 16) & 0xFF),
     55 			((r >> 8) & 0xFF),
     56 			((r >> 16) & 0xFF), ((r >> 8) & 0xFF), (r & 0xFF));
     57 		open_read_close(fname);
     58 	}
     59 	close(nullfd);
     60 	printf("Success:\t%d\nFail:\t%d\n", openlog[SUCCESS], openlog[FAIL]);
     61 	exit(0);
     62 }
     63 
     64 #define BUFS 8192
     65 void open_read_close(char *fname)
     66 {
     67 	int fd;
     68 	char buf[BUFS];
     69 	int c;
     70 
     71 	if ((fd = open(fname, O_RDONLY)) < 0) {
     72 		openlog[FAIL]++;
     73 		close(fd);
     74 		return;
     75 	}
     76 	openlog[SUCCESS]++;
     77 	while ((c = read(fd, buf, BUFS)) > 0) {
     78 		if (write(nullfd, buf, c) < 0) {
     79 			perror("/dev/null");
     80 			printf("Opened\t %d\nUnopend:\t%d\n", openlog[SUCCESS],
     81 			       openlog[FAIL]);
     82 			exit(1);
     83 		}
     84 	}
     85 	if (c < 0) {
     86 		perror(fname);
     87 		printf("Opened\t %d\nUnopend:\t%d\n", openlog[SUCCESS],
     88 		       openlog[FAIL]);
     89 		exit(1);
     90 	}
     91 	close(fd);
     92 }
     93