Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,8 @@ public static void ResumeRuntimeAsyncContext(Task task, ref AsyncDispatcherInfo
info.CurrentTask = task;
AsyncProfiler.InitInfo(ref info.AsyncProfilerInfo);

info.AsyncProfilerInfo.DispatcherId = (ulong)task.Id;

if (AsyncInstrumentation.IsEnabled.ResumeAsyncContext(flags))
{
if (AsyncInstrumentation.IsEnabled.AsyncProfiler(flags))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,25 @@ internal static partial class AsyncProfiler
{
internal static partial class DispatcherIds
{
public static ulong GetDispatcherId(ref AsyncDispatcherInfo info)
{
if (info.CurrentTask != null)
{
return (ulong)info.CurrentTask.Id;
}
return 0;
}
public static ulong GetDispatcherId(Task dispatcher) => (ulong)dispatcher.Id;

public static ulong GetDispatcherId(ref AsyncDispatcherInfo info) => info.AsyncProfilerInfo.DispatcherId;

public static unsafe ulong CaptureParentDispatcherId()
{
AsyncDispatcherInfo* v2 = AsyncDispatcherInfo.t_current;
AsyncStateMachineDispatcherInfo* v1 = AsyncStateMachineDispatcherInfo.t_current;

Task? parent = null;
if (v2 != null && (v1 == null || (void*)v2 < (void*)v1))
{
parent = v2->CurrentTask;
return v2->AsyncProfilerInfo.DispatcherId;
}
else if (v1 != null)
{
parent = v1->Dispatcher;
return v1->AsyncProfilerInfo.DispatcherId;
}

return parent != null ? (ulong)parent.Id : 0;
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,26 @@ private static EventManifestEntry[] BuildEntries()

internal ref struct Info
{
public ulong DispatcherId;
public object? Context;
public object? CurrentContinuation;
public bool CurrentContinuationCompleted;
public IAsyncStateMachineBox? LastContinuation;
public ref nint ContinuationTable;
public uint ContinuationIndex;
public bool CurrentContinuationCompleted;
public bool CurrentContinuationResumes;
public bool ReachedLastContinuation;
}

internal static void InitInfo(ref Info info)
{
info.DispatcherId = 0;
info.Context = null;
info.CurrentContinuation = null;
info.LastContinuation = null;
info.CurrentContinuationCompleted = false;
info.CurrentContinuationResumes = false;
info.ReachedLastContinuation = false;
ContinuationWrapper.InitInfo(ref info);
}

Expand Down Expand Up @@ -894,37 +902,26 @@ private static AsyncThreadContext CreateAsyncThreadContext()

internal static partial class DispatcherIds
{
public static ulong GetDispatcherId(Task dispatcher) => (ulong)dispatcher.Id;
public static ulong GetDispatcherId(IAsyncStateMachineDispatcher dispatcher) =>
dispatcher.DispatcherId;

public static ulong GetDispatcherId(ref AsyncStateMachineDispatcherInfo info)
{
if (info.Dispatcher != null)
{
return GetDispatcherId(info.Dispatcher);
}
return 0;
}
public static ulong GetDispatcherId(ref AsyncStateMachineDispatcherInfo info) =>
info.AsyncProfilerInfo.DispatcherId;

#if !RUNTIME_ASYNC_SUPPORTED
public static unsafe ulong CaptureParentDispatcherId()
{
AsyncStateMachineDispatcherInfo* info = AsyncStateMachineDispatcherInfo.t_current;
if (info == null)
{
return 0;
}

AsyncStateMachineDispatcher? parent = info->Dispatcher;
return parent is not null ? (ulong)parent.Id : 0;
return info != null ? info->AsyncProfilerInfo.DispatcherId : 0;
}
#endif
}

internal static partial class CreateAsyncContext
{
public static void Create(AsyncStateMachineDispatcher dispatcher, ref Info info, ulong parentDispatcherId, ulong dispatcherId)
public static void Create(ref AsyncStateMachineDispatcherInfo info, IAsyncStateMachineDispatcher dispatcher)
{
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info);
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info.AsyncProfilerInfo);

SyncPoint.Check(context);

Expand All @@ -934,19 +931,22 @@ public static void Create(AsyncStateMachineDispatcher dispatcher, ref Info info,
long currentTimestamp = Stopwatch.GetTimestamp();
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(activeEventKeywords))
{
ResumeAsyncContext.Append(dispatcher, context, currentTimestamp);
ResumeAsyncContext.Append(ref info, context, currentTimestamp);
}

if (IsEnabled.CreateStateMachineAsyncContextEvent(activeEventKeywords))
{
ulong parentDispatcherId = AsyncProfiler.DispatcherIds.CaptureParentDispatcherId();
ulong dispatcherId = AsyncProfiler.DispatcherIds.GetDispatcherId(dispatcher);

EmitEvent(context, currentTimestamp, parentDispatcherId, dispatcherId, AsyncEventID.CreateStateMachineAsyncContext);
}
}

AsyncThreadContext.Release(context);
}

public static void Create(ulong parentDispatcherId, ulong dispatcherId)
public static void Create(IAsyncStateMachineDispatcher dispatcher)
{
Info info = default;
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info);
Expand All @@ -955,22 +955,25 @@ public static void Create(ulong parentDispatcherId, ulong dispatcherId)

if (IsEnabled.CreateStateMachineAsyncContextEvent(context.ActiveEventKeywords))
{
ulong parentDispatcherId = AsyncProfiler.DispatcherIds.CaptureParentDispatcherId();
ulong dispatcherId = AsyncProfiler.DispatcherIds.GetDispatcherId(dispatcher);

EmitEvent(context, Stopwatch.GetTimestamp(), parentDispatcherId, dispatcherId, AsyncEventID.CreateStateMachineAsyncContext);
}

AsyncThreadContext.Release(context);
}

public static void Append(AsyncStateMachineDispatcher dispatcher, ref Info info)
public static void Append(ref AsyncStateMachineDispatcherInfo info)
{
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info);
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info.AsyncProfilerInfo);

SyncPoint.Check(context);

EventKeywords activeEventKeywords = context.ActiveEventKeywords;
if (IsEnabled.AnyAsyncEvents(activeEventKeywords) && IsEnabled.ResumeStateMachineAsyncCallstackEvent(activeEventKeywords))
{
ResumeAsyncContext.Append(dispatcher, context, Stopwatch.GetTimestamp());
ResumeAsyncContext.Append(ref info, context, Stopwatch.GetTimestamp());
}

AsyncThreadContext.Release(context);
Expand Down Expand Up @@ -1028,21 +1031,21 @@ public static void Resume(ref AsyncStateMachineDispatcherInfo info, AsyncThreadC
}
}

