Skip to content

Commit 5f33b10

Browse files
lewingCopilot
andcommitted
[wasi] Pump EventPipe jobs from the WASI event loop
Browser runs EventPipe jobs (session streaming, diagnostic server) from a JS setTimeout loop. WASI has no host event loop, so keep the jobs on a native list and drain it from managed WasiEventLoop via two QCalls, re-polling every 100ms with Task.Delay (which blocks in wasi:io/poll rather than spinning). Jobs only run while the event loop is pumped; otherwise streaming sessions still flush when they are disabled. Co-authored-by: Copilot App <[email protected]>
1 parent fdf9a73 commit 5f33b10

7 files changed

Lines changed: 163 additions & 1 deletion

File tree

‎src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,9 @@ ep_rt_thread_create (
10941094
#ifdef HOST_BROWSER
10951095
#include "wasm/entrypoints.h"
10961096
typedef size_t (*ep_rt_job_cb_t)(void *data);
1097+
#elif defined(TARGET_WASI)
1098+
// Queues a job on the native list drained by the managed WASI event loop (see eventpipeinternal.cpp).
1099+
bool ep_rt_coreclr_wasi_queue_job (size_t (*cb)(void *data), void *data);
10971100
#endif
10981101

10991102
static
@@ -1111,8 +1114,10 @@ ep_rt_queue_job (
11111114
if (!cb (params))
11121115
SystemJS_DiagnosticServerQueueJob (cb, params);
11131116
return true;
1117+
#elif defined(TARGET_WASI)
1118+
return ep_rt_coreclr_wasi_queue_job ((size_t (*)(void *))job_func, params);
11141119
#else
1115-
// No host event loop to run jobs on (e.g. WASI). Callers handle the failure; streaming sessions
1120+
// No host event loop to run jobs on. Callers handle the failure; streaming sessions
11161121
// are flushed when they are disabled.
11171122
return false;
11181123
#endif

‎src/coreclr/vm/eventpipeinternal.cpp‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,81 @@ extern "C" BOOL QCALLTYPE EventPipeInternal_WaitForSessionSignal(UINT64 sessionI
296296
return result;
297297
}
298298

299+
#if defined(TARGET_WASI) && defined(PERFTRACING_DISABLE_THREADS)
300+
// WASI has no host event loop to schedule EventPipe jobs (session streaming, diagnostic server) on,
301+
// so they are kept on this list and run by the managed WasiEventLoop (WasiEventPipeJobs.cs).
302+
// A job returns non-zero when it is done; otherwise it stays queued and runs again on the next pump.
303+
// Single-threaded by construction, so no locking.
304+
struct WasiEventPipeJob
305+
{
306+
size_t (*Callback)(void *data);
307+
void *Data;
308+
WasiEventPipeJob *Next;
309+
};
310+
311+
static WasiEventPipeJob *s_wasiJobsHead = nullptr;
312+
static WasiEventPipeJob *s_wasiJobsTail = nullptr;
313+
314+
static void WasiAppendJob(WasiEventPipeJob *job)
315+
{
316+
job->Next = nullptr;
317+
if (s_wasiJobsTail != nullptr)
318+
s_wasiJobsTail->Next = job;
319+
else
320+
s_wasiJobsHead = job;
321+
s_wasiJobsTail = job;
322+
}
323+
324+
bool ep_rt_coreclr_wasi_queue_job(size_t (*cb)(void *data), void *data)
325+
{
326+
_ASSERTE(cb != nullptr);
327+
WasiEventPipeJob *job = new (nothrow) WasiEventPipeJob();
328+
if (job == nullptr)
329+
return false;
330+
331+
job->Callback = cb;
332+
job->Data = data;
333+
WasiAppendJob(job);
334+
return true;
335+
}
336+
337+
extern "C" CLR_BOOL QCALLTYPE EventPipeInternal_WasiHasPendingJobs(QCallExceptionStatus* qcallError)
338+
{
339+
QCALL_CONTRACT;
340+
341+
CLR_BOOL pending = FALSE;
342+
343+
BEGIN_QCALL;
344+
345+
pending = s_wasiJobsHead != nullptr ? TRUE : FALSE;
346+
347+
END_QCALL;
348+
return pending;
349+
}
350+
351+
extern "C" void QCALLTYPE EventPipeInternal_WasiRunJobs(QCallExceptionStatus* qcallError)
352+
{
353+
QCALL_CONTRACT;
354+
355+
BEGIN_QCALL;
356+
357+
// Detach the current list so jobs queued while running are picked up on the next pump.
358+
WasiEventPipeJob *job = s_wasiJobsHead;
359+
s_wasiJobsHead = nullptr;
360+
s_wasiJobsTail = nullptr;
361+
362+
while (job != nullptr)
363+
{
364+
WasiEventPipeJob *next = job->Next;
365+
if (job->Callback(job->Data) != 0)
366+
delete job;
367+
else
368+
WasiAppendJob(job);
369+
job = next;
370+
}
371+
372+
END_QCALL;
373+
}
374+
#endif // TARGET_WASI && PERFTRACING_DISABLE_THREADS
375+
299376
#endif // FEATURE_PERFTRACING

‎src/coreclr/vm/eventpipeinternal.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ extern "C" BOOL QCALLTYPE EventPipeInternal_WaitForSessionSignal(
104104
INT32 timeoutMs,
105105
QCallExceptionStatus* qcallError);
106106

107+
#if defined(TARGET_WASI) && defined(PERFTRACING_DISABLE_THREADS)
108+
extern "C" CLR_BOOL QCALLTYPE EventPipeInternal_WasiHasPendingJobs(QCallExceptionStatus* qcallError);
109+
110+
extern "C" void QCALLTYPE EventPipeInternal_WasiRunJobs(QCallExceptionStatus* qcallError);
111+
#endif // TARGET_WASI && PERFTRACING_DISABLE_THREADS
112+
107113
#endif // FEATURE_PERFTRACING
108114

109115
#endif // __EVENTPIPEINTERNAL_H__

‎src/coreclr/vm/qcallentrypoints.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ static const Entry s_QCall[] =
489489
DllImportEntry(EventPipeInternal_GetNextEvent)
490490
DllImportEntry(EventPipeInternal_SignalSession)
491491
DllImportEntry(EventPipeInternal_WaitForSessionSignal)
492+
#if defined(TARGET_WASI) && defined(PERFTRACING_DISABLE_THREADS)
493+
DllImportEntry(EventPipeInternal_WasiHasPendingJobs)
494+
DllImportEntry(EventPipeInternal_WasiRunJobs)
495+
#endif // TARGET_WASI && PERFTRACING_DISABLE_THREADS
492496
#endif // FEATURE_PERFTRACING
493497
#if defined(TARGET_UNIX)
494498
DllImportEntry(FreeEnvironmentStringsW)

‎src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,9 @@
29932993
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\ThreadPool.Wasi.cs" />
29942994
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\TimerQueue.Wasi.cs" />
29952995
</ItemGroup>
2996+
<ItemGroup Condition="'$(TargetsWasi)' == 'true' and '$(FeatureMultithreading)' != 'true' and '$(FeaturePerfTracing)' == 'true' and '$(FeatureMono)' != 'true'">
2997+
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Wasi\WasiEventPipeJobs.cs" />
2998+
</ItemGroup>
29962999
<ItemGroup Condition="'$(TargetsBrowser)' == 'true' and '$(FeatureMultithreading)' == 'true'">
29973000
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.Browser.Threads.cs" />
29983001
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\ThreadPool.Browser.Threads.cs" />

‎src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiEventLoop.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ internal static T PollWasiEventLoopUntilResolved<T>(Task<T> mainTask)
5858
{
5959
ThreadPoolWorkQueue.Dispatch();
6060
WasiFinalizerScheduler.DrainIfPending();
61+
#if FEATURE_PERFTRACING && CORECLR
62+
WasiEventPipeJobs.EnsurePumpIfPending();
63+
#endif
6164
}
6265
}
6366
finally
@@ -80,6 +83,9 @@ internal static void PollWasiEventLoopUntilResolvedVoid(Task mainTask)
8083
{
8184
ThreadPoolWorkQueue.Dispatch();
8285
WasiFinalizerScheduler.DrainIfPending();
86+
#if FEATURE_PERFTRACING && CORECLR
87+
WasiEventPipeJobs.EnsurePumpIfPending();
88+
#endif
8389
}
8490
}
8591
finally
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
6+
using System.Threading.Tasks;
7+
8+
namespace System.Threading
9+
{
10+
// Runs the native EventPipe jobs (session streaming, diagnostic server) that browser schedules with
11+
// setTimeout. WASI has no host event loop, so the native side keeps a job list and WasiEventLoop
12+
// starts this pump when it sees pending jobs. Jobs only run while the event loop is being pumped.
13+
internal static partial class WasiEventPipeJobs
14+
{
15+
// Matches the browser re-schedule interval for unfinished jobs.
16+
private const int PumpIntervalMs = 100;
17+
18+
private static bool s_pumpRunning;
19+
20+
[ErrorHandler(typeof(QCallExceptionStatusMarshaller), ErrorLocation.HiddenLastParameter)]
21+
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_WasiHasPendingJobs")]
22+
[return: MarshalAs(UnmanagedType.Bool)]
23+
private static partial bool HasPendingJobs();
24+
25+
[ErrorHandler(typeof(QCallExceptionStatusMarshaller), ErrorLocation.HiddenLastParameter)]
26+
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_WasiRunJobs")]
27+
private static partial void RunJobs();
28+
29+
internal static void EnsurePumpIfPending()
30+
{
31+
if (s_pumpRunning || !HasPendingJobs())
32+
{
33+
return;
34+
}
35+
36+
s_pumpRunning = true;
37+
_ = PumpAsync();
38+
}
39+
40+
private static async Task PumpAsync()
41+
{
42+
try
43+
{
44+
while (true)
45+
{
46+
RunJobs();
47+
if (!HasPendingJobs())
48+
{
49+
break;
50+
}
51+
52+
await Task.Delay(PumpIntervalMs).ConfigureAwait(false);
53+
}
54+
}
55+
finally
56+
{
57+
s_pumpRunning = false;
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)