about summary refs log tree commit diff homepage
path: root/unittests/KDAlloc/randomtest.cpp
blob: 1120ee4e419998de0eac2234db9cf80766f9c022 (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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===-- randomtest.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 "xoshiro.h"

#if defined(USE_GTEST_INSTEAD_OF_MAIN)
#include "gtest/gtest.h"
#endif

#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <random>
#include <utility>
#include <vector>

namespace {
class RandomTest {
  xoshiro512 rng;

#if defined(USE_KDALLOC)
  klee::kdalloc::Allocator allocator;
#endif

  std::vector<std::pair<void *, std::size_t>> allocations;

  std::geometric_distribution<std::size_t> allocation_bin_distribution;
  std::geometric_distribution<std::size_t> large_allocation_distribution;

public:
  std::size_t maximum_concurrent_allocations = 0;
  std::uint64_t allocation_count = 0;
  std::uint64_t deallocation_count = 0;

  RandomTest(std::uint64_t seed = 0x31337)
      : rng(seed)
#if defined(USE_KDALLOC)
        ,
        allocator(klee::kdalloc::AllocatorFactory(
            static_cast<std::size_t>(1) << 42, 0))
#endif
        ,
        allocation_bin_distribution(0.3),
        large_allocation_distribution(0.00003) {
  }

  void run(std::uint64_t const iterations) {
    std::uniform_int_distribution<std::uint32_t> choice(0, 999);
    for (std::uint64_t i = 0; i < iterations; ++i) {
      auto chosen = choice(rng);
      if (chosen < 650) {
        ++allocation_count;
        allocate_sized();
      } else if (chosen < 700) {
        ++allocation_count;
        allocate_large();
      } else if (chosen < 1000) {
        ++deallocation_count;
        deallocate();
      }
    }
    cleanup();
  }

  void cleanup() {
    while (!allocations.empty()) {
      auto choice = std::uniform_int_distribution<std::size_t>(
          0, allocations.size() - 1)(rng);
#if defined(USE_KDALLOC)
      allocator.free(allocations[choice].first, allocations[choice].second);
#else
      free(allocations[choice].first);
#endif
      allocations[choice] = allocations.back();
      allocations.pop_back();
    }
  }

  void allocate_sized() {
    auto bin = allocation_bin_distribution(rng);
    while (bin >= 11) {
      bin = allocation_bin_distribution(rng);
    }
    auto min = (bin == 0 ? 1 : (static_cast<std::size_t>(1) << (bin + 1)) + 1);
    auto max = static_cast<std::size_t>(1) << (bin + 2);
    auto size = std::uniform_int_distribution<std::size_t>(min, max)(rng);

#if defined(USE_KDALLOC)
    allocations.emplace_back(allocator.allocate(size), size);
#else
    allocations.emplace_back(std::malloc(size), size);
#endif
    if (allocations.size() > maximum_concurrent_allocations) {
      maximum_concurrent_allocations = allocations.size();
    }
  }

  void allocate_large() {
    auto size = 0;
    while (size <= 4096 || size > 1073741825) {
      size = large_allocation_distribution(rng) + 4097;
    }

#if defined(USE_KDALLOC)
    allocations.emplace_back(allocator.allocate(size), size);
#else
    allocations.emplace_back(std::malloc(size), size);
#endif
    if (allocations.size() > maximum_concurrent_allocations) {
      maximum_concurrent_allocations = allocations.size();
    }
  }

  void deallocate() {
    if (allocations.empty()) {
      return;
    }
    auto choice = std::uniform_int_distribution<std::size_t>(
        0, allocations.size() - 1)(rng);
#if defined(USE_KDALLOC)
    allocator.free(allocations[choice].first, allocations[choice].second);
#else
    free(allocations[choice].first);
#endif
    allocations[choice] = allocations.back();
    allocations.pop_back();
  }
};
} // namespace

#if defined(USE_GTEST_INSTEAD_OF_MAIN)
int random_test() {
#else
int main() {
#endif
#if defined(USE_KDALLOC)
  std::cout << "Using kdalloc\n";
#else
  std::cout << "Using std::malloc\n";
#endif
  auto start = std::chrono::steady_clock::now();

  RandomTest tester;
  tester.run(10'000'000);

  auto stop = std::chrono::steady_clock::now();
  std::cout << std::dec
            << std::chrono::duration_cast<std::chrono::milliseconds>(stop -
                                                                     start)
                   .count()
            << " ms\n";
  std::cout << "\n";

  std::cout << "Allocations: " << tester.allocation_count << "\n";
  std::cout << "Deallocations: " << tester.deallocation_count << "\n";
  std::cout << "Maximum concurrent allocations: "
            << tester.maximum_concurrent_allocations << "\n";

  exit(0);
}

#if defined(USE_GTEST_INSTEAD_OF_MAIN)
TEST(KDAllocDeathTest, Random) {
  ASSERT_EXIT(random_test(), ::testing::ExitedWithCode(0), "");
}
#endif