Commit 502f9216 authored by Rodrigo Bruno's avatar Rodrigo Bruno Committed by Commit Bot

[heap] Refactor of HeapController to avoid Heap dependencies.

Bug: chromium:845409
Change-Id: If1fbb03258fdfae528069ba37bbef0395ddb230d
Reviewed-on: https://chromium-review.googlesource.com/1090920Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Rodrigo Bruno <rfbpb@google.com>
Cr-Commit-Position: refs/heads/master@{#53608}
parent d9daf859
......@@ -3,8 +3,6 @@
// found in the LICENSE file.
#include "src/heap/heap-controller.h"
#include "src/heap/heap.h"
#include "src/heap/memory-reducer.h"
#include "src/isolate-inl.h"
namespace v8 {
......@@ -102,12 +100,12 @@ double HeapController::MaxHeapGrowingFactor(size_t max_old_generation_size) {
return factor;
}
size_t HeapController::ComputeOldGenerationAllocationLimit(
size_t HeapController::CalculateOldGenerationAllocationLimit(
size_t old_gen_size, size_t max_old_generation_size, double gc_speed,
double mutator_speed) {
double mutator_speed, size_t new_space_capacity,
Heap::HeapGrowingMode growing_mode) {
double max_factor = MaxHeapGrowingFactor(max_old_generation_size);
double factor = HeapGrowingFactor(gc_speed, mutator_speed, max_factor);
size_t limit;
if (FLAG_trace_gc_verbose) {
heap_->isolate()->PrintWithTimestamp(
......@@ -117,12 +115,12 @@ size_t HeapController::ComputeOldGenerationAllocationLimit(
mutator_speed);
}
if (heap_->memory_reducer()->ShouldGrowHeapSlowly() ||
heap_->ShouldOptimizeForMemoryUsage()) {
if (growing_mode == Heap::HeapGrowingMode::kConservative) {
factor = Min(factor, kConservativeHeapGrowingFactor);
}
if (FLAG_stress_compaction || heap_->ShouldReduceMemory()) {
if (FLAG_stress_compaction ||
growing_mode == Heap::HeapGrowingMode::kMinimal) {
factor = kMinHeapGrowingFactor;
}
......@@ -130,57 +128,33 @@ size_t HeapController::ComputeOldGenerationAllocationLimit(
factor = 1.0 + FLAG_heap_growing_percent / 100.0;
}
limit = CalculateOldGenerationAllocationLimit(factor, old_gen_size,
max_old_generation_size);
if (FLAG_trace_gc_verbose) {
heap_->isolate()->PrintWithTimestamp(
"Grow: old size: %" PRIuS " KB, new limit: %" PRIuS " KB (%.1f)\n",
old_gen_size / KB, limit / KB, factor);
}
return limit;
}
size_t HeapController::DampenOldGenerationAllocationLimit(
size_t old_gen_size, size_t max_old_generation_size, double gc_speed,
double mutator_speed) {
double max_factor = MaxHeapGrowingFactor(max_old_generation_size);
double factor = HeapGrowingFactor(gc_speed, mutator_speed, max_factor);
size_t limit = CalculateOldGenerationAllocationLimit(factor, old_gen_size,
max_old_generation_size);
if (limit < heap_->old_generation_allocation_limit()) {
if (FLAG_trace_gc_verbose) {
heap_->isolate()->PrintWithTimestamp(
"Dampen: old size: %" PRIuS " KB, old limit: %" PRIuS
" KB, "
"new limit: %" PRIuS " KB (%.1f)\n",
old_gen_size / KB, heap_->old_generation_allocation_limit() / KB,
limit / KB, factor);
}
return limit;
}
return heap_->old_generation_allocation_limit();
}
size_t HeapController::CalculateOldGenerationAllocationLimit(
double factor, size_t old_gen_size, size_t max_old_generation_size) {
CHECK_LT(1.0, factor);
CHECK_LT(0, old_gen_size);
uint64_t limit = static_cast<uint64_t>(old_gen_size * factor);
limit = Max(limit, static_cast<uint64_t>(old_gen_size) +
MinimumAllocationLimitGrowingStep());
limit += heap_->new_space()->Capacity();
MinimumAllocationLimitGrowingStep(growing_mode));
limit += new_space_capacity;
uint64_t halfway_to_the_max =
(static_cast<uint64_t>(old_gen_size) + max_old_generation_size) / 2;
return static_cast<size_t>(Min(limit, halfway_to_the_max));
size_t result = static_cast<size_t>(Min(limit, halfway_to_the_max));
if (FLAG_trace_gc_verbose) {
heap_->isolate()->PrintWithTimestamp(
"Heap Controller Limit: old size: %" PRIuS " KB, new limit: %" PRIuS
" KB (%.1f)\n",
old_gen_size / KB, result / KB, factor);
}
return result;
}
size_t HeapController::MinimumAllocationLimitGrowingStep() {
size_t HeapController::MinimumAllocationLimitGrowingStep(
Heap::HeapGrowingMode growing_mode) {
const size_t kRegularAllocationLimitGrowingStep = 8;
const size_t kLowMemoryAllocationLimitGrowingStep = 2;
size_t limit = (Page::kPageSize > MB ? Page::kPageSize : MB);
return limit * (heap_->ShouldOptimizeForMemoryUsage()
return limit * (growing_mode == Heap::HeapGrowingMode::kConservative ||
growing_mode == Heap::HeapGrowingMode::kMinimal
? kLowMemoryAllocationLimitGrowingStep
: kRegularAllocationLimitGrowingStep);
}
......
......@@ -13,26 +13,17 @@
namespace v8 {
namespace internal {
class Heap;
class HeapController {
public:
explicit HeapController(Heap* heap) : heap_(heap) {}
// Computes the allocation limit to trigger the next full garbage collection.
size_t ComputeOldGenerationAllocationLimit(size_t old_gen_size,
size_t max_old_generation_size,
double gc_speed,
double mutator_speed);
// Decrease the allocation limit if the new limit based on the given
// parameters is lower than the current limit.
size_t DampenOldGenerationAllocationLimit(size_t old_gen_size,
size_t max_old_generation_size,
double gc_speed,
double mutator_speed);
size_t CalculateOldGenerationAllocationLimit(
size_t old_gen_size, size_t max_old_generation_size, double gc_speed,
double mutator_speed, size_t new_space_capacity,
Heap::HeapGrowingMode growing_mode);
size_t MinimumAllocationLimitGrowingStep();
size_t MinimumAllocationLimitGrowingStep(Heap::HeapGrowingMode growing_mode);
// The old space size has to be a multiple of Page::kPageSize.
// Sizes are in MB.
......@@ -52,18 +43,12 @@ class HeapController {
double mutator_speed,
double max_factor);
// Calculates the allocation limit based on a given growing factor and a
// given old generation size.
size_t CalculateOldGenerationAllocationLimit(double factor,
size_t old_gen_size,
size_t max_old_generation_size);
Heap* heap_;
static const double kMaxHeapGrowingFactorMemoryConstrained;
static const double kMaxHeapGrowingFactorIdle;
static const double kConservativeHeapGrowingFactor;
static const double kTargetMutatorUtilization;
Heap* heap_;
};
} // namespace internal
......
......@@ -1804,16 +1804,22 @@ bool Heap::PerformGarbageCollection(
// Register the amount of external allocated memory.
external_memory_at_last_mark_compact_ = external_memory_;
external_memory_limit_ = external_memory_ + kExternalAllocationSoftLimit;
old_generation_allocation_limit_ =
heap_controller()->ComputeOldGenerationAllocationLimit(
old_gen_size, max_old_generation_size_, gc_speed, mutator_speed);
size_t new_limit = heap_controller()->CalculateOldGenerationAllocationLimit(
old_gen_size, max_old_generation_size_, gc_speed, mutator_speed,
new_space()->Capacity(), CurrentHeapGrowingMode());
old_generation_allocation_limit_ = new_limit;
CheckIneffectiveMarkCompact(
old_gen_size, tracer()->AverageMarkCompactMutatorUtilization());
} else if (HasLowYoungGenerationAllocationRate() &&
old_generation_size_configured_) {
old_generation_allocation_limit_ =
heap_controller()->DampenOldGenerationAllocationLimit(
old_gen_size, max_old_generation_size_, gc_speed, mutator_speed);
size_t new_limit = heap_controller()->CalculateOldGenerationAllocationLimit(
old_gen_size, max_old_generation_size_, gc_speed, mutator_speed,
new_space()->Capacity(), CurrentHeapGrowingMode());
if (new_limit < old_generation_allocation_limit_) {
old_generation_allocation_limit_ = new_limit;
}
}
{
......@@ -2579,7 +2585,8 @@ void Heap::UnregisterArrayBuffer(JSArrayBuffer* buffer) {
void Heap::ConfigureInitialOldGenerationSize() {
if (!old_generation_size_configured_ && tracer()->SurvivalEventsRecorded()) {
old_generation_allocation_limit_ =
Max(heap_controller()->MinimumAllocationLimitGrowingStep(),
Max(heap_controller()->MinimumAllocationLimitGrowingStep(
CurrentHeapGrowingMode()),
static_cast<size_t>(
static_cast<double>(old_generation_allocation_limit_) *
(tracer()->AverageSurvivalRatio() / 100)));
......@@ -4305,6 +4312,17 @@ bool Heap::ShouldExpandOldGenerationOnSlowAllocation() {
return true;
}
Heap::HeapGrowingMode Heap::CurrentHeapGrowingMode() {
if (ShouldReduceMemory()) return HeapGrowingMode::kMinimal;
if (memory_reducer()->ShouldGrowHeapSlowly() ||
ShouldOptimizeForMemoryUsage()) {
return HeapGrowingMode::kConservative;
}
return HeapGrowingMode::kDefault;
}
// This function returns either kNoLimit, kSoftLimit, or kHardLimit.
// The kNoLimit means that either incremental marking is disabled or it is too
// early to start incremental marking.
......
......@@ -2098,6 +2098,9 @@ class Heap {
bool ShouldExpandOldGenerationOnSlowAllocation();
enum class HeapGrowingMode { kDefault, kConservative, kMinimal };
HeapGrowingMode CurrentHeapGrowingMode();
enum class IncrementalMarkingLimit { kNoLimit, kSoftLimit, kHardLimit };
IncrementalMarkingLimit IncrementalMarkingLimitReached();
......
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