1How to use our internal tools to profile and keep Zed fast.
2
3# Rough quick CPU profiling (Flamechart)
4
5See what the CPU spends the most time on. Strongly recommend you use
6[samply](https://github.com/mstange/samply). It opens an interactive profile in
7the browser (specifically a local instance of [firefox_profiler](https://profiler.firefox.com/)).
8
9See [samply](https://github.com/mstange/samply)'s README on how to install and run.
10
11The profile.json does not contain any symbols. Firefox profiler can add the local symbols to the profile for for. To do that hit the upload local profile button in the top right corner.
12
13<img width="851" height="613" alt="image" src="https://github.com/user-attachments/assets/cbef2b51-0442-4ee9-bc5c-95f6ccf9be2c" />
14
15# In depth CPU profiling (Tracing)
16
17See how long each annotated function call took and its arguments (if
18configured).
19
20Annotate any function you need appear in the profile with instrument. For more
21details see
22[tracing-instrument](https://docs.rs/tracing/latest/tracing/attr.instrument.html):
23
24```rust
25#[instrument(skip_all)]
26fn should_appear_in_profile(kitty: Cat) {
27 sleep(QUITE_LONG)
28}
29```
30
31Then either compile Zed with `ZTRACING=1 cargo r --features tracy --release`. The release build is optional but highly recommended as like every program Zeds performance characteristics change dramatically with optimizations. You do not want to chase slowdowns that do not exist in release.
32
33## One time Setup/Building the profiler:
34
35Download the profiler:
36[linux x86_64](https://zed-tracy-import-miniprofiler.nyc3.digitaloceanspaces.com/tracy-profiler-linux-x86_64)
37[macos aarch64](https://zed-tracy-import-miniprofiler.nyc3.digitaloceanspaces.com/tracy-profiler-0.13.0-macos-aarch64)
38
39### Alternative: Building it yourself
40
41- Clone the repo at git@github.com:wolfpld/tracy.git
42- `cd profiler && mkdir build && cd build`
43- Run cmake to generate build files: `cmake -G Ninja -DCMAKE_BUILD_TYPE=Release ..`
44- Build the profiler: `ninja`
45- [Optional] move the profiler somewhere nice like ~/.local/bin on linux
46
47## Usage
48
49Open the profiler (tracy-profiler), you should see zed in the list of `Discovered clients` click it.
50<img width="392" height="287" alt="image" src="https://github.com/user-attachments/assets/b6f06fc3-6b25-41c7-ade9-558cc93d6033" />
51
52To find functions that take a long time follow this image:
53<img width="888" height="1159" alt="image" src="https://github.com/user-attachments/assets/77087617-f53a-4331-863d-e59f8a5b6f0b" />
54
55# Task/Async profiling
56
57Get a profile of the zed foreground executor and background executors. Check if
58anything is blocking the foreground too long or taking too much (clock) time in
59the background.
60
61The profiler always runs in the background. You can save a trace from its UI or
62look at the results live.
63
64## Setup/Building the importer:
65
66Download the importer
67[linux x86_64](https://zed-tracy-import-miniprofiler.nyc3.digitaloceanspaces.com/tracy-import-miniprofiler-linux-x86_64)
68[mac aarch64](https://zed-tracy-import-miniprofiler.nyc3.digitaloceanspaces.com/tracy-import-miniprofiler-macos-aarch64)
69
70### Alternative: Building it yourself
71
72- Clone the repo at git@github.com:zed-industries/tracy.git on v0.12.2 branch
73- `cd import && mkdir build && cd build`
74- Run cmake to generate build files: `cmake -G Ninja -DCMAKE_BUILD_TYPE=Release ..`
75- Build the importer: `ninja`
76- Run the importer on the trace file: `./tracy-import-miniprofiler /path/to/trace.miniprof /path/to/output.tracy`
77- Open the trace in tracy:
78 - If you're on windows download the v0.12.2 version from the releases on the upstream repo
79 - If you're on other platforms open it on the website: https://tracy.nereid.pl/ (the version might mismatch so your luck might vary, we need to host our own ideally..)
80
81## To Save a Trace:
82
83- Run the action: `zed open performance profiler`
84- Hit the save button. This opens a save dialog or if that fails to open the trace gets saved in your working directory.
85- Convert the profile so it can be imported in tracy using the importer: `./tracy-import-miniprofiler <path to performance_profile.miniprof> output.tracy`
86- Go to <https://tracy.nereid.pl/> hit the 'power button' in the top left and then open saved trace.
87- Now zoom in to see the tasks and how long they took
88
89# Warn if function is slow
90
91```rust
92let _timer = zlog::time!("my_function_name").warn_if_gt(std::time::Duration::from_millis(100));
93```