Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
agent.cc
Go to the documentation of this file.
1 #include "tracing/agent.h"
2 
3 #include <sstream>
4 #include <string>
5 
6 #include "env-inl.h"
7 
8 namespace node {
9 namespace tracing {
10 
11 using v8::platform::tracing::TraceConfig;
12 using std::string;
13 
15  int err = uv_loop_init(&tracing_loop_);
16  CHECK_EQ(err, 0);
17 
18  NodeTraceWriter* trace_writer = new NodeTraceWriter(&tracing_loop_);
19  TraceBuffer* trace_buffer = new NodeTraceBuffer(
20  NodeTraceBuffer::kBufferChunks, trace_writer, &tracing_loop_);
21  tracing_controller_ = new TracingController();
22  tracing_controller_->Initialize(trace_buffer);
23 }
24 
25 void Agent::Start(const string& enabled_categories) {
26  TraceConfig* trace_config = new TraceConfig();
27  if (!enabled_categories.empty()) {
28  std::stringstream category_list(enabled_categories);
29  while (category_list.good()) {
30  std::string category;
31  getline(category_list, category, ',');
32  trace_config->AddIncludedCategory(category.c_str());
33  }
34  } else {
35  trace_config->AddIncludedCategory("v8");
36  trace_config->AddIncludedCategory("node");
37  }
38 
39  // This thread should be created *after* async handles are created
40  // (within NodeTraceWriter and NodeTraceBuffer constructors).
41  // Otherwise the thread could shut down prematurely.
42  int err = uv_thread_create(&thread_, ThreadCb, this);
43  CHECK_EQ(err, 0);
44 
45  tracing_controller_->StartTracing(trace_config);
46  started_ = true;
47 }
48 
49 void Agent::Stop() {
50  if (!started_) {
51  return;
52  }
53  // Perform final Flush on TraceBuffer. We don't want the tracing controller
54  // to flush the buffer again on destruction of the V8::Platform.
55  tracing_controller_->StopTracing();
56  tracing_controller_->Initialize(nullptr);
57  started_ = false;
58 
59  // Thread should finish when the tracing loop is stopped.
60  uv_thread_join(&thread_);
61 }
62 
63 // static
64 void Agent::ThreadCb(void* arg) {
65  Agent* agent = static_cast<Agent*>(arg);
66  uv_run(&agent->tracing_loop_, UV_RUN_DEFAULT);
67 }
68 
69 } // namespace tracing
70 } // namespace node
static const size_t kBufferChunks
void Start(const std::string &enabled_categories)