VizTracer is a low-overhead logging/debugging/profiling tool that can trace and visualize your python code execution.
The front-end UI is powered by Perfetto. Use "AWSD" to zoom/navigate. More help can be found in "Support - Controls".
The preferred way to install VizTracer is via pip
pip install viztracer
# Instead of "python3 my_script.py arg1 arg2"
viztracer my_script.py arg1 arg2
result.json
file will be generated, which you can open with vizviewer
vizviewer result.json
A VS Code Extension is available to make your life even easier.
--open
to open the reports right after tracing
flask
) are supported as well
You can also manually start/stop VizTracer in your script as well.
from viztracer import VizTracer
tracer = VizTracer()
tracer.start()
# Something happens here
tracer.stop()
tracer.save() # also takes output_file as an optional argument
Or, you can do it with with
statement
with VizTracer(output_file="optional.json") as tracer:
# Something happens here
If you are using Jupyter, you can use viztracer cell magics.
# You need to load the extension first
%load_ext viztracer
%%viztracer
# Your code after
A VizTracer Report
button will appear after the cell and you can click it to view the results
VizTracer can log native calls and GPU events of PyTorch (based on torch.profiler
) with
--log_torch
.
with VizTracer(log_torch=True) as tracer:
# Your torch code
viztracer --log_torch your_model.py
VizTracer can filter out the data you don't want to reduce overhead and keep info of a longer time period before you dump the log.
VizTracer can log extra information without changing your source code
VizTracer supports inserting custom events while the program is running. This works like a print debug, but you can know when this print happens while looking at trace data.
VizTracer supports python native threading
module without the need to do any modification to your code. Just start VizTracer
before you create threads and it will just work.
For other multi-thread scenarios, you can use enable_thread_tracing()
to notice VizTracer about the thread to trace it.
Refer to multi thread docs for details
VizTracer supports subprocess
, multiprocessing
, os.fork()
, concurrent.futures
, and loky
out of the box.
For more general multi-process cases, VizTracer can support with some extra steps.
Refer to multi process docs for details
VizTracer supports asyncio
natively, but could enhance the report by using --log_async
.
Refer to async docs for details
Perfetto supports native flamegraph, just select slices on the UI and choose "Slice Flamegraph".
VizTracer supports remote attach to an arbitrary Python process to trace it, as long as viztracer is importable
Refer to remote attach docs
VizTracer needs to dump the internal data to json format. It is recommended for the users to install orjson
, which is much faster than the builtin json
library. VizTracer will try to import orjson
and fall back to the builtin json
library if orjson
does not exist.
VizTracer will introduce 2x to 3x overhead in the worst case. The overhead is much better if there are less function calls or if filters are applied correctly.
For full documentation, please see https://viztracer.readthedocs.io/en/stable
Please send bug reports and feature requests through github issue tracker. VizTracer is currently under development now and it's open to any constructive suggestions.
Copyright 2020-2024 Tian Gao.
Distributed under the terms of the Apache 2.0 license.