# python-etwtrace **Repository Path**: mirrors_microsoft/python-etwtrace ## Basic Information - **Project Name**: python-etwtrace - **Description**: Python extension for generating profiling data and stack samples for Windows Performance Analyzer - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-11-21 - **Last Updated**: 2026-05-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # python-etwtrace Enables ETW tracing events to help with profiling using the [Windows Performance Toolkit](https://learn.microsoft.com/windows-hardware/test/wpt/). It supports Python 3.9 and later on Windows 64-bit and Windows ARM64. ![Windows Performance Analyzer with a mixed Python/native flame graph](https://github.com/microsoft/python-etwtrace/raw/main/WPA-Python.png) (Note that the WPA integration shown above requires the [current preview release](https://www.microsoft.com/store/productId/9N58QRW40DFW?ocid=pdpshare).) Two forms of profiling are supported: * stack sampling, where regular CPU sampling will include Python calls * instrumentation, where events are raised on entry/exit of Python functions If you will inspect results using [Windows Performance Analyzer](https://www.microsoft.com/store/productId/9N58QRW40DFW?ocid=pdpshare) (WPA), then you will prefer stack sampling (the default). This method inserts additional native function calls in place of pure-Python calls, and provides WPA with the metadata necessary to display the function. Configure the provided [stack tags](https://learn.microsoft.com/en-us/windows-hardware/test/wpt/stack-tags) file (`python -m etwtrace --stacktags`) in WPA and view the "Stack (Frame Tags)" column to filter out internal Python calls. You will need Python symbols for the best results; these are an optional component in the installer from python.org. If you are capturing ETW events for some other form of analysis, you may prefer more traditional instrumentation. This method generates ETW events on entry and exit of each function, which can be reconstructed into call stacks at any point. It also provides more accurate call count data than stack sampling. ![Windows Performance Analyzer with a call count, function info, and sequence views over instrumented data](https://github.com/microsoft/python-etwtrace/raw/main/WPA-Instrumented.png) ## Capturing events See [Windows Performance Recorder](https://learn.microsoft.com/windows-hardware/test/wpt/windows-performance-recorder) for detailed information on recording events, including installation of the Windows Performance Toolkit. Here we cover only the basics to capture a trace of some Python code. The `wpr` tool is used to start and stop recording a trace, and to export the results to an `.etl` file. The trace must be started and stopped as Administrator, however, the code under test may be run as a regular user. For basic capture, use the `--capture` argument to have `etwtrace` launch and stop `wpr`: ``` > python -m etwtrace --capture output.etl -- .\my-script.py ``` A [recording profile](https://learn.microsoft.com/windows-hardware/test/wpt/recording-profiles) is used to select the event sources that will be recorded. We include a profile configured for Python as [python.wprp](https://github.com/microsoft/python-etwtrace/blob/main/src/python.wprp). We recommend downloading this file from here, or finding it in the `etwtrace` package's install directory by running `python -m etwtrace --profile`. To record a Python trace: ``` # Ensure the output file does not exist, or tracing will fail > del output.etl > wpr -start python.wprp!Default # run your code as shown below ... > wpr -stop output.etl ``` You can pass `!Minimal` instead of `!Default` to reduce the size of the trace by omitting some other system providers. When running on Windows ARM64, use `!ARM64` instead of `!Default` to avoid some providers that are currently absent. To collect additional information, we suggest copying the configuration from `python.wprp` into your own recording profile. The WPR docs above provide more information. ## Launching with tracing To enable for a single command, launch with `-m etwtrace --