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 <limits>
6
7#include "src/globals.h"
8#include "src/heap/scavenge-job.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace v8 {
12namespace internal {
13
14const size_t kScavengeSpeedInBytesPerMs = 500 * KB;
15const size_t kNewSpaceCapacity = 8 * MB;
16
17
18TEST(ScavengeJob, AllocationLimitEmptyNewSpace) {
19  EXPECT_FALSE(ScavengeJob::ReachedIdleAllocationLimit(
20      kScavengeSpeedInBytesPerMs, 0, kNewSpaceCapacity));
21}
22
23
24TEST(ScavengeJob, AllocationLimitFullNewSpace) {
25  EXPECT_TRUE(ScavengeJob::ReachedIdleAllocationLimit(
26      kScavengeSpeedInBytesPerMs, kNewSpaceCapacity, kNewSpaceCapacity));
27}
28
29
30TEST(ScavengeJob, AllocationLimitUnknownScavengeSpeed) {
31  size_t expected_size = ScavengeJob::kInitialScavengeSpeedInBytesPerMs *
32                             ScavengeJob::kAverageIdleTimeMs -
33                         ScavengeJob::kBytesAllocatedBeforeNextIdleTask;
34  EXPECT_FALSE(ScavengeJob::ReachedIdleAllocationLimit(0, expected_size - 1,
35                                                       kNewSpaceCapacity));
36  EXPECT_TRUE(ScavengeJob::ReachedIdleAllocationLimit(0, expected_size,
37                                                      kNewSpaceCapacity));
38}
39
40
41TEST(ScavengeJob, AllocationLimitLowScavengeSpeed) {
42  size_t scavenge_speed = 1 * KB;
43  EXPECT_FALSE(ScavengeJob::ReachedIdleAllocationLimit(
44      scavenge_speed, ScavengeJob::kMinAllocationLimit - 1, kNewSpaceCapacity));
45  EXPECT_TRUE(ScavengeJob::ReachedIdleAllocationLimit(
46      scavenge_speed, ScavengeJob::kMinAllocationLimit, kNewSpaceCapacity));
47}
48
49
50TEST(ScavengeJob, AllocationLimitAverageScavengeSpeed) {
51  size_t expected_size =
52      kScavengeSpeedInBytesPerMs * ScavengeJob::kAverageIdleTimeMs -
53      ScavengeJob::kBytesAllocatedBeforeNextIdleTask;
54  EXPECT_FALSE(ScavengeJob::ReachedIdleAllocationLimit(
55      kScavengeSpeedInBytesPerMs, ScavengeJob::kMinAllocationLimit,
56      kNewSpaceCapacity));
57  EXPECT_FALSE(ScavengeJob::ReachedIdleAllocationLimit(
58      kScavengeSpeedInBytesPerMs, expected_size - 1, kNewSpaceCapacity));
59  EXPECT_TRUE(ScavengeJob::ReachedIdleAllocationLimit(
60      kScavengeSpeedInBytesPerMs, expected_size, kNewSpaceCapacity));
61}
62
63
64TEST(ScavengeJob, AllocationLimitHighScavengeSpeed) {
65  size_t scavenge_speed = kNewSpaceCapacity;
66  size_t expected_size =
67      static_cast<size_t>(
68          kNewSpaceCapacity *
69          ScavengeJob::kMaxAllocationLimitAsFractionOfNewSpace) -
70      ScavengeJob::kBytesAllocatedBeforeNextIdleTask;
71  EXPECT_FALSE(ScavengeJob::ReachedIdleAllocationLimit(
72      scavenge_speed, expected_size - 1, kNewSpaceCapacity));
73  EXPECT_TRUE(ScavengeJob::ReachedIdleAllocationLimit(
74      scavenge_speed, expected_size + 1, kNewSpaceCapacity));
75}
76
77
78TEST(ScavengeJob, EnoughIdleTimeForScavengeUnknownScavengeSpeed) {
79  size_t scavenge_speed = ScavengeJob::kInitialScavengeSpeedInBytesPerMs;
80  size_t new_space_size = 1 * MB;
81  size_t expected_time = (new_space_size + scavenge_speed - 1) / scavenge_speed;
82  EXPECT_TRUE(
83      ScavengeJob::EnoughIdleTimeForScavenge(expected_time, 0, new_space_size));
84  EXPECT_FALSE(ScavengeJob::EnoughIdleTimeForScavenge(expected_time - 1, 0,
85                                                      new_space_size));
86}
87
88
89TEST(ScavengeJob, EnoughIdleTimeForScavengeLowScavengeSpeed) {
90  size_t scavenge_speed = 1 * KB;
91  size_t new_space_size = 1 * MB;
92  size_t expected_time = (new_space_size + scavenge_speed - 1) / scavenge_speed;
93  EXPECT_TRUE(ScavengeJob::EnoughIdleTimeForScavenge(
94      expected_time, scavenge_speed, new_space_size));
95  EXPECT_FALSE(ScavengeJob::EnoughIdleTimeForScavenge(
96      expected_time - 1, scavenge_speed, new_space_size));
97}
98
99
100TEST(ScavengeJob, EnoughIdleTimeForScavengeHighScavengeSpeed) {
101  size_t scavenge_speed = kNewSpaceCapacity;
102  size_t new_space_size = 1 * MB;
103  size_t expected_time = (new_space_size + scavenge_speed - 1) / scavenge_speed;
104  EXPECT_TRUE(ScavengeJob::EnoughIdleTimeForScavenge(
105      expected_time, scavenge_speed, new_space_size));
106  EXPECT_FALSE(ScavengeJob::EnoughIdleTimeForScavenge(
107      expected_time - 1, scavenge_speed, new_space_size));
108}
109
110}  // namespace internal
111}  // namespace v8
112