/
profile.go
211 lines (181 loc) · 5.33 KB
/
profile.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020
package internal
import (
"bytes"
"strconv"
"sync"
"time"
)
// Supported profile runtimes
const (
RuntimeGolang = "golang"
)
// Supported profile categories
const (
CategoryCPU = "cpu"
CategoryMemory = "memory"
CategoryTime = "time"
)
// Supported profile types
const (
TypeCPUUsage = "cpu-usage"
TypeMemoryAllocation = "memory-allocations"
TypeBlockingCalls = "blocking-calls"
)
// Human-readable measurement units
const (
UnitSample = "sample"
UnitMillisecond = "millisecond"
UnitMicrosecond = "microsecond"
UnitNanosecond = "nanosecond"
UnitByte = "byte"
UnitKilobyte = "kilobyte"
UnitPercent = "percent"
)
// AgentProfile is a presenter type used to serialize a collected profile
// to JSON format supported by Instana profile sensor
type AgentProfile struct {
ID string `json:"id"`
Runtime string `json:"runtime"`
Category string `json:"category"`
Type string `json:"type"`
Unit string `json:"unit"`
Roots []AgentCallSite `json:"roots"`
Duration int64 `json:"duration"`
Timespan int64 `json:"timespan"`
Timestamp int64 `json:"timestamp"`
}
// NewAgentProfile creates a new profile payload for the host agent
func NewAgentProfile(p *Profile) AgentProfile {
callSites := make([]AgentCallSite, 0, len(p.Roots))
for _, root := range p.Roots {
callSites = append(callSites, NewAgentCallSite(root))
}
return AgentProfile{
ID: p.ID,
Runtime: p.Runtime,
Category: p.Category,
Type: p.Type,
Unit: p.Unit,
Roots: callSites,
Duration: p.Duration,
Timespan: p.Timespan,
Timestamp: p.Timestamp,
}
}
// Profile holds the gathered profiling data
type Profile struct {
ID string
Runtime string
Category string
Type string
Unit string
Roots []*CallSite
Duration int64
Timespan int64
Timestamp int64
}
// NewProfile inititalizes a new profile
func NewProfile(category string, typ string, unit string, roots []*CallSite, duration int64, timespan int64) *Profile {
return &Profile{
ID: GenerateUUID(),
Runtime: RuntimeGolang,
Category: category,
Type: typ,
Unit: unit,
Roots: roots,
Duration: duration / int64(time.Millisecond),
Timespan: timespan * 1000,
Timestamp: time.Now().Unix() * 1000,
}
}
// AgentCallSite is a presenter type used to serialize a call site
// to JSON format supported by Instana profile sensor
type AgentCallSite struct {
MethodName string `json:"method_name"`
FileName string `json:"file_name"`
FileLine int64 `json:"file_line"`
Measurement float64 `json:"measurement"`
NumSamples int64 `json:"num_samples"`
Children []AgentCallSite `json:"children"`
}
// NewAgentCallSite initializes a new call site payload for the host agent
func NewAgentCallSite(cs *CallSite) AgentCallSite {
children := make([]AgentCallSite, 0, len(cs.children))
for _, child := range cs.children {
children = append(children, NewAgentCallSite(child))
}
m, ns := cs.Measurement()
return AgentCallSite{
MethodName: cs.MethodName,
FileName: cs.FileName,
FileLine: cs.FileLine,
Measurement: m,
NumSamples: ns,
Children: children,
}
}
// CallSite represents a recorded method call
type CallSite struct {
MethodName string
FileName string
FileLine int64
Metadata map[string]string
measurement float64
numSamples int64
children map[string]*CallSite
updateLock *sync.RWMutex
}
// NewCallSite initializes a new CallSite
func NewCallSite(methodName string, fileName string, fileLine int64) *CallSite {
cn := &CallSite{
MethodName: methodName,
FileName: fileName,
FileLine: fileLine,
children: make(map[string]*CallSite),
updateLock: &sync.RWMutex{},
}
return cn
}
// FindOrAddChild adds a new subcall to a call tree. It returns the existing record the subcall already present
func (cs *CallSite) FindOrAddChild(methodName, fileName string, fileLine int64) *CallSite {
child := cs.findChild(methodName, fileName, fileLine)
if child == nil {
child = NewCallSite(methodName, fileName, fileLine)
cs.addChild(child)
}
return child
}
// Increment increases the sampled measurement while adding up the number of samples used
func (cs *CallSite) Increment(value float64, numSamples int64) {
cs.measurement += value
cs.numSamples += numSamples
}
// Measurement returns the sampled measurement along with the number of samples
func (cs *CallSite) Measurement() (value float64, numSamples int64) {
return cs.measurement, cs.numSamples
}
func (cs *CallSite) findChild(methodName, fileName string, fileLine int64) *CallSite {
cs.updateLock.RLock()
defer cs.updateLock.RUnlock()
if child, exists := cs.children[createKey(methodName, fileName, fileLine)]; exists {
return child
}
return nil
}
func (cs *CallSite) addChild(child *CallSite) {
cs.updateLock.Lock()
defer cs.updateLock.Unlock()
cs.children[createKey(child.MethodName, child.FileName, child.FileLine)] = child
}
func createKey(methodName, fileName string, fileLine int64) string {
var b bytes.Buffer
b.WriteString(methodName)
b.WriteString(" (")
b.WriteString(fileName)
b.WriteString(":")
b.WriteString(strconv.FormatInt(fileLine, 10))
b.WriteString(")")
return b.String()
}