Skip to content

Commit

Permalink
Merge pull request shirou#948 from AtakanColak/windows-cache-ppid
Browse files Browse the repository at this point in the history
Cache Process Parent ID in Windows
  • Loading branch information
shirou authored Oct 11, 2020
2 parents 42136c7 + 7cc8d3f commit fe68b86
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,19 @@ func (p *Process) Ppid() (int32, error) {
}

func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
// if cached already, return from cache
if p.parent != 0 {
return p.parent, nil
}

ppid, _, _, err := getFromSnapProcess(p.Pid)
if err != nil {
return 0, err
}

// if no errors, cache it
p.parent = ppid

return ppid, nil
}

Expand All @@ -252,10 +261,14 @@ func (p *Process) Name() (string, error) {
}

func (p *Process) NameWithContext(ctx context.Context) (string, error) {
_, _, name, err := getFromSnapProcess(p.Pid)
ppid, _, name, err := getFromSnapProcess(p.Pid)
if err != nil {
return "", fmt.Errorf("could not get Name: %s", err)
}

// if no errors, cache ppid
p.parent = ppid

return name, nil
}

Expand Down Expand Up @@ -524,10 +537,14 @@ func (p *Process) NumThreads() (int32, error) {
}

func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
_, ret, _, err := getFromSnapProcess(p.Pid)
ppid, ret, _, err := getFromSnapProcess(p.Pid)
if err != nil {
return 0, err
}

// if no errors, cache ppid
p.parent = ppid

return ret, nil
}
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
Expand Down

0 comments on commit fe68b86

Please sign in to comment.