From d5e200f4cd9ea847e100e9d4b5b3241591445797 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Thu, 9 Dec 2004 20:31:41 +0000 Subject: [PATCH] add simple file benchmark for users. --- Repair/RepairCompiler/MCC/CRuntime/file.c | 596 ++++++++++++++++++++++ Repair/RepairCompiler/MCC/CRuntime/file.h | 9 +- 2 files changed, 601 insertions(+), 4 deletions(-) create mode 100755 Repair/RepairCompiler/MCC/CRuntime/file.c diff --git a/Repair/RepairCompiler/MCC/CRuntime/file.c b/Repair/RepairCompiler/MCC/CRuntime/file.c new file mode 100755 index 0000000..b75300d --- /dev/null +++ b/Repair/RepairCompiler/MCC/CRuntime/file.c @@ -0,0 +1,596 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "file.h" +#include "test3_aux.h" +#include "memory.h" + + +#define false 0 +#define true 1 +struct filedesc files[MAXFILES]; +struct InodeBitmap ib; +struct BlockBitmap bb; +int bbbptr,ibbptr,itbptr,rdiptr; +int main(int argc, char **argv) { + int i; + initializemmap(); + + for(i=0;i1;i--) { + char filename[10]; + sprintf(filename,"fil%d",i); + openfile(ptr,filename); + } + for(j=0;j<90;j++) { + int i; + for(i=145;i>1;i--) { + char *buf="01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"; + writefile2(ptr,i,buf,122); + } + } + for(i=145;i>1;i--) { + closefile(ptr,i); + } + unmountdisk(ptr); + break; + } + case '5': { + int i,j; + struct block * ptr=mountdisk("disk"); + for(i=15;i>=0;i--) { + char filename[10]; + sprintf(filename,"fil%d",i); + openfile(ptr,filename); + } + for(j=0;j<60;j++) { + int i; + for(i=15;i>=0;i--) { + char name[10]; + int len=sprintf(name, "%d ",i); + writefile2(ptr,i,name,len); + } + } + for(i=15;i>=0;i--) { + closefile(ptr,i); + } + for(i=15;i>=0;i--) { + char filename[10]; + sprintf(filename,"fil%d",i); + openfile(ptr,filename); + } + for(j=0;j<60;j++) { + int i; + for(i=15;i>=0;i--) { + int l=0; + char name[10]; + int len=sprintf(name, "%d ",i); + readfile2(ptr,i,name,len); + sscanf(name, "%d ", &l); + if (l!=i) { + printf("ERROR in benchmark\n"); + } + } + } + for(i=15;i>=0;i--) { + closefile(ptr,i); + } + unmountdisk(ptr); + } + break; + case '6': { + int i,j; + /* { + struct block * ptr=chmountdisk("disk"); + chunmountdisk(ptr); + }*/ + struct block * ptr=mountdisk("disk"); + for(i=145;i>=0;i--) { + char filename[10]; + sprintf(filename,"fil%d",i); + openfile(ptr,filename); + } + for(j=0;j<6000;j++) { + int i; + for(i=145;i>=0;i--) { + char name[10]; + int len=sprintf(name, "%d ",i); + writefile2(ptr,i,name,len); + } + } + for(i=145;i>=0;i--) { + closefile(ptr,i); + } + for(i=145;i>=0;i--) { + char filename[10]; + sprintf(filename,"fil%d",i); + openfile(ptr,filename); + } + for(j=0;j<400;j++) { + int i; + for(i=145;i>=0;i--) { + int l=0; + char name[10]; + int len=sprintf(name, "%d ",i); + readfile2(ptr,i,name,len); + sscanf(name, "%d ", &l); + if (l!=i) { + printf("ERROR in benchmark\n"); + } + } + } + for(i=145;i>=0;i--) { + closefile(ptr,i); + } + unmountdisk(ptr); + } + break; + case 'x': { + struct block * ptr=mountdisk("disk"); + + printdirectory(ptr); + + // check the DSs + // check the DSs + unsigned long time = 0; + + time += selfcheck2(ptr); + + printf("\ncompiled: %u us\n", (time)); + + break; + } + + + } + + +} + + +unsigned long selfcheck2(struct block* d) { + + struct timeval begin,end; + unsigned long t; + gettimeofday(&begin,NULL); + +#include "test3.c" + + gettimeofday(&end,NULL); + t=(end.tv_sec-begin.tv_sec)*1000000+end.tv_usec-begin.tv_usec; + return t; +} + + +struct block * chmountdisk(char *filename) { + int fd=open(filename,O_CREAT|O_RDWR); + struct block *ptr=(struct block *) mmap(NULL,LENGTH,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_SHARED,fd,0); + alloc(ptr,LENGTH); + return ptr; +} + +void chunmountdisk(struct block *vptr) { + int val=munmap(vptr,LENGTH); + dealloc(vptr); +} + +struct block * mountdisk(char *filename) { + int fd=open(filename,O_CREAT|O_RDWR); + int i; + struct block *ptr=(struct block *) mmap(NULL,LENGTH,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_SHARED,fd,0); + alloc(ptr,LENGTH); + struct SuperBlock *sb=(struct SuperBlock *) &ptr[0]; + struct GroupBlock *gb=(struct GroupBlock *) &ptr[1]; + bbbptr=gb->BlockBitmapBlock; + ibbptr=gb->InodeBitmapBlock; + itbptr=gb->InodeTableBlock; + rdiptr=sb->RootDirectoryInode; + + { + struct InodeBitmap *ibb=(struct InodeBitmap *) &ptr[ibbptr]; + struct BlockBitmap *bbb=(struct BlockBitmap *) &ptr[bbbptr]; + + for(i=0;i<(NUMINODES/8+1);i++) + ib.inode[i]=ibb->inode[i]; + for(i=0;i<(NUMBLOCK/8+1);i++) + bb.blocks[i]=bbb->blocks[i]; + return ptr; + } +} + +void unmountdisk(struct block *vptr) { + struct InodeBitmap *ibb=(struct InodeBitmap *) &vptr[ibbptr]; + int i,val; + for(i=0;i<(NUMINODES/8+1);i++) + ibb->inode[i]=ib.inode[i]; + + { + struct BlockBitmap *bbb=(struct BlockBitmap *) &vptr[bbbptr]; + for(i=0;i<(NUMBLOCK/8+1);i++) + bbb->blocks[i]=bb.blocks[i]; + val=munmap(vptr,LENGTH); + dealloc(vptr); + } +} + +void printdirectory(struct block *ptr) { + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + int i; + for(i=0;i<12;i++) { + struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]]; + int j; + for(j=0;jentries[j].name[0]!=0) { + /* lets finish */ + printf("%s %d inode %d bytes\n",db->entries[j].name, db->entries[j].inodenumber,itb->entries[db->entries[j].inodenumber].filesize); + } + } + } +} + +void printfile(char *filename, struct block *ptr) { + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + int i; + for(i=0;i<12;i++) { + struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]]; + int j; + for(j=0;jentries[j].name[0]!=0) { + if(strcmp(filename,db->entries[j].name)==0) { + /* Found file */ + int inode=db->entries[j].inodenumber; + + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + int i; + for(i=0;i<((itb->entries[inode].filesize+BLOCKSIZE-1)/BLOCKSIZE);i++) { + struct block *b=&ptr[itb->entries[inode].Blockptr[i]]; + write(0,b,BLOCKSIZE); + } + } + } + } + } +} + +void removefile(char *filename, struct block *ptr) { + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + int i; + for(i=0;i<12;i++) { + struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]]; + int j; + for(j=0;jentries[j].name[0]!=0) { + if(strcmp(filename,db->entries[j].name)==0) { + /* Found file */ + db->entries[j].name[0]=0; //Delete entry + { + int inode=db->entries[j].inodenumber; + + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + itb->entries[inode].referencecount--; + + if (itb->entries[inode].referencecount==0) { + int i; + for(i=0;i<((itb->entries[inode].filesize+BLOCKSIZE-1)/BLOCKSIZE);i++) { + int blocknum=itb->entries[inode].Blockptr[i]; + bb.blocks[blocknum/8]^=(1<<(blocknum%8)); + } + ib.inode[inode/8]^=(1<<(inode%8)); + } + } + } + } + } + } +} + +void createlink(struct block *ptr,char *filename, char *linkname) { + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + int i; + for(i=0;i<12;i++) { + struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]]; + int j; + for(j=0;jentries[j].name[0]!=0) { + if(strcmp(filename,db->entries[j].name)==0) { + /* Found file */ + int inode=db->entries[j].inodenumber; + struct InodeBlock * itb=(struct InodeBlock *) &ptr[4]; + itb->entries[inode].referencecount++; + addtode(ptr, inode, linkname); + } + } + } + } +} + +void closefile(struct block *ptr, int fd) { + struct InodeBlock * itb=(struct InodeBlock *) &ptr[4]; + msync(&itb->entries[fd],sizeof(struct DirectoryEntry),MS_SYNC); + files[fd].used=false; +} + +bool writefile(struct block *ptr, int fd, char *s) { + return (writefile2(ptr,fd,s,1)==1); +} + +int writefile2(struct block *ptr, int fd, char *s, int len) { + struct filedesc *tfd=&files[fd]; + struct InodeBlock * itb=(struct InodeBlock *) &ptr[4]; + int filelen=itb->entries[tfd->inode].filesize; + int i; + + if (tfd->used==false) + return -1; + + if ((12*BLOCKSIZE-tfd->offset)offset; + for(i=0;ioffset/BLOCKSIZE; + int noffset=tfd->offset%BLOCKSIZE; + if (tfd->offset>=filelen) { + if (noffset==0) { + int bptr=getblock(ptr); + if (bptr==-1) { + if (itb->entries[files[fd].inode].filesizeentries[files[fd].inode].filesize=files[fd].offset; + return i; + } + itb->entries[tfd->inode].Blockptr[nbuffer]=bptr; + } + } + { + int block=itb->entries[tfd->inode].Blockptr[nbuffer]; + char *fchar=(char *)&ptr[block]; + int tocopy=len-i; + if (tocopy>(BLOCKSIZE-noffset)) + tocopy=BLOCKSIZE-noffset; + memcpy(&fchar[noffset],&s[i],tocopy); + msync(&fchar[noffset],tocopy,MS_SYNC); + i+=tocopy; + tfd->offset+=tocopy; + } + } + if (itb->entries[files[fd].inode].filesizeentries[files[fd].inode].filesize=files[fd].offset; + return len; +} + + +char readfile(struct block *ptr, int fd) { + char array[1]; + if (readfile2(ptr,fd,array,1)==1) + return array[0]; + else + return EOF; +} + +int readfile2(struct block *ptr, int fd, char *buf, int len) { + struct filedesc *tfd=&files[fd]; + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + int filelen=itb->entries[tfd->inode].filesize; + int i; + + if (tfd->used==false) + return -1; + if ((filelen-tfd->offset)offset; + for(i=0;ioffset/BLOCKSIZE; + int noffset=tfd->offset%BLOCKSIZE; + int block=itb->entries[tfd->inode].Blockptr[nbuffer]; + char *fchar=(char *)&ptr[block]; + int tocopy=len-i; + if (tocopy>(BLOCKSIZE-noffset)) + tocopy=BLOCKSIZE-noffset; + memcpy(&buf[i],&fchar[noffset],tocopy); + i+=tocopy; + tfd->offset+=tocopy; + } + return len; +} + +int openfile(struct block *ptr, char *filename) { + /* Locate fd */ + int fd=-1; + int k; + struct InodeBlock * itb; + int inode; + int i; + + for(k=0;kentries[rdiptr].Blockptr[i]]; + int j; + for(j=0;jentries[j].name[0]!=0) { + if(strcmp(filename,db->entries[j].name)==0) { + files[fd].inode=db->entries[j].inodenumber; + files[fd].offset=0; + return fd; + } + } + } + } + /* Create file */ + + inode=getinode(ptr); + if (inode==-1) { + files[fd].used=false; + return -1; + } + itb->entries[inode].filesize=0; + itb->entries[inode].referencecount=1; + + addtode(ptr, inode, filename); + files[fd].inode=inode; + files[fd].offset=0; + return fd; +} + +void createfile(struct block *ptr,char *filename, char *buf,int buflen) { + int fd=openfile(ptr,filename); + writefile2(ptr,fd,buf,buflen); + closefile(ptr,fd); +} + +void addtode(struct block *ptr, int inode, char * filename) { + struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr]; + int i; + for(i=0;i<12;i++) { + struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]]; + int j; + for(j=0;jentries[j].name[0]==0) { + /* lets finish */ + strncpy(db->entries[j].name,filename,124); + db->entries[j].inodenumber=inode; + msync(&db->entries[j],sizeof(struct DirectoryEntry),MS_SYNC); + return; + } + } + } +} + +int getinode(struct block *ptr) { + int i; + for(i=0;iFreeBlockCount=BLOCKSIZE-5; + sb->FreeInodeCount=NUMINODES-1; + sb->NumberofInodes=NUMINODES; + sb->NumberofBlocks=NUMBLOCK; + sb->RootDirectoryInode=0; + sb->blocksize=BLOCKSIZE; + } + { + struct GroupBlock * gb=(struct GroupBlock *) &ptr[1]; + gb->BlockBitmapBlock=2; + gb->InodeBitmapBlock=3; + gb->InodeTableBlock=4; + gb->GroupFreeBlockCount=BLOCKSIZE-5; + gb->GroupFreeInodeCount=NUMINODES-1; + } + { + struct BlockBitmap * bb=(struct BlockBitmap *) &ptr[2]; + int i; + for(i=0;i<(5+12);i++) + bb->blocks[i/8]=bb->blocks[i/8]|(1<<(i%8)); + } + { + struct InodeBitmap * ib=(struct InodeBitmap *) &ptr[3]; + ib->inode[0]=1; + } + { + int i; + struct InodeBlock * itb=(struct InodeBlock *) &ptr[4]; + itb->entries[0].filesize=12*BLOCKSIZE; + for(i=0;i<12;i++) + itb->entries[0].Blockptr[i]=i+5; + itb->entries[0].referencecount=1; + } + { + int val=munmap(vptr,LENGTH); + dealloc(vptr); + if (val!=0) + printf("Error!\n"); + } +} diff --git a/Repair/RepairCompiler/MCC/CRuntime/file.h b/Repair/RepairCompiler/MCC/CRuntime/file.h index 1dc3436..9c710a1 100755 --- a/Repair/RepairCompiler/MCC/CRuntime/file.h +++ b/Repair/RepairCompiler/MCC/CRuntime/file.h @@ -1,5 +1,7 @@ #ifndef FILE_H #define FILE_H +#define bool int + #define BLOCKSIZE 8192 #define NUMBLOCK 1024 @@ -84,15 +86,15 @@ struct block * mountdisk(char *filename); void unmountdisk(struct block *vptr); void closefile(struct block *ptr, int fd); bool writefile(struct block *ptr, int fd, char *s); -int writefile(struct block *ptr, int fd, char *s, int len); +int writefile2(struct block *ptr, int fd, char *s, int len); char readfile(struct block *ptr, int fd); -int readfile(struct block *ptr, int fd, char *buf, int len); +int readfile2(struct block *ptr, int fd, char *buf, int len); int openfile(struct block *ptr, char *filename); void printdirectory(struct block *ptr); void printfile(char *filename, struct block *ptr); void printinodeblock(struct block* ptr); - +unsigned long selfcheck2(struct block* d); #define MAXFILES 300 struct filedesc { @@ -101,4 +103,3 @@ struct filedesc { bool used; }; #endif - -- 2.34.1