dbcache.cpp raw

   1  // Copyright (c) The Limenka 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 <common/system_ram.h>
   6  #include <node/dbcache.h>
   7  
   8  #include <algorithm>
   9  #include <cstdint>
  10  #include <optional>
  11  
  12  namespace node {
  13  size_t GetTotalRam() noexcept { return TryGetTotalRam().value_or(FALLBACK_RAM_BYTES); }
  14  
  15  size_t GetDefaultDBCache(std::optional<uint64_t> total_ram) noexcept
  16  {
  17      if (!total_ram) total_ram.emplace(GetTotalRam());
  18      const uint64_t usable{*total_ram > RESERVED_RAM ? *total_ram - RESERVED_RAM : 0};
  19      return std::max(MIN_DEFAULT_DBCACHE, std::min(usable / 4, std::min(MAX_DEFAULT_DBCACHE, MAX_DBCACHE_BYTES)));
  20  }
  21  
  22  bool ShouldWarnOversizedDbCache(uint64_t dbcache, uint64_t total_ram) noexcept
  23  {
  24      const uint64_t headroom{total_ram > RESERVED_RAM ? total_ram - RESERVED_RAM : 0};
  25      return dbcache > std::max<uint64_t>(GetDefaultDBCache(total_ram), (headroom / 4) * 3);
  26  }
  27  } // namespace node
  28