GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_v8_platform-inl.h Lines: 67 69 97.1 %
Date: 2020-02-27 22:14:15 Branches: 14 24 58.3 %

Line Branch Exec Source
1
#ifndef SRC_NODE_V8_PLATFORM_INL_H_
2
#define SRC_NODE_V8_PLATFORM_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <memory>
7
8
#include "env-inl.h"
9
#include "node.h"
10
#include "node_metadata.h"
11
#include "node_options.h"
12
#include "tracing/node_trace_writer.h"
13
#include "tracing/trace_event.h"
14
#include "tracing/traced_value.h"
15
16
namespace node {
17
18
// Ensures that __metadata trace events are only emitted
19
// when tracing is enabled.
20
class NodeTraceStateObserver
21
    : public v8::TracingController::TraceStateObserver {
22
 public:
23
57
  inline void OnTraceEnabled() override {
24
114
    std::string title = GetProcessTitle("");
25
57
    if (!title.empty()) {
26
      // Only emit the metadata event if the title can be retrieved
27
      // successfully. Ignore it otherwise.
28

171
      TRACE_EVENT_METADATA1(
29
          "__metadata", "process_name", "name", TRACE_STR_COPY(title.c_str()));
30
    }
31

171
    TRACE_EVENT_METADATA1("__metadata",
32
                          "version",
33
                          "node",
34
                          per_process::metadata.versions.node.c_str());
35

114
    TRACE_EVENT_METADATA1(
36
        "__metadata", "thread_name", "name", "JavaScriptMainThread");
37
38
114
    auto trace_process = tracing::TracedValue::Create();
39
57
    trace_process->BeginDictionary("versions");
40
41
#define V(key)                                                                 \
42
  trace_process->SetString(#key, per_process::metadata.versions.key.c_str());
43
44
57
    NODE_VERSIONS_KEYS(V)
45
#undef V
46
47
57
    trace_process->EndDictionary();
48
49
57
    trace_process->SetString("arch", per_process::metadata.arch.c_str());
50
57
    trace_process->SetString("platform",
51
57
                             per_process::metadata.platform.c_str());
52
53
57
    trace_process->BeginDictionary("release");
54
57
    trace_process->SetString("name",
55
57
                             per_process::metadata.release.name.c_str());
56
#if NODE_VERSION_IS_LTS
57
    trace_process->SetString("lts", per_process::metadata.release.lts.c_str());
58
#endif
59
57
    trace_process->EndDictionary();
60

114
    TRACE_EVENT_METADATA1(
61
        "__metadata", "node", "process", std::move(trace_process));
62
63
    // This only runs the first time tracing is enabled
64
57
    controller_->RemoveTraceStateObserver(this);
65
57
  }
66
67
  inline void OnTraceDisabled() override {
68
    // Do nothing here. This should never be called because the
69
    // observer removes itself when OnTraceEnabled() is called.
70
    UNREACHABLE();
71
  }
72
73
4140
  explicit NodeTraceStateObserver(v8::TracingController* controller)
74
4140
      : controller_(controller) {}
75
8282
  ~NodeTraceStateObserver() override = default;
76
77
 private:
78
  v8::TracingController* controller_;
79
};
80
81
8403
struct V8Platform {
82
#if NODE_USE_V8_PLATFORM
83
4140
  inline void Initialize(int thread_pool_size) {
84
4140
    tracing_agent_ = std::make_unique<tracing::Agent>();
85
4140
    node::tracing::TraceEventHelper::SetAgent(tracing_agent_.get());
86
    node::tracing::TracingController* controller =
87
4140
        tracing_agent_->GetTracingController();
88
    trace_state_observer_ =
89
4140
        std::make_unique<NodeTraceStateObserver>(controller);
90
4140
    controller->AddTraceStateObserver(trace_state_observer_.get());
91
4140
    tracing_file_writer_ = tracing_agent_->DefaultHandle();
92
    // Only start the tracing agent if we enabled any tracing categories.
93
4140
    if (!per_process::cli_options->trace_event_categories.empty()) {
94
52
      StartTracingAgent();
95
    }
96
    // Tracing must be initialized before platform threads are created.
97
4140
    platform_ = new NodePlatform(thread_pool_size, controller);
98
4140
    v8::V8::InitializePlatform(platform_);
99
4140
  }
100
101
4135
  inline void Dispose() {
102
4135
    StopTracingAgent();
103
4135
    platform_->Shutdown();
104
4135
    delete platform_;
105
4135
    platform_ = nullptr;
106
    // Destroy tracing after the platform (and platform threads) have been
107
    // stopped.
108
4135
    tracing_agent_.reset(nullptr);
109
4135
    trace_state_observer_.reset(nullptr);
110
4135
  }
111
112
7492
  inline void DrainVMTasks(v8::Isolate* isolate) {
113
7492
    platform_->DrainTasks(isolate);
114
7491
  }
115
116
72
  inline void StartTracingAgent() {
117
    // Attach a new NodeTraceWriter only if this function hasn't been called
118
    // before.
119
72
    if (tracing_file_writer_.IsDefaultHandle()) {
120
      std::vector<std::string> categories =
121
110
          SplitString(per_process::cli_options->trace_event_categories, ',');
122
123
110
      tracing_file_writer_ = tracing_agent_->AddClient(
124
110
          std::set<std::string>(std::make_move_iterator(categories.begin()),
125
                                std::make_move_iterator(categories.end())),
126
165
          std::unique_ptr<tracing::AsyncTraceWriter>(
127
              new tracing::NodeTraceWriter(
128
110
                  per_process::cli_options->trace_event_file_pattern)),
129
55
          tracing::Agent::kUseDefaultCategories);
130
    }
131
72
  }
132
133
4135
  inline void StopTracingAgent() { tracing_file_writer_.reset(); }
134
135
8443
  inline tracing::AgentWriterHandle* GetTracingAgentWriter() {
136
8443
    return &tracing_file_writer_;
137
  }
138
139
4142
  inline NodePlatform* Platform() { return platform_; }
140
141
  std::unique_ptr<NodeTraceStateObserver> trace_state_observer_;
142
  std::unique_ptr<tracing::Agent> tracing_agent_;
143
  tracing::AgentWriterHandle tracing_file_writer_;
144
  NodePlatform* platform_;
145
#else   // !NODE_USE_V8_PLATFORM
146
  inline void Initialize(int thread_pool_size) {}
147
  inline void Dispose() {}
148
  inline void DrainVMTasks(v8::Isolate* isolate) {}
149
  inline void StartTracingAgent() {
150
    if (!per_process::cli_options->trace_event_categories.empty()) {
151
      fprintf(stderr,
152
              "Node compiled with NODE_USE_V8_PLATFORM=0, "
153
              "so event tracing is not available.\n");
154
    }
155
  }
156
  inline void StopTracingAgent() {}
157
158
  inline tracing::AgentWriterHandle* GetTracingAgentWriter() { return nullptr; }
159
160
  inline NodePlatform* Platform() { return nullptr; }
161
#endif  // !NODE_USE_V8_PLATFORM
162
};
163
164
namespace per_process {
165
extern struct V8Platform v8_platform;
166
}
167
168
20
inline void StartTracingAgent() {
169
20
  return per_process::v8_platform.StartTracingAgent();
170
}
171
172
8443
inline tracing::AgentWriterHandle* GetTracingAgentWriter() {
173
8443
  return per_process::v8_platform.GetTracingAgentWriter();
174
}
175
176
400
inline void DisposePlatform() {
177
400
  per_process::v8_platform.Dispose();
178
400
}
179
180
}  // namespace node
181
182
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
183
184
#endif  // SRC_NODE_V8_PLATFORM_INL_H_