Files
kata-containers/containerd-shim-v2/metrics.go
ZeroMagic 7951041eb0 containerd-shim-kata-v2: add the service Stats support
Add the Stats api support to get the container's
resouces statistic.

Signed-off-by: ZeroMagic <anthonyliu@zju.edu.cn>
2018-11-28 14:32:25 +08:00

72 lines
1.5 KiB
Go

// 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
}