Skip to content

Commit

Permalink
procstat performance enhancement (influxdata#7686)
Browse files Browse the repository at this point in the history
up to 40/120x better performance on FullPattern/Pattern functions
  • Loading branch information
ssoroka authored and rhajek committed Jul 13, 2020
1 parent eaeee02 commit bbdf82f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
15 changes: 14 additions & 1 deletion plugins/inputs/procstat/native_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (pg *NativeFinder) FullPattern(pattern string) ([]PID, error) {
if err != nil {
return pids, err
}
procs, err := process.Processes()
procs, err := pg.FastProcessList()
if err != nil {
return pids, err
}
Expand All @@ -81,3 +81,16 @@ func (pg *NativeFinder) FullPattern(pattern string) ([]PID, error) {
}
return pids, err
}

func (pg *NativeFinder) FastProcessList() ([]*process.Process, error) {
pids, err := process.Pids()
if err != nil {
return nil, err
}

result := make([]*process.Process, len(pids))
for i, pid := range pids {
result[i] = &process.Process{Pid: pid}
}
return result, nil
}
4 changes: 1 addition & 3 deletions plugins/inputs/procstat/native_finder_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package procstat

import (
"regexp"

"github.com/shirou/gopsutil/process"
)

//Pattern matches on the process name
Expand All @@ -15,7 +13,7 @@ func (pg *NativeFinder) Pattern(pattern string) ([]PID, error) {
if err != nil {
return pids, err
}
procs, err := process.Processes()
procs, err := pg.FastProcessList()
if err != nil {
return pids, err
}
Expand Down
4 changes: 1 addition & 3 deletions plugins/inputs/procstat/native_finder_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package procstat

import (
"regexp"

"github.com/shirou/gopsutil/process"
)

// Pattern matches on the process name
Expand All @@ -13,7 +11,7 @@ func (pg *NativeFinder) Pattern(pattern string) ([]PID, error) {
if err != nil {
return pids, err
}
procs, err := process.Processes()
procs, err := pg.FastProcessList()
if err != nil {
return pids, err
}
Expand Down

0 comments on commit bbdf82f

Please sign in to comment.