/
stats.go
218 lines (187 loc) · 4.55 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
package geetplaban
import (
"container/list"
"fmt"
"sync"
"sync/atomic"
"time"
)
const (
DEFAULT_NUM_SLOTS = 8
MAX_NUM_SLOTS = 256
MAX_NUM_SLOTS_MINUS_1 = 255
MIN_INTERVAL = 20
MAX_INTERVAL = 3600
)
func getCurrentInterval(width int64) int64 {
t := time.Now().Unix()
if width < MIN_INTERVAL {
width = MIN_INTERVAL
}
if width > MAX_INTERVAL {
width = MAX_INTERVAL
}
t = t - (t % width)
return t
}
type StageStats struct {
stage_id uint
emitted *uint64
failed *uint64
timespent *uint64
}
func NewStageStats(stage uint) *StageStats {
return &StageStats{stage, new(uint64), new(uint64), new(uint64)}
}
func (ss *StageStats) String() string {
return fmt.Sprintf("Sid:%d EM:%d F:%d T:%d",
ss.stage_id, *ss.emitted, *ss.failed, *ss.timespent)
}
func (ss *StageStats) addEmitted(val uint64) {
atomic.AddUint64(ss.emitted, val)
}
func (ss *StageStats) addFailed(val uint64) {
atomic.AddUint64(ss.failed, val)
}
func (ss *StageStats) addTimespent(val uint64) {
atomic.AddUint64(ss.timespent, val)
}
type AllStageStats struct {
rws []*sync.RWMutex
stage_stats []map[uint]*StageStats
num_slots uint
interval int64
}
func NewAllStageStats(num_slots uint, interval int64) *AllStageStats {
var rws []*sync.RWMutex
if num_slots > MAX_NUM_SLOTS {
num_slots = MAX_NUM_SLOTS
}
if num_slots == 0 {
num_slots = DEFAULT_NUM_SLOTS
}
i := uint(0)
for i < num_slots {
rws = append(rws, &sync.RWMutex{})
i++
}
var stage_stats []map[uint]*StageStats
i = 0
for i < num_slots {
stage_stats = append(stage_stats, make(map[uint]*StageStats))
i++
}
return &AllStageStats{rws, stage_stats, num_slots, interval}
}
func (allstgs *AllStageStats) getStats(stage_id uint) *StageStats {
slot := (stage_id & MAX_NUM_SLOTS_MINUS_1) % allstgs.num_slots
allstgs.rws[slot].RLock()
s, ok := allstgs.stage_stats[slot][stage_id]
allstgs.rws[slot].RUnlock()
if !ok {
allstgs.rws[slot].Lock()
s, ok = allstgs.stage_stats[slot][stage_id]
if !ok {
s = NewStageStats(stage_id)
allstgs.stage_stats[slot][stage_id] = s
}
allstgs.rws[slot].Unlock()
}
return s
}
func (allstgs *AllStageStats) addFailed(stage_id uint, delta uint64) {
s := allstgs.getStats(stage_id)
s.addFailed(delta)
}
func (allstgs *AllStageStats) addTimespent(stage_id uint, delta uint64) {
s := allstgs.getStats(stage_id)
s.addTimespent(delta)
}
func (allstgs *AllStageStats) addEmitted(stage_id uint, delta uint64) {
s := allstgs.getStats(stage_id)
s.addEmitted(delta)
}
func (allstgs *AllStageStats) dumpJson() {
for i, l := range allstgs.rws {
l.Lock()
for _, v := range allstgs.stage_stats[i] {
LOG.Infof("STAT: %s", v)
}
l.Unlock()
}
}
type AllStats struct {
t *time.Ticker
t2 *time.Ticker
flush *list.List
flushLock *sync.Mutex
current_stage_stats *AllStageStats
}
func NewAllStats() *AllStats {
t := time.NewTicker(1)
t2 := time.NewTicker(2)
flush := list.New()
flushl := &sync.Mutex{}
current_stage_stats := NewAllStageStats(8, getCurrentInterval(MIN_INTERVAL))
ret := &AllStats{t, t2, flush, flushl, current_stage_stats}
go ret.statsMonitor()
go ret.statsFlusher()
return ret
}
func (asts *AllStats) setCurrentStageStats(stgsts *AllStageStats) {
asts.current_stage_stats = stgsts
}
func (asts *AllStats) Stop() {
asts.t.Stop()
asts.t2.Stop()
}
func (asts *AllStats) addFailed(stage_id uint, delta uint64) {
asts.current_stage_stats.addFailed(stage_id, delta)
}
func (asts *AllStats) addEmitted(stage_id uint, delta uint64) {
asts.current_stage_stats.addEmitted(stage_id, delta)
}
func (asts *AllStats) addTimespent(stage_id uint, delta uint64) {
asts.current_stage_stats.addTimespent(stage_id, delta)
}
func (asts *AllStats) statsFlusher() {
for range asts.t2.C {
asts.flushLock.Lock()
e := asts.flush.Front()
asts.flushLock.Unlock()
if e == nil {
continue
}
asts.flushLock.Lock()
stgsts := asts.flush.Remove(e)
asts.flushLock.Unlock()
stgsts.(*AllStageStats).dumpJson()
}
}
func (asts *AllStats) statsMonitor() {
lastCheck := time.Now().Unix()
for range asts.t.C {
if time.Now().Unix()-lastCheck < 20 {
continue
}
lastCheck = time.Now().Unix()
temp := asts.current_stage_stats
asts.current_stage_stats = NewAllStageStats(8, getCurrentInterval(MIN_INTERVAL))
time.Sleep(2 * time.Second)
asts.flushLock.Lock()
asts.flush.PushBack(temp)
asts.flushLock.Unlock()
}
}
var (
allsts *AllStats = nil
stonce sync.Once
)
func init_stats() {
stonce.Do(func() {
allsts = NewAllStats()
})
}
func Stats() *AllStats {
return allsts
}