1 // Copyright 2015 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.
5 #include "src/heap/scavenge-job.h"
7 #include "src/base/platform/time.h"
8 #include "src/heap/heap-inl.h"
9 #include "src/heap/heap.h"
10 #include "src/isolate.h"
17 const double ScavengeJob::kMaxAllocationLimitAsFractionOfNewSpace = 0.8;
19 void ScavengeJob::IdleTask::RunInternal(double deadline_in_seconds) {
20 Heap* heap = isolate_->heap();
21 double deadline_in_ms =
23 static_cast<double>(base::Time::kMillisecondsPerSecond);
24 double start_ms = heap->MonotonicallyIncreasingTimeInMs();
25 double idle_time_in_ms = deadline_in_ms - start_ms;
26 size_t scavenge_speed_in_bytes_per_ms =
27 static_cast<size_t>(heap->tracer()->ScavengeSpeedInBytesPerMillisecond());
28 size_t new_space_size = heap->new_space()->Size();
29 size_t new_space_capacity = heap->new_space()->Capacity();
31 job_->NotifyIdleTask();
33 if (ReachedIdleAllocationLimit(scavenge_speed_in_bytes_per_ms, new_space_size,
34 new_space_capacity)) {
35 if (EnoughIdleTimeForScavenge(
36 idle_time_in_ms, scavenge_speed_in_bytes_per_ms, new_space_size)) {
37 heap->CollectGarbage(NEW_SPACE, "idle task: scavenge");
39 // Immediately request another idle task that can get larger idle time.
40 job_->RescheduleIdleTask(heap);
46 bool ScavengeJob::ReachedIdleAllocationLimit(
47 size_t scavenge_speed_in_bytes_per_ms, size_t new_space_size,
48 size_t new_space_capacity) {
49 if (scavenge_speed_in_bytes_per_ms == 0) {
50 scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs;
53 // Set the allocation limit to the number of bytes we can scavenge in an
55 size_t allocation_limit = kAverageIdleTimeMs * scavenge_speed_in_bytes_per_ms;
57 // Keep the limit smaller than the new space capacity.
60 static_cast<size_t>(new_space_capacity *
61 kMaxAllocationLimitAsFractionOfNewSpace));
62 // Adjust the limit to take into account bytes that will be allocated until
64 allocation_limit = allocation_limit < kBytesAllocatedBeforeNextIdleTask
66 : allocation_limit - kBytesAllocatedBeforeNextIdleTask;
67 // Keep the limit large enough to avoid scavenges in tiny new space.
68 allocation_limit = Max(allocation_limit, kMinAllocationLimit);
70 return allocation_limit <= new_space_size;
74 bool ScavengeJob::EnoughIdleTimeForScavenge(
75 double idle_time_in_ms, size_t scavenge_speed_in_bytes_per_ms,
76 size_t new_space_size) {
77 if (scavenge_speed_in_bytes_per_ms == 0) {
78 scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs;
80 return new_space_size <= idle_time_in_ms * scavenge_speed_in_bytes_per_ms;
84 void ScavengeJob::RescheduleIdleTask(Heap* heap) {
85 // Make sure that we don't reschedule more than one time.
86 // Otherwise, we might spam the scheduler with idle tasks.
87 if (!idle_task_rescheduled_) {
88 ScheduleIdleTask(heap);
89 idle_task_rescheduled_ = true;
94 void ScavengeJob::ScheduleIdleTaskIfNeeded(Heap* heap, int bytes_allocated) {
95 bytes_allocated_since_the_last_task_ += bytes_allocated;
96 if (bytes_allocated_since_the_last_task_ >=
97 static_cast<int>(kBytesAllocatedBeforeNextIdleTask)) {
98 ScheduleIdleTask(heap);
99 bytes_allocated_since_the_last_task_ = 0;
100 idle_task_rescheduled_ = false;
105 void ScavengeJob::ScheduleIdleTask(Heap* heap) {
106 if (!idle_task_pending_) {
107 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap->isolate());
108 if (V8::GetCurrentPlatform()->IdleTasksEnabled(isolate)) {
109 idle_task_pending_ = true;
110 auto task = new IdleTask(heap->isolate(), this);
111 V8::GetCurrentPlatform()->CallIdleOnForegroundThread(isolate, task);
116 } // namespace v8::internal