OLD | NEW |
(Empty) | |
| 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. |
| 29 |
| 30 #include "PowerGadgetLib.h" |
| 31 #include <Windows.h> |
| 32 #include <string> |
| 33 #include <vector> |
| 34 |
| 35 using namespace std; |
| 36 |
| 37 string g_lastError; |
| 38 HMODULE g_hModule = NULL; |
| 39 |
| 40 static bool split(const wstring& s, wstring &path) |
| 41 { |
| 42 bool bResult = false; |
| 43 vector<wstring> output; |
| 44 |
| 45 wstring::size_type prev_pos = 0, pos = 0; |
| 46 |
| 47 while((pos = s.find(L';', pos)) != wstring::npos) |
| 48 { |
| 49 wstring substring( s.substr(prev_pos, pos-prev_pos) ); |
| 50 if (substring.find(L"Power Gadget 2.") != wstring::npos) |
| 51 { |
| 52 path = substring; |
| 53 bResult = true; |
| 54 break; |
| 55 } |
| 56 prev_pos = ++pos; |
| 57 } |
| 58 |
| 59 if (!bResult) |
| 60 { |
| 61 wstring substring(s.substr(prev_pos, pos-prev_pos)); |
| 62 |
| 63 if (substring.find(L"Power Gadget 2.") != wstring::npos) |
| 64 { |
| 65 path = substring; |
| 66 bResult = true; |
| 67 } |
| 68 } |
| 69 |
| 70 if (bResult) |
| 71 { |
| 72 basic_string <char>::size_type pos = path.rfind(L" "); |
| 73 wstring version = path.substr(pos+1, path.length()); |
| 74 double fVer = _wtof(version.c_str()); |
| 75 if (fVer > 2.6) |
| 76 bResult = true; |
| 77 } |
| 78 |
| 79 return bResult; |
| 80 } |
| 81 |
| 82 static bool GetLibraryLocation(wstring& strLocation) |
| 83 { |
| 84 TCHAR *pszPath = _wgetenv(L"IPG_Dir"); |
| 85 if (pszPath == NULL || wcslen(pszPath) == 0) |
| 86 return false; |
| 87 |
| 88 TCHAR *pszVersion = _wgetenv(L"IPG_Ver"); |
| 89 if (pszVersion == NULL || wcslen(pszVersion) == 0) |
| 90 return false; |
| 91 |
| 92 int version = _wtof(pszVersion) * 100; |
| 93 if (version >= 270) |
| 94 { |
| 95 #if _M_X64 |
| 96 strLocation = wstring(pszPath) + L"\\EnergyLib64.dll"; |
| 97 #else |
| 98 strLocation = wstring(pszPath) + L"\\EnergyLib32.dll"; |
| 99 #endif |
| 100 return true; |
| 101 } |
| 102 else |
| 103 return false; |
| 104 } |
| 105 |
| 106 CIntelPowerGadgetLib::CIntelPowerGadgetLib(void) : |
| 107 pInitialize(NULL), |
| 108 pGetNumNodes(NULL), |
| 109 pGetMsrName(NULL), |
| 110 pGetMsrFunc(NULL), |
| 111 pGetIAFrequency(NULL), |
| 112 pGetTDP(NULL), |
| 113 pGetMaxTemperature(NULL), |
| 114 pGetTemperature(NULL), |
| 115 pReadSample(NULL), |
| 116 pGetSysTime(NULL), |
| 117 pGetRDTSC(NULL), |
| 118 pGetTimeInterval(NULL), |
| 119 pGetBaseFrequency(NULL), |
| 120 pGetPowerData(NULL), |
| 121 pStartLog(NULL), |
| 122 pStopLog(NULL), |
| 123 pGetNumMsrs(NULL) |
| 124 { |
| 125 wstring strLocation; |
| 126 if (GetLibraryLocation(strLocation) == false) |
| 127 { |
| 128 g_lastError = "Intel Power Gadget 2.7 or higher not found. If un
sure, check if the path is in the user's path environment variable"; |
| 129 return; |
| 130 } |
| 131 |
| 132 g_hModule = LoadLibrary(strLocation.c_str()); |
| 133 if (g_hModule == NULL) |
| 134 { |
| 135 g_lastError = "LoadLibrary failed"; |
| 136 return; |
| 137 } |
| 138 |
| 139 pInitialize = (IPGInitialize) GetProcAddress(g_hModule, "IntelEnergyLibIniti
alize"); |
| 140 pGetNumNodes = (IPGGetNumNodes) GetProcAddress(g_hModule, "GetNumNodes"); |
| 141 pGetMsrName = (IPGGetMsrName) GetProcAddress(g_hModule, "GetMsrName"); |
| 142 pGetMsrFunc = (IPGGetMsrFunc) GetProcAddress(g_hModule, "GetMsrFunc"); |
| 143 pGetIAFrequency = (IPGGetIAFrequency) GetProcAddress(g_hModule, "GetIAFreque
ncy"); |
| 144 pGetTDP = (IPGGetTDP) GetProcAddress(g_hModule, "GetTDP"); |
| 145 pGetMaxTemperature = (IPGGetMaxTemperature) GetProcAddress(g_hModule, "GetMa
xTemperature"); |
| 146 pGetTemperature = (IPGGetTemperature) GetProcAddress(g_hModule, "GetTemperat
ure"); |
| 147 pReadSample = (IPGReadSample) GetProcAddress(g_hModule, "ReadSample"); |
| 148 pGetSysTime = (IPGGetSysTime) GetProcAddress(g_hModule, "GetSysTime"); |
| 149 pGetRDTSC = (IPGGetRDTSC) GetProcAddress(g_hModule, "GetRDTSC"); |
| 150 pGetTimeInterval = (IPGGetTimeInterval) GetProcAddress(g_hModule, "GetTimeIn
terval"); |
| 151 pGetBaseFrequency = (IPGGetBaseFrequency) GetProcAddress(g_hModule, "GetBase
Frequency"); |
| 152 pGetPowerData = (IPGGetPowerData) GetProcAddress(g_hModule, "GetPowerData"); |
| 153 pStartLog = (IPGStartLog) GetProcAddress(g_hModule, "StartLog"); |
| 154 pStopLog = (IPGStopLog) GetProcAddress(g_hModule, "StopLog"); |
| 155 pGetNumMsrs = (IPGGetNumMsrs) GetProcAddress(g_hModule, "GetNumMsrs"); |
| 156 } |
| 157 |
| 158 |
| 159 CIntelPowerGadgetLib::~CIntelPowerGadgetLib(void) |
| 160 { |
| 161 if (g_hModule != NULL) |
| 162 FreeLibrary(g_hModule); |
| 163 } |
| 164 |
| 165 |
| 166 |
| 167 string CIntelPowerGadgetLib::GetLastError() |
| 168 { |
| 169 return g_lastError; |
| 170 } |
| 171 |
| 172 bool CIntelPowerGadgetLib::IntelEnergyLibInitialize(void) |
| 173 { |
| 174 if (pInitialize == NULL) |
| 175 return false; |
| 176 |
| 177 bool bSuccess = pInitialize(); |
| 178 if (!bSuccess) |
| 179 { |
| 180 g_lastError = "Initializing the energy library failed"; |
| 181 return false; |
| 182 } |
| 183 |
| 184 return true; |
| 185 } |
| 186 |
| 187 |
| 188 bool CIntelPowerGadgetLib::GetNumNodes(int * nNodes) |
| 189 { |
| 190 return pGetNumNodes(nNodes); |
| 191 } |
| 192 |
| 193 bool CIntelPowerGadgetLib::GetNumMsrs(int * nMsrs) |
| 194 { |
| 195 return pGetNumMsrs(nMsrs); |
| 196 } |
| 197 |
| 198 bool CIntelPowerGadgetLib::GetMsrName(int iMsr, wchar_t *pszName) |
| 199 { |
| 200 return pGetMsrName(iMsr, pszName); |
| 201 } |
| 202 |
| 203 bool CIntelPowerGadgetLib::GetMsrFunc(int iMsr, int *funcID) |
| 204 { |
| 205 return pGetMsrFunc(iMsr, funcID); |
| 206 } |
| 207 |
| 208 bool CIntelPowerGadgetLib::GetIAFrequency(int iNode, int *freqInMHz) |
| 209 { |
| 210 return pGetIAFrequency(iNode, freqInMHz); |
| 211 } |
| 212 |
| 213 bool CIntelPowerGadgetLib::GetTDP(int iNode, double *TDP) |
| 214 { |
| 215 return pGetTDP(iNode, TDP); |
| 216 } |
| 217 |
| 218 bool CIntelPowerGadgetLib::GetMaxTemperature(int iNode, int *degreeC) |
| 219 { |
| 220 return pGetMaxTemperature(iNode, degreeC); |
| 221 } |
| 222 |
| 223 bool CIntelPowerGadgetLib::GetTemperature(int iNode, int *degreeC) |
| 224 { |
| 225 return pGetTemperature(iNode, degreeC); |
| 226 } |
| 227 |
| 228 bool CIntelPowerGadgetLib::ReadSample() |
| 229 { |
| 230 bool bSuccess = pReadSample(); |
| 231 if (bSuccess == false) |
| 232 g_lastError = "MSR overflowed. You can safely discard this sampl
e"; |
| 233 return bSuccess; |
| 234 } |
| 235 |
| 236 bool CIntelPowerGadgetLib::GetSysTime(SYSTEMTIME *sysTime) |
| 237 { |
| 238 return pGetSysTime(sysTime); |
| 239 } |
| 240 |
| 241 bool CIntelPowerGadgetLib::GetRDTSC(UINT64 *TSC) |
| 242 { |
| 243 return pGetRDTSC(TSC); |
| 244 } |
| 245 |
| 246 bool CIntelPowerGadgetLib::GetTimeInterval(double *offset) |
| 247 { |
| 248 return pGetTimeInterval(offset); |
| 249 } |
| 250 |
| 251 bool CIntelPowerGadgetLib::GetBaseFrequency(int iNode, double *baseFrequency) |
| 252 { |
| 253 return pGetBaseFrequency(iNode, baseFrequency); |
| 254 } |
| 255 |
| 256 bool CIntelPowerGadgetLib::GetPowerData(int iNode, int iMSR, double *results, in
t *nResult) |
| 257 { |
| 258 return pGetPowerData(iNode, iMSR, results, nResult); |
| 259 } |
| 260 |
| 261 bool CIntelPowerGadgetLib::StartLog(wchar_t *szFilename) |
| 262 { |
| 263 return pStartLog(szFilename); |
| 264 } |
| 265 |
| 266 bool CIntelPowerGadgetLib::StopLog() |
| 267 { |
| 268 return pStopLog(); |
| 269 } |
OLD | NEW |