From 2dfb5da16b97a6c12e01977af5b5188522178a4e Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 20 Apr 2026 21:50:41 +0200 Subject: zen trace analysis support (#945) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Integrates the **tourist** trace analysis library and builds a full `zen trace` command suite for working with Unreal Engine `.utrace` files. ### Trace analysis library (`thirdparty/tourist/`) - Adds the tourist library as a third-party dependency with three modules: **foundation** (platform primitives, memory, scheduling), **trace** (UE Trace protocol decoding), and **analysis** (event dispatching and analyzer framework). - Cross-platform support for Windows, Linux, and macOS. ### `zen trace` CLI commands (`src/zen/cmds/`, `src/zen/trace/`) - **`zen trace analyze`** — Summarize a `.utrace` file: session metadata, thread inventory, command line + build configuration, CPU profiling scopes, timing, event rates, log messages, and (with symbols) memory allocation metrics including live-allocs dumps, callstack-keyed aggregation, and allocation churn. Optional HTML output for memory reports. - **`zen trace inspect`** — Dump the event schema (declared types, fields, sizes) from a trace file. - **`zen trace trim`** — Extract a time-window from a trace into a new `.utrace` file. - **`zen trace serve`** — Launch a local HTTP server hosting an interactive trace viewer; opens in the default browser. ### Symbolication (`src/zen/trace/symbol_resolver.*`, `thirdparty/raw_pdb/`) - Pluggable resolver with multiple backends: `pdb` (in-tree raw_pdb), `dbghelp` (Windows), `llvm-symbolizer` (all platforms), `atos` (macOS). An `auto` backend picks the best available tool per platform. - Microsoft Symbol Server support: downloads PDBs on demand using a redirect-aware HTTP client. - Local PDB cache keyed by image GUID preserves symbols across binary recompilation. - Callstack trimming heuristic strips UE internal noise from reports. - Binary analysis cache (`.ucache_z`) avoids re-resolving the same trace. ### Interactive trace viewer (`src/zen/frontend/html/`, `src/zen/trace/trace_viewer_service.*`) - Timeline: scope-level detail, horizontal zoom/pan, vertical scrolling, viewport-driven loading with pre-computed LOD for responsive navigation of large traces. - Thread grouping (collapsible sidebar sections) synthesized from name suffixes, natural sort order, visual distinction between lane threads and OS threads. - Bookmark and region annotations; region categories with per-category toggles; bookmark marker toggle in the toolbar. - Filterable Logs tab showing captured `UE_LOG` output. - Stats tab with per-scope aggregate statistics. - Memory tab with interactive allocation analysis and an allocation size histogram. - CsvProfiler event parsing and chart UI. ### Other in-branch supporting changes - **Cross-platform browser launcher** (`browser_launcher.{h,cpp}`) used by `trace serve`. - **`ReciprocalU64`** fast 64-bit integer division (zencore/intmath) for trace analyzers. - **`parallelsort`** cross-platform parallel sort helper (zenutil). - Frontend zip build rule so the viewer's HTML assets are bundled into `zen.exe`. - `/Zo` flag for better optimized debug info on Windows release builds. - `trace-tests.cpp` in the `zen-test` harness (harness itself landed on main via #985). --- src/zen/trace/timeline_query.cpp | 123 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/zen/trace/timeline_query.cpp (limited to 'src/zen/trace/timeline_query.cpp') diff --git a/src/zen/trace/timeline_query.cpp b/src/zen/trace/timeline_query.cpp new file mode 100644 index 000000000..d90c79a29 --- /dev/null +++ b/src/zen/trace/timeline_query.cpp @@ -0,0 +1,123 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include "timeline_query.h" + +#include + +namespace zen::trace_detail { + +namespace { + + // Pick the LOD level a given resolution should read from. Mirrors the + // historical selection in trace_viewer_service.cpp: resolution 0 reads the + // raw LOD 0; otherwise the smallest LOD whose ResolutionUs >= the request + // wins, falling back to the coarsest level if none qualify. + // + // Returned values: 0 == raw scopes (LOD 0), 1..kTimelineLodCount == DetailLevels[lod-1]. + size_t SelectLodIndex(uint32_t ResolutionUs) + { + if (ResolutionUs == 0) + { + return 0; + } + for (size_t I = 0; I < kTimelineLodCount; ++I) + { + if (kTimelineLodResolutions[I] >= ResolutionUs) + { + return I + 1; + } + } + return kTimelineLodCount; + } + + const eastl::vector& LodScopes(const ThreadTimeline& Timeline, size_t LodIndex) + { + if (LodIndex == 0) + { + return Timeline.Scopes; + } + return Timeline.DetailLevels[LodIndex - 1].Scopes; + } + + void ExtractScopesInto(const ThreadTimeline& Timeline, const TimelineQueryRequest& Req, std::vector& Out) + { + const eastl::vector& Scopes = LodScopes(Timeline, SelectLodIndex(Req.ResolutionUs)); + + auto MidIt = + std::lower_bound(Scopes.begin(), Scopes.end(), Req.StartUs, [](const TimelineScope& S, uint32_t V) { return S.BeginUs < V; }); + + for (auto It = Scopes.begin(); It != MidIt; ++It) + { + if ((It->BeginUs + It->DurationUs) < Req.StartUs || It->DurationUs < Req.MinDurUs) + { + continue; + } + Out.push_back({It->BeginUs, It->DurationUs, It->NameId, It->Depth, It->MergeCount}); + } + for (auto It = MidIt; It != Scopes.end(); ++It) + { + if (It->BeginUs > Req.EndUs) + { + break; + } + if (It->DurationUs < Req.MinDurUs) + { + continue; + } + Out.push_back({It->BeginUs, It->DurationUs, It->NameId, It->Depth, It->MergeCount}); + } + } + + const ThreadTimeline* FindThread(const TraceModel& Model, uint32_t ThreadId) + { + auto It = std::find_if(Model.Timelines.begin(), Model.Timelines.end(), [ThreadId](const ThreadTimeline& T) { + return T.ThreadId == ThreadId; + }); + return (It != Model.Timelines.end()) ? &*It : nullptr; + } + + class InMemoryTimelineQuery final : public TimelineQuery + { + public: + explicit InMemoryTimelineQuery(const TraceModel& Model) : m_Model(Model) {} + + void QueryThread(uint32_t ThreadId, const TimelineQueryRequest& Req, std::vector& Out) const override + { + const ThreadTimeline* Timeline = FindThread(m_Model, ThreadId); + if (Timeline) + { + ExtractScopesInto(*Timeline, Req, Out); + } + } + + void QueryBatch(std::span ThreadIds, const TimelineQueryRequest& Req, BatchResult& Out) const override + { + Out.Scopes.clear(); + Out.Ranges.clear(); + Out.Ranges.reserve(ThreadIds.size()); + + for (uint32_t ThreadId : ThreadIds) + { + const uint32_t Begin = uint32_t(Out.Scopes.size()); + const ThreadTimeline* Timeline = FindThread(m_Model, ThreadId); + if (Timeline) + { + ExtractScopesInto(*Timeline, Req, Out.Scopes); + } + Out.Ranges.push_back({Begin, uint32_t(Out.Scopes.size())}); + } + } + + private: + const TraceModel& m_Model; + }; + +} // namespace + +std::unique_ptr +MakeInMemoryTimelineQuery(const TraceModel& Model) +{ + return std::make_unique(Model); +} + +} // namespace zen::trace_detail -- cgit v1.2.3