Skip to content

Commit 66e4ba5

Browse files
joyeecheungtargos
authored andcommitted
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: #46817 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 946f19b commit 66e4ba5

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

src/aliased_buffer-inl.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,25 @@ void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
206206
count_ = new_capacity;
207207
}
208208

209+
template <typename NativeT, typename V8T>
210+
inline size_t AliasedBufferBase<NativeT, V8T>::SelfSize() const {
211+
return sizeof(*this);
212+
}
213+
214+
#define VM(NativeT, V8T) \
215+
template <> \
216+
inline const char* AliasedBufferBase<NativeT, v8::V8T>::MemoryInfoName() \
217+
const { \
218+
return "Aliased" #V8T; \
219+
} \
220+
template <> \
221+
inline void AliasedBufferBase<NativeT, v8::V8T>::MemoryInfo( \
222+
node::MemoryTracker* tracker) const { \
223+
tracker->TrackField("js_array", js_array_); \
224+
}
225+
ALIASED_BUFFER_LIST(VM)
226+
#undef VM
227+
209228
} // namespace node
210229

211230
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/aliased_buffer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include <cinttypes>
7+
#include "memory_tracker.h"
78
#include "v8.h"
89

910
namespace node {
@@ -28,7 +29,7 @@ typedef size_t AliasedBufferIndex;
2829
* observed. Any notification APIs will be left as a future exercise.
2930
*/
3031
template <class NativeT, class V8T>
31-
class AliasedBufferBase {
32+
class AliasedBufferBase : public MemoryRetainer {
3233
public:
3334
static_assert(std::is_scalar<NativeT>::value);
3435

@@ -157,6 +158,11 @@ class AliasedBufferBase {
157158
// an owning `AliasedBufferBase`.
158159
void reserve(size_t new_capacity);
159160

161+
inline size_t SelfSize() const override;
162+
163+
inline const char* MemoryInfoName() const override;
164+
inline void MemoryInfo(node::MemoryTracker* tracker) const override;
165+
160166
private:
161167
v8::Isolate* isolate_ = nullptr;
162168
size_t count_ = 0;

src/memory_tracker-inl.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6-
#include "aliased_buffer-inl.h"
76
#include "memory_tracker.h"
7+
#include "util-inl.h"
88

99
namespace node {
1010

@@ -270,13 +270,6 @@ void MemoryTracker::TrackInlineField(const char* name,
270270
TrackInlineFieldWithSize(name, sizeof(value), "uv_async_t");
271271
}
272272

273-
template <class NativeT, class V8T>
274-
void MemoryTracker::TrackField(const char* name,
275-
const AliasedBufferBase<NativeT, V8T>& value,
276-
const char* node_name) {
277-
TrackField(name, value.GetJSArray(), "AliasedBuffer");
278-
}
279-
280273
void MemoryTracker::Track(const MemoryRetainer* retainer,
281274
const char* edge_name) {
282275
v8::HandleScope handle_scope(isolate_);

src/memory_tracker.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
44

5-
#include "aliased_buffer.h"
65
#include "v8-profiler.h"
76

87
#include <uv.h>
@@ -238,10 +237,6 @@ class MemoryTracker {
238237
inline void TrackInlineField(const char* edge_name,
239238
const uv_async_t& value,
240239
const char* node_name = nullptr);
241-
template <class NativeT, class V8T>
242-
inline void TrackField(const char* edge_name,
243-
const AliasedBufferBase<NativeT, V8T>& value,
244-
const char* node_name = nullptr);
245240

246241
// Put a memory container into the graph, create an edge from
247242
// the current node if there is one on the stack.

test/pummel/test-heapdump-fs-promise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ validateSnapshotNodes('Node / FSReqPromise', [
1010
{
1111
children: [
1212
{ node_name: 'FSReqPromise', edge_name: 'native_to_javascript' },
13-
{ node_name: 'Float64Array', edge_name: 'stats_field_array' },
13+
{ node_name: 'Node / AliasedFloat64Array', edge_name: 'stats_field_array' },
1414
],
1515
},
1616
]);

0 commit comments

Comments
 (0)