2018-02-05 16:05:59 -05:00
|
|
|
package stats // import "github.com/docker/docker/daemon/stats"
|
2017-01-04 12:01:59 -05:00
|
|
|
|
|
|
|
import (
|
2018-03-05 18:59:04 -05:00
|
|
|
"bufio"
|
|
|
|
"sync"
|
2017-01-04 12:01:59 -05:00
|
|
|
"time"
|
|
|
|
|
2016-12-26 04:43:18 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-01-04 12:01:59 -05:00
|
|
|
"github.com/docker/docker/container"
|
|
|
|
"github.com/docker/docker/pkg/pubsub"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-01-04 12:01:59 -05:00
|
|
|
)
|
|
|
|
|
2018-03-05 18:59:04 -05:00
|
|
|
// Collector manages and provides container resource stats
|
|
|
|
type Collector struct {
|
|
|
|
m sync.Mutex
|
|
|
|
supervisor supervisor
|
|
|
|
interval time.Duration
|
|
|
|
publishers map[*container.Container]*pubsub.Publisher
|
|
|
|
bufReader *bufio.Reader
|
|
|
|
|
|
|
|
// The following fields are not set on Windows currently.
|
|
|
|
clockTicksPerSecond uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCollector creates a stats collector that will poll the supervisor with the specified interval
|
|
|
|
func NewCollector(supervisor supervisor, interval time.Duration) *Collector {
|
|
|
|
s := &Collector{
|
|
|
|
interval: interval,
|
|
|
|
supervisor: supervisor,
|
|
|
|
publishers: make(map[*container.Container]*pubsub.Publisher),
|
|
|
|
bufReader: bufio.NewReaderSize(nil, 128),
|
|
|
|
}
|
|
|
|
|
|
|
|
platformNewStatsCollector(s)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type supervisor interface {
|
|
|
|
// GetContainerStats collects all the stats related to a container
|
|
|
|
GetContainerStats(container *container.Container) (*types.StatsJSON, error)
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:01:59 -05:00
|
|
|
// Collect registers the container with the collector and adds it to
|
|
|
|
// the event loop for collection on the specified interval returning
|
|
|
|
// a channel for the subscriber to receive on.
|
|
|
|
func (s *Collector) Collect(c *container.Container) chan interface{} {
|
|
|
|
s.m.Lock()
|
|
|
|
defer s.m.Unlock()
|
|
|
|
publisher, exists := s.publishers[c]
|
|
|
|
if !exists {
|
|
|
|
publisher = pubsub.NewPublisher(100*time.Millisecond, 1024)
|
|
|
|
s.publishers[c] = publisher
|
|
|
|
}
|
|
|
|
return publisher.Subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopCollection closes the channels for all subscribers and removes
|
|
|
|
// the container from metrics collection.
|
|
|
|
func (s *Collector) StopCollection(c *container.Container) {
|
|
|
|
s.m.Lock()
|
|
|
|
if publisher, exists := s.publishers[c]; exists {
|
|
|
|
publisher.Close()
|
|
|
|
delete(s.publishers, c)
|
|
|
|
}
|
|
|
|
s.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe removes a specific subscriber from receiving updates for a container's stats.
|
|
|
|
func (s *Collector) Unsubscribe(c *container.Container, ch chan interface{}) {
|
|
|
|
s.m.Lock()
|
|
|
|
publisher := s.publishers[c]
|
|
|
|
if publisher != nil {
|
|
|
|
publisher.Evict(ch)
|
|
|
|
if publisher.Len() == 0 {
|
|
|
|
delete(s.publishers, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the collectors and will indefinitely collect stats from the supervisor
|
|
|
|
func (s *Collector) Run() {
|
|
|
|
type publishersPair struct {
|
|
|
|
container *container.Container
|
|
|
|
publisher *pubsub.Publisher
|
|
|
|
}
|
|
|
|
// we cannot determine the capacity here.
|
|
|
|
// it will grow enough in first iteration
|
|
|
|
var pairs []publishersPair
|
|
|
|
|
2018-03-07 16:20:21 -05:00
|
|
|
for {
|
2018-03-15 12:56:15 -04:00
|
|
|
// 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)
|
|
|
|
|
2017-01-04 12:01:59 -05:00
|
|
|
// it does not make sense in the first iteration,
|
|
|
|
// but saves allocations in further iterations
|
|
|
|
pairs = pairs[:0]
|
|
|
|
|
|
|
|
s.m.Lock()
|
|
|
|
for container, publisher := range s.publishers {
|
|
|
|
// copy pointers here to release the lock ASAP
|
|
|
|
pairs = append(pairs, publishersPair{container, publisher})
|
|
|
|
}
|
|
|
|
s.m.Unlock()
|
|
|
|
if len(pairs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-06 12:29:09 -05:00
|
|
|
onlineCPUs, err := s.getNumberOnlineCPUs()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("collecting system online cpu count: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:01:59 -05:00
|
|
|
for _, pair := range pairs {
|
|
|
|
stats, err := s.supervisor.GetContainerStats(pair.container)
|
2016-12-26 04:43:18 -05:00
|
|
|
|
2017-07-07 03:33:45 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
2018-03-07 16:20:21 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-07-07 03:33:45 -04:00
|
|
|
// FIXME: move to containerd on Linux (not Windows)
|
|
|
|
stats.CPUStats.SystemUsage = systemUsage
|
|
|
|
stats.CPUStats.OnlineCPUs = onlineCPUs
|
|
|
|
|
|
|
|
pair.publisher.Publish(*stats)
|
|
|
|
|
|
|
|
case notRunningErr, notFoundErr:
|
|
|
|
// publish empty stats containing only name and ID if not running or not found
|
2016-12-26 04:43:18 -05:00
|
|
|
pair.publisher.Publish(types.StatsJSON{
|
|
|
|
Name: pair.container.Name,
|
|
|
|
ID: pair.container.ID,
|
|
|
|
})
|
2017-01-04 12:01:59 -05:00
|
|
|
|
2017-07-07 03:33:45 -04:00
|
|
|
default:
|
|
|
|
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
|
|
|
|
}
|
2017-01-04 12:01:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type notRunningErr interface {
|
|
|
|
error
|
2017-07-19 10:20:13 -04:00
|
|
|
Conflict()
|
2017-01-04 12:01:59 -05:00
|
|
|
}
|
2017-07-07 03:33:45 -04:00
|
|
|
|
|
|
|
type notFoundErr interface {
|
|
|
|
error
|
2017-07-19 10:20:13 -04:00
|
|
|
NotFound()
|
2017-07-07 03:33:45 -04:00
|
|
|
}
|