From 6400b1521b76698e1489f614d9ff251323706f0c Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 11 Mar 2024 11:51:13 -0400 Subject: [PATCH] allocator stats --- CMakeLists.txt | 7 +++- include/blt/std/allocator.h | 77 ++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ad45dcd..9de8b41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5) include(cmake/color.cmake) -set(BLT_VERSION 0.14.11) +set(BLT_VERSION 0.14.12) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) @@ -21,6 +21,7 @@ option(BUILD_PARSE "Build the BLT parsers" ON) option(BUILD_TESTS "Build the BLT test set" OFF) +option(BLT_DISABLE_STATS "Disable tracking stats in certain objects. Enabling this will cause stat functions to return 0" OFF) option(BLT_DISABLE_LOGGING "Disable blt::logging (all macros and will safely disable logging function!)" OFF) option(BLT_DISABLE_TRACE "Disable blt::logging BLT_TRACE macro" OFF) option(BLT_DISABLE_DEBUG "Disable blt::logging BLT_DEBUG macro" OFF) @@ -29,6 +30,10 @@ option(BLT_DISABLE_WARN "Disable blt::logging BLT_WARN macro" OFF) option(BLT_DISABLE_ERROR "Disable blt::logging BLT_ERROR macro" OFF) option(BLT_DISABLE_FATAL "Disable blt::logging BLT_FATAL macro" OFF) +if(${BLT_DISABLE_STATS}) + add_compile_definitions(BLT_DISABLE_STATS) +endif () + configure_file(include/blt/config.h.in config/blt/config.h @ONLY) message("Enabling library compilation") diff --git a/include/blt/std/allocator.h b/include/blt/std/allocator.h index 3488d7f..3f8dac5 100644 --- a/include/blt/std/allocator.h +++ b/include/blt/std/allocator.h @@ -564,8 +564,62 @@ namespace blt { return reinterpret_cast(reinterpret_cast(p) & static_cast(~(BLOCK_SIZE - 1))); } - + + class stats_t + { + private: + blt::size_t allocated_blocks = 0; + blt::size_t allocated_bytes = 0; + blt::size_t peak_blocks = 0; + blt::size_t peak_bytes = 0; + public: + inline void incrementBlocks() + { + allocated_blocks++; + if (allocated_blocks > peak_blocks) + peak_blocks = allocated_blocks; + } + + inline void decrementBlocks() + { + allocated_blocks--; + } + + inline void incrementBytes(blt::size_t bytes) + { + allocated_bytes += bytes; + if (allocated_bytes > peak_bytes) + peak_bytes = allocated_bytes; + } + + inline void decrementBytes(blt::size_t bytes) + { + allocated_bytes -= bytes; + } + + inline auto getAllocatedBlocks() + { + return allocated_blocks; + } + + inline auto getAllocatedBytes() + { + return allocated_bytes; + } + + inline auto getPeakBlocks() + { + return peak_blocks; + } + + inline auto getPeakBytes() + { + return peak_bytes; + } + }; private: + stats_t stats; + /** * Logging function used for handling mmap errors. call after a failed mmap call. * @param LOG_FUNC function to log with, must be a BLT_*_STREAM @@ -701,6 +755,9 @@ namespace blt buffer = reinterpret_cast(std::aligned_alloc(BLOCK_SIZE, BLOCK_SIZE)); #endif construct(buffer); +#ifndef BLT_DISABLE_STATS + stats.incrementBlocks(); +#endif return buffer; } @@ -760,6 +817,9 @@ namespace blt */ inline void delete_block(block* p) { +#ifndef BLT_DISABLE_STATS + stats.decrementBlocks(); +#endif if constexpr (USE_HUGE) { if (munmap(p, BLOCK_SIZE)) @@ -770,6 +830,7 @@ namespace blt } else free(p); } + public: bump_allocator() = default; @@ -791,6 +852,10 @@ namespace blt { if constexpr (sizeof(T) > BLOCK_REMAINDER) throw std::bad_alloc(); + +#ifndef BLT_DISABLE_STATS + stats.incrementBytes(sizeof(T) * count); +#endif T* ptr = allocate_object(count); if (ptr != nullptr) @@ -808,10 +873,13 @@ namespace blt * @param p pointer to deallocate */ template - void deallocate(T* p) + void deallocate(T* p, blt::size_t count = 1) { if (p == nullptr) return; +#ifndef BLT_DISABLE_STATS + stats.decrementBytes(sizeof(T) * count); +#endif auto blk = to_block(p); if (--blk->metadata.allocated_objects == 0) { @@ -898,6 +966,11 @@ namespace blt deallocate(p); } + inline const auto& getStats() + { + return stats; + } + ~bump_allocator() { block* next = base;