GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_v8_platform-inl.h Lines: 65 67 97.0 %
Date: 2022-10-31 03:21:21 Branches: 17 28 60.7 %

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_platform.h"
12
#include "node_options.h"
13
#include "tracing/node_trace_writer.h"
14
#include "tracing/trace_event.h"
15
#include "tracing/traced_value.h"
16
#include "util.h"
17
18
namespace node {
19
20
// Ensures that __metadata trace events are only emitted
21
// when tracing is enabled.
22
class NodeTraceStateObserver
23
    : public v8::TracingController::TraceStateObserver {
24
 public:
25
89
  inline void OnTraceEnabled() override {
26
178
    std::string title = GetProcessTitle("");
27
89
    if (!title.empty()) {
28
      // Only emit the metadata event if the title can be retrieved
29
      // successfully. Ignore it otherwise.
30

267
      TRACE_EVENT_METADATA1(
31
          "__metadata", "process_name", "name", TRACE_STR_COPY(title.c_str()));
32
    }
33

267
    TRACE_EVENT_METADATA1("__metadata",
34
                          "version",
35
                          "node",
36
                          per_process::metadata.versions.node.c_str());
37

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

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