Commit 80242048 authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

[api] New v8::Isolate::MeasureMemory API with per-context sizes

This adds a new API function that can be customized by the embedder
by providing a delegate that defines contexts to be measured and
reports the results to JS.

A memory measurement request is carried out as follows:

1) MeasureMemory(delegate) invocation enqueues a new request in
   MemoryMeasurement::received_ and schedules a delayed GC task.

2) At the start of the next GC (that is triggered either by the
   GC schedule or by the delayed task) each request in received_
   moves to processing_. Per-context marking worklists are created
   for each native context that was selected by the delegates
   (using the ShouldMeasure predicate).

3) At the end of the GC the sizes of the native contexts are
   recorded for each request in processing_. The requests move
   to the done_ list and result reporting task is scheduled.

4) When the result reporting task runs it invokes the
   MeasurementComplete function of each delegate in done_.


Bug: chromium:973627

Change-Id: I0254cae693c5b8fab7c85a9eca0a3a128210b6c4
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1981493
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarHannes Payer <hpayer@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarDominik Inführ <dinfuehr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65856}
parent 14b5b2ad
......@@ -8044,8 +8044,66 @@ struct DeserializeInternalFieldsCallback {
};
typedef DeserializeInternalFieldsCallback DeserializeEmbedderFieldsCallback;
/**
* Controls how the default MeasureMemoryDelegate reports the result of
* the memory measurement to JS. With kSummary only the total size is reported.
* With kDetailed the result includes the size of each native context.
*/
enum class MeasureMemoryMode { kSummary, kDetailed };
/**
* Controls how promptly a memory measurement request is executed.
* By default the measurement is folded with the next scheduled GC which may
* happen after a while. The kEager starts increment GC right away and
* is useful for testing.
*/
enum class MeasureMemoryExecution { kDefault, kEager };
/**
* The delegate is used in Isolate::MeasureMemory API.
*
* It specifies the contexts that need to be measured and gets called when
* the measurement is completed to report the results.
*/
class V8_EXPORT MeasureMemoryDelegate {
public:
virtual ~MeasureMemoryDelegate() = default;
/**
* Returns true if the size of the given context needs to be measured.
*/
virtual bool ShouldMeasure(Local<Context> context) = 0;
/**
* This function is called when memory measurement finishes.
*
* \param context_sizes_in_bytes a vector of (context, size) pairs that
* includes each context for which ShouldMeasure returned true and that
* was not garbage collected while the memory measurement was in progress.
*
* \param unattributed_size_in_bytes total size of objects that were not
* attributed to any context (i.e. are likely shared objects).
*/
virtual void MeasurementComplete(
const std::vector<std::pair<Local<Context>, size_t>>&
context_sizes_in_bytes,
size_t unattributed_size_in_bytes) = 0;
/**
* Returns a default delegate that resolves the given promise when
* the memory measurement completes.
*
* \param isolate the current isolate
* \param context the current context
* \param promise_resolver the promise resolver that is given the
* result of the memory measurement.
* \param mode the detail level of the result.
*/
static std::unique_ptr<MeasureMemoryDelegate> Default(
Isolate* isolate, Local<Context> context,
Local<Promise::Resolver> promise_resolver, MeasureMemoryMode mode);
};
/**
* Isolate represents an isolated instance of the V8 engine. V8 isolates have
* completely separate states. Objects from one isolate must not be used in
......@@ -8578,15 +8636,24 @@ class V8_EXPORT Isolate {
bool GetHeapCodeAndMetadataStatistics(HeapCodeStatistics* object_statistics);
/**
* Enqueues a memory measurement request for the given context and mode.
* This API is experimental and may change significantly.
*
* \param mode Indicates whether the result should include per-context
* memory usage or just the total memory usage.
* \returns a promise that will be resolved with memory usage estimate.
* Enqueues a memory measurement request and invokes the delegate with the
* results.
*
* \param delegate the delegate that defines which contexts to measure and
* reports the results.
*
* \param execution promptness executing the memory measurement.
* The kEager value is expected to be used only in tests.
*/
v8::MaybeLocal<v8::Promise> MeasureMemory(v8::Local<v8::Context> context,
MeasureMemoryMode mode);
bool MeasureMemory(
std::unique_ptr<MeasureMemoryDelegate> delegate,
MeasureMemoryExecution execution = MeasureMemoryExecution::kDefault);
V8_DEPRECATE_SOON("Use the version with a delegate")
MaybeLocal<Promise> MeasureMemory(Local<Context> context,
MeasureMemoryMode mode);
/**
* Get a call stack sample from the isolate.
......
......@@ -8561,11 +8561,25 @@ bool Isolate::GetHeapCodeAndMetadataStatistics(
v8::MaybeLocal<v8::Promise> Isolate::MeasureMemory(
v8::Local<v8::Context> context, MeasureMemoryMode mode) {
return v8::MaybeLocal<v8::Promise>();
}
bool Isolate::MeasureMemory(std::unique_ptr<MeasureMemoryDelegate> delegate,
MeasureMemoryExecution execution) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
return isolate->heap()->MeasureMemory(std::move(delegate), execution);
}
std::unique_ptr<MeasureMemoryDelegate> MeasureMemoryDelegate::Default(
Isolate* isolate, Local<Context> context,
Local<Promise::Resolver> promise_resolver, MeasureMemoryMode mode) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::Handle<i::NativeContext> native_context =
handle(Utils::OpenHandle(*context)->native_context(), isolate);
return v8::Utils::PromiseToLocal(
isolate->heap()->MeasureMemory(native_context, mode));
handle(Utils::OpenHandle(*context)->native_context(), i_isolate);
i::Handle<i::JSPromise> js_promise =
i::Handle<i::JSPromise>::cast(Utils::OpenHandle(*promise_resolver));
return i_isolate->heap()->MeasureMemoryDelegate(native_context, js_promise,
mode);
}
void Isolate::GetStackSample(const RegisterState& state, void** frames,
......
......@@ -1204,9 +1204,13 @@ void Shell::PerformanceMeasureMemory(
mode = v8::MeasureMemoryMode::kDetailed;
}
}
v8::MaybeLocal<v8::Promise> result =
args.GetIsolate()->MeasureMemory(context, mode);
args.GetReturnValue().Set(result.FromMaybe(v8::Local<v8::Promise>()));
Local<v8::Promise::Resolver> promise_resolver =
v8::Promise::Resolver::New(context).ToLocalChecked();
args.GetIsolate()->MeasureMemory(
v8::MeasureMemoryDelegate::Default(isolate, context, promise_resolver,
mode),
v8::MeasureMemoryExecution::kEager);
args.GetReturnValue().Set(promise_resolver->GetPromise());
}
// Realm.current() returns the index of the currently active realm.
......
......@@ -3839,9 +3839,26 @@ bool Heap::InvokeNearHeapLimitCallback() {
return false;
}
Handle<JSPromise> Heap::MeasureMemory(Handle<NativeContext> context,
v8::MeasureMemoryMode mode) {
return memory_measurement_->EnqueueRequest(context, mode);
bool Heap::MeasureMemory(std::unique_ptr<v8::MeasureMemoryDelegate> delegate,
v8::MeasureMemoryExecution execution) {
HandleScope handle_scope(isolate());
std::vector<Handle<NativeContext>> contexts = FindAllNativeContexts();
std::vector<Handle<NativeContext>> to_measure;
for (auto& current : contexts) {
if (delegate->ShouldMeasure(
v8::Utils::ToLocal(Handle<Context>::cast(current)))) {
to_measure.push_back(current);
}
}
return memory_measurement_->EnqueueRequest(std::move(delegate), execution,
to_measure);
}
std::unique_ptr<v8::MeasureMemoryDelegate> Heap::MeasureMemoryDelegate(
Handle<NativeContext> context, Handle<JSPromise> promise,
v8::MeasureMemoryMode mode) {
return i::MemoryMeasurement::DefaultDelegate(isolate_, context, promise,
mode);
}
void Heap::CollectCodeStatistics() {
......@@ -3923,6 +3940,8 @@ const char* Heap::GarbageCollectionReasonToString(
return "external finalize";
case GarbageCollectionReason::kGlobalAllocationLimit:
return "global allocation limit";
case GarbageCollectionReason::kMeasureMemory:
return "measure memory";
case GarbageCollectionReason::kUnknown:
return "unknown";
}
......@@ -6076,12 +6095,12 @@ size_t Heap::NumberOfNativeContexts() {
return result;
}
std::vector<Address> Heap::FindNativeContexts() {
std::vector<Address> result;
std::vector<Handle<NativeContext>> Heap::FindAllNativeContexts() {
std::vector<Handle<NativeContext>> result;
Object context = native_contexts_list();
while (!context.IsUndefined(isolate())) {
Context native_context = Context::cast(context);
result.push_back(native_context.ptr());
NativeContext native_context = NativeContext::cast(context);
result.push_back(handle(native_context, isolate()));
context = native_context.next_context_link();
}
return result;
......
......@@ -138,6 +138,7 @@ enum class GarbageCollectionReason {
kTesting = 21,
kExternalFinalize = 22,
kGlobalAllocationLimit = 23,
kMeasureMemory = 24
// If you add new items here, then update the incremental_marking_reason,
// mark_compact_reason, and scavenge_reason counters in counters.h.
// Also update src/tools/metrics/histograms/histograms.xml in chromium.
......@@ -612,9 +613,12 @@ class Heap {
void RecordStats(HeapStats* stats, bool take_snapshot = false);
Handle<JSPromise> MeasureMemory(Handle<NativeContext> context,
v8::MeasureMemoryMode mode);
std::vector<Address> FindNativeContexts();
bool MeasureMemory(std::unique_ptr<v8::MeasureMemoryDelegate> delegate,
v8::MeasureMemoryExecution execution);
std::unique_ptr<v8::MeasureMemoryDelegate> MeasureMemoryDelegate(
Handle<NativeContext> context, Handle<JSPromise> promise,
v8::MeasureMemoryMode mode);
// Check new space expansion criteria and expand semispaces if it was hit.
void CheckNewSpaceExpansionCriteria();
......@@ -1862,6 +1866,9 @@ class Heap {
V8_EXPORT_PRIVATE void IncrementObjectCounters();
#endif // DEBUG
std::vector<Handle<NativeContext>> FindAllNativeContexts();
MemoryMeasurement* memory_measurement() { return memory_measurement_.get(); }
// The amount of memory that has been freed concurrently.
std::atomic<uintptr_t> external_memory_concurrently_freed_{0};
......
......@@ -324,7 +324,6 @@ void IncrementalMarking::StartMarking() {
heap()->isolate()->PrintWithTimestamp(
"[IncrementalMarking] Start marking\n");
}
is_compacting_ = !FLAG_never_compact && collector_->StartCompaction();
collector_->StartMarking();
......
......@@ -504,9 +504,14 @@ void MarkCompactCollector::StartMarking() {
heap_->new_space()->ResetOriginalTop();
heap_->new_lo_space()->ResetPendingObject();
}
std::vector<Address> contexts;
std::vector<Address> contexts =
heap()->memory_measurement()->StartProcessing();
if (FLAG_stress_per_context_marking_worklist) {
contexts = heap()->FindNativeContexts();
contexts.clear();
HandleScope handle_scope(heap()->isolate());
for (auto context : heap()->FindAllNativeContexts()) {
contexts.push_back(context->ptr());
}
}
marking_worklists_holder()->CreateContextWorklists(contexts);
marking_worklists_ = std::make_unique<MarkingWorklists>(
......@@ -536,7 +541,7 @@ void MarkCompactCollector::CollectGarbage() {
MarkLiveObjects();
ClearNonLiveReferences();
VerifyMarking();
heap()->memory_measurement()->FinishProcessing(native_context_stats_);
RecordObjectStats();
StartSweepSpaces();
......
......@@ -4,47 +4,71 @@
#include "src/heap/memory-measurement.h"
#include "include/v8.h"
#include "src/api/api-inl.h"
#include "src/api/api.h"
#include "src/execution/isolate-inl.h"
#include "src/execution/isolate.h"
#include "src/heap/factory-inl.h"
#include "src/heap/factory.h"
#include "src/heap/incremental-marking.h"
#include "src/heap/marking-worklist.h"
#include "src/objects/js-promise-inl.h"
#include "src/objects/js-promise.h"
#include "src/tasks/task-utils.h"
namespace v8 {
namespace internal {
MemoryMeasurement::MemoryMeasurement(Isolate* isolate) : isolate_(isolate) {}
namespace {
class MemoryMeasurementResultBuilder {
public:
MemoryMeasurementResultBuilder(Isolate* isolate, Factory* factory)
: isolate_(isolate), factory_(factory) {
result_ = NewJSObject();
}
void AddTotal(size_t estimate, size_t lower_bound, size_t upper_bound) {
AddProperty(result_, factory_->total_string(),
NewResult(estimate, lower_bound, upper_bound));
}
void AddCurrent(size_t estimate, size_t lower_bound, size_t upper_bound) {
detailed_ = true;
AddProperty(result_, factory_->current_string(),
NewResult(estimate, lower_bound, upper_bound));
}
void AddOther(size_t estimate, size_t lower_bound, size_t upper_bound) {
detailed_ = true;
other_.push_back(NewResult(estimate, lower_bound, upper_bound));
}
Handle<JSObject> Build() {
if (detailed_) {
int length = static_cast<int>(other_.size());
Handle<FixedArray> other = factory_->NewFixedArray(length);
for (int i = 0; i < length; i++) {
other->set(i, *other_[i]);
}
AddProperty(result_, factory_->other_string(),
factory_->NewJSArrayWithElements(other));
}
return result_;
}
void AddTotals(size_t estimate, size_t lower_bound, size_t upper_bound) {
Handle<JSObject> total = NewJSObject();
private:
Handle<JSObject> NewResult(size_t estimate, size_t lower_bound,
size_t upper_bound) {
Handle<JSObject> result = NewJSObject();
Handle<Object> estimate_obj = NewNumber(estimate);
AddProperty(total, factory_->jsMemoryEstimate_string(), estimate_obj);
AddProperty(result, factory_->jsMemoryEstimate_string(), estimate_obj);
Handle<Object> range = NewRange(lower_bound, upper_bound);
AddProperty(total, factory_->jsMemoryRange_string(), range);
AddProperty(result_, factory_->total_string(), total);
AddProperty(result, factory_->jsMemoryRange_string(), range);
return result;
}
Handle<JSObject> Build() { return result_; }
private:
Handle<Object> NewNumber(size_t value) {
return factory_->NewNumberFromSize(value);
}
Handle<JSObject> NewJSObject() {
return factory_->NewJSObject(isolate_->object_function());
}
Handle<JSArray> NewRange(size_t lower_bound, size_t upper_bound) {
Handle<Object> lower = NewNumber(lower_bound);
Handle<Object> upper = NewNumber(upper_bound);
......@@ -53,28 +77,240 @@ class MemoryMeasurementResultBuilder {
elements->set(1, *upper);
return factory_->NewJSArrayWithElements(elements);
}
void AddProperty(Handle<JSObject> object, Handle<String> name,
Handle<Object> value) {
JSObject::AddProperty(isolate_, object, name, value, NONE);
}
Isolate* isolate_;
Factory* factory_;
Handle<JSObject> result_;
std::vector<Handle<JSObject>> other_;
bool detailed_ = false;
};
} // anonymous namespace
Handle<JSPromise> MemoryMeasurement::EnqueueRequest(
Handle<NativeContext> context, v8::MeasureMemoryMode mode) {
Handle<JSPromise> promise = isolate_->factory()->NewJSPromise();
class V8_EXPORT_PRIVATE MeasureMemoryDelegate
: public v8::MeasureMemoryDelegate {
public:
MeasureMemoryDelegate(Isolate* isolate, Handle<NativeContext> context,
Handle<JSPromise> promise, v8::MeasureMemoryMode mode);
~MeasureMemoryDelegate();
// v8::MeasureMemoryDelegate overrides:
bool ShouldMeasure(v8::Local<v8::Context> context) override;
void MeasurementComplete(
const std::vector<std::pair<v8::Local<v8::Context>, size_t>>&
context_sizes_in_bytes,
size_t unattributed_size_in_bytes) override;
private:
Isolate* isolate_;
Handle<JSPromise> promise_;
Handle<NativeContext> context_;
v8::MeasureMemoryMode mode_;
};
MeasureMemoryDelegate::MeasureMemoryDelegate(Isolate* isolate,
Handle<NativeContext> context,
Handle<JSPromise> promise,
v8::MeasureMemoryMode mode)
: isolate_(isolate), mode_(mode) {
context_ = isolate->global_handles()->Create(*context);
promise_ = isolate->global_handles()->Create(*promise);
}
MeasureMemoryDelegate::~MeasureMemoryDelegate() {
isolate_->global_handles()->Destroy(promise_.location());
isolate_->global_handles()->Destroy(context_.location());
}
bool MeasureMemoryDelegate::ShouldMeasure(v8::Local<v8::Context> context) {
Handle<NativeContext> native_context =
Handle<NativeContext>::cast(Utils::OpenHandle(*context));
return context_->security_token() == native_context->security_token();
}
void MeasureMemoryDelegate::MeasurementComplete(
const std::vector<std::pair<v8::Local<v8::Context>, size_t>>&
context_sizes_in_bytes,
size_t shared_size) {
v8::Local<v8::Context> v8_context =
Utils::Convert<HeapObject, v8::Context>(context_);
v8::Context::Scope scope(v8_context);
size_t total_size = 0;
size_t current_size = 0;
for (const auto& context_and_size : context_sizes_in_bytes) {
total_size += context_and_size.second;
if (*Utils::OpenHandle(*context_and_size.first) == *context_) {
current_size = context_and_size.second;
}
}
MemoryMeasurementResultBuilder result_builder(isolate_, isolate_->factory());
result_builder.AddTotals(isolate_->heap()->SizeOfObjects(), 0,
isolate_->heap()->SizeOfObjects());
result_builder.AddTotal(total_size, total_size, total_size + shared_size);
if (mode_ == v8::MeasureMemoryMode::kDetailed) {
result_builder.AddCurrent(current_size, current_size,
current_size + shared_size);
for (const auto& context_and_size : context_sizes_in_bytes) {
if (*Utils::OpenHandle(*context_and_size.first) != *context_) {
size_t other_size = context_and_size.second;
result_builder.AddOther(other_size, other_size,
other_size + shared_size);
}
}
}
Handle<JSObject> result = result_builder.Build();
JSPromise::Resolve(promise, result).ToHandleChecked();
return promise;
JSPromise::Resolve(promise_, result).ToHandleChecked();
}
MemoryMeasurement::MemoryMeasurement(Isolate* isolate) : isolate_(isolate) {}
bool MemoryMeasurement::EnqueueRequest(
std::unique_ptr<v8::MeasureMemoryDelegate> delegate,
v8::MeasureMemoryExecution execution,
const std::vector<Handle<NativeContext>> contexts) {
int length = static_cast<int>(contexts.size());
Handle<WeakFixedArray> weak_contexts =
isolate_->factory()->NewWeakFixedArray(length);
for (int i = 0; i < length; ++i) {
weak_contexts->Set(i, HeapObjectReference::Weak(*contexts[i]));
}
Handle<WeakFixedArray> global_weak_contexts =
isolate_->global_handles()->Create(*weak_contexts);
Request request = {std::move(delegate), global_weak_contexts,
std::vector<size_t>(length), 0u};
received_.push_back(std::move(request));
ScheduleGCTask(execution);
return true;
}
std::vector<Address> MemoryMeasurement::StartProcessing() {
if (received_.empty()) return {};
std::unordered_set<Address> unique_contexts;
DCHECK(processing_.empty());
processing_ = std::move(received_);
for (const auto& request : processing_) {
Handle<WeakFixedArray> contexts = request.contexts;
for (int i = 0; i < contexts->length(); i++) {
HeapObject context;
if (contexts->Get(i).GetHeapObject(&context)) {
unique_contexts.insert(context.ptr());
}
}
}
return std::vector<Address>(unique_contexts.begin(), unique_contexts.end());
}
void MemoryMeasurement::FinishProcessing(const NativeContextStats& stats) {
if (processing_.empty()) return;
while (!processing_.empty()) {
Request request = std::move(processing_.front());
processing_.pop_front();
for (int i = 0; i < static_cast<int>(request.sizes.size()); i++) {
HeapObject context;
if (!request.contexts->Get(i).GetHeapObject(&context)) {
continue;
}
request.sizes[i] = stats.Get(context.ptr());
}
request.shared = stats.Get(MarkingWorklists::kSharedContext);
done_.push_back(std::move(request));
}
ScheduleReportingTask();
}
void MemoryMeasurement::ScheduleReportingTask() {
if (reporting_task_pending_) return;
reporting_task_pending_ = true;
auto taskrunner = V8::GetCurrentPlatform()->GetForegroundTaskRunner(
reinterpret_cast<v8::Isolate*>(isolate_));
taskrunner->PostTask(MakeCancelableTask(isolate_, [this] {
reporting_task_pending_ = false;
ReportResults();
}));
}
bool MemoryMeasurement::IsGCTaskPending(v8::MeasureMemoryExecution execution) {
return execution == v8::MeasureMemoryExecution::kEager
? eager_gc_task_pending_
: delayed_gc_task_pending_;
}
void MemoryMeasurement::SetGCTaskPending(v8::MeasureMemoryExecution execution) {
if (execution == v8::MeasureMemoryExecution::kEager) {
eager_gc_task_pending_ = true;
} else {
delayed_gc_task_pending_ = true;
}
}
void MemoryMeasurement::SetGCTaskDone(v8::MeasureMemoryExecution execution) {
if (execution == v8::MeasureMemoryExecution::kEager) {
eager_gc_task_pending_ = false;
} else {
delayed_gc_task_pending_ = false;
}
}
void MemoryMeasurement::ScheduleGCTask(v8::MeasureMemoryExecution execution) {
if (IsGCTaskPending(execution)) return;
SetGCTaskPending(execution);
auto taskrunner = V8::GetCurrentPlatform()->GetForegroundTaskRunner(
reinterpret_cast<v8::Isolate*>(isolate_));
auto task = MakeCancelableTask(isolate_, [this, execution] {
SetGCTaskDone(execution);
if (received_.empty()) return;
Heap* heap = isolate_->heap();
if (FLAG_incremental_marking) {
if (heap->incremental_marking()->IsStopped()) {
heap->StartIncrementalMarking(Heap::kNoGCFlags,
GarbageCollectionReason::kMeasureMemory);
} else {
if (execution == v8::MeasureMemoryExecution::kEager) {
heap->FinalizeIncrementalMarkingAtomically(
GarbageCollectionReason::kMeasureMemory);
}
ScheduleGCTask(execution);
}
} else {
heap->CollectGarbage(OLD_SPACE, GarbageCollectionReason::kMeasureMemory);
}
});
if (execution == v8::MeasureMemoryExecution::kEager) {
taskrunner->PostTask(std::move(task));
} else {
taskrunner->PostDelayedTask(std::move(task), kGCTaskDelayInSeconds);
}
}
void MemoryMeasurement::ReportResults() {
while (!done_.empty()) {
Request request = std::move(done_.front());
done_.pop_front();
HandleScope handle_scope(isolate_);
std::vector<std::pair<v8::Local<v8::Context>, size_t>> sizes;
DCHECK_EQ(request.sizes.size(),
static_cast<size_t>(request.contexts->length()));
for (int i = 0; i < request.contexts->length(); i++) {
HeapObject raw_context;
if (!request.contexts->Get(i).GetHeapObject(&raw_context)) {
continue;
}
v8::Local<v8::Context> context = Utils::Convert<HeapObject, v8::Context>(
handle(raw_context, isolate_));
sizes.push_back(std::make_pair(context, request.sizes[i]));
}
request.delegate->MeasurementComplete(sizes, request.shared);
}
}
std::unique_ptr<v8::MeasureMemoryDelegate> MemoryMeasurement::DefaultDelegate(
Isolate* isolate, Handle<NativeContext> context, Handle<JSPromise> promise,
v8::MeasureMemoryMode mode) {
return std::make_unique<MeasureMemoryDelegate>(isolate, context, promise,
mode);
}
bool NativeContextInferrer::InferForJSFunction(JSFunction function,
......@@ -97,17 +333,8 @@ bool NativeContextInferrer::InferForJSObject(Isolate* isolate, Map map,
return true;
}
}
if (*native_context == MarkingWorklists::kSharedContext) {
// This lookup is expensive, so perform it only if the object is currently
// attributed to the shared context.
// The maximum number of steps to perform when looking for the context.
const int kMaxSteps = 3;
Object maybe_constructor = map.TryGetConstructor(isolate, kMaxSteps);
if (maybe_constructor.IsJSFunction()) {
return InferForJSFunction(JSFunction::cast(maybe_constructor),
native_context);
}
}
// TODO(ulan): Add a more precise inference for the case when
// the current context is the shared context.
return false;
}
......
......@@ -5,6 +5,7 @@
#ifndef V8_HEAP_MEMORY_MEASUREMENT_H_
#define V8_HEAP_MEMORY_MEASUREMENT_H_
#include <list>
#include <unordered_map>
#include "src/common/globals.h"
......@@ -16,14 +17,44 @@ namespace v8 {
namespace internal {
class Heap;
class NativeContextStats;
class V8_EXPORT_PRIVATE MemoryMeasurement {
public:
explicit MemoryMeasurement(Isolate* isolate);
Handle<JSPromise> EnqueueRequest(Handle<NativeContext> context,
v8::MeasureMemoryMode mode);
bool EnqueueRequest(std::unique_ptr<v8::MeasureMemoryDelegate> delegate,
v8::MeasureMemoryExecution execution,
const std::vector<Handle<NativeContext>> contexts);
std::vector<Address> StartProcessing();
void FinishProcessing(const NativeContextStats& stats);
static std::unique_ptr<v8::MeasureMemoryDelegate> DefaultDelegate(
Isolate* isolate, Handle<NativeContext> context,
Handle<JSPromise> promise, v8::MeasureMemoryMode mode);
private:
static const int kGCTaskDelayInSeconds = 60;
struct Request {
std::unique_ptr<v8::MeasureMemoryDelegate> delegate;
Handle<WeakFixedArray> contexts;
std::vector<size_t> sizes;
size_t shared;
};
void ScheduleReportingTask();
void ReportResults();
void ScheduleGCTask(v8::MeasureMemoryExecution execution);
bool IsGCTaskPending(v8::MeasureMemoryExecution execution);
void SetGCTaskPending(v8::MeasureMemoryExecution execution);
void SetGCTaskDone(v8::MeasureMemoryExecution execution);
std::list<Request> received_;
std::list<Request> processing_;
std::list<Request> done_;
Isolate* isolate_;
bool reporting_task_pending_ = false;
bool delayed_gc_task_pending_ = false;
bool eager_gc_task_pending_ = false;
};
// Infers the native context for some of the heap objects.
......@@ -54,7 +85,6 @@ class V8_EXPORT_PRIVATE NativeContextStats {
if (it == size_by_context_.end()) return 0;
return it->second;
}
void Clear();
void Merge(const NativeContextStats& other);
......
......@@ -15,7 +15,6 @@
V(_, cardinal_string, "cardinal") \
V(_, caseFirst_string, "caseFirst") \
V(_, compare_string, "compare") \
V(_, current_string, "current") \
V(_, collation_string, "collation") \
V(_, compact_string, "compact") \
V(_, compactDisplay_string, "compactDisplay") \
......@@ -158,6 +157,7 @@
V(_, conjunction_string, "conjunction") \
V(_, construct_string, "construct") \
V(_, constructor_string, "constructor") \
V(_, current_string, "current") \
V(_, Date_string, "Date") \
V(_, date_to_string, "[object Date]") \
V(_, default_string, "default") \
......@@ -252,6 +252,7 @@
V(_, of_string, "of") \
V(_, ok, "ok") \
V(_, one_string, "1") \
V(_, other_string, "other") \
V(_, ownKeys_string, "ownKeys") \
V(_, percent_string, "percent") \
V(_, position_string, "position") \
......
......@@ -51,14 +51,15 @@ TEST(NativeContextInferrerJSObject) {
LocalContext env;
Isolate* isolate = CcTest::i_isolate();
HandleScope scope(isolate);
Handle<NativeContext> native_context = GetNativeContext(isolate, env.local());
v8::Local<v8::Value> result = CompileRun("({a : 10})");
Handle<Object> object = Utils::OpenHandle(*result);
Handle<HeapObject> function = Handle<HeapObject>::cast(object);
NativeContextInferrer inferrer;
Address inferred_context = 0;
CHECK(inferrer.Infer(isolate, function->map(), *function, &inferred_context));
CHECK_EQ(native_context->ptr(), inferred_context);
// TODO(ulan): Enable this test once we have more precise native
// context inference.
CHECK(
!inferrer.Infer(isolate, function->map(), *function, &inferred_context));
}
TEST(NativeContextStatsMerge) {
......
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --expose-gc
load('test/mjsunit/mjsunit.js');
function assertLessThanOrEqual(a, b) {
assertTrue(a <= b, `Expected ${a} <= ${b}`);
}
function createRealmAndAllocate(bytes) {
let realm = Realm.createAllowCrossRealmAccess();
Realm.eval(realm, `
this.numbers = [.1];
for (let i = 0; i < ${bytes} / 8; ++i) {
this.numbers.push(0.1);
}
`);
}
if (this.performance && performance.measureMemory) {
let number_of_realms = 3;
let realms = [];
let bytes_to_allocate = 1024 * 1024;
for (let i = 0; i < number_of_realms; i++) {
realms.push(createRealmAndAllocate(bytes_to_allocate));
}
assertPromiseResult((async () => {
let result = await performance.measureMemory({detailed: true});
print(JSON.stringify(result));
assertEquals(number_of_realms, result.other.length);
for (let other of result.other) {
// TODO(ulan): compare against bytes_to_allocate once
// we have more accurate native context inference.
assertLessThanOrEqual(0, other.jsMemoryEstimate);
}
})());
// Force a GC to complete memory measurement.
gc();
}
......@@ -2,8 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Test the performance.measureMemory() function of d8. This test only makes
// sense with d8.
// Flags: --expose-gc
load('test/mjsunit/mjsunit.js');
......@@ -11,37 +10,50 @@ function assertLessThanOrEqual(a, b) {
assertTrue(a <= b, `Expected ${a} <= ${b}`);
}
function checkMeasureMemoryResult(result) {
function checkMeasureMemoryResultField(field) {
assertTrue('jsMemoryEstimate' in field);
assertTrue('jsMemoryRange' in field);
assertEquals('number', typeof field.jsMemoryEstimate);
assertEquals(2, field.jsMemoryRange.length);
assertEquals('number', typeof field.jsMemoryRange[0]);
assertEquals('number', typeof field.jsMemoryRange[1]);
assertLessThanOrEqual(field.jsMemoryRange[0],
field.jsMemoryRange[1]);
assertLessThanOrEqual(field.jsMemoryRange[0],
field.jsMemoryEstimate);
assertLessThanOrEqual(field.jsMemoryEstimate,
field.jsMemoryRange[1]);
}
function checkMeasureMemoryResult(result, detailed) {
assertTrue('total' in result);
assertTrue('jsMemoryEstimate' in result.total);
assertTrue('jsMemoryRange' in result.total);
assertEquals('number', typeof result.total.jsMemoryEstimate);
assertEquals(2, result.total.jsMemoryRange.length);
assertEquals('number', typeof result.total.jsMemoryRange[0]);
assertEquals('number', typeof result.total.jsMemoryRange[1]);
assertLessThanOrEqual(result.total.jsMemoryRange[0],
result.total.jsMemoryRange[1]);
assertLessThanOrEqual(result.total.jsMemoryRange[0],
result.total.jsMemoryEstimate);
assertLessThanOrEqual(result.total.jsMemoryEstimate,
result.total.jsMemoryRange[1]);
checkMeasureMemoryResultField(result.total);
if (detailed) {
assertTrue('current' in result);
checkMeasureMemoryResultField(result.current);
assertTrue('other' in result);
for (let other of result.other) {
checkMeasureMemoryResultField(other);
}
}
}
if (this.performance && performance.measureMemory) {
assertPromiseResult((async () => {
let result = await performance.measureMemory();
checkMeasureMemoryResult(result);
checkMeasureMemoryResult(result, false);
})());
assertPromiseResult((async () => {
let result = await performance.measureMemory({detailed: false});
checkMeasureMemoryResult(result);
checkMeasureMemoryResult(result, false);
})());
assertPromiseResult((async () => {
let result = await performance.measureMemory({detailed: true});
// TODO(ulan): Also check the detailed results once measureMemory
// supports them.
checkMeasureMemoryResult(result);
print(JSON.stringify(result));
checkMeasureMemoryResult(result, true);
})());
// Force a GC to complete memory measurement.
gc();
}
......@@ -283,56 +283,56 @@ KNOWN_MAPS = {
("read_only_space", 0x01151): (95, "EnumCacheMap"),
("read_only_space", 0x011a1): (86, "ArrayBoilerplateDescriptionMap"),
("read_only_space", 0x0129d): (98, "InterceptorInfoMap"),
("read_only_space", 0x03299): (71, "PromiseFulfillReactionJobTaskMap"),
("read_only_space", 0x032c1): (72, "PromiseRejectReactionJobTaskMap"),
("read_only_space", 0x032e9): (73, "CallableTaskMap"),
("read_only_space", 0x03311): (74, "CallbackTaskMap"),
("read_only_space", 0x03339): (75, "PromiseResolveThenableJobTaskMap"),
("read_only_space", 0x03361): (78, "FunctionTemplateInfoMap"),
("read_only_space", 0x03389): (79, "ObjectTemplateInfoMap"),
("read_only_space", 0x033b1): (80, "AccessCheckInfoMap"),
("read_only_space", 0x033d9): (81, "AccessorInfoMap"),
("read_only_space", 0x03401): (82, "AccessorPairMap"),
("read_only_space", 0x03429): (83, "AliasedArgumentsEntryMap"),
("read_only_space", 0x03451): (84, "AllocationMementoMap"),
("read_only_space", 0x03479): (87, "AsmWasmDataMap"),
("read_only_space", 0x034a1): (88, "AsyncGeneratorRequestMap"),
("read_only_space", 0x034c9): (89, "BreakPointMap"),
("read_only_space", 0x034f1): (90, "BreakPointInfoMap"),
("read_only_space", 0x03519): (91, "CachedTemplateObjectMap"),
("read_only_space", 0x03541): (93, "ClassPositionsMap"),
("read_only_space", 0x03569): (94, "DebugInfoMap"),
("read_only_space", 0x03591): (97, "FunctionTemplateRareDataMap"),
("read_only_space", 0x035b9): (100, "InterpreterDataMap"),
("read_only_space", 0x035e1): (101, "PromiseCapabilityMap"),
("read_only_space", 0x03609): (102, "PromiseReactionMap"),
("read_only_space", 0x03631): (103, "PropertyDescriptorObjectMap"),
("read_only_space", 0x03659): (104, "PrototypeInfoMap"),
("read_only_space", 0x03681): (105, "ScriptMap"),
("read_only_space", 0x036a9): (109, "SourceTextModuleInfoEntryMap"),
("read_only_space", 0x036d1): (110, "StackFrameInfoMap"),
("read_only_space", 0x036f9): (111, "StackTraceFrameMap"),
("read_only_space", 0x03721): (112, "TemplateObjectDescriptionMap"),
("read_only_space", 0x03749): (113, "Tuple2Map"),
("read_only_space", 0x03771): (114, "WasmCapiFunctionDataMap"),
("read_only_space", 0x03799): (115, "WasmDebugInfoMap"),
("read_only_space", 0x037c1): (116, "WasmExceptionTagMap"),
("read_only_space", 0x037e9): (117, "WasmExportedFunctionDataMap"),
("read_only_space", 0x03811): (118, "WasmIndirectFunctionTableMap"),
("read_only_space", 0x03839): (119, "WasmJSFunctionDataMap"),
("read_only_space", 0x03861): (99, "InternalClassMap"),
("read_only_space", 0x03889): (107, "SmiPairMap"),
("read_only_space", 0x038b1): (106, "SmiBoxMap"),
("read_only_space", 0x038d9): (108, "SortStateMap"),
("read_only_space", 0x03901): (85, "AllocationSiteWithWeakNextMap"),
("read_only_space", 0x03929): (85, "AllocationSiteWithoutWeakNextMap"),
("read_only_space", 0x03951): (76, "LoadHandler1Map"),
("read_only_space", 0x03979): (76, "LoadHandler2Map"),
("read_only_space", 0x039a1): (76, "LoadHandler3Map"),
("read_only_space", 0x039c9): (77, "StoreHandler0Map"),
("read_only_space", 0x039f1): (77, "StoreHandler1Map"),
("read_only_space", 0x03a19): (77, "StoreHandler2Map"),
("read_only_space", 0x03a41): (77, "StoreHandler3Map"),
("read_only_space", 0x032ad): (71, "PromiseFulfillReactionJobTaskMap"),
("read_only_space", 0x032d5): (72, "PromiseRejectReactionJobTaskMap"),
("read_only_space", 0x032fd): (73, "CallableTaskMap"),
("read_only_space", 0x03325): (74, "CallbackTaskMap"),
("read_only_space", 0x0334d): (75, "PromiseResolveThenableJobTaskMap"),
("read_only_space", 0x03375): (78, "FunctionTemplateInfoMap"),
("read_only_space", 0x0339d): (79, "ObjectTemplateInfoMap"),
("read_only_space", 0x033c5): (80, "AccessCheckInfoMap"),
("read_only_space", 0x033ed): (81, "AccessorInfoMap"),
("read_only_space", 0x03415): (82, "AccessorPairMap"),
("read_only_space", 0x0343d): (83, "AliasedArgumentsEntryMap"),
("read_only_space", 0x03465): (84, "AllocationMementoMap"),
("read_only_space", 0x0348d): (87, "AsmWasmDataMap"),
("read_only_space", 0x034b5): (88, "AsyncGeneratorRequestMap"),
("read_only_space", 0x034dd): (89, "BreakPointMap"),
("read_only_space", 0x03505): (90, "BreakPointInfoMap"),
("read_only_space", 0x0352d): (91, "CachedTemplateObjectMap"),
("read_only_space", 0x03555): (93, "ClassPositionsMap"),
("read_only_space", 0x0357d): (94, "DebugInfoMap"),
("read_only_space", 0x035a5): (97, "FunctionTemplateRareDataMap"),
("read_only_space", 0x035cd): (100, "InterpreterDataMap"),
("read_only_space", 0x035f5): (101, "PromiseCapabilityMap"),
("read_only_space", 0x0361d): (102, "PromiseReactionMap"),
("read_only_space", 0x03645): (103, "PropertyDescriptorObjectMap"),
("read_only_space", 0x0366d): (104, "PrototypeInfoMap"),
("read_only_space", 0x03695): (105, "ScriptMap"),
("read_only_space", 0x036bd): (109, "SourceTextModuleInfoEntryMap"),
("read_only_space", 0x036e5): (110, "StackFrameInfoMap"),
("read_only_space", 0x0370d): (111, "StackTraceFrameMap"),
("read_only_space", 0x03735): (112, "TemplateObjectDescriptionMap"),
("read_only_space", 0x0375d): (113, "Tuple2Map"),
("read_only_space", 0x03785): (114, "WasmCapiFunctionDataMap"),
("read_only_space", 0x037ad): (115, "WasmDebugInfoMap"),
("read_only_space", 0x037d5): (116, "WasmExceptionTagMap"),
("read_only_space", 0x037fd): (117, "WasmExportedFunctionDataMap"),
("read_only_space", 0x03825): (118, "WasmIndirectFunctionTableMap"),
("read_only_space", 0x0384d): (119, "WasmJSFunctionDataMap"),
("read_only_space", 0x03875): (99, "InternalClassMap"),
("read_only_space", 0x0389d): (107, "SmiPairMap"),
("read_only_space", 0x038c5): (106, "SmiBoxMap"),
("read_only_space", 0x038ed): (108, "SortStateMap"),
("read_only_space", 0x03915): (85, "AllocationSiteWithWeakNextMap"),
("read_only_space", 0x0393d): (85, "AllocationSiteWithoutWeakNextMap"),
("read_only_space", 0x03965): (76, "LoadHandler1Map"),
("read_only_space", 0x0398d): (76, "LoadHandler2Map"),
("read_only_space", 0x039b5): (76, "LoadHandler3Map"),
("read_only_space", 0x039dd): (77, "StoreHandler0Map"),
("read_only_space", 0x03a05): (77, "StoreHandler1Map"),
("read_only_space", 0x03a2d): (77, "StoreHandler2Map"),
("read_only_space", 0x03a55): (77, "StoreHandler3Map"),
("map_space", 0x00121): (1057, "ExternalMap"),
("map_space", 0x00149): (1073, "JSMessageObjectMap"),
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment