From ecd50ab3834464746b436caae60b399e00fe1c96 Mon Sep 17 00:00:00 2001 From: Steven Soroka Date: Mon, 15 Jun 2020 17:58:59 -0400 Subject: [PATCH] procstat speed improvement --- plugins/inputs/procstat/native_finder.go | 15 ++++++++++++++- .../inputs/procstat/native_finder_notwindows.go | 4 +--- plugins/inputs/procstat/native_finder_windows.go | 4 +--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/plugins/inputs/procstat/native_finder.go b/plugins/inputs/procstat/native_finder.go index dc1f5b664c482..57d9d81c221ce 100644 --- a/plugins/inputs/procstat/native_finder.go +++ b/plugins/inputs/procstat/native_finder.go @@ -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 } @@ -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 +} diff --git a/plugins/inputs/procstat/native_finder_notwindows.go b/plugins/inputs/procstat/native_finder_notwindows.go index 33275460162d2..9d7409ba1df8e 100644 --- a/plugins/inputs/procstat/native_finder_notwindows.go +++ b/plugins/inputs/procstat/native_finder_notwindows.go @@ -4,8 +4,6 @@ package procstat import ( "regexp" - - "github.com/shirou/gopsutil/process" ) //Pattern matches on the process name @@ -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 } diff --git a/plugins/inputs/procstat/native_finder_windows.go b/plugins/inputs/procstat/native_finder_windows.go index d81623a29fe3c..6dcc0575af258 100644 --- a/plugins/inputs/procstat/native_finder_windows.go +++ b/plugins/inputs/procstat/native_finder_windows.go @@ -2,8 +2,6 @@ package procstat import ( "regexp" - - "github.com/shirou/gopsutil/process" ) // Pattern matches on the process name @@ -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 }