OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/heap/scavenge-job.h" | 5 #include "src/heap/scavenge-job.h" |
6 | 6 |
7 #include "src/base/platform/time.h" | 7 #include "src/base/platform/time.h" |
8 #include "src/heap/heap-inl.h" | 8 #include "src/heap/heap-inl.h" |
9 #include "src/heap/heap.h" | 9 #include "src/heap/heap.h" |
10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
11 #include "src/v8.h" | 11 #include "src/v8.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 | 16 |
17 const double ScavengeJob::kMaxAllocationLimitAsFractionOfNewSpace = 0.8; | 17 const double ScavengeJob::kMaxAllocationLimitAsFractionOfNewSpace = 0.8; |
18 | 18 |
19 void ScavengeJob::IdleTask::RunInternal(double deadline_in_seconds) { | 19 void ScavengeJob::IdleTask::RunInternal(double deadline_in_seconds) { |
20 Heap* heap = isolate()->heap(); | 20 Heap* heap = isolate()->heap(); |
21 double deadline_in_ms = | 21 double deadline_in_ms = |
22 deadline_in_seconds * | 22 deadline_in_seconds * |
23 static_cast<double>(base::Time::kMillisecondsPerSecond); | 23 static_cast<double>(base::Time::kMillisecondsPerSecond); |
24 double start_ms = heap->MonotonicallyIncreasingTimeInMs(); | 24 double start_ms = heap->MonotonicallyIncreasingTimeInMs(); |
25 double idle_time_in_ms = deadline_in_ms - start_ms; | 25 double idle_time_in_ms = deadline_in_ms - start_ms; |
26 size_t scavenge_speed_in_bytes_per_ms = | 26 double scavenge_speed_in_bytes_per_ms = |
27 static_cast<size_t>(heap->tracer()->ScavengeSpeedInBytesPerMillisecond()); | 27 heap->tracer()->ScavengeSpeedInBytesPerMillisecond(); |
28 size_t new_space_size = heap->new_space()->Size(); | 28 size_t new_space_size = heap->new_space()->Size(); |
29 size_t new_space_capacity = heap->new_space()->Capacity(); | 29 size_t new_space_capacity = heap->new_space()->Capacity(); |
30 | 30 |
31 job_->NotifyIdleTask(); | 31 job_->NotifyIdleTask(); |
32 | 32 |
33 if (ReachedIdleAllocationLimit(scavenge_speed_in_bytes_per_ms, new_space_size, | 33 if (ReachedIdleAllocationLimit(scavenge_speed_in_bytes_per_ms, new_space_size, |
34 new_space_capacity)) { | 34 new_space_capacity)) { |
35 if (EnoughIdleTimeForScavenge( | 35 if (EnoughIdleTimeForScavenge( |
36 idle_time_in_ms, scavenge_speed_in_bytes_per_ms, new_space_size)) { | 36 idle_time_in_ms, scavenge_speed_in_bytes_per_ms, new_space_size)) { |
37 heap->CollectGarbage(NEW_SPACE, "idle task: scavenge"); | 37 heap->CollectGarbage(NEW_SPACE, "idle task: scavenge"); |
38 } else { | 38 } else { |
39 // Immediately request another idle task that can get larger idle time. | 39 // Immediately request another idle task that can get larger idle time. |
40 job_->RescheduleIdleTask(heap); | 40 job_->RescheduleIdleTask(heap); |
41 } | 41 } |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 | |
46 bool ScavengeJob::ReachedIdleAllocationLimit( | 45 bool ScavengeJob::ReachedIdleAllocationLimit( |
47 size_t scavenge_speed_in_bytes_per_ms, size_t new_space_size, | 46 double scavenge_speed_in_bytes_per_ms, size_t new_space_size, |
48 size_t new_space_capacity) { | 47 size_t new_space_capacity) { |
49 if (scavenge_speed_in_bytes_per_ms == 0) { | 48 if (scavenge_speed_in_bytes_per_ms == 0) { |
50 scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs; | 49 scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs; |
51 } | 50 } |
52 | 51 |
53 // Set the allocation limit to the number of bytes we can scavenge in an | 52 // Set the allocation limit to the number of bytes we can scavenge in an |
54 // average idle task. | 53 // average idle task. |
55 size_t allocation_limit = kAverageIdleTimeMs * scavenge_speed_in_bytes_per_ms; | 54 double allocation_limit = kAverageIdleTimeMs * scavenge_speed_in_bytes_per_ms; |
56 | 55 |
57 // Keep the limit smaller than the new space capacity. | 56 // Keep the limit smaller than the new space capacity. |
58 allocation_limit = | 57 allocation_limit = |
59 Min(allocation_limit, | 58 Min<double>(allocation_limit, |
60 static_cast<size_t>(new_space_capacity * | 59 new_space_capacity * kMaxAllocationLimitAsFractionOfNewSpace); |
61 kMaxAllocationLimitAsFractionOfNewSpace)); | |
62 // Adjust the limit to take into account bytes that will be allocated until | 60 // Adjust the limit to take into account bytes that will be allocated until |
63 // the next check. | 61 // the next check and keep the limit large enough to avoid scavenges in tiny |
64 allocation_limit = allocation_limit < kBytesAllocatedBeforeNextIdleTask | 62 // new space. |
65 ? 0 | 63 allocation_limit = |
66 : allocation_limit - kBytesAllocatedBeforeNextIdleTask; | 64 Max<double>(allocation_limit - kBytesAllocatedBeforeNextIdleTask, |
67 // Keep the limit large enough to avoid scavenges in tiny new space. | 65 kMinAllocationLimit); |
68 allocation_limit = Max(allocation_limit, kMinAllocationLimit); | |
69 | 66 |
70 return allocation_limit <= new_space_size; | 67 return allocation_limit <= new_space_size; |
71 } | 68 } |
72 | 69 |
73 | |
74 bool ScavengeJob::EnoughIdleTimeForScavenge( | 70 bool ScavengeJob::EnoughIdleTimeForScavenge( |
75 double idle_time_in_ms, size_t scavenge_speed_in_bytes_per_ms, | 71 double idle_time_in_ms, double scavenge_speed_in_bytes_per_ms, |
76 size_t new_space_size) { | 72 size_t new_space_size) { |
77 if (scavenge_speed_in_bytes_per_ms == 0) { | 73 if (scavenge_speed_in_bytes_per_ms == 0) { |
78 scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs; | 74 scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs; |
79 } | 75 } |
80 return new_space_size <= idle_time_in_ms * scavenge_speed_in_bytes_per_ms; | 76 return new_space_size <= idle_time_in_ms * scavenge_speed_in_bytes_per_ms; |
81 } | 77 } |
82 | 78 |
83 | 79 |
84 void ScavengeJob::RescheduleIdleTask(Heap* heap) { | 80 void ScavengeJob::RescheduleIdleTask(Heap* heap) { |
85 // Make sure that we don't reschedule more than one time. | 81 // Make sure that we don't reschedule more than one time. |
(...skipping 21 matching lines...) Expand all Loading... |
107 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap->isolate()); | 103 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap->isolate()); |
108 if (V8::GetCurrentPlatform()->IdleTasksEnabled(isolate)) { | 104 if (V8::GetCurrentPlatform()->IdleTasksEnabled(isolate)) { |
109 idle_task_pending_ = true; | 105 idle_task_pending_ = true; |
110 auto task = new IdleTask(heap->isolate(), this); | 106 auto task = new IdleTask(heap->isolate(), this); |
111 V8::GetCurrentPlatform()->CallIdleOnForegroundThread(isolate, task); | 107 V8::GetCurrentPlatform()->CallIdleOnForegroundThread(isolate, task); |
112 } | 108 } |
113 } | 109 } |
114 } | 110 } |
115 } // namespace internal | 111 } // namespace internal |
116 } // namespace v8 | 112 } // namespace v8 |
OLD | NEW |