Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
75 changes: 44 additions & 31 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,30 @@ using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::Boolean;
using v8::Context;
using v8::CppHeapPointerTag;
using v8::CpuProfile;
using v8::CpuProfiler;
using v8::CpuProfilingMode;
using v8::CpuProfilingOptions;
using v8::CpuProfilingResult;
using v8::CpuProfilingStatus;
using v8::EmbedderGraph;
using v8::EscapableHandleScope;
using v8::ExternalMemoryAccounter;
using v8::Function;
using v8::GCCallbackFlags;
using v8::GCType;
using v8::Global;
using v8::HandleScope;
using v8::HeapProfiler;
using v8::HeapSpaceStatistics;
using v8::HeapStatistics;
using v8::Integer;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::LocalVector;
using v8::Maybe;
using v8::MaybeLocal;
using v8::NewStringType;
Expand All @@ -73,6 +85,7 @@ using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::Private;
using v8::ProfilerId;
using v8::Promise;
using v8::PromiseHookType;
using v8::Script;
Expand Down Expand Up @@ -103,7 +116,7 @@ void AsyncHooks::ResetPromiseHooks(Local<Function> init,
}

Local<Array> AsyncHooks::GetPromiseHooks(Isolate* isolate) const {
v8::LocalVector<Value> values(isolate, js_promise_hooks_.size());
LocalVector<Value> values(isolate, js_promise_hooks_.size());
for (size_t i = 0; i < js_promise_hooks_.size(); ++i) {
if (js_promise_hooks_[i].IsEmpty()) {
values[i] = Undefined(isolate);
Expand Down Expand Up @@ -522,7 +535,7 @@ void IsolateData::CreateProperties() {
// One byte because our strings are ASCII and we can safely skip V8's UTF-8
// decoding step.

v8::Isolate::Scope isolate_scope(isolate_);
Isolate::Scope isolate_scope(isolate_);
HandleScope handle_scope(isolate_);

#define V(PropertyName, StringValue) \
Expand Down Expand Up @@ -682,9 +695,8 @@ IsolateData::~IsolateData() {
// Deprecated API, embedders should use v8::Object::Wrap() directly instead.
void SetCppgcReference(Isolate* isolate,
Local<Object> object,
v8::Object::Wrappable* wrappable) {
v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>(
isolate, object, wrappable);
Object::Wrappable* wrappable) {
Object::Wrap<CppHeapPointerTag::kDefaultTag>(isolate, object, wrappable);
}

void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
Expand Down Expand Up @@ -740,7 +752,7 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {
USE(cb->Call(env_->context(), Undefined(isolate), arraysize(args), args));
}

void Environment::AssignToContext(Local<v8::Context> context,
void Environment::AssignToContext(Local<Context> context,
Realm* realm,
const ContextInfo& info) {
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
Expand All @@ -766,7 +778,7 @@ void Environment::AssignToContext(Local<v8::Context> context,
TrackContext(context);
}

void Environment::UnassignFromContext(Local<v8::Context> context) {
void Environment::UnassignFromContext(Local<Context> context) {
if (!context.IsEmpty()) {
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
nullptr,
Expand Down Expand Up @@ -1592,7 +1604,7 @@ MaybeLocal<Value> Environment::RunSnapshotSerializeCallback() const {
if (!snapshot_serialize_callback().IsEmpty()) {
Context::Scope context_scope(context());
return handle_scope.EscapeMaybe(snapshot_serialize_callback()->Call(
context(), v8::Undefined(isolate()), 0, nullptr));
context(), Undefined(isolate()), 0, nullptr));
}
return handle_scope.Escape(Undefined(isolate()));
}
Expand All @@ -1602,7 +1614,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
if (!snapshot_deserialize_main().IsEmpty()) {
Context::Scope context_scope(context());
return handle_scope.EscapeMaybe(snapshot_deserialize_main()->Call(
context(), v8::Undefined(isolate()), 0, nullptr));
context(), Undefined(isolate()), 0, nullptr));
}
return handle_scope.Escape(Undefined(isolate()));
}
Expand Down Expand Up @@ -1658,23 +1670,23 @@ Maybe<bool> Environment::CheckUnsettledTopLevelAwait() const {
if (!ctx->Global()
->GetPrivate(ctx, entry_point_promise_private_symbol())
.ToLocal(&entry_point_promise)) {
return v8::Nothing<bool>();
return Nothing<bool>();
}
if (!entry_point_promise->IsPromise()) {
return v8::Just(true);
return Just(true);
}
if (entry_point_promise.As<Promise>()->State() !=
Promise::PromiseState::kPending) {
return v8::Just(true);
return Just(true);
}

if (!ctx->Global()
->GetPrivate(ctx, entry_point_module_private_symbol())
.ToLocal(&value)) {
return v8::Nothing<bool>();
return Nothing<bool>();
}
if (!value->IsObject()) {
return v8::Just(true);
return Just(true);
}
Local<Object> object = value.As<Object>();
CHECK(BaseObject::IsBaseObject(isolate_data_, object));
Expand Down Expand Up @@ -2409,8 +2421,9 @@ void Environment::AddPointerCompressionHeapWarningCallback() {
pointer_compression_heap_warning_callback_added_ = true;
// Only full GCs: the heap that survives a mark-compact is the closest
// approximation of the live heap, and young-generation GCs are frequent.
isolate_->AddGCEpilogueCallback(
PointerCompressionHeapWarningCallback, this, v8::kGCTypeMarkSweepCompact);
isolate_->AddGCEpilogueCallback(PointerCompressionHeapWarningCallback,
this,
GCType::kGCTypeMarkSweepCompact);
}

void Environment::RemovePointerCompressionHeapWarningCallback() {
Expand All @@ -2420,12 +2433,14 @@ void Environment::RemovePointerCompressionHeapWarningCallback() {
this);
}

void Environment::PointerCompressionHeapWarningCallback(
Isolate* isolate, v8::GCType type, v8::GCCallbackFlags flags, void* data) {
void Environment::PointerCompressionHeapWarningCallback(Isolate* isolate,
GCType type,
GCCallbackFlags flags,
void* data) {
Environment* env = static_cast<Environment*>(data);
if (env->pointer_compression_heap_warning_scheduled_) return;

v8::HeapStatistics stats;
HeapStatistics stats;
isolate->GetHeapStatistics(&stats);
const uint64_t used = stats.used_heap_size();
if (used < env->pointer_compression_heap_likely_threshold_ &&
Expand Down Expand Up @@ -2757,25 +2772,23 @@ void Environment::RunWeakRefCleanup() {
isolate()->ClearKeptObjects();
}

v8::CpuProfilingResult Environment::StartCpuProfile(
CpuProfilingResult Environment::StartCpuProfile(
const CpuProfileOptions& options) {
HandleScope handle_scope(isolate());
if (!cpu_profiler_) {
cpu_profiler_ = v8::CpuProfiler::New(isolate());
}
v8::CpuProfilingOptions start_options(
v8::CpuProfilingMode::kLeafNodeLineNumbers,
options.max_samples,
options.sampling_interval_us);
v8::CpuProfilingResult result =
cpu_profiler_->Start(std::move(start_options));
if (result.status == v8::CpuProfilingStatus::kStarted) {
cpu_profiler_ = CpuProfiler::New(isolate());
}
CpuProfilingOptions start_options(CpuProfilingMode::kLeafNodeLineNumbers,
options.max_samples,
options.sampling_interval_us);
CpuProfilingResult result = cpu_profiler_->Start(std::move(start_options));
if (result.status == CpuProfilingStatus::kStarted) {
pending_profiles_.push_back(result.id);
}
return result;
}

v8::CpuProfile* Environment::StopCpuProfile(v8::ProfilerId profile_id) {
CpuProfile* Environment::StopCpuProfile(ProfilerId profile_id) {
if (!cpu_profiler_) {
return nullptr;
}
Expand All @@ -2784,7 +2797,7 @@ v8::CpuProfile* Environment::StopCpuProfile(v8::ProfilerId profile_id) {
if (it == pending_profiles_.end()) {
return nullptr;
}
v8::CpuProfile* profile = cpu_profiler_->Stop(*it);
CpuProfile* profile = cpu_profiler_->Stop(*it);
pending_profiles_.erase(it);
return profile;
}
Expand Down
58 changes: 31 additions & 27 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ using v8::CpuProfilingStatus;
using v8::DictionaryTemplate;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::GCCallbackFlags;
using v8::GCType;
using v8::HandleScope;
using v8::HeapCodeStatistics;
using v8::HeapProfiler;
using v8::HeapSpaceStatistics;
using v8::HeapStatistics;
using v8::Integer;
Expand All @@ -56,6 +59,7 @@ using v8::MaybeLocal;
using v8::Number;
using v8::Object;
using v8::ScriptCompiler;
using v8::SnapshotCreator;
using v8::String;
using v8::Uint32;
using v8::V8;
Expand Down Expand Up @@ -147,7 +151,7 @@ BindingData::BindingData(Realm* realm,
}

bool BindingData::PrepareForSerialization(Local<Context> context,
v8::SnapshotCreator* creator) {
SnapshotCreator* creator) {
DCHECK_NULL(internal_field_info_);
internal_field_info_ = InternalFieldInfoBase::New<InternalFieldInfo>(type());
internal_field_info_->heap_statistics_buffer =
Expand Down Expand Up @@ -191,7 +195,7 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
void SetHeapSnapshotNearHeapLimit(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsUint32());
Environment* env = Environment::GetCurrent(args);
uint32_t limit = args[0].As<v8::Uint32>()->Value();
uint32_t limit = args[0].As<Uint32>()->Value();
CHECK_GT(limit, 0);

std::string dir = env->options()->diagnostic_dir;
Expand All @@ -208,7 +212,7 @@ void SetHeapSnapshotNearHeapLimit(const FunctionCallbackInfo<Value>& args) {
void SetHeapProfileNearHeapLimit(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsUint32());
Environment* env = Environment::GetCurrent(args);
uint32_t limit = args[0].As<v8::Uint32>()->Value();
uint32_t limit = args[0].As<Uint32>()->Value();
CHECK_GT(limit, 0);

std::string dir = env->options()->diagnostic_dir;
Expand All @@ -230,8 +234,8 @@ void SetPointerCompressionHeapWarningThresholds(
CHECK(args[0]->IsNumber());
CHECK(args[1]->IsNumber());
Environment* env = Environment::GetCurrent(args);
double likely = args[0].As<v8::Number>()->Value();
double certain = args[1].As<v8::Number>()->Value();
double likely = args[0].As<Number>()->Value();
double certain = args[1].As<Number>()->Value();
CHECK_GE(likely, 0);
CHECK_GE(certain, 0);
env->set_pointer_compression_heap_warning_thresholds(
Expand All @@ -254,7 +258,7 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
HeapSpaceStatistics s;
Isolate* const isolate = args.GetIsolate();
CHECK(args[0]->IsUint32());
size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value());
size_t space_index = static_cast<size_t>(args[0].As<Uint32>()->Value());
isolate->GetHeapSpaceStatistics(&s, space_index);

AliasedFloat64Array& buffer = data->heap_space_statistics_buffer;
Expand Down Expand Up @@ -367,17 +371,17 @@ void GetHashSeed(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, hash_seed));
}

static const char* GetGCTypeName(v8::GCType gc_type) {
static const char* GetGCTypeName(GCType gc_type) {
switch (gc_type) {
case v8::GCType::kGCTypeScavenge:
case GCType::kGCTypeScavenge:
return "Scavenge";
case v8::GCType::kGCTypeMinorMarkSweep:
case GCType::kGCTypeMinorMarkSweep:
return "MinorMarkSweep";
case v8::GCType::kGCTypeMarkSweepCompact:
case GCType::kGCTypeMarkSweepCompact:
return "MarkSweepCompact";
case v8::GCType::kGCTypeIncrementalMarking:
case GCType::kGCTypeIncrementalMarking:
return "IncrementalMarking";
case v8::GCType::kGCTypeProcessWeakCallbacks:
case GCType::kGCTypeProcessWeakCallbacks:
return "ProcessWeakCallbacks";
default:
return "Unknown";
Expand Down Expand Up @@ -620,8 +624,8 @@ static void GetCppHeapStatistics(const FunctionCallbackInfo<Value>& args) {
}

static void BeforeGCCallback(Isolate* isolate,
v8::GCType gc_type,
v8::GCCallbackFlags flags,
GCType gc_type,
GCCallbackFlags flags,
void* data) {
GCProfiler* profiler = static_cast<GCProfiler*>(data);
if (profiler->current_gc_type != 0) {
Expand All @@ -638,8 +642,8 @@ static void BeforeGCCallback(Isolate* isolate,
}

static void AfterGCCallback(Isolate* isolate,
v8::GCType gc_type,
v8::GCCallbackFlags flags,
GCType gc_type,
GCCallbackFlags flags,
void* data) {
GCProfiler* profiler = static_cast<GCProfiler*>(data);
if (profiler->current_gc_type != gc_type) {
Expand Down Expand Up @@ -715,7 +719,7 @@ void GCProfiler::Start(const FunctionCallbackInfo<Value>& args) {
profiler->state = GCProfiler::GCProfilerState::kStarted;
}

void GCProfiler::Stop(const FunctionCallbackInfo<v8::Value>& args) {
void GCProfiler::Stop(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
GCProfiler* profiler;
ASSIGN_OR_RETURN_UNWRAP(&profiler, args.This());
Expand Down Expand Up @@ -818,16 +822,16 @@ void Initialize(Local<Object> target,
SetMethod(context, target, "stopHeapProfile", StopHeapProfile);

{
constexpr uint32_t kSamplingNoFlags = static_cast<uint32_t>(
v8::HeapProfiler::SamplingFlags::kSamplingNoFlags);
constexpr uint32_t kSamplingForceGC = static_cast<uint32_t>(
v8::HeapProfiler::SamplingFlags::kSamplingForceGC);
constexpr uint32_t kSamplingIncludeObjectsCollectedByMajorGC =
static_cast<uint32_t>(v8::HeapProfiler::SamplingFlags::
kSamplingIncludeObjectsCollectedByMajorGC);
constexpr uint32_t kSamplingIncludeObjectsCollectedByMinorGC =
static_cast<uint32_t>(v8::HeapProfiler::SamplingFlags::
kSamplingIncludeObjectsCollectedByMinorGC);
constexpr uint32_t kSamplingNoFlags =
static_cast<uint32_t>(HeapProfiler::SamplingFlags::kSamplingNoFlags);
constexpr uint32_t kSamplingForceGC =
static_cast<uint32_t>(HeapProfiler::SamplingFlags::kSamplingForceGC);
constexpr uint32_t kSamplingIncludeObjectsCollectedByMajorGC = static_cast<
uint32_t>(
HeapProfiler::SamplingFlags::kSamplingIncludeObjectsCollectedByMajorGC);
constexpr uint32_t kSamplingIncludeObjectsCollectedByMinorGC = static_cast<
uint32_t>(
HeapProfiler::SamplingFlags::kSamplingIncludeObjectsCollectedByMinorGC);

NODE_DEFINE_CONSTANT(target, kSamplingNoFlags);
NODE_DEFINE_CONSTANT(target, kSamplingForceGC);
Expand Down
Loading
Loading