-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.go
464 lines (365 loc) · 9.59 KB
/
init.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// -*- tab-width: 2 -*-
// Package counters enables 1 line creation of stats to track your program flow; you get summaries every minute
package counters
import (
"log"
"sort"
"strconv"
"sync"
"sync/atomic"
"time"
)
// numChannels is the number of API facing channels and reading goroutins
// to reduce lock contention.
const numChannels = 10
// MetricReport is the minutes change in
// the named metric.
type MetricReport struct {
Name string
Delta int64
}
// MetricReporter is a function callback that can be registered
// to dump metrics once a minute to some other system.
type MetricReporter func(metrics []MetricReport) // callback used below in SetMetricReporter
// ValReport is the minutes change in
// the named metric.
type ValReport struct {
Name string
Delta float64
}
// ValReporter is a function callback that can be registered
// to dump metrics once a minute to some other system.
type ValReporter func(metrics []ValReport) // callback used below in SetValReporter
type metaCounter struct {
name string
c1 string
c2 string
f MetaCounterF
}
type counter struct {
oldData int64
data int64
}
type counterMsg struct {
name string
suffix string
i int64
}
type value struct {
oldData float64
data float64
N float64
}
type valueMsg struct {
name string
suffix string
v float64
}
type ctx struct {
valuesByName map[string]*value // key present, nil value means check values
values map[string]*value
countersByName map[string]*counter // key present, nil value means check counter
counters map[string]*counter
metaCtrs map[string]*metaCounter
maxLen int // length of longest metric
logCb MetricReporter
valCb ValReporter
ctxLock sync.RWMutex
startTime time.Time
started bool
finished chan bool
c []chan counterMsg
v []chan valueMsg
fmtString string
fmtStringStr string
fmtStringF64 string
timeSleep float64
}
var theCtx = ctx{}
// LogCounters prints out the counters. It is called internally
// each minute but can be called externally e.g. at process end.
func LogCounters() { // nolint:cyclop
theCtx.ctxLock.Lock()
updateMaxLen(nil, nil)
theCtx.fmtString = "%-" + strconv.Itoa(theCtx.maxLen+12) + "s %20d %20d\n" //nolint:mnd
theCtx.fmtStringStr = "%-" + strconv.Itoa(theCtx.maxLen+12) + "s %20s %20s\n" //nolint:mnd
theCtx.fmtStringF64 = "%-" + strconv.Itoa(theCtx.maxLen+12) + "s %20f %20f\n" //nolint:mnd
fmtStringStr := theCtx.fmtStringStr
theCtx.ctxLock.Unlock()
log.Printf(fmtStringStr, "--------------------------", time.Now(), "")
log.Printf(fmtStringStr, "Uptime", time.Since(theCtx.startTime), "")
theCtx.ctxLock.Lock()
i := 0
// do meta counters first before oldData is updated
mctrNames := make([]string, len(theCtx.metaCtrs))
for k := range theCtx.metaCtrs { // cool scope is only in loop
mctrNames[i] = theCtx.metaCtrs[k].name
i++
}
log.Printf(theCtx.fmtStringStr, "---M-E-T-A- -C-O-U-N-T----", time.Now(), "")
sort.Strings(mctrNames)
for k := range mctrNames {
logMetaCounter(theCtx.metaCtrs[mctrNames[k]], theCtx.counters)
}
// then the counters
ctrNames := make([]string, len(theCtx.counters)+len(theCtx.countersByName))
valNames := make([]string, len(theCtx.values)+len(theCtx.valuesByName))
cbData := make([]MetricReport, len(theCtx.counters)+len(theCtx.countersByName)) // for CB
cbVal := make([]ValReport, len(theCtx.values)+len(theCtx.valuesByName)) // for CB
updateMaxLen(&ctrNames, &valNames)
sort.Strings(valNames)
for k := range valNames {
v, ok := theCtx.valuesByName[valNames[k]]
if !ok || v == nil {
v = theCtx.values[valNames[k]]
}
if theCtx.valCb != nil {
cbVal[k].Name = valNames[k]
cbVal[k].Delta = v.data - v.oldData
}
logValue(valNames[k], v)
newV := v
newV.oldData = newV.data // have to update old data
}
sort.Strings(ctrNames)
for k := range ctrNames {
v, ok := theCtx.countersByName[ctrNames[k]]
if !ok || v == nil {
v = theCtx.counters[ctrNames[k]]
}
data := atomic.LoadInt64(&v.data)
if theCtx.logCb != nil {
cbData[k].Name = ctrNames[k]
cbData[k].Delta = data - v.oldData
}
logCounter(ctrNames[k], v, data)
newC := v
newC.oldData = data // have to update old data
}
logCb := theCtx.logCb
valCb := theCtx.valCb
theCtx.ctxLock.Unlock()
if logCb != nil {
logCb(cbData)
}
if valCb != nil {
valCb(cbVal)
}
}
// updateMaxLen updates the max len for formatting for both vals and ctrs.
func updateMaxLen(ctrNames *[]string, valNames *[]string) { //nolint:cyclop
maxLen := 0
i := 0
for k := range theCtx.counters {
if len(k) > maxLen {
maxLen = len(k)
}
if ctrNames != nil {
(*ctrNames)[i] = k
}
i++
}
for k := range theCtx.countersByName {
if len(k) > maxLen {
maxLen = len(k)
}
if ctrNames != nil {
(*ctrNames)[i] = k
}
i++
}
i = 0
for k := range theCtx.values {
if len(k) > maxLen {
maxLen = len(k)
}
if valNames != nil {
(*valNames)[i] = k
}
i++
}
for k := range theCtx.valuesByName {
if len(k) > maxLen {
maxLen = len(k)
}
if valNames != nil {
(*valNames)[i] = k
}
i++
}
theCtx.maxLen = maxLen
}
func initCtx() {
theCtx.c = make([]chan counterMsg, numChannels)
for i := range numChannels {
theCtx.c[i] = make(chan counterMsg, 100000) //nolint:mnd
}
theCtx.v = make([]chan valueMsg, numChannels)
for i := range numChannels {
theCtx.v[i] = make(chan valueMsg, 100000) //nolint:mnd
}
theCtx.finished = make(chan bool, 1)
theCtx.counters = make(map[string]*counter)
theCtx.countersByName = make(map[string]*counter)
theCtx.values = make(map[string]*value)
theCtx.valuesByName = make(map[string]*value)
theCtx.metaCtrs = make(map[string]*metaCounter)
theCtx.started = true
theCtx.startTime = time.Now()
}
func minuteGoRoutine() {
theCtx.ctxLock.Lock()
if theCtx.timeSleep == 0 {
theCtx.timeSleep = 60.0
}
timeSleep := theCtx.timeSleep
theCtx.ctxLock.Unlock()
for {
n := time.Now()
time.Sleep(time.Second * (time.Duration(timeSleep) - time.Duration(int64(time.Since(n)/time.Second))))
checkRuntime()
LogCounters()
}
}
func readingValGoRoutine(index int) {
for {
select {
// no default because this should block
case <-theCtx.finished:
return
case vm := <-theCtx.v[index]:
getOrMakeAndSetValue(vm.name, vm.suffix, vm.v)
}
}
}
// getOrMakeCounter checks the 2 hashes and increments the appropriate place.
func getOrMakeAndIncrCounter(name string, suffix string, i int64) {
nameOnly := true // true means its in countersByName
key := ""
theCtx.ctxLock.RLock()
c, ok := theCtx.countersByName[name]
theCtx.ctxLock.RUnlock()
if ok && c == nil {
theCtx.ctxLock.RLock()
fullName := name + "/" + suffix
nameOnly = false
key = fullName
c, ok = theCtx.countersByName[fullName]
theCtx.ctxLock.RUnlock()
} else {
key = name
}
if !ok { // nolint:nestif
theCtx.ctxLock.Lock()
// another routine might have beat us to setting the counter, check again
if nameOnly {
c, ok = theCtx.countersByName[key]
} else {
c, ok = theCtx.counters[key]
}
if !ok {
c = &counter{}
c.data = i
if nameOnly {
theCtx.countersByName[key] = c
} else {
theCtx.counters[key] = c
}
} else {
atomic.AddInt64(&c.data, i)
}
theCtx.ctxLock.Unlock()
} else {
atomic.AddInt64(&c.data, i)
}
}
// getOrMakeValue returns a counter struct.
func getOrMakeAndSetValue(name string, suffix string, v float64) {
nameOnly := true
key := ""
theCtx.ctxLock.RLock()
c, ok := theCtx.valuesByName[name]
theCtx.ctxLock.RUnlock()
if ok && c == nil {
theCtx.ctxLock.RLock()
nameOnly = false
fullName := name + "/" + suffix // will malloc
key = fullName
c, ok = theCtx.valuesByName[fullName]
theCtx.ctxLock.RUnlock()
} else {
key = name
}
if !ok {
c = &value{}
c.data = v
theCtx.ctxLock.Lock()
if nameOnly {
theCtx.valuesByName[key] = c
} else {
theCtx.values[key] = c
}
theCtx.ctxLock.Unlock()
} else {
theCtx.ctxLock.Lock()
c.data = v // bad but no atomics and just 1/minute (from go stats)
theCtx.ctxLock.Unlock()
}
}
func readingCountsGoRoutine(index int) {
for {
select {
// no default because this should block
case <-theCtx.finished:
return
case cm := <-theCtx.c[index]:
getOrMakeAndIncrCounter(cm.name, cm.suffix, cm.i)
}
}
}
// InitCounters should be called at least once to start the go routines etc.
func InitCounters() {
theCtx.ctxLock.Lock()
defer theCtx.ctxLock.Unlock()
if theCtx.started {
return
}
initCtx()
// counters go routines
for i := range numChannels {
go readingCountsGoRoutine(i)
}
// values go routines
for i := range numChannels {
go readingValGoRoutine(i)
}
go minuteGoRoutine()
}
// SetMetricReporter specifies a function to be called once per
// LogInterval with the names of the current metrics and the last
// minute delta.
func SetMetricReporter(fn MetricReporter) {
theCtx.ctxLock.Lock()
theCtx.logCb = fn
theCtx.ctxLock.Unlock()
}
// SetValReporter specifies a function to be called once per
// LogInterval with the names of the current metrics which are
// float64s and the last minute delta.
func SetValReporter(fn ValReporter) {
theCtx.ctxLock.Lock()
theCtx.valCb = fn
theCtx.ctxLock.Unlock()
}
// SetLogInterval sets the number of seconds to sleep between logs of the counters.
func SetLogInterval(i float64) {
theCtx.ctxLock.Lock()
theCtx.timeSleep = i
theCtx.ctxLock.Unlock()
}
// SetFmtString sets the format string to log the counters with. It must have a %s and two %d.
func SetFmtString(fs string) {
theCtx.ctxLock.Lock()
theCtx.fmtString = fs // should validate
theCtx.ctxLock.Unlock()
}