Skip to content

Commit f127e0c

Browse files
bnoordhuisitaloacasas
authored andcommitted
lib,src: support values > 4GB in heap statistics
We were transporting the heap statistics as uint32 values to JS land but those wrap around for values > 4 GB. Use 64 bits floats instead, those should last us a while. Fixes: nodejs#10185 PR-URL: nodejs#10186 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent aa6b9f9 commit f127e0c

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

lib/v8.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const v8binding = process.binding('v8');
1818

1919
// Properties for heap statistics buffer extraction.
2020
const heapStatisticsBuffer =
21-
new Uint32Array(v8binding.heapStatisticsArrayBuffer);
21+
new Float64Array(v8binding.heapStatisticsArrayBuffer);
2222
const kTotalHeapSizeIndex = v8binding.kTotalHeapSizeIndex;
2323
const kTotalHeapSizeExecutableIndex = v8binding.kTotalHeapSizeExecutableIndex;
2424
const kTotalPhysicalSizeIndex = v8binding.kTotalPhysicalSizeIndex;
@@ -31,7 +31,7 @@ const kPeakMallocedMemoryIndex = v8binding.kPeakMallocedMemoryIndex;
3131

3232
// Properties for heap space statistics buffer extraction.
3333
const heapSpaceStatisticsBuffer =
34-
new Uint32Array(v8binding.heapSpaceStatisticsArrayBuffer);
34+
new Float64Array(v8binding.heapSpaceStatisticsArrayBuffer);
3535
const kHeapSpaces = v8binding.kHeapSpaces;
3636
const kNumberOfHeapSpaces = kHeapSpaces.length;
3737
const kHeapSpaceStatisticsPropertiesCount =

src/env-inl.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,22 @@ inline std::vector<int64_t>* Environment::destroy_ids_list() {
316316
return &destroy_ids_list_;
317317
}
318318

319-
inline uint32_t* Environment::heap_statistics_buffer() const {
319+
inline double* Environment::heap_statistics_buffer() const {
320320
CHECK_NE(heap_statistics_buffer_, nullptr);
321321
return heap_statistics_buffer_;
322322
}
323323

324-
inline void Environment::set_heap_statistics_buffer(uint32_t* pointer) {
324+
inline void Environment::set_heap_statistics_buffer(double* pointer) {
325325
CHECK_EQ(heap_statistics_buffer_, nullptr); // Should be set only once.
326326
heap_statistics_buffer_ = pointer;
327327
}
328328

329-
inline uint32_t* Environment::heap_space_statistics_buffer() const {
329+
inline double* Environment::heap_space_statistics_buffer() const {
330330
CHECK_NE(heap_space_statistics_buffer_, nullptr);
331331
return heap_space_statistics_buffer_;
332332
}
333333

334-
inline void Environment::set_heap_space_statistics_buffer(uint32_t* pointer) {
334+
inline void Environment::set_heap_space_statistics_buffer(double* pointer) {
335335
CHECK_EQ(heap_space_statistics_buffer_, nullptr); // Should be set only once.
336336
heap_space_statistics_buffer_ = pointer;
337337
}

src/env.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,11 @@ class Environment {
469469
// List of id's that have been destroyed and need the destroy() cb called.
470470
inline std::vector<int64_t>* destroy_ids_list();
471471

472-
inline uint32_t* heap_statistics_buffer() const;
473-
inline void set_heap_statistics_buffer(uint32_t* pointer);
472+
inline double* heap_statistics_buffer() const;
473+
inline void set_heap_statistics_buffer(double* pointer);
474474

475-
inline uint32_t* heap_space_statistics_buffer() const;
476-
inline void set_heap_space_statistics_buffer(uint32_t* pointer);
475+
inline double* heap_space_statistics_buffer() const;
476+
inline void set_heap_space_statistics_buffer(double* pointer);
477477

478478
inline char* http_parser_buffer() const;
479479
inline void set_http_parser_buffer(char* buffer);
@@ -581,8 +581,8 @@ class Environment {
581581
&HandleCleanup::handle_cleanup_queue_> handle_cleanup_queue_;
582582
int handle_cleanup_waiting_;
583583

584-
uint32_t* heap_statistics_buffer_ = nullptr;
585-
uint32_t* heap_space_statistics_buffer_ = nullptr;
584+
double* heap_statistics_buffer_ = nullptr;
585+
double* heap_space_statistics_buffer_ = nullptr;
586586

587587
char* http_parser_buffer_;
588588

src/node_v8.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
5757
Environment* env = Environment::GetCurrent(args);
5858
HeapStatistics s;
5959
env->isolate()->GetHeapStatistics(&s);
60-
uint32_t* const buffer = env->heap_statistics_buffer();
61-
#define V(index, name, _) buffer[index] = static_cast<uint32_t>(s.name());
60+
double* const buffer = env->heap_statistics_buffer();
61+
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
6262
HEAP_STATISTICS_PROPERTIES(V)
6363
#undef V
6464
}
@@ -68,13 +68,13 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
6868
Environment* env = Environment::GetCurrent(args);
6969
HeapSpaceStatistics s;
7070
Isolate* const isolate = env->isolate();
71-
uint32_t* buffer = env->heap_space_statistics_buffer();
71+
double* buffer = env->heap_space_statistics_buffer();
7272

7373
for (size_t i = 0; i < number_of_heap_spaces; i++) {
7474
isolate->GetHeapSpaceStatistics(&s, i);
7575
size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
7676
#define V(index, name, _) buffer[property_offset + index] = \
77-
static_cast<uint32_t>(s.name());
77+
static_cast<double>(s.name());
7878
HEAP_SPACE_STATISTICS_PROPERTIES(V)
7979
#undef V
8080
}
@@ -103,7 +103,7 @@ void InitializeV8Bindings(Local<Object> target,
103103
"updateHeapStatisticsArrayBuffer",
104104
UpdateHeapStatisticsArrayBuffer);
105105

106-
env->set_heap_statistics_buffer(new uint32_t[kHeapStatisticsPropertiesCount]);
106+
env->set_heap_statistics_buffer(new double[kHeapStatisticsPropertiesCount]);
107107

108108
const size_t heap_statistics_buffer_byte_length =
109109
sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
@@ -149,7 +149,7 @@ void InitializeV8Bindings(Local<Object> target,
149149
UpdateHeapSpaceStatisticsBuffer);
150150

151151
env->set_heap_space_statistics_buffer(
152-
new uint32_t[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
152+
new double[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
153153

154154
const size_t heap_space_statistics_buffer_byte_length =
155155
sizeof(*env->heap_space_statistics_buffer()) *

0 commit comments

Comments
 (0)