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 @@ ...@@ -3,8 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "src/heap/heap-controller.h" #include "src/heap/heap-controller.h"
#include "src/heap/heap.h"
#include "src/heap/memory-reducer.h"
#include "src/isolate-inl.h" #include "src/isolate-inl.h"
namespace v8 { namespace v8 {
...@@ -102,12 +100,12 @@ double HeapController::MaxHeapGrowingFactor(size_t max_old_generation_size) { ...@@ -102,12 +100,12 @@ double HeapController::MaxHeapGrowingFactor(size_t max_old_generation_size) {
return factor; return factor;
} }
size_t HeapController::ComputeOldGenerationAllocationLimit( size_t HeapController::CalculateOldGenerationAllocationLimit(
size_t old_gen_size, size_t max_old_generation_size, double gc_speed, 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 max_factor = MaxHeapGrowingFactor(max_old_generation_size);
double factor = HeapGrowingFactor(gc_speed, mutator_speed, max_factor); double factor = HeapGrowingFactor(gc_speed, mutator_speed, max_factor);
size_t limit;
if (FLAG_trace_gc_verbose) { if (FLAG_trace_gc_verbose) {
heap_->isolate()->PrintWithTimestamp( heap_->isolate()->PrintWithTimestamp(
...@@ -117,12 +115,12 @@ size_t HeapController::ComputeOldGenerationAllocationLimit( ...@@ -117,12 +115,12 @@ size_t HeapController::ComputeOldGenerationAllocationLimit(
mutator_speed); mutator_speed);
} }
if (heap_->memory_reducer()->ShouldGrowHeapSlowly() || if (growing_mode == Heap::HeapGrowingMode::kConservative) {
heap_->ShouldOptimizeForMemoryUsage()) {
factor = Min(factor, kConservativeHeapGrowingFactor); factor = Min(factor, kConservativeHeapGrowingFactor);
} }
if (FLAG_stress_compaction || heap_->ShouldReduceMemory()) { if (FLAG_stress_compaction ||
growing_mode == Heap::HeapGrowingMode::kMinimal) {
factor = kMinHeapGrowingFactor; factor = kMinHeapGrowingFactor;
} }
...@@ -130,57 +128,33 @@ size_t HeapController::ComputeOldGenerationAllocationLimit( ...@@ -130,57 +128,33 @@ size_t HeapController::ComputeOldGenerationAllocationLimit(
factor = 1.0 + FLAG_heap_growing_percent / 100.0; 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(1.0, factor);
CHECK_LT(0, old_gen_size); CHECK_LT(0, old_gen_size);
uint64_t limit = static_cast<uint64_t>(old_gen_size * factor); uint64_t limit = static_cast<uint64_t>(old_gen_size * factor);
limit = Max(limit, static_cast<uint64_t>(old_gen_size) + limit = Max(limit, static_cast<uint64_t>(old_gen_size) +
MinimumAllocationLimitGrowingStep()); MinimumAllocationLimitGrowingStep(growing_mode));
limit += heap_->new_space()->Capacity(); limit += new_space_capacity;
uint64_t halfway_to_the_max = uint64_t halfway_to_the_max =
(static_cast<uint64_t>(old_gen_size) + max_old_generation_size) / 2; (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 kRegularAllocationLimitGrowingStep = 8;
const size_t kLowMemoryAllocationLimitGrowingStep = 2; const size_t kLowMemoryAllocationLimitGrowingStep = 2;
size_t limit = (Page::kPageSize > MB ? Page::kPageSize : MB); 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 ? kLowMemoryAllocationLimitGrowingStep
: kRegularAllocationLimitGrowingStep); : kRegularAllocationLimitGrowingStep);
} }
......
...@@ -13,26 +13,17 @@ ...@@ -13,26 +13,17 @@
namespace v8 { namespace v8 {
namespace internal { namespace internal {
class Heap;
class HeapController { class HeapController {
public: public:
explicit HeapController(Heap* heap) : heap_(heap) {} explicit HeapController(Heap* heap) : heap_(heap) {}
// Computes the allocation limit to trigger the next full garbage collection. // Computes the allocation limit to trigger the next full garbage collection.
size_t ComputeOldGenerationAllocationLimit(size_t old_gen_size, size_t CalculateOldGenerationAllocationLimit(
size_t max_old_generation_size, size_t old_gen_size, size_t max_old_generation_size, double gc_speed,
double gc_speed, double mutator_speed, size_t new_space_capacity,
double mutator_speed); Heap::HeapGrowingMode growing_mode);
// 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 MinimumAllocationLimitGrowingStep(); size_t MinimumAllocationLimitGrowingStep(Heap::HeapGrowingMode growing_mode);
// The old space size has to be a multiple of Page::kPageSize. // The old space size has to be a multiple of Page::kPageSize.
// Sizes are in MB. // Sizes are in MB.
...@@ -52,18 +43,12 @@ class HeapController { ...@@ -52,18 +43,12 @@ class HeapController {
double mutator_speed, double mutator_speed,
double max_factor); 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 kMaxHeapGrowingFactorMemoryConstrained;
static const double kMaxHeapGrowingFactorIdle; static const double kMaxHeapGrowingFactorIdle;
static const double kConservativeHeapGrowingFactor; static const double kConservativeHeapGrowingFactor;
static const double kTargetMutatorUtilization; static const double kTargetMutatorUtilization;
Heap* heap_;
}; };
} // namespace internal } // namespace internal
......
...@@ -1804,16 +1804,22 @@ bool Heap::PerformGarbageCollection( ...@@ -1804,16 +1804,22 @@ bool Heap::PerformGarbageCollection(
// Register the amount of external allocated memory. // Register the amount of external allocated memory.
external_memory_at_last_mark_compact_ = external_memory_; external_memory_at_last_mark_compact_ = external_memory_;
external_memory_limit_ = external_memory_ + kExternalAllocationSoftLimit; external_memory_limit_ = external_memory_ + kExternalAllocationSoftLimit;
old_generation_allocation_limit_ =
heap_controller()->ComputeOldGenerationAllocationLimit( size_t new_limit = heap_controller()->CalculateOldGenerationAllocationLimit(
old_gen_size, max_old_generation_size_, gc_speed, mutator_speed); old_gen_size, max_old_generation_size_, gc_speed, mutator_speed,
new_space()->Capacity(), CurrentHeapGrowingMode());
old_generation_allocation_limit_ = new_limit;
CheckIneffectiveMarkCompact( CheckIneffectiveMarkCompact(
old_gen_size, tracer()->AverageMarkCompactMutatorUtilization()); old_gen_size, tracer()->AverageMarkCompactMutatorUtilization());
} else if (HasLowYoungGenerationAllocationRate() && } else if (HasLowYoungGenerationAllocationRate() &&
old_generation_size_configured_) { old_generation_size_configured_) {
old_generation_allocation_limit_ = size_t new_limit = heap_controller()->CalculateOldGenerationAllocationLimit(
heap_controller()->DampenOldGenerationAllocationLimit( old_gen_size, max_old_generation_size_, gc_speed, mutator_speed,
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) { ...@@ -2579,7 +2585,8 @@ void Heap::UnregisterArrayBuffer(JSArrayBuffer* buffer) {
void Heap::ConfigureInitialOldGenerationSize() { void Heap::ConfigureInitialOldGenerationSize() {
if (!old_generation_size_configured_ && tracer()->SurvivalEventsRecorded()) { if (!old_generation_size_configured_ && tracer()->SurvivalEventsRecorded()) {
old_generation_allocation_limit_ = old_generation_allocation_limit_ =
Max(heap_controller()->MinimumAllocationLimitGrowingStep(), Max(heap_controller()->MinimumAllocationLimitGrowingStep(
CurrentHeapGrowingMode()),
static_cast<size_t>( static_cast<size_t>(
static_cast<double>(old_generation_allocation_limit_) * static_cast<double>(old_generation_allocation_limit_) *
(tracer()->AverageSurvivalRatio() / 100))); (tracer()->AverageSurvivalRatio() / 100)));
...@@ -4305,6 +4312,17 @@ bool Heap::ShouldExpandOldGenerationOnSlowAllocation() { ...@@ -4305,6 +4312,17 @@ bool Heap::ShouldExpandOldGenerationOnSlowAllocation() {
return true; 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. // This function returns either kNoLimit, kSoftLimit, or kHardLimit.
// The kNoLimit means that either incremental marking is disabled or it is too // The kNoLimit means that either incremental marking is disabled or it is too
// early to start incremental marking. // early to start incremental marking.
......
...@@ -2098,6 +2098,9 @@ class Heap { ...@@ -2098,6 +2098,9 @@ class Heap {
bool ShouldExpandOldGenerationOnSlowAllocation(); bool ShouldExpandOldGenerationOnSlowAllocation();
enum class HeapGrowingMode { kDefault, kConservative, kMinimal };
HeapGrowingMode CurrentHeapGrowingMode();
enum class IncrementalMarkingLimit { kNoLimit, kSoftLimit, kHardLimit }; enum class IncrementalMarkingLimit { kNoLimit, kSoftLimit, kHardLimit };
IncrementalMarkingLimit IncrementalMarkingLimitReached(); 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