Skip to content

Commit 50cad07

Browse files
authored
Merge branch 'master' into refactor/parentwithcontext-ppidwithcontext
2 parents 0306525 + 34e74aa commit 50cad07

9 files changed

+181
-170
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module github.com/shirou/gopsutil/v3
33
go 1.15
44

55
require (
6-
github.com/google/go-cmp v0.5.6
6+
github.com/google/go-cmp v0.5.7
77
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0
88
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c
99
github.com/stretchr/testify v1.7.0
1010
github.com/tklauser/go-sysconf v0.3.9
1111
github.com/yusufpapurcu/wmi v1.2.2
12-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c
12+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320
1313
)

go.sum

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
44
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
5-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
6-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
7-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
85
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
96
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
107
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
8+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
9+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
1110
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
1211
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
1312
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -26,8 +25,8 @@ github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
2625
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2726
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2827
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c h1:taxlMj0D/1sOAuv/CbSD+MMDof2vbyPTqz5FNYKpXt8=
30-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
29+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3130
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
3231
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3332
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

host/host_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
283283
family = "debian"
284284
case "fedora":
285285
family = "fedora"
286-
case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm", "rocky":
286+
case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm", "rocky", "almalinux":
287287
family = "rhel"
288288
case "suse", "opensuse", "opensuse-leap", "opensuse-tumbleweed", "opensuse-tumbleweed-kubic", "sles", "sled", "caasp":
289289
family = "suse"

process/process_darwin.go

+15-123
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
"path/filepath"
1111
"strconv"
1212
"strings"
13-
"time"
1413

15-
"github.com/shirou/gopsutil/v3/cpu"
1614
"github.com/shirou/gopsutil/v3/internal/common"
1715
"github.com/shirou/gopsutil/v3/net"
1816
"github.com/tklauser/go-sysconf"
@@ -46,99 +44,60 @@ type _Ctype_struct___0 struct {
4644
func pidsWithContext(ctx context.Context) ([]int32, error) {
4745
var ret []int32
4846

49-
pids, err := callPsWithContext(ctx, "pid", 0, false, false)
47+
kprocs, err := unix.SysctlKinfoProcSlice("kern.proc.all")
5048
if err != nil {
5149
return ret, err
5250
}
5351

54-
for _, pid := range pids {
55-
v, err := strconv.Atoi(pid[0])
56-
if err != nil {
57-
return ret, err
58-
}
59-
ret = append(ret, int32(v))
52+
for _, proc := range kprocs {
53+
ret = append(ret, int32(proc.Proc.P_pid))
6054
}
6155

6256
return ret, nil
6357
}
6458

6559
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
66-
r, err := callPsWithContext(ctx, "ppid", p.Pid, false, false)
67-
if err != nil {
68-
return 0, err
69-
}
70-
71-
v, err := strconv.Atoi(r[0][0])
60+
k, err := p.getKProc()
7261
if err != nil {
7362
return 0, err
7463
}
7564

76-
return int32(v), err
65+
return k.Eproc.Ppid, nil
7766
}
7867

7968
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
8069
k, err := p.getKProc()
8170
if err != nil {
8271
return "", err
8372
}
84-
name := common.IntToString(k.Proc.P_comm[:])
73+
74+
name := common.ByteToString(k.Proc.P_comm[:])
8575

8676
if len(name) >= 15 {
8777
cmdName, err := p.cmdNameWithContext(ctx)
8878
if err != nil {
8979
return "", err
9080
}
9181
if len(cmdName) > 0 {
92-
extendedName := filepath.Base(cmdName[0])
82+
extendedName := filepath.Base(cmdName)
9383
if strings.HasPrefix(extendedName, p.name) {
9484
name = extendedName
9585
} else {
96-
name = cmdName[0]
86+
name = cmdName
9787
}
9888
}
9989
}
10090

10191
return name, nil
10292
}
10393

104-
// cmdNameWithContext returns the command name (including spaces) without any arguments
105-
func (p *Process) cmdNameWithContext(ctx context.Context) ([]string, error) {
106-
r, err := callPsWithContext(ctx, "command", p.Pid, false, true)
107-
if err != nil {
108-
return nil, err
109-
}
110-
return r[0], err
111-
}
112-
11394
func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
114-
r, err := callPsWithContext(ctx, "etime", p.Pid, false, false)
95+
k, err := p.getKProc()
11596
if err != nil {
11697
return 0, err
11798
}
11899

