Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
containerd-shim-kata-v2: add the service Stats support
Browse files Browse the repository at this point in the history
Add the Stats api support to get the container's
resouces statistic.

Signed-off-by: ZeroMagic <anthonyliu@zju.edu.cn>
  • Loading branch information
ZeroMagic authored and lifupan committed Nov 28, 2018
1 parent 5cc016c commit 7951041
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
71 changes: 71 additions & 0 deletions containerd-shim-v2/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//

package containerdshim

import (
"github.com/containerd/cgroups"
"github.com/containerd/typeurl"

google_protobuf "github.com/gogo/protobuf/types"
vc "github.com/kata-containers/runtime/virtcontainers"
)

func marshalMetrics(s *service, containerID string) (*google_protobuf.Any, error) {
stats, err := s.sandbox.StatsContainer(containerID)
if err != nil {
return nil, err
}

metrics := statsToMetrics(stats.CgroupStats)

data, err := typeurl.MarshalAny(metrics)
if err != nil {
return nil, err
}

return data, nil
}

func statsToMetrics(cgStats *vc.CgroupStats) *cgroups.Metrics {
var hugetlb []*cgroups.HugetlbStat
for _, v := range cgStats.HugetlbStats {
hugetlb = append(
hugetlb,
&cgroups.HugetlbStat{
Usage: v.Usage,
Max: v.MaxUsage,
Failcnt: v.Failcnt,
})
}

var perCPU []uint64
for _, v := range cgStats.CPUStats.CPUUsage.PercpuUsage {
perCPU = append(perCPU, v)
}

metrics := &cgroups.Metrics{
Hugetlb: hugetlb,
Pids: &cgroups.PidsStat{
Current: cgStats.PidsStats.Current,
Limit: cgStats.PidsStats.Limit,
},
CPU: &cgroups.CPUStat{
Usage: &cgroups.CPUUsage{
Total: cgStats.CPUStats.CPUUsage.TotalUsage,
PerCPU: perCPU,
},
},
Memory: &cgroups.MemoryStat{
Cache: cgStats.MemoryStats.Cache,
Usage: &cgroups.MemoryEntry{
Limit: cgStats.MemoryStats.Usage.Limit,
Usage: cgStats.MemoryStats.Usage.Usage,
},
},
}

return metrics
}
17 changes: 16 additions & 1 deletion containerd-shim-v2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,22 @@ func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (*pt
}

func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.StatsResponse, error) {
return nil, errdefs.ErrNotImplemented
s.Lock()
defer s.Unlock()

c, err := s.getContainer(r.ID)
if err != nil {
return nil, err
}

data, err := marshalMetrics(s, c.id)
if err != nil {
return nil, err
}

return &taskAPI.StatsResponse{
Stats: data,
}, nil
}

// Update a running container
Expand Down

0 comments on commit 7951041

Please sign in to comment.