/// <summary> /// Create a span record for the given span type. Must have a corresponding Leave. /// </summary> /// <param name="spanType"></param> public static void Enter(string spanType) { if (comm == null) { return; } var time = DateTime.UtcNow; ActivityTimer timer; if (!activityTimers.TryGetValue(spanType, out timer)) { timer = new ActivityTimer(); activityTimers.TryAdd(spanType, timer); } timer.Enter(time); if (records != null) { var spanRecord = new SpanRecord(comm.Rank, spanType, time.Ticks, isStart: true); lock (recordLock) { records.Add(spanRecord); } } Logging?.Invoke(comm.Rank, spanType, time, true); }
/// <summary> /// Complete a span record for the given span type. Must have previously been Entered. /// </summary> /// <param name="spanType"></param> public static void Leave(string spanType) { if (comm == null) { return; } var time = DateTime.UtcNow; ActivityTimer timer; if (activityTimers.TryGetValue(spanType, out timer)) { timer.Leave(time); } if (records != null) { var spanRecord = new SpanRecord(comm.Rank, spanType, time.Ticks, isStart: false); lock (recordLock) { records.Add(spanRecord); } } Logging?.Invoke(comm.Rank, spanType, time, false); }
private static IDictionary <int, List <Span> > ComputeSpans(IReadOnlyList <List <SpanRecord> > spanRecordsPerProcess, bool allowOpenSpans) { long earliestTime = spanRecordsPerProcess.Min(list => list[0].Time); long latestTime = spanRecordsPerProcess.Max(list => list[list.Count - 1].Time); var spansPerProcess = new SortedDictionary <int, List <Span> >(); for (int processId = 0; processId < spanRecordsPerProcess.Count; processId++) { checked { var spanRecordsForProcess = spanRecordsPerProcess[processId]; var spansForProcess = new List <Span>(); var stack = new Stack <SpanRecord>(); SpanRecord lastSpanRecord = null; foreach (var currentSpanRecord in spanRecordsForProcess) { if (currentSpanRecord.IsStart) { if (lastSpanRecord != null) { var spanType = stack.Count == 0 ? "None" : stack.Peek().Type; spansForProcess.Add(new Span(currentSpanRecord.Time - lastSpanRecord.Time, spanType)); } else if (currentSpanRecord.Time > earliestTime) { var spanType = "None"; spansForProcess.Add(new Span(currentSpanRecord.Time - earliestTime, spanType)); } stack.Push(currentSpanRecord); } else { if (stack.Count == 0) { throw new Exception($"The end span record does not have a corresponding start span record.{System.Environment.NewLine}{currentSpanRecord}"); } if (lastSpanRecord == null) { throw new Exception($"Span timer implementation error. {System.Environment.NewLine}{currentSpanRecord}"); } var popped = stack.Pop(); if (popped.Type != currentSpanRecord.Type) { throw new Exception($"Span type mismatch.{System.Environment.NewLine}{popped}{System.Environment.NewLine}{currentSpanRecord}"); } spansForProcess.Add(new Span(currentSpanRecord.Time - lastSpanRecord.Time, currentSpanRecord.Type)); } lastSpanRecord = currentSpanRecord; } if (stack.Count != 0) { var popped = stack.Pop(); if (allowOpenSpans) { spansForProcess.Add(new Span(latestTime - lastSpanRecord.Time, popped.Type)); } else { StringBuilder sb = new StringBuilder(); while (stack.Count > 0) { sb.AppendLine(stack.Pop().ToString()); } string suffix = (sb.Length > 0) ? "Remaining stack: " + sb : ""; throw new Exception($"{popped} has no matching End. {suffix}"); } } spansPerProcess.Add(processId, spansForProcess); } } return(spansPerProcess); }