Skip to main content

Generates ETW events for tracing Python apps with the Windows Performance Toolkit

Project description

python-etwtrace

Enables ETW tracing events to help with profiling using the Windows Performance Toolkit. It supports Python 3.9 and later on Windows 64-bit and Windows ARM64.

Windows Performance Analyzer with a mixed Python/native flame graph

(Note that the WPA integration shown above requires an as-yet unreleased update.)

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 (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 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

Capturing events

See 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 is used to select the event sources that will be recorded. We include a profile configured for Python as 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 -- <script> or -m etwtrace -- -m <module>:

> python -m etwtrace -- .\my-script.py arg1 arg2

Pass --instrumented before the -- to select that mode.

> python -m etwtrace --instrumented -- -m test_module

Pass --capture FILE before the -- to automatically start and stop wpr.

> python -m etwtrace --capture output.etl -- .\my-script.py arg1 arg2

To enable permanently for an environment, run the module with --enable:

> python -m etwtrace --enable
Created etwtrace.pth
Set %ETWTRACE_TYPE% to 'instrumented' to use instrumented events rather than stacks

To enable permanently but only when an environment variable is set, also provide the variable name. A second variable name may be provided to specify the kind of tracing ('instrumented' or 'stack' (default)); if omitted, the variable name will be derived from the first.

> python -m etwtrace --enable PYTHON_ETW_TRACE TRACE_TYPE
Created etwtrace.pth
Set %PYTHON_ETW_TRACE% to activate
Set %TRACE_TYPE% to 'instrumented' to use instrumented events rather than stacks

> $env:PYTHON_ETW_TRACE = "1"
> python .\my-script.py arg1 arg2

To disable, run with --disable or delete the created .pth file.

> python -m etwtrace --disable
Removed etwtrace.pth

Visual Studio integration

This module is also used for Visual Studio profiling of Python code, however, those interfaces are not supported for use outside of Visual Studio, and so are not documented here.

Please see the Visual Studio documentation for information about profiling Python code.

Building from Source

To build the sources:

> python -m pip install pymsbuild

# Builds into the source directory (-g for a debug configuration)
> python -m pymsbuild -g

# Ensure source directory is importable
> $env:PYTHONPATH = ".\src"
> python ...

Events

When enabled, ETW events are raised on thread creation, ending, and when a function is first executed. Other events may be raised explicitly by Python code that calls etwtrace.custom_mark, or the internal etwtrace._stack_mark function.

The PythonFunction event provides the range of memory that will appear in stack samples when the specified function is called. It can be used to map sampled frames back to the source file and line number of the function being executed. The FunctionID argument is a unique value for the lifetime of the process representing the function.

The PythonThread event typically comes as a range (using start and stop opcodes) and is intended to highlight a region of interest. Similarly, the PythonMark event may be a range highlighting a particular region of interest.

The PythonStackSample event is primarily used by tests to force a stack sample to be collected at a particular point in execution. When used for this, it should be configured in the collection profile to include the stack, as there is nothing inherent to the event that causes collection. This event is raised by the private and undocumented _mark_stack function.

The PythonFunctionPush and PythonFunctionPop events are raised in instrumentation mode on entry and exit of a function. The FunctionID and Caller (another function ID) will have previously appeared in PythonFunction events.

The Python events provider GUID is 99a10640-320d-4b37-9e26-c311d86da7ab.

Event Keyword Args
PythonThread 0x0100 ThreadId
PythonFunction 0x0400 FunctionID, BeginAddress, EndAddress, LineNumber, SourceFile, Name
PythonMark 0x0800 Mark
PythonStackSample 0x0200 Mark
PythonFunctionPush 0x1000 FunctionID, Caller, CallerLine
PythonFunctionPop 0x2000 FunctionID

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

etwtrace-0.1b5.tar.gz (187.7 kB view details)

Uploaded Source

Built Distributions

etwtrace-0.1b5-cp313-cp313-win_arm64.whl (165.5 kB view details)

Uploaded CPython 3.13 Windows ARM64

etwtrace-0.1b5-cp313-cp313-win_amd64.whl (186.2 kB view details)

Uploaded CPython 3.13 Windows x86-64

etwtrace-0.1b5-cp312-cp312-win_arm64.whl (165.3 kB view details)

Uploaded CPython 3.12 Windows ARM64

etwtrace-0.1b5-cp312-cp312-win_amd64.whl (186.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

etwtrace-0.1b5-cp311-cp311-win_arm64.whl (163.0 kB view details)

Uploaded CPython 3.11 Windows ARM64

etwtrace-0.1b5-cp311-cp311-win_amd64.whl (185.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

etwtrace-0.1b5-cp310-cp310-win_amd64.whl (190.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

etwtrace-0.1b5-cp39-cp39-win_amd64.whl (190.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

File details

Details for the file etwtrace-0.1b5.tar.gz.

File metadata

  • Download URL: etwtrace-0.1b5.tar.gz
  • Upload date:
  • Size: 187.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: RestSharp/106.13.0.0

File hashes

Hashes for etwtrace-0.1b5.tar.gz
Algorithm Hash digest
SHA256 96ea0a5fd94c0ca5e8918feccb9d43d9becc0e131da6833a15341e334bb78edc
MD5 45442c15af7978ae458a4349a8ef013c
BLAKE2b-256 6dfee343d5cc7cc5761d661643ef0d0094710c799a12e406a089bbd8848ae3b1

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp313-cp313-win_arm64.whl.

File metadata

File hashes

Hashes for etwtrace-0.1b5-cp313-cp313-win_arm64.whl
Algorithm Hash digest
SHA256 1f6daca9b81e067a1d6d18d3353ef77427bb11e1e0c815050d07fc115b4a1724
MD5 97110e4ae7c15195d8c4458fc7fefef1
BLAKE2b-256 4f9a9b02bac1ce3fb37240b077588145f474dd74035ad4e188e9fb82c8cd4a43

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for etwtrace-0.1b5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b1b0ba4d25dc15aaba9bd83041b50281327986fa50e81341420341a44d1a3ecf
MD5 d22ba1b5d9a29cc050587f7b7d363df5
BLAKE2b-256 1000cb94967a76d6e7253e7a418f346e873af1e3c699c5fb3db0180cb6e47776

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp312-cp312-win_arm64.whl.

File metadata

File hashes

Hashes for etwtrace-0.1b5-cp312-cp312-win_arm64.whl
Algorithm Hash digest
SHA256 790a3031945f7fcd488b05a6c353ee0aee8c64e8f42ff99a49438488ebc0770d
MD5 72a435a17297c23fc0a4f61ecb0db6a9
BLAKE2b-256 b1f803d4091d542c4a4833929a600a4a356e6cca0697a75c65608e8c1848a5f4

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for etwtrace-0.1b5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b76ca67980263b6cc13da7d6ac05ac34978a217fe3c6a906592498521347375e
MD5 cb05f86fa2a1ea95e8cad40ecee04899
BLAKE2b-256 00ca01dd33764461ee2bb1fe161fe07b68cfb6a35d9e8a02e202deb2f8d25a85

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp311-cp311-win_arm64.whl.

File metadata

File hashes

Hashes for etwtrace-0.1b5-cp311-cp311-win_arm64.whl
Algorithm Hash digest
SHA256 264577ff09861aa7326f95105f06196a70730df6c1570968753cc7098858cd9d
MD5 e367ec937c0321db28bb09487f27901d
BLAKE2b-256 08e97c2b61bdf72b9e7d71ff079eb37584609106a011e7a22c01fc985501d25f

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for etwtrace-0.1b5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 88546125ddea9f1b40324787912d7d909bf3c3d113aafed4a531b93554c3fde7
MD5 2d9e294c012612acb73189bccbd12469
BLAKE2b-256 552ad7448dff57942328774d1f912d08936bccaaa4b281cdf0f6809b6c7860a7

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for etwtrace-0.1b5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6968fdc4c6e82afc8037cffcdcaedfb025f6553f339df26fa19a3479a9d8705f
MD5 b84ced2bb2d7c4e824062bc98481bd8d
BLAKE2b-256 2e7710068028bdc4b727929117e20eec2b7b7cd9477f4962094ec915d62de92c

See more details on using hashes here.

File details

Details for the file etwtrace-0.1b5-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: etwtrace-0.1b5-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 190.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: RestSharp/106.13.0.0

File hashes

Hashes for etwtrace-0.1b5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 872fb7e481f83dcd1636f16d45298007e9330883af3bc394c77ba4db83db78b2
MD5 a3f873838c83e18ce93b5393a817d2e0
BLAKE2b-256 e81af5b1f81bd8698a922fbe6fe98b42f4195ff203e056557c2b5c6aedf96032

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page