Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / third_party / power_gadget / PowerGadgetLib.cpp
blob170085e37d6a1f7998017b75a697c60b7fca4ff2
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 split(const wstring& s, wstring &path)
40 bool bResult = false;
41 vector<wstring> output;
43 wstring::size_type prev_pos = 0, pos = 0;
45 while((pos = s.find(L';', pos)) != wstring::npos)
47 wstring substring( s.substr(prev_pos, pos-prev_pos) );
48 if (substring.find(L"Power Gadget 2.") != wstring::npos)
50 path = substring;
51 bResult = true;
52 break;
54 prev_pos = ++pos;
57 if (!bResult)
59 wstring substring(s.substr(prev_pos, pos-prev_pos));
61 if (substring.find(L"Power Gadget 2.") != wstring::npos)
63 path = substring;
64 bResult = true;
68 if (bResult)
70 basic_string <char>::size_type pos = path.rfind(L" ");
71 wstring version = path.substr(pos+1, path.length());
72 double fVer = _wtof(version.c_str());
73 if (fVer > 2.6)
74 bResult = true;
77 return bResult;
80 static bool GetLibraryLocation(wstring& strLocation)
82 TCHAR *pszPath = _wgetenv(L"IPG_Dir");
83 if (pszPath == NULL || wcslen(pszPath) == 0)
84 return false;
86 TCHAR *pszVersion = _wgetenv(L"IPG_Ver");
87 if (pszVersion == NULL || wcslen(pszVersion) == 0)
88 return false;
90 int version = _wtof(pszVersion) * 100;
91 if (version >= 270)
93 #if _M_X64
94 strLocation = wstring(pszPath) + L"\\EnergyLib64.dll";
95 #else
96 strLocation = wstring(pszPath) + L"\\EnergyLib32.dll";
97 #endif
98 return true;
100 else
101 return false;
104 CIntelPowerGadgetLib::CIntelPowerGadgetLib(void) :
105 pInitialize(NULL),
106 pGetNumNodes(NULL),
107 pGetNumMsrs(NULL),
108 pGetMsrName(NULL),
109 pGetMsrFunc(NULL),
110 pGetIAFrequency(NULL),
111 pGetTDP(NULL),
112 pGetMaxTemperature(NULL),
113 pGetTemperature(NULL),
114 pReadSample(NULL),
115 pGetSysTime(NULL),
116 pGetRDTSC(NULL),
117 pGetTimeInterval(NULL),
118 pGetBaseFrequency(NULL),
119 pGetPowerData(NULL),
120 pStartLog(NULL),
121 pStopLog(NULL)
123 wstring strLocation;
124 if (GetLibraryLocation(strLocation) == false)
126 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";
127 return;
130 g_hModule = LoadLibrary(strLocation.c_str());
131 if (g_hModule == NULL)
133 g_lastError = "LoadLibrary failed";
134 return;
137 pInitialize = (IPGInitialize) GetProcAddress(g_hModule, "IntelEnergyLibInitialize");
138 pGetNumNodes = (IPGGetNumNodes) GetProcAddress(g_hModule, "GetNumNodes");
139 pGetMsrName = (IPGGetMsrName) GetProcAddress(g_hModule, "GetMsrName");
140 pGetMsrFunc = (IPGGetMsrFunc) GetProcAddress(g_hModule, "GetMsrFunc");
141 pGetIAFrequency = (IPGGetIAFrequency) GetProcAddress(g_hModule, "GetIAFrequency");
142 pGetTDP = (IPGGetTDP) GetProcAddress(g_hModule, "GetTDP");
143 pGetMaxTemperature = (IPGGetMaxTemperature) GetProcAddress(g_hModule, "GetMaxTemperature");
144 pGetTemperature = (IPGGetTemperature) GetProcAddress(g_hModule, "GetTemperature");
145 pReadSample = (IPGReadSample) GetProcAddress(g_hModule, "ReadSample");
146 pGetSysTime = (IPGGetSysTime) GetProcAddress(g_hModule, "GetSysTime");
147 pGetRDTSC = (IPGGetRDTSC) GetProcAddress(g_hModule, "GetRDTSC");
148 pGetTimeInterval = (IPGGetTimeInterval) GetProcAddress(g_hModule, "GetTimeInterval");
149 pGetBaseFrequency = (IPGGetBaseFrequency) GetProcAddress(g_hModule, "GetBaseFrequency");
150 pGetPowerData = (IPGGetPowerData) GetProcAddress(g_hModule, "GetPowerData");
151 pStartLog = (IPGStartLog) GetProcAddress(g_hModule, "StartLog");
152 pStopLog = (IPGStopLog) GetProcAddress(g_hModule, "StopLog");
153 pGetNumMsrs = (IPGGetNumMsrs) GetProcAddress(g_hModule, "GetNumMsrs");
157 CIntelPowerGadgetLib::~CIntelPowerGadgetLib(void)
159 if (g_hModule != NULL)
160 FreeLibrary(g_hModule);
165 string CIntelPowerGadgetLib::GetLastError()
167 return g_lastError;
170 bool CIntelPowerGadgetLib::IntelEnergyLibInitialize(void)
172 if (pInitialize == NULL)
173 return false;
175 bool bSuccess = pInitialize();
176 if (!bSuccess)
178 g_lastError = "Initializing the energy library failed";
179 return false;
182 return true;
186 bool CIntelPowerGadgetLib::GetNumNodes(int * nNodes)
188 return pGetNumNodes(nNodes);
191 bool CIntelPowerGadgetLib::GetNumMsrs(int * nMsrs)
193 return pGetNumMsrs(nMsrs);
196 bool CIntelPowerGadgetLib::GetMsrName(int iMsr, wchar_t *pszName)
198 return pGetMsrName(iMsr, pszName);
201 bool CIntelPowerGadgetLib::GetMsrFunc(int iMsr, int *funcID)
203 return pGetMsrFunc(iMsr, funcID);
206 bool CIntelPowerGadgetLib::GetIAFrequency(int iNode, int *freqInMHz)
208 return pGetIAFrequency(iNode, freqInMHz);
211 bool CIntelPowerGadgetLib::GetTDP(int iNode, double *TDP)
213 return pGetTDP(iNode, TDP);
216 bool CIntelPowerGadgetLib::GetMaxTemperature(int iNode, int *degreeC)
218 return pGetMaxTemperature(iNode, degreeC);
221 bool CIntelPowerGadgetLib::GetTemperature(int iNode, int *degreeC)
223 return pGetTemperature(iNode, degreeC);
226 bool CIntelPowerGadgetLib::ReadSample()
228 bool bSuccess = pReadSample();
229 if (bSuccess == false)
230 g_lastError = "MSR overflowed. You can safely discard this sample";
231 return bSuccess;
234 bool CIntelPowerGadgetLib::GetSysTime(SYSTEMTIME *sysTime)
236 return pGetSysTime(sysTime);
239 bool CIntelPowerGadgetLib::GetRDTSC(UINT64 *TSC)
241 return pGetRDTSC(TSC);
244 bool CIntelPowerGadgetLib::GetTimeInterval(double *offset)
246 return pGetTimeInterval(offset);
249 bool CIntelPowerGadgetLib::GetBaseFrequency(int iNode, double *baseFrequency)
251 return pGetBaseFrequency(iNode, baseFrequency);
254 bool CIntelPowerGadgetLib::GetPowerData(int iNode, int iMSR, double *results, int *nResult)
256 return pGetPowerData(iNode, iMSR, results, nResult);
259 bool CIntelPowerGadgetLib::StartLog(wchar_t *szFilename)
261 return pStartLog(szFilename);
264 bool CIntelPowerGadgetLib::StopLog()
266 return pStopLog();