Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
e73537f
[wasm] Enable the tracing component if threading is supported
lambdageek Jun 17, 2022
8c3360a
WIP: add a way to specify EP sessions in the MonoConfig
lambdageek May 23, 2022
6334ee7
Add a mechanism to copy startup configs into the runtime and session
lambdageek May 23, 2022
4897241
WIP: C side startup provider copying
lambdageek May 31, 2022
43d6ec8
WIP checkpoint. Do more from JS
lambdageek Jun 2, 2022
b232adb
checkpoint: starting a session at startup works
lambdageek Jun 3, 2022
3d22fe1
WIP: checkpoint add a controller and a webworker for DS
lambdageek Jun 6, 2022
ee53f05
WIP checkpoint EventPipeIPCSession class skeleton
lambdageek Jun 8, 2022
6ddb829
WIP checkpoint: runtime crashes; but WS from JS works
lambdageek Jun 8, 2022
5c4aa81
WIP: diagnostic server
lambdageek Jun 14, 2022
dfcc613
XXX PrintfDebuggingHacks
lambdageek Jun 13, 2022
0daa0e0
WIP some bits of the websocket worker
lambdageek Jun 14, 2022
377a50a
WIP some notes on diagnostics and JS workers
lambdageek Jun 15, 2022
4a17d5d
fix eslint
lambdageek Jun 17, 2022
fd8df98
debug printfs etc
lambdageek Jun 17, 2022
f235f55
WIP: start moving the diagnostic server to a JS pthread
lambdageek Jun 21, 2022
431e2c6
WIP: move things around
lambdageek Jun 22, 2022
563b31a
cleanup
lambdageek Jun 23, 2022
5e999a3
notes
lambdageek Jun 22, 2022
2906e1e
[diagnostic_server] wasm-specific fn_table
lambdageek Jun 23, 2022
6c249f0
[wasm-ep] disable DS connect ports in C, too
lambdageek Jun 24, 2022
567484c
asyncify finalize_startup; make 1 diagnostics init function
lambdageek Jun 24, 2022
199cc80
(not implemented) set browser-eventpipe sample to start a DS server
lambdageek Jun 24, 2022
c3e7fe1
ping in the DS server (not functional yet)
lambdageek Jun 24, 2022
e64e539
Start diagnostic server pthread
lambdageek Jun 24, 2022
00f73ba
WIP try to start the server
lambdageek Jun 25, 2022
f803c3c
WIP diagnostic server server
lambdageek Jun 29, 2022
58851b4
Add a mock WebSocket connection to simulate the remote end
lambdageek Jul 1, 2022
c497c59
cleanup diagnostics.ts
lambdageek Jul 5, 2022
74d8443
wasm-mt: use a PThreadSelf struct instead of a raw MessagePort
lambdageek Jul 5, 2022
9ce7348
Move all the EP and diagnostic server modules to one directory
lambdageek Jul 5, 2022
e8fd671
Refactor; remove dead code; rationalize controller
lambdageek Jul 5, 2022
e8aace4
WIP more server pthread impl
lambdageek Jul 6, 2022
54e618b
WIP: start adding queue from streaming thread to DS thread
lambdageek Jul 6, 2022
c1c4c86
[wasm] Incremental build and rollup warnings cleanups
lambdageek Jul 7, 2022
2cca7f0
WIP: work on wiring up DS protocol commands (mock); resume hack
lambdageek Jul 7, 2022
0609be7
WIP: set up a WasmIpcStream, create EP sessions from DS
lambdageek Jul 8, 2022
9e00d8b
WIP: starting to stream works; needs PTHREAD_POOL_SIZE bump
lambdageek Jul 9, 2022
50e035c
cleanup browser-eventpipe sample
lambdageek Jul 10, 2022
69d0ca7
refactor to simplify and cleanup; rm duplicate code
lambdageek Jul 11, 2022
032e41e
call mono_wasm_event_pipe_early_startup_callback from event_pipe init
lambdageek Jul 11, 2022
637f88a
if diagnostics server isn't enabled, don't try to initialize it
lambdageek Jul 11, 2022
33c7d39
WIP: start parsing binary commands
lambdageek Jul 11, 2022
7c2d7a1
WIP: start wiring up binary protocol parsing to the websocket
lambdageek Jul 13, 2022
e015940
WIP: Can parse a CollectTracing2 command and attempt to create a
lambdageek Jul 14, 2022
182872c
[wasm-ep] use the new PromiseController<T>
lambdageek Jul 15, 2022
0e7a6ba
get back to the server loop quicker by queueing the parsing in the mi…
lambdageek Jul 15, 2022
6ef49c3
update mock for binary ADVR_V1 message
lambdageek Jul 15, 2022
76abc4d
sample: don't suspend, and use a mock url
lambdageek Jul 15, 2022
32dbdd9
use better values for parse results
lambdageek Jul 16, 2022
c417200
parse a few more binary protocol commands
lambdageek Jul 16, 2022
c3c9a25
wasm_ipc_stream: wire up close command
lambdageek Jul 18, 2022
07c0691
Send proper OK messages in replies to binary protocol commands
lambdageek Jul 18, 2022
ab5e247
(testing) turn off the file session for now
lambdageek Jul 18, 2022
bde9669
TODO: handle WS connection failures
lambdageek Jul 18, 2022
c4b8cc1
remove em_asm(console.log); simplify wasm EP init
lambdageek Jul 19, 2022
051e257
remove debug output
lambdageek Jul 19, 2022
c75ee2c
remove debug output in startup
lambdageek Jul 19, 2022
bca82f0
cleanup wasm ipc stream impl
lambdageek Jul 19, 2022
83e4d29
put diagnostics mocks behind a const flag
lambdageek Jul 19, 2022
7f95626
don't build wasm-specific DS if threads are disabled
lambdageek Jul 19, 2022
033b0c4
refactor and cleanup
lambdageek Jul 19, 2022
0d1eefe
help treeshaking
lambdageek Jul 19, 2022
be703f2
update DS design notes
lambdageek Jul 19, 2022
7a63e7e
remove more printfs
lambdageek Jul 19, 2022
e5d1838
use PromiseController in more places
lambdageek Jul 19, 2022
d92d45f
remove more console.debug in startup
lambdageek Jul 19, 2022
26751d6
Merge remote-tracking branch 'origin/main' into wasm-ep-on-startup
lambdageek Jul 19, 2022
530fa1c
fix Windows build
lambdageek Jul 19, 2022
ca2f204
add MONO_WASM prefix to console logging outputs
lambdageek Jul 19, 2022
d1d088a
fix sample logic when startup session is disabled
lambdageek Jul 20, 2022
e6979c1
improve debug output for DS server
lambdageek Jul 20, 2022
0bf7c0b
bugfix: don't confuse buf_addr for the value stored in it
lambdageek Jul 20, 2022
b3676b5
slight refactor of EventPipeSocketConnection and more logging
lambdageek Jul 20, 2022
2d22094
review feedback
lambdageek Jul 20, 2022
88ef693
Merge remote-tracking branch 'origin/main' into wasm-ep-on-startup
lambdageek Jul 20, 2022
b8b2148
merge fixup
lambdageek Jul 20, 2022
0de9719
fix bug in queue_push_sync main thread detection
lambdageek Jul 20, 2022
17d12e8
fix typo
lambdageek Jul 21, 2022
6abe463
Merge remote-tracking branch 'origin/main' into wasm-ep-on-startup
lambdageek Jul 22, 2022
d2f0acd
merge fixup
lambdageek Jul 22, 2022
2e135e7
fix rollup warning when making the crypto worker
lambdageek Jul 22, 2022
7da9d41
add MONO_WASM: prefix to logging
lambdageek Jul 22, 2022
f4219a6
make diagnostic server mocking friendlier
lambdageek Jul 21, 2022
18e199a
disable mocking in the sample project by default
lambdageek Jul 22, 2022
4bdbbfa
Merge remote-tracking branch 'origin/main' into wasm-ep-on-startup
lambdageek Jul 24, 2022
cbd4691
fixup after merge
lambdageek Jul 24, 2022
2fe9a3e
review feedback
lambdageek Jul 25, 2022
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
Prev Previous commit
Next Next commit
wasm-mt: use a PThreadSelf struct instead of a raw MessagePort
  • Loading branch information
