mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-04 02:56:18 +00:00
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>
This commit is contained in:
parent
5cc016c8a2
commit
7951041eb0
71
containerd-shim-v2/metrics.go
Normal file
71
containerd-shim-v2/metrics.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user