91 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Copyright 2016, Anton Blanchard, Michael Ellerman, IBM Corp.4 */5 6#include <stdio.h>7#include <stdlib.h>8#include <sys/mman.h>9#include <time.h>10#include <getopt.h>11 12#include "utils.h"13 14#define ITERATIONS 500000015 16#define MEMSIZE (1UL << 27)17#define PAGE_SIZE (1UL << 16)18#define CHUNK_COUNT (MEMSIZE/PAGE_SIZE)19 20static int pg_fault;21static int iterations = ITERATIONS;22 23static struct option options[] = {24 { "pgfault", no_argument, &pg_fault, 1 },25 { "iterations", required_argument, 0, 'i' },26 { 0, },27};28 29static void usage(void)30{31 printf("mmap_bench <--pgfault> <--iterations count>\n");32}33 34int test_mmap(void)35{36 struct timespec ts_start, ts_end;37 unsigned long i = iterations;38 39 clock_gettime(CLOCK_MONOTONIC, &ts_start);40 41 while (i--) {42 char *c = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE,43 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);44 FAIL_IF(c == MAP_FAILED);45 if (pg_fault) {46 int count;47 for (count = 0; count < CHUNK_COUNT; count++)48 c[count << 16] = 'c';49 }50 munmap(c, MEMSIZE);51 }52 53 clock_gettime(CLOCK_MONOTONIC, &ts_end);54 55 printf("time = %.6f\n", ts_end.tv_sec - ts_start.tv_sec + (ts_end.tv_nsec - ts_start.tv_nsec) / 1e9);56 57 return 0;58}59 60int main(int argc, char *argv[])61{62 signed char c;63 while (1) {64 int option_index = 0;65 66 c = getopt_long(argc, argv, "", options, &option_index);67 68 if (c == -1)69 break;70 71 switch (c) {72 case 0:73 if (options[option_index].flag != 0)74 break;75 76 usage();77 exit(1);78 break;79 case 'i':80 iterations = atoi(optarg);81 break;82 default:83 usage();84 exit(1);85 }86 }87 88 test_harness_set_timeout(300);89 return test_harness(test_mmap, "mmap_bench");90}91