Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 6da5fdb

Browse files
committed
Adding ETW trace processor to find chrome.exe idle wakeups
An idle wakeup is when a CPU goes from idle to running code. These can be energy intensive because they suggest that the CPU was in a potentially low power state and was then forced to run code.
1 parent d107b77 commit 6da5fdb

5 files changed

Lines changed: 125 additions & 2 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*.svg
1515
*.pdb
1616
*.user
17+
*.launchSettings.json
1718
# New Visual Studio 2015 intellisense DB files
1819
# http://blogs.msdn.com/b/vcblog/archive/2015/11/11/new-improved-and-faster-database-engine.aspx
1920
*.VC.db
@@ -41,6 +42,9 @@ etwpackage*.zip
4142
etwsymbols*.zip
4243
sourceindex.txt
4344

45+
# Avoid shipping nuget files and others in /obj/ directories.
46+
obj/
47+
4448
Debug/
4549
Release/
4650
*DoNotShip/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2021 Google Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Detect idle wakeups in Chrome in an ETW using TraceProcessing
18+
// Explanations of the techniques can be found here:
19+
// https://randomascii.wordpress.com/2020/01/05/bulk-etw-trace-analysis-in-c/
20+
21+
// See this blog post for details of the Trace Processor package used to
22+
// drive this:
23+
// https://blogs.windows.com/windowsdeveloper/2019/05/09/announcing-traceprocessor-preview-0-1-0/
24+
// Note that this URL has changed once already, so caveat blog lector
25+
26+
using Microsoft.Windows.EventTracing;
27+
class IdleWakeups
28+
{
29+
static void Main(string[] args)
30+
{
31+
foreach (string traceName in args)
32+
{
33+
Console.WriteLine("Processing trace '{0}'", traceName);
34+
var settings = new TraceProcessorSettings
35+
{
36+
// Don't print a setup message on first run.
37+
SuppressFirstTimeSetupMessage = true
38+
};
39+
using (ITraceProcessor trace = TraceProcessor.Create(traceName, settings))
40+
{
41+
// Specify what data we want, process the trace, then get the data.
42+
var pendingContextSwitchData = trace.UseContextSwitchData();
43+
trace.Process();
44+
var csData = pendingContextSwitchData.Result;
45+
46+
long chromeSwitches = 0;
47+
long chromeIdleSwitches = 0;
48+
// Iterate through all context switches in the trace.
49+
foreach (var contextSwitch in csData.ContextSwitches)
50+
{
51+
var imageName = contextSwitch.SwitchIn.Process.ImageName;
52+
var oldImageName = contextSwitch.SwitchOut.Process.ImageName;
53+
if (imageName == "chrome.exe")
54+
{
55+
chromeSwitches++;
56+
if (oldImageName == "Idle")
57+
chromeIdleSwitches++;
58+
}
59+
}
60+
Console.WriteLine("{0} idlewakeups out of {1} context switches ({2:P}).",
61+
chromeIdleSwitches, chromeSwitches,
62+
chromeIdleSwitches / (double)chromeSwitches);
63+
}
64+
}
65+
}
66+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Windows.EventTracing.Processing.All" Version="1.9.2" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31912.275
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdleWakeups", "IdleWakeups.csproj", "{B088F2D9-100B-401D-A43A-3358BF682195}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B088F2D9-100B-401D-A43A-3358BF682195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B088F2D9-100B-401D-A43A-3358BF682195}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B088F2D9-100B-401D-A43A-3358BF682195}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B088F2D9-100B-401D-A43A-3358BF682195}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {1273C88D-6FDB-4224-9D13-03762DB203A5}
24+
EndGlobalSection
25+
EndGlobal

TraceProcessors/TraceProcessors.sln

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.28803.202
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31919.166
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentifyChromeProcesses", "IdentifyChromeProcesses\IdentifyChromeProcesses.csproj", "{42AC0C9A-BBE7-4C8E-BB00-84D9C9FC8EE3}"
77
EndProject
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeapSnapshotCompare", "Heap
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VideoConfCPUCounters", "VideoConfCPUCounters\VideoConfCPUCounters.csproj", "{C37B7893-D2B9-4DA1-835C-04339B38BFF7}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdleWakeups", "IdleWakeups\IdleWakeups.csproj", "{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,18 @@ Global
6971
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|x64.Build.0 = Release|Any CPU
7072
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|x86.ActiveCfg = Release|Any CPU
7173
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|x86.Build.0 = Release|Any CPU
74+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Debug|x64.ActiveCfg = Debug|Any CPU
77+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Debug|x64.Build.0 = Debug|Any CPU
78+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Debug|x86.ActiveCfg = Debug|Any CPU
79+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Debug|x86.Build.0 = Debug|Any CPU
80+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Release|Any CPU.ActiveCfg = Release|Any CPU
81+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Release|Any CPU.Build.0 = Release|Any CPU
82+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Release|x64.ActiveCfg = Release|Any CPU
83+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Release|x64.Build.0 = Release|Any CPU
84+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Release|x86.ActiveCfg = Release|Any CPU
85+
{3CD706EF-50E6-46EF-BF11-3AFB86C57E10}.Release|x86.Build.0 = Release|Any CPU
7286
EndGlobalSection
7387
GlobalSection(SolutionProperties) = preSolution
7488
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)