From c9c40215f5b9472b371daa2b740cbecf28dbc0e2 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Tue, 29 Sep 2020 23:56:25 +0200 Subject: [PATCH 1/3] [process][darwin] Fix #925 properly with unix.SysctlRaw("kern.proc.pid", PID) --- process/process_darwin.go | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 1a0ccfb12..3bc229554 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -12,7 +12,6 @@ import ( "strconv" "strings" "time" - "unsafe" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/internal/common" @@ -602,24 +601,9 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) { // Returns a proc as defined here: // http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html func (p *Process) getKProc() (*KinfoProc, error) { - return p.getKProcWithContext(context.Background()) -} - -func (p *Process) getKProcWithContext(ctx context.Context) (*KinfoProc, error) { - mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid} - procK := KinfoProc{} - length := uint64(unsafe.Sizeof(procK)) - buf := make([]byte, length) - _, _, syserr := unix.Syscall6( - 202, // unix.SYS___SYSCTL https://github.com/golang/sys/blob/76b94024e4b621e672466e8db3d7f084e7ddcad2/unix/zsysnum_darwin_amd64.go#L146 - uintptr(unsafe.Pointer(&mib[0])), - uintptr(len(mib)), - uintptr(unsafe.Pointer(&buf[0])), - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if syserr != 0 { - return nil, syserr + buf, err := unix.SysctlRaw("kern.proc.pid", int(p.Pid)) + if err != nil { + return nil, err } k, err := parseKinfoProc(buf) if err != nil { From 64ba9d03cd1781ac8b6ad04a40df47e57cf69ab0 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Tue, 29 Sep 2020 23:58:34 +0200 Subject: [PATCH 2/3] [process] Properly test Ppid() against known value --- process/process_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/process/process_test.go b/process/process_test.go index e7a3b2bf3..e18a26d55 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -181,6 +181,10 @@ func Test_Process_Ppid(t *testing.T) { if v == 0 { t.Errorf("return value is 0 %v", v) } + expected := os.Getppid() + if v != int32(expected) { + t.Errorf("return value is %v, expected %v", v, expected) + } } func Test_Process_Status(t *testing.T) { From eaa34817d0a04e90321b166a0842186e6704b987 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Wed, 30 Sep 2020 00:00:10 +0200 Subject: [PATCH 3/3] [process] Add benchmarks for New(), Name() Ppid() run only them with: go test -bench=. -run=xxx github.com/shirou/gopsutil/process --- process/process_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/process/process_test.go b/process/process_test.go index e18a26d55..28518b8bd 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -662,3 +662,24 @@ func Test_AllProcesses_cmdLine(t *testing.T) { } } } + +func BenchmarkNewProcess(b *testing.B) { + checkPid := os.Getpid() + for i := 0; i < b.N; i++ { + NewProcess(int32(checkPid)) + } +} + +func BenchmarkProcessName(b *testing.B) { + p := testGetProcess() + for i := 0; i < b.N; i++ { + p.Name() + } +} + +func BenchmarkProcessPpid(b *testing.B) { + p := testGetProcess() + for i := 0; i < b.N; i++ { + p.Ppid() + } +}