dotnet / BenchmarkDotNet

Powerful .NET library for benchmarking
https://benchmarkdotnet.org
MIT License
10.24k stars 952 forks source link

Better support for asymmetric CPU cores #2557

Closed huoyaoyuan closed 1 month ago

huoyaoyuan commented 3 months ago

When running benchmark on my Raptor Lake processor, I find the time of each iteration shifts between two values if I switch the console window to background.

This is probably due to the "smart" scheduling for P/E cores of Windows. I've also heard that thread affinity API getting ignored by the system in some situations.

It would also be interesting to see the results on E-cores. To be more general, there can be a configuration to set thread affinity for benchmarks. This can also help benchmarking for arbitrary core counts.

timcassell commented 3 months ago

We already support --affinity command line arg and Job.WithAffinity config option. Does that do what you need?

I've also heard that thread affinity API getting ignored by the system in some situations.

If that's true, that would be an issue for runtime or the OS, not BDN.

huoyaoyuan commented 3 months ago

We already support --affinity command line arg and Job.WithAffinity config option. Does that do what you need?

--affinity 1 and --affinity 65536 give me significant comparison of P/E cores.

Is there more handy way to compare different affinities? Job is just heavier to use. I can't find affinity setting in attributes.

timcassell commented 3 months ago

I don't think there is another built-in way to do it. You could create your own attribute to do it, though.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
public class AffinitizedJobAttribute : JobConfigBaseAttribute
{
    // Add whatever other settings you need.
    public AffinitizedJobAttribute(int affinity) : base(Job.Default.WithAffinity((IntPtr) affinity)) { }
}
timcassell commented 1 month ago

I don't think there is anything actionable here, so closing this out. Let me know if you disagree.