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: 137 142 96.5 %
Date: 2019-03-02 22:23:06 Branches: 53 114 46.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
57
inline const char* GetNodeName(const char* node_name, const char* edge_name) {
13
57
  if (node_name != nullptr) {
14
47
    return node_name;
15
  }
16
10
  if (edge_name != nullptr) {
17
10
    return edge_name;
18
  }
19
  return "";
20
}
21
22
364
class MemoryRetainerNode : public v8::EmbedderGraph::Node {
23
 public:
24
121
  inline MemoryRetainerNode(MemoryTracker* tracker,
25
                                     const MemoryRetainer* retainer)
26
121
      : retainer_(retainer) {
27
121
    CHECK_NOT_NULL(retainer_);
28
121
    v8::HandleScope handle_scope(tracker->isolate());
29
121
    v8::Local<v8::Object> obj = retainer_->WrappedObject();
30
214
    if (!obj.IsEmpty()) wrapper_node_ = tracker->graph()->V8Node(obj);
31
32
121
    name_ = retainer_->MemoryInfoName();
33
121
    size_ = retainer_->SelfSize();
34
121
  }
35
36
61
  inline MemoryRetainerNode(MemoryTracker* tracker,
37
                                     const char* name,
38
                                     size_t size,
39
                                     bool is_root_node = false)
40
61
      : retainer_(nullptr) {
41
61
    name_ = name;
42
61
    size_ = size;
43
61
    is_root_node_ = is_root_node;
44
61
  }
45
46
182
  const char* Name() override { return name_.c_str(); }
47
270
  const char* NamePrefix() override { return "Node /"; }
48
182
  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
182
  bool IsRootNode() override {
55
182
    if (retainer_ != nullptr) {
56
121
      return retainer_->IsRootNode();
57
    }
58
61
    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
65
void MemoryTracker::TrackFieldWithSize(const char* edge_name,
79
                                       size_t size,
80
                                       const char* node_name) {
81
65
  if (size > 0) AddNode(GetNodeName(node_name, edge_name), size, edge_name);
82
65
}
83
84
6
void MemoryTracker::TrackField(const char* edge_name,
85
                               const MemoryRetainer& value,
86
                               const char* node_name) {
87
6
  TrackField(edge_name, &value);
88
6
}
89
90
34
void MemoryTracker::TrackField(const char* edge_name,
91
                               const MemoryRetainer* value,
92
                               const char* node_name) {
93
68
  if (value == nullptr) return;
94
34
  auto it = seen_.find(value);
95
34
  if (it != seen_.end()) {
96
12
    graph_->AddEdge(CurrentNode(), it->second, edge_name);
97
  } else {
98
22
    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
22
    return;
108
  }
109
10
  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









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



20
  for (Iterator it = value.begin(); it != value.end(); ++it) {
128
    // Use nullptr as edge names so the elements appear as indexed properties
129
10
    TrackField(nullptr, *it, element_name);
130
  }
131
10
  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
2
void MemoryTracker::TrackField(const char* name,
203
                               const uv_buf_t& value,
204
                               const char* node_name) {
205
2
  TrackFieldWithSize(name, value.len, "uv_buf_t");
206
2
}
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
8
void MemoryTracker::TrackField(const char* name,
215
                               const uv_async_t& value,
216
                               const char* node_name) {
217
8
  TrackFieldWithSize(name, sizeof(value), "uv_async_t");
218
8
}
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
123
void MemoryTracker::Track(const MemoryRetainer* retainer,
228
                          const char* edge_name) {
229
123
  v8::HandleScope handle_scope(isolate_);
230
123
  auto it = seen_.find(retainer);
231
123
  if (it != seen_.end()) {
232
2
    if (CurrentNode() != nullptr) {
233
      graph_->AddEdge(CurrentNode(), it->second, edge_name);
234
    }
235
125
    return;  // It has already been tracked, no need to call MemoryInfo again
236
  }
237
121
  MemoryRetainerNode* n = PushNode(retainer, edge_name);
238
121
  retainer->MemoryInfo(this);
239
121
  CHECK_EQ(CurrentNode(), n);
240
121
  CHECK_NE(n->size_, 0);
241
121
  PopNode();
242
}
243
244
8
void MemoryTracker::TrackInlineField(const MemoryRetainer* retainer,
245
                                     const char* edge_name) {
246
8
  Track(retainer, edge_name);
247
8
  CHECK(CurrentNode());
248
8
  CurrentNode()->size_ -= retainer->SelfSize();
249
8
}
250
251
451
MemoryRetainerNode* MemoryTracker::CurrentNode() const {
252
451
  if (node_stack_.empty()) return nullptr;
253
358
  return node_stack_.top();
254
}
255
256
121
MemoryRetainerNode* MemoryTracker::AddNode(const MemoryRetainer* retainer,
257
                                           const char* edge_name) {
258
121
  auto it = seen_.find(retainer);
259
121
  if (it != seen_.end()) {
260
    return it->second;
261
  }
262
263
121
  MemoryRetainerNode* n = new MemoryRetainerNode(this, retainer);
264
121
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
265
121
  seen_[retainer] = n;
266
121
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
267
268
121
  if (n->JSWrapperNode() != nullptr) {
269
93
    graph_->AddEdge(n, n->JSWrapperNode(), "wrapped");
270
93
    graph_->AddEdge(n->JSWrapperNode(), n, "wrapper");
271
  }
272
273
121
  return n;
274
}
275
276
61
MemoryRetainerNode* MemoryTracker::AddNode(const char* node_name,
277
                                           size_t size,
278
                                           const char* edge_name) {
279
61
  MemoryRetainerNode* n = new MemoryRetainerNode(this, node_name, size);
280
61
  graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
281
282
61
  if (CurrentNode() != nullptr) graph_->AddEdge(CurrentNode(), n, edge_name);
283
284
61
  return n;
285
}
286
287
121
MemoryRetainerNode* MemoryTracker::PushNode(const MemoryRetainer* retainer,
288
                                            const char* edge_name) {
289
121
  MemoryRetainerNode* n = AddNode(retainer, edge_name);
290
121
  node_stack_.push(n);
291
121
  return n;
292
}
293
294
14
MemoryRetainerNode* MemoryTracker::PushNode(const char* node_name,
295
                                            size_t size,
296
                                            const char* edge_name) {
297
14
  MemoryRetainerNode* n = AddNode(node_name, size, edge_name);
298
14
  node_stack_.push(n);
299
14
  return n;
300
}
301
302
135
void MemoryTracker::PopNode() {
303
135
  node_stack_.pop();
304
135
}
305
306
}  // namespace node
307
308
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
309
310
#endif  // SRC_MEMORY_TRACKER_INL_H_