proxygen
ThreadCachedArenaTest.cpp File Reference
#include <folly/memory/ThreadCachedArena.h>
#include <algorithm>
#include <iterator>
#include <map>
#include <mutex>
#include <random>
#include <thread>
#include <unordered_map>
#include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/Memory.h>
#include <folly/Range.h>
#include <folly/lang/Align.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (ThreadCachedArena, BlockSize)
 
 TEST (ThreadCachedArena, SingleThreaded)
 
 TEST (ThreadCachedArena, MultiThreaded)
 
 TEST (ThreadCachedArena, ThreadCachedArenaAllocator)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 266 of file ThreadCachedArenaTest.cpp.

References testing::InitGoogleTest(), RUN_ALL_TESTS(), and folly::runBenchmarks().

266  {
268  gflags::ParseCommandLineFlags(&argc, &argv, true);
269  auto ret = RUN_ALL_TESTS();
270  if (!ret && FLAGS_benchmark) {
272  }
273  return ret;
274 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
TEST ( ThreadCachedArena  ,
BlockSize   
)

Definition at line 95 of file ThreadCachedArenaTest.cpp.

References folly::ThreadCachedArena::allocate(), EXPECT_LE, folly::max_align_v, and uint8_t.

95  {
96  static const size_t alignment = max_align_v;
97  static const size_t requestedBlockSize = 64;
98 
99  ThreadCachedArena arena(requestedBlockSize);
100  size_t blockSize = alignment;
101  uint8_t* prev = static_cast<uint8_t*>(arena.allocate(1));
102 
103  // Keep allocating until we're no longer one single alignment away from the
104  // previous allocation -- that's when we've gotten to the next block.
105  uint8_t* p;
106  while ((p = static_cast<uint8_t*>(arena.allocate(1))) == prev + alignment) {
107  prev = p;
108  blockSize += alignment;
109  }
110 
111  VLOG(1) << "Requested block size: " << requestedBlockSize
112  << ", actual: " << blockSize;
113  EXPECT_LE(requestedBlockSize, blockSize);
114 }
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
constexpr std::size_t max_align_v
Definition: Align.h:90
TEST ( ThreadCachedArena  ,
SingleThreaded   
)

Definition at line 116 of file ThreadCachedArenaTest.cpp.

References EXPECT_EQ, EXPECT_GT, and folly::ThreadCachedArena::totalSize().

116  {
117  static const size_t requestedBlockSize = 64;
118  ThreadCachedArena arena(requestedBlockSize);
119  EXPECT_EQ(arena.totalSize(), sizeof(ThreadCachedArena));
120 
121  ArenaTester tester(arena);
122  tester.allocate(100, 100 << 10);
123  tester.verify();
124 
125  EXPECT_GT(arena.totalSize(), sizeof(ThreadCachedArena));
126 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_GT(val1, val2)
Definition: gtest.h:1934
TEST ( ThreadCachedArena  ,
MultiThreaded   
)

Definition at line 128 of file ThreadCachedArenaTest.cpp.

References i, folly::gen::move, folly::pushmi::detail::t, and threads.

128  {
129  static const size_t requestedBlockSize = 64;
130  ThreadCachedArena arena(requestedBlockSize);
131  ArenaTester mainTester(arena);
132 
133  // Do this twice, to catch the possibility that memory from the first
134  // round gets freed
135  static const size_t numThreads = 20;
136  for (size_t i = 0; i < 2; i++) {
137  std::vector<std::thread> threads;
138  threads.reserve(numThreads);
139  for (size_t j = 0; j < numThreads; j++) {
140  threads.emplace_back([&arena, &mainTester]() {
141  ArenaTester tester(arena);
142  tester.allocate(500, 1 << 10);
143  tester.verify();
144  mainTester.merge(std::move(tester));
145  });
146  }
147  for (auto& t : threads) {
148  t.join();
149  }
150  }
151 
152  mainTester.verify();
153 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::vector< std::thread::id > threads

Definition at line 155 of file ThreadCachedArenaTest.cpp.

References folly::BENCHMARK(), folly::BENCHMARK_DRAW_LINE(), EXPECT_EQ, i, and map().

155  {
156  using Map = std::unordered_map<
157  int,
158  int,
159  std::hash<int>,
160  std::equal_to<int>,
162 
163  static const size_t requestedBlockSize = 64;
164  ThreadCachedArena arena(requestedBlockSize);
165 
166  Map map{0,
167  std::hash<int>(),
168  std::equal_to<int>(),
169  ThreadCachedArenaAllocator<std::pair<const int, int>>(arena)};
170 
171  for (int i = 0; i < 1000; i++) {
172  map[i] = i;
173  }
174 
175  for (int i = 0; i < 1000; i++) {
176  EXPECT_EQ(i, map[i]);
177  }
178 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::unordered_map< int64_t, VecT > Map
Definition: Traits.h:594