-
Notifications
You must be signed in to change notification settings - Fork 39
/
profile.go
174 lines (144 loc) · 3.71 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
package internal
import (
"bytes"
"os"
"strconv"
"sync"
"time"
)
const (
RuntimeGolang = "golang"
CategoryCPU = "cpu"
CategoryMemory = "memory"
CategoryTime = "time"
TypeCPUUsage = "cpu-usage"
TypeMemoryAllocation = "memory-allocations"
TypeBlockingCalls = "blocking-calls"
UnitSample = "sample"
UnitMillisecond = "millisecond"
UnitMicrosecond = "microsecond"
UnitNanosecond = "nanosecond"
UnitByte = "byte"
UnitKilobyte = "kilobyte"
UnitPercent = "percent"
)
type CallSite struct {
MethodName string
FileName string
FileLine int64
Metadata map[string]string
measurement float64
numSamples int64
children map[string]*CallSite
updateLock *sync.RWMutex
}
type Profile struct {
ID string
ProcessID string
Runtime string
Category string
Type string
Unit string
Roots []*CallSite
Duration int64
Timespan int64
Timestamp int64
}
func NewProfile(category string, typ string, unit string, roots []*CallSite, duration int64, timespan int64) *Profile {
p := &Profile{
ProcessID: strconv.Itoa(os.Getpid()),
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,
}
return p
}
func (p *Profile) ToMap() map[string]interface{} {
rootsMap := make([]interface{}, 0)
for _, root := range p.Roots {
rootsMap = append(rootsMap, root.ToMap())
}
profileMap := map[string]interface{}{
"pid": p.ProcessID,
"id": p.ID,
"runtime": p.Runtime,
"category": p.Category,
"type": p.Type,
"unit": p.Unit,
"roots": rootsMap,
"duration": p.Duration,
"timespan": p.Timespan,
"timestamp": p.Timestamp,
}
return profileMap
}
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
}
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
}
func (cs *CallSite) Increment(value float64, numSamples int64) {
cs.measurement += value
cs.numSamples += numSamples
}
func (cs *CallSite) Measurement() (value float64, numSamples int64) {
return cs.measurement, cs.numSamples
}
func (cs *CallSite) ToMap() map[string]interface{} {
childrenMap := make([]interface{}, 0)
for _, child := range cs.children {
childrenMap = append(childrenMap, child.ToMap())
}
m, ns := cs.Measurement()
callSiteMap := map[string]interface{}{
"method_name": cs.MethodName,
"file_name": cs.FileName,
"file_line": cs.FileLine,
"measurement": m,
"num_samples": ns,
"children": childrenMap,
}
return callSiteMap
}
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()
}