@@ -93,14 +93,9 @@ func (h *Handler) GetStats() (*info.ContainerStats, error) {
93
93
stats := newContainerStats (libcontainerStats , h .includedMetrics )
94
94
95
95
if h .includedMetrics .Has (container .ProcessSchedulerMetrics ) {
96
- pids , err : = h .cgroupManager . GetAllPids ()
96
+ stats . Cpu . Schedstat , err = h .schedulerStatsFromProcs ()
97
97
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 )
104
99
}
105
100
}
106
101
@@ -314,9 +309,13 @@ func processStatsFromProcs(rootFs string, cgroupPath string, rootPid int) (info.
314
309
return processStats , nil
315
310
}
316
311
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
+ }
318
317
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" ))
320
319
if err != nil {
321
320
return info.CpuSchedstat {}, fmt .Errorf ("couldn't open scheduler statistics for process %d: %v" , pid , err )
322
321
}
@@ -329,10 +328,10 @@ func schedulerStatsFromProcs(rootFs string, pids []int, pidMetricsCache map[int]
329
328
if len (rawMetrics ) != 3 {
330
329
return info.CpuSchedstat {}, fmt .Errorf ("unexpected number of metrics in schedstat file for process %d" , pid )
331
330
}
332
- cacheEntry , ok := pidMetricsCache [pid ]
331
+ cacheEntry , ok := h . pidMetricsCache [pid ]
333
332
if ! ok {
334
333
cacheEntry = & info.CpuSchedstat {}
335
- pidMetricsCache [pid ] = cacheEntry
334
+ h . pidMetricsCache [pid ] = cacheEntry
336
335
}
337
336
for i , rawMetric := range rawMetrics {
338
337
metric , err := strconv .ParseUint (string (rawMetric ), 10 , 64 )
@@ -350,7 +349,7 @@ func schedulerStatsFromProcs(rootFs string, pids []int, pidMetricsCache map[int]
350
349
}
351
350
}
352
351
schedstats := info.CpuSchedstat {}
353
- for _ , v := range pidMetricsCache {
352
+ for _ , v := range h . pidMetricsCache {
354
353
schedstats .RunPeriods += v .RunPeriods
355
354
schedstats .RunqueueTime += v .RunqueueTime
356
355
schedstats .RunTime += v .RunTime
0 commit comments