GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/memory_tracker-inl.h Lines: 0 122 0.0 %
Date: 2019-02-01 22:03:38 Branches: 0 112 0.0 %

Line Branch Exec Source
1
#ifndef SRC_MEMORY_TRACKER_INL_H_
2
#define SRC_MEMORY_TRACKER_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "memory_tracker.h"
7
8
namespace node {
9
10
// Fallback edge_name if node_name is not available, or "" if edge_name
11
// is not available either.
12
inline const char* GetNodeName(const char* node_name, const char* edge_name) {
13
  if (node_name != nullptr) {
14
    return node_name;
15
  }
16
  if (edge_name != nullptr) {
17
    return edge_name;
18
  }
19
  return "";
20
}
21
22
class MemoryRetainerNode : public v8::EmbedderGraph::Node {
23
 public:
24
  inline MemoryRetainerNode(MemoryTracker* tracker,
25
                                     const MemoryRetainer* retainer)
26
      : retainer_(retainer) {
27
    CHECK_NOT_NULL(retainer_);
28
    v8::HandleScope handle_scope(tracker->isolate());
29
    v8::Local<v8::Object> obj = retainer_->WrappedObject();
30
    if (!obj.IsEmpty()) wrapper_node_ = tracker->graph()->V8Node(obj);
31
32
    name_ = retainer_->MemoryInfoName();
33
    size_ = retainer_->SelfSize();
34
  }
35
36
  inline MemoryRetainerNode(MemoryTracker* tracker,
37
                                     const char* name,
38
                                     size_t size,
39
                                     bool is_root_node = false)
40
      : retainer_(nullptr) {
41
    name_ = name;
42
    size_ = size;
43
    is_root_node_ = is_root_node;
44
  }
45
46
  const char* Name() override { return name_.c_str(); }
47
  const char* NamePrefix() override { return "Node /"; }
48
  size_t SizeInBytes() override { return size_; }
49
  // TODO(addaleax): Merging this with the "official" WrapperNode() method
50
  // seems to lose accuracy, e.g. SizeInBytes() is disregarded.
51
  // Figure out whether to do anything about that.
52
  Node* JSWrapperNode() { return wrapper_node_; }
53
54
  bool IsRootNode() override {
55
    if (retainer_ != nullptr) {
56
      return retainer_->IsRootNode();
57
    }
58
    return is_root_node_;
59
  }
60
61
 private:
62
  friend class MemoryTracker;
63
64
  // If retainer_ is not nullptr, then it must have a wrapper_node_,
65
  // and we have
66
  // name_ == retainer_->MemoryInfoName()
67
  // size_ == retainer_->SelfSize()
68
  // is_root_node_ == retainer_->IsRootNode()
69
  const MemoryRetainer* retainer_;
70
  Node* wrapper_node_ = nullptr;
71
72
  // Otherwise (retainer == nullptr), we set these fields in an ad-hoc way
73
  bool is_root_node_ = false;
74
  std::string name_;
75
  size_t size_ = 0;
76
};
77
78
void MemoryTracker::TrackFieldWithSize(const char* edge_name,
79
                                       size_t size,
80
                                       const char* node_name) {
81
  if (size > 0) AddNode(GetNodeName(node_name, edge_name), size, edge_name);
82
}
83
84
void MemoryTracker::TrackField(const char* edge_name,
85
                               const MemoryRetainer& value,
86
                               const char* node_name) {
87
  TrackField(edge_name, &value);
88
}
89
90
void MemoryTracker::TrackField(const char* edge_name,
91
                               const MemoryRetainer* value,
92
                               const char* node_name) {
93
  if (value == nullptr) return;
94
  auto it = seen_.find(value);
95
  if (it != seen_.end()) {
96
    graph_->AddEdge(CurrentNode(), it->second, edge_name);
97
  } else {
98
    Track(value, edge_name);
99
  }
100
}
101
102
template <typename T>
103
void MemoryTracker::TrackField(const char* edge_name,
104
                               const std::unique_ptr<T>& value,
105
                               const char* node_name) {
106
  if (value.get() == nullptr) {
107
    return;
108
  }
109
  TrackField(edge_name, value.get(), node_name);
110
}
111
112
template <typename T, typename Iterator>
113
void MemoryTracker::TrackField(const char* edge_name,
114
                               const T& value,
115
                               const char* node_name,
116
                               const char* element_name,
117
                               bool subtract_from_self) {
118
  // If the container is empty, the size has been accounted into the parent's
119
  // self size
120
  if (value.begin() == value.end()) return;
121
  // Fall back to edge name if node names are not provided
122
  if (CurrentNode() != nullptr && subtract_from_self) {
123
    // Shift the self size of this container out to a separate node
124
    CurrentNode()->size_ -= sizeof(T);
125
  }
126
  PushNode(GetNodeName(node_name, edge_name), sizeof(T), edge_name);
127
  for (Iterator it = value.begin(); it != value.end(); ++it) {
128
    // Use nullptr as edge names so the elements appear as indexed properties
129
    TrackField(nullptr, *it, element_name);
130
  }
131
  PopNode();
132
}
133
134
template <typename T>
135
void MemoryTracker::TrackField(const char* edge_name,
136
                               const std::queue<T>& value,
137
                               const char* node_name,
138
                               const char* element_name) {
139
  struct ContainerGetter : public std::queue<T> {
140
    static const typename std::queue<T>::container_type& Get(
141
        const std::queue<T>& value) {
142
      return value.*&ContainerGetter::c;
143
    }
144
  };
145
146
  const auto& container = ContainerGetter::Get(value);
147
  TrackField(edge_name, container, node_name, element_name);
148
}
149
150
template <typename T, typename test_for_number, typename dummy>
151
void MemoryTracker::TrackField(const char* edge_name,
152
                               const T& value,
153
                               const char* node_name) {
154
  // For numbers, creating new nodes is not worth the overhead.
155
  CurrentNode()->size_ += sizeof(T);
156
}
157
158
template <typename T, typename U>
159
void MemoryTracker::TrackField(const char* edge_name,
160
                               const std::pair<T, U>& value,
161
                               const char* node_name) {
162
  PushNode(node_name == nullptr ? "pair" : node_name,
163
           sizeof(const std::pair<T, U>),
164
           edge_name);
165
  // TODO(joyeecheung): special case if one of these is a number type
166
  // that meets the test_for_number trait so that their sizes don't get
167
  // merged into the pair node
168
  TrackField("first", value.first);
169
  TrackField("second", value.second);
170
  PopNode();
171
}
172
173
template <typename T>
174
void MemoryTracker::TrackField(const char* edge_name,
175
                               const std::basic_string<T>& value,
176
                               const char* node_name) {
177
  TrackFieldWithSize(edge_name, value.size() * sizeof(T), "std::basic_string");
178
}
179
180
template <typename T, typename Traits>
181
void MemoryTracker::TrackField(const char* edge_name,
182
                               const v8::Persistent<T, Traits>& value,
183
                               const char* node_name) {
184
  TrackField(edge_name, value.Get(isolate_));
185
}
186
187
template <typename T>
188
void MemoryTracker::TrackField(const char* edge_name,
189
                               const v8::Local<T>& value,
190
                               const char* node_name) {
191
  if (!value.IsEmpty())
192
    graph_->AddEdge(CurrentNode(), graph_->V8Node(value), edge_name);
193
}
194
195
template <typename T>
196
void MemoryTracker::TrackField(const char* edge_name,
197
                               const MallocedBuffer<T>& value,
198
                               const char* node_name) {
199
  TrackFieldWithSize(edge_name, value.size, "MallocedBuffer");
200
}
201
202
void MemoryTracker::TrackField(const char* name,
203
                               const uv_buf_t& value,
204
                               const char* node_name) {
205
  TrackFieldWithSize(name, value.len, "uv_buf_t");
206
}
207
208
void MemoryTracker::TrackField(const char* name,
209
                               const uv_timer_t& value,
210
                               const char* node_name) {
211
  TrackFieldWithSize(name, sizeof(value), "uv_timer_t");
212
}
213
214
void MemoryTracker::TrackField(const char* name,
215
                               const uv_async_t& value,
216
                               const char* node_name) {
217
  TrackFieldWithSize(name, sizeof(value), "uv_async_t");
218
}
219
220
template <class NativeT, class V8T>
221
void MemoryTracker::TrackField(const char* name,
222
                               const AliasedBuffer<NativeT, V8T>& value,
223
                               const char* node_name) {
224
  TrackField(name, value.GetJSArray(), "AliasedBuffer");
225
}
226
227
void MemoryTracker::Track(const MemoryRetainer* retainer,
228
                          const char* edge_name) {
229
  v8::HandleScope handle_scope(isolate_);
230
  auto it = seen_.find(retainer);
231
  if (it != seen_.end()) {
232
    if (CurrentNode() != nullptr) {
233
      graph_->AddEdge(CurrentNode(), it->second, edge_name);
234
    }
235
    return;  // It has already been tracked, no need to call MemoryInfo again
236
  }
237
  MemoryRetainerNode* n = PushNode(retainer, edge_name);
238
  retainer->MemoryInfo(this);
239
  CHECK_EQ(CurrentNode(), n);
240
  CHECK_NE(n->size_, 0);
241
  PopNode();
242
}
243
244
MemoryRetainerNode* MemoryTracker::CurrentNode() const {
245
  if (node_stack_.empty()) return nullptr;
246
  return node_stack_.top();
247
}
248
249
MemoryRetainerNode* MemoryTracker::AddNode(const MemoryRetainer* retainer,
250
                                           const char* edge_name) {
251
  auto it = seen_.find(retainer);
252
  if (it != seen_.end()) {
253
    return it->second;
254
  }
255
256
  MemoryRetainerNode* n = new MemoryRetainerNode(this, retainer);
257
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
258
  seen_[retainer] = n;
259
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
260
261
  if (n->JSWrapperNode() != nullptr) {
262
    graph_->AddEdge(n, n->JSWrapperNode(), "wrapped");
263
    graph_->AddEdge(n->JSWrapperNode(), n, "wrapper");
264
  }
265
266
  return n;
267
}
268
269
MemoryRetainerNode* MemoryTracker::AddNode(const char* node_name,
270
                                           size_t size,
271
                                           const char* edge_name) {
272
  MemoryRetainerNode* n = new MemoryRetainerNode(this, node_name, size);
273
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
274
275
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
276
277
  return n;
278
}
279
280
MemoryRetainerNode* MemoryTracker::PushNode(const MemoryRetainer* retainer,
281
                                            const char* edge_name) {
282
  MemoryRetainerNode* n = AddNode(retainer, edge_name);
283
  node_stack_.push(n);
284
  return n;
285
}
286
287
MemoryRetainerNode* MemoryTracker::PushNode(const char* node_name,
288
                                            size_t size,
289
                                            const char* edge_name) {
290
  MemoryRetainerNode* n = AddNode(node_name, size, edge_name);
291
  node_stack_.push(n);
292
  return n;
293
}
294
295
void MemoryTracker::PopNode() {
296
  node_stack_.pop();
297
}
298
299
}  // namespace node
300
301
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
302
303
#endif  // SRC_MEMORY_TRACKER_INL_H_