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
|
package statsengine
import (
"ior/internal/event"
"ior/internal/types"
"math"
"math/rand"
"sort"
"time"
)
const syscallReservoirSampleCapDefault = 10_000
type syscallAccumulator struct {
byID map[types.TraceId]*syscallStats
sampleCap int
rng *rand.Rand
}
type syscallStats struct {
traceID types.TraceId
name string
count uint64
errorCount uint64
totalBytes uint64
totalLatency uint64
minLatency uint64
maxLatency uint64
seenLatencies uint64
samples []uint64
}
func newSyscallAccumulator() *syscallAccumulator {
return newSyscallAccumulatorWithConfig(syscallReservoirSampleCapDefault, rand.New(rand.NewSource(time.Now().UnixNano())))
}
func newSyscallAccumulatorWithConfig(sampleCap int, rng *rand.Rand) *syscallAccumulator {
if sampleCap <= 0 {
sampleCap = syscallReservoirSampleCapDefault
}
if rng == nil {
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
}
return &syscallAccumulator{
byID: make(map[types.TraceId]*syscallStats),
sampleCap: sampleCap,
rng: rng,
}
}
func (a *syscallAccumulator) Add(pair *event.Pair) {
if a == nil || pair == nil || pair.EnterEv == nil {
return
}
traceID := pair.EnterEv.GetTraceId()
stats := a.byID[traceID]
if stats == nil {
stats = &syscallStats{traceID: traceID, name: traceID.Name()}
a.byID[traceID] = stats
}
stats.count++
stats.totalBytes += pair.Bytes
stats.totalLatency += pair.Duration
stats.updateMinMax(pair.Duration)
stats.addSample(pair.Duration, a.sampleCap, a.rng)
if retEv, ok := pair.ExitEv.(*types.RetEvent); ok && retEv.Ret < 0 {
stats.errorCount++
}
}
func (a *syscallAccumulator) Snapshot(elapsed time.Duration) []SyscallSnapshot {
if a == nil {
return nil
}
rateDiv := elapsed.Seconds()
result := make([]SyscallSnapshot, 0, len(a.byID))
for _, stats := range a.byID {
result = append(result, stats.toSnapshot(rateDiv))
}
sort.Slice(result, func(i, j int) bool {
if result[i].Count != result[j].Count {
return result[i].Count > result[j].Count
}
return result[i].Name < result[j].Name
})
return result
}
func (s *syscallStats) updateMinMax(duration uint64) {
if s.count == 1 || duration < s.minLatency {
s.minLatency = duration
}
if duration > s.maxLatency {
s.maxLatency = duration
}
}
func (s *syscallStats) addSample(duration uint64, cap int, rng *rand.Rand) {
s.seenLatencies++
if len(s.samples) < cap {
s.samples = append(s.samples, duration)
return
}
idx := rng.Int63n(int64(s.seenLatencies))
if idx >= int64(cap) {
return
}
s.samples[idx] = duration
}
func (s *syscallStats) toSnapshot(rateDiv float64) SyscallSnapshot {
sortedSamples := append([]uint64(nil), s.samples...)
sort.Slice(sortedSamples, func(i, j int) bool {
return sortedSamples[i] < sortedSamples[j]
})
return SyscallSnapshot{
TraceID: s.traceID,
Name: s.name,
Count: s.count,
RatePerSec: safeRate(s.count, rateDiv),
Errors: s.errorCount,
Bytes: s.totalBytes,
LatencyMinNs: s.minLatency,
LatencyMaxNs: s.maxLatency,
LatencyMeanNs: float64(s.totalLatency) / float64(maxU64(s.count, 1)),
LatencyP50Ns: samplePercentile(sortedSamples, 0.50),
LatencyP95Ns: samplePercentile(sortedSamples, 0.95),
LatencyP99Ns: samplePercentile(sortedSamples, 0.99),
}
}
func samplePercentile(sorted []uint64, p float64) uint64 {
if len(sorted) == 0 {
return 0
}
if p <= 0 {
return sorted[0]
}
if p >= 1 {
return sorted[len(sorted)-1]
}
rank := int(math.Ceil(p*float64(len(sorted)))) - 1
if rank < 0 {
rank = 0
}
if rank >= len(sorted) {
rank = len(sorted) - 1
}
return sorted[rank]
}
func safeRate(count uint64, elapsedSeconds float64) float64 {
if elapsedSeconds <= 0 {
return 0
}
return float64(count) / elapsedSeconds
}
func maxU64(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
|