From 270f187e2aad578ec9b47ca70075e5627af031d9 Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Thu, 11 Jul 2024 22:32:41 +0200 Subject: [PATCH] wip --- include/util/meminfo.hpp | 16 +++++++++++++--- include/util/pool_allocator.hpp | 29 ++++++++++------------------- include/util/query_heap.hpp | 8 ++------ src/benchmarks/bench.cpp | 4 +++- test/data/Makefile | 2 +- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/include/util/meminfo.hpp b/include/util/meminfo.hpp index e2059975150..4c3e16f0b7a 100644 --- a/include/util/meminfo.hpp +++ b/include/util/meminfo.hpp @@ -2,6 +2,7 @@ #define MEMINFO_HPP #include "util/log.hpp" +#include #ifndef _WIN32 #include @@ -10,18 +11,27 @@ namespace osrm::util { -inline void DumpMemoryStats() +inline size_t PeakRAMUsedInBytes() { #ifndef _WIN32 rusage usage; getrusage(RUSAGE_SELF, &usage); #ifdef __linux__ // Under linux, ru.maxrss is in kb - util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss * 1024; + return usage.ru_maxrss * 1024; #else // __linux__ // Under BSD systems (OSX), it's in bytes - util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss; + return usage.ru_maxrss; #endif // __linux__ +#else // _WIN32 + return 0; +#endif // _WIN32 +} + +inline void DumpMemoryStats() +{ +#ifndef _WIN32 + util::Log() << "RAM: peak bytes used: " << PeakRAMUsedInBytes(); #else // _WIN32 util::Log() << "RAM: peak bytes used: "; #endif // _WIN32 diff --git a/include/util/pool_allocator.hpp b/include/util/pool_allocator.hpp index fc2d71b0655..a0203b941a9 100644 --- a/include/util/pool_allocator.hpp +++ b/include/util/pool_allocator.hpp @@ -14,14 +14,14 @@ namespace osrm::util { -#if 1 - -template +template class PoolAllocator; -template +template class MemoryManager { +private: + constexpr static size_t MIN_ITEMS_IN_BLOCK = 1024; public: static std::shared_ptr instance() { @@ -82,7 +82,7 @@ class MemoryManager void allocate_block(size_t items_in_block) { - items_in_block = std::max(items_in_block, MinItemsInBlock); + items_in_block = std::max(items_in_block, MIN_ITEMS_IN_BLOCK); size_t block_size = items_in_block * sizeof(T); T *block = static_cast(std::malloc(block_size)); @@ -104,21 +104,21 @@ class MemoryManager size_t total_allocated_ = 0; }; -template +template class PoolAllocator { public: using value_type = T; - PoolAllocator() noexcept : pool(MemoryManager::instance()) {}; + PoolAllocator() noexcept : pool(MemoryManager::instance()) {}; template - PoolAllocator(const PoolAllocator &) noexcept : pool(MemoryManager::instance()) {} + PoolAllocator(const PoolAllocator &) noexcept : pool(MemoryManager::instance()) {} template struct rebind { - using other = PoolAllocator; + using other = PoolAllocator; }; T *allocate(std::size_t n) @@ -139,13 +139,7 @@ class PoolAllocator PoolAllocator &operator=(PoolAllocator &&) noexcept = default; private: - std::shared_ptr> pool; - - static size_t get_next_power_of_two_exponent(size_t n) - { - BOOST_ASSERT(n > 0); - return (sizeof(size_t) * 8) - std::countl_zero(n - 1); - } + std::shared_ptr> pool; }; template @@ -160,7 +154,4 @@ bool operator!=(const PoolAllocator &, const PoolAllocator &) return false; } -#else -template using PoolAllocator = std::allocator; -#endif } // namespace osrm::util diff --git a/include/util/query_heap.hpp b/include/util/query_heap.hpp index c79ffc19b6a..44f18355334 100644 --- a/include/util/query_heap.hpp +++ b/include/util/query_heap.hpp @@ -194,8 +194,7 @@ template , - bool ThreadLocal = true> + typename IndexStorage = ArrayStorage> class QueryHeap { private: @@ -214,16 +213,13 @@ class QueryHeap } }; - using AllocatorType = typename std::conditional, - std::allocator>::type; using HeapContainer = boost::heap::d_ary_heap, boost::heap::mutable_, boost::heap::compare>, - boost::heap::allocator>; + boost::heap::allocator>>; using HeapHandle = typename HeapContainer::handle_type; public: diff --git a/src/benchmarks/bench.cpp b/src/benchmarks/bench.cpp index 019ff645659..2bf62905e9a 100644 --- a/src/benchmarks/bench.cpp +++ b/src/benchmarks/bench.cpp @@ -12,7 +12,7 @@ #include "osrm/coordinate.hpp" #include "osrm/engine_config.hpp" #include "osrm/json_container.hpp" - +#include "util/meminfo.hpp" #include "osrm/osrm.hpp" #include "osrm/status.hpp" @@ -655,6 +655,8 @@ try std::cerr << "Unknown benchmark: " << benchmarkToRun << std::endl; return EXIT_FAILURE; } + + std::cerr << "Peak RAM: " << osrm::util::PeakRAMUsedInBytes() / (1024 * 1024) << "MB" << std::endl; return EXIT_SUCCESS; } catch (const std::exception &e) diff --git a/test/data/Makefile b/test/data/Makefile index 8d0748b546b..b79e87fe0e2 100755 --- a/test/data/Makefile +++ b/test/data/Makefile @@ -65,4 +65,4 @@ benchmark: data $(DATA_NAME).requests checksum: $(MD5SUM) $(DATA_NAME).osm.pbf $(DATA_NAME).poly > data.md5sum -.PHONY: clean checksum data +.PHONY: clean checksum benchmark data \ No newline at end of file