Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #458 from thaJeztah/18.03-backport-resilient-cpu-s…
Browse files Browse the repository at this point in the history
…ampling

[18.03] daemon/stats: more resilient cpu sampling
  • Loading branch information
andrewhsu committed Apr 12, 2018
2 parents e921cf3 + 912261e commit 5770d86
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions components/engine/daemon/stats/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func (s *Collector) Run() {
// it will grow enough in first iteration
var pairs []publishersPair

for range time.Tick(s.interval) {
for {
// Put sleep at the start so that it will always be hit,
// preventing a tight loop if no stats are collected.
time.Sleep(s.interval)

// it does not make sense in the first iteration,
// but saves allocations in further iterations
pairs = pairs[:0]
Expand All @@ -72,12 +76,6 @@ func (s *Collector) Run() {
continue
}

systemUsage, err := s.getSystemCPUUsage()
if err != nil {
logrus.Errorf("collecting system cpu usage: %v", err)
continue
}

onlineCPUs, err := s.getNumberOnlineCPUs()
if err != nil {
logrus.Errorf("collecting system online cpu count: %v", err)
Expand All @@ -89,6 +87,14 @@ func (s *Collector) Run() {

switch err.(type) {
case nil:
// Sample system CPU usage close to container usage to avoid
// noise in metric calculations.
systemUsage, err := s.getSystemCPUUsage()
if err != nil {
logrus.WithError(err).WithField("container_id", pair.container.ID).Errorf("collecting system cpu usage")
continue
}

// FIXME: move to containerd on Linux (not Windows)
stats.CPUStats.SystemUsage = systemUsage
stats.CPUStats.OnlineCPUs = onlineCPUs
Expand Down

0 comments on commit 5770d86

Please sign in to comment.