Skip to content

Commit 999fdfa

Browse files
committed
Calling hcsshim instead of docker api to get stats for windows to greatly reduce latency
1 parent e8f1269 commit 999fdfa

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

pkg/kubelet/dockershim/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ go_library(
7878
"@io_bazel_rules_go//go/platform:windows": [
7979
"//pkg/kubelet/apis:go_default_library",
8080
"//pkg/kubelet/winstats:go_default_library",
81+
"//vendor/github.com/Microsoft/hcsshim:go_default_library",
8182
"//vendor/golang.org/x/sys/windows/registry:go_default_library",
8283
],
8384
"//conditions:default": [],

pkg/kubelet/dockershim/docker_stats_windows.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"context"
2323
"time"
2424

25+
"github.com/Microsoft/hcsshim"
2526
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
27+
"k8s.io/klog"
2628
)
2729

2830
func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.ContainerStats, error) {
@@ -31,7 +33,18 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont
3133
return nil, err
3234
}
3335

34-
statsJSON, err := ds.client.GetContainerStats(containerID)
36+
hcsshim_container, err := hcsshim.OpenContainer(containerID)
37+
if err != nil {
38+
return nil, err
39+
}
40+
defer func() {
41+
closeErr := hcsshim_container.Close()
42+
if closeErr != nil {
43+
klog.Errorf("Error closing container '%s': %v", containerID, closeErr)
44+
}
45+
}()
46+
47+
stats, err := hcsshim_container.Statistics()
3548
if err != nil {
3649
return nil, err
3750
}
@@ -47,7 +60,6 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont
4760
}
4861
status := statusResp.GetStatus()
4962

50-
dockerStats := statsJSON.Stats
5163
timestamp := time.Now().UnixNano()
5264
containerStats := &runtimeapi.ContainerStats{
5365
Attributes: &runtimeapi.ContainerAttributes{
@@ -58,13 +70,12 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont
5870
},
5971
Cpu: &runtimeapi.CpuUsage{
6072
Timestamp: timestamp,
61-
// have to multiply cpu usage by 100 since docker stats units is in 100's of nano seconds for Windows
62-
// see https://github.com/moby/moby/blob/v1.13.1/api/types/stats.go#L22
63-
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{Value: dockerStats.CPUStats.CPUUsage.TotalUsage * 100},
73+
// have to multiply cpu usage by 100 since stats units is in 100's of nano seconds for Windows
74+
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{Value: stats.Processor.TotalRuntime100ns * 100},
6475
},
6576
Memory: &runtimeapi.MemoryUsage{
6677
Timestamp: timestamp,
67-
WorkingSetBytes: &runtimeapi.UInt64Value{Value: dockerStats.MemoryStats.PrivateWorkingSet},
78+
WorkingSetBytes: &runtimeapi.UInt64Value{Value: stats.Memory.UsagePrivateWorkingSetBytes},
6879
},
6980
WritableLayer: &runtimeapi.FilesystemUsage{
7081
Timestamp: timestamp,

0 commit comments

Comments
 (0)