summaryrefslogtreecommitdiff
path: root/internal/runtime_builder.go
blob: 17a69c653539d08eb51e3281b08ee693f7352b03 (plain)
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
package internal

import (
	"ior/internal/flags"
	"ior/internal/flamegraph"
	"ior/internal/statsengine"
	"ior/internal/streamrow"
)

// runtimeComponents holds the freshly allocated trace-session components
// produced by RuntimeBuilder.Build. All fields are non-nil after a successful
// build. The caller is responsible for wiring these into the runtime bindings
// and event-loop callbacks.
type runtimeComponents struct {
	engine    *statsengine.Engine
	streamBuf *streamrow.RingBuffer
	streamSeq *streamrow.Sequencer
	liveTrie  *flamegraph.LiveTrie
}

// RuntimeBuilder encapsulates the allocation of per-trace-session runtime
// components. Construct one with newRuntimeBuilder, then call Build to
// produce a fresh runtimeComponents struct ready for wiring.
type RuntimeBuilder struct {
	// cfg holds the configuration used to size and configure the components.
	cfg flags.Config
}

// newRuntimeBuilder creates a RuntimeBuilder configured from cfg.
// The builder captures cfg at construction time; later cfg changes do not affect
// a builder already created.
func newRuntimeBuilder(cfg flags.Config) RuntimeBuilder {
	return RuntimeBuilder{cfg: cfg}
}

// Build allocates a fresh set of runtime components for one trace session.
// It creates a new stats engine, stream ring buffer, sequencer, and live trie
// each time it is called; callers must not share the returned components across
// concurrent trace sessions.
func (b RuntimeBuilder) Build() runtimeComponents {
	return runtimeComponents{
		engine:    statsengine.NewEngine(statsengine.DefaultTopN),
		streamBuf: streamrow.NewRingBuffer(),
		streamSeq: streamrow.NewSequencer(0),
		liveTrie:  flamegraph.NewLiveTrie(b.cfg.CollapsedFields, b.cfg.CountField),
	}
}