0001
0002
0003
0004
0005
0006 #include <stdio.h>
0007 #include <stdlib.h>
0008 #include <sys/mman.h>
0009 #include <time.h>
0010 #include <getopt.h>
0011
0012 #include "utils.h"
0013
0014 #define ITERATIONS 5000000
0015
0016 #define MEMSIZE (1UL << 27)
0017 #define PAGE_SIZE (1UL << 16)
0018 #define CHUNK_COUNT (MEMSIZE/PAGE_SIZE)
0019
0020 static int pg_fault;
0021 static int iterations = ITERATIONS;
0022
0023 static struct option options[] = {
0024 { "pgfault", no_argument, &pg_fault, 1 },
0025 { "iterations", required_argument, 0, 'i' },
0026 { 0, },
0027 };
0028
0029 static void usage(void)
0030 {
0031 printf("mmap_bench <--pgfault> <--iterations count>\n");
0032 }
0033
0034 int test_mmap(void)
0035 {
0036 struct timespec ts_start, ts_end;
0037 unsigned long i = iterations;
0038
0039 clock_gettime(CLOCK_MONOTONIC, &ts_start);
0040
0041 while (i--) {
0042 char *c = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE,
0043 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
0044 FAIL_IF(c == MAP_FAILED);
0045 if (pg_fault) {
0046 int count;
0047 for (count = 0; count < CHUNK_COUNT; count++)
0048 c[count << 16] = 'c';
0049 }
0050 munmap(c, MEMSIZE);
0051 }
0052
0053 clock_gettime(CLOCK_MONOTONIC, &ts_end);
0054
0055 printf("time = %.6f\n", ts_end.tv_sec - ts_start.tv_sec + (ts_end.tv_nsec - ts_start.tv_nsec) / 1e9);
0056
0057 return 0;
0058 }
0059
0060 int main(int argc, char *argv[])
0061 {
0062 signed char c;
0063 while (1) {
0064 int option_index = 0;
0065
0066 c = getopt_long(argc, argv, "", options, &option_index);
0067
0068 if (c == -1)
0069 break;
0070
0071 switch (c) {
0072 case 0:
0073 if (options[option_index].flag != 0)
0074 break;
0075
0076 usage();
0077 exit(1);
0078 break;
0079 case 'i':
0080 iterations = atoi(optarg);
0081 break;
0082 default:
0083 usage();
0084 exit(1);
0085 }
0086 }
0087
0088 test_harness_set_timeout(300);
0089 return test_harness(test_mmap, "mmap_bench");
0090 }