Skip to content

Commit e2717d8

Browse files
committed
Avoid collecting network stats for non root cgroups in raw handler.
Signed-off-by: Vishnu kannan <vishnuk@google.com>
1 parent f149fcd commit e2717d8

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

cadvisor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ var enableProfiling = flag.Bool("profiling", false, "Enable profiling via web in
5656

5757
var (
5858
// Metrics to be ignored.
59-
ignoreMetrics metricSetValue = metricSetValue{container.MetricSet{}}
59+
// Tcp metrics are ignored by default.
60+
ignoreMetrics metricSetValue = metricSetValue{container.MetricSet{container.NetworkTcpUsageMetrics: struct{}{}}}
6061

6162
// List of metrics that can be ignored.
6263
ignoreWhitelist = container.MetricSet{
@@ -87,8 +88,6 @@ func (ml *metricSetValue) Set(value string) error {
8788

8889
func init() {
8990
flag.Var(&ignoreMetrics, "disable_metrics", "comma-separated list of metrics to be disabled. Options are `disk`, `network`, `tcp`. Note: tcp is disabled by default due to high CPU usage.")
90-
// Tcp metrics are ignored by default.
91-
flag.Set("disable_metrics", "tcp")
9291
}
9392

9493
func main() {

container/libcontainer/helpers.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,30 @@ func GetStats(cgroupManager cgroups.Manager, rootFs string, pid int, ignoreMetri
9292
stats := toContainerStats(libcontainerStats)
9393

9494
// If we know the pid then get network stats from /proc/<pid>/net/dev
95-
if pid > 0 {
96-
if !ignoreMetrics.Has(container.NetworkUsageMetrics) {
97-
netStats, err := networkStatsFromProc(rootFs, pid)
98-
if err != nil {
99-
glog.V(2).Infof("Unable to get network stats from pid %d: %v", pid, err)
100-
} else {
101-
stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
102-
}
95+
if pid == 0 {
96+
return stats, nil
97+
}
98+
if !ignoreMetrics.Has(container.NetworkUsageMetrics) {
99+
netStats, err := networkStatsFromProc(rootFs, pid)
100+
if err != nil {
101+
glog.V(2).Infof("Unable to get network stats from pid %d: %v", pid, err)
102+
} else {
103+
stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
104+
}
105+
}
106+
if !ignoreMetrics.Has(container.NetworkTcpUsageMetrics) {
107+
t, err := tcpStatsFromProc(rootFs, pid, "net/tcp")
108+
if err != nil {
109+
glog.V(2).Infof("Unable to get tcp stats from pid %d: %v", pid, err)
110+
} else {
111+
stats.Network.Tcp = t
103112
}
104-
if !ignoreMetrics.Has(container.NetworkTcpUsageMetrics) {
105-
t, err := tcpStatsFromProc(rootFs, pid, "net/tcp")
106-
if err != nil {
107-
glog.V(2).Infof("Unable to get tcp stats from pid %d: %v", pid, err)
108-
} else {
109-
stats.Network.Tcp = t
110-
}
111113

112-
t6, err := tcpStatsFromProc(rootFs, pid, "net/tcp6")
113-
if err != nil {
114-
glog.V(2).Infof("Unable to get tcp6 stats from pid %d: %v", pid, err)
115-
} else {
116-
stats.Network.Tcp6 = t6
117-
}
114+
t6, err := tcpStatsFromProc(rootFs, pid, "net/tcp6")
115+
if err != nil {
116+
glog.V(2).Infof("Unable to get tcp6 stats from pid %d: %v", pid, err)
117+
} else {
118+
stats.Network.Tcp6 = t6
118119
}
119120
}
120121

container/raw/handler.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package raw
1818
import (
1919
"fmt"
2020
"io/ioutil"
21-
"os"
2221
"path"
2322
"strings"
2423

@@ -62,6 +61,8 @@ type rawContainerHandler struct {
6261

6362
// Metrics to be ignored.
6463
ignoreMetrics container.MetricSet
64+
65+
pid int
6566
}
6667

6768
func (self *rawContainerHandler) GetCgroupPaths() map[string]string {
@@ -88,6 +89,10 @@ func (self *rawContainerHandler) HasFilesystem() bool {
8889
return false
8990
}
9091

92+
func isRootCgroup(name string) bool {
93+
return name == "/"
94+
}
95+
9196
func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, watcher *common.InotifyWatcher, rootFs string, ignoreMetrics container.MetricSet) (container.ContainerHandler, error) {
9297
cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems.MountPoints, name)
9398

@@ -112,6 +117,11 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu
112117
}
113118
}
114119

120+
pid := 0
121+
if isRootCgroup(name) {
122+
pid = 1
123+
}
124+
115125
return &rawContainerHandler{
116126
name: name,
117127
cgroupSubsystems: cgroupSubsystems,
@@ -124,6 +134,7 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu
124134
watcher: watcher,
125135
rootFs: rootFs,
126136
ignoreMetrics: ignoreMetrics,
137+
pid: pid,
127138
}, nil
128139
}
129140

@@ -136,7 +147,7 @@ func (self *rawContainerHandler) ContainerReference() (info.ContainerReference,
136147

137148
func (self *rawContainerHandler) GetRootNetworkDevices() ([]info.NetInfo, error) {
138149
nd := []info.NetInfo{}
139-
if self.name == "/" {
150+
if isRootCgroup(self.name) {
140151
mi, err := self.machineInfoFactory.GetMachineInfo()
141152
if err != nil {
142153
return nd, err
@@ -158,7 +169,7 @@ func (self *rawContainerHandler) GetSpec() (info.ContainerSpec, error) {
158169

159170
func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error {
160171
// Get Filesystem information only for the root cgroup.
161-
if self.name == "/" {
172+
if isRootCgroup(self.name) {
162173
filesystems, err := self.fsInfo.GetGlobalFsInfo()
163174
if err != nil {
164175
return err
@@ -221,7 +232,7 @@ func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error {
221232
}
222233

223234
func (self *rawContainerHandler) GetStats() (*info.ContainerStats, error) {
224-
stats, err := libcontainer.GetStats(self.cgroupManager, self.rootFs, os.Getpid(), self.ignoreMetrics)
235+
stats, err := libcontainer.GetStats(self.cgroupManager, self.rootFs, self.pid, self.ignoreMetrics)
225236
if err != nil {
226237
return stats, err
227238
}

0 commit comments

Comments
 (0)