Skip to content

Commit d24da95

Browse files
joyeecheungantsmartian
authored andcommitted
src: move v8_platform implementation into node_v8_platform-inl.h
So that the v8_platform global variable can be referenced in other files. PR-URL: nodejs#25541 Reviewed-By: Gus Caplan <me@gus.host>
1 parent 76687de commit d24da95

7 files changed

+201
-185
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@
487487
'src/node_union_bytes.h',
488488
'src/node_url.h',
489489
'src/node_version.h',
490+
'src/node_v8_platform-inl.h',
490491
'src/node_watchdog.h',
491492
'src/node_worker.h',
492493
'src/pipe_wrap.h',

src/env.cc

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "node_options-inl.h"
99
#include "node_platform.h"
1010
#include "node_process.h"
11+
#include "node_v8_platform-inl.h"
1112
#include "node_worker.h"
1213
#include "tracing/agent.h"
1314
#include "tracing/traced_value.h"

src/inspector/tracing_agent.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "tracing_agent.h"
22
#include "main_thread_interface.h"
33
#include "node_internals.h"
4+
#include "node_v8_platform-inl.h"
45

56
#include "env-inl.h"
67
#include "v8.h"

src/node.cc

+17-179
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
#include "node_platform.h"
3333
#include "node_process.h"
3434
#include "node_revert.h"
35+
#include "node_v8_platform-inl.h"
3536
#include "node_version.h"
36-
#include "tracing/traced_value.h"
3737

