Skip to content

Commit d21b7a7

Browse files
committed
common/pkg/cgroups: Replace handler interface with stat functions
Replace the controllerHandler interface with a simple statFunc type and convert all handler structs to plain functions. This eliminates unnecessary abstraction since all handlers only implemented a single Stat method. Changes: - Replace controllerHandler interface with statFunc type - Convert linuxBlkioHandler, linuxCPUHandler, linuxMemHandler, and linuxPidHandler structs to blkioStat, cpuStat, memoryStat, and pidsStat functions - Remove init() function and factory methods (getBlkioHandler, etc.) - Simplify handlers map to direct function references - Update Stat() method to call functions directly - Update AvailableControllers signature to use statFunc type Signed-off-by: Lokesh Mandvekar <[email protected]>
1 parent c0c9f9e commit d21b7a7

File tree

5 files changed

+18
-51
lines changed

5 files changed

+18
-51
lines changed

common/pkg/cgroups/blkio_linux.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@ import (
99
"github.com/opencontainers/cgroups"
1010
)
1111

12-
type linuxBlkioHandler struct {
13-
}
14-
15-
func getBlkioHandler() *linuxBlkioHandler {
16-
return &linuxBlkioHandler{}
17-
}
18-
19-
// Stat fills a metrics structure with usage stats for the controller.
20-
func (c *linuxBlkioHandler) Stat(ctr *CgroupControl, m *cgroups.Stats) error {
12+
// blkioStat fills a metrics structure with usage stats for the blkio controller.
13+
func blkioStat(ctr *CgroupControl, m *cgroups.Stats) error {
2114
var ioServiceBytesRecursive []cgroups.BlkioStatEntry
2215

2316
// more details on the io.stat file format:X https://facebookmicrosites.github.io/cgroup2/docs/io-controller.html

common/pkg/cgroups/cgroups_linux.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ type CgroupControl struct {
4242
systemd bool
4343
}
4444

45-
type controllerHandler interface {
46-
Stat(*CgroupControl, *cgroups.Stats) error
47-
}
45+
// statFunc is a function that gathers statistics for a cgroup controller.
46+
type statFunc func(*CgroupControl, *cgroups.Stats) error
4847

4948
const (
5049
cgroupRoot = "/sys/fs/cgroup"
@@ -58,15 +57,11 @@ const (
5857
Blkio = "blkio"
5958
)
6059

61-
var handlers map[string]controllerHandler
62-
63-
func init() {
64-
handlers = map[string]controllerHandler{
65-
CPU: getCPUHandler(),
66-
Memory: getMemoryHandler(),
67-
Pids: getPidsHandler(),
68-
Blkio: getBlkioHandler(),
69-
}
60+
var handlers = map[string]statFunc{
61+
CPU: cpuStat,
62+
Memory: memoryStat,
63+
Pids: pidsStat,
64+
Blkio: blkioStat,
7065
}
7166

7267
// getAvailableControllers get the available controllers.
@@ -95,7 +90,7 @@ func getAvailableControllers() ([]string, error) {
9590
}
9691

9792
// AvailableControllers get string:bool map of all the available controllers.
98-
func AvailableControllers(exclude map[string]controllerHandler) ([]string, error) {
93+
func AvailableControllers(exclude map[string]statFunc) ([]string, error) {
9994
return getAvailableControllers()
10095
}
10196

@@ -312,8 +307,8 @@ func (c *CgroupControl) Update(resources *cgroups.Resources) error {
312307
func (c *CgroupControl) Stat() (*cgroups.Stats, error) {
313308
m := cgroups.Stats{}
314309
found := false
315-
for _, h := range handlers {
316-
if err := h.Stat(c, &m); err != nil {
310+
for _, statFunc := range handlers {
311+
if err := statFunc(c, &m); err != nil {
317312
if !errors.Is(err, os.ErrNotExist) {
318313
return nil, err
319314
}

common/pkg/cgroups/cpu_linux.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ import (
88
"github.com/opencontainers/cgroups"
99
)
1010

11-
type linuxCPUHandler struct {
12-
}
13-
14-
func getCPUHandler() *linuxCPUHandler {
15-
return &linuxCPUHandler{}
16-
}
17-
18-
// Stat fills a metrics structure with usage stats for the controller.
19-
func (c *linuxCPUHandler) Stat(ctr *CgroupControl, m *cgroups.Stats) error {
11+
// cpuStat fills a metrics structure with usage stats for the cpu controller.
12+
func cpuStat(ctr *CgroupControl, m *cgroups.Stats) error {
2013
cpu := cgroups.CpuStats{}
2114
values, err := readCgroup2MapFile(ctr, "cpu.stat")
2215
if err != nil {

common/pkg/cgroups/memory_linux.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ import (
88
"github.com/opencontainers/cgroups"
99
)
1010

11-
type linuxMemHandler struct {
12-
}
13-
14-
func getMemoryHandler() *linuxMemHandler {
15-
return &linuxMemHandler{}
16-
}
17-
18-
// Stat fills a metrics structure with usage stats for the controller.
19-
func (c *linuxMemHandler) Stat(ctr *CgroupControl, m *cgroups.Stats) error {
11+
// memoryStat fills a metrics structure with usage stats for the memory controller.
12+
func memoryStat(ctr *CgroupControl, m *cgroups.Stats) error {
2013
var err error
2114
memUsage := cgroups.MemoryStats{}
2215

common/pkg/cgroups/pids_linux.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ import (
88
"github.com/opencontainers/cgroups"
99
)
1010

11-
type linuxPidHandler struct {
12-
}
13-
14-
func getPidsHandler() *linuxPidHandler {
15-
return &linuxPidHandler{}
16-
}
17-
18-
// Stat fills a metrics structure with usage stats for the controller.
19-
func (c *linuxPidHandler) Stat(ctr *CgroupControl, m *cgroups.Stats) error {
11+
// pidsStat fills a metrics structure with usage stats for the pids controller.
12+
func pidsStat(ctr *CgroupControl, m *cgroups.Stats) error {
2013
if ctr.config.Path == "" {
2114
// nothing we can do to retrieve the pids.current path
2215
return nil

0 commit comments

Comments
 (0)