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
@@ -163,169 +161,10 @@ bool v8_initialized = false;
163
161
// node_internals.h
164
162
// process-relative uptime base, initialized at start-up
165
163
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
- }
260
164
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
329
168
330
169
#ifdef __POSIX__
331
170
static const unsigned kMaxSignal = 32 ;
@@ -1188,7 +1027,7 @@ Environment* GetCurrentEnvironment(Local<Context> context) {
1188
1027
1189
1028
1190
1029
MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform () {
1191
- return v8_platform.Platform ();
1030
+ return per_process:: v8_platform.Platform ();
1192
1031
}
1193
1032
1194
1033
@@ -1200,8 +1039,8 @@ MultiIsolatePlatform* CreatePlatform(
1200
1039
1201
1040
1202
1041
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 ();
1205
1044
}
1206
1045
1207
1046
@@ -1282,7 +1121,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
1282
1121
do {
1283
1122
uv_run (env.event_loop (), UV_RUN_DEFAULT);
1284
1123
1285
- v8_platform.DrainVMTasks (isolate);
1124
+ per_process:: v8_platform.DrainVMTasks (isolate);
1286
1125
1287
1126
more = uv_loop_alive (env.event_loop ());
1288
1127
if (more)
@@ -1310,8 +1149,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
1310
1149
env.RunCleanup ();
1311
1150
RunAtExit (&env);
1312
1151
1313
- v8_platform.DrainVMTasks (isolate);
1314
- v8_platform.CancelVMTasks (isolate);
1152
+ per_process:: v8_platform.DrainVMTasks (isolate);
1153
+ per_process:: v8_platform.CancelVMTasks (isolate);
1315
1154
#if defined(LEAK_SANITIZER)
1316
1155
__lsan_do_leak_check ();
1317
1156
#endif
@@ -1339,7 +1178,7 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
1339
1178
1340
1179
// Register the isolate on the platform before the isolate gets initialized,
1341
1180
// 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);
1343
1182
Isolate::Initialize (isolate, params);
1344
1183
1345
1184
isolate->AddMessageListenerWithErrorLevel (OnMessage,
@@ -1385,11 +1224,10 @@ inline int Start(uv_loop_t* event_loop,
1385
1224
Isolate::Scope isolate_scope (isolate);
1386
1225
HandleScope handle_scope (isolate);
1387
1226
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 ()),
1393
1231
&FreeIsolateData);
1394
1232
// TODO(addaleax): This should load a real per-Isolate option, currently
1395
1233
// this is still effectively per-process.
@@ -1407,7 +1245,7 @@ inline int Start(uv_loop_t* event_loop,
1407
1245
}
1408
1246
1409
1247
isolate->Dispose ();
1410
- v8_platform.Platform ()->UnregisterIsolate (isolate);
1248
+ per_process:: v8_platform.Platform ()->UnregisterIsolate (isolate);
1411
1249
1412
1250
return exit_code;
1413
1251
}
@@ -1472,7 +1310,7 @@ int Start(int argc, char** argv) {
1472
1310
// that happen to terminate during shutdown from being run unsafely.
1473
1311
// Since uv_run cannot be called, uv_async handles held by the platform
1474
1312
// will never be fully cleaned up.
1475
- v8_platform.Dispose ();
1313
+ per_process:: v8_platform.Dispose ();
1476
1314
1477
1315
return exit_code;
1478
1316
}
0 commit comments