-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathHardwareCounters.cs
More file actions
112 lines (97 loc) · 5.68 KB
/
HardwareCounters.cs
File metadata and controls
112 lines (97 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using BenchmarkDotNet.Validators;
using Microsoft.Diagnostics.Tracing.Session;
namespace BenchmarkDotNet.Diagnostics.Windows
{
public static class HardwareCounters
{
private static readonly Dictionary<HardwareCounter, string> EtwTranslations
= new Dictionary<HardwareCounter, string>
{
{ HardwareCounter.Timer, "Timer" },
{ HardwareCounter.TotalIssues, "TotalIssues" },
{ HardwareCounter.BranchInstructions, "BranchInstructions" },
{ HardwareCounter.CacheMisses, "CacheMisses" },
{ HardwareCounter.BranchMispredictions, "BranchMispredictions" },
{ HardwareCounter.TotalCycles, "TotalCycles" },
{ HardwareCounter.UnhaltedCoreCycles, "UnhaltedCoreCycles" },
{ HardwareCounter.InstructionRetired, "InstructionRetired" },
{ HardwareCounter.UnhaltedReferenceCycles, "UnhaltedReferenceCycles" },
{ HardwareCounter.LlcReference, "LLCReference" },
{ HardwareCounter.LlcMisses, "LLCMisses" },
{ HardwareCounter.BranchInstructionRetired, "BranchInstructionRetired" },
{ HardwareCounter.BranchMispredictsRetired, "BranchMispredictsRetired" }
};
public static IEnumerable<ValidationError> Validate(ValidationParameters validationParameters, bool mandatory)
{
if (!OsDetector.IsWindows())
{
yield return new ValidationError(true, "Hardware Counters and EtwProfiler are supported only on Windows");
yield break;
}
var hasHardwareCounters = validationParameters.Config.GetHardwareCounters().Any();
var hasCustomCounters = validationParameters.Config.GetCustomCounters().Any();
if (!hasHardwareCounters && !hasCustomCounters && mandatory)
{
yield return new ValidationError(true, "No Hardware Counters defined, probably a bug");
yield break;
}
if (TraceEventSession.IsElevated() != true)
yield return new ValidationError(true, "Must be elevated (Admin) to use ETW Kernel Session (required for Hardware Counters and EtwProfiler).");
var availableCpuCounters = TraceEventProfileSources.GetInfo();
foreach (var hardwareCounter in validationParameters.Config.GetHardwareCounters())
{
if (!EtwTranslations.TryGetValue(hardwareCounter, out string counterName))
{
yield return new ValidationError(true,
$"Counter {hardwareCounter} not recognized. " +
$"Please make sure that you are using counter available on your machine. " +
$"You can get the list of available counters by running `tracelog.exe -profilesources Help`");
continue;
}
if (!availableCpuCounters.ContainsKey(counterName))
yield return new ValidationError(true, $"The counter {counterName} is not available. Please make sure you are Windows 8+ without Hyper-V");
}
// Validate custom counters
foreach (var customCounter in validationParameters.Config.GetCustomCounters()
.Where(c => !availableCpuCounters.ContainsKey(c.ProfileSourceName)))
{
var availableCounterNames = availableCpuCounters.Keys.ToList();
var displayedCounterNames = string.Join(", ", availableCounterNames.Take(20));
var suffix = availableCounterNames.Count > 20 ? $" (and {availableCounterNames.Count - 20} more)" : string.Empty;
yield return new ValidationError(true,
$"Custom counter '{customCounter.ProfileSourceName}' is not available on this machine. " +
$"Available counters: {displayedCounterNames}{suffix}");
}
foreach (var benchmark in validationParameters.Benchmarks)
{
if (benchmark.Job.Infrastructure.TryGetToolchain(out var toolchain) && toolchain is InProcessEmitToolchain)
{
yield return new ValidationError(true, "Hardware Counters and EtwProfiler are not supported for InProcessToolchain.", benchmark);
}
}
}
internal static PreciseMachineCounter FromCounter(HardwareCounter counter, Func<ProfileSourceInfo, int> intervalSelector)
{
var profileSource = TraceEventProfileSources.GetInfo()[EtwTranslations[counter]]; // it can't fail, diagnoser validates that first
return new PreciseMachineCounter(profileSource.ID, profileSource.Name, counter, intervalSelector(profileSource));
}
internal static PreciseMachineCounter FromCustomCounter(CustomCounter customCounter, Func<ProfileSourceInfo, int> intervalSelector)
{
var profileSource = TraceEventProfileSources.GetInfo()[customCounter.ProfileSourceName];
return new PreciseMachineCounter(profileSource.ID, profileSource.Name, customCounter, customCounter.Interval);
}
internal static void Enable(IEnumerable<PreciseMachineCounter> counters)
{
TraceEventProfileSources.Set( // it's a must have to get the events enabled!!
counters.Select(counter => counter.ProfileSourceId).ToArray(),
counters.Select(counter => counter.Interval).ToArray());
}
}
}