Skip to content

Commit

Permalink
Merge pull request #66367 from cpuguy83/speedup_pidof
Browse files Browse the repository at this point in the history
getPids - don't recursively traverse every dir in /proc
  • Loading branch information
k8s-ci-robot committed Oct 29, 2018
2 parents 8c6fbd7 + 01034af commit 4c874db
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
86 changes: 50 additions & 36 deletions pkg/util/procfs/procfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package procfs
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -105,47 +106,60 @@ func PidOf(name string) ([]int, error) {

func getPids(re *regexp.Regexp) []int {
pids := []int{}
filepath.Walk("/proc", func(path string, info os.FileInfo, err error) error {

dirFD, err := os.Open("/proc")
if err != nil {
return nil
}
defer dirFD.Close()

for {
// Read a small number at a time in case there are many entries, we don't want to
// allocate a lot here.
ls, err := dirFD.Readdir(10)
if err == io.EOF {
break
}
if err != nil {
// We should continue processing other directories/files
return nil
}
base := filepath.Base(path)
// Traverse only the directories we are interested in
if info.IsDir() && path != "/proc" {

for _, entry := range ls {
if !entry.IsDir() {
continue
}

// If the directory is not a number (i.e. not a PID), skip it
if _, err := strconv.Atoi(base); err != nil {
return filepath.SkipDir
pid, err := strconv.Atoi(entry.Name())
if err != nil {
continue
}

cmdline, err := ioutil.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline"))
if err != nil {
glog.V(4).Infof("Error reading file %s: %+v", filepath.Join("/proc", entry.Name(), "cmdline"), err)
continue
}

// The bytes we read have '\0' as a separator for the command line
parts := bytes.SplitN(cmdline, []byte{0}, 2)
if len(parts) == 0 {
continue
}
// Split the command line itself we are interested in just the first part
exe := strings.FieldsFunc(string(parts[0]), func(c rune) bool {
return unicode.IsSpace(c) || c == ':'
})
if len(exe) == 0 {
continue
}
// Check if the name of the executable is what we are looking for
if re.MatchString(exe[0]) {
// Grab the PID from the directory path
pids = append(pids, pid)
}
}
if base != "cmdline" {
return nil
}
cmdline, err := ioutil.ReadFile(path)
if err != nil {
glog.V(4).Infof("Error reading file %s: %+v", path, err)
return nil
}
// The bytes we read have '\0' as a separator for the command line
parts := bytes.SplitN(cmdline, []byte{0}, 2)
if len(parts) == 0 {
return nil
}
// Split the command line itself we are interested in just the first part
exe := strings.FieldsFunc(string(parts[0]), func(c rune) bool {
return unicode.IsSpace(c) || c == ':'
})
if len(exe) == 0 {
return nil
}
// Check if the name of the executable is what we are looking for
if re.MatchString(exe[0]) {
dirname := filepath.Base(filepath.Dir(path))
// Grab the PID from the directory path
pid, _ := strconv.Atoi(dirname)
pids = append(pids, pid)
}
return nil
})
}

return pids
}
19 changes: 19 additions & 0 deletions pkg/util/procfs/procfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"syscall"
"testing"
Expand Down Expand Up @@ -95,3 +96,21 @@ func TestPKill(t *testing.T) {
t.Fatalf("timeout waiting for %v", sig)
}
}

func BenchmarkGetPids(b *testing.B) {
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
b.Skipf("not supported on GOOS=%s", runtime.GOOS)
}

re, err := regexp.Compile("(^|/)" + filepath.Base(os.Args[0]) + "$")
assert.Empty(b, err)

for i := 0; i < b.N; i++ {
pids := getPids(re)

b.StopTimer()
assert.NotZero(b, pids)
assert.Contains(b, pids, os.Getpid())
b.StartTimer()
}
}

0 comments on commit 4c874db

Please sign in to comment.