GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/inspector/tracing_agent.cc Lines: 57 59 96.6 %
Date: 2019-01-07 12:15:22 Branches: 12 18 66.7 %

Line Branch Exec Source
1
#include "tracing_agent.h"
2
#include "node_internals.h"
3
4
#include "env-inl.h"
5
#include "v8.h"
6
7
#include <set>
8
#include <sstream>
9
10
namespace node {
11
namespace inspector {
12
namespace protocol {
13
14
namespace {
15
using v8::platform::tracing::TraceWriter;
16
17
4
class InspectorTraceWriter : public node::tracing::AsyncTraceWriter {
18
 public:
19
2
  explicit InspectorTraceWriter(NodeTracing::Frontend* frontend)
20
2
                                : frontend_(frontend) {}
21
22
136
  void AppendTraceEvent(
23
      v8::platform::tracing::TraceObject* trace_event) override {
24
136
    if (!json_writer_)
25
4
      json_writer_.reset(TraceWriter::CreateJSONTraceWriter(stream_, "value"));
26
136
    json_writer_->AppendTraceEvent(trace_event);
27
136
  }
28
29
4
  void Flush(bool) override {
30
4
    if (!json_writer_)
31
4
      return;
32
4
    json_writer_.reset();
33
    std::ostringstream result(
34
        "{\"method\":\"NodeTracing.dataCollected\",\"params\":",
35
4
        std::ostringstream::ate);
36
4
    result << stream_.str();
37
4
    result << "}";
38
4
    frontend_->sendRawNotification(result.str());
39
4
    stream_.str("");
40
  }
41
42
 private:
43
  std::unique_ptr<TraceWriter> json_writer_;
44
  std::ostringstream stream_;
45
  NodeTracing::Frontend* frontend_;
46
};
47
}  // namespace
48
49
206
TracingAgent::TracingAgent(Environment* env)
50
206
                           : env_(env) {
51
206
}
52
53
618
TracingAgent::~TracingAgent() {
54
206
  trace_writer_.reset();
55
412
}
56
57
206
void TracingAgent::Wire(UberDispatcher* dispatcher) {
58
206
  frontend_.reset(new NodeTracing::Frontend(dispatcher->channel()));
59
206
  NodeTracing::Dispatcher::wire(dispatcher, this);
60
206
}
61
62
3
DispatchResponse TracingAgent::start(
63
    std::unique_ptr<protocol::NodeTracing::TraceConfig> traceConfig) {
64
3
  if (!trace_writer_.empty()) {
65
    return DispatchResponse::Error(
66
        "Call NodeTracing::end to stop tracing before updating the config");
67
  }
68
3
  if (!env_->is_main_thread()) {
69
    return DispatchResponse::Error(
70
1
        "Tracing properties can only be changed through main thread sessions");
71
  }
72
73
2
  std::set<std::string> categories_set;
74
  protocol::Array<std::string>* categories =
75
2
      traceConfig->getIncludedCategories();
76
4
  for (size_t i = 0; i < categories->length(); i++)
77
2
    categories_set.insert(categories->get(i));
78
79
2
  if (categories_set.empty())
80
    return DispatchResponse::Error("At least one category should be enabled");
81
82
2
  tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
83
2
  if (writer != nullptr) {
84
6
    trace_writer_ = writer->agent()->AddClient(
85
        categories_set,
86
        std::unique_ptr<InspectorTraceWriter>(
87
4
            new InspectorTraceWriter(frontend_.get())),
88
2
        tracing::Agent::kIgnoreDefaultCategories);
89
  }
90
2
  return DispatchResponse::OK();
91
}
92
93
2
DispatchResponse TracingAgent::stop() {
94
2
  trace_writer_.reset();
95
2
  frontend_->tracingComplete();
96
2
  return DispatchResponse::OK();
97
}
98
99
1
DispatchResponse TracingAgent::getCategories(
100
    std::unique_ptr<protocol::Array<String>>* categories) {
101
1
  *categories = Array<String>::create();
102
1
  categories->get()->addItem("node");
103
1
  categories->get()->addItem("node.async");
104
1
  categories->get()->addItem("node.bootstrap");
105
1
  categories->get()->addItem("node.fs.sync");
106
1
  categories->get()->addItem("node.perf");
107
1
  categories->get()->addItem("node.perf.usertiming");
108
1
  categories->get()->addItem("node.perf.timerify");
109
1
  categories->get()->addItem("v8");
110
1
  return DispatchResponse::OK();
111
}
112
113
}  // namespace protocol
114
}  // namespace inspector
115
}  // namespace node