lambdageek committed Jul 16, 2022
commit 74d844314cc76177dd5c2bed7b21b6532fc623ec
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/pthreads/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export type pthread_ptr = number;

export interface PThreadInfo {
readonly pthread_id: pthread_ptr;
readonly is_main_thread: boolean;
readonly isBrowserThread: boolean;
}

export const MainThread: PThreadInfo = {
get pthread_id(): pthread_ptr {
return getBrowserThreadID();
},
is_main_thread: true
isBrowserThread: true
};

let browser_thread_id_lazy: pthread_ptr | undefined;
Expand Down
29 changes: 17 additions & 12 deletions src/mono/wasm/runtime/pthreads/worker/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import MonoWasmThreads from "consts:monoWasmThreads";
import type { pthread_ptr } from "../shared";
import type { pthread_ptr, PThreadInfo, MonoThreadMessage } from "../shared";

/// Identification of the current thread executing on a worker
export interface PThreadSelf extends PThreadInfo {
readonly pthread_id: pthread_ptr;
readonly portToBrowser: MessagePort;
readonly isBrowserThread: boolean;
postMessageToBrowser: <T extends MonoThreadMessage>(message: T, transfer?: Transferable[]) => void;
addEventListenerFromBrowser: (listener: <T extends MonoThreadMessage>(event: MessageEvent<T>) => void) => void;
}

