Repository navigation
Add perfetto trace events for Rust - #8744
bartlomieju wants to merge 1 commit into
Conversation
Rust code had no way to emit perfetto trace events. The Rust server port (c7094b7) dropped the TRACE_EVENTs that server.c++ had, so the HTTP listener now leaves no slice in a trace. The new perfetto crate (src/workerd/util/perfetto) emits slices on the "workerd" category through a cxx bridge to C++ shims that include use-perfetto-categories.h. `perfetto::trace_event!("Name");` opens a slice on the thread track until the end of the enclosing block, as C++ TRACE_EVENT does. `perfetto::trace_event_async!("Name");` opens one on a track of its own. The ScopedSlice and AsyncSlice guards behind the macros are public for explicit use. There are two macros because slices on a thread track nest as a stack. An end event carries no name, so the trace processor closes the innermost open slice with it. When a future yields with a thread-track slice open, another future on the same event loop thread can open its own, and the first future's end then closes the wrong slice. So ScopedSlice is !Send, and clippy.toml makes holding it across an .await a lint error (await_holding_invalid_type). AsyncSlice uses a hashed per-slice track id under the process track, like async-trace-perfetto.c++, so it can span awaits and end on any thread. When nothing is tracing, a slice costs a relaxed load of the category's enabled byte. Rust caches that byte's address (static storage in perfetto) after the first call, so later calls make no FFI call, only a check that the address is cached. A slice that began while the category was disabled skips its end, even if a session has started since. With perfetto compiled out, the shims are no-ops and the byte is never set. Names are &'static CStr. Perfetto interns a static name by its address and reads it with strlen, so the macros NUL-terminate the literal at compile time. The first use is in HttpConnection::request() and connect() in the Rust server's listener. These are async slices, since both handlers await the worker. The tests start an in-process PerfettoSession, decode the trace with the pbzero decoders, and check the slice names, tracks and nesting. That includes slices that began before the session, interleaved async slices, slices held across awaits, and slices ended on another thread. Without perfetto, they check that nothing is recorded.
61cb258 to
5314be7
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8744 +/- ##
==========================================
- Coverage 67.73% 67.72% -0.02%
==========================================
Files 847 850 +3
Lines 215027 215109 +82
Branches 32588 32592 +4
==========================================
+ Hits 145647 145673 +26
- Misses 50137 50195 +58
+ Partials 19243 19241 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💬 Approved with comments — The production code looks sound. One WARNING: a test begins its slices outside the session lock, which can make the perfetto tests flaky. One INFO: a session that is never stopped is left running.
Findings: 1 warning · 1 info (2 are inline comments on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #8744.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## WARNING (1)
### [Code Quality] `slices_that_began_before_the_session_do_not_end_in_it` begins its slices before taking the session lock, so it races with other tests' sessions and can make them, and itself, fail intermittently.
- **File:** src/workerd/util/perfetto/lib-test.rs:95
## INFO (1)
### [Code Quality] `Session` has no `Drop`, so a test that panics between `start()` and `stop()` leaves the C++ session running, and later tests then fail in `start_session`.
- **File:** src/workerd/util/perfetto/test_session.rs:50
📋 Code Quality: ⚠️ 1 warning · 1 info
The crate and bridge look sound. Two test-harness problems: a race on the session lock, and no cleanup after a panic.
Recommendation: 📝 Minor Comments
- [WARNING]
slices_that_began_before_the_session_do_not_end_in_itbegins its slices before taking the session lock, so it races with other tests' sessions and can make them, and itself, fail intermittently. (src/workerd/util/perfetto/lib-test.rs:95) - [INFO]
Sessionhas noDrop, so a test that panics betweenstart()andstop()leaves the C++ session running, and later tests then fail instart_session. (src/workerd/util/perfetto/test_session.rs:50)
View full review
Race in slices_that_began_before_the_session_do_not_end_in_it (src/workerd/util/perfetto/lib-test.rs:95-96)
- The test calls
ScopedSlice::beginandAsyncSlice::beginbeforerecord()takesSESSION_LOCK. - Rust runs tests in parallel. If another test's session is running at that moment,
category_enabled()is true, so both slices become active and writeBevents into that other test's trace. - That other test then sees events it doesn't expect and fails.
- This test fails too: its own session records the slices'
Eevents, so it gets more than["B thread during", "E thread"]. - Fix: begin the slices while holding
perfetto_test_session::no_session(), then release it beforerecord.
Session has no Drop (src/workerd/util/perfetto/test_session.rs:50)
- If a test panics between
start()andstop(), the C++ session stays running. - The poisoned lock is recovered, so the next test goes ahead, but its
start_sessionfails because a session is already running. - One real failure then shows up as several unrelated ones.
✅ All other checks passed
🔒 Security: ✅ LGTM
- Trace event names are compile-time static C strings, and
__event_namerejects interior NULs. - No untrusted input crosses the FFI, and the listener's new slices carry no request data.
- No secrets in the change.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 5314be72
| let scoped = ScopedSlice::begin(c"before"); | ||
| let async_slice = AsyncSlice::begin(c"before async"); |
There was a problem hiding this comment.
slices_that_began_before_the_session_do_not_end_in_it begins its slices before taking the session lock, so it races with other tests' sessions and can make them, and itself, fail intermittently.
These two slices begin before record() takes SESSION_LOCK. If another test's session is running at that moment, category_enabled() is true, so the slices write B events into that session. That test then sees unexpected events, and this one records extra E events and fails its exact assertion. Begin the slices while holding no_session() so no session can be running when they start.
| let scoped = ScopedSlice::begin(c"before"); | |
| let async_slice = AsyncSlice::begin(c"before async"); | |
| let (scoped, async_slice) = { | |
| let _no_session = perfetto_test_session::no_session(); | |
| (ScopedSlice::begin(c"before"), AsyncSlice::begin(c"before async")) | |
| }; |
|
|
||
| /// A tracing session writing to a file in the test's temporary directory. Sessions run one at a | ||
| /// time. | ||
| pub struct Session { |
There was a problem hiding this comment.
ℹ️ INFO · Code Quality
Session has no Drop, so a test that panics between start() and stop() leaves the C++ session running, and later tests then fail in start_session.
If the closure passed to record panics, stop() never runs and the C++ session stays running. The poisoned lock is recovered, but the next test's start_session then fails its "a session is already running" check, so one failure shows up as several. A Drop that stops the session when stop() was not called would contain it.
Code moving from C++ to Rust loses its perfetto trace events, because
Rust can't emit them. The Rust server already emits none of the C++
server's TRACE_EVENTs, and Rust code that runs inside a C++ span has
to keep that span in a C++ call, which stops working once the work
inside the span moves into Rust.
This adds a perfetto crate (src/workerd/util/perfetto) that writes
slices into the same session and "workerd" category as C++.
trace_event!("Name");opens a slice on the thread's track until theend of the block, like TRACE_EVENT.
trace_event_async!("Name");putsit on its own track, so it can span an .await: thread-track slices
nest as a stack, so one held across an .await would close another
future's slice. clippy.toml makes holding the thread-track guard
across an .await a lint error. As a first use, the Rust server's HTTP
listener gets its request() and connect() slices back.
#7528 does this with perfetto's Rust SDK, which needs a patch to
perfetto's build that every embedder has to carry through perfetto
updates, and takes a mutex per event. This instead calls a few C++
shims over the C++ SDK we already link: no patch, no new crates, and
the same cost as C++ TRACE_EVENT. It only supports slices with static
names, which is enough for the events being ported now. #7528's API
can replace it if we need arguments or flows later. When nothing is
tracing, a slice costs one relaxed atomic load.
The tests start an in-process PerfettoSession, decode the trace, and
check slice names, tracks and nesting, including slices that span
awaits or end on another thread. With perfetto compiled out, they check
that nothing is recorded.