Commit 9c67143f authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

[heap] Introduce backing store counters on Heap

- Update those counters from space
- Add fast path for move memory

Bug: chromium:845409
Change-Id: Icd72e551df2422a635801fb4e31588073f81544e
Reviewed-on: https://chromium-review.googlesource.com/1219707Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55798}
parent 77db602f
...@@ -100,6 +100,11 @@ void LocalArrayBufferTracker::Add(JSArrayBuffer* buffer, size_t length) { ...@@ -100,6 +100,11 @@ void LocalArrayBufferTracker::Add(JSArrayBuffer* buffer, size_t length) {
page_->IncrementExternalBackingStoreBytes( page_->IncrementExternalBackingStoreBytes(
ExternalBackingStoreType::kArrayBuffer, length); ExternalBackingStoreType::kArrayBuffer, length);
AddInternal(buffer, length);
}
void LocalArrayBufferTracker::AddInternal(JSArrayBuffer* buffer,
size_t length) {
auto ret = array_buffers_.insert( auto ret = array_buffers_.insert(
{buffer, {buffer,
{buffer->backing_store(), length, buffer->backing_store(), {buffer->backing_store(), length, buffer->backing_store(),
......
...@@ -26,11 +26,10 @@ void LocalArrayBufferTracker::Process(Callback callback) { ...@@ -26,11 +26,10 @@ void LocalArrayBufferTracker::Process(Callback callback) {
JSArrayBuffer* new_buffer = nullptr; JSArrayBuffer* new_buffer = nullptr;
JSArrayBuffer* old_buffer = nullptr; JSArrayBuffer* old_buffer = nullptr;
size_t freed_memory = 0; size_t freed_memory = 0;
size_t moved_memory = 0;
for (TrackingData::iterator it = array_buffers_.begin(); for (TrackingData::iterator it = array_buffers_.begin();
it != array_buffers_.end(); ++it) { it != array_buffers_.end(); ++it) {
old_buffer = it->first; old_buffer = it->first;
Page* old_page = Page::FromAddress(old_buffer->address()); DCHECK_EQ(page_, Page::FromAddress(old_buffer->address()));
const CallbackResult result = callback(old_buffer, &new_buffer); const CallbackResult result = callback(old_buffer, &new_buffer);
if (result == kKeepEntry) { if (result == kKeepEntry) {
kept_array_buffers.insert(*it); kept_array_buffers.insert(*it);
...@@ -49,26 +48,25 @@ void LocalArrayBufferTracker::Process(Callback callback) { ...@@ -49,26 +48,25 @@ void LocalArrayBufferTracker::Process(Callback callback) {
// We should decrement before adding to avoid potential overflows in // We should decrement before adding to avoid potential overflows in
// the external memory counters. // the external memory counters.
DCHECK_EQ(it->first->is_wasm_memory(), it->second.is_wasm_memory); DCHECK_EQ(it->first->is_wasm_memory(), it->second.is_wasm_memory);
old_page->DecrementExternalBackingStoreBytes( tracker->AddInternal(new_buffer, length);
ExternalBackingStoreType::kArrayBuffer, length); MemoryChunk::MoveExternalBackingStoreBytes(
tracker->Add(new_buffer, length); ExternalBackingStoreType::kArrayBuffer,
static_cast<MemoryChunk*>(page_),
static_cast<MemoryChunk*>(target_page), length);
} }
moved_memory += it->second.length;
} else if (result == kRemoveEntry) { } else if (result == kRemoveEntry) {
const size_t length = it->second.length; freed_memory += it->second.length;
freed_memory += length;
// We pass backing_store() and stored length to the collector for freeing // We pass backing_store() and stored length to the collector for freeing
// the backing store. Wasm allocations will go through their own tracker // the backing store. Wasm allocations will go through their own tracker
// based on the backing store. // based on the backing store.
backing_stores_to_free.push_back(it->second); backing_stores_to_free.push_back(it->second);
old_page->DecrementExternalBackingStoreBytes(
ExternalBackingStoreType::kArrayBuffer, length);
} else { } else {
UNREACHABLE(); UNREACHABLE();
} }
} }
if (moved_memory || freed_memory) { if (freed_memory) {
page_->DecrementExternalBackingStoreBytes(
ExternalBackingStoreType::kArrayBuffer, freed_memory);
// TODO(wez): Remove backing-store from external memory accounting. // TODO(wez): Remove backing-store from external memory accounting.
page_->heap()->update_external_memory_concurrently_freed( page_->heap()->update_external_memory_concurrently_freed(
static_cast<intptr_t>(freed_memory)); static_cast<intptr_t>(freed_memory));
......
...@@ -113,6 +113,10 @@ class LocalArrayBufferTracker { ...@@ -113,6 +113,10 @@ class LocalArrayBufferTracker {
typedef std::unordered_map<JSArrayBuffer*, JSArrayBuffer::Allocation, Hasher> typedef std::unordered_map<JSArrayBuffer*, JSArrayBuffer::Allocation, Hasher>
TrackingData; TrackingData;
// Internal version of add that does not update counters. Requires separate
// logic for updating external memory counters.
inline void AddInternal(JSArrayBuffer* buffer, size_t length);
inline Space* space(); inline Space* space();
Page* page_; Page* page_;
......
...@@ -581,6 +581,21 @@ int Heap::MaxNumberToStringCacheSize() const { ...@@ -581,6 +581,21 @@ int Heap::MaxNumberToStringCacheSize() const {
// of entries. // of entries.
return static_cast<int>(number_string_cache_size * 2); return static_cast<int>(number_string_cache_size * 2);
} }
void Heap::IncrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount) {
DCHECK_GE(backing_store_bytes_ + amount, backing_store_bytes_);
backing_store_bytes_ += amount;
// TODO(mlippautz): Implement interrupt for global memory allocations that can
// trigger garbage collections.
}
void Heap::DecrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount) {
DCHECK_GE(backing_store_bytes_, amount);
backing_store_bytes_ -= amount;
}
AlwaysAllocateScope::AlwaysAllocateScope(Isolate* isolate) AlwaysAllocateScope::AlwaysAllocateScope(Isolate* isolate)
: heap_(isolate->heap()) { : heap_(isolate->heap()) {
heap_->always_allocate_scope_count_++; heap_->always_allocate_scope_count_++;
......
...@@ -155,6 +155,7 @@ Heap::Heap() ...@@ -155,6 +155,7 @@ Heap::Heap()
// Will be 4 * reserved_semispace_size_ to ensure that young // Will be 4 * reserved_semispace_size_ to ensure that young
// generation can be aligned to its size. // generation can be aligned to its size.
maximum_committed_(0), maximum_committed_(0),
backing_store_bytes_(0),
survived_since_last_expansion_(0), survived_since_last_expansion_(0),
survived_last_scavenge_(0), survived_last_scavenge_(0),
always_allocate_scope_count_(0), always_allocate_scope_count_(0),
...@@ -477,6 +478,8 @@ void Heap::PrintShortHeapStatistics() { ...@@ -477,6 +478,8 @@ void Heap::PrintShortHeapStatistics() {
CommittedMemoryOfHeapAndUnmapper() / KB); CommittedMemoryOfHeapAndUnmapper() / KB);
PrintIsolate(isolate_, "External memory reported: %6" PRId64 " KB\n", PrintIsolate(isolate_, "External memory reported: %6" PRId64 " KB\n",
external_memory_ / KB); external_memory_ / KB);
PrintIsolate(isolate_, "Backing store memory: %6" PRIuS " KB\n",
backing_store_bytes_ / KB);
PrintIsolate(isolate_, "External memory global %zu KB\n", PrintIsolate(isolate_, "External memory global %zu KB\n",
external_memory_callback_() / KB); external_memory_callback_() / KB);
PrintIsolate(isolate_, "Total time spent in GC : %.1f ms\n", PrintIsolate(isolate_, "Total time spent in GC : %.1f ms\n",
...@@ -2289,15 +2292,6 @@ bool Heap::ExternalStringTable::Contains(HeapObject* obj) { ...@@ -2289,15 +2292,6 @@ bool Heap::ExternalStringTable::Contains(HeapObject* obj) {
return false; return false;
} }
void Heap::ProcessMovedExternalString(Page* old_page, Page* new_page,
ExternalString* string) {
size_t size = string->ExternalPayloadSize();
new_page->IncrementExternalBackingStoreBytes(
ExternalBackingStoreType::kExternalString, size);
old_page->DecrementExternalBackingStoreBytes(
ExternalBackingStoreType::kExternalString, size);
}
String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap, String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap,
Object** p) { Object** p) {
MapWord first_word = HeapObject::cast(*p)->map_word(); MapWord first_word = HeapObject::cast(*p)->map_word();
...@@ -2325,9 +2319,11 @@ String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap, ...@@ -2325,9 +2319,11 @@ String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap,
// Filtering Thin strings out of the external string table. // Filtering Thin strings out of the external string table.
return nullptr; return nullptr;
} else if (new_string->IsExternalString()) { } else if (new_string->IsExternalString()) {
heap->ProcessMovedExternalString( MemoryChunk::MoveExternalBackingStoreBytes(
ExternalBackingStoreType::kExternalString,
Page::FromAddress(reinterpret_cast<Address>(*p)), Page::FromAddress(reinterpret_cast<Address>(*p)),
Page::FromHeapObject(new_string), ExternalString::cast(new_string)); Page::FromHeapObject(new_string),
ExternalString::cast(new_string)->ExternalPayloadSize());
return new_string; return new_string;
} }
......
...@@ -207,6 +207,8 @@ enum class ClearRecordedSlots { kYes, kNo }; ...@@ -207,6 +207,8 @@ enum class ClearRecordedSlots { kYes, kNo };
enum class ClearFreedMemoryMode { kClearFreedMemory, kDontClearFreedMemory }; enum class ClearFreedMemoryMode { kClearFreedMemory, kDontClearFreedMemory };
enum ExternalBackingStoreType { kArrayBuffer, kExternalString, kNumTypes };
enum class FixedArrayVisitationMode { kRegular, kIncremental }; enum class FixedArrayVisitationMode { kRegular, kIncremental };
enum class TraceRetainingPathMode { kEnabled, kDisabled }; enum class TraceRetainingPathMode { kEnabled, kDisabled };
...@@ -690,8 +692,7 @@ class Heap { ...@@ -690,8 +692,7 @@ class Heap {
external_memory_concurrently_freed_ = 0; external_memory_concurrently_freed_ = 0;
} }
void ProcessMovedExternalString(Page* old_page, Page* new_page, size_t backing_store_bytes() const { return backing_store_bytes_; }
ExternalString* string);
void CompactWeakArrayLists(PretenureFlag pretenure); void CompactWeakArrayLists(PretenureFlag pretenure);
...@@ -1843,6 +1844,12 @@ class Heap { ...@@ -1843,6 +1844,12 @@ class Heap {
void CheckIneffectiveMarkCompact(size_t old_generation_size, void CheckIneffectiveMarkCompact(size_t old_generation_size,
double mutator_utilization); double mutator_utilization);
inline void IncrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount);
inline void DecrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount);
// =========================================================================== // ===========================================================================
// Growing strategy. ========================================================= // Growing strategy. =========================================================
// =========================================================================== // ===========================================================================
...@@ -2011,6 +2018,9 @@ class Heap { ...@@ -2011,6 +2018,9 @@ class Heap {
bool old_generation_size_configured_; bool old_generation_size_configured_;
size_t maximum_committed_; size_t maximum_committed_;
// Backing store bytes (array buffers and external strings).
std::atomic<size_t> backing_store_bytes_;
// For keeping track of how much data has survived // For keeping track of how much data has survived
// scavenge since last new space expansion. // scavenge since last new space expansion.
size_t survived_since_last_expansion_; size_t survived_since_last_expansion_;
...@@ -2286,6 +2296,7 @@ class Heap { ...@@ -2286,6 +2296,7 @@ class Heap {
friend class Page; friend class Page;
friend class PagedSpace; friend class PagedSpace;
friend class Scavenger; friend class Scavenger;
friend class Space;
friend class StoreBuffer; friend class StoreBuffer;
friend class Sweeper; friend class Sweeper;
friend class heap::TestMemoryAllocatorScope; friend class heap::TestMemoryAllocatorScope;
......
...@@ -2274,9 +2274,11 @@ static String* UpdateReferenceInExternalStringTableEntry(Heap* heap, ...@@ -2274,9 +2274,11 @@ static String* UpdateReferenceInExternalStringTableEntry(Heap* heap,
String* new_string = String::cast(map_word.ToForwardingAddress()); String* new_string = String::cast(map_word.ToForwardingAddress());
if (new_string->IsExternalString()) { if (new_string->IsExternalString()) {
heap->ProcessMovedExternalString( MemoryChunk::MoveExternalBackingStoreBytes(
ExternalBackingStoreType::kExternalString,
Page::FromAddress(reinterpret_cast<Address>(*p)), Page::FromAddress(reinterpret_cast<Address>(*p)),
Page::FromHeapObject(new_string), ExternalString::cast(new_string)); Page::FromHeapObject(new_string),
ExternalString::cast(new_string)->ExternalPayloadSize());
} }
return new_string; return new_string;
} }
......
...@@ -93,6 +93,33 @@ HeapObject* HeapObjectIterator::FromCurrentPage() { ...@@ -93,6 +93,33 @@ HeapObject* HeapObjectIterator::FromCurrentPage() {
return nullptr; return nullptr;
} }
void Space::IncrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount) {
DCHECK_GE(external_backing_store_bytes_[type] + amount,
external_backing_store_bytes_[type]);
external_backing_store_bytes_[type] += amount;
heap()->IncrementExternalBackingStoreBytes(type, amount);
}
void Space::DecrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount) {
DCHECK_GE(external_backing_store_bytes_[type], amount);
external_backing_store_bytes_[type] -= amount;
heap()->DecrementExternalBackingStoreBytes(type, amount);
}
void Space::MoveExternalBackingStoreBytes(ExternalBackingStoreType type,
Space* from, Space* to,
size_t amount) {
if (from == to) return;
DCHECK_GE(from->external_backing_store_bytes_[type], amount);
from->external_backing_store_bytes_[type] -= amount;
DCHECK_GE(to->external_backing_store_bytes_[type] + amount,
to->external_backing_store_bytes_[type]);
to->external_backing_store_bytes_[type] += amount;
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// SemiSpace // SemiSpace
...@@ -190,6 +217,34 @@ MemoryChunk* MemoryChunk::FromAnyPointerAddress(Heap* heap, Address addr) { ...@@ -190,6 +217,34 @@ MemoryChunk* MemoryChunk::FromAnyPointerAddress(Heap* heap, Address addr) {
return chunk; return chunk;
} }
void MemoryChunk::IncrementExternalBackingStoreBytes(
ExternalBackingStoreType type, size_t amount) {
DCHECK_GE(external_backing_store_bytes_[type] + amount,
external_backing_store_bytes_[type]);
external_backing_store_bytes_[type] += amount;
owner()->IncrementExternalBackingStoreBytes(type, amount);
}
void MemoryChunk::DecrementExternalBackingStoreBytes(
ExternalBackingStoreType type, size_t amount) {
DCHECK_GE(external_backing_store_bytes_[type], amount);
external_backing_store_bytes_[type] -= amount;
owner()->DecrementExternalBackingStoreBytes(type, amount);
}
void MemoryChunk::MoveExternalBackingStoreBytes(ExternalBackingStoreType type,
MemoryChunk* from,
MemoryChunk* to,
size_t amount) {
DCHECK_GE(from->external_backing_store_bytes_[type], amount);
from->external_backing_store_bytes_[type] -= amount;
DCHECK_GE(to->external_backing_store_bytes_[type] + amount,
to->external_backing_store_bytes_[type]);
to->external_backing_store_bytes_[type] += amount;
Space::MoveExternalBackingStoreBytes(type, from->owner(), to->owner(),
amount);
}
void Page::MarkNeverAllocateForTesting() { void Page::MarkNeverAllocateForTesting() {
DCHECK(this->owner()->identity() != NEW_SPACE); DCHECK(this->owner()->identity() != NEW_SPACE);
DCHECK(!IsFlagSet(NEVER_ALLOCATE_ON_PAGE)); DCHECK(!IsFlagSet(NEVER_ALLOCATE_ON_PAGE));
......
...@@ -1308,19 +1308,6 @@ void MemoryChunk::ReleaseYoungGenerationBitmap() { ...@@ -1308,19 +1308,6 @@ void MemoryChunk::ReleaseYoungGenerationBitmap() {
young_generation_bitmap_ = nullptr; young_generation_bitmap_ = nullptr;
} }
void MemoryChunk::IncrementExternalBackingStoreBytes(
ExternalBackingStoreType type, size_t amount) {
external_backing_store_bytes_[type] += amount;
owner()->IncrementExternalBackingStoreBytes(type, amount);
}
void MemoryChunk::DecrementExternalBackingStoreBytes(
ExternalBackingStoreType type, size_t amount) {
DCHECK_GE(external_backing_store_bytes_[type], amount);
external_backing_store_bytes_[type] -= amount;
owner()->DecrementExternalBackingStoreBytes(type, amount);
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// PagedSpace implementation // PagedSpace implementation
......
...@@ -143,12 +143,6 @@ enum FreeMode { kLinkCategory, kDoNotLinkCategory }; ...@@ -143,12 +143,6 @@ enum FreeMode { kLinkCategory, kDoNotLinkCategory };
enum class SpaceAccountingMode { kSpaceAccounted, kSpaceUnaccounted }; enum class SpaceAccountingMode { kSpaceAccounted, kSpaceUnaccounted };
enum ExternalBackingStoreType {
kArrayBuffer,
kExternalString,
kNumTypes
};
enum RememberedSetType { enum RememberedSetType {
OLD_TO_NEW, OLD_TO_NEW,
OLD_TO_OLD, OLD_TO_OLD,
...@@ -379,7 +373,7 @@ class MemoryChunk { ...@@ -379,7 +373,7 @@ class MemoryChunk {
kPointerSize // std::atomic<ConcurrentSweepingState> concurrent_sweeping_ kPointerSize // std::atomic<ConcurrentSweepingState> concurrent_sweeping_
+ kPointerSize // base::Mutex* page_protection_change_mutex_ + kPointerSize // base::Mutex* page_protection_change_mutex_
+ kPointerSize // unitptr_t write_unprotect_counter_ + kPointerSize // unitptr_t write_unprotect_counter_
+ kSizetSize * kNumTypes + kSizetSize * ExternalBackingStoreType::kNumTypes
// std::atomic<size_t> external_backing_store_bytes_ // std::atomic<size_t> external_backing_store_bytes_
+ kSizetSize // size_t allocated_bytes_ + kSizetSize // size_t allocated_bytes_
+ kSizetSize // size_t wasted_memory_ + kSizetSize // size_t wasted_memory_
...@@ -445,6 +439,10 @@ class MemoryChunk { ...@@ -445,6 +439,10 @@ class MemoryChunk {
!chunk->high_water_mark_.compare_exchange_weak(old_mark, new_mark)); !chunk->high_water_mark_.compare_exchange_weak(old_mark, new_mark));
} }
static inline void MoveExternalBackingStoreBytes(
ExternalBackingStoreType type, MemoryChunk* from, MemoryChunk* to,
size_t amount);
Address address() const { Address address() const {
return reinterpret_cast<Address>(const_cast<MemoryChunk*>(this)); return reinterpret_cast<Address>(const_cast<MemoryChunk*>(this));
} }
...@@ -551,10 +549,12 @@ class MemoryChunk { ...@@ -551,10 +549,12 @@ class MemoryChunk {
} }
} }
void IncrementExternalBackingStoreBytes(ExternalBackingStoreType type, inline void IncrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount); size_t amount);
void DecrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount); inline void DecrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount);
size_t ExternalBackingStoreBytes(ExternalBackingStoreType type) { size_t ExternalBackingStoreBytes(ExternalBackingStoreType type) {
return external_backing_store_bytes_[type]; return external_backing_store_bytes_[type];
} }
...@@ -945,6 +945,9 @@ class Space : public Malloced { ...@@ -945,6 +945,9 @@ class Space : public Malloced {
0; 0;
} }
static inline void MoveExternalBackingStoreBytes(
ExternalBackingStoreType type, Space* from, Space* to, size_t amount);
virtual ~Space() { virtual ~Space() {
delete[] external_backing_store_bytes_; delete[] external_backing_store_bytes_;
external_backing_store_bytes_ = nullptr; external_backing_store_bytes_ = nullptr;
...@@ -984,12 +987,6 @@ class Space : public Malloced { ...@@ -984,12 +987,6 @@ class Space : public Malloced {
// (e.g. see LargeObjectSpace). // (e.g. see LargeObjectSpace).
virtual size_t SizeOfObjects() { return Size(); } virtual size_t SizeOfObjects() { return Size(); }
// Returns amount of off-heap memory in-use by objects in this Space.
virtual size_t ExternalBackingStoreBytes(
ExternalBackingStoreType type) const {
return external_backing_store_bytes_[type];
}
// Approximate amount of physical memory committed for this space. // Approximate amount of physical memory committed for this space.
virtual size_t CommittedPhysicalMemory() = 0; virtual size_t CommittedPhysicalMemory() = 0;
...@@ -1019,14 +1016,16 @@ class Space : public Malloced { ...@@ -1019,14 +1016,16 @@ class Space : public Malloced {
committed_ -= bytes; committed_ -= bytes;
} }
void IncrementExternalBackingStoreBytes(ExternalBackingStoreType type, inline void IncrementExternalBackingStoreBytes(ExternalBackingStoreType type,
size_t amount) { size_t amount);
external_backing_store_bytes_[type] += amount;
} inline void DecrementExternalBackingStoreBytes(ExternalBackingStoreType type,
void DecrementExternalBackingStoreBytes(ExternalBackingStoreType type, size_t amount);
size_t amount) {
DCHECK_GE(external_backing_store_bytes_[type], amount); // Returns amount of off-heap memory in-use by objects in this Space.
external_backing_store_bytes_[type] -= amount; virtual size_t ExternalBackingStoreBytes(
ExternalBackingStoreType type) const {
return external_backing_store_bytes_[type];
} }
V8_EXPORT_PRIVATE void* GetRandomMmapAddr(); V8_EXPORT_PRIVATE void* GetRandomMmapAddr();
......
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