Skip to content

Commit fb1932a

Browse files
kolyshkindims
authored andcommitted
cgroup2: simplify readKVFile
Since all the values that we deal with are of the same type (uint64), let's remove inteface{} and all those typecasts and accessor functions. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent a1771db commit fb1932a

2 files changed

Lines changed: 58 additions & 114 deletions

File tree

cgroup2/manager.go

Lines changed: 54 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
523523
if err != nil {
524524
return nil, err
525525
}
526-
out := make(map[string]interface{})
526+
out := make(map[string]uint64)
527527
for _, controller := range controllers {
528528
switch controller {
529529
case "cpu", "memory":
@@ -535,7 +535,7 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
535535
}
536536
}
537537
}
538-
memoryEvents := make(map[string]interface{})
538+
memoryEvents := make(map[string]uint64)
539539
if err := readKVStatsFile(c.path, "memory.events", memoryEvents); err != nil {
540540
if !os.IsNotExist(err) {
541541
return nil, err
@@ -548,57 +548,57 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
548548
Limit: getStatFileContentUint64(filepath.Join(c.path, "pids.max")),
549549
}
550550
metrics.CPU = &stats.CPUStat{
551-
UsageUsec: getUint64Value("usage_usec", out),
552-
UserUsec: getUint64Value("user_usec", out),
553-
SystemUsec: getUint64Value("system_usec", out),
554-
NrPeriods: getUint64Value("nr_periods", out),
555-
NrThrottled: getUint64Value("nr_throttled", out),
556-
ThrottledUsec: getUint64Value("throttled_usec", out),
551+
UsageUsec: out["usage_usec"],
552+
UserUsec: out["user_usec"],
553+
SystemUsec: out["system_usec"],
554+
NrPeriods: out["nr_periods"],
555+
NrThrottled: out["nr_throttled"],
556+
ThrottledUsec: out["throttled_usec"],
557557
}
558558
metrics.Memory = &stats.MemoryStat{
559-
Anon: getUint64Value("anon", out),
560-
File: getUint64Value("file", out),
561-
KernelStack: getUint64Value("kernel_stack", out),
562-
Slab: getUint64Value("slab", out),
563-
Sock: getUint64Value("sock", out),
564-
Shmem: getUint64Value("shmem", out),
565-
FileMapped: getUint64Value("file_mapped", out),
566-
FileDirty: getUint64Value("file_dirty", out),
567-
FileWriteback: getUint64Value("file_writeback", out),
568-
AnonThp: getUint64Value("anon_thp", out),
569-
InactiveAnon: getUint64Value("inactive_anon", out),
570-
ActiveAnon: getUint64Value("active_anon", out),
571-
InactiveFile: getUint64Value("inactive_file", out),
572-
ActiveFile: getUint64Value("active_file", out),
573-
Unevictable: getUint64Value("unevictable", out),
574-
SlabReclaimable: getUint64Value("slab_reclaimable", out),
575-
SlabUnreclaimable: getUint64Value("slab_unreclaimable", out),
576-
Pgfault: getUint64Value("pgfault", out),
577-
Pgmajfault: getUint64Value("pgmajfault", out),
578-
WorkingsetRefault: getUint64Value("workingset_refault", out),
579-
WorkingsetActivate: getUint64Value("workingset_activate", out),
580-
WorkingsetNodereclaim: getUint64Value("workingset_nodereclaim", out),
581-
Pgrefill: getUint64Value("pgrefill", out),
582-
Pgscan: getUint64Value("pgscan", out),
583-
Pgsteal: getUint64Value("pgsteal", out),
584-
Pgactivate: getUint64Value("pgactivate", out),
585-
Pgdeactivate: getUint64Value("pgdeactivate", out),
586-
Pglazyfree: getUint64Value("pglazyfree", out),
587-
Pglazyfreed: getUint64Value("pglazyfreed", out),
588-
ThpFaultAlloc: getUint64Value("thp_fault_alloc", out),
589-
ThpCollapseAlloc: getUint64Value("thp_collapse_alloc", out),
559+
Anon: out["anon"],
560+
File: out["file"],
561+
KernelStack: out["kernel_stack"],
562+
Slab: out["slab"],
563+
Sock: out["sock"],
564+
Shmem: out["shmem"],
565+
FileMapped: out["file_mapped"],
566+
FileDirty: out["file_dirty"],
567+
FileWriteback: out["file_writeback"],
568+
AnonThp: out["anon_thp"],
569+
InactiveAnon: out["inactive_anon"],
570+
ActiveAnon: out["active_anon"],
571+
InactiveFile: out["inactive_file"],
572+
ActiveFile: out["active_file"],
573+
Unevictable: out["unevictable"],
574+
SlabReclaimable: out["slab_reclaimable"],
575+
SlabUnreclaimable: out["slab_unreclaimable"],
576+
Pgfault: out["pgfault"],
577+
Pgmajfault: out["pgmajfault"],
578+
WorkingsetRefault: out["workingset_refault"],
579+
WorkingsetActivate: out["workingset_activate"],
580+
WorkingsetNodereclaim: out["workingset_nodereclaim"],
581+
Pgrefill: out["pgrefill"],
582+
Pgscan: out["pgscan"],
583+
Pgsteal: out["pgsteal"],
584+
Pgactivate: out["pgactivate"],
585+
Pgdeactivate: out["pgdeactivate"],
586+
Pglazyfree: out["pglazyfree"],
587+
Pglazyfreed: out["pglazyfreed"],
588+
ThpFaultAlloc: out["thp_fault_alloc"],
589+
ThpCollapseAlloc: out["thp_collapse_alloc"],
590590
Usage: getStatFileContentUint64(filepath.Join(c.path, "memory.current")),
591591
UsageLimit: getStatFileContentUint64(filepath.Join(c.path, "memory.max")),
592592
SwapUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.current")),
593593
SwapLimit: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.max")),
594594
}
595595
if len(memoryEvents) > 0 {
596596
metrics.MemoryEvents = &stats.MemoryEvents{
597-
Low: getUint64Value("low", memoryEvents),
598-
High: getUint64Value("high", memoryEvents),
599-
Max: getUint64Value("max", memoryEvents),
600-
Oom: getUint64Value("oom", memoryEvents),
601-
OomKill: getUint64Value("oom_kill", memoryEvents),
597+
Low: memoryEvents["low"],
598+
High: memoryEvents["high"],
599+
Max: memoryEvents["max"],
600+
Oom: memoryEvents["oom"],
601+
OomKill: memoryEvents["oom_kill"],
602602
}
603603
}
604604
metrics.Io = &stats.IOStat{Usage: readIoStats(c.path)}
@@ -611,19 +611,7 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
611611
return &metrics, nil
612612
}
613613

