|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved |
| 2 | +// This file is best viewed using outline mode (Ctrl-M Ctrl-O) |
| 3 | +// |
| 4 | +// This program uses code hyperlinks available as part of the HyperAddin Visual Studio plug-in. |
| 5 | +// It is available from http://www.codeplex.com/hyperAddin |
| 6 | +// using Microsoft.Diagnostics.Tracing.Parsers; |
| 7 | +using Microsoft.Diagnostics.Tracing.Etlx; |
| 8 | +using Microsoft.Diagnostics.Tracing.Parsers; |
| 9 | +using Microsoft.Diagnostics.Tracing.Parsers.ApplicationServer; |
| 10 | +using Microsoft.Diagnostics.Tracing.Parsers.AspNet; |
| 11 | +using Microsoft.Diagnostics.Tracing.Parsers.Clr; |
| 12 | +using Microsoft.Diagnostics.Tracing.Session; |
| 13 | +using Microsoft.Diagnostics.Tracing.Stacks; |
| 14 | +using Microsoft.Diagnostics.Tracing.Analysis.JIT; |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Diagnostics; |
| 18 | +using System.Text; |
| 19 | +using StartStopKey = System.Guid; // The start-stop key is unique in the trace. We incorperate the process as well as activity ID to achieve this. |
| 20 | + |
| 21 | +namespace Microsoft.Diagnostics.Tracing |
| 22 | +{ |
| 23 | + public class CLRRuntimeActivityComputer |
| 24 | + { |
| 25 | + struct IdOfIncompleteAction : IEquatable<IdOfIncompleteAction> |
| 26 | + { |
| 27 | + public long Identifier; |
| 28 | + public int ThreadID; |
| 29 | + |
| 30 | + public override int GetHashCode() |
| 31 | + { |
| 32 | + return Identifier.GetHashCode(); |
| 33 | + } |
| 34 | + |
| 35 | + public override bool Equals(object obj) |
| 36 | + { |
| 37 | + if (!(obj is IdOfIncompleteAction)) |
| 38 | + return false; |
| 39 | + |
| 40 | + return Equals((IdOfIncompleteAction)obj); |
| 41 | + } |
| 42 | + |
| 43 | + public bool Equals(IdOfIncompleteAction other) |
| 44 | + { |
| 45 | + return (Identifier == other.Identifier) && (ThreadID == other.ThreadID); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + struct IncompleteActionDesc |
| 50 | + { |
| 51 | + public StartStopStackMingledComputer.EventUID Start; |
| 52 | + public string Name; |
| 53 | + } |
| 54 | + |
| 55 | + Dictionary<IdOfIncompleteAction, IncompleteActionDesc> _incompleteJitEvents = new Dictionary<IdOfIncompleteAction, IncompleteActionDesc>(); |
| 56 | + Dictionary<int, List<StartStopStackMingledComputer.StartStopThreadEventData>> _parsedData = new Dictionary<int, List<StartStopStackMingledComputer.StartStopThreadEventData>>(); |
| 57 | + |
| 58 | + public Dictionary<int, List<StartStopStackMingledComputer.StartStopThreadEventData>> StartStopEvents => _parsedData; |
| 59 | + |
| 60 | + public CLRRuntimeActivityComputer(TraceLogEventSource source) |
| 61 | + { |
| 62 | + source.Clr.MethodJittingStarted += Clr_MethodJittingStarted; |
| 63 | + source.Clr.MethodLoadVerbose += Clr_MethodLoadVerbose; |
| 64 | + source.Clr.MethodLoad += Clr_MethodLoad; |
| 65 | + source.Process(); |
| 66 | + source.Clr.MethodJittingStarted -= Clr_MethodJittingStarted; |
| 67 | + source.Clr.MethodLoadVerbose -= Clr_MethodLoadVerbose; |
| 68 | + source.Clr.MethodLoad -= Clr_MethodLoad; |
| 69 | + } |
| 70 | + |
| 71 | + private void Clr_MethodLoad(MethodLoadUnloadTraceData obj) |
| 72 | + { |
| 73 | + MethodJittedEvent(obj, obj.MethodID); |
| 74 | + } |
| 75 | + |
| 76 | + private void Clr_MethodLoadVerbose(MethodLoadUnloadVerboseTraceData obj) |
| 77 | + { |
| 78 | + MethodJittedEvent(obj, obj.MethodID); |
| 79 | + } |
| 80 | + |
| 81 | + private void MethodJittedEvent(TraceEvent evt, long methodID) |
| 82 | + { |
| 83 | + IdOfIncompleteAction id = new IdOfIncompleteAction(); |
| 84 | + id.Identifier = methodID; |
| 85 | + id.ThreadID = evt.ThreadID; |
| 86 | + if (_incompleteJitEvents.TryGetValue(id, out IncompleteActionDesc jitStartData)) |
| 87 | + { |
| 88 | + // JitStart is processed, don't process it again. |
| 89 | + _incompleteJitEvents.Remove(id); |
| 90 | + |
| 91 | + if (!_parsedData.ContainsKey(id.ThreadID)) |
| 92 | + _parsedData[id.ThreadID] = new List<StartStopStackMingledComputer.StartStopThreadEventData>(); |
| 93 | + |
| 94 | + List<StartStopStackMingledComputer.StartStopThreadEventData> startStopData = _parsedData[id.ThreadID]; |
| 95 | + startStopData.Add(new StartStopStackMingledComputer.StartStopThreadEventData(jitStartData.Start, new StartStopStackMingledComputer.EventUID(evt), jitStartData.Name)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void Clr_MethodJittingStarted(MethodJittingStartedTraceData obj) |
| 100 | + { |
| 101 | + IncompleteActionDesc incompleteDesc = new IncompleteActionDesc(); |
| 102 | + incompleteDesc.Start = new StartStopStackMingledComputer.EventUID(obj); |
| 103 | + incompleteDesc.Name = JITStats.GetMethodName(obj); |
| 104 | + |
| 105 | + IdOfIncompleteAction id = new IdOfIncompleteAction(); |
| 106 | + id.Identifier = obj.MethodID; |
| 107 | + id.ThreadID = obj.ThreadID; |
| 108 | + |
| 109 | + _incompleteJitEvents[id] = incompleteDesc; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments