Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BaseTypes.cs « profiler-decoder-library « Mono.Profiler - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d183447d3e2956ed00a490d4a7362b1c3beadbd7 (plain)
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
// Author:
// Massimiliano Mantione (massi@ximian.com)
//
// (C) 2008 Novell, Inc  http://www.novell.com
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;	
using System.IO;
using System.Collections.Generic;

namespace Mono.Profiler {
	public interface ILoadedElement {
		uint ID {get;}
		string Name {get;}
	}
	public interface ILoadedClass : ILoadedElement {
		uint Size {get;}
	}
	public interface ILoadedMethod<LC> : ILoadedElement where LC : ILoadedClass {
		LC Class {get;}
	}
	public interface IUnmanagedFunctionFromID<MR,UFR> : ILoadedElement where UFR : IUnmanagedFunctionFromRegion where MR : IExecutableMemoryRegion<UFR> {
		MR Region {get;}
	}
	public interface IUnmanagedFunctionFromRegion {
		string Name {get; set;}
		uint StartOffset {get; set;}
		uint EndOffset {get; set;}
	}
	public interface IExecutableMemoryRegion<UFR> : ILoadedElement where UFR : IUnmanagedFunctionFromRegion {
		ulong StartAddress {get;}
		ulong EndAddress {get;}
		uint FileOffset {get;}
		UFR NewFunction (string name, uint offset);
		UFR GetFunction (uint offset);
		UFR[] Functions {get;}
		void SortFunctions ();
	}
	public interface IHeapObject<HO,LC> where HO: IHeapObject<HO,LC> where LC : ILoadedClass {
		ulong ID {get;}
		LC Class {get;}
		uint Size {get;}
		HO[] References {get;}
		HO[] BackReferences {get;}
	}
	public interface IHeapSnapshot<HO,LC> where HO: IHeapObject<HO,LC> where LC : ILoadedClass {
		uint Collection {get;}
		ulong StartCounter {get;}
		DateTime StartTime {get;}
		ulong EndCounter {get;}
		DateTime EndTime {get;}
		HO NewHeapObject (ulong id, LC c, uint size, ulong[] referenceIds, int referencesCount);
		HO GetHeapObject (ulong id);
		HO[] HeapObjects {get;}
		bool RecordSnapshot {get;}
	}
	
	public interface ILoadedElementFactory<LC,LM,UFR,UFI,MR,HO,HS> where LC : ILoadedClass where LM : ILoadedMethod<LC> where UFR : IUnmanagedFunctionFromRegion where UFI : IUnmanagedFunctionFromID<MR,UFR> where MR : IExecutableMemoryRegion<UFR> where HO: IHeapObject<HO,LC> where HS: IHeapSnapshot<HO,LC> {
		LC NewClass (uint id, string name, uint size);
		LM NewMethod (uint id, LC c, string name);
		MR NewExecutableMemoryRegion (uint id, string fileName, uint fileOffset, ulong startAddress, ulong endAddress);
		UFI NewUnmanagedFunction (uint id, string name, MR region);
		HS NewHeapSnapshot (uint collection, ulong startCounter, DateTime startTime, ulong endCounter, DateTime endTime, LC[] initialAllocations, bool recordSnapshot);
		bool RecordHeapSnapshots {get; set;}
	}
	
	public interface ILoadedElementHandler<LC,LM,UFR,UFI,MR,HO,HS> : ILoadedElementFactory<LC,LM,UFR,UFI,MR,HO,HS> where LC : ILoadedClass where LM : ILoadedMethod<LC> where UFR : IUnmanagedFunctionFromRegion where UFI : IUnmanagedFunctionFromID<MR,UFR> where MR : IExecutableMemoryRegion<UFR> where HO: IHeapObject<HO,LC> where HS: IHeapSnapshot<HO,LC> {
		LC[] Classes {get;}
		LC GetClass (uint id);
		LM[] Methods {get;}
		LM GetMethod (uint id);
		MR[] ExecutableMemoryRegions {get;}
		MR GetExecutableMemoryRegion (uint id);
		MR GetExecutableMemoryRegion (ulong address);
		void InvalidateExecutableMemoryRegion (uint id);
		void SortExecutableMemoryRegions ();
		UFI[] UnmanagedFunctionsByID {get;}
		UFI GetUnmanagedFunctionByID (uint id);
		HS[] HeapSnapshots {get;}
	}
	public interface IProfilerEventHandler<LC,LM,UFR,UFI,MR,EH,HO,HS> where LC : ILoadedClass where LM : ILoadedMethod<LC> where UFR : IUnmanagedFunctionFromRegion where UFI : IUnmanagedFunctionFromID<MR,UFR> where MR : IExecutableMemoryRegion<UFR> where EH : ILoadedElementHandler<LC,LM,UFR,UFI,MR,HO,HS> where HO: IHeapObject<HO,LC> where HS: IHeapSnapshot<HO,LC> {
		DirectivesHandler Directives {get;}
		
