Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inacccurate memory metric due to concurrent writes #7600

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Nethermind/Nethermind.Trie.Test/Pruning/TreeStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,32 @@ public void Memory_with_two_nodes_is_correct()
trieNode2.GetMemorySize(false) + ExpectedPerNodeKeyMemorySize);
}

[Test]
public void Memory_with_concurrent_commits_is_correct()
{
using TrieStore fullTrieStore = CreateTrieStore(pruningStrategy: new TestPruningStrategy(true));

IScopedTrieStore trieStore = fullTrieStore.GetTrieStore(null);
PatriciaTree tree = new PatriciaTree(trieStore, LimboLogs.Instance);

Random rand = new Random(0);

Span<byte> key = stackalloc byte[32];
Span<byte> value = stackalloc byte[32];
for (int i = 0; i < 1000; i++)
{
rand.NextBytes(key);
rand.NextBytes(value);

tree.Set(key, value.ToArray());
}

tree.Commit(0);

fullTrieStore.MemoryUsedByDirtyCache.Should().Be(_scheme == INodeStorage.KeyScheme.Hash ? 591672L : 661820L);
fullTrieStore.CommittedNodesCount.Should().Be(1349);
}

[Test]
public void Memory_with_two_times_two_nodes_is_correct()
{
Expand Down
19 changes: 17 additions & 2 deletions src/Nethermind/Nethermind.Trie/Pruning/TrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public long MemoryUsedByDirtyCache
}
}

public void IncrementMemoryUsedByDirtyCache(long nodeMemoryUsage)
{
Metrics.MemoryUsedByCache = Interlocked.Add(ref _memoryUsedByDirtyCache, nodeMemoryUsage);
}

public int CommittedNodesCount
{
get => _committedNodesCount;
Expand All @@ -129,6 +134,11 @@ private set
}
}

private void IncrementCommittedNodesCount()
{
Metrics.CommittedNodesCount = Interlocked.Increment(ref _committedNodesCount);
}

public int PersistedNodesCount
{
get => _persistedNodesCount;
Expand All @@ -139,6 +149,11 @@ private set
}
}

private void IncrementPersistedNodesCount()
{
Metrics.PersistedNodeCount = Interlocked.Increment(ref _persistedNodesCount);
}

public int CachedNodesCount
{
get
Expand Down Expand Up @@ -178,7 +193,7 @@ private void CommitNode(long blockNumber, Hash256? address, ref TreePath path, i
PersistNode(address, path, node, blockNumber, writeFlags);
}

CommittedNodesCount++;
IncrementCommittedNodesCount();
}

[MethodImpl(MethodImplOptions.NoInlining)]
Expand Down Expand Up @@ -858,7 +873,7 @@ private void PersistNode(Hash256? address, in TreePath path, TrieNode currentNod
writeBatch.Set(address, path, currentNode.Keccak, currentNode.FullRlp, writeFlags);
currentNode.IsPersisted = true;
currentNode.LastSeen = Math.Max(blockNumber, currentNode.LastSeen);
PersistedNodesCount++;
IncrementPersistedNodesCount();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void SaveInCache(in Key key, TrieNode node)
if (TryAdd(key, node))
{
Metrics.CachedNodesCount = Interlocked.Increment(ref _count);
_trieStore.MemoryUsedByDirtyCache += node.GetMemorySize(false) + KeyMemoryUsage;
_trieStore.IncrementMemoryUsedByDirtyCache(node.GetMemorySize(false) + KeyMemoryUsage);
}
}

Expand Down
Loading