forked from intel/pcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcm-memory.cpp
1153 lines (1069 loc) · 47.2 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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2009-2020, 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) + PMM memory traffic
*/
#define HACK_TO_REMOVE_DUPLICATE_ERROR
#include <iostream>
#ifdef _MSC_VER
#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"
#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;
const uint32 max_sockets = 256;
const uint32 max_imc_channels = ServerUncorePowerState::maxChannels;
const uint32 max_edc_channels = ServerUncorePowerState::maxChannels;
const uint32 max_imc_controllers = ServerUncorePowerState::maxControllers;
typedef struct memdata {
float iMC_Rd_socket_chan[max_sockets][max_imc_channels];
float iMC_Wr_socket_chan[max_sockets][max_imc_channels];
float iMC_PMM_Rd_socket_chan[max_sockets][max_imc_channels];
float iMC_PMM_Wr_socket_chan[max_sockets][max_imc_channels];
float iMC_PMM_MemoryMode_Miss_socket_chan[max_sockets][max_imc_channels];
float iMC_Rd_socket[max_sockets];
float iMC_Wr_socket[max_sockets];
float iMC_PMM_Rd_socket[max_sockets];
float iMC_PMM_Wr_socket[max_sockets];
float iMC_PMM_MemoryMode_Miss_socket[max_sockets];
float M2M_NM_read_hit_rate[max_sockets][max_imc_controllers];
float EDC_Rd_socket_chan[max_sockets][max_edc_channels];
float EDC_Wr_socket_chan[max_sockets][max_edc_channels];
float EDC_Rd_socket[max_sockets];
float EDC_Wr_socket[max_sockets];
uint64 partial_write[max_sockets];
bool PMM, PMMMixedMode;
} memdata_t;
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 << " -pmm => monitor PMM memory bandwidth (instead of partial writes)." << endl;
cerr << " -mixed => monitor PMM mixed mode (AppDirect + Memory Mode)." << endl;
cerr << " -nc | --nochannel | /nc => suppress output for individual channels." << 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, const bool show_channel_output)
{
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;
if (show_channel_output) {
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(PCM *m, memdata_t *md, uint32 no_columns, uint32 skt)
{
for (uint32 channel = 0; channel < max_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 (md->iMC_Rd_socket_chan[i][channel] < 0.0 || md->iMC_Wr_socket_chan[i][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)<<md->iMC_Rd_socket_chan[i][channel]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- Writes(MB/s): "<<setw(8)<<md->iMC_Wr_socket_chan[i][channel]<<" --|";
}
cout << endl;
if(md->PMM)
{
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- PMM Reads(MB/s) : "<<setw(8)<<md->iMC_PMM_Rd_socket_chan[i][channel]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- PMM Writes(MB/s) : "<<setw(8)<<md->iMC_PMM_Wr_socket_chan[i][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,ServerPCICFGUncore::EventPosition::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,ServerPCICFGUncore::EventPosition::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,ServerPCICFGUncore::EventPosition::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,ServerPCICFGUncore::EventPosition::WRITE_RANK_B,uncState1[i],uncState2[i]) * 64 / 1000000.0 / (elapsedTime/1000.0))<<" --|";
}
cout << endl;
}
}
}
float AD_BW(const memdata_t *md, const uint32 skt)
{
const auto totalPMM = md->iMC_PMM_Rd_socket[skt] + md->iMC_PMM_Wr_socket[skt];
return (std::max)(totalPMM - md->iMC_PMM_MemoryMode_Miss_socket[skt], float(0.0));
}
float PMM_MM_Ratio(const memdata_t *md, const uint32 skt)
{
const auto dram = md->iMC_Rd_socket[skt] + md->iMC_Wr_socket[skt];
return md->iMC_PMM_MemoryMode_Miss_socket[skt] / dram;
}
void printSocketBWFooter(uint32 no_columns, uint32 skt, const memdata_t *md)
{
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" Mem Read (MB/s) : "<<setw(8)<<md->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)<<md->iMC_Wr_socket[i]<<" --|";
}
cout << endl;
if (md->PMM || md->PMMMixedMode)
{
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" PMM Read (MB/s): "<<setw(8)<<md->iMC_PMM_Rd_socket[i]<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" PMM Write(MB/s): "<<setw(8)<<md->iMC_PMM_Wr_socket[i]<<" --|";
}
cout << endl;
}
if (md->PMMMixedMode)
{
for (uint32 i = skt; i < (skt + no_columns); ++i)
{
cout << "|-- NODE" << setw(2) << i << " PMM AD Bw(MB/s): " << setw(8) << AD_BW(md, i) << " --|";
}
cout << endl;
for (uint32 i = skt; i < (skt + no_columns); ++i)
{
cout << "|-- NODE" << setw(2) << i << " PMM MM Bw(MB/s): " << setw(8) << md->iMC_PMM_MemoryMode_Miss_socket[i] << " --|";
}
cout << endl;
for (uint32 i = skt; i < (skt + no_columns); ++i)
{
cout << "|-- NODE" << setw(2) << i << " PMM MM Bw/DRAM Bw:" << setw(8) << PMM_MM_Ratio(md, i) << " --|";
}
cout << endl;
}
if (md->PMM)
{
for (uint32 ctrl = 0; ctrl < max_imc_controllers; ++ctrl)
{
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<"."<<ctrl<<" NM read hit rate :"<<setw(6)<<md->M2M_NM_read_hit_rate[i][ctrl]<<" --|";
}
cout << endl;
}
}
if (md->PMM == false && md->PMMMixedMode == false)
{
for (uint32 i=skt; i<(skt+no_columns); ++i) {
cout << "|-- NODE"<<setw(2)<<i<<" P. Write (T/s): "<<dec<<setw(10)<<md->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<<(md->iMC_Rd_socket[i]+md->iMC_Wr_socket[i]+
md->iMC_PMM_Rd_socket[i]+md->iMC_PMM_Wr_socket[i])<<" --|";
}
cout << endl;
for (uint32 i=skt; i<(no_columns+skt); ++i) {
cout << "|---------------------------------------|";
}
cout << endl;
}
void display_bandwidth(PCM *m, memdata_t *md, const uint32 no_columns, const bool show_channel_output)
{
float sysReadDRAM = 0.0, sysWriteDRAM = 0.0, sysReadPMM = 0.0, sysWritePMM = 0.0;
uint32 numSockets = m->getNumSockets();
uint32 skt = 0;
cout.setf(ios::fixed);
cout.precision(2);
while (skt < numSockets)
{
auto printRow = [&skt,&show_channel_output,&m,&md,&sysReadDRAM,&sysWriteDRAM, &sysReadPMM, &sysWritePMM](const uint32 no_columns)
{
printSocketBWHeader(no_columns, skt, show_channel_output);
if (show_channel_output)
printSocketChannelBW(m, md, no_columns, skt);
printSocketBWFooter(no_columns, skt, md);
for (uint32 i = skt; i < (skt + no_columns); i++)
{
sysReadDRAM += md->iMC_Rd_socket[i];
sysWriteDRAM += md->iMC_Wr_socket[i];
sysReadPMM += md->iMC_PMM_Rd_socket[i];
sysWritePMM += md->iMC_PMM_Wr_socket[i];
}
skt += no_columns;
};
// Full row
if ((skt + no_columns) <= numSockets)
{
printRow(no_columns);
}
else //Display the remaining sockets in this row
{
if (m->MCDRAMmemoryTrafficMetricsAvailable() == false)
{
printRow(numSockets - skt);
}
else
{
cout << "\
\r|---------------------------------------||---------------------------------------|\n\
\r|-- Processor socket "
<< skt << " --|\n\
\r|---------------------------------------||---------------------------------------|\n\
\r|-- DDR4 Channel Monitoring --||-- MCDRAM Channel Monitoring --|\n\
\r|---------------------------------------||---------------------------------------|\n\
\r";
uint32 max_channels = max_imc_channels <= max_edc_channels ? max_edc_channels : max_imc_channels;
if (show_channel_output)
{
float iMC_Rd, iMC_Wr, EDC_Rd, EDC_Wr;
for (uint64 channel = 0; channel < max_channels; ++channel)
{
if (channel <= max_imc_channels)
{
iMC_Rd = md->iMC_Rd_socket_chan[skt][channel];
iMC_Wr = md->iMC_Wr_socket_chan[skt][channel];
}
else
{
iMC_Rd = -1.0;
iMC_Wr = -1.0;
}
if (channel <= max_edc_channels)
{
EDC_Rd = md->EDC_Rd_socket_chan[skt][channel];
EDC_Wr = md->EDC_Wr_socket_chan[skt][channel];
}
else
{
EDC_Rd = -1.0;
EDC_Rd = -1.0;
}
if (iMC_Rd >= 0.0 && iMC_Wr >= 0.0 && EDC_Rd >= 0.0 && EDC_Wr >= 0.0)
cout << "|-- DDR4 Ch " << channel << ": Reads (MB/s):" << setw(9) << iMC_Rd
<< " --||-- EDC Ch " << channel << ": Reads (MB/s):" << setw(10) << EDC_Rd
<< " --|\n|-- Writes(MB/s):" << setw(9) << iMC_Wr
<< " --||-- Writes(MB/s):" << setw(10) << EDC_Wr
<< " --|\n";
else if ((iMC_Rd < 0.0 || iMC_Wr < 0.0) && EDC_Rd >= 0.0 && EDC_Wr >= 0.0)
cout << "|-- "
<< " --||-- EDC Ch " << channel << ": Reads (MB/s):" << setw(10) << EDC_Rd
<< " --|\n|-- "
<< " --||-- Writes(MB/s):" << setw(10) << EDC_Wr
<< " --|\n";
else if (iMC_Rd >= 0.0 && iMC_Wr >= 0.0 && (EDC_Rd < 0.0 || EDC_Wr < 0.0))
cout << "|-- DDR4 Ch " << channel << ": Reads (MB/s):" << setw(9) << iMC_Rd
<< " --||-- "
<< " --|\n|-- Writes(MB/s):" << setw(9) << iMC_Wr
<< " --||-- "
<< " --|\n";
else
continue;
}
}
cout << "\
\r|-- DDR4 Mem Read (MB/s):"
<< setw(11) << md->iMC_Rd_socket[skt] << " --||-- MCDRAM Read (MB/s):" << setw(14) << md->EDC_Rd_socket[skt] << " --|\n\
\r|-- DDR4 Mem Write (MB/s):"
<< setw(11) << md->iMC_Wr_socket[skt] << " --||-- MCDRAM Write(MB/s):" << setw(14) << md->EDC_Wr_socket[skt] << " --|\n\
\r|-- DDR4 Memory (MB/s) :"
<< setw(11) << md->iMC_Rd_socket[skt] + md->iMC_Wr_socket[skt] << " --||-- MCDRAM (MB/s) :" << setw(14) << md->EDC_Rd_socket[skt] + md->EDC_Wr_socket[skt] << " --|\n\
\r|---------------------------------------||---------------------------------------|\n\
\r";
sysReadDRAM += (md->iMC_Rd_socket[skt] + md->EDC_Rd_socket[skt]);
sysWriteDRAM += (md->iMC_Wr_socket[skt] + md->EDC_Wr_socket[skt]);
skt += 1;
}
}
}
{
cout << "\
\r|---------------------------------------||---------------------------------------|\n";
if(md->PMM || md->PMMMixedMode)
cout << "\
\r|-- System DRAM Read Throughput(MB/s):"<<setw(14)<<sysReadDRAM<<" --|\n\
\r|-- System DRAM Write Throughput(MB/s):"<<setw(14)<<sysWriteDRAM<<" --|\n\
\r|-- System PMM Read Throughput(MB/s):"<<setw(14)<<sysReadPMM<<" --|\n\
\r|-- System PMM Write Throughput(MB/s):"<<setw(14)<<sysWritePMM<<" --|\n";
cout << "\
\r|-- System Read Throughput(MB/s):"<<setw(14)<<sysReadDRAM+sysReadPMM<<" --|\n\
\r|-- System Write Throughput(MB/s):"<<setw(14)<<sysWriteDRAM+sysWritePMM<<" --|\n\
\r|-- System Memory Throughput(MB/s):"<<setw(14)<<sysReadDRAM+sysReadPMM+sysWriteDRAM+sysWritePMM<<" --|\n\
\r|---------------------------------------||---------------------------------------|" << endl;
}
}
enum CsvOutputType
{
Header1,
Header2,
Data
};
template <class H1, class H2, class D>
void choose(const CsvOutputType outputType, H1 h1Func, H2 h2Func, D dataFunc)
{
switch (outputType)
{
case Header1:
h1Func();
break;
case Header2:
h2Func();
break;
case Data:
dataFunc();
break;
default:
std::cerr << "PCM internal error: wrong CSvOutputType" << std::endl;
}
}
void display_bandwidth_csv(PCM *m, memdata_t *md, uint64 elapsedTime, const bool show_channel_output, const CsvOutputType outputType)
{
const uint32 numSockets = m->getNumSockets();
choose(outputType,
[]() {
cout << ",,"; // Time
},
[]() { cout << "Date,Time,"; },
[]() {
tm tt = pcm_localtime();
cout.precision(3);
cout << 1900 + tt.tm_year << '-' << 1 + tt.tm_mon << '-' << tt.tm_mday << ','
<< tt.tm_hour << ':' << tt.tm_min << ':' << tt.tm_sec << ',';
cout.setf(ios::fixed);
cout.precision(2);
});
float sysReadDRAM = 0.0, sysWriteDRAM = 0.0, sysReadPMM = 0.0, sysWritePMM = 0.0;
for (uint32 skt = 0; skt < numSockets; ++skt)
{
auto printSKT = [skt](int c = 1) {
for (int i = 0; i < c; ++i)
cout << "SKT" << skt << ',';
};
if (show_channel_output)
{
for (uint64 channel = 0; channel < max_imc_channels; ++channel)
{
if (md->iMC_Rd_socket_chan[skt][channel] < 0.0 && md->iMC_Wr_socket_chan[skt][channel] < 0.0) //If the channel read neg. value, the channel is not working; skip it.
continue;
choose(outputType,
[printSKT]() {
printSKT(2);
},
[&channel]() {
cout << "Ch" << channel << "Read,"
<< "Ch" << channel << "Write,";
},
[&md, &skt, &channel]() {
cout << setw(8) << md->iMC_Rd_socket_chan[skt][channel] << ','
<< setw(8) << md->iMC_Wr_socket_chan[skt][channel] << ',';
});
if (md->PMM)
{
choose(outputType,
[printSKT]() {
printSKT(2);
},
[&channel]() {
cout << "Ch" << channel << "PMM_Read,"
<< "Ch" << channel << "PMM_Write,";
},
[&skt, &md, &channel]() {
cout << setw(8) << md->iMC_PMM_Rd_socket_chan[skt][channel] << ','
<< setw(8) << md->iMC_PMM_Wr_socket_chan[skt][channel] << ',';
});
}
}
}
choose(outputType,
[printSKT]() {
printSKT(2);
},
[]() {
cout << "Mem Read (MB/s),Mem Write (MB/s),";
},
[&md, &skt]() {
cout << setw(8) << md->iMC_Rd_socket[skt] << ','
<< setw(8) << md->iMC_Wr_socket[skt] << ',';
});
if (md->PMM || md->PMMMixedMode)
{
choose(outputType,
[printSKT]() {
printSKT(2);
},
[]() {
cout << "PMM_Read (MB/s), PMM_Write (MB/s),";
},
[&md, &skt]() {
cout << setw(8) << md->iMC_PMM_Rd_socket[skt] << ','
<< setw(8) << md->iMC_PMM_Wr_socket[skt] << ',';
});
}
if (md->PMMMixedMode)
{
choose(outputType,
[printSKT]() {
printSKT(3);
},
[]() {
cout << "PMM_AD (MB/s), PMM_MM (MB/s), PMM_MM_Bw/DRAM_Bw,";
},
[&md, &skt]() {
cout << setw(8) << AD_BW(md, skt) << ','
<< setw(8) << md->iMC_PMM_MemoryMode_Miss_socket[skt] << ','
<< setw(8) << PMM_MM_Ratio(md, skt) << ',';
});
}
if (m->getCPUModel() != PCM::KNL)
{
if (md->PMM == false && md->PMMMixedMode == false)
{
choose(outputType,
[printSKT]() {
printSKT();
},
[]() {
cout << "P. Write (T/s),";
},
[&md, &skt]() {
cout << setw(10) << dec << md->partial_write[skt] << ',';
});
}
}
choose(outputType,
[printSKT]() {
printSKT();
},
[]() {
cout << "Memory (MB/s),";
},
[&]() {
cout << setw(8) << md->iMC_Rd_socket[skt] + md->iMC_Wr_socket[skt] << ',';
sysReadDRAM += md->iMC_Rd_socket[skt];
sysWriteDRAM += md->iMC_Wr_socket[skt];
sysReadPMM += md->iMC_PMM_Rd_socket[skt];
sysWritePMM += md->iMC_PMM_Wr_socket[skt];
});
if (m->MCDRAMmemoryTrafficMetricsAvailable())
{
if (show_channel_output)
{
for (uint64 channel = 0; channel < max_edc_channels; ++channel)
{
if (md->EDC_Rd_socket_chan[skt][channel] < 0.0 && md->EDC_Wr_socket_chan[skt][channel] < 0.0) //If the channel read neg. value, the channel is not working; skip it.
continue;
choose(outputType,
[printSKT]() {
printSKT(2);
},
[&channel]() {
cout << "EDC_Ch" << channel << "Read,"
<< "EDC_Ch" << channel << "Write,";
},
[&md, &skt, &channel]() {
cout << setw(8) << md->EDC_Rd_socket_chan[skt][channel] << ','
<< setw(8) << md->EDC_Wr_socket_chan[skt][channel] << ',';
});
}
}
choose(outputType,
[printSKT]() {
printSKT(3);
},
[]() {
cout << "MCDRAM Read (MB/s), MCDRAM Write (MB/s), MCDRAM (MB/s),";
},
[&]() {
cout << setw(8) << md->EDC_Rd_socket[skt] << ','
<< setw(8) << md->EDC_Wr_socket[skt] << ','
<< setw(8) << md->EDC_Rd_socket[skt] + md->EDC_Wr_socket[skt] << ',';
sysReadDRAM += md->EDC_Rd_socket[skt];
sysWriteDRAM += md->EDC_Wr_socket[skt];
});
}
}
if (md->PMM || md->PMMMixedMode)
{
choose(outputType,
[]() {
cout << "System,System,System,System,";
},
[]() {
cout << "DRAMRead,DRAMWrite,PMMREAD,PMMWrite,";
},
[&]() {
cout << setw(10) << sysReadDRAM << ','
<< setw(10) << sysWriteDRAM << ','
<< setw(10) << sysReadPMM << ','
<< setw(10) << sysWritePMM << ',';
});
}
choose(outputType,
[]() {
cout << "System,System,System\n";
},
[]() {
cout << "Read,Write,Memory" << endl;
},
[&]() {
cout << setw(10) << sysReadDRAM + sysReadPMM << ','
<< setw(10) << sysWriteDRAM + sysWritePMM << ','
<< setw(10) << sysReadDRAM + sysReadPMM + sysWriteDRAM + sysWritePMM << endl;
});
}
void calculate_bandwidth(PCM *m, const ServerUncorePowerState uncState1[], const ServerUncorePowerState uncState2[], const uint64 elapsedTime, const bool csv, bool & csvheader, uint32 no_columns, const bool PMM, const bool show_channel_output, const bool PMMMixedMode)
{
//const uint32 num_imc_channels = m->getMCChannelsPerSocket();
//const uint32 num_edc_channels = m->getEDCChannelsPerSocket();
memdata_t md;
md.PMM = PMM;
md.PMMMixedMode = PMMMixedMode;
for(uint32 skt = 0; skt < m->getNumSockets(); ++skt)
{
md.iMC_Rd_socket[skt] = 0.0;
md.iMC_Wr_socket[skt] = 0.0;
md.iMC_PMM_Rd_socket[skt] = 0.0;
md.iMC_PMM_Wr_socket[skt] = 0.0;
md.iMC_PMM_MemoryMode_Miss_socket[skt] = 0.0;
md.EDC_Rd_socket[skt] = 0.0;
md.EDC_Wr_socket[skt] = 0.0;
md.partial_write[skt] = 0;
for (uint32 i = 0; i < max_imc_controllers; ++i)
{
md.M2M_NM_read_hit_rate[skt][i] = 0.;
}
const uint32 numChannels1 = (uint32)m->getMCChannels(skt, 0); // number of channels in the first controller
auto toBW = [&elapsedTime](const uint64 nEvents)
{
return (float)(nEvents * 64 / 1000000.0 / (elapsedTime / 1000.0));
};
switch (m->getCPUModel())
{
case PCM::KNL:
for (uint32 channel = 0; channel < max_edc_channels; ++channel)
{
if (getEDCCounter(channel, ServerPCICFGUncore::EventPosition::READ, uncState1[skt], uncState2[skt]) == 0.0 && getEDCCounter(channel, ServerPCICFGUncore::EventPosition::WRITE, uncState1[skt], uncState2[skt]) == 0.0)
{
md.EDC_Rd_socket_chan[skt][channel] = -1.0;
md.EDC_Wr_socket_chan[skt][channel] = -1.0;
continue;
}
md.EDC_Rd_socket_chan[skt][channel] = toBW(getEDCCounter(channel, ServerPCICFGUncore::EventPosition::READ, uncState1[skt], uncState2[skt]));
md.EDC_Wr_socket_chan[skt][channel] = toBW(getEDCCounter(channel, ServerPCICFGUncore::EventPosition::WRITE, uncState1[skt], uncState2[skt]));
md.EDC_Rd_socket[skt] += md.EDC_Rd_socket_chan[skt][channel];
md.EDC_Wr_socket[skt] += md.EDC_Wr_socket_chan[skt][channel];
}
default:
for (uint32 channel = 0; channel < max_imc_channels; ++channel)
{
uint64 reads = 0, writes = 0, pmmReads = 0, pmmWrites = 0, pmmMemoryModeCleanMisses = 0, pmmMemoryModeDirtyMisses = 0;
reads = getMCCounter(channel, ServerPCICFGUncore::EventPosition::READ, uncState1[skt], uncState2[skt]);
writes = getMCCounter(channel, ServerPCICFGUncore::EventPosition::WRITE, uncState1[skt], uncState2[skt]);
if (PMM)
{
pmmReads = getMCCounter(channel, ServerPCICFGUncore::EventPosition::PMM_READ, uncState1[skt], uncState2[skt]);
pmmWrites = getMCCounter(channel, ServerPCICFGUncore::EventPosition::PMM_WRITE, uncState1[skt], uncState2[skt]);
}
if (PMMMixedMode)
{
pmmMemoryModeCleanMisses = getMCCounter(channel, ServerPCICFGUncore::EventPosition::PMM_MM_MISS_CLEAN, uncState1[skt], uncState2[skt]);
pmmMemoryModeDirtyMisses = getMCCounter(channel, ServerPCICFGUncore::EventPosition::PMM_MM_MISS_DIRTY, uncState1[skt], uncState2[skt]);
}
if (reads + writes == 0)
{
if ((!PMM || (pmmReads + pmmWrites == 0)) || (!PMMMixedMode || (pmmMemoryModeCleanMisses + pmmMemoryModeDirtyMisses == 0)))
{
md.iMC_Rd_socket_chan[skt][channel] = -1.0;
md.iMC_Wr_socket_chan[skt][channel] = -1.0;
continue;
}
}
md.iMC_Rd_socket_chan[skt][channel] = toBW(reads);
md.iMC_Wr_socket_chan[skt][channel] = toBW(writes);
md.iMC_Rd_socket[skt] += md.iMC_Rd_socket_chan[skt][channel];
md.iMC_Wr_socket[skt] += md.iMC_Wr_socket_chan[skt][channel];
if (PMM)
{
md.iMC_PMM_Rd_socket_chan[skt][channel] = toBW(pmmReads);
md.iMC_PMM_Wr_socket_chan[skt][channel] = toBW(pmmWrites);
md.iMC_PMM_Rd_socket[skt] += md.iMC_PMM_Rd_socket_chan[skt][channel];
md.iMC_PMM_Wr_socket[skt] += md.iMC_PMM_Wr_socket_chan[skt][channel];
md.M2M_NM_read_hit_rate[skt][(channel < numChannels1) ? 0 : 1] += (float)reads;
}
else if (PMMMixedMode)
{
md.iMC_PMM_MemoryMode_Miss_socket_chan[skt][channel] = toBW(pmmMemoryModeCleanMisses + 2 * pmmMemoryModeDirtyMisses);
md.iMC_PMM_MemoryMode_Miss_socket[skt] += md.iMC_PMM_MemoryMode_Miss_socket_chan[skt][channel];
}
else
{
md.partial_write[skt] += (uint64)(getMCCounter(channel, ServerPCICFGUncore::EventPosition::PARTIAL, uncState1[skt], uncState2[skt]) / (elapsedTime / 1000.0));
}
}
}
if (PMMMixedMode)
{
for(uint32 c = 0; c < max_imc_controllers; ++c)
{
md.iMC_PMM_Rd_socket[skt] += toBW(getM2MCounter(c, ServerPCICFGUncore::EventPosition::PMM_READ, uncState1[skt],uncState2[skt]));
md.iMC_PMM_Wr_socket[skt] += toBW(getM2MCounter(c, ServerPCICFGUncore::EventPosition::PMM_WRITE, uncState1[skt],uncState2[skt]));;
}
}
if (PMM)
{
for(uint32 c = 0; c < max_imc_controllers; ++c)
{
if(md.M2M_NM_read_hit_rate[skt][c] != 0.0)
{
md.M2M_NM_read_hit_rate[skt][c] = ((float)getM2MCounter(c, ServerPCICFGUncore::EventPosition::NM_HIT, uncState1[skt],uncState2[skt]))/ md.M2M_NM_read_hit_rate[skt][c];
}
}
}
}
if (csv)
{
if (csvheader)
{
display_bandwidth_csv(m, &md, elapsedTime, show_channel_output, Header1);
display_bandwidth_csv(m, &md, elapsedTime, show_channel_output, Header2);
csvheader = false;
}
display_bandwidth_csv(m, &md, elapsedTime, show_channel_output, Data);
}
else
{
display_bandwidth(m, &md, no_columns, show_channel_output);
}
}
void calculate_bandwidth_rank(PCM *m, const ServerUncorePowerState uncState1[], const ServerUncorePowerState uncState2[], const uint64 elapsedTime, const bool csv, bool & csvheader, const uint32 no_columns, const int rankA, const int rankB)
{
uint32 skt = 0;
cout.setf(ios::fixed);
cout.precision(2);
uint32 numSockets = m->getNumSockets();
while(skt < numSockets)
{
auto printRow = [&skt, &uncState1, &uncState2, &elapsedTime, &rankA, &rankB](const uint32 no_columns) {
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;
};
// Full row
if ((skt + no_columns) <= numSockets)
{
printRow(no_columns);
}
else //Display the remaining sockets in this row
{
printRow(numSockets - skt);
}
}
}
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
cerr << endl;
cerr << " Processor Counter Monitor: Memory Bandwidth Monitoring Utility " << PCM_VERSION << endl;
cerr << 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, show_channel_output=true;
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;
bool PMM = false;
bool PMMMixedMode = false;
unsigned int numberOfIterations = 0; // number of iterations
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, "-i", 2) == 0 ||
strncmp(*argv, "/i", 2) == 0)
{
string cmd = string(*argv);
size_t found = cmd.find('=', 2);
if (found != string::npos) {
string tmp = cmd.substr(found + 1);
if (!tmp.empty()) {
numberOfIterations = (unsigned int)atoi(tmp.c_str());
}
}
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;
}
if (strncmp(*argv, "--nochannel", 11) == 0 ||
strncmp(*argv, "-nc", 3) == 0 ||
strncmp(*argv, "/nc", 3) == 0)
{
show_channel_output = false;
continue;
}
if (strncmp(*argv, "-pmm", 6) == 0 ||
strncmp(*argv, "/pmm", 6) == 0)
{
PMM = true;
continue;
}
if (strncmp(*argv, "-mixed", 6) == 0 ||
strncmp(*argv, "/mixed", 6) == 0)
{
PMMMixedMode = true;
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 = Driver(Driver::msrLocalPath());
if (!tmpDrvObject.start())
{
wcerr << "Can not access CPU counters" << endl;
wcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " 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;