forked from davidbyttow/govips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
56 lines (49 loc) · 1.1 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
package vips
import "sync"
// RuntimeStats is a data structure to house a map of govips operation counts
type RuntimeStats struct {
OperationCounts map[string]int64
}
var (
operationCounter chan string
runtimeStats *RuntimeStats
statLock sync.RWMutex
)
func incOpCounter(op string) {
if operationCounter != nil {
operationCounter <- op
}
}
func collectStats() chan struct{} {
operationCounter = make(chan string, 100)
done := make(chan struct{})
exit := false
go func() {
for !exit {
select {
case op := <-operationCounter:
statLock.Lock()
runtimeStats.OperationCounts[op] = runtimeStats.OperationCounts[op] + 1
statLock.Unlock()
case <-done:
exit = true
break
}
}
}()
return done
}
// ReadRuntimeStats returns operation counts for govips
func ReadRuntimeStats(stats *RuntimeStats) {
statLock.RLock()
defer statLock.RUnlock()
stats.OperationCounts = make(map[string]int64)
for k, v := range runtimeStats.OperationCounts {
stats.OperationCounts[k] = v
}
}
func init() {
runtimeStats = &RuntimeStats{
OperationCounts: make(map[string]int64),
}
}