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
Kubelet: add usageNanoCores from CRI stats provider #73659
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"path" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
cadvisorfs "github.com/google/cadvisor/fs" | ||
|
@@ -38,6 +39,11 @@ import ( | |
kubetypes "k8s.io/kubernetes/pkg/kubelet/types" | ||
) | ||
|
||
var ( | ||
// defaultCachePeriod is the default cache period for each cpuUsage. | ||
defaultCachePeriod = 10 * time.Minute | ||
) | ||
|
||
// criStatsProvider implements the containerStatsProvider interface by getting | ||
// the container stats from CRI. | ||
type criStatsProvider struct { | ||
|
@@ -54,6 +60,10 @@ type criStatsProvider struct { | |
imageService internalapi.ImageManagerService | ||
// logMetrics provides the metrics for container logs | ||
logMetricsService LogMetricsService | ||
|
||
// cpuUsageCache caches the cpu usage for containers. | ||
cpuUsageCache map[string]*runtimeapi.CpuUsage | ||
mutex sync.Mutex | ||
} | ||
|
||
// newCRIStatsProvider returns a containerStatsProvider implementation that | ||
|
@@ -71,6 +81,7 @@ func newCRIStatsProvider( | |
runtimeService: runtimeService, | ||
imageService: imageService, | ||
logMetricsService: logMetricsService, | ||
cpuUsageCache: make(map[string]*runtimeapi.CpuUsage), | ||
} | ||
} | ||
|
||
|
@@ -165,6 +176,8 @@ func (p *criStatsProvider) ListPodStats() ([]statsapi.PodStats, error) { | |
} | ||
ps.Containers = append(ps.Containers, *cs) | ||
} | ||
// cleanup outdated caches. | ||
p.cleanupOutdatedCaches() | ||
|
||
result := make([]statsapi.PodStats, 0, len(sandboxIDToPodStats)) | ||
for _, s := range sandboxIDToPodStats { | ||
|
@@ -247,6 +260,8 @@ func (p *criStatsProvider) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, erro | |
} | ||
ps.Containers = append(ps.Containers, *cs) | ||
} | ||
// cleanup outdated caches. | ||
p.cleanupOutdatedCaches() | ||
|
||
result := make([]statsapi.PodStats, 0, len(sandboxIDToPodStats)) | ||
for _, s := range sandboxIDToPodStats { | ||
|
@@ -450,6 +465,11 @@ func (p *criStatsProvider) makeContainerStats( | |
if stats.Cpu.UsageCoreNanoSeconds != nil { | ||
result.CPU.UsageCoreNanoSeconds = &stats.Cpu.UsageCoreNanoSeconds.Value | ||
} | ||
|
||
usageNanoCores := p.getContainerUsageNanoCores(stats) | ||
if usageNanoCores != nil { | ||
result.CPU.UsageNanoCores = usageNanoCores | ||
} | ||
} | ||
if stats.Memory != nil { | ||
result.Memory.Time = metav1.NewTime(time.Unix(0, stats.Memory.Timestamp)) | ||
|
@@ -506,6 +526,11 @@ func (p *criStatsProvider) makeContainerCPUAndMemoryStats( | |
if stats.Cpu.UsageCoreNanoSeconds != nil { | ||
result.CPU.UsageCoreNanoSeconds = &stats.Cpu.UsageCoreNanoSeconds.Value | ||
} | ||
|
||
usageNanoCores := p.getContainerUsageNanoCores(stats) | ||
if usageNanoCores != nil { | ||
result.CPU.UsageNanoCores = usageNanoCores | ||
} | ||
} | ||
if stats.Memory != nil { | ||
result.Memory.Time = metav1.NewTime(time.Unix(0, stats.Memory.Timestamp)) | ||
|
@@ -516,6 +541,44 @@ func (p *criStatsProvider) makeContainerCPUAndMemoryStats( | |
return result | ||
} | ||
|
||
// getContainerUsageNanoCores gets usageNanoCores based on cached usageCoreNanoSeconds. | ||
func (p *criStatsProvider) getContainerUsageNanoCores(stats *runtimeapi.ContainerStats) *uint64 { | ||
if stats == nil || stats.Cpu == nil || stats.Cpu.UsageCoreNanoSeconds == nil { | ||
return nil | ||
} | ||
|
||
p.mutex.Lock() | ||
defer func() { | ||
// Update cache with new value. | ||
p.cpuUsageCache[stats.Attributes.Id] = stats.Cpu | ||
p.mutex.Unlock() | ||
}() | ||
|
||
cached, ok := p.cpuUsageCache[stats.Attributes.Id] | ||
if !ok || cached.UsageCoreNanoSeconds == nil { | ||
return nil | ||
} | ||
|
||
nanoSeconds := stats.Cpu.Timestamp - cached.Timestamp | ||
usageNanoCores := (stats.Cpu.UsageCoreNanoSeconds.Value - cached.UsageCoreNanoSeconds.Value) * uint64(time.Second/time.Nanosecond) / uint64(nanoSeconds) | ||
return &usageNanoCores | ||
} | ||
|
||
func (p *criStatsProvider) cleanupOutdatedCaches() { | ||
p.mutex.Lock() | ||
defer p.mutex.Unlock() | ||
|
||
for k, v := range p.cpuUsageCache { | ||
if v == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stats.Cpu is checked on line 546 above, when would the value be nil ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is for preventing panics, e.g. in tests |
||
delete(p.cpuUsageCache, k) | ||
} | ||
|
||
if time.Since(time.Unix(0, v.Timestamp)) > defaultCachePeriod { | ||
delete(p.cpuUsageCache, k) | ||
} | ||
} | ||
} | ||
|
||
// removeTerminatedContainer returns the specified container but with | ||
// the stats of the terminated containers removed. | ||
func removeTerminatedContainer(containers []*runtimeapi.Container) []*runtimeapi.Container { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there may not be any expired entries for successive calls to cleanupOutdatedCaches, I wonder if the func can return early if it is called within defaultCachePeriod of the previous call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, but that requires more work here, e.g. another property indicating the oldest Timestamp in the cache.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding a property recording the time of last cleanup (so that we don't call this so often) ?