Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | #include "src/heap/mark-compact.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6 | |
| 7 | #include "src/base/atomicops.h" |
| 8 | #include "src/base/bits.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | #include "src/base/sys-info.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/code-stubs.h" |
| 11 | #include "src/compilation-cache.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include "src/deoptimizer.h" |
| 13 | #include "src/execution.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14 | #include "src/frames-inl.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15 | #include "src/gdb-jit.h" |
| 16 | #include "src/global-handles.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | #include "src/heap/array-buffer-tracker.h" |
| 18 | #include "src/heap/gc-tracer.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19 | #include "src/heap/incremental-marking.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 20 | #include "src/heap/mark-compact-inl.h" |
| 21 | #include "src/heap/object-stats.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | #include "src/heap/objects-visiting-inl.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 23 | #include "src/heap/objects-visiting.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 24 | #include "src/heap/page-parallel-job.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | #include "src/heap/spaces-inl.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 26 | #include "src/ic/ic.h" |
| 27 | #include "src/ic/stub-cache.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | #include "src/profiler/cpu-profiler.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 29 | #include "src/utils-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | #include "src/v8.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | |
| 32 | namespace v8 { |
| 33 | namespace internal { |
| 34 | |
| 35 | |
| 36 | const char* Marking::kWhiteBitPattern = "00"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | const char* Marking::kBlackBitPattern = "11"; |
| 38 | const char* Marking::kGreyBitPattern = "10"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | const char* Marking::kImpossibleBitPattern = "01"; |
| 40 | |
| 41 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 42 | // The following has to hold in order for {Marking::MarkBitFrom} to not produce |
| 43 | // invalid {kImpossibleBitPattern} in the marking bitmap by overlapping. |
| 44 | STATIC_ASSERT(Heap::kMinObjectSizeInWords >= 2); |
| 45 | |
| 46 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | // ------------------------------------------------------------------------- |
| 48 | // MarkCompactCollector |
| 49 | |
| 50 | MarkCompactCollector::MarkCompactCollector(Heap* heap) |
| 51 | : // NOLINT |
| 52 | #ifdef DEBUG |
| 53 | state_(IDLE), |
| 54 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | marking_parity_(ODD_MARKING_PARITY), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | was_marked_incrementally_(false), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 57 | evacuation_(false), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | heap_(heap), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 59 | marking_deque_memory_(NULL), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 60 | marking_deque_memory_committed_(0), |
| 61 | code_flusher_(nullptr), |
| 62 | have_code_to_deoptimize_(false), |
| 63 | compacting_(false), |
| 64 | sweeping_in_progress_(false), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | pending_sweeper_tasks_semaphore_(0), |
| 66 | pending_compaction_tasks_semaphore_(0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | #ifdef VERIFY_HEAP |
| 70 | class VerifyMarkingVisitor : public ObjectVisitor { |
| 71 | public: |
| 72 | explicit VerifyMarkingVisitor(Heap* heap) : heap_(heap) {} |
| 73 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | void VisitPointers(Object** start, Object** end) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | for (Object** current = start; current < end; current++) { |
| 76 | if ((*current)->IsHeapObject()) { |
| 77 | HeapObject* object = HeapObject::cast(*current); |
| 78 | CHECK(heap_->mark_compact_collector()->IsMarked(object)); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | void VisitEmbeddedPointer(RelocInfo* rinfo) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
| 85 | if (!rinfo->host()->IsWeakObject(rinfo->target_object())) { |
| 86 | Object* p = rinfo->target_object(); |
| 87 | VisitPointer(&p); |
| 88 | } |
| 89 | } |
| 90 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 91 | void VisitCell(RelocInfo* rinfo) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | Code* code = rinfo->host(); |
| 93 | DCHECK(rinfo->rmode() == RelocInfo::CELL); |
| 94 | if (!code->IsWeakObject(rinfo->target_cell())) { |
| 95 | ObjectVisitor::VisitCell(rinfo); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | Heap* heap_; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | static void VerifyMarking(Heap* heap, Address bottom, Address top) { |
| 105 | VerifyMarkingVisitor visitor(heap); |
| 106 | HeapObject* object; |
| 107 | Address next_object_must_be_here_or_later = bottom; |
| 108 | |
| 109 | for (Address current = bottom; current < top; current += kPointerSize) { |
| 110 | object = HeapObject::FromAddress(current); |
| 111 | if (MarkCompactCollector::IsMarked(object)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | CHECK(Marking::IsBlack(Marking::MarkBitFrom(object))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | CHECK(current >= next_object_must_be_here_or_later); |
| 114 | object->Iterate(&visitor); |
| 115 | next_object_must_be_here_or_later = current + object->Size(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 116 | // The next word for sure belongs to the current object, jump over it. |
| 117 | current += kPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 122 | static void VerifyMarkingBlackPage(Heap* heap, Page* page) { |
| 123 | CHECK(page->IsFlagSet(Page::BLACK_PAGE)); |
| 124 | VerifyMarkingVisitor visitor(heap); |
| 125 | HeapObjectIterator it(page); |
| 126 | for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) { |
| 127 | CHECK(Marking::IsBlack(Marking::MarkBitFrom(object))); |
| 128 | object->Iterate(&visitor); |
| 129 | } |
| 130 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 131 | |
| 132 | static void VerifyMarking(NewSpace* space) { |
| 133 | Address end = space->top(); |
| 134 | NewSpacePageIterator it(space->bottom(), end); |
| 135 | // The bottom position is at the start of its page. Allows us to use |
| 136 | // page->area_start() as start of range on all pages. |
| 137 | CHECK_EQ(space->bottom(), |
| 138 | NewSpacePage::FromAddress(space->bottom())->area_start()); |
| 139 | while (it.has_next()) { |
| 140 | NewSpacePage* page = it.next(); |
| 141 | Address limit = it.has_next() ? page->area_end() : end; |
| 142 | CHECK(limit == end || !page->Contains(end)); |
| 143 | VerifyMarking(space->heap(), page->area_start(), limit); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
| 148 | static void VerifyMarking(PagedSpace* space) { |
| 149 | PageIterator it(space); |
| 150 | |
| 151 | while (it.has_next()) { |
| 152 | Page* p = it.next(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 153 | if (p->IsFlagSet(Page::BLACK_PAGE)) { |
| 154 | VerifyMarkingBlackPage(space->heap(), p); |
| 155 | } else { |
| 156 | VerifyMarking(space->heap(), p->area_start(), p->area_end()); |
| 157 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static void VerifyMarking(Heap* heap) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 163 | VerifyMarking(heap->old_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | VerifyMarking(heap->code_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 165 | VerifyMarking(heap->map_space()); |
| 166 | VerifyMarking(heap->new_space()); |
| 167 | |
| 168 | VerifyMarkingVisitor visitor(heap); |
| 169 | |
| 170 | LargeObjectIterator it(heap->lo_space()); |
| 171 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
| 172 | if (MarkCompactCollector::IsMarked(obj)) { |
| 173 | obj->Iterate(&visitor); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | heap->IterateStrongRoots(&visitor, VISIT_ONLY_STRONG); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | class VerifyEvacuationVisitor : public ObjectVisitor { |
| 182 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 183 | void VisitPointers(Object** start, Object** end) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 184 | for (Object** current = start; current < end; current++) { |
| 185 | if ((*current)->IsHeapObject()) { |
| 186 | HeapObject* object = HeapObject::cast(*current); |
| 187 | CHECK(!MarkCompactCollector::IsOnEvacuationCandidate(object)); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | |
| 194 | static void VerifyEvacuation(Page* page) { |
| 195 | VerifyEvacuationVisitor visitor; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 196 | HeapObjectIterator iterator(page); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 197 | for (HeapObject* heap_object = iterator.Next(); heap_object != NULL; |
| 198 | heap_object = iterator.Next()) { |
| 199 | // We skip free space objects. |
| 200 | if (!heap_object->IsFiller()) { |
| 201 | heap_object->Iterate(&visitor); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | static void VerifyEvacuation(NewSpace* space) { |
| 208 | NewSpacePageIterator it(space->bottom(), space->top()); |
| 209 | VerifyEvacuationVisitor visitor; |
| 210 | |
| 211 | while (it.has_next()) { |
| 212 | NewSpacePage* page = it.next(); |
| 213 | Address current = page->area_start(); |
| 214 | Address limit = it.has_next() ? page->area_end() : space->top(); |
| 215 | CHECK(limit == space->top() || !page->Contains(space->top())); |
| 216 | while (current < limit) { |
| 217 | HeapObject* object = HeapObject::FromAddress(current); |
| 218 | object->Iterate(&visitor); |
| 219 | current += object->Size(); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | static void VerifyEvacuation(Heap* heap, PagedSpace* space) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 226 | if (FLAG_use_allocation_folding && (space == heap->old_space())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | PageIterator it(space); |
| 230 | |
| 231 | while (it.has_next()) { |
| 232 | Page* p = it.next(); |
| 233 | if (p->IsEvacuationCandidate()) continue; |
| 234 | VerifyEvacuation(p); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | static void VerifyEvacuation(Heap* heap) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 240 | VerifyEvacuation(heap, heap->old_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 241 | VerifyEvacuation(heap, heap->code_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 242 | VerifyEvacuation(heap, heap->map_space()); |
| 243 | VerifyEvacuation(heap->new_space()); |
| 244 | |
| 245 | VerifyEvacuationVisitor visitor; |
| 246 | heap->IterateStrongRoots(&visitor, VISIT_ALL); |
| 247 | } |
| 248 | #endif // VERIFY_HEAP |
| 249 | |
| 250 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 251 | void MarkCompactCollector::SetUp() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 252 | DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0); |
| 253 | DCHECK(strcmp(Marking::kBlackBitPattern, "11") == 0); |
| 254 | DCHECK(strcmp(Marking::kGreyBitPattern, "10") == 0); |
| 255 | DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0); |
| 256 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 257 | EnsureMarkingDequeIsReserved(); |
| 258 | EnsureMarkingDequeIsCommitted(kMinMarkingDequeSize); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 259 | |
| 260 | if (FLAG_flush_code) { |
| 261 | code_flusher_ = new CodeFlusher(isolate()); |
| 262 | if (FLAG_trace_code_flushing) { |
| 263 | PrintF("[code-flushing is now on]\n"); |
| 264 | } |
| 265 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 269 | void MarkCompactCollector::TearDown() { |
| 270 | AbortCompaction(); |
| 271 | delete marking_deque_memory_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 272 | delete code_flusher_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 273 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 274 | |
| 275 | |
| 276 | void MarkCompactCollector::AddEvacuationCandidate(Page* p) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 277 | DCHECK(!p->NeverEvacuate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 278 | p->MarkEvacuationCandidate(); |
| 279 | evacuation_candidates_.Add(p); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | static void TraceFragmentation(PagedSpace* space) { |
| 284 | int number_of_pages = space->CountTotalPages(); |
| 285 | intptr_t reserved = (number_of_pages * space->AreaSize()); |
| 286 | intptr_t free = reserved - space->SizeOfObjects(); |
| 287 | PrintF("[%s]: %d pages, %d (%.1f%%) free\n", |
| 288 | AllocationSpaceName(space->identity()), number_of_pages, |
| 289 | static_cast<int>(free), static_cast<double>(free) * 100 / reserved); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | bool MarkCompactCollector::StartCompaction(CompactionMode mode) { |
| 294 | if (!compacting_) { |
| 295 | DCHECK(evacuation_candidates_.length() == 0); |
| 296 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 297 | CollectEvacuationCandidates(heap()->old_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 298 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 299 | if (FLAG_compact_code_space) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 300 | CollectEvacuationCandidates(heap()->code_space()); |
| 301 | } else if (FLAG_trace_fragmentation) { |
| 302 | TraceFragmentation(heap()->code_space()); |
| 303 | } |
| 304 | |
| 305 | if (FLAG_trace_fragmentation) { |
| 306 | TraceFragmentation(heap()->map_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | heap()->old_space()->EvictEvacuationCandidatesFromLinearAllocationArea(); |
| 310 | heap()->code_space()->EvictEvacuationCandidatesFromLinearAllocationArea(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 311 | |
| 312 | compacting_ = evacuation_candidates_.length() > 0; |
| 313 | } |
| 314 | |
| 315 | return compacting_; |
| 316 | } |
| 317 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 318 | void MarkCompactCollector::ClearInvalidRememberedSetSlots() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 319 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 320 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_STORE_BUFFER); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 321 | RememberedSet<OLD_TO_NEW>::ClearInvalidSlots(heap()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 322 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 323 | // There is not need to filter the old to old set because |
| 324 | // it is completely cleared after the mark-compact GC. |
| 325 | // The slots that become invalid due to runtime transitions are |
| 326 | // cleared eagerly immediately after the transition. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 327 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 328 | #ifdef VERIFY_HEAP |
| 329 | if (FLAG_verify_heap) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 330 | RememberedSet<OLD_TO_NEW>::VerifyValidSlots(heap()); |
| 331 | RememberedSet<OLD_TO_OLD>::VerifyValidSlots(heap()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 332 | } |
| 333 | #endif |
| 334 | } |
| 335 | |
| 336 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 337 | void MarkCompactCollector::CollectGarbage() { |
| 338 | // Make sure that Prepare() has been called. The individual steps below will |
| 339 | // update the state as they proceed. |
| 340 | DCHECK(state_ == PREPARE_GC); |
| 341 | |
| 342 | MarkLiveObjects(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 343 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | DCHECK(heap_->incremental_marking()->IsStopped()); |
| 345 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 346 | ClearNonLiveReferences(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 347 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | #ifdef VERIFY_HEAP |
| 349 | if (FLAG_verify_heap) { |
| 350 | VerifyMarking(heap_); |
| 351 | } |
| 352 | #endif |
| 353 | |
| 354 | SweepSpaces(); |
| 355 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 356 | EvacuateNewSpaceAndCandidates(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 357 | |
| 358 | Finish(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
| 362 | #ifdef VERIFY_HEAP |
| 363 | void MarkCompactCollector::VerifyMarkbitsAreClean(PagedSpace* space) { |
| 364 | PageIterator it(space); |
| 365 | |
| 366 | while (it.has_next()) { |
| 367 | Page* p = it.next(); |
| 368 | CHECK(p->markbits()->IsClean()); |
| 369 | CHECK_EQ(0, p->LiveBytes()); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | |
| 374 | void MarkCompactCollector::VerifyMarkbitsAreClean(NewSpace* space) { |
| 375 | NewSpacePageIterator it(space->bottom(), space->top()); |
| 376 | |
| 377 | while (it.has_next()) { |
| 378 | NewSpacePage* p = it.next(); |
| 379 | CHECK(p->markbits()->IsClean()); |
| 380 | CHECK_EQ(0, p->LiveBytes()); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | |
| 385 | void MarkCompactCollector::VerifyMarkbitsAreClean() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 386 | VerifyMarkbitsAreClean(heap_->old_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 387 | VerifyMarkbitsAreClean(heap_->code_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 388 | VerifyMarkbitsAreClean(heap_->map_space()); |
| 389 | VerifyMarkbitsAreClean(heap_->new_space()); |
| 390 | |
| 391 | LargeObjectIterator it(heap_->lo_space()); |
| 392 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
| 393 | MarkBit mark_bit = Marking::MarkBitFrom(obj); |
| 394 | CHECK(Marking::IsWhite(mark_bit)); |
| 395 | CHECK_EQ(0, Page::FromAddress(obj->address())->LiveBytes()); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | void MarkCompactCollector::VerifyWeakEmbeddedObjectsInCode() { |
| 401 | HeapObjectIterator code_iterator(heap()->code_space()); |
| 402 | for (HeapObject* obj = code_iterator.Next(); obj != NULL; |
| 403 | obj = code_iterator.Next()) { |
| 404 | Code* code = Code::cast(obj); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 405 | if (!code->is_optimized_code()) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 406 | if (WillBeDeoptimized(code)) continue; |
| 407 | code->VerifyEmbeddedObjectsDependency(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | |
| 412 | void MarkCompactCollector::VerifyOmittedMapChecks() { |
| 413 | HeapObjectIterator iterator(heap()->map_space()); |
| 414 | for (HeapObject* obj = iterator.Next(); obj != NULL; obj = iterator.Next()) { |
| 415 | Map* map = Map::cast(obj); |
| 416 | map->VerifyOmittedMapChecks(); |
| 417 | } |
| 418 | } |
| 419 | #endif // VERIFY_HEAP |
| 420 | |
| 421 | |
| 422 | static void ClearMarkbitsInPagedSpace(PagedSpace* space) { |
| 423 | PageIterator it(space); |
| 424 | |
| 425 | while (it.has_next()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 426 | Page* p = it.next(); |
| 427 | Bitmap::Clear(p); |
| 428 | if (p->IsFlagSet(Page::BLACK_PAGE)) { |
| 429 | p->ClearFlag(Page::BLACK_PAGE); |
| 430 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | static void ClearMarkbitsInNewSpace(NewSpace* space) { |
| 436 | NewSpacePageIterator it(space->ToSpaceStart(), space->ToSpaceEnd()); |
| 437 | |
| 438 | while (it.has_next()) { |
| 439 | Bitmap::Clear(it.next()); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | |
| 444 | void MarkCompactCollector::ClearMarkbits() { |
| 445 | ClearMarkbitsInPagedSpace(heap_->code_space()); |
| 446 | ClearMarkbitsInPagedSpace(heap_->map_space()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 447 | ClearMarkbitsInPagedSpace(heap_->old_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 448 | ClearMarkbitsInNewSpace(heap_->new_space()); |
| 449 | |
| 450 | LargeObjectIterator it(heap_->lo_space()); |
| 451 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 452 | Marking::MarkWhite(Marking::MarkBitFrom(obj)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 453 | MemoryChunk* chunk = MemoryChunk::FromAddress(obj->address()); |
| 454 | chunk->ResetProgressBar(); |
| 455 | chunk->ResetLiveBytes(); |
| 456 | if (chunk->IsFlagSet(Page::BLACK_PAGE)) { |
| 457 | chunk->ClearFlag(Page::BLACK_PAGE); |
| 458 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | class MarkCompactCollector::SweeperTask : public v8::Task { |
| 464 | public: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 465 | SweeperTask(Heap* heap, AllocationSpace space_to_start) |
| 466 | : heap_(heap), space_to_start_(space_to_start) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 467 | |
| 468 | virtual ~SweeperTask() {} |
| 469 | |
| 470 | private: |
| 471 | // v8::Task overrides. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 472 | void Run() override { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 473 | DCHECK_GE(space_to_start_, FIRST_PAGED_SPACE); |
| 474 | DCHECK_LE(space_to_start_, LAST_PAGED_SPACE); |
| 475 | const int offset = space_to_start_ - FIRST_PAGED_SPACE; |
| 476 | const int num_spaces = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1; |
| 477 | for (int i = 0; i < num_spaces; i++) { |
| 478 | const int space_id = FIRST_PAGED_SPACE + ((i + offset) % num_spaces); |
| 479 | DCHECK_GE(space_id, FIRST_PAGED_SPACE); |
| 480 | DCHECK_LE(space_id, LAST_PAGED_SPACE); |
| 481 | heap_->mark_compact_collector()->SweepInParallel( |
| 482 | heap_->paged_space(space_id), 0); |
| 483 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 484 | heap_->mark_compact_collector()->pending_sweeper_tasks_semaphore_.Signal(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | Heap* heap_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 488 | AllocationSpace space_to_start_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 489 | |
| 490 | DISALLOW_COPY_AND_ASSIGN(SweeperTask); |
| 491 | }; |
| 492 | |
| 493 | |
| 494 | void MarkCompactCollector::StartSweeperThreads() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 495 | V8::GetCurrentPlatform()->CallOnBackgroundThread( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 496 | new SweeperTask(heap(), OLD_SPACE), v8::Platform::kShortRunningTask); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 497 | V8::GetCurrentPlatform()->CallOnBackgroundThread( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 498 | new SweeperTask(heap(), CODE_SPACE), v8::Platform::kShortRunningTask); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 499 | V8::GetCurrentPlatform()->CallOnBackgroundThread( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 500 | new SweeperTask(heap(), MAP_SPACE), v8::Platform::kShortRunningTask); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
| 504 | void MarkCompactCollector::SweepOrWaitUntilSweepingCompleted(Page* page) { |
| 505 | PagedSpace* owner = reinterpret_cast<PagedSpace*>(page->owner()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 506 | if (!page->SweepingDone()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 507 | SweepInParallel(page, owner); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 508 | if (!page->SweepingDone()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 509 | // We were not able to sweep that page, i.e., a concurrent |
| 510 | // sweeper thread currently owns this page. Wait for the sweeper |
| 511 | // thread to be done with this page. |
| 512 | page->WaitUntilSweepingCompleted(); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | |
| 518 | void MarkCompactCollector::SweepAndRefill(CompactionSpace* space) { |
| 519 | if (FLAG_concurrent_sweeping && !IsSweepingCompleted()) { |
| 520 | SweepInParallel(heap()->paged_space(space->identity()), 0); |
| 521 | space->RefillFreeList(); |
| 522 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
| 526 | void MarkCompactCollector::EnsureSweepingCompleted() { |
| 527 | DCHECK(sweeping_in_progress_ == true); |
| 528 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 529 | // If sweeping is not completed or not running at all, we try to complete it |
| 530 | // here. |
| 531 | if (!FLAG_concurrent_sweeping || !IsSweepingCompleted()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 532 | SweepInParallel(heap()->paged_space(OLD_SPACE), 0); |
| 533 | SweepInParallel(heap()->paged_space(CODE_SPACE), 0); |
| 534 | SweepInParallel(heap()->paged_space(MAP_SPACE), 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 535 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 536 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 537 | if (FLAG_concurrent_sweeping) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 538 | pending_sweeper_tasks_semaphore_.Wait(); |
| 539 | pending_sweeper_tasks_semaphore_.Wait(); |
| 540 | pending_sweeper_tasks_semaphore_.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 541 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 542 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 543 | ParallelSweepSpacesComplete(); |
| 544 | sweeping_in_progress_ = false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 545 | heap()->old_space()->RefillFreeList(); |
| 546 | heap()->code_space()->RefillFreeList(); |
| 547 | heap()->map_space()->RefillFreeList(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 548 | |
| 549 | #ifdef VERIFY_HEAP |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 550 | if (FLAG_verify_heap && !evacuation()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 551 | VerifyEvacuation(heap_); |
| 552 | } |
| 553 | #endif |
| 554 | } |
| 555 | |
| 556 | |
| 557 | bool MarkCompactCollector::IsSweepingCompleted() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 558 | if (!pending_sweeper_tasks_semaphore_.WaitFor( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 559 | base::TimeDelta::FromSeconds(0))) { |
| 560 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 561 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 562 | pending_sweeper_tasks_semaphore_.Signal(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 563 | return true; |
| 564 | } |
| 565 | |
| 566 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 567 | void Marking::TransferMark(Heap* heap, Address old_start, Address new_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 568 | // This is only used when resizing an object. |
| 569 | DCHECK(MemoryChunk::FromAddress(old_start) == |
| 570 | MemoryChunk::FromAddress(new_start)); |
| 571 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 572 | if (!heap->incremental_marking()->IsMarking() || |
| 573 | Page::FromAddress(old_start)->IsFlagSet(Page::BLACK_PAGE)) |
| 574 | return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 575 | |
| 576 | // If the mark doesn't move, we don't check the color of the object. |
| 577 | // It doesn't matter whether the object is black, since it hasn't changed |
| 578 | // size, so the adjustment to the live data count will be zero anyway. |
| 579 | if (old_start == new_start) return; |
| 580 | |
| 581 | MarkBit new_mark_bit = MarkBitFrom(new_start); |
| 582 | MarkBit old_mark_bit = MarkBitFrom(old_start); |
| 583 | |
| 584 | #ifdef DEBUG |
| 585 | ObjectColor old_color = Color(old_mark_bit); |
| 586 | #endif |
| 587 | |
| 588 | if (Marking::IsBlack(old_mark_bit)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 589 | Marking::BlackToWhite(old_mark_bit); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 590 | Marking::MarkBlack(new_mark_bit); |
| 591 | return; |
| 592 | } else if (Marking::IsGrey(old_mark_bit)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 593 | Marking::GreyToWhite(old_mark_bit); |
| 594 | heap->incremental_marking()->WhiteToGreyAndPush( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 595 | HeapObject::FromAddress(new_start), new_mark_bit); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 596 | heap->incremental_marking()->RestartIfNotMarking(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | #ifdef DEBUG |
| 600 | ObjectColor new_color = Color(new_mark_bit); |
| 601 | DCHECK(new_color == old_color); |
| 602 | #endif |
| 603 | } |
| 604 | |
| 605 | |
| 606 | const char* AllocationSpaceName(AllocationSpace space) { |
| 607 | switch (space) { |
| 608 | case NEW_SPACE: |
| 609 | return "NEW_SPACE"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 610 | case OLD_SPACE: |
| 611 | return "OLD_SPACE"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 612 | case CODE_SPACE: |
| 613 | return "CODE_SPACE"; |
| 614 | case MAP_SPACE: |
| 615 | return "MAP_SPACE"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 616 | case LO_SPACE: |
| 617 | return "LO_SPACE"; |
| 618 | default: |
| 619 | UNREACHABLE(); |
| 620 | } |
| 621 | |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 626 | void MarkCompactCollector::ComputeEvacuationHeuristics( |
| 627 | int area_size, int* target_fragmentation_percent, |
| 628 | int* max_evacuated_bytes) { |
| 629 | // For memory reducing mode we directly define both constants. |
| 630 | const int kTargetFragmentationPercentForReduceMemory = 20; |
| 631 | const int kMaxEvacuatedBytesForReduceMemory = 12 * Page::kPageSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 632 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 633 | // For regular mode (which is latency critical) we define less aggressive |
| 634 | // defaults to start and switch to a trace-based (using compaction speed) |
| 635 | // approach as soon as we have enough samples. |
| 636 | const int kTargetFragmentationPercent = 70; |
| 637 | const int kMaxEvacuatedBytes = 4 * Page::kPageSize; |
| 638 | // Time to take for a single area (=payload of page). Used as soon as there |
| 639 | // exist enough compaction speed samples. |
| 640 | const int kTargetMsPerArea = 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 641 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 642 | if (heap()->ShouldReduceMemory()) { |
| 643 | *target_fragmentation_percent = kTargetFragmentationPercentForReduceMemory; |
| 644 | *max_evacuated_bytes = kMaxEvacuatedBytesForReduceMemory; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 645 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 646 | const double estimated_compaction_speed = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 647 | heap()->tracer()->CompactionSpeedInBytesPerMillisecond(); |
| 648 | if (estimated_compaction_speed != 0) { |
| 649 | // Estimate the target fragmentation based on traced compaction speed |
| 650 | // and a goal for a single page. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 651 | const double estimated_ms_per_area = |
| 652 | 1 + area_size / estimated_compaction_speed; |
| 653 | *target_fragmentation_percent = static_cast<int>( |
| 654 | 100 - 100 * kTargetMsPerArea / estimated_ms_per_area); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 655 | if (*target_fragmentation_percent < |
| 656 | kTargetFragmentationPercentForReduceMemory) { |
| 657 | *target_fragmentation_percent = |
| 658 | kTargetFragmentationPercentForReduceMemory; |
| 659 | } |
| 660 | } else { |
| 661 | *target_fragmentation_percent = kTargetFragmentationPercent; |
| 662 | } |
| 663 | *max_evacuated_bytes = kMaxEvacuatedBytes; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 664 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | |
| 668 | void MarkCompactCollector::CollectEvacuationCandidates(PagedSpace* space) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 669 | DCHECK(space->identity() == OLD_SPACE || space->identity() == CODE_SPACE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 670 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 671 | int number_of_pages = space->CountTotalPages(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 672 | int area_size = space->AreaSize(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 673 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 674 | // Pairs of (live_bytes_in_page, page). |
| 675 | typedef std::pair<int, Page*> LiveBytesPagePair; |
| 676 | std::vector<LiveBytesPagePair> pages; |
| 677 | pages.reserve(number_of_pages); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 678 | |
| 679 | PageIterator it(space); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 680 | while (it.has_next()) { |
| 681 | Page* p = it.next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 682 | if (p->NeverEvacuate()) continue; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 683 | if (p->IsFlagSet(Page::BLACK_PAGE)) continue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 684 | // Invariant: Evacuation candidates are just created when marking is |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 685 | // started. This means that sweeping has finished. Furthermore, at the end |
| 686 | // of a GC all evacuation candidates are cleared and their slot buffers are |
| 687 | // released. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 688 | CHECK(!p->IsEvacuationCandidate()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 689 | CHECK_NULL(p->old_to_old_slots()); |
| 690 | CHECK_NULL(p->typed_old_to_old_slots()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 691 | CHECK(p->SweepingDone()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 692 | DCHECK(p->area_size() == area_size); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 693 | pages.push_back(std::make_pair(p->LiveBytesFromFreeList(), p)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 694 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 695 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 696 | int candidate_count = 0; |
| 697 | int total_live_bytes = 0; |
| 698 | |
| 699 | const bool reduce_memory = heap()->ShouldReduceMemory(); |
| 700 | if (FLAG_manual_evacuation_candidates_selection) { |
| 701 | for (size_t i = 0; i < pages.size(); i++) { |
| 702 | Page* p = pages[i].second; |
| 703 | if (p->IsFlagSet(MemoryChunk::FORCE_EVACUATION_CANDIDATE_FOR_TESTING)) { |
| 704 | candidate_count++; |
| 705 | total_live_bytes += pages[i].first; |
| 706 | p->ClearFlag(MemoryChunk::FORCE_EVACUATION_CANDIDATE_FOR_TESTING); |
| 707 | AddEvacuationCandidate(p); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 710 | } else if (FLAG_stress_compaction) { |
| 711 | for (size_t i = 0; i < pages.size(); i++) { |
| 712 | Page* p = pages[i].second; |
| 713 | if (i % 2 == 0) { |
| 714 | candidate_count++; |
| 715 | total_live_bytes += pages[i].first; |
| 716 | AddEvacuationCandidate(p); |
| 717 | } |
| 718 | } |
| 719 | } else { |
| 720 | // The following approach determines the pages that should be evacuated. |
| 721 | // |
| 722 | // We use two conditions to decide whether a page qualifies as an evacuation |
| 723 | // candidate, or not: |
| 724 | // * Target fragmentation: How fragmented is a page, i.e., how is the ratio |
| 725 | // between live bytes and capacity of this page (= area). |
| 726 | // * Evacuation quota: A global quota determining how much bytes should be |
| 727 | // compacted. |
| 728 | // |
| 729 | // The algorithm sorts all pages by live bytes and then iterates through |
| 730 | // them starting with the page with the most free memory, adding them to the |
| 731 | // set of evacuation candidates as long as both conditions (fragmentation |
| 732 | // and quota) hold. |
| 733 | int max_evacuated_bytes; |
| 734 | int target_fragmentation_percent; |
| 735 | ComputeEvacuationHeuristics(area_size, &target_fragmentation_percent, |
| 736 | &max_evacuated_bytes); |
| 737 | |
| 738 | const intptr_t free_bytes_threshold = |
| 739 | target_fragmentation_percent * (area_size / 100); |
| 740 | |
| 741 | // Sort pages from the most free to the least free, then select |
| 742 | // the first n pages for evacuation such that: |
| 743 | // - the total size of evacuated objects does not exceed the specified |
| 744 | // limit. |
| 745 | // - fragmentation of (n+1)-th page does not exceed the specified limit. |
| 746 | std::sort(pages.begin(), pages.end(), |
| 747 | [](const LiveBytesPagePair& a, const LiveBytesPagePair& b) { |
| 748 | return a.first < b.first; |
| 749 | }); |
| 750 | for (size_t i = 0; i < pages.size(); i++) { |
| 751 | int live_bytes = pages[i].first; |
| 752 | int free_bytes = area_size - live_bytes; |
| 753 | if (FLAG_always_compact || |
| 754 | ((free_bytes >= free_bytes_threshold) && |
| 755 | ((total_live_bytes + live_bytes) <= max_evacuated_bytes))) { |
| 756 | candidate_count++; |
| 757 | total_live_bytes += live_bytes; |
| 758 | } |
| 759 | if (FLAG_trace_fragmentation_verbose) { |
| 760 | PrintIsolate(isolate(), |
| 761 | "compaction-selection-page: space=%s free_bytes_page=%d " |
| 762 | "fragmentation_limit_kb=%d fragmentation_limit_percent=%d " |
| 763 | "sum_compaction_kb=%d " |
| 764 | "compaction_limit_kb=%d\n", |
| 765 | AllocationSpaceName(space->identity()), free_bytes / KB, |
| 766 | free_bytes_threshold / KB, target_fragmentation_percent, |
| 767 | total_live_bytes / KB, max_evacuated_bytes / KB); |
| 768 | } |
| 769 | } |
| 770 | // How many pages we will allocated for the evacuated objects |
| 771 | // in the worst case: ceil(total_live_bytes / area_size) |
| 772 | int estimated_new_pages = (total_live_bytes + area_size - 1) / area_size; |
| 773 | DCHECK_LE(estimated_new_pages, candidate_count); |
| 774 | int estimated_released_pages = candidate_count - estimated_new_pages; |
| 775 | // Avoid (compact -> expand) cycles. |
| 776 | if ((estimated_released_pages == 0) && !FLAG_always_compact) { |
| 777 | candidate_count = 0; |
| 778 | } |
| 779 | for (int i = 0; i < candidate_count; i++) { |
| 780 | AddEvacuationCandidate(pages[i].second); |
| 781 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 784 | if (FLAG_trace_fragmentation) { |
| 785 | PrintIsolate(isolate(), |
| 786 | "compaction-selection: space=%s reduce_memory=%d pages=%d " |
| 787 | "total_live_bytes=%d\n", |
| 788 | AllocationSpaceName(space->identity()), reduce_memory, |
| 789 | candidate_count, total_live_bytes / KB); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
| 793 | |
| 794 | void MarkCompactCollector::AbortCompaction() { |
| 795 | if (compacting_) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 796 | RememberedSet<OLD_TO_OLD>::ClearAll(heap()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 797 | for (Page* p : evacuation_candidates_) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 798 | p->ClearEvacuationCandidate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 799 | } |
| 800 | compacting_ = false; |
| 801 | evacuation_candidates_.Rewind(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 802 | } |
| 803 | DCHECK_EQ(0, evacuation_candidates_.length()); |
| 804 | } |
| 805 | |
| 806 | |
| 807 | void MarkCompactCollector::Prepare() { |
| 808 | was_marked_incrementally_ = heap()->incremental_marking()->IsMarking(); |
| 809 | |
| 810 | #ifdef DEBUG |
| 811 | DCHECK(state_ == IDLE); |
| 812 | state_ = PREPARE_GC; |
| 813 | #endif |
| 814 | |
| 815 | DCHECK(!FLAG_never_compact || !FLAG_always_compact); |
| 816 | |
| 817 | if (sweeping_in_progress()) { |
| 818 | // Instead of waiting we could also abort the sweeper threads here. |
| 819 | EnsureSweepingCompleted(); |
| 820 | } |
| 821 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 822 | // If concurrent unmapping tasks are still running, we should wait for |
| 823 | // them here. |
| 824 | heap()->WaitUntilUnmappingOfFreeChunksCompleted(); |
| 825 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 826 | // Clear marking bits if incremental marking is aborted. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 827 | if (was_marked_incrementally_ && heap_->ShouldAbortIncrementalMarking()) { |
| 828 | heap()->incremental_marking()->Stop(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 829 | ClearMarkbits(); |
| 830 | AbortWeakCollections(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 831 | AbortWeakCells(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 832 | AbortTransitionArrays(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 833 | AbortCompaction(); |
| 834 | was_marked_incrementally_ = false; |
| 835 | } |
| 836 | |
| 837 | // Don't start compaction if we are in the middle of incremental |
| 838 | // marking cycle. We did not collect any slots. |
| 839 | if (!FLAG_never_compact && !was_marked_incrementally_) { |
| 840 | StartCompaction(NON_INCREMENTAL_COMPACTION); |
| 841 | } |
| 842 | |
| 843 | PagedSpaces spaces(heap()); |
| 844 | for (PagedSpace* space = spaces.next(); space != NULL; |
| 845 | space = spaces.next()) { |
| 846 | space->PrepareForMarkCompact(); |
| 847 | } |
| 848 | |
| 849 | #ifdef VERIFY_HEAP |
| 850 | if (!was_marked_incrementally_ && FLAG_verify_heap) { |
| 851 | VerifyMarkbitsAreClean(); |
| 852 | } |
| 853 | #endif |
| 854 | } |
| 855 | |
| 856 | |
| 857 | void MarkCompactCollector::Finish() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 858 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_FINISH); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 859 | |
| 860 | // The hashing of weak_object_to_code_table is no longer valid. |
| 861 | heap()->weak_object_to_code_table()->Rehash( |
| 862 | heap()->isolate()->factory()->undefined_value()); |
| 863 | |
| 864 | // Clear the marking state of live large objects. |
| 865 | heap_->lo_space()->ClearMarkingStateOfLiveObjects(); |
| 866 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 867 | #ifdef DEBUG |
| 868 | DCHECK(state_ == SWEEP_SPACES || state_ == RELOCATE_OBJECTS); |
| 869 | state_ = IDLE; |
| 870 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 871 | heap_->isolate()->inner_pointer_to_code_cache()->Flush(); |
| 872 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 873 | // The stub cache is not traversed during GC; clear the cache to |
| 874 | // force lazy re-initialization of it. This must be done after the |
| 875 | // GC, because it relies on the new address of certain old space |
| 876 | // objects (empty string, illegal builtin). |
| 877 | isolate()->stub_cache()->Clear(); |
| 878 | |
| 879 | if (have_code_to_deoptimize_) { |
| 880 | // Some code objects were marked for deoptimization during the GC. |
| 881 | Deoptimizer::DeoptimizeMarkedCode(isolate()); |
| 882 | have_code_to_deoptimize_ = false; |
| 883 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 884 | |
| 885 | heap_->incremental_marking()->ClearIdleMarkingDelayCounter(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 886 | |
| 887 | if (marking_parity_ == EVEN_MARKING_PARITY) { |
| 888 | marking_parity_ = ODD_MARKING_PARITY; |
| 889 | } else { |
| 890 | DCHECK(marking_parity_ == ODD_MARKING_PARITY); |
| 891 | marking_parity_ = EVEN_MARKING_PARITY; |
| 892 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | |
| 896 | // ------------------------------------------------------------------------- |
| 897 | // Phase 1: tracing and marking live objects. |
| 898 | // before: all objects are in normal state. |
| 899 | // after: a live object's map pointer is marked as '00'. |
| 900 | |
| 901 | // Marking all live objects in the heap as part of mark-sweep or mark-compact |
| 902 | // collection. Before marking, all objects are in their normal state. After |
| 903 | // marking, live objects' map pointers are marked indicating that the object |
| 904 | // has been found reachable. |
| 905 | // |
| 906 | // The marking algorithm is a (mostly) depth-first (because of possible stack |
| 907 | // overflow) traversal of the graph of objects reachable from the roots. It |
| 908 | // uses an explicit stack of pointers rather than recursion. The young |
| 909 | // generation's inactive ('from') space is used as a marking stack. The |
| 910 | // objects in the marking stack are the ones that have been reached and marked |
| 911 | // but their children have not yet been visited. |
| 912 | // |
| 913 | // The marking stack can overflow during traversal. In that case, we set an |
| 914 | // overflow flag. When the overflow flag is set, we continue marking objects |
| 915 | // reachable from the objects on the marking stack, but no longer push them on |
| 916 | // the marking stack. Instead, we mark them as both marked and overflowed. |
| 917 | // When the stack is in the overflowed state, objects marked as overflowed |
| 918 | // have been reached and marked but their children have not been visited yet. |
| 919 | // After emptying the marking stack, we clear the overflow flag and traverse |
| 920 | // the heap looking for objects marked as overflowed, push them on the stack, |
| 921 | // and continue with marking. This process repeats until all reachable |
| 922 | // objects have been marked. |
| 923 | |
| 924 | void CodeFlusher::ProcessJSFunctionCandidates() { |
| 925 | Code* lazy_compile = isolate_->builtins()->builtin(Builtins::kCompileLazy); |
| 926 | Object* undefined = isolate_->heap()->undefined_value(); |
| 927 | |
| 928 | JSFunction* candidate = jsfunction_candidates_head_; |
| 929 | JSFunction* next_candidate; |
| 930 | while (candidate != NULL) { |
| 931 | next_candidate = GetNextCandidate(candidate); |
| 932 | ClearNextCandidate(candidate, undefined); |
| 933 | |
| 934 | SharedFunctionInfo* shared = candidate->shared(); |
| 935 | |
| 936 | Code* code = shared->code(); |
| 937 | MarkBit code_mark = Marking::MarkBitFrom(code); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 938 | if (Marking::IsWhite(code_mark)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 939 | if (FLAG_trace_code_flushing && shared->is_compiled()) { |
| 940 | PrintF("[code-flushing clears: "); |
| 941 | shared->ShortPrint(); |
| 942 | PrintF(" - age: %d]\n", code->GetAge()); |
| 943 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 944 | // Always flush the optimized code map if there is one. |
| 945 | if (!shared->OptimizedCodeMapIsCleared()) { |
| 946 | shared->ClearOptimizedCodeMap(); |
| 947 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 948 | shared->set_code(lazy_compile); |
| 949 | candidate->set_code(lazy_compile); |
| 950 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 951 | DCHECK(Marking::IsBlack(code_mark)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 952 | candidate->set_code(code); |
| 953 | } |
| 954 | |
| 955 | // We are in the middle of a GC cycle so the write barrier in the code |
| 956 | // setter did not record the slot update and we have to do that manually. |
| 957 | Address slot = candidate->address() + JSFunction::kCodeEntryOffset; |
| 958 | Code* target = Code::cast(Code::GetObjectFromEntryAddress(slot)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 959 | isolate_->heap()->mark_compact_collector()->RecordCodeEntrySlot( |
| 960 | candidate, slot, target); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 961 | |
| 962 | Object** shared_code_slot = |
| 963 | HeapObject::RawField(shared, SharedFunctionInfo::kCodeOffset); |
| 964 | isolate_->heap()->mark_compact_collector()->RecordSlot( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 965 | shared, shared_code_slot, *shared_code_slot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 966 | |
| 967 | candidate = next_candidate; |
| 968 | } |
| 969 | |
| 970 | jsfunction_candidates_head_ = NULL; |
| 971 | } |
| 972 | |
| 973 | |
| 974 | void CodeFlusher::ProcessSharedFunctionInfoCandidates() { |
| 975 | Code* lazy_compile = isolate_->builtins()->builtin(Builtins::kCompileLazy); |
| 976 | |
| 977 | SharedFunctionInfo* candidate = shared_function_info_candidates_head_; |
| 978 | SharedFunctionInfo* next_candidate; |
| 979 | while (candidate != NULL) { |
| 980 | next_candidate = GetNextCandidate(candidate); |
| 981 | ClearNextCandidate(candidate); |
| 982 | |
| 983 | Code* code = candidate->code(); |
| 984 | MarkBit code_mark = Marking::MarkBitFrom(code); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 985 | if (Marking::IsWhite(code_mark)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 986 | if (FLAG_trace_code_flushing && candidate->is_compiled()) { |
| 987 | PrintF("[code-flushing clears: "); |
| 988 | candidate->ShortPrint(); |
| 989 | PrintF(" - age: %d]\n", code->GetAge()); |
| 990 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 991 | // Always flush the optimized code map if there is one. |
| 992 | if (!candidate->OptimizedCodeMapIsCleared()) { |
| 993 | candidate->ClearOptimizedCodeMap(); |
| 994 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 995 | candidate->set_code(lazy_compile); |
| 996 | } |
| 997 | |
| 998 | Object** code_slot = |
| 999 | HeapObject::RawField(candidate, SharedFunctionInfo::kCodeOffset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1000 | isolate_->heap()->mark_compact_collector()->RecordSlot(candidate, code_slot, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1001 | *code_slot); |
| 1002 | |
| 1003 | candidate = next_candidate; |
| 1004 | } |
| 1005 | |
| 1006 | shared_function_info_candidates_head_ = NULL; |
| 1007 | } |
| 1008 | |
| 1009 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1010 | void CodeFlusher::EvictCandidate(SharedFunctionInfo* shared_info) { |
| 1011 | // Make sure previous flushing decisions are revisited. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1012 | isolate_->heap()->incremental_marking()->IterateBlackObject(shared_info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (FLAG_trace_code_flushing) { |
| 1015 | PrintF("[code-flushing abandons function-info: "); |
| 1016 | shared_info->ShortPrint(); |
| 1017 | PrintF("]\n"); |
| 1018 | } |
| 1019 | |
| 1020 | SharedFunctionInfo* candidate = shared_function_info_candidates_head_; |
| 1021 | SharedFunctionInfo* next_candidate; |
| 1022 | if (candidate == shared_info) { |
| 1023 | next_candidate = GetNextCandidate(shared_info); |
| 1024 | shared_function_info_candidates_head_ = next_candidate; |
| 1025 | ClearNextCandidate(shared_info); |
| 1026 | } else { |
| 1027 | while (candidate != NULL) { |
| 1028 | next_candidate = GetNextCandidate(candidate); |
| 1029 | |
| 1030 | if (next_candidate == shared_info) { |
| 1031 | next_candidate = GetNextCandidate(shared_info); |
| 1032 | SetNextCandidate(candidate, next_candidate); |
| 1033 | ClearNextCandidate(shared_info); |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | candidate = next_candidate; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | void CodeFlusher::EvictCandidate(JSFunction* function) { |
| 1044 | DCHECK(!function->next_function_link()->IsUndefined()); |
| 1045 | Object* undefined = isolate_->heap()->undefined_value(); |
| 1046 | |
| 1047 | // Make sure previous flushing decisions are revisited. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1048 | isolate_->heap()->incremental_marking()->IterateBlackObject(function); |
| 1049 | isolate_->heap()->incremental_marking()->IterateBlackObject( |
| 1050 | function->shared()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1051 | |
| 1052 | if (FLAG_trace_code_flushing) { |
| 1053 | PrintF("[code-flushing abandons closure: "); |
| 1054 | function->shared()->ShortPrint(); |
| 1055 | PrintF("]\n"); |
| 1056 | } |
| 1057 | |
| 1058 | JSFunction* candidate = jsfunction_candidates_head_; |
| 1059 | JSFunction* next_candidate; |
| 1060 | if (candidate == function) { |
| 1061 | next_candidate = GetNextCandidate(function); |
| 1062 | jsfunction_candidates_head_ = next_candidate; |
| 1063 | ClearNextCandidate(function, undefined); |
| 1064 | } else { |
| 1065 | while (candidate != NULL) { |
| 1066 | next_candidate = GetNextCandidate(candidate); |
| 1067 | |
| 1068 | if (next_candidate == function) { |
| 1069 | next_candidate = GetNextCandidate(function); |
| 1070 | SetNextCandidate(candidate, next_candidate); |
| 1071 | ClearNextCandidate(function, undefined); |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | candidate = next_candidate; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1081 | void CodeFlusher::IteratePointersToFromSpace(ObjectVisitor* v) { |
| 1082 | Heap* heap = isolate_->heap(); |
| 1083 | |
| 1084 | JSFunction** slot = &jsfunction_candidates_head_; |
| 1085 | JSFunction* candidate = jsfunction_candidates_head_; |
| 1086 | while (candidate != NULL) { |
| 1087 | if (heap->InFromSpace(candidate)) { |
| 1088 | v->VisitPointer(reinterpret_cast<Object**>(slot)); |
| 1089 | } |
| 1090 | candidate = GetNextCandidate(*slot); |
| 1091 | slot = GetNextCandidateSlot(*slot); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1096 | class MarkCompactMarkingVisitor |
| 1097 | : public StaticMarkingVisitor<MarkCompactMarkingVisitor> { |
| 1098 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1099 | static void Initialize(); |
| 1100 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1101 | INLINE(static void VisitPointer(Heap* heap, HeapObject* object, Object** p)) { |
| 1102 | MarkObjectByPointer(heap->mark_compact_collector(), object, p); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1105 | INLINE(static void VisitPointers(Heap* heap, HeapObject* object, |
| 1106 | Object** start, Object** end)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1107 | // Mark all objects pointed to in [start, end). |
| 1108 | const int kMinRangeForMarkingRecursion = 64; |
| 1109 | if (end - start >= kMinRangeForMarkingRecursion) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1110 | if (VisitUnmarkedObjects(heap, object, start, end)) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1111 | // We are close to a stack overflow, so just mark the objects. |
| 1112 | } |
| 1113 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
| 1114 | for (Object** p = start; p < end; p++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1115 | MarkObjectByPointer(collector, object, p); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | // Marks the object black and pushes it on the marking stack. |
| 1120 | INLINE(static void MarkObject(Heap* heap, HeapObject* object)) { |
| 1121 | MarkBit mark = Marking::MarkBitFrom(object); |
| 1122 | heap->mark_compact_collector()->MarkObject(object, mark); |
| 1123 | } |
| 1124 | |
| 1125 | // Marks the object black without pushing it on the marking stack. |
| 1126 | // Returns true if object needed marking and false otherwise. |
| 1127 | INLINE(static bool MarkObjectWithoutPush(Heap* heap, HeapObject* object)) { |
| 1128 | MarkBit mark_bit = Marking::MarkBitFrom(object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1129 | if (Marking::IsWhite(mark_bit)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1130 | heap->mark_compact_collector()->SetMark(object, mark_bit); |
| 1131 | return true; |
| 1132 | } |
| 1133 | return false; |
| 1134 | } |
| 1135 | |
| 1136 | // Mark object pointed to by p. |
| 1137 | INLINE(static void MarkObjectByPointer(MarkCompactCollector* collector, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1138 | HeapObject* object, Object** p)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1139 | if (!(*p)->IsHeapObject()) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1140 | HeapObject* target_object = HeapObject::cast(*p); |
| 1141 | collector->RecordSlot(object, p, target_object); |
| 1142 | MarkBit mark = Marking::MarkBitFrom(target_object); |
| 1143 | collector->MarkObject(target_object, mark); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | // Visit an unmarked object. |
| 1148 | INLINE(static void VisitUnmarkedObject(MarkCompactCollector* collector, |
| 1149 | HeapObject* obj)) { |
| 1150 | #ifdef DEBUG |
| 1151 | DCHECK(collector->heap()->Contains(obj)); |
| 1152 | DCHECK(!collector->heap()->mark_compact_collector()->IsMarked(obj)); |
| 1153 | #endif |
| 1154 | Map* map = obj->map(); |
| 1155 | Heap* heap = obj->GetHeap(); |
| 1156 | MarkBit mark = Marking::MarkBitFrom(obj); |
| 1157 | heap->mark_compact_collector()->SetMark(obj, mark); |
| 1158 | // Mark the map pointer and the body. |
| 1159 | MarkBit map_mark = Marking::MarkBitFrom(map); |
| 1160 | heap->mark_compact_collector()->MarkObject(map, map_mark); |
| 1161 | IterateBody(map, obj); |
| 1162 | } |
| 1163 | |
| 1164 | // Visit all unmarked objects pointed to by [start, end). |
| 1165 | // Returns false if the operation fails (lack of stack space). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1166 | INLINE(static bool VisitUnmarkedObjects(Heap* heap, HeapObject* object, |
| 1167 | Object** start, Object** end)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1168 | // Return false is we are close to the stack limit. |
| 1169 | StackLimitCheck check(heap->isolate()); |
| 1170 | if (check.HasOverflowed()) return false; |
| 1171 | |
| 1172 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
| 1173 | // Visit the unmarked objects. |
| 1174 | for (Object** p = start; p < end; p++) { |
| 1175 | Object* o = *p; |
| 1176 | if (!o->IsHeapObject()) continue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1177 | collector->RecordSlot(object, p, o); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1178 | HeapObject* obj = HeapObject::cast(o); |
| 1179 | MarkBit mark = Marking::MarkBitFrom(obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1180 | if (Marking::IsBlackOrGrey(mark)) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1181 | VisitUnmarkedObject(collector, obj); |
| 1182 | } |
| 1183 | return true; |
| 1184 | } |
| 1185 | |
| 1186 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1187 | // Code flushing support. |
| 1188 | |
| 1189 | static const int kRegExpCodeThreshold = 5; |
| 1190 | |
| 1191 | static void UpdateRegExpCodeAgeAndFlush(Heap* heap, JSRegExp* re, |
| 1192 | bool is_one_byte) { |
| 1193 | // Make sure that the fixed array is in fact initialized on the RegExp. |
| 1194 | // We could potentially trigger a GC when initializing the RegExp. |
| 1195 | if (HeapObject::cast(re->data())->map()->instance_type() != |
| 1196 | FIXED_ARRAY_TYPE) |
| 1197 | return; |
| 1198 | |
| 1199 | // Make sure this is a RegExp that actually contains code. |
| 1200 | if (re->TypeTag() != JSRegExp::IRREGEXP) return; |
| 1201 | |
| 1202 | Object* code = re->DataAt(JSRegExp::code_index(is_one_byte)); |
| 1203 | if (!code->IsSmi() && |
| 1204 | HeapObject::cast(code)->map()->instance_type() == CODE_TYPE) { |
| 1205 | // Save a copy that can be reinstated if we need the code again. |
| 1206 | re->SetDataAt(JSRegExp::saved_code_index(is_one_byte), code); |
| 1207 | |
| 1208 | // Saving a copy might create a pointer into compaction candidate |
| 1209 | // that was not observed by marker. This might happen if JSRegExp data |
| 1210 | // was marked through the compilation cache before marker reached JSRegExp |
| 1211 | // object. |
| 1212 | FixedArray* data = FixedArray::cast(re->data()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1213 | if (Marking::IsBlackOrGrey(Marking::MarkBitFrom(data))) { |
| 1214 | Object** slot = |
| 1215 | data->data_start() + JSRegExp::saved_code_index(is_one_byte); |
| 1216 | heap->mark_compact_collector()->RecordSlot(data, slot, code); |
| 1217 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1218 | |
| 1219 | // Set a number in the 0-255 range to guarantee no smi overflow. |
| 1220 | re->SetDataAt(JSRegExp::code_index(is_one_byte), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1221 | Smi::FromInt(heap->ms_count() & 0xff)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1222 | } else if (code->IsSmi()) { |
| 1223 | int value = Smi::cast(code)->value(); |
| 1224 | // The regexp has not been compiled yet or there was a compilation error. |
| 1225 | if (value == JSRegExp::kUninitializedValue || |
| 1226 | value == JSRegExp::kCompilationErrorValue) { |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | // Check if we should flush now. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1231 | if (value == ((heap->ms_count() - kRegExpCodeThreshold) & 0xff)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1232 | re->SetDataAt(JSRegExp::code_index(is_one_byte), |
| 1233 | Smi::FromInt(JSRegExp::kUninitializedValue)); |
| 1234 | re->SetDataAt(JSRegExp::saved_code_index(is_one_byte), |
| 1235 | Smi::FromInt(JSRegExp::kUninitializedValue)); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | // Works by setting the current sweep_generation (as a smi) in the |
| 1242 | // code object place in the data array of the RegExp and keeps a copy |
| 1243 | // around that can be reinstated if we reuse the RegExp before flushing. |
| 1244 | // If we did not use the code for kRegExpCodeThreshold mark sweep GCs |
| 1245 | // we flush the code. |
| 1246 | static void VisitRegExpAndFlushCode(Map* map, HeapObject* object) { |
| 1247 | Heap* heap = map->GetHeap(); |
| 1248 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
| 1249 | if (!collector->is_code_flushing_enabled()) { |
| 1250 | VisitJSRegExp(map, object); |
| 1251 | return; |
| 1252 | } |
| 1253 | JSRegExp* re = reinterpret_cast<JSRegExp*>(object); |
| 1254 | // Flush code or set age on both one byte and two byte code. |
| 1255 | UpdateRegExpCodeAgeAndFlush(heap, re, true); |
| 1256 | UpdateRegExpCodeAgeAndFlush(heap, re, false); |
| 1257 | // Visit the fields of the RegExp, including the updated FixedArray. |
| 1258 | VisitJSRegExp(map, object); |
| 1259 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1260 | }; |
| 1261 | |
| 1262 | |
| 1263 | void MarkCompactMarkingVisitor::Initialize() { |
| 1264 | StaticMarkingVisitor<MarkCompactMarkingVisitor>::Initialize(); |
| 1265 | |
| 1266 | table_.Register(kVisitJSRegExp, &VisitRegExpAndFlushCode); |
| 1267 | |
| 1268 | if (FLAG_track_gc_object_stats) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1269 | ObjectStatsVisitor::Initialize(&table_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1274 | class CodeMarkingVisitor : public ThreadVisitor { |
| 1275 | public: |
| 1276 | explicit CodeMarkingVisitor(MarkCompactCollector* collector) |
| 1277 | : collector_(collector) {} |
| 1278 | |
| 1279 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
| 1280 | collector_->PrepareThreadForCodeFlushing(isolate, top); |
| 1281 | } |
| 1282 | |
| 1283 | private: |
| 1284 | MarkCompactCollector* collector_; |
| 1285 | }; |
| 1286 | |
| 1287 | |
| 1288 | class SharedFunctionInfoMarkingVisitor : public ObjectVisitor { |
| 1289 | public: |
| 1290 | explicit SharedFunctionInfoMarkingVisitor(MarkCompactCollector* collector) |
| 1291 | : collector_(collector) {} |
| 1292 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1293 | void VisitPointers(Object** start, Object** end) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1294 | for (Object** p = start; p < end; p++) VisitPointer(p); |
| 1295 | } |
| 1296 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1297 | void VisitPointer(Object** slot) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1298 | Object* obj = *slot; |
| 1299 | if (obj->IsSharedFunctionInfo()) { |
| 1300 | SharedFunctionInfo* shared = reinterpret_cast<SharedFunctionInfo*>(obj); |
| 1301 | MarkBit shared_mark = Marking::MarkBitFrom(shared); |
| 1302 | MarkBit code_mark = Marking::MarkBitFrom(shared->code()); |
| 1303 | collector_->MarkObject(shared->code(), code_mark); |
| 1304 | collector_->MarkObject(shared, shared_mark); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | private: |
| 1309 | MarkCompactCollector* collector_; |
| 1310 | }; |
| 1311 | |
| 1312 | |
| 1313 | void MarkCompactCollector::PrepareThreadForCodeFlushing(Isolate* isolate, |
| 1314 | ThreadLocalTop* top) { |
| 1315 | for (StackFrameIterator it(isolate, top); !it.done(); it.Advance()) { |
| 1316 | // Note: for the frame that has a pending lazy deoptimization |
| 1317 | // StackFrame::unchecked_code will return a non-optimized code object for |
| 1318 | // the outermost function and StackFrame::LookupCode will return |
| 1319 | // actual optimized code object. |
| 1320 | StackFrame* frame = it.frame(); |
| 1321 | Code* code = frame->unchecked_code(); |
| 1322 | MarkBit code_mark = Marking::MarkBitFrom(code); |
| 1323 | MarkObject(code, code_mark); |
| 1324 | if (frame->is_optimized()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1325 | Code* optimized_code = frame->LookupCode(); |
| 1326 | MarkBit optimized_code_mark = Marking::MarkBitFrom(optimized_code); |
| 1327 | MarkObject(optimized_code, optimized_code_mark); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | |
| 1333 | void MarkCompactCollector::PrepareForCodeFlushing() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1334 | // If code flushing is disabled, there is no need to prepare for it. |
| 1335 | if (!is_code_flushing_enabled()) return; |
| 1336 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1337 | // Make sure we are not referencing the code from the stack. |
| 1338 | DCHECK(this == heap()->mark_compact_collector()); |
| 1339 | PrepareThreadForCodeFlushing(heap()->isolate(), |
| 1340 | heap()->isolate()->thread_local_top()); |
| 1341 | |
| 1342 | // Iterate the archived stacks in all threads to check if |
| 1343 | // the code is referenced. |
| 1344 | CodeMarkingVisitor code_marking_visitor(this); |
| 1345 | heap()->isolate()->thread_manager()->IterateArchivedThreads( |
| 1346 | &code_marking_visitor); |
| 1347 | |
| 1348 | SharedFunctionInfoMarkingVisitor visitor(this); |
| 1349 | heap()->isolate()->compilation_cache()->IterateFunctions(&visitor); |
| 1350 | heap()->isolate()->handle_scope_implementer()->Iterate(&visitor); |
| 1351 | |
| 1352 | ProcessMarkingDeque(); |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | // Visitor class for marking heap roots. |
| 1357 | class RootMarkingVisitor : public ObjectVisitor { |
| 1358 | public: |
| 1359 | explicit RootMarkingVisitor(Heap* heap) |
| 1360 | : collector_(heap->mark_compact_collector()) {} |
| 1361 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1362 | void VisitPointer(Object** p) override { MarkObjectByPointer(p); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1363 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1364 | void VisitPointers(Object** start, Object** end) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1365 | for (Object** p = start; p < end; p++) MarkObjectByPointer(p); |
| 1366 | } |
| 1367 | |
| 1368 | // Skip the weak next code link in a code object, which is visited in |
| 1369 | // ProcessTopOptimizedFrame. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1370 | void VisitNextCodeLink(Object** p) override {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1371 | |
| 1372 | private: |
| 1373 | void MarkObjectByPointer(Object** p) { |
| 1374 | if (!(*p)->IsHeapObject()) return; |
| 1375 | |
| 1376 | // Replace flat cons strings in place. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1377 | HeapObject* object = HeapObject::cast(*p); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1378 | MarkBit mark_bit = Marking::MarkBitFrom(object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1379 | if (Marking::IsBlackOrGrey(mark_bit)) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1380 | |
| 1381 | Map* map = object->map(); |
| 1382 | // Mark the object. |
| 1383 | collector_->SetMark(object, mark_bit); |
| 1384 | |
| 1385 | // Mark the map pointer and body, and push them on the marking stack. |
| 1386 | MarkBit map_mark = Marking::MarkBitFrom(map); |
| 1387 | collector_->MarkObject(map, map_mark); |
| 1388 | MarkCompactMarkingVisitor::IterateBody(map, object); |
| 1389 | |
| 1390 | // Mark all the objects reachable from the map and body. May leave |
| 1391 | // overflowed objects in the heap. |
| 1392 | collector_->EmptyMarkingDeque(); |
| 1393 | } |
| 1394 | |
| 1395 | MarkCompactCollector* collector_; |
| 1396 | }; |
| 1397 | |
| 1398 | |
| 1399 | // Helper class for pruning the string table. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1400 | template <bool finalize_external_strings, bool record_slots> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1401 | class StringTableCleaner : public ObjectVisitor { |
| 1402 | public: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1403 | StringTableCleaner(Heap* heap, HeapObject* table) |
| 1404 | : heap_(heap), pointers_removed_(0), table_(table) { |
| 1405 | DCHECK(!record_slots || table != nullptr); |
| 1406 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1407 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1408 | void VisitPointers(Object** start, Object** end) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1409 | // Visit all HeapObject pointers in [start, end). |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1410 | MarkCompactCollector* collector = heap_->mark_compact_collector(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1411 | for (Object** p = start; p < end; p++) { |
| 1412 | Object* o = *p; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1413 | if (o->IsHeapObject()) { |
| 1414 | if (Marking::IsWhite(Marking::MarkBitFrom(HeapObject::cast(o)))) { |
| 1415 | if (finalize_external_strings) { |
| 1416 | DCHECK(o->IsExternalString()); |
| 1417 | heap_->FinalizeExternalString(String::cast(*p)); |
| 1418 | } else { |
| 1419 | pointers_removed_++; |
| 1420 | } |
| 1421 | // Set the entry to the_hole_value (as deleted). |
| 1422 | *p = heap_->the_hole_value(); |
| 1423 | } else if (record_slots) { |
| 1424 | // StringTable contains only old space strings. |
| 1425 | DCHECK(!heap_->InNewSpace(o)); |
| 1426 | collector->RecordSlot(table_, p, o); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1427 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | int PointersRemoved() { |
| 1433 | DCHECK(!finalize_external_strings); |
| 1434 | return pointers_removed_; |
| 1435 | } |
| 1436 | |
| 1437 | private: |
| 1438 | Heap* heap_; |
| 1439 | int pointers_removed_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1440 | HeapObject* table_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1441 | }; |
| 1442 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1443 | typedef StringTableCleaner<false, true> InternalizedStringTableCleaner; |
| 1444 | typedef StringTableCleaner<true, false> ExternalStringTableCleaner; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Implementation of WeakObjectRetainer for mark compact GCs. All marked objects |
| 1447 | // are retained. |
| 1448 | class MarkCompactWeakObjectRetainer : public WeakObjectRetainer { |
| 1449 | public: |
| 1450 | virtual Object* RetainAs(Object* object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1451 | MarkBit mark_bit = Marking::MarkBitFrom(HeapObject::cast(object)); |
| 1452 | DCHECK(!Marking::IsGrey(mark_bit)); |
| 1453 | if (Marking::IsBlack(mark_bit)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1454 | return object; |
| 1455 | } else if (object->IsAllocationSite() && |
| 1456 | !(AllocationSite::cast(object)->IsZombie())) { |
| 1457 | // "dead" AllocationSites need to live long enough for a traversal of new |
| 1458 | // space. These sites get a one-time reprieve. |
| 1459 | AllocationSite* site = AllocationSite::cast(object); |
| 1460 | site->MarkZombie(); |
| 1461 | site->GetHeap()->mark_compact_collector()->MarkAllocationSite(site); |
| 1462 | return object; |
| 1463 | } else { |
| 1464 | return NULL; |
| 1465 | } |
| 1466 | } |
| 1467 | }; |
| 1468 | |
| 1469 | |
| 1470 | // Fill the marking stack with overflowed objects returned by the given |
| 1471 | // iterator. Stop when the marking stack is filled or the end of the space |
| 1472 | // is reached, whichever comes first. |
| 1473 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1474 | void MarkCompactCollector::DiscoverGreyObjectsWithIterator(T* it) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1475 | // The caller should ensure that the marking stack is initially not full, |
| 1476 | // so that we don't waste effort pointlessly scanning for objects. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1477 | DCHECK(!marking_deque()->IsFull()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1478 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1479 | Map* filler_map = heap()->one_pointer_filler_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1480 | for (HeapObject* object = it->Next(); object != NULL; object = it->Next()) { |
| 1481 | MarkBit markbit = Marking::MarkBitFrom(object); |
| 1482 | if ((object->map() != filler_map) && Marking::IsGrey(markbit)) { |
| 1483 | Marking::GreyToBlack(markbit); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1484 | PushBlack(object); |
| 1485 | if (marking_deque()->IsFull()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1490 | void MarkCompactCollector::DiscoverGreyObjectsOnPage(MemoryChunk* p) { |
| 1491 | DCHECK(!marking_deque()->IsFull()); |
| 1492 | LiveObjectIterator<kGreyObjects> it(p); |
| 1493 | HeapObject* object = NULL; |
| 1494 | while ((object = it.Next()) != NULL) { |
| 1495 | MarkBit markbit = Marking::MarkBitFrom(object); |
| 1496 | DCHECK(Marking::IsGrey(markbit)); |
| 1497 | Marking::GreyToBlack(markbit); |
| 1498 | PushBlack(object); |
| 1499 | if (marking_deque()->IsFull()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1503 | class RecordMigratedSlotVisitor final : public ObjectVisitor { |
| 1504 | public: |
| 1505 | inline void VisitPointer(Object** p) final { |
| 1506 | RecordMigratedSlot(*p, reinterpret_cast<Address>(p)); |
| 1507 | } |
| 1508 | |
| 1509 | inline void VisitPointers(Object** start, Object** end) final { |
| 1510 | while (start < end) { |
| 1511 | RecordMigratedSlot(*start, reinterpret_cast<Address>(start)); |
| 1512 | ++start; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | inline void VisitCodeEntry(Address code_entry_slot) final { |
| 1517 | Address code_entry = Memory::Address_at(code_entry_slot); |
| 1518 | if (Page::FromAddress(code_entry)->IsEvacuationCandidate()) { |
| 1519 | RememberedSet<OLD_TO_OLD>::InsertTyped(Page::FromAddress(code_entry_slot), |
| 1520 | CODE_ENTRY_SLOT, code_entry_slot); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | private: |
| 1525 | inline void RecordMigratedSlot(Object* value, Address slot) { |
| 1526 | if (value->IsHeapObject()) { |
| 1527 | Page* p = Page::FromAddress(reinterpret_cast<Address>(value)); |
| 1528 | if (p->InNewSpace()) { |
| 1529 | RememberedSet<OLD_TO_NEW>::Insert(Page::FromAddress(slot), slot); |
| 1530 | } else if (p->IsEvacuationCandidate()) { |
| 1531 | RememberedSet<OLD_TO_OLD>::Insert(Page::FromAddress(slot), slot); |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1536 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1537 | class MarkCompactCollector::HeapObjectVisitor { |
| 1538 | public: |
| 1539 | virtual ~HeapObjectVisitor() {} |
| 1540 | virtual bool Visit(HeapObject* object) = 0; |
| 1541 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1542 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1543 | class MarkCompactCollector::EvacuateVisitorBase |
| 1544 | : public MarkCompactCollector::HeapObjectVisitor { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1545 | protected: |
| 1546 | enum MigrationMode { kFast, kProfiled }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1547 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1548 | EvacuateVisitorBase(Heap* heap, CompactionSpaceCollection* compaction_spaces) |
| 1549 | : heap_(heap), |
| 1550 | compaction_spaces_(compaction_spaces), |
| 1551 | profiling_( |
| 1552 | heap->isolate()->cpu_profiler()->is_profiling() || |
| 1553 | heap->isolate()->logger()->is_logging_code_events() || |
| 1554 | heap->isolate()->heap_profiler()->is_tracking_object_moves()) {} |
| 1555 | |
| 1556 | inline bool TryEvacuateObject(PagedSpace* target_space, HeapObject* object, |
| 1557 | HeapObject** target_object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1558 | int size = object->Size(); |
| 1559 | AllocationAlignment alignment = object->RequiredAlignment(); |
| 1560 | AllocationResult allocation = target_space->AllocateRaw(size, alignment); |
| 1561 | if (allocation.To(target_object)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1562 | MigrateObject(*target_object, object, size, target_space->identity()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1563 | return true; |
| 1564 | } |
| 1565 | return false; |
| 1566 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1567 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1568 | inline void MigrateObject(HeapObject* dst, HeapObject* src, int size, |
| 1569 | AllocationSpace dest) { |
| 1570 | if (profiling_) { |
| 1571 | MigrateObject<kProfiled>(dst, src, size, dest); |
| 1572 | } else { |
| 1573 | MigrateObject<kFast>(dst, src, size, dest); |
| 1574 | } |
| 1575 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1576 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1577 | template <MigrationMode mode> |
| 1578 | inline void MigrateObject(HeapObject* dst, HeapObject* src, int size, |
| 1579 | AllocationSpace dest) { |
| 1580 | Address dst_addr = dst->address(); |
| 1581 | Address src_addr = src->address(); |
| 1582 | DCHECK(heap_->AllowedToBeMigrated(src, dest)); |
| 1583 | DCHECK(dest != LO_SPACE); |
| 1584 | if (dest == OLD_SPACE) { |
| 1585 | DCHECK_OBJECT_SIZE(size); |
| 1586 | DCHECK(IsAligned(size, kPointerSize)); |
| 1587 | heap_->CopyBlock(dst_addr, src_addr, size); |
| 1588 | if ((mode == kProfiled) && FLAG_ignition && dst->IsBytecodeArray()) { |
| 1589 | PROFILE(heap_->isolate(), |
| 1590 | CodeMoveEvent(AbstractCode::cast(src), dst_addr)); |
| 1591 | } |
| 1592 | RecordMigratedSlotVisitor visitor; |
| 1593 | dst->IterateBodyFast(dst->map()->instance_type(), size, &visitor); |
| 1594 | } else if (dest == CODE_SPACE) { |
| 1595 | DCHECK_CODEOBJECT_SIZE(size, heap_->code_space()); |
| 1596 | if (mode == kProfiled) { |
| 1597 | PROFILE(heap_->isolate(), |
| 1598 | CodeMoveEvent(AbstractCode::cast(src), dst_addr)); |
| 1599 | } |
| 1600 | heap_->CopyBlock(dst_addr, src_addr, size); |
| 1601 | RememberedSet<OLD_TO_OLD>::InsertTyped(Page::FromAddress(dst_addr), |
| 1602 | RELOCATED_CODE_OBJECT, dst_addr); |
| 1603 | Code::cast(dst)->Relocate(dst_addr - src_addr); |
| 1604 | } else { |
| 1605 | DCHECK_OBJECT_SIZE(size); |
| 1606 | DCHECK(dest == NEW_SPACE); |
| 1607 | heap_->CopyBlock(dst_addr, src_addr, size); |
| 1608 | } |
| 1609 | if (mode == kProfiled) { |
| 1610 | heap_->OnMoveEvent(dst, src, size); |
| 1611 | } |
| 1612 | Memory::Address_at(src_addr) = dst_addr; |
| 1613 | } |
| 1614 | |
| 1615 | Heap* heap_; |
| 1616 | CompactionSpaceCollection* compaction_spaces_; |
| 1617 | bool profiling_; |
| 1618 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1619 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1620 | class MarkCompactCollector::EvacuateNewSpaceVisitor final |
| 1621 | : public MarkCompactCollector::EvacuateVisitorBase { |
| 1622 | public: |
| 1623 | static const intptr_t kLabSize = 4 * KB; |
| 1624 | static const intptr_t kMaxLabObjectSize = 256; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1625 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1626 | explicit EvacuateNewSpaceVisitor(Heap* heap, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1627 | CompactionSpaceCollection* compaction_spaces, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1628 | HashMap* local_pretenuring_feedback) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1629 | : EvacuateVisitorBase(heap, compaction_spaces), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1630 | buffer_(LocalAllocationBuffer::InvalidBuffer()), |
| 1631 | space_to_allocate_(NEW_SPACE), |
| 1632 | promoted_size_(0), |
| 1633 | semispace_copied_size_(0), |
| 1634 | local_pretenuring_feedback_(local_pretenuring_feedback) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1635 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1636 | bool Visit(HeapObject* object) override { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1637 | heap_->UpdateAllocationSite<Heap::kCached>(object, |
| 1638 | local_pretenuring_feedback_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1639 | int size = object->Size(); |
| 1640 | HeapObject* target_object = nullptr; |
| 1641 | if (heap_->ShouldBePromoted(object->address(), size) && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1642 | TryEvacuateObject(compaction_spaces_->Get(OLD_SPACE), object, |
| 1643 | &target_object)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1644 | // If we end up needing more special cases, we should factor this out. |
| 1645 | if (V8_UNLIKELY(target_object->IsJSArrayBuffer())) { |
| 1646 | heap_->array_buffer_tracker()->Promote( |
| 1647 | JSArrayBuffer::cast(target_object)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1648 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1649 | promoted_size_ += size; |
| 1650 | return true; |
| 1651 | } |
| 1652 | HeapObject* target = nullptr; |
| 1653 | AllocationSpace space = AllocateTargetObject(object, &target); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1654 | MigrateObject(HeapObject::cast(target), object, size, space); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1655 | if (V8_UNLIKELY(target->IsJSArrayBuffer())) { |
| 1656 | heap_->array_buffer_tracker()->MarkLive(JSArrayBuffer::cast(target)); |
| 1657 | } |
| 1658 | semispace_copied_size_ += size; |
| 1659 | return true; |
| 1660 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1661 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1662 | intptr_t promoted_size() { return promoted_size_; } |
| 1663 | intptr_t semispace_copied_size() { return semispace_copied_size_; } |
| 1664 | |
| 1665 | private: |
| 1666 | enum NewSpaceAllocationMode { |
| 1667 | kNonstickyBailoutOldSpace, |
| 1668 | kStickyBailoutOldSpace, |
| 1669 | }; |
| 1670 | |
| 1671 | inline AllocationSpace AllocateTargetObject(HeapObject* old_object, |
| 1672 | HeapObject** target_object) { |
| 1673 | const int size = old_object->Size(); |
| 1674 | AllocationAlignment alignment = old_object->RequiredAlignment(); |
| 1675 | AllocationResult allocation; |
| 1676 | if (space_to_allocate_ == NEW_SPACE) { |
| 1677 | if (size > kMaxLabObjectSize) { |
| 1678 | allocation = |
| 1679 | AllocateInNewSpace(size, alignment, kNonstickyBailoutOldSpace); |
| 1680 | } else { |
| 1681 | allocation = AllocateInLab(size, alignment); |
| 1682 | } |
| 1683 | } |
| 1684 | if (allocation.IsRetry() || (space_to_allocate_ == OLD_SPACE)) { |
| 1685 | allocation = AllocateInOldSpace(size, alignment); |
| 1686 | } |
| 1687 | bool ok = allocation.To(target_object); |
| 1688 | DCHECK(ok); |
| 1689 | USE(ok); |
| 1690 | return space_to_allocate_; |
| 1691 | } |
| 1692 | |
| 1693 | inline bool NewLocalAllocationBuffer() { |
| 1694 | AllocationResult result = |
| 1695 | AllocateInNewSpace(kLabSize, kWordAligned, kStickyBailoutOldSpace); |
| 1696 | LocalAllocationBuffer saved_old_buffer = buffer_; |
| 1697 | buffer_ = LocalAllocationBuffer::FromResult(heap_, result, kLabSize); |
| 1698 | if (buffer_.IsValid()) { |
| 1699 | buffer_.TryMerge(&saved_old_buffer); |
| 1700 | return true; |
| 1701 | } |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| 1705 | inline AllocationResult AllocateInNewSpace(int size_in_bytes, |
| 1706 | AllocationAlignment alignment, |
| 1707 | NewSpaceAllocationMode mode) { |
| 1708 | AllocationResult allocation = |
| 1709 | heap_->new_space()->AllocateRawSynchronized(size_in_bytes, alignment); |
| 1710 | if (allocation.IsRetry()) { |
| 1711 | if (!heap_->new_space()->AddFreshPageSynchronized()) { |
| 1712 | if (mode == kStickyBailoutOldSpace) space_to_allocate_ = OLD_SPACE; |
| 1713 | } else { |
| 1714 | allocation = heap_->new_space()->AllocateRawSynchronized(size_in_bytes, |
| 1715 | alignment); |
| 1716 | if (allocation.IsRetry()) { |
| 1717 | if (mode == kStickyBailoutOldSpace) space_to_allocate_ = OLD_SPACE; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1718 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1719 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1720 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1721 | return allocation; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1722 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1723 | |
| 1724 | inline AllocationResult AllocateInOldSpace(int size_in_bytes, |
| 1725 | AllocationAlignment alignment) { |
| 1726 | AllocationResult allocation = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1727 | compaction_spaces_->Get(OLD_SPACE)->AllocateRaw(size_in_bytes, |
| 1728 | alignment); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1729 | if (allocation.IsRetry()) { |
| 1730 | FatalProcessOutOfMemory( |
| 1731 | "MarkCompactCollector: semi-space copy, fallback in old gen\n"); |
| 1732 | } |
| 1733 | return allocation; |
| 1734 | } |
| 1735 | |
| 1736 | inline AllocationResult AllocateInLab(int size_in_bytes, |
| 1737 | AllocationAlignment alignment) { |
| 1738 | AllocationResult allocation; |
| 1739 | if (!buffer_.IsValid()) { |
| 1740 | if (!NewLocalAllocationBuffer()) { |
| 1741 | space_to_allocate_ = OLD_SPACE; |
| 1742 | return AllocationResult::Retry(OLD_SPACE); |
| 1743 | } |
| 1744 | } |
| 1745 | allocation = buffer_.AllocateRawAligned(size_in_bytes, alignment); |
| 1746 | if (allocation.IsRetry()) { |
| 1747 | if (!NewLocalAllocationBuffer()) { |
| 1748 | space_to_allocate_ = OLD_SPACE; |
| 1749 | return AllocationResult::Retry(OLD_SPACE); |
| 1750 | } else { |
| 1751 | allocation = buffer_.AllocateRawAligned(size_in_bytes, alignment); |
| 1752 | if (allocation.IsRetry()) { |
| 1753 | space_to_allocate_ = OLD_SPACE; |
| 1754 | return AllocationResult::Retry(OLD_SPACE); |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | return allocation; |
| 1759 | } |
| 1760 | |
| 1761 | LocalAllocationBuffer buffer_; |
| 1762 | AllocationSpace space_to_allocate_; |
| 1763 | intptr_t promoted_size_; |
| 1764 | intptr_t semispace_copied_size_; |
| 1765 | HashMap* local_pretenuring_feedback_; |
| 1766 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1767 | |
| 1768 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1769 | class MarkCompactCollector::EvacuateOldSpaceVisitor final |
| 1770 | : public MarkCompactCollector::EvacuateVisitorBase { |
| 1771 | public: |
| 1772 | EvacuateOldSpaceVisitor(Heap* heap, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1773 | CompactionSpaceCollection* compaction_spaces) |
| 1774 | : EvacuateVisitorBase(heap, compaction_spaces) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1775 | |
| 1776 | bool Visit(HeapObject* object) override { |
| 1777 | CompactionSpace* target_space = compaction_spaces_->Get( |
| 1778 | Page::FromAddress(object->address())->owner()->identity()); |
| 1779 | HeapObject* target_object = nullptr; |
| 1780 | if (TryEvacuateObject(target_space, object, &target_object)) { |
| 1781 | DCHECK(object->map_word().IsForwardingAddress()); |
| 1782 | return true; |
| 1783 | } |
| 1784 | return false; |
| 1785 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1786 | }; |
| 1787 | |
| 1788 | |
| 1789 | void MarkCompactCollector::DiscoverGreyObjectsInSpace(PagedSpace* space) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1790 | PageIterator it(space); |
| 1791 | while (it.has_next()) { |
| 1792 | Page* p = it.next(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1793 | if (!p->IsFlagSet(Page::BLACK_PAGE)) { |
| 1794 | DiscoverGreyObjectsOnPage(p); |
| 1795 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1796 | if (marking_deque()->IsFull()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1801 | void MarkCompactCollector::DiscoverGreyObjectsInNewSpace() { |
| 1802 | NewSpace* space = heap()->new_space(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1803 | NewSpacePageIterator it(space->bottom(), space->top()); |
| 1804 | while (it.has_next()) { |
| 1805 | NewSpacePage* page = it.next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1806 | DiscoverGreyObjectsOnPage(page); |
| 1807 | if (marking_deque()->IsFull()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | bool MarkCompactCollector::IsUnmarkedHeapObject(Object** p) { |
| 1813 | Object* o = *p; |
| 1814 | if (!o->IsHeapObject()) return false; |
| 1815 | HeapObject* heap_object = HeapObject::cast(o); |
| 1816 | MarkBit mark = Marking::MarkBitFrom(heap_object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1817 | return Marking::IsWhite(mark); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | bool MarkCompactCollector::IsUnmarkedHeapObjectWithHeap(Heap* heap, |
| 1822 | Object** p) { |
| 1823 | Object* o = *p; |
| 1824 | DCHECK(o->IsHeapObject()); |
| 1825 | HeapObject* heap_object = HeapObject::cast(o); |
| 1826 | MarkBit mark = Marking::MarkBitFrom(heap_object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1827 | return Marking::IsWhite(mark); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | |
| 1831 | void MarkCompactCollector::MarkStringTable(RootMarkingVisitor* visitor) { |
| 1832 | StringTable* string_table = heap()->string_table(); |
| 1833 | // Mark the string table itself. |
| 1834 | MarkBit string_table_mark = Marking::MarkBitFrom(string_table); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1835 | if (Marking::IsWhite(string_table_mark)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1836 | // String table could have already been marked by visiting the handles list. |
| 1837 | SetMark(string_table, string_table_mark); |
| 1838 | } |
| 1839 | // Explicitly mark the prefix. |
| 1840 | string_table->IteratePrefix(visitor); |
| 1841 | ProcessMarkingDeque(); |
| 1842 | } |
| 1843 | |
| 1844 | |
| 1845 | void MarkCompactCollector::MarkAllocationSite(AllocationSite* site) { |
| 1846 | MarkBit mark_bit = Marking::MarkBitFrom(site); |
| 1847 | SetMark(site, mark_bit); |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | void MarkCompactCollector::MarkRoots(RootMarkingVisitor* visitor) { |
| 1852 | // Mark the heap roots including global variables, stack variables, |
| 1853 | // etc., and all objects reachable from them. |
| 1854 | heap()->IterateStrongRoots(visitor, VISIT_ONLY_STRONG); |
| 1855 | |
| 1856 | // Handle the string table specially. |
| 1857 | MarkStringTable(visitor); |
| 1858 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1859 | // There may be overflowed objects in the heap. Visit them now. |
| 1860 | while (marking_deque_.overflowed()) { |
| 1861 | RefillMarkingDeque(); |
| 1862 | EmptyMarkingDeque(); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1867 | void MarkCompactCollector::MarkImplicitRefGroups( |
| 1868 | MarkObjectFunction mark_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1869 | List<ImplicitRefGroup*>* ref_groups = |
| 1870 | isolate()->global_handles()->implicit_ref_groups(); |
| 1871 | |
| 1872 | int last = 0; |
| 1873 | for (int i = 0; i < ref_groups->length(); i++) { |
| 1874 | ImplicitRefGroup* entry = ref_groups->at(i); |
| 1875 | DCHECK(entry != NULL); |
| 1876 | |
| 1877 | if (!IsMarked(*entry->parent)) { |
| 1878 | (*ref_groups)[last++] = entry; |
| 1879 | continue; |
| 1880 | } |
| 1881 | |
| 1882 | Object*** children = entry->children; |
| 1883 | // A parent object is marked, so mark all child heap objects. |
| 1884 | for (size_t j = 0; j < entry->length; ++j) { |
| 1885 | if ((*children[j])->IsHeapObject()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1886 | mark_object(heap(), HeapObject::cast(*children[j])); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | // Once the entire group has been marked, dispose it because it's |
| 1891 | // not needed anymore. |
| 1892 | delete entry; |
| 1893 | } |
| 1894 | ref_groups->Rewind(last); |
| 1895 | } |
| 1896 | |
| 1897 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1898 | // Mark all objects reachable from the objects on the marking stack. |
| 1899 | // Before: the marking stack contains zero or more heap object pointers. |
| 1900 | // After: the marking stack is empty, and all objects reachable from the |
| 1901 | // marking stack have been marked, or are overflowed in the heap. |
| 1902 | void MarkCompactCollector::EmptyMarkingDeque() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1903 | Map* filler_map = heap_->one_pointer_filler_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1904 | while (!marking_deque_.IsEmpty()) { |
| 1905 | HeapObject* object = marking_deque_.Pop(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1906 | // Explicitly skip one word fillers. Incremental markbit patterns are |
| 1907 | // correct only for objects that occupy at least two words. |
| 1908 | Map* map = object->map(); |
| 1909 | if (map == filler_map) continue; |
| 1910 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1911 | DCHECK(object->IsHeapObject()); |
| 1912 | DCHECK(heap()->Contains(object)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1913 | DCHECK(!Marking::IsWhite(Marking::MarkBitFrom(object))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1914 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1915 | MarkBit map_mark = Marking::MarkBitFrom(map); |
| 1916 | MarkObject(map, map_mark); |
| 1917 | |
| 1918 | MarkCompactMarkingVisitor::IterateBody(map, object); |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | // Sweep the heap for overflowed objects, clear their overflow bits, and |
| 1924 | // push them on the marking stack. Stop early if the marking stack fills |
| 1925 | // before sweeping completes. If sweeping completes, there are no remaining |
| 1926 | // overflowed objects in the heap so the overflow flag on the markings stack |
| 1927 | // is cleared. |
| 1928 | void MarkCompactCollector::RefillMarkingDeque() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1929 | isolate()->CountUsage(v8::Isolate::UseCounterFeature::kMarkDequeOverflow); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1930 | DCHECK(marking_deque_.overflowed()); |
| 1931 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1932 | DiscoverGreyObjectsInNewSpace(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1933 | if (marking_deque_.IsFull()) return; |
| 1934 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1935 | DiscoverGreyObjectsInSpace(heap()->old_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1936 | if (marking_deque_.IsFull()) return; |
| 1937 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1938 | DiscoverGreyObjectsInSpace(heap()->code_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1939 | if (marking_deque_.IsFull()) return; |
| 1940 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1941 | DiscoverGreyObjectsInSpace(heap()->map_space()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1942 | if (marking_deque_.IsFull()) return; |
| 1943 | |
| 1944 | LargeObjectIterator lo_it(heap()->lo_space()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1945 | DiscoverGreyObjectsWithIterator(&lo_it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1946 | if (marking_deque_.IsFull()) return; |
| 1947 | |
| 1948 | marking_deque_.ClearOverflowed(); |
| 1949 | } |
| 1950 | |
| 1951 | |
| 1952 | // Mark all objects reachable (transitively) from objects on the marking |
| 1953 | // stack. Before: the marking stack contains zero or more heap object |
| 1954 | // pointers. After: the marking stack is empty and there are no overflowed |
| 1955 | // objects in the heap. |
| 1956 | void MarkCompactCollector::ProcessMarkingDeque() { |
| 1957 | EmptyMarkingDeque(); |
| 1958 | while (marking_deque_.overflowed()) { |
| 1959 | RefillMarkingDeque(); |
| 1960 | EmptyMarkingDeque(); |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | |
| 1965 | // Mark all objects reachable (transitively) from objects on the marking |
| 1966 | // stack including references only considered in the atomic marking pause. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1967 | void MarkCompactCollector::ProcessEphemeralMarking( |
| 1968 | ObjectVisitor* visitor, bool only_process_harmony_weak_collections) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1969 | bool work_to_do = true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1970 | DCHECK(marking_deque_.IsEmpty() && !marking_deque_.overflowed()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1971 | while (work_to_do) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1972 | if (!only_process_harmony_weak_collections) { |
| 1973 | isolate()->global_handles()->IterateObjectGroups( |
| 1974 | visitor, &IsUnmarkedHeapObjectWithHeap); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1975 | MarkImplicitRefGroups(&MarkCompactMarkingVisitor::MarkObject); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1976 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1977 | ProcessWeakCollections(); |
| 1978 | work_to_do = !marking_deque_.IsEmpty(); |
| 1979 | ProcessMarkingDeque(); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | |
| 1984 | void MarkCompactCollector::ProcessTopOptimizedFrame(ObjectVisitor* visitor) { |
| 1985 | for (StackFrameIterator it(isolate(), isolate()->thread_local_top()); |
| 1986 | !it.done(); it.Advance()) { |
| 1987 | if (it.frame()->type() == StackFrame::JAVA_SCRIPT) { |
| 1988 | return; |
| 1989 | } |
| 1990 | if (it.frame()->type() == StackFrame::OPTIMIZED) { |
| 1991 | Code* code = it.frame()->LookupCode(); |
| 1992 | if (!code->CanDeoptAt(it.frame()->pc())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1993 | Code::BodyDescriptor::IterateBody(code, visitor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1994 | } |
| 1995 | ProcessMarkingDeque(); |
| 1996 | return; |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2002 | void MarkCompactCollector::EnsureMarkingDequeIsReserved() { |
| 2003 | DCHECK(!marking_deque_.in_use()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2004 | if (marking_deque_memory_ == NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2005 | marking_deque_memory_ = new base::VirtualMemory(kMaxMarkingDequeSize); |
| 2006 | marking_deque_memory_committed_ = 0; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2007 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2008 | if (marking_deque_memory_ == NULL) { |
| 2009 | V8::FatalProcessOutOfMemory("EnsureMarkingDequeIsReserved"); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | |
| 2014 | void MarkCompactCollector::EnsureMarkingDequeIsCommitted(size_t max_size) { |
| 2015 | // If the marking deque is too small, we try to allocate a bigger one. |
| 2016 | // If that fails, make do with a smaller one. |
| 2017 | CHECK(!marking_deque_.in_use()); |
| 2018 | for (size_t size = max_size; size >= kMinMarkingDequeSize; size >>= 1) { |
| 2019 | base::VirtualMemory* memory = marking_deque_memory_; |
| 2020 | size_t currently_committed = marking_deque_memory_committed_; |
| 2021 | |
| 2022 | if (currently_committed == size) return; |
| 2023 | |
| 2024 | if (currently_committed > size) { |
| 2025 | bool success = marking_deque_memory_->Uncommit( |
| 2026 | reinterpret_cast<Address>(marking_deque_memory_->address()) + size, |
| 2027 | currently_committed - size); |
| 2028 | if (success) { |
| 2029 | marking_deque_memory_committed_ = size; |
| 2030 | return; |
| 2031 | } |
| 2032 | UNREACHABLE(); |
| 2033 | } |
| 2034 | |
| 2035 | bool success = memory->Commit( |
| 2036 | reinterpret_cast<Address>(memory->address()) + currently_committed, |
| 2037 | size - currently_committed, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2038 | false); // Not executable. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2039 | if (success) { |
| 2040 | marking_deque_memory_committed_ = size; |
| 2041 | return; |
| 2042 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2043 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2044 | V8::FatalProcessOutOfMemory("EnsureMarkingDequeIsCommitted"); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | |
| 2048 | void MarkCompactCollector::InitializeMarkingDeque() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2049 | DCHECK(!marking_deque_.in_use()); |
| 2050 | DCHECK(marking_deque_memory_committed_ > 0); |
| 2051 | Address addr = static_cast<Address>(marking_deque_memory_->address()); |
| 2052 | size_t size = marking_deque_memory_committed_; |
| 2053 | if (FLAG_force_marking_deque_overflows) size = 64 * kPointerSize; |
| 2054 | marking_deque_.Initialize(addr, addr + size); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2058 | void MarkingDeque::Initialize(Address low, Address high) { |
| 2059 | DCHECK(!in_use_); |
| 2060 | HeapObject** obj_low = reinterpret_cast<HeapObject**>(low); |
| 2061 | HeapObject** obj_high = reinterpret_cast<HeapObject**>(high); |
| 2062 | array_ = obj_low; |
| 2063 | mask_ = base::bits::RoundDownToPowerOfTwo32( |
| 2064 | static_cast<uint32_t>(obj_high - obj_low)) - |
| 2065 | 1; |
| 2066 | top_ = bottom_ = 0; |
| 2067 | overflowed_ = false; |
| 2068 | in_use_ = true; |
| 2069 | } |
| 2070 | |
| 2071 | |
| 2072 | void MarkingDeque::Uninitialize(bool aborting) { |
| 2073 | if (!aborting) { |
| 2074 | DCHECK(IsEmpty()); |
| 2075 | DCHECK(!overflowed_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2076 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2077 | DCHECK(in_use_); |
| 2078 | top_ = bottom_ = 0xdecbad; |
| 2079 | in_use_ = false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2083 | void MarkCompactCollector::MarkLiveObjects() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2084 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_MARK); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2085 | double start_time = 0.0; |
| 2086 | if (FLAG_print_cumulative_gc_stat) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2087 | start_time = heap_->MonotonicallyIncreasingTimeInMs(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2088 | } |
| 2089 | // The recursive GC marker detects when it is nearing stack overflow, |
| 2090 | // and switches to a different marking system. JS interrupts interfere |
| 2091 | // with the C stack limit check. |
| 2092 | PostponeInterruptsScope postpone(isolate()); |
| 2093 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2094 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2095 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_MARK_FINISH_INCREMENTAL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2096 | IncrementalMarking* incremental_marking = heap_->incremental_marking(); |
| 2097 | if (was_marked_incrementally_) { |
| 2098 | incremental_marking->Finalize(); |
| 2099 | } else { |
| 2100 | // Abort any pending incremental activities e.g. incremental sweeping. |
| 2101 | incremental_marking->Stop(); |
| 2102 | if (marking_deque_.in_use()) { |
| 2103 | marking_deque_.Uninitialize(true); |
| 2104 | } |
| 2105 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | #ifdef DEBUG |
| 2109 | DCHECK(state_ == PREPARE_GC); |
| 2110 | state_ = MARK_LIVE_OBJECTS; |
| 2111 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2112 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2113 | EnsureMarkingDequeIsCommittedAndInitialize( |
| 2114 | MarkCompactCollector::kMaxMarkingDequeSize); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2115 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2116 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2117 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_MARK_PREPARE_CODE_FLUSH); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2118 | PrepareForCodeFlushing(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | RootMarkingVisitor root_visitor(heap()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2122 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2123 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2124 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_MARK_ROOTS); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2125 | MarkRoots(&root_visitor); |
| 2126 | ProcessTopOptimizedFrame(&root_visitor); |
| 2127 | } |
| 2128 | |
| 2129 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2130 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_MARK_WEAK_CLOSURE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2131 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2132 | // The objects reachable from the roots are marked, yet unreachable |
| 2133 | // objects are unmarked. Mark objects reachable due to host |
| 2134 | // application specific logic or through Harmony weak maps. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2135 | { |
| 2136 | TRACE_GC(heap()->tracer(), |
| 2137 | GCTracer::Scope::MC_MARK_WEAK_CLOSURE_EPHEMERAL); |
| 2138 | ProcessEphemeralMarking(&root_visitor, false); |
| 2139 | ProcessMarkingDeque(); |
| 2140 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2141 | |
| 2142 | // The objects reachable from the roots, weak maps or object groups |
| 2143 | // are marked. Objects pointed to only by weak global handles cannot be |
| 2144 | // immediately reclaimed. Instead, we have to mark them as pending and mark |
| 2145 | // objects reachable from them. |
| 2146 | // |
| 2147 | // First we identify nonlive weak handles and mark them as pending |
| 2148 | // destruction. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2149 | { |
| 2150 | TRACE_GC(heap()->tracer(), |
| 2151 | GCTracer::Scope::MC_MARK_WEAK_CLOSURE_WEAK_HANDLES); |
| 2152 | heap()->isolate()->global_handles()->IdentifyWeakHandles( |
| 2153 | &IsUnmarkedHeapObject); |
| 2154 | ProcessMarkingDeque(); |
| 2155 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2156 | // Then we mark the objects. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2157 | |
| 2158 | { |
| 2159 | TRACE_GC(heap()->tracer(), |
| 2160 | GCTracer::Scope::MC_MARK_WEAK_CLOSURE_WEAK_ROOTS); |
| 2161 | heap()->isolate()->global_handles()->IterateWeakRoots(&root_visitor); |
| 2162 | ProcessMarkingDeque(); |
| 2163 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2164 | |
| 2165 | // Repeat Harmony weak maps marking to mark unmarked objects reachable from |
| 2166 | // the weak roots we just marked as pending destruction. |
| 2167 | // |
| 2168 | // We only process harmony collections, as all object groups have been fully |
| 2169 | // processed and no weakly reachable node can discover new objects groups. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2170 | { |
| 2171 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_MARK_WEAK_CLOSURE_HARMONY); |
| 2172 | ProcessEphemeralMarking(&root_visitor, true); |
| 2173 | ProcessMarkingDeque(); |
| 2174 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2177 | if (FLAG_print_cumulative_gc_stat) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2178 | heap_->tracer()->AddMarkingTime(heap_->MonotonicallyIncreasingTimeInMs() - |
| 2179 | start_time); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2180 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2181 | if (FLAG_track_gc_object_stats) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2182 | if (FLAG_trace_gc_object_stats) { |
| 2183 | heap()->object_stats_->TraceObjectStats(); |
| 2184 | } |
| 2185 | heap()->object_stats_->CheckpointObjectStats(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2190 | void MarkCompactCollector::ClearNonLiveReferences() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2191 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2192 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2193 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2194 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_STRING_TABLE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2195 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2196 | // Prune the string table removing all strings only pointed to by the |
| 2197 | // string table. Cannot use string_table() here because the string |
| 2198 | // table is marked. |
| 2199 | StringTable* string_table = heap()->string_table(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2200 | InternalizedStringTableCleaner internalized_visitor(heap(), string_table); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2201 | string_table->IterateElements(&internalized_visitor); |
| 2202 | string_table->ElementsRemoved(internalized_visitor.PointersRemoved()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2203 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2204 | ExternalStringTableCleaner external_visitor(heap(), nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2205 | heap()->external_string_table_.Iterate(&external_visitor); |
| 2206 | heap()->external_string_table_.CleanUp(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2209 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2210 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_WEAK_LISTS); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2211 | // Process the weak references. |
| 2212 | MarkCompactWeakObjectRetainer mark_compact_object_retainer; |
| 2213 | heap()->ProcessAllWeakReferences(&mark_compact_object_retainer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2216 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2217 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_GLOBAL_HANDLES); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2218 | |
| 2219 | // Remove object groups after marking phase. |
| 2220 | heap()->isolate()->global_handles()->RemoveObjectGroups(); |
| 2221 | heap()->isolate()->global_handles()->RemoveImplicitRefGroups(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2224 | // Flush code from collected candidates. |
| 2225 | if (is_code_flushing_enabled()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2226 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_CODE_FLUSH); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2227 | code_flusher_->ProcessCandidates(); |
| 2228 | } |
| 2229 | |
| 2230 | |
| 2231 | DependentCode* dependent_code_list; |
| 2232 | Object* non_live_map_list; |
| 2233 | ClearWeakCells(&non_live_map_list, &dependent_code_list); |
| 2234 | |
| 2235 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2236 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_MAPS); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2237 | ClearSimpleMapTransitions(non_live_map_list); |
| 2238 | ClearFullMapTransitions(); |
| 2239 | } |
| 2240 | |
| 2241 | MarkDependentCodeForDeoptimization(dependent_code_list); |
| 2242 | |
| 2243 | ClearWeakCollections(); |
| 2244 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2245 | ClearInvalidRememberedSetSlots(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | |
| 2249 | void MarkCompactCollector::MarkDependentCodeForDeoptimization( |
| 2250 | DependentCode* list_head) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2251 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_DEPENDENT_CODE); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2252 | Isolate* isolate = this->isolate(); |
| 2253 | DependentCode* current = list_head; |
| 2254 | while (current->length() > 0) { |
| 2255 | have_code_to_deoptimize_ |= current->MarkCodeForDeoptimization( |
| 2256 | isolate, DependentCode::kWeakCodeGroup); |
| 2257 | current = current->next_link(); |
| 2258 | } |
| 2259 | |
| 2260 | WeakHashTable* table = heap_->weak_object_to_code_table(); |
| 2261 | uint32_t capacity = table->Capacity(); |
| 2262 | for (uint32_t i = 0; i < capacity; i++) { |
| 2263 | uint32_t key_index = table->EntryToIndex(i); |
| 2264 | Object* key = table->get(key_index); |
| 2265 | if (!table->IsKey(key)) continue; |
| 2266 | uint32_t value_index = table->EntryToValueIndex(i); |
| 2267 | Object* value = table->get(value_index); |
| 2268 | DCHECK(key->IsWeakCell()); |
| 2269 | if (WeakCell::cast(key)->cleared()) { |
| 2270 | have_code_to_deoptimize_ |= |
| 2271 | DependentCode::cast(value)->MarkCodeForDeoptimization( |
| 2272 | isolate, DependentCode::kWeakCodeGroup); |
| 2273 | table->set(key_index, heap_->the_hole_value()); |
| 2274 | table->set(value_index, heap_->the_hole_value()); |
| 2275 | table->ElementRemoved(); |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | |
| 2281 | void MarkCompactCollector::ClearSimpleMapTransitions( |
| 2282 | Object* non_live_map_list) { |
| 2283 | Object* the_hole_value = heap()->the_hole_value(); |
| 2284 | Object* weak_cell_obj = non_live_map_list; |
| 2285 | while (weak_cell_obj != Smi::FromInt(0)) { |
| 2286 | WeakCell* weak_cell = WeakCell::cast(weak_cell_obj); |
| 2287 | Map* map = Map::cast(weak_cell->value()); |
| 2288 | DCHECK(Marking::IsWhite(Marking::MarkBitFrom(map))); |
| 2289 | Object* potential_parent = map->constructor_or_backpointer(); |
| 2290 | if (potential_parent->IsMap()) { |
| 2291 | Map* parent = Map::cast(potential_parent); |
| 2292 | if (Marking::IsBlackOrGrey(Marking::MarkBitFrom(parent)) && |
| 2293 | parent->raw_transitions() == weak_cell) { |
| 2294 | ClearSimpleMapTransition(parent, map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2295 | } |
| 2296 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2297 | weak_cell->clear(); |
| 2298 | weak_cell_obj = weak_cell->next(); |
| 2299 | weak_cell->clear_next(the_hole_value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2304 | void MarkCompactCollector::ClearSimpleMapTransition(Map* map, |
| 2305 | Map* dead_transition) { |
| 2306 | // A previously existing simple transition (stored in a WeakCell) is going |
| 2307 | // to be cleared. Clear the useless cell pointer, and take ownership |
| 2308 | // of the descriptor array. |
| 2309 | map->set_raw_transitions(Smi::FromInt(0)); |
| 2310 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2311 | DescriptorArray* descriptors = map->instance_descriptors(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2312 | if (descriptors == dead_transition->instance_descriptors() && |
| 2313 | number_of_own_descriptors > 0) { |
| 2314 | TrimDescriptorArray(map, descriptors); |
| 2315 | DCHECK(descriptors->number_of_descriptors() == number_of_own_descriptors); |
| 2316 | map->set_owns_descriptors(true); |
| 2317 | } |
| 2318 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2319 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2320 | |
| 2321 | void MarkCompactCollector::ClearFullMapTransitions() { |
| 2322 | HeapObject* undefined = heap()->undefined_value(); |
| 2323 | Object* obj = heap()->encountered_transition_arrays(); |
| 2324 | while (obj != Smi::FromInt(0)) { |
| 2325 | TransitionArray* array = TransitionArray::cast(obj); |
| 2326 | int num_transitions = array->number_of_entries(); |
| 2327 | DCHECK_EQ(TransitionArray::NumberOfTransitions(array), num_transitions); |
| 2328 | if (num_transitions > 0) { |
| 2329 | Map* map = array->GetTarget(0); |
| 2330 | Map* parent = Map::cast(map->constructor_or_backpointer()); |
| 2331 | bool parent_is_alive = |
| 2332 | Marking::IsBlackOrGrey(Marking::MarkBitFrom(parent)); |
| 2333 | DescriptorArray* descriptors = |
| 2334 | parent_is_alive ? parent->instance_descriptors() : nullptr; |
| 2335 | bool descriptors_owner_died = |
| 2336 | CompactTransitionArray(parent, array, descriptors); |
| 2337 | if (descriptors_owner_died) { |
| 2338 | TrimDescriptorArray(parent, descriptors); |
| 2339 | } |
| 2340 | } |
| 2341 | obj = array->next_link(); |
| 2342 | array->set_next_link(undefined, SKIP_WRITE_BARRIER); |
| 2343 | } |
| 2344 | heap()->set_encountered_transition_arrays(Smi::FromInt(0)); |
| 2345 | } |
| 2346 | |
| 2347 | |
| 2348 | bool MarkCompactCollector::CompactTransitionArray( |
| 2349 | Map* map, TransitionArray* transitions, DescriptorArray* descriptors) { |
| 2350 | int num_transitions = transitions->number_of_entries(); |
| 2351 | bool descriptors_owner_died = false; |
| 2352 | int transition_index = 0; |
| 2353 | // Compact all live transitions to the left. |
| 2354 | for (int i = 0; i < num_transitions; ++i) { |
| 2355 | Map* target = transitions->GetTarget(i); |
| 2356 | DCHECK_EQ(target->constructor_or_backpointer(), map); |
| 2357 | if (Marking::IsWhite(Marking::MarkBitFrom(target))) { |
| 2358 | if (descriptors != nullptr && |
| 2359 | target->instance_descriptors() == descriptors) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2360 | descriptors_owner_died = true; |
| 2361 | } |
| 2362 | } else { |
| 2363 | if (i != transition_index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2364 | Name* key = transitions->GetKey(i); |
| 2365 | transitions->SetKey(transition_index, key); |
| 2366 | Object** key_slot = transitions->GetKeySlot(transition_index); |
| 2367 | RecordSlot(transitions, key_slot, key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2368 | // Target slots do not need to be recorded since maps are not compacted. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2369 | transitions->SetTarget(transition_index, transitions->GetTarget(i)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2370 | } |
| 2371 | transition_index++; |
| 2372 | } |
| 2373 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2374 | // If there are no transitions to be cleared, return. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2375 | if (transition_index == num_transitions) { |
| 2376 | DCHECK(!descriptors_owner_died); |
| 2377 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2378 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2379 | // Note that we never eliminate a transition array, though we might right-trim |
| 2380 | // such that number_of_transitions() == 0. If this assumption changes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2381 | // TransitionArray::Insert() will need to deal with the case that a transition |
| 2382 | // array disappeared during GC. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2383 | int trim = TransitionArray::Capacity(transitions) - transition_index; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2384 | if (trim > 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2385 | heap_->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>( |
| 2386 | transitions, trim * TransitionArray::kTransitionSize); |
| 2387 | transitions->SetNumberOfTransitions(transition_index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2388 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2389 | return descriptors_owner_died; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | |
| 2393 | void MarkCompactCollector::TrimDescriptorArray(Map* map, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2394 | DescriptorArray* descriptors) { |
| 2395 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 2396 | if (number_of_own_descriptors == 0) { |
| 2397 | DCHECK(descriptors == heap_->empty_descriptor_array()); |
| 2398 | return; |
| 2399 | } |
| 2400 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2401 | int number_of_descriptors = descriptors->number_of_descriptors_storage(); |
| 2402 | int to_trim = number_of_descriptors - number_of_own_descriptors; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2403 | if (to_trim > 0) { |
| 2404 | heap_->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>( |
| 2405 | descriptors, to_trim * DescriptorArray::kDescriptorSize); |
| 2406 | descriptors->SetNumberOfDescriptors(number_of_own_descriptors); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2407 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2408 | if (descriptors->HasEnumCache()) TrimEnumCache(map, descriptors); |
| 2409 | descriptors->Sort(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2410 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2411 | if (FLAG_unbox_double_fields) { |
| 2412 | LayoutDescriptor* layout_descriptor = map->layout_descriptor(); |
| 2413 | layout_descriptor = layout_descriptor->Trim(heap_, map, descriptors, |
| 2414 | number_of_own_descriptors); |
| 2415 | SLOW_DCHECK(layout_descriptor->IsConsistentWithMap(map, true)); |
| 2416 | } |
| 2417 | } |
| 2418 | DCHECK(descriptors->number_of_descriptors() == number_of_own_descriptors); |
| 2419 | map->set_owns_descriptors(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | |
| 2423 | void MarkCompactCollector::TrimEnumCache(Map* map, |
| 2424 | DescriptorArray* descriptors) { |
| 2425 | int live_enum = map->EnumLength(); |
| 2426 | if (live_enum == kInvalidEnumCacheSentinel) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2427 | live_enum = |
| 2428 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2429 | } |
| 2430 | if (live_enum == 0) return descriptors->ClearEnumCache(); |
| 2431 | |
| 2432 | FixedArray* enum_cache = descriptors->GetEnumCache(); |
| 2433 | |
| 2434 | int to_trim = enum_cache->length() - live_enum; |
| 2435 | if (to_trim <= 0) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2436 | heap_->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>( |
| 2437 | descriptors->GetEnumCache(), to_trim); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2438 | |
| 2439 | if (!descriptors->HasEnumIndicesCache()) return; |
| 2440 | FixedArray* enum_indices_cache = descriptors->GetEnumIndicesCache(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2441 | heap_->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(enum_indices_cache, |
| 2442 | to_trim); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | void MarkCompactCollector::ProcessWeakCollections() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2447 | Object* weak_collection_obj = heap()->encountered_weak_collections(); |
| 2448 | while (weak_collection_obj != Smi::FromInt(0)) { |
| 2449 | JSWeakCollection* weak_collection = |
| 2450 | reinterpret_cast<JSWeakCollection*>(weak_collection_obj); |
| 2451 | DCHECK(MarkCompactCollector::IsMarked(weak_collection)); |
| 2452 | if (weak_collection->table()->IsHashTable()) { |
| 2453 | ObjectHashTable* table = ObjectHashTable::cast(weak_collection->table()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2454 | for (int i = 0; i < table->Capacity(); i++) { |
| 2455 | if (MarkCompactCollector::IsMarked(HeapObject::cast(table->KeyAt(i)))) { |
| 2456 | Object** key_slot = |
| 2457 | table->RawFieldOfElementAt(ObjectHashTable::EntryToIndex(i)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2458 | RecordSlot(table, key_slot, *key_slot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2459 | Object** value_slot = |
| 2460 | table->RawFieldOfElementAt(ObjectHashTable::EntryToValueIndex(i)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2461 | MarkCompactMarkingVisitor::MarkObjectByPointer(this, table, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2462 | value_slot); |
| 2463 | } |
| 2464 | } |
| 2465 | } |
| 2466 | weak_collection_obj = weak_collection->next(); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | |
| 2471 | void MarkCompactCollector::ClearWeakCollections() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2472 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_CLEAR_WEAK_COLLECTIONS); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2473 | Object* weak_collection_obj = heap()->encountered_weak_collections(); |
| 2474 | while (weak_collection_obj != Smi::FromInt(0)) { |
| 2475 | JSWeakCollection* weak_collection = |
| 2476 | reinterpret_cast<JSWeakCollection*>(weak_collection_obj); |
| 2477 | DCHECK(MarkCompactCollector::IsMarked(weak_collection)); |
| 2478 | if (weak_collection->table()->IsHashTable()) { |
| 2479 | ObjectHashTable* table = ObjectHashTable::cast(weak_collection->table()); |
| 2480 | for (int i = 0; i < table->Capacity(); i++) { |
| 2481 | HeapObject* key = HeapObject::cast(table->KeyAt(i)); |
| 2482 | if (!MarkCompactCollector::IsMarked(key)) { |
| 2483 | table->RemoveEntry(i); |
| 2484 | } |
| 2485 | } |
| 2486 | } |
| 2487 | weak_collection_obj = weak_collection->next(); |
| 2488 | weak_collection->set_next(heap()->undefined_value()); |
| 2489 | } |
| 2490 | heap()->set_encountered_weak_collections(Smi::FromInt(0)); |
| 2491 | } |
| 2492 | |
| 2493 | |
| 2494 | void MarkCompactCollector::AbortWeakCollections() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2495 | Object* weak_collection_obj = heap()->encountered_weak_collections(); |
| 2496 | while (weak_collection_obj != Smi::FromInt(0)) { |
| 2497 | JSWeakCollection* weak_collection = |
| 2498 | reinterpret_cast<JSWeakCollection*>(weak_collection_obj); |
| 2499 | weak_collection_obj = weak_collection->next(); |
| 2500 | weak_collection->set_next(heap()->undefined_value()); |
| 2501 | } |
| 2502 | heap()->set_encountered_weak_collections(Smi::FromInt(0)); |
| 2503 | } |
| 2504 | |
| 2505 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2506 | void MarkCompactCollector::ClearWeakCells(Object** non_live_map_list, |
| 2507 | DependentCode** dependent_code_list) { |
| 2508 | Heap* heap = this->heap(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2509 | TRACE_GC(heap->tracer(), GCTracer::Scope::MC_CLEAR_WEAK_CELLS); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2510 | Object* weak_cell_obj = heap->encountered_weak_cells(); |
| 2511 | Object* the_hole_value = heap->the_hole_value(); |
| 2512 | DependentCode* dependent_code_head = |
| 2513 | DependentCode::cast(heap->empty_fixed_array()); |
| 2514 | Object* non_live_map_head = Smi::FromInt(0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2515 | while (weak_cell_obj != Smi::FromInt(0)) { |
| 2516 | WeakCell* weak_cell = reinterpret_cast<WeakCell*>(weak_cell_obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2517 | Object* next_weak_cell = weak_cell->next(); |
| 2518 | bool clear_value = true; |
| 2519 | bool clear_next = true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2520 | // We do not insert cleared weak cells into the list, so the value |
| 2521 | // cannot be a Smi here. |
| 2522 | HeapObject* value = HeapObject::cast(weak_cell->value()); |
| 2523 | if (!MarkCompactCollector::IsMarked(value)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2524 | // Cells for new-space objects embedded in optimized code are wrapped in |
| 2525 | // WeakCell and put into Heap::weak_object_to_code_table. |
| 2526 | // Such cells do not have any strong references but we want to keep them |
| 2527 | // alive as long as the cell value is alive. |
| 2528 | // TODO(ulan): remove this once we remove Heap::weak_object_to_code_table. |
| 2529 | if (value->IsCell()) { |
| 2530 | Object* cell_value = Cell::cast(value)->value(); |
| 2531 | if (cell_value->IsHeapObject() && |
| 2532 | MarkCompactCollector::IsMarked(HeapObject::cast(cell_value))) { |
| 2533 | // Resurrect the cell. |
| 2534 | MarkBit mark = Marking::MarkBitFrom(value); |
| 2535 | SetMark(value, mark); |
| 2536 | Object** slot = HeapObject::RawField(value, Cell::kValueOffset); |
| 2537 | RecordSlot(value, slot, *slot); |
| 2538 | slot = HeapObject::RawField(weak_cell, WeakCell::kValueOffset); |
| 2539 | RecordSlot(weak_cell, slot, *slot); |
| 2540 | clear_value = false; |
| 2541 | } |
| 2542 | } |
| 2543 | if (value->IsMap()) { |
| 2544 | // The map is non-live. |
| 2545 | Map* map = Map::cast(value); |
| 2546 | // Add dependent code to the dependent_code_list. |
| 2547 | DependentCode* candidate = map->dependent_code(); |
| 2548 | // We rely on the fact that the weak code group comes first. |
| 2549 | STATIC_ASSERT(DependentCode::kWeakCodeGroup == 0); |
| 2550 | if (candidate->length() > 0 && |
| 2551 | candidate->group() == DependentCode::kWeakCodeGroup) { |
| 2552 | candidate->set_next_link(dependent_code_head); |
| 2553 | dependent_code_head = candidate; |
| 2554 | } |
| 2555 | // Add the weak cell to the non_live_map list. |
| 2556 | weak_cell->set_next(non_live_map_head); |
| 2557 | non_live_map_head = weak_cell; |
| 2558 | clear_value = false; |
| 2559 | clear_next = false; |
| 2560 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2561 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2562 | // The value of the weak cell is alive. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2563 | Object** slot = HeapObject::RawField(weak_cell, WeakCell::kValueOffset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2564 | RecordSlot(weak_cell, slot, *slot); |
| 2565 | clear_value = false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2566 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2567 | if (clear_value) { |
| 2568 | weak_cell->clear(); |
| 2569 | } |
| 2570 | if (clear_next) { |
| 2571 | weak_cell->clear_next(the_hole_value); |
| 2572 | } |
| 2573 | weak_cell_obj = next_weak_cell; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2574 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2575 | heap->set_encountered_weak_cells(Smi::FromInt(0)); |
| 2576 | *non_live_map_list = non_live_map_head; |
| 2577 | *dependent_code_list = dependent_code_head; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | |
| 2581 | void MarkCompactCollector::AbortWeakCells() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2582 | Object* the_hole_value = heap()->the_hole_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2583 | Object* weak_cell_obj = heap()->encountered_weak_cells(); |
| 2584 | while (weak_cell_obj != Smi::FromInt(0)) { |
| 2585 | WeakCell* weak_cell = reinterpret_cast<WeakCell*>(weak_cell_obj); |
| 2586 | weak_cell_obj = weak_cell->next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2587 | weak_cell->clear_next(the_hole_value); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2588 | } |
| 2589 | heap()->set_encountered_weak_cells(Smi::FromInt(0)); |
| 2590 | } |
| 2591 | |
| 2592 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2593 | void MarkCompactCollector::AbortTransitionArrays() { |
| 2594 | HeapObject* undefined = heap()->undefined_value(); |
| 2595 | Object* obj = heap()->encountered_transition_arrays(); |
| 2596 | while (obj != Smi::FromInt(0)) { |
| 2597 | TransitionArray* array = TransitionArray::cast(obj); |
| 2598 | obj = array->next_link(); |
| 2599 | array->set_next_link(undefined, SKIP_WRITE_BARRIER); |
| 2600 | } |
| 2601 | heap()->set_encountered_transition_arrays(Smi::FromInt(0)); |
| 2602 | } |
| 2603 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2604 | static inline SlotType SlotTypeForRMode(RelocInfo::Mode rmode) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2605 | if (RelocInfo::IsCodeTarget(rmode)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2606 | return CODE_TARGET_SLOT; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2607 | } else if (RelocInfo::IsCell(rmode)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2608 | return CELL_TARGET_SLOT; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2609 | } else if (RelocInfo::IsEmbeddedObject(rmode)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2610 | return EMBEDDED_OBJECT_SLOT; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2611 | } else if (RelocInfo::IsDebugBreakSlot(rmode)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2612 | return DEBUG_TARGET_SLOT; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2613 | } |
| 2614 | UNREACHABLE(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2615 | return NUMBER_OF_SLOT_TYPES; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2618 | void MarkCompactCollector::RecordRelocSlot(Code* host, RelocInfo* rinfo, |
| 2619 | Object* target) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2620 | Page* target_page = Page::FromAddress(reinterpret_cast<Address>(target)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2621 | Page* source_page = Page::FromAddress(reinterpret_cast<Address>(host)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2622 | RelocInfo::Mode rmode = rinfo->rmode(); |
| 2623 | if (target_page->IsEvacuationCandidate() && |
| 2624 | (rinfo->host() == NULL || |
| 2625 | !ShouldSkipEvacuationSlotRecording(rinfo->host()))) { |
| 2626 | Address addr = rinfo->pc(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2627 | SlotType slot_type = SlotTypeForRMode(rmode); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2628 | if (rinfo->IsInConstantPool()) { |
| 2629 | addr = rinfo->constant_pool_entry_address(); |
| 2630 | if (RelocInfo::IsCodeTarget(rmode)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2631 | slot_type = CODE_ENTRY_SLOT; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2632 | } else { |
| 2633 | DCHECK(RelocInfo::IsEmbeddedObject(rmode)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2634 | slot_type = OBJECT_SLOT; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2635 | } |
| 2636 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2637 | RememberedSet<OLD_TO_OLD>::InsertTyped(source_page, slot_type, addr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2641 | static inline void UpdateTypedSlot(Isolate* isolate, ObjectVisitor* v, |
| 2642 | SlotType slot_type, Address addr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2643 | switch (slot_type) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2644 | case CODE_TARGET_SLOT: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2645 | RelocInfo rinfo(isolate, addr, RelocInfo::CODE_TARGET, 0, NULL); |
| 2646 | rinfo.Visit(isolate, v); |
| 2647 | break; |
| 2648 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2649 | case CELL_TARGET_SLOT: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2650 | RelocInfo rinfo(isolate, addr, RelocInfo::CELL, 0, NULL); |
| 2651 | rinfo.Visit(isolate, v); |
| 2652 | break; |
| 2653 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2654 | case CODE_ENTRY_SLOT: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2655 | v->VisitCodeEntry(addr); |
| 2656 | break; |
| 2657 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2658 | case RELOCATED_CODE_OBJECT: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2659 | HeapObject* obj = HeapObject::FromAddress(addr); |
| 2660 | Code::BodyDescriptor::IterateBody(obj, v); |
| 2661 | break; |
| 2662 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2663 | case DEBUG_TARGET_SLOT: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2664 | RelocInfo rinfo(isolate, addr, RelocInfo::DEBUG_BREAK_SLOT_AT_POSITION, 0, |
| 2665 | NULL); |
| 2666 | if (rinfo.IsPatchedDebugBreakSlotSequence()) rinfo.Visit(isolate, v); |
| 2667 | break; |
| 2668 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2669 | case EMBEDDED_OBJECT_SLOT: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2670 | RelocInfo rinfo(isolate, addr, RelocInfo::EMBEDDED_OBJECT, 0, NULL); |
| 2671 | rinfo.Visit(isolate, v); |
| 2672 | break; |
| 2673 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2674 | case OBJECT_SLOT: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2675 | v->VisitPointer(reinterpret_cast<Object**>(addr)); |
| 2676 | break; |
| 2677 | } |
| 2678 | default: |
| 2679 | UNREACHABLE(); |
| 2680 | break; |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2685 | // Visitor for updating pointers from live objects in old spaces to new space. |
| 2686 | // It does not expect to encounter pointers to dead objects. |
| 2687 | class PointersUpdatingVisitor : public ObjectVisitor { |
| 2688 | public: |
| 2689 | explicit PointersUpdatingVisitor(Heap* heap) : heap_(heap) {} |
| 2690 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2691 | void VisitPointer(Object** p) override { UpdatePointer(p); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2692 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2693 | void VisitPointers(Object** start, Object** end) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2694 | for (Object** p = start; p < end; p++) UpdatePointer(p); |
| 2695 | } |
| 2696 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2697 | void VisitCell(RelocInfo* rinfo) override { |
| 2698 | DCHECK(rinfo->rmode() == RelocInfo::CELL); |
| 2699 | Object* cell = rinfo->target_cell(); |
| 2700 | Object* old_cell = cell; |
| 2701 | VisitPointer(&cell); |
| 2702 | if (cell != old_cell) { |
| 2703 | rinfo->set_target_cell(reinterpret_cast<Cell*>(cell)); |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | void VisitEmbeddedPointer(RelocInfo* rinfo) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2708 | DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
| 2709 | Object* target = rinfo->target_object(); |
| 2710 | Object* old_target = target; |
| 2711 | VisitPointer(&target); |
| 2712 | // Avoid unnecessary changes that might unnecessary flush the instruction |
| 2713 | // cache. |
| 2714 | if (target != old_target) { |
| 2715 | rinfo->set_target_object(target); |
| 2716 | } |
| 2717 | } |
| 2718 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2719 | void VisitCodeTarget(RelocInfo* rinfo) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2720 | DCHECK(RelocInfo::IsCodeTarget(rinfo->rmode())); |
| 2721 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 2722 | Object* old_target = target; |
| 2723 | VisitPointer(&target); |
| 2724 | if (target != old_target) { |
| 2725 | rinfo->set_target_address(Code::cast(target)->instruction_start()); |
| 2726 | } |
| 2727 | } |
| 2728 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2729 | void VisitCodeAgeSequence(RelocInfo* rinfo) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2730 | DCHECK(RelocInfo::IsCodeAgeSequence(rinfo->rmode())); |
| 2731 | Object* stub = rinfo->code_age_stub(); |
| 2732 | DCHECK(stub != NULL); |
| 2733 | VisitPointer(&stub); |
| 2734 | if (stub != rinfo->code_age_stub()) { |
| 2735 | rinfo->set_code_age_stub(Code::cast(stub)); |
| 2736 | } |
| 2737 | } |
| 2738 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2739 | void VisitDebugTarget(RelocInfo* rinfo) override { |
| 2740 | DCHECK(RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 2741 | rinfo->IsPatchedDebugBreakSlotSequence()); |
| 2742 | Object* target = |
| 2743 | Code::GetCodeFromTargetAddress(rinfo->debug_call_address()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2744 | VisitPointer(&target); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2745 | rinfo->set_debug_call_address(Code::cast(target)->instruction_start()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static inline void UpdateSlot(Heap* heap, Object** slot) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2749 | Object* obj = reinterpret_cast<Object*>( |
| 2750 | base::NoBarrier_Load(reinterpret_cast<base::AtomicWord*>(slot))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2751 | |
| 2752 | if (!obj->IsHeapObject()) return; |
| 2753 | |
| 2754 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 2755 | |
| 2756 | MapWord map_word = heap_obj->map_word(); |
| 2757 | if (map_word.IsForwardingAddress()) { |
| 2758 | DCHECK(heap->InFromSpace(heap_obj) || |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2759 | MarkCompactCollector::IsOnEvacuationCandidate(heap_obj) || |
| 2760 | Page::FromAddress(heap_obj->address()) |
| 2761 | ->IsFlagSet(Page::COMPACTION_WAS_ABORTED)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2762 | HeapObject* target = map_word.ToForwardingAddress(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2763 | base::NoBarrier_CompareAndSwap( |
| 2764 | reinterpret_cast<base::AtomicWord*>(slot), |
| 2765 | reinterpret_cast<base::AtomicWord>(obj), |
| 2766 | reinterpret_cast<base::AtomicWord>(target)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2767 | DCHECK(!heap->InFromSpace(target) && |
| 2768 | !MarkCompactCollector::IsOnEvacuationCandidate(target)); |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | private: |
| 2773 | inline void UpdatePointer(Object** p) { UpdateSlot(heap_, p); } |
| 2774 | |
| 2775 | Heap* heap_; |
| 2776 | }; |
| 2777 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2778 | static String* UpdateReferenceInExternalStringTableEntry(Heap* heap, |
| 2779 | Object** p) { |
| 2780 | MapWord map_word = HeapObject::cast(*p)->map_word(); |
| 2781 | |
| 2782 | if (map_word.IsForwardingAddress()) { |
| 2783 | return String::cast(map_word.ToForwardingAddress()); |
| 2784 | } |
| 2785 | |
| 2786 | return String::cast(*p); |
| 2787 | } |
| 2788 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2789 | bool MarkCompactCollector::IsSlotInBlackObject(MemoryChunk* p, Address slot) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2790 | Space* owner = p->owner(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2791 | DCHECK(owner != heap_->lo_space() && owner != nullptr); |
| 2792 | USE(owner); |
| 2793 | |
| 2794 | // If we are on a black page, we cannot find the actual object start |
| 2795 | // easiliy. We just return true but do not set the out_object. |
| 2796 | if (p->IsFlagSet(Page::BLACK_PAGE)) { |
| 2797 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2800 | uint32_t mark_bit_index = p->AddressToMarkbitIndex(slot); |
| 2801 | unsigned int cell_index = mark_bit_index >> Bitmap::kBitsPerCellLog2; |
| 2802 | MarkBit::CellType index_mask = 1u << Bitmap::IndexInCell(mark_bit_index); |
| 2803 | MarkBit::CellType* cells = p->markbits()->cells(); |
| 2804 | Address base_address = p->area_start(); |
| 2805 | unsigned int base_address_cell_index = Bitmap::IndexToCell( |
| 2806 | Bitmap::CellAlignIndex(p->AddressToMarkbitIndex(base_address))); |
| 2807 | |
| 2808 | // Check if the slot points to the start of an object. This can happen e.g. |
| 2809 | // when we left trim a fixed array. Such slots are invalid and we can remove |
| 2810 | // them. |
| 2811 | if (index_mask > 1) { |
| 2812 | if ((cells[cell_index] & index_mask) != 0 && |
| 2813 | (cells[cell_index] & (index_mask >> 1)) == 0) { |
| 2814 | return false; |
| 2815 | } |
| 2816 | } else { |
| 2817 | // Left trimming moves the mark bits so we cannot be in the very first cell. |
| 2818 | DCHECK(cell_index != base_address_cell_index); |
| 2819 | if ((cells[cell_index] & index_mask) != 0 && |
| 2820 | (cells[cell_index - 1] & (1u << Bitmap::kBitIndexMask)) == 0) { |
| 2821 | return false; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | // Check if the object is in the current cell. |
| 2826 | MarkBit::CellType slot_mask; |
| 2827 | if ((cells[cell_index] == 0) || |
| 2828 | (base::bits::CountTrailingZeros32(cells[cell_index]) > |
| 2829 | base::bits::CountTrailingZeros32(cells[cell_index] | index_mask))) { |
| 2830 | // If we are already in the first cell, there is no live object. |
| 2831 | if (cell_index == base_address_cell_index) return false; |
| 2832 | |
| 2833 | // If not, find a cell in a preceding cell slot that has a mark bit set. |
| 2834 | do { |
| 2835 | cell_index--; |
| 2836 | } while (cell_index > base_address_cell_index && cells[cell_index] == 0); |
| 2837 | |
| 2838 | // The slot must be in a dead object if there are no preceding cells that |
| 2839 | // have mark bits set. |
| 2840 | if (cells[cell_index] == 0) { |
| 2841 | return false; |
| 2842 | } |
| 2843 | |
| 2844 | // The object is in a preceding cell. Set the mask to find any object. |
| 2845 | slot_mask = ~0u; |
| 2846 | } else { |
| 2847 | // We are interested in object mark bits right before the slot. |
| 2848 | slot_mask = index_mask + (index_mask - 1); |
| 2849 | } |
| 2850 | |
| 2851 | MarkBit::CellType current_cell = cells[cell_index]; |
| 2852 | CHECK(current_cell != 0); |
| 2853 | |
| 2854 | // Find the last live object in the cell. |
| 2855 | unsigned int leading_zeros = |
| 2856 | base::bits::CountLeadingZeros32(current_cell & slot_mask); |
| 2857 | CHECK(leading_zeros != Bitmap::kBitsPerCell); |
| 2858 | int offset = static_cast<int>(Bitmap::kBitIndexMask - leading_zeros) - 1; |
| 2859 | |
| 2860 | base_address += (cell_index - base_address_cell_index) * |
| 2861 | Bitmap::kBitsPerCell * kPointerSize; |
| 2862 | Address address = base_address + offset * kPointerSize; |
| 2863 | HeapObject* object = HeapObject::FromAddress(address); |
| 2864 | CHECK(Marking::IsBlack(Marking::MarkBitFrom(object))); |
| 2865 | CHECK(object->address() < reinterpret_cast<Address>(slot)); |
| 2866 | if ((object->address() + kPointerSize) <= slot && |
| 2867 | (object->address() + object->Size()) > slot) { |
| 2868 | // If the slot is within the last found object in the cell, the slot is |
| 2869 | // in a live object. |
| 2870 | // Slots pointing to the first word of an object are invalid and removed. |
| 2871 | // This can happen when we move the object header while left trimming. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2872 | return true; |
| 2873 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2874 | return false; |
| 2875 | } |
| 2876 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2877 | HeapObject* MarkCompactCollector::FindBlackObjectBySlotSlow(Address slot) { |
| 2878 | Page* p = Page::FromAddress(slot); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2879 | Space* owner = p->owner(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2880 | if (owner == heap_->lo_space() || owner == nullptr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2881 | Object* large_object = heap_->lo_space()->FindObject(slot); |
| 2882 | // This object has to exist, otherwise we would not have recorded a slot |
| 2883 | // for it. |
| 2884 | CHECK(large_object->IsHeapObject()); |
| 2885 | HeapObject* large_heap_object = HeapObject::cast(large_object); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2886 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2887 | if (IsMarked(large_heap_object)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2888 | return large_heap_object; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2889 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2890 | return nullptr; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2893 | if (p->IsFlagSet(Page::BLACK_PAGE)) { |
| 2894 | HeapObjectIterator it(p); |
| 2895 | HeapObject* object = nullptr; |
| 2896 | while ((object = it.Next()) != nullptr) { |
| 2897 | int size = object->Size(); |
| 2898 | if (object->address() > slot) return nullptr; |
| 2899 | if (object->address() <= slot && slot < (object->address() + size)) { |
| 2900 | return object; |
| 2901 | } |
| 2902 | } |
| 2903 | } else { |
| 2904 | LiveObjectIterator<kBlackObjects> it(p); |
| 2905 | HeapObject* object = nullptr; |
| 2906 | while ((object = it.Next()) != nullptr) { |
| 2907 | int size = object->Size(); |
| 2908 | if (object->address() > slot) return nullptr; |
| 2909 | if (object->address() <= slot && slot < (object->address() + size)) { |
| 2910 | return object; |
| 2911 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2912 | } |
| 2913 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2914 | return nullptr; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | |
| 2918 | void MarkCompactCollector::EvacuateNewSpacePrologue() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2919 | NewSpace* new_space = heap()->new_space(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2920 | NewSpacePageIterator it(new_space->bottom(), new_space->top()); |
| 2921 | // Append the list of new space pages to be processed. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2922 | while (it.has_next()) { |
| 2923 | newspace_evacuation_candidates_.Add(it.next()); |
| 2924 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2925 | new_space->Flip(); |
| 2926 | new_space->ResetAllocationInfo(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2927 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2928 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2929 | void MarkCompactCollector::EvacuateNewSpaceEpilogue() { |
| 2930 | newspace_evacuation_candidates_.Rewind(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2934 | class MarkCompactCollector::Evacuator : public Malloced { |
| 2935 | public: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2936 | explicit Evacuator(MarkCompactCollector* collector) |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2937 | : collector_(collector), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2938 | compaction_spaces_(collector->heap()), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2939 | local_pretenuring_feedback_(HashMap::PointersMatch, |
| 2940 | kInitialLocalPretenuringFeedbackCapacity), |
| 2941 | new_space_visitor_(collector->heap(), &compaction_spaces_, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2942 | &local_pretenuring_feedback_), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2943 | old_space_visitor_(collector->heap(), &compaction_spaces_), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2944 | duration_(0.0), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2945 | bytes_compacted_(0) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2946 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2947 | inline bool EvacuatePage(MemoryChunk* chunk); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2948 | |
| 2949 | // Merge back locally cached info sequentially. Note that this method needs |
| 2950 | // to be called from the main thread. |
| 2951 | inline void Finalize(); |
| 2952 | |
| 2953 | CompactionSpaceCollection* compaction_spaces() { return &compaction_spaces_; } |
| 2954 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2955 | private: |
| 2956 | static const int kInitialLocalPretenuringFeedbackCapacity = 256; |
| 2957 | |
| 2958 | Heap* heap() { return collector_->heap(); } |
| 2959 | |
| 2960 | void ReportCompactionProgress(double duration, intptr_t bytes_compacted) { |
| 2961 | duration_ += duration; |
| 2962 | bytes_compacted_ += bytes_compacted; |
| 2963 | } |
| 2964 | |
| 2965 | inline bool EvacuateSinglePage(MemoryChunk* p, HeapObjectVisitor* visitor); |
| 2966 | |
| 2967 | MarkCompactCollector* collector_; |
| 2968 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2969 | // Locally cached collector data. |
| 2970 | CompactionSpaceCollection compaction_spaces_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2971 | HashMap local_pretenuring_feedback_; |
| 2972 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2973 | // Visitors for the corresponding spaces. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2974 | EvacuateNewSpaceVisitor new_space_visitor_; |
| 2975 | EvacuateOldSpaceVisitor old_space_visitor_; |
| 2976 | |
| 2977 | // Book keeping info. |
| 2978 | double duration_; |
| 2979 | intptr_t bytes_compacted_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2980 | }; |
| 2981 | |
| 2982 | bool MarkCompactCollector::Evacuator::EvacuateSinglePage( |
| 2983 | MemoryChunk* p, HeapObjectVisitor* visitor) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2984 | bool success = false; |
| 2985 | DCHECK(p->IsEvacuationCandidate() || p->InNewSpace()); |
| 2986 | int saved_live_bytes = p->LiveBytes(); |
| 2987 | double evacuation_time; |
| 2988 | { |
| 2989 | AlwaysAllocateScope always_allocate(heap()->isolate()); |
| 2990 | TimedScope timed_scope(&evacuation_time); |
| 2991 | success = collector_->VisitLiveObjects(p, visitor, kClearMarkbits); |
| 2992 | } |
| 2993 | if (FLAG_trace_evacuation) { |
| 2994 | PrintIsolate(heap()->isolate(), |
| 2995 | "evacuation[%p]: page=%p new_space=%d executable=%d " |
| 2996 | "live_bytes=%d time=%f\n", |
| 2997 | this, p, p->InNewSpace(), |
| 2998 | p->IsFlagSet(MemoryChunk::IS_EXECUTABLE), saved_live_bytes, |
| 2999 | evacuation_time); |
| 3000 | } |
| 3001 | if (success) { |
| 3002 | ReportCompactionProgress(evacuation_time, saved_live_bytes); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3003 | } |
| 3004 | return success; |
| 3005 | } |
| 3006 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3007 | bool MarkCompactCollector::Evacuator::EvacuatePage(MemoryChunk* chunk) { |
| 3008 | bool success = false; |
| 3009 | if (chunk->InNewSpace()) { |
| 3010 | DCHECK_EQ(chunk->concurrent_sweeping_state().Value(), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3011 | NewSpacePage::kSweepingDone); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3012 | success = EvacuateSinglePage(chunk, &new_space_visitor_); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3013 | DCHECK(success); |
| 3014 | USE(success); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3015 | } else { |
| 3016 | DCHECK(chunk->IsEvacuationCandidate()); |
| 3017 | DCHECK_EQ(chunk->concurrent_sweeping_state().Value(), Page::kSweepingDone); |
| 3018 | success = EvacuateSinglePage(chunk, &old_space_visitor_); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3019 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3020 | return success; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | void MarkCompactCollector::Evacuator::Finalize() { |
| 3024 | heap()->old_space()->MergeCompactionSpace(compaction_spaces_.Get(OLD_SPACE)); |
| 3025 | heap()->code_space()->MergeCompactionSpace( |
| 3026 | compaction_spaces_.Get(CODE_SPACE)); |
| 3027 | heap()->tracer()->AddCompactionEvent(duration_, bytes_compacted_); |
| 3028 | heap()->IncrementPromotedObjectsSize(new_space_visitor_.promoted_size()); |
| 3029 | heap()->IncrementSemiSpaceCopiedObjectSize( |
| 3030 | new_space_visitor_.semispace_copied_size()); |
| 3031 | heap()->IncrementYoungSurvivorsCounter( |
| 3032 | new_space_visitor_.promoted_size() + |
| 3033 | new_space_visitor_.semispace_copied_size()); |
| 3034 | heap()->MergeAllocationSitePretenuringFeedback(local_pretenuring_feedback_); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3035 | } |
| 3036 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3037 | int MarkCompactCollector::NumberOfParallelCompactionTasks(int pages, |
| 3038 | intptr_t live_bytes) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3039 | if (!FLAG_parallel_compaction) return 1; |
| 3040 | // Compute the number of needed tasks based on a target compaction time, the |
| 3041 | // profiled compaction speed and marked live memory. |
| 3042 | // |
| 3043 | // The number of parallel compaction tasks is limited by: |
| 3044 | // - #evacuation pages |
| 3045 | // - (#cores - 1) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3046 | const double kTargetCompactionTimeInMs = 1; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3047 | const int kNumSweepingTasks = 3; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3048 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3049 | double compaction_speed = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3050 | heap()->tracer()->CompactionSpeedInBytesPerMillisecond(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3051 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3052 | const int available_cores = Max( |
| 3053 | 1, static_cast<int>( |
| 3054 | V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads()) - |
| 3055 | kNumSweepingTasks - 1); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3056 | int tasks; |
| 3057 | if (compaction_speed > 0) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3058 | tasks = 1 + static_cast<int>(live_bytes / compaction_speed / |
| 3059 | kTargetCompactionTimeInMs); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3060 | } else { |
| 3061 | tasks = pages; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3062 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3063 | const int tasks_capped_pages = Min(pages, tasks); |
| 3064 | return Min(available_cores, tasks_capped_pages); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3065 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3066 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3067 | class EvacuationJobTraits { |
| 3068 | public: |
| 3069 | typedef int* PerPageData; // Pointer to number of aborted pages. |
| 3070 | typedef MarkCompactCollector::Evacuator* PerTaskData; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3071 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3072 | static const bool NeedSequentialFinalization = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3073 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3074 | static bool ProcessPageInParallel(Heap* heap, PerTaskData evacuator, |
| 3075 | MemoryChunk* chunk, PerPageData) { |
| 3076 | return evacuator->EvacuatePage(chunk); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3077 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3078 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3079 | static void FinalizePageSequentially(Heap*, MemoryChunk* chunk, bool success, |
| 3080 | PerPageData data) { |
| 3081 | if (chunk->InNewSpace()) { |
| 3082 | DCHECK(success); |
| 3083 | } else { |
| 3084 | Page* p = static_cast<Page*>(chunk); |
| 3085 | if (success) { |
| 3086 | DCHECK(p->IsEvacuationCandidate()); |
| 3087 | DCHECK(p->SweepingDone()); |
| 3088 | p->Unlink(); |
| 3089 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3090 | // We have partially compacted the page, i.e., some objects may have |
| 3091 | // moved, others are still in place. |
| 3092 | // We need to: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3093 | // - Leave the evacuation candidate flag for later processing of slots |
| 3094 | // buffer entries. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3095 | // - Leave the slots buffer there for processing of entries added by |
| 3096 | // the write barrier. |
| 3097 | // - Rescan the page as slot recording in the migration buffer only |
| 3098 | // happens upon moving (which we potentially didn't do). |
| 3099 | // - Leave the page in the list of pages of a space since we could not |
| 3100 | // fully evacuate it. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3101 | DCHECK(p->IsEvacuationCandidate()); |
| 3102 | p->SetFlag(Page::COMPACTION_WAS_ABORTED); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3103 | *data += 1; |
| 3104 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3105 | } |
| 3106 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3107 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3108 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3109 | void MarkCompactCollector::EvacuatePagesInParallel() { |
| 3110 | PageParallelJob<EvacuationJobTraits> job( |
| 3111 | heap_, heap_->isolate()->cancelable_task_manager()); |
| 3112 | |
| 3113 | int abandoned_pages = 0; |
| 3114 | intptr_t live_bytes = 0; |
| 3115 | for (Page* page : evacuation_candidates_) { |
| 3116 | live_bytes += page->LiveBytes(); |
| 3117 | job.AddPage(page, &abandoned_pages); |
| 3118 | } |
| 3119 | for (NewSpacePage* page : newspace_evacuation_candidates_) { |
| 3120 | live_bytes += page->LiveBytes(); |
| 3121 | job.AddPage(page, &abandoned_pages); |
| 3122 | } |
| 3123 | DCHECK_GE(job.NumberOfPages(), 1); |
| 3124 | |
| 3125 | // Used for trace summary. |
| 3126 | double compaction_speed = 0; |
| 3127 | if (FLAG_trace_evacuation) { |
| 3128 | compaction_speed = heap()->tracer()->CompactionSpeedInBytesPerMillisecond(); |
| 3129 | } |
| 3130 | |
| 3131 | const int wanted_num_tasks = |
| 3132 | NumberOfParallelCompactionTasks(job.NumberOfPages(), live_bytes); |
| 3133 | Evacuator** evacuators = new Evacuator*[wanted_num_tasks]; |
| 3134 | for (int i = 0; i < wanted_num_tasks; i++) { |
| 3135 | evacuators[i] = new Evacuator(this); |
| 3136 | } |
| 3137 | job.Run(wanted_num_tasks, [evacuators](int i) { return evacuators[i]; }); |
| 3138 | for (int i = 0; i < wanted_num_tasks; i++) { |
| 3139 | evacuators[i]->Finalize(); |
| 3140 | delete evacuators[i]; |
| 3141 | } |
| 3142 | delete[] evacuators; |
| 3143 | |
| 3144 | if (FLAG_trace_evacuation) { |
| 3145 | PrintIsolate( |
| 3146 | isolate(), |
| 3147 | "%8.0f ms: evacuation-summary: parallel=%s pages=%d aborted=%d " |
| 3148 | "wanted_tasks=%d tasks=%d cores=%d live_bytes=%" V8_PTR_PREFIX |
| 3149 | "d compaction_speed=%.f\n", |
| 3150 | isolate()->time_millis_since_init(), |
| 3151 | FLAG_parallel_compaction ? "yes" : "no", job.NumberOfPages(), |
| 3152 | abandoned_pages, wanted_num_tasks, job.NumberOfTasks(), |
| 3153 | V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads(), |
| 3154 | live_bytes, compaction_speed); |
| 3155 | } |
| 3156 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3157 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3158 | class EvacuationWeakObjectRetainer : public WeakObjectRetainer { |
| 3159 | public: |
| 3160 | virtual Object* RetainAs(Object* object) { |
| 3161 | if (object->IsHeapObject()) { |
| 3162 | HeapObject* heap_object = HeapObject::cast(object); |
| 3163 | MapWord map_word = heap_object->map_word(); |
| 3164 | if (map_word.IsForwardingAddress()) { |
| 3165 | return map_word.ToForwardingAddress(); |
| 3166 | } |
| 3167 | } |
| 3168 | return object; |
| 3169 | } |
| 3170 | }; |
| 3171 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3172 | enum SweepingMode { SWEEP_ONLY, SWEEP_AND_VISIT_LIVE_OBJECTS }; |
| 3173 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3174 | enum SkipListRebuildingMode { REBUILD_SKIP_LIST, IGNORE_SKIP_LIST }; |
| 3175 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3176 | enum FreeSpaceTreatmentMode { IGNORE_FREE_SPACE, ZAP_FREE_SPACE }; |
| 3177 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3178 | // Sweeps a page. After sweeping the page can be iterated. |
| 3179 | // Slots in live objects pointing into evacuation candidates are updated |
| 3180 | // if requested. |
| 3181 | // Returns the size of the biggest continuous freed memory chunk in bytes. |
| 3182 | template <SweepingMode sweeping_mode, |
| 3183 | MarkCompactCollector::SweepingParallelism parallelism, |
| 3184 | SkipListRebuildingMode skip_list_mode, |
| 3185 | FreeSpaceTreatmentMode free_space_mode> |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3186 | static int Sweep(PagedSpace* space, Page* p, ObjectVisitor* v) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3187 | DCHECK(!p->IsEvacuationCandidate() && !p->SweepingDone()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3188 | DCHECK(!p->IsFlagSet(Page::BLACK_PAGE)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3189 | DCHECK_EQ(skip_list_mode == REBUILD_SKIP_LIST, |
| 3190 | space->identity() == CODE_SPACE); |
| 3191 | DCHECK((p->skip_list() == NULL) || (skip_list_mode == REBUILD_SKIP_LIST)); |
| 3192 | DCHECK(parallelism == MarkCompactCollector::SWEEP_ON_MAIN_THREAD || |
| 3193 | sweeping_mode == SWEEP_ONLY); |
| 3194 | |
| 3195 | Address free_start = p->area_start(); |
| 3196 | DCHECK(reinterpret_cast<intptr_t>(free_start) % (32 * kPointerSize) == 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3197 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3198 | // If we use the skip list for code space pages, we have to lock the skip |
| 3199 | // list because it could be accessed concurrently by the runtime or the |
| 3200 | // deoptimizer. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3201 | SkipList* skip_list = p->skip_list(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3202 | if ((skip_list_mode == REBUILD_SKIP_LIST) && skip_list) { |
| 3203 | skip_list->Clear(); |
| 3204 | } |
| 3205 | |
| 3206 | intptr_t freed_bytes = 0; |
| 3207 | intptr_t max_freed_bytes = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3208 | int curr_region = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3209 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3210 | LiveObjectIterator<kBlackObjects> it(p); |
| 3211 | HeapObject* object = NULL; |
| 3212 | while ((object = it.Next()) != NULL) { |
| 3213 | DCHECK(Marking::IsBlack(Marking::MarkBitFrom(object))); |
| 3214 | Address free_end = object->address(); |
| 3215 | if (free_end != free_start) { |
| 3216 | int size = static_cast<int>(free_end - free_start); |
| 3217 | if (free_space_mode == ZAP_FREE_SPACE) { |
| 3218 | memset(free_start, 0xcc, size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3219 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3220 | freed_bytes = space->UnaccountedFree(free_start, size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3221 | max_freed_bytes = Max(freed_bytes, max_freed_bytes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3222 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3223 | Map* map = object->synchronized_map(); |
| 3224 | int size = object->SizeFromMap(map); |
| 3225 | if (sweeping_mode == SWEEP_AND_VISIT_LIVE_OBJECTS) { |
| 3226 | object->IterateBody(map->instance_type(), size, v); |
| 3227 | } |
| 3228 | if ((skip_list_mode == REBUILD_SKIP_LIST) && skip_list != NULL) { |
| 3229 | int new_region_start = SkipList::RegionNumber(free_end); |
| 3230 | int new_region_end = |
| 3231 | SkipList::RegionNumber(free_end + size - kPointerSize); |
| 3232 | if (new_region_start != curr_region || new_region_end != curr_region) { |
| 3233 | skip_list->AddObject(free_end, size); |
| 3234 | curr_region = new_region_end; |
| 3235 | } |
| 3236 | } |
| 3237 | free_start = free_end + size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3238 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3239 | |
| 3240 | // Clear the mark bits of that page and reset live bytes count. |
| 3241 | Bitmap::Clear(p); |
| 3242 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3243 | if (free_start != p->area_end()) { |
| 3244 | int size = static_cast<int>(p->area_end() - free_start); |
| 3245 | if (free_space_mode == ZAP_FREE_SPACE) { |
| 3246 | memset(free_start, 0xcc, size); |
| 3247 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3248 | freed_bytes = space->UnaccountedFree(free_start, size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3249 | max_freed_bytes = Max(freed_bytes, max_freed_bytes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3250 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3251 | p->concurrent_sweeping_state().SetValue(Page::kSweepingDone); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3252 | return FreeList::GuaranteedAllocatable(static_cast<int>(max_freed_bytes)); |
| 3253 | } |
| 3254 | |
| 3255 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3256 | void MarkCompactCollector::InvalidateCode(Code* code) { |
| 3257 | if (heap_->incremental_marking()->IsCompacting() && |
| 3258 | !ShouldSkipEvacuationSlotRecording(code)) { |
| 3259 | DCHECK(compacting_); |
| 3260 | |
| 3261 | // If the object is white than no slots were recorded on it yet. |
| 3262 | MarkBit mark_bit = Marking::MarkBitFrom(code); |
| 3263 | if (Marking::IsWhite(mark_bit)) return; |
| 3264 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3265 | // Ignore all slots that might have been recorded in the body of the |
| 3266 | // deoptimized code object. Assumption: no slots will be recorded for |
| 3267 | // this object after invalidating it. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3268 | Page* page = Page::FromAddress(code->address()); |
| 3269 | Address start = code->instruction_start(); |
| 3270 | Address end = code->address() + code->Size(); |
| 3271 | RememberedSet<OLD_TO_OLD>::RemoveRangeTyped(page, start, end); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | |
| 3276 | // Return true if the given code is deoptimized or will be deoptimized. |
| 3277 | bool MarkCompactCollector::WillBeDeoptimized(Code* code) { |
| 3278 | return code->is_optimized_code() && code->marked_for_deoptimization(); |
| 3279 | } |
| 3280 | |
| 3281 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3282 | #ifdef VERIFY_HEAP |
| 3283 | static void VerifyAllBlackObjects(MemoryChunk* page) { |
| 3284 | LiveObjectIterator<kAllLiveObjects> it(page); |
| 3285 | HeapObject* object = NULL; |
| 3286 | while ((object = it.Next()) != NULL) { |
| 3287 | CHECK(Marking::IsBlack(Marking::MarkBitFrom(object))); |
| 3288 | } |
| 3289 | } |
| 3290 | #endif // VERIFY_HEAP |
| 3291 | |
| 3292 | |
| 3293 | bool MarkCompactCollector::VisitLiveObjects(MemoryChunk* page, |
| 3294 | HeapObjectVisitor* visitor, |
| 3295 | IterationMode mode) { |
| 3296 | #ifdef VERIFY_HEAP |
| 3297 | VerifyAllBlackObjects(page); |
| 3298 | #endif // VERIFY_HEAP |
| 3299 | |
| 3300 | LiveObjectIterator<kBlackObjects> it(page); |
| 3301 | HeapObject* object = nullptr; |
| 3302 | while ((object = it.Next()) != nullptr) { |
| 3303 | DCHECK(Marking::IsBlack(Marking::MarkBitFrom(object))); |
| 3304 | if (!visitor->Visit(object)) { |
| 3305 | if (mode == kClearMarkbits) { |
| 3306 | page->markbits()->ClearRange( |
| 3307 | page->AddressToMarkbitIndex(page->area_start()), |
| 3308 | page->AddressToMarkbitIndex(object->address())); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3309 | if (page->old_to_new_slots() != nullptr) { |
| 3310 | page->old_to_new_slots()->RemoveRange( |
| 3311 | 0, static_cast<int>(object->address() - page->address())); |
| 3312 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3313 | RecomputeLiveBytes(page); |
| 3314 | } |
| 3315 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3316 | } |
| 3317 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3318 | if (mode == kClearMarkbits) { |
| 3319 | Bitmap::Clear(page); |
| 3320 | } |
| 3321 | return true; |
| 3322 | } |
| 3323 | |
| 3324 | |
| 3325 | void MarkCompactCollector::RecomputeLiveBytes(MemoryChunk* page) { |
| 3326 | LiveObjectIterator<kBlackObjects> it(page); |
| 3327 | int new_live_size = 0; |
| 3328 | HeapObject* object = nullptr; |
| 3329 | while ((object = it.Next()) != nullptr) { |
| 3330 | new_live_size += object->Size(); |
| 3331 | } |
| 3332 | page->SetLiveBytes(new_live_size); |
| 3333 | } |
| 3334 | |
| 3335 | |
| 3336 | void MarkCompactCollector::VisitLiveObjectsBody(Page* page, |
| 3337 | ObjectVisitor* visitor) { |
| 3338 | #ifdef VERIFY_HEAP |
| 3339 | VerifyAllBlackObjects(page); |
| 3340 | #endif // VERIFY_HEAP |
| 3341 | |
| 3342 | LiveObjectIterator<kBlackObjects> it(page); |
| 3343 | HeapObject* object = NULL; |
| 3344 | while ((object = it.Next()) != NULL) { |
| 3345 | DCHECK(Marking::IsBlack(Marking::MarkBitFrom(object))); |
| 3346 | Map* map = object->synchronized_map(); |
| 3347 | int size = object->SizeFromMap(map); |
| 3348 | object->IterateBody(map->instance_type(), size, visitor); |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | |
| 3353 | void MarkCompactCollector::SweepAbortedPages() { |
| 3354 | // Second pass on aborted pages. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3355 | for (Page* p : evacuation_candidates_) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3356 | if (p->IsFlagSet(Page::COMPACTION_WAS_ABORTED)) { |
| 3357 | p->ClearFlag(MemoryChunk::COMPACTION_WAS_ABORTED); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3358 | p->concurrent_sweeping_state().SetValue(Page::kSweepingInProgress); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3359 | PagedSpace* space = static_cast<PagedSpace*>(p->owner()); |
| 3360 | switch (space->identity()) { |
| 3361 | case OLD_SPACE: |
| 3362 | Sweep<SWEEP_ONLY, SWEEP_ON_MAIN_THREAD, IGNORE_SKIP_LIST, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3363 | IGNORE_FREE_SPACE>(space, p, nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3364 | break; |
| 3365 | case CODE_SPACE: |
| 3366 | if (FLAG_zap_code_space) { |
| 3367 | Sweep<SWEEP_ONLY, SWEEP_ON_MAIN_THREAD, REBUILD_SKIP_LIST, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3368 | ZAP_FREE_SPACE>(space, p, nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3369 | } else { |
| 3370 | Sweep<SWEEP_ONLY, SWEEP_ON_MAIN_THREAD, REBUILD_SKIP_LIST, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3371 | IGNORE_FREE_SPACE>(space, p, nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3372 | } |
| 3373 | break; |
| 3374 | default: |
| 3375 | UNREACHABLE(); |
| 3376 | break; |
| 3377 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3378 | { |
| 3379 | base::LockGuard<base::Mutex> guard(&swept_pages_mutex_); |
| 3380 | swept_pages(space->identity())->Add(p); |
| 3381 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3382 | } |
| 3383 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | |
| 3387 | void MarkCompactCollector::EvacuateNewSpaceAndCandidates() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3388 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_EVACUATE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3389 | Heap::RelocationLock relocation_lock(heap()); |
| 3390 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3391 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3392 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_EVACUATE_COPY); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3393 | EvacuationScope evacuation_scope(this); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3394 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3395 | EvacuateNewSpacePrologue(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3396 | EvacuatePagesInParallel(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3397 | EvacuateNewSpaceEpilogue(); |
| 3398 | heap()->new_space()->set_age_mark(heap()->new_space()->top()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | UpdatePointersAfterEvacuation(); |
| 3402 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3403 | // Give pages that are queued to be freed back to the OS. Note that filtering |
| 3404 | // slots only handles old space (for unboxed doubles), and thus map space can |
| 3405 | // still contain stale pointers. We only free the chunks after pointer updates |
| 3406 | // to still have access to page headers. |
| 3407 | heap()->FreeQueuedChunks(); |
| 3408 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3409 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3410 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_EVACUATE_CLEAN_UP); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3411 | // After updating all pointers, we can finally sweep the aborted pages, |
| 3412 | // effectively overriding any forward pointers. |
| 3413 | SweepAbortedPages(); |
| 3414 | |
| 3415 | // EvacuateNewSpaceAndCandidates iterates over new space objects and for |
| 3416 | // ArrayBuffers either re-registers them as live or promotes them. This is |
| 3417 | // needed to properly free them. |
| 3418 | heap()->array_buffer_tracker()->FreeDead(false); |
| 3419 | |
| 3420 | // Deallocate evacuated candidate pages. |
| 3421 | ReleaseEvacuationCandidates(); |
| 3422 | } |
| 3423 | |
| 3424 | #ifdef VERIFY_HEAP |
| 3425 | if (FLAG_verify_heap && !sweeping_in_progress_) { |
| 3426 | VerifyEvacuation(heap()); |
| 3427 | } |
| 3428 | #endif |
| 3429 | } |
| 3430 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3431 | template <PointerDirection direction> |
| 3432 | class PointerUpdateJobTraits { |
| 3433 | public: |
| 3434 | typedef int PerPageData; // Per page data is not used in this job. |
| 3435 | typedef PointersUpdatingVisitor* PerTaskData; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3436 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3437 | static bool ProcessPageInParallel(Heap* heap, PerTaskData visitor, |
| 3438 | MemoryChunk* chunk, PerPageData) { |
| 3439 | UpdateUntypedPointers(heap, chunk); |
| 3440 | UpdateTypedPointers(heap, chunk, visitor); |
| 3441 | return true; |
| 3442 | } |
| 3443 | static const bool NeedSequentialFinalization = false; |
| 3444 | static void FinalizePageSequentially(Heap*, MemoryChunk*, bool, PerPageData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3447 | private: |
| 3448 | static void UpdateUntypedPointers(Heap* heap, MemoryChunk* chunk) { |
| 3449 | if (direction == OLD_TO_NEW) { |
| 3450 | RememberedSet<OLD_TO_NEW>::IterateWithWrapper(heap, chunk, |
| 3451 | UpdateOldToNewSlot); |
| 3452 | } else { |
| 3453 | RememberedSet<OLD_TO_OLD>::Iterate(chunk, [heap](Address slot) { |
| 3454 | PointersUpdatingVisitor::UpdateSlot(heap, |
| 3455 | reinterpret_cast<Object**>(slot)); |
| 3456 | return REMOVE_SLOT; |
| 3457 | }); |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | static void UpdateTypedPointers(Heap* heap, MemoryChunk* chunk, |
| 3462 | PointersUpdatingVisitor* visitor) { |
| 3463 | if (direction == OLD_TO_OLD) { |
| 3464 | Isolate* isolate = heap->isolate(); |
| 3465 | RememberedSet<OLD_TO_OLD>::IterateTyped( |
| 3466 | chunk, [isolate, visitor](SlotType type, Address slot) { |
| 3467 | UpdateTypedSlot(isolate, visitor, type, slot); |
| 3468 | return REMOVE_SLOT; |
| 3469 | }); |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | static void UpdateOldToNewSlot(HeapObject** address, HeapObject* object) { |
| 3474 | MapWord map_word = object->map_word(); |
| 3475 | // Since we only filter invalid slots in old space, the store buffer can |
| 3476 | // still contain stale pointers in large object and in map spaces. Ignore |
| 3477 | // these pointers here. |
| 3478 | DCHECK(map_word.IsForwardingAddress() || |
| 3479 | !object->GetHeap()->old_space()->Contains( |
| 3480 | reinterpret_cast<Address>(address))); |
| 3481 | if (map_word.IsForwardingAddress()) { |
| 3482 | // Update the corresponding slot. |
| 3483 | *address = map_word.ToForwardingAddress(); |
| 3484 | } |
| 3485 | } |
| 3486 | }; |
| 3487 | |
| 3488 | int NumberOfPointerUpdateTasks(int pages) { |
| 3489 | if (!FLAG_parallel_pointer_update) return 1; |
| 3490 | const int kMaxTasks = 4; |
| 3491 | const int kPagesPerTask = 4; |
| 3492 | return Min(kMaxTasks, (pages + kPagesPerTask - 1) / kPagesPerTask); |
| 3493 | } |
| 3494 | |
| 3495 | template <PointerDirection direction> |
| 3496 | void UpdatePointersInParallel(Heap* heap) { |
| 3497 | PageParallelJob<PointerUpdateJobTraits<direction> > job( |
| 3498 | heap, heap->isolate()->cancelable_task_manager()); |
| 3499 | RememberedSet<direction>::IterateMemoryChunks( |
| 3500 | heap, [&job](MemoryChunk* chunk) { job.AddPage(chunk, 0); }); |
| 3501 | PointersUpdatingVisitor visitor(heap); |
| 3502 | int num_pages = job.NumberOfPages(); |
| 3503 | int num_tasks = NumberOfPointerUpdateTasks(num_pages); |
| 3504 | job.Run(num_tasks, [&visitor](int i) { return &visitor; }); |
| 3505 | } |
| 3506 | |
| 3507 | class ToSpacePointerUpdateJobTraits { |
| 3508 | public: |
| 3509 | typedef std::pair<Address, Address> PerPageData; |
| 3510 | typedef PointersUpdatingVisitor* PerTaskData; |
| 3511 | |
| 3512 | static bool ProcessPageInParallel(Heap* heap, PerTaskData visitor, |
| 3513 | MemoryChunk* chunk, PerPageData limits) { |
| 3514 | for (Address cur = limits.first; cur < limits.second;) { |
| 3515 | HeapObject* object = HeapObject::FromAddress(cur); |
| 3516 | Map* map = object->map(); |
| 3517 | int size = object->SizeFromMap(map); |
| 3518 | object->IterateBody(map->instance_type(), size, visitor); |
| 3519 | cur += size; |
| 3520 | } |
| 3521 | return true; |
| 3522 | } |
| 3523 | static const bool NeedSequentialFinalization = false; |
| 3524 | static void FinalizePageSequentially(Heap*, MemoryChunk*, bool, PerPageData) { |
| 3525 | } |
| 3526 | }; |
| 3527 | |
| 3528 | void UpdateToSpacePointersInParallel(Heap* heap) { |
| 3529 | PageParallelJob<ToSpacePointerUpdateJobTraits> job( |
| 3530 | heap, heap->isolate()->cancelable_task_manager()); |
| 3531 | Address space_start = heap->new_space()->bottom(); |
| 3532 | Address space_end = heap->new_space()->top(); |
| 3533 | NewSpacePageIterator it(space_start, space_end); |
| 3534 | while (it.has_next()) { |
| 3535 | NewSpacePage* page = it.next(); |
| 3536 | Address start = |
| 3537 | page->Contains(space_start) ? space_start : page->area_start(); |
| 3538 | Address end = page->Contains(space_end) ? space_end : page->area_end(); |
| 3539 | job.AddPage(page, std::make_pair(start, end)); |
| 3540 | } |
| 3541 | PointersUpdatingVisitor visitor(heap); |
| 3542 | int num_tasks = FLAG_parallel_pointer_update ? job.NumberOfPages() : 1; |
| 3543 | job.Run(num_tasks, [&visitor](int i) { return &visitor; }); |
| 3544 | } |
| 3545 | |
| 3546 | void MarkCompactCollector::UpdatePointersAfterEvacuation() { |
| 3547 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_EVACUATE_UPDATE_POINTERS); |
| 3548 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3549 | PointersUpdatingVisitor updating_visitor(heap()); |
| 3550 | |
| 3551 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3552 | TRACE_GC(heap()->tracer(), |
| 3553 | GCTracer::Scope::MC_EVACUATE_UPDATE_POINTERS_TO_NEW); |
| 3554 | UpdateToSpacePointersInParallel(heap_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3555 | // Update roots. |
| 3556 | heap_->IterateRoots(&updating_visitor, VISIT_ALL_IN_SWEEP_NEWSPACE); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3557 | UpdatePointersInParallel<OLD_TO_NEW>(heap_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3560 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3561 | Heap* heap = this->heap(); |
| 3562 | TRACE_GC(heap->tracer(), |
| 3563 | GCTracer::Scope::MC_EVACUATE_UPDATE_POINTERS_TO_EVACUATED); |
| 3564 | UpdatePointersInParallel<OLD_TO_OLD>(heap_); |
| 3565 | } |
| 3566 | |
| 3567 | { |
| 3568 | TRACE_GC(heap()->tracer(), |
| 3569 | GCTracer::Scope::MC_EVACUATE_UPDATE_POINTERS_BETWEEN_EVACUATED); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3570 | for (Page* p : evacuation_candidates_) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3571 | DCHECK(p->IsEvacuationCandidate()); |
| 3572 | // Important: skip list should be cleared only after roots were updated |
| 3573 | // because root iteration traverses the stack and might have to find |
| 3574 | // code objects from non-updated pc pointing into evacuation candidate. |
| 3575 | SkipList* list = p->skip_list(); |
| 3576 | if (list != NULL) list->Clear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3577 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3578 | // First pass on aborted pages, fixing up all live objects. |
| 3579 | if (p->IsFlagSet(Page::COMPACTION_WAS_ABORTED)) { |
| 3580 | p->ClearEvacuationCandidate(); |
| 3581 | VisitLiveObjectsBody(p, &updating_visitor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | } |
| 3585 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3586 | { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3587 | TRACE_GC(heap()->tracer(), |
| 3588 | GCTracer::Scope::MC_EVACUATE_UPDATE_POINTERS_WEAK); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3589 | // Update pointers from external string table. |
| 3590 | heap_->UpdateReferencesInExternalStringTable( |
| 3591 | &UpdateReferenceInExternalStringTableEntry); |
| 3592 | |
| 3593 | EvacuationWeakObjectRetainer evacuation_object_retainer; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3594 | heap()->ProcessWeakListRoots(&evacuation_object_retainer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3595 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3599 | void MarkCompactCollector::ReleaseEvacuationCandidates() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3600 | for (Page* p : evacuation_candidates_) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3601 | if (!p->IsEvacuationCandidate()) continue; |
| 3602 | PagedSpace* space = static_cast<PagedSpace*>(p->owner()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3603 | p->ResetLiveBytes(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3604 | CHECK(p->SweepingDone()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3605 | space->ReleasePage(p); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3606 | } |
| 3607 | evacuation_candidates_.Rewind(0); |
| 3608 | compacting_ = false; |
| 3609 | heap()->FreeQueuedChunks(); |
| 3610 | } |
| 3611 | |
| 3612 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3613 | int MarkCompactCollector::SweepInParallel(PagedSpace* space, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3614 | int required_freed_bytes, |
| 3615 | int max_pages) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3616 | int max_freed = 0; |
| 3617 | int max_freed_overall = 0; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3618 | int page_count = 0; |
| 3619 | for (Page* p : sweeping_list(space)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3620 | max_freed = SweepInParallel(p, space); |
| 3621 | DCHECK(max_freed >= 0); |
| 3622 | if (required_freed_bytes > 0 && max_freed >= required_freed_bytes) { |
| 3623 | return max_freed; |
| 3624 | } |
| 3625 | max_freed_overall = Max(max_freed, max_freed_overall); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3626 | page_count++; |
| 3627 | if (max_pages > 0 && page_count >= max_pages) { |
| 3628 | break; |
| 3629 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3630 | } |
| 3631 | return max_freed_overall; |
| 3632 | } |
| 3633 | |
| 3634 | |
| 3635 | int MarkCompactCollector::SweepInParallel(Page* page, PagedSpace* space) { |
| 3636 | int max_freed = 0; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3637 | if (page->mutex()->TryLock()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3638 | // If this page was already swept in the meantime, we can return here. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3639 | if (page->concurrent_sweeping_state().Value() != Page::kSweepingPending) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3640 | page->mutex()->Unlock(); |
| 3641 | return 0; |
| 3642 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3643 | page->concurrent_sweeping_state().SetValue(Page::kSweepingInProgress); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3644 | if (space->identity() == OLD_SPACE) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3645 | max_freed = Sweep<SWEEP_ONLY, SWEEP_IN_PARALLEL, IGNORE_SKIP_LIST, |
| 3646 | IGNORE_FREE_SPACE>(space, page, NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3647 | } else if (space->identity() == CODE_SPACE) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3648 | max_freed = Sweep<SWEEP_ONLY, SWEEP_IN_PARALLEL, REBUILD_SKIP_LIST, |
| 3649 | IGNORE_FREE_SPACE>(space, page, NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3650 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3651 | max_freed = Sweep<SWEEP_ONLY, SWEEP_IN_PARALLEL, IGNORE_SKIP_LIST, |
| 3652 | IGNORE_FREE_SPACE>(space, page, NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3653 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3654 | { |
| 3655 | base::LockGuard<base::Mutex> guard(&swept_pages_mutex_); |
| 3656 | swept_pages(space->identity())->Add(page); |
| 3657 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3658 | page->concurrent_sweeping_state().SetValue(Page::kSweepingDone); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3659 | page->mutex()->Unlock(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3660 | } |
| 3661 | return max_freed; |
| 3662 | } |
| 3663 | |
| 3664 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3665 | void MarkCompactCollector::StartSweepSpace(PagedSpace* space) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3666 | space->ClearStats(); |
| 3667 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3668 | PageIterator it(space); |
| 3669 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3670 | int will_be_swept = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3671 | bool unused_page_present = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3672 | |
| 3673 | while (it.has_next()) { |
| 3674 | Page* p = it.next(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3675 | DCHECK(p->SweepingDone()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3676 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3677 | if (p->IsEvacuationCandidate()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3678 | // Will be processed in EvacuateNewSpaceAndCandidates. |
| 3679 | DCHECK(evacuation_candidates_.length() > 0); |
| 3680 | continue; |
| 3681 | } |
| 3682 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3683 | // We can not sweep black pages, since all mark bits are set for these |
| 3684 | // pages. |
| 3685 | if (p->IsFlagSet(Page::BLACK_PAGE)) { |
| 3686 | Bitmap::Clear(p); |
| 3687 | p->concurrent_sweeping_state().SetValue(Page::kSweepingDone); |
| 3688 | p->ClearFlag(Page::BLACK_PAGE); |
| 3689 | // TODO(hpayer): Free unused memory of last black page. |
| 3690 | continue; |
| 3691 | } |
| 3692 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3693 | if (p->IsFlagSet(Page::NEVER_ALLOCATE_ON_PAGE)) { |
| 3694 | // We need to sweep the page to get it into an iterable state again. Note |
| 3695 | // that this adds unusable memory into the free list that is later on |
| 3696 | // (in the free list) dropped again. Since we only use the flag for |
| 3697 | // testing this is fine. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3698 | p->concurrent_sweeping_state().SetValue(Page::kSweepingInProgress); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3699 | Sweep<SWEEP_ONLY, SWEEP_ON_MAIN_THREAD, IGNORE_SKIP_LIST, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3700 | IGNORE_FREE_SPACE>(space, p, nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3701 | continue; |
| 3702 | } |
| 3703 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3704 | // One unused page is kept, all further are released before sweeping them. |
| 3705 | if (p->LiveBytes() == 0) { |
| 3706 | if (unused_page_present) { |
| 3707 | if (FLAG_gc_verbose) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3708 | PrintIsolate(isolate(), "sweeping: released page: %p", p); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3709 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3710 | space->ReleasePage(p); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3711 | continue; |
| 3712 | } |
| 3713 | unused_page_present = true; |
| 3714 | } |
| 3715 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3716 | p->concurrent_sweeping_state().SetValue(Page::kSweepingPending); |
| 3717 | sweeping_list(space).push_back(p); |
| 3718 | int to_sweep = p->area_size() - p->LiveBytes(); |
| 3719 | space->accounting_stats_.ShrinkSpace(to_sweep); |
| 3720 | will_be_swept++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | if (FLAG_gc_verbose) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3724 | PrintIsolate(isolate(), "sweeping: space=%s initialized_for_sweeping=%d", |
| 3725 | AllocationSpaceName(space->identity()), will_be_swept); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3726 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3727 | std::sort(sweeping_list(space).begin(), sweeping_list(space).end(), |
| 3728 | [](Page* a, Page* b) { return a->LiveBytes() < b->LiveBytes(); }); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3732 | void MarkCompactCollector::SweepSpaces() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3733 | TRACE_GC(heap()->tracer(), GCTracer::Scope::MC_SWEEP); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3734 | double start_time = 0.0; |
| 3735 | if (FLAG_print_cumulative_gc_stat) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3736 | start_time = heap_->MonotonicallyIncreasingTimeInMs(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | #ifdef DEBUG |
| 3740 | state_ = SWEEP_SPACES; |
| 3741 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3742 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3743 | { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3744 | sweeping_in_progress_ = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3745 | { |
| 3746 | GCTracer::Scope sweep_scope(heap()->tracer(), |
| 3747 | GCTracer::Scope::MC_SWEEP_OLD); |
| 3748 | StartSweepSpace(heap()->old_space()); |
| 3749 | } |
| 3750 | { |
| 3751 | GCTracer::Scope sweep_scope(heap()->tracer(), |
| 3752 | GCTracer::Scope::MC_SWEEP_CODE); |
| 3753 | StartSweepSpace(heap()->code_space()); |
| 3754 | } |
| 3755 | { |
| 3756 | GCTracer::Scope sweep_scope(heap()->tracer(), |
| 3757 | GCTracer::Scope::MC_SWEEP_MAP); |
| 3758 | StartSweepSpace(heap()->map_space()); |
| 3759 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3760 | if (FLAG_concurrent_sweeping) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3761 | StartSweeperThreads(); |
| 3762 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3763 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3764 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3765 | // Deallocate unmarked large objects. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3766 | heap_->lo_space()->FreeUnmarkedObjects(); |
| 3767 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3768 | if (FLAG_print_cumulative_gc_stat) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3769 | heap_->tracer()->AddSweepingTime(heap_->MonotonicallyIncreasingTimeInMs() - |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3770 | start_time); |
| 3771 | } |
| 3772 | } |
| 3773 | |
| 3774 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3775 | void MarkCompactCollector::ParallelSweepSpacesComplete() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3776 | sweeping_list(heap()->old_space()).clear(); |
| 3777 | sweeping_list(heap()->code_space()).clear(); |
| 3778 | sweeping_list(heap()->map_space()).clear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3781 | Isolate* MarkCompactCollector::isolate() const { return heap_->isolate(); } |
| 3782 | |
| 3783 | |
| 3784 | void MarkCompactCollector::Initialize() { |
| 3785 | MarkCompactMarkingVisitor::Initialize(); |
| 3786 | IncrementalMarking::Initialize(); |
| 3787 | } |
| 3788 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3789 | void MarkCompactCollector::RecordCodeEntrySlot(HeapObject* host, Address slot, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3790 | Code* target) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3791 | Page* target_page = Page::FromAddress(reinterpret_cast<Address>(target)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3792 | Page* source_page = Page::FromAddress(reinterpret_cast<Address>(host)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3793 | if (target_page->IsEvacuationCandidate() && |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3794 | !ShouldSkipEvacuationSlotRecording(host)) { |
| 3795 | RememberedSet<OLD_TO_OLD>::InsertTyped(source_page, CODE_ENTRY_SLOT, slot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | |
| 3800 | void MarkCompactCollector::RecordCodeTargetPatch(Address pc, Code* target) { |
| 3801 | DCHECK(heap()->gc_state() == Heap::MARK_COMPACT); |
| 3802 | if (is_compacting()) { |
| 3803 | Code* host = |
| 3804 | isolate()->inner_pointer_to_code_cache()->GcSafeFindCodeForInnerPointer( |
| 3805 | pc); |
| 3806 | MarkBit mark_bit = Marking::MarkBitFrom(host); |
| 3807 | if (Marking::IsBlack(mark_bit)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3808 | RelocInfo rinfo(isolate(), pc, RelocInfo::CODE_TARGET, 0, host); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3809 | RecordRelocSlot(host, &rinfo, target); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | } |
| 3813 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3814 | } // namespace internal |
| 3815 | } // namespace v8 |