Home | History | Annotate | Download | only in fs-bench
      1 /* random-del-create.c (GPL)*/
      2 /* Hironobu SUZUKI <hironobu (at) h2np.net> */
      3 
      4 #include <stdio.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 #define  MAXERROR 1024
     19 
     20 extern int box_muler(int, int);
     21 extern void create_or_delete(char *);
     22 
     23 int delete_file(char *filename);
     24 int create_file(char *filename);
     25 
     26 int cfilecount = 0;
     27 int dfilecount = 0;
     28 int errorcount = 0;
     29 
     30 int main(int ac, char **av)
     31 {
     32 	int r;
     33 	char fname[1024];
     34 	time_t t;
     35 	int i;
     36 	int m;
     37 
     38 	if (ac != 2) {
     39 		printf("%s hex-style-filename \n", av[0]);
     40 		printf("ex) %s 00022300\n", av[0]);
     41 		exit(1);
     42 	}
     43 	sscanf(av[1], "%x", &m);
     44 	if (m < 1 || m > MAXNUM) {
     45 		printf("out of size %d\n", m);
     46 		exit(1);
     47 	}
     48 
     49 	time(&t);
     50 	srandom((unsigned int)getpid() ^
     51 		(((unsigned int)t << 16) | (unsigned int)t >> 16));
     52 
     53 	/* 00/00/00/00 */
     54 	for (i = 0; i < m; i++) {
     55 		r = random() % m;
     56 		sprintf(fname, "00/%2.2x/%2.2x/00%2.2x%2.2x%2.2x",
     57 			((r >> 16) & 0xFF),
     58 			((r >> 8) & 0xFF),
     59 			((r >> 16) & 0xFF), ((r >> 8) & 0xFF), (r & 0xFF));
     60 		create_or_delete(fname);
     61 	}
     62 	fprintf(stderr, "Total create files: %d\n", cfilecount);
     63 	fprintf(stderr, "Total delete files: %d\n", dfilecount);
     64 	fprintf(stderr, "Total error       : %d\n", errorcount);
     65 	exit(0);
     66 }
     67 
     68 #define MAXFSIZE (192*1024)
     69 #define AVEFSIZE (MAXFSIZE/2)
     70 #define POOLDISKSPACE (AVEFSIZE*128)
     71 
     72 static int disk_space_pool = 0;
     73 void create_or_delete(char *fname)
     74 {
     75 	int r;
     76 
     77 	r = (random() & 1);
     78 	if (r && disk_space_pool > POOLDISKSPACE) {
     79 		/* create */
     80 		create_file(fname);
     81 	} else {
     82 		delete_file(fname);
     83 	}
     84 	if ((errorcount > dfilecount || errorcount > cfilecount)
     85 	    && (errorcount > MAXERROR)) {
     86 		fprintf(stderr, "too much error -- stop\n");
     87 		fprintf(stderr, "Total create files: %d\n", cfilecount);
     88 		fprintf(stderr, "Total delete files: %d\n", dfilecount);
     89 		fprintf(stderr, "Total error       : %d\n", errorcount);
     90 		exit(1);
     91 	}
     92 }
     93 
     94 int create_file(char *filename)
     95 {
     96 	int fd;
     97 	int randomsize;
     98 	char wbuf[MAXFSIZE];
     99 	if ((fd = creat(filename, S_IRWXU)) < 0) {
    100 		errorcount++;
    101 		return (-1);
    102 	}
    103 	if ((randomsize = box_muler(0, MAXFSIZE)) < 0) {
    104 		randomsize = MAXFSIZE;
    105 	}
    106 	if (write(fd, wbuf, randomsize) < 0) {
    107 		errorcount++;
    108 		close(fd);
    109 		return (-1);
    110 	}
    111 	cfilecount++;
    112 	disk_space_pool -= randomsize;
    113 	close(fd);
    114 
    115 	return 0;
    116 }
    117 
    118 #include <sys/stat.h>
    119 #include <unistd.h>
    120 
    121 int delete_file(char *filename)
    122 {
    123 	struct stat buf;
    124 	int st;
    125 	st = stat(filename, &buf);
    126 	if (st < 0) {
    127 		errorcount++;
    128 		return (-1);
    129 	}
    130 	disk_space_pool += buf.st_size;
    131 	if (unlink(filename) < 0) {
    132 		errorcount++;
    133 		return (-1);
    134 	}
    135 	dfilecount++;
    136 
    137 	return 0;
    138 }
    139