Skip to content

Commit 2b607f0

Browse files
committed
container/libcontainer: make schedulerStatsFromProcs a method
Instead of passing a few parameters, make this function a method, to simplify its calling as well as further development. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 8b96dc6 commit 2b607f0

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

container/libcontainer/handler.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,9 @@ func (h *Handler) GetStats() (*info.ContainerStats, error) {
9393
stats := newContainerStats(libcontainerStats, h.includedMetrics)
9494

9595
if h.includedMetrics.Has(container.ProcessSchedulerMetrics) {
96-
pids, err := h.cgroupManager.GetAllPids()
96+
stats.Cpu.Schedstat, err = h.schedulerStatsFromProcs()
9797
if err != nil {
98-
klog.V(4).Infof("Could not get PIDs for container %d: %v", h.pid, err)
99-
} else {
100-
stats.Cpu.Schedstat, err = schedulerStatsFromProcs(h.rootFs, pids, h.pidMetricsCache)
101-
if err != nil {
102-
klog.V(4).Infof("Unable to get Process Scheduler Stats: %v", err)
103-
}
98+
klog.V(4).Infof("Unable to get Process Scheduler Stats: %v", err)
10499
}
105100
}
106101

@@ -314,9 +309,13 @@ func processStatsFromProcs(rootFs string, cgroupPath string, rootPid int) (info.
314309
return processStats, nil
315310
}
316311

317-
func schedulerStatsFromProcs(rootFs string, pids []int, pidMetricsCache map[int]*info.CpuSchedstat) (info.CpuSchedstat, error) {
312+
func (h *Handler) schedulerStatsFromProcs() (info.CpuSchedstat, error) {
313+
pids, err := h.cgroupManager.GetAllPids()
314+
if err != nil {
315+
return info.CpuSchedstat{}, fmt.Errorf("Could not get PIDs for container %d: %w", h.pid, err)
316+
}
318317
for _, pid := range pids {
319-
f, err := os.Open(path.Join(rootFs, "proc", strconv.Itoa(pid), "schedstat"))
318+
f, err := os.Open(path.Join(h.rootFs, "proc", strconv.Itoa(pid), "schedstat"))
320319
if err != nil {
321320
return info.CpuSchedstat{}, fmt.Errorf("couldn't open scheduler statistics for process %d: %v", pid, err)
322321
}
@@ -329,10 +328,10 @@ func schedulerStatsFromProcs(rootFs string, pids []int, pidMetricsCache map[int]
329328
if len(rawMetrics) != 3 {
330329
return info.CpuSchedstat{}, fmt.Errorf("unexpected number of metrics in schedstat file for process %d", pid)
331330
}
332-
cacheEntry, ok := pidMetricsCache[pid]
331+
cacheEntry, ok := h.pidMetricsCache[pid]
333332
if !ok {
334333
cacheEntry = &info.CpuSchedstat{}
335-
pidMetricsCache[pid] = cacheEntry
334+
h.pidMetricsCache[pid] = cacheEntry
336335
}
337336
for i, rawMetric := range rawMetrics {
338337
metric, err := strconv.ParseUint(string(rawMetric), 10, 64)
@@ -350,7 +349,7 @@ func schedulerStatsFromProcs(rootFs string, pids []int, pidMetricsCache map[int]
350349
}
351350
}
352351
schedstats := info.CpuSchedstat{}
353-
for _, v := range pidMetricsCache {
352+
for _, v := range h.pidMetricsCache {
354353
schedstats.RunPeriods += v.RunPeriods
355354
schedstats.RunqueueTime += v.RunqueueTime
356355
schedstats.RunTime += v.RunTime

0 commit comments

Comments
 (0)