3838
#if HAVE_OPENSSL
3939
#include "node_crypto.h"
@@ -56,8 +56,6 @@
5656
#include "handle_wrap.h"
5757
#include "req_wrap-inl.h"
5858
#include "string_bytes.h"
59-
#include "tracing/agent.h"
60-
#include "tracing/node_trace_writer.h"
6159
#include "util.h"
6260
#include "uv.h"
6361
#if NODE_USE_V8_PLATFORM
@@ -163,169 +161,10 @@ bool v8_initialized = false;
163161
// node_internals.h
164162
// process-relative uptime base, initialized at start-up
165163
double prog_start_time;
166-
} // namespace per_process
167-
168-
// Ensures that __metadata trace events are only emitted
169-
// when tracing is enabled.
170-
class NodeTraceStateObserver :
171-
public TracingController::TraceStateObserver {
172-
public:
173-
void OnTraceEnabled() override {
174-
char name_buffer[512];
175-
if (uv_get_process_title(name_buffer, sizeof(name_buffer)) == 0) {
176-
// Only emit the metadata event if the title can be retrieved
177-
// successfully. Ignore it otherwise.
178-
TRACE_EVENT_METADATA1("__metadata", "process_name",
179-
"name", TRACE_STR_COPY(name_buffer));
180-
}
181-
TRACE_EVENT_METADATA1("__metadata",
182-
"version",
183-
"node",
184-
per_process::metadata.versions.node.c_str());
185-
TRACE_EVENT_METADATA1("__metadata", "thread_name",
186-
"name", "JavaScriptMainThread");
187-
188-
auto trace_process = tracing::TracedValue::Create();
189-
trace_process->BeginDictionary("versions");
190-
191-
#define V(key) \
192-
trace_process->SetString(#key, per_process::metadata.versions.key.c_str());
193-
194-
NODE_VERSIONS_KEYS(V)
195-
#undef V
196-
197-
trace_process->EndDictionary();
198-
199-
trace_process->SetString("arch", per_process::metadata.arch.c_str());
200-
trace_process->SetString("platform",
201-
per_process::metadata.platform.c_str());
202-
203-
trace_process->BeginDictionary("release");
204-
trace_process->SetString("name",
205-
per_process::metadata.release.name.c_str());
206-
#if NODE_VERSION_IS_LTS
207-
trace_process->SetString("lts", per_process::metadata.release.lts.c_str());
208-
#endif
209-
trace_process->EndDictionary();
210-
TRACE_EVENT_METADATA1("__metadata", "node",
211-
"process", std::move(trace_process));
212-
213-
// This only runs the first time tracing is enabled
214-
controller_->RemoveTraceStateObserver(this);
215-
}
216-
217-
void OnTraceDisabled() override {
218-
// Do nothing here. This should never be called because the
219-
// observer removes itself when OnTraceEnabled() is called.
220-
UNREACHABLE();
221-
}
222-
223-
explicit NodeTraceStateObserver(TracingController* controller) :
224-
controller_(controller) {}
225-
~NodeTraceStateObserver() override {}
226-
227-
private:
228-
TracingController* controller_;
229-
};
230-
231-
static struct {
232-
#if NODE_USE_V8_PLATFORM
233-
void Initialize(int thread_pool_size) {
234-
tracing_agent_.reset(new tracing::Agent());
235-
node::tracing::TraceEventHelper::SetAgent(tracing_agent_.get());
236-
node::tracing::TracingController* controller =
237-
tracing_agent_->GetTracingController();
238-
trace_state_observer_.reset(new NodeTraceStateObserver(controller));
239-
controller->AddTraceStateObserver(trace_state_observer_.get());
240-
StartTracingAgent();
241-
// Tracing must be initialized before platform threads are created.
242-
platform_ = new NodePlatform(thread_pool_size, controller);
243-
V8::InitializePlatform(platform_);
244-
}
245-
246-
void Dispose() {
247-
StopTracingAgent();
248-
platform_->Shutdown();
249-
delete platform_;
250-
platform_ = nullptr;
251-
// Destroy tracing after the platform (and platform threads) have been
252-
// stopped.
253-
tracing_agent_.reset(nullptr);
254-
trace_state_observer_.reset(nullptr);
255-
}
256-
257-
void DrainVMTasks(Isolate* isolate) {
258-
platform_->DrainTasks(isolate);
259-
}
260164

261-
void CancelVMTasks(Isolate* isolate) {
262-
platform_->CancelPendingDelayedTasks(isolate);
263-
}
264-
265-
void StartTracingAgent() {
266-
if (per_process::cli_options->trace_event_categories.empty()) {
267-
tracing_file_writer_ = tracing_agent_->DefaultHandle();
268-
} else {
269-
std::vector<std::string> categories =
270-
SplitString(per_process::cli_options->trace_event_categories, ',');
271-
272-
tracing_file_writer_ = tracing_agent_->AddClient(
273-
std::set<std::string>(std::make_move_iterator(categories.begin()),
274-
std::make_move_iterator(categories.end())),
275-
std::unique_ptr<tracing::AsyncTraceWriter>(
276-
new tracing::NodeTraceWriter(
277-
per_process::cli_options->trace_event_file_pattern)),
278-
tracing::Agent::kUseDefaultCategories);
279-
}
280-
}
281-
282-
void StopTracingAgent() {
283-
tracing_file_writer_.reset();
284-
}
285-
286-
tracing::AgentWriterHandle* GetTracingAgentWriter() {
287-
return &tracing_file_writer_;
288-
}
289-
290-
NodePlatform* Platform() {
291-
return platform_;
292-
}
293-
294-
std::unique_ptr<NodeTraceStateObserver> trace_state_observer_;
295-
std::unique_ptr<tracing::Agent> tracing_agent_;
296-
tracing::AgentWriterHandle tracing_file_writer_;
297-
NodePlatform* platform_;
298-
#else // !NODE_USE_V8_PLATFORM
299-
void Initialize(int thread_pool_size) {}
300-
void Dispose() {}
301-
void DrainVMTasks(Isolate* isolate) {}
302-
void CancelVMTasks(Isolate* isolate) {}
303-
304-
void StartTracingAgent() {
305-
if (!trace_enabled_categories.empty()) {
306-
fprintf(stderr, "Node compiled with NODE_USE_V8_PLATFORM=0, "
307-
"so event tracing is not available.\n");
308-
}
309-
}
310-
void StopTracingAgent() {}
311-
312-
tracing::AgentWriterHandle* GetTracingAgentWriter() {
313-
return nullptr;
314-
}
315-
316-
NodePlatform* Platform() {
317-
return nullptr;
318-
}
319-
#endif // !NODE_USE_V8_PLATFORM
320-
} v8_platform;
321-
322-
tracing::AgentWriterHandle* GetTracingAgentWriter() {
323-
return v8_platform.GetTracingAgentWriter();
324-
}
325-
326-
void DisposePlatform() {
327-
v8_platform.Dispose();
328-
}
165+
// node_v8_platform-inl.h
166+
struct V8Platform v8_platform;
167+
} // namespace per_process
329168

