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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
|
//
// 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 NVIDIA 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 ``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.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.
#include "MemTracker.h"
#if PX_WINDOWS_FAMILY // only compile this source code for windows!
#define USE_HASH_MAP 1
// VC10 hash maps are abominably slow with iterator debug level == 0
// Note: All STL containers are used internally, so their should not be any resultant conflict
#if (PX_VC == 10) && USE_HASH_MAP
#define OVERRIDE_ITERATOR_DEBUG_LEVEL 1
#else
#define OVERRIDE_ITERATOR_DEBUG_LEVEL 0
#endif
#if OVERRIDE_ITERATOR_DEBUG_LEVEL
#undef _ITERATOR_DEBUG_LEVEL
#define _ITERATOR_DEBUG_LEVEL 0
#define _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH 1
#endif
#include "htmltable.h"
#include "PxSimpleTypes.h"
#include "PxAssert.h"
#include <stdio.h>
#include <string>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <map>
#include <vector>
#include <windows.h>
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include <hash_map>
#pragma warning(disable:4100 4996 4267 4239)
namespace MEM_TRACKER
{
class ContextType
{
public:
ContextType(void)
{
mContext = NULL;
mType = NULL;
mAllocCount = 0;
mAllocSize = 0;
}
ContextType(const char *context,const char *type,size_t size)
{
mContext = context;
mType = type;
char scratch[512];
sprintf_s(scratch,512,"PlatformAnalyzer/ContextType/%s/%s/Count(AllocCount)", mContext, mType );
mAllocCountSpec = scratch;
sprintf_s(scratch,512,"PlatformAnalyzer/ContextType/%s/%s/Size(AllocSize)", mContext, mType );
mAllocSizeSpec = scratch;
mAllocCount = 1;
mAllocSize = size;
}
const char *mContext;
const char *mType;
uint32_t mAllocCount;
size_t mAllocSize;
std::string mAllocCountSpec;
std::string mAllocSizeSpec;
};
class ByContextIndex
{
public:
ByContextIndex(const char *context,const char *type)
{
mContext = context;
mType = type;
}
const char *mContext;
const char *mType;
};
class ByContextHasher
{
public:
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
size_t operator()(const ByContextIndex &index) const
{
size_t hash1 = (size_t)index.mContext;
size_t hash2 = (size_t)index.mType;
size_t hash = hash1 ^ hash2;
return hash;
}
bool operator()(const ByContextIndex &s1,const ByContextIndex &s2) const
{
if ( s1.mContext < s2.mContext ) return true;
if ( s1.mContext > s2.mContext ) return false;
return s1.mType < s2.mType;
}
};
#if USE_HASH_MAP
typedef stdext::hash_map< ByContextIndex, ContextType, ByContextHasher > ByContextTypeHash;
#else
typedef std::map< ByContextIndex, ContextType, ByContextHasher > ByContextTypeHash;
#endif
static void getDateTime(char *date_time)
{
time_t ltime;
struct tm *today;
_tzset();
time( <ime );
today = localtime( <ime );
strftime( date_time, 128,"%A-%B-%d-%Y-%I-%M-%S-%p", today );
}
size_t getPow2(size_t s,size_t &p)
{
size_t ret = 0;
while ( s > p )
{
p = p<<1;
ret++;
}
return ret;
}
class MemTrack
{
public:
MemoryType mType;
size_t mSize;
size_t mThreadId;
const char *mContext;
const char *mClassName;
const char *mFileName;
uint32_t mLineNo;
size_t mAllocCount;
};
class ByType
{
public:
ByType(size_t size)
{
mSize = size;
mCount = 1;
}
ByType(void) { };
size_t mSize;
size_t mCount;
};
class BySource
{
public:
BySource(void) { };
BySource(const MemTrack &t)
{
mClassName = t.mClassName;
mFileName = t.mFileName;
mLineNo = t.mLineNo;
mSize = t.mSize;
mLineNo = t.mLineNo;
mCount = 0;
}
const char *mClassName;
const char *mFileName;
size_t mLineNo;
size_t mSize;
size_t mCount;
};
#if USE_HASH_MAP
typedef stdext::hash_map< size_t, ByType > ByTypeHash;
#else
typedef std::map< size_t, ByType > ByTypeHash;
#endif
class BySourceIndex
{
public:
BySourceIndex(const char *fileName,uint32_t lineno)
{
mFileName = fileName;
mLineNo = lineno;
}
const char *mFileName;
uint32_t mLineNo;
};
class BySourceHasher
{
public:
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
size_t operator()(const BySourceIndex &index) const
{
size_t hash = (size_t)index.mFileName;
hash = hash^index.mLineNo;
return hash;
}
bool operator()(const BySourceIndex &s1,const BySourceIndex &s2) const
{
if ( s1.mFileName < s2.mFileName ) return true;
if ( s1.mFileName > s2.mFileName ) return false;
return s1.mLineNo < s2.mLineNo;
}
};
#if USE_HASH_MAP
typedef stdext::hash_map< BySourceIndex, BySource, BySourceHasher > BySourceHash;
typedef stdext::hash_map< size_t , MemTrack > MemTrackHash;
#else
typedef std::map< BySourceIndex, BySource, BySourceHasher > BySourceHash;
typedef std::map< size_t , MemTrack > MemTrackHash;
#endif
typedef std::vector< MemTrack > MemTrackVector;
class ReportContext
{
public:
ReportContext(const char *context)
{
mContext = context;
}
void add(const MemTrack &t)
{
assert(t.mAllocCount > 0 );
mMemTracks.push_back(t);
}
const char * getContext(void) const { return mContext; };
// first we generate a report based on type.
void generateReport(nvidia::HtmlDocument *document,nvidia::HtmlTable *contextTable)
{
unsigned int totalMemory = 0;
unsigned int totalAllocations = 0;
unsigned int typeCount = 0;
unsigned int sourceCount = 0;
{
ByTypeHash bt;
MemTrackVector::iterator i;
for (i=mMemTracks.begin(); i!=mMemTracks.end(); i++)
{
const MemTrack &t = (*i);
size_t hash = (size_t)t.mClassName;
ByTypeHash::iterator found = bt.find(hash);
if ( found == bt.end() )
{
ByType b(t.mSize);
bt[hash] = b;
}
else
{
ByType &b = (ByType &)(*found).second;
b.mSize+=t.mSize;
b.mCount++;
}
}
{
typeCount = bt.size();
char scratch[512];
char date_time[512];
getDateTime(date_time);
sprintf(scratch,"Memory Usage by Class Name or Memory Type for Context: %s : %s", mContext, date_time );
nvidia::HtmlTable *table = document->createHtmlTable(scratch);
// 1 2 3
table->addHeader("Memory/Type,Memory/Size,Allocation/Count");
table->addSort("Sorted By Memory Size",2,false,3,false);
for (ByTypeHash::iterator iter = bt.begin(); iter !=bt.end(); ++iter)
{
ByType b = (*iter).second;
table->addColumn( (const char *)(*iter).first );
table->addColumn( (uint32_t)b.mSize );
table->addColumn( (uint32_t)b.mCount );
table->nextRow();
}
table->computeTotals();
}
}
{
BySourceHash bt;
MemTrackVector::iterator i;
for (i=mMemTracks.begin(); i!=mMemTracks.end(); i++)
{
const MemTrack &t = (*i);
BySource b(t);
BySourceIndex index(b.mFileName,b.mLineNo);
BySourceHash::iterator found = bt.find(index);
if ( found == bt.end() )
{
b.mCount = 1;
bt[index] = b;
}
else
{
BySource &bs = (BySource &)(*found).second;
bs.mSize+=t.mSize;
bs.mCount++;
}
}
{
sourceCount = bt.size();
char scratch[512];
char date_time[512];
getDateTime(date_time);
sprintf(scratch,"Memory Usage by Source File and Line Number for Context: %s : %s", mContext, date_time );
nvidia::HtmlTable *table = document->createHtmlTable(scratch);
// 1 2 3 4 5
table->addHeader("Source/File,Line/Number,Memory/Type,Memory/Size,Allocation/Count");
table->addSort("Sorted By Memory Size",4,false,5,false);
table->excludeTotals(2);
for (BySourceHash::iterator i=bt.begin(); i!=bt.end(); ++i)
{
BySource b = (*i).second;
table->addColumn( b.mFileName );
table->addColumn( (uint32_t)b.mLineNo );
table->addColumn( b.mClassName );
table->addColumn( (uint32_t)b.mSize );
assert( b.mCount > 0 );
table->addColumn( (uint32_t)b.mCount );
table->nextRow();
totalMemory+=b.mSize;
totalAllocations+=b.mCount;
}
table->computeTotals();
}
}
// Power of two, sizes 1-256
{
char scratch[512];
char date_time[512];
getDateTime(date_time);
sprintf(scratch,"Power of Two Memory 1 to 256 bytes for Context: %s : %s", mContext, date_time );
nvidia::HtmlTable *table = document->createHtmlTable(scratch);
// 1 2 3 4
table->addHeader("Mem/Size,Total/Alloc,Alloc/Count,Fragment/Amount");
table->addSort("Sorted By Memory Size",2,false,3,false);
table->excludeTotals(1);
// 0 1 2 3 4 5
// 8, 16, 32, 64, 128, 256
struct Power2
{
Power2(void)
{
memSize = 0;
memTotal = 0;
memCount = 0;
fragmentTotal = 0;
}
unsigned int memSize;
unsigned int memTotal;
unsigned int memCount;
unsigned int fragmentTotal;
};
Power2 p[6];
MemTrackVector::iterator i;
for (i=mMemTracks.begin(); i!=mMemTracks.end(); i++)
{
const MemTrack &t = (*i);
if ( t.mSize <= 256 )
{
size_t p2=8;
size_t index = getPow2(t.mSize,p2);
p[index].memSize = p2;
p[index].memTotal+=t.mSize;
p[index].memCount++;
p[index].fragmentTotal+=(p2-t.mSize);
}
}
for (size_t i=0; i<6; i++)
{
Power2 &t = p[i];
if ( t.memCount > 0 )
{
table->addColumn(t.memSize);
table->addColumn(t.memTotal);
table->addColumn(t.memCount);
table->addColumn(t.fragmentTotal);
table->nextRow();
}
}
table->computeTotals();
}
// power of two allocations > 256 bytes
{
char scratch[512];
char date_time[512];
getDateTime(date_time);
sprintf(scratch,"Power of Two Memory Greater than 256 bytes for Context: %s : %s", mContext, date_time );
nvidia::HtmlTable *table = document->createHtmlTable(scratch);
// 1 2 3 4
table->addHeader("Mem/Size,Total/Alloc,Alloc/Count,Fragment/Amount");
table->addSort("Sorted By Memory Size",2,false,3,false);
table->excludeTotals(1);
struct Power2
{
Power2(void)
{
memSize = 0;
memTotal = 0;
memCount = 0;
fragmentTotal = 0;
}
unsigned int memSize;
unsigned int memTotal;
unsigned int memCount;
unsigned int fragmentTotal;
};
Power2 p[32];
MemTrackVector::iterator i;
for (i=mMemTracks.begin(); i!=mMemTracks.end(); i++)
{
const MemTrack &t = (*i);
if ( t.mSize > 256 )
{
size_t p2=512;
size_t index = getPow2(t.mSize,p2);
p[index].memSize = p2;
p[index].memTotal+=t.mSize;
p[index].memCount++;
p[index].fragmentTotal+=(p2-t.mSize);
}
}
for (size_t i=0; i<32; i++)
{
Power2 &t = p[i];
if ( t.memCount > 0 )
{
table->addColumn(t.memSize);
table->addColumn(t.memTotal);
table->addColumn(t.memCount);
table->addColumn(t.fragmentTotal);
table->nextRow();
}
}
table->computeTotals();
}
// context summary..
if ( contextTable )
{
contextTable->addColumn( mContext );
contextTable->addColumn( totalMemory );
contextTable->addColumn( totalAllocations );
contextTable->addColumn( typeCount );
contextTable->addColumn(sourceCount );
contextTable->nextRow();
}
}
private:
const char *mContext;
MemTrackVector mMemTracks;
};
#if USE_HASH_MAP
typedef stdext::hash_map< size_t , ReportContext * > ReportContextHash;
#else
typedef std::map< size_t , ReportContext * > ReportContextHash;
#endif
class MyMemTracker : public MemTracker
{
public:
MyMemTracker(void)
{
mSingleThreaded = false;
mFrameNo = 1;
mAllocCount = 0;
mAllocSize = 0;
mAllocFrameCount = 0;
mFreeFrameCount = 0;
mAllocFrameSize = 0;
mFreeFrameSize = 0;
mDocument = 0;
mFrameSummary = 0;
mDetailed = 0;
// nvidia::HtmlTableInterface *h = nvidia::getHtmlTableInterface();
// if ( h )
// {
//mDocument = h->createHtmlDocument("MemTracker");
// }
}
virtual ~MyMemTracker(void)
{
if ( mDocument )
{
nvidia::HtmlTableInterface *h = nvidia::getHtmlTableInterface();
h->releaseHtmlDocument(mDocument);
}
}
virtual void trackAlloc(size_t threadId,void *mem,size_t size,MemoryType type,const char *context,const char *className,const char *fileName,uint32_t lineno)
{
if ( mem )
{
addContextType(context,className,size);
mAllocCount++;
mAllocSize+=size;
mAllocFrameCount++;
mAllocFrameSize+=size;
size_t hash = (size_t) mem;
MemTrack t;
t.mType = type;
t.mSize = size;
t.mThreadId = threadId;
t.mContext = context;
t.mClassName = className;
t.mFileName = fileName;
t.mLineNo = lineno;
t.mAllocCount = 1;
MemTrackHash::iterator found = mMemory.find(hash);
if ( found != mMemory.end() )
{
PX_ALWAYS_ASSERT();
const MemTrack &previous = (*found).second;
printf("Prev: %s\r\n", previous.mClassName );
PX_UNUSED(previous);
}
// track which allocation number this one was.
{
BySource b(t);
b.mCount = 1;
BySourceIndex index(b.mFileName,b.mLineNo);
BySourceHash::iterator found = mSourceHash.find(index);
if ( found == mSourceHash.end() )
{
mSourceHash[index] = b;
}
else
{
BySource bs = (*found).second;
bs.mCount++;
t.mAllocCount = bs.mCount;
mSourceHash[index] = bs;
}
}
if ( mDetailed )
{
mDetailed->addColumnHex( (size_t)mem );
switch ( type )
{
case MT_NEW:
mDetailed->addColumn("NEW");
break;
case MT_NEW_ARRAY:
mDetailed->addColumn("NEW_ARRAY");
break;
case MT_MALLOC:
mDetailed->addColumn("MALLOC");
break;
case MT_GLOBAL_NEW:
mDetailed->addColumn("GLOBAL_NEW");
break;
case MT_GLOBAL_NEW_ARRAY:
mDetailed->addColumn("GLOBAL_NEW_ARRAY");
break;
default:
PX_ALWAYS_ASSERT();
mDetailed->addColumn("ERROR");
break;
}
mDetailed->addColumn((uint32_t)size);
mDetailed->addColumn((uint32_t)t.mAllocCount);
mDetailed->addColumnHex(threadId);
mDetailed->addColumn(context);
mDetailed->addColumn(className);
mDetailed->addColumn(fileName);
mDetailed->addColumn((uint32_t)lineno);
mDetailed->nextRow();
}
mMemory[hash] = t;
PX_ASSERT( mAllocCount == mMemory.size() );
}
}
virtual void trackRealloc(size_t threadId,
void *oldMem,
void *newMem,
size_t newSize,
const char *context,
const char *className,
const char *fileName,
uint32_t lineno)
{
TrackInfo info;
bool found = trackInfo(oldMem,info);
PX_ASSERT(found);
if ( found )
{
PX_ASSERT( info.mType == MT_MALLOC );
trackFree(threadId,oldMem,MT_FREE,context,fileName,lineno);
trackAlloc(threadId,newMem,newSize,info.mType,context,className,fileName,lineno);
}
}
virtual const char * typeString(MemoryType type)
{
const char *ret = "unknown";
switch ( type )
{
case MT_NEW:
ret = "new operator";
break;
case MT_NEW_ARRAY:
ret = "new[] array operator";
break;
case MT_MALLOC:
ret = "malloc";
break;
case MT_FREE:
ret = "free";
break;
case MT_DELETE:
ret = "delete operator";
break;
case MT_DELETE_ARRAY:
ret = "delete[] array operator";
break;
case MT_GLOBAL_NEW:
ret = "global new";
break;
case MT_GLOBAL_NEW_ARRAY:
ret = "global new[] array";
break;
case MT_GLOBAL_DELETE:
ret = "global delete";
break;
case MT_GLOBAL_DELETE_ARRAY:
ret = "global delete array";
break;
}
return ret;
}
virtual const char * trackValidateFree(size_t threadId,void *mem,MemoryType type,const char *context,const char *fileName,uint32_t lineno)
{
const char *ret = NULL;
if ( mem )
{
char scratch[1024];
scratch[0] = 0;
size_t hash = (size_t) mem;
MemTrackHash::iterator found = mMemory.find(hash);
if ( found == mMemory.end() )
{
sprintf_s(scratch,1024,"Error! Tried to free memory never tracked. Source: %s : Line: %d\r\n", fileName, lineno);
PX_ALWAYS_ASSERT();
}
else
{
MemTrack &t = (MemTrack &)(*found).second;
switch ( type )
{
case MT_DELETE:
if ( t.mType != MT_NEW )
{
sprintf_s(scratch,1024,"Error: Allocated with %s but deallocated with the delete operator\r\n", typeString(t.mType));
}
break;
case MT_DELETE_ARRAY:
if ( t.mType != MT_NEW_ARRAY )
{
sprintf_s(scratch,1024,"Error: Allocated with %s but deallocated with the delete array operator.\r\n", typeString(t.mType));
}
break;
case MT_FREE:
if ( t.mType != MT_MALLOC )
{
sprintf_s(scratch,1024,"Error: Allocated with %s but deallocated with malloc.\r\n", typeString(t.mType));
}
break;
case MT_GLOBAL_DELETE:
if ( t.mType != MT_GLOBAL_NEW )
{
sprintf_s(scratch,1024,"Error: Allocated with %s but deallocated with global delete.\r\n", typeString(t.mType));
}
break;
case MT_GLOBAL_DELETE_ARRAY:
if ( t.mType != MT_GLOBAL_NEW_ARRAY )
{
sprintf_s(scratch,1024,"Error: Allocated with %s but deallocated with global delete array.\r\n", typeString(t.mType));
}
break;
default:
sprintf_s(scratch,1024,"Invalid memory type encountered. Data corrupted!?\r\n");
break;
}
if ( t.mThreadId != threadId && mSingleThreaded )
{
sprintf_s(scratch,1024,"Memory de-allocated from a different thread than it was allocated from!\r\n");
}
if ( !context )
{
sprintf_s(scratch,1024,"Null context!\r\n");
}
if ( !t.mContext )
{
sprintf_s(scratch,1024,"Original memory block allocated with a null context, or the data is corrupted!\r\n");
}
if ( context != t.mContext )
{
sprintf_s(scratch,1024,"Memory is de-allocated from a different context. Allocated from (%s) deallocated from (%s)\r\n", context, t.mContext );
}
}
if ( scratch[0] )
{
mErrorMessage = scratch;
ret = mErrorMessage.c_str();
}
}
return ret;
}
void addContextType(const char *context,const char *type,size_t size)
{
ByContextIndex index(context,type);
ByContextTypeHash::iterator found = mContextType.find(index);
if ( found != mContextType.end() )
{
ContextType &c = (ContextType &)(*found).second;
PX_ASSERT( c.mContext == context );
PX_ASSERT( c.mType == type );
c.mAllocCount++;
c.mAllocSize+=size;
}
else
{
ContextType c(context,type,size);
mContextType[index] = c;
}
#if 0
int foundCount=0;
{
for (ByContextTypeHash::iterator i=mContextType.begin(); i!=mContextType.end(); ++i)
{
const ByContextIndex &index = (*i).first;
if ( index.mContext == context && index.mType == type )
{
foundCount++;
}
}
}
PX_ASSERT( foundCount == 1 );
#endif
}
void removeContextType(const char *context,const char *type,size_t size)
{
ByContextIndex index(context,type);
ByContextTypeHash::iterator found = mContextType.find(index);
if ( found != mContextType.end() )
{
ContextType &c = (ContextType &)(*found).second;
c.mAllocCount--;
c.mAllocSize-=size;
}
else
{
PX_ALWAYS_ASSERT();
}
}
virtual void trackFree(size_t threadId,void *mem,MemoryType type,const char *context,const char *fileName,uint32_t lineno)
{
if ( mem )
{
size_t hash = (size_t) mem;
MemTrackHash::iterator found = mMemory.find(hash);
if ( found == mMemory.end() )
{
PX_ALWAYS_ASSERT();
}
else
{
const MemTrack &t = (*found).second;
removeContextType(t.mContext,t.mClassName,t.mSize);
if ( mDetailed )
{
mDetailed->addColumnHex( (size_t) mem );
switch ( type )
{
case MT_FREE:
mDetailed->addColumn("FREE");
break;
case MT_DELETE:
mDetailed->addColumn("DELETE");
break;
case MT_DELETE_ARRAY:
mDetailed->addColumn("DELETE_ARRAY");
break;
case MT_GLOBAL_DELETE:
mDetailed->addColumn("GLOBAL_DELETE");
break;
case MT_GLOBAL_DELETE_ARRAY:
mDetailed->addColumn("GLOBAL_DELETE_ARRAY");
break;
default:
printf("Invalid memory allocation type! %s : %d\r\n", fileName, lineno );
mDetailed->addColumn("INVALID ALLOC TYPE");
break;
}
mDetailed->addColumn((uint32_t)t.mSize);
mDetailed->addColumn((uint32_t)t.mAllocCount);
mDetailed->addColumnHex(threadId);
mDetailed->addColumn(context);
mDetailed->addColumn(t.mClassName);
mDetailed->addColumn(fileName);
mDetailed->addColumn((uint32_t)lineno);
mDetailed->nextRow();
}
mAllocCount--;
mAllocSize-=t.mSize;
mFreeFrameCount++;
mFreeFrameSize+=t.mSize;
mMemory.erase(hash);
PX_ASSERT( mAllocCount == mMemory.size() );
}
}
}
void trackFrame(void)
{
mFrameNo++;
if ( mAllocFrameCount || mFreeFrameCount )
{
if ( mFrameSummary )
{
mFrameSummary->addColumn((uint32_t)mFrameNo);
mFrameSummary->addColumn((uint32_t)mAllocCount);
mFrameSummary->addColumn((uint32_t)mAllocSize);
mFrameSummary->addColumn((uint32_t)mAllocFrameCount);
mFrameSummary->addColumn((uint32_t)mAllocFrameSize);
mFrameSummary->addColumn((uint32_t)mFreeFrameCount);
mFrameSummary->addColumn((uint32_t)mFreeFrameSize);
mFrameSummary->addColumn((uint32_t)(mAllocFrameCount-mFreeFrameCount));
mFrameSummary->addColumn((uint32_t)(mAllocFrameSize-mFreeFrameSize));
mFrameSummary->nextRow();
}
mAllocFrameCount = 0;
mAllocFrameSize = 0;
mFreeFrameCount = 0;
mFreeFrameSize = 0;
if ( mDetailed )
{
char scratch[512];
sprintf(scratch,"New Frame %d", (int)mFrameNo );
mDetailed->addColumn(scratch);
mDetailed->addColumn((uint32_t)mAllocSize);
mDetailed->nextRow();
}
}
}
virtual void releaseReportMemory(void *mem)
{
::free(mem);
}
void *generateReport(MemoryReportFormat format,const char *fname,uint32_t &saveLen,bool reportAllLeaks)
{
void *ret = NULL;
saveLen = 0;
//
nvidia::HtmlTable *contextTable = NULL;
{
char scratch[512];
char date_time[512];
getDateTime(date_time);
sprintf(scratch,"Summary Report for All Contexts : %s", date_time);
contextTable = mDocument->createHtmlTable(scratch);
// 1 2 3
contextTable->addHeader("Memory/Context,Total/Memory,Alloc/Count,Type/Count,Source/Count");
contextTable->addSort("Sorted By Total Memory",2,true,3,true);
}
ReportContextHash rchash;
{
ReportContext *current = 0;
for (MemTrackHash::iterator i=mMemory.begin(); i!=mMemory.end(); ++i)
{
MemTrack t = (*i).second;
if ( (current == 0) || current->getContext() != t.mContext )
{
size_t hash = (size_t)t.mContext;
ReportContextHash::iterator found = rchash.find(hash);
if ( found == rchash.end() )
{
current = new ReportContext(t.mContext);
rchash[hash] = current;
}
else
{
current = (*found).second;
}
}
current->add(t);
}
//
if ( reportAllLeaks )
{
char scratch[512];
char date_time[512];
getDateTime(date_time);
sprintf(scratch,"Memory Leaks in order of allocation : %s", date_time);
nvidia::HtmlTable *table = mDocument->createHtmlTable(scratch);
// 1 2 3 4 5 6 7 8
table->addHeader("Memory/Address,Alloc/Count,Alloc/Size,ThreadId,Context,Class/Type,Source/File,Lineno");
table->addSort("Sorted By Source File and Line Number",7,true,8,true);
table->excludeTotals(2);
table->excludeTotals(8);
for (MemTrackHash::iterator i=mMemory.begin(); i!=mMemory.end(); ++i)
{
MemTrack t = (*i).second;
table->addColumnHex( (size_t)(*i).first ); // memory address
table->addColumn((uint32_t) t.mAllocCount ); // allocation count.
table->addColumn((uint32_t) t.mSize ); // size of allocation.
table->addColumnHex( t.mThreadId ); // thread id
table->addColumn( t.mContext ); // context
table->addColumn( t.mClassName ); //
table->addColumn( t.mFileName );
table->addColumn((uint32_t) t.mLineNo );
table->nextRow();
}
table->computeTotals();
}
}
//
{
for (ReportContextHash::iterator i=rchash.begin(); i!=rchash.end(); ++i)
{
ReportContext *rc = (*i).second;
rc->generateReport(mDocument,contextTable);
delete rc;
}
}
//
if ( mFrameSummary )
{
mFrameSummary->excludeTotals(1);
mFrameSummary->excludeTotals(2);
mFrameSummary->excludeTotals(3);
mFrameSummary->excludeTotals(8);
mFrameSummary->excludeTotals(9);
mFrameSummary->computeTotals();
}
contextTable->computeTotals();
nvidia::HtmlSaveType saveType = nvidia::HST_SIMPLE_HTML;
switch ( format )
{
case MRF_SIMPLE_HTML: // just a very simple HTML document containing the tables.
saveType = nvidia::HST_SIMPLE_HTML;
break;
case MRF_CSV: // Saves the Tables out as comma seperated value text
saveType = nvidia::HST_CSV;
break;
case MRF_TEXT: // Saves the tables out in human readable text format.
saveType = nvidia::HST_TEXT;
break;
case MRF_TEXT_EXTENDED: // Saves the tables out in human readable text format, but uses the MS-DOS style extended ASCII character set for the borders.
saveType = nvidia::HST_TEXT_EXTENDED;
break;
}
size_t len;
const char *data = mDocument->saveDocument(len,saveType);
if ( data )
{
ret = ::malloc(len);
memcpy(ret,data,len);
saveLen = len;
mDocument->releaseDocumentMemory(data);
}
return ret;
}
virtual void usage(void)
{
printf("On Frame Number: %d has performed %d memory allocations for a total %d bytes of memory.\r\n", (int)mFrameNo, (int)mAllocCount, (int)mAllocSize );
}
virtual size_t detectLeaks(size_t &acount)
{
acount = mAllocCount;
return mAllocSize;
}
virtual void setLogLevel(bool logEveryAllocation,bool logEveryFrame,bool verifySingleThreaded)
{
mSingleThreaded = verifySingleThreaded;
mLogEveryAllocation = logEveryAllocation;
mLogEveryFrame = logEveryFrame;
if ( mDocument )
{
if ( mLogEveryFrame && mFrameSummary == 0 )
{
char date_time[512];
getDateTime(date_time);
char scratch[1024];
sprintf(scratch,"Per Frame Memory Usage Summary : %s ", date_time);
mFrameSummary = mDocument->createHtmlTable(scratch);
// 1 2 3 4 5 6 7 8 9
mFrameSummary->addHeader("Frame/Number,Total/Alloc Count,Total/Alloc Mem,Frame/Alloc Count,Frame/Alloc Mem,Frame/Free Count,Frame/Free Mem,Frame/Delta Count,Frame/Deleta Mem");
}
if ( mLogEveryAllocation && mDetailed == 0 )
{
char date_time[512];
getDateTime(date_time);
char scratch[2048];
sprintf(scratch,"Detailed Memory Usage Report : %s ",date_time);
mDetailed = mDocument->createHtmlTable(scratch);
// 1 2 3 4 5 6 7 8
mDetailed->addHeader("Memory,Event,Size,Alloc Count,ThreadId,Context,Class or Type,Source File,Line Number");
mDetailed->setOrder(1000); // make sure it displays this last!
}
}
}
virtual bool trackInfo(const void *mem,TrackInfo &info)
{
bool ret = false;
if ( mem )
{
size_t hash = (size_t) mem;
MemTrackHash::iterator found = mMemory.find(hash);
if ( found == mMemory.end() )
{
printf("Error! Tried to get information for memory never tracked.\r\n");
}
else
{
MemTrack &t = (MemTrack &)(*found).second;
info.mMemory = mem;
info.mType = t.mType;
info.mSize = t.mSize;
info.mContext = t.mContext;
info.mClassName = t.mClassName;
info.mFileName = t.mFileName;
info.mLineNo = t.mLineNo;
info.mAllocCount = t.mAllocCount;
ret = true;
}
}
return ret;
}
private:
bool mLogEveryAllocation;
bool mLogEveryFrame;
size_t mFrameNo;
MemTrackHash mMemory;
size_t mAllocFrameCount;
size_t mFreeFrameCount;
size_t mAllocFrameSize;
size_t mFreeFrameSize;
size_t mAllocCount;
size_t mAllocSize;
bool mSingleThreaded;
nvidia::HtmlTable *mFrameSummary;
nvidia::HtmlTable *mDetailed;
nvidia::HtmlDocument *mDocument;
BySourceHash mSourceHash;
ByContextTypeHash mContextType;
std::string mErrorMessage;
};
MemTracker *createMemTracker(void)
{
MyMemTracker *m = new MyMemTracker;
return static_cast< MemTracker *>(m);
}
void releaseMemTracker(MemTracker *mt)
{
MyMemTracker *m = static_cast< MyMemTracker *>(mt);
delete m;
}
};
#endif
|