docs/WindowsProfilingCompiler.md
This document describes Windows profiling tools from the angle of profiling the Swift compiler itself. The information is mostly general purpose, but specifics relevant to other applications aren't considered.
The recommended approach when you need detailed function and I/O operation-level profiling of the Swift compiler on Windows is to use Event Tracing for Windows (ETW). This includes using Windows Performance Recorder (WPR) to collect profiling data in ETL format and then Windows Performance Analyzer (WPA) to analyze it. For specific analyses not available in WPA, you will also be able to use xperf.exe to convert a binary ETL file to a text format for analysis by another program. We also discuss a couple of alternatives which may in certain cases be easier to use or provide specific data of interest: Visual Studio (the full IDE, not VS Code) and Intel VTune.
The document is organized as follows:
wpr (Windows Performance Recorder) or a wrapper script like utils/windows-profiling-tools/RunProfiler.ps1 to collect a .etl file (event trace log).
RunProfiler.ps1: ...\RunProfiler.ps1 -TracePath C:\Temp\program.etl -Target swiftc program.swift. Here program.swift just serves as a workload for running the compiler. Different parts of the compiler will naturally be exercised more or less depending on the program.swiftc directly, you can instead call a build system like swift build or cmake to build a larger program. Longer compilations will result in larger .etl files. Try a small program first, to see
what the .etl file collection and analysis process is like (including the size of .etl files).wpa.exe <path\to\trace.etl> to load the .etl file into WPA and analyze it.
swift.exe. Then right click on the selected processes and choose "Filter To Selection" to hide all other processes. Alternatively, right click on the table header, select "Edit Filters," and enter "Process Name" constraints.S:\Program Files\Swift\Runtimes\0.0.0\usr\bin and S:\Program Files\Swift\Toolchains\0.0.0+Asserts\usr\bin the list in the Paths tab. This enables WPA to find PDB files for our EXEs and DLLs. You shouldn't have to reconfigure this every time you run WPA.Visual Studio (the full IDE, not VS Code) has a built-in profiler that can profile arbitrary executables without requiring a Visual Studio solution or project. This includes the Swift compiler, provided it is built with PDB format debug symbols. By default, Visual Studio's profiler only records profiling data for the directly invoked process, not child processes. This makes it a less than ideal choice for top level Swift builds that invoke many child processes, but can still be useful if you have a specific swift-frontend.exe command you would like to profile.
Assuming you want to profile swiftc.exe's call to swift-frontend.exe run on an input source file S:\Temp\hello.swift:
swiftc.exe -v hello.swift (for example). This will give you a swift-frontend.exe command that swiftc.exe calls.S:\Program Files\Swift\Toolchains\0.0.0+Asserts\usr\bin\swift-frontend.exeS:\TempIntel VTune supports profiling using Intel processors' Performance Monitoring Unit (PMU), which provides hardware-level performance counters. It's available as an integrated GUI application as well as a standalone command line executable (vtune.exe) that can collect performance logs to be analyzed in the GUI. We'll discuss the GUI workflow here.
Where: Local Host (default)
What: Launch Application (default)
Application: for example, S:\Program Files\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe
Application parameters: for example, hello.swift
Uncheck "Use application directory as working directory"
Working directory: for example, S:\Temp
Advanced >
C:\Users\%USER%\Documents\VTune\Projects\swift_hello (note: manually fill in your %USER%)How: Select what you would like to profile. Hotspots will tell you where most of the time is spent in the code.
Click the "Search Sources/Binaries" button (folder with a magnifying glass) and add the following search directories:
S:\Program Files\Swift\Runtimes\0.0.0\usr\binS:\Program Files\Swift\Toolchains\0.0.0+Asserts\usr\binClick the "Start" button ("play" button style triangle above). If you would prefer to run vtune.exe from the command line, click the ">_" button seen above to get the command to run.
swiftc.exe hello.swift exits, you will be taken to an analysis tab to view the results, which include hot traces and time consuming functions.This section describes Windows profiling tools that can be used with the Swift compiler and the relationships between them.
wpr.exe. Collects profiling data for all running processes and saves it to a .etl file. See utils/windows-profiling-tools/RunProfiler.ps1 for a PowerShell script that runs WPR for the duration of a specified command invocation..exes that don’t have to be part of a Visual Studio solution/project, starting by opening VS without any files/project, then going to Debug > Performance Profiler... By default, it only profiles the initial process
launched from the .exe, not child processes..etl is the output binary event log file format produced by tools like WPR and XPerf. There are also a command line tools including xperf.exe and tracerpt.exe that can be used to convert an ETL file into multiple text-based formats..wprp is an XML configuration file format used to configure WPR’s operation. For example, utils/windows-profiling-tools/CpuAndWaitsWithLargeBuffers.wprp increases the in-memory buffer size that WPR configures ETW to use so as not to drop events during recording..etl files. It can be invoked on the command line by running wpa.exe with the .etl file as an argument. See Using Event Tracing for Windows (ETW) to profile Swift compiler runs for how to view program profiles with symbols..etl files that has been replaced by WPA.vtune.exe (possibly run on a different host).wpaexport.exe takes a profile built in the WPA GUI and exports data from a .etl file according to that profile. Note that profiles that include hierarchical lists in the GUI may only export the top levels of the hierarchy. In this case, you can reconfigure the WPA GUI view to make the list flat and then export a new profile.xperf.exe can be used to operate on .etl files recorded with either wpr.exe or xperf.exe itself. Examples:
.etl file in text format: xperf -i <INPUT>.etl -o <OUTPUT>.txt -symbols -target machine -a dumper -stacktimeshifting where <INPUT> and <OUTPUT> are files of your choice with _NT_SYMBOL_PATH set in the environment to point to directories containing PDB files, e.g. in PowerShell:
$env:_NT_SYMBOL_PATH="S:\b\5\bin\bin" (list all directories containing relevant PDB files, separated by semicolons).etl file, not including the time taken by calls to other functions: xperf -i <INPUT>.etl -o <OUTPUT>.txt -symbols -a profile -detailTo build the Swift compiler with PDB format debug symbols that are used by profile analysis tools like WPA or Visual Studio, follow the https://github.com/compnerd/swift-build/blob/main/docs/WindowsQuickStart.md instructions, using the following build.cmd command line (or a variant on it based on your exact needs):
S:\SourceCache\swift\utils\build.cmd -Windows -DebugInfo -SkipPackaging -WindowsSDKArchitectures x64 -CDebugFormat codeview -SwiftDebugFormat codeview
If you need to build a package that you can install on a separate Windows host, either remove -SkipPackaging in the above command, or if you've already run the above build, substitute -SkipBuild in place of -SkipPackaging.