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: 62 64 96.9 %
Date: 2019-02-13 22:28:58 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 "env-inl.h"
7
#include "node.h"
8
#include "node_metadata.h"
9
#include "node_options.h"
10
#include "tracing/node_trace_writer.h"
11
#include "tracing/trace_event.h"
12
#include "tracing/traced_value.h"
13
14
namespace node {
15
16
// Ensures that __metadata trace events are only emitted
17
// when tracing is enabled.
18
class NodeTraceStateObserver
19
    : public v8::TracingController::TraceStateObserver {
20
 public:
21
68
  inline void OnTraceEnabled() override {
22
    char name_buffer[512];
23
68
    if (uv_get_process_title(name_buffer, sizeof(name_buffer)) == 0) {
24
      // Only emit the metadata event if the title can be retrieved
25
      // successfully. Ignore it otherwise.
26

134
      TRACE_EVENT_METADATA1(
27
          "__metadata", "process_name", "name", TRACE_STR_COPY(name_buffer));
28
    }
29

136
    TRACE_EVENT_METADATA1("__metadata",
30
                          "version",
31
                          "node",
32
                          per_process::metadata.versions.node.c_str());
33

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

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