Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure unique requestId values in ETW events #1304

Merged
merged 1 commit into from
Apr 19, 2024
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
18 changes: 10 additions & 8 deletions src/Microsoft.VisualStudio.Threading/AsyncReaderWriterLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,22 +2717,24 @@ public override void Post(SendOrPostCallback d, object? state)
{
Requires.NotNull(d, nameof(d));

int? requestId = null;
if (ThreadingEventSource.Instance.IsEnabled())
{
ThreadingEventSource.Instance.PostExecutionStart(d.GetHashCode(), false);
requestId = JoinableTaskFactory.SingleExecuteProtector.GetNextRequestId();
ThreadingEventSource.Instance.PostExecutionStart(requestId.Value, false);
}

// Take special care to minimize allocations and overhead by avoiding implicit delegates and closures.
// The C# compiler caches this delegate in a static field because it never touches "this"
// nor any other local variables, which means the only allocations from this call
// are our Tuple and the ThreadPool's bare-minimum necessary to track the work.
ThreadPool.QueueUserWorkItem(
s =>
static s =>
{
var tuple = (Tuple<NonConcurrentSynchronizationContext, SendOrPostCallback, object>)s!;
tuple.Item1.PostHelper(tuple.Item2, tuple.Item3);
var tuple = (Tuple<NonConcurrentSynchronizationContext, SendOrPostCallback, object, int?>)s!;
tuple.Item1.PostHelper(tuple.Item2, tuple.Item3, tuple.Item4);
},
Tuple.Create(this, d, state));
Tuple.Create(this, d, state, requestId));
}

/// <inheritdoc/>
Expand Down Expand Up @@ -2769,7 +2771,7 @@ internal void EarlyExitSynchronizationContext()
/// We use async void instead of async Task because the caller will never
/// use the result, and this way the compiler doesn't have to create the Task object.
/// </remarks>
private async void PostHelper(SendOrPostCallback d, object state)
private async void PostHelper(SendOrPostCallback d, object state, int? requestId)
{
bool delegateInvoked = false;
try
Expand All @@ -2779,9 +2781,9 @@ private async void PostHelper(SendOrPostCallback d, object state)
try
{
SynchronizationContext.SetSynchronizationContext(this);
if (ThreadingEventSource.Instance.IsEnabled())
if (ThreadingEventSource.Instance.IsEnabled() && requestId.HasValue)
{
ThreadingEventSource.Instance.PostExecutionStop(d.GetHashCode());
ThreadingEventSource.Instance.PostExecutionStop(requestId.Value);
}

delegateInvoked = true; // set now, before the delegate might throw.
Expand Down
10 changes: 1 addition & 9 deletions src/Microsoft.VisualStudio.Threading/JoinableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,15 +737,7 @@ internal void Post(SendOrPostCallback d, object? state, bool mainThreadAffinitiz
bool backgroundThreadQueueUpdated = false;
wrapper = SingleExecuteProtector.Create(this, d, state);

if (ThreadingEventSource.Instance.IsEnabled())
{
ThreadingEventSource.Instance.PostExecutionStart(wrapper.GetHashCode(), mainThreadAffinitized);
}

if (mainThreadAffinitized && !synchronouslyBlockingMainThread)
{
wrapper.RaiseTransitioningEvents();
}
wrapper.RaiseTransitioningEvents(mainThreadAffinitized, synchronouslyBlockingMainThread);

lock (this.JoinableTaskContext.SyncContextLock)
{
Expand Down
43 changes: 36 additions & 7 deletions src/Microsoft.VisualStudio.Threading/JoinableTaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,11 +1066,21 @@ internal class SingleExecuteProtector
/// </summary>
internal static readonly WaitCallback ExecuteOnceWaitCallback = state => ((SingleExecuteProtector)state!).TryExecute();

/// <summary>
/// Tracks the next request ID to assign to <see cref="requestId"/> for a new object.
/// </summary>
private static int nextRequestId;

/// <summary>
/// The job that created this wrapper.
/// </summary>
private JoinableTask? job;

/// <summary>
/// The ID to use when calling <see cref="ThreadingEventSource.PostExecutionStop(int)"/>.
/// </summary>
private int? requestId;

private bool raiseTransitionComplete;

/// <summary>
Expand Down Expand Up @@ -1118,6 +1128,17 @@ internal string DelegateLabel
}
}

/// <summary>
/// Returns a unique value for use when calling <see cref="ThreadingEventSource.PostExecutionStart(int, bool)"/>
/// and <see cref="ThreadingEventSource.PostExecutionStop(int)"/>.
/// </summary>
/// <returns>The unique value.</returns>
/// <remarks>
/// Values may be negative or zero if more than <see cref="int.MaxValue"/> IDs have been created.
/// Values may be reused if more than 2^32 IDs have been created.
/// </remarks>
internal static int GetNextRequestId() => Interlocked.Increment(ref nextRequestId);

/// <summary>
/// Initializes a new instance of the <see cref="SingleExecuteProtector"/> class.
/// </summary>
Expand Down Expand Up @@ -1209,13 +1230,21 @@ internal IEnumerable<string> WalkAsyncReturnStackFrames()
}
}

internal void RaiseTransitioningEvents()
internal void RaiseTransitioningEvents(bool mainThreadAffinitized, bool synchronouslyBlockingMainThread)
{
Assumes.False(this.raiseTransitionComplete); // if this method is called twice, that's the sign of a problem.
RoslynDebug.Assert(this.job is object);
if (ThreadingEventSource.Instance.IsEnabled())
{
this.requestId = GetNextRequestId();
ThreadingEventSource.Instance.PostExecutionStart(this.requestId.Value, mainThreadAffinitized);
}

this.raiseTransitionComplete = true;
this.job.Factory.OnTransitioningToMainThread(this.job);
if (mainThreadAffinitized && !synchronouslyBlockingMainThread)
{
Assumes.False(this.raiseTransitionComplete); // if this method is called twice, that's the sign of a problem.
this.raiseTransitionComplete = true;
RoslynDebug.Assert(this.job is object);
this.job.Factory.OnTransitioningToMainThread(this.job);
}
}

/// <summary>
Expand Down Expand Up @@ -1259,9 +1288,9 @@ internal bool TryExecute()
/// </summary>
private void OnExecuting()
{
if (ThreadingEventSource.Instance.IsEnabled())
if (ThreadingEventSource.Instance.IsEnabled() && this.requestId is int requestId)
{
ThreadingEventSource.Instance.PostExecutionStop(this.GetHashCode());
ThreadingEventSource.Instance.PostExecutionStop(requestId);
}

// While raising the event, automatically remove the handlers since we'll only
Expand Down