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

Limit contended paths to physical cores not logical cores #7132

Merged
merged 3 commits into from
Jun 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Core.Threading;
using Nethermind.Core.Cpu;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
Expand Down Expand Up @@ -54,7 +55,7 @@ private void PreWarmCachesParallel(Block suggestedBlock, Hash256 parentStateRoot

try
{
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount - 2), CancellationToken = cancellationToken };
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = Math.Max(1, RuntimeInformation.PhysicalCoreCount - 2), CancellationToken = cancellationToken };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make this one specifically configurable.

IReleaseSpec spec = specProvider.GetSpec(suggestedBlock.Header);

WarmupTransactions(parallelOptions, spec, suggestedBlock, parentStateRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System;
using System.Diagnostics;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal class ConsoleExitHandler : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// Derived from https://github.com/dotnet/BenchmarkDotNet
// Licensed under the MIT License

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal class CpuInfo
public class CpuInfo
{
public string ProcessorName { get; }
public int? PhysicalProcessorCount { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using System.Globalization;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static class DefaultCultureInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

using System.Globalization;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal readonly struct Frequency
public readonly struct Frequency
{
public static readonly Frequency Zero = new Frequency(0.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// Derived from https://github.com/AndreyAkinshin/perfolizer
// Licensed under the MIT License

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal class FrequencyUnit
public class FrequencyUnit
{
public static readonly FrequencyUnit Hz = new FrequencyUnit("Hz", "Hertz", 1L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Derived from https://github.com/dotnet/BenchmarkDotNet
// Licensed under the MIT License

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static class ProcCpuInfoKeyNames
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static partial class ProcCpuInfoParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System;
using System.Linq;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

/// <summary>
/// CPU information from output of the `cat /proc/info` command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Diagnostics;
using System.IO;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

// we need it public to reuse it in the auto-generated dll
// but we hide it from intellisense with following attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System;
using System.Diagnostics;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static class ProcessHelper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// Licensed under the MIT License

using System;
using System.Threading.Tasks;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static class RuntimeInformation
public static class RuntimeInformation
{
[System.Runtime.Versioning.SupportedOSPlatformGuard("windows")]
internal static bool IsWindows() => OperatingSystem.IsWindows(); // prefer linker-friendly OperatingSystem APIs
Expand All @@ -19,7 +20,7 @@ internal static class RuntimeInformation
[System.Runtime.Versioning.SupportedOSPlatformGuard("macos")]
internal static bool IsMacOS() => OperatingSystem.IsMacOS();

internal static CpuInfo? GetCpuInfo()
public static CpuInfo? GetCpuInfo()
{
if (IsWindows())
return WmicCpuInfoProvider.WmicCpuInfo.Value;
Expand All @@ -31,5 +32,9 @@ internal static class RuntimeInformation
return null;
}

public static int PhysicalCoreCount { get; } = GetCpuInfo()?.PhysicalCoreCount ?? Environment.ProcessorCount;
public static ParallelOptions ParallelOptionsPhysicalCores { get; } = new() { MaxDegreeOfParallelism = PhysicalCoreCount };
public static ParallelOptions ParallelOptionsLogicalCores { get; } = new() { MaxDegreeOfParallelism = Environment.ProcessorCount };

public static bool Is64BitPlatform() => IntPtr.Size == 8;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static partial class SectionsHelper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using System.Collections.Generic;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static class SysctlCpuInfoParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using System;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

/// <summary>
/// CPU information from output of the `sysctl -a` command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

using System.Globalization;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal readonly struct TimeInterval
public readonly struct TimeInterval
{
public static readonly TimeInterval Nanosecond = TimeUnit.Nanosecond.ToInterval(1L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using System;
using System.Linq;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal class TimeUnit : IEquatable<TimeUnit>
public class TimeUnit : IEquatable<TimeUnit>
{
public static readonly TimeUnit Nanosecond = new TimeUnit("ns", "Nanosecond", 1L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// Derived from https://github.com/AndreyAkinshin/perfolizer
// Licensed under the MIT License

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal class UnitPresentation
public class UnitPresentation
{
public static readonly UnitPresentation Default = new UnitPresentation(isVisible: true, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Derived from https://github.com/dotnet/BenchmarkDotNet
// Licensed under the MIT License

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static class WmicCpuInfoKeyNames
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using System.Collections.Generic;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

internal static class WmicCpuInfoParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using System;

namespace Nethermind.Init.Cpu;
namespace Nethermind.Core.Cpu;

/// <summary>
/// CPU information from output of the `wmic cpu get Name, NumberOfCores, NumberOfLogicalProcessors /Format:List` command.
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Init/Steps/LogHardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Api;
using Nethermind.Core.Cpu;
using ILogger = Nethermind.Logging.ILogger;

namespace Nethermind.Init.Steps;
Expand All @@ -25,7 +26,7 @@ public Task Execute(CancellationToken cancellationToken)

try
{
var cpu = Cpu.RuntimeInformation.GetCpuInfo();
var cpu = RuntimeInformation.GetCpuInfo();
if (cpu is not null)
{
_logger.Info($"CPU: {cpu.ProcessorName} ({cpu.PhysicalCoreCount}C{cpu.LogicalCoreCount}T)");
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.State/PersistentStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Nethermind.State
{
using Nethermind.Core.Cpu;
/// <summary>
/// Manages persistent storage allowing for snapshotting and restoring
/// Persists data to ITrieStore
Expand Down Expand Up @@ -263,7 +264,7 @@ void UpdateRootHashesSingleThread()
void UpdateRootHashesMultiThread()
{
// We can recalculate the roots in parallel as they are all independent tries
Parallel.ForEach(_storages, kvp =>
Parallel.ForEach(_storages, RuntimeInformation.ParallelOptionsLogicalCores, kvp =>
{
if (!_toUpdateRoots.Contains(kvp.Key))
{
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Trie/PatriciaTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Core.Buffers;
using Nethermind.Core.Cpu;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Logging;
Expand Down Expand Up @@ -227,7 +228,7 @@ private void Commit(NodeCommitInfo nodeCommitInfo, bool skipSelf = false)
if (nodesToCommit.Count >= 4)
{
ClearExceptions();
Parallel.For(0, nodesToCommit.Count, i =>
Parallel.For(0, nodesToCommit.Count, RuntimeInformation.ParallelOptionsLogicalCores, i =>
{
try
{
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Trie/Pruning/TrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Nethermind.Trie.Pruning
{
using Nethermind.Core.Cpu;
/// <summary>
/// Trie store helps to manage trie commits block by block.
/// If persistence and pruning are needed they have a chance to execute their behaviour on commits.
Expand Down Expand Up @@ -1275,7 +1276,7 @@ void PersistNode(TrieNode n, Hash256? address, TreePath path)
n.IsPersisted = true;
}
}
Parallel.For(0, nodesCopy.Length, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount / 2 }, i =>
Parallel.For(0, nodesCopy.Length, RuntimeInformation.ParallelOptionsPhysicalCores, i =>
{
if (cancellationToken.IsCancellationRequested) return;
DirtyNodesCache.Key key = nodesCopy[i].Key;
Expand Down
5 changes: 3 additions & 2 deletions src/Nethermind/Nethermind.Trie/TrieNode.Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Core.Buffers;
using Nethermind.Core.Cpu;
using Nethermind.Core.Crypto;
using Nethermind.Serialization.Rlp;
using Nethermind.Trie.Pruning;
Expand Down Expand Up @@ -174,7 +175,7 @@ private static int GetChildrenRlpLengthForBranchParallel(ITrieNodeResolver tree,
private static int GetChildrenRlpLengthForBranchNonRlpParallel(ITrieNodeResolver tree, TreePath rootPath, TrieNode item, ICappedArrayPool bufferPool)
{
int totalLength = 0;
Parallel.For(0, BranchesCount,
Parallel.For(0, BranchesCount, RuntimeInformation.ParallelOptionsLogicalCores,
() => 0,
(i, _, local) =>
{
Expand Down Expand Up @@ -235,7 +236,7 @@ private static int GetChildrenRlpLengthForBranchNonRlp(ITrieNodeResolver tree, r
private static int GetChildrenRlpLengthForBranchRlpParallel(ITrieNodeResolver tree, TreePath rootPath, TrieNode item, ICappedArrayPool? bufferPool)
{
int totalLength = 0;
Parallel.For(0, BranchesCount,
Parallel.For(0, BranchesCount, RuntimeInformation.ParallelOptionsLogicalCores,
() => 0,
(i, _, local) =>
{
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Trie/TrieNode.Visitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Core.Buffers;
using Nethermind.Core.Cpu;
using Nethermind.Core.Crypto;
using Nethermind.Serialization.Rlp;
using Nethermind.Trie.Pruning;
Expand Down Expand Up @@ -192,7 +193,7 @@ void VisitMultiThread(TreePath parentPath, ITreeVisitor<TNodeContext> treeVisito
var copy = nodeContext;

// multithreaded route
Parallel.For(0, BranchesCount, i =>
Parallel.For(0, BranchesCount, RuntimeInformation.ParallelOptionsPhysicalCores, i =>
{
visitContext.Semaphore.Wait();
try
Expand Down