Skip to content

Commit cd54c85

Browse files
committed
feat(metrics): add timing instrumentation for cross-client execution metrics
Pipe timing data to EvmMetrics for state_read_ms, state_hash_ms, and commit_ms: - WorldStateMetricsDecorator: pipe existing timing to EvmMetrics for RecalculateStateRoot, Commit (both overloads), and CommitTree - StateProvider.GetState: add timing around DB reads (not cache hits) - PersistentStorageProvider.LoadFromTree: add timing for storage reads Uses Stopwatch.GetTimestamp()/GetElapsedTime() with TimeSpan.Ticks (100ns precision) for minimal overhead (~10-20ns per measurement).
1 parent 1c74158 commit cd54c85

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/Nethermind/Nethermind.State/PersistentStorageProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.Diagnostics.CodeAnalysis;
89
using System.Linq;
910
using System.Runtime.CompilerServices;
@@ -352,9 +353,12 @@ public void WarmUp(in StorageCell storageCell, bool isEmpty)
352353

353354
private ReadOnlySpan<byte> LoadFromTree(in StorageCell storageCell)
354355
{
356+
long start = Stopwatch.GetTimestamp();
355357
// Track storage read for execution metrics
356358
EvmMetrics.IncrementStorageReads();
357-
return GetOrCreateStorage(storageCell.Address).LoadFromTree(storageCell);
359+
ReadOnlySpan<byte> result = GetOrCreateStorage(storageCell.Address).LoadFromTree(storageCell);
360+
EvmMetrics.AddStateReadTime(Stopwatch.GetElapsedTime(start).Ticks);
361+
return result;
358362
}
359363

360364
private void PushToRegistryOnly(in StorageCell cell, byte[] value)

src/Nethermind/Nethermind.State/StateProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,12 @@ public bool WarmUp(Address address)
718718
ref ChangeTrace accountChanges = ref CollectionsMarshal.GetValueRefOrAddDefault(_blockChanges, addressAsKey, out bool exists);
719719
if (!exists)
720720
{
721+
long start = Stopwatch.GetTimestamp();
721722
Metrics.IncrementStateTreeReads();
722723
// Track account read for execution metrics (DB reads only, not cache hits)
723724
EvmMetrics.IncrementAccountReads();
724725
Account? account = _tree.Get(address);
726+
EvmMetrics.AddStateReadTime(Stopwatch.GetElapsedTime(start).Ticks);
725727

726728
accountChanges = new(account, account);
727729
}

src/Nethermind/Nethermind.State/WorldStateMetricsDecorator.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Nethermind.Evm.State;
1212
using Nethermind.Evm.Tracing.State;
1313
using Nethermind.Int256;
14+
using EvmMetrics = Nethermind.Evm.Metrics;
1415

1516
namespace Nethermind.State;
1617

@@ -48,7 +49,10 @@ public void RecalculateStateRoot()
4849
{
4950
long start = Stopwatch.GetTimestamp();
5051
innerState.RecalculateStateRoot();
51-
StateMerkleizationTime += Stopwatch.GetElapsedTime(start).TotalMilliseconds;
52+
TimeSpan elapsed = Stopwatch.GetElapsedTime(start);
53+
StateMerkleizationTime += elapsed.TotalMilliseconds;
54+
// Pipe to cross-client execution metrics
55+
EvmMetrics.AddStateHashTime(elapsed.Ticks);
5256
}
5357

5458
public Hash256 StateRoot => innerState.StateRoot;
@@ -88,23 +92,41 @@ public void Commit(IReleaseSpec releaseSpec, bool isGenesis = false, bool commit
8892
{
8993
long start = Stopwatch.GetTimestamp();
9094
innerState.Commit(releaseSpec, isGenesis, commitRoots);
95+
TimeSpan elapsed = Stopwatch.GetElapsedTime(start);
9196
if (commitRoots)
92-
StateMerkleizationTime += Stopwatch.GetElapsedTime(start).TotalMilliseconds;
97+
{
98+
StateMerkleizationTime += elapsed.TotalMilliseconds;
99+
EvmMetrics.AddStateHashTime(elapsed.Ticks);
100+
}
101+
else
102+
{
103+
EvmMetrics.AddCommitTime(elapsed.Ticks);
104+
}
93105
}
94106

95107
public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer tracer, bool isGenesis = false, bool commitRoots = true)
96108
{
97109
long start = Stopwatch.GetTimestamp();
98110
innerState.Commit(releaseSpec, tracer, isGenesis, commitRoots);
111+
TimeSpan elapsed = Stopwatch.GetElapsedTime(start);
99112
if (commitRoots)
100-
StateMerkleizationTime += Stopwatch.GetElapsedTime(start).TotalMilliseconds;
113+
{
114+
StateMerkleizationTime += elapsed.TotalMilliseconds;
115+
EvmMetrics.AddStateHashTime(elapsed.Ticks);
116+
}
117+
else
118+
{
119+
EvmMetrics.AddCommitTime(elapsed.Ticks);
120+
}
101121
}
102122

103123
public void CommitTree(long blockNumber)
104124
{
105125
long start = Stopwatch.GetTimestamp();
106126
innerState.CommitTree(blockNumber);
107-
StateMerkleizationTime += Stopwatch.GetElapsedTime(start).TotalMilliseconds;
127+
TimeSpan elapsed = Stopwatch.GetElapsedTime(start);
128+
StateMerkleizationTime += elapsed.TotalMilliseconds;
129+
EvmMetrics.AddStateHashTime(elapsed.Ticks);
108130
}
109131

110132
public ArrayPoolList<AddressAsKey>? GetAccountChanges() => innerState.GetAccountChanges();

0 commit comments

Comments
 (0)