/* test.cc - disk test Uses C++ isms, so compile with 'CC test.cc -lposix4' This test program creates and fills a 4MB file, then processes the equivalent of 1000 TRANS_SIZE KB transactions. A transaction involves writing TRANS_SIZE KB to the file, then calling pwrite(). When the end Input: file name , Transaction size, IO method ( fdatasync, directIO) Output: Average transaction time in us */ #include #include #include #include #include #include #define MB (1024*1024) #define K (1024) #define PAGESIZE (8192) #define BLOCKSIZE (512) #define SLEEP_US (2000) #define FILE_SIZE (4 * MB) typedef long long hrtime_t; char * filename; char * IO_METHOD; unsigned int TRANS_SIZE ; int fd; char *trans; unsigned int cursor; unsigned int file_length; void open_file( int attr); void fill_file( unsigned int); void do_commit( unsigned int); main( int argc, char *argv[]) { if ( argc < 4) { printf ("Usage: test.out [file name] [txn size] [fdatasync/directio]\n"); exit(1); } hrtime_t start, delta; filename= argv[1]; TRANS_SIZE = ((int)atoi(argv[2]) * K); IO_METHOD = argv[3]; open_file( 0); file_length = FILE_SIZE; if (! strcmp(IO_METHOD, "directio") ) { printf("directio in use \n"); directio(fd, DIRECTIO_ON); } else { if ( !strcmp(IO_METHOD, "fdatasync") ) { printf("fdatasync is in use \n"); directio(fd, DIRECTIO_OFF); } else {printf ("Usage: test.out [file name] [txn size] [fdatasync/directIO]\n"); exit(1); } } trans= (char*) memalign( PAGESIZE, MB); unsigned char val = '4'; memset( trans,(int)val , TRANS_SIZE); start= gethrtime(); for ( int i=1000; i; i--) do_commit( TRANS_SIZE); delta= gethrtime() - start; printf( "%lld us\n", delta/1000LL/1000LL); close(fd); return 0; } void open_file( int attr) { fd= open( filename, O_RDWR | O_CREAT | attr, 0644); if ( fd == -1) { perror( filename); exit(1); } } void fill_file( unsigned int length) { char *zero_block; zero_block= (char*) memalign( PAGESIZE, BLOCKSIZE); memset( zero_block, 0, BLOCKSIZE); for ( ; length; length-= BLOCKSIZE) write( fd, zero_block, BLOCKSIZE); lseek( fd, 0, SEEK_SET); fdatasync(fd); free( zero_block); } void do_commit( unsigned int size) { hrtime_t start0; hrtime_t end0 ; start0= gethrtime(); if ( pwrite( fd, trans, size, cursor) != size) { perror( "pwrite"); exit(1); } end0 = gethrtime() - start0; /* when directio is enabled - fdatasync has no effect */ fdatasync(fd); cursor+= size; if ( cursor >= file_length) cursor= 0; }