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

Cache message to Debug logs #7677

Merged
merged 8 commits into from
Nov 24, 2024
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.IsWarn) _logger.Warn("Caches are not empty. Clearing them.");
if (_logger.IsWarn) _logger.Warn($"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 @@ -132,9 +132,10 @@ the previous head state.*/
}
else
{
if (preWarmer?.ClearCaches() ?? false)
CacheType result = preWarmer?.ClearCaches() ?? default;
if (result != default)
{
if (_logger.IsWarn) _logger.Warn("Low txs, caches are not empty. Clearing them.");
if (_logger.IsWarn) _logger.Warn($"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
}