#define _LARGEFILE64_SOURC #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main (int argc, char *argv[]) { if (argc<6){ cout << "usage " << endl; return 0; } char* dev_name = argv[1]; int fd = open(dev_name, O_LARGEFILE | O_DIRECT | O_WRONLY , 777 ); if (fd<0){ perror("open "); return (-1); } long long write_sz_bytes = ( (long long)atoi(argv[2]))<<10; long long offset_sz_bytes = ( (long long) atoi(argv[3]) )<<10; long long diskSizeBytes = ( (long long)atoi(argv[4]))<<30; int loops = atoi(argv[5]); struct iovec vec[10]; int blocks = (write_sz_bytes >>20); for( int i = 0 ; i < blocks; i++){ char* buffer = (char*)valloc((1<<20)); if (!buffer) { perror("alloc : "); return -1; } vec[i].iov_base = buffer; vec[i].iov_len = 1048576; memset(buffer,0x00,1048576); } int ret=0; while( (--loops)>0 ){ if ( lseek64(fd,offset_sz_bytes,SEEK_SET) < 0 ){ printf("%s: failed on lseek offset=%lld\n",offset_sz_bytes); return (0); } ret = writev(fd,(struct iovec*)&vec,blocks); if ( ret != write_sz_bytes ) { perror("failed to write: "); printf("write size=%lld offset=%lld\n",write_sz_bytes,offset_sz_bytes); return -1; } offset_sz_bytes = write_sz_bytes *( random() % diskSizeBytes ); long long rnd = (long long)random(); offset_sz_bytes = write_sz_bytes * (long long)( rnd % diskSizeBytes ); if(offset_sz_bytes>diskSizeBytes){ offset_sz_bytes = (offset_sz_bytes - diskSizeBytes ) % diskSizeBytes; offset_sz_bytes = (offset_sz_bytes/write_sz_bytes)*write_sz_bytes; } printf("writing %d bytes at offset %lld\n",ret,offset_sz_bytes); } return(0); }