Repository navigation
Add perfetto trace events for Rust - #8744
Conversation
61cb258 to
5314be7
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8744 +/- ##
==========================================
- Coverage 67.54% 67.52% -0.03%
==========================================
Files 872 875 +3
Lines 220300 220386 +86
Branches 32952 32956 +4
==========================================
+ Hits 148808 148816 +8
- Misses 52037 52115 +78
Partials 19455 19455 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Superseded by the review of
c22a0cc5.
This review, of 5314be72
💬 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
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.
5314be7 to
c22a0cc
Compare
There was a problem hiding this comment.
Superseded by the review of
0e576c8c.
This review, of c22a0cc5
✅ Approved — Both earlier test findings (the lock race and the missing Drop on Session) are fixed, and nothing new turned up.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues found.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
lib-test.rs:96-113: the slices begin whileno_session()is held, so they are inactive and add noEevents to the trace. The expected events are correct.test_session.rs:84-92:Session::dropstops a session that is still running before_lockis released, so one panicking test no longer makes later tests fail.listen/mod.rs: thetrace_event_async!guards live in theasync_traitfutures, so each slice lasts until its request ends.
⚡ Performance: ✅ LGTM
Only test code changed since the last review. When nothing is tracing, a slice costs one relaxed atomic load.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: c22a0cc5
Perfetto keeps every track uuid a thread has written in a sorted set for the life of the session, so with a fresh track per async slice, each slice cost more than the one before it (about 4.4 µs per slice over the first 50k, 26 µs at 300k). A slice now hands its track back when it ends, and the next one to begin takes it, which bounds the number of tracks by peak concurrency and gives one track row per concurrent slice rather than one per slice.
There was a problem hiding this comment.
Superseded by the review of
19a11322.
This review, of 0e576c8c
✅ Approved — The new commit adds a free list so ended async slices hand their tracks to later ones. It keeps open slices on separate tracks and bounds the number of tracks by peak concurrency. The issues raised earlier are fixed, and nothing new turned up.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Event names are compile-time &'static CStr values, so no external data reaches the C++ shims. Track ids are hashed counters with no meaning outside the trace.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
🔄 Since last review: 1 finding resolved (the approver's note on track growth), 0 preserved, 0 new.
- Track reuse is safe. An
AsyncSliceputs its track on the free list only after it emits its end event (lib.rs:131-134). Only a laterbegintakes the track back off, so two open slices never share a track. - Slices that began while tracing was off stay off the list. Such a slice has
track == None, so it never touches the free list. - Lock handling. The mutex is held only for a single
poporpush, and a poisoned lock is recovered withPoisonError::into_inner. - Tests.
async_slices_reuse_the_tracks_of_ended_slicesexpects the right events. It does not depend on how much other tests have used the process-wide free list, because the decoder numbers tracks by first appearance within each trace. - Earlier fixes still hold. The lock race in
lib-test.rsand the missingDroponSessionare still fixed.
⚡ Performance: ✅ LGTM
Async slices take the free-list mutex only while the "workerd" category is enabled, and each critical section is a single push or pop. When nothing is tracing, a slice still costs one relaxed atomic load. Reusing tracks keeps the set of tracks perfetto has seen bounded by peak concurrency, so a slice no longer costs more as the session records more slices.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 0e576c8c
There was a problem hiding this comment.
✅ Approved — No files under review changed since the last approved review apart from a merge from main, and I found no issues.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Event names are compile-time &'static CStr values, so no external data reaches the C++ shims. ffi.c++ checks that the category-state byte has the same layout as AtomicU8. Track ids come from hashed counters or the free list and carry no request data.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
- Track reuse. An
AsyncSlicehands its track back only after it emits its end event. A slice that began while tracing was off has no track. So two open slices never share a track. - Clippy lint. The
clippy.tomlentry matches the lint thelib.rsdocs name, andScopedSliceis!Send. - Tests. They cover nesting, interleaving, awaits, slices that end on another thread, track reuse, slices that begin before the session, and the build with perfetto compiled out. Earlier fixes (the session lock and
DroponSession) still hold.
⚡ Performance: ✅ LGTM
When nothing is tracing, a slice costs one relaxed atomic load. Async slices take the free-list mutex only while the category is enabled, and only for one push or pop. Because ended slices hand their tracks to later ones, the number of tracks stays bounded by peak concurrency.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 19a11322
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.