From 987e875520c589ece276460e72a22c5b3cdb3f68 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Thu, 10 Feb 2022 18:26:11 +0100 Subject: [PATCH] FlatHashMap: support mem_stat->rss in memory benchmark; move memory benchmark in other folder, fix build. --- CMakeLists.txt | 2 +- benchmark/CMakeLists.txt | 17 +++++ .../test => benchmark}/hashset_memory.cpp | 62 +++++++++++++++---- memprof/memprof.cpp | 10 +-- tdutils/CMakeLists.txt | 4 -- 5 files changed, 72 insertions(+), 23 deletions(-) rename {tdutils/test => benchmark}/hashset_memory.cpp (70%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 228dc8f7d..3bd72c996 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -744,7 +744,7 @@ if (MEMPROF) endif() endif() -add_library(memprof_stat STATIC ${MEMPROF_STAT_SOURCE}) +add_library(memprof_stat EXCLUDE_FROM_ALL STATIC ${MEMPROF_STAT_SOURCE}) target_include_directories(memprof_stat PUBLIC $) target_link_libraries(memprof_stat PRIVATE tdutils) diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index c4f17cae9..e8608df9b 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -72,3 +72,20 @@ if (NOT WIN32 AND NOT CYGWIN) add_executable(bench_queue bench_queue.cpp) target_link_libraries(bench_queue PRIVATE tdutils) endif() + +find_package(ABSL QUIET) +find_package(benchmark QUIET) +find_package(gflags QUIET) +find_package(folly QUIET) + +if (ABSL_FOUND AND benchmark_FOUND AND gflags_FOUND AND folly_FOUND) + add_executable(memory-hashset-memprof EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/hashset_memory.cpp) + target_compile_definitions(memory-hashset-memprof PRIVATE USE_MEMPROF=1) + target_link_libraries(memory-hashset-memprof PRIVATE tdutils memprof_stat) + target_link_libraries(memory-hashset-memprof PRIVATE SYSTEM Folly::folly absl::flat_hash_map absl::hash) + + add_executable(memory-hashset-os ${CMAKE_CURRENT_SOURCE_DIR}/hashset_memory.cpp) + target_compile_definitions(memory-hashset-os PRIVATE USE_MEMPROF=0) + target_link_libraries(memory-hashset-os PRIVATE tdutils) + target_link_libraries(memory-hashset-os PRIVATE SYSTEM Folly::folly absl::flat_hash_map absl::hash) +endif() diff --git a/tdutils/test/hashset_memory.cpp b/benchmark/hashset_memory.cpp similarity index 70% rename from tdutils/test/hashset_memory.cpp rename to benchmark/hashset_memory.cpp index 55f82f626..a9ee5f9be 100644 --- a/tdutils/test/hashset_memory.cpp +++ b/benchmark/hashset_memory.cpp @@ -4,19 +4,43 @@ // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include "memprof/memprof.h" + +#if USE_MEMPROF +#include "memprof/memprof_stat.h" +#endif #include "td/utils/check.h" #include "td/utils/FlatHashMap.h" #include "td/utils/format.h" +#include "td/utils/misc.h" +#include "td/utils/port/Stat.h" #include "td/utils/Slice.h" -#include "td/utils/UInt.h" #include #include #include #include +int mem_stat_i = -1; +int mem_stat_cur = 0; +bool use_memprof() { + return mem_stat_i < 0 +#if USE_MEMPROF + && is_memprof_on() +#endif + ; +} +auto get_memory() { +#if USE_MEMPROF + if (use_memprof()) { + return get_used_memory_size(); + } +#else + CHECK(!use_memprof()); + return td::mem_stat().ok().resident_size_; +#endif +}; + template class Generator { public: @@ -64,7 +88,11 @@ class Generator> { template void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slice value_name) { - sb << name << "<" << key_name << "," << value_name << ">:\n"; + mem_stat_cur++; + if (mem_stat_i >= 0 && mem_stat_cur != mem_stat_i) { + return; + } + sb << name << "<" << key_name << "," << value_name << "> " << (use_memprof() ? "memprof" : "os") << "\n"; size_t ideal_size = sizeof(KeyT) + sizeof(ValueT) + Generator::dyn_size(); sb << "\tempty:" << sizeof(T); @@ -78,12 +106,11 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic for (size_t size : {10000000u}) { Generator key_generator; Generator value_generator; - auto start_mem = get_used_memory_size(); + auto start_mem = get_memory(); T ht; auto ratio = [&]() { - auto end_mem = get_used_memory_size(); + auto end_mem = get_memory(); auto used_mem = end_mem - start_mem; - //return double(used_mem); return double(used_mem) / double(ideal_size * ht.size()); }; double min_ratio; @@ -118,19 +145,19 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic sb << "\n"; } - template using Bytes = std::array; -template class T> +template