Skip to content

Commit 440bb76

Browse files
authored
Merge pull request #268 from sunya-ch/reader-1
handle inactive pods with inactive threshold
2 parents 49cd3dc + ae0d23b commit 440bb76

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

pkg/collector/reader.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type CgroupTime struct {
5454
const (
5555
samplePeriodSec = 3
5656
samplePeriod = samplePeriodSec * 1000 * time.Millisecond
57+
58+
maxInactivePods = 10
5759
)
5860

5961
var (
@@ -197,12 +199,7 @@ func (c *Collector) readBPFEvent() (pidPodName map[uint32]string, containerIDPod
197199
}
198200
}
199201
c.resetBPFTables()
200-
// clean podEnergy
201-
for podName := range podEnergy {
202-
if _, found := foundPod[podName]; !found {
203-
delete(podEnergy, podName)
204-
}
205-
}
202+
handleInactivePods(foundPod)
206203
return pidPodName, containerIDPodName
207204
}
208205

@@ -397,3 +394,20 @@ func getActiveCPUs(cpuTime *[C.CPU_VECTOR_SIZE]uint16) (activeCPUs []int32) {
397394
}
398395
return
399396
}
397+
398+
// handleInactivePods
399+
func handleInactivePods(foundPod map[string]bool) {
400+
numOfInactive := len(podEnergy) - len(foundPod)
401+
if numOfInactive > maxInactivePods {
402+
alivePods, err := podlister.GetAlivePods()
403+
if err != nil {
404+
klog.V(5).Infoln(err)
405+
return
406+
}
407+
for podName := range podEnergy {
408+
if _, found := alivePods[podName]; !found {
409+
delete(podEnergy, podName)
410+
}
411+
}
412+
}
413+
}

pkg/podlister/resolve_container.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ import (
2626
"regexp"
2727
"strings"
2828

29+
"github.com/sustainable-computing-io/kepler/pkg/config"
2930
"golang.org/x/sys/unix"
3031
"k8s.io/klog/v2"
31-
32-
"github.com/sustainable-computing-io/kepler/pkg/config"
3332
)
3433

3534
type ContainerInfo struct {
@@ -284,3 +283,17 @@ func extractPodContainerIDfromPath(path string) (string, error) {
284283
}
285284
return systemProcessName, fmt.Errorf("failed to find pod's container id")
286285
}
286+
287+
// GetAlivePods returns alive pod map
288+
func GetAlivePods() (map[string]bool, error) {
289+
pods, err := podLister.ListPods()
290+
alivePods := make(map[string]bool)
291+
if err != nil {
292+
return alivePods, err
293+
}
294+
for i := 0; i < len(*pods); i++ {
295+
podName := (*pods)[i].Name
296+
alivePods[podName] = true
297+
}
298+
return alivePods, nil
299+
}

0 commit comments

Comments
 (0)