Merge pull request #948 from AtakanColak/windows-cache-ppid

Cache Process Parent ID in Windows
This commit is contained in:
shirou 2020-10-11 18:52:27 +09:00 committed by GitHub
commit fe68b86b08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 2 deletions

View File

@ -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
}
@ -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
}
@ -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) {