Skip to content

Commit 4ea21f6

Browse files
committed
persisting mount info instead of reading /proc/<pid>/mountinfo on every scrape
1 parent e5df00a commit 4ea21f6

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

containers/container.go

+26-22
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ type Container struct {
9898

9999
oomKills int
100100

101-
mountIds map[string]struct{}
101+
mounts map[string]proc.MountInfo
102102

103103
nsIPs []netaddr.IP
104104

@@ -136,7 +136,7 @@ func NewContainer(cg *cgroup.Cgroup, md *ContainerMetadata, hostConntrack *Connt
136136
retransmits: map[AddrPair]int64{},
137137
l7Stats: map[ebpftracer.L7Protocol]map[AddrPair]*L7Stats{},
138138

139-
mountIds: map[string]struct{}{},
139+
mounts: map[string]proc.MountInfo{},
140140

141141
logParsers: map[string]*LogParser{},
142142

@@ -360,13 +360,30 @@ func (c *Container) onProcessExit(pid uint32, oomKill bool) {
360360

361361
func (c *Container) onFileOpen(pid uint32, fd uint64) {
362362
mntId, logPath := resolveFd(pid, fd)
363-
c.lock.Lock()
364-
defer c.lock.Unlock()
365-
if mntId != "" {
366-
c.mountIds[mntId] = struct{}{}
367-
}
363+
func() {
364+
if mntId == "" {
365+
return
366+
}
367+
c.lock.Lock()
368+
_, ok := c.mounts[mntId]
369+
c.lock.Unlock()
370+
if ok {
371+
return
372+
}
373+
byMountId := proc.GetMountInfo(pid)
374+
if byMountId == nil {
375+
return
376+
}
377+
if mi, ok := byMountId[mntId]; ok {
378+
c.lock.Lock()
379+
c.mounts[mntId] = mi
380+
c.lock.Unlock()
381+
}
382+
}()
368383
if logPath != "" {
384+
c.lock.Lock()
369385
c.runLogParser(logPath)
386+
c.lock.Unlock()
370387
}
371388
}
372389

@@ -571,24 +588,11 @@ func (c *Container) updateDelays() {
571588
}
572589

573590
func (c *Container) getMounts() map[string]map[string]*proc.FSStat {
574-
mounts := map[string]proc.MountInfo{}
575-
for p := range c.pids {
576-
mi := proc.GetMountInfo(p)
577-
if mi != nil {
578-
mounts = mi
579-
break
580-
}
581-
}
582-
for mountId := range mounts {
583-
if _, ok := c.mountIds[mountId]; !ok {
584-
delete(mounts, mountId)
585-
}
586-
}
587-
if len(mounts) == 0 {
591+
if len(c.mounts) == 0 {
588592
return nil
589593
}
590594
res := map[string]map[string]*proc.FSStat{}
591-
for _, mi := range mounts {
595+
for _, mi := range c.mounts {
592596
var stat *proc.FSStat
593597
for pid := range c.pids {
594598
s, err := proc.StatFS(proc.Path(pid, "root", mi.MountPoint))

0 commit comments

Comments
 (0)