perfcounters.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 winstats
  15. import (
  16. "errors"
  17. "fmt"
  18. "time"
  19. "unsafe"
  20. "github.com/JeffAshton/win_pdh"
  21. )
  22. const (
  23. cpuQuery = "\\Processor(_Total)\\% Processor Time"
  24. memoryPrivWorkingSetQuery = "\\Process(_Total)\\Working Set - Private"
  25. memoryCommittedBytesQuery = "\\Memory\\Committed Bytes"
  26. // Perf counters are updated every second. This is the same as the default cadvisor collection period
  27. // see https://github.com/google/cadvisor/blob/master/docs/runtime_options.md#housekeeping
  28. perfCounterUpdatePeriod = 1 * time.Second
  29. // defaultCachePeriod is the default cache period for each cpuUsage.
  30. // This matches with the cadvisor setting and the time interval we use for containers.
  31. // see https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/cadvisor/cadvisor_linux.go#L63
  32. defaultCachePeriod = 10 * time.Second
  33. )
  34. type perfCounter struct {
  35. queryHandle win_pdh.PDH_HQUERY
  36. counterHandle win_pdh.PDH_HCOUNTER
  37. }
  38. func newPerfCounter(counter string) (*perfCounter, error) {
  39. var queryHandle win_pdh.PDH_HQUERY
  40. var counterHandle win_pdh.PDH_HCOUNTER
  41. ret := win_pdh.PdhOpenQuery(0, 0, &queryHandle)
  42. if ret != win_pdh.ERROR_SUCCESS {
  43. return nil, errors.New("unable to open query through DLL call")
  44. }
  45. ret = win_pdh.PdhAddEnglishCounter(queryHandle, counter, 0, &counterHandle)
  46. if ret != win_pdh.ERROR_SUCCESS {
  47. return nil, fmt.Errorf("unable to add process counter. Error code is %x", ret)
  48. }
  49. ret = win_pdh.PdhCollectQueryData(queryHandle)
  50. if ret != win_pdh.ERROR_SUCCESS {
  51. return nil, fmt.Errorf("unable to collect data from counter. Error code is %x", ret)
  52. }
  53. return &perfCounter{
  54. queryHandle: queryHandle,
  55. counterHandle: counterHandle,
  56. }, nil
  57. }
  58. // getData is used for getting data without * in counter name.
  59. func (p *perfCounter) getData() (uint64, error) {
  60. ret := win_pdh.PdhCollectQueryData(p.queryHandle)
  61. if ret != win_pdh.ERROR_SUCCESS {
  62. return 0, fmt.Errorf("unable to collect data from counter. Error code is %x", ret)
  63. }
  64. var bufSize, bufCount uint32
  65. var size = uint32(unsafe.Sizeof(win_pdh.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE{}))
  66. var emptyBuf [1]win_pdh.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE // need at least 1 addressable null ptr.
  67. var data uint64
  68. ret = win_pdh.PdhGetFormattedCounterArrayDouble(p.counterHandle, &bufSize, &bufCount, &emptyBuf[0])
  69. if ret != win_pdh.PDH_MORE_DATA {
  70. return 0, fmt.Errorf("unable to collect data from counter. Error code is %x", ret)
  71. }
  72. filledBuf := make([]win_pdh.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE, bufCount*size)
  73. ret = win_pdh.PdhGetFormattedCounterArrayDouble(p.counterHandle, &bufSize, &bufCount, &filledBuf[0])
  74. if ret != win_pdh.ERROR_SUCCESS {
  75. return 0, fmt.Errorf("unable to collect data from counter. Error code is %x", ret)
  76. }
  77. for i := 0; i < int(bufCount); i++ {
  78. c := filledBuf[i]
  79. data = uint64(c.FmtValue.DoubleValue)
  80. }
  81. return data, nil
  82. }
  83. // getData is used for getting data with * in counter name.
  84. func (p *perfCounter) getDataList() (map[string]uint64, error) {
  85. ret := win_pdh.PdhCollectQueryData(p.queryHandle)
  86. if ret != win_pdh.ERROR_SUCCESS {
  87. return nil, fmt.Errorf("unable to collect data from counter. Error code is %x", ret)
  88. }
  89. var bufSize, bufCount uint32
  90. var size = uint32(unsafe.Sizeof(win_pdh.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE{}))
  91. var emptyBuf [1]win_pdh.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE // need at least 1 addressable null ptr.
  92. data := map[string]uint64{}
  93. ret = win_pdh.PdhGetFormattedCounterArrayDouble(p.counterHandle, &bufSize, &bufCount, &emptyBuf[0])
  94. if ret != win_pdh.PDH_MORE_DATA {
  95. return nil, fmt.Errorf("unable to collect data from counter. Error code is %x", ret)
  96. }
  97. filledBuf := make([]win_pdh.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE, bufCount*size)
  98. ret = win_pdh.PdhGetFormattedCounterArrayDouble(p.counterHandle, &bufSize, &bufCount, &filledBuf[0])
  99. if ret != win_pdh.ERROR_SUCCESS {
  100. return nil, fmt.Errorf("unable to collect data from counter. Error code is %x", ret)
  101. }
  102. for i := 0; i < int(bufCount); i++ {
  103. c := filledBuf[i]
  104. value := uint64(c.FmtValue.DoubleValue)
  105. name := win_pdh.UTF16PtrToString(c.SzName)
  106. data[name] = value
  107. }
  108. return data, nil
  109. }