machine_libipmctl.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //go:build libipmctl && cgo
  2. // +build libipmctl,cgo
  3. // Copyright 2020 Google Inc. All Rights Reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package nvm
  17. // #cgo pkg-config: libipmctl
  18. // #include <nvm_management.h>
  19. import "C"
  20. import (
  21. "fmt"
  22. "sync"
  23. info "github.com/google/cadvisor/info/v1"
  24. "k8s.io/klog/v2"
  25. )
  26. var (
  27. isNVMLibInitialized = false
  28. nvmLibMutex = sync.Mutex{}
  29. )
  30. func init() {
  31. nvmLibMutex.Lock()
  32. defer nvmLibMutex.Unlock()
  33. cErr := C.nvm_init()
  34. if cErr != C.NVM_SUCCESS {
  35. // Unfortunately klog does not seem to work here. I believe it's better to
  36. // output information using fmt rather then let it disappear silently.
  37. fmt.Printf("libipmctl initialization failed with status %d", cErr)
  38. return
  39. }
  40. isNVMLibInitialized = true
  41. }
  42. // getAvgPowerBudget retrieves configured power budget
  43. // (in watts) for NVM devices. When libipmct is not available
  44. // zero is returned.
  45. func getAvgPowerBudget() (uint, error) {
  46. // Get number of devices on the platform
  47. // see: https://github.com/intel/ipmctl/blob/v01.00.00.3497/src/os/nvm_api/nvm_management.h#L1478
  48. count := C.uint(0)
  49. err := C.nvm_get_number_of_devices(&count)
  50. if err != C.NVM_SUCCESS {
  51. klog.Warningf("Unable to get number of NVM devices. Status code: %d", err)
  52. return uint(0), fmt.Errorf("Unable to get number of NVM devices. Status code: %d", err)
  53. }
  54. if count == 0 {
  55. klog.Warningf("There are no NVM devices!")
  56. return uint(0), nil
  57. }
  58. // Load basic device information for all the devices
  59. // to obtain UID of the first one.
  60. devices := make([]C.struct_device_discovery, count)
  61. err = C.nvm_get_devices(&devices[0], C.uchar(count))
  62. if err != C.NVM_SUCCESS {
  63. klog.Warningf("Unable to get all NVM devices. Status code: %d", err)
  64. return uint(0), fmt.Errorf("Unable to get all NVM devices. Status code: %d", err)
  65. }
  66. // Power budget is same for all the devices
  67. // so we can rely on any of them.
  68. device := C.struct_device_details{}
  69. err = C.nvm_get_device_details(&devices[0].uid[0], &device)
  70. if err != C.NVM_SUCCESS {
  71. uid := C.GoString(&devices[0].uid[0])
  72. klog.Warningf("Unable to get details of NVM device %q. Status code: %d", uid, err)
  73. return uint(0), fmt.Errorf("Unable to get details of NVM device %q. Status code: %d", uid, err)
  74. }
  75. return uint(device.avg_power_budget / 1000), nil
  76. }
  77. // getCapacities retrieves the total NVM capacity in bytes for memory mode and app direct mode
  78. func getCapacities() (uint64, uint64, error) {
  79. caps := C.struct_device_capacities{}
  80. err := C.nvm_get_nvm_capacities(&caps)
  81. if err != C.NVM_SUCCESS {
  82. klog.Warningf("Unable to get NVM capacity. Status code: %d", err)
  83. return uint64(0), uint64(0), fmt.Errorf("Unable to get NVM capacity. Status code: %d", err)
  84. }
  85. return uint64(caps.memory_capacity), uint64(caps.app_direct_capacity), nil
  86. }
  87. // GetInfo returns information specific for non-volatile memory modules
  88. func GetInfo() (info.NVMInfo, error) {
  89. nvmLibMutex.Lock()
  90. defer nvmLibMutex.Unlock()
  91. nvmInfo := info.NVMInfo{}
  92. if !isNVMLibInitialized {
  93. klog.V(1).Info("libipmctl has not been initialized. NVM information will not be available")
  94. return nvmInfo, nil
  95. }
  96. var err error
  97. nvmInfo.MemoryModeCapacity, nvmInfo.AppDirectModeCapacity, err = getCapacities()
  98. if err != nil {
  99. return info.NVMInfo{}, fmt.Errorf("Unable to get NVM capacities, err: %s", err)
  100. }
  101. nvmInfo.AvgPowerBudget, err = getAvgPowerBudget()
  102. if err != nil {
  103. return info.NVMInfo{}, fmt.Errorf("Unable to get NVM average power budget, err: %s", err)
  104. }
  105. return nvmInfo, nil
  106. }
  107. // Finalize un-initializes libipmctl. See https://github.com/google/cadvisor/issues/2457.
  108. func Finalize() {
  109. nvmLibMutex.Lock()
  110. defer nvmLibMutex.Unlock()
  111. klog.V(1).Info("Attempting to un-initialize libipmctl")
  112. if !isNVMLibInitialized {
  113. klog.V(1).Info("libipmctl has not been initialized; not un-initializing.")
  114. return
  115. }
  116. C.nvm_uninit()
  117. isNVMLibInitialized = false
  118. }