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>
21 #include "IntelPowerGadget.h"
24 IntelPowerGadget::IntelPowerGadget() :
25 libpowergadget(nullptr),
32 GetIAFrequency(nullptr),
34 GetMaxTemperature(nullptr),
35 GetThresholds(nullptr),
36 GetTemperature(nullptr),
40 GetTimeInterval(nullptr),
41 GetBaseFrequency(nullptr),
42 GetPowerData(nullptr),
54 IntelPowerGadget::Init()
57 const char *path
= PR_GetEnv("IPG_Dir");
58 nsCString ipg_library
;
60 ipg_library
.Append(path
);
61 ipg_library
.Append('/');
62 ipg_library
.AppendLiteral(PG_LIBRARY_NAME
);
63 libpowergadget
= PR_LoadLibrary(ipg_library
.get());
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");
91 int msrCount
= GetNumberMsrs();
92 wchar_t name
[1024] = {0};
93 for(int i
= 0; i
< msrCount
; ++i
) {
98 if(wcscmp(name
, L
"CPU Frequency") == 0 && (func
== 0)) {
102 else if(wcscmp(name
, L
"Processor") == 0 && (func
== 1)) {
103 this->packageMSR
= i
;
106 else if(wcscmp(name
, L
"IA") == 0 && (func
== 1)) {
109 // MSR for Temperature
110 else if(wcscmp(name
, L
"Package") == 0 && (func
== 2)) {
114 // Grab one sample at startup for a diff
121 IntelPowerGadget::~IntelPowerGadget()
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;
133 GetIAFrequency
= nullptr;
135 GetMaxTemperature
= nullptr;
136 GetThresholds
= nullptr;
137 GetTemperature
= nullptr;
138 ReadSample
= nullptr;
139 GetSysTime
= nullptr;
141 GetTimeInterval
= nullptr;
142 GetBaseFrequency
= nullptr;
143 GetPowerData
= nullptr;
146 GetNumMsrs
= nullptr;
151 IntelPowerGadget::GetNumberNodes()
155 int ok
= GetNumNodes(&nodes
);
161 IntelPowerGadget::GetNumberMsrs()
165 int ok
= GetNumMsrs(&msrs
);
171 IntelPowerGadget::GetCPUFrequency(int node
)
175 int ok
= GetIAFrequency(node
, &frequency
);
181 IntelPowerGadget::GetTdp(int node
)
185 int ok
= GetTDP(node
, &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;
205 int ok
= GetTemperature(node
, &temperatureC
);
211 IntelPowerGadget::TakeSample()
221 IntelPowerGadget::GetRdtsc()
225 int ok
= GetRDTSC(&rdtsc
);
231 IntelPowerGadget::GetInterval()
233 double interval
= 0.0;
234 if(GetTimeInterval
) {
235 int ok
= GetTimeInterval(&interval
);
241 IntelPowerGadget::GetCPUBaseFrequency(int node
)
244 if(GetBaseFrequency
) {
245 int ok
= GetBaseFrequency(node
, &freq
);
251 IntelPowerGadget::GetTotalPackagePowerInWatts()
253 int nodes
= GetNumberNodes();
254 double totalPower
= 0.0;
255 for(int i
= 0; i
< nodes
; ++i
) {
256 totalPower
+= GetPackagePowerInWatts(i
);
262 IntelPowerGadget::GetPackagePowerInWatts(int node
)
265 double result
[] = {0.0, 0.0, 0.0};
266 if(GetPowerData
&& packageMSR
!= -1) {
267 int ok
= GetPowerData(node
, packageMSR
, result
, &numResult
);
273 IntelPowerGadget::GetTotalCPUPowerInWatts()
275 int nodes
= GetNumberNodes();
276 double totalPower
= 0.0;
277 for(int i
= 0; i
< nodes
; ++i
) {
278 totalPower
+= GetCPUPowerInWatts(i
);
284 IntelPowerGadget::GetCPUPowerInWatts(int node
)
287 double result
[] = {0.0, 0.0, 0.0};
288 if(GetPowerData
&& cpuMSR
!= -1) {
289 int ok
= GetPowerData(node
, cpuMSR
, result
, &numResult
);
295 IntelPowerGadget::GetTotalGPUPowerInWatts()
297 int nodes
= GetNumberNodes();
298 double totalPower
= 0.0;
299 for(int i
= 0; i
< nodes
; ++i
) {
300 totalPower
+= GetGPUPowerInWatts(i
);
306 IntelPowerGadget::GetGPUPowerInWatts(int node
)