6 #include "rfs_assert.h"
15 #define MAX_FILES 100000
16 #define MAX_DIRS 100000
17 #define FILE_FH_HTABLE_SIZE MAX_FILES
18 #define MAX_NAMELEN 256
19 #define MAX_PLAY_PATH_SIZE 256
20 #define MAX_COMMAND_LEN (MAX_PLAY_PATH_SIZE+16)
21 #define NFS_MAXDATA 32768
22 #define TRACE_FH_SIZE 64
24 #define FH_T_FLAG_FREE 0
25 #define FH_T_FLAG_IN_USE 1
33 //char trace_fh [TRACE_FH_SIZE+1];
34 char path[MAX_PLAY_PATH_SIZE];
40 //struct generic_entry * htable;
47 fh_info_t file_fh, dir_fh;
48 profile_t read_line_profile, fgets_profile;
49 char trace_file[MAX_NAMELEN];
50 FILE * profile_fp = NULL;
51 char testdir[MAX_NAMELEN];
53 int age_create_num = 0;
54 int age_mkdir_num = 0;
55 int assure_create_num = 0;
56 int assure_mkdir_num = 0;
57 int age_write_num = 0;
58 int nonage_write_num = 0;
59 int overlap_write_num = 0;
61 int init_profile_variables()
63 init_profile ("read_line profile", &read_line_profile);
64 init_profile ("fgets profile", &fgets_profile);
67 int init_fh_info (char * name, fh_info_t * fh_infop, int fh_size, int htable_size)
69 RFS_ASSERT (strlen(name) < sizeof(fh_infop->name));
70 strcpy (fh_infop->name, name);
73 fh_infop->fh_size = fh_size;
74 //fh_infop->htable_size = htable_size;
75 fh_infop->fh = (fh_t *)malloc (sizeof(fh_t)*fh_size);
76 RFS_ASSERT (fh_infop->fh);
77 //fh_infop->htable = malloc (sizeof(struct*generic_entry)*htable_size);
78 //RFS_ASSERT (fh_infop->htable);
79 printf("initialize %s size %d bytes\n",
80 //name, sizeof(fh_t)*fh_size + sizeof(struct*generic_entry)*htable_size);
81 name, sizeof(fh_t)*fh_size);
86 init_fh_info ("file_fh", &file_fh, MAX_FILES, MAX_FILES);
87 init_fh_info ("dir_fh", &dir_fh, MAX_DIRS, MAX_DIRS);
90 int add_fh_t (fh_info_t * fh_table, char * path, int exist_flag)
94 for (i=0; i<fh_table->fh_size; i++,fh_table->index++) {
95 if (fh_table->index==fh_table->fh_size)
97 if (fh_table->fh[fh_table->index].flag == FH_T_FLAG_FREE) {
98 fh_table->fh[fh_table->index].flag = FH_T_FLAG_IN_USE;
99 //RFS_ASSERT(strlen(path)<MAX_PLAY_PATH_SIZE);
100 strcpy (fh_table->fh[fh_table->index].path, path);
101 if (fh_table->index > fh_table->fh_max)
102 fh_table->fh_max = fh_table->index;
106 //print_fh_map(fh_table);
110 int create_mkdir_op (int flag)
113 char name[MAX_NAMELEN];
114 char command[MAX_COMMAND_LEN];
118 fh_info_t * fh_infop;
120 while (count++ < 100) {
121 i = random()%dir_fh.fh_max;
122 if (dir_fh.fh[i].flag==FH_T_FLAG_IN_USE) {
123 assure_exist(dir_fh.fh[i].path);
124 strcpy (name, dir_fh.fh[i].path);
125 if (flag == IS_FILE) {
126 sprintf (name+strlen(name), "AGEfile%d", fhno++);
127 fd = creat (name, S_IRWXU);
129 //printf ("create fd %d\n", fd);
133 sprintf (name+strlen(name), "AGEdir%d", fhno++);
134 fd = mkdir (name, S_IRWXU);
140 if (errno == ENOENT) {
141 dir_fh.fh[i].flag = FH_T_FLAG_FREE;
146 add_fh_t (fh_infop, name, EXIST);
160 while (count++<100) {
161 i = random()%file_fh.fh_max;
162 if (file_fh.fh[i].flag == FH_T_FLAG_IN_USE) {
164 if (!strstr(file_fh.fh[i].path, "AGE"))
167 assure_exist(file_fh.fh[i].path);
168 ret = remove (file_fh.fh[i].path);
169 RFS_ASSERT (ret ==0);
170 file_fh.fh[i].flag = FH_T_FLAG_FREE;
181 char command[MAX_COMMAND_LEN];
184 while (count++<100) {
185 i = random()%dir_fh.fh_max;
186 if ( (dir_fh.fh[i].flag == FH_T_FLAG_IN_USE) ) {
188 if (!strstr(file_fh.fh[i].path, "AGE"))
191 assure_exist(file_fh.fh[i].path);
192 ret = rmdir (dir_fh.fh[i].path);
194 dir_fh.fh[i].flag = FH_T_FLAG_FREE;
197 RFS_ASSERT ((ret == -1) && (errno == ENOTEMPTY));
198 //strcpy (command, "rm -r %s", dir_fh.fh[i].path);
205 int loop_write (int fd, char * buf, int buflen)
211 ret = write (fd, buf+pos, buflen-pos);
214 perror ("loop write");
217 if (ret == buflen-pos)
224 int assure_exist(char * path)
226 char name[MAX_NAMELEN];
229 int non_exist_flag = 0;
233 ret = stat (path, &st);
236 RFS_ASSERT (errno == ENOENT);
238 RFS_ASSERT (!strstr (path, "AGE"));
241 while (count++<100) {
242 for (; *p!=0 && *p!='/'; p++, q++ )
245 ret = stat (name, &st);
247 RFS_ASSERT (errno == ENOENT)
249 ret = creat (name, S_IRWXU);
250 assure_create_num ++;
251 RFS_ASSERT (ret >=0);
254 ret = mkdir (name, S_IRWXU);
256 RFS_ASSERT (ret >=0);
270 int write_op (int off, int size)
272 static char buf[NFS_MAXDATA];
279 RFS_ASSERT (size <= NFS_MAXDATA);
280 while (count++<100) {
281 i = random()%file_fh.fh_max;
282 if ( (file_fh.fh[i].flag == FH_T_FLAG_IN_USE) ) {
283 assure_exist(file_fh.fh[i].path);
284 fd = open(file_fh.fh[i].path, O_WRONLY);
288 //printf ("write fd %d\n", fd);
291 if (st.st_size < (off+size)) {
292 int written_bytes = 0;
293 while (written_bytes+NFS_MAXDATA < off+size-st.st_size) {
294 loop_write (fd, buf, NFS_MAXDATA);
295 written_bytes += NFS_MAXDATA;
297 loop_write (fd, buf, off+size-st.st_size-written_bytes);
298 if (strstr(file_fh.fh[i].path, "AGE")) {
299 age_write_num+=(written_bytes+NFS_MAXDATA-1)/NFS_MAXDATA;
301 nonage_write_num+=(written_bytes+NFS_MAXDATA-1)/NFS_MAXDATA;
305 if (strstr(file_fh.fh[i].path, "AGE")) {
309 loop_write (fd, buf, size);
318 int truncate_op(int size)
324 while (count++<100) {
325 i = random()%file_fh.fh_max;
326 if ( (file_fh.fh[i].flag == FH_T_FLAG_IN_USE) ) {
328 if (!strstr(file_fh.fh[i].path, "AGE"))
331 assure_exist (file_fh.fh[i].path);
332 ret = truncate(file_fh.fh[i].path, size);
335 RFS_ASSERT (errno == ENOENT);
336 file_fh.fh[i].flag = FH_T_FLAG_FREE;
343 int print_fh_map(fh_info_t * fhp)
347 for (i=0; i<fhp->fh_max; i++) {
348 if (fhp->fh[i].flag == FH_T_FLAG_IN_USE) {
350 printf("%s[%d] %s\n", fhp->name, i, fhp->fh[i].path);
353 fprintf(stderr, "fh_max %d total %d entries \n", fhp->fh_max, num);
356 void read_fh_map(char * fh_map_file)
361 char trace_fh[TRACE_FH_SIZE];
368 char name[MAX_NAMELEN];
370 fp = fopen(fh_map_file, "r");
372 printf ("can not opern %s\n", fh_map_file);
376 RFS_ASSERT (fp!=NULL);
380 memset(buf, 0, sizeof(buf));
381 while (fgets(buf, 1024, fp)) {
382 RFS_ASSERT (fh_map_debug==0);
384 if (lineno % 10000==0)
385 printf("%d fh_map entry read\n", lineno);
387 RFS_ASSERT (buf[strlen(buf)-1]=='\n');
388 buf[strlen(buf)-1]=0;
390 trace_path = buf + TRACE_FH_SIZE +1;
392 strcpy (name, testdir);
393 strcat (name, trace_path);
394 if ((*(buf+strlen(buf)-1))=='/') {
395 *(buf+strlen(buf)-1)=0;
396 add_fh_t (&dir_fh, name, NOT_EXIST);
398 add_fh_t (&file_fh, name, NOT_EXIST);
404 print_fh_map (&file_fh);
405 print_fh_map (&dir_fh);
411 printf("age trace_file fh_path_map testdir\n");
414 inline char * read_line (int disk_index)
416 static FILE * fp=NULL;
418 static int start_disk_index=0;
420 static int finish_flag = 0;
422 #define READ_LINE_BUF_SIZE 1000
423 #define READ_LINE_LENGTH 32
425 static char line_buf[READ_LINE_BUF_SIZE][READ_LINE_LENGTH];
426 start_profile (&read_line_profile);
429 if (strcmp(trace_file, "stdin")) {
430 fp = fopen(trace_file, "r");
432 printf("can not open files %s\n", fp);
438 RFS_ASSERT (fp!=NULL);
439 for (i=0; i<READ_LINE_BUF_SIZE; i++) {
440 start_profile(&fgets_profile);
441 if (!fgets(line_buf[i], READ_LINE_LENGTH, fp)) {
444 end_profile(&fgets_profile);
445 //printf ("read_line, line_buf[%d]:%s", i, line_buf[i]);
449 RFS_ASSERT (disk_index <= start_disk_index+READ_LINE_BUF_SIZE)
450 if (disk_index==(start_disk_index+READ_LINE_BUF_SIZE)) {
454 start_profile(&fgets_profile);
455 if (!fgets(line_buf[start], READ_LINE_LENGTH, fp)) {
456 end_profile(&fgets_profile);
461 end_profile(&fgets_profile);
462 //printf ("read_line, line_buf[%d]:%s", start, line_buf[start]);
463 start = (start+1) % READ_LINE_BUF_SIZE;
466 RFS_ASSERT (disk_index < start_disk_index+READ_LINE_BUF_SIZE)
467 i = (start+disk_index-start_disk_index)%READ_LINE_BUF_SIZE;
469 end_profile (&read_line_profile);
470 return (line_buf[i]);
480 ret = statfs (testdir, &stfs);
481 RFS_ASSERT (ret == 0);
482 fprintf(stderr, "number of files %d size of file system %d\n", stfs.f_files, stfs.f_bfree);
483 fprintf(stderr, "assure_create_num %d assure_mkdir_num %d, age_create_num %d age_mkdir_num %d age_write_num %d nonage_write_num %d overlap_write_num %d\n",
484 assure_create_num, assure_mkdir_num, age_create_num, age_mkdir_num, age_write_num, nonage_write_num, overlap_write_num);
485 printf("assure_create_num %d assure_mkdir_num %d, age_create_num %d age_mkdir_num %d age_write_num %d nonage_write_num %d overlap_write_num %d\n",
486 assure_create_num, assure_mkdir_num, age_create_num, age_mkdir_num, age_write_num, nonage_write_num, overlap_write_num);
489 int main(int argc, char ** argv)
492 static int disk_index=0;
493 int j, nfs3proc, size, off, count;
504 strcpy (trace_file, argv[1]);
505 strcpy (testdir, argv[3]);
506 ret = stat (testdir, &st);
507 RFS_ASSERT (ret == 0);
508 read_fh_map(argv[2]);
509 init_profile_variables();
511 while ((buf=read_line(++disk_index))!=NULL) {
513 memset (procname, 0, sizeof(procname));
514 //printf ("line[%d] %s", disk_index, buf);
515 sscanf (&buf[0], "%s", procname);
517 while ((*(buf+j)!=' ') && (*(buf+j)!='\n'))
520 if (!strcmp(procname, "write"))
521 sscanf (&buf[j], "off %x count %x", &off, &count);
523 if (!strcmp(procname, "setattr"))
524 sscanf (&buf[j], "size %x", &size);
526 if (!strcmp(procname, "write"))
527 ret = write_op(off, count);
528 else if (!strcmp(procname, "create"))
529 ret = create_mkdir_op(IS_FILE);
530 else if (!strcmp(procname, "mkdir"))
531 ret = create_mkdir_op(IS_DIR);
532 else if (!strcmp(procname, "remove"))
534 else if (!strcmp(procname, "rmdir"))
536 else if (!strcmp(procname, "setattr"))
537 ret = truncate_op(size);
539 printf("disk_index %d procname %s\n", disk_index, procname);
543 printf("execute disk_line[%d] %s error\n", disk_index, buf);
545 if ((disk_index%100)==0) {
546 fprintf (stderr, "%d disk trace parsed \n", disk_index);