Skip to content

Commit

Permalink
🚸 Small corrections for statistics tracking (#424)
Browse files Browse the repository at this point in the history
## Description

Another small follow-up to #419 that squashed another couple of quirks
in the statistics tracking.

## Checklist:

<!---
This checklist serves as a reminder of a couple of things that ensure
your pull request will be merged swiftly.
-->

- [x] The pull request only contains commits that are related to it.
- [x] I have added appropriate tests and documentation.
- [x] I have made sure that all CI jobs on GitHub pass.
- [x] The pull request introduces no new warnings and follows the
project's style guidelines.

---------

Signed-off-by: burgholzer <[email protected]>
  • Loading branch information
burgholzer authored Sep 14, 2023
1 parent 7f3c2e3 commit 52813cc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
6 changes: 3 additions & 3 deletions include/dd/UniqueTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ template <class Node, std::size_t NBUCKET = 32768> class UniqueTable {
}

std::size_t garbageCollect(bool force = false) {
if ((!force && !possiblyNeedsCollection()) || getNumEntries() == 0) {
return 0;
const std::size_t numEntriesBefore = getNumEntries();
if ((!force && numEntriesBefore < gcLimit) || numEntriesBefore == 0U) {
return 0U;
}

const std::size_t numEntriesBefore = getNumEntries();
std::size_t v = 0U;
for (auto& table : tables) {
auto& stat = stats[v];
Expand Down
11 changes: 4 additions & 7 deletions src/dd/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@ template <typename T>
void MemoryManager<T>::reset(const bool resizeToTotal) noexcept {
available = nullptr;

chunks.erase(chunks.begin() + 1, chunks.end());
for (auto& entry : chunks[0]) {
entry.ref = 0U;
}
auto numAllocations = stats.numAllocations;
chunks.resize(1U);
if (resizeToTotal) {
chunks[0].resize(stats.numAllocated);
++numAllocations;
}

chunkIt = chunks[0].begin();
chunkEndIt = chunks[0].end();

stats.reset();
stats.numAllocations = 1U;
stats.numAllocations = numAllocations;
stats.numAllocated = chunks[0].size();
}

Expand All @@ -103,8 +102,6 @@ T* MemoryManager<T>::getEntryFromAvailableList() noexcept {
auto* entry = available;
available = available->next;
stats.trackReusedEntries();
// Reclaimed entries might have a non-zero reference count
entry->ref = 0;
return entry;
}

Expand Down
19 changes: 10 additions & 9 deletions src/dd/statistics/MemoryManagerStatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace dd {
template <typename T>
std::size_t
MemoryManagerStatistics<T>::getNumAvailableFromChunks() const noexcept {
return numAllocated - numUsed;
return getTotalNumAvailable() - numAvailableForReuse;
}

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

template <typename T>
Expand Down Expand Up @@ -75,17 +75,18 @@ template <typename T> nlohmann::json MemoryManagerStatistics<T>::json() const {
}

nlohmann::json j = Statistics::json();
j["num_allocations"] = numAllocations;
j["num_allocated"] = numAllocated;
j["memory_allocated_MiB"] = getAllocatedMemoryMiB();
j["num_used"] = numUsed;
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["total_num_available"] = getTotalNumAvailable();
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();
j["peak_num_used"] = peakNumUsed;
j["peak_num_available_for_reuse"] = peakNumAvailableForReuse;
j["peak_memory_used_MiB"] = getPeakUsedMemoryMiB();
return j;
}

Expand Down
23 changes: 12 additions & 11 deletions test/dd/test_complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,34 +474,35 @@ TEST_F(CNTest, MaxRefCountReached) {
}

TEST_F(CNTest, ComplexTableAllocation) {
auto allocs = mm.getStats().numAllocated;
auto mem = MemoryManager<RealNumber>{};
auto allocs = mem.getStats().numAllocated;
std::cout << allocs << "\n";
std::vector<RealNumber*> nums{allocs};
// get all the numbers that are pre-allocated
for (auto i = 0U; i < allocs; ++i) {
nums[i] = mm.get();
nums[i] = mem.get();
}

// trigger new allocation
const auto* num = mm.get();
const auto* num = mem.get();
ASSERT_NE(num, nullptr);
EXPECT_EQ(mm.getStats().numAllocated,
EXPECT_EQ(mem.getStats().numAllocated,
(1. + MemoryManager<RealNumber>::GROWTH_FACTOR) *
static_cast<fp>(allocs));

// clearing the complex table should reduce the allocated size to the original
// size
mm.reset();
EXPECT_EQ(mm.getStats().numAllocated, allocs);
mem.reset();
EXPECT_EQ(mem.getStats().numAllocated, allocs);

EXPECT_EQ(mm.getStats().numAvailableForReuse, 0U);
EXPECT_EQ(mem.getStats().numAvailableForReuse, 0U);
// obtain entry
auto* entry = mm.get();
auto* entry = mem.get();
// immediately return entry
mm.returnEntry(entry);
EXPECT_EQ(mm.getStats().numAvailableForReuse, 1U);
mem.returnEntry(entry);
EXPECT_EQ(mem.getStats().numAvailableForReuse, 1U);
// obtain the same entry again, but this time from the available stack
auto* entry2 = mm.get();
auto* entry2 = mem.get();
EXPECT_EQ(entry, entry2);
}

Expand Down

0 comments on commit 52813cc

Please sign in to comment.