7
7
#include " node_context_data.h"
8
8
#include " node_worker.h"
9
9
#include " tracing/agent.h"
10
+ #include " tracing/traced_value.h"
10
11
11
12
#include < stdio.h>
12
13
#include < algorithm>
@@ -33,6 +34,25 @@ using worker::Worker;
33
34
34
35
#define kTraceCategoryCount 1
35
36
37
+ // TODO(@jasnell): Likely useful to move this to util or node_internal to
38
+ // allow reuse. But since we're not reusing it yet...
39
+ class TraceEventScope {
40
+ public:
41
+ TraceEventScope (const char * category,
42
+ const char * name,
43
+ void * id) : category_(category), name_(name), id_(id) {
44
+ TRACE_EVENT_NESTABLE_ASYNC_BEGIN0 (category_, name_, id_);
45
+ }
46
+ ~TraceEventScope () {
47
+ TRACE_EVENT_NESTABLE_ASYNC_END0 (category_, name_, id_);
48
+ }
49
+
50
+ private:
51
+ const char * category_;
52
+ const char * name_;
53
+ void * id_;
54
+ };
55
+
36
56
int const Environment::kNodeContextTag = 0x6e6f64 ;
37
57
void * Environment::kNodeContextTagPtr = const_cast <void *>(
38
58
static_cast <const void *>(&Environment::kNodeContextTag ));
@@ -224,6 +244,9 @@ Environment::~Environment() {
224
244
delete[] heap_statistics_buffer_;
225
245
delete[] heap_space_statistics_buffer_;
226
246
delete[] http_parser_buffer_;
247
+
248
+ TRACE_EVENT_NESTABLE_ASYNC_END0 (
249
+ TRACING_CATEGORY_NODE1 (environment), " Environment" , this );
227
250
}
228
251
229
252
void Environment::Start (const std::vector<std::string>& args,
@@ -232,6 +255,23 @@ void Environment::Start(const std::vector<std::string>& args,
232
255
HandleScope handle_scope (isolate ());
233
256
Context::Scope context_scope (context ());
234
257
258
+ if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED (
259
+ TRACING_CATEGORY_NODE1 (environment)) != 0 ) {
260
+ auto traced_value = tracing::TracedValue::Create ();
261
+ traced_value->BeginArray (" args" );
262
+ for (const std::string& arg : args)
263
+ traced_value->AppendString (arg);
264
+ traced_value->EndArray ();
265
+ traced_value->BeginArray (" exec_args" );
266
+ for (const std::string& arg : exec_args)
267
+ traced_value->AppendString (arg);
268
+ traced_value->EndArray ();
269
+ TRACE_EVENT_NESTABLE_ASYNC_BEGIN1 (
270
+ TRACING_CATEGORY_NODE1 (environment),
271
+ " Environment" , this ,
272
+ " args" , std::move (traced_value));
273
+ }
274
+
235
275
CHECK_EQ (0 , uv_timer_init (event_loop (), timer_handle ()));
236
276
uv_unref (reinterpret_cast <uv_handle_t *>(timer_handle ()));
237
277
@@ -401,6 +441,8 @@ void Environment::PrintSyncTrace() const {
401
441
}
402
442
403
443
void Environment::RunCleanup () {
444
+ TraceEventScope trace_scope (TRACING_CATEGORY_NODE1 (environment),
445
+ " RunCleanup" , this );
404
446
CleanupHandles ();
405
447
406
448
while (!cleanup_hooks_.empty ()) {
@@ -432,6 +474,8 @@ void Environment::RunCleanup() {
432
474
}
433
475
434
476
void Environment::RunBeforeExitCallbacks () {
477
+ TraceEventScope trace_scope (TRACING_CATEGORY_NODE1 (environment),
478
+ " BeforeExit" , this );
435
479
for (ExitCallback before_exit : before_exit_functions_) {
436
480
before_exit.cb_ (before_exit.arg_ );
437
481
}
@@ -443,6 +487,8 @@ void Environment::BeforeExit(void (*cb)(void* arg), void* arg) {
443
487
}
444
488
445
489
void Environment::RunAtExitCallbacks () {
490
+ TraceEventScope trace_scope (TRACING_CATEGORY_NODE1 (environment),
491
+ " AtExit" , this );
446
492
for (ExitCallback at_exit : at_exit_functions_) {
447
493
at_exit.cb_ (at_exit.arg_ );
448
494
}
@@ -496,13 +542,16 @@ void Environment::EnvPromiseHook(v8::PromiseHookType type,
496
542
497
543
Environment* env = Environment::GetCurrent (context);
498
544
if (env == nullptr ) return ;
499
-
545
+ TraceEventScope trace_scope (TRACING_CATEGORY_NODE1 (environment),
546
+ " EnvPromiseHook" , env);
500
547
for (const PromiseHookCallback& hook : env->promise_hooks_ ) {
501
548
hook.cb_ (type, promise, parent, hook.arg_ );
502
549
}
503
550
}
504
551
505
552
void Environment::RunAndClearNativeImmediates () {
553
+ TraceEventScope trace_scope (TRACING_CATEGORY_NODE1 (environment),
554
+ " RunAndClearNativeImmediates" , this );
506
555
size_t count = native_immediate_callbacks_.size ();
507
556
if (count > 0 ) {
508
557
size_t ref_count = 0 ;
@@ -555,6 +604,8 @@ void Environment::ToggleTimerRef(bool ref) {
555
604
556
605
void Environment::RunTimers (uv_timer_t * handle) {
557
606
Environment* env = Environment::from_timer_handle (handle);
607
+ TraceEventScope trace_scope (TRACING_CATEGORY_NODE1 (environment),
608
+ " RunTimers" , env);
558
609
559
610
if (!env->can_call_into_js ())
560
611
return ;
@@ -615,6 +666,8 @@ void Environment::RunTimers(uv_timer_t* handle) {
615
666
616
667
void Environment::CheckImmediate (uv_check_t * handle) {
617
668
Environment* env = Environment::from_immediate_check_handle (handle);
669
+ TraceEventScope trace_scope (TRACING_CATEGORY_NODE1 (environment),
670
+ " CheckImmediate" , env);
618
671
619
672
if (env->immediate_info ()->count () == 0 )
620
673
return ;
0 commit comments