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

worker: set up child Isolate inside Worker thread #26011

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ Environment::Environment(IsolateData* isolate_data,
trace_category_state_(isolate_, kTraceCategoryCount),
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
flags_(flags),
thread_id_(thread_id == static_cast<uint64_t>(-1) ?
AllocateThreadId() : thread_id),
thread_id_(thread_id == kNoThreadId ? AllocateThreadId() : thread_id),
fs_stats_field_array_(isolate_, kFsStatsBufferLength),
fs_stats_field_bigint_array_(isolate_, kFsStatsBufferLength),
context_(context->GetIsolate(), context) {
Expand Down
3 changes: 2 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ class Environment {
Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context,
Flags flags = Flags(),
uint64_t thread_id = static_cast<uint64_t>(-1));
uint64_t thread_id = kNoThreadId);
~Environment();

void Start(bool start_profiler_idle_notifier);
Expand Down Expand Up @@ -771,6 +771,7 @@ class Environment {
inline void set_has_run_bootstrapping_code(bool has_run_bootstrapping_code);

static uint64_t AllocateThreadId();
static constexpr uint64_t kNoThreadId = -1;

inline bool is_main_thread() const;
inline bool owns_process_state() const;
Expand Down
6 changes: 3 additions & 3 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class WorkerThreadData {
}

private:
Worker* w_;
Worker* const w_;
uv_loop_t loop_;
DeleteFnPtr<ArrayBufferAllocator, FreeArrayBufferAllocator>
array_buffer_allocator_;
Expand Down Expand Up @@ -181,7 +181,7 @@ void Worker::Run() {
bool inspector_started = false;

DeleteFnPtr<Environment, FreeEnvironment> env_;
OnScopeLeave cleanup_env([&]{
OnScopeLeave cleanup_env([&]() {
if (!env_) return;
env_->set_can_call_into_js(false);
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate_,
Expand All @@ -204,7 +204,7 @@ void Worker::Run() {
RunAtExit(env_.get());
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
if (inspector_started)
WaitForWorkerInspectorToStop(env_.get());
WaitForWorkerInspectorToStop(env_.get());
addaleax marked this conversation as resolved.
Show resolved Hide resolved
#endif

{
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-worker-prof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('assert');
const { spawnSync } = require('child_process');
const { Worker } = require('worker_threads');

if (!common.isMainThread)
common.skip('process.chdir is not available in Workers');

// Test that --prof also tracks Worker threads.
// Refs: https://github.com/nodejs/node/issues/24016

if (process.argv[2] === 'child') {
const spin = `
const start = Date.now();
while (Date.now() - start < 200);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
`;
new Worker(spin, { eval: true });
eval(spin);
return;
}

tmpdir.refresh();
process.chdir(tmpdir.path);
spawnSync(process.execPath, ['--prof', __filename, 'child']);
const logfiles = fs.readdirSync('.').filter((name) => /\.log$/.test(name));
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.

for (const logfile of logfiles) {
const lines = fs.readFileSync(logfile, 'utf8').split('\n');
const ticks = lines.filter((line) => /^tick,/.test(line)).length;

// Test that at least 20 ticks have been recorded for both parent and child
// threads. When not tracking Worker threads, only 1 or 2 ticks would
// have been recorded.
// When running locally on x64 Linux, this number is usually at least 150
// for both threads, so 15 seems like a very safe threshold.
assert(ticks >= 15, `${ticks} >= 15`);
}