From 57e301539bff2599974b7269a56377330c9b730e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 10 Jun 2018 16:40:13 +0200 Subject: src: enable more detailed memory tracking This will enable more detailed heap snapshots based on a newer V8 API. This commit itself is not tied to that API and could be backported. PR-URL: https://github.com/nodejs/node/pull/21742 Reviewed-By: James M Snell --- src/memory_tracker.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/memory_tracker.h (limited to 'src/memory_tracker.h') diff --git a/src/memory_tracker.h b/src/memory_tracker.h new file mode 100644 index 0000000000..18822651f6 --- /dev/null +++ b/src/memory_tracker.h @@ -0,0 +1,87 @@ +#ifndef SRC_MEMORY_TRACKER_H_ +#define SRC_MEMORY_TRACKER_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include +#include +#include +#include +#include + +namespace node { + +class MemoryTracker; + +namespace crypto { +class NodeBIO; +} + +class MemoryRetainer { + public: + virtual ~MemoryRetainer() {} + + // Subclasses should implement this to provide information for heap snapshots. + virtual void MemoryInfo(MemoryTracker* tracker) const = 0; + + virtual v8::Local WrappedObject() const { + return v8::Local(); + } + + virtual bool IsRootNode() const { return false; } +}; + +class MemoryTracker { + public: + template + inline void TrackThis(const T* obj); + + inline void TrackFieldWithSize(const char* name, size_t size); + + inline void TrackField(const char* name, const MemoryRetainer& value); + inline void TrackField(const char* name, const MemoryRetainer* value); + template + inline void TrackField(const char* name, const std::unique_ptr& value); + template + inline void TrackField(const char* name, const T& value); + template + inline void TrackField(const char* name, const std::queue& value); + template + inline void TrackField(const char* name, const std::basic_string& value); + template ::is_specialized, bool>::type, + typename dummy = bool> + inline void TrackField(const char* name, const T& value); + template + inline void TrackField(const char* name, const std::pair& value); + template + inline void TrackField(const char* name, + const v8::Persistent& value); + template + inline void TrackField(const char* name, const v8::Local& value); + template + inline void TrackField(const char* name, const MallocedBuffer& value); + inline void TrackField(const char* name, const uv_buf_t& value); + template + inline void TrackField(const char* name, + const AliasedBuffer& value); + + inline void Track(const MemoryRetainer* value); + inline size_t accumulated_size() const { return accumulated_size_; } + + inline void set_track_only_self(bool value) { track_only_self_ = value; } + + inline MemoryTracker() {} + + private: + bool track_only_self_ = false; + size_t accumulated_size_ = 0; + std::unordered_set seen_; +}; + +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_MEMORY_TRACKER_H_ -- cgit v1.2.3