allocator stats

v1
Brett 2024-03-11 11:51:13 -04:00
parent 263bbc88cf
commit 6400b1521b
2 changed files with 81 additions and 3 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
include(cmake/color.cmake) 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_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT) 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(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_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_TRACE "Disable blt::logging BLT_TRACE macro" OFF)
option(BLT_DISABLE_DEBUG "Disable blt::logging BLT_DEBUG 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_ERROR "Disable blt::logging BLT_ERROR macro" OFF)
option(BLT_DISABLE_FATAL "Disable blt::logging BLT_FATAL 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) configure_file(include/blt/config.h.in config/blt/config.h @ONLY)
message("Enabling library compilation") message("Enabling library compilation")

View File

@ -564,8 +564,62 @@ namespace blt
{ {
return reinterpret_cast<block*>(reinterpret_cast<std::uintptr_t>(p) & static_cast<std::uintptr_t>(~(BLOCK_SIZE - 1))); return reinterpret_cast<block*>(reinterpret_cast<std::uintptr_t>(p) & static_cast<std::uintptr_t>(~(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: private:
stats_t stats;
/** /**
* Logging function used for handling mmap errors. call after a failed mmap call. * 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 * @param LOG_FUNC function to log with, must be a BLT_*_STREAM
@ -701,6 +755,9 @@ namespace blt
buffer = reinterpret_cast<block*>(std::aligned_alloc(BLOCK_SIZE, BLOCK_SIZE)); buffer = reinterpret_cast<block*>(std::aligned_alloc(BLOCK_SIZE, BLOCK_SIZE));
#endif #endif
construct(buffer); construct(buffer);
#ifndef BLT_DISABLE_STATS
stats.incrementBlocks();
#endif
return buffer; return buffer;
} }
@ -760,6 +817,9 @@ namespace blt
*/ */
inline void delete_block(block* p) inline void delete_block(block* p)
{ {
#ifndef BLT_DISABLE_STATS
stats.decrementBlocks();
#endif
if constexpr (USE_HUGE) if constexpr (USE_HUGE)
{ {
if (munmap(p, BLOCK_SIZE)) if (munmap(p, BLOCK_SIZE))
@ -770,6 +830,7 @@ namespace blt
} else } else
free(p); free(p);
} }
public: public:
bump_allocator() = default; bump_allocator() = default;
@ -791,6 +852,10 @@ namespace blt
{ {
if constexpr (sizeof(T) > BLOCK_REMAINDER) if constexpr (sizeof(T) > BLOCK_REMAINDER)
throw std::bad_alloc(); throw std::bad_alloc();
#ifndef BLT_DISABLE_STATS
stats.incrementBytes(sizeof(T) * count);
#endif
T* ptr = allocate_object<T>(count); T* ptr = allocate_object<T>(count);
if (ptr != nullptr) if (ptr != nullptr)
@ -808,10 +873,13 @@ namespace blt
* @param p pointer to deallocate * @param p pointer to deallocate
*/ */
template<typename T> template<typename T>
void deallocate(T* p) void deallocate(T* p, blt::size_t count = 1)
{ {
if (p == nullptr) if (p == nullptr)
return; return;
#ifndef BLT_DISABLE_STATS
stats.decrementBytes(sizeof(T) * count);
#endif
auto blk = to_block(p); auto blk = to_block(p);
if (--blk->metadata.allocated_objects == 0) if (--blk->metadata.allocated_objects == 0)
{ {
@ -898,6 +966,11 @@ namespace blt
deallocate(p); deallocate(p);
} }
inline const auto& getStats()
{
return stats;
}
~bump_allocator() ~bump_allocator()
{ {
block* next = base; block* next = base;