Skip to content

Commit f2e88f3

Browse files
thaJeztahJackson McKay
authored andcommitted
cgroups2/Stats: split out reading cpu, memory, memory events (#1)
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 <github@gone.nl>
1 parent 68d5d0c commit f2e88f3

File tree

1 file changed

+73
-44
lines changed

1 file changed

+73
-44
lines changed

cgroup2/manager.go

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -560,36 +560,73 @@ func (c *Manager) MoveTo(destination *Manager) error {
560560

561561
func (c *Manager) Stat() (*stats.Metrics, error) {
562562
var metrics stats.Metrics
563+
var err error
563564

565+
metrics.Pids = &stats.PidsStat{
566+
Current: getStatFileContentUint64(filepath.Join(c.path, "pids.current")),
567+
Limit: getStatFileContentUint64(filepath.Join(c.path, "pids.max")),
568+
}
569+
570+
metrics.CPU, err = readCPUStats(c.path)
571+
if err != nil {
572+
return nil, err
573+
}
574+
575+
metrics.Memory, err = readMemoryStats(c.path)
576+
if err != nil {
577+
return nil, err
578+
}
579+
580+
metrics.MemoryEvents, err = readMemoryEvents(c.path)
581+
if err != nil {
582+
return nil, err
583+
}
584+
585+
metrics.Io = &stats.IOStat{
586+
Usage: readIoStats(c.path),
587+
PSI: getStatPSIFromFile(filepath.Join(c.path, "io.pressure")),
588+
}
589+
590+
metrics.Rdma = &stats.RdmaStat{
591+
Current: rdmaStats(filepath.Join(c.path, "rdma.current")),
592+
Limit: rdmaStats(filepath.Join(c.path, "rdma.max")),
593+
}
594+
595+
metrics.Hugetlb = readHugeTlbStats(c.path)
596+
597+
return &metrics, nil
598+
}
599+
600+
func readCPUStats(cgroupPath string) (*stats.CPUStat, error) {
564601
cpuStat := make(map[string]uint64)
565-
if err := readKVStatsFile(c.path, "cpu.stat", cpuStat); err != nil {
566-
if !os.IsNotExist(err) {
567-
return nil, err
602+
if err := readKVStatsFile(cgroupPath, "cpu.stat", cpuStat); err != nil {
603+
if os.IsNotExist(err) {
604+
return &stats.CPUStat{}, nil
568605
}
606+
return nil, err
569607
}
570-
metrics.CPU = &stats.CPUStat{
608+
return &stats.CPUStat{
571609
UsageUsec: cpuStat["usage_usec"],
572610
UserUsec: cpuStat["user_usec"],
573611
SystemUsec: cpuStat["system_usec"],
574612
NrPeriods: cpuStat["nr_periods"],
575613
NrThrottled: cpuStat["nr_throttled"],
576614
ThrottledUsec: cpuStat["throttled_usec"],
577-
PSI: getStatPSIFromFile(filepath.Join(c.path, "cpu.pressure")),
578-
}
579-
if nr_bursts, ok := cpuStat["nr_bursts"]; ok {
580-
metrics.CPU.NrBursts = nr_bursts
581-
}
582-
if burst_usec, ok := cpuStat["burst_usec"]; ok {
583-
metrics.CPU.BurstUsec = burst_usec
584-
}
615+
NrBursts: cpuStat["nr_bursts"],
616+
BurstUsec: cpuStat["burst_usec"],
617+
PSI: getStatPSIFromFile(filepath.Join(cgroupPath, "cpu.pressure")),
618+
}, nil
619+
}
585620

621+
func readMemoryStats(cgroupPath string) (*stats.MemoryStat, error) {
586622
memoryStat := make(map[string]uint64, 40)
587-
if err := readKVStatsFile(c.path, "memory.stat", memoryStat); err != nil {
588-
if !os.IsNotExist(err) {
589-
return nil, err
623+
if err := readKVStatsFile(cgroupPath, "memory.stat", memoryStat); err != nil {
624+
if os.IsNotExist(err) {
625+
return &stats.MemoryStat{}, nil
590626
}
627+
return nil, err
591628
}
592-
metrics.Memory = &stats.MemoryStat{
629+
return &stats.MemoryStat{
593630
Anon: memoryStat["anon"],
594631
File: memoryStat["file"],
595632
KernelStack: memoryStat["kernel_stack"],
@@ -621,41 +658,33 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
621658
Pglazyfreed: memoryStat["pglazyfreed"],
622659
ThpFaultAlloc: memoryStat["thp_fault_alloc"],
623660
ThpCollapseAlloc: memoryStat["thp_collapse_alloc"],
624-
Usage: getStatFileContentUint64(filepath.Join(c.path, "memory.current")),
625-
UsageLimit: getStatFileContentUint64(filepath.Join(c.path, "memory.max")),
626-
MaxUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.peak")),
627-
SwapUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.current")),
628-
SwapLimit: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.max")),
629-
SwapMaxUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.peak")),
630-
PSI: getStatPSIFromFile(filepath.Join(c.path, "memory.pressure")),
631-
}
661+
Usage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.current")),
662+
UsageLimit: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.max")),
663+
MaxUsage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.peak")),
664+
SwapUsage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.swap.current")),
665+
SwapLimit: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.swap.max")),
666+
SwapMaxUsage: getStatFileContentUint64(filepath.Join(cgroupPath, "memory.swap.peak")),
667+
PSI: getStatPSIFromFile(filepath.Join(cgroupPath, "memory.pressure")),
668+
}, nil
669+
}
632670

671+
func readMemoryEvents(cgroupPath string) (*stats.MemoryEvents, error) {
633672
memoryEvents := make(map[string]uint64)
634-
if err := readKVStatsFile(c.path, "memory.events", memoryEvents); err != nil {
673+
if err := readKVStatsFile(cgroupPath, "memory.events", memoryEvents); err != nil {
635674
if !os.IsNotExist(err) {
636675
return nil, err
637676
}
638677
}
639-
if len(memoryEvents) > 0 {
640-
metrics.MemoryEvents = &stats.MemoryEvents{
641-
Low: memoryEvents["low"],
642-
High: memoryEvents["high"],
643-
Max: memoryEvents["max"],
644-
Oom: memoryEvents["oom"],
645-
OomKill: memoryEvents["oom_kill"],
646-
}
647-
}
648-
metrics.Io = &stats.IOStat{
649-
Usage: readIoStats(c.path),
650-
PSI: getStatPSIFromFile(filepath.Join(c.path, "io.pressure")),
651-
}
652-
metrics.Rdma = &stats.RdmaStat{
653-
Current: rdmaStats(filepath.Join(c.path, "rdma.current")),
654-
Limit: rdmaStats(filepath.Join(c.path, "rdma.max")),
678+
if len(memoryEvents) == 0 {
679+
return nil, nil
655680
}
656-
metrics.Hugetlb = readHugeTlbStats(c.path)
657-
658-
return &metrics, nil
681+
return &stats.MemoryEvents{
682+
Low: memoryEvents["low"],
683+
High: memoryEvents["high"],
684+
Max: memoryEvents["max"],
685+
Oom: memoryEvents["oom"],
686+
OomKill: memoryEvents["oom_kill"],
687+
}, nil
659688
}
660689

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

0 commit comments

Comments
 (0)