-
Notifications
You must be signed in to change notification settings - Fork 30
/
statgo.go
75 lines (60 loc) · 1.23 KB
/
statgo.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
package statgo
// #cgo LDFLAGS: -lstatgrab
// #include <statgrab.h>
import "C"
import (
"runtime"
"sync"
)
// Stat handle to access libstatgrab
type Stat struct {
sync.Mutex
exitMessage chan bool
}
// NewStat return a new Stat handle
func NewStat() *Stat {
s := &Stat{}
runtime.SetFinalizer(s, (*Stat).free)
initDone := make(chan bool)
s.exitMessage = make(chan bool)
C.sg_init(1)
go func() {
// We need some function calls to be performed on the same thread
// Those for which statgrab is using a thread local
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Throw away the first reading as thats averaged over the machines uptime
C.sg_get_cpu_stats_diff(nil)
C.sg_get_network_io_stats_diff(nil)
C.sg_get_page_stats_diff(nil)
C.sg_get_disk_io_stats_diff(nil)
initDone <- true
for {
select {
case <-s.exitMessage:
return
case f := <-mainfunc:
f()
}
}
}()
<-initDone
return s
}
func (s *Stat) free() {
s.Lock()
C.sg_shutdown()
s.exitMessage <- true
s.Unlock()
}
// queue of work to run in main thread.
var mainfunc = make(chan func())
// do runs f on the main thread.
func do(f func()) {
done := make(chan bool, 1)
mainfunc <- func() {
f()
done <- true
}
<-done
}