-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
perf(process): build snapshot map once on Windows to fix O(N²) process listing #2062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -321,6 +321,9 @@ func (p *Process) PpidWithContext(_ context.Context) (int32, error) { | |||||
| } | ||||||
|
|
||||||
| func (p *Process) NameWithContext(ctx context.Context) (string, error) { | ||||||
| if p.name != "" { | ||||||
| return p.name, nil | ||||||
| } | ||||||
| if p.Pid == 0 { | ||||||
| return "System Idle Process", nil | ||||||
| } | ||||||
|
|
@@ -587,6 +590,9 @@ func (p *Process) NumFDsWithContext(_ context.Context) (int32, error) { | |||||
| } | ||||||
|
|
||||||
| func (p *Process) NumThreadsWithContext(_ context.Context) (int32, error) { | ||||||
| if p.numThreads != 0 { | ||||||
| return p.numThreads, nil | ||||||
| } | ||||||
| ppid, ret, _, err := getFromSnapProcess(p.Pid) | ||||||
| if err != nil { | ||||||
| return 0, err | ||||||
|
|
@@ -598,6 +604,8 @@ func (p *Process) NumThreadsWithContext(_ context.Context) (int32, error) { | |||||
| p.setPpid(ppid) | ||||||
| } | ||||||
|
|
||||||
| p.numThreads = ret | ||||||
|
|
||||||
| return ret, nil | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -900,6 +908,29 @@ func getFromSnapProcess(pid int32) (int32, int32, string, error) { //nolint:unpa | |||||
| return 0, 0, "", fmt.Errorf("couldn't find pid: %d", pid) | ||||||
| } | ||||||
|
|
||||||
| func buildSnapProcessMap() (map[uint32]windows.ProcessEntry32, error) { | ||||||
| snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
| defer windows.CloseHandle(snap) | ||||||
|
|
||||||
| var pe32 windows.ProcessEntry32 | ||||||
| pe32.Size = uint32(unsafe.Sizeof(pe32)) | ||||||
| if err = windows.Process32First(snap, &pe32); err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| snapMap := make(map[uint32]windows.ProcessEntry32) | ||||||
| for { | ||||||
| snapMap[pe32.ProcessID] = pe32 | ||||||
| if err = windows.Process32Next(snap, &pe32); err != nil { | ||||||
| break | ||||||
| } | ||||||
| } | ||||||
| return snapMap, nil | ||||||
| } | ||||||
|
|
||||||
| func ProcessesWithContext(ctx context.Context) ([]*Process, error) { | ||||||
| out := []*Process{} | ||||||
|
|
||||||
|
|
@@ -908,11 +939,23 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) { | |||||
| return out, fmt.Errorf("could not get Processes %w", err) | ||||||
| } | ||||||
|
|
||||||
| snapMap, err := buildSnapProcessMap() | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
|
||||||
| return nil, err | |
| return out, fmt.Errorf("could not build process snapshot %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buildSnapProcessMapbreaks out of the loop on anyProcess32Nexterror and returns the map, which can silently return an incomplete snapshot ifProcess32Nextfails for reasons other than "no more files". Please explicitly treatwindows.ERROR_NO_MORE_FILES(or the appropriate sentinel) as the normal termination condition, and return the error for any other failure.