-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Linux RAPL counters #2284 #3032
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
Draft
wild0ne
wants to merge
3
commits into
dotnet:master
Choose a base branch
from
wild0ne:poc-rapl-energy-counters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+364
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/BenchmarkDotNet.Diagnostics.Energy/BenchmarkDotNet.Diagnostics.Energy.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <Import Project="..\..\build\common.props" /> | ||
| <Import Project="..\..\build\common.targets" /> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0;net462</TargetFrameworks> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <NoWarn>$(NoWarn);1591</NoWarn> | ||
| <AssemblyName>BenchmarkDotNet.Diagnostics.Energy</AssemblyName> | ||
| <PackageId>BenchmarkDotNet.Diagnostics.Energy</PackageId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\BenchmarkDotNet\BenchmarkDotNet.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace BenchmarkDotNet.Diagnosers | ||
| { | ||
| internal abstract class EnergyCounter | ||
| { | ||
| public EnergyCounter(string name, string id) | ||
| { | ||
| Name = !string.IsNullOrEmpty(name) ? name : throw new ArgumentException(nameof(name)); | ||
| Id = !string.IsNullOrEmpty(id) ? id : throw new ArgumentException(nameof(id)); | ||
| } | ||
|
|
||
| public abstract (bool, string) TestRead(); | ||
|
|
||
| public abstract void FixStart(); | ||
|
|
||
| public abstract void FixFinish(); | ||
|
|
||
| public abstract long GetValue(); | ||
|
|
||
| public string Name { get; protected set; } | ||
|
|
||
| public string Id { get; protected set; } | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
src/BenchmarkDotNet.Diagnostics.Energy/EnergyCounterDiscovery.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // #define FAKE_RAPL | ||
|
|
||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace BenchmarkDotNet.Diagnosers | ||
| { | ||
| internal static class EnergyCounterDiscovery | ||
| { | ||
| public static IEnumerable<EnergyCounter> Discover(EnergyCountersSetup setup) | ||
| { | ||
| #if FAKE_RAPL | ||
| return Filter(GetFake(), setup); | ||
| #else | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
| return Filter(DiscoverLinux(), setup); | ||
| else | ||
| throw new NotImplementedException(string.Format("RAPL support for {0} is not implemented yet", RuntimeInformation.OSDescription)); | ||
| #endif | ||
| } | ||
|
|
||
| private static IEnumerable<EnergyCounter> Filter(IEnumerable<EnergyCounter> counters, EnergyCountersSetup setup) | ||
| { | ||
| switch (setup) | ||
| { | ||
| case EnergyCountersSetup.All: | ||
| return counters; | ||
|
|
||
| case EnergyCountersSetup.Default: | ||
| return counters.Where(c => c.Name == "core"); | ||
|
|
||
| default: | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| private static IEnumerable<EnergyCounter> DiscoverLinux() | ||
| { | ||
| const int MAX_PACKAGES = int.MaxValue; | ||
| const int MAX_PACKAGE_UNITS = int.MaxValue; | ||
|
|
||
| for (int i = 0; i < MAX_PACKAGES; i++) | ||
| { | ||
| string path = $"/sys/class/powercap/intel-rapl/intel-rapl:{i}"; | ||
| if (LinuxEnergyCounter.IsValid(path)) | ||
| { | ||
| yield return LinuxEnergyCounter.FromPath(path); | ||
|
|
||
| for (int j = 0; j < MAX_PACKAGE_UNITS; j++) | ||
| { | ||
| path = $"/sys/class/powercap/intel-rapl/intel-rapl:{i}/intel-rapl:{i}:{j}"; | ||
| if (LinuxEnergyCounter.IsValid(path)) | ||
| yield return LinuxEnergyCounter.FromPath(path); | ||
| else | ||
| break; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #if FAKE_RAPL | ||
| private static IEnumerable<EnergyCounter> GetFake() | ||
| { | ||
| yield return new FakeEnergyCounter("package-0", 17995193, "intel-rapl:0"); | ||
| yield return new FakeEnergyCounter("core", 9955052, "intel-rapl:0/intel-rapl:0:0"); | ||
| yield return new FakeEnergyCounter("dram", 1858455, "intel-rapl:0/intel-rapl:0:1"); | ||
| yield return new FakeEnergyCounter("uncore", 773924, "intel-rapl:0/intel-rapl:0:2"); | ||
| } | ||
|
|
||
| private class FakeEnergyCounter : EnergyCounter | ||
| { | ||
| private long _value; | ||
|
|
||
| public FakeEnergyCounter(string name, long value, string id) : base(name, id) { | ||
| _value = value; | ||
| } | ||
|
|
||
| public override (bool, string) TestRead() => (true, string.Empty); | ||
|
|
||
| public override void FixStart() {} | ||
|
|
||
| public override void FixFinish() {} | ||
|
|
||
| public override long GetValue() => _value; | ||
| } | ||
| #endif | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/BenchmarkDotNet.Diagnostics.Energy/EnergyCountersSetup.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace BenchmarkDotNet.Diagnosers | ||
| { | ||
| /// <summary> | ||
| /// Energy counters setup | ||
| /// </summary> | ||
| public enum EnergyCountersSetup | ||
| { | ||
| /// <summary> | ||
| /// Default setup (core only) | ||
| /// </summary> | ||
| Default = 0, | ||
|
|
||
| /// <summary> | ||
| /// All discovered counters | ||
| /// </summary> | ||
| All = 1, | ||
| } | ||
| } |
113 changes: 113 additions & 0 deletions
113
src/BenchmarkDotNet.Diagnostics.Energy/EnergyDiagnoser.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| using System.Diagnostics; | ||
| using BenchmarkDotNet.Analysers; | ||
| using BenchmarkDotNet.Columns; | ||
| using BenchmarkDotNet.Engines; | ||
| using BenchmarkDotNet.Exporters; | ||
| using BenchmarkDotNet.Loggers; | ||
| using BenchmarkDotNet.Reports; | ||
| using BenchmarkDotNet.Running; | ||
| using BenchmarkDotNet.Validators; | ||
|
|
||
| namespace BenchmarkDotNet.Diagnosers | ||
| { | ||
| public class EnergyDiagnoser : IDiagnoser | ||
| { | ||
| private EnergyCounter[]? _counters; | ||
|
|
||
| private bool _validationFailed = false; | ||
|
|
||
| private const string DiagnoserId = nameof(EnergyDiagnoser); | ||
|
|
||
| public static readonly EnergyDiagnoser Default = new EnergyDiagnoser(new EnergyDiagnoserConfig()); | ||
|
|
||
| public EnergyDiagnoser(EnergyDiagnoserConfig config) => Config = config; | ||
|
|
||
| public EnergyDiagnoserConfig Config { get; } | ||
|
|
||
| public RunMode GetRunMode(BenchmarkCase benchmarkCase) => RunMode.NoOverhead; | ||
|
|
||
| public IEnumerable<string> Ids => new[] { DiagnoserId }; | ||
|
|
||
| public IEnumerable<IExporter> Exporters => Array.Empty<IExporter>(); | ||
|
|
||
| public IEnumerable<IAnalyser> Analysers => Array.Empty<IAnalyser>(); | ||
|
|
||
| public void DisplayResults(ILogger logger) { } | ||
|
|
||
| public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters) | ||
| { | ||
| try | ||
| { | ||
| _counters = EnergyCounterDiscovery.Discover(Config.EnergyCountersSetup).ToArray(); | ||
| if (_counters.Length == 0) | ||
| throw new Exception("No RAPL counters found (or not enough rights)"); | ||
|
|
||
| } | ||
| catch (Exception e) | ||
| { | ||
| _validationFailed = true; | ||
| return [new ValidationError(false, e.Message)]; | ||
| } | ||
|
|
||
| var errors = _counters.Select(ec => ec.TestRead()).Where(x => x.Item1 == false).Select(x => new ValidationError(false, x.Item2)); | ||
| _validationFailed = errors.Any(); | ||
| return errors; | ||
| } | ||
|
|
||
| public void Handle(HostSignal signal, DiagnoserActionParameters _) | ||
| { | ||
| if (_validationFailed) | ||
| return; | ||
|
|
||
| if (signal == HostSignal.BeforeActualRun) | ||
| { | ||
| for (int i = 0; i < _counters.Length; i++) | ||
| _counters[i].FixStart(); | ||
| } | ||
| else if (signal == HostSignal.AfterActualRun) | ||
| { | ||
| for (int i = 0; i < _counters.Length; i++) | ||
| _counters[i].FixFinish(); | ||
| } | ||
| } | ||
|
|
||
| public IEnumerable<Metric> ProcessResults(DiagnoserResults diagnoserResults) | ||
| { | ||
| if (_validationFailed) | ||
| yield break; | ||
|
|
||
| long operations = diagnoserResults.Measurements.Where(m => m.IterationStage == IterationStage.Actual).Sum(m => m.Operations); | ||
| Debug.Assert(operations > 0); | ||
|
|
||
| int priority = 0; | ||
| foreach (var energyCounter in _counters.OrderBy(c => c.Id)) | ||
| { | ||
| long uj = energyCounter.GetValue(); | ||
| double avg_uj = operations > 0 && uj > 0 ? ((double)uj) / operations : 0.0; | ||
|
|
||
| yield return new Metric(new EnergyMetricDescriptor(priority++, energyCounter.Name, energyCounter.Id), avg_uj); | ||
| } | ||
| } | ||
|
|
||
| private class EnergyMetricDescriptor : IMetricDescriptor | ||
| { | ||
| public EnergyMetricDescriptor(int priority, string unitName, string id) | ||
| { | ||
| Id = id; | ||
| DisplayName = $"EC {unitName}"; | ||
| Legend = $"Average energy consumption of unit {unitName}, uj/op"; | ||
| PriorityInCategory = priority; | ||
| } | ||
|
|
||
| public string Id { get; } | ||
| public string DisplayName { get; } | ||
| public string Legend { get; } | ||
| public string NumberFormat => "#,000.000 uj"; | ||
| public UnitType UnitType => UnitType.Dimensionless; | ||
| public string Unit => "uj"; | ||
| public bool TheGreaterTheBetter => false; | ||
| public int PriorityInCategory { get; } | ||
| public bool GetIsAvailable(Metric metric) => metric.Value > 0; | ||
| } | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/BenchmarkDotNet.Diagnostics.Energy/EnergyDiagnoserAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using BenchmarkDotNet.Configs; | ||
| using BenchmarkDotNet.Diagnosers; | ||
|
|
||
| namespace BenchmarkDotNet.Attributes | ||
| { | ||
| [AttributeUsage(AttributeTargets.Class)] | ||
| public class EnergyDiagnoserAttribute : Attribute, IConfigSource | ||
| { | ||
| public IConfig Config { get; } | ||
|
|
||
| public EnergyDiagnoserAttribute(EnergyCountersSetup setup = EnergyCountersSetup.Default) | ||
| { | ||
| Config = ManualConfig.CreateEmpty().AddDiagnoser(new EnergyDiagnoser(new EnergyDiagnoserConfig(setup))); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/BenchmarkDotNet.Diagnostics.Energy/EnergyDiagnoserConfig.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace BenchmarkDotNet.Diagnosers | ||
| { | ||
| public class EnergyDiagnoserConfig | ||
| { | ||
| public EnergyDiagnoserConfig(EnergyCountersSetup setup = EnergyCountersSetup.Default) | ||
| { | ||
| EnergyCountersSetup = setup; | ||
| } | ||
|
|
||
| public EnergyCountersSetup EnergyCountersSetup { get; } | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/BenchmarkDotNet.Diagnostics.Energy/LinuxEnergyCounter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System.Diagnostics; | ||
|
|
||
| namespace BenchmarkDotNet.Diagnosers | ||
| { | ||
| /// <summary> | ||
| /// Energy counter which reads from /sys/class/powercap/intel-rapl/** | ||
| /// </summary> | ||
| internal class LinuxEnergyCounter : EnergyCounter | ||
| { | ||
| private readonly string _energyPath; | ||
| private long _start; | ||
| private long _finish; | ||
|
|
||
| public LinuxEnergyCounter(string energyPath, string name, string id) : base(name, id) | ||
| { | ||
| _energyPath = !string.IsNullOrEmpty(energyPath) ? energyPath : throw new ArgumentException(nameof(energyPath)); | ||
| } | ||
|
|
||
| public override (bool, string) TestRead() | ||
| { | ||
| try | ||
| { | ||
| string fileContents = File.ReadAllText(_energyPath); | ||
| bool good = long.Parse(fileContents) > 0; | ||
| return (true, string.Empty); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| return (false, e.Message); | ||
| } | ||
| } | ||
|
|
||
| public override void FixStart() | ||
| { | ||
| try | ||
| { | ||
| _start = long.Parse(File.ReadAllText(_energyPath)); | ||
| } | ||
| catch { } | ||
| } | ||
|
|
||
| public override void FixFinish() | ||
| { | ||
| try | ||
| { | ||
| _finish = long.Parse(File.ReadAllText(_energyPath)); | ||
| } | ||
| catch { } | ||
| } | ||
|
|
||
| public override long GetValue() | ||
| { | ||
| return _start > 0 && _finish > 0 ? (_finish - _start) : 0; | ||
| } | ||
|
|
||
| public static bool IsValid(string path) | ||
| { | ||
| return File.Exists(Path.Combine(path, "name")) && File.Exists(Path.Combine(path, "energy_uj")); | ||
| } | ||
|
|
||
| public static LinuxEnergyCounter FromPath(string path) | ||
| { | ||
| string name = File.ReadAllText(Path.Combine(path, "name")).Trim(); | ||
| Debug.Assert(!string.IsNullOrEmpty(name)); | ||
| return new LinuxEnergyCounter($"{path}/energy_uj", name, path); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use 'good'