aboutsummaryrefslogtreecommitdiff
path: root/src/zen/trace/timeline_query.h
blob: f773d8e5828fe42c48f112f79aeb4b72f1734167 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "trace_model.h"

#include <cstdint>
#include <memory>
#include <span>
#include <vector>

namespace zen::trace_detail {

// Plain-data view of a single timeline scope returned by a TimelineQuery.
// Mirrors the on-disk TimelineScope but is intentionally decoupled from the
// in-memory model so that alternative backends can share the same result type.
struct TimelineScopeView
{
	uint32_t BeginUs;
	uint32_t DurationUs;
	uint32_t NameId;
	uint16_t Depth;
	uint16_t MergeCount;  // 0 == raw LOD 0, N>0 == N merged scopes
};

// Common parameters for a viewport-style timeline query.
struct TimelineQueryRequest
{
	uint32_t StartUs;
	uint32_t EndUs;
	uint32_t MinDurUs;
	uint32_t ResolutionUs;	// 0 == LOD 0 (raw); >0 picks the smallest LOD with ResolutionUs >= this
};

// Backend-agnostic interface for serving timeline scope data to the trace
// viewer HTTP handlers. Currently only the in-memory implementation exists,
// but the abstraction is preserved as a clean swap point if a different
// backend (e.g. on-disk indexed store) ever becomes useful.
class TimelineQuery
{
public:
	virtual ~TimelineQuery() = default;

	// Append all scopes for a single thread matching the request to Out.
	// Out is not cleared; callers can chain queries into the same buffer.
	virtual void QueryThread(uint32_t ThreadId, const TimelineQueryRequest& Req, std::vector<TimelineScopeView>& Out) const = 0;

	// Result of a batch query: a single flat scope vector plus per-thread
	// ranges into it. Ranges[i] corresponds to ThreadIds[i] from the request.
	struct BatchResult
	{
		struct Range
		{
			uint32_t Begin;
			uint32_t End;
		};
		std::vector<TimelineScopeView> Scopes;
		std::vector<Range>			   Ranges;
	};

	// Query several threads in one call. Out is cleared before being filled.
	virtual void QueryBatch(std::span<const uint32_t> ThreadIds, const TimelineQueryRequest& Req, BatchResult& Out) const = 0;
};

// In-memory implementation. Holds a reference to Model — the model must
// outlive the returned object.
std::unique_ptr<TimelineQuery> MakeInMemoryTimelineQuery(const TraceModel& Model);

}  // namespace zen::trace_detail