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: 132 137 96.4 %
Date: 2019-02-23 22:23:05 Branches: 58 112 51.8 %

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

16
  if (value.get() == nullptr) {
107
24
    return;
108
  }
109
8
  TrackField(edge_name, value.get(), node_name);
110
}
111
112
template <typename T, typename Iterator>
113
52
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



104
  if (value.begin() == value.end()) return;
121
  // Fall back to edge name if node names are not provided
122









12
  if (CurrentNode() != nullptr && subtract_from_self) {
123
    // Shift the self size of this container out to a separate node
124
12
    CurrentNode()->size_ -= sizeof(T);
125
  }
126
12
  PushNode(GetNodeName(node_name, edge_name), sizeof(T), edge_name);
127



24
  for (Iterator it = value.begin(); it != value.end(); ++it) {
128
    // Use nullptr as edge names so the elements appear as indexed properties
129
12
    TrackField(nullptr, *it, element_name);
130
  }
131
12
  PopNode();
132
}
133
134
template <typename T>
135
12
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
12
    static const typename std::queue<T>::container_type& Get(
141
        const std::queue<T>& value) {
142
12
      return value.*&ContainerGetter::c;
143
    }
144
  };
145
146
12
  const auto& container = ContainerGetter::Get(value);
147
12
  TrackField(edge_name, container, node_name, element_name);
148
12
}
149
150
template <typename T, typename test_for_number, typename dummy>
151
4
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
4
  CurrentNode()->size_ += sizeof(T);
156
4
}
157
158
template <typename T, typename U>
159
4
void MemoryTracker::TrackField(const char* edge_name,
160
                               const std::pair<T, U>& value,
161
                               const char* node_name) {
162
4
  PushNode(node_name == nullptr ? "pair" : node_name,
163
           sizeof(const std::pair<T, U>),
164
4
           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
4
  TrackField("first", value.first);
169
4
  TrackField("second", value.second);
170
4
  PopNode();
171
4
}
172
173
template <typename T>
174
2
void MemoryTracker::TrackField(const char* edge_name,
175
                               const std::basic_string<T>& value,
176
                               const char* node_name) {
177
2
  TrackFieldWithSize(edge_name, value.size() * sizeof(T), "std::basic_string");
178
2
}
179
180
template <typename T, typename Traits>
181
1
void MemoryTracker::TrackField(const char* edge_name,
182
                               const v8::Persistent<T, Traits>& value,
183
                               const char* node_name) {
184
2
  TrackField(edge_name, value.Get(isolate_));
185
1
}
186
187
template <typename T>
188
3
void MemoryTracker::TrackField(const char* edge_name,
189
                               const v8::Local<T>& value,
190
                               const char* node_name) {
191


3
  if (!value.IsEmpty())
192
6
    graph_->AddEdge(CurrentNode(), graph_->V8Node(value), edge_name);
193
3
}
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
4
void MemoryTracker::TrackField(const char* name,
203
                               const uv_buf_t& value,
204
                               const char* node_name) {
205
4
  TrackFieldWithSize(name, value.len, "uv_buf_t");
206
4
}
207
208
2
void MemoryTracker::TrackField(const char* name,
209
                               const uv_timer_t& value,
210
                               const char* node_name) {
211
2
  TrackFieldWithSize(name, sizeof(value), "uv_timer_t");
212
2
}
213
214
4
void MemoryTracker::TrackField(const char* name,
215
                               const uv_async_t& value,
216
                               const char* node_name) {
217
4
  TrackFieldWithSize(name, sizeof(value), "uv_async_t");
218
4
}
219
220
template <class NativeT, class V8T>
221
2
void MemoryTracker::TrackField(const char* name,
222
                               const AliasedBuffer<NativeT, V8T>& value,
223
                               const char* node_name) {
224
2
  TrackField(name, value.GetJSArray(), "AliasedBuffer");
225
2
}
226
227
119
void MemoryTracker::Track(const MemoryRetainer* retainer,
228
                          const char* edge_name) {
229
119
  v8::HandleScope handle_scope(isolate_);
230
119
  auto it = seen_.find(retainer);
231
119
  if (it != seen_.end()) {
232
2
    if (CurrentNode() != nullptr) {
233
      graph_->AddEdge(CurrentNode(), it->second, edge_name);
234
    }
235
121
    return;  // It has already been tracked, no need to call MemoryInfo again
236
  }
237
117
  MemoryRetainerNode* n = PushNode(retainer, edge_name);
238
117
  retainer->MemoryInfo(this);
239
117
  CHECK_EQ(CurrentNode(), n);
240
117
  CHECK_NE(n->size_, 0);
241
117
  PopNode();
242
}
243
244
441
MemoryRetainerNode* MemoryTracker::CurrentNode() const {
245
441
  if (node_stack_.empty()) return nullptr;
246
346
  return node_stack_.top();
247
}
248
249
117
MemoryRetainerNode* MemoryTracker::AddNode(const MemoryRetainer* retainer,
250
                                           const char* edge_name) {
251
117
  auto it = seen_.find(retainer);
252
117
  if (it != seen_.end()) {
253
    return it->second;
254
  }
255
256
117
  MemoryRetainerNode* n = new MemoryRetainerNode(this, retainer);
257
117
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
258
117
  seen_[retainer] = n;
259
117
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
260
261
117
  if (n->JSWrapperNode() != nullptr) {
262
95
    graph_->AddEdge(n, n->JSWrapperNode(), "wrapped");
263
95
    graph_->AddEdge(n->JSWrapperNode(), n, "wrapper");
264
  }
265
266
117
  return n;
267
}
268
269
69
MemoryRetainerNode* MemoryTracker::AddNode(const char* node_name,
270
                                           size_t size,
271
                                           const char* edge_name) {
272
69
  MemoryRetainerNode* n = new MemoryRetainerNode(this, node_name, size);
273
69
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
274
275
69
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
276
277
69
  return n;
278
}
279
280
117
MemoryRetainerNode* MemoryTracker::PushNode(const MemoryRetainer* retainer,
281
                                            const char* edge_name) {
282
117
  MemoryRetainerNode* n = AddNode(retainer, edge_name);
283
117
  node_stack_.push(n);
284
117
  return n;
285
}
286
287
16
MemoryRetainerNode* MemoryTracker::PushNode(const char* node_name,
288
                                            size_t size,
289
                                            const char* edge_name) {
290
16
  MemoryRetainerNode* n = AddNode(node_name, size, edge_name);
291
16
  node_stack_.push(n);
292
16
  return n;
293
}
294
295
133
void MemoryTracker::PopNode() {
296
133
  node_stack_.pop();
297
133
}
298
299
}  // namespace node
300
301
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
302
303
#endif  // SRC_MEMORY_TRACKER_INL_H_