Skip to content

Commit

Permalink
♻️ move templated method definitions for MemoryManagerStatistics to h…
Browse files Browse the repository at this point in the history
…eader file for use in other packages
  • Loading branch information
Joshy-R committed Mar 9, 2025
1 parent 19b4c10 commit c874258
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 109 deletions.
89 changes: 89 additions & 0 deletions include/mqt-core/dd/statistics/MemoryManagerStatistics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include "dd/statistics/Statistics.hpp"
#include "nlohmann/json_fwd.hpp"

#include <algorithm>
#include <cstddef>
#include <nlohmann/json.hpp>

namespace dd {

Expand Down Expand Up @@ -73,4 +75,91 @@ template <typename T> struct MemoryManagerStatistics : public Statistics {
[[nodiscard]] nlohmann::json json() const override;
};

///-----------------------------------------------------------------------------
/// \n Method Definitions \n
///-----------------------------------------------------------------------------

template <typename T>
std::size_t
MemoryManagerStatistics<T>::getNumAvailableFromChunks() const noexcept {
return getTotalNumAvailable() - numAvailableForReuse;
}

template <typename T>
std::size_t MemoryManagerStatistics<T>::getTotalNumAvailable() const noexcept {
return numAllocated - numUsed;
}

template <typename T>
double MemoryManagerStatistics<T>::getUsageRatio() const noexcept {
return static_cast<double>(numUsed) / static_cast<double>(numAllocated);
}

template <typename T>
double MemoryManagerStatistics<T>::getAllocatedMemoryMiB() const noexcept {
return static_cast<double>(numAllocated) * ENTRY_MEMORY_MIB;
}

template <typename T>
double MemoryManagerStatistics<T>::getUsedMemoryMiB() const noexcept {
return static_cast<double>(numUsed) * ENTRY_MEMORY_MIB;
}

template <typename T>
double MemoryManagerStatistics<T>::getPeakUsedMemoryMiB() const noexcept {
return static_cast<double>(peakNumUsed) * ENTRY_MEMORY_MIB;
}

template <typename T>
void MemoryManagerStatistics<T>::trackUsedEntries(
const std::size_t numEntries) noexcept {
numUsed += numEntries;
peakNumUsed = std::max(peakNumUsed, numUsed);
}

template <typename T>
void MemoryManagerStatistics<T>::trackReusedEntries(
const std::size_t numEntries) noexcept {
numUsed += numEntries;
peakNumUsed = std::max(peakNumUsed, numUsed);
numAvailableForReuse -= numEntries;
}

template <typename T>
void MemoryManagerStatistics<T>::trackReturnedEntry() noexcept {
++numAvailableForReuse;
peakNumAvailableForReuse =
std::max(peakNumAvailableForReuse, numAvailableForReuse);
--numUsed;
}

template <typename T> void MemoryManagerStatistics<T>::reset() noexcept {
numAllocations = 0U;
numAllocated = 0U;
numUsed = 0U;
numAvailableForReuse = 0U;
}

template <typename T>
nlohmann::basic_json<> MemoryManagerStatistics<T>::json() const {
if (peakNumUsed == 0) {
return "unused";
}

auto j = Statistics::json();
j["memory_allocated_MiB"] = getAllocatedMemoryMiB();
j["memory_used_MiB"] = getUsedMemoryMiB();
j["memory_used_MiB_peak"] = getPeakUsedMemoryMiB();
j["num_allocated"] = numAllocated;
j["num_allocations"] = numAllocations;
j["num_available_for_reuse"] = numAvailableForReuse;
j["num_available_for_reuse_peak"] = peakNumAvailableForReuse;
j["num_available_from_chunks"] = getNumAvailableFromChunks();
j["num_available_total"] = getTotalNumAvailable();
j["num_used"] = numUsed;
j["num_used_peak"] = peakNumUsed;
j["usage_ratio"] = getUsageRatio();
return j;
}

} // namespace dd
109 changes: 0 additions & 109 deletions src/dd/statistics/MemoryManagerStatistics.cpp

This file was deleted.

0 comments on commit c874258

Please sign in to comment.