-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
100 lines (83 loc) · 2.92 KB
/
state.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
package mon
import (
"sync"
"sync/atomic"
"unsafe"
"github.com/zeebo/mon/internal/lfht"
"github.com/zeebo/mon/inthist"
"github.com/zeebo/swaparoo"
)
var (
statesMu sync.Mutex // protects concurrent Collect calls.
states [2]lfht.Table // states maps names to State pointers.
tracker swaparoo.Tracker // keeps track of which state is valid.
)
func newState() unsafe.Pointer { return unsafe.Pointer(new(State)) }
func newCounter() unsafe.Pointer { return unsafe.Pointer(new(int64)) }
// State keeps track of all of the timer information for some calls.
type State struct {
errors lfht.Table
his inthist.Histogram
}
// Times calls the callback with all of the histograms that have been captured.
func Times(cb func(string, *State) bool) {
token := tracker.Acquire()
for iter := states[token.Gen()%2].Iterator(); iter.Next(); {
if !cb(iter.Key(), (*State)(iter.Value())) {
goto done
}
}
done:
token.Release()
}
// Collect consumes all of the histograms that have been captures and calls
// the callback for each one.
func Collect(cb func(string, *State) bool) {
statesMu.Lock()
gen := tracker.Increment().Wait()
for iter := states[gen%2].Iterator(); iter.Next(); {
if !cb(iter.Key(), (*State)(iter.Value())) {
goto done
}
}
done:
states[gen%2] = lfht.Table{}
statesMu.Unlock()
}
// GetState returns the current state for some name, allocating a new one if necessary.
func GetState(name string) *State {
token := tracker.Acquire()
state := (*State)(states[token.Gen()%2].Upsert(name, newState))
token.Release()
return state
}
// LookupState returns the current state for some name, returning nil if none exists.
func LookupState(name string) *State {
token := tracker.Acquire()
state := (*State)(states[token.Gen()%2].Lookup(name))
token.Release()
return state
}
// done informs the State that a task has completed in the given
// amount of nanoseconds.
func (s *State) done(v int64, kind string) {
s.his.Observe(v)
if kind != "" {
counter := (*int64)(s.errors.Upsert(kind, newCounter))
atomic.AddInt64(counter, 1)
}
}
// Histogram returns the Histogram associated with the state.
func (s *State) Histogram() *inthist.Histogram { return &s.his }
// Errors returns a tree of error counters. Be sure to use atomic.LoadInt64 on the results.
func (s *State) Errors() *lfht.Table { return &s.errors }
// Total returns the number of completed calls.
func (s *State) Total() int64 { return s.his.Total() }
// Quantile returns an estimation of the qth quantile in [0, 1].
func (s *State) Quantile(q float64) int64 { return s.his.Quantile(q) }
// Sum returns an estimation of the sum.
func (s *State) Sum() float64 { return s.his.Sum() }
// Average returns an estimation of the sum and average.
func (s *State) Average() (float64, float64) { return s.his.Average() }
// Variance returns an estimation of the sum, average and variance.
func (s *State) Variance() (float64, float64, float64) { return s.his.Variance() }