Skip to content

Commit 77b7464

Browse files
authored
refactor usage nanocores
Signed-off-by: James Sturtevant <[email protected]>
1 parent 9dd6f89 commit 77b7464

4 files changed

Lines changed: 77 additions & 72 deletions

File tree

pkg/cri/server/container_stats_list.go

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ package server
1919
import (
2020
"context"
2121
"fmt"
22+
"time"
2223

2324
tasks "github.com/containerd/containerd/api/services/tasks/v1"
2425
"github.com/containerd/containerd/api/types"
25-
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
26-
2726
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
27+
"github.com/containerd/containerd/pkg/cri/store/stats"
28+
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2829
)
2930

3031
// ListContainerStats returns stats of all running containers.
@@ -115,3 +116,69 @@ func matchLabelSelector(selector, labels map[string]string) bool {
115116
}
116117
return true
117118
}
119+
120+
func (c *criService) getUsageNanoCores(containerID string, isSandbox bool, currentUsageCoreNanoSeconds uint64, currentTimestamp time.Time) (uint64, error) {
121+
var oldStats *stats.ContainerStats
122+
123+
if isSandbox {
124+
sandbox, err := c.sandboxStore.Get(containerID)
125+
if err != nil {
126+
return 0, fmt.Errorf("failed to get sandbox container: %s: %w", containerID, err)
127+
}
128+
oldStats = sandbox.Stats
129+
} else {
130+
container, err := c.containerStore.Get(containerID)
131+
if err != nil {
132+
return 0, fmt.Errorf("failed to get container ID: %s: %w", containerID, err)
133+
}
134+
oldStats = container.Stats
135+
}
136+
137+
if oldStats == nil {
138+
newStats := &stats.ContainerStats{
139+
UsageCoreNanoSeconds: currentUsageCoreNanoSeconds,
140+
Timestamp: currentTimestamp,
141+
}
142+
if isSandbox {
143+
err := c.sandboxStore.UpdateContainerStats(containerID, newStats)
144+
if err != nil {
145+
return 0, fmt.Errorf("failed to update sandbox stats container ID: %s: %w", containerID, err)
146+
}
147+
} else {
148+
err := c.containerStore.UpdateContainerStats(containerID, newStats)
149+
if err != nil {
150+
return 0, fmt.Errorf("failed to update container stats ID: %s: %w", containerID, err)
151+
}
152+
}
153+
return 0, nil
154+
}
155+
156+
nanoSeconds := currentTimestamp.UnixNano() - oldStats.Timestamp.UnixNano()
157+
158+
// zero or negative interval
159+
if nanoSeconds <= 0 {
160+
return 0, nil
161+
}
162+
163+
newUsageNanoCores := uint64(float64(currentUsageCoreNanoSeconds-oldStats.UsageCoreNanoSeconds) /
164+
float64(nanoSeconds) * float64(time.Second/time.Nanosecond))
165+
166+
newStats := &stats.ContainerStats{
167+
UsageCoreNanoSeconds: currentUsageCoreNanoSeconds,
168+
Timestamp: currentTimestamp,
169+
}
170+
if isSandbox {
171+
err := c.sandboxStore.UpdateContainerStats(containerID, newStats)
172+
if err != nil {
173+
return 0, fmt.Errorf("failed to update sandbox container stats: %s: %w", containerID, err)
174+
}
175+
176+
} else {
177+
err := c.containerStore.UpdateContainerStats(containerID, newStats)
178+
if err != nil {
179+
return 0, fmt.Errorf("failed to update container stats ID: %s: %w", containerID, err)
180+
}
181+
}
182+
183+
return newUsageNanoCores, nil
184+
}

pkg/cri/server/container_stats_list_linux.go

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2929

3030
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
31-
stats "github.com/containerd/containerd/pkg/cri/store/stats"
3231
)
3332

