|
| 1 | +// +build !solaris |
| 2 | + |
| 3 | +package stats |
| 4 | + |
| 5 | +import ( |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/Sirupsen/logrus" |
| 9 | + "github.com/docker/docker/container" |
| 10 | + "github.com/docker/docker/pkg/pubsub" |
| 11 | +) |
| 12 | + |
| 13 | +// Collect registers the container with the collector and adds it to |
| 14 | +// the event loop for collection on the specified interval returning |
| 15 | +// a channel for the subscriber to receive on. |
| 16 | +func (s *Collector) Collect(c *container.Container) chan interface{} { |
| 17 | + s.m.Lock() |
| 18 | + defer s.m.Unlock() |
| 19 | + publisher, exists := s.publishers[c] |
| 20 | + if !exists { |
| 21 | + publisher = pubsub.NewPublisher(100*time.Millisecond, 1024) |
| 22 | + s.publishers[c] = publisher |
| 23 | + } |
| 24 | + return publisher.Subscribe() |
| 25 | +} |
| 26 | + |
| 27 | +// StopCollection closes the channels for all subscribers and removes |
| 28 | +// the container from metrics collection. |
| 29 | +func (s *Collector) StopCollection(c *container.Container) { |
| 30 | + s.m.Lock() |
| 31 | + if publisher, exists := s.publishers[c]; exists { |
| 32 | + publisher.Close() |
| 33 | + delete(s.publishers, c) |
| 34 | + } |
| 35 | + s.m.Unlock() |
| 36 | +} |
| 37 | + |
| 38 | +// Unsubscribe removes a specific subscriber from receiving updates for a container's stats. |
| 39 | +func (s *Collector) Unsubscribe(c *container.Container, ch chan interface{}) { |
| 40 | + s.m.Lock() |
| 41 | + publisher := s.publishers[c] |
| 42 | + if publisher != nil { |
| 43 | + publisher.Evict(ch) |
| 44 | + if publisher.Len() == 0 { |
| 45 | + delete(s.publishers, c) |
| 46 | + } |
| 47 | + } |
| 48 | + s.m.Unlock() |
| 49 | +} |
| 50 | + |
| 51 | +// Run starts the collectors and will indefinitely collect stats from the supervisor |
| 52 | +func (s *Collector) Run() { |
| 53 | + type publishersPair struct { |
| 54 | + container *container.Container |
| 55 | + publisher *pubsub.Publisher |
| 56 | + } |
| 57 | + // we cannot determine the capacity here. |
| 58 | + // it will grow enough in first iteration |
| 59 | + var pairs []publishersPair |
| 60 | + |
| 61 | + for range time.Tick(s.interval) { |
| 62 | + // it does not make sense in the first iteration, |
| 63 | + // but saves allocations in further iterations |
| 64 | + pairs = pairs[:0] |
| 65 | + |
| 66 | + s.m.Lock() |
| 67 | + for container, publisher := range s.publishers { |
| 68 | + // copy pointers here to release the lock ASAP |
| 69 | + pairs = append(pairs, publishersPair{container, publisher}) |
| 70 | + } |
| 71 | + s.m.Unlock() |
| 72 | + if len(pairs) == 0 { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + systemUsage, err := s.getSystemCPUUsage() |
| 77 | + if err != nil { |
| 78 | + logrus.Errorf("collecting system cpu usage: %v", err) |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + for _, pair := range pairs { |
| 83 | + stats, err := s.supervisor.GetContainerStats(pair.container) |
| 84 | + if err != nil { |
| 85 | + if _, ok := err.(notRunningErr); !ok { |
| 86 | + logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err) |
| 87 | + } |
| 88 | + continue |
| 89 | + } |
| 90 | + // FIXME: move to containerd on Linux (not Windows) |
| 91 | + stats.CPUStats.SystemUsage = systemUsage |
| 92 | + |
| 93 | + pair.publisher.Publish(*stats) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +type notRunningErr interface { |
| 99 | + error |
| 100 | + ContainerIsRunning() bool |
| 101 | +} |
0 commit comments