2018-06-07 12:00:16 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
|
|
|
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
|
|
|
#include "src/objects/objects.h"
|
2018-06-07 12:00:16 +00:00
|
|
|
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/handles-inl.h"
|
|
|
|
#include "src/handles/handles.h"
|
2018-06-07 12:00:16 +00:00
|
|
|
|
|
|
|
#include "src/heap/heap-controller.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2019-05-23 15:40:41 +00:00
|
|
|
using MemoryControllerTest = TestWithIsolate;
|
2018-06-08 15:57:43 +00:00
|
|
|
|
2018-06-07 12:00:16 +00:00
|
|
|
double Round(double x) {
|
|
|
|
// Round to three digits.
|
|
|
|
return floor(x * 1000 + 0.5) / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckEqualRounded(double expected, double actual) {
|
|
|
|
expected = Round(expected);
|
|
|
|
actual = Round(actual);
|
|
|
|
EXPECT_DOUBLE_EQ(expected, actual);
|
|
|
|
}
|
|
|
|
|
2019-05-23 15:40:41 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using V8Controller = MemoryController<V8HeapTrait>;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_F(MemoryControllerTest, HeapGrowingFactor) {
|
|
|
|
CheckEqualRounded(V8HeapTrait::kMaxGrowingFactor,
|
|
|
|
V8Controller::DynamicGrowingFactor(34, 1, 4.0));
|
|
|
|
CheckEqualRounded(3.553, V8Controller::DynamicGrowingFactor(45, 1, 4.0));
|
|
|
|
CheckEqualRounded(2.830, V8Controller::DynamicGrowingFactor(50, 1, 4.0));
|
|
|
|
CheckEqualRounded(1.478, V8Controller::DynamicGrowingFactor(100, 1, 4.0));
|
|
|
|
CheckEqualRounded(1.193, V8Controller::DynamicGrowingFactor(200, 1, 4.0));
|
|
|
|
CheckEqualRounded(1.121, V8Controller::DynamicGrowingFactor(300, 1, 4.0));
|
|
|
|
CheckEqualRounded(V8Controller::DynamicGrowingFactor(300, 1, 4.0),
|
|
|
|
V8Controller::DynamicGrowingFactor(600, 2, 4.0));
|
|
|
|
CheckEqualRounded(V8HeapTrait::kMinGrowingFactor,
|
|
|
|
V8Controller::DynamicGrowingFactor(400, 1, 4.0));
|
2018-06-07 12:00:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 15:40:41 +00:00
|
|
|
TEST_F(MemoryControllerTest, MaxHeapGrowingFactor) {
|
2019-05-28 15:51:07 +00:00
|
|
|
CheckEqualRounded(1.3, V8Controller::MaxGrowingFactor(V8HeapTrait::kMinSize));
|
|
|
|
CheckEqualRounded(1.600,
|
|
|
|
V8Controller::MaxGrowingFactor(V8HeapTrait::kMaxSize / 2));
|
|
|
|
CheckEqualRounded(2.0,
|
|
|
|
V8Controller::MaxGrowingFactor(
|
|
|
|
(V8HeapTrait::kMaxSize - Heap::kPointerMultiplier)));
|
2019-05-23 15:40:41 +00:00
|
|
|
CheckEqualRounded(4.0, V8Controller::MaxGrowingFactor(
|
2019-05-28 15:51:07 +00:00
|
|
|
static_cast<size_t>(V8HeapTrait::kMaxSize)));
|
2018-06-07 12:00:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 15:40:41 +00:00
|
|
|
TEST_F(MemoryControllerTest, OldGenerationAllocationLimit) {
|
2018-06-08 15:57:43 +00:00
|
|
|
Heap* heap = i_isolate()->heap();
|
|
|
|
size_t old_gen_size = 128 * MB;
|
|
|
|
size_t max_old_generation_size = 512 * MB;
|
|
|
|
double gc_speed = 100;
|
|
|
|
double mutator_speed = 1;
|
|
|
|
size_t new_space_capacity = 16 * MB;
|
|
|
|
|
2019-05-23 15:40:41 +00:00
|
|
|
double factor = V8Controller::GrowingFactor(heap, max_old_generation_size,
|
|
|
|
gc_speed, mutator_speed);
|
2018-06-08 15:57:43 +00:00
|
|
|
|
2019-05-17 19:35:42 +00:00
|
|
|
EXPECT_EQ(static_cast<size_t>(old_gen_size * factor + new_space_capacity),
|
2019-05-23 15:40:41 +00:00
|
|
|
V8Controller::CalculateAllocationLimit(
|
2019-06-06 10:24:27 +00:00
|
|
|
heap, old_gen_size, 0u, max_old_generation_size,
|
|
|
|
new_space_capacity, factor, Heap::HeapGrowingMode::kDefault));
|
2018-06-21 07:26:32 +00:00
|
|
|
|
2020-11-24 16:41:59 +00:00
|
|
|
factor = std::min({factor, V8HeapTrait::kConservativeGrowingFactor});
|
2019-05-17 19:35:42 +00:00
|
|
|
EXPECT_EQ(static_cast<size_t>(old_gen_size * factor + new_space_capacity),
|
2019-05-23 15:40:41 +00:00
|
|
|
V8Controller::CalculateAllocationLimit(
|
2019-06-06 10:24:27 +00:00
|
|
|
heap, old_gen_size, 0u, max_old_generation_size,
|
|
|
|
new_space_capacity, factor, Heap::HeapGrowingMode::kSlow));
|
2018-06-08 15:57:43 +00:00
|
|
|
|
2020-11-24 16:41:59 +00:00
|
|
|
factor = std::min({factor, V8HeapTrait::kConservativeGrowingFactor});
|
2019-06-06 10:24:27 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
static_cast<size_t>(old_gen_size * factor + new_space_capacity),
|
|
|
|
V8Controller::CalculateAllocationLimit(
|
|
|
|
heap, old_gen_size, 0u, max_old_generation_size, new_space_capacity,
|
|
|
|
factor, Heap::HeapGrowingMode::kConservative));
|
2018-09-10 19:05:40 +00:00
|
|
|
|
2019-05-23 15:40:41 +00:00
|
|
|
factor = V8HeapTrait::kMinGrowingFactor;
|
2019-05-17 19:35:42 +00:00
|
|
|
EXPECT_EQ(static_cast<size_t>(old_gen_size * factor + new_space_capacity),
|
2019-05-23 15:40:41 +00:00
|
|
|
V8Controller::CalculateAllocationLimit(
|
2019-06-06 10:24:27 +00:00
|
|
|
heap, old_gen_size, 0u, max_old_generation_size,
|
|
|
|
new_space_capacity, factor, Heap::HeapGrowingMode::kMinimal));
|
|
|
|
|
|
|
|
factor = V8HeapTrait::kMinGrowingFactor;
|
|
|
|
size_t min_old_generation_size =
|
|
|
|
2 * static_cast<size_t>(old_gen_size * factor + new_space_capacity);
|
|
|
|
EXPECT_EQ(
|
|
|
|
min_old_generation_size,
|
|
|
|
V8Controller::CalculateAllocationLimit(
|
|
|
|
heap, old_gen_size, min_old_generation_size, max_old_generation_size,
|
|
|
|
new_space_capacity, factor, Heap::HeapGrowingMode::kMinimal));
|
2018-06-08 15:57:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 12:00:16 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|