mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
commit
50e1c3da5a
@ -527,34 +527,22 @@ func Processes() ([]*Process, error) {
|
||||
}
|
||||
|
||||
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
|
||||
results := []*Process{}
|
||||
out := []*Process{}
|
||||
|
||||
mib := []int32{CTLKern, KernProc, KernProcAll, 0}
|
||||
buf, length, err := common.CallSyscall(mib)
|
||||
pids, err := PidsWithContext(ctx)
|
||||
if err != nil {
|
||||
return results, err
|
||||
return out, err
|
||||
}
|
||||
|
||||
// get kinfo_proc size
|
||||
k := KinfoProc{}
|
||||
procinfoLen := int(unsafe.Sizeof(k))
|
||||
count := int(length / uint64(procinfoLen))
|
||||
|
||||
// parse buf to procs
|
||||
for i := 0; i < count; i++ {
|
||||
b := buf[i*procinfoLen : i*procinfoLen+procinfoLen]
|
||||
k, err := parseKinfoProc(b)
|
||||
for _, pid := range pids {
|
||||
p, err := NewProcess(pid)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
p, err := NewProcess(int32(k.Proc.P_pid))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, p)
|
||||
out = append(out, p)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseKinfoProc(buf []byte) (KinfoProc, error) {
|
||||
|
@ -1220,7 +1220,7 @@ func Processes() ([]*Process, error) {
|
||||
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
|
||||
out := []*Process{}
|
||||
|
||||
pids, err := Pids()
|
||||
pids, err := PidsWithContext(ctx)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
@ -603,21 +603,22 @@ func Processes() ([]*Process, error) {
|
||||
}
|
||||
|
||||
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
|
||||
pids, err := Pids()
|
||||
out := []*Process{}
|
||||
|
||||
pids, err := PidsWithContext(ctx)
|
||||
if err != nil {
|
||||
return []*Process{}, fmt.Errorf("could not get Processes %s", err)
|
||||
return out, fmt.Errorf("could not get Processes %s", err)
|
||||
}
|
||||
|
||||
results := []*Process{}
|
||||
for _, pid := range pids {
|
||||
p, err := NewProcess(int32(pid))
|
||||
p, err := NewProcess(pid)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, p)
|
||||
out = append(out, p)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func getProcInfo(pid int32) (*SystemProcessInformation, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user