From 2843dfa2072234f2b2849be0cae35589c4b19ad2 Mon Sep 17 00:00:00 2001 From: bpopovschi Date: Tue, 5 Nov 2019 16:15:39 +0200 Subject: [PATCH] Added cgroup v2 memory implementation Signed-off-by: bpopovschi --- cmd/cgroups-playground/main.go | 34 ++ v2/cpu.go | 2 +- v2/memory.go | 189 ++++++++ v2/stats/metrics.pb.go | 761 +++++++++++++++++++++++++++++++-- v2/stats/metrics.pb.txt | 84 ++++ v2/stats/metrics.proto | 17 + v2/utils.go | 6 + 7 files changed, 1055 insertions(+), 38 deletions(-) create mode 100644 v2/memory.go diff --git a/cmd/cgroups-playground/main.go b/cmd/cgroups-playground/main.go index 85d6d3a5..534852c0 100644 --- a/cmd/cgroups-playground/main.go +++ b/cmd/cgroups-playground/main.go @@ -79,5 +79,39 @@ func xmain() error { } logrus.Infof("CPU usage stats: usage in kernel mode - %d", stats.CPU.Usage.Kernel) + err = memoryTest(unifiedMountpoint, g) + if err != nil { + return err + } + + return nil +} + +func memoryTest(unifiedMountpoint string, g v2.GroupPath) error { + memoryCgroup, err := v2.NewMemory(unifiedMountpoint) + if err != nil { + return err + } + var limit int64 = 10000 + resources := specs.LinuxResources{ + Memory: &specs.LinuxMemory{Limit: &limit}, + } + err = memoryCgroup.Create(g, &resources) + if err != nil { + return err + } + stats := stats2.Metrics{ + Memory: &stats2.MemoryStat{ + Usage: &stats2.MemoryEntry{}, + }, + } + err = memoryCgroup.Stat(g, &stats) + if err != nil { + return err + } + logrus.Infof("Memory usage stats: usage limit - %d", stats.Memory.Usage.Limit) + logrus.Infof("Memory usage stats: usage - %d", stats.Memory.Usage.Usage) + logrus.Infof("Memory usage stats: cache - %d", stats.Memory.Cache) + return nil } diff --git a/v2/cpu.go b/v2/cpu.go index 0ab4cd3d..bb6275e1 100644 --- a/v2/cpu.go +++ b/v2/cpu.go @@ -119,5 +119,5 @@ func (c *cpuController) Stat(g GroupPath, stats *statsv2.Metrics) error { } func (c *cpuController) Available(g GroupPath) (bool, error) { - return available(c.unifiedMountpoint, g, Pids) + return available(c.unifiedMountpoint, g, Cpu) } diff --git a/v2/memory.go b/v2/memory.go new file mode 100644 index 00000000..1ecb7573 --- /dev/null +++ b/v2/memory.go @@ -0,0 +1,189 @@ +/* + 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" + "strings" + + statsv2 "github.com/containerd/cgroups/v2/stats" + specs "github.com/opencontainers/runtime-spec/specs-go" +) + +// NewMemory returns a Memory controller given the root folder of cgroups. +func NewMemory(unifiedMountpoint string) (*memoryController, error) { + mc := &memoryController{ + unifiedMountpoint: unifiedMountpoint, + } + ok, err := mc.Available("/") + if err != nil { + return nil, err + } + if !ok { + return nil, ErrMemoryNotSupported + } + return mc, nil +} + +type memoryController struct { + unifiedMountpoint string +} + +func (m *memoryController) Name() Name { + return Memory +} + +func (m *memoryController) path(g GroupPath) string { + return filepath.Join(m.unifiedMountpoint, string(g)) +} + +func (m *memoryController) Create(g GroupPath, resources *specs.LinuxResources) error { + if err := os.MkdirAll(m.path(g), defaultDirPerm); err != nil { + return err + } + if resources.Memory == nil { + return nil + } + if resources.Memory.Kernel != nil { + // Check if kernel memory is enabled + // We have to limit the kernel memory here as it won't be accounted at all + // until a limit is set on the cgroup and limit cannot be set once the + // cgroup has children, or if there are already tasks in the cgroup. + for _, i := range []int64{1, -1} { + if err := ioutil.WriteFile( + filepath.Join(m.path(g), "memory.kmem.limit_in_bytes"), + []byte(strconv.FormatInt(i, 10)), + defaultFilePerm, + ); err != nil { + return err + } + } + } + // According to the crun docs v1 cgroups memory.swap can be directly converted to memory.swap_max in v2 + // https://github.com/containers/crun/blob/master/crun.1.md#cgroup-v2 + if mSwap := resources.Memory.Swap; mSwap != nil { + if err := ioutil.WriteFile( + filepath.Join(m.path(g), "memory.swap_max"), + []byte(strconv.FormatInt(*mSwap, 10)), + defaultFilePerm, + ); err != nil { + return err + } + } + + // According to the crun docs v1 cgroups memory.limit can be directly converted to memory.max in v2 + if mMax := resources.Memory.Limit; mMax != nil { + if err := ioutil.WriteFile( + filepath.Join(m.path(g), "memory.max"), + []byte(strconv.FormatInt(*mMax, 10)), + defaultFilePerm, + ); err != nil { + return err + } + } + + // According to the crun docs v1 cgroups memory.reservation can be directly converted to memory.high in v2 + if mHigh := resources.Memory.Reservation; mHigh != nil { + if err := ioutil.WriteFile( + filepath.Join(m.path(g), "memory.high"), + []byte(strconv.FormatInt(*mHigh, 10)), + defaultFilePerm, + ); err != nil { + return err + } + } + return nil +} + +func (m *memoryController) Update(g GroupPath, resources *specs.LinuxResources) error { + return m.Create(g, resources) +} + +func (m *memoryController) Stat(g GroupPath, stats *statsv2.Metrics) error { + f, err := os.Open(filepath.Join(m.path(g), "memory.stat")) + if err != nil { + return err + } + defer f.Close() + stats.Memory = &statsv2.MemoryStat{ + Usage: &statsv2.MemoryEntry{}, + Swap: &statsv2.MemoryEntry{}, + } + 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 + } + if key == "cache" { + stats.Memory.Cache = v + break + } + } + + for _, t := range []struct { + module string + entry *statsv2.MemoryEntry + }{ + { + module: "", + entry: stats.Memory.Usage, + }, + { + module: "memsw", + entry: stats.Memory.Swap, + }, + } { + + for _, tt := range []struct { + name string + value *uint64 + }{ + { + name: "usage_in_bytes", + value: &t.entry.Usage, + }, + { + name: "limit_in_bytes", + value: &t.entry.Limit, + }, + } { + parts := []string{"memory"} + if t.module != "" { + parts = append(parts, t.module) + } + parts = append(parts, tt.name) + v, err := readUint(filepath.Join(m.path(g), strings.Join(parts, "."))) + if err != nil { + return err + } + *tt.value = v + } + } + return nil +} + +func (m *memoryController) Available(g GroupPath) (bool, error) { + return available(m.unifiedMountpoint, g, Memory) +} diff --git a/v2/stats/metrics.pb.go b/v2/stats/metrics.pb.go index 6bd04bac..f05693ae 100644 --- a/v2/stats/metrics.pb.go +++ b/v2/stats/metrics.pb.go @@ -25,11 +25,12 @@ var _ = math.Inf 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:"-"` + Pids *PidsStat `protobuf:"bytes,1,opt,name=pids,proto3" json:"pids,omitempty"` + CPU *CPUStat `protobuf:"bytes,2,opt,name=cpu,proto3" json:"cpu,omitempty"` + Memory *MemoryStat `protobuf:"bytes,4,opt,name=memory,proto3" json:"memory,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Metrics) Reset() { *m = Metrics{} } @@ -186,11 +187,98 @@ func (m *CPUUsage) XXX_DiscardUnknown() { var xxx_messageInfo_CPUUsage proto.InternalMessageInfo +type MemoryStat struct { + Cache uint64 `protobuf:"varint,1,opt,name=cache,proto3" json:"cache,omitempty"` + Usage *MemoryEntry `protobuf:"bytes,2,opt,name=usage,proto3" json:"usage,omitempty"` + Swap *MemoryEntry `protobuf:"bytes,3,opt,name=swap,proto3" json:"swap,omitempty"` + Kernel *MemoryEntry `protobuf:"bytes,4,opt,name=kernel,proto3" json:"kernel,omitempty"` + KernelTCP *MemoryEntry `protobuf:"bytes,5,opt,name=kernel_tcp,json=kernelTcp,proto3" json:"kernel_tcp,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *MemoryStat) Reset() { *m = MemoryStat{} } +func (*MemoryStat) ProtoMessage() {} +func (*MemoryStat) Descriptor() ([]byte, []int) { + return fileDescriptor_2fc6005842049e6b, []int{4} +} +func (m *MemoryStat) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MemoryStat) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MemoryStat.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 *MemoryStat) XXX_Merge(src proto.Message) { + xxx_messageInfo_MemoryStat.Merge(m, src) +} +func (m *MemoryStat) XXX_Size() int { + return m.Size() +} +func (m *MemoryStat) XXX_DiscardUnknown() { + xxx_messageInfo_MemoryStat.DiscardUnknown(m) +} + +var xxx_messageInfo_MemoryStat proto.InternalMessageInfo + +type MemoryEntry struct { + Limit uint64 `protobuf:"varint,1,opt,name=limit,proto3" json:"limit,omitempty"` + Usage uint64 `protobuf:"varint,2,opt,name=usage,proto3" json:"usage,omitempty"` + Max uint64 `protobuf:"varint,3,opt,name=max,proto3" json:"max,omitempty"` + Failcnt uint64 `protobuf:"varint,4,opt,name=failcnt,proto3" json:"failcnt,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *MemoryEntry) Reset() { *m = MemoryEntry{} } +func (*MemoryEntry) ProtoMessage() {} +func (*MemoryEntry) Descriptor() ([]byte, []int) { + return fileDescriptor_2fc6005842049e6b, []int{5} +} +func (m *MemoryEntry) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MemoryEntry) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MemoryEntry.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 *MemoryEntry) XXX_Merge(src proto.Message) { + xxx_messageInfo_MemoryEntry.Merge(m, src) +} +func (m *MemoryEntry) XXX_Size() int { + return m.Size() +} +func (m *MemoryEntry) XXX_DiscardUnknown() { + xxx_messageInfo_MemoryEntry.DiscardUnknown(m) +} + +var xxx_messageInfo_MemoryEntry 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") + proto.RegisterType((*MemoryStat)(nil), "io.containerd.cgroups.v2.MemoryStat") + proto.RegisterType((*MemoryEntry)(nil), "io.containerd.cgroups.v2.MemoryEntry") } func init() { @@ -198,28 +286,38 @@ func init() { } var fileDescriptor_2fc6005842049e6b = []byte{ - // 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,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xe0, 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, + // 482 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x53, 0x4d, 0x6f, 0xd4, 0x30, + 0x10, 0x6d, 0xba, 0xde, 0xa4, 0x9d, 0x0a, 0x09, 0x59, 0x15, 0x8a, 0x38, 0x64, 0x4b, 0x00, 0xa9, + 0xa7, 0x44, 0x5a, 0x10, 0xe2, 0xa3, 0x5c, 0x36, 0xe2, 0x84, 0x2a, 0x45, 0x69, 0xf7, 0x5c, 0xb9, + 0x5e, 0x93, 0x5a, 0x6c, 0x62, 0x63, 0x3b, 0x85, 0xde, 0xf8, 0x45, 0xfc, 0x03, 0xee, 0x3d, 0x72, + 0xe4, 0xb4, 0xa2, 0xf9, 0x25, 0xc8, 0x4e, 0xc2, 0xf6, 0xb2, 0x2a, 0xbd, 0xcd, 0x58, 0xef, 0xcd, + 0x9b, 0xf7, 0x32, 0x81, 0x97, 0x25, 0x37, 0x17, 0xcd, 0x79, 0x42, 0x45, 0x95, 0x52, 0x51, 0x1b, + 0xc2, 0x6b, 0xa6, 0x16, 0x29, 0x2d, 0x95, 0x68, 0xa4, 0x4e, 0x2f, 0xa7, 0xa9, 0x36, 0xc4, 0xe8, + 0xb4, 0x62, 0x46, 0x71, 0xaa, 0x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x00, } func (m *Metrics) Marshal() (dAtA []byte, err error) { @@ -257,6 +355,16 @@ func (m *Metrics) MarshalTo(dAtA []byte) (int, error) { } i += n2 } + if m.Memory != nil { + dAtA[i] = 0x22 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Memory.Size())) + n3, err := m.Memory.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n3 + } if m.XXX_unrecognized != nil { i += copy(dAtA[i:], m.XXX_unrecognized) } @@ -313,11 +421,11 @@ func (m *CPUStat) MarshalTo(dAtA []byte) (int, error) { dAtA[i] = 0xa i++ i = encodeVarintMetrics(dAtA, i, uint64(m.Usage.Size())) - n3, err := m.Usage.MarshalTo(dAtA[i:]) + n4, err := m.Usage.MarshalTo(dAtA[i:]) if err != nil { return 0, err } - i += n3 + i += n4 } if m.XXX_unrecognized != nil { i += copy(dAtA[i:], m.XXX_unrecognized) @@ -356,21 +464,128 @@ func (m *CPUUsage) MarshalTo(dAtA []byte) (int, error) { i = encodeVarintMetrics(dAtA, i, uint64(m.User)) } if len(m.PerCPU) > 0 { - dAtA5 := make([]byte, len(m.PerCPU)*10) - var j4 int + dAtA6 := make([]byte, len(m.PerCPU)*10) + var j5 int for _, num := range m.PerCPU { for num >= 1<<7 { - dAtA5[j4] = uint8(uint64(num)&0x7f | 0x80) + dAtA6[j5] = uint8(uint64(num)&0x7f | 0x80) num >>= 7 - j4++ + j5++ } - dAtA5[j4] = uint8(num) - j4++ + dAtA6[j5] = uint8(num) + j5++ + } + dAtA[i] = 0x22 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(j5)) + i += copy(dAtA[i:], dAtA6[:j5]) + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *MemoryStat) 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 *MemoryStat) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Cache != 0 { + dAtA[i] = 0x8 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Cache)) + } + if m.Usage != nil { + dAtA[i] = 0x12 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Usage.Size())) + n7, err := m.Usage.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err } + i += n7 + } + if m.Swap != nil { + dAtA[i] = 0x1a + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Swap.Size())) + n8, err := m.Swap.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n8 + } + if m.Kernel != nil { dAtA[i] = 0x22 i++ - i = encodeVarintMetrics(dAtA, i, uint64(j4)) - i += copy(dAtA[i:], dAtA5[:j4]) + i = encodeVarintMetrics(dAtA, i, uint64(m.Kernel.Size())) + n9, err := m.Kernel.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n9 + } + if m.KernelTCP != nil { + dAtA[i] = 0x2a + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.KernelTCP.Size())) + n10, err := m.KernelTCP.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n10 + } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) + } + return i, nil +} + +func (m *MemoryEntry) 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 *MemoryEntry) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if m.Limit != 0 { + dAtA[i] = 0x8 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Limit)) + } + if m.Usage != 0 { + dAtA[i] = 0x10 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Usage)) + } + if m.Max != 0 { + dAtA[i] = 0x18 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Max)) + } + if m.Failcnt != 0 { + dAtA[i] = 0x20 + i++ + i = encodeVarintMetrics(dAtA, i, uint64(m.Failcnt)) } if m.XXX_unrecognized != nil { i += copy(dAtA[i:], m.XXX_unrecognized) @@ -401,6 +616,10 @@ func (m *Metrics) Size() (n int) { l = m.CPU.Size() n += 1 + l + sovMetrics(uint64(l)) } + if m.Memory != nil { + l = m.Memory.Size() + n += 1 + l + sovMetrics(uint64(l)) + } if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } @@ -469,6 +688,61 @@ func (m *CPUUsage) Size() (n int) { return n } +func (m *MemoryStat) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Cache != 0 { + n += 1 + sovMetrics(uint64(m.Cache)) + } + if m.Usage != nil { + l = m.Usage.Size() + n += 1 + l + sovMetrics(uint64(l)) + } + if m.Swap != nil { + l = m.Swap.Size() + n += 1 + l + sovMetrics(uint64(l)) + } + if m.Kernel != nil { + l = m.Kernel.Size() + n += 1 + l + sovMetrics(uint64(l)) + } + if m.KernelTCP != nil { + l = m.KernelTCP.Size() + n += 1 + l + sovMetrics(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *MemoryEntry) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Limit != 0 { + n += 1 + sovMetrics(uint64(m.Limit)) + } + if m.Usage != 0 { + n += 1 + sovMetrics(uint64(m.Usage)) + } + if m.Max != 0 { + n += 1 + sovMetrics(uint64(m.Max)) + } + if m.Failcnt != 0 { + n += 1 + sovMetrics(uint64(m.Failcnt)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + func sovMetrics(x uint64) (n int) { for { n++ @@ -489,6 +763,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) + `,`, + `Memory:` + strings.Replace(fmt.Sprintf("%v", this.Memory), "MemoryStat", "MemoryStat", 1) + `,`, `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, `}`, }, "") @@ -531,6 +806,35 @@ func (this *CPUUsage) String() string { }, "") return s } +func (this *MemoryStat) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&MemoryStat{`, + `Cache:` + fmt.Sprintf("%v", this.Cache) + `,`, + `Usage:` + strings.Replace(fmt.Sprintf("%v", this.Usage), "MemoryEntry", "MemoryEntry", 1) + `,`, + `Swap:` + strings.Replace(fmt.Sprintf("%v", this.Swap), "MemoryEntry", "MemoryEntry", 1) + `,`, + `Kernel:` + strings.Replace(fmt.Sprintf("%v", this.Kernel), "MemoryEntry", "MemoryEntry", 1) + `,`, + `KernelTCP:` + strings.Replace(fmt.Sprintf("%v", this.KernelTCP), "MemoryEntry", "MemoryEntry", 1) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s +} +func (this *MemoryEntry) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&MemoryEntry{`, + `Limit:` + fmt.Sprintf("%v", this.Limit) + `,`, + `Usage:` + fmt.Sprintf("%v", this.Usage) + `,`, + `Max:` + fmt.Sprintf("%v", this.Max) + `,`, + `Failcnt:` + fmt.Sprintf("%v", this.Failcnt) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s +} func valueToStringMetrics(v interface{}) string { rv := reflect.ValueOf(v) if rv.IsNil() { @@ -640,6 +944,42 @@ func (m *Metrics) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Memory", 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.Memory == nil { + m.Memory = &MemoryStat{} + } + if err := m.Memory.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipMetrics(dAtA[iNdEx:]) @@ -1034,6 +1374,353 @@ func (m *CPUUsage) Unmarshal(dAtA []byte) error { } return nil } +func (m *MemoryStat) 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: MemoryStat: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MemoryStat: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Cache", wireType) + } + m.Cache = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Cache |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + 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 = &MemoryEntry{} + } + if err := m.Usage.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Swap", 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.Swap == nil { + m.Swap = &MemoryEntry{} + } + if err := m.Swap.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Kernel", 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.Kernel == nil { + m.Kernel = &MemoryEntry{} + } + if err := m.Kernel.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field KernelTCP", 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.KernelTCP == nil { + m.KernelTCP = &MemoryEntry{} + } + if err := m.KernelTCP.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 *MemoryEntry) 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: MemoryEntry: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MemoryEntry: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Limit", wireType) + } + m.Limit = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Limit |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Usage", wireType) + } + m.Usage = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Usage |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 3: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Max", wireType) + } + m.Max = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Max |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Failcnt", wireType) + } + m.Failcnt = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMetrics + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Failcnt |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + 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 d1a93364..381e8f99 100755 --- a/v2/stats/metrics.pb.txt +++ b/v2/stats/metrics.pb.txt @@ -23,6 +23,14 @@ file { } json_name: "cpu" } + field { + name: "memory" + number: 4 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".io.containerd.cgroups.v2.MemoryStat" + json_name: "memory" + } } message_type { name: "PidsStat" @@ -86,5 +94,81 @@ file { json_name: "perCpu" } } + message_type { + name: "MemoryStat" + field { + name: "cache" + number: 1 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "cache" + } + field { + name: "usage" + number: 2 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".io.containerd.cgroups.v2.MemoryEntry" + json_name: "usage" + } + field { + name: "swap" + number: 3 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".io.containerd.cgroups.v2.MemoryEntry" + json_name: "swap" + } + field { + name: "kernel" + number: 4 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".io.containerd.cgroups.v2.MemoryEntry" + json_name: "kernel" + } + field { + name: "kernel_tcp" + number: 5 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".io.containerd.cgroups.v2.MemoryEntry" + options { + 65004: "KernelTCP" + } + json_name: "kernelTcp" + } + } + message_type { + name: "MemoryEntry" + field { + name: "limit" + number: 1 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "limit" + } + field { + name: "usage" + number: 2 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "usage" + } + field { + name: "max" + number: 3 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "max" + } + field { + name: "failcnt" + number: 4 + label: LABEL_OPTIONAL + type: TYPE_UINT64 + json_name: "failcnt" + } + } syntax: "proto3" } diff --git a/v2/stats/metrics.proto b/v2/stats/metrics.proto index 88fd4bd4..a3fc0faa 100644 --- a/v2/stats/metrics.proto +++ b/v2/stats/metrics.proto @@ -7,6 +7,7 @@ package io.containerd.cgroups.v2; message Metrics { PidsStat pids = 1; CPUStat cpu = 2 [(gogoproto.customname) = "CPU"]; + MemoryStat memory = 4; } message PidsStat { @@ -25,4 +26,20 @@ message CPUUsage { uint64 user = 3; repeated uint64 per_cpu = 4 [(gogoproto.customname) = "PerCPU"]; +} + +message MemoryStat { + uint64 cache = 1; + MemoryEntry usage = 2; + MemoryEntry swap = 3; + MemoryEntry kernel = 4; + MemoryEntry kernel_tcp = 5 [(gogoproto.customname) = "KernelTCP"]; + +} + +message MemoryEntry { + uint64 limit = 1; + uint64 usage = 2; + uint64 max = 3; + uint64 failcnt = 4; } \ No newline at end of file diff --git a/v2/utils.go b/v2/utils.go index e8910d14..cc648fb5 100644 --- a/v2/utils.go +++ b/v2/utils.go @@ -49,6 +49,12 @@ func defaults(unifiedMountpoint string) ([]Subsystem, map[Name]error) { } else { subsystems = append(subsystems, x) } + + if x, err := NewMemory(unifiedMountpoint); err != nil { + unavailables[Memory] = err + } else { + subsystems = append(subsystems, x) + } return subsystems, unavailables }