Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: src/heap/scavenge-job.cc

Issue 1352453004: Perform scavenge in idle tasks. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 within the [min, max] range.
58 allocation_limit =
59 Min(allocation_limit,
60 static_cast<size_t>(new_space_capacity *
61 kMaxAllocationLimitAsFractionOfNewSpace));
62 allocation_limit = Max(allocation_limit, kMinAllocationLimit);
63
64 // Is the limit going to be exceeded before the next task is scheduled?
65 return allocation_limit < new_space_size + kBytesAllocatedBeforeNextIdleTask;
66 }
67
68
69 bool ScavengeJob::EnoughIdleTimeForScavenge(
70 double idle_time_in_ms, size_t scavenge_speed_in_bytes_per_ms,
71 size_t new_space_size) {
72 if (scavenge_speed_in_bytes_per_ms == 0) {
73 scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs;
74 }
75 return new_space_size <= idle_time_in_ms * scavenge_speed_in_bytes_per_ms;
76 }
77
78
79 void ScavengeJob::RescheduleIdleTask(Heap* heap) {
80 // Make sure that we don't reschedule more than one time.
81 // Otherwise, we might spam the scheduler with idle tasks.
82 if (!idle_task_rescheduled_) {
83 ScheduleIdleTask(heap);
84 idle_task_rescheduled_ = true;
85 }
86 }
87
88
89 void ScavengeJob::ScheduleIdleTask(Heap* heap, int bytes_allocated) {
90 bytes_allocated_since_the_last_task_ += bytes_allocated;
91 if (bytes_allocated_since_the_last_task_ >=
92 kBytesAllocatedBeforeNextIdleTask) {
93 ScheduleIdleTask(heap);
94 bytes_allocated_since_the_last_task_ = 0;
95 idle_task_rescheduled_ = false;
96 }
97 }
98
99
100 void ScavengeJob::ScheduleIdleTask(Heap* heap) {
101 if (!idle_task_pending_) {
102 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap->isolate());
103 if (V8::GetCurrentPlatform()->IdleTasksEnabled(isolate)) {
104 idle_task_pending_ = true;
105 auto task = new IdleTask(heap->isolate(), this);
106 V8::GetCurrentPlatform()->CallIdleOnForegroundThread(isolate, task);
107 }
108 }
109 }
110 }
111 } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698