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.h Lines: 10 10 100.0 %
Date: 2019-09-15 22:29:17 Branches: 1 2 50.0 %

Line Branch Exec Source
1
#pragma once
2
3
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
4
5
#include "aliased_buffer.h"
6
#include "v8-profiler.h"
7
8
#include <uv.h>
9
10
#include <limits>
11
#include <queue>
12
#include <stack>
13
#include <string>
14
#include <unordered_map>
15
16
namespace node {
17
18
// Set the node name of a MemoryRetainer to klass
19
#define SET_MEMORY_INFO_NAME(Klass)                                            \
20
  inline std::string MemoryInfoName() const override { return #Klass; }
21
22
// Set the self size of a MemoryRetainer to the stack-allocated size of a
23
// certain class
24
#define SET_SELF_SIZE(Klass)                                                   \
25
  inline size_t SelfSize() const override { return sizeof(Klass); }
26
27
// Used when there is no additional fields to track
28
#define SET_NO_MEMORY_INFO()                                                   \
29
  inline void MemoryInfo(node::MemoryTracker* tracker) const override {}
30
31
class MemoryTracker;
32
class MemoryRetainerNode;
33
34
namespace crypto {
35
class NodeBIO;
36
}
37
38
class CleanupHookCallback;
39
40
/* Example:
41
 *
42
 * class ExampleRetainer : public MemoryRetainer {
43
 *   public:
44
 *     // Or use SET_NO_MEMORY_INFO() when there is no additional fields
45
 *     // to track.
46
 *     void MemoryInfo(MemoryTracker* tracker) const override {
47
 *       // Node name and size comes from the MemoryInfoName and SelfSize of
48
 *       // AnotherRetainerClass
49
 *       tracker->TrackField("another_retainer", another_retainer_);
50
 *
51
 *       // Add non_pointer_retainer as a separate node into the graph
52
 *       // and track its memory information recursively.
53
 *       // Note that we need to make sure its size is not accounted in
54
 *       // ExampleRetainer::SelfSize().
55
 *       tracker->TrackField("non_pointer_retainer", &non_pointer_retainer_);
56
 *
57
 *       // Specify node name and size explicitly
58
 *       tracker->TrackFieldWithSize("internal_member",
59
 *                                   internal_member_.size(),
60
 *                                   "InternalClass");
61
 *       // Node name falls back to the edge name,
62
 *       // elements in the container appear as grandchildren nodes
63
 *       tracker->TrackField("vector", vector_);
64
 *       // Node name and size come from the JS object
65
 *       tracker->TrackField("target", target_);
66
 *     }
67
 *
68
 *     // Or use SET_MEMORY_INFO_NAME(ExampleRetainer)
69
 *     std::string MemoryInfoName() const override {
70
 *       return "ExampleRetainer";
71
 *     }
72
 *
73
 *     // Classes that only want to return its sizeof() value can use the
74
 *     // SET_SELF_SIZE(Class) macro instead.
75
 *     size_t SelfSize() const override {
76
 *       // We need to exclude the size of non_pointer_retainer so that
77
 *       // we can track it separately in ExampleRetainer::MemoryInfo().
78
 *       return sizeof(ExampleRetainer) - sizeof(NonPointerRetainerClass);
79
 *     }
80
 *
81
 *     // Note: no need to implement these two methods when implementing
82
 *     // a BaseObject or an AsyncWrap class
83
 *     bool IsRootNode() const override { return !wrapped_.IsWeak(); }
84
 *     v8::Local<v8::Object> WrappedObject() const override {
85
 *       return node::PersistentToLocal::Default(wrapped_);
86
 *     }
87
 *
88
 *   private:
89
 *     AnotherRetainerClass* another_retainer_;
90
 *     NonPointerRetainerClass non_pointer_retainer;
91
 *     InternalClass internal_member_;
92
 *     std::vector<uv_async_t> vector_;
93
 *     v8::Global<Object> target_;
94
 *
95
 *     v8::Global<Object> wrapped_;
96
 * }
97
 *
98
 * This creates the following graph:
99
 *   Node / ExampleRetainer
100
 *    |> another_retainer :: Node / AnotherRetainerClass
101
 *    |> internal_member :: Node / InternalClass
102
 *    |> vector :: Node / vector (elements will be grandchildren)
103
 *        |> [1] :: Node / uv_async_t (uv_async_t has predefined names)
104
 *        |> [2] :: Node / uv_async_t
105
 *        |> ...
106
 *    |> target :: TargetClass (JS class name of the target object)
107
 *    |> wrapped :: WrappedClass (JS class name of the wrapped object)
108
 *        |> wrapper :: Node / ExampleRetainer (back reference)
109
 */
110
1566038
class MemoryRetainer {
111
 public:
112
1579444
  virtual ~MemoryRetainer() = default;
113
114
  // Subclasses should implement these methods to provide information
115
  // for the V8 heap snapshot generator.
116
  // The MemoryInfo() method is assumed to be called within a context
117
  // where all the edges start from the node of the current retainer,
118
  // and point to the nodes as specified by tracker->Track* calls.
119
  virtual void MemoryInfo(MemoryTracker* tracker) const = 0;
120
  virtual std::string MemoryInfoName() const = 0;
121
  virtual size_t SelfSize() const = 0;
122
123
330
  virtual v8::Local<v8::Object> WrappedObject() const {
124
330
    return v8::Local<v8::Object>();
125
  }
126
127
1047
  virtual bool IsRootNode() const { return false; }
128
};
129
130
51
class MemoryTracker {
131
 public:
132
  // Used to specify node name and size explicitly
133
  inline void TrackFieldWithSize(const char* edge_name,
134
                                 size_t size,
135
                                 const char* node_name = nullptr);
136
  // Shortcut to extract the underlying object out of the smart pointer
137
  template <typename T>
138
  inline void TrackField(const char* edge_name,
139
                         const std::unique_ptr<T>& value,
140
                         const char* node_name = nullptr);
141
142
  // For containers, the elements will be graphed as grandchildren nodes
143
  // if the container is not empty.
144
  // By default, we assume the parent count the stack size of the container
145
  // into its SelfSize so that will be subtracted from the parent size when we
146
  // spin off a new node for the container.
147
  // TODO(joyeecheung): use RTTI to retrieve the class name at runtime?
148
  template <typename T, typename Iterator = typename T::const_iterator>
149
  inline void TrackField(const char* edge_name,
150
                         const T& value,
151
                         const char* node_name = nullptr,
152
                         const char* element_name = nullptr,
153
                         bool subtract_from_self = true);
154
  template <typename T>
155
  inline void TrackField(const char* edge_name,
156
                         const std::queue<T>& value,
157
                         const char* node_name = nullptr,
158
                         const char* element_name = nullptr);
159
  template <typename T, typename U>
160
  inline void TrackField(const char* edge_name,
161
                         const std::pair<T, U>& value,
162
                         const char* node_name = nullptr);
163
164
  // For the following types, node_name will be ignored and predefined names
165
  // will be used instead. They are only in the signature for template
166
  // expansion.
167
  inline void TrackField(const char* edge_name,
168
                         const MemoryRetainer& value,
169
                         const char* node_name = nullptr);
170
  inline void TrackField(const char* edge_name,
171
                         const MemoryRetainer* value,
172
                         const char* node_name = nullptr);
173
  template <typename T>
174
  inline void TrackField(const char* edge_name,
175
                         const std::basic_string<T>& value,
176
                         const char* node_name = nullptr);
177
  template <typename T,
178
            typename test_for_number = typename std::
179
                enable_if<std::numeric_limits<T>::is_specialized, bool>::type,
180
            typename dummy = bool>
181
  inline void TrackField(const char* edge_name,
182
                         const T& value,
183
                         const char* node_name = nullptr);
184
  template <typename T>
185
  void TrackField(const char* edge_name,
186
                  const v8::Eternal<T>& value,
187
                  const char* node_name);
188
  template <typename T>
189
  inline void TrackField(const char* edge_name,
190
                         const v8::PersistentBase<T>& value,
191
                         const char* node_name = nullptr);
192
  template <typename T>
193
  inline void TrackField(const char* edge_name,
194
                         const v8::Local<T>& value,
195
                         const char* node_name = nullptr);
196
  template <typename T>
197
  inline void TrackField(const char* edge_name,
198
                         const MallocedBuffer<T>& value,
199
                         const char* node_name = nullptr);
200
  // We do not implement CleanupHookCallback as MemoryRetainer
201
  // but instead specialize the method here to avoid the cost of
202
  // virtual pointers.
203
  // TODO(joyeecheung): do this for BaseObject and remove WrappedObject()
204
  void TrackField(const char* edge_name,
205
                  const CleanupHookCallback& value,
206
                  const char* node_name = nullptr);
207
  inline void TrackField(const char* edge_name,
208
                         const uv_buf_t& value,
209
                         const char* node_name = nullptr);
210
  inline void TrackField(const char* edge_name,
211
                         const uv_timer_t& value,
212
                         const char* node_name = nullptr);
213
  inline void TrackField(const char* edge_name,
214
                         const uv_async_t& value,
215
                         const char* node_name = nullptr);
216
  template <class NativeT, class V8T>
217
  inline void TrackField(const char* edge_name,
218
                         const AliasedBufferBase<NativeT, V8T>& value,
219
                         const char* node_name = nullptr);
220
221
  // Put a memory container into the graph, create an edge from
222
  // the current node if there is one on the stack.
223
  inline void Track(const MemoryRetainer* retainer,
224
                    const char* edge_name = nullptr);
225
226
  // Useful for parents that do not wish to perform manual
227
  // adjustments to its `SelfSize()` when embedding retainer
228
  // objects inline.
229
  // Put a memory container into the graph, create an edge from
230
  // the current node if there is one on the stack - there should
231
  // be one, of the container object which the current field is part of.
232
  // Reduce the size of memory from the container so as to avoid
233
  // duplication in accounting.
234
  inline void TrackInlineField(const MemoryRetainer* retainer,
235
                               const char* edge_name = nullptr);
236
237
351
  inline v8::EmbedderGraph* graph() { return graph_; }
238
681
  inline v8::Isolate* isolate() { return isolate_; }
239
240
51
  inline explicit MemoryTracker(v8::Isolate* isolate,
241
                                v8::EmbedderGraph* graph)
242
51
    : isolate_(isolate), graph_(graph) {}
243
244
 private:
245
  typedef std::unordered_map<const MemoryRetainer*, MemoryRetainerNode*>
246
      NodeMap;
247
248
  inline MemoryRetainerNode* CurrentNode() const;
249
  inline MemoryRetainerNode* AddNode(const MemoryRetainer* retainer,
250
                                     const char* edge_name = nullptr);
251
  inline MemoryRetainerNode* PushNode(const MemoryRetainer* retainer,
252
                                      const char* edge_name = nullptr);
253
  inline MemoryRetainerNode* AddNode(const char* node_name,
254
                                     size_t size,
255
                                     const char* edge_name = nullptr);
256
  inline MemoryRetainerNode* PushNode(const char* node_name,
257
                                      size_t size,
258
                                      const char* edge_name = nullptr);
259
  inline void PopNode();
260
261
  v8::Isolate* isolate_;
262
  v8::EmbedderGraph* graph_;
263
  std::stack<MemoryRetainerNode*> node_stack_;
264
  NodeMap seen_;
265
};
266
267
}  // namespace node
268
269
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS