GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/tracing/agent.cc Lines: 33 33 100.0 %
Date: 2017-01-02 Branches: 8 12 66.7 %

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