power_sampler.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_WIN_IDLEWAKEUPS_POWER_SAMPLER_H_
  5. #define TOOLS_WIN_IDLEWAKEUPS_POWER_SAMPLER_H_
  6. #include <map>
  7. #include <string>
  8. #include <windows.h>
  9. // https://software.intel.com/en-us/blogs/2012/12/13/using-the-intel-power-gadget-api-on-mac-os-x
  10. typedef int (*IntelEnergyLibInitialize_t)();
  11. typedef int (*GetNumMsrs_t)(int* nMsr);
  12. typedef int (*GetMsrName_t)(int iMsr, wchar_t* szName);
  13. typedef int (*GetMsrFunc_t)(int iMsr, int* pFuncID);
  14. typedef int (*GetPowerData_t)(int iNode,
  15. int iMsr,
  16. double* pResult,
  17. int* nResult);
  18. typedef int (*ReadSample_t)();
  19. class PowerSampler {
  20. public:
  21. PowerSampler();
  22. ~PowerSampler();
  23. void SampleCPUPowerState();
  24. double get_power(std::wstring key) { return power_[key]; }
  25. private:
  26. // Power consumed in mWh since the last sample.
  27. std::map<std::wstring, double> power_;
  28. HMODULE energy_lib_ = nullptr;
  29. IntelEnergyLibInitialize_t IntelEnergyLibInitialize = nullptr;
  30. GetNumMsrs_t GetNumMsrs = nullptr;
  31. GetMsrName_t GetMsrName = nullptr;
  32. GetMsrFunc_t GetMsrFunc = nullptr;
  33. GetPowerData_t GetPowerData = nullptr;
  34. ReadSample_t ReadSample = nullptr;
  35. PowerSampler& operator=(const PowerSampler&) = delete;
  36. PowerSampler(const PowerSampler&) = delete;
  37. };
  38. #endif // TOOLS_WIN_IDLEWAKEUPS_POWER_SAMPLER_H_