		EH LoadedElements {get;}
		
		void Start (uint version, string runtimeFile, ProfilerFlags flags, ulong startCounter, DateTime startTime);
		void End (uint version, ulong endCounter, DateTime endTime);
		
		void StartBlock (ulong startCounter, DateTime startTime, ulong threadId);
		void EndBlock (ulong endCounter, DateTime endTime, ulong threadId);
		
		void ModuleLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success);
		void ModuleUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name);
		void AssemblyLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success);
		void AssemblyUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name);
		void ApplicationDomainLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success);
		void ApplicationDomainUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name);
		
		void SetCurrentThread (ulong threadId);
		
		void ClassStartLoad (LC c, ulong counter);
		void ClassEndLoad (LC c, ulong counter, bool success);
		void ClassStartUnload (LC c, ulong counter);
		void ClassEndUnload (LC c, ulong counter);
		
		void Allocation (LC c, uint size, LM caller, ulong counter);
		void Exception (LC c, ulong counter);
		
		void MethodEnter (LM m, ulong counter);
		void MethodExit (LM m, ulong counter);
		void MethodJitStart (LM m, ulong counter);
		void MethodJitEnd (LM m, ulong counter, bool success);
		void MethodFreed (LM m, ulong counter);
		
		void MethodStatisticalHit (LM m);
		void UnknownMethodStatisticalHit ();
		void UnmanagedFunctionStatisticalHit (UFR f);
		void UnmanagedFunctionStatisticalHit (UFI f);
		void UnknownUnmanagedFunctionStatisticalHit (MR region, uint offset);
		void UnknownUnmanagedFunctionStatisticalHit (ulong address);
		void StatisticalCallChainStart (uint chainDepth);
		
		void ThreadStart (ulong threadId, ulong counter);
		void ThreadEnd (ulong threadId, ulong counter);
		
		void GarbageCollectionStart (uint collection, uint generation, ulong counter);
		void GarbageCollectionEnd (uint collection, uint generation, ulong counter);
		void GarbageCollectionMarkStart (uint collection, uint generation, ulong counter);
		void GarbageCollectionMarkEnd (uint collection, uint generation, ulong counter);
		void GarbageCollectionSweepStart (uint collection, uint generation, ulong counter);
		void GarbageCollectionSweepEnd (uint collection, uint generation, ulong counter);
		void GarbageCollectionResize (uint collection, ulong newSize);
		void GarbageCollectionStopWorldStart (uint collection, uint generation, ulong counter);
		void GarbageCollectionStopWorldEnd (uint collection, uint generation, ulong counter);
		void GarbageCollectionStartWorldStart (uint collection, uint generation, ulong counter);
		void GarbageCollectionStartWorldEnd (uint collection, uint generation, ulong counter);
		
		void HeapReportStart (HS snapshot);
		void HeapObjectUnreachable (LC c, uint size);
		void HeapObjectReachable (HO o);
		void HeapReportEnd (HS snapshot);
		
		void AllocationSummaryStart (uint collection, ulong startCounter, DateTime startTime);
		void ClassAllocationSummary (LC c, uint reachableInstances, uint reachableBytes, uint unreachableInstances, uint unreachableBytes);
		void AllocationSummaryEnd (uint collection, ulong endCounter, DateTime endTime);
	}
	
	public class BaseLoadedElement : ILoadedElement {
		uint id;
		public uint ID {
			get {
				return id;
			}
		}
		
		string name;
		public string Name {
			get {
				return name;
			}
		}
		
		public BaseLoadedElement (uint id, string name) {
			this.id = id;
			this.name = name;
		}
	}
	public class BaseLoadedClass : BaseLoadedElement, ILoadedClass {
		uint size;
		public uint Size {
			get {
				return size;
			}
		}
		
		public BaseLoadedClass (uint id, string name, uint size) : base (id, name) {
			this.size = size;
		}
	}
	public class BaseLoadedMethod<LC> : BaseLoadedElement, ILoadedMethod<LC> where LC : ILoadedClass {
		LC c;
		public LC Class {
			get {
				return c;
			}
		}
		
		public BaseLoadedMethod (uint id, LC c, string name) : base (id, name) {
			this.c = c;
		}
	}
	public class BaseUnmanagedFunctionFromRegion : IUnmanagedFunctionFromRegion {
		IExecutableMemoryRegion<IUnmanagedFunctionFromRegion> region;
		public IExecutableMemoryRegion<IUnmanagedFunctionFromRegion> Region {
			get {
				return region;
			}
			set {
				region = value;
			}
		}
		string name;
		public string Name {
			get {
				return name;
			}
			set {
				name = value;
			}
		}
		uint startOffset;
		public uint StartOffset {
			get {
				return startOffset;
			}
			set {
				startOffset = value;
			}
		}
		uint endOffset;
		public uint EndOffset {
			get {
				return endOffset;
			}
			set {
				endOffset = value;
			}
		}
		public BaseUnmanagedFunctionFromRegion () {
			this.region = null;
			this.name = null;
			this.startOffset = 0;
			this.endOffset = 0;
		}
		public BaseUnmanagedFunctionFromRegion (IExecutableMemoryRegion<IUnmanagedFunctionFromRegion> region, string name, uint startOffset, uint endOffset) {
			this.region = region;
			this.name = name;
			this.startOffset = startOffset;
			this.endOffset = endOffset;
		}
	}
	public class BaseUnmanagedFunctionFromID<MR,UFR>: BaseLoadedElement, IUnmanagedFunctionFromID<MR,UFR>  where UFR : IUnmanagedFunctionFromRegion where MR : IExecutableMemoryRegion<UFR> {
		MR region;
		public MR Region {
			get {
				return region;
			}
		}
		
		public BaseUnmanagedFunctionFromID (uint id, string name, MR region) : base (id, name) {
			this.region = region;
		}
	}
	
	public class HeapObject<LC> : IHeapObject<HeapObject<LC>,LC> where LC : ILoadedClass {
		ulong id;
		public ulong ID {
			get {
				return id;
			}
		}
		LC c;
		public LC Class {
			get {
				return c;
			}
			internal set {
				c = value;
			}
		}
		
		uint size;
		public uint Size {
			get {
				return size;
			}
			internal set {
				size = value;
			}
		}
		
		static HeapObject<LC>[] emptyReferences = new HeapObject<LC> [0];
		public static HeapObject<LC>[] EmptyReferences {
			get {
				return emptyReferences;
			}
		}
		
		HeapObject<LC>[] references;
		public HeapObject<LC>[] References {
			get {
				return references;
			}
			internal set {
				references = value;
			}
		}
		
		HeapObject<LC>[] backReferences;
		public HeapObject<LC>[] BackReferences {
			get {
				return backReferences;
			}
			internal set {
				backReferences = value;
			}
		}
		
		int backReferencesCounter;
		internal void IncrementBackReferences () {
			backReferencesCounter ++;
		}
		internal void AllocateBackReferences () {
			if (references != null) {
				int referencesCount = 0;
				foreach (HeapObject<LC> reference in references) {
					if (reference != null) {
						referencesCount ++;
					}
				}
				if (referencesCount != references.Length) {
					if (referencesCount > 0) {
						HeapObject<LC>[] newReferences = new HeapObject<LC> [referencesCount];
						referencesCount = 0;
						foreach (HeapObject<LC> reference in references) {
							if (reference != null) {
								newReferences [referencesCount] = reference;
								referencesCount ++;
							}
						}
						references = newReferences;
					} else {
						references = emptyReferences;
					} 
				}
			} else {
				references = emptyReferences;
			}
			
			if (backReferencesCounter > 0) {
				backReferences = new HeapObject<LC> [backReferencesCounter];
				backReferencesCounter = 0;
			} else {
				references = emptyReferences;
			}
		}
		internal void AddBackReference (HeapObject<LC> heapObject) {
			backReferences [backReferencesCounter] = heapObject;
			backReferencesCounter ++;
		}
		
		public HeapObject (ulong id, LC c, uint size, HeapObject<LC>[] references) {
			this.id = id;
			this.c = c;
			this.size = size;
			this.references = references;
			this.backReferences = null;
			this.backReferencesCounter = 0;
		}
		public HeapObject (ulong id) {
			this.id = id;
			this.c = default(LC);
			this.size = 0;
			this.references = null;
			this.backReferences = null;
			this.backReferencesCounter = 0;
		}
	}
	
	public abstract class BaseHeapSnapshot<HO,LC> : IHeapSnapshot<HeapObject<LC>,LC> where LC : ILoadedClass {
		Dictionary<ulong,HeapObject<LC>> heap;
		bool backReferencesInitialized;
		
		uint collection;
		public uint Collection {
			get {
				return collection;
			}
		}
		
		ulong startCounter;
		public ulong StartCounter {
			get {
				return startCounter;
			}
		}
		DateTime startTime;
		public DateTime StartTime {
			get {
				return startTime;
			}
		}
		ulong endCounter;
		public ulong EndCounter {
			get {
				return endCounter;
			}
		}
		DateTime endTime;
		public DateTime EndTime {
			get {
				return endTime;
			}
		}
		
		public HeapObject<LC> NewHeapObject (ulong id, LC c, uint size, ulong[] referenceIds, int referencesCount) {
			if (backReferencesInitialized) {
				throw new Exception ("Cannot create heap objects after backReferencesInitialized is true");
			}
			
			if (recordSnapshot) {
				HeapObject<LC>[] references = new HeapObject<LC>[referencesCount];
				HeapObject<LC> result = GetOrCreateHeapObject (id);
				for (int i = 0; i < references.Length; i++) {
					references [i] = GetOrCreateHeapObject (referenceIds [i]);
					references [i].IncrementBackReferences ();
				}
				result.References = references;
				result.Size = size;
				result.Class = c;
				return result;
			} else {
				return null;
			}
		}
		
		public void InitializeBackReferences () {
			if (backReferencesInitialized) {
				throw new Exception ("Cannot call InitializeBackReferences twice");
			}
			
			//FIXME: Bad objects should not happen anymore...
			Dictionary<ulong,HeapObject<LC>> badObjects = new Dictionary<ulong,HeapObject<LC>> ();
			
			foreach (HeapObject<LC> heapObject in heap.Values) {
				if (heapObject.Class != null) {
					heapObject.AllocateBackReferences ();
				} else {
					badObjects.Add (heapObject.ID, heapObject);
				}
			}
			
			foreach (ulong id in badObjects.Keys) {
				heap.Remove (id);
			}
			
			foreach (HeapObject<LC> heapObject in heap.Values) {
				foreach (HeapObject<LC> reference in heapObject.References) {
					reference.AddBackReference (heapObject);
				}
			}
			
			backReferencesInitialized = true;
		}
		
		HeapObject<LC> GetOrCreateHeapObject (ulong id) {
			if (recordSnapshot) {
				if (heap.ContainsKey (id)) {
					return heap [id];
				} else {
					HeapObject<LC> result = new HeapObject<LC> (id);
					heap [id] = result;
					return result;
				}
			} else {
				return null;
			}
		}
		
		public HeapObject<LC> GetHeapObject (ulong id) {
			return heap [id];
		}
		
		public HeapObject<LC>[] HeapObjects {
			get {
				HeapObject<LC>[] result = new HeapObject<LC> [heap.Values.Count];
				heap.Values.CopyTo (result, 0);
				return result;
			}
		}
		
		bool recordSnapshot;
		public bool RecordSnapshot {
			get {
				return recordSnapshot;
			}
		}
		
		public BaseHeapSnapshot (uint collection, ulong startCounter, DateTime startTime, ulong endCounter, DateTime endTime, bool recordSnapshot) {
			this.collection = collection;
			this.startCounter = startCounter;
			this.startTime = startTime;
			this.endCounter = endCounter;
			this.endTime = endTime;
			this.recordSnapshot = recordSnapshot;
			heap = new Dictionary<ulong,HeapObject<LC>> ();
			backReferencesInitialized = false;
		}
	}
	
	public struct AllocationClassData<LC> where LC : ILoadedClass  {
		LC c;
		public LC Class {
			get {
				return c;
			}
		}
		uint reachableInstances;
		public uint ReachableInstances {
			get {
				return reachableInstances;
			}
		}
		uint reachableBytes;
		public uint ReachableBytes {
			get {
				return reachableBytes;
			}
		}
		uint unreachableInstances;
		public uint UnreachableInstances {
			get {
				return unreachableInstances;
			}
		}
		uint unreachableBytes;
		public uint UnreachableBytes {
			get {
				return unreachableBytes;
			}
		}
		
		public static Comparison<AllocationClassData<LC>> CompareByReachableBytes = delegate (AllocationClassData<LC> a, AllocationClassData<LC> b) {
			return a.ReachableBytes.CompareTo (b.ReachableBytes);
		};
		public static Comparison<AllocationClassData<LC>> CompareByReachableInstances = delegate (AllocationClassData<LC> a, AllocationClassData<LC> b) {
			return a.ReachableInstances.CompareTo (b.ReachableInstances);
		};
		
		public AllocationClassData (LC c, uint reachableInstances, uint reachableBytes, uint unreachableInstances, uint unreachableBytes) {
			this.c = c;
			this.reachableInstances = reachableInstances;
			this.reachableBytes = reachableBytes;
			this.unreachableInstances = unreachableInstances;
			this.unreachableBytes = unreachableBytes;
		}
	}
	
	public class BaseAllocationSummary<LC> where LC : ILoadedClass {
		uint collection;
		public uint Collection {
			get {
				return collection;
			}
		}
		List<AllocationClassData<LC>> data;
		public AllocationClassData<LC>[] Data {
			get {
				AllocationClassData<LC>[] result = data.ToArray ();
				Array.Sort (result, AllocationClassData<LC>.CompareByReachableBytes);
				Array.Reverse (result);
				return result;
			}
		}
		
		internal void RecordData (LC c, uint reachableInstances, uint reachableBytes, uint unreachableInstances, uint unreachableBytes) {
			data.Add (new AllocationClassData<LC> (c, reachableInstances, reachableBytes, unreachableInstances, unreachableBytes));
		}
		
		ulong startCounter;
		public ulong StartCounter {
			get {
				return startCounter;
			}
		}
		DateTime startTime;
		public DateTime StartTime {
			get {
				return startTime;
			}
		}
		ulong endCounter;
		public ulong EndCounter {
			get {
				return endCounter;
			}
			internal set {
				endCounter = value;
			}
		}
		DateTime endTime;
		public DateTime EndTime {
			get {
				return endTime;
			}
			internal set {
				endTime = value;
			}
		}
		
		public BaseAllocationSummary (uint collection, ulong startCounter, DateTime startTime) {
			this.collection = collection;
			this.startCounter = startCounter;
			this.startTime = startTime;
			this.endCounter = startCounter;
			this.endTime = startTime;
			data = new List<AllocationClassData<LC>> ();
		}
	}
	
	public enum DirectiveCodes {
		END = 0,
		ALLOCATIONS_CARRY_CALLER = 1,
		LAST
	}
	
	public class DirectivesHandler {
		bool allocationsCarryCallerMethod;
		public bool AllocationsCarryCallerMethod {
			get {
				return allocationsCarryCallerMethod;
			}
		}
		public void AllocationsCarryCallerMethodReceived () {
			allocationsCarryCallerMethod = true;
		}
		
		public DirectivesHandler () {
			allocationsCarryCallerMethod = false;
		}
	}
	
	public class BaseProfilerEventHandler<LC,LM,UFR,UFI,MR,EH,HO,HS> : IProfilerEventHandler<LC,LM,UFR,UFI,MR,EH,HO,HS> where LC : ILoadedClass where LM : ILoadedMethod<LC> where UFR : IUnmanagedFunctionFromRegion where UFI : IUnmanagedFunctionFromID<MR,UFR> where MR : IExecutableMemoryRegion<UFR> where EH : ILoadedElementHandler<LC,LM,UFR,UFI,MR,HO,HS> where HO: IHeapObject<HO,LC> where HS: IHeapSnapshot<HO,LC> {
		DirectivesHandler directives = new DirectivesHandler ();
		public DirectivesHandler Directives {
			get {
				return directives;
			}
		}
		
		EH loadedElements;
		public EH LoadedElements {
			get {
				return loadedElements;
			}
		}
		
		public BaseProfilerEventHandler (EH loadedElements) {
			this.loadedElements = loadedElements;
		}
		
		public virtual void Start (uint version, string runtimeFile, ProfilerFlags flags, ulong startCounter, DateTime startTime) {}
		public virtual void End (uint version, ulong endCounter, DateTime endTime) {}
		
		public virtual void StartBlock (ulong startCounter, DateTime startTime, ulong threadId) {}
		public virtual void EndBlock (ulong endCounter, DateTime endTime, ulong threadId) {}
		
		public virtual void ModuleLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success) {}
		public virtual void ModuleUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name) {}
		public virtual void AssemblyLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success) {}
		public virtual void AssemblyUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name) {}
		public virtual void ApplicationDomainLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success) {}
		public virtual void ApplicationDomainUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name) {}
		
		public virtual void SetCurrentThread (ulong threadId) {}
		
		public virtual void ClassStartLoad (LC c, ulong counter) {}
		public virtual void ClassEndLoad (LC c, ulong counter, bool success) {}
		public virtual void ClassStartUnload (LC c, ulong counter) {}
		public virtual void ClassEndUnload (LC c, ulong counter) {}
		
		public virtual void Allocation (LC c, uint size, LM caller, ulong counter) {}
		public virtual void Exception (LC c, ulong counter) {}
		
		public virtual void MethodEnter (LM m, ulong counter) {}
		public virtual void MethodExit (LM m, ulong counter) {}
		public virtual void MethodJitStart (LM m, ulong counter) {}
		public virtual void MethodJitEnd (LM m, ulong counter, bool success) {}
		public virtual void MethodFreed (LM m, ulong counter) {}
		
		public virtual void MethodStatisticalHit (LM m) {}
		public virtual void UnknownMethodStatisticalHit () {}
		public virtual void UnmanagedFunctionStatisticalHit (UFR f) {}
		public virtual void UnmanagedFunctionStatisticalHit (UFI f) {}
		public virtual void UnknownUnmanagedFunctionStatisticalHit (MR region, uint offset) {}
		public virtual void UnknownUnmanagedFunctionStatisticalHit (ulong address) {}
		public virtual void StatisticalCallChainStart (uint chainDepth) {}
		
		public virtual void ThreadStart (ulong threadId, ulong counter) {}
		public virtual void ThreadEnd (ulong threadId, ulong counter) {}
		
		public virtual void GarbageCollectionStart (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionEnd (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionMarkStart (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionMarkEnd (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionSweepStart (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionSweepEnd (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionResize (uint collection, ulong newSize) {}
		public virtual void GarbageCollectionStopWorldStart (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionStopWorldEnd (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionStartWorldStart (uint collection, uint generation, ulong counter) {}
		public virtual void GarbageCollectionStartWorldEnd (uint collection, uint generation, ulong counter) {}
		
		public virtual void HeapReportStart (HS snapshot) {}
		public virtual void HeapObjectUnreachable (LC c, uint size) {}
		public virtual void HeapObjectReachable (HO o) {}
		public virtual void HeapReportEnd (HS snapshot) {}
		
		public virtual void AllocationSummaryStart (uint collection, ulong startCounter, DateTime startTime) {}
		public virtual void ClassAllocationSummary (LC c, uint reachableInstances, uint reachableBytes, uint unreachableInstances, uint unreachableBytes) {}
		public virtual void AllocationSummaryEnd (uint collection, ulong endCounter, DateTime endTime) {}
	}
	
	public class BaseExecutableMemoryRegion<UFR> : BaseLoadedElement, IExecutableMemoryRegion<UFR> where UFR : IUnmanagedFunctionFromRegion, new() {
		uint fileOffset;
		public uint FileOffset {
			get {
				return fileOffset;
			}
		}
		
		ulong startAddress;
		public ulong StartAddress {
			get {
				return startAddress;
			}
		}
		
		ulong endAddress;
		public ulong EndAddress {
			get {
				return endAddress;
			}
		}
		
		List<UFR> functions;
		
		public UFR NewFunction (string name, uint offset) {
			UFR result = new UFR ();
			result.Name = name;
			result.StartOffset = offset;
			result.EndOffset = offset;
			functions.Add (result);
			return result;
		}
		
		public UFR GetFunction (uint offset) {
			int lowIndex = 0;
			int highIndex = functions.Count;
			int middleIndex = lowIndex + ((highIndex - lowIndex) / 2);
			UFR middleFunction = (middleIndex < functions.Count) ? functions [middleIndex] : default (UFR);
			
			while (lowIndex != highIndex) {
				if (middleFunction.StartOffset > offset) {
					if (middleIndex > 0) {
						highIndex = middleIndex;
					} else {
						return default (UFR);
					}
				} else if (middleFunction.EndOffset < offset) {
					if (middleIndex < functions.Count - 1) {
						lowIndex = middleIndex;
					} else {
						return default (UFR);
					}
				} else {
					return middleFunction;
				}
				
				middleIndex = lowIndex + ((highIndex - lowIndex) / 2);
				middleFunction = functions [middleIndex];
			}
			
			if ((middleFunction == null) || (middleFunction.StartOffset > offset) || (middleFunction.EndOffset < offset)) {
				return default (UFR);
			} else {
				return middleFunction;
			}
		}
		
		public UFR[] Functions {
			get {
				UFR[] result = new UFR [functions.Count];
				functions.CopyTo (result);
				return result;
			}
		}
		
		public static Comparison<UFR> CompareByStartOffset = delegate (UFR a, UFR b) {
			return a.StartOffset.CompareTo (b.StartOffset);
		};
		public void SortFunctions () {
			functions.Sort (CompareByStartOffset);
			if (functions.Count > 0) {
				UFR previousFunction = functions [0];
				for (int i = 1; i < functions.Count; i++) {
					UFR currentFunction = functions [i];
					previousFunction.EndOffset = currentFunction.StartOffset - 1;
					previousFunction = currentFunction;
				}
				previousFunction.EndOffset = (uint) (EndAddress - StartAddress);
			}
		}
		
		public BaseExecutableMemoryRegion (uint id, string name, uint fileOffset, ulong startAddress, ulong endAddress) : base (id, name) {
			this.fileOffset = fileOffset;
			this.startAddress = startAddress;
			this.endAddress = endAddress;
			functions = new List<UFR> ();
			
			NativeLibraryReader.FillFunctions<BaseExecutableMemoryRegion<UFR>,UFR> (this);
		}
	}
	
	public class LoadedElementHandler<LC,LM,UFR,UFI,MR,HO,HS> : ILoadedElementHandler<LC,LM,UFR,UFI,MR,HO,HS> where LC : ILoadedClass where LM : ILoadedMethod<LC> where UFR : IUnmanagedFunctionFromRegion where UFI : IUnmanagedFunctionFromID<MR,UFR> where MR : IExecutableMemoryRegion<UFR> where HO: IHeapObject<HO,LC> where HS: IHeapSnapshot<HO,LC> {
		ILoadedElementFactory<LC,LM,UFR,UFI,MR,HO,HS> factory;
		
		int loadedClassesCount;
		LC[] loadedClasses;
		public LC[] Classes {
			get {
				LC[] result = new LC [loadedClassesCount];
				int resultIndex = 0;
				for (int i = 0; i < loadedClasses.Length; i++) {
					LC c = loadedClasses [i];
					if (c != null) {
						result [resultIndex] = c;
						resultIndex ++;
					}
				}
				return result;
			}
		}
		public LC GetClass (uint id) {
			return loadedClasses [(int) id];
		}
		
		int loadedMethodsCount;
		LM[] loadedMethods;
		public LM[] Methods {
			get {
				LM[] result = new LM [loadedMethodsCount];
				int resultIndex = 0;
				for (int i = 0; i < loadedMethods.Length; i++) {
					LM m = loadedMethods [i];
					if (m != null) {
						result [resultIndex] = m;
						resultIndex ++;
					}
				}
				return result;
			}
		}
		public LM GetMethod (uint id) {
			return loadedMethods [(int) id];
		}
		
		Dictionary<uint,MR> memoryRegions;
		List<MR> sortedMemoryRegions;
		public MR[] ExecutableMemoryRegions {
			get {
				MR[] result = new MR [memoryRegions.Count];
				memoryRegions.Values.CopyTo (result, 0);
				return result;
			}
		}
		public MR GetExecutableMemoryRegion (uint id) {
			return memoryRegions [id];
		}
		public MR GetExecutableMemoryRegion (ulong address) {
			int lowIndex = 0;
			int highIndex = sortedMemoryRegions.Count;
			int middleIndex = lowIndex + ((highIndex - lowIndex) / 2);
			MR middleRegion = (middleIndex < sortedMemoryRegions.Count) ? sortedMemoryRegions [middleIndex] : default (MR);
			
			while (lowIndex != highIndex) {
				if (middleRegion.StartAddress > address) {
					if (middleIndex > 0) {
						highIndex = middleIndex;
					} else {
						return default (MR);
					}
				} else if (middleRegion.EndAddress < address) {
					if (middleIndex < sortedMemoryRegions.Count - 1) {
						lowIndex = middleIndex;
					} else {
						return default (MR);
					}
				} else {
					return middleRegion;
				}
				
				middleIndex = lowIndex + ((highIndex - lowIndex) / 2);
				middleRegion = sortedMemoryRegions [middleIndex];
			}
			
			if ((middleRegion == null) || (middleRegion.StartAddress > address) || (middleRegion.EndAddress < address)) {
				return default (MR);
			} else {
				return middleRegion;
			}
		}
		public void InvalidateExecutableMemoryRegion (uint id) {
			MR region = GetExecutableMemoryRegion (id);
			if (region != null) {
				sortedMemoryRegions.Remove (region);
			}
		}
		static Comparison<MR> CompareByStartAddress = delegate (MR a, MR b) {
			return a.StartAddress.CompareTo (b.StartAddress);
		};
		public void SortExecutableMemoryRegions () {
				sortedMemoryRegions.Sort (CompareByStartAddress);
		}
		
		public LC NewClass (uint id, string name, uint size) {
			LC result = factory.NewClass (id, name, size);
			if (loadedClasses.Length <= id) {
				LC[] newLoadedClasses = new LC [((int) id + 1) * 2];
				loadedClasses.CopyTo (newLoadedClasses, 0);
				loadedClasses = newLoadedClasses;
			}
			loadedClasses [(int) id] = result;
			loadedClassesCount ++;
			return result;
		}
		
		public LM NewMethod (uint id, LC c, string name) {
			LM result = factory.NewMethod (id, c, name);
			if (loadedMethods.Length <= id) {
				LM[] newLoadedMethods = new LM [((int) id + 1) * 2];
				loadedMethods.CopyTo (newLoadedMethods, 0);
				loadedMethods = newLoadedMethods;
			}
			loadedMethods [(int) id] = result;
			loadedMethodsCount ++;
			return result;
		}
		
		public MR NewExecutableMemoryRegion (uint id, string fileName, uint fileOffset, ulong startAddress, ulong endAddress) {
			MR result = factory.NewExecutableMemoryRegion (id, fileName, fileOffset, startAddress, endAddress);
			memoryRegions.Add (id, result);
			sortedMemoryRegions.Add (result);
			return result;
		}
		
		List<HS> heapSnapshots;
		public HS NewHeapSnapshot (uint collection, ulong startCounter, DateTime startTime, ulong endCounter, DateTime endTime, LC[] initialAllocations, bool recordSnapshot) {
			HS result = factory.NewHeapSnapshot (collection, startCounter, startTime, endCounter, endTime, initialAllocations, recordSnapshot);
			heapSnapshots.Add (result);
			return result;
		}
		public HS[] HeapSnapshots {
			get {
				HS[] result = new HS [heapSnapshots.Count];
				heapSnapshots.CopyTo (result);
				return result;
			}
		}
		
		int unmanagedFunctionsByIDCount;
		UFI[] unmanagedFunctionsByID;
		public UFI[] UnmanagedFunctionsByID {
			get {
				UFI[] result = new UFI [unmanagedFunctionsByIDCount];
				int resultIndex = 0;
				for (int i = 0; i < unmanagedFunctionsByID.Length; i++) {
					UFI f = unmanagedFunctionsByID [i];
					if (f != null) {
						result [resultIndex] = f;
						resultIndex ++;
					}
				}
				return result;
			}
		}
		public UFI GetUnmanagedFunctionByID (uint id) {
			return unmanagedFunctionsByID [(int) id];
		}
		public UFI NewUnmanagedFunction (uint id, string name, MR region) {
			UFI result = factory.NewUnmanagedFunction (id, name, region);
			if (unmanagedFunctionsByID.Length <= id) {
				UFI[] newUnmanagedFunctionsByID = new UFI [((int) id + 1) * 2];
				unmanagedFunctionsByID.CopyTo (newUnmanagedFunctionsByID, 0);
				unmanagedFunctionsByID = newUnmanagedFunctionsByID;
			}
			unmanagedFunctionsByID [(int) id] = result;
			unmanagedFunctionsByIDCount ++;
			return result;
		}
		
		public bool RecordHeapSnapshots {
			get {
				return factory.RecordHeapSnapshots;
			}
			set {
				factory.RecordHeapSnapshots = value;
			}
		}
		
		public LoadedElementHandler (ILoadedElementFactory<LC,LM,UFR,UFI,MR,HO,HS> factory) {
			this.factory = factory;
			loadedClasses = new LC [1000];
			loadedClassesCount = 0;
			loadedMethods = new LM [5000];
			loadedMethodsCount = 0;
			memoryRegions = new Dictionary<uint,MR> ();
			sortedMemoryRegions = new List<MR> ();
			heapSnapshots = new List<HS> ();
			unmanagedFunctionsByID = new UFI [1000];
			unmanagedFunctionsByIDCount = 0;
		}
	}
}