1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/profiler/IntelPowerGadget.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,310 @@ 1.4 +/* 1.5 + * Copyright 2013, Intel Corporation 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + * 1.19 + * Author: Joe Olivas <joseph.k.olivas@intel.com> 1.20 + */ 1.21 + 1.22 +#include "nsDebug.h" 1.23 +#include "nsString.h" 1.24 +#include "IntelPowerGadget.h" 1.25 +#include "prenv.h" 1.26 + 1.27 +IntelPowerGadget::IntelPowerGadget() : 1.28 + libpowergadget(nullptr), 1.29 + Initialize(nullptr), 1.30 + GetNumNodes(nullptr), 1.31 + GetMsrName(nullptr), 1.32 + GetMsrFunc(nullptr), 1.33 + ReadMSR(nullptr), 1.34 + WriteMSR(nullptr), 1.35 + GetIAFrequency(nullptr), 1.36 + GetTDP(nullptr), 1.37 + GetMaxTemperature(nullptr), 1.38 + GetThresholds(nullptr), 1.39 + GetTemperature(nullptr), 1.40 + ReadSample(nullptr), 1.41 + GetSysTime(nullptr), 1.42 + GetRDTSC(nullptr), 1.43 + GetTimeInterval(nullptr), 1.44 + GetBaseFrequency(nullptr), 1.45 + GetPowerData(nullptr), 1.46 + StartLog(nullptr), 1.47 + StopLog(nullptr), 1.48 + GetNumMsrs(nullptr), 1.49 + packageMSR(-1), 1.50 + cpuMSR(-1), 1.51 + freqMSR(-1), 1.52 + tempMSR(-1) 1.53 +{ 1.54 +} 1.55 + 1.56 +bool 1.57 +IntelPowerGadget::Init() 1.58 +{ 1.59 + bool success = false; 1.60 + const char *path = PR_GetEnv("IPG_Dir"); 1.61 + nsCString ipg_library; 1.62 + if (path && *path) { 1.63 + ipg_library.Append(path); 1.64 + ipg_library.AppendLiteral("/"); 1.65 + ipg_library.AppendLiteral(PG_LIBRARY_NAME); 1.66 + libpowergadget = PR_LoadLibrary(ipg_library.get()); 1.67 + } 1.68 + 1.69 + if(libpowergadget) { 1.70 + Initialize = (IPGInitialize) PR_FindFunctionSymbol(libpowergadget, "IntelEnergyLibInitialize"); 1.71 + GetNumNodes = (IPGGetNumNodes) PR_FindFunctionSymbol(libpowergadget, "GetNumNodes"); 1.72 + GetMsrName = (IPGGetMsrName) PR_FindFunctionSymbol(libpowergadget, "GetMsrName"); 1.73 + GetMsrFunc = (IPGGetMsrFunc) PR_FindFunctionSymbol(libpowergadget, "GetMsrFunc"); 1.74 + ReadMSR = (IPGReadMSR) PR_FindFunctionSymbol(libpowergadget, "ReadMSR"); 1.75 + WriteMSR = (IPGWriteMSR) PR_FindFunctionSymbol(libpowergadget, "WriteMSR"); 1.76 + GetIAFrequency = (IPGGetIAFrequency) PR_FindFunctionSymbol(libpowergadget, "GetIAFrequency"); 1.77 + GetTDP = (IPGGetTDP) PR_FindFunctionSymbol(libpowergadget, "GetTDP"); 1.78 + GetMaxTemperature = (IPGGetMaxTemperature) PR_FindFunctionSymbol(libpowergadget, "GetMaxTemperature"); 1.79 + GetThresholds = (IPGGetThresholds) PR_FindFunctionSymbol(libpowergadget, "GetThresholds"); 1.80 + GetTemperature = (IPGGetTemperature) PR_FindFunctionSymbol(libpowergadget, "GetTemperature"); 1.81 + ReadSample = (IPGReadSample) PR_FindFunctionSymbol(libpowergadget, "ReadSample"); 1.82 + GetSysTime = (IPGGetSysTime) PR_FindFunctionSymbol(libpowergadget, "GetSysTime"); 1.83 + GetRDTSC = (IPGGetRDTSC) PR_FindFunctionSymbol(libpowergadget, "GetRDTSC"); 1.84 + GetTimeInterval = (IPGGetTimeInterval) PR_FindFunctionSymbol(libpowergadget, "GetTimeInterval"); 1.85 + GetBaseFrequency = (IPGGetBaseFrequency) PR_FindFunctionSymbol(libpowergadget, "GetBaseFrequency"); 1.86 + GetPowerData = (IPGGetPowerData) PR_FindFunctionSymbol(libpowergadget, "GetPowerData"); 1.87 + StartLog = (IPGStartLog) PR_FindFunctionSymbol(libpowergadget, "StartLog"); 1.88 + StopLog = (IPGStopLog) PR_FindFunctionSymbol(libpowergadget, "StopLog"); 1.89 + GetNumMsrs = (IPGGetNumMsrs) PR_FindFunctionSymbol(libpowergadget, "GetNumMsrs"); 1.90 + } 1.91 + 1.92 + if(Initialize) { 1.93 + Initialize(); 1.94 + int msrCount = GetNumberMsrs(); 1.95 + wchar_t name[1024] = {0}; 1.96 + for(int i = 0; i < msrCount; ++i) { 1.97 + GetMsrName(i, name); 1.98 + int func = 0; 1.99 + GetMsrFunc(i, &func); 1.100 + // MSR for frequency 1.101 + if(wcscmp(name, L"CPU Frequency") == 0 && (func == 0)) { 1.102 + this->freqMSR = i; 1.103 + } 1.104 + // MSR for Package 1.105 + else if(wcscmp(name, L"Processor") == 0 && (func == 1)) { 1.106 + this->packageMSR = i; 1.107 + } 1.108 + // MSR for CPU 1.109 + else if(wcscmp(name, L"IA") == 0 && (func == 1)) { 1.110 + this->cpuMSR = i; 1.111 + } 1.112 + // MSR for Temperature 1.113 + else if(wcscmp(name, L"Package") == 0 && (func == 2)) { 1.114 + this->tempMSR = i; 1.115 + } 1.116 + } 1.117 + // Grab one sample at startup for a diff 1.118 + TakeSample(); 1.119 + success = true; 1.120 + } 1.121 + return success; 1.122 +} 1.123 + 1.124 +IntelPowerGadget::~IntelPowerGadget() 1.125 +{ 1.126 + if(libpowergadget) { 1.127 + NS_WARNING("Unloading PowerGadget library!\n"); 1.128 + PR_UnloadLibrary(libpowergadget); 1.129 + libpowergadget = nullptr; 1.130 + Initialize = nullptr; 1.131 + GetNumNodes = nullptr; 1.132 + GetMsrName = nullptr; 1.133 + GetMsrFunc = nullptr; 1.134 + ReadMSR = nullptr; 1.135 + WriteMSR = nullptr; 1.136 + GetIAFrequency = nullptr; 1.137 + GetTDP = nullptr; 1.138 + GetMaxTemperature = nullptr; 1.139 + GetThresholds = nullptr; 1.140 + GetTemperature = nullptr; 1.141 + ReadSample = nullptr; 1.142 + GetSysTime = nullptr; 1.143 + GetRDTSC = nullptr; 1.144 + GetTimeInterval = nullptr; 1.145 + GetBaseFrequency = nullptr; 1.146 + GetPowerData = nullptr; 1.147 + StartLog = nullptr; 1.148 + StopLog = nullptr; 1.149 + GetNumMsrs = nullptr; 1.150 + } 1.151 +} 1.152 + 1.153 +int 1.154 +IntelPowerGadget::GetNumberNodes() 1.155 +{ 1.156 + int nodes = 0; 1.157 + if(GetNumNodes) { 1.158 + int ok = GetNumNodes(&nodes); 1.159 + } 1.160 + return nodes; 1.161 +} 1.162 + 1.163 +int 1.164 +IntelPowerGadget::GetNumberMsrs() 1.165 +{ 1.166 + int msrs = 0; 1.167 + if(GetNumMsrs) { 1.168 + int ok = GetNumMsrs(&msrs); 1.169 + } 1.170 + return msrs; 1.171 +} 1.172 + 1.173 +int 1.174 +IntelPowerGadget::GetCPUFrequency(int node) 1.175 +{ 1.176 + int frequency = 0; 1.177 + if(GetIAFrequency) { 1.178 + int ok = GetIAFrequency(node, &frequency); 1.179 + } 1.180 + return frequency; 1.181 +} 1.182 + 1.183 +double 1.184 +IntelPowerGadget::GetTdp(int node) 1.185 +{ 1.186 + double tdp = 0.0; 1.187 + if(GetTDP) { 1.188 + int ok = GetTDP(node, &tdp); 1.189 + } 1.190 + return tdp; 1.191 +} 1.192 + 1.193 +int 1.194 +IntelPowerGadget::GetMaxTemp(int node) 1.195 +{ 1.196 + int maxTemperatureC = 0; 1.197 + if(GetMaxTemperature) { 1.198 + int ok = GetMaxTemperature(node, &maxTemperatureC); 1.199 + } 1.200 + return maxTemperatureC; 1.201 +} 1.202 + 1.203 +int 1.204 +IntelPowerGadget::GetTemp(int node) 1.205 +{ 1.206 + int temperatureC = 0; 1.207 + if(GetTemperature) { 1.208 + int ok = GetTemperature(node, &temperatureC); 1.209 + } 1.210 + return temperatureC; 1.211 +} 1.212 + 1.213 +int 1.214 +IntelPowerGadget::TakeSample() 1.215 +{ 1.216 + int ok = 0; 1.217 + if(ReadSample) { 1.218 + ok = ReadSample(); 1.219 + } 1.220 + return ok; 1.221 +} 1.222 + 1.223 +uint64_t 1.224 +IntelPowerGadget::GetRdtsc() 1.225 +{ 1.226 + uint64_t rdtsc = 0; 1.227 + if(GetRDTSC) { 1.228 + int ok = GetRDTSC(&rdtsc); 1.229 + } 1.230 + return rdtsc; 1.231 +} 1.232 + 1.233 +double 1.234 +IntelPowerGadget::GetInterval() 1.235 +{ 1.236 + double interval = 0.0; 1.237 + if(GetTimeInterval) { 1.238 + int ok = GetTimeInterval(&interval); 1.239 + } 1.240 + return interval; 1.241 +} 1.242 + 1.243 +double 1.244 +IntelPowerGadget::GetCPUBaseFrequency(int node) 1.245 +{ 1.246 + double freq = 0.0; 1.247 + if(GetBaseFrequency) { 1.248 + int ok = GetBaseFrequency(node, &freq); 1.249 + } 1.250 + return freq; 1.251 +} 1.252 + 1.253 +double 1.254 +IntelPowerGadget::GetTotalPackagePowerInWatts() 1.255 +{ 1.256 + int nodes = GetNumberNodes(); 1.257 + double totalPower = 0.0; 1.258 + for(int i = 0; i < nodes; ++i) { 1.259 + totalPower += GetPackagePowerInWatts(i); 1.260 + } 1.261 + return totalPower; 1.262 +} 1.263 + 1.264 +double 1.265 +IntelPowerGadget::GetPackagePowerInWatts(int node) 1.266 +{ 1.267 + int numResult = 0; 1.268 + double result[] = {0.0, 0.0, 0.0}; 1.269 + if(GetPowerData && packageMSR != -1) { 1.270 + int ok = GetPowerData(node, packageMSR, result, &numResult); 1.271 + } 1.272 + return result[0]; 1.273 +} 1.274 + 1.275 +double 1.276 +IntelPowerGadget::GetTotalCPUPowerInWatts() 1.277 +{ 1.278 + int nodes = GetNumberNodes(); 1.279 + double totalPower = 0.0; 1.280 + for(int i = 0; i < nodes; ++i) { 1.281 + totalPower += GetCPUPowerInWatts(i); 1.282 + } 1.283 + return totalPower; 1.284 +} 1.285 + 1.286 +double 1.287 +IntelPowerGadget::GetCPUPowerInWatts(int node) 1.288 +{ 1.289 + int numResult = 0; 1.290 + double result[] = {0.0, 0.0, 0.0}; 1.291 + if(GetPowerData && cpuMSR != -1) { 1.292 + int ok = GetPowerData(node, cpuMSR, result, &numResult); 1.293 + } 1.294 + return result[0]; 1.295 +} 1.296 + 1.297 +double 1.298 +IntelPowerGadget::GetTotalGPUPowerInWatts() 1.299 +{ 1.300 + int nodes = GetNumberNodes(); 1.301 + double totalPower = 0.0; 1.302 + for(int i = 0; i < nodes; ++i) { 1.303 + totalPower += GetGPUPowerInWatts(i); 1.304 + } 1.305 + return totalPower; 1.306 +} 1.307 + 1.308 +double 1.309 +IntelPowerGadget::GetGPUPowerInWatts(int node) 1.310 +{ 1.311 + return 0.0; 1.312 +} 1.313 +