Skip to content

Commit

Permalink
Add video-conference CPU perf counters monitoring tool
Browse files Browse the repository at this point in the history
This batch file and C# tool are designed to record CPU performance
counters and then analyze them in the context of video conferencing in
Chrome. It focuses on the system processes involved in video
conferencing, plus all Chrome processes. No sampling is enabled so the
traces created can only give process/thread level information about
where time is going.
  • Loading branch information
randomascii committed May 27, 2020
1 parent 64d045f commit 88a8d15
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 0 deletions.
14 changes: 14 additions & 0 deletions TraceProcessors/TraceProcessors.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPUSummary", "CPUSummary\CP
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeapSnapshotCompare", "HeapSnapshotCompare\HeapSnapshotCompare.csproj", "{96201BE4-B511-49A3-A98D-537B77E054C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VideoConfCPUCounters", "VideoConfCPUCounters\VideoConfCPUCounters.csproj", "{C37B7893-D2B9-4DA1-835C-04339B38BFF7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -55,6 +57,18 @@ Global
{96201BE4-B511-49A3-A98D-537B77E054C6}.Release|x64.Build.0 = Release|Any CPU
{96201BE4-B511-49A3-A98D-537B77E054C6}.Release|x86.ActiveCfg = Release|Any CPU
{96201BE4-B511-49A3-A98D-537B77E054C6}.Release|x86.Build.0 = Release|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Debug|x64.ActiveCfg = Debug|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Debug|x64.Build.0 = Debug|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Debug|x86.ActiveCfg = Debug|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Debug|x86.Build.0 = Debug|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|Any CPU.Build.0 = Release|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|x64.ActiveCfg = Release|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|x64.Build.0 = Release|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|x86.ActiveCfg = Release|Any CPU
{C37B7893-D2B9-4DA1-835C-04339B38BFF7}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 14 additions & 0 deletions TraceProcessors/VideoConfCPUCounters/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Security.Principal.Windows" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
36 changes: 36 additions & 0 deletions TraceProcessors/VideoConfCPUCounters/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("VideoConfCPUCounters")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VideoConfCPUCounters")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c37b7893-d2b9-4da1-835c-04339b38bff7")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
226 changes: 226 additions & 0 deletions TraceProcessors/VideoConfCPUCounters/VideoConfCPUCounters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
Copyright 2020 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// This program summarizes CPU usage and CPU perf counters for the processes
// involved in Chrome video conferencing.

// See these blog posts for details of the Trace Processor package used to
// drive this:
// https://blogs.windows.com/windowsdeveloper/2019/05/09/announcing-traceprocessor-preview-0-1-0/
// https://blogs.windows.com/windowsdeveloper/2019/08/07/traceprocessor-0-2-0/#L2W90BVvLzJ8XwEY.97
// https://randomascii.wordpress.com/2020/01/05/bulk-etw-trace-analysis-in-c/
// This uses the Microsoft.Windows.EventTracing.Processing.All package from NuGet

using Microsoft.Windows.EventTracing;
using Microsoft.Windows.EventTracing.Processes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VideoConfCPUCounters
{
class Counters
{
public Counters(string processDescription)
{
description = processDescription;
counters = new Dictionary<string, ulong>();
}
public Dictionary<string, ulong> counters;
public long runTime_ns;
public long contextSwitches;
public string description;
};

class VideoConfCPUCounters
{
static Dictionary<IProcess, Counters> FindInterestingProcesses(IProcessDataSource processData)
{
var countersByProcess = new Dictionary<IProcess, Counters>();

foreach (var process in processData.Processes)
{
string description = "";
// We are interested in all Chrome processes.
if (process.ImageName == "chrome.exe")
{
// Find the space-terminated word after 'type='.
// Mark the first .* as lazy/ungreedy/reluctant so that if
// there are multiple --type options (such as with the V8
// Proxy Resolver utility process) the first one will win.
// Or, at least, that's what the comments in the Python
// version of this said.
var r = new Regex(@".*? --type=(?<type>[^ ]*) .*");
// Find the utility sub-type, if present.
var r_sub = new Regex(@".*? --utility-sub-type=(?<subtype>[^ ]*) .*");
string type;
var match = r.Match(process.CommandLine);
if (match.Success)
{
type = match.Groups["type"].ToString();
// Shorten the tag for better formatting
if (type == "crashpad-handler")
type = "crashpad";
if (type == "renderer" &&
process.CommandLine.Contains(" --extension-process "))
{
// Extension processes are renderers with
// --extension-process on the command line.
type = "extension";
}

var match_sub = r_sub.Match(process.CommandLine);
if (match_sub.Success)
{
// Split utility-process sub-types on period
// boundaries and take the last component. This
// changes video_capture.mojom.VideoCaptureService
// into just VideoCaptureService.
char[] separator = { '.' };
var parts = match_sub.Groups["subtype"].ToString()
.Split(separator);
type = parts[parts.Length - 1];
}
}
else
{
type = "browser";
}

description = "chrome - " + type;
}
else if (process.ImageName == "svchost.exe")
{
if (process.CommandLine.Contains("-k Camera") ||
process.CommandLine.Contains("-s FrameServer"))
description = "svchost - FrameServer";
}
else if (process.ImageName == "dwm.exe" ||
process.ImageName == "audiodg.exe" ||
process.ImageName == "System")
description = process.ImageName;

if (description.Length > 0)
{
countersByProcess[process] = new Counters(description);
}
}

return countersByProcess;
}

static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Specify the name of one trace to be summarized.");
return;
}

var traceName = args[0];

if (!File.Exists(traceName))
{
Console.Error.WriteLine("File '{0}' does not exist.", traceName);
return;
}

var settings = new TraceProcessorSettings
{
// Don't print a setup message on first run.
SuppressFirstTimeSetupMessage = true
};

using (ITraceProcessor trace = TraceProcessor.Create(traceName, settings))
{
// Get process details, including command lines.
var pendingProcessData = trace.UseProcesses();
// Get CPU performance counters, on every context switch.
var pendingCounterData = trace.UseProcessorCounters();

trace.Process();

var processData = pendingProcessData.Result;
var counterData = pendingCounterData.Result;

var countersByProcess = FindInterestingProcesses(processData);

// Accumulate data for all of the interesting processes.
foreach (var entry in counterData.ContextSwitchCounterDeltas)
{
// This sometimes happens - handle it.
if (entry.Process == null)
continue;

Counters last;
if (!countersByProcess.TryGetValue(entry.Process, out last))
continue;

// Accumulate counter values and execution time.
foreach (var key in entry.RawCounterDeltas.Keys)
{
last.counters.TryGetValue(key, out ulong lastCount);
lastCount += entry.RawCounterDeltas[key];
last.counters[key] = lastCount;
}
last.runTime_ns += (entry.StopTime - entry.StartTime).Nanoseconds;
last.contextSwitches += 1;

countersByProcess[entry.Process] = last;
}

// Sort the data by CPU time and print it.
var sortedCounterData = new List<KeyValuePair<IProcess, Counters>>(countersByProcess);
sortedCounterData.Sort((x, y) => y.Value.runTime_ns.CompareTo(x.Value.runTime_ns));

bool printHeader = true;
foreach (var entry in sortedCounterData)
{
if (printHeader)
{
Console.Write("{0,-29} - CPU time (s) - context switches", "Image name");
foreach (var counterName in entry.Value.counters.Keys)
{
int fieldWidth = Math.Max(13, counterName.Length);
Console.Write(", {0}", counterName.PadLeft(fieldWidth));
}
Console.WriteLine();
printHeader = false;
}

// Arbitrary cutoff for what is "interesting"
if (entry.Value.runTime_ns < 100 * 1000 * 1000)
continue;

Console.Write("{0,-29} - {1,8:0.00} - {2,16}", entry.Value.description,
entry.Value.runTime_ns / 1e9, entry.Value.contextSwitches);
foreach (var counterName in entry.Value.counters.Keys)
{
int fieldWidth = Math.Max(13, counterName.Length);
Console.Write(", {0}",
entry.Value.counters[counterName].ToString().PadLeft(fieldWidth));
}
Console.WriteLine();
}
}
}
}
}
Loading

0 comments on commit 88a8d15

Please sign in to comment.