forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
97 lines (78 loc) · 1.65 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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package router
import (
"encoding/json"
"sync"
"github.com/wandoulabs/codis/pkg/utils/atomic2"
)
type OpStats struct {
opstr string
calls atomic2.Int64
usecs atomic2.Int64
}
func (s *OpStats) OpStr() string {
return s.opstr
}
func (s *OpStats) Calls() int64 {
return s.calls.Get()
}
func (s *OpStats) USecs() int64 {
return s.usecs.Get()
}
func (s *OpStats) MarshalJSON() ([]byte, error) {
var m = make(map[string]interface{})
var calls = s.calls.Get()
var usecs = s.usecs.Get()
var perusecs int64 = 0
if calls != 0 {
perusecs = usecs / calls
}
m["cmd"] = s.opstr
m["calls"] = calls
m["usecs"] = usecs
m["usecs_percall"] = perusecs
return json.Marshal(m)
}
var cmdstats struct {
requests atomic2.Int64
opmap map[string]*OpStats
rwlck sync.RWMutex
}
func init() {
cmdstats.opmap = make(map[string]*OpStats)
}
func OpCounts() int64 {
return cmdstats.requests.Get()
}
func GetOpStats(opstr string, create bool) *OpStats {
cmdstats.rwlck.RLock()
s := cmdstats.opmap[opstr]
cmdstats.rwlck.RUnlock()
if s != nil || !create {
return s
}
cmdstats.rwlck.Lock()
s = cmdstats.opmap[opstr]
if s == nil {
s = &OpStats{opstr: opstr}
cmdstats.opmap[opstr] = s
}
cmdstats.rwlck.Unlock()
return s
}
func GetAllOpStats() []*OpStats {
var all = make([]*OpStats, 0, 128)
cmdstats.rwlck.RLock()
for _, s := range cmdstats.opmap {
all = append(all, s)
}
cmdstats.rwlck.RUnlock()
return all
}
func incrOpStats(opstr string, usecs int64) {
s := GetOpStats(opstr, true)
s.calls.Incr()
s.usecs.Add(usecs)
cmdstats.requests.Incr()
}