|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.Diagnostics.Runtime; |
| 5 | + |
| 6 | +namespace ParallelStacks |
| 7 | +{ |
| 8 | + public class ParallelStack |
| 9 | + { |
| 10 | + public static ParallelStack Build(ClrRuntime runtime) |
| 11 | + { |
| 12 | + var ps = new ParallelStack(); |
| 13 | + var stackFrames = new List<ClrStackFrame>(64); |
| 14 | + foreach (var thread in runtime.Threads) |
| 15 | + { |
| 16 | + stackFrames.Clear(); |
| 17 | + |
| 18 | + foreach (var stackFrame in thread.StackTrace.Reverse()) |
| 19 | + { |
| 20 | + if (stackFrame.Kind != ClrStackFrameType.ManagedMethod) |
| 21 | + continue; |
| 22 | + |
| 23 | + stackFrames.Add(stackFrame); |
| 24 | + } |
| 25 | + |
| 26 | + if (stackFrames.Count == 0) |
| 27 | + continue; |
| 28 | + |
| 29 | + ps.AddStack(thread.ManagedThreadId, stackFrames.ToArray()); |
| 30 | + } |
| 31 | + |
| 32 | + return ps; |
| 33 | + } |
| 34 | + |
| 35 | + private bool _useDml; |
| 36 | + |
| 37 | + private ParallelStack(ClrStackFrame frame = null) |
| 38 | + { |
| 39 | + Stacks = new List<ParallelStack>(); |
| 40 | + ThreadIds = new List<int>(); |
| 41 | + Frame = (frame == null) ? null : new StackFrame(frame); |
| 42 | + } |
| 43 | + |
| 44 | + public List<ParallelStack> Stacks { get; } |
| 45 | + |
| 46 | + public StackFrame Frame { get; } |
| 47 | + |
| 48 | + public List<int> ThreadIds { get; set; } |
| 49 | + |
| 50 | + public void WriteToConsole(bool useDml = false) |
| 51 | + { |
| 52 | + _useDml = useDml; |
| 53 | + WriteStack(this); |
| 54 | + } |
| 55 | + |
| 56 | + private const int padding = 5; |
| 57 | + private void WriteStack(ParallelStack stack, int increment = 0) |
| 58 | + { |
| 59 | + var alignment = new string(' ', padding * increment); |
| 60 | + if (stack.Stacks.Count == 0) |
| 61 | + { |
| 62 | + var lastFrame = stack.Frame; |
| 63 | + Console.Write($"{Environment.NewLine}{alignment}"); |
| 64 | + WriteFrameSeparator(" ~~~~"); |
| 65 | + //Console.Write($"{Environment.NewLine}{alignment}{stack.ThreadIds.Count,padding} "); |
| 66 | + WriteCount($"{Environment.NewLine}{alignment}{stack.ThreadIds.Count,padding} "); |
| 67 | + WriteFrame(lastFrame); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + foreach (var nextStackFrame in stack.Stacks.OrderBy(s => s.ThreadIds.Count)) |
| 72 | + { |
| 73 | + WriteStack(nextStackFrame, |
| 74 | + (nextStackFrame.ThreadIds.Count == stack.ThreadIds.Count) ? increment : increment + 1); |
| 75 | + } |
| 76 | + |
| 77 | + var currentFrame = stack.Frame; |
| 78 | + //Console.Write($"{Environment.NewLine}{alignment}{stack.ThreadIds.Count,padding} "); |
| 79 | + WriteCount($"{Environment.NewLine}{alignment}{stack.ThreadIds.Count,padding} "); |
| 80 | + WriteFrame(currentFrame); |
| 81 | + } |
| 82 | + |
| 83 | + private void WriteFrame(StackFrame frame) |
| 84 | + { |
| 85 | + if (!string.IsNullOrEmpty(frame.TypeName)) |
| 86 | + { |
| 87 | + var namespaces = frame.TypeName.Split('.'); |
| 88 | + for (int i = 0; i < namespaces.Length - 1; i++) |
| 89 | + { |
| 90 | + WriteNamespace(namespaces[i]); |
| 91 | + WriteSeparator("."); |
| 92 | + } |
| 93 | + WriteMethodType(namespaces[namespaces.Length - 1]); |
| 94 | + WriteSeparator("."); |
| 95 | + } |
| 96 | + |
| 97 | + WriteMethod(frame.MethodName); |
| 98 | + WriteSeparator("("); |
| 99 | + |
| 100 | + var parameters = frame.Signature; |
| 101 | + for (int current = 0; current < parameters.Count; current++) |
| 102 | + { |
| 103 | + var parameter = parameters[current]; |
| 104 | + // handle byref case |
| 105 | + var pos = parameter.LastIndexOf(" ByRef"); |
| 106 | + if (pos != -1) |
| 107 | + { |
| 108 | + WriteType(parameter.Substring(0, pos)); |
| 109 | + WriteDark(" ByRef"); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + WriteType(parameter); |
| 114 | + } |
| 115 | + if (current < parameters.Count - 1) WriteSeparator(", "); |
| 116 | + } |
| 117 | + WriteSeparator(")"); |
| 118 | + } |
| 119 | + |
| 120 | + private void WriteCount(string count) |
| 121 | + { |
| 122 | + if (_useDml) |
| 123 | + { |
| 124 | + Console.Write($"<col fg=\"srcpair\" bg=\"wbg\">{count}</col>"); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + WriteWithColor(count, ConsoleColor.Cyan); |
| 129 | + } |
| 130 | + private void WriteNamespace(string ns) |
| 131 | + { |
| 132 | + WriteWithColor(ns, ConsoleColor.DarkCyan); |
| 133 | + } |
| 134 | + private void WriteType(string type) |
| 135 | + { |
| 136 | + WriteWithColor(type, ConsoleColor.Gray); |
| 137 | + } |
| 138 | + private void WriteSeparator(string separator) |
| 139 | + { |
| 140 | + WriteWithColor(separator, ConsoleColor.White); |
| 141 | + } |
| 142 | + private void WriteDark(string separator) |
| 143 | + { |
| 144 | + WriteWithColor(separator, ConsoleColor.DarkGray); |
| 145 | + } |
| 146 | + private void WriteMethod(string method) |
| 147 | + { |
| 148 | + if (_useDml) |
| 149 | + { |
| 150 | + Console.Write($"<col fg=\"srckw\" bg=\"wbg\">{method}</col>"); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + WriteWithColor(method, ConsoleColor.Cyan); |
| 155 | + } |
| 156 | + private void WriteMethodType(string type) |
| 157 | + { |
| 158 | + if (_useDml) |
| 159 | + { |
| 160 | + Console.Write($"<b><col fg=\"srckw\" bg=\"wbg\">{type}</col></b>"); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + WriteWithColor(type, ConsoleColor.DarkCyan); |
| 165 | + } |
| 166 | + private void WriteFrameSeparator(string text) |
| 167 | + { |
| 168 | + if (_useDml) |
| 169 | + { |
| 170 | + Console.Write($"<b><col fg=\"srcpair\" bg=\"wbg\">{text}</col></b>"); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + WriteWithColor(text, ConsoleColor.Yellow); |
| 175 | + } |
| 176 | + |
| 177 | + private void WriteWithColor(string text, ConsoleColor color) |
| 178 | + { |
| 179 | + var current = Console.ForegroundColor; |
| 180 | + Console.ForegroundColor = color; |
| 181 | + Console.Write(text); |
| 182 | + Console.ForegroundColor = current; |
| 183 | + } |
| 184 | + |
| 185 | + private void AddStack(int threadId, ClrStackFrame[] frames, int index = 0) |
| 186 | + { |
| 187 | + ThreadIds.Add(threadId); |
| 188 | + var firstFrame = frames[index].DisplayString; |
| 189 | + var callstack = Stacks.FirstOrDefault(s => s.Frame.Text == firstFrame); |
| 190 | + if (callstack == null) |
| 191 | + { |
| 192 | + callstack = new ParallelStack(frames[index]); |
| 193 | + Stacks.Add(callstack); |
| 194 | + } |
| 195 | + |
| 196 | + if (index == frames.Length - 1) |
| 197 | + { |
| 198 | + callstack.ThreadIds.Add(threadId); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + callstack.AddStack(threadId, frames, index + 1); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments