1 /*
2 Copyright (c) 2009-2019, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9     * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 //
14 // monitor CPU conters for ksysguard
15 //
16 // contact: Thomas Willhalm, Patrick Ungerer, Roman Dementiev
17 //
18 // This program is not a tutorial on how to write nice interpreters
19 // but a proof of concept on using ksysguard with performance counters
20 //
21 
22 /*!     \file pcm-sensor.cpp
23         \brief Example of using CPU counters: implements a graphical plugin for KDE ksysguard
24 */
25 #include <iostream>
26 #include <string>
27 #include <sstream>
28 #include "cpuasynchcounter.h"
29 #include "utils.h"
30 
31 using namespace std;
32 using namespace pcm;
33 
main()34 int main()
35 {
36     set_signal_handlers();
37 
38     AsynchronCounterState counters;
39 
40     cout << "CPU counter sensor " << PCM_VERSION << "\n";
41     cout << "ksysguardd 1.2.0\n";
42     cout << "ksysguardd> ";
43 
44     while (1)
45     {
46         string s;
47         cin >> s;
48 
49         // list counters
50         if (s == "monitors") {
51             for (uint32 i = 0; i < counters.getNumCores(); ++i) {
52                 for (uint32 a = 0; a < counters.getNumSockets(); ++a)
53                     if (a == counters.getSocketId(i)) {
54                         cout << "Socket" << a << "/CPU" << i << "/Frequency\tfloat\n";
55                         cout << "Socket" << a << "/CPU" << i << "/IPC\tfloat\n";
56                         cout << "Socket" << a << "/CPU" << i << "/L2CacheHitRatio\tfloat\n";
57                         cout << "Socket" << a << "/CPU" << i << "/L3CacheHitRatio\tfloat\n";
58                         cout << "Socket" << a << "/CPU" << i << "/L2CacheMisses\tinteger\n";
59                         cout << "Socket" << a << "/CPU" << i << "/L3CacheMisses\tinteger\n";
60                         cout << "Socket" << a << "/CPU" << i << "/L3Occupancy\tfloat\n";
61                         cout << "Socket" << a << "/CPU" << i << "/LocalMemoryBandwidth\tfloat\n";
62                         cout << "Socket" << a << "/CPU" << i << "/RemoteMemoryBandwidth\tfloat\n";
63                         cout << "Socket" << a << "/CPU" << i << "/CoreC0StateResidency\tfloat\n";
64                         cout << "Socket" << a << "/CPU" << i << "/CoreC3StateResidency\tfloat\n";
65                         cout << "Socket" << a << "/CPU" << i << "/CoreC6StateResidency\tfloat\n";
66                         cout << "Socket" << a << "/CPU" << i << "/CoreC7StateResidency\tfloat\n";
67                         cout << "Socket" << a << "/CPU" << i << "/ThermalHeadroom\tinteger\n";
68                     }
69             }
70             for (uint32 a = 0; a < counters.getNumSockets(); ++a) {
71                 cout << "Socket" << a << "/BytesReadFromMC\tfloat\n";
72                 cout << "Socket" << a << "/BytesWrittenToMC\tfloat\n";
73                 cout << "Socket" << a << "/BytesReadFromPMM\tfloat\n";
74                 cout << "Socket" << a << "/BytesWrittenToPMM\tfloat\n";
75                 cout << "Socket" << a << "/Frequency\tfloat\n";
76                 cout << "Socket" << a << "/IPC\tfloat\n";
77                 cout << "Socket" << a << "/L2CacheHitRatio\tfloat\n";
78                 cout << "Socket" << a << "/L3CacheHitRatio\tfloat\n";
79                 cout << "Socket" << a << "/L2CacheMisses\tinteger\n";
80                 cout << "Socket" << a << "/L3CacheMisses\tinteger\n";
81                 cout << "Socket" << a << "/L3Occupancy\tfloat\n";
82                 cout << "Socket" << a << "/LocalMemoryBandwidth\tfloat\n";
83                 cout << "Socket" << a << "/RemoteMemoryBandwidth\tfloat\n";
84                 cout << "Socket" << a << "/CoreC0StateResidency\tfloat\n";
85                 cout << "Socket" << a << "/CoreC3StateResidency\tfloat\n";
86                 cout << "Socket" << a << "/CoreC6StateResidency\tfloat\n";
87                 cout << "Socket" << a << "/CoreC7StateResidency\tfloat\n";
88                 cout << "Socket" << a << "/PackageC2StateResidency\tfloat\n";
89                 cout << "Socket" << a << "/PackageC3StateResidency\tfloat\n";
90                 cout << "Socket" << a << "/PackageC6StateResidency\tfloat\n";
91                 cout << "Socket" << a << "/PackageC7StateResidency\tfloat\n";
92                 cout << "Socket" << a << "/ThermalHeadroom\tinteger\n";
93                 cout << "Socket" << a << "/CPUEnergy\tfloat\n";
94                 cout << "Socket" << a << "/DRAMEnergy\tfloat\n";
95             }
96             for (uint32 a = 0; a < counters.getNumSockets(); ++a) {
97                 for (uint32 l = 0; l < counters.getQPILinksPerSocket(); ++l)
98                     cout << "Socket" << a << "/BytesIncomingToQPI" << l << "\tfloat\n";
99             }
100 
101             cout << "QPI_Traffic\tfloat\n";
102             cout << "Frequency\tfloat\n";
103             cout << "IPC\tfloat\n";       //double check output
104             cout << "L2CacheHitRatio\tfloat\n";
105             cout << "L3CacheHitRatio\tfloat\n";
106             cout << "L2CacheMisses\tinteger\n";
107             cout << "L3CacheMisses\tinteger\n";
108             cout << "CoreC0StateResidency\tfloat\n";
109             cout << "CoreC3StateResidency\tfloat\n";
110             cout << "CoreC6StateResidency\tfloat\n";
111             cout << "CoreC7StateResidency\tfloat\n";
112             cout << "PackageC2StateResidency\tfloat\n";
113             cout << "PackageC3StateResidency\tfloat\n";
114             cout << "PackageC6StateResidency\tfloat\n";
115             cout << "PackageC7StateResidency\tfloat\n";
116             cout << "CPUEnergy\tfloat\n";
117             cout << "DRAMEnergy\tfloat\n";
118         }
119 
120         // provide metadata
121 
122         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
123             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
124                 if (a == counters.getSocketId(i)) {
125                     {
126                         stringstream c;
127                         c << "Socket" << a << "/CPU" << i << "/Frequency?";
128                         if (s == c.str()) {
129                             cout << "FREQ. CPU" << i << "\t\t\tMHz\n";
130                         }
131                     }
132                     {
133                         stringstream c;
134                         c << "Socket" << a << "/CPU" << i << "/ThermalHeadroom?";
135                         if (s == c.str()) {
136                             cout << "Temperature reading in 1 degree Celsius relative to the TjMax temperature (thermal headroom) for CPU" << i << "\t\t\t°C\n";
137                         }
138                     }
139                     {
140                         stringstream c;
141                         c << "Socket" << a << "/CPU" << i << "/CoreC0StateResidency?";
142                         if (s == c.str()) {
143                             cout << "core C0-state residency for CPU" << i << "\t\t\t%\n";
144                         }
145                     }
146                     {
147                         stringstream c;
148                         c << "Socket" << a << "/CPU" << i << "/CoreC3StateResidency?";
149                         if (s == c.str()) {
150                             cout << "core C3-state residency for CPU" << i << "\t\t\t%\n";
151                         }
152                     }
153                     {
154                         stringstream c;
155                         c << "Socket" << a << "/CPU" << i << "/CoreC6StateResidency?";
156                         if (s == c.str()) {
157                             cout << "core C6-state residency for CPU" << i << "\t\t\t%\n";
158                         }
159                     }
160                     {
161                         stringstream c;
162                         c << "Socket" << a << "/CPU" << i << "/CoreC7StateResidency?";
163                         if (s == c.str()) {
164                             cout << "core C7-state residency for CPU" << i << "\t\t\t%\n";
165                         }
166                     }
167                 }
168         }
169         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
170             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
171                 if (a == counters.getSocketId(i)) {
172                     stringstream c;
173                     c << "Socket" << a << "/CPU" << i << "/IPC?";
174                     if (s == c.str()) {
175                         cout << "IPC CPU" << i << "\t0\t\t\n";
176                         //cout << "CPU" << i << "\tInstructions per Cycle\t0\t1\t \n";
177                     }
178                 }
179         }
180         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
181             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
182                 if (a == counters.getSocketId(i)) {
183                     stringstream c;
184                     c << "Socket" << a << "/CPU" << i << "/L2CacheHitRatio?";
185                     if (s == c.str()) {
186                         cout << "L2 Cache Hit Ratio CPU" << i << "\t0\t\t\n";
187                         //   cout << "CPU" << i << "\tL2 Cache Hit Ratio\t0\t1\t \n";
188                     }
189                 }
190         }
191         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
192             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
193                 if (a == counters.getSocketId(i)) {
194                     stringstream c;
195                     c << "Socket" << a << "/CPU" << i << "/L3CacheHitRatio?";
196                     if (s == c.str()) {
197                         cout << "L3 Cache Hit Ratio CPU" << i << "\t0\t\t \n";
198                     }
199                 }
200         }
201         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
202             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
203                 if (a == counters.getSocketId(i)) {
204                     stringstream c;
205                     c << "Socket" << a << "/CPU" << i << "/L2CacheMisses?";
206                     if (s == c.str()) {
207                         cout << "L2 Cache Misses CPU" << i << "\t0\t\t \n";
208                         //cout << "CPU" << i << "\tL2 Cache Misses\t0\t1\t \n";
209                     }
210                 }
211         }
212         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
213             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
214                 if (a == counters.getSocketId(i)) {
215                     stringstream c;
216                     c << "Socket" << a << "/CPU" << i << "/L3CacheMisses?";
217                     if (s == c.str()) {
218                         cout << "L3 Cache Misses CPU" << i << "\t0\t\t \n";
219                         //cout << "CPU" << i << "\tL3 Cache Misses\t0\t1\t \n";
220                     }
221                 }
222         }
223         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
224             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
225                 if (a == counters.getSocketId(i)) {
226                     stringstream c;
227                     c << "Socket" << a << "/CPU" << i << "/L3Occupancy?";
228                     if (s == c.str()) {
229                         cout << "L3 Cache Occupancy CPU " << i << "\t0\t\t \n";
230                         //cout << "CPU" << i << "\tL3 Cache Occupancy\t0\t1\t \n";
231                     }
232                 }
233         }
234         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
235             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
236                 if (a == counters.getSocketId(i)) {
237                     stringstream c;
238                     c << "Socket" << a << "/CPU" << i << "/LocalMemoryBandwidth?";
239                     if (s == c.str()) {
240                         cout << "Local Memory Bandwidth CPU " << i << "\t0\t\t \n";
241                         //cout << "CPU" << i << "\tLocal Memory Bandwidth\t0\t1\t \n";
242                     }
243                 }
244         }
245         for (uint32 i = 0; i < counters.getNumCores(); ++i) {
246             for (uint32 a = 0; a < counters.getNumSockets(); ++a)
247                 if (a == counters.getSocketId(i)) {
248                     stringstream c;
249                     c << "Socket" << a << "/CPU" << i << "/RemoteMemoryBandwidth?";
250                     if (s == c.str()) {
251                         cout << "Remote Memory Bandwidth CPU " << i << "\t0\t\t \n";
252                         //cout << "CPU" << i << "\tRemote Memory Bandwidth\t0\t1\t \n";
253                     }
254                 }
255         }
256         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
257             stringstream c;
258             c << "Socket" << i << "/BytesReadFromMC?";
259             if (s == c.str()) {
260                 cout << "read from MC Socket" << i << "\t0\t\tGB\n";
261             }
262         }
263         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
264             stringstream c;
265             c << "Socket" << i << "/BytesReadFromPMM?";
266             if (s == c.str()) {
267                 cout << "read from PMM memory on Socket" << i << "\t0\t\tGB\n";
268             }
269         }
270         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
271             stringstream c;
272             c << "Socket" << i << "/DRAMEnergy?";
273             if (s == c.str()) {
274                 cout << "Energy consumed by DRAM on socket " << i << "\t0\t\tJoule\n";
275             }
276         }
277         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
278             stringstream c;
279             c << "Socket" << i << "/CPUEnergy?";
280             if (s == c.str()) {
281                 cout << "Energy consumed by CPU package " << i << "\t0\t\tJoule\n";
282             }
283         }
284         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
285             stringstream c;
286             c << "Socket" << i << "/ThermalHeadroom?";
287             if (s == c.str()) {
288                 cout << "Temperature reading in 1 degree Celsius relative to the TjMax temperature (thermal headroom) for CPU package " << i << "\t0\t\t°C\n";
289             }
290         }
291         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
292             stringstream c;
293             c << "Socket" << i << "/CoreC0StateResidency?";
294             if (s == c.str()) {
295                 cout << "core C0-state residency for CPU package " << i << "\t0\t\t%\n";
296             }
297         }
298         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
299             stringstream c;
300             c << "Socket" << i << "/CoreC3StateResidency?";
301             if (s == c.str()) {
302                 cout << "core C3-state residency for CPU package " << i << "\t0\t\t%\n";
303             }
304         }
305         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
306             stringstream c;
307             c << "Socket" << i << "/CoreC6StateResidency?";
308             if (s == c.str()) {
309                 cout << "core C6-state residency for CPU package " << i << "\t0\t\t%\n";
310             }
311         }
312         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
313             stringstream c;
314             c << "Socket" << i << "/CoreC7StateResidency?";
315             if (s == c.str()) {
316                 cout << "core C7-state residency for CPU package " << i << "\t0\t\t%\n";
317             }
318         }
319         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
320             stringstream c;
321             c << "Socket" << i << "/PackageC2StateResidency?";
322             if (s == c.str()) {
323                 cout << "package C2-state residency for CPU package " << i << "\t0\t\t%\n";
324             }
325         }
326         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
327             stringstream c;
328             c << "Socket" << i << "/PackageC3StateResidency?";
329             if (s == c.str()) {
330                 cout << "package C3-state residency for CPU package " << i << "\t0\t\t%\n";
331             }
332         }
333         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
334             stringstream c;
335             c << "Socket" << i << "/PackageC6StateResidency?";
336             if (s == c.str()) {
337                 cout << "package C6-state residency for CPU package " << i << "\t0\t\t%\n";
338             }
339         }
340         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
341             stringstream c;
342             c << "Socket" << i << "/PackageC7StateResidency?";
343             if (s == c.str()) {
344                 cout << "package C7-state residency for CPU package " << i << "\t0\t\t%\n";
345             }
346         }
347         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
348             stringstream c;
349             c << "Socket" << i << "/BytesWrittenToPMM?";
350             if (s == c.str()) {
351                 cout << "written to PMM memory on Socket" << i << "\t0\t\tGB\n";
352                 //cout << "CPU" << i << "\tBytes written to memory channel\t0\t1\t GB\n";
353             }
354         }
355 
356         for (uint32 l = 0; l < counters.getQPILinksPerSocket(); ++l) {
357             for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
358                 stringstream c;
359                 c << "Socket" << i << "/BytesIncomingToQPI" << l << "?";
360                 if (s == c.str()) {
361                     //cout << "Socket" << i << "\tBytes incoming to QPI link\t" << l<< "\t\t GB\n";
362                     cout << "incoming to Socket" << i << " QPI Link" << l << "\t0\t\tGB\n";
363                 }
364             }
365         }
366 
367         {
368             stringstream c;
369             c << "QPI_Traffic?";
370             if (s == c.str()) {
371                 cout << "Traffic on all QPIs\t0\t\tGB\n";
372             }
373         }
374 
375         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
376             stringstream c;
377             c << "Socket" << i << "/Frequency?";
378             if (s == c.str()) {
379                 cout << "Socket" << i << " Frequency\t0\t\tMHz\n";
380             }
381         }
382 
383         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
384             stringstream c;
385             c << "Socket" << i << "/IPC?";
386             if (s == c.str()) {
387                 cout << "Socket" << i << " IPC\t0\t\t\n";
388             }
389         }
390 
391         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
392             stringstream c;
393             c << "Socket" << i << "/L2CacheHitRatio?";
394             if (s == c.str()) {
395                 cout << "Socket" << i << " L2 Cache Hit Ratio\t0\t\t\n";
396             }
397         }
398 
399         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
400             stringstream c;
401             c << "Socket" << i << "/L3CacheHitRatio?";
402             if (s == c.str()) {
403                 cout << "Socket" << i << " L3 Cache Hit Ratio\t0\t\t\n";
404             }
405         }
406 
407         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
408             stringstream c;
409             c << "Socket" << i << "/L2CacheMisses?";
410             if (s == c.str()) {
411                 cout << "Socket" << i << " L2 Cache Misses\t0\t\t\n";
412             }
413         }
414 
415         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
416             stringstream c;
417             c << "Socket" << i << "/L3CacheMisses?";
418             if (s == c.str()) {
419                 cout << "Socket" << i << " L3 Cache Misses\t0\t\t\n";
420             }
421         }
422 
423         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
424             stringstream c;
425             c << "Socket" << i << "/L3Occupancy";
426             if (s == c.str()) {
427                 cout << "Socket" << i << " L3 Cache Occupancy\t0\t\t\n";
428             }
429         }
430 
431         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
432             stringstream c;
433             c << "Socket" << i << "/LocalMemoryBandwidth";
434             if (s == c.str()) {
435                 cout << "Socket" << i << " Local Memory Bandwidth\t0\t\t\n";
436             }
437         }
438 
439         for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
440             stringstream c;
441             c << "Socket" << i << "/RemoteMemoryBandwidth";
442             if (s == c.str()) {
443                 cout << "Socket" << i << " Remote Memory Bandwidth\t0\t\t\n";
444             }
445         }
446 
447         {
448             stringstream c;
449             c << "Frequency?";
450             if (s == c.str()) {
451                 cout << "Frequency system wide\t0\t\tMhz\n";
452             }
453         }
454 
455         {
456             stringstream c;
457             c << "IPC?";
458             if (s == c.str()) {
459                 cout << "IPC system wide\t0\t\t\n";
460             }
461         }
462 
463         {
464             stringstream c;
465             c << "L2CacheHitRatio?";
466             if (s == c.str()) {
467                 cout << "System wide L2 Cache Hit Ratio\t0\t\t\n";
468             }
469         }
470 
471         {
472             stringstream c;
473             c << "L3CacheHitRatio?";
474             if (s == c.str()) {
475                 cout << "System wide L3 Cache Hit Ratio\t0\t\t\n";
476             }
477         }
478 
479         {
480             stringstream c;
481             c << "L2CacheMisses?";
482             if (s == c.str()) {
483                 cout << "System wide L2 Cache Misses\t0\t\t\n";
484             }
485         }
486 
487         {
488             stringstream c;
489             c << "L3CacheMisses?";
490             if (s == c.str()) {
491                 cout << "System wide L3 Cache Misses\t0\t\t\n";
492             }
493         }
494 
495         {
496             stringstream c;
497             c << "L3CacheMisses?";
498             if (s == c.str()) {
499                 cout << "System wide L3 Cache Misses\t0\t\t\n";
500             }
501         }
502 
503         {
504             stringstream c;
505             c << "DRAMEnergy?";
506             if (s == c.str()) {
507                 cout << "System wide energy consumed by DRAM \t0\t\tJoule\n";
508             }
509         }
510         {
511             stringstream c;
512             c << "CPUEnergy?";
513             if (s == c.str()) {
514                 cout << "System wide energy consumed by CPU packages \t0\t\tJoule\n";
515             }
516         }
517         {
518             stringstream c;
519             c << "CoreC0StateResidency?";
520             if (s == c.str()) {
521                 cout << "System wide core C0-state residency \t0\t\t%\n";
522             }
523         }
524         {
525             stringstream c;
526             c << "CoreC3StateResidency?";
527             if (s == c.str()) {
528                 cout << "System wide core C3-state residency \t0\t\t%\n";
529             }
530         }
531         {
532             stringstream c;
533             c << "CoreC6StateResidency?";
534             if (s == c.str()) {
535                 cout << "System wide core C6-state residency \t0\t\t%\n";
536             }
537         }
538         {
539             stringstream c;
540             c << "CoreC7StateResidency?";
541             if (s == c.str()) {
542                 cout << "System wide core C7-state residency \t0\t\t%\n";
543             }
544         }
545         {
546             stringstream c;
547             c << "PackageC2StateResidency?";
548             if (s == c.str()) {
549                 cout << "System wide package C2-state residency \t0\t\t%\n";
550             }
551         }
552         {
553             stringstream c;
554             c << "PackageC3StateResidency?";
555             if (s == c.str()) {
556                 cout << "System wide package C3-state residency \t0\t\t%\n";
557             }
558         }
559         {
560             stringstream c;
561             c << "PackageC6StateResidency?";
562             if (s == c.str()) {
563                 cout << "System wide package C6-state residency \t0\t\t%\n";
564             }
565         }
566         {
567             stringstream c;
568             c << "PackageC7StateResidency?";
569             if (s == c.str()) {
570                 cout << "System wide package C7-state residency \t0\t\t%\n";
571             }
572         }
573 
574         // sensors
575 
576 #define OUTPUT_CORE_METRIC(name, function) \
577     for (uint32 i = 0; i<counters.getNumCores(); ++i) { \
578                              for (uint32 a = 0; a<counters.getNumSockets(); ++a) \
579                                                   if (a == counters.getSocketId(i)) { \
580                                                       stringstream c; \
581                                                       c << "Socket" << a << "/CPU" << i << name; \
582                                                       if (s == c.str()) { \
583                                                           cout << function << "\n"; \
584                                                       } \
585                                                   } \
586                                                   }
587 
588         OUTPUT_CORE_METRIC("/Frequency", (counters.get<double, ::getAverageFrequency>(i) / 1000000))
589         OUTPUT_CORE_METRIC("/IPC", (counters.get<double, ::getIPC>(i)))
590         OUTPUT_CORE_METRIC("/L2CacheHitRatio", (counters.get<double, ::getL2CacheHitRatio>(i)))
591         OUTPUT_CORE_METRIC("/L3CacheHitRatio", (counters.get<double, ::getL3CacheHitRatio>(i)))
592         OUTPUT_CORE_METRIC("/L2CacheMisses", (counters.get<uint64, ::getL2CacheMisses>(i)))
593         OUTPUT_CORE_METRIC("/L3CacheMisses", (counters.get<uint64, ::getL3CacheMisses>(i)))
594         OUTPUT_CORE_METRIC("/L3Occupancy", (counters.get<uint64, ::getL3CacheOccupancy>(i)))
595         OUTPUT_CORE_METRIC("/LocalMemoryBandwidth", (counters.get<uint64, ::getLocalMemoryBW>(i)))
596         OUTPUT_CORE_METRIC("/RemoteMemoryBandwidth", (counters.get<uint64, ::getRemoteMemoryBW>(i)))
597         OUTPUT_CORE_METRIC("/CoreC0StateResidency", (counters.get<double, ::getCoreCStateResidency>(0, i) * 100.))
598         OUTPUT_CORE_METRIC("/CoreC3StateResidency", (counters.get<double, ::getCoreCStateResidency>(3, i) * 100.))
599         OUTPUT_CORE_METRIC("/CoreC6StateResidency", (counters.get<double, ::getCoreCStateResidency>(6, i) * 100.))
600         OUTPUT_CORE_METRIC("/CoreC7StateResidency", (counters.get<double, ::getCoreCStateResidency>(7, i) * 100.))
601         OUTPUT_CORE_METRIC("/ThermalHeadroom", (counters.get<int32, ::getThermalHeadroom>(i)))
602 
603         #define OUTPUT_SOCKET_METRIC(name, function) \
604     for (uint32 i = 0; i<counters.getNumSockets(); ++i) { \
605                              stringstream c; \
606                              c << "Socket" << i << name; \
607                              if (s == c.str()) { \
608                                  cout << function << "\n"; \
609                              } \
610                          }
611 
612         OUTPUT_SOCKET_METRIC("/DRAMEnergy", (counters.getSocket<double, ::getDRAMConsumedJoules>(i)))
613         OUTPUT_SOCKET_METRIC("/CPUEnergy", (counters.getSocket<double, ::getConsumedJoules>(i)))
614         OUTPUT_SOCKET_METRIC("/CoreC0StateResidency", (counters.getSocket<double, ::getCoreCStateResidency>(0, i) * 100.))
615         OUTPUT_SOCKET_METRIC("/CoreC3StateResidency", (counters.getSocket<double, ::getCoreCStateResidency>(3, i) * 100.))
616         OUTPUT_SOCKET_METRIC("/CoreC6StateResidency", (counters.getSocket<double, ::getCoreCStateResidency>(6, i) * 100.))
617         OUTPUT_SOCKET_METRIC("/CoreC7StateResidency", (counters.getSocket<double, ::getCoreCStateResidency>(7, i) * 100.))
618         OUTPUT_SOCKET_METRIC("/PackageC2StateResidency", (counters.getSocket<double, ::getPackageCStateResidency>(2, i) * 100.))
619         OUTPUT_SOCKET_METRIC("/PackageC3StateResidency", (counters.getSocket<double, ::getPackageCStateResidency>(3, i) * 100.))
620         OUTPUT_SOCKET_METRIC("/PackageC6StateResidency", (counters.getSocket<double, ::getPackageCStateResidency>(6, i) * 100.))
621         OUTPUT_SOCKET_METRIC("/PackageC7StateResidency", (counters.getSocket<double, ::getPackageCStateResidency>(7, i) * 100.))
622         OUTPUT_SOCKET_METRIC("/ThermalHeadroom", (counters.getSocket<int32, ::getThermalHeadroom>(i)))
623         OUTPUT_SOCKET_METRIC("/BytesReadFromMC", (double(counters.getSocket<uint64, ::getBytesReadFromMC>(i)) / 1024 / 1024 / 1024))
624         OUTPUT_SOCKET_METRIC("/BytesWrittenToMC", (double(counters.getSocket<uint64, ::getBytesWrittenToMC>(i)) / 1024 / 1024 / 1024))
625         OUTPUT_SOCKET_METRIC("/BytesReadFromPMM", (double(counters.getSocket<uint64, ::getBytesReadFromPMM>(i)) / 1024 / 1024 / 1024))
626         OUTPUT_SOCKET_METRIC("/BytesWrittenToPMM", (double(counters.getSocket<uint64, ::getBytesWrittenToPMM>(i)) / 1024 / 1024 / 1024))
627         OUTPUT_SOCKET_METRIC("/Frequency", (counters.getSocket<double, ::getAverageFrequency>(i) / 1000000))
628         OUTPUT_SOCKET_METRIC("/IPC", (counters.getSocket<double, ::getIPC>(i)))
629         OUTPUT_SOCKET_METRIC("/L2CacheHitRatio", (counters.getSocket<double, ::getL2CacheHitRatio>(i)))
630         OUTPUT_SOCKET_METRIC("/L3CacheHitRatio", (counters.getSocket<double, ::getL3CacheHitRatio>(i)))
631         OUTPUT_SOCKET_METRIC("/L2CacheMisses", (counters.getSocket<uint64, ::getL2CacheMisses>(i)))
632         OUTPUT_SOCKET_METRIC("/L3CacheMisses", (counters.getSocket<uint64, ::getL3CacheMisses>(i)))
633         OUTPUT_SOCKET_METRIC("/L3Occupancy", (counters.getSocket<uint64, ::getL3CacheOccupancy>(i)))
634         OUTPUT_SOCKET_METRIC("/LocalMemoryBandwidth", (counters.getSocket<uint64, ::getLocalMemoryBW>(i)))
635         OUTPUT_SOCKET_METRIC("/RemoteMemoryBandwidth", (counters.getSocket<uint64, ::getRemoteMemoryBW>(i)))
636 
637         for (uint32 l = 0; l < counters.getQPILinksPerSocket(); ++l) {
638             for (uint32 i = 0; i < counters.getNumSockets(); ++i) {
639                 stringstream c;
640                 c << "Socket" << i << "/BytesIncomingToQPI" << l;
641                 if (s == c.str()) {
642                     cout << double(counters.getSocket<uint64, ::getIncomingQPILinkBytes>(i, l)) / 1024 / 1024 / 1024 << "\n";
643                 }
644             }
645         }
646 
647     #define OUTPUT_SYSTEM_METRIC(name, function) \
648     { \
649         stringstream c; \
650         c << name; \
651         if (s == c.str()) { \
652             cout << function << "\n"; \
653         } \
654     }
655 
656         OUTPUT_SYSTEM_METRIC("DRAMEnergy", (counters.getSystem<double, ::getDRAMConsumedJoules>()))
657         OUTPUT_SYSTEM_METRIC("CPUEnergy", (counters.getSystem<double, ::getConsumedJoules>()))
658         OUTPUT_SYSTEM_METRIC("CoreC0StateResidency", (counters.getSystem<double, ::getCoreCStateResidency>(0) * 100.))
659         OUTPUT_SYSTEM_METRIC("CoreC3StateResidency", (counters.getSystem<double, ::getCoreCStateResidency>(3) * 100.))
660         OUTPUT_SYSTEM_METRIC("CoreC6StateResidency", (counters.getSystem<double, ::getCoreCStateResidency>(6) * 100.))
661         OUTPUT_SYSTEM_METRIC("CoreC7StateResidency", (counters.getSystem<double, ::getCoreCStateResidency>(7) * 100.))
662         OUTPUT_SYSTEM_METRIC("PackageC2StateResidency", (counters.getSystem<double, ::getPackageCStateResidency>(2) * 100.))
663         OUTPUT_SYSTEM_METRIC("PackageC3StateResidency", (counters.getSystem<double, ::getPackageCStateResidency>(3) * 100.))
664         OUTPUT_SYSTEM_METRIC("PackageC6StateResidency", (counters.getSystem<double, ::getPackageCStateResidency>(6) * 100.))
665         OUTPUT_SYSTEM_METRIC("PackageC7StateResidency", (counters.getSystem<double, ::getPackageCStateResidency>(7) * 100.))
666         OUTPUT_SYSTEM_METRIC("Frequency", (double(counters.getSystem<double, ::getAverageFrequency>()) / 1000000))
667         OUTPUT_SYSTEM_METRIC("IPC", (double(counters.getSystem<double, ::getIPC>())))
668         OUTPUT_SYSTEM_METRIC("L2CacheHitRatio", (double(counters.getSystem<double, ::getL2CacheHitRatio>())))
669         OUTPUT_SYSTEM_METRIC("L3CacheHitRatio", (double(counters.getSystem<double, ::getL3CacheHitRatio>())))
670         OUTPUT_SYSTEM_METRIC("L2CacheMisses", (double(counters.getSystem<uint64, ::getL2CacheMisses>())))
671         OUTPUT_SYSTEM_METRIC("L3CacheMisses", (double(counters.getSystem<uint64, ::getL3CacheMisses>())))
672         OUTPUT_SYSTEM_METRIC("QPI_Traffic", (double(counters.getSystem<uint64, ::getAllIncomingQPILinkBytes>()) / 1024 / 1024 / 1024))
673 
674         // exit
675         if (s == "quit" || s == "exit") {
676             break;
677         }
678 
679 
680         cout << "ksysguardd> ";
681     }
682 
683     return 0;
684 }
685