| 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 i; |
| 14 |
struct timeval begin_time, end_time, elapsed_time; |
| 15 |
struct timeval total_time_mmap, total_time_munmap, total_time_in, total_time_out; |
| 16 |
|
| 17 |
timerclear(&total_time_mmap); |
| 18 |
timerclear(&total_time_munmap); |
| 19 |
timerclear(&total_time_in); |
| 20 |
timerclear(&total_time_out); |
| 21 |
|
| 22 |
for (i = 0; i < test_runs; i++) { |
| 23 |
gettimeofday(&begin_time, NULL); |
| 24 |
buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 25 |
gettimeofday(&end_time, NULL); |
| 26 |
if (buf == ((void *)-1)) { |
| 27 |
fprintf(stderr, "Failed to mmap anonymous memory: %s\n", strerror(errno)); |
| 28 |
goto err_map; |
| 29 |
} |
| 30 |
timersub(&end_time, &begin_time, &elapsed_time); |
| 31 |
timeradd(&total_time_mmap, &elapsed_time, &total_time_mmap); |
| 32 |
|
| 33 |
gettimeofday(&begin_time, NULL); |
| 34 |
munmap(buf, alloc_size); |
| 35 |
gettimeofday(&end_time, NULL); |
| 36 |
timersub(&end_time, &begin_time, &elapsed_time); |
| 37 |
timeradd(&total_time_mmap, &elapsed_time, &total_time_mmap); |
| 38 |
} |
| 39 |
|
| 40 |
printf("mmap: %llu us\n", total_time_mmap.tv_sec * USEC_PER_SEC + total_time_mmap.tv_usec); |
| 41 |
printf("munmap: %llu us\n", total_time_munmap.tv_sec * USEC_PER_SEC + total_time_munmap.tv_usec); |
| 42 |
|
| 43 |
ret = 0; |
| 44 |
goto end; |
| 45 |
// err: |
| 46 |
munmap(buf, alloc_size); // unreached? |
| 47 |
end: |
| 48 |
err_map: |
| 49 |
return ret; |
| 50 |
} |