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
|
#include "UnityPrefix.h"
#include "Runtime/Profiler/MemoryProfiler.h"
#include "Runtime/Allocator/MemoryManager.h"
#if ENABLE_MEM_PROFILER
#include "Runtime/Allocator/MemoryMacros.h"
#include "Runtime/Utilities/BitUtility.h"
#if RECORD_ALLOCATION_SITES
#include "Runtime/Mono/MonoManager.h"
#include "Runtime/Mono/MonoUtility.h"
#endif
#if !UNITY_EDITOR && ENABLE_STACKS_ON_ALL_ALLOCS
#include "Runtime/Utilities/Stacktrace.cpp"
#endif
#include "Runtime/Threads/AtomicOps.h"
#define ROOT_UNRELATED_ALLOCATIONS 0
MemoryProfiler* MemoryProfiler::s_MemoryProfiler = NULL;
UNITY_TLS_VALUE(ProfilerAllocationHeader**) MemoryProfiler::m_RootStack;
UNITY_TLS_VALUE(UInt32) MemoryProfiler::m_RootStackSize;
UNITY_TLS_VALUE(ProfilerAllocationHeader**) MemoryProfiler::m_CurrentRootHeader;
UNITY_TLS_VALUE(bool) MemoryProfiler::m_RecordingAllocation;
struct ProfilerAllocationHeader
{
enum RootStatusMask
{
kIsRootAlloc = 0x1,
kRegisteredRoot = 0x2
};
#if MAINTAIN_RELATED_ALLOCATION_LIST
// pointer to next allocation in root chain. Root is first element in this list
ProfilerAllocationHeader* next;
union
{
// pointer to prev allocation in root chain. Needed for removal
ProfilerAllocationHeader* prev;
// for Roots, the rootReference points to the AllocationRootReference,
AllocationRootReference* rootReference;
};
#endif
volatile int accumulatedSize; // accumulated size of every allocation that relates to this one - just size if child alloc
ProfilerAllocationHeader* GetRootPtr() { return (ProfilerAllocationHeader*)((size_t)relatesTo&~0x3); }
void SetRootPtr (ProfilerAllocationHeader* ptr) { relatesTo = ptr; }
bool IsRoot () { return ((size_t)relatesTo&kIsRootAlloc)!=0; }
bool IsRegisteredRoot () { return ((size_t)relatesTo&kRegisteredRoot)!=0; }
void SetAsRegisteredRoot () { relatesTo = (ProfilerAllocationHeader*)(kIsRootAlloc|kRegisteredRoot); }
void SetAsRoot () { relatesTo = (ProfilerAllocationHeader*)(kIsRootAlloc); }
ProfilerAllocationHeader* relatesTo;
#if RECORD_ALLOCATION_SITES
const MemoryProfiler::AllocationSite* site; // Site of where the allocation was done
#endif
};
void MemoryProfiler::StaticInitialize()
{
// This delayed initialization is necessary to avoid recursion, when TLS variables that are used
// by the profiler, are themselves managed by the memory manager.
MemoryProfiler* temp = new (MemoryManager::LowLevelAllocate(sizeof(MemoryProfiler))) MemoryProfiler();
temp->AllocateStructs();
s_MemoryProfiler = temp;
}
void MemoryProfiler::StaticDestroy()
{
s_MemoryProfiler->~MemoryProfiler();
MemoryManager::LowLevelFree(s_MemoryProfiler);
s_MemoryProfiler = NULL;
}
#include "Runtime/Allocator/BaseAllocator.h"
#include "Runtime/Utilities/Word.h"
#include "Runtime/Utilities/Stacktrace.h"
MemoryProfiler::MemoryProfiler()
: m_DefaultRootHeader (NULL)
, m_SizeUsed (0)
, m_NumAllocations (0)
, m_AccSizeUsed (0)
, m_AccNumAllocations (0)
{
memset (m_SizeDistribution, 0, sizeof(m_SizeDistribution));
}
MemoryProfiler::~MemoryProfiler()
{
m_RecordingAllocation = true;
#if RECORD_ALLOCATION_SITES
UNITY_DELETE(m_AllocationSites,kMemMemoryProfiler);
UNITY_DELETE(m_AllocationSizes,kMemMemoryProfiler);
#endif
UNITY_DELETE(m_ReferenceIDSizes,kMemMemoryProfiler);
UNITY_DELETE(m_RootAllocationTypes,kMemMemoryProfiler);
// using allocator directly to avoid allocation registration
BaseAllocator* alloc = GetMemoryManager().GetAllocator(kMemProfiler);
alloc->Deallocate(m_RootStack);
m_RecordingAllocation = false;
}
void MemoryProfiler::SetupAllocationHeader(ProfilerAllocationHeader* header, ProfilerAllocationHeader* root, int size)
{
header->SetRootPtr(root);
header->accumulatedSize = size;
#if MAINTAIN_RELATED_ALLOCATION_LIST
header->next = NULL;
header->prev = NULL;
#endif
#if RECORD_ALLOCATION_SITES
header->site = NULL;
#endif
}
void MemoryProfiler::AllocateStructs()
{
m_RecordingAllocation = true;
m_RootStackSize = 20;
// using allocator directly to avoid allocation registration
BaseAllocator* alloc = GetMemoryManager().GetAllocator(kMemProfiler);
m_RootStack = (ProfilerAllocationHeader**)alloc->Allocate(m_RootStackSize*sizeof(ProfilerAllocationHeader*), kDefaultMemoryAlignment);
m_CurrentRootHeader = &m_RootStack[0];
*m_CurrentRootHeader = NULL;
#if RECORD_ALLOCATION_SITES
m_AllocationSites = UNITY_NEW(AllocationSites, kMemMemoryProfiler)();
m_AllocationSizes = UNITY_NEW(AllocationSizes,kMemMemoryProfiler)();
#endif
m_ReferenceIDSizes = UNITY_NEW(ReferenceID,kMemMemoryProfiler)();
m_RootAllocationTypes = UNITY_NEW(RootAllocationTypes, kMemMemoryProfiler)();
#if ROOT_UNRELATED_ALLOCATIONS
int* defaultRootContainer = UNITY_NEW(int, kMemProfiler);
m_DefaultRootHeader = GetMemoryManager().GetAllocator(kMemProfiler)->GetProfilerHeader(defaultRootContainer);
SetupAllocationHeader(m_DefaultRootHeader, NULL, sizeof(int));
RegisterRootAllocation(defaultRootContainer, GetMemoryManager().GetAllocator(kMemProfiler), "UnrootedAllocations", "");
*m_CurrentRootHeader = m_DefaultRootHeader;
#endif
m_RecordingAllocation = false;
}
void MemoryProfiler::ThreadCleanup()
{
m_RecordingAllocation = true;
if(m_RootStack)
{
// using allocator directly to avoid allocation registration
BaseAllocator* alloc = GetMemoryManager().GetAllocator(kMemProfiler);
alloc->Deallocate(m_RootStack);
}
m_RecordingAllocation = false;
}
void* g_LastAllocations[2];
int g_LastAllocationIndex = 0;
void MemoryProfiler::InitAllocation(void* ptr, BaseAllocator* alloc)
{
Assert(!s_MemoryProfiler);
if (alloc)
{
ProfilerAllocationHeader* const header = alloc->GetProfilerHeader(ptr);
if (header)
{
size_t const size = alloc->GetPtrSize(ptr);
SetupAllocationHeader(header, NULL, size);
}
}
}
void MemoryProfiler::RegisterAllocation(void* ptr, MemLabelRef label, const char* file, int line, size_t allocsize)
{
BaseAllocator* alloc = GetMemoryManager().GetAllocator(label);
size_t size = alloc ? alloc->GetPtrSize(ptr) : allocsize;
#if RECORD_ALLOCATION_SITES
Mutex::AutoLock lock(m_Mutex);
#endif
if (m_RecordingAllocation)
{
//mircea@ due to stupid init order, the gConsolePath std::string is not initialized when the assert triggers.
// we really really really really need to find a good solution to avoid shit like that!
//Assert(label.label == kMemMemoryProfilerId);
ProfilerAllocationHeader* header = alloc ? alloc->GetProfilerHeader(ptr) : NULL;
if(header)
{
SetupAllocationHeader(header, NULL, size);
}
return;
}
m_RecordingAllocation = true;
ProfilerAllocationHeader* root = label.GetRootHeader();
if(label.UseAutoRoot())
{
if(m_CurrentRootHeader != NULL)
root = *m_CurrentRootHeader;
else
root = NULL;
}
#if RECORD_ALLOCATION_SITES
m_SizeUsed += size;
m_NumAllocations++;
m_AccSizeUsed += size;
m_AccNumAllocations++;
m_SizeDistribution[HighestBit(size)]++;
AllocationSite site;
site.label = label.label;
site.file = file;
site.line = line;
site.allocated = 0;
site.alloccount = 0;
site.ownedAllocated = 0;
site.ownedCount = 0;
site.cummulativeAllocated = 0;
site.cummulativeAlloccount = 0;
#if ENABLE_STACKS_ON_ALL_ALLOCS
site.stack[0] = 0;
static volatile bool track = false;
//if(root && track)
{
// if(Thread::CurrentThreadIsMainThread())
{
//static int counter = 0;
//if(((counter++) & 0x100) == 0 || file == NULL)
{
SET_ALLOC_OWNER(NULL);
site.stackHash = GetStacktrace(site.stack, 20, 3);
}
}
}
#endif
AllocationSites::iterator it = m_AllocationSites->find(site); // std::set will allocate on insertion (very rare)
if(it == m_AllocationSites->end())
it = m_AllocationSites->insert(site).first;
AllocationSite* mutablesite = const_cast<AllocationSite*>(&(*it));
mutablesite->allocated += size;
mutablesite->alloccount++;
mutablesite->cummulativeAllocated += size;
mutablesite->cummulativeAlloccount++;
if(root)
{
mutablesite->ownedAllocated += size;
mutablesite->ownedCount++;
}
#endif
ProfilerAllocationHeader* header = alloc ? alloc->GetProfilerHeader(ptr) : NULL;
if(header)
{
SetupAllocationHeader(header, root, size);
#if RECORD_ALLOCATION_SITES
header->site = &(*it);
#endif
if(root != NULL)
{
// Find the root owner and add this to the allocation list and accumulated size
if(root)
{
AtomicAdd(&root->accumulatedSize, size);
#if MAINTAIN_RELATED_ALLOCATION_LIST
InsertAfterRoot(root, header);
#endif
}
}
}
else if(alloc) // no alloc present for stray mallocs
{
#if RECORD_ALLOCATION_SITES
LocalHeaderInfo info = {size, &(*it)};
m_AllocationSizes->insert(std::make_pair(ptr, info)); // Will allocate
#endif
}
g_LastAllocations[g_LastAllocationIndex]= ptr;
g_LastAllocationIndex ^= 1;
m_RecordingAllocation = false;
}
void MemoryProfiler::UnregisterAllocation(void* ptr, BaseAllocator* alloc, size_t freesize, ProfilerAllocationHeader** outputRootHeader, MemLabelRef label)
{
if(ptr == NULL)
return;
#if RECORD_ALLOCATION_SITES
Mutex::AutoLock lock(m_Mutex);
#endif
if (m_RecordingAllocation)
return;
size_t size = alloc ? alloc->GetPtrSize(ptr) : freesize;
ProfilerAllocationHeader* header = alloc ? alloc->GetProfilerHeader(ptr) : NULL;
#if RECORD_ALLOCATION_SITES
if(header && header->site == NULL)
return;
#endif
ProfilerAllocationHeader* root = NULL;
m_RecordingAllocation = true;
// Assert(!header || header->IsRoot() || header->GetRootPtr() == label.GetRootHeader());
#if RECORD_ALLOCATION_SITES
const AllocationSite* site = NULL;
if(!alloc)
{
#if ENABLE_STACKS_ON_ALL_ALLOCS
AllocationSite tmpsite = {kMemLabelCount, {0}, 0, NULL, 0, 0, 0, 0, 0, 0, 0};
#else
AllocationSite tmpsite = {kMemLabelCount, NULL, 0, 0, 0, 0, 0, 0, 0};
#endif
AllocationSites::iterator it = m_AllocationSites->insert(tmpsite).first;
site = &(*it);
}
else if ( !header )
{
AllocationSizes::iterator itPtrSize = m_AllocationSizes->find(ptr);
#if UNITY_IPHONE
// oh hello osam apple. When we override global new - we override it for everything.
// BUT some libraries calls delete on pointers that were new-ed with system operator new
// effectively crashing here. So play safe, yes
if( itPtrSize == m_AllocationSizes->end() )
{
m_RecordingAllocation = false;
return kMemNewDeleteId;
}
#endif
Assert(itPtrSize != m_AllocationSizes->end());
size = (*itPtrSize).second.size;
site = (*itPtrSize).second.site;
m_AllocationSizes->erase(itPtrSize);
}
#endif
if(header)
{
root = header->GetRootPtr();
if (header->GetRootPtr() || header->IsRoot())
{
if(header->IsRoot())
root = header;
int memoryleft = AtomicAdd(&root->accumulatedSize, -(int)size);
if (root == header)
{
// This is the root object. Check that all linked allocations are deleted. If not, unlink all.
if (memoryleft != 0)
{
#if MAINTAIN_RELATED_ALLOCATION_LIST
Mutex::AutoLock lock(m_Mutex);
UnlinkAllAllocations(root);
#else
ErrorString("Not all allocations related to a root has been deleted - might cause unity to crash later on!!");
#endif
}
root->rootReference->root = NULL;
root->rootReference->Release();
root->rootReference = NULL;
}
#if MAINTAIN_RELATED_ALLOCATION_LIST
UnlinkHeader(header);
#endif
}
else
AtomicAdd(&header->accumulatedSize, -(int)size);
#if RECORD_ALLOCATION_SITES
site = header->site;
#endif
}
#if RECORD_ALLOCATION_SITES
AllocationSite* mutablesite = const_cast<AllocationSite*>(site);
mutablesite->allocated -= size;
mutablesite->alloccount--;
if(root)
{
mutablesite->ownedAllocated -= size;
mutablesite->ownedCount--;
}
m_SizeUsed -= size;
m_NumAllocations--;
m_SizeDistribution[HighestBit(size)]--;
#endif
// Roots are registered with their related data pointing to themselves.
if(header && header->IsRegisteredRoot() )
UnregisterRootAllocation (ptr);
if (outputRootHeader != NULL)
*outputRootHeader = root;
m_RecordingAllocation = false;
return;
}
void MemoryProfiler::TransferOwnership(void* ptr, BaseAllocator* alloc, ProfilerAllocationHeader* newRootHeader)
{
Assert(alloc);
size_t size = alloc->GetPtrSize(ptr);
ProfilerAllocationHeader* header = alloc->GetProfilerHeader(ptr);
if(header)
{
ProfilerAllocationHeader* root = header->GetRootPtr();
if(root != NULL)
{
#if MAINTAIN_RELATED_ALLOCATION_LIST
// Unlink from currentRoot
UnlinkHeader(header);
#endif
AtomicAdd(&root->accumulatedSize, -(int)size);
header->SetRootPtr(NULL);
#if RECORD_ALLOCATION_SITES
AllocationSite* mutablesite = const_cast<AllocationSite*>(header->site);
mutablesite->ownedAllocated -= size;
mutablesite->ownedCount--;
#endif
}
if(newRootHeader == NULL)
newRootHeader = m_DefaultRootHeader;
// we have a new root. Relink
if(newRootHeader)
{
DebugAssert(newRootHeader->IsRoot());
AtomicAdd(&newRootHeader->accumulatedSize, size);
#if MAINTAIN_RELATED_ALLOCATION_LIST
InsertAfterRoot(newRootHeader, header);
#endif
header->SetRootPtr(newRootHeader);
#if RECORD_ALLOCATION_SITES
AllocationSite* mutablesite = const_cast<AllocationSite*>(header->site);
mutablesite->ownedAllocated += size;
mutablesite->ownedCount++;
#endif
}
}
}
#if MAINTAIN_RELATED_ALLOCATION_LIST
void MemoryProfiler::UnlinkAllAllocations(ProfilerAllocationHeader* root)
{
#if ENABLE_STACKS_ON_ALL_ALLOCS
// Print stack for root and stacks for all unallocated child allocations
{
char buffer[4048];
void*const* s = root->site->stack;
GetReadableStackTrace(buffer, 4048, (void**)(s), 20);
printf_console(FormatString<TEMP_STRING>("Root still has %d allocated memory\nRoot allocated from\n%s\n",root->accumulatedSize, buffer).c_str());
ProfilerAllocationHeader* header = root->next;
while(header)
{
// for each allocation: print stack and decrease sites ownedcount
char buffer[4048];
void*const* s = header->site->stack;
GetReadableStackTrace(buffer, 4048, (void**)(s), 20);
printf_console(FormatString<TEMP_STRING>("%s\n", buffer).c_str());
AllocationSite* mutablesite = const_cast<AllocationSite*>(header->site);
mutablesite->ownedAllocated -= header->accumulatedSize;
mutablesite->ownedCount--;
header = header->next;
}
}
#else
// ErrorString("Not all allocations related to a root has been deleted - might cause unity to crash later on!!");
#endif
// unlink all allocations
ProfilerAllocationHeader* header = root->next;
while(header)
{
ProfilerAllocationHeader* nextHeader = header->next;
UnlinkHeader(header);
#if ROOT_UNRELATED_ALLOCATIONS
header->SetRootPtr(m_DefaultRootHeader); // set root owner
InsertAfterRoot(m_DefaultRootHeader, header);
#else
header->SetRootPtr(NULL); // clear root owner
#endif
header = nextHeader;
}
root->next = NULL;
}
void MemoryProfiler::InsertAfterRoot( ProfilerAllocationHeader* root, ProfilerAllocationHeader* header )
{
DebugAssert(root->IsRoot());
Mutex::AutoLock lock(m_Mutex);
if(root->next)
root->next->prev = header;
header->next = root->next;
header->prev = root;
root->next = header;
}
void MemoryProfiler::UnlinkHeader(ProfilerAllocationHeader* header )
{
Mutex::AutoLock lock(m_Mutex);
DebugAssert(header->prev == NULL || header->prev->next == header);
DebugAssert(header->next == NULL || header->next->prev == header);
if(header->prev)
header->prev->next = header->next;
if(header->next)
header->next->prev = header->prev;
header->prev = NULL;
header->next = NULL;
}
AllocationRootReference* MemoryProfiler::GetRootReferenceFromHeader(ProfilerAllocationHeader* root)
{
if (root == NULL || root->rootReference == NULL || GetMemoryProfiler()->IsRecording())
return NULL;
Assert(root->IsRoot());
root->rootReference->Retain();
return root->rootReference;
}
#endif
void MemoryProfiler::ValidateRoot(ProfilerAllocationHeader* root)
{
if(!root)
return;
size_t accSize = 0;
Assert(root->IsRoot());
Assert(root->next == NULL || root->next->prev == root);
Assert(root->rootReference != NULL && root->rootReference->root != root);
ProfilerAllocationHeader* header = root->next;
while(header)
{
Assert (header->next == NULL || header->next->prev == header);
Assert (header->relatesTo == root);
accSize += header->accumulatedSize;
header = header->next;
}
Assert(root->accumulatedSize >= accSize);
}
bool MemoryProfiler::PushAllocationRoot(void* root, bool forcePush)
{
ProfilerAllocationHeader** current_root_header = m_CurrentRootHeader;
if(current_root_header == NULL)
{
if (root == NULL)
return false;
m_RecordingAllocation = true;
m_RootStackSize = 10;
// using allocator directly to avoid allocation registration
BaseAllocator* alloc = GetMemoryManager().GetAllocator(kMemProfiler);
m_RootStack = (ProfilerAllocationHeader**)alloc->Allocate(m_RootStackSize*sizeof(ProfilerAllocationHeader*), kDefaultMemoryAlignment);
m_CurrentRootHeader = &m_RootStack[0];
*m_CurrentRootHeader = 0;
current_root_header = m_CurrentRootHeader;
m_RecordingAllocation = false;
}
ProfilerAllocationHeader* rootHeader = NULL;
if(root != NULL)
{
BaseAllocator* rootAlloc = GetMemoryManager().GetAllocatorContainingPtr(root);
Assert(rootAlloc);
rootHeader = (ProfilerAllocationHeader*)rootAlloc->GetProfilerHeader(root);
Assert(rootHeader == NULL || rootHeader->IsRoot());
}
if(!forcePush && rootHeader == *current_root_header)
return false;
UInt32 offset = m_CurrentRootHeader-m_RootStack;
if(offset == m_RootStackSize-1)
{
m_RecordingAllocation = true;
m_RootStackSize = m_RootStackSize*2;
// using allocator directly to avoid allocation registration
BaseAllocator* alloc = GetMemoryManager().GetAllocator(kMemProfiler);
m_RootStack = (ProfilerAllocationHeader**)alloc->Reallocate(&m_RootStack[0],m_RootStackSize*sizeof(ProfilerAllocationHeader*), kDefaultMemoryAlignment);
m_CurrentRootHeader = &m_RootStack[offset];
current_root_header = m_CurrentRootHeader;
m_RecordingAllocation = false;
}
*(++current_root_header) = rootHeader;
m_CurrentRootHeader = current_root_header;
return true;
}
void MemoryProfiler::PopAllocationRoot()
{
--m_CurrentRootHeader;
}
ProfilerAllocationHeader* MemoryProfiler::GetCurrentRootHeader()
{
ProfilerAllocationHeader* root = m_CurrentRootHeader ? *m_CurrentRootHeader : NULL;
return root;
}
int MemoryProfiler::GetHeaderSize()
{
return sizeof(ProfilerAllocationHeader);
}
size_t MemoryProfiler::GetRelatedMemorySize(const void* ptr){
// this could use a linked list of related allocations, and thereby this info could be viewed as a hierarchy
BaseAllocator* alloc = GetMemoryManager().GetAllocatorContainingPtr(ptr);
ProfilerAllocationHeader* header = alloc ? alloc->GetProfilerHeader(ptr) : NULL;
return header ? header->accumulatedSize : 0;
}
void MemoryProfiler::RegisterMemoryToID( size_t id, size_t size )
{
Mutex::AutoLock lock(m_Mutex);
m_RecordingAllocation = true;
ReferenceID::iterator itIDSize = m_ReferenceIDSizes->find(id);
if(itIDSize == m_ReferenceIDSizes->end())
m_ReferenceIDSizes->insert(std::make_pair(id,size));
else
itIDSize->second += size;
m_RecordingAllocation = false;
}
void MemoryProfiler::UnregisterMemoryToID( size_t id, size_t size )
{
Mutex::AutoLock lock(m_Mutex);
m_RecordingAllocation = true;
ReferenceID::iterator itIDSize = m_ReferenceIDSizes->find(id);
if (itIDSize != m_ReferenceIDSizes->end())
{
itIDSize->second -= size;
if(itIDSize->second == 0)
m_ReferenceIDSizes->erase(itIDSize);
}
else
ErrorString("Id not found in map");
m_RecordingAllocation = false;
}
size_t MemoryProfiler::GetRelatedIDMemorySize(size_t id)
{
Mutex::AutoLock lock(m_Mutex);
ReferenceID::iterator itIDSize = m_ReferenceIDSizes->find(id);
if(itIDSize == m_ReferenceIDSizes->end())
return 0;
return itIDSize->second;
}
ProfilerString MemoryProfiler::GetOverview() const
{
#if RECORD_ALLOCATION_SITES
struct MemCatInfo
{
UInt64 totalMem;
UInt64 totalCount;
UInt64 cummulativeMem;
UInt64 cummulativeCount;
};
MemCatInfo info[kMemLabelCount+1];
MemCatInfo allocatorMemUsage[16];
MemCatInfo totalMemUsage;
BaseAllocator* allocators[16];
memset (&info,0,sizeof(info));
memset (&allocatorMemUsage,0,sizeof(allocatorMemUsage));
memset (&totalMemUsage,0,sizeof(totalMemUsage));
memset (&allocators,0,sizeof(allocators));
AllocationSites::iterator it = m_AllocationSites->begin();
for( ;it != m_AllocationSites->end(); ++it)
{
MemLabelIdentifier label = (*it).label;
if (label >= kMemLabelCount)
label = kMemLabelCount;
info[label].totalMem += (*it).allocated;
info[label].totalCount += (*it).alloccount;
info[label].cummulativeMem += (*it).cummulativeAllocated;
info[label].cummulativeCount += (*it).cummulativeAlloccount;
MemLabelId memLabel(label, NULL);
BaseAllocator* alloc = GetMemoryManager().GetAllocator(memLabel);
int allocatorindex = GetMemoryManager().GetAllocatorIndex(alloc);
allocatorMemUsage[allocatorindex].totalMem += (*it).allocated;
allocatorMemUsage[allocatorindex].totalCount += (*it).alloccount;
allocatorMemUsage[allocatorindex].cummulativeMem += (*it).cummulativeAllocated;
allocatorMemUsage[allocatorindex].cummulativeCount += (*it).cummulativeAlloccount;
allocators[allocatorindex] = alloc;
totalMemUsage.totalMem += (*it).allocated;
totalMemUsage.totalCount += (*it).alloccount;
totalMemUsage.cummulativeMem += (*it).cummulativeAllocated;
totalMemUsage.cummulativeCount += (*it).cummulativeAlloccount;
}
const int kStringSize = 400*1024;
TEMP_STRING str;
str.reserve(kStringSize);
// Total memory registered in all allocators
str += FormatString<TEMP_STRING>("[ Total Memory ] : %0.2fMB ( %d ) [%0.2fMB ( %d )]\n\n", (float)(totalMemUsage.totalMem)/(1024.f*1024.f), totalMemUsage.totalCount,
(float)(totalMemUsage.cummulativeMem)/(1024.f*1024.f), totalMemUsage.cummulativeCount);
// Memory registered by allocators
for(int i = 0; i < 16; i++)
{
if (allocatorMemUsage[i].cummulativeCount == 0)
continue;
BaseAllocator* alloc = allocators[i];
str += FormatString<TEMP_STRING>("[ %s ] : %0.2fKB ( %d ) [acc: %0.2fMB ( %d )] (Requested:%0.2fKB, Overhead:%0.2fKB, Reserved:%0.2fMB)\n", (alloc?alloc->GetName():"Custom"), (float)(allocatorMemUsage[i].totalMem)/1024.f, allocatorMemUsage[i].totalCount,
(float)(allocatorMemUsage[i].cummulativeMem)/(1024.f*1024.f), allocatorMemUsage[i].cummulativeCount,(float) (alloc?alloc->GetAllocatedMemorySize():0)/1024.f, (float)((alloc?alloc->GetAllocatorSizeTotalUsed():0) - (alloc?alloc->GetAllocatedMemorySize():0))/1024.f, (alloc?alloc->GetReservedSizeTotal():0)/(1024.f*1024.f));
}
// Memory registered on labels
for(int i = 0; i <= kMemLabelCount; i++)
{
if (info[i].cummulativeCount == 0)
continue;
MemLabelId label((MemLabelIdentifier)i, NULL);
BaseAllocator* alloc = GetMemoryManager().GetAllocator(label);
str += FormatString<TEMP_STRING>("\n[ %s : %s ]\n", GetMemoryManager().GetMemcatName(label), alloc?alloc->GetName():"Custom");
str += FormatString<TEMP_STRING>(" TotalAllocated : %0.2fKB ( %d ) [%0.2fMB ( %d )]\n", (float)(info[i].totalMem)/1024.f, info[i].totalCount,
(float)(info[i].cummulativeMem)/(1024.f*1024.f), info[i].cummulativeCount);
}
#if ENABLE_STACKS_ON_ALL_ALLOCS
const int kBufferSize = 8*1024;
char buffer[kBufferSize];
UNITY_VECTOR(kMemMemoryProfiler,const AllocationSite*) sortedVector;
sortedVector.reserve(m_AllocationSites->size());
it = m_AllocationSites->begin();
for( ;it != m_AllocationSites->end(); ++it)
sortedVector.push_back(&(*it));
std::sort(sortedVector.begin(), sortedVector.end(), AllocationSite::Sorter());
{
UNITY_VECTOR(kMemMemoryProfiler, const AllocationSite*)::iterator it = sortedVector.begin();
for( ;it != sortedVector.end(); ++it)
{
if(str.length()>kStringSize-kBufferSize)
break;
if( (*it)->alloccount == 0)
break;
str += FormatString<TEMP_STRING>("\n[ %s ]\n", GetMemoryManager().GetMemcatName(MemLabelId((*it)->label, NULL)));
if((*it)->stack[0] != 0)
{
GetReadableStackTrace(buffer, kBufferSize, (void**)((*it)->stack), 20);
str += FormatString<TEMP_STRING>("%s\n", buffer);
}
else
str += FormatString<TEMP_STRING>("[ %s:%d ]\n", (*it)->file, (*it)->line);
//, (*it).file);
str += FormatString<TEMP_STRING>(" TotalAllocated : %0.2fKB ( %d ) [%0.2fMB ( %d )]\n", (float)((*it)->allocated)/1024.f, (*it)->alloccount,
(float)((*it)->cummulativeAllocated)/(1024.f*1024.f), (*it)->cummulativeAlloccount);
}
}
#endif
return ProfilerString(str.c_str());
#else
return ProfilerString();
#endif
}
#if RECORD_ALLOCATION_SITES
struct MonoObjectMemoryStackInfo
{
bool expanded;
bool sorted;
int allocated;
int ownedAllocated;
MonoArray* callerSites;
ScriptingStringPtr name;
};
MonoObject* MemoryProfiler::MemoryStackEntry::Deserialize()
{
MonoClass* klass = GetMonoManager().GetMonoClass ("ObjectMemoryStackInfo", "UnityEditorInternal");
MonoObject* obj = mono_object_new (mono_domain_get(), klass);
MonoObjectMemoryStackInfo& memInfo = ExtractMonoObjectData<MonoObjectMemoryStackInfo> (obj);
memInfo.expanded = false;
memInfo.sorted = false;
memInfo.allocated = totalMemory;
memInfo.ownedAllocated = ownedMemory;
string tempname (name.c_str(),name.size()-2);
memInfo.name = MonoStringNew(tempname);
memInfo.callerSites = mono_array_new(mono_domain_get(), klass, callerSites.size());
std::map<void*,MemoryStackEntry>::iterator it = callerSites.begin();
int index = 0;
while(it != callerSites.end())
{
MonoObject* child = (*it).second.Deserialize();
GetMonoArrayElement<MonoObject*> (memInfo.callerSites,index++) = child;
++it;
}
return obj;
}
MemoryProfiler::MemoryStackEntry* MemoryProfiler::GetStackOverview() const
{
m_RecordingAllocation = true;
MemoryStackEntry* topLevel = UNITY_NEW(MemoryStackEntry,kMemProfiler);
topLevel->name = "Allocated unity memory ";
AllocationSites::iterator it = m_AllocationSites->begin();
for( ;it != m_AllocationSites->end(); ++it)
{
int size = (*it).allocated;
if(size == 0)
continue;
int ownedsize = (*it).ownedAllocated;
void*const* stack = &(*it).stack[0];
MemoryStackEntry* currentLevel = topLevel;
currentLevel->totalMemory += size;
currentLevel->ownedMemory += ownedsize;
while(*stack)
{
currentLevel = &(currentLevel->callerSites[*stack]);
if(currentLevel->totalMemory == 0)
{
char buffer[2048];
void* temp[2];
temp[0] = *stack;
temp[1] = 0;
GetReadableStackTrace(buffer, 2048, (void**)temp, 1);
currentLevel->name = std::string(buffer,40);
}
currentLevel->totalMemory += size;
currentLevel->ownedMemory += ownedsize;
++stack;
}
}
m_RecordingAllocation = false;
return topLevel;
}
void MemoryProfiler::ClearStackOverview(MemoryProfiler::MemoryStackEntry* entry) const
{
m_RecordingAllocation = true;
UNITY_DELETE(entry, kMemProfiler);
m_RecordingAllocation = false;
}
#endif
void MemoryProfiler::RegisterRootAllocation (void* root, BaseAllocator* allocator, const char* areaName, const char* objectName)
{
bool result = true;
if(areaName != NULL)
{
m_Mutex.Lock();
m_RecordingAllocation = true;
RootAllocationType typeData;
typeData.areaName = areaName;
typeData.objectName = objectName;
result = m_RootAllocationTypes->insert(std::make_pair (root, typeData)).second;
m_RecordingAllocation = false;
m_Mutex.Unlock();
}
if (result)
{
ProfilerAllocationHeader* header = allocator->GetProfilerHeader(root);
if(header)
{
if (!header->rootReference)
{
m_RecordingAllocation = true;
header->rootReference = UNITY_NEW(AllocationRootReference, kMemProfiler) (header);
m_RecordingAllocation = false;
}
if(areaName)
header->SetAsRegisteredRoot();
else
header->SetAsRoot();
}
}
else
{
ErrorString("Registered allocation root already exists");
}
}
void MemoryProfiler::UnregisterRootAllocation (void* root)
{
m_Mutex.Lock();
m_RecordingAllocation = true;
bool result = m_RootAllocationTypes->erase(root);
m_RecordingAllocation = false;
m_Mutex.Unlock();
if (!result)
{
ErrorString("Allocation root has already been deleted");
}
}
void MemoryProfiler::SetRootAllocationObjectName (void* root, const char* objectName)
{
Mutex::AutoLock lock (m_Mutex);
m_RecordingAllocation = true;
RootAllocationTypes::iterator it = m_RootAllocationTypes->find(root);
Assert(it != m_RootAllocationTypes->end());
(*it).second.objectName = objectName;
m_RecordingAllocation = false;
}
void MemoryProfiler::GetRootAllocationInfos (RootAllocationInfos& infos)
{
Mutex::AutoLock lock (m_Mutex);
m_RecordingAllocation = true;
int index = infos.size();
infos.resize_uninitialized(infos.size() + m_RootAllocationTypes->size());
for (RootAllocationTypes::const_iterator i=m_RootAllocationTypes->begin();i != m_RootAllocationTypes->end();++i)
{
RootAllocationInfo& info = infos[index++];
info.memorySize = GetRelatedMemorySize(i->first);
info.areaName = i->second.areaName;
info.objectName = i->second.objectName.c_str();
}
m_RecordingAllocation = false;
}
ProfilerAllocationHeader* MemoryProfiler::GetAllocationRootHeader(void* ptr, MemLabelRef label) const
{
BaseAllocator* alloc = GetMemoryManager().GetAllocator(label);
ProfilerAllocationHeader* header = alloc?alloc->GetProfilerHeader(ptr):NULL;
return header?header->GetRootPtr(): NULL;
}
#if RECORD_ALLOCATION_SITES
ProfilerString MemoryProfiler::GetUnrootedAllocationsOverview()
{
ProfilerString result;
#if ROOT_UNRELATED_ALLOCATIONS
m_RecordingAllocation = true;
std::map<const AllocationSite*,size_t> allocations;
ProfilerAllocationHeader* next = m_DefaultRootHeader->next;
while(next)
{
if(!next->IsRoot())
{
Assert(next->relatesTo == m_DefaultRootHeader);
allocations[next->site]+=next->accumulatedSize;
}
next = next->next;
}
std::vector<std::pair<const AllocationSite*,size_t> > sorted;
std::map<const AllocationSite*,size_t>::iterator it = allocations.begin();
for( ;it != allocations.end(); ++it)
{
sorted.push_back(*it);
}
std::sort(sorted.begin(), sorted.end(), AllocationSiteSizeSorter());
for(int i = 0;i < sorted.size(); ++i)
{
int size = sorted[i].second;
printf_console("%db\n", size);
char buffer[2048];
GetReadableStackTrace(buffer, 2048, (void**)sorted[i].first->stack, 10);
printf_console("%s\n", buffer);
if(i > 20)
break;
}
m_RecordingAllocation = false;
#endif
return result;
}
#endif
#endif
|