Skip to content

Commit f4b4f91

Browse files
committed
UPSTREAM: <carry>: stats: handful of optimizations
Signed-off-by: Peter Hunt <[email protected]>
1 parent 0d70dca commit f4b4f91

File tree

2 files changed

+17
-60
lines changed

2 files changed

+17
-60
lines changed

pkg/kubelet/cm/cgroup_manager_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func ParseSystemdToCgroupName(name string) CgroupName {
101101
driverName := path.Base(name)
102102
driverName = strings.TrimSuffix(driverName, systemdSuffix)
103103
parts := strings.Split(driverName, "-")
104-
result := []string{}
104+
result := make([]string, 0, len(parts))
105105
for _, part := range parts {
106106
result = append(result, unescapeSystemdCgroupName(part))
107107
}

pkg/kubelet/stats/cadvisor_stats_provider.go

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,7 @@ func (p *cadvisorStatsProvider) ListPodStats(ctx context.Context) ([]statsapi.Po
9797
filteredInfos, allInfos := filterTerminatedContainerInfoAndAssembleByPodCgroupKey(infos)
9898
// Map each container to a pod and update the PodStats with container data.
9999
podToStats := map[statsapi.PodReference]*statsapi.PodStats{}
100-
for key, cinfo := range filteredInfos {
101-
// On systemd using devicemapper each mount into the container has an
102-
// associated cgroup. We ignore them to ensure we do not get duplicate
103-
// entries in our summary. For details on .mount units:
104-
// http://man7.org/linux/man-pages/man5/systemd.mount.5.html
105-
if strings.HasSuffix(key, ".mount") {
106-
continue
107-
}
108-
// Build the Pod key if this container is managed by a Pod
109-
if !isPodManagedContainer(&cinfo) {
110-
continue
111-
}
100+
for _, cinfo := range filteredInfos {
112101
ref := buildPodRef(cinfo.Spec.Labels)
113102

114103
// Lookup the PodStats for the pod using the PodRef. If none exists,
@@ -190,18 +179,7 @@ func (p *cadvisorStatsProvider) ListPodCPUAndMemoryStats(_ context.Context) ([]s
190179
filteredInfos, allInfos := filterTerminatedContainerInfoAndAssembleByPodCgroupKey(infos)
191180
// Map each container to a pod and update the PodStats with container data.
192181
podToStats := map[statsapi.PodReference]*statsapi.PodStats{}
193-
for key, cinfo := range filteredInfos {
194-
// On systemd using devicemapper each mount into the container has an
195-
// associated cgroup. We ignore them to ensure we do not get duplicate
196-
// entries in our summary. For details on .mount units:
197-
// http://man7.org/linux/man-pages/man5/systemd.mount.5.html
198-
if strings.HasSuffix(key, ".mount") {
199-
continue
200-
}
201-
// Build the Pod key if this container is managed by a Pod
202-
if !isPodManagedContainer(&cinfo) {
203-
continue
204-
}
182+
for _, cinfo := range filteredInfos {
205183
ref := buildPodRef(cinfo.Spec.Labels)
206184

207185
// Lookup the PodStats for the pod using the PodRef. If none exists,
@@ -394,19 +372,27 @@ func filterTerminatedContainerInfoAndAssembleByPodCgroupKey(containerInfo map[st
394372
cinfoMap := make(map[containerID][]containerInfoWithCgroup)
395373
cinfosByPodCgroupKey := make(map[string]cadvisorapiv2.ContainerInfo)
396374
for key, cinfo := range containerInfo {
375+
if !isPodManagedContainer(&cinfo) {
376+
continue
377+
}
378+
397379
var podCgroupKey string
398380
if cm.IsSystemdStyleName(key) {
399381
// Convert to internal cgroup name and take the last component only.
400382
internalCgroupName := cm.ParseSystemdToCgroupName(key)
401383
podCgroupKey = internalCgroupName[len(internalCgroupName)-1]
384+
} else if strings.HasSuffix(key, ".mount") {
385+
// On systemd using devicemapper each mount into the container has an
386+
// associated cgroup. We ignore them to ensure we do not get duplicate
387+
// entries in our summary. For details on .mount units:
388+
// http://man7.org/linux/man-pages/man5/systemd.mount.5.html
389+
continue
402390
} else {
403391
// Take last component only.
404392
podCgroupKey = filepath.Base(key)
405393
}
394+
406395
cinfosByPodCgroupKey[podCgroupKey] = cinfo
407-
if !isPodManagedContainer(&cinfo) {
408-
continue
409-
}
410396
cinfoID := containerID{
411397
podRef: buildPodRef(cinfo.Spec.Labels),
412398
containerName: kubetypes.GetContainerName(cinfo.Spec.Labels),
@@ -419,9 +405,9 @@ func filterTerminatedContainerInfoAndAssembleByPodCgroupKey(containerInfo map[st
419405
result := make(map[string]cadvisorapiv2.ContainerInfo)
420406
for _, refs := range cinfoMap {
421407
if len(refs) == 1 {
422-
// ContainerInfo with no CPU/memory/network usage for uncleaned cgroups of
408+
// ContainerInfo with no CPU/memory usage for uncleaned cgroups of
423409
// already terminated containers, which should not be shown in the results.
424-
if !isContainerTerminated(&refs[0].cinfo) {
410+
if hasMemoryAndCPUInstUsage(&refs[0].cinfo) {
425411
result[refs[0].cgroup] = refs[0].cinfo
426412
}
427413
continue
@@ -477,41 +463,12 @@ func hasMemoryAndCPUInstUsage(info *cadvisorapiv2.ContainerInfo) bool {
477463
if !found {
478464
return false
479465
}
480-
if cstat.CpuInst == nil {
466+
if cstat.CpuInst == nil || cstat.Memory == nil {
481467
return false
482468
}
483469
return cstat.CpuInst.Usage.Total != 0 && cstat.Memory.RSS != 0
484470
}
485471

486-
// isContainerTerminated returns true if the specified container meet one of the following conditions
487-
// 1. info.spec both cpu memory and network are false conditions
488-
// 2. info.Stats both network and cpu or memory are nil
489-
// 3. both zero CPU instantaneous usage zero memory RSS usage and zero network usage,
490-
// and false otherwise.
491-
func isContainerTerminated(info *cadvisorapiv2.ContainerInfo) bool {
492-
if !info.Spec.HasCpu && !info.Spec.HasMemory && !info.Spec.HasNetwork {
493-
return true
494-
}
495-
cstat, found := latestContainerStats(info)
496-
if !found {
497-
return true
498-
}
499-
if cstat.Network != nil {
500-
iStats := cadvisorInfoToNetworkStats(info)
501-
if iStats != nil {
502-
for _, iStat := range iStats.Interfaces {
503-
if *iStat.RxErrors != 0 || *iStat.TxErrors != 0 || *iStat.RxBytes != 0 || *iStat.TxBytes != 0 {
504-
return false
505-
}
506-
}
507-
}
508-
}
509-
if cstat.CpuInst == nil || cstat.Memory == nil {
510-
return true
511-
}
512-
return cstat.CpuInst.Usage.Total == 0 && cstat.Memory.RSS == 0
513-
}
514-
515472
func getCadvisorContainerInfo(ca cadvisor.Interface) (map[string]cadvisorapiv2.ContainerInfo, error) {
516473
infos, err := ca.ContainerInfoV2("/", cadvisorapiv2.RequestOptions{
517474
IdType: cadvisorapiv2.TypeName,

0 commit comments

Comments
 (0)