src: fix AliasedBuffer memory attribution in heap snapshots

Before the AliasedBuffers were represented solely by the TypedArrays
event though they also retain additional memory themselves.
Make the accounting more accurate by implementing MemoryRetainer in
AliasedBuffer.

PR-URL: https://github.com/nodejs/node/pull/46817
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Joyee Cheung 2023-02-24 15:51:07 +01:00
parent 8b2126f63f
commit 3646a66044
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
5 changed files with 28 additions and 15 deletions

View File

@ -206,6 +206,25 @@ void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
count_ = new_capacity;
}
template <typename NativeT, typename V8T>
inline size_t AliasedBufferBase<NativeT, V8T>::SelfSize() const {
return sizeof(*this);
}
#define VM(NativeT, V8T) \
template <> \
inline const char* AliasedBufferBase<NativeT, v8::V8T>::MemoryInfoName() \
const { \
return "Aliased" #V8T; \
} \
template <> \
inline void AliasedBufferBase<NativeT, v8::V8T>::MemoryInfo( \
node::MemoryTracker* tracker) const { \
tracker->TrackField("js_array", js_array_); \
}
ALIASED_BUFFER_LIST(VM)
#undef VM
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

View File

@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <cinttypes>
#include "memory_tracker.h"
#include "v8.h"
namespace node {
@ -28,7 +29,7 @@ typedef size_t AliasedBufferIndex;
* observed. Any notification APIs will be left as a future exercise.
*/
template <class NativeT, class V8T>
class AliasedBufferBase {
class AliasedBufferBase : public MemoryRetainer {
public:
static_assert(std::is_scalar<NativeT>::value);
@ -157,6 +158,11 @@ class AliasedBufferBase {
// an owning `AliasedBufferBase`.
void reserve(size_t new_capacity);
inline size_t SelfSize() const override;
inline const char* MemoryInfoName() const override;
inline void MemoryInfo(node::MemoryTracker* tracker) const override;
private:
v8::Isolate* isolate_ = nullptr;
size_t count_ = 0;

View File

@ -3,8 +3,8 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "aliased_buffer-inl.h"
#include "memory_tracker.h"
#include "util-inl.h"
namespace node {
@ -270,13 +270,6 @@ void MemoryTracker::TrackInlineField(const char* name,
TrackInlineFieldWithSize(name, sizeof(value), "uv_async_t");
}
template <class NativeT, class V8T>
void MemoryTracker::TrackField(const char* name,
const AliasedBufferBase<NativeT, V8T>& value,
const char* node_name) {
TrackField(name, value.GetJSArray(), "AliasedBuffer");
}
void MemoryTracker::Track(const MemoryRetainer* retainer,
const char* edge_name) {
v8::HandleScope handle_scope(isolate_);

View File

@ -2,7 +2,6 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "aliased_buffer.h"
#include "v8-profiler.h"
#include <uv.h>
@ -238,10 +237,6 @@ class MemoryTracker {
inline void TrackInlineField(const char* edge_name,
const uv_async_t& value,
const char* node_name = nullptr);
template <class NativeT, class V8T>
inline void TrackField(const char* edge_name,
const AliasedBufferBase<NativeT, V8T>& value,
const char* node_name = nullptr);
// Put a memory container into the graph, create an edge from
// the current node if there is one on the stack.

View File

@ -10,7 +10,7 @@ validateSnapshotNodes('Node / FSReqPromise', [
{
children: [
{ node_name: 'FSReqPromise', edge_name: 'native_to_javascript' },
{ node_name: 'Float64Array', edge_name: 'stats_field_array' },
{ node_name: 'Node / AliasedFloat64Array', edge_name: 'stats_field_array' },
],
},
]);