mmap_bench.c 1.6 KB

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