614-
func getUint64Value(key string, out map[string]interface{}) uint64 {
615-
v, ok := out[key]
616-
if !ok {
617-
return 0
618-
}
619-
switch t := v.(type) {
620-
case uint64:
621-
return t
622-
}
623-
return 0
624-
}
625-
626-
func readKVStatsFile(path string, file string, out map[string]interface{}) error {
614+
func readKVStatsFile(path string, file string, out map[string]uint64) error {
627615
f, err := os.Open(filepath.Join(path, file))
628616
if err != nil {
629617
return err
@@ -668,16 +656,12 @@ func (c *Manager) freeze(path string, state State) error {
668656

669657
func (c *Manager) isCgroupEmpty() bool {
670658
// In case of any error we return true so that we exit and don't leak resources
671-
out := make(map[string]interface{})
659+
out := make(map[string]uint64)
672660
if err := readKVStatsFile(c.path, "cgroup.events", out); err != nil {
673661
return true
674662
}
675663
if v, ok := out["populated"]; ok {
676-
populated, ok := v.(uint64)
677-
if !ok {
678-
return true
679-
}
680-
return populated == 0
664+
return v == 0
681665
}
682666
return true
683667
}
@@ -712,41 +696,6 @@ func (c *Manager) EventChan() (<-chan Event, <-chan error) {
712696
return ec, errCh
713697
}
714698

715-
func parseMemoryEvents(out map[string]interface{}) (Event, error) {
716-
e := Event{}
717-
if v, ok := out["high"]; ok {
718-
e.High, ok = v.(uint64)
719-
if !ok {
720-
return Event{}, fmt.Errorf("cannot convert high to uint64: %+v", v)
721-
}
722-
}
723-
if v, ok := out["low"]; ok {
724-
e.Low, ok = v.(uint64)
725-
if !ok {
726-
return Event{}, fmt.Errorf("cannot convert low to uint64: %+v", v)
727-
}
728-
}
729-
if v, ok := out["max"]; ok {
730-
e.Max, ok = v.(uint64)
731-
if !ok {
732-
return Event{}, fmt.Errorf("cannot convert max to uint64: %+v", v)
733-
}
734-
}
735-
if v, ok := out["oom"]; ok {
736-
e.OOM, ok = v.(uint64)
737-
if !ok {
738-
return Event{}, fmt.Errorf("cannot convert oom to uint64: %+v", v)
739-
}
740-
}
741-
if v, ok := out["oom_kill"]; ok {
742-
e.OOMKill, ok = v.(uint64)
743-
if !ok {
744-
return Event{}, fmt.Errorf("cannot convert oom_kill to uint64: %+v", v)
745-
}
746-
}
747-
return e, nil
748-
}
749-
750699
func (c *Manager) waitForEvents(ec chan<- Event, errCh chan<- error) {
751700
defer close(errCh)
752701

@@ -765,20 +714,21 @@ func (c *Manager) waitForEvents(ec chan<- Event, errCh chan<- error) {
765714
return
766715
}
767716
if bytesRead >= unix.SizeofInotifyEvent {
768-
out := make(map[string]interface{})
717+
out := make(map[string]uint64)
769718
if err := readKVStatsFile(c.path, "memory.events", out); err != nil {
770719
// When cgroup is deleted read may return -ENODEV instead of -ENOENT from open.
771720
if _, statErr := os.Lstat(filepath.Join(c.path, "memory.events")); !os.IsNotExist(statErr) {
772721
errCh <- err
773722
}
774723
return
775724
}
776-
e, err := parseMemoryEvents(out)
777-
if err != nil {
778-
errCh <- err
779-
return
725+
ec <- Event{
726+
Low: out["low"],
727+
High: out["high"],
728+
Max: out["max"],
729+
OOM: out["oom"],
730+
OOMKill: out["oom_kill"],
780731
}
781-
ec <- e
782732
if c.isCgroupEmpty() {
783733
return
784734
}

cgroup2/utils.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,13 @@ func parseCgroupProcsFile(path string) ([]uint64, error) {
9292
return out, nil
9393
}
9494

95-
func parseKV(raw string) (string, interface{}, error) {
95+
func parseKV(raw string) (string, uint64, error) {
9696
parts := strings.Fields(raw)
97-
switch len(parts) {
98-
case 2:
99-
v, err := parseUint(parts[1], 10, 64)
100-
if err != nil {
101-
// if we cannot parse as a uint, parse as a string
102-
return parts[0], parts[1], nil
103-
}
104-
return parts[0], v, nil
105-
default:
97+
if len(parts) != 2 {
10698
return "", 0, ErrInvalidFormat
10799
}
100+
v, err := parseUint(parts[1], 10, 64)
101+
return parts[0], v, err
108102
}
109103

110104
func parseUint(s string, base, bitSize int) (uint64, error) {

0 commit comments

Comments
 (0)