Guest User

Untitled

a guest
Dec 30th, 2018
2,705
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/mman.h>
  5. #include <fcntl.h>
  6. #include <sys/time.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9.  
  10.  
  11. #define BUFSZ 1000000000  // 10GB
  12. // adjust above for smaller tests
  13. // requires 10GB io_benchmark.dat file in the working directory filled with random data
  14. static unsigned char buf[BUFSZ];
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.         unsigned int total, msize, offset, secs;
  19.         int fd, i, j, rslt;
  20.         void * p;
  21.         char c;
  22.         struct timeval start, end;
  23.  
  24. // Time 1000 mmaps and munmaps from random offsets for various
  25. // sizes of mapped chunk.
  26.         for (msize = 4096; msize <= 1048576; msize *= 16) {
  27.                 gettimeofday(&start, NULL);
  28.                 fd = open("io_benchmark.dat", O_RDONLY); // file
  29.                 if (fd  < 0 ) abort();
  30.                 for (i = 0; i < 1000; i++) {
  31.                 // Make sure the block to be mapped doesn't start in the
  32.                 // last meg and is multiply of 4096.
  33.                 offset = (off_t) random() % (BUFSZ - 1048576) /10000 * 4096;
  34.                 p = mmap(0, (size_t) msize, PROT_READ, MAP_PRIVATE, fd, offset);
  35.                 if (p == MAP_FAILED){
  36.                         printf("map failed: %s offset: %i \n", strerror(errno), offset);
  37.                 }
  38.                 for (j = 1; j < msize; j++) {
  39.                         c = *((char *)p+j);
  40.                         }
  41.                 munmap(p, (size_t) msize);
  42.                 }
  43.         gettimeofday(&end, NULL);
  44.         secs = ((end.tv_sec * 1000) + (end.tv_usec /1000)) - ((start.tv_sec * 1000) + (start.tv_usec /1000)) ;
  45.         printf("1000 mmaps %d kB blocks: %i ms\n", msize/1024, secs);
  46.   rslt = close(fd);
  47.   if (fd  < 0 ) abort();
  48.   }
  49.     return 0;
  50. }
RAW Paste Data