Skip to content

Commit 58f4859

Browse files
committed
cgroups2/Stats: split out reading cpu, memory, memory events
As outlined in the PR, usage_usec, user_usec, and system_use may always exist, but in situations where the files parsed do not exist, we shouldn't have to try to assign values (which would always be 0). Split out parsing of these to separate functions, so make this more transparent. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent f27d233 commit 58f4859

File tree

1 file changed

+65
-38
lines changed

1 file changed

+65
-38
lines changed

cgroup2/manager.go

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -552,28 +552,63 @@ func (c *Manager) MoveTo(destination *Manager) error {
552552
func (c *Manager) Stat() (*stats.Metrics, error) {
553553
var metrics stats.Metrics
554554

555+
var err error
556+
metrics.CPU, err = readCPUStats(c.path)
557+
if err != nil {
558+
return nil, err
559+
}
560+
561+
metrics.Memory, err = readMemoryStats(c.path)
562+
if err != nil {
563+
return nil, err
564+
}
565+
566+
metrics.MemoryEvents, err = readMemoryEvents(c.path)
567+
if err != nil {
568+
return nil, err
569+
}
570+
571+
metrics.Io = &stats.IOStat{
572+
Usage: readIoStats(c.path),
573+
PSI: getStatPSIFromFile(filepath.Join(c.path, "io.pressure")),
574+
}
575+
metrics.Rdma = &stats.RdmaStat{
576+
Current: rdmaStats(filepath.Join(c.path, "rdma.current")),
577+
Limit: rdmaStats(filepath.Join(c.path, "rdma.max")),
578+
}
579+
metrics.Hugetlb = readHugeTlbStats(c.path)
580+
581+
return &metrics, nil
582+
}
583+
584+
func readCPUStats(cgroupPath string) (*stats.CPUStat, error) {
555585
cpuStat := make(map[string]uint64)
556-
if err := readKVStatsFile(c.path, "cpu.stat", cpuStat); err != nil {
557-
if !os.IsNotExist(err) {
558-
return nil, err
586+
if err := readKVStatsFile(cgroupPath, "cpu.stat", cpuStat); err != nil {
587+
if os.IsNotExist(err) {
588+
return &stats.CPUStat{}, nil
559589
}
590+
return nil, err
560591
}
561-
metrics.CPU = &stats.CPUStat{
592+
return &stats.CPUStat{
562593
UsageUsec: cpuStat["usage_usec"],
563594
UserUsec: cpuStat["user_usec"],
564595
SystemUsec: cpuStat["system_usec"],
565596
NrPeriods: cpuStat["nr_periods"],
566597
NrThrottled: cpuStat["nr_throttled"],
567598
ThrottledUsec: cpuStat["throttled_usec"],
568-
PSI: getStatPSIFromFile(filepath.Join(c.path, "cpu.pressure")),
569-
}
599+
PSI: getStatPSIFromFile(filepath.Join(cgroupPath, "cpu.pressure")),
600+
}, nil
601+
}
602+
603+
func readMemoryStats(cgroupPath string) (*stats.MemoryStat, error) {
570604
memoryStat := make(map[string]uint64, 40)
571-
if err := readKVStatsFile(c.path, "memory.stat", memoryStat); err != nil {
572-
if !os.IsNotExist(err) {
573-
return nil, err
605+
if err := readKVStatsFile(cgroupPath, "memory.stat", memoryStat); err != nil {
606+
if os.IsNotExist(err) {
607+
return &stats.MemoryStat{}, nil
574608
}
609+
return nil, err
575610
}
576-
metrics.Memory = &stats.MemoryStat{
611+
return &stats.MemoryStat{
577612
Anon: memoryStat["anon"],
578613
File: memoryStat["file"],
579614
KernelStack: memoryStat["kernel_stack"],
@@ -605,41 +640,33 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
605640
Pglazyfreed: memoryStat["pglazyfreed"],
606641
ThpFaultAlloc: memoryStat["thp_fault_alloc"],
607642
ThpCollapseAlloc: memoryStat["thp_collapse_alloc"],
608-
Usage: getStatFileContentUint64(filepath.Join(c.path, "memory.current")),
609-
UsageLimit: getStatFileContentUint64(filepath.Join(c.path, "memory.max")),
610-
MaxUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.peak")),
611-
SwapUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.current")),
612-
SwapLimit: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.max")),
613-
SwapMaxUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.peak")),
614-
PSI: getStatPSIFromFile(filepath.Join(c.path, "memory.pressure")),
615-
}
643+
Usage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.current")),
644+
UsageLimit: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.max")),
645+
MaxUsage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.peak")),
646+
SwapUsage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.swap.current")),
647+
SwapLimit: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.swap.max")),
648+
SwapMaxUsage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.swap.peak")),
649+
PSI: getStatPSIFromFile(filepath.Join(cgroupPath, "memory.pressure")),
650+
}, nil
651+
}
616652

653+
func readMemoryEvents(cgroupPath string) (*stats.MemoryEvents, error) {
617654
memoryEvents := make(map[string]uint64)
618-
if err := readKVStatsFile(c.path, "memory.events", memoryEvents); err != nil {
655+
if err := readKVStatsFile(cgroupPath, "memory.events", memoryEvents); err != nil {
619656
if !os.IsNotExist(err) {
620657
return nil, err
621658
}
622659
}
623-
if len(memoryEvents) > 0 {
624-
metrics.MemoryEvents = &stats.MemoryEvents{
625-
Low: memoryEvents["low"],
626-
High: memoryEvents["high"],
627-
Max: memoryEvents["max"],
628-
Oom: memoryEvents["oom"],
629-
OomKill: memoryEvents["oom_kill"],
630-
}
660+
if len(memoryEvents) == 0 {
661+
return nil, nil
631662
}
632-
metrics.Io = &stats.IOStat{
633-
Usage: readIoStats(c.path),
634-
PSI: getStatPSIFromFile(filepath.Join(c.path, "io.pressure")),
635-
}
636-
metrics.Rdma = &stats.RdmaStat{
637-
Current: rdmaStats(filepath.Join(c.path, "rdma.current")),
638-
Limit: rdmaStats(filepath.Join(c.path, "rdma.max")),
639-
}
640-
metrics.Hugetlb = readHugeTlbStats(c.path)
641-
642-
return &metrics, nil
663+
return &stats.MemoryEvents{
664+
Low: memoryEvents["low"],
665+
High: memoryEvents["high"],
666+
Max: memoryEvents["max"],
667+
Oom: memoryEvents["oom"],
668+
OomKill: memoryEvents["oom_kill"],
669+
}, nil
643670
}
644671

645672
func readKVStatsFile(path string, file string, out map[string]uint64) error {

0 commit comments

Comments
 (0)