about summary refs log tree commit diff homepage
path: root/unittests/KDAlloc/rusage.cpp
blob: 453c5e5d74854e03de5cdb94b08c8b2707883312 (plain) (blame)
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
//===-- rusage.cpp --------------------------------------------------------===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "klee/KDAlloc/kdalloc.h"

#include "gtest/gtest.h"

#include <vector>

#include <sys/resource.h>

// This test is disabled for asan and msan because they create additional page
// faults
#if !defined(__has_feature) ||                                                 \
    (!__has_feature(memory_sanitizer) && !__has_feature(address_sanitizer))

std::size_t write_to_allocations(std::vector<void *> &allocations) {
  struct rusage ru;
  getrusage(RUSAGE_SELF, &ru);
  auto initial = ru.ru_minflt;

  for (auto p : allocations) {
    auto pp = static_cast<char *>(p);
    *pp = 1;
  }

  getrusage(RUSAGE_SELF, &ru);
  return ru.ru_minflt - initial;
}

TEST(KDAllocTest, Rusage) {
  // initialize a factory and an associated allocator (using the location "0"
  // gives an OS-assigned location)
  klee::kdalloc::AllocatorFactory factory(static_cast<std::size_t>(1) << 30,
                                          0); // 1 GB
  klee::kdalloc::Allocator allocator = factory.makeAllocator();

  std::vector<void *> allocations;
  for (std::size_t i = 0; i < 1000; ++i) {
    allocations.emplace_back(allocator.allocate(16));
  }

  // When writing to allocations we should have at least one page fault per
  // object, but may have more due to unrelated behavior (e.g., swapping). In
  // the hopes that such interference is relatively rare, we try a few times to
  // encounter a perfect run, where the number of page faults matches the number
  // of allocated objects exactly.
  constexpr std::size_t tries = 100;
  for (std::size_t i = 0; i < tries; ++i) {
    auto pageFaults = write_to_allocations(allocations);
    if (pageFaults == allocations.size()) {
      // we have a perfect match
      return;
    }

    ASSERT_GE(pageFaults, allocations.size())
        << "There should be at least as many page faults as allocated objects";

    factory.getMapping().clear();
  }

  FAIL()
      << "No single try out of " << tries
      << " yielded a perfect match between page faults and allocated objects";
}

#endif