summaryrefslogtreecommitdiffhomepage
path: root/test/perf/linux/mapping_benchmark.cc
blob: 39c30fe697dea7a9976eab42343e3bc90c5f2103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

#include "gtest/gtest.h"
#include "benchmark/benchmark.h"
#include "test/util/logging.h"
#include "test/util/memory_util.h"
#include "test/util/posix_error.h"
#include "test/util/test_util.h"

namespace gvisor {
namespace testing {

namespace {

// Conservative value for /proc/sys/vm/max_map_count, which limits the number of
// VMAs, minus a safety margin for VMAs that already exist for the test binary.
// The default value for max_map_count is
// include/linux/mm.h:DEFAULT_MAX_MAP_COUNT = 65530.
constexpr size_t kMaxVMAs = 64001;

// Map then unmap pages without touching them.
void BM_MapUnmap(benchmark::State& state) {
  // Number of pages to map.
  const int pages = state.range(0);

  while (state.KeepRunning()) {
    void* addr = mmap(0, pages * kPageSize, PROT_READ | PROT_WRITE,
                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    TEST_CHECK_MSG(addr != MAP_FAILED, "mmap failed");

    int ret = munmap(addr, pages * kPageSize);
    TEST_CHECK_MSG(ret == 0, "munmap failed");
  }
}

BENCHMARK(BM_MapUnmap)->Range(1, 1 << 17)->UseRealTime();

// Map, touch, then unmap pages.
void BM_MapTouchUnmap(benchmark::State& state) {
  // Number of pages to map.
  const int pages = state.range(0);

  while (state.KeepRunning()) {
    void* addr = mmap(0, pages * kPageSize, PROT_READ | PROT_WRITE,
                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    TEST_CHECK_MSG(addr != MAP_FAILED, "mmap failed");

    char* c = reinterpret_cast<char*>(addr);
    char* end = c + pages * kPageSize;
    while (c < end) {
      *c = 42;
      c += kPageSize;
    }

    int ret = munmap(addr, pages * kPageSize);
    TEST_CHECK_MSG(ret == 0, "munmap failed");
  }
}

BENCHMARK(BM_MapTouchUnmap)->Range(1, 1 << 17)->UseRealTime();

// Map and touch many pages, unmapping all at once.
//
// NOTE(b/111429208): This is a regression test to ensure performant mapping and
// allocation even with tons of mappings.
void BM_MapTouchMany(benchmark::State& state) {
  // Number of pages to map.
  const int page_count = state.range(0);

  while (state.KeepRunning()) {
    std::vector<void*> pages;

    for (int i = 0; i < page_count; i++) {
      void* addr = mmap(nullptr, kPageSize, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
      TEST_CHECK_MSG(addr != MAP_FAILED, "mmap failed");

      char* c = reinterpret_cast<char*>(addr);
      *c = 42;

      pages.push_back(addr);
    }

    for (void* addr : pages) {
      int ret = munmap(addr, kPageSize);
      TEST_CHECK_MSG(ret == 0, "munmap failed");
    }
  }

  state.SetBytesProcessed(kPageSize * page_count * state.iterations());
}

BENCHMARK(BM_MapTouchMany)->Range(1, 1 << 12)->UseRealTime();

void BM_PageFault(benchmark::State& state) {
  // Map the region in which we will take page faults. To ensure that each page
  // fault maps only a single page, each page we touch must correspond to a
  // distinct VMA. Thus we need a 1-page gap between each 1-page VMA. However,
  // each gap consists of a PROT_NONE VMA, instead of an unmapped hole, so that
  // if there are background threads running, they can't inadvertently creating
  // mappings in our gaps that are unmapped when the test ends.
  size_t test_pages = kMaxVMAs;
  // Ensure that test_pages is odd, since we want the test region to both
  // begin and end with a mapped page.
  if (test_pages % 2 == 0) {
    test_pages--;
  }
  const size_t test_region_bytes = test_pages * kPageSize;
  // Use MAP_SHARED here because madvise(MADV_DONTNEED) on private mappings on
  // gVisor won't force future sentry page faults (by design). Use MAP_POPULATE
  // so that Linux pre-allocates the shmem file used to back the mapping.
  Mapping m = ASSERT_NO_ERRNO_AND_VALUE(
      MmapAnon(test_region_bytes, PROT_READ, MAP_SHARED | MAP_POPULATE));
  for (size_t i = 0; i < test_pages / 2; i++) {
    ASSERT_THAT(
        mprotect(reinterpret_cast<void*>(m.addr() + ((2 * i + 1) * kPageSize)),
                 kPageSize, PROT_NONE),
        SyscallSucceeds());
  }

  const size_t mapped_pages = test_pages / 2 + 1;
  // "Start" at the end of the mapped region to force the mapped region to be
  // reset, since we mapped it with MAP_POPULATE.
  size_t cur_page = mapped_pages;
  for (auto _ : state) {
    if (cur_page >= mapped_pages) {
      // We've reached the end of our mapped region and have to reset it to
      // incur page faults again.
      state.PauseTiming();
      ASSERT_THAT(madvise(m.ptr(), test_region_bytes, MADV_DONTNEED),
                  SyscallSucceeds());
      cur_page = 0;
      state.ResumeTiming();
    }
    const uintptr_t addr = m.addr() + (2 * cur_page * kPageSize);
    const char c = *reinterpret_cast<volatile char*>(addr);
    benchmark::DoNotOptimize(c);
    cur_page++;
  }
}

BENCHMARK(BM_PageFault)->UseRealTime();

}  // namespace

}  // namespace testing
}  // namespace gvisor