aboutsummaryrefslogtreecommitdiff
path: root/src/zen/trace/callstack_formatter.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-04-20 21:50:41 +0200
committerGitHub Enterprise <[email protected]>2026-04-20 21:50:41 +0200
commit2dfb5da16b97a6c12e01977af5b5188522178a4e (patch)
tree428aa0aa8e6079c64438931e0fd4f828c613c94d /src/zen/trace/callstack_formatter.cpp
parentAdd CompactString utility type (#990) (diff)
downloadarchived-zen-2dfb5da16b97a6c12e01977af5b5188522178a4e.tar.xz
archived-zen-2dfb5da16b97a6c12e01977af5b5188522178a4e.zip
zen trace analysis support (#945)
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).
Diffstat (limited to 'src/zen/trace/callstack_formatter.cpp')
-rw-r--r--src/zen/trace/callstack_formatter.cpp251
1 files changed, 251 insertions, 0 deletions
diff --git a/src/zen/trace/callstack_formatter.cpp b/src/zen/trace/callstack_formatter.cpp
new file mode 100644
index 000000000..0c601d5c0
--- /dev/null
+++ b/src/zen/trace/callstack_formatter.cpp
@@ -0,0 +1,251 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include "callstack_formatter.h"
+
+#include <zencore/fmtutils.h>
+#include <zencore/string.h>
+#include <zenutil/wildcard.h>
+
+#include <algorithm>
+
+namespace zen::trace_detail {
+
+namespace {
+
+ static bool IsKnownRuntimeModuleName(std::string_view ModuleName)
+ {
+ std::string LowerName = zen::ToLower(ModuleName);
+ return LowerName == "ntdll.dll" || LowerName == "kernel32.dll" || LowerName == "kernelbase.dll" || LowerName == "ucrtbase.dll" ||
+ LowerName == "ucrtbased.dll" || LowerName.starts_with("vcruntime") || LowerName.starts_with("msvcp") ||
+ LowerName.starts_with("api-ms-win-") || LowerName == "libc.so" || LowerName.starts_with("libc.so.") ||
+ LowerName == "libstdc++.so" || LowerName.starts_with("libstdc++.so.") || LowerName == "libgcc_s.so" ||
+ LowerName.starts_with("libgcc_s.so.") || LowerName == "libpthread.so" || LowerName.starts_with("libpthread.so.") ||
+ LowerName == "libm.so" || LowerName.starts_with("libm.so.") || LowerName == "ld-linux.so" ||
+ LowerName.starts_with("ld-linux") || LowerName == "libsystem_kernel.dylib" || LowerName == "libsystem_malloc.dylib" ||
+ LowerName == "libsystem_pthread.dylib" || LowerName == "libdyld.dylib";
+ }
+
+ static bool PathLooksThirdParty(std::string_view ModulePath)
+ {
+ std::string LowerPath = zen::ToLower(ModulePath);
+ return LowerPath.find("/thirdparty/") != std::string::npos || LowerPath.find("\\thirdparty\\") != std::string::npos ||
+ LowerPath.find("/third-party/") != std::string::npos || LowerPath.find("\\third-party\\") != std::string::npos ||
+ LowerPath.find("/external/") != std::string::npos || LowerPath.find("\\external\\") != std::string::npos ||
+ LowerPath.find("/extern/") != std::string::npos || LowerPath.find("\\extern\\") != std::string::npos ||
+ LowerPath.find("/engine/binaries/thirdparty/") != std::string::npos ||
+ LowerPath.find("\\engine\\binaries\\thirdparty\\") != std::string::npos ||
+ LowerPath.find("c:\\windows\\system32\\") != std::string::npos ||
+ LowerPath.find("c:\\windows\\syswow64\\") != std::string::npos || LowerPath.starts_with("/usr/lib/") ||
+ LowerPath.starts_with("/lib/") || LowerPath.starts_with("/system/");
+ }
+
+ static bool MatchesAnyPattern(std::string_view Text, const std::vector<std::string>& Patterns)
+ {
+ for (const std::string& Pattern : Patterns)
+ {
+ if (zen::MatchWildcard(Pattern, Text, /*CaseSensitive=*/false))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static bool ShouldSkipFrameByPattern(const CallstackFilterOptions& Options,
+ const TraceModel& Model,
+ const ResolvedFrame& Frame,
+ std::string_view Description)
+ {
+ if (MatchesAnyPattern(Description, Options.SkipPatterns))
+ {
+ return true;
+ }
+ if (Frame.ModuleIndex != ~0u && Frame.ModuleIndex < Model.Modules.size())
+ {
+ const ModuleInfo& Module = Model.Modules[Frame.ModuleIndex];
+ if (MatchesAnyPattern(Module.Name, Options.SkipPatterns) || MatchesAnyPattern(Module.FullPath, Options.SkipPatterns))
+ {
+ return true;
+ }
+ }
+
+ static const std::vector<std::string> kDefaultSkipPatterns = {
+ "zen::MemoryTrace_*",
+ "*mi_*",
+ "*_mi_*",
+ "*rpmalloc*",
+ "*mimalloc*",
+ "*je_malloc*",
+ "*je_free*",
+ "*malloc*",
+ "*free*",
+ "*realloc*",
+ };
+ return MatchesAnyPattern(Description, kDefaultSkipPatterns);
+ }
+
+ static bool IsThirdPartyFrame(const TraceModel& Model, const ResolvedFrame& Frame, std::string_view Description)
+ {
+ if (Description.starts_with("std::"))
+ {
+ return true;
+ }
+ if (Frame.ModuleIndex == ~0u || Frame.ModuleIndex >= Model.Modules.size())
+ {
+ return false;
+ }
+
+ const ModuleInfo& Module = Model.Modules[Frame.ModuleIndex];
+ return IsKnownRuntimeModuleName(Module.Name) || PathLooksThirdParty(Module.FullPath);
+ }
+
+} // namespace
+
+CallstackFormatter::CallstackFormatter(const TraceModel& InModel, const SymbolResolver* InSymbols) : m_Model(InModel), m_Symbols(InSymbols)
+{
+}
+
+const CallstackEntry*
+CallstackFormatter::FindCallstackEntry(uint32_t CallstackId) const
+{
+ auto It =
+ eastl::lower_bound(m_Model.Callstacks.begin(), m_Model.Callstacks.end(), CallstackId, [](const CallstackEntry& E, uint32_t Id) {
+ return E.Id < Id;
+ });
+ if (It == m_Model.Callstacks.end() || It->Id != CallstackId)
+ {
+ return nullptr;
+ }
+ return &*It;
+}
+
+const std::string&
+CallstackFormatter::Describe(const ResolvedFrame& Frame)
+{
+ auto It = m_Cache.find(Frame.Address);
+ if (It != m_Cache.end())
+ {
+ return It->second;
+ }
+
+ std::string Result = m_Symbols ? m_Symbols->Resolve(Frame.Address) : std::string{};
+ if (Result.empty())
+ {
+ if (Frame.ModuleIndex != ~0u && Frame.ModuleIndex < m_Model.Modules.size())
+ {
+ Result = fmt::format("{} + 0x{:X}", m_Model.Modules[Frame.ModuleIndex].Name, Frame.Offset);
+ }
+ else
+ {
+ Result = fmt::format("0x{:X}", Frame.Address);
+ }
+ }
+
+ auto [InsertedIt, Inserted] = m_Cache.emplace(Frame.Address, std::move(Result));
+ ZEN_UNUSED(Inserted);
+ return InsertedIt->second;
+}
+
+FilteredCallstackView
+CallstackFormatter::BuildView(const CallstackEntry& Entry, const CallstackFilterOptions& Options)
+{
+ FilteredCallstackView Result;
+ Result.Frames.reserve(Entry.Frames.size());
+ if (Entry.Frames.empty())
+ {
+ return Result;
+ }
+
+ eastl::vector<bool> ExplicitSkip;
+ eastl::vector<bool> ThirdParty;
+ ExplicitSkip.reserve(Entry.Frames.size());
+ ThirdParty.reserve(Entry.Frames.size());
+
+ for (const ResolvedFrame& Frame : Entry.Frames)
+ {
+ const std::string& Description = Describe(Frame);
+ ExplicitSkip.push_back(ShouldSkipFrameByPattern(Options, m_Model, Frame, Description));
+ ThirdParty.push_back(IsThirdPartyFrame(m_Model, Frame, Description));
+ }
+
+ eastl::vector<size_t> VisibleFrameIndices;
+ VisibleFrameIndices.reserve(Entry.Frames.size());
+
+ if (!Options.EnableHeuristic)
+ {
+ for (size_t Index = 0; Index < Entry.Frames.size(); ++Index)
+ {
+ if (!ExplicitSkip[Index])
+ {
+ VisibleFrameIndices.push_back(Index);
+ }
+ }
+ if (VisibleFrameIndices.empty())
+ {
+ VisibleFrameIndices.push_back(0);
+ }
+ }
+ else
+ {
+ size_t FirstProgramIndex = Entry.Frames.size();
+ size_t BoundaryThirdPartyIndex = Entry.Frames.size();
+ for (size_t Index = 0; Index < Entry.Frames.size(); ++Index)
+ {
+ if (ExplicitSkip[Index])
+ {
+ continue;
+ }
+ if (ThirdParty[Index])
+ {
+ BoundaryThirdPartyIndex = Index;
+ continue;
+ }
+ FirstProgramIndex = Index;
+ break;
+ }
+
+ if (FirstProgramIndex == Entry.Frames.size())
+ {
+ for (size_t Index = 0; Index < Entry.Frames.size(); ++Index)
+ {
+ if (!ExplicitSkip[Index])
+ {
+ VisibleFrameIndices.push_back(Index);
+ }
+ }
+ if (VisibleFrameIndices.empty())
+ {
+ VisibleFrameIndices.push_back(0);
+ }
+ }
+ else
+ {
+ if (BoundaryThirdPartyIndex < Entry.Frames.size())
+ {
+ VisibleFrameIndices.push_back(BoundaryThirdPartyIndex);
+ Result.IncludedThirdPartyBoundary = true;
+ }
+ for (size_t Index = FirstProgramIndex; Index < Entry.Frames.size(); ++Index)
+ {
+ if (!ExplicitSkip[Index])
+ {
+ VisibleFrameIndices.push_back(Index);
+ }
+ }
+ if (VisibleFrameIndices.empty())
+ {
+ VisibleFrameIndices.push_back(FirstProgramIndex);
+ }
+ }
+ }
+
+ Result.HiddenPrefixCount = uint32_t(VisibleFrameIndices.front());
+ for (size_t FrameIndex : VisibleFrameIndices)
+ {
+ Result.Frames.push_back(
+ {.OriginalIndex = FrameIndex, .Frame = &Entry.Frames[FrameIndex], .Display = Describe(Entry.Frames[FrameIndex])});
+ }
+ return Result;
+}
+
+} // namespace zen::trace_detail