Commit 98e955a7 authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Make {WasmMemoryTracker} independent of the Isolate.

This removes two pointers to Histograms from the memory tracker. These
histograms are stored as part of the Isolate and their lifetime is also
coupled to the Isolate. We cannot bind the pointers but need to pass
them (or the Isolate) as a parameter instead.

R=clemensh@chromium.org
BUG=v8:7424

Change-Id: I6b141b924bd858234641d6603a25fcb08cdf40e3
Reviewed-on: https://chromium-review.googlesource.com/1140312
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54512}
parent 9991fbd2
......@@ -2968,10 +2968,6 @@ bool Isolate::Init(StartupDeserializer* des) {
wasm_engine_.reset(
new wasm::WasmEngine(std::unique_ptr<wasm::WasmCodeManager>(
new wasm::WasmCodeManager(kMaxWasmCodeMemory))));
wasm_engine_->memory_tracker()->SetAllocationResultHistogram(
counters()->wasm_memory_allocation_result());
wasm_engine_->memory_tracker()->SetAddressSpaceUsageHistogram(
counters()->wasm_address_space_usage_mb());
wasm_engine_->code_manager()->SetModuleCodeSizeHistogram(
counters()->wasm_module_code_size_mb());
}
......
......@@ -18715,7 +18715,8 @@ void JSArrayBuffer::Neuter() {
void JSArrayBuffer::StopTrackingWasmMemory(Isolate* isolate) {
DCHECK(is_wasm_memory());
isolate->wasm_engine()->memory_tracker()->ReleaseAllocation(backing_store());
isolate->wasm_engine()->memory_tracker()->ReleaseAllocation(isolate,
backing_store());
set_is_wasm_memory(false);
}
......@@ -18735,7 +18736,8 @@ void JSArrayBuffer::FreeBackingStore(Isolate* isolate, Allocation allocation) {
if (allocation.is_wasm_memory) {
wasm::WasmMemoryTracker* memory_tracker =
isolate->wasm_engine()->memory_tracker();
if (!memory_tracker->FreeMemoryIfIsWasmMemory(allocation.backing_store)) {
if (!memory_tracker->FreeMemoryIfIsWasmMemory(isolate,
allocation.backing_store)) {
CHECK(FreePages(allocation.allocation_base, allocation.length));
}
} else {
......
......@@ -18,6 +18,12 @@ namespace {
constexpr size_t kNegativeGuardSize = 1u << 31; // 2GiB
void AddAllocationStatusSample(Isolate* isolate,
WasmMemoryTracker::AllocationStatus status) {
isolate->counters()->wasm_memory_allocation_result()->AddSample(
static_cast<int>(status));
}
void* TryAllocateBackingStore(WasmMemoryTracker* memory_tracker, Heap* heap,
size_t size, bool require_full_guard_regions,
void** allocation_base,
......@@ -62,8 +68,8 @@ void* TryAllocateBackingStore(WasmMemoryTracker* memory_tracker, Heap* heap,
if (FLAG_abort_on_stack_or_string_length_overflow) {
FATAL("could not allocate wasm memory");
}
memory_tracker->AddAllocationStatusSample(
AllocationStatus::kAddressSpaceLimitReachedFailure);
AddAllocationStatusSample(
heap->isolate(), AllocationStatus::kAddressSpaceLimitReachedFailure);
return nullptr;
}
......@@ -72,7 +78,7 @@ void* TryAllocateBackingStore(WasmMemoryTracker* memory_tracker, Heap* heap,
PageAllocator::kNoAccess);
if (*allocation_base == nullptr) {
memory_tracker->ReleaseReservation(*allocation_length);
memory_tracker->AddAllocationStatusSample(AllocationStatus::kOtherFailure);
AddAllocationStatusSample(heap->isolate(), AllocationStatus::kOtherFailure);
return nullptr;
}
byte* memory = reinterpret_cast<byte*>(*allocation_base);
......@@ -91,9 +97,9 @@ void* TryAllocateBackingStore(WasmMemoryTracker* memory_tracker, Heap* heap,
}
}
memory_tracker->RegisterAllocation(*allocation_base, *allocation_length,
memory, size);
memory_tracker->AddAllocationStatusSample(
memory_tracker->RegisterAllocation(heap->isolate(), *allocation_base,
*allocation_length, memory, size);
AddAllocationStatusSample(heap->isolate(),
did_retry ? AllocationStatus::kSuccessAfterRetry
: AllocationStatus::kSuccess);
return memory;
......@@ -138,14 +144,15 @@ void WasmMemoryTracker::ReleaseReservation(size_t num_bytes) {
DCHECK_LE(num_bytes, old_reserved);
}
void WasmMemoryTracker::RegisterAllocation(void* allocation_base,
void WasmMemoryTracker::RegisterAllocation(Isolate* isolate,
void* allocation_base,
size_t allocation_length,
void* buffer_start,
size_t buffer_length) {
base::LockGuard<base::Mutex> scope_lock(&mutex_);
allocated_address_space_ += allocation_length;
AddAddressSpaceSample();
AddAddressSpaceSample(isolate);
allocations_.emplace(buffer_start,
AllocationData{allocation_base, allocation_length,
......@@ -153,12 +160,7 @@ void WasmMemoryTracker::RegisterAllocation(void* allocation_base,
}
WasmMemoryTracker::AllocationData WasmMemoryTracker::ReleaseAllocation(
const void* buffer_start) {
return InternalReleaseAllocation(buffer_start);
}
WasmMemoryTracker::AllocationData WasmMemoryTracker::InternalReleaseAllocation(
const void* buffer_start) {
Isolate* isolate, const void* buffer_start) {
base::LockGuard<base::Mutex> scope_lock(&mutex_);
auto find_result = allocations_.find(buffer_start);
......@@ -170,7 +172,7 @@ WasmMemoryTracker::AllocationData WasmMemoryTracker::InternalReleaseAllocation(
DCHECK_LE(num_bytes, allocated_address_space_);
reserved_address_space_ -= num_bytes;
allocated_address_space_ -= num_bytes;
AddAddressSpaceSample();
AddAddressSpaceSample(isolate);
AllocationData allocation_data = find_result->second;
allocations_.erase(find_result);
......@@ -209,28 +211,21 @@ bool WasmMemoryTracker::HasFullGuardRegions(const void* buffer_start) {
return start + kWasmMaxHeapOffset < limit;
}
bool WasmMemoryTracker::FreeMemoryIfIsWasmMemory(const void* buffer_start) {
bool WasmMemoryTracker::FreeMemoryIfIsWasmMemory(Isolate* isolate,
const void* buffer_start) {
if (IsWasmMemory(buffer_start)) {
const AllocationData allocation = ReleaseAllocation(buffer_start);
const AllocationData allocation = ReleaseAllocation(isolate, buffer_start);
CHECK(FreePages(allocation.allocation_base, allocation.allocation_length));
return true;
}
return false;
}
void WasmMemoryTracker::AddAllocationStatusSample(AllocationStatus status) {
if (allocation_result_) {
allocation_result_->AddSample(static_cast<int>(status));
}
}
void WasmMemoryTracker::AddAddressSpaceSample() {
if (address_space_usage_mb_) {
void WasmMemoryTracker::AddAddressSpaceSample(Isolate* isolate) {
// Report address space usage in MiB so the full range fits in an int on all
// platforms.
address_space_usage_mb_->AddSample(
isolate->counters()->wasm_address_space_usage_mb()->AddSample(
static_cast<int>(allocated_address_space_ >> 20));
}
}
Handle<JSArrayBuffer> SetupArrayBuffer(Isolate* isolate, void* backing_store,
......
......@@ -15,9 +15,6 @@
namespace v8 {
namespace internal {
class Histogram; // defined in counters.h
namespace wasm {
class WasmMemoryTracker {
......@@ -31,8 +28,9 @@ class WasmMemoryTracker {
// allocate the buffer), false otherwise.
bool ReserveAddressSpace(size_t num_bytes);
void RegisterAllocation(void* allocation_base, size_t allocation_length,
void* buffer_start, size_t buffer_length);
void RegisterAllocation(Isolate* isolate, void* allocation_base,
size_t allocation_length, void* buffer_start,
size_t buffer_length);
struct AllocationData {
void* allocation_base = nullptr;
......@@ -65,7 +63,7 @@ class WasmMemoryTracker {
void ReleaseReservation(size_t num_bytes);
// Removes an allocation from the tracker
AllocationData ReleaseAllocation(const void* buffer_start);
AllocationData ReleaseAllocation(Isolate* isolate, const void* buffer_start);
bool IsWasmMemory(const void* buffer_start);
......@@ -80,14 +78,7 @@ class WasmMemoryTracker {
// Checks if a buffer points to a Wasm memory and if so does any necessary
// work to reclaim the buffer. If this function returns false, the caller must
// free the buffer manually.
bool FreeMemoryIfIsWasmMemory(const void* buffer_start);
void SetAllocationResultHistogram(Histogram* allocation_result) {
allocation_result_ = allocation_result;
}
void SetAddressSpaceUsageHistogram(Histogram* address_space_usage) {
address_space_usage_mb_ = address_space_usage;
}
bool FreeMemoryIfIsWasmMemory(Isolate* isolate, const void* buffer_start);
// Allocation results are reported to UMA
//
......@@ -103,11 +94,8 @@ class WasmMemoryTracker {
kOtherFailure // Failed for an unknown reason
};
void AddAllocationStatusSample(AllocationStatus status);
private:
AllocationData InternalReleaseAllocation(const void* buffer_start);
void AddAddressSpaceSample();
void AddAddressSpaceSample(Isolate* isolate);
// Clients use a two-part process. First they "reserve" the address space,
// which signifies an intent to actually allocate it. This determines whether
......@@ -129,10 +117,6 @@ class WasmMemoryTracker {
// buffer, rather than by the start of the allocation.
std::unordered_map<const void*, AllocationData> allocations_;
// Keep pointers to
Histogram* allocation_result_ = nullptr;
Histogram* address_space_usage_mb_ = nullptr; // in MiB
DISALLOW_COPY_AND_ASSIGN(WasmMemoryTracker);
};
......
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