Skip to content

Commit

Permalink
Output which caches not cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Oct 29, 2024
1 parent 2dc2f1a commit 1d28a50
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public Task PreWarmCaches(Block suggestedBlock, Hash256? parentStateRoot, Access
{
if (preBlockCaches is not null)
{
if (preBlockCaches.ClearCaches())
CacheType result = preBlockCaches.ClearCaches();
if (result != default)
{
if (_logger.IsDebug) _logger.Debug("Caches are not empty. Clearing them.");
if (_logger.IsDebug) _logger.Debug($"Caches {result} are not empty. Clearing them.");
}

var physicalCoreCount = RuntimeInformation.PhysicalCoreCount;
Expand All @@ -54,7 +55,7 @@ public Task PreWarmCaches(Block suggestedBlock, Hash256? parentStateRoot, Access
// Parent state root is null for genesis block
private static bool IsGenesisBlock(Hash256? parentStateRoot) => parentStateRoot is null;

public bool ClearCaches() => preBlockCaches?.ClearCaches() ?? false;
public CacheType ClearCaches() => preBlockCaches?.ClearCaches() ?? default;

private void PreWarmCachesParallel(Block suggestedBlock, Hash256 parentStateRoot, ParallelOptions parallelOptions, AddressWarmer addressWarmer, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ the previous head state.*/
}
else
{
if (preWarmer?.ClearCaches() ?? false)
CacheType result = preWarmer?.ClearCaches() ?? default;
if (result != default)
{
if (_logger.IsDebug) _logger.Debug("Low txs, caches are not empty. Clearing them.");
if (_logger.IsDebug) _logger.Debug($"Low txs, caches {result} are not empty. Clearing them.");
}
// Even though we skip prewarming we still need to ensure the caches are cleared
(processedBlock, receipts) = ProcessOne(suggestedBlock, options, blockTracer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Eip2930;
using Nethermind.State;

namespace Nethermind.Consensus.Processing;

public interface IBlockCachePreWarmer
{
Task PreWarmCaches(Block suggestedBlock, Hash256 parentStateRoot, AccessList? systemTxAccessList, CancellationToken cancellationToken = default);
bool ClearCaches();
CacheType ClearCaches();
}
26 changes: 18 additions & 8 deletions src/Nethermind/Nethermind.State/PreBlockCaches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PreBlockCaches
private const int InitialCapacity = 4096 * 8;
private static int LockPartitions => CollectionExtensions.LockPartitions;

private readonly Func<bool>[] _clearCaches;
private readonly Func<CacheType>[] _clearCaches;

private readonly ConcurrentDictionary<StorageCell, byte[]> _storageCache = new(LockPartitions, InitialCapacity);
private readonly ConcurrentDictionary<AddressAsKey, Account> _stateCache = new(LockPartitions, InitialCapacity);
Expand All @@ -28,10 +28,10 @@ public PreBlockCaches()
{
_clearCaches =
[
_storageCache.NoResizeClear,
_stateCache.NoResizeClear,
_rlpCache.NoResizeClear,
_precompileCache.NoResizeClear
() => _storageCache.NoResizeClear() ? CacheType.Storage : CacheType.None,
() => _stateCache.NoResizeClear() ? CacheType.State : CacheType.None,
() => _rlpCache.NoResizeClear() ? CacheType.Rlp : CacheType.None,
() => _precompileCache.NoResizeClear() ? CacheType.Precompile : CacheType.None
];
}

Expand All @@ -40,10 +40,10 @@ public PreBlockCaches()
public ConcurrentDictionary<NodeKey, byte[]?> RlpCache => _rlpCache;
public ConcurrentDictionary<PrecompileCacheKey, (ReadOnlyMemory<byte>, bool)> PrecompileCache => _precompileCache;

public bool ClearCaches()
public CacheType ClearCaches()
{
bool isDirty = false;
foreach (Func<bool> clearCache in _clearCaches)
CacheType isDirty = CacheType.None;
foreach (Func<CacheType> clearCache in _clearCaches)
{
isDirty |= clearCache();
}
Expand All @@ -60,3 +60,13 @@ public readonly struct PrecompileCacheKey(Address address, ReadOnlyMemory<byte>
public override int GetHashCode() => Data.Span.FastHash() ^ Address.GetHashCode();
}
}

[Flags]
public enum CacheType
{
None = 0,
Storage = 0b1,
State = 0b10,
Rlp = 0b100,
Precompile = 0b1000
}

0 comments on commit 1d28a50

Please sign in to comment.