From 6deeca0380ebffb419d4e3dbc3b49d169dd46f2f Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Fri, 5 Feb 2016 15:01:09 +0000 Subject: [PATCH] Cleanup --- .../procspy/background_reader_linux.go | 10 ++++----- probe/endpoint/procspy/proc_linux.go | 22 +++++-------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/probe/endpoint/procspy/background_reader_linux.go b/probe/endpoint/procspy/background_reader_linux.go index 2aea73bbe..3007db945 100644 --- a/probe/endpoint/procspy/background_reader_linux.go +++ b/probe/endpoint/procspy/background_reader_linux.go @@ -16,9 +16,6 @@ const ( maxRateLimit = 250 * time.Millisecond // Read at least 4 * fdBlockSize file descriptors per namespace per second fdBlockSize = 100 targetWalkTime = 10 * time.Second // Aim at walking all files in 10 seconds - - maxRateLimitF = float64(maxRateLimit) - targetWalkTimeF = float64(targetWalkTime) ) type backgroundReader struct { @@ -57,10 +54,13 @@ func StartBackgroundReader(walker process.Walker) { } func (br *backgroundReader) loop() { + const ( + maxRateLimitF = float64(maxRateLimit) + targetWalkTimeF = float64(targetWalkTime) + ) + rateLimit := initialRateLimit - ticker := time.Tick(rateLimit) - for { start := time.Now() sockets, err := walkProcPid(br.walkingBuf, br.walker, ticker, fdBlockSize) diff --git a/probe/endpoint/procspy/proc_linux.go b/probe/endpoint/procspy/proc_linux.go index ec0a9d492..2af9b999d 100644 --- a/probe/endpoint/procspy/proc_linux.go +++ b/probe/endpoint/procspy/proc_linux.go @@ -108,14 +108,9 @@ func readProcessConnections(buf *bytes.Buffer, namespaceProcs []*process.Process // walkNamespacePid does the work of walkProcPid for a single namespace func walkNamespacePid(buf *bytes.Buffer, sockets map[uint64]*Proc, namespaceProcs []*process.Process, ticker <-chan time.Time, fdBlockSize int) error { - found, err := readProcessConnections(buf, namespaceProcs) - if err != nil { + if found, err := readProcessConnections(buf, namespaceProcs); err != nil || !found { return err } - if !found { - // There's no point in reading /fd/* - return nil - } var statT syscall.Stat_t var fdBlockCount int @@ -126,20 +121,15 @@ func walkNamespacePid(buf *bytes.Buffer, sockets map[uint64]*Proc, namespaceProc fdBase := filepath.Join(procRoot, dirName, "fd") if fdBlockCount > fdBlockSize { - // we surpased the filedescriptor rate limit + // we surpassed the filedescriptor rate limit <-ticker fdBlockCount = 0 + // read the connections again to // avoid the race between between /net/tcp{,6} and /proc/PID/fd/* - // FIXME:refactor this - found, err := readProcessConnections(buf, namespaceProcs[i:]) - if err != nil { + if found, err := readProcessConnections(buf, namespaceProcs[i:]); err != nil || !found { return err } - if !found { - // There's no point in reading /fd/* - return nil - } } fds, err := fs.ReadDirNames(fdBase) @@ -150,6 +140,8 @@ func walkNamespacePid(buf *bytes.Buffer, sockets map[uint64]*Proc, namespaceProc var proc *Proc for _, fd := range fds { + fdBlockCount++ + // Direct use of syscall.Stat() to save garbage. err = fs.Stat(filepath.Join(fdBase, fd), &statT) if err != nil { @@ -171,8 +163,6 @@ func walkNamespacePid(buf *bytes.Buffer, sockets map[uint64]*Proc, namespaceProc } sockets[statT.Ino] = proc - - fdBlockCount += 1 } }