Bug 1079322 - Extract properties using libc functions. r=gwagner
[gecko.git] / tools / profiler / IntelPowerGadget.cpp
blobfe267b80f359ef6a15fa2652c14f14c6822a8e39
1 /*
2 * Copyright 2013, Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * Author: Joe Olivas <joseph.k.olivas@intel.com>
19 #include "nsDebug.h"
20 #include "nsString.h"
21 #include "IntelPowerGadget.h"
22 #include "prenv.h"
24 IntelPowerGadget::IntelPowerGadget() :
25 libpowergadget(nullptr),
26 Initialize(nullptr),
27 GetNumNodes(nullptr),
28 GetMsrName(nullptr),
29 GetMsrFunc(nullptr),
30 ReadMSR(nullptr),
31 WriteMSR(nullptr),
32 GetIAFrequency(nullptr),
33 GetTDP(nullptr),
34 GetMaxTemperature(nullptr),
35 GetThresholds(nullptr),
36 GetTemperature(nullptr),
37 ReadSample(nullptr),
38 GetSysTime(nullptr),
39 GetRDTSC(nullptr),
40 GetTimeInterval(nullptr),
41 GetBaseFrequency(nullptr),
42 GetPowerData(nullptr),
43 StartLog(nullptr),
44 StopLog(nullptr),
45 GetNumMsrs(nullptr),
46 packageMSR(-1),
47 cpuMSR(-1),
48 freqMSR(-1),
49 tempMSR(-1)
53 bool
54 IntelPowerGadget::Init()
56 bool success = false;
57 const char *path = PR_GetEnv("IPG_Dir");
58 nsCString ipg_library;
59 if (path && *path) {
60 ipg_library.Append(path);
61 ipg_library.Append('/');
62 ipg_library.AppendLiteral(PG_LIBRARY_NAME);
63 libpowergadget = PR_LoadLibrary(ipg_library.get());
66 if(libpowergadget) {
67 Initialize = (IPGInitialize) PR_FindFunctionSymbol(libpowergadget, "IntelEnergyLibInitialize");
68 GetNumNodes = (IPGGetNumNodes) PR_FindFunctionSymbol(libpowergadget, "GetNumNodes");
69 GetMsrName = (IPGGetMsrName) PR_FindFunctionSymbol(libpowergadget, "GetMsrName");
70 GetMsrFunc = (IPGGetMsrFunc) PR_FindFunctionSymbol(libpowergadget, "GetMsrFunc");
71 ReadMSR = (IPGReadMSR) PR_FindFunctionSymbol(libpowergadget, "ReadMSR");
72 WriteMSR = (IPGWriteMSR) PR_FindFunctionSymbol(libpowergadget, "WriteMSR");
73 GetIAFrequency = (IPGGetIAFrequency) PR_FindFunctionSymbol(libpowergadget, "GetIAFrequency");
74 GetTDP = (IPGGetTDP) PR_FindFunctionSymbol(libpowergadget, "GetTDP");
75 GetMaxTemperature = (IPGGetMaxTemperature) PR_FindFunctionSymbol(libpowergadget, "GetMaxTemperature");
76 GetThresholds = (IPGGetThresholds) PR_FindFunctionSymbol(libpowergadget, "GetThresholds");
77 GetTemperature = (IPGGetTemperature) PR_FindFunctionSymbol(libpowergadget, "GetTemperature");
78 ReadSample = (IPGReadSample) PR_FindFunctionSymbol(libpowergadget, "ReadSample");
79 GetSysTime = (IPGGetSysTime) PR_FindFunctionSymbol(libpowergadget, "GetSysTime");
80 GetRDTSC = (IPGGetRDTSC) PR_FindFunctionSymbol(libpowergadget, "GetRDTSC");
81 GetTimeInterval = (IPGGetTimeInterval) PR_FindFunctionSymbol(libpowergadget, "GetTimeInterval");
82 GetBaseFrequency = (IPGGetBaseFrequency) PR_FindFunctionSymbol(libpowergadget, "GetBaseFrequency");
83 GetPowerData = (IPGGetPowerData) PR_FindFunctionSymbol(libpowergadget, "GetPowerData");
84 StartLog = (IPGStartLog) PR_FindFunctionSymbol(libpowergadget, "StartLog");
85 StopLog = (IPGStopLog) PR_FindFunctionSymbol(libpowergadget, "StopLog");
86 GetNumMsrs = (IPGGetNumMsrs) PR_FindFunctionSymbol(libpowergadget, "GetNumMsrs");
89 if(Initialize) {
90 Initialize();
91 int msrCount = GetNumberMsrs();
92 wchar_t name[1024] = {0};
93 for(int i = 0; i < msrCount; ++i) {
94 GetMsrName(i, name);
95 int func = 0;
96 GetMsrFunc(i, &func);
97 // MSR for frequency
98 if(wcscmp(name, L"CPU Frequency") == 0 && (func == 0)) {
99 this->freqMSR = i;
101 // MSR for Package
102 else if(wcscmp(name, L"Processor") == 0 && (func == 1)) {
103 this->packageMSR = i;
105 // MSR for CPU
106 else if(wcscmp(name, L"IA") == 0 && (func == 1)) {
107 this->cpuMSR = i;
109 // MSR for Temperature
110 else if(wcscmp(name, L"Package") == 0 && (func == 2)) {
111 this->tempMSR = i;
114 // Grab one sample at startup for a diff
115 TakeSample();
116 success = true;
118 return success;
121 IntelPowerGadget::~IntelPowerGadget()
123 if(libpowergadget) {
124 NS_WARNING("Unloading PowerGadget library!\n");
125 PR_UnloadLibrary(libpowergadget);
126 libpowergadget = nullptr;
127 Initialize = nullptr;
128 GetNumNodes = nullptr;
129 GetMsrName = nullptr;
130 GetMsrFunc = nullptr;
131 ReadMSR = nullptr;
132 WriteMSR = nullptr;
133 GetIAFrequency = nullptr;
134 GetTDP = nullptr;
135 GetMaxTemperature = nullptr;
136 GetThresholds = nullptr;
137 GetTemperature = nullptr;
138 ReadSample = nullptr;
139 GetSysTime = nullptr;
140 GetRDTSC = nullptr;
141 GetTimeInterval = nullptr;
142 GetBaseFrequency = nullptr;
143 GetPowerData = nullptr;
144 StartLog = nullptr;
145 StopLog = nullptr;
146 GetNumMsrs = nullptr;
151 IntelPowerGadget::GetNumberNodes()
153 int nodes = 0;
154 if(GetNumNodes) {
155 int ok = GetNumNodes(&nodes);
157 return nodes;
161 IntelPowerGadget::GetNumberMsrs()
163 int msrs = 0;
164 if(GetNumMsrs) {
165 int ok = GetNumMsrs(&msrs);
167 return msrs;
171 IntelPowerGadget::GetCPUFrequency(int node)
173 int frequency = 0;
174 if(GetIAFrequency) {
175 int ok = GetIAFrequency(node, &frequency);
177 return frequency;
180 double
181 IntelPowerGadget::GetTdp(int node)
183 double tdp = 0.0;
184 if(GetTDP) {
185 int ok = GetTDP(node, &tdp);
187 return tdp;
191 IntelPowerGadget::GetMaxTemp(int node)
193 int maxTemperatureC = 0;
194 if(GetMaxTemperature) {
195 int ok = GetMaxTemperature(node, &maxTemperatureC);
197 return maxTemperatureC;
201 IntelPowerGadget::GetTemp(int node)
203 int temperatureC = 0;
204 if(GetTemperature) {
205 int ok = GetTemperature(node, &temperatureC);
207 return temperatureC;
211 IntelPowerGadget::TakeSample()
213 int ok = 0;
214 if(ReadSample) {
215 ok = ReadSample();
217 return ok;
220 uint64_t
221 IntelPowerGadget::GetRdtsc()
223 uint64_t rdtsc = 0;
224 if(GetRDTSC) {
225 int ok = GetRDTSC(&rdtsc);
227 return rdtsc;
230 double
231 IntelPowerGadget::GetInterval()
233 double interval = 0.0;
234 if(GetTimeInterval) {
235 int ok = GetTimeInterval(&interval);
237 return interval;
240 double
241 IntelPowerGadget::GetCPUBaseFrequency(int node)
243 double freq = 0.0;
244 if(GetBaseFrequency) {
245 int ok = GetBaseFrequency(node, &freq);
247 return freq;
250 double
251 IntelPowerGadget::GetTotalPackagePowerInWatts()
253 int nodes = GetNumberNodes();
254 double totalPower = 0.0;
255 for(int i = 0; i < nodes; ++i) {
256 totalPower += GetPackagePowerInWatts(i);
258 return totalPower;
261 double
262 IntelPowerGadget::GetPackagePowerInWatts(int node)
264 int numResult = 0;
265 double result[] = {0.0, 0.0, 0.0};
266 if(GetPowerData && packageMSR != -1) {
267 int ok = GetPowerData(node, packageMSR, result, &numResult);
269 return result[0];
272 double
273 IntelPowerGadget::GetTotalCPUPowerInWatts()
275 int nodes = GetNumberNodes();
276 double totalPower = 0.0;
277 for(int i = 0; i < nodes; ++i) {
278 totalPower += GetCPUPowerInWatts(i);
280 return totalPower;
283 double
284 IntelPowerGadget::GetCPUPowerInWatts(int node)
286 int numResult = 0;
287 double result[] = {0.0, 0.0, 0.0};
288 if(GetPowerData && cpuMSR != -1) {
289 int ok = GetPowerData(node, cpuMSR, result, &numResult);
291 return result[0];
294 double
295 IntelPowerGadget::GetTotalGPUPowerInWatts()
297 int nodes = GetNumberNodes();
298 double totalPower = 0.0;
299 for(int i = 0; i < nodes; ++i) {
300 totalPower += GetGPUPowerInWatts(i);
302 return totalPower;
305 double
306 IntelPowerGadget::GetGPUPowerInWatts(int node)
308 return 0.0;