32
32
#include " node_platform.h"
33
33
#include " node_process.h"
34
34
#include " node_revert.h"
35
+ #include " node_v8_platform-inl.h"
35
36
#include " node_version.h"
36
- #include " tracing/traced_value.h"
37
37
38
38
#if HAVE_OPENSSL
39
39
#include " node_crypto.h"
56
56
#include " handle_wrap.h"
57
57
#include " req_wrap-inl.h"
58
58
#include " string_bytes.h"
59
- #include " tracing/agent.h"
60
- #include " tracing/node_trace_writer.h"
61
59
#include " util.h"
62
60
#include " uv.h"
63
61
#if NODE_USE_V8_PLATFORM
@@ -165,169 +163,10 @@ bool v8_initialized = false;
165
163
// node_internals.h
166
164
// process-relative uptime base, initialized at start-up
167
165
double 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
- }
262
166
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
331
170
332
171
#ifdef __POSIX__
333
172
static const unsigned kMaxSignal = 32 ;
@@ -1258,7 +1097,7 @@ Environment* GetCurrentEnvironment(Local<Context> context) {
1258
1097
1259
1098
1260
1099
MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform () {
1261
- return v8_platform.Platform ();
1100
+ return per_process:: v8_platform.Platform ();
1262
1101
}
1263
1102
1264
1103
@@ -1270,8 +1109,8 @@ MultiIsolatePlatform* CreatePlatform(
1270
1109
1271
1110
1272
1111
MultiIsolatePlatform* 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 ();
1275
1114
}
1276
1115
1277
1116
@@ -1359,7 +1198,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
1359
1198
do {
1360
1199
uv_run (env.event_loop (), UV_RUN_DEFAULT);
1361
1200
1362
- v8_platform.DrainVMTasks (isolate);
1201
+ per_process:: v8_platform.DrainVMTasks (isolate);
1363
1202
1364
1203
more = uv_loop_alive (env.event_loop ());
1365
1204
if (more)
@@ -1387,8 +1226,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
1387
1226
env.RunCleanup ();
1388
1227
RunAtExit (&env);
1389
1228
1390
- v8_platform.DrainVMTasks (isolate);
1391
- v8_platform.CancelVMTasks (isolate);
1229
+ per_process:: v8_platform.DrainVMTasks (isolate);
1230
+ per_process:: v8_platform.CancelVMTasks (isolate);
1392
1231
#if defined(LEAK_SANITIZER)
1393
1232
__lsan_do_leak_check ();
1394
1233
#endif
@@ -1416,7 +1255,7 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
1416
1255
1417
1256
// Register the isolate on the platform before the isolate gets initialized,
1418
1257
// 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);
1420
1259
Isolate::Initialize (isolate, params);
1421
1260
1422
1261
isolate->AddMessageListenerWithErrorLevel (OnMessage,
@@ -1462,11 +1301,10 @@ inline int Start(uv_loop_t* event_loop,
1462
1301
Isolate::Scope isolate_scope (isolate);
1463
1302
HandleScope handle_scope (isolate);
1464
1303
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 ()),
1470
1308
&FreeIsolateData);
1471
1309
// TODO(addaleax): This should load a real per-Isolate option, currently
1472
1310
// this is still effectively per-process.
@@ -1484,7 +1322,7 @@ inline int Start(uv_loop_t* event_loop,
1484
1322
}
1485
1323
1486
1324
isolate->Dispose ();
1487
- v8_platform.Platform ()->UnregisterIsolate (isolate);
1325
+ per_process:: v8_platform.Platform ()->UnregisterIsolate (isolate);
1488
1326
1489
1327
return exit_code;
1490
1328
}
@@ -1549,7 +1387,7 @@ int Start(int argc, char** argv) {
1549
1387
// that happen to terminate during shutdown from being run unsafely.
1550
1388
// Since uv_run cannot be called, uv_async handles held by the platform
1551
1389
// will never be fully cleaned up.
1552
- v8_platform.Dispose ();
1390
+ per_process:: v8_platform.Dispose ();
1553
1391
1554
1392
return exit_code;
1555
1393
}
0 commit comments