Commit c687c999 authored by Dominik Inführ's avatar Dominik Inführ Committed by Commit Bot

[heap] Introduce AllocationCounter class to manage allocation observers

AllocationCounter just stores all allocation observers for now.

Bug: v8:10315
Change-Id: I4ff4208877dd9454c9eef5e5d2e2349b7f00065f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2306793
Commit-Queue: Dominik Inführ <dinfuehr@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68942}
parent bad01351
......@@ -10,6 +10,17 @@
namespace v8 {
namespace internal {
void AllocationCounter::AddAllocationObserver(AllocationObserver* observer) {
allocation_observers_.push_back(observer);
}
void AllocationCounter::RemoveAllocationObserver(AllocationObserver* observer) {
auto it = std::find(allocation_observers_.begin(),
allocation_observers_.end(), observer);
DCHECK(allocation_observers_.end() != it);
allocation_observers_.erase(it);
}
void AllocationObserver::AllocationStep(int bytes_allocated,
Address soon_object, size_t size) {
DCHECK_GE(bytes_allocated, 0);
......
......@@ -5,11 +5,30 @@
#ifndef V8_HEAP_ALLOCATION_OBSERVER_H_
#define V8_HEAP_ALLOCATION_OBSERVER_H_
#include <vector>
#include "src/common/globals.h"
namespace v8 {
namespace internal {
class AllocationObserver;
class AllocationCounter {
public:
auto begin() { return allocation_observers_.begin(); }
auto end() { return allocation_observers_.end(); }
void AddAllocationObserver(AllocationObserver* observer);
void RemoveAllocationObserver(AllocationObserver* observer);
bool HasAllocationObservers() { return !allocation_observers_.empty(); }
size_t NumberAllocationObservers() { return allocation_observers_.size(); }
private:
std::vector<AllocationObserver*> allocation_observers_;
};
// -----------------------------------------------------------------------------
// Allows observation of allocations.
class AllocationObserver {
......
......@@ -248,15 +248,12 @@ void Page::DestroyBlackAreaBackground(Address start, Address end) {
// PagedSpace implementation
void Space::AddAllocationObserver(AllocationObserver* observer) {
allocation_observers_.push_back(observer);
allocation_counter_.AddAllocationObserver(observer);
StartNextInlineAllocationStep();
}
void Space::RemoveAllocationObserver(AllocationObserver* observer) {
auto it = std::find(allocation_observers_.begin(),
allocation_observers_.end(), observer);
DCHECK(allocation_observers_.end() != it);
allocation_observers_.erase(it);
allocation_counter_.RemoveAllocationObserver(observer);
StartNextInlineAllocationStep();
}
......@@ -275,7 +272,7 @@ void Space::AllocationStep(int bytes_since_last, Address soon_object,
DCHECK(!heap()->allocation_step_in_progress());
heap()->set_allocation_step_in_progress(true);
heap()->CreateFillerObjectAt(soon_object, size, ClearRecordedSlots::kNo);
for (AllocationObserver* observer : allocation_observers_) {
for (AllocationObserver* observer : allocation_counter_) {
observer->AllocationStep(bytes_since_last, soon_object, size);
}
heap()->set_allocation_step_in_progress(false);
......@@ -288,7 +285,7 @@ void Space::AllocationStepAfterMerge(Address first_object_in_chunk, int size) {
DCHECK(!heap()->allocation_step_in_progress());
heap()->set_allocation_step_in_progress(true);
for (AllocationObserver* observer : allocation_observers_) {
for (AllocationObserver* observer : allocation_counter_) {
observer->AllocationStep(size, first_object_in_chunk, size);
}
heap()->set_allocation_step_in_progress(false);
......@@ -296,11 +293,11 @@ void Space::AllocationStepAfterMerge(Address first_object_in_chunk, int size) {
intptr_t Space::GetNextInlineAllocationStepSize() {
intptr_t next_step = 0;
for (AllocationObserver* observer : allocation_observers_) {
for (AllocationObserver* observer : allocation_counter_) {
next_step = next_step ? Min(next_step, observer->bytes_to_next_step())
: observer->bytes_to_next_step();
}
DCHECK(allocation_observers_.size() == 0 || next_step > 0);
DCHECK(!allocation_counter_.HasAllocationObservers() || next_step > 0);
return next_step;
}
......@@ -405,7 +402,8 @@ void SpaceWithLinearArea::AddAllocationObserver(AllocationObserver* observer) {
void SpaceWithLinearArea::RemoveAllocationObserver(
AllocationObserver* observer) {
Address top_for_next_step =
allocation_observers_.size() == 1 ? kNullAddress : top();
allocation_counter_.NumberAllocationObservers() == 1 ? kNullAddress
: top();
InlineAllocationStep(top(), top_for_next_step, kNullAddress, 0);
Space::RemoveAllocationObserver(observer);
DCHECK_IMPLIES(top_on_previous_step_, AllocationObserversActive());
......
......@@ -12,6 +12,7 @@
#include "src/base/iterator.h"
#include "src/base/macros.h"
#include "src/common/globals.h"
#include "src/heap/allocation-observer.h"
#include "src/heap/base-space.h"
#include "src/heap/basic-memory-chunk.h"
#include "src/heap/free-list.h"
......@@ -193,10 +194,11 @@ class V8_EXPORT_PRIVATE Space : public BaseSpace {
protected:
intptr_t GetNextInlineAllocationStepSize();
bool AllocationObserversActive() {
return !allocation_observers_paused_ && !allocation_observers_.empty();
return !allocation_observers_paused_ &&
allocation_counter_.HasAllocationObservers();
}
std::vector<AllocationObserver*> allocation_observers_;
AllocationCounter allocation_counter_;
// The List manages the pages that belong to the given space.
heap::List<MemoryChunk> memory_chunk_list_;
......
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