docker_stats_windows.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // +build windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package dockershim
  15. import (
  16. "context"
  17. "time"
  18. "github.com/Microsoft/hcsshim"
  19. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  20. "k8s.io/klog"
  21. )
  22. func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.ContainerStats, error) {
  23. info, err := ds.client.Info()
  24. if err != nil {
  25. return nil, err
  26. }
  27. hcsshim_container, err := hcsshim.OpenContainer(containerID)
  28. if err != nil {
  29. return nil, err
  30. }
  31. defer func() {
  32. closeErr := hcsshim_container.Close()
  33. if closeErr != nil {
  34. klog.Errorf("Error closing container '%s': %v", containerID, closeErr)
  35. }
  36. }()
  37. stats, err := hcsshim_container.Statistics()
  38. if err != nil {
  39. return nil, err
  40. }
  41. containerJSON, err := ds.client.InspectContainerWithSize(containerID)
  42. if err != nil {
  43. return nil, err
  44. }
  45. statusResp, err := ds.ContainerStatus(context.Background(), &runtimeapi.ContainerStatusRequest{ContainerId: containerID})
  46. if err != nil {
  47. return nil, err
  48. }
  49. status := statusResp.GetStatus()
  50. timestamp := time.Now().UnixNano()
  51. containerStats := &runtimeapi.ContainerStats{
  52. Attributes: &runtimeapi.ContainerAttributes{
  53. Id: containerID,
  54. Metadata: status.Metadata,
  55. Labels: status.Labels,
  56. Annotations: status.Annotations,
  57. },
  58. Cpu: &runtimeapi.CpuUsage{
  59. Timestamp: timestamp,
  60. // have to multiply cpu usage by 100 since stats units is in 100's of nano seconds for Windows
  61. UsageCoreNanoSeconds: &runtimeapi.UInt64Value{Value: stats.Processor.TotalRuntime100ns * 100},
  62. },
  63. Memory: &runtimeapi.MemoryUsage{
  64. Timestamp: timestamp,
  65. WorkingSetBytes: &runtimeapi.UInt64Value{Value: stats.Memory.UsagePrivateWorkingSetBytes},
  66. },
  67. WritableLayer: &runtimeapi.FilesystemUsage{
  68. Timestamp: timestamp,
  69. FsId: &runtimeapi.FilesystemIdentifier{Mountpoint: info.DockerRootDir},
  70. UsedBytes: &runtimeapi.UInt64Value{Value: uint64(*containerJSON.SizeRw)},
  71. },
  72. }
  73. return containerStats, nil
  74. }