diff options
| author | Stefan Boberg <[email protected]> | 2026-04-20 21:50:41 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-04-20 21:50:41 +0200 |
| commit | 2dfb5da16b97a6c12e01977af5b5188522178a4e (patch) | |
| tree | 428aa0aa8e6079c64438931e0fd4f828c613c94d /thirdparty/tourist/trace/src/data.cpp | |
| parent | Add CompactString utility type (#990) (diff) | |
| download | archived-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 'thirdparty/tourist/trace/src/data.cpp')
| -rw-r--r-- | thirdparty/tourist/trace/src/data.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/thirdparty/tourist/trace/src/data.cpp b/thirdparty/tourist/trace/src/data.cpp new file mode 100644 index 000000000..da42284dc --- /dev/null +++ b/thirdparty/tourist/trace/src/data.cpp @@ -0,0 +1,166 @@ +#include <foundation/platform.h> +#include <foundation/types.h> +#include <trace/detail/data.h> + +//------------------------------------------------------------------------------ +#ifdef _WIN32 + +DataSource::DataSource(const Path& path) +{ + HANDLE handle = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, + nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if (handle == INVALID_HANDLE_VALUE) + throw std::runtime_error("Failed to open file"); + + _handle = uintptr(handle); +} + +DataSource::~DataSource() +{ + auto handle = HANDLE(_handle); + CloseHandle(handle); +} + +int32 DataSource::read(void* out, uint32 size) +{ + auto handle = HANDLE(_handle); + + DWORD bytes_read; + if (!ReadFile(handle, out, size, &bytes_read, nullptr)) + throw std::runtime_error("Read error"); + + return bytes_read; +} + +int64 DataSource::get_size() const +{ + if (_size >= 0) + return _size; + + auto handle = HANDLE(_handle); + + LARGE_INTEGER out; + GetFileSizeEx(handle, &out); + return _size = out.QuadPart; +} + +#else // POSIX + +DataSource::DataSource(const Path& path) +{ + int fd = ::open(path.string().c_str(), O_RDONLY); + if (fd < 0) + throw std::runtime_error("Failed to open file"); + + _handle = uintptr(fd); +} + +DataSource::~DataSource() +{ + ::close(int(_handle)); +} + +int32 DataSource::read(void* out, uint32 size) +{ + ssize_t bytes_read = ::read(int(_handle), out, size); + if (bytes_read < 0) + throw std::runtime_error("Read error"); + + return int32(bytes_read); +} + +int64 DataSource::get_size() const +{ + if (_size >= 0) + return _size; + + struct stat st; + if (fstat(int(_handle), &st) < 0) + return _size = 0; + + return _size = st.st_size; +} + +#endif + + + +//------------------------------------------------------------------------------ +DataStream::DataStream(DataSource& data_source, Allocator& allocator) +: _data_source(data_source) +, _allocator(allocator) +{ +} + +//------------------------------------------------------------------------------ +uint64 DataStream::tell() const +{ + return _position; +} + +//------------------------------------------------------------------------------ +uint32 DataStream::get_available() const +{ + return _stream.get_remaining(); +} + +//------------------------------------------------------------------------------ +Buffer DataStream::read(uint32 size) +{ + const uint8* ptr = _read(size); + return _buffer.create_sub_buffer(ptr, size); +} + +//------------------------------------------------------------------------------ +const uint8* DataStream::_read(uint32 size) +{ + _position += size; + + if (_stream.get_remaining() >= size) + return _stream.read(size); + + MutableBuffer buffer = _allocator.create_buffer(BUFFER_SIZE); + uint8* cursor = buffer.get_pointer(); + uint32 buffer_size = buffer.get_size(); + + if (uint32 remaining = _stream.get_remaining()) + { + const void* src = _stream.read(remaining); + std::memcpy(cursor, src, remaining); + cursor += remaining; + buffer_size -= remaining; + } + + uint32 read_size = 0; + while (true) + { + int32 i = _data_source.read(cursor + read_size, buffer_size - read_size); + if (i <= 0) + throw Eof(); + + if ((read_size += i) >= size) + break; + } + + if (read_size < buffer_size) + { + buffer_size = BUFFER_SIZE - buffer_size + read_size; + _buffer = buffer.create_sub_buffer(0u, buffer_size); + } + else + _buffer = std::move(buffer); + + _stream = _buffer.create_stream(); + + return _stream.read(size); +} + +//------------------------------------------------------------------------------ +template <> int8 DataStream::read< int8 >() { return *( int8 *)_read(sizeof( int8 )); } +template <> int16 DataStream::read< int16>() { return *( int16*)_read(sizeof( int16)); } +template <> int32 DataStream::read< int32>() { return *( int32*)_read(sizeof( int32)); } +template <> int64 DataStream::read< int64>() { return *( int64*)_read(sizeof( int64)); } +template <> uint8 DataStream::read<uint8 >() { return *(uint8 *)_read(sizeof(uint8 )); } +template <> uint16 DataStream::read<uint16>() { return *(uint16*)_read(sizeof(uint16)); } +template <> uint32 DataStream::read<uint32>() { return *(uint32*)_read(sizeof(uint32)); } +template <> uint64 DataStream::read<uint64>() { return *(uint64*)_read(sizeof(uint64)); } |