export const dotnetPthreadCreated = "dotnet:pthread:created" as const;
export const dotnetPthreadAttached = "dotnet:pthread:attached" as const;
Expand All @@ -15,8 +24,7 @@ export interface WorkerThreadEventMap {
}

export interface WorkerThreadEvent extends Event {
readonly pthread_ptr: pthread_ptr;
readonly portToMain: MessagePort;
readonly pthread_self: PThreadSelf;
}

export interface WorkerThreadEventTarget extends EventTarget {
Expand All @@ -26,18 +34,15 @@ export interface WorkerThreadEventTarget extends EventTarget {
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
}

let WorkerThreadEventClassConstructor: new (type: keyof WorkerThreadEventMap, pthread_ptr: pthread_ptr, port: MessagePort) => WorkerThreadEvent;
export const makeWorkerThreadEvent: (type: keyof WorkerThreadEventMap, pthread_ptr: pthread_ptr, port: MessagePort) => WorkerThreadEvent = !MonoWasmThreads
let WorkerThreadEventClassConstructor: new (type: keyof WorkerThreadEventMap, pthread_self: PThreadSelf) => WorkerThreadEvent;
export const makeWorkerThreadEvent: (type: keyof WorkerThreadEventMap, pthread_self: PThreadSelf) => WorkerThreadEvent = !MonoWasmThreads
? (() => { throw new Error("threads support disabled"); })
: ((type: keyof WorkerThreadEventMap, pthread_ptr: pthread_ptr, port: MessagePort) => {
: ((type: keyof WorkerThreadEventMap, pthread_self: PThreadSelf) => {
if (!WorkerThreadEventClassConstructor) WorkerThreadEventClassConstructor = class WorkerThreadEventImpl extends Event implements WorkerThreadEvent {
readonly pthread_ptr: pthread_ptr;
readonly portToMain: MessagePort;
constructor(type: keyof WorkerThreadEventMap, pthread_ptr: pthread_ptr, portToMain: MessagePort) {
constructor(type: keyof WorkerThreadEventMap, readonly pthread_self: PThreadSelf) {
super(type);
this.pthread_ptr = pthread_ptr;
this.portToMain = portToMain;
}
};
return new WorkerThreadEventClassConstructor(type, pthread_ptr, port);
return new WorkerThreadEventClassConstructor(type, pthread_self);
});

40 changes: 14 additions & 26 deletions src/mono/wasm/runtime/pthreads/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import MonoWasmThreads from "consts:monoWasmThreads";
import { Module, ENVIRONMENT_IS_PTHREAD } from "../../imports";
import { makeChannelCreatedMonoMessage, pthread_ptr } from "../shared";
import { mono_assert, is_nullish } from "../../types";
import type { MonoThreadMessage, PThreadInfo } from "../shared";
import type { MonoThreadMessage } from "../shared";
import {
PThreadSelf,
makeWorkerThreadEvent,
dotnetPthreadCreated,
dotnetPthreadAttached,
Expand All @@ -24,28 +25,18 @@ export {
WorkerThreadEventTarget,
} from "./events";

export interface PThreadSelf extends PThreadInfo {
postMessageToBrowser: <T extends MonoThreadMessage>(message: T, transfer?: Transferable[]) => void;
addEventListenerFromBrowser: (listener: <T extends MonoThreadMessage>(event: MessageEvent<T>) => void) => void;
}

class WorkerSelf implements PThreadSelf {
readonly port: MessagePort;
readonly pthread_id: pthread_ptr;
readonly is_main_thread = false;
constructor(pthread_id: pthread_ptr, port: MessagePort) {
this.port = port;
this.pthread_id = pthread_id;
}
readonly isBrowserThread = false;
constructor(readonly pthread_id: pthread_ptr, readonly portToBrowser: MessagePort) { }
postMessageToBrowser(message: MonoThreadMessage, transfer?: Transferable[]) {
if (transfer) {
this.port.postMessage(message, transfer);
this.portToBrowser.postMessage(message, transfer);
} else {
this.port.postMessage(message);
this.portToBrowser.postMessage(message);
}
}
addEventListenerFromBrowser(listener: (event: MessageEvent<MonoThreadMessage>) => void) {
this.port.addEventListener("message", listener);
this.portToBrowser.addEventListener("message", listener);
}
}

Expand All @@ -64,28 +55,25 @@ function monoDedicatedChannelMessageFromMainToWorker(event: MessageEvent<string>
console.debug("got message from main on the dedicated channel", event.data);
}

let portToMain: MessagePort | null = null;

function setupChannelToMainThread(pthread_ptr: pthread_ptr): MessagePort {
function setupChannelToMainThread(pthread_ptr: pthread_ptr): PThreadSelf {
console.debug("creating a channel", pthread_ptr);
const channel = new MessageChannel();
const workerPort = channel.port1;
const mainPort = channel.port2;
workerPort.addEventListener("message", monoDedicatedChannelMessageFromMainToWorker);
workerPort.start();
portToMain = workerPort;
pthread_self = new WorkerSelf(pthread_ptr, workerPort);
self.postMessage(makeChannelCreatedMonoMessage(pthread_ptr, mainPort), [mainPort]);
return workerPort;
return pthread_self;
}

/// This is an implementation detail function.
/// Called in the worker thread from mono when a pthread becomes attached to the mono runtime.
export function mono_wasm_pthread_on_pthread_attached(pthread_id: pthread_ptr): void {
const port = portToMain;
mono_assert(port !== null, "expected a port to the main thread");
const self = pthread_self;
mono_assert(self !== null && self.pthread_id == pthread_id, "expected pthread_self to be set already when attaching");
console.debug("attaching pthread to runtime", pthread_id);
currentWorkerThreadEvents.dispatchEvent(makeWorkerThreadEvent(dotnetPthreadAttached, pthread_id, port));
currentWorkerThreadEvents.dispatchEvent(makeWorkerThreadEvent(dotnetPthreadAttached, self));
}

/// This is an implementation detail function.
Expand All @@ -97,7 +85,7 @@ export function afterThreadInitTLS(): void {
const pthread_ptr = (<any>Module)["_pthread_self"]();
mono_assert(!is_nullish(pthread_ptr), "pthread_self() returned null");
console.debug("after thread init, pthread ptr", pthread_ptr);
const port = setupChannelToMainThread(pthread_ptr);
currentWorkerThreadEvents.dispatchEvent(makeWorkerThreadEvent(dotnetPthreadCreated, pthread_ptr, port));
const self = setupChannelToMainThread(pthread_ptr);
currentWorkerThreadEvents.dispatchEvent(makeWorkerThreadEvent(dotnetPthreadCreated, self));
}
}
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,6 @@ async function mono_wasm_pthread_worker_init(): Promise<void> {
// This is a good place for subsystems to attach listeners for pthreads_worker.currrentWorkerThreadEvents
console.debug("mono_wasm_pthread_worker_init");
pthreads_worker.currentWorkerThreadEvents.addEventListener(pthreads_worker.dotnetPthreadCreated, (ev) => {
console.debug("thread created", ev.pthread_ptr);
console.debug("thread created", ev.pthread_self.pthread_id);
});
}