lockedpool.cpp raw

   1  // Copyright (c) 2016-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <bench/bench.h>
   6  #include <support/lockedpool.h>
   7  // IWYU incorrectly suggests removing this header.
   8  // See https://github.com/include-what-you-use/include-what-you-use/issues/2014.
   9  #include <util/byte_units.h> // IWYU pragma: keep
  10  
  11  #include <cstddef>
  12  #include <cstdint>
  13  #include <vector>
  14  
  15  #define ASIZE 2048
  16  #define MSIZE 2048
  17  
  18  static void BenchLockedPool(benchmark::Bench& bench)
  19  {
  20      void *synth_base = reinterpret_cast<void*>(0x08000000);
  21      const size_t synth_size{1_MiB};
  22      Arena b(synth_base, synth_size, 16);
  23  
  24      std::vector<void*> addr{ASIZE, nullptr};
  25      uint32_t s = 0x12345678;
  26      bench.run([&] {
  27          int idx = s & (addr.size() - 1);
  28          if (s & 0x80000000) {
  29              b.free(addr[idx]);
  30              addr[idx] = nullptr;
  31          } else if (!addr[idx]) {
  32              addr[idx] = b.alloc((s >> 16) & (MSIZE - 1));
  33          }
  34          bool lsb = s & 1;
  35          s >>= 1;
  36          if (lsb)
  37              s ^= 0xf00f00f0; // LFSR period 0xf7ffffe0
  38      });
  39      for (void *ptr: addr)
  40          b.free(ptr);
  41      addr.clear();
  42  }
  43  
  44  BENCHMARK(BenchLockedPool);
  45