-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstats.go
258 lines (221 loc) · 5.21 KB
/
stats.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package pinpoint
import (
"os"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/process"
)
type inspectorStats struct {
sampleTime time.Time
interval int64
cpuProcLoad float64
cpuSysLoad float64
heapUsed int64
heapMax int64
nonHeapUsed int64
nonHeapMax int64
gcNum int64
gcTime int64
numOpenFD int64
numThreads int64
responseAvg int64
responseMax int64
sampleNew int64
sampleCont int64
unSampleNew int64
unSampleCont int64
skipNew int64
skipCont int64
activeSpan []int32
}
var (
proc *process.Process
lastMemStat runtime.MemStats
lastCollectTime time.Time
accResponseTime int64
maxResponseTime int64
requestCount int64
sampleNew int64
unSampleNew int64
sampleCont int64
unSampleCont int64
skipNew int64
skipCont int64
activeSpan sync.Map
)
func initStats() {
var err error
proc, err = process.NewProcess(int32(os.Getpid()))
if err != nil {
proc = nil
} else {
proc.Percent(0)
}
cpu.Percent(0, false)
runtime.ReadMemStats(&lastMemStat)
lastCollectTime = time.Now()
activeSpan = sync.Map{}
}
func getNumFD() int32 {
if proc != nil {
n, _ := proc.NumFDs()
return n
}
return 0
}
func getNumThreads() int32 {
if proc != nil {
n, _ := proc.NumThreads()
return n
}
return 0
}
func getCpuLoad() (float64, float64) {
var procCpu float64
if proc != nil {
procCpu, _ = proc.Percent(0)
} else {
procCpu = 0
}
sysCpu, _ := cpu.Percent(0, false)
return procCpu / 100, sysCpu[0] / 100
}
func getStats() *inspectorStats {
now := time.Now()
procCpu, sysCpu := getCpuLoad()
var memStat runtime.MemStats
runtime.ReadMemStats(&memStat)
elapsed := now.Sub(lastCollectTime).Seconds()
stats := inspectorStats{
sampleTime: now,
interval: int64(elapsed) * 1000,
cpuProcLoad: procCpu,
cpuSysLoad: sysCpu,
heapUsed: int64(memStat.HeapInuse),
heapMax: int64(memStat.HeapSys),
nonHeapUsed: int64(memStat.StackInuse),
nonHeapMax: int64(memStat.StackSys),
gcNum: int64(memStat.NumGC - lastMemStat.NumGC),
gcTime: int64(memStat.PauseTotalNs-lastMemStat.PauseTotalNs) / int64(time.Millisecond),
numOpenFD: int64(getNumFD()),
numThreads: int64(getNumThreads()),
responseAvg: calcResponseAvg(),
responseMax: maxResponseTime,
sampleNew: sampleNew,
sampleCont: sampleCont,
unSampleNew: unSampleNew,
unSampleCont: unSampleCont,
skipNew: skipNew,
skipCont: skipCont,
activeSpan: activeSpanCount(now),
}
lastMemStat = memStat
lastCollectTime = now
resetResponseTime()
return &stats
}
func calcResponseAvg() int64 {
if requestCount > 0 {
return accResponseTime / requestCount
}
return 0
}
func activeSpanCount(now time.Time) []int32 {
count := []int32{0, 0, 0, 0}
activeSpan.Range(func(k, v interface{}) bool {
s := v.(time.Time)
d := now.Sub(s).Seconds()
if d < 1 {
count[0]++
} else if d < 3 {
count[1]++
} else if d < 5 {
count[2]++
} else {
count[3]++
}
return true
})
return count
}
func (agent *agent) collectAgentStatWorker() {
Log("stats").Infof("start collect agent stat goroutine")
defer agent.workerWg.Done()
initStats()
resetResponseTime()
interval := time.Duration(agent.config.Int(CfgStatCollectInterval)) * time.Millisecond
agent.statTicker = time.NewTicker(interval)
agent.statDone = make(chan bool)
cfgBatchCount := agent.config.Int(CfgStatBatchCount)
collected := make([]*inspectorStats, cfgBatchCount)
batch := 0
for agent.enable {
select {
case <-agent.statDone:
Log("stats").Infof("end collect agent stat goroutine")
return
case <-agent.statTicker.C:
collected[batch] = getStats()
batch++
if batch == cfgBatchCount {
agent.enqueueStat(makePAgentStatBatch(collected))
batch = 0
}
}
}
}
func collectResponseTime(resTime int64) {
atomic.AddInt64(&accResponseTime, resTime)
atomic.AddInt64(&requestCount, 1)
if atomic.LoadInt64(&maxResponseTime) < resTime {
atomic.StoreInt64(&maxResponseTime, resTime)
}
}
func resetResponseTime() {
atomic.StoreInt64(&accResponseTime, 0)
atomic.StoreInt64(&requestCount, 0)
atomic.StoreInt64(&maxResponseTime, 0)
atomic.StoreInt64(&sampleNew, 0)
atomic.StoreInt64(&unSampleNew, 0)
atomic.StoreInt64(&sampleCont, 0)
atomic.StoreInt64(&unSampleCont, 0)
atomic.StoreInt64(&skipNew, 0)
atomic.StoreInt64(&skipCont, 0)
}
func addSampledActiveSpan(span *span) {
activeSpan.Store(span.spanId, span.startTime)
addRealTimeSampledActiveSpan(span)
}
func dropSampledActiveSpan(span *span) {
activeSpan.Delete(span.spanId)
dropRealTimeSampledActiveSpan(span)
}
func addUnSampledActiveSpan(span *noopSpan) {
activeSpan.Store(span.spanId, span.startTime)
addRealTimeUnSampledActiveSpan(span)
}
func dropUnSampledActiveSpan(span *noopSpan) {
activeSpan.Delete(span.spanId)
dropRealTimeUnSampledActiveSpan(span)
}
func incrSampleNew() {
atomic.AddInt64(&sampleNew, 1)
}
func incrUnSampleNew() {
atomic.AddInt64(&unSampleNew, 1)
}
func incrSampleCont() {
atomic.AddInt64(&sampleCont, 1)
}
func incrUnSampleCont() {
atomic.AddInt64(&unSampleCont, 1)
}
func incrSkipNew() {
atomic.AddInt64(&skipNew, 1)
}
func incrSkipCont() {
atomic.AddInt64(&skipCont, 1)
}