|
1 /* |
|
2 * Copyright 2013, Intel Corporation |
|
3 * |
|
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 |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
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. |
|
15 * |
|
16 * Author: Joe Olivas <joseph.k.olivas@intel.com> |
|
17 */ |
|
18 |
|
19 #include "nsDebug.h" |
|
20 #include "nsString.h" |
|
21 #include "IntelPowerGadget.h" |
|
22 #include "prenv.h" |
|
23 |
|
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) |
|
50 { |
|
51 } |
|
52 |
|
53 bool |
|
54 IntelPowerGadget::Init() |
|
55 { |
|
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.AppendLiteral("/"); |
|
62 ipg_library.AppendLiteral(PG_LIBRARY_NAME); |
|
63 libpowergadget = PR_LoadLibrary(ipg_library.get()); |
|
64 } |
|
65 |
|
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"); |
|
87 } |
|
88 |
|
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; |
|
100 } |
|
101 // MSR for Package |
|
102 else if(wcscmp(name, L"Processor") == 0 && (func == 1)) { |
|
103 this->packageMSR = i; |
|
104 } |
|
105 // MSR for CPU |
|
106 else if(wcscmp(name, L"IA") == 0 && (func == 1)) { |
|
107 this->cpuMSR = i; |
|
108 } |
|
109 // MSR for Temperature |
|
110 else if(wcscmp(name, L"Package") == 0 && (func == 2)) { |
|
111 this->tempMSR = i; |
|
112 } |
|
113 } |
|
114 // Grab one sample at startup for a diff |
|
115 TakeSample(); |
|
116 success = true; |
|
117 } |
|
118 return success; |
|
119 } |
|
120 |
|
121 IntelPowerGadget::~IntelPowerGadget() |
|
122 { |
|
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; |
|
147 } |
|
148 } |
|
149 |
|
150 int |
|
151 IntelPowerGadget::GetNumberNodes() |
|
152 { |
|
153 int nodes = 0; |
|
154 if(GetNumNodes) { |
|
155 int ok = GetNumNodes(&nodes); |
|
156 } |
|
157 return nodes; |
|
158 } |
|
159 |
|
160 int |
|
161 IntelPowerGadget::GetNumberMsrs() |
|
162 { |
|
163 int msrs = 0; |
|
164 if(GetNumMsrs) { |
|
165 int ok = GetNumMsrs(&msrs); |
|
166 } |
|
167 return msrs; |
|
168 } |
|
169 |
|
170 int |
|
171 IntelPowerGadget::GetCPUFrequency(int node) |
|
172 { |
|
173 int frequency = 0; |
|
174 if(GetIAFrequency) { |
|
175 int ok = GetIAFrequency(node, &frequency); |
|
176 } |
|
177 return frequency; |
|
178 } |
|
179 |
|
180 double |
|
181 IntelPowerGadget::GetTdp(int node) |
|
182 { |
|
183 double tdp = 0.0; |
|
184 if(GetTDP) { |
|
185 int ok = GetTDP(node, &tdp); |
|
186 } |
|
187 return tdp; |
|
188 } |
|
189 |
|
190 int |
|
191 IntelPowerGadget::GetMaxTemp(int node) |
|
192 { |
|
193 int maxTemperatureC = 0; |
|
194 if(GetMaxTemperature) { |
|
195 int ok = GetMaxTemperature(node, &maxTemperatureC); |
|
196 } |
|
197 return maxTemperatureC; |
|
198 } |
|
199 |
|
200 int |
|
201 IntelPowerGadget::GetTemp(int node) |
|
202 { |
|
203 int temperatureC = 0; |
|
204 if(GetTemperature) { |
|
205 int ok = GetTemperature(node, &temperatureC); |
|
206 } |
|
207 return temperatureC; |
|
208 } |
|
209 |
|
210 int |
|
211 IntelPowerGadget::TakeSample() |
|
212 { |
|
213 int ok = 0; |
|
214 if(ReadSample) { |
|
215 ok = ReadSample(); |
|
216 } |
|
217 return ok; |
|
218 } |
|
219 |
|
220 uint64_t |
|
221 IntelPowerGadget::GetRdtsc() |
|
222 { |
|
223 uint64_t rdtsc = 0; |
|
224 if(GetRDTSC) { |
|
225 int ok = GetRDTSC(&rdtsc); |
|
226 } |
|
227 return rdtsc; |
|
228 } |
|
229 |
|
230 double |
|
231 IntelPowerGadget::GetInterval() |
|
232 { |
|
233 double interval = 0.0; |
|
234 if(GetTimeInterval) { |
|
235 int ok = GetTimeInterval(&interval); |
|
236 } |
|
237 return interval; |
|
238 } |
|
239 |
|
240 double |
|
241 IntelPowerGadget::GetCPUBaseFrequency(int node) |
|
242 { |
|
243 double freq = 0.0; |
|
244 if(GetBaseFrequency) { |
|
245 int ok = GetBaseFrequency(node, &freq); |
|
246 } |
|
247 return freq; |
|
248 } |
|
249 |
|
250 double |
|
251 IntelPowerGadget::GetTotalPackagePowerInWatts() |
|
252 { |
|
253 int nodes = GetNumberNodes(); |
|
254 double totalPower = 0.0; |
|
255 for(int i = 0; i < nodes; ++i) { |
|
256 totalPower += GetPackagePowerInWatts(i); |
|
257 } |
|
258 return totalPower; |
|
259 } |
|
260 |
|
261 double |
|
262 IntelPowerGadget::GetPackagePowerInWatts(int node) |
|
263 { |
|
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); |
|
268 } |
|
269 return result[0]; |
|
270 } |
|
271 |
|
272 double |
|
273 IntelPowerGadget::GetTotalCPUPowerInWatts() |
|
274 { |
|
275 int nodes = GetNumberNodes(); |
|
276 double totalPower = 0.0; |
|
277 for(int i = 0; i < nodes; ++i) { |
|
278 totalPower += GetCPUPowerInWatts(i); |
|
279 } |
|
280 return totalPower; |
|
281 } |
|
282 |
|
283 double |
|
284 IntelPowerGadget::GetCPUPowerInWatts(int node) |
|
285 { |
|
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); |
|
290 } |
|
291 return result[0]; |
|
292 } |
|
293 |
|
294 double |
|
295 IntelPowerGadget::GetTotalGPUPowerInWatts() |
|
296 { |
|
297 int nodes = GetNumberNodes(); |
|
298 double totalPower = 0.0; |
|
299 for(int i = 0; i < nodes; ++i) { |
|
300 totalPower += GetGPUPowerInWatts(i); |
|
301 } |
|
302 return totalPower; |
|
303 } |
|
304 |
|
305 double |
|
306 IntelPowerGadget::GetGPUPowerInWatts(int node) |
|
307 { |
|
308 return 0.0; |
|
309 } |
|
310 |