Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / third_party / power_gadget / PowerGadgetLib.cpp
blobe50a489930a29561dd7f9c72b99e7eb1494e5e79
1 // Copyright (c) 2014, Intel Corporation
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Intel Corporation nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "PowerGadgetLib.h"
31 #include <vector>
33 using namespace std;
35 string g_lastError;
36 HMODULE g_hModule = NULL;
38 static bool GetLibraryLocation(wstring& strLocation)
40 TCHAR *pszPath = _wgetenv(L"IPG_Dir");
41 if (pszPath == NULL || wcslen(pszPath) == 0)
42 return false;
44 TCHAR *pszVersion = _wgetenv(L"IPG_Ver");
45 if (pszVersion == NULL || wcslen(pszVersion) == 0)
46 return false;
48 int version = _wtof(pszVersion) * 100;
49 if (version >= 270)
51 #if _M_X64
52 strLocation = wstring(pszPath) + L"\\EnergyLib64.dll";
53 #else
54 strLocation = wstring(pszPath) + L"\\EnergyLib32.dll";
55 #endif
56 return true;
58 else
59 return false;
62 CIntelPowerGadgetLib::CIntelPowerGadgetLib(void) :
63 pInitialize(NULL),
64 pGetNumNodes(NULL),
65 pGetNumMsrs(NULL),
66 pGetMsrName(NULL),
67 pGetMsrFunc(NULL),
68 pGetIAFrequency(NULL),
69 pGetTDP(NULL),
70 pGetMaxTemperature(NULL),
71 pGetTemperature(NULL),
72 pReadSample(NULL),
73 pGetSysTime(NULL),
74 pGetRDTSC(NULL),
75 pGetTimeInterval(NULL),
76 pGetBaseFrequency(NULL),
77 pGetPowerData(NULL),
78 pStartLog(NULL),
79 pStopLog(NULL)
81 wstring strLocation;
82 if (GetLibraryLocation(strLocation) == false)
84 g_lastError = "Intel Power Gadget 2.7 or higher not found. If unsure, check if the path is in the user's path environment variable";
85 return;
88 g_hModule = LoadLibrary(strLocation.c_str());
89 if (g_hModule == NULL)
91 g_lastError = "LoadLibrary failed";
92 return;
95 pInitialize = (IPGInitialize) GetProcAddress(g_hModule, "IntelEnergyLibInitialize");
96 pGetNumNodes = (IPGGetNumNodes) GetProcAddress(g_hModule, "GetNumNodes");
97 pGetMsrName = (IPGGetMsrName) GetProcAddress(g_hModule, "GetMsrName");
98 pGetMsrFunc = (IPGGetMsrFunc) GetProcAddress(g_hModule, "GetMsrFunc");
99 pGetIAFrequency = (IPGGetIAFrequency) GetProcAddress(g_hModule, "GetIAFrequency");
100 pGetTDP = (IPGGetTDP) GetProcAddress(g_hModule, "GetTDP");
101 pGetMaxTemperature = (IPGGetMaxTemperature) GetProcAddress(g_hModule, "GetMaxTemperature");
102 pGetTemperature = (IPGGetTemperature) GetProcAddress(g_hModule, "GetTemperature");
103 pReadSample = (IPGReadSample) GetProcAddress(g_hModule, "ReadSample");
104 pGetSysTime = (IPGGetSysTime) GetProcAddress(g_hModule, "GetSysTime");
105 pGetRDTSC = (IPGGetRDTSC) GetProcAddress(g_hModule, "GetRDTSC");
106 pGetTimeInterval = (IPGGetTimeInterval) GetProcAddress(g_hModule, "GetTimeInterval");
107 pGetBaseFrequency = (IPGGetBaseFrequency) GetProcAddress(g_hModule, "GetBaseFrequency");
108 pGetPowerData = (IPGGetPowerData) GetProcAddress(g_hModule, "GetPowerData");
109 pStartLog = (IPGStartLog) GetProcAddress(g_hModule, "StartLog");
110 pStopLog = (IPGStopLog) GetProcAddress(g_hModule, "StopLog");
111 pGetNumMsrs = (IPGGetNumMsrs) GetProcAddress(g_hModule, "GetNumMsrs");
115 CIntelPowerGadgetLib::~CIntelPowerGadgetLib(void)
117 if (g_hModule != NULL)
118 FreeLibrary(g_hModule);
123 string CIntelPowerGadgetLib::GetLastError()
125 return g_lastError;
128 bool CIntelPowerGadgetLib::IntelEnergyLibInitialize(void)
130 if (pInitialize == NULL)
131 return false;
133 bool bSuccess = pInitialize();
134 if (!bSuccess)
136 g_lastError = "Initializing the energy library failed";
137 return false;
140 return true;
144 bool CIntelPowerGadgetLib::GetNumNodes(int * nNodes)
146 return pGetNumNodes(nNodes);
149 bool CIntelPowerGadgetLib::GetNumMsrs(int * nMsrs)
151 return pGetNumMsrs(nMsrs);
154 bool CIntelPowerGadgetLib::GetMsrName(int iMsr, wchar_t *pszName)
156 return pGetMsrName(iMsr, pszName);
159 bool CIntelPowerGadgetLib::GetMsrFunc(int iMsr, int *funcID)
161 return pGetMsrFunc(iMsr, funcID);
164 bool CIntelPowerGadgetLib::GetIAFrequency(int iNode, int *freqInMHz)
166 return pGetIAFrequency(iNode, freqInMHz);
169 bool CIntelPowerGadgetLib::GetTDP(int iNode, double *TDP)
171 return pGetTDP(iNode, TDP);
174 bool CIntelPowerGadgetLib::GetMaxTemperature(int iNode, int *degreeC)
176 return pGetMaxTemperature(iNode, degreeC);
179 bool CIntelPowerGadgetLib::GetTemperature(int iNode, int *degreeC)
181 return pGetTemperature(iNode, degreeC);
184 bool CIntelPowerGadgetLib::ReadSample()
186 bool bSuccess = pReadSample();
187 if (bSuccess == false)
188 g_lastError = "MSR overflowed. You can safely discard this sample";
189 return bSuccess;
192 bool CIntelPowerGadgetLib::GetSysTime(SYSTEMTIME *sysTime)
194 return pGetSysTime(sysTime);
197 bool CIntelPowerGadgetLib::GetRDTSC(UINT64 *TSC)
199 return pGetRDTSC(TSC);
202 bool CIntelPowerGadgetLib::GetTimeInterval(double *offset)
204 return pGetTimeInterval(offset);
207 bool CIntelPowerGadgetLib::GetBaseFrequency(int iNode, double *baseFrequency)
209 return pGetBaseFrequency(iNode, baseFrequency);
212 bool CIntelPowerGadgetLib::GetPowerData(int iNode, int iMSR, double *results, int *nResult)
214 return pGetPowerData(iNode, iMSR, results, nResult);
217 bool CIntelPowerGadgetLib::StartLog(wchar_t *szFilename)
219 return pStartLog(szFilename);
222 bool CIntelPowerGadgetLib::StopLog()
224 return pStopLog();