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

EventProcessor.cs « profiler-decoder-library « Mono.Profiler - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf7e580ba81506219a88e8ec4abb80dc01fd734e (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
// 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 {
	class UnknownStatisticalHitsCollector : IStatisticalHitItem {
		uint statisticalHits;
		public uint StatisticalHits {
			get {
				return statisticalHits;
			}
			internal set {
				statisticalHits = value;
			}
		}
		internal void IncrementStatisticalHits () {
			statisticalHits ++;
		}
		
		public string Name {
			get {
				return "[UNKNOWN MEMORY REGION]";
			}
		}
		
		StatisticalHitItemCallCounts callCounts;
		public bool HasCallCounts {
			get {
				return (callCounts != null);
			}
		}
		public StatisticalHitItemCallCounts CallCounts {
			get {
				if (callCounts == null) {
					callCounts = new StatisticalHitItemCallCounts ();
				}
				return callCounts;
			}
		}
		
		public UnknownStatisticalHitsCollector () {
			statisticalHits = 0;
		}
	}
	
	public class ProfilerEventHandler : BaseProfilerEventHandler<LoadedClass,LoadedMethod,UnmanagedFunctionFromRegion,UnmanagedFunctionFromID,ExecutableMemoryRegion,LoadedElementHandler<LoadedClass,LoadedMethod,UnmanagedFunctionFromRegion,UnmanagedFunctionFromID,ExecutableMemoryRegion,HeapObject<LoadedClass>,HeapSnapshot>,HeapObject<LoadedClass>,HeapSnapshot> {
		Dictionary<ulong,CallStack> perThreadStacks;
		CallStack stack;
		UnknownStatisticalHitsCollector unknownStatisticalHitsCollector;
		
		uint version;
		public uint Version {
			get {
				return version;
			}
		}
		
		string runtimeFile;
		public string RuntimeFile {
			get {
				return runtimeFile;
			}
		}
		
		ProfilerFlags flags;
		public ProfilerFlags Flags {
			get {
				return flags;
			}
		}
		
		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;
			}
		}
		
		ulong currentCounter;
		public ulong CurrentCounter {
			get {
				return currentCounter;
			}
		}
		
		DateTime currentTime;
		public DateTime CurrentTime {
			get {
				return currentTime;
			}
		}
		
		double ticksPerCounterUnit;
		void updateTicksPerCounterUnit () {
			if (currentCounter > startCounter) {
				ulong counterSpan = currentCounter - startCounter;
				TimeSpan timeSpan = currentTime - startTime;
				ticksPerCounterUnit = ((double)timeSpan.Ticks) / ((double)counterSpan);
			}
		}
		void updateCounterAndTime (ulong currentCounter, DateTime currentTime) {
			this.currentCounter = currentCounter;
			this.currentTime = currentTime;
			updateTicksPerCounterUnit ();
		}
		public DateTime counterToDateTime (ulong counter) {
			return StartTime + TimeSpan.FromTicks ((long) (ticksPerCounterUnit * (double)(counter - StartCounter)));
		}
		public TimeSpan clicksToTimeSpan (ulong clicks) {
			return TimeSpan.FromTicks ((long) (ticksPerCounterUnit * (double)clicks));
		}
		public double clicksToSeconds (ulong clicks) {
			return (ticksPerCounterUnit * (double)clicks) / TimeSpan.TicksPerSecond;
		}
		
		public override void Start (uint version, string runtimeFile, ProfilerFlags flags, ulong startCounter, DateTime startTime) {
			this.version = version;
			this.runtimeFile = runtimeFile;
			this.flags = flags;
			this.startCounter = startCounter;
			this.startTime = startTime;
		}
		
		public override void End (uint version, ulong endCounter, DateTime endTime) {
			if (this.version != version) {
				throw new Exception (String.Format ("Version {0} specified at start is inconsistent witn {1} specified at end", this.version, version));
			}
			this.endCounter = endCounter;
			this.endTime = endTime;
			updateCounterAndTime (endCounter, endTime);
		}
		
		public override void StartBlock (ulong startCounter, DateTime startTime, ulong threadId) {
			updateCounterAndTime (startCounter, startTime);
		}
		
		public override void EndBlock (ulong endCounter, DateTime endTime, ulong threadId) {
			updateCounterAndTime (endCounter, endTime);
		}
		
		public override void ModuleLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success) {}
		public override void ModuleUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name) {}
		public override void AssemblyLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success) {}
		public override void AssemblyUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name) {}
		public override void ApplicationDomainLoaded (ulong threadId, ulong startCounter, ulong endCounter, string name, bool success) {}
		public override void ApplicationDomainUnloaded (ulong threadId, ulong startCounter, ulong endCounter, string name) {}
		
		public override void SetCurrentThread (ulong threadId) {
			if (perThreadStacks.ContainsKey (threadId)) {
				stack = perThreadStacks [threadId];
			} else {
				stack = new CallStack (threadId);
				perThreadStacks.Add (threadId, stack);
			}
		}
		
		public override void ClassStartLoad (LoadedClass c, ulong counter) {}
		public override void ClassEndLoad (LoadedClass c, ulong counter, bool success) {}
		public override void ClassStartUnload (LoadedClass c, ulong counter) {}
		public override void ClassEndUnload (LoadedClass c, ulong counter) {}
		
		public override void Allocation (LoadedClass c, uint size, LoadedMethod caller, bool jitTime, ulong counter) {
			StackTrace trace;
			
			if (Directives.AllocationsHaveStackTrace) {
				trace = StackTrace.NewStackTrace (stack);
			} else {
				trace = null;
			}
			
			if (caller == null) {
				if ((stack != null) && (stack.StackTop != null)) {
					caller = stack.StackTop.Method;
					jitTime = stack.StackTop.IsBeingJitted;
				}
			}
			c.InstanceCreated (size, caller, jitTime, trace);
		}
		
		public override void Exception (LoadedClass c, ulong counter) {}
		
		public override void MethodEnter (LoadedMethod m, ulong counter) {
			stack.MethodEnter (m, counter);
		}
		
		public override void MethodExit (LoadedMethod m, ulong counter) {
			stack.MethodExit (m, counter);
		}
		
		public override void MethodJitStart (LoadedMethod m, ulong counter) {
			m.StartJit = counter;
			stack.MethodJitStart (m, counter);
		}
		
		public override void MethodJitEnd (LoadedMethod m, ulong counter, bool success) {
			m.JitClicks += (counter - m.StartJit);
			stack.MethodJitEnd (m, counter);
		}
		
		public override void MethodFreed (LoadedMethod m, ulong counter) {}
		
		public override void AdjustStack (uint lastValidFrame, uint topSectionSize, StackSectionElement<LoadedClass,LoadedMethod>[] topSection) {
			stack.AdjustStack (lastValidFrame, topSectionSize, topSection);
		}
		
		uint remainingCallersInChain;
		IStatisticalHitItem lastCallee;
		// Returns true if the hit must be counted (this is the first chain item)
		bool HandleCallChain (IStatisticalHitItem caller) {
			bool result;
			
			if (remainingCallersInChain > 0) {
				remainingCallersInChain --;
				if (lastCallee != null) {
					//Console.WriteLine ("HandleCallChain[{0}]  {1} on {2}", remainingCallersInChain, caller.Name, lastCallee.Name);
					lastCallee.CallCounts.AddCaller (caller);
					caller.CallCounts.AddCallee (lastCallee);
				}
				result = false;
			} else {
				//Console.WriteLine ("HandleCallChain[{0}] {1}", remainingCallersInChain, caller.Name);
				result = true;
			}
			
			lastCallee = caller;
			
			return result;
		}
		
		public override void StatisticalCallChainStart (uint chainDepth) {
			remainingCallersInChain = chainDepth;
			//Console.WriteLine ("StatisticalCallChainStart ({0})", chainDepth);
		}
		
		public override void MethodStatisticalHit (LoadedMethod m) {
			if (HandleCallChain (m)) {
				m.StatisticalHits ++;
			}
		}
		
		public override void UnmanagedFunctionStatisticalHit (UnmanagedFunctionFromRegion f) {
			if (HandleCallChain (f)) {
				f.StatisticalHits ++;
			}
		}
		public override void UnmanagedFunctionStatisticalHit (UnmanagedFunctionFromID f) {
			if (HandleCallChain (f)) {
				f.StatisticalHits ++;
			}
		}
		public override void UnknownUnmanagedFunctionStatisticalHit (ExecutableMemoryRegion region, uint offset) {
			if (HandleCallChain (region)) {
				region.IncrementStatisticalHits ();
			}
		}
		public override void UnknownUnmanagedFunctionStatisticalHit (ulong address) {
			if (HandleCallChain (unknownStatisticalHitsCollector)) {
				unknownStatisticalHitsCollector.IncrementStatisticalHits ();
			}
		}
		
		public override void ThreadStart (ulong threadId, ulong counter) {}
		public override void ThreadEnd (ulong threadId, ulong counter) {}
		
		public IStatisticalHitItem[] StatisticalHitItems {
			get {
				LoadedMethod[] methods = LoadedElements.Methods;
				ExecutableMemoryRegion [] regions = LoadedElements.ExecutableMemoryRegions;
				UnmanagedFunctionFromRegion[][] regionFunctions = new UnmanagedFunctionFromRegion [regions.Length][];
				UnmanagedFunctionFromID[] idFunctions = LoadedElements.UnmanagedFunctionsByID;
				int resultIndex = 0;
				for (int i = 0; i < regions.Length; i++) {
					ExecutableMemoryRegion region = regions [i];
					regionFunctions [i] = region.Functions;
					resultIndex += regionFunctions [i].Length;
				}
				IStatisticalHitItem[] result = new IStatisticalHitItem [resultIndex + methods.Length + idFunctions.Length + regions.Length + 1];
				
				resultIndex = 0;
				for (int i = 0; i < regions.Length; i++) {
					UnmanagedFunctionFromRegion[] functions = regionFunctions [i];
					Array.ConstrainedCopy (functions, 0, result, resultIndex, functions.Length);
					resultIndex += functions.Length;
				}
				Array.ConstrainedCopy (methods, 0, result, resultIndex, methods.Length);
				resultIndex += methods.Length;
				Array.ConstrainedCopy (idFunctions, 0, result, resultIndex, idFunctions.Length);
				resultIndex += idFunctions.Length;
				Array.ConstrainedCopy (regions, 0, result, resultIndex, regions.Length);
				resultIndex += regions.Length;
				result [resultIndex] = unknownStatisticalHitsCollector;
				
				return result;
			}
		}
		
		public class GcStatistics {
			ProfilerEventHandler data;
			
			uint collection;
			public ulong Collection {
				get {
					return collection;
				}
			}
			
			ulong startCounter;
			public ulong StartCounter {
				get {
					return startCounter;
				}
				internal set {
					startCounter = value;
				}
			}
			ulong endCounter;
			public ulong EndCounter {
				get {
					return endCounter;
				}
				internal set {
					endCounter = value;
				}
			}
			ulong markStartCounter;
			public ulong MarkStartCounter {
				get {
					return markStartCounter;
				}
				internal set {
					markStartCounter = value;
				}
			}
			ulong markEndCounter;
			public ulong MarkEndCounter {
				get {
					return markEndCounter;
				}
				internal set {
					markEndCounter = value;
				}
			}
			ulong sweepStartCounter;
			public ulong SweepStartCounter {
				get {
					return sweepStartCounter;
				}
				internal set {
					sweepStartCounter = value;
				}
			}
			ulong sweepEndCounter;
			public ulong SweepEndCounter {
				get {
					return sweepEndCounter;
				}
				internal set {
					sweepEndCounter = value;
				}
			}
			uint generation;
			public uint Generation {
				get {
					return generation;
				}
				internal set {
					generation = value;
				}
			}
			ulong? newHeapSize;
			public ulong? NewHeapSize {
				get {
					return newHeapSize;
				}
				internal set {
					newHeapSize = value;
				}
			}
			
			public double Start {
				get {
					return data.clicksToSeconds (startCounter - data.StartCounter);
				}
			}
			public double Duration {
				get {
					return data.clicksToSeconds (endCounter - startCounter);
				}
			}
			public double MarkDuration {
				get {
					return data.clicksToSeconds (markEndCounter - markStartCounter);
				}
			}
			public double SweepDuration {
				get {
					return data.clicksToSeconds (sweepEndCounter - sweepStartCounter);
				}
			}
			
			public GcStatistics (ProfilerEventHandler data, uint collection) {
				this.data = data;
				this.collection = collection;
				startCounter = 0;
				endCounter = 0;
				markStartCounter = 0;
				markEndCounter = 0;
				sweepStartCounter = 0;
				sweepEndCounter = 0;
				generation = 0;
				newHeapSize = null;
			}
		}
		
		List<GcStatistics> gcStatistics;
		static Comparison<GcStatistics> compareGcStatisticsByAllocation = delegate (GcStatistics a, GcStatistics b) {
			return a.Collection.CompareTo (b.Collection);
		};
		public GcStatistics[] GarbageCollectioncStatistics {
			get {
				GcStatistics[] result = gcStatistics.ToArray ();
				Array.Sort (result, compareGcStatisticsByAllocation);
				return result;
			}
		}
		
		GcStatistics currentGcStatistics;
		List<GcStatistics> pendingGcStatistics;
		GcStatistics GetGcStatistics (uint collection) {
			if ((currentGcStatistics != null) && (currentGcStatistics.Collection == collection)) {
				return currentGcStatistics;
			} else {
				foreach (GcStatistics gcs in pendingGcStatistics) {
					if (gcs.Collection == collection) {
						GcStatistics result = gcs;
						if (currentGcStatistics != null) {
							pendingGcStatistics.Add (currentGcStatistics);
						}
						currentGcStatistics = result;
						return result;
					}
				}
				return NewGcStatistics (collection);
			}
		} 
		GcStatistics NewGcStatistics (uint collection) {
			GcStatistics result = new GcStatistics (this, collection);
			if (currentGcStatistics != null) {
				pendingGcStatistics.Add (currentGcStatistics);
			}
			currentGcStatistics = result;
			return result;
		}
		
		public override void GarbageCollectionStart (uint collection, uint generation, ulong counter) {
			GcStatistics gcs = NewGcStatistics (collection);
			gcs.Generation = generation;
			gcs.StartCounter = counter;
		}
		public override void GarbageCollectionEnd (uint collection, uint generation, ulong counter) {
			GcStatistics gcs = GetGcStatistics (collection);
			gcs.EndCounter = counter;
			pendingGcStatistics.Remove (gcs);
			gcStatistics.Add (gcs);
			currentGcStatistics = null;
		}
		public override void GarbageCollectionMarkStart (uint collection, uint generation, ulong counter) {
			GcStatistics gcs = GetGcStatistics (collection);
			gcs.MarkStartCounter = counter;
		}
		public override void GarbageCollectionMarkEnd (uint collection, uint generation, ulong counter) {
			GcStatistics gcs = GetGcStatistics (collection);
			gcs.MarkEndCounter = counter;
		}
		public override void GarbageCollectionSweepStart (uint collection, uint generation, ulong counter) {
			GcStatistics gcs = GetGcStatistics (collection);
			gcs.SweepStartCounter = counter;
		}
		public override void GarbageCollectionSweepEnd (uint collection, uint generation, ulong counter) {
			GcStatistics gcs = GetGcStatistics (collection);
			gcs.SweepEndCounter = counter;
		}
		public override void GarbageCollectionResize (uint collection, ulong newSize) {
			GcStatistics gcs = NewGcStatistics (collection);
			gcs.NewHeapSize = newSize;
			gcStatistics.Add (gcs);
			currentGcStatistics = null;
		}
		
		HeapSnapshot currentHeapSnapshot = null;
		
		public override void HeapReportStart (HeapSnapshot snapshot) {
			currentHeapSnapshot = snapshot;
		}
		public override void HeapObjectUnreachable (LoadedClass c, uint size) {
			c.InstanceFreed (size);
			currentHeapSnapshot.HeapObjectUnreachable (c, size);
		}
		public override void HeapObjectReachable (HeapObject<LoadedClass> o) {
		}
		public override void HeapReportEnd (HeapSnapshot snapshot) {
			snapshot.InitializeBackReferences ();
		}
		
		List<AllocationSummary> allocationSummaries;
		public AllocationSummary [] AllocationSummaries {
			get {
				return allocationSummaries.ToArray ();
			}
		}
		AllocationSummary currentAllocationSummary;
		
		public override void AllocationSummaryStart (uint collection, ulong startCounter, DateTime startTime) {
			currentAllocationSummary = new AllocationSummary (collection, startCounter, startTime);
		}
		public override void ClassAllocationSummary (LoadedClass c, uint reachableInstances, uint reachableBytes, uint unreachableInstances, uint unreachableBytes) {
			if (currentAllocationSummary != null) {
				currentAllocationSummary.RecordData (c, reachableInstances, reachableBytes, unreachableInstances, unreachableBytes);
			}
		}
		public override void AllocationSummaryEnd (uint collection, ulong endCounter, DateTime endTime) {
			if ((currentAllocationSummary != null) && (currentAllocationSummary.Collection == collection)) {
				currentAllocationSummary.EndCounter = endCounter;
				currentAllocationSummary.EndTime = endTime;
				allocationSummaries.Add (currentAllocationSummary);
				currentAllocationSummary = null;
			}
		}
		
		public ProfilerEventHandler () : base (new LoadedElementHandler<LoadedClass,LoadedMethod,UnmanagedFunctionFromRegion,UnmanagedFunctionFromID,ExecutableMemoryRegion,HeapObject<LoadedClass>,HeapSnapshot> (new LoadedElementFactory ())) {
			perThreadStacks = new Dictionary<ulong,CallStack> ();
			stack = null;
			unknownStatisticalHitsCollector = new UnknownStatisticalHitsCollector ();
			gcStatistics = new List<GcStatistics> ();
			pendingGcStatistics = new List<GcStatistics> ();
			currentGcStatistics = null;
			allocationSummaries = new List<AllocationSummary> ();
			currentAllocationSummary = null;
		}
	}
}