-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe063de
commit 84a584e
Showing
22 changed files
with
1,349 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.Threads.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Runtime.Versioning; | ||
using System.Threading; | ||
|
||
namespace System.Diagnostics.Tracing | ||
{ | ||
internal sealed partial class CounterGroup | ||
{ | ||
private static Thread? s_pollingThread; | ||
// Used for sleeping for a certain amount of time while allowing the thread to be woken up | ||
private static AutoResetEvent? s_pollingThreadSleepEvent; | ||
|
||
private static void CreatePollingTimer() | ||
{ | ||
// Create the polling thread and init all the shared state if needed | ||
if (s_pollingThread == null) | ||
{ | ||
s_pollingThreadSleepEvent = new AutoResetEvent(false); | ||
s_counterGroupEnabledList = new List<CounterGroup>(); | ||
// TODO | ||
s_pollingThread = new Thread(PollForValues) | ||
{ | ||
IsBackground = true, | ||
Name = ".NET Counter Poller" | ||
}; | ||
s_pollingThread.Start(); | ||
} | ||
else | ||
{ | ||
// notify the polling thread that the polling interval may have changed and the sleep should be recomputed | ||
s_pollingThreadSleepEvent!.Set(); | ||
} | ||
} | ||
|
||
private static void PollForValues() | ||
{ | ||
AutoResetEvent? sleepEvent = null; | ||
lock (s_counterGroupLock) | ||
{ | ||
sleepEvent = s_pollingThreadSleepEvent; | ||
} | ||
|
||
while (true) | ||
{ | ||
var sleepDurationInMilliseconds = PollOnce(); | ||
|
||
sleepEvent?.WaitOne(sleepDurationInMilliseconds); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.Wasm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Runtime.Versioning; | ||
using System.Threading; | ||
|
||
namespace System.Diagnostics.Tracing | ||
{ | ||
internal sealed partial class CounterGroup | ||
{ | ||
private static Timer? s_pollingTimer; | ||
|
||
private static void CreatePollingTimer() | ||
{ | ||
if (s_pollingTimer == null) | ||
{ | ||
s_pollingTimer = new Timer(PollForValues, null, 0, 0); | ||
s_counterGroupEnabledList = new List<CounterGroup>(); | ||
} | ||
else | ||
{ | ||
// notify the polling callback that the polling interval may have changed and the sleep should be recomputed | ||
s_pollingTimer.Change(0, 0); | ||
} | ||
} | ||
|
||
private static void PollForValues(object? state) | ||
{ | ||
var sleepDurationInMilliseconds = PollOnce(); | ||
s_pollingTimer!.Change(sleepDurationInMilliseconds, 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventDispatcher.Threads.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Diagnostics.Tracing | ||
{ | ||
internal sealed partial class EventPipeEventDispatcher | ||
{ | ||
private void StartDispatchTask(ulong sessionID, DateTime syncTimeUtc, long syncTimeQPC, long timeQPCFrequency) | ||
{ | ||
Debug.Assert(Monitor.IsEntered(m_dispatchControlLock)); | ||
Debug.Assert(sessionID != 0); | ||
|
||
m_dispatchTaskCancellationSource = new CancellationTokenSource(); | ||
Task? previousDispatchTask = m_dispatchTask; | ||
m_dispatchTask = Task.Factory.StartNew(() => DispatchEventsToEventListeners(sessionID, syncTimeUtc, syncTimeQPC, timeQPCFrequency, previousDispatchTask, m_dispatchTaskCancellationSource.Token), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); | ||
} | ||
|
||
private void DispatchEventsToEventListeners(ulong sessionID, DateTime syncTimeUtc, long syncTimeQPC, long timeQPCFrequency, Task? previousDispatchTask, CancellationToken token) | ||
{ | ||
Debug.Assert(sessionID != 0); | ||
previousDispatchTask?.Wait(CancellationToken.None); | ||
|
||
// Struct to fill with the call to GetNextEvent. | ||
while (!token.IsCancellationRequested) | ||
{ | ||
bool eventsReceived = DispatchEventsToEventListenersOnce(sessionID, syncTimeUtc, syncTimeQPC, timeQPCFrequency, token); | ||
|
||
// Wait for more events. | ||
if (!token.IsCancellationRequested) | ||
{ | ||
if (!eventsReceived) | ||
{ | ||
EventPipeInternal.WaitForSessionSignal(sessionID, Timeout.Infinite); | ||
} | ||
|
||
Thread.Sleep(10); | ||
} | ||
} | ||
|
||
// Wait for SignalSession() to be called before we call disable, otherwise | ||
// the SignalSession() call could be on a disabled session. | ||
SpinWait sw = default; | ||
while (Volatile.Read(ref m_sessionID) == sessionID) | ||
{ | ||
sw.SpinOnce(); | ||
} | ||
|
||
// Disable the old session. This can happen asynchronously since we aren't using the old session | ||
// anymore. | ||
EventPipeInternal.Disable(sessionID); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...es/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventDispatcher.Wasm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Diagnostics.Tracing | ||
{ | ||
// this is single-threaded version of EventPipeEventDispatcher | ||
internal sealed partial class EventPipeEventDispatcher | ||
{ | ||
private void StartDispatchTask(ulong sessionID, DateTime syncTimeUtc, long syncTimeQPC, long timeQPCFrequency) | ||
{ | ||
Debug.Assert(Monitor.IsEntered(m_dispatchControlLock)); | ||
Debug.Assert(sessionID != 0); | ||
|
||
m_dispatchTaskCancellationSource = new CancellationTokenSource(); | ||
Task? previousDispatchTask = m_dispatchTask; | ||
if (previousDispatchTask != null) | ||
{ | ||
m_dispatchTask = previousDispatchTask.ContinueWith(_ => DispatchEventsToEventListeners(sessionID, syncTimeUtc, syncTimeQPC, timeQPCFrequency, m_dispatchTaskCancellationSource.Token), | ||
m_dispatchTaskCancellationSource.Token, TaskContinuationOptions.None, TaskScheduler.Default); | ||
} | ||
else | ||
{ | ||
m_dispatchTask = DispatchEventsToEventListeners(sessionID, syncTimeUtc, syncTimeQPC, timeQPCFrequency, m_dispatchTaskCancellationSource.Token); | ||
} | ||
} | ||
|
||
private async Task DispatchEventsToEventListeners(ulong sessionID, DateTime syncTimeUtc, long syncTimeQPC, long timeQPCFrequency, CancellationToken token) | ||
{ | ||
Debug.Assert(sessionID != 0); | ||
|
||
while (!token.IsCancellationRequested) | ||
{ | ||
DispatchEventsToEventListenersOnce(sessionID, syncTimeUtc, syncTimeQPC, timeQPCFrequency, token); | ||
await Task.Delay(100, token).ConfigureAwait(false); | ||
} | ||
|
||
EventPipeInternal.Disable(sessionID); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.