330169
#ifdef __POSIX__
331170
static const unsigned kMaxSignal = 32;
@@ -1188,7 +1027,7 @@ Environment* GetCurrentEnvironment(Local<Context> context) {
11881027

11891028

11901029
MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform() {
1191-
return v8_platform.Platform();
1030+
return per_process::v8_platform.Platform();
11921031
}
11931032

11941033

@@ -1200,8 +1039,8 @@ MultiIsolatePlatform* CreatePlatform(
12001039

12011040

12021041
MultiIsolatePlatform* InitializeV8Platform(int thread_pool_size) {
1203-
v8_platform.Initialize(thread_pool_size);
1204-
return v8_platform.Platform();
1042+
per_process::v8_platform.Initialize(thread_pool_size);
1043+
return per_process::v8_platform.Platform();
12051044
}
12061045

12071046

@@ -1282,7 +1121,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
12821121
do {
12831122
uv_run(env.event_loop(), UV_RUN_DEFAULT);
12841123

1285-
v8_platform.DrainVMTasks(isolate);
1124+
per_process::v8_platform.DrainVMTasks(isolate);
12861125

12871126
more = uv_loop_alive(env.event_loop());
12881127
if (more)
@@ -1310,8 +1149,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
13101149
env.RunCleanup();
13111150
RunAtExit(&env);
13121151

1313-
v8_platform.DrainVMTasks(isolate);
1314-
v8_platform.CancelVMTasks(isolate);
1152+
per_process::v8_platform.DrainVMTasks(isolate);
1153+
per_process::v8_platform.CancelVMTasks(isolate);
13151154
#if defined(LEAK_SANITIZER)
13161155
__lsan_do_leak_check();
13171156
#endif
@@ -1339,7 +1178,7 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
13391178

13401179
// Register the isolate on the platform before the isolate gets initialized,
13411180
// so that the isolate can access the platform during initialization.
1342-
v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
1181+
per_process::v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
13431182
Isolate::Initialize(isolate, params);
13441183

13451184
isolate->AddMessageListenerWithErrorLevel(OnMessage,
@@ -1385,11 +1224,10 @@ inline int Start(uv_loop_t* event_loop,
13851224
Isolate::Scope isolate_scope(isolate);
13861225
HandleScope handle_scope(isolate);
13871226
std::unique_ptr<IsolateData, decltype(&FreeIsolateData)> isolate_data(
1388-
CreateIsolateData(
1389-
isolate,
1390-
event_loop,
1391-
v8_platform.Platform(),
1392-
allocator.get()),
1227+
CreateIsolateData(isolate,
1228+
event_loop,
1229+
per_process::v8_platform.Platform(),
1230+
allocator.get()),
13931231
&FreeIsolateData);
13941232
// TODO(addaleax): This should load a real per-Isolate option, currently
13951233
// this is still effectively per-process.
@@ -1407,7 +1245,7 @@ inline int Start(uv_loop_t* event_loop,
14071245
}
14081246

14091247
isolate->Dispose();
1410-
v8_platform.Platform()->UnregisterIsolate(isolate);
1248+
per_process::v8_platform.Platform()->UnregisterIsolate(isolate);
14111249

14121250
return exit_code;
14131251
}
@@ -1472,7 +1310,7 @@ int Start(int argc, char** argv) {
14721310
// that happen to terminate during shutdown from being run unsafely.
14731311
// Since uv_run cannot be called, uv_async handles held by the platform
14741312
// will never be fully cleaned up.
1475-
v8_platform.Dispose();
1313+
per_process::v8_platform.Dispose();
14761314

14771315
return exit_code;
14781316
}

src/node_internals.h

-3
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@ int ThreadPoolWork::CancelWork() {
244244
return uv_cancel(reinterpret_cast<uv_req_t*>(&work_req_));
245245
}
246246

247-
tracing::AgentWriterHandle* GetTracingAgentWriter();
248-
void DisposePlatform();
249-
250247
#define TRACING_CATEGORY_NODE "node"
251248
#define TRACING_CATEGORY_NODE1(one) \
252249
TRACING_CATEGORY_NODE "," \

src/node_trace_events.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#include "base_object-inl.h"
2+
#include "env.h"
13
#include "node.h"
2-
#include "node_internals.h"
4+
#include "node_v8_platform-inl.h"
35
#include "tracing/agent.h"
4-
#include "env.h"
5-
#include "base_object-inl.h"
66

77
#include <set>
88
#include <string>

0 commit comments

Comments
 (0)