OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "stdafx.h" |
| 6 |
| 7 #include "power_sampler.h" |
| 8 |
| 9 // These correspond to the funcID values returned by GetMsrFunc |
| 10 const int MSR_FUNC_FREQ = 0; |
| 11 const int MSR_FUNC_POWER = 1; |
| 12 const int MSR_FUNC_TEMP = 2; |
| 13 const int MSR_FUNC_MAX_POWER = 3; /* ????? */ |
| 14 |
| 15 void PowerSampler::SampleCPUPowerState() { |
| 16 if (!IntelEnergyLibInitialize || !GetNumMsrs || !GetMsrName || !GetMsrFunc || |
| 17 !GetPowerData || !ReadSample) { |
| 18 return; |
| 19 } |
| 20 |
| 21 int num_MSRs = 0; |
| 22 GetNumMsrs(&num_MSRs); |
| 23 ReadSample(); |
| 24 |
| 25 for (int i = 0; i < num_MSRs; ++i) { |
| 26 int func_id; |
| 27 wchar_t MSR_name[1024]; |
| 28 GetMsrFunc(i, &func_id); |
| 29 |
| 30 int nData; |
| 31 double data[3] = {}; |
| 32 GetPowerData(0, i, data, &nData); |
| 33 |
| 34 if (func_id == MSR_FUNC_POWER) { |
| 35 // data[0] is Power (W) |
| 36 // data[1] is Energy (J) |
| 37 // data[2] is Energy (mWh) |
| 38 // Round to nearest .0001 to avoid distracting excess precision. |
| 39 GetMsrName(i, MSR_name); |
| 40 power_[MSR_name] = round(data[0] * 10000) / 10000; |
| 41 } |
| 42 } |
| 43 } |
| 44 |
| 45 PowerSampler::PowerSampler() { |
| 46 // If Intel Power Gadget is installed then use it to get CPU power data. |
| 47 #if _M_X64 |
| 48 PCWSTR dllName = L"\\EnergyLib64.dll"; |
| 49 #else |
| 50 PCWSTR dllName = L"\\EnergyLib32.dll"; |
| 51 #endif |
| 52 #pragma warning(disable : 4996) |
| 53 PCWSTR powerGadgetDir = _wgetenv(L"IPG_Dir"); |
| 54 if (powerGadgetDir) |
| 55 energy_lib_ = LoadLibrary((std::wstring(powerGadgetDir) + dllName).c_str()); |
| 56 if (energy_lib_) { |
| 57 IntelEnergyLibInitialize = (IntelEnergyLibInitialize_t)GetProcAddress( |
| 58 energy_lib_, "IntelEnergyLibInitialize"); |
| 59 GetNumMsrs = (GetNumMsrs_t)GetProcAddress(energy_lib_, "GetNumMsrs"); |
| 60 GetMsrName = (GetMsrName_t)GetProcAddress(energy_lib_, "GetMsrName"); |
| 61 GetMsrFunc = (GetMsrFunc_t)GetProcAddress(energy_lib_, "GetMsrFunc"); |
| 62 GetPowerData = (GetPowerData_t)GetProcAddress(energy_lib_, "GetPowerData"); |
| 63 ReadSample = (ReadSample_t)GetProcAddress(energy_lib_, "ReadSample"); |
| 64 |
| 65 if (IntelEnergyLibInitialize && ReadSample) { |
| 66 IntelEnergyLibInitialize(); |
| 67 ReadSample(); |
| 68 } |
| 69 } |
| 70 } |
| 71 |
| 72 PowerSampler::~PowerSampler() { |
| 73 if (energy_lib_) |
| 74 FreeLibrary(energy_lib_); |
| 75 } |
OLD | NEW |