Skip to content

Commit 980cc82

Browse files
committed
[process][windows] Retrieve process name as basename of executable
We align ourself with psutil https://github.com/giampaolo/psutil/blob/8e4099d9f063ceb4ee3da5845562c5b934f83544/psutil/_pswindows.py#L749-L759 Benchmarks show vast improvements go test -run=BenchmarkProcessName -bench=BenchmarkProcessName ./process goos: windows goarch: amd64 pkg: github.com/shirou/gopsutil/v3/process cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz BenchmarkProcessName-4 180 6564033 ns/op BenchmarkProcessNameViaExe-4 22111 51153 ns/op PASS ok github.com/shirou/gopsutil/v3/process 3.914s Fixes #1368
1 parent bd4529a commit 980cc82

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

process/process_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func Test_Process_Name(t *testing.T) {
294294
t.Errorf("getting name error %v", err)
295295
}
296296
if !strings.Contains(n, "process.test") {
297-
t.Errorf("invalid Exe %s", n)
297+
t.Errorf("invalid Name %s", n)
298298
}
299299
}
300300

process/process_windows.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"os"
13+
"path/filepath"
1314
"reflect"
1415
"strings"
1516
"syscall"
@@ -319,18 +320,19 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
319320
}
320321

321322
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
322-
ppid, _, name, err := getFromSnapProcess(p.Pid)
323-
if err != nil {
324-
return "", fmt.Errorf("could not get Name: %s", err)
323+
if p.Pid == 0 {
324+
return "System Idle Process", nil
325+
}
326+
if p.Pid == 4 {
327+
return "System", nil
325328
}
326329

327-
// if no errors and not cached already, cache ppid
328-
p.parent = ppid
329-
if 0 == p.getPpid() {
330-
p.setPpid(ppid)
330+
exe, err := p.ExeWithContext(ctx)
331+
if err != nil {
332+
return "", fmt.Errorf("could not get Name: %s", err)
331333
}
332334

333-
return name, nil
335+
return filepath.Base(exe), nil
334336
}
335337

336338
func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {

0 commit comments

Comments
 (0)