forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.go
77 lines (65 loc) · 1.66 KB
/
process.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
package procstat
import (
"fmt"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/process"
)
type Process interface {
PID() PID
Tags() map[string]string
PageFaults() (*process.PageFaultsStat, error)
IOCounters() (*process.IOCountersStat, error)
MemoryInfo() (*process.MemoryInfoStat, error)
Name() (string, error)
Cmdline() (string, error)
NumCtxSwitches() (*process.NumCtxSwitchesStat, error)
NumFDs() (int32, error)
NumThreads() (int32, error)
Percent(interval time.Duration) (float64, error)
MemoryPercent() (float32, error)
Times() (*cpu.TimesStat, error)
RlimitUsage(bool) ([]process.RlimitStat, error)
Username() (string, error)
CreateTime() (int64, error)
}
type PIDFinder interface {
PidFile(path string) ([]PID, error)
Pattern(pattern string) ([]PID, error)
Uid(user string) ([]PID, error)
FullPattern(path string) ([]PID, error)
}
type Proc struct {
hasCPUTimes bool
tags map[string]string
*process.Process
}
func NewProc(pid PID) (Process, error) {
process, err := process.NewProcess(int32(pid))
if err != nil {
return nil, err
}
proc := &Proc{
Process: process,
hasCPUTimes: false,
tags: make(map[string]string),
}
return proc, nil
}
func (p *Proc) Tags() map[string]string {
return p.tags
}
func (p *Proc) PID() PID {
return PID(p.Process.Pid)
}
func (p *Proc) Username() (string, error) {
return p.Process.Username()
}
func (p *Proc) Percent(interval time.Duration) (float64, error) {
cpu_perc, err := p.Process.Percent(time.Duration(0))
if !p.hasCPUTimes && err == nil {
p.hasCPUTimes = true
return 0, fmt.Errorf("must call Percent twice to compute percent cpu")
}
return cpu_perc, err
}