Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
v8: use AliasedBuffers for passing heap statistics around
Instead of holding shared pointers to ArrayBuffers, simplify
the code by using AliasedBuffers directly which allows the
binding to own the buffers.
  • Loading branch information
joyeecheung committed Apr 19, 2020
commit b82a16fb9e2c3711e0a7a2645cd04b152a5f146d
36 changes: 13 additions & 23 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ class Deserializer extends _Deserializer { }
const {
cachedDataVersionTag,
setFlagsFromString: _setFlagsFromString,
heapStatisticsArrayBuffer,
heapSpaceStatisticsArrayBuffer,
heapCodeStatisticsArrayBuffer,
updateHeapStatisticsArrayBuffer,
updateHeapSpaceStatisticsArrayBuffer,
updateHeapCodeStatisticsArrayBuffer,
heapStatisticsBuffer,
heapSpaceStatisticsBuffer,
heapCodeStatisticsBuffer,
updateHeapStatisticsBuffer,
updateHeapSpaceStatisticsBuffer,
updateHeapCodeStatisticsBuffer,

// Properties for heap statistics buffer extraction.
kTotalHeapSizeIndex,
Expand Down Expand Up @@ -109,15 +109,6 @@ const {

const kNumberOfHeapSpaces = kHeapSpaces.length;

const heapStatisticsBuffer =
new Float64Array(heapStatisticsArrayBuffer);

const heapSpaceStatisticsBuffer =
new Float64Array(heapSpaceStatisticsArrayBuffer);

const heapCodeStatisticsBuffer =
new Float64Array(heapCodeStatisticsArrayBuffer);

function setFlagsFromString(flags) {
validateString(flags, 'flags');
_setFlagsFromString(flags);
Expand All @@ -126,7 +117,7 @@ function setFlagsFromString(flags) {
function getHeapStatistics() {
const buffer = heapStatisticsBuffer;

updateHeapStatisticsArrayBuffer();
updateHeapStatisticsBuffer();

return {
'total_heap_size': buffer[kTotalHeapSizeIndex],
Expand All @@ -146,16 +137,15 @@ function getHeapStatistics() {
function getHeapSpaceStatistics() {
const heapSpaceStatistics = new Array(kNumberOfHeapSpaces);
const buffer = heapSpaceStatisticsBuffer;
updateHeapSpaceStatisticsArrayBuffer();

for (let i = 0; i < kNumberOfHeapSpaces; i++) {
const propertyOffset = i * kHeapSpaceStatisticsPropertiesCount;
updateHeapSpaceStatisticsBuffer(i);
heapSpaceStatistics[i] = {
space_name: kHeapSpaces[i],
space_size: buffer[propertyOffset + kSpaceSizeIndex],
space_used_size: buffer[propertyOffset + kSpaceUsedSizeIndex],
space_available_size: buffer[propertyOffset + kSpaceAvailableSizeIndex],
physical_space_size: buffer[propertyOffset + kPhysicalSpaceSizeIndex]
space_size: buffer[kSpaceSizeIndex],
space_used_size: buffer[kSpaceUsedSizeIndex],
space_available_size: buffer[kSpaceAvailableSizeIndex],
physical_space_size: buffer[kPhysicalSpaceSizeIndex]
};
}

Expand All @@ -165,7 +155,7 @@ function getHeapSpaceStatistics() {
function getHeapCodeStatistics() {
const buffer = heapCodeStatisticsBuffer;

updateHeapCodeStatisticsArrayBuffer();
updateHeapCodeStatisticsBuffer();
return {
'code_and_metadata_size': buffer[kCodeAndMetadataSizeIndex],
'bytecode_and_metadata_size': buffer[kBytecodeAndMetadataSizeIndex],
Expand Down
131 changes: 53 additions & 78 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::HeapCodeStatistics;
Expand Down Expand Up @@ -78,13 +77,29 @@ static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
HEAP_SPACE_STATISTICS_PROPERTIES(V);
#undef V

#define HEAP_CODE_STATISTICS_PROPERTIES(V) \
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \
V(2, external_script_source_size, kExternalScriptSourceSizeIndex)

#define V(a, b, c) +1
static const size_t kHeapCodeStatisticsPropertiesCount =
HEAP_CODE_STATISTICS_PROPERTIES(V);
#undef V

class BindingData : public BaseObject {
public:
BindingData(Environment* env, Local<Object> obj) : BaseObject(env, obj) {}

std::shared_ptr<BackingStore> heap_statistics_buffer;
std::shared_ptr<BackingStore> heap_space_statistics_buffer;
std::shared_ptr<BackingStore> heap_code_statistics_buffer;
BindingData(Environment* env, Local<Object> obj)
: BaseObject(env, obj),
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
heap_space_statistics_buffer(env->isolate(),
kHeapSpaceStatisticsPropertiesCount),
heap_code_statistics_buffer(env->isolate(),
kHeapCodeStatisticsPropertiesCount) {}

AliasedFloat64Array heap_statistics_buffer;
AliasedFloat64Array heap_space_statistics_buffer;
AliasedFloat64Array heap_code_statistics_buffer;

void MemoryInfo(MemoryTracker* tracker) const override {
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
Expand All @@ -97,15 +112,6 @@ class BindingData : public BaseObject {
SET_MEMORY_INFO_NAME(BindingData)
};

#define HEAP_CODE_STATISTICS_PROPERTIES(V) \
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \
V(2, external_script_source_size, kExternalScriptSourceSizeIndex)

#define V(a, b, c) +1
static const size_t kHeapCodeStatisticsPropertiesCount =
HEAP_CODE_STATISTICS_PROPERTIES(V);
#undef V

void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand All @@ -115,13 +121,11 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(result);
}


void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
BindingData* data = Unwrap<BindingData>(args.Data());
HeapStatistics s;
args.GetIsolate()->GetHeapStatistics(&s);
double* const buffer =
static_cast<double*>(data->heap_statistics_buffer->Data());
AliasedFloat64Array& buffer = data->heap_statistics_buffer;
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
HEAP_STATISTICS_PROPERTIES(V)
#undef V
Expand All @@ -132,29 +136,23 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
BindingData* data = Unwrap<BindingData>(args.Data());
HeapSpaceStatistics s;
Isolate* const isolate = args.GetIsolate();
size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces();
CHECK(args[0]->IsUint32());
size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value());
isolate->GetHeapSpaceStatistics(&s, space_index);

double* const buffer =
static_cast<double*>(data->heap_space_statistics_buffer->Data());
AliasedFloat64Array& buffer = data->heap_space_statistics_buffer;

for (size_t i = 0; i < number_of_heap_spaces; i++) {
isolate->GetHeapSpaceStatistics(&s, i);
size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
#define V(index, name, _) \
buffer[property_offset + index] = static_cast<double>(s.name());
HEAP_SPACE_STATISTICS_PROPERTIES(V)
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
HEAP_SPACE_STATISTICS_PROPERTIES(V)
#undef V
}
}


void UpdateHeapCodeStatisticsArrayBuffer(
const FunctionCallbackInfo<Value>& args) {
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
BindingData* data = Unwrap<BindingData>(args.Data());
HeapCodeStatistics s;
args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
double* const buffer =
static_cast<double*>(data->heap_code_statistics_buffer->Data());
AliasedFloat64Array& buffer = data->heap_code_statistics_buffer;

#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
HEAP_CODE_STATISTICS_PROPERTIES(V)
#undef V
Expand All @@ -181,20 +179,14 @@ void Initialize(Local<Object> target,
CachedDataVersionTag);

// Export symbols used by v8.getHeapStatistics()
env->SetMethod(target,
"updateHeapStatisticsArrayBuffer",
UpdateHeapStatisticsArrayBuffer);

const size_t heap_statistics_buffer_byte_length =
sizeof(double) * kHeapStatisticsPropertiesCount;
env->SetMethod(
target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer);

Local<ArrayBuffer> heap_statistics_ab =
ArrayBuffer::New(env->isolate(), heap_statistics_buffer_byte_length);
binding_data->heap_statistics_buffer = heap_statistics_ab->GetBackingStore();
target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(),
"heapStatisticsArrayBuffer"),
heap_statistics_ab).Check();
target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
binding_data->heap_statistics_buffer.GetJSArray())
.Check();

#define V(i, _, name) \
target->Set(env->context(), \
Expand All @@ -205,22 +197,14 @@ void Initialize(Local<Object> target,
#undef V

// Export symbols used by v8.getHeapCodeStatistics()
env->SetMethod(target,
"updateHeapCodeStatisticsArrayBuffer",
UpdateHeapCodeStatisticsArrayBuffer);

const size_t heap_code_statistics_buffer_byte_length =
sizeof(double) * kHeapCodeStatisticsPropertiesCount;
env->SetMethod(
target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer);

Local<ArrayBuffer> heap_code_statistics_ab =
ArrayBuffer::New(env->isolate(),
heap_code_statistics_buffer_byte_length);
binding_data->heap_code_statistics_buffer =
heap_code_statistics_ab->GetBackingStore();
target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(),
"heapCodeStatisticsArrayBuffer"),
heap_code_statistics_ab).Check();
target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
binding_data->heap_code_statistics_buffer.GetJSArray())
.Check();

#define V(i, _, name) \
target->Set(env->context(), \
Expand Down Expand Up @@ -258,24 +242,15 @@ void Initialize(Local<Object> target,
number_of_heap_spaces)).Check();

env->SetMethod(target,
"updateHeapSpaceStatisticsArrayBuffer",
"updateHeapSpaceStatisticsBuffer",
UpdateHeapSpaceStatisticsBuffer);

const size_t heap_space_statistics_buffer_byte_length =
sizeof(double) *
kHeapSpaceStatisticsPropertiesCount *
number_of_heap_spaces;

Local<ArrayBuffer> heap_space_statistics_ab =
ArrayBuffer::New(env->isolate(),
heap_space_statistics_buffer_byte_length);
binding_data->heap_space_statistics_buffer =
heap_space_statistics_ab->GetBackingStore();

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(),
"heapSpaceStatisticsArrayBuffer"),
heap_space_statistics_ab).Check();
target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(),
"heapSpaceStatisticsBuffer"),
binding_data->heap_space_statistics_buffer.GetJSArray())
.Check();

#define V(i, _, name) \
target->Set(env->context(), \
Expand Down