forked from mhillenbrand/IntelPerformanceCounterMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcm-memory.cpp
733 lines (662 loc) · 30 KB
/
pcm-memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
Copyright (c) 2009-2012, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 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.
* 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.
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.
*/
// written by Patrick Lu
// increased max sockets to 256 - Thomas Willhalm
/*! \file pcm-memory.cpp
\brief Example of using CPU counters: implements a performance counter monitoring utility for memory controller channels and DIMMs (ranks)
*/
#define HACK_TO_REMOVE_DUPLICATE_ERROR
#include <iostream>
#ifdef _MSC_VER
#pragma warning(disable : 4996) // for sprintf
#include <windows.h>
#include "../PCM_Win/windriver.h"
#else
#include <unistd.h>
#include <signal.h>
#include <sys/time.h> // for gettimeofday()
#endif
#include <math.h>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <assert.h>
#include "cpucounters.h"
#include "utils.h"
//Programmable iMC counter
#define READ 0
#define WRITE 1
#define READ_RANK_A 0
#define WRITE_RANK_A 1
#define READ_RANK_B 2
#define WRITE_RANK_B 3
#define PARTIAL 2
#define PCM_DELAY_DEFAULT 1.0 // in seconds
#define PCM_DELAY_MIN 0.015 // 15 milliseconds is practical on most modern CPUs
#define PCM_CALIBRATION_INTERVAL 50 // calibrate clock only every 50th iteration
#define DEFAULT_DISPLAY_COLUMNS 2
using namespace std;
void print_help(const string prog_name)
{
cerr << endl << " Usage: " << endl << " " << prog_name
<< " --help | [delay] [options] [-- external_program [external_program_options]]" << endl;
cerr << " <delay> => time interval to sample performance counters." << endl;
cerr << " If not specified, or 0, with external program given" << endl;
cerr << " will read counters only after external program finishes" << endl;
cerr << " Supported <options> are: " << endl;
cerr << " -h | --help | /h => print this help and exit" << endl;
cerr << " -rank=X | /rank=X => monitor DIMM rank X. At most 2 out of 8 total ranks can be monitored simultaneously." << endl;
cerr << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or" << endl
<< " to a file, in case filename is provided" << endl;
cerr << " -columns=X | /columns=X => Number of columns to display the NUMA Nodes, defaults to 2." << endl;
#ifdef _MSC_VER
cerr << " --uninstallDriver | --installDriver=> (un)install driver" << endl;
#endif
cerr << " Examples:" << endl;
cerr << " " << prog_name << " 1 => print counters every second without core and socket output" << endl;
cerr << " " << prog_name << " 0.5 -csv=test.log => twice a second save counter values to test.log in CSV format" << endl;
cerr << " " << prog_name << " /csv 5 2>/dev/null => one sampe every 5 seconds, and discard all diagnostic output" << endl;
cerr << endl;
}
void printSocketBWHeader(uint32 no_columns, uint32 skt)
{
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|---------------------------------------|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-- Socket "<<setw(2)<<i<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|---------------------------------------|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-- Memory Channel Monitoring --|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|---------------------------------------|";
}
cout << endl;
}
void printSocketRankBWHeader(uint32 no_columns, uint32 skt)
{
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-------------------------------------------|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-- Socket "<<setw(2)<<i<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-------------------------------------------|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-- DIMM Rank Monitoring --|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-------------------------------------------|";
}
cout << endl;
}
void printSocketChannelBW(uint32 no_columns, uint32 skt, uint32 num_imc_channels, float* iMC_Rd_socket_chan, float* iMC_Wr_socket_chan)
{
for (uint32 channel = 0; channel < num_imc_channels; ++channel) {
// check all the sockets for bad channel "channel"
unsigned bad_channels = 0;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
if (iMC_Rd_socket_chan[i*num_imc_channels + channel] < 0.0 || iMC_Wr_socket_chan[i*num_imc_channels + channel] < 0.0) //If the channel read neg. value, the channel is not working; skip it.
++bad_channels;
}
if (bad_channels == no_columns) { // the channel is missing on all sockets in the row
continue;
}
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- Mem Ch "<<setw(2)<<channel<<": Reads (MB/s): "<<setw(8)<<iMC_Rd_socket_chan[i*num_imc_channels+channel]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- Writes(MB/s): "<<setw(8)<<iMC_Wr_socket_chan[i*num_imc_channels+channel]<<" --|";
}
cout << endl;
}
}
void printSocketChannelBW(uint32 no_columns, uint32 skt, uint32 num_imc_channels, const ServerUncorePowerState * uncState1, const ServerUncorePowerState * uncState2, uint64 elapsedTime, int rankA, int rankB)
{
for (uint32 channel = 0; channel < num_imc_channels; ++channel) {
if(rankA >= 0) {
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- Mem Ch "<<setw(2)<<channel<<" R " << setw(1) << rankA <<": Reads (MB/s): "<<setw(8)<<(float) (getMCCounter(channel,READ_RANK_A,uncState1[i],uncState2[i]) * 64 / 1000000.0 / (elapsedTime/1000.0))<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- Writes(MB/s): "<<setw(8)<<(float) (getMCCounter(channel,WRITE_RANK_A,uncState1[i],uncState2[i]) * 64 / 1000000.0 / (elapsedTime/1000.0))<<" --|";
}
cout << endl;
}
if(rankB >= 0) {
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- Mem Ch "<<setw(2) << channel<<" R " << setw(1) << rankB <<": Reads (MB/s): "<<setw(8)<<(float) (getMCCounter(channel,READ_RANK_B,uncState1[i],uncState2[i]) * 64 / 1000000.0 / (elapsedTime/1000.0))<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- Writes(MB/s): "<<setw(8)<<(float) (getMCCounter(channel,WRITE_RANK_B,uncState1[i],uncState2[i]) * 64 / 1000000.0 / (elapsedTime/1000.0))<<" --|";
}
cout << endl;
}
}
}
void printSocketBWFooter(uint32 no_columns, uint32 skt, float* iMC_Rd_socket, float* iMC_Wr_socket, uint64* partial_write)
{
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" Mem Read (MB/s) : "<<setw(8)<<iMC_Rd_socket[i]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" Mem Write(MB/s) : "<<setw(8)<<iMC_Wr_socket[i]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" P. Write (T/s): "<<dec<<setw(10)<<partial_write[i]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" Memory (MB/s): "<<setw(11)<<std::right<<iMC_Rd_socket[i]+iMC_Wr_socket[i]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|---------------------------------------|";
}
cout << endl;
}
void display_bandwidth(float *iMC_Rd_socket_chan, float *iMC_Wr_socket_chan, float *iMC_Rd_socket, float *iMC_Wr_socket, uint32 numSockets, uint32 num_imc_channels, uint64 *partial_write, uint32 no_columns )
{
float sysRead = 0.0, sysWrite = 0.0;
uint32 skt = 0;
cout.setf(ios::fixed);
cout.precision(2);
while(skt < numSockets)
{
// Full row
if ( (skt+no_columns) <= numSockets )
{
printSocketBWHeader (no_columns, skt);
printSocketChannelBW(no_columns, skt, num_imc_channels, iMC_Rd_socket_chan, iMC_Wr_socket_chan);
printSocketBWFooter (no_columns, skt, iMC_Rd_socket, iMC_Wr_socket, partial_write);
for (uint32 i=skt; i<(skt+no_columns); i++) {
sysRead += iMC_Rd_socket[i];
sysWrite += iMC_Wr_socket[i];
}
skt += no_columns;
}
else //Display one socket in this row
{
cout << "\
\r|---------------------------------------|\n\
\r|-- Socket "<<skt<<" --|\n\
\r|---------------------------------------|\n\
\r|-- Memory Channel Monitoring --|\n\
\r|---------------------------------------|\n\
\r";
for(uint64 channel = 0; channel < num_imc_channels; ++channel)
{
if(iMC_Rd_socket_chan[skt*num_imc_channels+channel] < 0.0 && iMC_Wr_socket_chan[skt*num_imc_channels+channel] < 0.0) //If the channel read neg. value, the channel is not working; skip it.
continue;
cout << "|-- Mem Ch "
<<channel
<<": Reads (MB/s):"
<<setw(8)
<<iMC_Rd_socket_chan[skt*num_imc_channels+channel]
<<" --|\n|-- Writes(MB/s):"
<<setw(8)
<<iMC_Wr_socket_chan[skt*num_imc_channels+channel]
<<" --|\n";
}
cout << "\
\r|-- NODE"<<skt<<" Mem Read (MB/s): "<<setw(8)<<iMC_Rd_socket[skt]<<" --|\n\
\r|-- NODE"<<skt<<" Mem Write (MB/s) :"<<setw(8)<<iMC_Wr_socket[skt]<<" --|\n\
\r|-- NODE"<<skt<<" P. Write (T/s) :"<<setw(10)<<dec<<partial_write[skt]<<" --|\n\
\r|-- NODE"<<skt<<" Memory (MB/s): "<<setw(8)<<iMC_Rd_socket[skt]+iMC_Wr_socket[skt]<<" --|\n\
\r|---------------------------------------|\n\
\r";
sysRead += iMC_Rd_socket[skt];
sysWrite += iMC_Wr_socket[skt];
skt += 1;
}
}
cout << "\
\r|---------------------------------------||---------------------------------------|\n\
\r|-- System Read Throughput(MB/s):"<<setw(10)<<sysRead<<" --|\n\
\r|-- System Write Throughput(MB/s):"<<setw(10)<<sysWrite<<" --|\n\
\r|-- System Memory Throughput(MB/s):"<<setw(10)<<sysRead+sysWrite<<" --|\n\
\r|---------------------------------------||---------------------------------------|" << endl;
}
void display_bandwidth_csv_header(float *iMC_Rd_socket_chan, float *iMC_Wr_socket_chan, float *iMC_Rd_socket, float *iMC_Wr_socket, uint32 numSockets, uint32 num_imc_channels, uint64 *partial_write)
{
cout << ";;" ; // Time
for (uint32 skt=0; skt < numSockets; ++skt)
{
for(uint64 channel = 0; channel < num_imc_channels; ++channel)
{
if(iMC_Rd_socket_chan[skt*num_imc_channels+channel] < 0.0 && iMC_Wr_socket_chan[skt*num_imc_channels+channel] < 0.0) //If the channel read neg. value, the channel is not working; skip it.
continue;
cout << "SKT" << skt << ";SKT" << skt << ';';
}
cout << "SKT"<<skt<<";"
<< "SKT"<<skt<<";"
<< "SKT"<<skt<<";"
<< "SKT"<<skt<<";";
}
cout << "System;System;System\n";
cout << "Date;Time;" ;
for (uint32 skt=0; skt < numSockets; ++skt)
{
for(uint64 channel = 0; channel < num_imc_channels; ++channel)
{
if(iMC_Rd_socket_chan[skt*num_imc_channels+channel] < 0.0 && iMC_Wr_socket_chan[skt*num_imc_channels+channel] < 0.0) //If the channel read neg. value, the channel is not working; skip it.
continue;
cout << "Ch" <<channel <<"Read;"
<< "Ch" <<channel <<"Write;";
}
cout << "Mem Read (MB/s);Mem Write (MB/s); P. Write (T/s); Memory (MB/s);";
}
cout << "Read;Write;Memory" << endl;
}
void display_bandwidth_csv(float *iMC_Rd_socket_chan, float *iMC_Wr_socket_chan, float *iMC_Rd_socket, float *iMC_Wr_socket, uint32 numSockets, uint32 num_imc_channels, uint64 *partial_write, uint64 elapsedTime)
{
time_t t = time(NULL);
tm *tt = localtime(&t);
cout.precision(3);
cout << 1900+tt->tm_year << '-' << 1+tt->tm_mon << '-' << tt->tm_mday << ';'
<< tt->tm_hour << ':' << tt->tm_min << ':' << tt->tm_sec << ';';
float sysRead = 0.0, sysWrite = 0.0;
cout.setf(ios::fixed);
cout.precision(2);
for (uint32 skt=0; skt < numSockets; ++skt)
{
for(uint64 channel = 0; channel < num_imc_channels; ++channel)
{
if(iMC_Rd_socket_chan[skt*num_imc_channels+channel] < 0.0 && iMC_Wr_socket_chan[skt*num_imc_channels+channel] < 0.0) //If the channel read neg. value, the channel is not working; skip it.
continue;
cout <<setw(8) <<iMC_Rd_socket_chan[skt*num_imc_channels+channel] << ';'
<<setw(8) <<iMC_Wr_socket_chan[skt*num_imc_channels+channel] << ';';
}
cout <<setw(8) <<iMC_Rd_socket[skt] <<';'
<<setw(8) <<iMC_Wr_socket[skt] <<';'
<<setw(10) <<dec<<partial_write[skt] <<';'
<<setw(8) <<iMC_Rd_socket[skt]+iMC_Wr_socket[skt] <<';';
sysRead += iMC_Rd_socket[skt];
sysWrite += iMC_Wr_socket[skt];
}
cout <<setw(10) <<sysRead <<';'
<<setw(10) <<sysWrite <<';'
<<setw(10) <<sysRead+sysWrite << endl;
}
const uint32 max_sockets = 256;
const uint32 max_imc_channels = 8;
void calculate_bandwidth(PCM *m, const ServerUncorePowerState uncState1[], const ServerUncorePowerState uncState2[], uint64 elapsedTime, bool csv, bool & csvheader, uint32 no_columns)
{
//const uint32 num_imc_channels = m->getMCChannelsPerSocket();
float iMC_Rd_socket_chan[max_sockets][max_imc_channels];
float iMC_Wr_socket_chan[max_sockets][max_imc_channels];
float iMC_Rd_socket[max_sockets];
float iMC_Wr_socket[max_sockets];
uint64 partial_write[max_sockets];
for(uint32 skt = 0; skt < m->getNumSockets(); ++skt)
{
iMC_Rd_socket[skt] = 0.0;
iMC_Wr_socket[skt] = 0.0;
partial_write[skt] = 0;
for(uint32 channel = 0; channel < max_imc_channels; ++channel)
{
if(getMCCounter(channel,READ,uncState1[skt],uncState2[skt]) == 0.0 && getMCCounter(channel,WRITE,uncState1[skt],uncState2[skt]) == 0.0) //In case of JKT-EN, there are only three channels. Skip one and continue.
{
iMC_Rd_socket_chan[skt][channel] = -1.0;
iMC_Wr_socket_chan[skt][channel] = -1.0;
continue;
}
iMC_Rd_socket_chan[skt][channel] = (float) (getMCCounter(channel,READ,uncState1[skt],uncState2[skt]) * 64 / 1000000.0 / (elapsedTime/1000.0));
iMC_Wr_socket_chan[skt][channel] = (float) (getMCCounter(channel,WRITE,uncState1[skt],uncState2[skt]) * 64 / 1000000.0 / (elapsedTime/1000.0));
iMC_Rd_socket[skt] += iMC_Rd_socket_chan[skt][channel];
iMC_Wr_socket[skt] += iMC_Wr_socket_chan[skt][channel];
partial_write[skt] += (uint64) (getMCCounter(channel,PARTIAL,uncState1[skt],uncState2[skt]) / (elapsedTime/1000.0));
}
}
if (csv) {
if (csvheader) {
display_bandwidth_csv_header(iMC_Rd_socket_chan[0], iMC_Wr_socket_chan[0], iMC_Rd_socket, iMC_Wr_socket, m->getNumSockets(), max_imc_channels, partial_write);
csvheader = false;
}
display_bandwidth_csv(iMC_Rd_socket_chan[0], iMC_Wr_socket_chan[0], iMC_Rd_socket, iMC_Wr_socket, m->getNumSockets(), max_imc_channels, partial_write, elapsedTime);
} else {
display_bandwidth(iMC_Rd_socket_chan[0], iMC_Wr_socket_chan[0], iMC_Rd_socket, iMC_Wr_socket, m->getNumSockets(), max_imc_channels, partial_write, no_columns);
}
}
void calculate_bandwidth(PCM *m, const ServerUncorePowerState uncState1[], const ServerUncorePowerState uncState2[], uint64 elapsedTime, bool csv, bool & csvheader, uint32 no_columns, int rankA, int rankB)
{
uint32 skt = 0;
cout.setf(ios::fixed);
cout.precision(2);
uint32 numSockets = m->getNumSockets();
while(skt < numSockets)
{
// Full row
if ( (skt+no_columns) <= numSockets )
{
printSocketRankBWHeader(no_columns, skt);
printSocketChannelBW(no_columns, skt, max_imc_channels, uncState1, uncState2, elapsedTime, rankA, rankB);
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|-------------------------------------------|";
}
cout << endl;
skt += no_columns;
}
else //Display one socket in this row
{
cout << "\
\r|-------------------------------------------|\n\
\r|-- Socket "<<skt<<" --|\n\
\r|-------------------------------------------|\n\
\r|-- DIMM Rank Monitoring --|\n\
\r|-------------------------------------------|\n\
\r";
for(uint64 channel = 0; channel < max_imc_channels; ++channel)
{
if(rankA >=0)
cout << "|-- Mem Ch "
<< setw(2) << channel
<< " R " << setw(1) << rankA
<<": Reads (MB/s):"
<<setw(8)
<<(float) (getMCCounter(channel,READ_RANK_A,uncState1[skt],uncState2[skt]) * 64 / 1000000.0 / (elapsedTime/1000.0))
<<" --|\n|-- Writes(MB/s):"
<<setw(8)
<<(float) (getMCCounter(channel,WRITE_RANK_A,uncState1[skt],uncState2[skt]) * 64 / 1000000.0 / (elapsedTime/1000.0))
<<" --|\n";
if(rankB >=0)
cout << "|-- Mem Ch "
<< setw(2) << channel
<< " R " << setw(1) << rankB
<<": Reads (MB/s):"
<<setw(8)
<<(float) (getMCCounter(channel,READ_RANK_B,uncState1[skt],uncState2[skt]) * 64 / 1000000.0 / (elapsedTime/1000.0))
<<" --|\n|-- Writes(MB/s):"
<<setw(8)
<<(float) (getMCCounter(channel,WRITE_RANK_B,uncState1[skt],uncState2[skt]) * 64 / 1000000.0 / (elapsedTime/1000.0))
<<" --|\n";
}
cout << "\
\r|-------------------------------------------|\n\
\r";
skt += 1;
}
}
}
int main(int argc, char * argv[])
{
set_signal_handlers();
#ifdef PCM_FORCE_SILENT
null_stream nullStream1, nullStream2;
std::cout.rdbuf(&nullStream1);
std::cerr.rdbuf(&nullStream2);
#endif
#ifdef _MSC_VER
TCHAR driverPath[1040]; // length for current directory + "\\msr.sys"
GetCurrentDirectory(1024, driverPath);
wcscat_s(driverPath, 1040, L"\\msr.sys");
#endif
cerr << endl;
cerr << " Intel(r) Performance Counter Monitor: Memory Bandwidth Monitoring Utility " << INTEL_PCM_VERSION << endl;
cerr << endl;
cerr << INTEL_PCM_COPYRIGHT << endl;
cerr << " This utility measures memory bandwidth per channel or per DIMM rank in real-time" << endl;
cerr << endl;
double delay = -1.0;
bool csv = false, csvheader=false;
uint32 no_columns = DEFAULT_DISPLAY_COLUMNS; // Default number of columns is 2
char * sysCmd = NULL;
char ** sysArgv = NULL;
#ifndef _MSC_VER
long diff_usec = 0; // deviation of clock is useconds between measurements
int calibrated = PCM_CALIBRATION_INTERVAL - 2; // keeps track is the clock calibration needed
#endif
int rankA = -1, rankB = -1;
string program = string(argv[0]);
PCM * m = PCM::getInstance();
if(argc > 1) do
{
argv++;
argc--;
if (strncmp(*argv, "--help", 6) == 0 ||
strncmp(*argv, "-h", 2) == 0 ||
strncmp(*argv, "/h", 2) == 0)
{
print_help(program);
exit(EXIT_FAILURE);
}
else
if (strncmp(*argv, "-csv",4) == 0 ||
strncmp(*argv, "/csv",4) == 0)
{
csv = true;
csvheader = true;
string cmd = string(*argv);
size_t found = cmd.find('=',4);
if (found != string::npos) {
string filename = cmd.substr(found+1);
if (!filename.empty()) {
m->setOutput(filename);
}
}
continue;
}
else
if (strncmp(*argv, "-columns", 8) == 0 ||
strncmp(*argv, "/columns", 8) == 0)
{
string cmd = string(*argv);
size_t found = cmd.find('=',2);
if (found != string::npos) {
no_columns = atoi(cmd.substr(found+1).c_str());
if (no_columns == 0)
no_columns = DEFAULT_DISPLAY_COLUMNS;
if (no_columns > m->getNumSockets())
no_columns = m->getNumSockets();
}
continue;
}
if (strncmp(*argv, "-rank", 5) == 0 ||
strncmp(*argv, "/rank", 5) == 0)
{
string cmd = string(*argv);
size_t found = cmd.find('=',2);
if (found != string::npos) {
int rank = atoi(cmd.substr(found+1).c_str());
if (rankA >= 0 && rankB >= 0)
{
std::cerr << "At most two DIMM ranks can be monitored "<< std::endl;
exit(EXIT_FAILURE);
}
else
{
if(rank > 7) {
std::cerr << "Invalid rank number "<<rank << std::endl;
exit(EXIT_FAILURE);
}
if(rankA < 0) rankA = rank;
else if(rankB < 0) rankB = rank;
}
}
continue;
}
#ifdef _MSC_VER
else
if (strncmp(*argv, "--uninstallDriver", 17) == 0)
{
Driver tmpDrvObject;
tmpDrvObject.uninstall();
cerr << "msr.sys driver has been uninstalled. You might need to reboot the system to make this effective." << endl;
exit(EXIT_SUCCESS);
}
else
if (strncmp(*argv, "--installDriver", 15) == 0)
{
Driver tmpDrvObject;
if (!tmpDrvObject.start(driverPath))
{
cerr << "Can not access CPU counters" << endl;
cerr << "You must have signed msr.sys driver in your current directory and have administrator rights to run this program" << endl;
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
#endif
else
if (strncmp(*argv, "--", 2) == 0)
{
argv++;
sysCmd = *argv;
sysArgv = argv;
break;
}
else
{
// any other options positional that is a floating point number is treated as <delay>,
// while the other options are ignored with a warning issues to stderr
double delay_input;
std::istringstream is_str_stream(*argv);
is_str_stream >> noskipws >> delay_input;
if(is_str_stream.eof() && !is_str_stream.fail()) {
delay = delay_input;
} else {
cerr << "WARNING: unknown command-line option: \"" << *argv << "\". Ignoring it." << endl;
print_help(program);
exit(EXIT_FAILURE);
}
continue;
}
} while(argc > 1); // end of command line partsing loop
m->disableJKTWorkaround();
PCM::ErrorCode status = m->programServerUncoreMemoryMetrics(rankA, rankB);
switch (status)
{
case PCM::Success:
break;
case PCM::MSRAccessDenied:
cerr << "Access to Intel(r) Performance Counter Monitor has denied (no MSR or PCI CFG space access)." << endl;
exit(EXIT_FAILURE);
case PCM::PMUBusy:
cerr << "Access to Intel(r) Performance Counter Monitor has denied (Performance Monitoring Unit is occupied by other application). Try to stop the application that uses PMU." << endl;
cerr << "Alternatively you can try to reset PMU configuration at your own risk. Try to reset? (y/n)" << endl;
char yn;
std::cin >> yn;
if ('y' == yn)
{
m->resetPMU();
cerr << "PMU configuration has been reset. Try to rerun the program again." << endl;
}
exit(EXIT_FAILURE);
default:
cerr << "Access to Intel(r) Performance Counter Monitor has denied (Unknown error)." << endl;
exit(EXIT_FAILURE);
}
cerr << "\nDetected "<< m->getCPUBrandString() << " \"Intel(r) microarchitecture codename "<<m->getUArchCodename()<<"\""<<endl;
if(!m->hasPCICFGUncore())
{
cerr << "Jaketown, Ivytown or Haswell Server CPU is required for this tool!" << endl;
if(m->memoryTrafficMetricsAvailable())
cerr << "For processor-level memory bandwidth statistics please use pcm.x" << endl;
exit(EXIT_FAILURE);
}
if(m->getNumSockets() > max_sockets)
{
cerr << "Only systems with up to "<<max_sockets<<" sockets are supported! Program aborted" << endl;
exit(EXIT_FAILURE);
}
ServerUncorePowerState * BeforeState = new ServerUncorePowerState[m->getNumSockets()];
ServerUncorePowerState * AfterState = new ServerUncorePowerState[m->getNumSockets()];
uint64 BeforeTime = 0, AfterTime = 0;
if ( (sysCmd != NULL) && (delay<=0.0) ) {
// in case external command is provided in command line, and
// delay either not provided (-1) or is zero
m->setBlocked(true);
} else {
m->setBlocked(false);
}
if (csv) {
if( delay<=0.0 ) delay = PCM_DELAY_DEFAULT;
} else {
// for non-CSV mode delay < 1.0 does not make a lot of practical sense:
// hard to read from the screen, or
// in case delay is not provided in command line => set default
if( ((delay<1.0) && (delay>0.0)) || (delay<=0.0) ) delay = PCM_DELAY_DEFAULT;
}
cerr << "Update every "<<delay<<" seconds"<< endl;
for(uint32 i=0; i<m->getNumSockets(); ++i)
BeforeState[i] = m->getServerUncorePowerState(i);
BeforeTime = m->getTickCount();
if( sysCmd != NULL ) {
MySystem(sysCmd, sysArgv);
}
while(1)
{
if(!csv) cout << std::flush;
int delay_ms = int(delay * 1000);
int calibrated_delay_ms = delay_ms;
#ifdef _MSC_VER
// compensate slow Windows console output
if(AfterTime) delay_ms -= (int)(m->getTickCount() - BeforeTime);
if(delay_ms < 0) delay_ms = 0;
#else
// compensation of delay on Linux/UNIX
// to make the samling interval as monotone as possible
struct timeval start_ts, end_ts;
if(calibrated == 0) {
gettimeofday(&end_ts, NULL);
diff_usec = (end_ts.tv_sec-start_ts.tv_sec)*1000000.0+(end_ts.tv_usec-start_ts.tv_usec);
calibrated_delay_ms = delay_ms - diff_usec/1000.0;
}
#endif
MySleepMs(calibrated_delay_ms);
#ifndef _MSC_VER
calibrated = (calibrated + 1) % PCM_CALIBRATION_INTERVAL;
if(calibrated == 0) {
gettimeofday(&start_ts, NULL);
}
#endif
AfterTime = m->getTickCount();
for(uint32 i=0; i<m->getNumSockets(); ++i)
AfterState[i] = m->getServerUncorePowerState(i);
if (!csv) {
cout << "Time elapsed: "<<dec<<fixed<<AfterTime-BeforeTime<<" ms\n";
cout << "Called sleep function for "<<dec<<fixed<<delay_ms<<" ms\n";
}
if(rankA >= 0 || rankB >= 0)
calculate_bandwidth(m,BeforeState,AfterState,AfterTime-BeforeTime,csv,csvheader, no_columns, rankA, rankB);
else
calculate_bandwidth(m,BeforeState,AfterState,AfterTime-BeforeTime,csv,csvheader, no_columns);
swap(BeforeTime, AfterTime);
swap(BeforeState, AfterState);
if ( m->isBlocked() ) {
// in case PCM was blocked after spawning child application: break monitoring loop here
break;
}
}
delete[] BeforeState;
delete[] AfterState;
exit(EXIT_SUCCESS);
}