forked from CodisLabs/codis
/
stats.go
226 lines (190 loc) · 3.92 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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package proxy
import (
"math"
"sort"
"sync"
"sync/atomic"
"time"
"github.com/CodisLabs/codis/pkg/utils"
"github.com/CodisLabs/codis/pkg/utils/sync2/atomic2"
)
type opStats struct {
opstr string
calls atomic2.Int64
nsecs atomic2.Int64
fails atomic2.Int64
redis struct {
errors atomic2.Int64
}
}
func (s *opStats) OpStats() *OpStats {
o := &OpStats{
OpStr: s.opstr,
Calls: s.calls.Int64(),
Usecs: s.nsecs.Int64() / 1e3,
Fails: s.fails.Int64(),
}
if o.Calls != 0 {
o.UsecsPercall = o.Usecs / o.Calls
}
o.RedisErrType = s.redis.errors.Int64()
return o
}
type OpStats struct {
OpStr string `json:"opstr"`
Calls int64 `json:"calls"`
Usecs int64 `json:"usecs"`
UsecsPercall int64 `json:"usecs_percall"`
Fails int64 `json:"fails"`
RedisErrType int64 `json:"redis_errtype"`
}
var cmdstats struct {
sync.RWMutex
opmap map[string]*opStats
total atomic2.Int64
fails atomic2.Int64
redis struct {
errors atomic2.Int64
}
qps atomic2.Int64
}
func init() {
cmdstats.opmap = make(map[string]*opStats, 128)
go func() {
for {
start := time.Now()
total := cmdstats.total.Int64()
time.Sleep(time.Second)
delta := cmdstats.total.Int64() - total
normalized := math.Max(0, float64(delta)) * float64(time.Second) / float64(time.Since(start))
cmdstats.qps.Set(int64(normalized + 0.5))
}
}()
}
func OpTotal() int64 {
return cmdstats.total.Int64()
}
func OpFails() int64 {
return cmdstats.fails.Int64()
}
func OpRedisErrors() int64 {
return cmdstats.redis.errors.Int64()
}
func OpQPS() int64 {
return cmdstats.qps.Int64()
}
func getOpStats(opstr string, create bool) *opStats {
cmdstats.RLock()
s := cmdstats.opmap[opstr]
cmdstats.RUnlock()
if s != nil || !create {
return s
}
cmdstats.Lock()
s = cmdstats.opmap[opstr]
if s == nil {
s = &opStats{opstr: opstr}
cmdstats.opmap[opstr] = s
}
cmdstats.Unlock()
return s
}
type sliceOpStats []*OpStats
func (s sliceOpStats) Len() int {
return len(s)
}
func (s sliceOpStats) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sliceOpStats) Less(i, j int) bool {
return s[i].OpStr < s[j].OpStr
}
func GetOpStatsAll() []*OpStats {
var all = make([]*OpStats, 0, 128)
cmdstats.RLock()
for _, s := range cmdstats.opmap {
all = append(all, s.OpStats())
}
cmdstats.RUnlock()
sort.Sort(sliceOpStats(all))
return all
}
func ResetStats() {
cmdstats.Lock()
cmdstats.opmap = make(map[string]*opStats, 128)
cmdstats.Unlock()
cmdstats.total.Set(0)
cmdstats.fails.Set(0)
cmdstats.redis.errors.Set(0)
sessions.total.Set(sessions.alive.Int64())
}
func incrOpTotal(n int64) {
cmdstats.total.Add(n)
}
func incrOpFails(n int64) {
cmdstats.fails.Add(n)
}
func incrOpStats(e *opStats) {
s := getOpStats(e.opstr, true)
s.calls.Add(e.calls.Swap(0))
s.nsecs.Add(e.nsecs.Swap(0))
if n := e.fails.Swap(0); n != 0 {
s.fails.Add(n)
cmdstats.fails.Add(n)
}
if n := e.redis.errors.Swap(0); n != 0 {
s.redis.errors.Add(n)
cmdstats.redis.errors.Add(n)
}
}
var sessions struct {
total atomic2.Int64
alive atomic2.Int64
}
func incrSessions() int64 {
sessions.total.Incr()
return sessions.alive.Incr()
}
func decrSessions() {
sessions.alive.Decr()
}
func SessionsTotal() int64 {
return sessions.total.Int64()
}
func SessionsAlive() int64 {
return sessions.alive.Int64()
}
type SysUsage struct {
Now time.Time
CPU float64
*utils.Usage
}
var lastSysUsage atomic.Value
func init() {
go func() {
for {
cpu, usage, err := utils.CPUUsage(time.Second)
if err != nil {
lastSysUsage.Store(&SysUsage{
Now: time.Now(),
})
} else {
lastSysUsage.Store(&SysUsage{
Now: time.Now(),
CPU: cpu, Usage: usage,
})
}
if err != nil {
time.Sleep(time.Second * 5)
}
}
}()
}
func GetSysUsage() *SysUsage {
if p := lastSysUsage.Load(); p != nil {
return p.(*SysUsage)
}
return nil
}