diff --git a/api/grpc/server/server.go b/api/grpc/server/server.go index 3e08bad..7b9bb57 100644 --- a/api/grpc/server/server.go +++ b/api/grpc/server/server.go @@ -233,3 +233,39 @@ func (s *apiServer) Events(r *types.EventsRequest, stream types.API_EventsServer } return nil } + +func (s *apiServer) GetStats(r *types.StatsRequest, stream types.API_GetStatsServer) error { + e := containerd.NewEvent(containerd.StatsEventType) + e.ID = r.Id + s.sv.SendEvent(e) + if err := <-e.Err; err != nil { + if err == containerd.ErrContainerNotFound { + return grpc.Errorf(codes.NotFound, err.Error()) + } + return err + } + defer func() { + ue := containerd.NewEvent(containerd.UnsubscribeStatsEventType) + ue.ID = e.ID + ue.Stats = e.Stats + s.sv.SendEvent(ue) + if err := <-ue.Err; err != nil { + logrus.Errorf("Error unsubscribing %s: %v", r.Id, err) + } + }() + for { + select { + case st := <-e.Stats: + pbSt, ok := st.(*types.Stats) + if !ok { + panic("invalid stats type from collector") + } + if err := stream.Send(pbSt); err != nil { + return err + } + case <-stream.Context().Done(): + return nil + } + } + return nil +} diff --git a/api/grpc/types/api.pb.go b/api/grpc/types/api.pb.go index 18878fc..80daf9a 100644 --- a/api/grpc/types/api.pb.go +++ b/api/grpc/types/api.pb.go @@ -44,6 +44,7 @@ It has these top-level messages: HugetlbStats CgroupStats Stats + StatsRequest */ package types @@ -638,6 +639,7 @@ func (m *CgroupStats) GetHugetlbStats() map[string]*HugetlbStats { type Stats struct { NetworkStats []*NetworkStats `protobuf:"bytes,1,rep,name=network_stats" json:"network_stats,omitempty"` CgroupStats *CgroupStats `protobuf:"bytes,2,opt,name=cgroup_stats" json:"cgroup_stats,omitempty"` + Timestamp uint64 `protobuf:"varint,3,opt,name=timestamp" json:"timestamp,omitempty"` } func (m *Stats) Reset() { *m = Stats{} } @@ -659,6 +661,15 @@ func (m *Stats) GetCgroupStats() *CgroupStats { return nil } +type StatsRequest struct { + Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"` +} + +func (m *StatsRequest) Reset() { *m = StatsRequest{} } +func (m *StatsRequest) String() string { return proto.CompactTextString(m) } +func (*StatsRequest) ProtoMessage() {} +func (*StatsRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{35} } + func init() { proto.RegisterType((*CreateContainerRequest)(nil), "types.CreateContainerRequest") proto.RegisterType((*CreateContainerResponse)(nil), "types.CreateContainerResponse") @@ -695,6 +706,7 @@ func init() { proto.RegisterType((*HugetlbStats)(nil), "types.HugetlbStats") proto.RegisterType((*CgroupStats)(nil), "types.CgroupStats") proto.RegisterType((*Stats)(nil), "types.Stats") + proto.RegisterType((*StatsRequest)(nil), "types.StatsRequest") } // Reference imports to suppress errors if they are not otherwise used. @@ -713,6 +725,7 @@ type APIClient interface { ListCheckpoint(ctx context.Context, in *ListCheckpointRequest, opts ...grpc.CallOption) (*ListCheckpointResponse, error) State(ctx context.Context, in *StateRequest, opts ...grpc.CallOption) (*StateResponse, error) Events(ctx context.Context, in *EventsRequest, opts ...grpc.CallOption) (API_EventsClient, error) + GetStats(ctx context.Context, in *StatsRequest, opts ...grpc.CallOption) (API_GetStatsClient, error) } type aPIClient struct { @@ -827,6 +840,38 @@ func (x *aPIEventsClient) Recv() (*Event, error) { return m, nil } +func (c *aPIClient) GetStats(ctx context.Context, in *StatsRequest, opts ...grpc.CallOption) (API_GetStatsClient, error) { + stream, err := grpc.NewClientStream(ctx, &_API_serviceDesc.Streams[1], c.cc, "/types.API/GetStats", opts...) + if err != nil { + return nil, err + } + x := &aPIGetStatsClient{stream} + if err := x.ClientStream.SendMsg(in); err != nil { + return nil, err + } + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + return x, nil +} + +type API_GetStatsClient interface { + Recv() (*Stats, error) + grpc.ClientStream +} + +type aPIGetStatsClient struct { + grpc.ClientStream +} + +func (x *aPIGetStatsClient) Recv() (*Stats, error) { + m := new(Stats) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + // Server API for API service type APIServer interface { @@ -839,6 +884,7 @@ type APIServer interface { ListCheckpoint(context.Context, *ListCheckpointRequest) (*ListCheckpointResponse, error) State(context.Context, *StateRequest) (*StateResponse, error) Events(*EventsRequest, API_EventsServer) error + GetStats(*StatsRequest, API_GetStatsServer) error } func RegisterAPIServer(s *grpc.Server, srv APIServer) { @@ -962,6 +1008,27 @@ func (x *aPIEventsServer) Send(m *Event) error { return x.ServerStream.SendMsg(m) } +func _API_GetStats_Handler(srv interface{}, stream grpc.ServerStream) error { + m := new(StatsRequest) + if err := stream.RecvMsg(m); err != nil { + return err + } + return srv.(APIServer).GetStats(m, &aPIGetStatsServer{stream}) +} + +type API_GetStatsServer interface { + Send(*Stats) error + grpc.ServerStream +} + +type aPIGetStatsServer struct { + grpc.ServerStream +} + +func (x *aPIGetStatsServer) Send(m *Stats) error { + return x.ServerStream.SendMsg(m) +} + var _API_serviceDesc = grpc.ServiceDesc{ ServiceName: "types.API", HandlerType: (*APIServer)(nil), @@ -1005,97 +1072,104 @@ var _API_serviceDesc = grpc.ServiceDesc{ Handler: _API_Events_Handler, ServerStreams: true, }, + { + StreamName: "GetStats", + Handler: _API_GetStats_Handler, + ServerStreams: true, + }, }, } var fileDescriptor0 = []byte{ - // 1396 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xb4, 0x57, 0xd9, 0x92, 0xdb, 0x44, - 0x17, 0x1e, 0xdb, 0xf2, 0x76, 0xbc, 0x8d, 0x35, 0x19, 0xc7, 0xe3, 0xff, 0x0f, 0x49, 0x44, 0x80, - 0x14, 0x95, 0x72, 0x85, 0x09, 0x4b, 0x08, 0x17, 0x10, 0x66, 0x52, 0x04, 0x2a, 0x81, 0xa9, 0x59, - 0xa8, 0xe2, 0x02, 0x5c, 0x1a, 0xa9, 0xb1, 0x1b, 0xcb, 0x92, 0x90, 0x5a, 0xb3, 0x14, 0x6f, 0xc0, - 0x25, 0x0f, 0xc3, 0x03, 0xf0, 0x04, 0x3c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bc, 0x3c, 0x24, 0x51, 0xce, 0xdc, 0xf2, 0x47, 0xb7, 0xf2, 0x9b, 0x4a, 0xeb, - 0x31, 0xd4, 0xc4, 0x95, 0x50, 0xa5, 0x2c, 0x77, 0x43, 0x1c, 0xb5, 0xb3, 0xbb, 0xc6, 0xd6, 0xe3, - 0xd2, 0x79, 0x8d, 0xbf, 0xc0, 0x9f, 0xfc, 0x1d, 0x00, 0x00, 0xff, 0xff, 0x45, 0x12, 0x50, 0x50, - 0x8e, 0x0f, 0x00, 0x00, + // 1426 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xb4, 0x57, 0xd9, 0x6e, 0x1c, 0x55, + 0x13, 0xf6, 0xcc, 0xf4, 0x6c, 0x35, 0x8b, 0xed, 0x76, 0xec, 0x8c, 0xe7, 0xff, 0x43, 0x92, 0x26, + 0x40, 0x84, 0x22, 0x2b, 0x38, 0x2c, 0x21, 0x5c, 0x40, 0x70, 0xa2, 0x04, 0x94, 0x80, 0x95, 0xd8, + 0x48, 0xdc, 0x30, 0x6a, 0x77, 0x1f, 0x66, 0x0e, 0xd3, 0x1b, 0xdd, 0xa7, 0xbd, 0x88, 0x37, 0xe0, + 0x81, 0x78, 0x00, 0x24, 0xee, 0x79, 0x0e, 0x9e, 0x82, 0x3a, 0x4b, 0x9f, 0x5e, 0x66, 0x09, 0x5c, + 0x70, 0x33, 0xd2, 0xa9, 0xae, 0xfa, 0xaa, 0xea, 0xab, 0x65, 0xce, 0x81, 0xae, 0x1d, 0xd1, 0x83, + 0x28, 0x0e, 0x59, 0x68, 0x36, 0xd9, 0x55, 0x44, 0x12, 0xeb, 0x17, 0xd8, 0x3b, 0x8a, 0x89, 0xcd, + 0xc8, 0x51, 0x18, 0x30, 0x9b, 0x06, 0x24, 0x7e, 0x45, 0x7e, 0x4e, 0x49, 0xc2, 0x4c, 0x80, 0x3a, + 0x75, 0x47, 0xb5, 0x5b, 0xb5, 0xbb, 0x5d, 0x13, 0x0f, 0x67, 0x69, 0xe0, 0x7a, 0xe4, 0xd8, 0x66, + 0xb3, 0x51, 0x5d, 0xc8, 0x06, 0xd0, 0x4c, 0x98, 0x4b, 0x83, 0x51, 0x43, 0x1c, 0x87, 0xd0, 0xc2, + 0x63, 0x98, 0xb2, 0x91, 0x51, 0x38, 0x93, 0x38, 0x1e, 0x35, 0x33, 0x08, 0x67, 0x46, 0x9c, 0x79, + 0x14, 0xd2, 0x80, 0x8d, 0xda, 0x5c, 0x66, 0xed, 0xc3, 0xf5, 0x05, 0xe7, 0x49, 0x14, 0x06, 0x09, + 0xb1, 0x1e, 0xc2, 0xe0, 0x35, 0x9d, 0x06, 0xb6, 0xb7, 0x2c, 0x9c, 0x1e, 0x34, 0x22, 0x3c, 0xf0, + 0x38, 0x06, 0xc2, 0x91, 0xd0, 0x14, 0x81, 0x0c, 0xac, 0x2d, 0x18, 0x66, 0x96, 0x0a, 0x8b, 0xc1, + 0xf6, 0x63, 0xd7, 0x3d, 0x8e, 0x43, 0x87, 0x24, 0xc9, 0x32, 0xbc, 0x2d, 0xe8, 0x30, 0x12, 0xfb, + 0x94, 0x83, 0x70, 0xd0, 0x8e, 0xb9, 0x0f, 0x46, 0x9a, 0x90, 0x58, 0x40, 0xf6, 0x0e, 0x7b, 0x07, + 0x82, 0xac, 0x83, 0x53, 0x14, 0x99, 0x7d, 0x30, 0xec, 0x78, 0x9a, 0x60, 0x9a, 0x0d, 0x19, 0x0a, + 0x09, 0xce, 0x31, 0x47, 0x75, 0x70, 0x2e, 0xdc, 0x51, 0x4b, 0x24, 0xf7, 0x10, 0x0c, 0xa1, 0x8f, + 0xc2, 0x54, 0x79, 0x1a, 0xf0, 0xc3, 0x54, 0x47, 0xbe, 0x07, 0x43, 0xdb, 0x75, 0x29, 0xa3, 0x21, + 0x3a, 0x7e, 0x46, 0xdd, 0x04, 0xdd, 0x35, 0x30, 0x83, 0xdb, 0x60, 0x16, 0xe3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x0f, 0xfe, 0x0e, 0x00, 0x00, 0xff, 0xff, 0xef, 0x98, 0x5d, 0x0a, 0xf0, 0x0f, + 0x00, 0x00, } diff --git a/api/grpc/types/api.proto b/api/grpc/types/api.proto index 2a73ba8..b69e1b5 100644 --- a/api/grpc/types/api.proto +++ b/api/grpc/types/api.proto @@ -12,6 +12,7 @@ service API { rpc ListCheckpoint(ListCheckpointRequest) returns (ListCheckpointResponse) {} rpc State(StateRequest) returns (StateResponse) {} rpc Events(EventsRequest) returns (stream Event) {} + rpc GetStats(StatsRequest) returns (stream Stats) {} } message CreateContainerRequest { @@ -225,4 +226,9 @@ message CgroupStats { message Stats { repeated NetworkStats network_stats = 1; CgroupStats cgroup_stats = 2; + uint64 timestamp = 3; }; + +message StatsRequest { + string id = 1; +} diff --git a/ctr/container.go b/ctr/container.go index f931111..7125a71 100644 --- a/ctr/container.go +++ b/ctr/container.go @@ -230,3 +230,31 @@ var ExecCommand = cli.Command{ } }, } + +var StatsCommand = cli.Command{ + Name: "stats", + Usage: "get stats for running container", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "id", + Usage: "container id", + }, + }, + Action: func(context *cli.Context) { + req := &types.StatsRequest{ + Id: context.String("id"), + } + c := getClient() + stream, err := c.GetStats(netcontext.Background(), req) + if err != nil { + fatal(err.Error(), 1) + } + for { + stats, err := stream.Recv() + if err != nil { + fatal(err.Error(), 1) + } + fmt.Println(stats) + } + }, +} diff --git a/ctr/main.go b/ctr/main.go index 632f168..8d27baf 100644 --- a/ctr/main.go +++ b/ctr/main.go @@ -37,6 +37,7 @@ func main() { CheckpointCommand, ContainersCommand, EventsCommand, + StatsCommand, } app.Before = func(context *cli.Context) error { if context.GlobalBool("debug") { diff --git a/event.go b/event.go index 6a5c5ea..632fa1a 100644 --- a/event.go +++ b/event.go @@ -21,6 +21,8 @@ const ( UpdateContainerEventType EventType = "updateContainer" CreateCheckpointEventType EventType = "createCheckpoint" DeleteCheckpointEventType EventType = "deleteCheckpoint" + StatsEventType EventType = "events" + UnsubscribeStatsEventType EventType = "unsubscribeEvents" ) func NewEvent(t EventType) *Event { @@ -47,6 +49,7 @@ type Event struct { Containers []runtime.Container Checkpoint *runtime.Checkpoint Err chan error + Stats chan interface{} } type Handler interface { diff --git a/stats.go b/stats.go index 4f4a21b..83d84ae 100644 --- a/stats.go +++ b/stats.go @@ -17,3 +17,29 @@ func Metrics() map[string]interface{} { "events-subscribers": EventSubscriberCounter, } } + +type StatsEvent struct { + s *Supervisor +} + +type UnsubscribeStatsEvent struct { + s *Supervisor +} + +func (h *StatsEvent) Handle(e *Event) error { + i, ok := h.s.containers[e.ID] + if !ok { + return ErrContainerNotFound + } + e.Stats = h.s.statsCollector.collect(i.container) + return nil +} + +func (h *UnsubscribeStatsEvent) Handle(e *Event) error { + i, ok := h.s.containers[e.ID] + if !ok { + return ErrContainerNotFound + } + h.s.statsCollector.unsubscribe(i.container, e.Stats) + return nil +} diff --git a/stats_collector.go b/stats_collector.go new file mode 100644 index 0000000..f802cad --- /dev/null +++ b/stats_collector.go @@ -0,0 +1,240 @@ +package containerd + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + "sync" + "time" + + "github.com/Sirupsen/logrus" + "github.com/docker/containerd/api/grpc/types" + "github.com/docker/containerd/runtime" + "github.com/docker/docker/pkg/pubsub" + "github.com/opencontainers/runc/libcontainer" + "github.com/opencontainers/runc/libcontainer/cgroups" + "github.com/opencontainers/runc/libcontainer/system" +) + +func convertBlkioEntryToPb(b []cgroups.BlkioStatEntry) []*types.BlkioStatsEntry { + var pbEs []*types.BlkioStatsEntry + for _, e := range b { + pbEs = append(pbEs, &types.BlkioStatsEntry{ + Major: e.Major, + Minor: e.Minor, + Op: e.Op, + Value: e.Value, + }) + } + return pbEs +} + +func convertToPb(st *runtime.Stat) *types.Stats { + pbSt := &types.Stats{ + Timestamp: uint64(st.Timestamp.Unix()), + CgroupStats: &types.CgroupStats{}, + } + lcSt, ok := st.Data.(*libcontainer.Stats) + if !ok { + return pbSt + } + cpuSt := lcSt.CgroupStats.CpuStats + pbSt.CgroupStats.CpuStats = &types.CpuStats{ + CpuUsage: &types.CpuUsage{ + TotalUsage: cpuSt.CpuUsage.TotalUsage, + PercpuUsage: cpuSt.CpuUsage.PercpuUsage, + UsageInKernelmode: cpuSt.CpuUsage.UsageInKernelmode, + UsageInUsermode: cpuSt.CpuUsage.UsageInUsermode, + }, + ThrottlingData: &types.ThrottlingData{ + Periods: cpuSt.ThrottlingData.Periods, + ThrottledPeriods: cpuSt.ThrottlingData.ThrottledPeriods, + ThrottledTime: cpuSt.ThrottlingData.ThrottledTime, + }, + } + memSt := lcSt.CgroupStats.MemoryStats + pbSt.CgroupStats.MemoryStats = &types.MemoryStats{ + Cache: memSt.Cache, + Usage: &types.MemoryData{ + Usage: memSt.Usage.Usage, + MaxUsage: memSt.Usage.MaxUsage, + Failcnt: memSt.Usage.Failcnt, + }, + SwapUsage: &types.MemoryData{ + Usage: memSt.SwapUsage.Usage, + MaxUsage: memSt.SwapUsage.MaxUsage, + Failcnt: memSt.SwapUsage.Failcnt, + }, + } + blkSt := lcSt.CgroupStats.BlkioStats + pbSt.CgroupStats.BlkioStats = &types.BlkioStats{ + IoServiceBytesRecursive: convertBlkioEntryToPb(blkSt.IoServiceBytesRecursive), + IoServicedRecursive: convertBlkioEntryToPb(blkSt.IoServicedRecursive), + IoQueuedRecursive: convertBlkioEntryToPb(blkSt.IoQueuedRecursive), + IoServiceTimeRecursive: convertBlkioEntryToPb(blkSt.IoServiceTimeRecursive), + IoWaitTimeRecursive: convertBlkioEntryToPb(blkSt.IoWaitTimeRecursive), + IoMergedRecursive: convertBlkioEntryToPb(blkSt.IoMergedRecursive), + IoTimeRecursive: convertBlkioEntryToPb(blkSt.IoTimeRecursive), + SectorsRecursive: convertBlkioEntryToPb(blkSt.SectorsRecursive), + } + pbSt.CgroupStats.HugetlbStats = make(map[string]*types.HugetlbStats) + for k, st := range lcSt.CgroupStats.HugetlbStats { + pbSt.CgroupStats.HugetlbStats[k] = &types.HugetlbStats{ + Usage: st.Usage, + MaxUsage: st.MaxUsage, + Failcnt: st.Failcnt, + } + } + return pbSt +} + +type statsPair struct { + ct runtime.Container + pub *pubsub.Publisher +} + +func newStatsCollector(interval time.Duration) *statsCollector { + s := &statsCollector{ + interval: interval, + clockTicksPerSecond: uint64(system.GetClockTicks()), + bufReader: bufio.NewReaderSize(nil, 128), + publishers: make(map[string]*statsPair), + } + go s.run() + return s +} + +// statsCollector manages and provides container resource stats +type statsCollector struct { + m sync.Mutex + supervisor *Supervisor + interval time.Duration + clockTicksPerSecond uint64 + publishers map[string]*statsPair + bufReader *bufio.Reader +} + +// 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 *statsCollector) collect(c runtime.Container) chan interface{} { + s.m.Lock() + defer s.m.Unlock() + publisher, exists := s.publishers[c.ID()] + if !exists { + pub := pubsub.NewPublisher(100*time.Millisecond, 1024) + publisher = &statsPair{ct: c, pub: pub} + s.publishers[c.ID()] = publisher + } + return publisher.pub.Subscribe() +} + +// stopCollection closes the channels for all subscribers and removes +// the container from metrics collection. +func (s *statsCollector) stopCollection(c runtime.Container) { + s.m.Lock() + if publisher, exists := s.publishers[c.ID()]; exists { + publisher.pub.Close() + delete(s.publishers, c.ID()) + } + s.m.Unlock() +} + +// unsubscribe removes a specific subscriber from receiving updates for a container's stats. +func (s *statsCollector) unsubscribe(c runtime.Container, ch chan interface{}) { + s.m.Lock() + publisher := s.publishers[c.ID()] + if publisher != nil { + publisher.pub.Evict(ch) + if publisher.pub.Len() == 0 { + delete(s.publishers, c.ID()) + } + } + s.m.Unlock() +} + +func (s *statsCollector) run() { + type publishersPair struct { + container runtime.Container + publisher *pubsub.Publisher + } + // we cannot determine the capacity here. + // it will grow enough in first iteration + var pairs []*statsPair + + for range time.Tick(s.interval) { + // it does not make sense in the first iteration, + // but saves allocations in further iterations + pairs = pairs[:0] + + s.m.Lock() + for _, publisher := range s.publishers { + // copy pointers here to release the lock ASAP + pairs = append(pairs, publisher) + } + s.m.Unlock() + if len(pairs) == 0 { + continue + } + + for _, pair := range pairs { + stats, err := pair.ct.Stats() + if err != nil { + logrus.Errorf("Error getting stats for container ID %s", pair.ct.ID()) + continue + } + + pair.pub.Publish(convertToPb(stats)) + } + } +} + +const nanoSecondsPerSecond = 1e9 + +// 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) { + var line string + f, err := os.Open("/proc/stat") + if err != nil { + return 0, err + } + 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) + switch parts[0] { + case "cpu": + if len(parts) < 8 { + return 0, fmt.Errorf("bad format of cpu stats") + } + var totalClockTicks uint64 + for _, i := range parts[1:8] { + v, err := strconv.ParseUint(i, 10, 64) + if err != nil { + return 0, fmt.Errorf("error parsing cpu stats") + } + totalClockTicks += v + } + return (totalClockTicks * nanoSecondsPerSecond) / + s.clockTicksPerSecond, nil + } + } + return 0, fmt.Errorf("bad stats format") +} diff --git a/supervisor.go b/supervisor.go index c186c6d..09c07c1 100644 --- a/supervisor.go +++ b/supervisor.go @@ -7,12 +7,15 @@ import ( goruntime "runtime" "sync" "syscall" + "time" "github.com/Sirupsen/logrus" "github.com/docker/containerd/runtime" "github.com/opencontainers/runc/libcontainer" ) +const statsInterval = 1 * time.Second + // NewSupervisor returns an initialized Process supervisor. func NewSupervisor(id, stateDir string, tasks chan *StartTask) (*Supervisor, error) { if err := os.MkdirAll(stateDir, 0755); err != nil { @@ -28,14 +31,15 @@ func NewSupervisor(id, stateDir string, tasks chan *StartTask) (*Supervisor, err return nil, err } s := &Supervisor{ - stateDir: stateDir, - containers: make(map[string]*containerInfo), - processes: make(map[int]*containerInfo), - runtime: r, - tasks: tasks, - events: make(chan *Event, DefaultBufferSize), - machine: machine, - subscribers: make(map[chan *Event]struct{}), + stateDir: stateDir, + containers: make(map[string]*containerInfo), + processes: make(map[int]*containerInfo), + runtime: r, + tasks: tasks, + events: make(chan *Event, DefaultBufferSize), + machine: machine, + subscribers: make(map[chan *Event]struct{}), + statsCollector: newStatsCollector(statsInterval), } // register default event handlers s.handlers = map[EventType]Handler{ @@ -49,6 +53,8 @@ func NewSupervisor(id, stateDir string, tasks chan *StartTask) (*Supervisor, err UpdateContainerEventType: &UpdateEvent{s}, CreateCheckpointEventType: &CreateCheckpointEvent{s}, DeleteCheckpointEventType: &DeleteCheckpointEvent{s}, + StatsEventType: &StatsEvent{s}, + UnsubscribeStatsEventType: &UnsubscribeStatsEvent{s}, } // start the container workers for concurrent container starts return s, nil @@ -74,6 +80,7 @@ type Supervisor struct { subscribers map[chan *Event]struct{} machine Machine containerGroup sync.WaitGroup + statsCollector *statsCollector } // Stop closes all tasks and sends a SIGTERM to each container's pid1 then waits for they to