public static void Append(AsyncStateMachineDispatcher dispatcher, AsyncThreadContext context, long currentTimestamp)
public static void Append(ref AsyncStateMachineDispatcherInfo info, AsyncThreadContext context, long currentTimestamp)
{
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(context.ActiveEventKeywords) && dispatcher.ContinuationChainChanged)
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(context.ActiveEventKeywords) && info.ContinuationChainChanged)
{
AsyncCallstack.EmitEvent(dispatcher, context, dispatcher.NextContinuationForDiagnostics, currentTimestamp, AsyncEventID.AppendStateMachineAsyncCallstack, DispatcherIds.GetDispatcherId(dispatcher));
AsyncCallstack.EmitEvent(ref info, context, info.NextContinuationForDiagnostics, currentTimestamp, AsyncEventID.AppendStateMachineAsyncCallstack, DispatcherIds.GetDispatcherId(ref info));
}
}

public static void Append(AsyncStateMachineDispatcher dispatcher, IAsyncStateMachineBox enteringBox, AsyncThreadContext context, long currentTimestamp)
public static void Append(ref AsyncStateMachineDispatcherInfo info, IAsyncStateMachineBox enteringBox, AsyncThreadContext context, long currentTimestamp)
{
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(context.ActiveEventKeywords) && dispatcher.ReachedLastContinuation)
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(context.ActiveEventKeywords) && info.AsyncProfilerInfo.ReachedLastContinuation)
{
if (!ReferenceEquals(enteringBox, dispatcher.LastContinuation))
if (!ReferenceEquals(enteringBox, info.AsyncProfilerInfo.LastContinuation))
{
AsyncCallstack.EmitEvent(dispatcher, context, enteringBox, currentTimestamp, AsyncEventID.AppendStateMachineAsyncCallstack, DispatcherIds.GetDispatcherId(dispatcher));
AsyncCallstack.EmitEvent(ref info, context, enteringBox, currentTimestamp, AsyncEventID.AppendStateMachineAsyncCallstack, DispatcherIds.GetDispatcherId(ref info));
}
}
}
Expand All @@ -1067,25 +1070,15 @@ public static void EmitEvent(AsyncThreadContext context, long currentTimestamp,

internal static partial class SuspendAsyncContext
{
public static void Suspend(AsyncStateMachineDispatcher dispatcher, ref Info info)
public static void Suspend(ref Info info)
{
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info);

SyncPoint.Check(context);

EventKeywords activeEventKeywords = context.ActiveEventKeywords;
if (IsEnabled.AnyAsyncEvents(activeEventKeywords))
if (IsEnabled.SuspendStateMachineAsyncContextEvent(context.ActiveEventKeywords))
{
long currentTimestamp = Stopwatch.GetTimestamp();
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(activeEventKeywords))
{
ResumeAsyncContext.Append(dispatcher, context, currentTimestamp);
}

if (IsEnabled.SuspendStateMachineAsyncContextEvent(activeEventKeywords))
{
EmitEvent(context, currentTimestamp, AsyncEventID.SuspendStateMachineAsyncContext);
}
EmitEvent(context, Stopwatch.GetTimestamp(), AsyncEventID.SuspendStateMachineAsyncContext);
}

AsyncThreadContext.Release(context);
Expand All @@ -1100,9 +1093,9 @@ public static void EmitEvent(AsyncThreadContext context, long currentTimestamp,

internal static partial class CompleteAsyncContext
{
public static void Complete(AsyncStateMachineDispatcher dispatcher, ref Info info)
public static void Complete(ref AsyncStateMachineDispatcherInfo info)
{
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info);
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info.AsyncProfilerInfo);

SyncPoint.Check(context);

Expand All @@ -1112,7 +1105,7 @@ public static void Complete(AsyncStateMachineDispatcher dispatcher, ref Info inf
long currentTimestamp = Stopwatch.GetTimestamp();
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(activeEventKeywords))
{
ResumeAsyncContext.Append(dispatcher, context, currentTimestamp);
ResumeAsyncContext.Append(ref info, context, currentTimestamp);
}

if (IsEnabled.CompleteStateMachineAsyncContextEvent(activeEventKeywords))
Expand Down Expand Up @@ -1163,17 +1156,17 @@ public static void EmitEvent(AsyncThreadContext context, long currentTimestamp,

internal static partial class ResumeAsyncMethod
{
public static void Resume(AsyncStateMachineDispatcher dispatcher, IAsyncStateMachineBox box, ref Info info)
public static void Resume(ref AsyncStateMachineDispatcherInfo info, IAsyncStateMachineBox box)
{
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info);
AsyncThreadContext context = AsyncThreadContext.Acquire(ref info.AsyncProfilerInfo);

EventKeywords activeEventKeywords = context.ActiveEventKeywords;
if (IsEnabled.AnyAsyncEvents(activeEventKeywords))
{
long currentTimestamp = Stopwatch.GetTimestamp();
if (IsEnabled.ResumeStateMachineAsyncCallstackEvent(activeEventKeywords))
{
ResumeAsyncContext.Append(dispatcher, box, context, currentTimestamp);
ResumeAsyncContext.Append(ref info, box, context, currentTimestamp);
}

if (IsEnabled.ResumeStateMachineAsyncMethodEvent(activeEventKeywords))
Expand Down Expand Up @@ -1624,11 +1617,11 @@ public static void EmitEvent(ref AsyncStateMachineDispatcherInfo info, AsyncThre

EmitAsyncCallstack(context, currentTimestamp, currentTimestamp - context.LastEventTimestamp, AsyncEventID.ResumeStateMachineAsyncCallstack, 0, dispatcherId, ref state);

info.Dispatcher.LastContinuation = IsTruncated(in state) ? null : ResolveAsyncStateMachineBox(state.LastContinuation);
info.Dispatcher.ReachedLastContinuation = false;
info.AsyncProfilerInfo.LastContinuation = IsTruncated(in state) ? null : ResolveAsyncStateMachineBox(state.LastContinuation);
info.AsyncProfilerInfo.ReachedLastContinuation = false;
}

public static void EmitEvent(AsyncStateMachineDispatcher dispatcher, AsyncThreadContext context, object? continuation, long currentTimestamp, AsyncEventID eventID, ulong dispatcherId)
public static void EmitEvent(ref AsyncStateMachineDispatcherInfo info, AsyncThreadContext context, object? continuation, long currentTimestamp, AsyncEventID eventID, ulong dispatcherId)
{
Debug.Assert(eventID == AsyncEventID.ResumeStateMachineAsyncCallstack || eventID == AsyncEventID.AppendStateMachineAsyncCallstack);

Expand All @@ -1642,14 +1635,14 @@ public static void EmitEvent(AsyncStateMachineDispatcher dispatcher, AsyncThread

EmitAsyncCallstack(context, currentTimestamp, currentTimestamp - context.LastEventTimestamp, eventID, 0, dispatcherId, ref state);

dispatcher.LastContinuation = IsTruncated(in state) ? null : ResolveAsyncStateMachineBox(state.LastContinuation);
info.AsyncProfilerInfo.LastContinuation = IsTruncated(in state) ? null : ResolveAsyncStateMachineBox(state.LastContinuation);
}
else
{
dispatcher.LastContinuation = null;
info.AsyncProfilerInfo.LastContinuation = null;
}

dispatcher.ReachedLastContinuation = false;
info.AsyncProfilerInfo.ReachedLastContinuation = false;
}
}

Expand Down Expand Up @@ -1680,7 +1673,7 @@ private static bool CaptureStateMachineAsyncCallstack(byte[] buffer, ref int ind

while (state.Count < maxAsyncCallstackFrames && state.Continuation != null)
{
if (state.Continuation is AsyncStateMachineDispatcher)
if (state.Continuation is IAsyncStateMachineDispatcher { IsLeaf: true })
{
state.Continuation = null;
break;
Expand Down
Loading
Loading