Line data Source code
1 : // Copyright 2014 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include <cmath>
6 : #include <iostream>
7 : #include <limits>
8 :
9 : #include "src/objects-inl.h"
10 : #include "src/objects.h"
11 :
12 : #include "src/handles-inl.h"
13 : #include "src/handles.h"
14 :
15 : #include "src/heap/heap-controller.h"
16 : #include "test/unittests/test-utils.h"
17 : #include "testing/gtest/include/gtest/gtest.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 :
22 : using HeapControllerTest = TestWithIsolate;
23 :
24 0 : double Round(double x) {
25 : // Round to three digits.
26 12 : return floor(x * 1000 + 0.5) / 1000;
27 : }
28 :
29 12 : void CheckEqualRounded(double expected, double actual) {
30 : expected = Round(expected);
31 : actual = Round(actual);
32 12 : EXPECT_DOUBLE_EQ(expected, actual);
33 12 : }
34 :
35 15444 : TEST_F(HeapControllerTest, HeapGrowingFactor) {
36 : HeapController heap_controller(i_isolate()->heap());
37 : double min_factor = heap_controller.min_growing_factor_;
38 : double max_factor = heap_controller.max_growing_factor_;
39 :
40 1 : CheckEqualRounded(max_factor, heap_controller.GrowingFactor(34, 1, 4.0));
41 1 : CheckEqualRounded(3.553, heap_controller.GrowingFactor(45, 1, 4.0));
42 1 : CheckEqualRounded(2.830, heap_controller.GrowingFactor(50, 1, 4.0));
43 1 : CheckEqualRounded(1.478, heap_controller.GrowingFactor(100, 1, 4.0));
44 1 : CheckEqualRounded(1.193, heap_controller.GrowingFactor(200, 1, 4.0));
45 1 : CheckEqualRounded(1.121, heap_controller.GrowingFactor(300, 1, 4.0));
46 1 : CheckEqualRounded(heap_controller.GrowingFactor(300, 1, 4.0),
47 1 : heap_controller.GrowingFactor(600, 2, 4.0));
48 1 : CheckEqualRounded(min_factor, heap_controller.GrowingFactor(400, 1, 4.0));
49 1 : }
50 :
51 15444 : TEST_F(HeapControllerTest, MaxHeapGrowingFactor) {
52 : HeapController heap_controller(i_isolate()->heap());
53 1 : CheckEqualRounded(
54 1 : 1.3, heap_controller.MaxGrowingFactor(HeapController::kMinSize * MB));
55 1 : CheckEqualRounded(1.600, heap_controller.MaxGrowingFactor(
56 1 : HeapController::kMaxSize / 2 * MB));
57 1 : CheckEqualRounded(
58 : 1.999, heap_controller.MaxGrowingFactor(
59 1 : (HeapController::kMaxSize - Heap::kPointerMultiplier) * MB));
60 1 : CheckEqualRounded(4.0,
61 : heap_controller.MaxGrowingFactor(
62 1 : static_cast<size_t>(HeapController::kMaxSize) * MB));
63 1 : }
64 :
65 15444 : TEST_F(HeapControllerTest, OldGenerationAllocationLimit) {
66 : Heap* heap = i_isolate()->heap();
67 : HeapController heap_controller(heap);
68 : size_t old_gen_size = 128 * MB;
69 : size_t max_old_generation_size = 512 * MB;
70 : double gc_speed = 100;
71 : double mutator_speed = 1;
72 : size_t new_space_capacity = 16 * MB;
73 :
74 1 : double max_factor = heap_controller.MaxGrowingFactor(max_old_generation_size);
75 : double factor =
76 1 : heap_controller.GrowingFactor(gc_speed, mutator_speed, max_factor);
77 :
78 2 : EXPECT_EQ(
79 : static_cast<size_t>(old_gen_size * factor + new_space_capacity),
80 : heap->heap_controller()->CalculateAllocationLimit(
81 : old_gen_size, max_old_generation_size, max_factor, gc_speed,
82 0 : mutator_speed, new_space_capacity, Heap::HeapGrowingMode::kDefault));
83 :
84 1 : factor = Min(factor, heap_controller.conservative_growing_factor_);
85 2 : EXPECT_EQ(
86 : static_cast<size_t>(old_gen_size * factor + new_space_capacity),
87 : heap->heap_controller()->CalculateAllocationLimit(
88 : old_gen_size, max_old_generation_size, max_factor, gc_speed,
89 0 : mutator_speed, new_space_capacity, Heap::HeapGrowingMode::kSlow));
90 :
91 1 : factor = Min(factor, heap_controller.conservative_growing_factor_);
92 2 : EXPECT_EQ(static_cast<size_t>(old_gen_size * factor + new_space_capacity),
93 : heap->heap_controller()->CalculateAllocationLimit(
94 : old_gen_size, max_old_generation_size, max_factor, gc_speed,
95 : mutator_speed, new_space_capacity,
96 0 : Heap::HeapGrowingMode::kConservative));
97 :
98 1 : factor = heap_controller.min_growing_factor_;
99 2 : EXPECT_EQ(
100 : static_cast<size_t>(old_gen_size * factor + new_space_capacity),
101 : heap->heap_controller()->CalculateAllocationLimit(
102 : old_gen_size, max_old_generation_size, max_factor, gc_speed,
103 0 : mutator_speed, new_space_capacity, Heap::HeapGrowingMode::kMinimal));
104 1 : }
105 :
106 15444 : TEST_F(HeapControllerTest, MaxOldGenerationSize) {
107 : HeapController heap_controller(i_isolate()->heap());
108 : uint64_t configurations[][2] = {
109 : {0, HeapController::kMinSize},
110 : {512, HeapController::kMinSize},
111 : {1 * GB, 256 * Heap::kPointerMultiplier},
112 : {2 * static_cast<uint64_t>(GB), 512 * Heap::kPointerMultiplier},
113 : {4 * static_cast<uint64_t>(GB), HeapController::kMaxSize},
114 1 : {8 * static_cast<uint64_t>(GB), HeapController::kMaxSize}};
115 :
116 13 : for (auto configuration : configurations) {
117 12 : ASSERT_EQ(configuration[1],
118 : static_cast<uint64_t>(
119 : Heap::ComputeMaxOldGenerationSize(configuration[0])));
120 : }
121 : }
122 :
123 : } // namespace internal
124 9264 : } // namespace v8
|