Skip to content

Commit e8e9829

Browse files
committed
Revert "EW-8447 Fix CPU profiling"
This reverts commit 19c8ed5.
1 parent 187ef2a commit e8e9829

4 files changed

Lines changed: 12 additions & 100 deletions

File tree

src/workerd/io/worker.c++

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,11 +2339,8 @@ struct MessageQueue {
23392339

23402340
class Worker::Isolate::InspectorChannelImpl final: public v8_inspector::V8Inspector::Channel {
23412341
public:
2342-
InspectorChannelImpl(kj::Own<const Worker::Isolate> isolateParam,
2343-
ExecutorNotifierPair isolateThreadExecutorNotifierPair,
2344-
kj::WebSocket& webSocket)
2345-
: ioHandler(kj::mv(isolateThreadExecutorNotifierPair), webSocket),
2346-
state(kj::heap<State>(this, kj::mv(isolateParam))) {
2342+
InspectorChannelImpl(kj::Own<const Worker::Isolate> isolateParam, kj::WebSocket& webSocket)
2343+
: ioHandler(webSocket), state(kj::heap<State>(this, kj::mv(isolateParam))) {
23472344
ioHandler.connect(*this);
23482345
}
23492346

@@ -2596,12 +2593,11 @@ private:
25962593
// the InspectorChannelImpl and the InspectorClient.
25972594
class WebSocketIoHandler final {
25982595
public:
2599-
WebSocketIoHandler(ExecutorNotifierPair isolateThreadExecutorNotifierPair, kj::WebSocket& webSocket)
2600-
: isolateThreadExecutor(kj::mv(isolateThreadExecutorNotifierPair.executor)),
2601-
incomingQueueNotifier(kj::mv(isolateThreadExecutorNotifierPair.notifier)),
2602-
webSocket(webSocket) {
2596+
WebSocketIoHandler(kj::WebSocket& webSocket)
2597+
: webSocket(webSocket) {
26032598
// Assume we are being instantiated on the InspectorService thread, the thread that will do
26042599
// I/O for CDP messages. Messages are delivered to the InspectorChannelImpl on the Isolate thread.
2600+
incomingQueueNotifier = XThreadNotifier::create();
26052601
outgoingQueueNotifier = XThreadNotifier::create();
26062602
}
26072603

@@ -2634,20 +2630,7 @@ private:
26342630
// Message pumping promise that should be evaluated on the InspectorService
26352631
// thread.
26362632
kj::Promise<void> messagePump() {
2637-
// Although inspector I/O must happen on the InspectorService thread (to make sure breakpoints
2638-
// don't block inspector I/O), inspector messages must be actually dispatched on the Isolate
2639-
// thread. So, we run the dispatch loop on the Isolate thread.
2640-
//
2641-
// Note that the above comment is only really accurate in vanilla workerd. In the case of the
2642-
// internal Cloudflare Workers runtime, `isolateThreadExecutor` may actually refer to the
2643-
// current thread's `kj::Executor`. That's fine; calling `executeAsync()` on the current
2644-
// thread's executor just posts the task to the event loop, and everything works as expected.
2645-
auto dispatchLoopPromise = isolateThreadExecutor->executeAsync([this]() {
2646-
return dispatchLoop();
2647-
});
2648-
return receiveLoop()
2649-
.exclusiveJoin(kj::mv(dispatchLoopPromise))
2650-
.exclusiveJoin(transmitLoop());
2633+
return receiveLoop().exclusiveJoin(dispatchLoop()).exclusiveJoin(transmitLoop());
26512634
}
26522635

26532636
void send(kj::String message) {
@@ -2688,7 +2671,6 @@ private:
26882671
outgoingQueueNotifier->notify();
26892672
}
26902673

2691-
// Must be called on the InspectorService thread.
26922674
kj::Promise<void> receiveLoop() {
26932675
for (;;) {
26942676
auto message = co_await webSocket.receive(MAX_MESSAGE_SIZE);
@@ -2711,7 +2693,6 @@ private:
27112693
}
27122694
}
27132695

2714-
// Must be called on the Isolate thread.
27152696
kj::Promise<void> dispatchLoop() {
27162697
for (;;) {
27172698
co_await incomingQueueNotifier->awaitNotification();
@@ -2721,7 +2702,6 @@ private:
27212702
}
27222703
}
27232704

2724-
// Must be called on the InspectorService thread.
27252705
kj::Promise<void> transmitLoop() {
27262706
for (;;) {
27272707
co_await outgoingQueueNotifier->awaitNotification();
@@ -2748,17 +2728,10 @@ private:
27482728
}
27492729
}
27502730

2751-
// We need access to the Isolate thread's kj::Executor to run the inspector dispatch loop. This
2752-
// doesn't actually have to be an Own, because the Isolate thread will destroy the Isolate
2753-
// before it exits, but it doesn't hurt.
2754-
kj::Own<const kj::Executor> isolateThreadExecutor;
2755-
27562731
kj::MutexGuarded<MessageQueue> incomingQueue;
2757-
// This XThreadNotifier must be created on the Isolate thread.
27582732
kj::Own<XThreadNotifier> incomingQueueNotifier;
27592733

27602734
kj::MutexGuarded<MessageQueue> outgoingQueue;
2761-
// This XThreadNotifier must be created on the InspectorService thread.
27622735
kj::Own<XThreadNotifier> outgoingQueueNotifier;
27632736

27642737
kj::WebSocket& webSocket; // only accessed on the InspectorService thread.
@@ -2908,17 +2881,11 @@ kj::Promise<void> Worker::Isolate::attachInspector(
29082881
headers.set(controlHeaderId, "{\"ewLog\":{\"status\":\"ok\"}}");
29092882
auto webSocket = response.acceptWebSocket(headers);
29102883

2911-
// This `attachInspector()` overload is used by the internal Cloudflare Workers runtime, which has
2912-
// no concept of a single Isolate thread. Instead, it's okay for all inspector messages to be
2913-
// dispatched on the calling thread.
2914-
auto executorNotifierPair = ExecutorNotifierPair{};
2915-
2916-
return attachInspector(kj::mv(executorNotifierPair), timer, timerOffset, *webSocket)
2884+
return attachInspector(timer, timerOffset, *webSocket)
29172885
.attach(kj::mv(webSocket));
29182886
}
29192887

29202888
kj::Promise<void> Worker::Isolate::attachInspector(
2921-
ExecutorNotifierPair isolateThreadExecutorNotifierPair,
29222889
kj::Timer& timer,
29232890
kj::Duration timerOffset,
29242891
kj::WebSocket& webSocket) const {
@@ -2939,10 +2906,7 @@ kj::Promise<void> Worker::Isolate::attachInspector(
29392906

29402907
lockedSelf.impl->inspectorClient.setInspectorTimerInfo(timer, timerOffset);
29412908

2942-
auto channel = kj::heap<Worker::Isolate::InspectorChannelImpl>(
2943-
kj::atomicAddRef(*this),
2944-
kj::mv(isolateThreadExecutorNotifierPair),
2945-
webSocket);
2909+
auto channel = kj::heap<Worker::Isolate::InspectorChannelImpl>(kj::atomicAddRef(*this), webSocket);
29462910
lockedSelf.currentInspectorSession = *channel;
29472911
lockedSelf.impl->inspectorClient.setChannel(*channel);
29482912

src/workerd/io/worker.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,31 +288,14 @@ class Worker::Isolate: public kj::AtomicRefcounted {
288288
uint getLockSuccessCount() const;
289289

290290
// Accepts a connection to the V8 inspector and handles requests until the client disconnects.
291-
// Also adds a special JSON value to the header identified by `controlHeaderId`, for compatibility
292-
// with internal Cloudflare systems.
293-
//
294-
// This overload will dispatch all inspector messages on the _calling thread's_ `kj::Executor`.
295-
// When linked against vanilla V8, this means that CPU profiling will only profile JavaScript
296-
// running on the _calling thread_, which will most likely only be inspector console commands, and
297-
// is not typically desired.
298-
//
299-
// For the above reason , this overload is curently only suitable for use by the internal Workers
300-
// Runtime codebase, which patches V8 to profile whichever thread currently holds the `v8::Locker`
301-
// for this Isolate.
302291
kj::Promise<void> attachInspector(
303292
kj::Timer& timer,
304293
kj::Duration timerOffset,
305294
kj::HttpService::Response& response,
306295
const kj::HttpHeaderTable& headerTable,
307296
kj::HttpHeaderId controlHeaderId) const;
308297

309-
// Accepts a connection to the V8 inspector and handles requests until the client disconnects.
310-
//
311-
// This overload will dispatch all inspector messages on the `kj::Executor` passed in via
312-
// `isolateThreadExecutorNotifierPair`. For CPU profiling to work as expected, this `kj::Executor`
313-
// must be associated with the same thread which executes the Worker's JavaScript.
314298
kj::Promise<void> attachInspector(
315-
ExecutorNotifierPair isolateThreadExecutorNotifierPair,
316299
kj::Timer& timer,
317300
kj::Duration timerOffset,
318301
kj::WebSocket& webSocket) const;

src/workerd/server/server.c++

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,12 +1165,10 @@ private:
11651165
class Server::InspectorService final: public kj::HttpService, public kj::HttpServerErrorHandler {
11661166
public:
11671167
InspectorService(
1168-
ExecutorNotifierPair isolateThreadExecutorNotifierPair,
11691168
kj::Timer& timer,
11701169
kj::HttpHeaderTable::Builder& headerTableBuilder,
11711170
InspectorServiceIsolateRegistrar& registrar)
1172-
: isolateThreadExecutorNotifierPair(kj::mv(isolateThreadExecutorNotifierPair)),
1173-
timer(timer),
1171+
: timer(timer),
11741172
headerTable(headerTableBuilder.getFutureTable()),
11751173
server(timer, headerTable, *this, kj::HttpServerSettings {
11761174
.errorHandler = *this
@@ -1228,8 +1226,7 @@ public:
12281226
auto webSocket = response.acceptWebSocket(responseHeaders);
12291227
kj::Duration timerOffset = 0 * kj::MILLISECONDS;
12301228
try {
1231-
co_return co_await ref->attachInspector(
1232-
isolateThreadExecutorNotifierPair.clone(), timer, timerOffset, *webSocket);
1229+
co_return co_await ref->attachInspector(timer, timerOffset, *webSocket);
12331230
} catch (...) {
12341231
auto exception = kj::getCaughtExceptionAsKj();
12351232
if (exception.getType() == kj::Exception::Type::DISCONNECTED) {
@@ -1347,7 +1344,6 @@ public:
13471344
}
13481345

13491346
private:
1350-
ExecutorNotifierPair isolateThreadExecutorNotifierPair;
13511347
kj::Timer& timer;
13521348
kj::HttpHeaderTable& headerTable;
13531349
kj::HashMap<kj::String, kj::Own<const Worker::Isolate::WeakIsolateRef>> isolates;
@@ -3441,30 +3437,14 @@ uint startInspector(kj::StringPtr inspectorAddress,
34413437
static constexpr uint DEFAULT_PORT = 9229;
34423438
kj::MutexGuarded<uint> inspectorPort(UNASSIGNED_PORT);
34433439

3444-
// `startInspector()` is called on the Isolate thread. V8 requires CPU profiling to be started and
3445-
// stopped on the same thread which executes JavaScript -- that is, the Isolate thread -- which
3446-
// means we need to dispatch inspector messages on this thread. To help make that happen, we
3447-
// capture this thread's kj::Executor and create an XThreadNotifier tied to this thread here, and
3448-
// pass it into the InspectorService below. Later, when the InspectorService receives a WebSocket
3449-
// connection, it calls `Isolate::attachInspector()`, which starts a dispatch loop on the
3450-
// kj::Executor we create here. The InspectorService reads subsequent WebSocket inspector messages
3451-
// and feeds them to that dispatch loop via the XThreadNotifier we create here.
3452-
auto isolateThreadExecutorNotifierPair = ExecutorNotifierPair{};
3453-
3454-
// Start the InspectorService thread.
3455-
kj::Thread thread([inspectorAddress, &inspectorPort, &registrar,
3456-
isolateThreadExecutorNotifierPair = kj::mv(isolateThreadExecutorNotifierPair)]() mutable {
3440+
kj::Thread thread([inspectorAddress, &inspectorPort, &registrar](){
34573441
kj::AsyncIoContext io = kj::setupAsyncIo();
34583442

34593443
kj::HttpHeaderTable::Builder headerTableBuilder;
34603444

34613445
// Create the special inspector service.
34623446
auto inspectorService(
3463-
kj::heap<Server::InspectorService>(
3464-
kj::mv(isolateThreadExecutorNotifierPair),
3465-
io.provider->getTimer(),
3466-
headerTableBuilder,
3467-
registrar));
3447+
kj::heap<Server::InspectorService>(io.provider->getTimer(), headerTableBuilder, registrar));
34683448
auto ownHeaderTable = headerTableBuilder.build();
34693449

34703450
// Configure and start the inspector socket.

src/workerd/util/xthreadnotifier.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,4 @@ class XThreadNotifier final: public kj::AtomicRefcounted {
4242
kj::MutexGuarded<kj::PromiseCrossThreadFulfillerPair<void>> paf;
4343
};
4444

45-
46-
// Convenience struct for creating and passing around a kj::Executor and XThreadNotifier. The
47-
// default constructor creates a pair of the objects which are both tied to the current thread.
48-
struct ExecutorNotifierPair {
49-
kj::Own<const kj::Executor> executor = kj::getCurrentThreadExecutor().addRef();
50-
kj::Own<XThreadNotifier> notifier = XThreadNotifier::create();
51-
52-
ExecutorNotifierPair clone() {
53-
return {
54-
.executor = executor->addRef(),
55-
.notifier = kj::atomicAddRef(*notifier),
56-
};
57-
}
58-
};
59-
6045
} // namespace workerd

0 commit comments

Comments
 (0)