Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <errno.h>
- #define BUFSZ 1000000000 // 10GB
- // adjust above for smaller tests
- // requires 10GB io_benchmark.dat file in the working directory filled with random data
- static unsigned char buf[BUFSZ];
- int main(int argc, char *argv[])
- {
- unsigned int total, msize, offset, secs;
- int fd, i, j, rslt;
- void * p;
- char c;
- struct timeval start, end;
- // Time 1000 mmaps and munmaps from random offsets for various
- // sizes of mapped chunk.
- for (msize = 4096; msize <= 1048576; msize *= 16) {
- gettimeofday(&start, NULL);
- fd = open("io_benchmark.dat", O_RDONLY); // file
- if (fd < 0 ) abort();
- for (i = 0; i < 1000; i++) {
- // Make sure the block to be mapped doesn't start in the
- // last meg and is multiply of 4096.
- offset = (off_t) random() % (BUFSZ - 1048576) /10000 * 4096;
- p = mmap(0, (size_t) msize, PROT_READ, MAP_PRIVATE, fd, offset);
- if (p == MAP_FAILED){
- printf("map failed: %s offset: %i \n", strerror(errno), offset);
- }
- for (j = 1; j < msize; j++) {
- c = *((char *)p+j);
- }
- munmap(p, (size_t) msize);
- }
- gettimeofday(&end, NULL);
- secs = ((end.tv_sec * 1000) + (end.tv_usec /1000)) - ((start.tv_sec * 1000) + (start.tv_usec /1000)) ;
- printf("1000 mmaps %d kB blocks: %i ms\n", msize/1024, secs);
- rslt = close(fd);
- if (fd < 0 ) abort();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement