29
29
namespace node {
30
30
31
31
using v8::Array;
32
- using v8::ArrayBuffer;
33
- using v8::BackingStore;
34
32
using v8::Context;
35
33
using v8::FunctionCallbackInfo;
36
34
using v8::HeapCodeStatistics;
@@ -78,13 +76,29 @@ static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
78
76
HEAP_SPACE_STATISTICS_PROPERTIES (V);
79
77
#undef V
80
78
79
+ #define HEAP_CODE_STATISTICS_PROPERTIES (V ) \
80
+ V (0 , code_and_metadata_size, kCodeAndMetadataSizeIndex ) \
81
+ V (1 , bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex ) \
82
+ V (2 , external_script_source_size, kExternalScriptSourceSizeIndex )
83
+
84
+ #define V (a, b, c ) +1
85
+ static const size_t kHeapCodeStatisticsPropertiesCount =
86
+ HEAP_CODE_STATISTICS_PROPERTIES (V);
87
+ #undef V
88
+
81
89
class BindingData : public BaseObject {
82
90
public:
83
- BindingData (Environment* env, Local<Object> obj) : BaseObject(env, obj) {}
84
-
85
- std::shared_ptr<BackingStore> heap_statistics_buffer;
86
- std::shared_ptr<BackingStore> heap_space_statistics_buffer;
87
- std::shared_ptr<BackingStore> heap_code_statistics_buffer;
91
+ BindingData (Environment* env, Local<Object> obj)
92
+ : BaseObject(env, obj),
93
+ heap_statistics_buffer (env->isolate (), kHeapStatisticsPropertiesCount),
94
+ heap_space_statistics_buffer(env->isolate (),
95
+ kHeapSpaceStatisticsPropertiesCount),
96
+ heap_code_statistics_buffer(env->isolate (),
97
+ kHeapCodeStatisticsPropertiesCount) {}
98
+
99
+ AliasedFloat64Array heap_statistics_buffer;
100
+ AliasedFloat64Array heap_space_statistics_buffer;
101
+ AliasedFloat64Array heap_code_statistics_buffer;
88
102
89
103
void MemoryInfo (MemoryTracker* tracker) const override {
90
104
tracker->TrackField (" heap_statistics_buffer" , heap_statistics_buffer);
@@ -97,15 +111,6 @@ class BindingData : public BaseObject {
97
111
SET_MEMORY_INFO_NAME(BindingData)
98
112
};
99
113
100
- #define HEAP_CODE_STATISTICS_PROPERTIES (V ) \
101
- V (0 , code_and_metadata_size, kCodeAndMetadataSizeIndex ) \
102
- V (1 , bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex ) \
103
- V (2 , external_script_source_size, kExternalScriptSourceSizeIndex )
104
-
105
- #define V (a, b, c ) +1
106
- static const size_t kHeapCodeStatisticsPropertiesCount =
107
- HEAP_CODE_STATISTICS_PROPERTIES (V);
108
- #undef V
109
114
110
115
void CachedDataVersionTag (const FunctionCallbackInfo<Value>& args) {
111
116
Environment* env = Environment::GetCurrent (args);
@@ -115,13 +120,11 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
115
120
args.GetReturnValue ().Set (result);
116
121
}
117
122
118
-
119
- void UpdateHeapStatisticsArrayBuffer (const FunctionCallbackInfo<Value>& args) {
123
+ void UpdateHeapStatisticsBuffer (const FunctionCallbackInfo<Value>& args) {
120
124
BindingData* data = Unwrap<BindingData>(args.Data ());
121
125
HeapStatistics s;
122
126
args.GetIsolate ()->GetHeapStatistics (&s);
123
- double * const buffer =
124
- static_cast <double *>(data->heap_statistics_buffer ->Data ());
127
+ AliasedFloat64Array& buffer = data->heap_statistics_buffer ;
125
128
#define V (index, name, _ ) buffer[index] = static_cast <double >(s.name());
126
129
HEAP_STATISTICS_PROPERTIES (V)
127
130
#undef V
@@ -132,29 +135,23 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
132
135
BindingData* data = Unwrap<BindingData>(args.Data ());
133
136
HeapSpaceStatistics s;
134
137
Isolate* const isolate = args.GetIsolate ();
135
- size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces ();
138
+ CHECK (args[0 ]->IsUint32 ());
139
+ size_t space_index = static_cast <size_t >(args[0 ].As <v8::Uint32>()->Value ());
140
+ isolate->GetHeapSpaceStatistics (&s, space_index);
136
141
137
- double * const buffer =
138
- static_cast <double *>(data->heap_space_statistics_buffer ->Data ());
142
+ AliasedFloat64Array& buffer = data->heap_space_statistics_buffer ;
139
143
140
- for (size_t i = 0 ; i < number_of_heap_spaces; i++) {
141
- isolate->GetHeapSpaceStatistics (&s, i);
142
- size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount ;
143
- #define V (index, name, _ ) \
144
- buffer[property_offset + index ] = static_cast <double >(s.name ());
145
- HEAP_SPACE_STATISTICS_PROPERTIES (V)
144
+ #define V (index, name, _ ) buffer[index] = static_cast <double >(s.name());
145
+ HEAP_SPACE_STATISTICS_PROPERTIES (V)
146
146
#undef V
147
- }
148
147
}
149
148
150
-
151
- void UpdateHeapCodeStatisticsArrayBuffer (
152
- const FunctionCallbackInfo<Value>& args) {
149
+ void UpdateHeapCodeStatisticsBuffer (const FunctionCallbackInfo<Value>& args) {
153
150
BindingData* data = Unwrap<BindingData>(args.Data ());
154
151
HeapCodeStatistics s;
155
152
args.GetIsolate ()->GetHeapCodeAndMetadataStatistics (&s);
156
- double * const buffer =
157
- static_cast < double *>(data-> heap_code_statistics_buffer -> Data ());
153
+ AliasedFloat64Array& buffer = data-> heap_code_statistics_buffer ;
154
+
158
155
#define V (index, name, _ ) buffer[index] = static_cast <double >(s.name());
159
156
HEAP_CODE_STATISTICS_PROPERTIES (V)
160
157
#undef V
@@ -181,20 +178,14 @@ void Initialize(Local<Object> target,
181
178
CachedDataVersionTag);
182
179
183
180
// Export symbols used by v8.getHeapStatistics()
184
- env->SetMethod (target,
185
- " updateHeapStatisticsArrayBuffer" ,
186
- UpdateHeapStatisticsArrayBuffer);
181
+ env->SetMethod (
182
+ target, " updateHeapStatisticsBuffer" , UpdateHeapStatisticsBuffer);
187
183
188
- const size_t heap_statistics_buffer_byte_length =
189
- sizeof (double ) * kHeapStatisticsPropertiesCount ;
190
-
191
- Local<ArrayBuffer> heap_statistics_ab =
192
- ArrayBuffer::New (env->isolate (), heap_statistics_buffer_byte_length);
193
- binding_data->heap_statistics_buffer = heap_statistics_ab->GetBackingStore ();
194
- target->Set (env->context (),
195
- FIXED_ONE_BYTE_STRING (env->isolate (),
196
- " heapStatisticsArrayBuffer" ),
197
- heap_statistics_ab).Check ();
184
+ target
185
+ ->Set (env->context (),
186
+ FIXED_ONE_BYTE_STRING (env->isolate (), " heapStatisticsBuffer" ),
187
+ binding_data->heap_statistics_buffer .GetJSArray ())
188
+ .Check ();
198
189
199
190
#define V (i, _, name ) \
200
191
target->Set (env->context (), \
@@ -205,22 +196,14 @@ void Initialize(Local<Object> target,
205
196
#undef V
206
197
207
198
// Export symbols used by v8.getHeapCodeStatistics()
208
- env->SetMethod (target,
209
- " updateHeapCodeStatisticsArrayBuffer" ,
210
- UpdateHeapCodeStatisticsArrayBuffer);
211
-
212
- const size_t heap_code_statistics_buffer_byte_length =
213
- sizeof (double ) * kHeapCodeStatisticsPropertiesCount ;
199
+ env->SetMethod (
200
+ target, " updateHeapCodeStatisticsBuffer" , UpdateHeapCodeStatisticsBuffer);
214
201
215
- Local<ArrayBuffer> heap_code_statistics_ab =
216
- ArrayBuffer::New (env->isolate (),
217
- heap_code_statistics_buffer_byte_length);
218
- binding_data->heap_code_statistics_buffer =
219
- heap_code_statistics_ab->GetBackingStore ();
220
- target->Set (env->context (),
221
- FIXED_ONE_BYTE_STRING (env->isolate (),
222
- " heapCodeStatisticsArrayBuffer" ),
223
- heap_code_statistics_ab).Check ();
202
+ target
203
+ ->Set (env->context (),
204
+ FIXED_ONE_BYTE_STRING (env->isolate (), " heapCodeStatisticsBuffer" ),
205
+ binding_data->heap_code_statistics_buffer .GetJSArray ())
206
+ .Check ();
224
207
225
208
#define V (i, _, name ) \
226
209
target->Set (env->context (), \
@@ -230,14 +213,6 @@ void Initialize(Local<Object> target,
230
213
HEAP_CODE_STATISTICS_PROPERTIES (V)
231
214
#undef V
232
215
233
- // Export symbols used by v8.getHeapSpaceStatistics()
234
- target->Set (env->context (),
235
- FIXED_ONE_BYTE_STRING (env->isolate (),
236
- " kHeapSpaceStatisticsPropertiesCount" ),
237
- Uint32::NewFromUnsigned (env->isolate (),
238
- kHeapSpaceStatisticsPropertiesCount ))
239
- .Check ();
240
-
241
216
size_t number_of_heap_spaces = env->isolate ()->NumberOfHeapSpaces ();
242
217
243
218
// Heap space names are extracted once and exposed to JavaScript to
@@ -258,24 +233,15 @@ void Initialize(Local<Object> target,
258
233
number_of_heap_spaces)).Check ();
259
234
260
235
env->SetMethod (target,
261
- " updateHeapSpaceStatisticsArrayBuffer " ,
236
+ " updateHeapSpaceStatisticsBuffer " ,
262
237
UpdateHeapSpaceStatisticsBuffer);
263
238
264
- const size_t heap_space_statistics_buffer_byte_length =
265
- sizeof (double ) *
266
- kHeapSpaceStatisticsPropertiesCount *
267
- number_of_heap_spaces;
268
-
269
- Local<ArrayBuffer> heap_space_statistics_ab =
270
- ArrayBuffer::New (env->isolate (),
271
- heap_space_statistics_buffer_byte_length);
272
- binding_data->heap_space_statistics_buffer =
273
- heap_space_statistics_ab->GetBackingStore ();
274
-
275
- target->Set (env->context (),
276
- FIXED_ONE_BYTE_STRING (env->isolate (),
277
- " heapSpaceStatisticsArrayBuffer" ),
278
- heap_space_statistics_ab).Check ();
239
+ target
240
+ ->Set (env->context (),
241
+ FIXED_ONE_BYTE_STRING (env->isolate (),
242
+ " heapSpaceStatisticsBuffer" ),
243
+ binding_data->heap_space_statistics_buffer .GetJSArray ())
244
+ .Check ();
279
245
280
246
#define V (i, _, name ) \
281
247
target->Set (env->context (), \
0 commit comments