1 #include <errno.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <sys/mman.h> 6 #include <sys/time.h> 7 8 #include "pagingtest.h" 9 10 int mmap_test(int test_runs, unsigned long long alloc_size) { 11 void *buf; 12 int ret = -1; 13 int rc; 14 int i; 15 struct timeval begin_time, end_time, elapsed_time; 16 struct timeval total_time_mmap, total_time_munmap, total_time_in, total_time_out; 17 18 timerclear(&total_time_mmap); 19 timerclear(&total_time_munmap); 20 timerclear(&total_time_in); 21 timerclear(&total_time_out); 22 23 for (i = 0; i < test_runs; i++) { 24 gettimeofday(&begin_time, NULL); 25 buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 26 gettimeofday(&end_time, NULL); 27 if (buf == ((void *)-1)) { 28 fprintf(stderr, "Failed to mmap anonymous memory: %s\n", strerror(errno)); 29 goto err_map; 30 } 31 timersub(&end_time, &begin_time, &elapsed_time); 32 timeradd(&total_time_mmap, &elapsed_time, &total_time_mmap); 33 34 gettimeofday(&begin_time, NULL); 35 munmap(buf, alloc_size); 36 gettimeofday(&end_time, NULL); 37 timersub(&end_time, &begin_time, &elapsed_time); 38 timeradd(&total_time_mmap, &elapsed_time, &total_time_mmap); 39 } 40 41 printf("mmap: %llu us\n", total_time_mmap.tv_sec * USEC_PER_SEC + total_time_mmap.tv_usec); 42 printf("munmap: %llu us\n", total_time_munmap.tv_sec * USEC_PER_SEC + total_time_munmap.tv_usec); 43 44 ret = 0; 45 goto end; 46 err: 47 munmap(buf, alloc_size); 48 end: 49 err_map: 50 return ret; 51 } 52