2015-05-28 15:21:32 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2015-01-07 17:43:04 -05:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-01-07 19:22:42 -05:00
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-01-07 17:43:04 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-01-07 17:43:04 -05:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-01-19 18:29:42 -05:00
|
|
|
"github.com/docker/docker/pkg/pubsub"
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2015-01-07 17:43:04 -05:00
|
|
|
)
|
|
|
|
|
2015-11-03 14:06:16 -05:00
|
|
|
type statsSupervisor interface {
|
|
|
|
// GetContainerStats collects all the stats related to a container
|
2015-11-12 14:55:17 -05:00
|
|
|
GetContainerStats(container *container.Container) (*execdriver.ResourceStats, error)
|
2015-11-03 14:06:16 -05:00
|
|
|
}
|
|
|
|
|
2015-01-07 21:02:08 -05:00
|
|
|
// newStatsCollector returns a new statsCollector that collections
|
|
|
|
// network and cgroup stats for a registered container at the specified
|
|
|
|
// interval. The collector allows non-running containers to be added
|
|
|
|
// and will start processing stats when they are started.
|
2015-11-03 14:06:16 -05:00
|
|
|
func (daemon *Daemon) newStatsCollector(interval time.Duration) *statsCollector {
|
2015-01-07 17:43:04 -05:00
|
|
|
s := &statsCollector{
|
2015-07-30 17:01:53 -04:00
|
|
|
interval: interval,
|
2015-11-03 14:06:16 -05:00
|
|
|
supervisor: daemon,
|
2015-11-12 14:55:17 -05:00
|
|
|
publishers: make(map[*container.Container]*pubsub.Publisher),
|
2015-07-30 17:01:53 -04:00
|
|
|
clockTicksPerSecond: uint64(system.GetClockTicks()),
|
|
|
|
bufReader: bufio.NewReaderSize(nil, 128),
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
2015-01-19 18:29:42 -05:00
|
|
|
go s.run()
|
2015-01-07 17:43:04 -05:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// statsCollector manages and provides container resource stats
|
|
|
|
type statsCollector struct {
|
2015-07-30 17:01:53 -04:00
|
|
|
m sync.Mutex
|
2015-11-03 14:06:16 -05:00
|
|
|
supervisor statsSupervisor
|
2015-07-30 17:01:53 -04:00
|
|
|
interval time.Duration
|
|
|
|
clockTicksPerSecond uint64
|
2015-11-12 14:55:17 -05:00
|
|
|
publishers map[*container.Container]*pubsub.Publisher
|
2015-07-30 17:01:53 -04:00
|
|
|
bufReader *bufio.Reader
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
|
|
|
|
2015-01-07 21:02:08 -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.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *statsCollector) collect(c *container.Container) chan interface{} {
|
2015-01-07 17:43:04 -05:00
|
|
|
s.m.Lock()
|
2015-01-07 21:02:08 -05:00
|
|
|
defer s.m.Unlock()
|
2015-01-19 18:29:42 -05:00
|
|
|
publisher, exists := s.publishers[c]
|
|
|
|
if !exists {
|
|
|
|
publisher = pubsub.NewPublisher(100*time.Millisecond, 1024)
|
|
|
|
s.publishers[c] = publisher
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|
2015-01-19 18:29:42 -05:00
|
|
|
return publisher.Subscribe()
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
|
|
|
|
2015-01-07 21:02:08 -05:00
|
|
|
// stopCollection closes the channels for all subscribers and removes
|
|
|
|
// the container from metrics collection.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *statsCollector) stopCollection(c *container.Container) {
|
2015-01-07 17:43:04 -05:00
|
|
|
s.m.Lock()
|
2015-01-19 18:29:42 -05:00
|
|
|
if publisher, exists := s.publishers[c]; exists {
|
|
|
|
publisher.Close()
|
|
|
|
delete(s.publishers, c)
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|
2015-01-19 18:29:42 -05:00
|
|
|
s.m.Unlock()
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|
|
|
|
|
2015-01-19 18:29:42 -05:00
|
|
|
// unsubscribe removes a specific subscriber from receiving updates for a container's stats.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *statsCollector) unsubscribe(c *container.Container, ch chan interface{}) {
|
2015-01-07 21:02:08 -05:00
|
|
|
s.m.Lock()
|
2015-01-19 18:29:42 -05:00
|
|
|
publisher := s.publishers[c]
|
|
|
|
if publisher != nil {
|
|
|
|
publisher.Evict(ch)
|
2015-01-20 14:37:50 -05:00
|
|
|
if publisher.Len() == 0 {
|
|
|
|
delete(s.publishers, c)
|
|
|
|
}
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|
2015-01-07 17:43:04 -05:00
|
|
|
s.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:29:42 -05:00
|
|
|
func (s *statsCollector) run() {
|
2015-04-23 17:39:05 -04:00
|
|
|
type publishersPair struct {
|
2015-11-12 14:55:17 -05:00
|
|
|
container *container.Container
|
2015-04-23 17:39:05 -04:00
|
|
|
publisher *pubsub.Publisher
|
|
|
|
}
|
|
|
|
// we cannot determine the capacity here.
|
|
|
|
// it will grow enough in first iteration
|
|
|
|
var pairs []publishersPair
|
|
|
|
|
2015-04-20 04:08:01 -04:00
|
|
|
for range time.Tick(s.interval) {
|
2015-04-23 17:39:05 -04:00
|
|
|
// it does not make sense in the first iteration,
|
|
|
|
// but saves allocations in further iterations
|
|
|
|
pairs = pairs[:0]
|
|
|
|
|
|
|
|
s.m.Lock()
|
2015-01-19 18:29:42 -05:00
|
|
|
for container, publisher := range s.publishers {
|
2015-04-23 17:39:05 -04:00
|
|
|
// copy pointers here to release the lock ASAP
|
|
|
|
pairs = append(pairs, publishersPair{container, publisher})
|
|
|
|
}
|
|
|
|
s.m.Unlock()
|
2015-09-08 14:57:22 -04:00
|
|
|
if len(pairs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
systemUsage, err := s.getSystemCPUUsage()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("collecting system cpu usage: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-04-23 17:39:05 -04:00
|
|
|
|
|
|
|
for _, pair := range pairs {
|
2015-11-03 14:06:16 -05:00
|
|
|
stats, err := s.supervisor.GetContainerStats(pair.container)
|
2015-01-19 18:29:42 -05:00
|
|
|
if err != nil {
|
|
|
|
if err != execdriver.ErrNotRunning {
|
2015-04-23 17:39:05 -04:00
|
|
|
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
2015-01-19 18:29:42 -05:00
|
|
|
continue
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
2015-01-19 18:29:42 -05:00
|
|
|
stats.SystemUsage = systemUsage
|
2015-11-02 21:06:44 -05:00
|
|
|
|
2015-04-23 17:39:05 -04:00
|
|
|
pair.publisher.Publish(stats)
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
2015-01-19 18:29:42 -05:00
|
|
|
}
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|
2015-01-07 19:22:42 -05:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
const nanoSecondsPerSecond = 1e9
|
2015-01-19 17:07:21 -05:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// getSystemCPUUsage returns the host system's cpu usage in
|
|
|
|
// nanoseconds. An error is returned if the format of the underlying
|
|
|
|
// file does not match.
|
|
|
|
//
|
|
|
|
// Uses /proc/stat defined by POSIX. Looks for the cpu
|
|
|
|
// statistics line and then sums up the first seven fields
|
|
|
|
// provided. See `man 5 proc` for details on specific field
|
|
|
|
// information.
|
|
|
|
func (s *statsCollector) getSystemCPUUsage() (uint64, error) {
|
2015-05-15 05:22:50 -04:00
|
|
|
var line string
|
2015-01-07 19:22:42 -05:00
|
|
|
f, err := os.Open("/proc/stat")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2015-05-15 05:22:50 -04:00
|
|
|
defer func() {
|
|
|
|
s.bufReader.Reset(nil)
|
|
|
|
f.Close()
|
|
|
|
}()
|
|
|
|
s.bufReader.Reset(f)
|
|
|
|
err = nil
|
|
|
|
for err == nil {
|
|
|
|
line, err = s.bufReader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parts := strings.Fields(line)
|
2015-01-07 19:22:42 -05:00
|
|
|
switch parts[0] {
|
|
|
|
case "cpu":
|
|
|
|
if len(parts) < 8 {
|
2015-09-16 14:56:26 -04:00
|
|
|
return 0, derr.ErrorCodeBadCPUFields
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
var totalClockTicks uint64
|
2015-01-07 19:22:42 -05:00
|
|
|
for _, i := range parts[1:8] {
|
|
|
|
v, err := strconv.ParseUint(i, 10, 64)
|
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return 0, derr.ErrorCodeBadCPUInt.WithArgs(i, err)
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
totalClockTicks += v
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
return (totalClockTicks * nanoSecondsPerSecond) /
|
|
|
|
s.clockTicksPerSecond, nil
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-16 14:56:26 -04:00
|
|
|
return 0, derr.ErrorCodeBadStatFormat
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|