-
Notifications
You must be signed in to change notification settings - Fork 0
/
processinfosampler.go
66 lines (57 loc) · 1.45 KB
/
processinfosampler.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
package processinfo
import (
"context"
"time"
"github.com/olexnzarov/processinfo/internal/snapshot"
)
type sampler struct {
pid int
channel chan *ProcessInfo
previous *snapshot.Snapshot
}
// NewSampler returns a channel to which process information will be sent every 'rate' moment.
// It will contain an average CPU utilization during that time.
// The channel will be closed when the context is done.
// If any error occurs during the sampling, the information sent will be zeroed.
func NewSampler(ctx context.Context, pid int, rate time.Duration) <-chan *ProcessInfo {
sampler := &sampler{
pid: pid,
channel: make(chan *ProcessInfo, 1),
previous: snapshot.Zero,
}
go sampler.start(ctx, rate)
return sampler.channel
}
func (s *sampler) sendSample() {
current, err := snapshot.Get(s.pid)
// If something happened, just set everything to zero.
// Process might not be running anymore.
if err != nil {
current = snapshot.Zero
s.previous = snapshot.Zero
}
info := &ProcessInfo{
CPU: current.GetProcessorUtilization(s.previous),
Memory: current.GetMemory(),
}
s.previous = current
// Send the information non-blockingly.
select {
case s.channel <- info:
default:
}
}
func (s *sampler) start(ctx context.Context, rate time.Duration) {
defer close(s.channel)
s.sendSample()
ticker := time.NewTicker(rate)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.sendSample()
}
}
}