2015-01-07 17:43:04 -05:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-01-07 19:22:42 -05:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"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-01-07 17:43:04 -05:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-01-19 18:29:42 -05:00
|
|
|
"github.com/docker/docker/pkg/pubsub"
|
2015-01-19 17:07:21 -05:00
|
|
|
"github.com/docker/libcontainer/system"
|
2015-01-07 17:43:04 -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-01-07 17:43:04 -05:00
|
|
|
func newStatsCollector(interval time.Duration) *statsCollector {
|
|
|
|
s := &statsCollector{
|
|
|
|
interval: interval,
|
2015-01-19 18:29:42 -05:00
|
|
|
publishers: make(map[*Container]*pubsub.Publisher),
|
2015-01-19 17:07:21 -05:00
|
|
|
clockTicks: uint64(system.GetClockTicks()),
|
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 {
|
|
|
|
m sync.Mutex
|
|
|
|
interval time.Duration
|
2015-01-19 17:07:21 -05:00
|
|
|
clockTicks uint64
|
2015-01-19 18:29:42 -05:00
|
|
|
publishers map[*Container]*pubsub.Publisher
|
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-01-19 18:29:42 -05:00
|
|
|
func (s *statsCollector) collect(c *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-01-07 17:43:04 -05:00
|
|
|
func (s *statsCollector) stopCollection(c *Container) {
|
|
|
|
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.
|
|
|
|
func (s *statsCollector) unsubscribe(c *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() {
|
|
|
|
for _ = range time.Tick(s.interval) {
|
|
|
|
for container, publisher := range s.publishers {
|
|
|
|
systemUsage, err := s.getSystemCpuUsage()
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("collecting system cpu usage for %s: %v", container.ID, err)
|
2015-01-19 18:29:42 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stats, err := container.Stats()
|
|
|
|
if err != nil {
|
|
|
|
if err != execdriver.ErrNotRunning {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("collecting stats for %s: %v", 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
|
|
|
|
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-01-19 17:07:21 -05:00
|
|
|
const nanoSeconds = 1e9
|
|
|
|
|
2015-01-20 21:13:47 -05:00
|
|
|
// getSystemCpuUSage returns the host system's cpu usage in nanoseconds
|
2015-01-19 17:07:21 -05:00
|
|
|
// for the system to match the cgroup readings are returned in the same format.
|
2015-01-07 21:02:08 -05:00
|
|
|
func (s *statsCollector) getSystemCpuUsage() (uint64, error) {
|
2015-01-07 19:22:42 -05:00
|
|
|
f, err := os.Open("/proc/stat")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
for sc.Scan() {
|
|
|
|
parts := strings.Fields(sc.Text())
|
|
|
|
switch parts[0] {
|
|
|
|
case "cpu":
|
|
|
|
if len(parts) < 8 {
|
|
|
|
return 0, fmt.Errorf("invalid number of cpu fields")
|
|
|
|
}
|
2015-01-19 17:07:21 -05:00
|
|
|
var sum 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-01-19 17:07:21 -05:00
|
|
|
return 0, fmt.Errorf("Unable to convert value %s to int: %s", i, err)
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|
2015-01-19 17:07:21 -05:00
|
|
|
sum += v
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|
2015-01-19 17:07:21 -05:00
|
|
|
return (sum * nanoSeconds) / s.clockTicks, nil
|
2015-01-07 19:22:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("invalid stat format")
|
|
|
|
}
|