-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for custom hardware counter names #2953
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
Open
ramartinez7
wants to merge
8
commits into
dotnet:master
Choose a base branch
from
ramartinez7:feature/custom-hardware-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.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bfd224b
Add support for custom hardware counter names
ramartinez7 2ecca69
Simplify null validation in CustomCounter constructor
ramartinez7 eaee3f7
Remove unnecessary interval comparison in FromCustomCounter
ramartinez7 9c4e635
Add ProfileSourceId collision detection in PmcStats
ramartinez7 a14f23f
Update comment to mention both hardware and custom counters
ramartinez7 59b0b1e
Fix misleading ellipsis in custom counter validation error
ramartinez7 6c932b4
Add validation for positive interval in CustomCounter
ramartinez7 243a83a
Refactor custom counter validation to use explicit filtering
ramartinez7 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
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
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
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,40 @@ | ||
| using System; | ||
| using BenchmarkDotNet.Configs; | ||
| using BenchmarkDotNet.Diagnosers; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace BenchmarkDotNet.Attributes | ||
| { | ||
| /// <summary> | ||
| /// Specifies custom hardware counters to be collected during benchmarking. | ||
| /// Use this when the predefined HardwareCounter enum values don't match the counters | ||
| /// available on your machine (e.g., AMD-specific counters like DcacheMisses, IcacheMisses). | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly)] | ||
| public class CustomCountersAttribute : Attribute, IConfigSource | ||
| { | ||
| // CLS-Compliant Code requires a constructor without an array in the argument list | ||
| [PublicAPI] | ||
| protected CustomCountersAttribute() | ||
| { | ||
| Config = ManualConfig.CreateEmpty(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a CustomCountersAttribute with the specified counter names. | ||
| /// Counter names must match the ETW profile source names available on the machine. | ||
| /// Use TraceEventProfileSources.GetInfo().Keys to discover available counters. | ||
| /// </summary> | ||
| /// <param name="counterNames">The ETW profile source names (e.g., "DcacheMisses", "IcacheMisses")</param> | ||
| public CustomCountersAttribute(params string[] counterNames) | ||
| { | ||
| var config = ManualConfig.CreateEmpty(); | ||
| foreach (var name in counterNames) | ||
| { | ||
| config.AddCustomCounters(new CustomCounter(name)); | ||
| } | ||
| Config = config; | ||
| } | ||
| public IConfig Config { get; } | ||
| } | ||
| } |
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
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
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
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
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
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
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,71 @@ | ||
| using System; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace BenchmarkDotNet.Diagnosers | ||
| { | ||
| /// <summary> | ||
| /// Represents a custom hardware performance counter that can be specified by its ETW profile source name. | ||
| /// Use this when the predefined <see cref="HardwareCounter"/> enum values don't match the counters | ||
| /// available on your machine (e.g., AMD-specific counters like DcacheMisses, IcacheMisses). | ||
| /// Run <c>TraceEventProfileSources.GetInfo().Keys</c> to discover available counters on your system. | ||
| /// </summary> | ||
| public class CustomCounter | ||
| { | ||
| /// <summary> | ||
| /// Default sampling interval for custom counters. | ||
| /// </summary> | ||
| public const int DefaultInterval = 1_000_003; | ||
|
|
||
| /// <summary> | ||
| /// The exact name of the ETW profile source as returned by TraceEventProfileSources.GetInfo(). | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public string ProfileSourceName { get; } | ||
|
|
||
| /// <summary> | ||
| /// A short name used for display in reports and columns. | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public string ShortName { get; } | ||
|
|
||
| /// <summary> | ||
| /// The sampling interval for this counter. | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public int Interval { get; } | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether higher values are better for this counter. | ||
| /// Default is false (lower is better, e.g., cache misses). | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public bool HigherIsBetter { get; } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new custom hardware counter. | ||
| /// </summary> | ||
| /// <param name="profileSourceName">The exact name of the ETW profile source (e.g., "DcacheMisses", "IcacheMisses").</param> | ||
| /// <param name="shortName">Optional short name for display. If null, uses the profile source name.</param> | ||
| /// <param name="interval">Sampling interval. If not specified, uses DefaultInterval (1,000,003).</param> | ||
| /// <param name="higherIsBetter">Whether higher values are better for this counter.</param> | ||
| public CustomCounter(string profileSourceName, string? shortName = null, int interval = DefaultInterval, bool higherIsBetter = false) | ||
| { | ||
| if (profileSourceName == null) | ||
| throw new ArgumentNullException(nameof(profileSourceName)); | ||
ramartinez7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (string.IsNullOrWhiteSpace(profileSourceName)) | ||
| throw new ArgumentException("Profile source name cannot be empty or whitespace.", nameof(profileSourceName)); | ||
|
|
||
| ProfileSourceName = profileSourceName; | ||
| ShortName = shortName ?? profileSourceName; | ||
| Interval = interval; | ||
| HigherIsBetter = higherIsBetter; | ||
ramartinez7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public override string ToString() => ProfileSourceName; | ||
|
|
||
| public override bool Equals(object? obj) | ||
| => obj is CustomCounter other && ProfileSourceName == other.ProfileSourceName; | ||
|
|
||
| public override int GetHashCode() => ProfileSourceName.GetHashCode(); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.