Skip to content

Commit a3ad63b

Browse files
bnoordhuisMylesBorins
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: #10185 PR-URL: #10186 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent 69b55f3 commit a3ad63b

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;
@@ -28,7 +28,7 @@ const kHeapSizeLimitIndex = v8binding.kHeapSizeLimitIndex;
2828

2929
// Properties for heap space statistics buffer extraction.
3030
const heapSpaceStatisticsBuffer =
31-
new Uint32Array(v8binding.heapSpaceStatisticsArrayBuffer);
31+
new Float64Array(v8binding.heapSpaceStatisticsArrayBuffer);
3232
const kHeapSpaces = v8binding.kHeapSpaces;
3333
const kNumberOfHeapSpaces = kHeapSpaces.length;
3434
const kHeapSpaceStatisticsPropertiesCount =

src/env-inl.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -396,22 +396,22 @@ inline std::vector<int64_t>* Environment::destroy_ids_list() {
396396
return &destroy_ids_list_;
397397
}
398398

399-
inline uint32_t* Environment::heap_statistics_buffer() const {
399+
inline double* Environment::heap_statistics_buffer() const {
400400
CHECK_NE(heap_statistics_buffer_, nullptr);
401401
return heap_statistics_buffer_;
402402
}
403403

404-
inline void Environment::set_heap_statistics_buffer(uint32_t* pointer) {
404+
inline void Environment::set_heap_statistics_buffer(double* pointer) {
405405
CHECK_EQ(heap_statistics_buffer_, nullptr); // Should be set only once.
406406
heap_statistics_buffer_ = pointer;
407407
}
408408

409-
inline uint32_t* Environment::heap_space_statistics_buffer() const {
409+
inline double* Environment::heap_space_statistics_buffer() const {
410410
CHECK_NE(heap_space_statistics_buffer_, nullptr);
411411
return heap_space_statistics_buffer_;
412412
}
413413

414-
inline void Environment::set_heap_space_statistics_buffer(uint32_t* pointer) {
414+
inline void Environment::set_heap_space_statistics_buffer(double* pointer) {
415415
CHECK_EQ(heap_space_statistics_buffer_, nullptr); // Should be set only once.
416416
heap_space_statistics_buffer_ = pointer;
417417
}

src/env.h

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

461-
inline uint32_t* heap_statistics_buffer() const;
462-
inline void set_heap_statistics_buffer(uint32_t* pointer);
461+
inline double* heap_statistics_buffer() const;
462+
inline void set_heap_statistics_buffer(double* pointer);
463463

464-
inline uint32_t* heap_space_statistics_buffer() const;
465-
inline void set_heap_space_statistics_buffer(uint32_t* pointer);
464+
inline double* heap_space_statistics_buffer() const;
465+
inline void set_heap_space_statistics_buffer(double* pointer);
466466

467467
inline char* http_parser_buffer() const;
468468
inline void set_http_parser_buffer(char* buffer);
@@ -578,8 +578,8 @@ class Environment {
578578
&HandleCleanup::handle_cleanup_queue_> handle_cleanup_queue_;
579579
int handle_cleanup_waiting_;
580580

581-
uint32_t* heap_statistics_buffer_ = nullptr;
582-
uint32_t* heap_space_statistics_buffer_ = nullptr;
581+
double* heap_statistics_buffer_ = nullptr;
582+
double* heap_space_statistics_buffer_ = nullptr;
583583

584584
char* http_parser_buffer_;
585585

src/node_v8.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
5454
Environment* env = Environment::GetCurrent(args);
5555
HeapStatistics s;
5656
env->isolate()->GetHeapStatistics(&s);
57-
uint32_t* const buffer = env->heap_statistics_buffer();
58-
#define V(index, name, _) buffer[index] = static_cast<uint32_t>(s.name());
57+
double* const buffer = env->heap_statistics_buffer();
58+
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
5959
HEAP_STATISTICS_PROPERTIES(V)
6060
#undef V
6161
}
@@ -65,13 +65,13 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
6565
Environment* env = Environment::GetCurrent(args);
6666
HeapSpaceStatistics s;
6767
Isolate* const isolate = env->isolate();
68-
uint32_t* buffer = env->heap_space_statistics_buffer();
68+
double* buffer = env->heap_space_statistics_buffer();
6969

7070
for (size_t i = 0; i < number_of_heap_spaces; i++) {
7171
isolate->GetHeapSpaceStatistics(&s, i);
7272
size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
7373
#define V(index, name, _) buffer[property_offset + index] = \
74-
static_cast<uint32_t>(s.name());
74+
static_cast<double>(s.name());
7575
HEAP_SPACE_STATISTICS_PROPERTIES(V)
7676
#undef V
7777
}
@@ -100,7 +100,7 @@ void InitializeV8Bindings(Local<Object> target,
100100
"updateHeapStatisticsArrayBuffer",
101101
UpdateHeapStatisticsArrayBuffer);
102102

103-
env->set_heap_statistics_buffer(new uint32_t[kHeapStatisticsPropertiesCount]);
103+
env->set_heap_statistics_buffer(new double[kHeapStatisticsPropertiesCount]);
104104

105105
const size_t heap_statistics_buffer_byte_length =
106106
sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
@@ -146,7 +146,7 @@ void InitializeV8Bindings(Local<Object> target,
146146
UpdateHeapSpaceStatisticsBuffer);
147147

148148
env->set_heap_space_statistics_buffer(
149-
new uint32_t[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
149+
new double[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
150150

151151
const size_t heap_space_statistics_buffer_byte_length =
152152
sizeof(*env->heap_space_statistics_buffer()) *

0 commit comments

Comments
 (0)