Skip to content
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

[process][windows] Use win32 API in process.Children() instead of slow WMI call #609

Merged
merged 1 commit into from
Nov 24, 2018

Conversation

Lomanic
Copy link
Collaborator

@Lomanic Lomanic commented Nov 18, 2018

The CreateToolhelp32Snapshot+Process32First+Process32Next combo already
iterates over all processes, so it would be inefficient to enumerate all
processes with process.Processes() and then calling p.Ppid() on each of
them: we just use this combo to get all processes and their ppid in a
single iteration.

This is faster by a factor of 25 compared to the previous WMI call.

Benchmark program:

package main

import (
	"fmt"
	"github.com/shirou/gopsutil/process"
	"os"
	"time"
)

func main() {
	start := time.Now()
	procs, err := process.Processes()
	if err != nil {
		fmt.Println("1", err)
		os.Exit(1)
	}
	for _, p := range procs {
		children, err := p.Children()
		if err != nil {
			fmt.Println("2", err)
			os.Exit(1)
		}
		fmt.Println(p.Pid, children)
	}
	elapsed := time.Now().Sub(start)
	fmt.Println("Elapsed time", elapsed, "(", elapsed/(time.Duration(len(procs))), "per process)")
}

Results:

  • before patch (wmi): Elapsed time 6.5693758s ( 56.148511ms per process)
  • after patch (win32): Elapsed time 251.0143ms ( 2.127239ms per process)

…w WMI call

The CreateToolhelp32Snapshot+Process32First+Process32Next combo already
iterates over all processes, so it would be inefficient to enumerate all
processes with process.Processes() and then calling p.Ppid() on each of
them: we just use this combo to get all processes and their ppid in a
single iteration.

This is faster by a factor of 25 compared to the previous WMI call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant