-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.go
More file actions
114 lines (101 loc) · 3.48 KB
/
stats.go
File metadata and controls
114 lines (101 loc) · 3.48 KB
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
package bytespool
// SetWithStats enables or disables statistics collection.
// When enabled, statistics will be collected, but this may affect performance.
// When disabled (default), all atomic operations for statistics are skipped for better performance.
// This function is not thread-safe and should be called before any pool operations.
func SetWithStats(t bool) {
DefaultCapacityPools.SetWithStats(t)
}
// GetWithStats returns the current status of statistics collection.
// When true, statistics are being collected.
// When false (default), statistics are not being collected.
func GetWithStats() bool {
return DefaultCapacityPools.GetWithStats()
}
// RuntimeStats returns runtime statistics for byte pools.
// The statistics include:
// - MinSize: minimum pool size
// - MaxSize: maximum pool size
// - NewBytes: total bytes newly allocated for pools
// - OutBytes: total bytes allocated outside pools
// - OutCount: total number of bytes allocated outside pools
// - ReusedBytes: total bytes reused from pools
//
// The statistics collection can be enabled/disabled with SetWithStats().
// When disabled (default), all counters will be zero.
func RuntimeStats(ps ...*CapacityPools) map[string]uint64 {
p := DefaultCapacityPools
if len(ps) > 0 {
p = ps[0]
}
if !p.GetWithStats() {
return nil
}
nb := p.getTotalNewBytes()
ob := p.getTotalOutBytes()
oc := p.getOutCount()
rb := p.getTotalReusedBytes()
return map[string]uint64{
"MinSize": uint64(p.MinSize()),
"MaxSize": uint64(p.MaxSize()),
"NewBytes": nb,
"OutBytes": ob,
"OutCount": oc,
"ReusedBytes": rb,
}
}
// RuntimeSummary is a structured summary of runtime pool statistics.
// It contains global byte counters and the top pools by reuse hits.
type RuntimeSummary struct {
MinSize int // minimum pool size
MaxSize int // maximum pool size
NewBytes uint64 // total bytes newly allocated for pools
OutBytes uint64 // total bytes allocated outside pools
OutCount uint64 // total number of bytes allocated outside pools
ReusedBytes uint64 // total bytes reused from pools
TopPools []PoolStat // top pools by reuse hits (ranked)
}
// RuntimeStatsSummary returns a structured RuntimeSummary for the provided
// CapacityPools (or the default pools when none provided).
func RuntimeStatsSummary(topN int, ps ...*CapacityPools) RuntimeSummary {
p := DefaultCapacityPools
if len(ps) > 0 {
p = ps[0]
}
if !p.GetWithStats() {
return RuntimeSummary{}
}
summary := RuntimeSummary{
MinSize: p.MinSize(),
MaxSize: p.MaxSize(),
NewBytes: p.getTotalNewBytes(),
OutBytes: p.getTotalOutBytes(),
OutCount: p.getOutCount(),
ReusedBytes: p.getTotalReusedBytes(),
}
if topN > 0 {
summary.TopPools = p.getPoolReuseStats(topN)
}
return summary
}
// PoolStat represents a pool statistic entry
// - Rank: pool rank by reuse hits (1-based)
// - Capacity: pool capacity
// - ReuseHits: total number of times bytes have been reused from the pool
type PoolStat struct {
Rank int // pool rank by reuse hits (1-based)
Capacity int // pool capacity
ReuseHits uint64 // total number of times bytes have been reused from the pool
}
// PoolReuseStats returns the top N pool reuse statistics (by reuse hits).
// If n <= 0 it returns an empty slice.
func PoolReuseStats(topN int, ps ...*CapacityPools) []PoolStat {
p := DefaultCapacityPools
if len(ps) > 0 {
p = ps[0]
}
if topN <= 0 || !p.GetWithStats() {
return nil
}
return p.getPoolReuseStats(topN)
}