OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "src/heap/scavenge-job.h" |
| 6 |
| 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" |
| 11 #include "src/v8.h" |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 |
| 16 |
| 17 const double ScavengeJob::kMaxAllocationLimitAsFractionOfNewSpace = 0.8; |
| 18 |
| 19 void ScavengeJob::IdleTask::RunInternal(double deadline_in_seconds) { |
| 20 Heap* heap = isolate_->heap(); |
| 21 double deadline_in_ms = |
| 22 deadline_in_seconds * |
| 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(); |
| 30 |
| 31 job_->NotifyIdleTask(); |
| 32 |
| 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"); |
| 38 } else { |
| 39 // Immediately request another idle task that can get larger idle time. |
| 40 job_->RescheduleIdleTask(heap); |
| 41 } |
| 42 } |
| 43 } |
| 44 |
| 45 |
| 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; |
| 51 } |
| 52 |
| 53 // Set the allocation limit to the number of bytes we can scavenge in an |
| 54 // average idle task. |
| 55 size_t allocation_limit = kAverageIdleTimeMs * scavenge_speed_in_bytes_per_ms; |
| 56 |
| 57 // Keep the limit smaller than the new space capacity. |
| 58 allocation_limit = |
| 59 Min(allocation_limit, |
| 60 static_cast<size_t>(new_space_capacity * |
| 61 kMaxAllocationLimitAsFractionOfNewSpace)); |
| 62 // Adjust the limit to take into account bytes that will be allocated until |
| 63 // the next check. |
| 64 allocation_limit = allocation_limit < kBytesAllocatedBeforeNextIdleTask |
| 65 ? 0 |
| 66 : allocation_limit - kBytesAllocatedBeforeNextIdleTask; |
| 67 // Keep the limit large enough to avoid scavenges in tiny new space. |
| 68 allocation_limit = Max(allocation_limit, kMinAllocationLimit); |
| 69 |
| 70 return allocation_limit <= new_space_size; |
| 71 } |
| 72 |
| 73 |
| 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; |
| 79 } |
| 80 return new_space_size <= idle_time_in_ms * scavenge_speed_in_bytes_per_ms; |
| 81 } |
| 82 |
| 83 |
| 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; |
| 90 } |
| 91 } |
| 92 |
| 93 |
| 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; |
| 101 } |
| 102 } |
| 103 |
| 104 |
| 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); |
| 112 } |
| 113 } |
| 114 } |
| 115 } |
| 116 } // namespace v8::internal |
OLD | NEW |