From 8f1875b57fa4054436a878d34ca02c238fa8ba34 Mon Sep 17 00:00:00 2001 From: bpopovschi Date: Mon, 4 Nov 2019 20:08:37 +0200 Subject: [PATCH] Added cgroups v2 CPU implementation. Signed-off-by: bpopovschi --- cmd/cgroups-playground/main.go | 29 +- v2/cpu.go | 123 +++++++ v2/errors.go | 1 + v2/stats/metrics.pb.go | 603 ++++++++++++++++++++++++++++++++- v2/stats/metrics.pb.txt | 57 ++++ v2/stats/metrics.proto | 16 +- v2/utils.go | 20 ++ 7 files changed, 832 insertions(+), 17 deletions(-) create mode 100644 v2/cpu.go diff --git a/cmd/cgroups-playground/main.go b/cmd/cgroups-playground/main.go index 68bc98c0..85d6d3a5 100644 --- a/cmd/cgroups-playground/main.go +++ b/cmd/cgroups-playground/main.go @@ -17,10 +17,11 @@ package main import ( - "os" - "github.com/containerd/cgroups/v2" + stats2 "github.com/containerd/cgroups/v2/stats" + "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" + "os" ) func main() { @@ -54,5 +55,29 @@ func xmain() error { for i, s := range subsystems { logrus.Infof("Subsystem %d: %q", i, s.Name()) } + + cpuCgroup, err := v2.NewCpu(unifiedMountpoint) + if err != nil { + return err + } + var period, shares uint64 = 1000, 5000 + resources := specs.LinuxResources{ + CPU: &specs.LinuxCPU{Period: &period, Shares: &shares}, + } + err = cpuCgroup.Create(g, &resources) + if err != nil { + return err + } + stats := stats2.Metrics{ + CPU: &stats2.CPUStat{ + Usage: &stats2.CPUUsage{}, + }, + } + err = cpuCgroup.Stat(g, &stats) + if err != nil { + return err + } + logrus.Infof("CPU usage stats: usage in kernel mode - %d", stats.CPU.Usage.Kernel) + return nil } diff --git a/v2/cpu.go b/v2/cpu.go new file mode 100644 index 00000000..0ab4cd3d --- /dev/null +++ b/v2/cpu.go @@ -0,0 +1,123 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package v2 + +import ( + "bufio" + "io/ioutil" + "os" + "path/filepath" + "strconv" + + statsv2 "github.com/containerd/cgroups/v2/stats" + specs "github.com/opencontainers/runtime-spec/specs-go" +) + +func NewCpu(unifiedMountpoint string) (*cpuController, error) { + c := &cpuController{ + unifiedMountpoint: unifiedMountpoint, + } + + ok, err := c.Available("/") + if err != nil { + return nil, err + } + if !ok { + return nil, ErrCPUNotSupported + } + return c, nil +} + +type cpuController struct { + unifiedMountpoint string +} + +func (c *cpuController) Name() Name { + return Cpu +} + +func (c *cpuController) path(g GroupPath) string { + return filepath.Join(c.unifiedMountpoint, string(g)) +} + +func (c *cpuController) Create(g GroupPath, resources *specs.LinuxResources) error { + if err := os.MkdirAll(c.path(g), defaultDirPerm); err != nil { + return err + } + if cpuShares := resources.CPU.Shares; cpuShares != nil { + // Converting cgroups configuration from v1 to v2 + // more here https://github.com/containers/crun/blob/master/crun.1.md#cgroup-v2 + convertedWeight := (1 + ((*cpuShares-2)*9999)/262142) + weight := []byte(strconv.FormatUint(convertedWeight, 10)) + if err := ioutil.WriteFile( + filepath.Join(c.path(g), "cpu.weight"), + weight, + defaultFilePerm, + ); err != nil { + return err + } + } + + if cpuPeriod := resources.CPU.Period; cpuPeriod != nil { + max := []byte(strconv.FormatUint(*cpuPeriod, 10)) + if err := ioutil.WriteFile( + filepath.Join(c.path(g), "cpu.max"), + max, + defaultFilePerm, + ); err != nil { + return err + } + } + + return nil +} + +func (c *cpuController) Update(g GroupPath, resources *specs.LinuxResources) error { + return c.Create(g, resources) +} + +func (c *cpuController) Stat(g GroupPath, stats *statsv2.Metrics) error { + f, err := os.Open(filepath.Join(c.path(g), "cpu.stat")) + if err != nil { + return err + } + defer f.Close() + // get or create the cpu field because cpuacct can also set values on this struct + sc := bufio.NewScanner(f) + for sc.Scan() { + if err := sc.Err(); err != nil { + return err + } + key, v, err := parseKV(sc.Text()) + if err != nil { + return err + } + switch key { + case "usage_usec": + stats.CPU.Usage.Total = v + case "user_usec": + stats.CPU.Usage.User = v + case "system_usec": + stats.CPU.Usage.Kernel = v + } + } + return nil +} + +func (c *cpuController) Available(g GroupPath) (bool, error) { + return available(c.unifiedMountpoint, g, Pids) +} diff --git a/v2/errors.go b/v2/errors.go index f56aafdd..60af72dd 100644 --- a/v2/errors.go +++ b/v2/errors.go @@ -28,6 +28,7 @@ var ( ErrFreezerNotSupported = errors.New("cgroups: freezer cgroup (v2) not supported on this system") ErrMemoryNotSupported = errors.New("cgroups: memory cgroup (v2) not supported on this system") ErrPidsNotSupported = errors.New("cgroups: pids cgroup (v2) not supported on this system") + ErrCPUNotSupported = errors.New("cgroups: cpu cgroup (v2) not supported on this system") ErrCgroupDeleted = errors.New("cgroups: cgroup deleted") ErrNoCgroupMountDestination = errors.New("cgroups: cannot find cgroup mount destination") diff --git a/v2/stats/metrics.pb.go b/v2/stats/metrics.pb.go index a4a6858d..6bd04bac 100644 --- a/v2/stats/metrics.pb.go +++ b/v2/stats/metrics.pb.go @@ -5,6 +5,7 @@ package stats import ( fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" io "io" math "math" @@ -25,6 +26,7 @@ const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package type Metrics struct { Pids *PidsStat `protobuf:"bytes,1,opt,name=pids,proto3" json:"pids,omitempty"` + CPU *CPUStat `protobuf:"bytes,2,opt,name=cpu,proto3" json:"cpu,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -102,9 +104,93 @@ func (m *PidsStat) XXX_DiscardUnknown() { var xxx_messageInfo_PidsStat proto.InternalMessageInfo +type CPUStat struct { + Usage *CPUUsage `protobuf:"bytes,1,opt,name=usage,proto3" json:"usage,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *CPUStat) Reset() { *m = CPUStat{} } +func (*CPUStat) ProtoMessage() {} +func (*CPUStat) Descriptor() ([]byte, []int) { + return fileDescriptor_2fc6005842049e6b, []int{2} +} +func (m *CPUStat) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *CPUStat) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_CPUStat.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *CPUStat) XXX_Merge(src proto.Message) { + xxx_messageInfo_CPUStat.Merge(m, src) +} +func (m *CPUStat) XXX_Size() int { + return m.Size() +} +func (m *CPUStat) XXX_DiscardUnknown() { + xxx_messageInfo_CPUStat.DiscardUnknown(m) +} + +var xxx_messageInfo_CPUStat proto.InternalMessageInfo + +type CPUUsage struct { + // values in nanoseconds + Total uint64 `protobuf:"varint,1,opt,name=total,proto3" json:"total,omitempty"` + Kernel uint64 `protobuf:"varint,2,opt,name=kernel,proto3" json:"kernel,omitempty"` + User uint64 `protobuf:"varint,3,opt,name=user,proto3" json:"user,omitempty"` + PerCPU []uint64 `protobuf:"varint,4,rep,packed,name=per_cpu,json=perCpu,proto3" json:"per_cpu,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *CPUUsage) Reset() { *m = CPUUsage{} } +func (*CPUUsage) ProtoMessage() {} +func (*CPUUsage) Descriptor() ([]byte, []int) { + return fileDescriptor_2fc6005842049e6b, []int{3} +} +func (m *CPUUsage) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *CPUUsage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_CPUUsage.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *CPUUsage) XXX_Merge(src proto.Message) { + xxx_messageInfo_CPUUsage.Merge(m, src) +} +func (m *CPUUsage) XXX_Size() int { + return m.Size() +} +func (m *CPUUsage) XXX_DiscardUnknown() { + xxx_messageInfo_CPUUsage.DiscardUnknown(m) +} + +var xxx_messageInfo_CPUUsage proto.InternalMessageInfo + func init() { proto.RegisterType((*Metrics)(nil), "io.containerd.cgroups.v2.Metrics") proto.RegisterType((*PidsStat)(nil), "io.containerd.cgroups.v2.PidsStat") + proto.RegisterType((*CPUStat)(nil), "io.containerd.cgroups.v2.CPUStat") + proto.RegisterType((*CPUUsage)(nil), "io.containerd.cgroups.v2.CPUUsage") } func init() { @@ -112,20 +198,28 @@ func init() { } var fileDescriptor_2fc6005842049e6b = []byte{ - // 199 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x32, 0x49, 0xcf, 0x2c, 0xc9, - 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0xcf, 0x2b, 0x49, 0xcc, 0xcc, 0x4b, 0x2d, - 0x4a, 0xd1, 0x4f, 0x4e, 0x2f, 0xca, 0x2f, 0x2d, 0x28, 0xd6, 0x2f, 0x33, 0xd2, 0x2f, 0x2e, 0x49, - 0x2c, 0x29, 0xd6, 0xcf, 0x4d, 0x2d, 0x29, 0xca, 0x4c, 0x2e, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, - 0x17, 0x92, 0xc8, 0xcc, 0xd7, 0x43, 0xa8, 0xd6, 0x83, 0xaa, 0xd6, 0x2b, 0x33, 0x52, 0x72, 0xe4, - 0x62, 0xf7, 0x85, 0x28, 0x15, 0x32, 0xe3, 0x62, 0x29, 0xc8, 0x4c, 0x29, 0x96, 0x60, 0x54, 0x60, - 0xd4, 0xe0, 0x36, 0x52, 0xd2, 0xc3, 0xa5, 0x47, 0x2f, 0x20, 0x33, 0xa5, 0x38, 0xb8, 0x24, 0xb1, - 0x24, 0x08, 0xac, 0x5e, 0xc9, 0x8a, 0x8b, 0x03, 0x26, 0x22, 0x24, 0xc1, 0xc5, 0x9e, 0x5c, 0x5a, - 0x54, 0x94, 0x9a, 0x57, 0x02, 0x36, 0x86, 0x25, 0x08, 0xc6, 0x15, 0x12, 0xe1, 0x62, 0xcd, 0xc9, - 0xcc, 0xcd, 0x2c, 0x91, 0x60, 0x02, 0x8b, 0x43, 0x38, 0x4e, 0x12, 0x27, 0x1e, 0xca, 0x31, 0xdc, - 0x78, 0x28, 0xc7, 0xd0, 0xf0, 0x48, 0x8e, 0xf1, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, - 0x1f, 0x3c, 0x92, 0x63, 0x4c, 0x62, 0x03, 0xbb, 0xdc, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xe2, - 0xc2, 0x63, 0xc7, 0xf1, 0x00, 0x00, 0x00, + // 329 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x91, 0x3f, 0x4b, 0xc3, 0x40, + 0x18, 0xc6, 0x1b, 0x93, 0x26, 0xe5, 0x75, 0x3b, 0x8a, 0x04, 0x87, 0xb4, 0xc6, 0xa5, 0xd3, 0x05, + 0xaa, 0x88, 0x88, 0x53, 0x33, 0x0b, 0x21, 0x92, 0x59, 0xd2, 0xf4, 0x88, 0x87, 0x6d, 0xee, 0xbc, + 0x3f, 0x5d, 0xf5, 0xe3, 0x75, 0x74, 0x74, 0x2a, 0x36, 0x9f, 0x44, 0xee, 0x92, 0xe2, 0x54, 0xdc, + 0xde, 0xe7, 0xe5, 0xf7, 0x3c, 0xcf, 0x9b, 0x1c, 0xdc, 0xd6, 0x54, 0xbd, 0xea, 0x25, 0xae, 0xd8, + 0x26, 0xa9, 0x58, 0xa3, 0x4a, 0xda, 0x10, 0xb1, 0x4a, 0xaa, 0x5a, 0x30, 0xcd, 0x65, 0xb2, 0x9d, + 0x27, 0x52, 0x95, 0x4a, 0x26, 0x1b, 0xa2, 0x04, 0xad, 0x24, 0xe6, 0x82, 0x29, 0x86, 0x42, 0xca, + 0xf0, 0x1f, 0x8d, 0x7b, 0x1a, 0x6f, 0xe7, 0x97, 0xe3, 0x9a, 0xd5, 0xcc, 0x42, 0x89, 0x99, 0x3a, + 0x3e, 0xfe, 0x80, 0xe0, 0xa9, 0x0b, 0x40, 0x77, 0xe0, 0x71, 0xba, 0x92, 0xa1, 0x33, 0x75, 0x66, + 0xe7, 0xf3, 0x18, 0x9f, 0x4a, 0xc2, 0x19, 0x5d, 0xc9, 0x67, 0x55, 0xaa, 0xdc, 0xf2, 0xe8, 0x11, + 0xdc, 0x8a, 0xeb, 0xf0, 0xcc, 0xda, 0xae, 0x4e, 0xdb, 0xd2, 0xac, 0x30, 0xae, 0x45, 0xd0, 0xee, + 0x27, 0x6e, 0x9a, 0x15, 0xb9, 0xb1, 0xc5, 0x0f, 0x30, 0x3a, 0xe6, 0xa1, 0x10, 0x82, 0x4a, 0x0b, + 0x41, 0x1a, 0x65, 0x8f, 0xf0, 0xf2, 0xa3, 0x44, 0x63, 0x18, 0xae, 0xe9, 0x86, 0x2a, 0xdb, 0xe2, + 0xe5, 0x9d, 0x88, 0x53, 0x08, 0xfa, 0x50, 0x74, 0x0f, 0x43, 0x2d, 0xcb, 0x9a, 0xfc, 0x7f, 0x7d, + 0x9a, 0x15, 0x85, 0x21, 0xf3, 0xce, 0x10, 0xbf, 0xc3, 0xe8, 0xb8, 0x32, 0x35, 0x8a, 0xa9, 0x72, + 0xdd, 0xd7, 0x77, 0x02, 0x5d, 0x80, 0xff, 0x46, 0x44, 0x43, 0xd6, 0x7d, 0x7b, 0xaf, 0x10, 0x02, + 0x4f, 0x4b, 0x22, 0x42, 0xd7, 0x6e, 0xed, 0x8c, 0xae, 0x21, 0xe0, 0x44, 0xbc, 0x98, 0x1f, 0xe2, + 0x4d, 0xdd, 0x99, 0xb7, 0x80, 0x76, 0x3f, 0xf1, 0x33, 0x22, 0xcc, 0x07, 0xfb, 0x9c, 0x88, 0x94, + 0xeb, 0x45, 0xb8, 0x3b, 0x44, 0x83, 0xef, 0x43, 0x34, 0xf8, 0x6c, 0x23, 0x67, 0xd7, 0x46, 0xce, + 0x57, 0x1b, 0x39, 0x3f, 0x6d, 0xe4, 0x2c, 0x7d, 0xfb, 0x2a, 0x37, 0xbf, 0x01, 0x00, 0x00, 0xff, + 0xff, 0xf3, 0xea, 0x41, 0x99, 0xfd, 0x01, 0x00, 0x00, } func (m *Metrics) Marshal() (dAtA []byte, err error) { @@ -153,6 +247,16 @@ func (m *Metrics) MarshalTo(dAtA []byte) (int, error) { } i += n1 } + if m.CPU != nil { + dAtA[i] = 0x12 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.CPU.Size())) + n2, err := m.CPU.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n2 + } if m.XXX_unrecognized != nil { i += copy(dAtA[i:], m.XXX_unrecognized) } @@ -190,6 +294,90 @@ func (m *PidsStat) MarshalTo(dAtA []byte) (int, error) { return i, nil } +func (m *CPUStat) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *CPUStat) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Usage != nil { + dAtA[i] = 0xa + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Usage.Size())) + n3, err := m.Usage.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n3 + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *CPUUsage) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *CPUUsage) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Total != 0 { + dAtA[i] = 0x8 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Total)) + } + if m.Kernel != 0 { + dAtA[i] = 0x10 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Kernel)) + } + if m.User != 0 { + dAtA[i] = 0x18 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.User)) + } + if len(m.PerCPU) > 0 { + dAtA5 := make([]byte, len(m.PerCPU)*10) + var j4 int + for _, num := range m.PerCPU { + for num >= 1<<7 { + dAtA5[j4] = uint8(uint64(num)&0x7f | 0x80) + num >>= 7 + j4++ + } + dAtA5[j4] = uint8(num) + j4++ + } + dAtA[i] = 0x22 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(j4)) + i += copy(dAtA[i:], dAtA5[:j4]) + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + func encodeVarintMetrics(dAtA []byte, offset int, v uint64) int { for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) @@ -209,6 +397,10 @@ func (m *Metrics) Size() (n int) { l = m.Pids.Size() n += 1 + l + sovMetrics(uint64(l)) } + if m.CPU != nil { + l = m.CPU.Size() + n += 1 + l + sovMetrics(uint64(l)) + } if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } @@ -233,6 +425,50 @@ func (m *PidsStat) Size() (n int) { return n } +func (m *CPUStat) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Usage != nil { + l = m.Usage.Size() + n += 1 + l + sovMetrics(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *CPUUsage) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Total != 0 { + n += 1 + sovMetrics(uint64(m.Total)) + } + if m.Kernel != 0 { + n += 1 + sovMetrics(uint64(m.Kernel)) + } + if m.User != 0 { + n += 1 + sovMetrics(uint64(m.User)) + } + if len(m.PerCPU) > 0 { + l = 0 + for _, e := range m.PerCPU { + l += sovMetrics(uint64(e)) + } + n += 1 + sovMetrics(uint64(l)) + l + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + func sovMetrics(x uint64) (n int) { for { n++ @@ -252,6 +488,7 @@ func (this *Metrics) String() string { } s := strings.Join([]string{`&Metrics{`, `Pids:` + strings.Replace(fmt.Sprintf("%v", this.Pids), "PidsStat", "PidsStat", 1) + `,`, + `CPU:` + strings.Replace(fmt.Sprintf("%v", this.CPU), "CPUStat", "CPUStat", 1) + `,`, `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, `}`, }, "") @@ -269,6 +506,31 @@ func (this *PidsStat) String() string { }, "") return s } +func (this *CPUStat) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&CPUStat{`, + `Usage:` + strings.Replace(fmt.Sprintf("%v", this.Usage), "CPUUsage", "CPUUsage", 1) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s +} +func (this *CPUUsage) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&CPUUsage{`, + `Total:` + fmt.Sprintf("%v", this.Total) + `,`, + `Kernel:` + fmt.Sprintf("%v", this.Kernel) + `,`, + `User:` + fmt.Sprintf("%v", this.User) + `,`, + `PerCPU:` + fmt.Sprintf("%v", this.PerCPU) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s +} func valueToStringMetrics(v interface{}) string { rv := reflect.ValueOf(v) if rv.IsNil() { @@ -342,6 +604,42 @@ func (m *Metrics) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CPU", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthMetrics + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthMetrics + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.CPU == nil { + m.CPU = &CPUStat{} + } + if err := m.CPU.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipMetrics(dAtA[iNdEx:]) @@ -459,6 +757,283 @@ func (m *PidsStat) Unmarshal(dAtA []byte) error { } return nil } +func (m *CPUStat) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: CPUStat: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: CPUStat: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Usage", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthMetrics + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthMetrics + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Usage == nil { + m.Usage = &CPUUsage{} + } + if err := m.Usage.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipMetrics(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthMetrics + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthMetrics + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *CPUUsage) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: CPUUsage: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: CPUUsage: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Total", wireType) + } + m.Total = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Total |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Kernel", wireType) + } + m.Kernel = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Kernel |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 3: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field User", wireType) + } + m.User = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.User |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 4: + if wireType == 0 { + var v uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.PerCPU = append(m.PerCPU, v) + } else if wireType == 2 { + var packedLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + packedLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if packedLen < 0 { + return ErrInvalidLengthMetrics + } + postIndex := iNdEx + packedLen + if postIndex < 0 { + return ErrInvalidLengthMetrics + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + var elementCount int + var count int + for _, integer := range dAtA[iNdEx:postIndex] { + if integer < 128 { + count++ + } + } + elementCount = count + if elementCount != 0 && len(m.PerCPU) == 0 { + m.PerCPU = make([]uint64, 0, elementCount) + } + for iNdEx < postIndex { + var v uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.PerCPU = append(m.PerCPU, v) + } + } else { + return fmt.Errorf("proto: wrong wireType = %d for field PerCPU", wireType) + } + default: + iNdEx = preIndex + skippy, err := skipMetrics(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthMetrics + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthMetrics + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipMetrics(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/v2/stats/metrics.pb.txt b/v2/stats/metrics.pb.txt index 026b1808..d1a93364 100755 --- a/v2/stats/metrics.pb.txt +++ b/v2/stats/metrics.pb.txt @@ -1,6 +1,7 @@ file { name: "github.com/containerd/cgroups/v2/stats/metrics.proto" package: "io.containerd.cgroups.v2" + dependency: "gogoproto/gogo.proto" message_type { name: "Metrics" field { @@ -11,6 +12,17 @@ file { type_name: ".io.containerd.cgroups.v2.PidsStat" json_name: "pids" } + field { + name: "cpu" + number: 2 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".io.containerd.cgroups.v2.CPUStat" + options { + 65004: "CPU" + } + json_name: "cpu" + } } message_type { name: "PidsStat" @@ -29,5 +41,50 @@ file { json_name: "limit" } } + message_type { + name: "CPUStat" + field { + name: "usage" + number: 1 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".io.containerd.cgroups.v2.CPUUsage" + json_name: "usage" + } + } + message_type { + name: "CPUUsage" + field { + name: "total" + number: 1 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "total" + } + field { + name: "kernel" + number: 2 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "kernel" + } + field { + name: "user" + number: 3 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "user" + } + field { + name: "per_cpu" + number: 4 + label: LABEL_REPEATED + type: TYPE_UINT64 + options { + 65004: "PerCPU" + } + json_name: "perCpu" + } + } syntax: "proto3" } diff --git a/v2/stats/metrics.proto b/v2/stats/metrics.proto index 3e1cede6..88fd4bd4 100644 --- a/v2/stats/metrics.proto +++ b/v2/stats/metrics.proto @@ -2,13 +2,27 @@ syntax = "proto3"; package io.containerd.cgroups.v2; -// import "gogoproto/gogo.proto"; + import "gogoproto/gogo.proto"; message Metrics { PidsStat pids = 1; + CPUStat cpu = 2 [(gogoproto.customname) = "CPU"]; } message PidsStat { uint64 current = 1; uint64 limit = 2; } + +message CPUStat { + CPUUsage usage = 1; +} + +message CPUUsage { + // values in nanoseconds + uint64 total = 1; + uint64 kernel = 2; + uint64 user = 3; + repeated uint64 per_cpu = 4 [(gogoproto.customname) = "PerCPU"]; + +} \ No newline at end of file diff --git a/v2/utils.go b/v2/utils.go index 152b3eac..e8910d14 100644 --- a/v2/utils.go +++ b/v2/utils.go @@ -43,6 +43,12 @@ func defaults(unifiedMountpoint string) ([]Subsystem, map[Name]error) { } else { subsystems = append(subsystems, x) } + + if x, err := NewCpu(unifiedMountpoint); err != nil { + unavailables[Cpu] = err + } else { + subsystems = append(subsystems, x) + } return subsystems, unavailables } @@ -88,6 +94,20 @@ func parseCgroupProcsFile(path string) ([]Process, error) { return out, nil } +func parseKV(raw string) (string, uint64, error) { + parts := strings.Fields(raw) + switch len(parts) { + case 2: + v, err := parseUint(parts[1], 10, 64) + if err != nil { + return "", 0, err + } + return parts[0], v, nil + default: + return "", 0, ErrInvalidFormat + } +} + func readUint(path string) (uint64, error) { v, err := ioutil.ReadFile(path) if err != nil {