@@ -22,7 +22,9 @@ import (
22
22
"context"
23
23
"time"
24
24
25
+ "github.com/Microsoft/hcsshim"
25
26
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
27
+ "k8s.io/klog"
26
28
)
27
29
28
30
func (ds * dockerService ) getContainerStats (containerID string ) (* runtimeapi.ContainerStats , error ) {
@@ -31,7 +33,18 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont
31
33
return nil , err
32
34
}
33
35
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 ()
35
48
if err != nil {
36
49
return nil , err
37
50
}
@@ -47,7 +60,6 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont
47
60
}
48
61
status := statusResp .GetStatus ()
49
62
50
- dockerStats := statsJSON .Stats
51
63
timestamp := time .Now ().UnixNano ()
52
64
containerStats := & runtimeapi.ContainerStats {
53
65
Attributes : & runtimeapi.ContainerAttributes {
@@ -58,13 +70,12 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont
58
70
},
59
71
Cpu : & runtimeapi.CpuUsage {
60
72
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 },
64
75
},
65
76
Memory : & runtimeapi.MemoryUsage {
66
77
Timestamp : timestamp ,
67
- WorkingSetBytes : & runtimeapi.UInt64Value {Value : dockerStats . MemoryStats . PrivateWorkingSet },
78
+ WorkingSetBytes : & runtimeapi.UInt64Value {Value : stats . Memory . UsagePrivateWorkingSetBytes },
68
79
},
69
80
WritableLayer : & runtimeapi.FilesystemUsage {
70
81
Timestamp : timestamp ,
0 commit comments