3433
func (c *criService) containerMetrics(
@@ -81,72 +80,6 @@ func (c *criService) containerMetrics(
8180
return &cs, nil
8281
}
8382

84-
func (c *criService) getUsageNanoCores(containerID string, isSandbox bool, currentUsageCoreNanoSeconds uint64, currentTimestamp time.Time) (uint64, error) {
85-
var oldStats *stats.ContainerStats
86-
87-
if isSandbox {
88-
sandbox, err := c.sandboxStore.Get(containerID)
89-
if err != nil {
90-
return 0, fmt.Errorf("failed to get sandbox container: %s: %w", containerID, err)
91-
}
92-
oldStats = sandbox.Stats
93-
} else {
94-
container, err := c.containerStore.Get(containerID)
95-
if err != nil {
96-
return 0, fmt.Errorf("failed to get container ID: %s: %w", containerID, err)
97-
}
98-
oldStats = container.Stats
99-
}
100-
101-
if oldStats == nil {
102-
newStats := &stats.ContainerStats{
103-
UsageCoreNanoSeconds: currentUsageCoreNanoSeconds,
104-
Timestamp: currentTimestamp,
105-
}
106-
if isSandbox {
107-
err := c.sandboxStore.UpdateContainerStats(containerID, newStats)
108-
if err != nil {
109-
return 0, fmt.Errorf("failed to update sandbox stats container ID: %s: %w", containerID, err)
110-
}
111-
} else {
112-
err := c.containerStore.UpdateContainerStats(containerID, newStats)
113-
if err != nil {
114-
return 0, fmt.Errorf("failed to update container stats ID: %s: %w", containerID, err)
115-
}
116-
}
117-
return 0, nil
118-
}
119-
120-
nanoSeconds := currentTimestamp.UnixNano() - oldStats.Timestamp.UnixNano()
121-
122-
// zero or negative interval
123-
if nanoSeconds <= 0 {
124-
return 0, nil
125-
}
126-
127-
newUsageNanoCores := uint64(float64(currentUsageCoreNanoSeconds-oldStats.UsageCoreNanoSeconds) /
128-
float64(nanoSeconds) * float64(time.Second/time.Nanosecond))
129-
130-
newStats := &stats.ContainerStats{
131-
UsageCoreNanoSeconds: currentUsageCoreNanoSeconds,
132-
Timestamp: currentTimestamp,
133-
}
134-
if isSandbox {
135-
err := c.sandboxStore.UpdateContainerStats(containerID, newStats)
136-
if err != nil {
137-
return 0, fmt.Errorf("failed to update sandbox container stats: %s: %w", containerID, err)
138-
}
139-
140-
} else {
141-
err := c.containerStore.UpdateContainerStats(containerID, newStats)
142-
if err != nil {
143-
return 0, fmt.Errorf("failed to update container stats ID: %s: %w", containerID, err)
144-
}
145-
}
146-
147-
return newUsageNanoCores, nil
148-
}
149-
15083
// getWorkingSet calculates workingset memory from cgroup memory stats.
15184
// The caller should make sure memory is not nil.
15285
// workingset = usage - total_inactive_file

pkg/cri/server/container_stats_list_windows.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (c *criService) containerMetrics(
4141
inodesUsed = sn.Inodes
4242
}
4343

44-
cs, err := cpuAndMemoryStats(stats)
44+
cs, err := c.cpuAndMemoryStats(meta.ID, stats, false /* isSandbox */)
4545
if err != nil {
4646
return cs, err
4747
}
@@ -64,7 +64,7 @@ func (c *criService) containerMetrics(
6464
return cs, nil
6565
}
6666

67-
func cpuAndMemoryStats(stats *types.Metric) (*runtime.ContainerStats, error) {
67+
func (c *criService) cpuAndMemoryStats(containerId string, stats *types.Metric, isSandbox bool) (*runtime.ContainerStats, error) {
6868
var cs runtime.ContainerStats
6969
if stats != nil {
7070
s, err := typeurl.UnmarshalAny(stats.Data)
@@ -76,9 +76,14 @@ func cpuAndMemoryStats(stats *types.Metric) (*runtime.ContainerStats, error) {
7676
return nil, errors.New("windows stats is empty")
7777
}
7878
if wstats.Processor != nil {
79+
usageNanoCores, err := c.getUsageNanoCores(containerId, isSandbox, wstats.Processor.TotalRuntimeNS, wstats.Timestamp)
80+
if err != nil {
81+
return nil, fmt.Errorf("failed to obtain cpu stats: %w", err)
82+
}
7983
cs.Cpu = &runtime.CpuUsage{
8084
Timestamp: wstats.Timestamp.UnixNano(),
8185
UsageCoreNanoSeconds: &runtime.UInt64Value{Value: wstats.Processor.TotalRuntimeNS},
86+
UsageNanoCores: &runtime.UInt64Value{Value: usageNanoCores},
8287
}
8388
}
8489
if wstats.Memory != nil {

pkg/cri/server/sandbox_stats_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (c *criService) podSandboxStats(ctx context.Context, sandbox sandboxstore.S
5252
}
5353

5454
// cpu and memory
55-
cs, err := cpuAndMemoryStats(metrics)
55+
cs, err := c.cpuAndMemoryStats(meta.ID, metrics, true)
5656
if err != nil {
5757
return nil, fmt.Errorf("failed to extract windows container metrics %s", stats)
5858
}

0 commit comments

Comments
 (0)