@@ -10,7 +10,6 @@ import (
10
10
"path/filepath"
11
11
"strconv"
12
12
"strings"
13
- "time"
14
13
15
14
"github.com/shirou/gopsutil/v3/cpu"
16
15
"github.com/shirou/gopsutil/v3/internal/common"
@@ -46,42 +45,34 @@ type _Ctype_struct___0 struct {
46
45
func pidsWithContext (ctx context.Context ) ([]int32 , error ) {
47
46
var ret []int32
48
47
49
- pids , err := callPsWithContext ( ctx , "pid" , 0 , false , false )
48
+ kprocs , err := unix . SysctlKinfoProcSlice ( "kern.proc.all" )
50
49
if err != nil {
51
50
return ret , err
52
51
}
53
52
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 ))
53
+ for _ , proc := range kprocs {
54
+ ret = append (ret , int32 (proc .Proc .P_pid ))
60
55
}
61
56
62
57
return ret , nil
63
58
}
64
59
65
60
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 ])
61
+ k , err := p .getKProc ()
72
62
if err != nil {
73
63
return 0 , err
74
64
}
75
65
76
- return int32 ( v ), err
66
+ return k . Eproc . Ppid , nil
77
67
}
78
68
79
69
func (p * Process ) NameWithContext (ctx context.Context ) (string , error ) {
80
70
k , err := p .getKProc ()
81
71
if err != nil {
82
72
return "" , err
83
73
}
84
- name := common .IntToString (k .Proc .P_comm [:])
74
+
75
+ name := common .ByteToString (k .Proc .P_comm [:])
85
76
86
77
if len (name ) >= 15 {
87
78
cmdName , err := p .cmdNameWithContext (ctx )
@@ -111,51 +102,21 @@ func (p *Process) cmdNameWithContext(ctx context.Context) ([]string, error) {
111
102
}
112
103
113
104
func (p * Process ) createTimeWithContext (ctx context.Context ) (int64 , error ) {
114
- r , err := callPsWithContext ( ctx , "etime" , p . Pid , false , false )
105
+ k , err := p . getKProc ( )
115
106
if err != nil {
116
107
return 0 , err
117
108
}
118
109
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
110
+ return k .Proc .P_starttime .Sec * 1000 + int64 (k .Proc .P_starttime .Usec )/ 1000 , nil
142
111
}
143
112
144
113
func (p * Process ) ParentWithContext (ctx context.Context ) (* Process , error ) {
145
- out , err := common . CallLsofWithContext (ctx , invoke , p . Pid , "-FR" )
114
+ ppid , err := p . PpidWithContext (ctx )
146
115
if err != nil {
147
116
return nil , err
148
117
}
149
- for _ , line := range out {
150
- if len (line ) >= 1 && line [0 ] == 'R' {
151
- v , err := strconv .Atoi (line [1 :])
152
- if err != nil {
153
- return nil , err
154
- }
155
- return NewProcessWithContext (ctx , int32 (v ))
156
- }
157
- }
158
- return nil , fmt .Errorf ("could not find parent line" )
118
+
119
+ return NewProcessWithContext (ctx , ppid )
159
120
}
160
121
161
122
func (p * Process ) StatusWithContext (ctx context.Context ) ([]string , error ) {
@@ -188,7 +149,7 @@ func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
188
149
}
189
150
190
151
// See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html
191
- userEffectiveUID := int32 (k .Eproc .Ucred .UID )
152
+ userEffectiveUID := int32 (k .Eproc .Ucred .Uid )
192
153
193
154
return []int32 {userEffectiveUID }, nil
194
155
}
@@ -200,7 +161,7 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
200
161
}
201
162
202
163
gids := make ([]int32 , 0 , 3 )
203
- gids = append (gids , int32 (k .Eproc .Pcred .P_rgid ), int32 (k .Eproc .Ucred . Ngroups ), int32 (k .Eproc .Pcred .P_svgid ))
164
+ gids = append (gids , int32 (k .Eproc .Pcred .P_rgid ), int32 (k .Eproc .Pcred . P_rgid ), int32 (k .Eproc .Pcred .P_svgid ))
204
165
205
166
return gids , nil
206
167
}
@@ -399,17 +360,8 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
399
360
400
361
// Returns a proc as defined here:
401
362
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html
402
- func (p * Process ) getKProc () (* KinfoProc , error ) {
403
- buf , err := unix .SysctlRaw ("kern.proc.pid" , int (p .Pid ))
404
- if err != nil {
405
- return nil , err
406
- }
407
- k , err := parseKinfoProc (buf )
408
- if err != nil {
409
- return nil , err
410
- }
411
-
412
- return & k , nil
363
+ func (p * Process ) getKProc () (* unix.KinfoProc , error ) {
364
+ return unix .SysctlKinfoProc ("kern.proc.pid" , int (p .Pid ))
413
365
}
414
366
415
367
// call ps command.
0 commit comments