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: 119 146 81.5 %
Date: 2020-02-27 22:14:15 Branches: 72 132 54.5 %

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
2108
inline const char* GetNodeName(const char* node_name, const char* edge_name) {
13
2108
  if (node_name != nullptr) {
14
2018
    return node_name;
15
  }
16
90
  if (edge_name != nullptr) {
17
90
    return edge_name;
18
  }
19
  return "";
20
}
21
22
5616
class MemoryRetainerNode : public v8::EmbedderGraph::Node {
23
 public:
24
386
  inline MemoryRetainerNode(MemoryTracker* tracker,
25
                                     const MemoryRetainer* retainer)
26
386
      : retainer_(retainer) {
27
386
    CHECK_NOT_NULL(retainer_);
28
772
    v8::HandleScope handle_scope(tracker->isolate());
29
386
    v8::Local<v8::Object> obj = retainer_->WrappedObject();
30
677
    if (!obj.IsEmpty()) wrapper_node_ = tracker->graph()->V8Node(obj);
31
32
386
    name_ = retainer_->MemoryInfoName();
33
386
    size_ = retainer_->SelfSize();
34
386
  }
35
36
2422
  inline MemoryRetainerNode(MemoryTracker* tracker,
37
                                     const char* name,
38
                                     size_t size,
39
                                     bool is_root_node = false)
40
2422
      : retainer_(nullptr) {
41
2422
    name_ = name;
42
2422
    size_ = size;
43
2422
    is_root_node_ = is_root_node;
44
2422
  }
45
46
2808
  const char* Name() override { return name_.c_str(); }
47
2808
  const char* NamePrefix() override { return "Node /"; }
48
2808
  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
968
  Node* JSWrapperNode() { return wrapper_node_; }
53
54
5616
  bool IsRootNode() override {
55
5616
    if (retainer_ != nullptr) {
56
772
      return retainer_->IsRootNode();
57
    }
58
4844
    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
2034
void MemoryTracker::TrackFieldWithSize(const char* edge_name,
79
                                       size_t size,
80
                                       const char* node_name) {
81
2034
  if (size > 0) AddNode(GetNodeName(node_name, edge_name), size, edge_name);
82
2034
}
83
84
void MemoryTracker::TrackInlineFieldWithSize(const char* edge_name,
85
                                             size_t size,
86
                                             const char* node_name) {
87
  if (size > 0) AddNode(GetNodeName(node_name, edge_name), size, edge_name);
88
  CHECK(CurrentNode());
89
  CurrentNode()->size_ -= size;
90
}
91
92
57
void MemoryTracker::TrackField(const char* edge_name,
93
                               const MemoryRetainer& value,
94
                               const char* node_name) {
95
57
  TrackField(edge_name, &value);
96
57
}
97
98
375
void MemoryTracker::TrackField(const char* edge_name,
99
                               const MemoryRetainer* value,
100
                               const char* node_name) {
101
375
  if (value == nullptr) return;
102
375
  auto it = seen_.find(value);
103
375
  if (it != seen_.end()) {
104
8
    graph_->AddEdge(CurrentNode(), it->second, edge_name);
105
  } else {
106
367
    Track(value, edge_name);
107
  }
108
}
109
110
template <typename T, typename D>
111
2
void MemoryTracker::TrackField(const char* edge_name,
112
                               const std::unique_ptr<T, D>& value,
113
                               const char* node_name) {
114

2
  if (value.get() == nullptr) {
115
2
    return;
116
  }
117
  TrackField(edge_name, value.get(), node_name);
118
}
119
120
template <typename T>
121
void MemoryTracker::TrackField(const char* edge_name,
122
                               const std::shared_ptr<T>& value,
123
                               const char* node_name) {
124
  if (value.get() == nullptr) {
125
    return;
126
  }
127
  TrackField(edge_name, value.get(), node_name);
128
}
129
130
template <typename T, bool kIsWeak>
131
4
void MemoryTracker::TrackField(const char* edge_name,
132
                               const BaseObjectPtrImpl<T, kIsWeak>& value,
133
                               const char* node_name) {
134

4
  if (value.get() == nullptr) return;
135
4
  TrackField(edge_name, value.get(), node_name);
136
}
137
138
template <typename T, typename Iterator>
139
300
void MemoryTracker::TrackField(const char* edge_name,
140
                               const T& value,
141
                               const char* node_name,
142
                               const char* element_name,
143
                               bool subtract_from_self) {
144
  // If the container is empty, the size has been accounted into the parent's
145
  // self size
146



300
  if (value.begin() == value.end()) return;
147
  // Fall back to edge name if node names are not provided
148









86
  if (CurrentNode() != nullptr && subtract_from_self) {
149
    // Shift the self size of this container out to a separate node
150
86
    CurrentNode()->size_ -= sizeof(T);
151
  }
152
86
  PushNode(GetNodeName(node_name, edge_name), sizeof(T), edge_name);
153



3347
  for (Iterator it = value.begin(); it != value.end(); ++it) {
154
    // Use nullptr as edge names so the elements appear as indexed properties
155
3261
    TrackField(nullptr, *it, element_name);
156
  }
157
86
  PopNode();
158
}
159
160
template <typename T>
161
8
void MemoryTracker::TrackField(const char* edge_name,
162
                               const std::queue<T>& value,
163
                               const char* node_name,
164
                               const char* element_name) {
165
  struct ContainerGetter : public std::queue<T> {
166
8
    static const typename std::queue<T>::container_type& Get(
167
        const std::queue<T>& value) {
168
8
      return value.*&ContainerGetter::c;
169
    }
170
  };
171
172
8
  const auto& container = ContainerGetter::Get(value);
173
8
  TrackField(edge_name, container, node_name, element_name);
174
8
}
175
176
template <typename T, typename test_for_number, typename dummy>
177
79
void MemoryTracker::TrackField(const char* edge_name,
178
                               const T& value,
179
                               const char* node_name) {
180
  // For numbers, creating new nodes is not worth the overhead.
181
79
  CurrentNode()->size_ += sizeof(T);
182
79
}
183
184
template <typename T, typename U>
185
void MemoryTracker::TrackField(const char* edge_name,
186
                               const std::pair<T, U>& value,
187
                               const char* node_name) {
188
  PushNode(node_name == nullptr ? "pair" : node_name,
189
           sizeof(const std::pair<T, U>),
190
           edge_name);
191
  // TODO(joyeecheung): special case if one of these is a number type
192
  // that meets the test_for_number trait so that their sizes don't get
193
  // merged into the pair node
194
  TrackField("first", value.first);
195
  TrackField("second", value.second);
196
  PopNode();
197
}
198
199
template <typename T>
200
1971
void MemoryTracker::TrackField(const char* edge_name,
201
                               const std::basic_string<T>& value,
202
                               const char* node_name) {
203
1971
  TrackFieldWithSize(edge_name, value.size() * sizeof(T), "std::basic_string");
204
1971
}
205
206
template <typename T>
207
893
void MemoryTracker::TrackField(const char* edge_name,
208
                               const v8::Eternal<T>& value,
209
                               const char* node_name) {
210
1786
  TrackField(edge_name, value.Get(isolate_));
211
893
}
212
213
template <typename T>
214
168
void MemoryTracker::TrackField(const char* edge_name,
215
                               const v8::PersistentBase<T>& value,
216
                               const char* node_name) {
217
336
  TrackField(edge_name, value.Get(isolate_));
218
168
}
219
220
template <typename T>
221
6458
void MemoryTracker::TrackField(const char* edge_name,
222
                               const v8::Local<T>& value,
223
                               const char* node_name) {
224




6458
  if (!value.IsEmpty())
225
12292
    graph_->AddEdge(CurrentNode(), graph_->V8Node(value), edge_name);
226
6458
}
227
228
template <typename T>
229
void MemoryTracker::TrackField(const char* edge_name,
230
                               const MallocedBuffer<T>& value,
231
                               const char* node_name) {
232
  TrackFieldWithSize(edge_name, value.size, "MallocedBuffer");
233
}
234
235
void MemoryTracker::TrackField(const char* edge_name,
236
                               const v8::BackingStore* value,
237
                               const char* node_name) {
238
  TrackFieldWithSize(edge_name, value->ByteLength(), "BackingStore");
239
}
240
241
void MemoryTracker::TrackField(const char* name,
242
                               const uv_buf_t& value,
243
                               const char* node_name) {
244
  TrackFieldWithSize(name, value.len, "uv_buf_t");
245
}
246
247
void MemoryTracker::TrackField(const char* name,
248
                               const uv_timer_t& value,
249
                               const char* node_name) {
250
  TrackFieldWithSize(name, sizeof(value), "uv_timer_t");
251
}
252
253
void MemoryTracker::TrackField(const char* name,
254
                               const uv_async_t& value,
255
                               const char* node_name) {
256
  TrackFieldWithSize(name, sizeof(value), "uv_async_t");
257
}
258
259
void MemoryTracker::TrackInlineField(const char* name,
260
                                     const uv_async_t& value,
261
                                     const char* node_name) {
262
  TrackInlineFieldWithSize(name, sizeof(value), "uv_async_t");
263
}
264
265
template <class NativeT, class V8T>
266
172
void MemoryTracker::TrackField(const char* name,
267
                               const AliasedBufferBase<NativeT, V8T>& value,
268
                               const char* node_name) {
269
172
  TrackField(name, value.GetJSArray(), "AliasedBuffer");
270
172
}
271
272
386
void MemoryTracker::Track(const MemoryRetainer* retainer,
273
                          const char* edge_name) {
274
772
  v8::HandleScope handle_scope(isolate_);
275
386
  auto it = seen_.find(retainer);
276
386
  if (it != seen_.end()) {
277
    if (CurrentNode() != nullptr) {
278
      graph_->AddEdge(CurrentNode(), it->second, edge_name);
279
    }
280
    return;  // It has already been tracked, no need to call MemoryInfo again
281
  }
282
386
  MemoryRetainerNode* n = PushNode(retainer, edge_name);
283
386
  retainer->MemoryInfo(this);
284
386
  CHECK_EQ(CurrentNode(), n);
285
386
  CHECK_NE(n->size_, 0);
286
386
  PopNode();
287
}
288
289
void MemoryTracker::TrackInlineField(const MemoryRetainer* retainer,
290
                                     const char* edge_name) {
291
  Track(retainer, edge_name);
292
  CHECK(CurrentNode());
293
  CurrentNode()->size_ -= retainer->SelfSize();
294
}
295
296
12702
MemoryRetainerNode* MemoryTracker::CurrentNode() const {
297
12702
  if (node_stack_.empty()) return nullptr;
298
12683
  return node_stack_.top();
299
}
300
301
386
MemoryRetainerNode* MemoryTracker::AddNode(const MemoryRetainer* retainer,
302
                                           const char* edge_name) {
303
386
  auto it = seen_.find(retainer);
304
386
  if (it != seen_.end()) {
305
    return it->second;
306
  }
307
308
386
  MemoryRetainerNode* n = new MemoryRetainerNode(this, retainer);
309
386
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
310
386
  seen_[retainer] = n;
311
386
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
312
313
386
  if (n->JSWrapperNode() != nullptr) {
314
291
    graph_->AddEdge(n, n->JSWrapperNode(), "wrapped");
315
291
    graph_->AddEdge(n->JSWrapperNode(), n, "wrapper");
316
  }
317
318
386
  return n;
319
}
320
321
2422
MemoryRetainerNode* MemoryTracker::AddNode(const char* node_name,
322
                                           size_t size,
323
                                           const char* edge_name) {
324
2422
  MemoryRetainerNode* n = new MemoryRetainerNode(this, node_name, size);
325
2422
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
326
327
2422
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
328
329
2422
  return n;
330
}
331
332
386
MemoryRetainerNode* MemoryTracker::PushNode(const MemoryRetainer* retainer,
333
                                            const char* edge_name) {
334
386
  MemoryRetainerNode* n = AddNode(retainer, edge_name);
335
386
  node_stack_.push(n);
336
386
  return n;
337
}
338
339
400
MemoryRetainerNode* MemoryTracker::PushNode(const char* node_name,
340
                                            size_t size,
341
                                            const char* edge_name) {
342
400
  MemoryRetainerNode* n = AddNode(node_name, size, edge_name);
343
400
  node_stack_.push(n);
344
400
  return n;
345
}
346
347
786
void MemoryTracker::PopNode() {
348
786
  node_stack_.pop();
349
786
}
350
351
}  // namespace node
352
353
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
354
355
#endif  // SRC_MEMORY_TRACKER_INL_H_