Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013, Intel Corporation |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | * |
michael@0 | 16 | * Author: Joe Olivas <joseph.k.olivas@intel.com> |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | #include "nsDebug.h" |
michael@0 | 20 | #include "nsString.h" |
michael@0 | 21 | #include "IntelPowerGadget.h" |
michael@0 | 22 | #include "prenv.h" |
michael@0 | 23 | |
michael@0 | 24 | IntelPowerGadget::IntelPowerGadget() : |
michael@0 | 25 | libpowergadget(nullptr), |
michael@0 | 26 | Initialize(nullptr), |
michael@0 | 27 | GetNumNodes(nullptr), |
michael@0 | 28 | GetMsrName(nullptr), |
michael@0 | 29 | GetMsrFunc(nullptr), |
michael@0 | 30 | ReadMSR(nullptr), |
michael@0 | 31 | WriteMSR(nullptr), |
michael@0 | 32 | GetIAFrequency(nullptr), |
michael@0 | 33 | GetTDP(nullptr), |
michael@0 | 34 | GetMaxTemperature(nullptr), |
michael@0 | 35 | GetThresholds(nullptr), |
michael@0 | 36 | GetTemperature(nullptr), |
michael@0 | 37 | ReadSample(nullptr), |
michael@0 | 38 | GetSysTime(nullptr), |
michael@0 | 39 | GetRDTSC(nullptr), |
michael@0 | 40 | GetTimeInterval(nullptr), |
michael@0 | 41 | GetBaseFrequency(nullptr), |
michael@0 | 42 | GetPowerData(nullptr), |
michael@0 | 43 | StartLog(nullptr), |
michael@0 | 44 | StopLog(nullptr), |
michael@0 | 45 | GetNumMsrs(nullptr), |
michael@0 | 46 | packageMSR(-1), |
michael@0 | 47 | cpuMSR(-1), |
michael@0 | 48 | freqMSR(-1), |
michael@0 | 49 | tempMSR(-1) |
michael@0 | 50 | { |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | bool |
michael@0 | 54 | IntelPowerGadget::Init() |
michael@0 | 55 | { |
michael@0 | 56 | bool success = false; |
michael@0 | 57 | const char *path = PR_GetEnv("IPG_Dir"); |
michael@0 | 58 | nsCString ipg_library; |
michael@0 | 59 | if (path && *path) { |
michael@0 | 60 | ipg_library.Append(path); |
michael@0 | 61 | ipg_library.AppendLiteral("/"); |
michael@0 | 62 | ipg_library.AppendLiteral(PG_LIBRARY_NAME); |
michael@0 | 63 | libpowergadget = PR_LoadLibrary(ipg_library.get()); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | if(libpowergadget) { |
michael@0 | 67 | Initialize = (IPGInitialize) PR_FindFunctionSymbol(libpowergadget, "IntelEnergyLibInitialize"); |
michael@0 | 68 | GetNumNodes = (IPGGetNumNodes) PR_FindFunctionSymbol(libpowergadget, "GetNumNodes"); |
michael@0 | 69 | GetMsrName = (IPGGetMsrName) PR_FindFunctionSymbol(libpowergadget, "GetMsrName"); |
michael@0 | 70 | GetMsrFunc = (IPGGetMsrFunc) PR_FindFunctionSymbol(libpowergadget, "GetMsrFunc"); |
michael@0 | 71 | ReadMSR = (IPGReadMSR) PR_FindFunctionSymbol(libpowergadget, "ReadMSR"); |
michael@0 | 72 | WriteMSR = (IPGWriteMSR) PR_FindFunctionSymbol(libpowergadget, "WriteMSR"); |
michael@0 | 73 | GetIAFrequency = (IPGGetIAFrequency) PR_FindFunctionSymbol(libpowergadget, "GetIAFrequency"); |
michael@0 | 74 | GetTDP = (IPGGetTDP) PR_FindFunctionSymbol(libpowergadget, "GetTDP"); |
michael@0 | 75 | GetMaxTemperature = (IPGGetMaxTemperature) PR_FindFunctionSymbol(libpowergadget, "GetMaxTemperature"); |
michael@0 | 76 | GetThresholds = (IPGGetThresholds) PR_FindFunctionSymbol(libpowergadget, "GetThresholds"); |
michael@0 | 77 | GetTemperature = (IPGGetTemperature) PR_FindFunctionSymbol(libpowergadget, "GetTemperature"); |
michael@0 | 78 | ReadSample = (IPGReadSample) PR_FindFunctionSymbol(libpowergadget, "ReadSample"); |
michael@0 | 79 | GetSysTime = (IPGGetSysTime) PR_FindFunctionSymbol(libpowergadget, "GetSysTime"); |
michael@0 | 80 | GetRDTSC = (IPGGetRDTSC) PR_FindFunctionSymbol(libpowergadget, "GetRDTSC"); |
michael@0 | 81 | GetTimeInterval = (IPGGetTimeInterval) PR_FindFunctionSymbol(libpowergadget, "GetTimeInterval"); |
michael@0 | 82 | GetBaseFrequency = (IPGGetBaseFrequency) PR_FindFunctionSymbol(libpowergadget, "GetBaseFrequency"); |
michael@0 | 83 | GetPowerData = (IPGGetPowerData) PR_FindFunctionSymbol(libpowergadget, "GetPowerData"); |
michael@0 | 84 | StartLog = (IPGStartLog) PR_FindFunctionSymbol(libpowergadget, "StartLog"); |
michael@0 | 85 | StopLog = (IPGStopLog) PR_FindFunctionSymbol(libpowergadget, "StopLog"); |
michael@0 | 86 | GetNumMsrs = (IPGGetNumMsrs) PR_FindFunctionSymbol(libpowergadget, "GetNumMsrs"); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | if(Initialize) { |
michael@0 | 90 | Initialize(); |
michael@0 | 91 | int msrCount = GetNumberMsrs(); |
michael@0 | 92 | wchar_t name[1024] = {0}; |
michael@0 | 93 | for(int i = 0; i < msrCount; ++i) { |
michael@0 | 94 | GetMsrName(i, name); |
michael@0 | 95 | int func = 0; |
michael@0 | 96 | GetMsrFunc(i, &func); |
michael@0 | 97 | // MSR for frequency |
michael@0 | 98 | if(wcscmp(name, L"CPU Frequency") == 0 && (func == 0)) { |
michael@0 | 99 | this->freqMSR = i; |
michael@0 | 100 | } |
michael@0 | 101 | // MSR for Package |
michael@0 | 102 | else if(wcscmp(name, L"Processor") == 0 && (func == 1)) { |
michael@0 | 103 | this->packageMSR = i; |
michael@0 | 104 | } |
michael@0 | 105 | // MSR for CPU |
michael@0 | 106 | else if(wcscmp(name, L"IA") == 0 && (func == 1)) { |
michael@0 | 107 | this->cpuMSR = i; |
michael@0 | 108 | } |
michael@0 | 109 | // MSR for Temperature |
michael@0 | 110 | else if(wcscmp(name, L"Package") == 0 && (func == 2)) { |
michael@0 | 111 | this->tempMSR = i; |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | // Grab one sample at startup for a diff |
michael@0 | 115 | TakeSample(); |
michael@0 | 116 | success = true; |
michael@0 | 117 | } |
michael@0 | 118 | return success; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | IntelPowerGadget::~IntelPowerGadget() |
michael@0 | 122 | { |
michael@0 | 123 | if(libpowergadget) { |
michael@0 | 124 | NS_WARNING("Unloading PowerGadget library!\n"); |
michael@0 | 125 | PR_UnloadLibrary(libpowergadget); |
michael@0 | 126 | libpowergadget = nullptr; |
michael@0 | 127 | Initialize = nullptr; |
michael@0 | 128 | GetNumNodes = nullptr; |
michael@0 | 129 | GetMsrName = nullptr; |
michael@0 | 130 | GetMsrFunc = nullptr; |
michael@0 | 131 | ReadMSR = nullptr; |
michael@0 | 132 | WriteMSR = nullptr; |
michael@0 | 133 | GetIAFrequency = nullptr; |
michael@0 | 134 | GetTDP = nullptr; |
michael@0 | 135 | GetMaxTemperature = nullptr; |
michael@0 | 136 | GetThresholds = nullptr; |
michael@0 | 137 | GetTemperature = nullptr; |
michael@0 | 138 | ReadSample = nullptr; |
michael@0 | 139 | GetSysTime = nullptr; |
michael@0 | 140 | GetRDTSC = nullptr; |
michael@0 | 141 | GetTimeInterval = nullptr; |
michael@0 | 142 | GetBaseFrequency = nullptr; |
michael@0 | 143 | GetPowerData = nullptr; |
michael@0 | 144 | StartLog = nullptr; |
michael@0 | 145 | StopLog = nullptr; |
michael@0 | 146 | GetNumMsrs = nullptr; |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | int |
michael@0 | 151 | IntelPowerGadget::GetNumberNodes() |
michael@0 | 152 | { |
michael@0 | 153 | int nodes = 0; |
michael@0 | 154 | if(GetNumNodes) { |
michael@0 | 155 | int ok = GetNumNodes(&nodes); |
michael@0 | 156 | } |
michael@0 | 157 | return nodes; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | int |
michael@0 | 161 | IntelPowerGadget::GetNumberMsrs() |
michael@0 | 162 | { |
michael@0 | 163 | int msrs = 0; |
michael@0 | 164 | if(GetNumMsrs) { |
michael@0 | 165 | int ok = GetNumMsrs(&msrs); |
michael@0 | 166 | } |
michael@0 | 167 | return msrs; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | int |
michael@0 | 171 | IntelPowerGadget::GetCPUFrequency(int node) |
michael@0 | 172 | { |
michael@0 | 173 | int frequency = 0; |
michael@0 | 174 | if(GetIAFrequency) { |
michael@0 | 175 | int ok = GetIAFrequency(node, &frequency); |
michael@0 | 176 | } |
michael@0 | 177 | return frequency; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | double |
michael@0 | 181 | IntelPowerGadget::GetTdp(int node) |
michael@0 | 182 | { |
michael@0 | 183 | double tdp = 0.0; |
michael@0 | 184 | if(GetTDP) { |
michael@0 | 185 | int ok = GetTDP(node, &tdp); |
michael@0 | 186 | } |
michael@0 | 187 | return tdp; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | int |
michael@0 | 191 | IntelPowerGadget::GetMaxTemp(int node) |
michael@0 | 192 | { |
michael@0 | 193 | int maxTemperatureC = 0; |
michael@0 | 194 | if(GetMaxTemperature) { |
michael@0 | 195 | int ok = GetMaxTemperature(node, &maxTemperatureC); |
michael@0 | 196 | } |
michael@0 | 197 | return maxTemperatureC; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | int |
michael@0 | 201 | IntelPowerGadget::GetTemp(int node) |
michael@0 | 202 | { |
michael@0 | 203 | int temperatureC = 0; |
michael@0 | 204 | if(GetTemperature) { |
michael@0 | 205 | int ok = GetTemperature(node, &temperatureC); |
michael@0 | 206 | } |
michael@0 | 207 | return temperatureC; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | int |
michael@0 | 211 | IntelPowerGadget::TakeSample() |
michael@0 | 212 | { |
michael@0 | 213 | int ok = 0; |
michael@0 | 214 | if(ReadSample) { |
michael@0 | 215 | ok = ReadSample(); |
michael@0 | 216 | } |
michael@0 | 217 | return ok; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | uint64_t |
michael@0 | 221 | IntelPowerGadget::GetRdtsc() |
michael@0 | 222 | { |
michael@0 | 223 | uint64_t rdtsc = 0; |
michael@0 | 224 | if(GetRDTSC) { |
michael@0 | 225 | int ok = GetRDTSC(&rdtsc); |
michael@0 | 226 | } |
michael@0 | 227 | return rdtsc; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | double |
michael@0 | 231 | IntelPowerGadget::GetInterval() |
michael@0 | 232 | { |
michael@0 | 233 | double interval = 0.0; |
michael@0 | 234 | if(GetTimeInterval) { |
michael@0 | 235 | int ok = GetTimeInterval(&interval); |
michael@0 | 236 | } |
michael@0 | 237 | return interval; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | double |
michael@0 | 241 | IntelPowerGadget::GetCPUBaseFrequency(int node) |
michael@0 | 242 | { |
michael@0 | 243 | double freq = 0.0; |
michael@0 | 244 | if(GetBaseFrequency) { |
michael@0 | 245 | int ok = GetBaseFrequency(node, &freq); |
michael@0 | 246 | } |
michael@0 | 247 | return freq; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | double |
michael@0 | 251 | IntelPowerGadget::GetTotalPackagePowerInWatts() |
michael@0 | 252 | { |
michael@0 | 253 | int nodes = GetNumberNodes(); |
michael@0 | 254 | double totalPower = 0.0; |
michael@0 | 255 | for(int i = 0; i < nodes; ++i) { |
michael@0 | 256 | totalPower += GetPackagePowerInWatts(i); |
michael@0 | 257 | } |
michael@0 | 258 | return totalPower; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | double |
michael@0 | 262 | IntelPowerGadget::GetPackagePowerInWatts(int node) |
michael@0 | 263 | { |
michael@0 | 264 | int numResult = 0; |
michael@0 | 265 | double result[] = {0.0, 0.0, 0.0}; |
michael@0 | 266 | if(GetPowerData && packageMSR != -1) { |
michael@0 | 267 | int ok = GetPowerData(node, packageMSR, result, &numResult); |
michael@0 | 268 | } |
michael@0 | 269 | return result[0]; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | double |
michael@0 | 273 | IntelPowerGadget::GetTotalCPUPowerInWatts() |
michael@0 | 274 | { |
michael@0 | 275 | int nodes = GetNumberNodes(); |
michael@0 | 276 | double totalPower = 0.0; |
michael@0 | 277 | for(int i = 0; i < nodes; ++i) { |
michael@0 | 278 | totalPower += GetCPUPowerInWatts(i); |
michael@0 | 279 | } |
michael@0 | 280 | return totalPower; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | double |
michael@0 | 284 | IntelPowerGadget::GetCPUPowerInWatts(int node) |
michael@0 | 285 | { |
michael@0 | 286 | int numResult = 0; |
michael@0 | 287 | double result[] = {0.0, 0.0, 0.0}; |
michael@0 | 288 | if(GetPowerData && cpuMSR != -1) { |
michael@0 | 289 | int ok = GetPowerData(node, cpuMSR, result, &numResult); |
michael@0 | 290 | } |
michael@0 | 291 | return result[0]; |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | double |
michael@0 | 295 | IntelPowerGadget::GetTotalGPUPowerInWatts() |
michael@0 | 296 | { |
michael@0 | 297 | int nodes = GetNumberNodes(); |
michael@0 | 298 | double totalPower = 0.0; |
michael@0 | 299 | for(int i = 0; i < nodes; ++i) { |
michael@0 | 300 | totalPower += GetGPUPowerInWatts(i); |
michael@0 | 301 | } |
michael@0 | 302 | return totalPower; |
michael@0 | 303 | } |
michael@0 | 304 | |
michael@0 | 305 | double |
michael@0 | 306 | IntelPowerGadget::GetGPUPowerInWatts(int node) |
michael@0 | 307 | { |
michael@0 | 308 | return 0.0; |
michael@0 | 309 | } |
michael@0 | 310 |