mirror of https://github.com/k3s-io/k3s
Merge pull request #66367 from cpuguy83/speedup_pidof
getPids - don't recursively traverse every dir in /procpull/58/head
commit
4c874dbefe
|
@ -21,6 +21,7 @@ package procfs
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -105,47 +106,60 @@ func PidOf(name string) ([]int, error) {
|
||||||
|
|
||||||
func getPids(re *regexp.Regexp) []int {
|
func getPids(re *regexp.Regexp) []int {
|
||||||
pids := []int{}
|
pids := []int{}
|
||||||
filepath.Walk("/proc", func(path string, info os.FileInfo, err error) error {
|
|
||||||
|
dirFD, err := os.Open("/proc")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// We should continue processing other directories/files
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
base := filepath.Base(path)
|
defer dirFD.Close()
|
||||||
// Traverse only the directories we are interested in
|
|
||||||
if info.IsDir() && path != "/proc" {
|
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 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range ls {
|
||||||
|
if !entry.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// If the directory is not a number (i.e. not a PID), skip it
|
// If the directory is not a number (i.e. not a PID), skip it
|
||||||
if _, err := strconv.Atoi(base); err != nil {
|
pid, err := strconv.Atoi(entry.Name())
|
||||||
return filepath.SkipDir
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if base != "cmdline" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
cmdline, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.V(4).Infof("Error reading file %s: %+v", path, err)
|
continue
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// The bytes we read have '\0' as a separator for the command line
|
||||||
parts := bytes.SplitN(cmdline, []byte{0}, 2)
|
parts := bytes.SplitN(cmdline, []byte{0}, 2)
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
// Split the command line itself we are interested in just the first part
|
// Split the command line itself we are interested in just the first part
|
||||||
exe := strings.FieldsFunc(string(parts[0]), func(c rune) bool {
|
exe := strings.FieldsFunc(string(parts[0]), func(c rune) bool {
|
||||||
return unicode.IsSpace(c) || c == ':'
|
return unicode.IsSpace(c) || c == ':'
|
||||||
})
|
})
|
||||||
if len(exe) == 0 {
|
if len(exe) == 0 {
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
// Check if the name of the executable is what we are looking for
|
// Check if the name of the executable is what we are looking for
|
||||||
if re.MatchString(exe[0]) {
|
if re.MatchString(exe[0]) {
|
||||||
dirname := filepath.Base(filepath.Dir(path))
|
|
||||||
// Grab the PID from the directory path
|
// Grab the PID from the directory path
|
||||||
pid, _ := strconv.Atoi(dirname)
|
|
||||||
pids = append(pids, pid)
|
pids = append(pids, pid)
|
||||||
}
|
}
|
||||||
return nil
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
return pids
|
return pids
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -95,3 +96,21 @@ func TestPKill(t *testing.T) {
|
||||||
t.Fatalf("timeout waiting for %v", sig)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue