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"
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
@@ -165,169 +163,10 @@ bool v8_initialized = false;
165163// node_internals.h
166164// process-relative uptime base, initialized at start-up
167165double prog_start_time;
168- } // namespace per_process
169-
170- // Ensures that __metadata trace events are only emitted
171- // when tracing is enabled.
172- class NodeTraceStateObserver :
173- public TracingController::TraceStateObserver {
174- public:
175- void OnTraceEnabled () override {
176- char name_buffer[512 ];
177- if (uv_get_process_title (name_buffer, sizeof (name_buffer)) == 0 ) {
178- // Only emit the metadata event if the title can be retrieved
179- // successfully. Ignore it otherwise.
180- TRACE_EVENT_METADATA1 (" __metadata" , " process_name" ,
181- " name" , TRACE_STR_COPY (name_buffer));
182- }
183- TRACE_EVENT_METADATA1 (" __metadata" ,
184- " version" ,
185- " node" ,
186- per_process::metadata.versions .node .c_str ());
187- TRACE_EVENT_METADATA1 (" __metadata" , " thread_name" ,
188- " name" , " JavaScriptMainThread" );
189-
190- auto trace_process = tracing::TracedValue::Create ();
191- trace_process->BeginDictionary (" versions" );
192-
193- #define V (key ) \
194- trace_process->SetString (#key, per_process::metadata.versions .key .c_str ());
195-
196- NODE_VERSIONS_KEYS (V)
197- #undef V
198-
199- trace_process->EndDictionary ();
200-
201- trace_process->SetString (" arch" , per_process::metadata.arch .c_str ());
202- trace_process->SetString (" platform" ,
203- per_process::metadata.platform .c_str ());
204-
205- trace_process->BeginDictionary (" release" );
206- trace_process->SetString (" name" ,
207- per_process::metadata.release .name .c_str ());
208- #if NODE_VERSION_IS_LTS
209- trace_process->SetString (" lts" , per_process::metadata.release .lts .c_str ());
210- #endif
211- trace_process->EndDictionary ();
212- TRACE_EVENT_METADATA1 (" __metadata" , " node" ,
213- " process" , std::move (trace_process));
214-
215- // This only runs the first time tracing is enabled
216- controller_->RemoveTraceStateObserver (this );
217- }
218-
219- void OnTraceDisabled () override {
220- // Do nothing here. This should never be called because the
221- // observer removes itself when OnTraceEnabled() is called.
222- UNREACHABLE ();
223- }
224-
225- explicit NodeTraceStateObserver (TracingController* controller) :
226- controller_(controller) {}
227- ~NodeTraceStateObserver () override {}
228-
229- private:
230- TracingController* controller_;
231- };
232-
233- static struct {
234- #if NODE_USE_V8_PLATFORM
235- void Initialize (int thread_pool_size) {
236- tracing_agent_.reset (new tracing::Agent ());
237- node::tracing::TraceEventHelper::SetAgent (tracing_agent_.get ());
238- node::tracing::TracingController* controller =
239- tracing_agent_->GetTracingController ();
240- trace_state_observer_.reset (new NodeTraceStateObserver (controller));
241- controller->AddTraceStateObserver (trace_state_observer_.get ());
242- StartTracingAgent ();
243- // Tracing must be initialized before platform threads are created.
244- platform_ = new NodePlatform (thread_pool_size, controller);
245- V8::InitializePlatform (platform_);
246- }
247-
248- void Dispose () {
249- StopTracingAgent ();
250- platform_->Shutdown ();
251- delete platform_;
252- platform_ = nullptr ;
253- // Destroy tracing after the platform (and platform threads) have been
254- // stopped.
255- tracing_agent_.reset (nullptr );
256- trace_state_observer_.reset (nullptr );
257- }
258-
259- void DrainVMTasks (Isolate* isolate) {
260- platform_->DrainTasks (isolate);
261- }
262166
263- void CancelVMTasks (Isolate* isolate) {
264- platform_->CancelPendingDelayedTasks (isolate);
265- }
266-
267- void StartTracingAgent () {
268- if (per_process::cli_options->trace_event_categories .empty ()) {
269- tracing_file_writer_ = tracing_agent_->DefaultHandle ();
270- } else {
271- std::vector<std::string> categories =
272- SplitString (per_process::cli_options->trace_event_categories , ' ,' );
273-
274- tracing_file_writer_ = tracing_agent_->AddClient (
275- std::set<std::string>(std::make_move_iterator (categories.begin ()),
276- std::make_move_iterator (categories.end ())),
277- std::unique_ptr<tracing::AsyncTraceWriter>(
278- new tracing::NodeTraceWriter (
279- per_process::cli_options->trace_event_file_pattern )),
280- tracing::Agent::kUseDefaultCategories );
281- }
282- }
283-
284- void StopTracingAgent () {
285- tracing_file_writer_.reset ();
286- }
287-
288- tracing::AgentWriterHandle* GetTracingAgentWriter () {
289- return &tracing_file_writer_;
290- }
291-
292- NodePlatform* Platform () {
293- return platform_;
294- }
295-
296- std::unique_ptr<NodeTraceStateObserver> trace_state_observer_;
297- std::unique_ptr<tracing::Agent> tracing_agent_;
298- tracing::AgentWriterHandle tracing_file_writer_;
299- NodePlatform* platform_;
300- #else // !NODE_USE_V8_PLATFORM
301- void Initialize (int thread_pool_size) {}
302- void Dispose () {}
303- void DrainVMTasks (Isolate* isolate) {}
304- void CancelVMTasks (Isolate* isolate) {}
305-
306- void StartTracingAgent () {
307- if (!trace_enabled_categories.empty ()) {
308- fprintf (stderr, " Node compiled with NODE_USE_V8_PLATFORM=0, "
309- " so event tracing is not available.\n " );
310- }
311- }
312- void StopTracingAgent () {}
313-
314- tracing::AgentWriterHandle* GetTracingAgentWriter () {
315- return nullptr ;
316- }
317-
318- NodePlatform* Platform () {
319- return nullptr ;
320- }
321- #endif // !NODE_USE_V8_PLATFORM
322- } v8_platform;
323-
324- tracing::AgentWriterHandle* GetTracingAgentWriter () {
325- return v8_platform.GetTracingAgentWriter ();
326- }
327-
328- void DisposePlatform () {
329- v8_platform.Dispose ();
330- }
167+ // node_v8_platform-inl.h
168+ struct V8Platform v8_platform;
169+ } // namespace per_process
331170
332171#ifdef __POSIX__
333172static const unsigned kMaxSignal = 32 ;
@@ -1258,7 +1097,7 @@ Environment* GetCurrentEnvironment(Local<Context> context) {
12581097
12591098
12601099MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform () {
1261- return v8_platform.Platform ();
1100+ return per_process:: v8_platform.Platform ();
12621101}
12631102
12641103
@@ -1270,8 +1109,8 @@ MultiIsolatePlatform* CreatePlatform(
12701109
12711110
12721111MultiIsolatePlatform* InitializeV8Platform (int thread_pool_size) {
1273- v8_platform.Initialize (thread_pool_size);
1274- return v8_platform.Platform ();
1112+ per_process:: v8_platform.Initialize (thread_pool_size);
1113+ return per_process:: v8_platform.Platform ();
12751114}
12761115
12771116
@@ -1359,7 +1198,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
13591198 do {
13601199 uv_run (env.event_loop (), UV_RUN_DEFAULT);
13611200
1362- v8_platform.DrainVMTasks (isolate);
1201+ per_process:: v8_platform.DrainVMTasks (isolate);
13631202
13641203 more = uv_loop_alive (env.event_loop ());
13651204 if (more)
@@ -1387,8 +1226,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
13871226 env.RunCleanup ();
13881227 RunAtExit (&env);
13891228
1390- v8_platform.DrainVMTasks (isolate);
1391- v8_platform.CancelVMTasks (isolate);
1229+ per_process:: v8_platform.DrainVMTasks (isolate);
1230+ per_process:: v8_platform.CancelVMTasks (isolate);
13921231#if defined(LEAK_SANITIZER)
13931232 __lsan_do_leak_check ();
13941233#endif
@@ -1416,7 +1255,7 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
14161255
14171256 // Register the isolate on the platform before the isolate gets initialized,
14181257 // so that the isolate can access the platform during initialization.
1419- v8_platform.Platform ()->RegisterIsolate (isolate, event_loop);
1258+ per_process:: v8_platform.Platform ()->RegisterIsolate (isolate, event_loop);
14201259 Isolate::Initialize (isolate, params);
14211260
14221261 isolate->AddMessageListenerWithErrorLevel (OnMessage,
@@ -1462,11 +1301,10 @@ inline int Start(uv_loop_t* event_loop,
14621301 Isolate::Scope isolate_scope (isolate);
14631302 HandleScope handle_scope (isolate);
14641303 std::unique_ptr<IsolateData, decltype (&FreeIsolateData)> isolate_data (
1465- CreateIsolateData (
1466- isolate,
1467- event_loop,
1468- v8_platform.Platform (),
1469- allocator.get ()),
1304+ CreateIsolateData (isolate,
1305+ event_loop,
1306+ per_process::v8_platform.Platform (),
1307+ allocator.get ()),
14701308 &FreeIsolateData);
14711309 // TODO(addaleax): This should load a real per-Isolate option, currently
14721310 // this is still effectively per-process.
@@ -1484,7 +1322,7 @@ inline int Start(uv_loop_t* event_loop,
14841322 }
14851323
14861324 isolate->Dispose ();
1487- v8_platform.Platform ()->UnregisterIsolate (isolate);
1325+ per_process:: v8_platform.Platform ()->UnregisterIsolate (isolate);
14881326
14891327 return exit_code;
14901328}
@@ -1549,7 +1387,7 @@ int Start(int argc, char** argv) {
15491387 // that happen to terminate during shutdown from being run unsafely.
15501388 // Since uv_run cannot be called, uv_async handles held by the platform
15511389 // will never be fully cleaned up.
1552- v8_platform.Dispose ();
1390+ per_process:: v8_platform.Dispose ();
15531391
15541392 return exit_code;
15551393}
0 commit comments