119-
elapsedSegments := strings.Split(strings.Replace(r[0][0], "-", ":", 1), ":")
120-
var elapsedDurations []time.Duration
121-
for i := len(elapsedSegments) - 1; i >= 0; i-- {
122-
p, err := strconv.ParseInt(elapsedSegments[i], 10, 0)
123-
if err != nil {
124-
return 0, err
125-
}
126-
elapsedDurations = append(elapsedDurations, time.Duration(p))
127-
}
128-
129-
elapsed := time.Duration(elapsedDurations[0]) * time.Second
130-
if len(elapsedDurations) > 1 {
131-
elapsed += time.Duration(elapsedDurations[1]) * time.Minute
132-
}
133-
if len(elapsedDurations) > 2 {
134-
elapsed += time.Duration(elapsedDurations[2]) * time.Hour
135-
}
136-
if len(elapsedDurations) > 3 {
137-
elapsed += time.Duration(elapsedDurations[3]) * time.Hour * 24
138-
}
139-
140-
start := time.Now().Add(-elapsed)
141-
return start.Unix() * 1000, nil
100+
return k.Proc.P_starttime.Sec*1000 + int64(k.Proc.P_starttime.Usec)/1000, nil
142101
}
143102

144103
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
@@ -171,7 +130,7 @@ func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
171130
}
172131

173132
// See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html
174-
userEffectiveUID := int32(k.Eproc.Ucred.UID)
133+
userEffectiveUID := int32(k.Eproc.Ucred.Uid)
175134

176135
return []int32{userEffectiveUID}, nil
177136
}
@@ -183,7 +142,7 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
183142
}
184143

185144
gids := make([]int32, 0, 3)
186-
gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Ucred.Ngroups), int32(k.Eproc.Pcred.P_svgid))
145+
gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Pcred.P_svgid))
187146

188147
return gids, nil
189148
}
@@ -233,14 +192,6 @@ func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, e
233192
return nil, common.ErrNotImplementedError
234193
}
235194

236-
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
237-
r, err := callPsWithContext(ctx, "utime,stime", p.Pid, true, false)
238-
if err != nil {
239-
return 0, err
240-
}
241-
return int32(len(r)), nil
242-
}
243-
244195
func convertCPUTimes(s string) (ret float64, err error) {
245196
var t int
246197
var _tmp string
@@ -287,56 +238,6 @@ func convertCPUTimes(s string) (ret float64, err error) {
287238
return float64(t) / float64(clockTicks), nil
288239
}
289240

290-
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
291-
r, err := callPsWithContext(ctx, "utime,stime", p.Pid, false, false)
292-
if err != nil {
293-
return nil, err
294-
}
295-
296-
utime, err := convertCPUTimes(r[0][0])
297-
if err != nil {
298-
return nil, err
299-
}
300-
stime, err := convertCPUTimes(r[0][1])
301-
if err != nil {
302-
return nil, err
303-
}
304-
305-
ret := &cpu.TimesStat{
306-
CPU: "cpu",
307-
User: utime,
308-
System: stime,
309-
}
310-
return ret, nil
311-
}
312-
313-
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
314-
r, err := callPsWithContext(ctx, "rss,vsize,pagein", p.Pid, false, false)
315-
if err != nil {
316-
return nil, err
317-
}
318-
rss, err := strconv.Atoi(r[0][0])
319-
if err != nil {
320-
return nil, err
321-
}
322-
vms, err := strconv.Atoi(r[0][1])
323-
if err != nil {
324-
return nil, err
325-
}
326-
pagein, err := strconv.Atoi(r[0][2])
327-
if err != nil {
328-
return nil, err
329-
}
330-
331-
ret := &MemoryInfoStat{
332-
RSS: uint64(rss) * 1024,
333-
VMS: uint64(vms) * 1024,
334-
Swap: uint64(pagein),
335-
}
336-
337-
return ret, nil
338-
}
339-
340241
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
341242
pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid)
342243
if err != nil {
@@ -382,17 +283,8 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
382283

383284
// Returns a proc as defined here:
384285
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html
385-
func (p *Process) getKProc() (*KinfoProc, error) {
386-
buf, err := unix.SysctlRaw("kern.proc.pid", int(p.Pid))
387-
if err != nil {
388-
return nil, err
389-
}
390-
k, err := parseKinfoProc(buf)
391-
if err != nil {
392-
return nil, err
393-
}
394-
395-
return &k, nil
286+
func (p *Process) getKProc() (*unix.KinfoProc, error) {
287+
return unix.SysctlKinfoProc("kern.proc.pid", int(p.Pid))
396288
}
397289

398290
// call ps command.

0 commit comments

Comments
 (0)