forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
222 lines (191 loc) · 4.29 KB
/
monitor.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
// +build windows
package monitor
import (
"errors"
"fmt"
"math"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
)
var (
// Global kernel32 DLL
kernel32DLL = syscall.MustLoadDLL("kernel32")
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724400(v=vs.85).aspx
procGetSystemTimes = kernel32DLL.MustFindProc("GetSystemTimes")
)
type CPU struct {
User float64
Kernel float64
Idle float64
}
// Total returns the sum of user and kernel CPU time.
func (c CPU) Total() float64 {
return c.User + c.Kernel
}
type CPUTime struct {
previous uint64
delta uint64
load float64
}
func (c CPUTime) CPU() float64 { return c.load }
type monitorState int32
const (
stateStopped monitorState = iota
stateRunning
stateExited
)
type state struct {
val monitorState
}
func (s *state) Set(n monitorState) {
atomic.StoreInt32((*int32)(&s.val), int32(n))
}
func (s *state) Is(n monitorState) bool {
return atomic.LoadInt32((*int32)(&s.val)) == int32(n)
}
type Monitor struct {
user CPUTime
kernel CPUTime
idle CPUTime
mem MemStat // system memory
tick *time.Ticker // use tick.Stop() to stop monitoring
err error // system error, if any
inited bool // monitor initialized
mu sync.RWMutex // pids mutex
state state
cond *sync.Cond // Optional sync conditional for StatsCollector
}
func New(freq time.Duration) (*Monitor, error) {
if freq < time.Millisecond*10 {
freq = time.Millisecond * 500
}
m := &Monitor{
tick: time.NewTicker(freq),
inited: true,
}
if err := m.monitorLoop(); err != nil {
return nil, err
}
return m, nil
}
// condMonitor, returns a Monitor that broadcasts on cond on each update.
func condMonitor(freq time.Duration, cond *sync.Cond) (*Monitor, error) {
m := &Monitor{
tick: time.NewTicker(freq),
inited: true,
cond: cond,
}
m.state.Set(stateRunning)
if err := m.monitorLoop(); err != nil {
return nil, err
}
return m, nil
}
func (m *Monitor) MemStat() MemStat {
m.mu.RLock()
mem := m.mem
m.mu.RUnlock()
return mem
}
func (m *Monitor) CPU() (cpu CPU, err error) {
m.mu.RLock()
if !m.inited {
err = errors.New("monitor: not initialized")
}
if m.err != nil {
err = m.err
}
cpu = CPU{
Kernel: m.kernel.load,
User: m.user.load,
Idle: m.idle.load,
}
m.mu.RUnlock()
return
}
func (m *Monitor) monitorLoop() error {
m.state.Set(stateRunning)
if err := m.updateSystemCPU(); err != nil {
m.err = err
return m.err
}
go func() {
defer m.state.Set(stateExited)
for range m.tick.C {
// Hard error
if err := m.updateSystemCPU(); err != nil {
m.err = err
return
}
// TODO: FIX ME
if !m.state.Is(stateRunning) {
continue
}
}
}()
return nil
}
func (m *Monitor) updateSystemCPU() error {
if m.err != nil {
return m.err
}
var (
idleTime filetime
kernelTime filetime
userTime filetime
)
r1, _, e1 := syscall.Syscall(procGetSystemTimes.Addr(), 3,
uintptr(unsafe.Pointer(&idleTime)),
uintptr(unsafe.Pointer(&kernelTime)),
uintptr(unsafe.Pointer(&userTime)),
)
if err := checkErrno(r1, e1); err != nil {
m.err = fmt.Errorf("GetSystemTimes: %s", error(e1))
return m.err
}
m.calculateSystemCPU(kernelTime.Uint64(), userTime.Uint64(), idleTime.Uint64())
return nil
}
func (m *Monitor) calculateSystemCPU(kernelTicks, userTicks, idleTicks uint64) {
m.mu.Lock()
kernel := kernelTicks - m.kernel.previous
user := userTicks - m.user.previous
idle := idleTicks - m.idle.previous
total := kernel + user
if total > 0 {
m.idle.load = float64(idle) / float64(total)
m.idle.previous = idleTicks
m.idle.delta = idle
m.kernel.load = math.Max(float64(kernel-idle)/float64(total), 0)
m.kernel.previous = kernelTicks
m.kernel.delta = kernel
m.user.load = math.Max(1-m.idle.load-m.kernel.load, 0)
m.user.previous = userTicks
m.user.delta = user
} else {
m.idle.load = 0
m.kernel.load = 0
m.user.load = 0
}
m.mu.Unlock()
}
func checkErrno(r1 uintptr, err error) error {
if r1 == 0 {
if e, ok := err.(syscall.Errno); ok && e != 0 {
return err
}
return syscall.EINVAL
}
return nil
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx
type filetime struct {
LowDateTime uint32
HighDateTime uint32
}
func (f filetime) Uint64() uint64 {
return uint64(f.HighDateTime)<<32 | uint64(f.LowDateTime)
}