This commit is contained in:
Alfonso Acosta
2016-02-05 15:01:09 +00:00
parent 87dd43f782
commit 6deeca0380
2 changed files with 11 additions and 21 deletions

View File

@@ -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)

View File

@@ -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
}
}