Commit 5febb062 authored by Omer Katz's avatar Omer Katz Committed by V8 LUCI CQ

[heap] Pass AllocationCounter to Space

Extract AllocationCounter (which manages the allocation observers) out
of Space and pass it on initialization.
AllocationCounter will instead be a field of the subclasses (e.g.
PagedSpace, NewSpace).
This will allow to reuse the same AllocationCounter for the NewSpace and
PagedSpaceBase parts of PagedNewSpace (crrev.com/c/3641178).

Bug: v8:12612
Change-Id: Ie6ed10d1b138a5724cf9b469ab9c943518850ad4
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3745403
Commit-Queue: Omer Katz <omerkatz@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81524}
parent 2d74bfa4
...@@ -95,7 +95,7 @@ HeapObject LargeObjectSpaceObjectIterator::Next() { ...@@ -95,7 +95,7 @@ HeapObject LargeObjectSpaceObjectIterator::Next() {
// OldLargeObjectSpace // OldLargeObjectSpace
LargeObjectSpace::LargeObjectSpace(Heap* heap, AllocationSpace id) LargeObjectSpace::LargeObjectSpace(Heap* heap, AllocationSpace id)
: Space(heap, id, new NoFreeList()), : Space(heap, id, new NoFreeList(), &allocation_counter_),
size_(0), size_(0),
page_count_(0), page_count_(0),
objects_size_(0), objects_size_(0),
......
...@@ -164,6 +164,8 @@ class V8_EXPORT_PRIVATE LargeObjectSpace : public Space { ...@@ -164,6 +164,8 @@ class V8_EXPORT_PRIVATE LargeObjectSpace : public Space {
// Used to protect pending_object_. // Used to protect pending_object_.
base::SharedMutex pending_allocation_mutex_; base::SharedMutex pending_allocation_mutex_;
AllocationCounter allocation_counter_;
private: private:
friend class LargeObjectSpaceObjectIterator; friend class LargeObjectSpaceObjectIterator;
}; };
......
...@@ -443,7 +443,8 @@ void SemiSpaceObjectIterator::Initialize(Address start, Address end) { ...@@ -443,7 +443,8 @@ void SemiSpaceObjectIterator::Initialize(Address start, Address end) {
// NewSpace implementation // NewSpace implementation
NewSpace::NewSpace(Heap* heap, LinearAllocationArea* allocation_info) NewSpace::NewSpace(Heap* heap, LinearAllocationArea* allocation_info)
: SpaceWithLinearArea(heap, NEW_SPACE, new NoFreeList(), allocation_info, : SpaceWithLinearArea(heap, NEW_SPACE, new NoFreeList(),
&allocation_counter_, allocation_info,
linear_area_original_data_) {} linear_area_original_data_) {}
void NewSpace::ResetParkedAllocationBuffers() { void NewSpace::ResetParkedAllocationBuffers() {
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/common/globals.h" #include "src/common/globals.h"
#include "src/heap/allocation-observer.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/spaces.h" #include "src/heap/spaces.h"
#include "src/logging/log.h" #include "src/logging/log.h"
...@@ -42,7 +43,7 @@ class SemiSpace final : public Space { ...@@ -42,7 +43,7 @@ class SemiSpace final : public Space {
static void Swap(SemiSpace* from, SemiSpace* to); static void Swap(SemiSpace* from, SemiSpace* to);
SemiSpace(Heap* heap, SemiSpaceId semispace) SemiSpace(Heap* heap, SemiSpaceId semispace)
: Space(heap, NEW_SPACE, new NoFreeList()), : Space(heap, NEW_SPACE, new NoFreeList(), &allocation_counter_),
current_capacity_(0), current_capacity_(0),
target_capacity_(0), target_capacity_(0),
maximum_capacity_(0), maximum_capacity_(0),
...@@ -209,6 +210,8 @@ class SemiSpace final : public Space { ...@@ -209,6 +210,8 @@ class SemiSpace final : public Space {
Page* current_page_; Page* current_page_;
AllocationCounter allocation_counter_;
friend class SemiSpaceNewSpace; friend class SemiSpaceNewSpace;
friend class SemiSpaceObjectIterator; friend class SemiSpaceObjectIterator;
}; };
...@@ -331,6 +334,7 @@ class NewSpace : NON_EXPORTED_BASE(public SpaceWithLinearArea) { ...@@ -331,6 +334,7 @@ class NewSpace : NON_EXPORTED_BASE(public SpaceWithLinearArea) {
base::Mutex mutex_; base::Mutex mutex_;
AllocationCounter allocation_counter_;
LinearAreaOriginalData linear_area_original_data_; LinearAreaOriginalData linear_area_original_data_;
ParkedAllocationBuffersVector parked_allocation_buffers_; ParkedAllocationBuffersVector parked_allocation_buffers_;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/execution/isolate.h" #include "src/execution/isolate.h"
#include "src/execution/vm-state-inl.h" #include "src/execution/vm-state-inl.h"
#include "src/heap/allocation-observer.h"
#include "src/heap/array-buffer-sweeper.h" #include "src/heap/array-buffer-sweeper.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/incremental-marking.h" #include "src/heap/incremental-marking.h"
...@@ -113,11 +114,12 @@ Page* PagedSpaceBase::InitializePage(MemoryChunk* chunk) { ...@@ -113,11 +114,12 @@ Page* PagedSpaceBase::InitializePage(MemoryChunk* chunk) {
PagedSpaceBase::PagedSpaceBase( PagedSpaceBase::PagedSpaceBase(
Heap* heap, AllocationSpace space, Executability executable, Heap* heap, AllocationSpace space, Executability executable,
FreeList* free_list, LinearAllocationArea* allocation_info_, FreeList* free_list, AllocationCounter* allocation_counter,
LinearAllocationArea* allocation_info_,
LinearAreaOriginalData& linear_area_original_data, LinearAreaOriginalData& linear_area_original_data,
CompactionSpaceKind compaction_space_kind) CompactionSpaceKind compaction_space_kind)
: SpaceWithLinearArea(heap, space, free_list, allocation_info_, : SpaceWithLinearArea(heap, space, free_list, allocation_counter,
linear_area_original_data), allocation_info_, linear_area_original_data),
executable_(executable), executable_(executable),
compaction_space_kind_(compaction_space_kind) { compaction_space_kind_(compaction_space_kind) {
area_size_ = MemoryChunkLayout::AllocatableMemoryInMemoryChunk(space); area_size_ = MemoryChunkLayout::AllocatableMemoryInMemoryChunk(space);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/common/globals.h" #include "src/common/globals.h"
#include "src/flags/flags.h" #include "src/flags/flags.h"
#include "src/heap/allocation-observer.h"
#include "src/heap/allocation-stats.h" #include "src/heap/allocation-stats.h"
#include "src/heap/memory-chunk-layout.h" #include "src/heap/memory-chunk-layout.h"
#include "src/heap/memory-chunk.h" #include "src/heap/memory-chunk.h"
...@@ -91,7 +92,8 @@ class V8_EXPORT_PRIVATE PagedSpaceBase ...@@ -91,7 +92,8 @@ class V8_EXPORT_PRIVATE PagedSpaceBase
// Creates a space with an id. // Creates a space with an id.
PagedSpaceBase( PagedSpaceBase(
Heap* heap, AllocationSpace id, Executability executable, Heap* heap, AllocationSpace id, Executability executable,
FreeList* free_list, LinearAllocationArea* allocation_info, FreeList* free_list, AllocationCounter* allocation_counter,
LinearAllocationArea* allocation_info,
LinearAreaOriginalData& linear_area_original_data, LinearAreaOriginalData& linear_area_original_data,
CompactionSpaceKind compaction_space_kind = CompactionSpaceKind::kNone); CompactionSpaceKind compaction_space_kind = CompactionSpaceKind::kNone);
...@@ -422,10 +424,12 @@ class V8_EXPORT_PRIVATE PagedSpace : public PagedSpaceBase { ...@@ -422,10 +424,12 @@ class V8_EXPORT_PRIVATE PagedSpace : public PagedSpaceBase {
Heap* heap, AllocationSpace id, Executability executable, Heap* heap, AllocationSpace id, Executability executable,
FreeList* free_list, LinearAllocationArea* allocation_info, FreeList* free_list, LinearAllocationArea* allocation_info,
CompactionSpaceKind compaction_space_kind = CompactionSpaceKind::kNone) CompactionSpaceKind compaction_space_kind = CompactionSpaceKind::kNone)
: PagedSpaceBase(heap, id, executable, free_list, allocation_info, : PagedSpaceBase(heap, id, executable, free_list, &allocation_counter_,
linear_area_original_data_, compaction_space_kind) {} allocation_info, linear_area_original_data_,
compaction_space_kind) {}
private: private:
AllocationCounter allocation_counter_;
LinearAreaOriginalData linear_area_original_data_; LinearAreaOriginalData linear_area_original_data_;
}; };
......
...@@ -220,16 +220,16 @@ void Page::DestroyBlackAreaBackground(Address start, Address end) { ...@@ -220,16 +220,16 @@ void Page::DestroyBlackAreaBackground(Address start, Address end) {
// PagedSpace implementation // PagedSpace implementation
void Space::AddAllocationObserver(AllocationObserver* observer) { void Space::AddAllocationObserver(AllocationObserver* observer) {
allocation_counter_.AddAllocationObserver(observer); allocation_counter_->AddAllocationObserver(observer);
} }
void Space::RemoveAllocationObserver(AllocationObserver* observer) { void Space::RemoveAllocationObserver(AllocationObserver* observer) {
allocation_counter_.RemoveAllocationObserver(observer); allocation_counter_->RemoveAllocationObserver(observer);
} }
void Space::PauseAllocationObservers() { allocation_counter_.Pause(); } void Space::PauseAllocationObservers() { allocation_counter_->Pause(); }
void Space::ResumeAllocationObservers() { allocation_counter_.Resume(); } void Space::ResumeAllocationObservers() { allocation_counter_->Resume(); }
Address SpaceWithLinearArea::ComputeLimit(Address start, Address end, Address SpaceWithLinearArea::ComputeLimit(Address start, Address end,
size_t min_size) const { size_t min_size) const {
...@@ -240,13 +240,13 @@ Address SpaceWithLinearArea::ComputeLimit(Address start, Address end, ...@@ -240,13 +240,13 @@ Address SpaceWithLinearArea::ComputeLimit(Address start, Address end,
return start + min_size; return start + min_size;
} }
if (SupportsAllocationObserver() && allocation_counter_.IsActive()) { if (SupportsAllocationObserver() && allocation_counter_->IsActive()) {
// Ensure there are no unaccounted allocations. // Ensure there are no unaccounted allocations.
DCHECK_EQ(allocation_info_->start(), allocation_info_->top()); DCHECK_EQ(allocation_info_->start(), allocation_info_->top());
// Generated code may allocate inline from the linear allocation area for. // Generated code may allocate inline from the linear allocation area for.
// To make sure we can observe these allocations, we use a lower ©limit. // To make sure we can observe these allocations, we use a lower ©limit.
size_t step = allocation_counter_.NextBytes(); size_t step = allocation_counter_->NextBytes();
DCHECK_NE(step, 0); DCHECK_NE(step, 0);
size_t rounded_step = size_t rounded_step =
RoundSizeDownToObjectAlignment(static_cast<int>(step - 1)); RoundSizeDownToObjectAlignment(static_cast<int>(step - 1));
...@@ -330,7 +330,7 @@ LocalAllocationBuffer& LocalAllocationBuffer::operator=( ...@@ -330,7 +330,7 @@ LocalAllocationBuffer& LocalAllocationBuffer::operator=(
} }
void SpaceWithLinearArea::AddAllocationObserver(AllocationObserver* observer) { void SpaceWithLinearArea::AddAllocationObserver(AllocationObserver* observer) {
if (!allocation_counter_.IsStepInProgress()) { if (!allocation_counter_->IsStepInProgress()) {
AdvanceAllocationObservers(); AdvanceAllocationObservers();
Space::AddAllocationObserver(observer); Space::AddAllocationObserver(observer);
UpdateInlineAllocationLimit(0); UpdateInlineAllocationLimit(0);
...@@ -341,7 +341,7 @@ void SpaceWithLinearArea::AddAllocationObserver(AllocationObserver* observer) { ...@@ -341,7 +341,7 @@ void SpaceWithLinearArea::AddAllocationObserver(AllocationObserver* observer) {
void SpaceWithLinearArea::RemoveAllocationObserver( void SpaceWithLinearArea::RemoveAllocationObserver(
AllocationObserver* observer) { AllocationObserver* observer) {
if (!allocation_counter_.IsStepInProgress()) { if (!allocation_counter_->IsStepInProgress()) {
AdvanceAllocationObservers(); AdvanceAllocationObservers();
Space::RemoveAllocationObserver(observer); Space::RemoveAllocationObserver(observer);
UpdateInlineAllocationLimit(0); UpdateInlineAllocationLimit(0);
...@@ -364,8 +364,8 @@ void SpaceWithLinearArea::ResumeAllocationObservers() { ...@@ -364,8 +364,8 @@ void SpaceWithLinearArea::ResumeAllocationObservers() {
void SpaceWithLinearArea::AdvanceAllocationObservers() { void SpaceWithLinearArea::AdvanceAllocationObservers() {
if (allocation_info_->top() && if (allocation_info_->top() &&
allocation_info_->start() != allocation_info_->top()) { allocation_info_->start() != allocation_info_->top()) {
allocation_counter_.AdvanceAllocationObservers(allocation_info_->top() - allocation_counter_->AdvanceAllocationObservers(allocation_info_->top() -
allocation_info_->start()); allocation_info_->start());
MarkLabStartInitialized(); MarkLabStartInitialized();
} }
} }
...@@ -397,9 +397,9 @@ void SpaceWithLinearArea::InvokeAllocationObservers( ...@@ -397,9 +397,9 @@ void SpaceWithLinearArea::InvokeAllocationObservers(
DCHECK(size_in_bytes == aligned_size_in_bytes || DCHECK(size_in_bytes == aligned_size_in_bytes ||
aligned_size_in_bytes == allocation_size); aligned_size_in_bytes == allocation_size);
if (!SupportsAllocationObserver() || !allocation_counter_.IsActive()) return; if (!SupportsAllocationObserver() || !allocation_counter_->IsActive()) return;
if (allocation_size >= allocation_counter_.NextBytes()) { if (allocation_size >= allocation_counter_->NextBytes()) {
// Only the first object in a LAB should reach the next step. // Only the first object in a LAB should reach the next step.
DCHECK_EQ(soon_object, allocation_info_->start() + aligned_size_in_bytes - DCHECK_EQ(soon_object, allocation_info_->start() + aligned_size_in_bytes -
size_in_bytes); size_in_bytes);
...@@ -423,8 +423,8 @@ void SpaceWithLinearArea::InvokeAllocationObservers( ...@@ -423,8 +423,8 @@ void SpaceWithLinearArea::InvokeAllocationObservers(
#endif #endif
// Run AllocationObserver::Step through the AllocationCounter. // Run AllocationObserver::Step through the AllocationCounter.
allocation_counter_.InvokeAllocationObservers(soon_object, size_in_bytes, allocation_counter_->InvokeAllocationObservers(soon_object, size_in_bytes,
allocation_size); allocation_size);
// Ensure that start/top/limit didn't change. // Ensure that start/top/limit didn't change.
DCHECK_EQ(saved_allocation_info.start(), allocation_info_->start()); DCHECK_EQ(saved_allocation_info.start(), allocation_info_->start());
...@@ -432,9 +432,9 @@ void SpaceWithLinearArea::InvokeAllocationObservers( ...@@ -432,9 +432,9 @@ void SpaceWithLinearArea::InvokeAllocationObservers(
DCHECK_EQ(saved_allocation_info.limit(), allocation_info_->limit()); DCHECK_EQ(saved_allocation_info.limit(), allocation_info_->limit());
} }
DCHECK_IMPLIES(allocation_counter_.IsActive(), DCHECK_IMPLIES(allocation_counter_->IsActive(),
(allocation_info_->limit() - allocation_info_->start()) < (allocation_info_->limit() - allocation_info_->start()) <
allocation_counter_.NextBytes()); allocation_counter_->NextBytes());
} }
#if DEBUG #if DEBUG
......
...@@ -112,9 +112,12 @@ class SemiSpace; ...@@ -112,9 +112,12 @@ class SemiSpace;
// sealed after startup (i.e. not ReadOnlySpace). // sealed after startup (i.e. not ReadOnlySpace).
class V8_EXPORT_PRIVATE Space : public BaseSpace { class V8_EXPORT_PRIVATE Space : public BaseSpace {
public: public:
Space(Heap* heap, AllocationSpace id, FreeList* free_list) Space(Heap* heap, AllocationSpace id, FreeList* free_list,
AllocationCounter* allocation_counter)
: BaseSpace(heap, id), : BaseSpace(heap, id),
free_list_(std::unique_ptr<FreeList>(free_list)) { free_list_(std::unique_ptr<FreeList>(free_list)),
allocation_counter_(allocation_counter) {
DCHECK_NOT_NULL(allocation_counter_);
external_backing_store_bytes_ = external_backing_store_bytes_ =
new std::atomic<size_t>[ExternalBackingStoreType::kNumTypes]; new std::atomic<size_t>[ExternalBackingStoreType::kNumTypes];
external_backing_store_bytes_[ExternalBackingStoreType::kArrayBuffer] = 0; external_backing_store_bytes_[ExternalBackingStoreType::kArrayBuffer] = 0;
...@@ -141,8 +144,6 @@ class V8_EXPORT_PRIVATE Space : public BaseSpace { ...@@ -141,8 +144,6 @@ class V8_EXPORT_PRIVATE Space : public BaseSpace {
virtual void ResumeAllocationObservers(); virtual void ResumeAllocationObservers();
virtual void StartNextInlineAllocationStep() {}
// Returns size of objects. Can differ from the allocated size // Returns size of objects. Can differ from the allocated size
// (e.g. see OldLargeObjectSpace). // (e.g. see OldLargeObjectSpace).
virtual size_t SizeOfObjects() const { return Size(); } virtual size_t SizeOfObjects() const { return Size(); }
...@@ -195,8 +196,6 @@ class V8_EXPORT_PRIVATE Space : public BaseSpace { ...@@ -195,8 +196,6 @@ class V8_EXPORT_PRIVATE Space : public BaseSpace {
#endif #endif
protected: protected:
AllocationCounter allocation_counter_;
// The List manages the pages that belong to the given space. // The List manages the pages that belong to the given space.
heap::List<MemoryChunk> memory_chunk_list_; heap::List<MemoryChunk> memory_chunk_list_;
...@@ -204,6 +203,8 @@ class V8_EXPORT_PRIVATE Space : public BaseSpace { ...@@ -204,6 +203,8 @@ class V8_EXPORT_PRIVATE Space : public BaseSpace {
std::atomic<size_t>* external_backing_store_bytes_; std::atomic<size_t>* external_backing_store_bytes_;
std::unique_ptr<FreeList> free_list_; std::unique_ptr<FreeList> free_list_;
AllocationCounter* const allocation_counter_;
}; };
static_assert(sizeof(std::atomic<intptr_t>) == kSystemPointerSize); static_assert(sizeof(std::atomic<intptr_t>) == kSystemPointerSize);
...@@ -510,9 +511,10 @@ class LinearAreaOriginalData { ...@@ -510,9 +511,10 @@ class LinearAreaOriginalData {
class SpaceWithLinearArea : public Space { class SpaceWithLinearArea : public Space {
public: public:
SpaceWithLinearArea(Heap* heap, AllocationSpace id, FreeList* free_list, SpaceWithLinearArea(Heap* heap, AllocationSpace id, FreeList* free_list,
AllocationCounter* allocation_counter,
LinearAllocationArea* allocation_info, LinearAllocationArea* allocation_info,
LinearAreaOriginalData& linear_area_original_data) LinearAreaOriginalData& linear_area_original_data)
: Space(heap, id, free_list), : Space(heap, id, free_list, allocation_counter),
allocation_info_(allocation_info), allocation_info_(allocation_info),
linear_area_original_data_(linear_area_original_data) {} linear_area_original_data_(linear_area_original_data) {}
......
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