Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AcceleratorStats to cri_stats_provider #96873

Merged
merged 1 commit into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion pkg/kubelet/stats/cri_stats_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (p *criStatsProvider) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, erro
if !caFound {
klog.V(4).Infof("Unable to find cadvisor stats for %q", containerID)
} else {
p.addCadvisorContainerStats(cs, &caStats)
p.addCadvisorContainerCPUAndMemoryStats(cs, &caStats)
}
ps.Containers = append(ps.Containers, *cs)
}
Expand Down Expand Up @@ -785,6 +785,25 @@ func (p *criStatsProvider) addCadvisorContainerStats(
cs.UserDefinedMetrics = cadvisorInfoToUserDefinedMetrics(caPodStats)
}

cpu, memory := cadvisorInfoToCPUandMemoryStats(caPodStats)
if cpu != nil {
cs.CPU = cpu
}
if memory != nil {
cs.Memory = memory
}
accelerators := cadvisorInfoToAcceleratorStats(caPodStats)
cs.Accelerators = accelerators
}

func (p *criStatsProvider) addCadvisorContainerCPUAndMemoryStats(
cs *statsapi.ContainerStats,
caPodStats *cadvisorapiv2.ContainerInfo,
) {
if caPodStats.Spec.HasCustomMetrics {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely omit custom metrics if we only want CPU and Memory metrics

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it looks like this is just maintaining the existing behavior

cs.UserDefinedMetrics = cadvisorInfoToUserDefinedMetrics(caPodStats)
}

cpu, memory := cadvisorInfoToCPUandMemoryStats(caPodStats)
if cpu != nil {
cs.CPU = cpu
Expand Down
17 changes: 17 additions & 0 deletions pkg/kubelet/stats/cri_stats_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,14 @@ func TestCRIListPodStats(t *testing.T) {
c0 := containerStatsMap[cName0]
assert.Equal(container0.CreatedAt, c0.StartTime.UnixNano())
checkCRICPUAndMemoryStats(assert, c0, infos[container0.ContainerStatus.Id].Stats[0])
checkCRIAcceleratorStats(assert, c0, infos[container0.ContainerStatus.Id].Stats[0])
checkCRIRootfsStats(assert, c0, containerStats0, &imageFsInfo)
checkCRILogsStats(assert, c0, &rootFsInfo, containerLogStats0)

c1 := containerStatsMap[cName1]
assert.Equal(container1.CreatedAt, c1.StartTime.UnixNano())
checkCRICPUAndMemoryStats(assert, c1, infos[container1.ContainerStatus.Id].Stats[0])
checkCRIAcceleratorStats(assert, c1, infos[container1.ContainerStatus.Id].Stats[0])
checkCRIRootfsStats(assert, c1, containerStats1, nil)
checkCRILogsStats(assert, c1, &rootFsInfo, containerLogStats1)
checkCRINetworkStats(assert, p0.Network, infos[sandbox0.PodSandboxStatus.Id].Stats[0].Network)
Expand All @@ -280,6 +283,7 @@ func TestCRIListPodStats(t *testing.T) {
assert.Equal(cName2, c2.Name)
assert.Equal(container2.CreatedAt, c2.StartTime.UnixNano())
checkCRICPUAndMemoryStats(assert, c2, infos[container2.ContainerStatus.Id].Stats[0])
checkCRIAcceleratorStats(assert, c2, infos[container2.ContainerStatus.Id].Stats[0])
checkCRIRootfsStats(assert, c2, containerStats2, &imageFsInfo)
checkCRILogsStats(assert, c2, &rootFsInfo, containerLogStats2)
checkCRINetworkStats(assert, p1.Network, infos[sandbox1.PodSandboxStatus.Id].Stats[0].Network)
Expand All @@ -296,6 +300,7 @@ func TestCRIListPodStats(t *testing.T) {
assert.Equal(cName3, c3.Name)
assert.Equal(container4.CreatedAt, c3.StartTime.UnixNano())
checkCRICPUAndMemoryStats(assert, c3, infos[container4.ContainerStatus.Id].Stats[0])
checkCRIAcceleratorStats(assert, c3, infos[container4.ContainerStatus.Id].Stats[0])
checkCRIRootfsStats(assert, c3, containerStats4, &imageFsInfo)

checkCRILogsStats(assert, c3, &rootFsInfo, containerLogStats4)
Expand Down Expand Up @@ -692,6 +697,18 @@ func checkCRICPUAndMemoryStats(assert *assert.Assertions, actual statsapi.Contai
assert.Equal(cs.Memory.ContainerData.Pgmajfault, *actual.Memory.MajorPageFaults)
}

func checkCRIAcceleratorStats(assert *assert.Assertions, actual statsapi.ContainerStats, cs *cadvisorapiv2.ContainerStats) {
assert.Equal(len(cs.Accelerators), len(actual.Accelerators))
for i := range cs.Accelerators {
assert.Equal(cs.Accelerators[i].Make, actual.Accelerators[i].Make)
assert.Equal(cs.Accelerators[i].Model, actual.Accelerators[i].Model)
assert.Equal(cs.Accelerators[i].ID, actual.Accelerators[i].ID)
assert.Equal(cs.Accelerators[i].MemoryTotal, actual.Accelerators[i].MemoryTotal)
assert.Equal(cs.Accelerators[i].MemoryUsed, actual.Accelerators[i].MemoryUsed)
assert.Equal(cs.Accelerators[i].DutyCycle, actual.Accelerators[i].DutyCycle)
}
}

func checkCRIRootfsStats(assert *assert.Assertions, actual statsapi.ContainerStats, cs *runtimeapi.ContainerStats, imageFsInfo *cadvisorapiv2.FsInfo) {
assert.Equal(cs.WritableLayer.Timestamp, actual.Rootfs.Time.UnixNano())
if imageFsInfo != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/kubelet/stats/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ func cadvisorInfoToContainerCPUAndMemoryStats(name string, info *cadvisorapiv2.C
return result
}

// cadvisorInfoToAcceleratorStats returns the statsapi.AcceleratorStats converted from
// the container info from cadvisor.
func cadvisorInfoToAcceleratorStats(info *cadvisorapiv2.ContainerInfo) []statsapi.AcceleratorStats {
cstat, found := latestContainerStats(info)
if !found || cstat.Accelerators == nil {
return nil
}
var result []statsapi.AcceleratorStats
for _, acc := range cstat.Accelerators {
result = append(result, statsapi.AcceleratorStats{
Make: acc.Make,
Model: acc.Model,
ID: acc.ID,
MemoryTotal: acc.MemoryTotal,
MemoryUsed: acc.MemoryUsed,
DutyCycle: acc.DutyCycle,
})
}
return result
}

func cadvisorInfoToProcessStats(info *cadvisorapiv2.ContainerInfo) *statsapi.ProcessStats {
cstat, found := latestContainerStats(info)
if !found || cstat.Processes == nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/kubelet/stats/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const (
offsetFsTotalUsageBytes
offsetFsBaseUsageBytes
offsetFsInodeUsage
offsetAcceleratorDutyCycle
)

var (
Expand Down Expand Up @@ -499,6 +500,16 @@ func getTestContainerInfo(seed int, podName string, podNamespace string, contain
BaseUsageBytes: &baseUsageBytes,
InodeUsage: &inodeUsage,
},
Accelerators: []cadvisorapiv1.AcceleratorStats{
{
Make: "nvidia",
Model: "Tesla K80",
ID: "foobar",
MemoryTotal: uint64(seed + offsetMemUsageBytes),
MemoryUsed: uint64(seed + offsetMemUsageBytes),
DutyCycle: uint64(seed + offsetAcceleratorDutyCycle),
},
},
}
stats.Cpu.Usage.Total = uint64(seed + offsetCPUUsageCoreSeconds)
stats.CpuInst.Usage.Total = uint64(seed + offsetCPUUsageCores)
Expand Down