blob: 95642e90f19d11b3212bc37013ec46433da9701c [file] [log] [blame]
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "incremental-marking.h"
31
32#include "code-stubs.h"
33#include "compilation-cache.h"
34#include "v8conversions.h"
35
36namespace v8 {
37namespace internal {
38
39
40IncrementalMarking::IncrementalMarking(Heap* heap)
41 : heap_(heap),
42 state_(STOPPED),
43 marking_deque_memory_(NULL),
44 steps_count_(0),
45 steps_took_(0),
46 longest_step_(0.0),
47 old_generation_space_available_at_start_of_incremental_(0),
48 old_generation_space_used_at_start_of_incremental_(0),
49 steps_count_since_last_gc_(0),
50 steps_took_since_last_gc_(0),
51 should_hurry_(false),
52 allocation_marking_factor_(0),
53 allocated_(0) {
54}
55
56
57void IncrementalMarking::TearDown() {
58 delete marking_deque_memory_;
59}
60
61
62void IncrementalMarking::RecordWriteFromCode(HeapObject* obj,
63 Object* value,
64 Isolate* isolate) {
65 ASSERT(obj->IsHeapObject());
66
67 // Fast cases should already be covered by RecordWriteStub.
68 ASSERT(value->IsHeapObject());
69 ASSERT(!value->IsHeapNumber());
70 ASSERT(!value->IsString() || value->IsConsString());
71 ASSERT(Marking::IsWhite(Marking::MarkBitFrom(HeapObject::cast(value))));
72
73 IncrementalMarking* marking = isolate->heap()->incremental_marking();
74 ASSERT(!marking->is_compacting_);
75 marking->RecordWrite(obj, NULL, value);
76}
77
78
79void IncrementalMarking::RecordWriteForEvacuationFromCode(HeapObject* obj,
80 Object** slot,
81 Isolate* isolate) {
82 IncrementalMarking* marking = isolate->heap()->incremental_marking();
83 ASSERT(marking->is_compacting_);
84 marking->RecordWrite(obj, slot, *slot);
85}
86
87
88void IncrementalMarking::RecordCodeTargetPatch(Address pc, HeapObject* value) {
89 if (IsMarking()) {
90 Code* host = heap_->isolate()->inner_pointer_to_code_cache()->
91 GcSafeFindCodeForInnerPointer(pc);
92 RelocInfo rinfo(pc, RelocInfo::CODE_TARGET, 0, host);
93 RecordWriteIntoCode(host, &rinfo, value);
94 }
95}
96
97
98void IncrementalMarking::RecordWriteOfCodeEntry(JSFunction* host,
99 Object** slot,
100 Code* value) {
101 if (BaseRecordWrite(host, slot, value) && is_compacting_) {
102 ASSERT(slot != NULL);
103 heap_->mark_compact_collector()->
104 RecordCodeEntrySlot(reinterpret_cast<Address>(slot), value);
105 }
106}
107
108
109
110class IncrementalMarkingMarkingVisitor : public ObjectVisitor {
111 public:
112 IncrementalMarkingMarkingVisitor(Heap* heap,
113 IncrementalMarking* incremental_marking)
114 : heap_(heap),
115 incremental_marking_(incremental_marking) {
116 }
117
118 void VisitEmbeddedPointer(Code* host, Object** p) {
119 Object* obj = *p;
120 if (obj->NonFailureIsHeapObject()) {
121 heap_->mark_compact_collector()->RecordSlot(
122 reinterpret_cast<Object**>(host),
123 p,
124 obj);
125 MarkObject(obj);
126 }
127 }
128
129 void VisitCodeTarget(RelocInfo* rinfo) {
130 ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode()));
131 Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
132 heap_->mark_compact_collector()->RecordRelocSlot(rinfo, Code::cast(target));
133 MarkObject(target);
134 }
135
136 void VisitDebugTarget(RelocInfo* rinfo) {
137 ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) &&
138 rinfo->IsPatchedReturnSequence()) ||
139 (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) &&
140 rinfo->IsPatchedDebugBreakSlotSequence()));
141 Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address());
142 heap_->mark_compact_collector()->RecordRelocSlot(rinfo, Code::cast(target));
143 MarkObject(target);
144 }
145
146 void VisitCodeEntry(Address entry_address) {
147 Object* target = Code::GetObjectFromEntryAddress(entry_address);
148 heap_->mark_compact_collector()->
149 RecordCodeEntrySlot(entry_address, Code::cast(target));
150 MarkObject(target);
151 }
152
153 void VisitPointer(Object** p) {
154 Object* obj = *p;
155 if (obj->NonFailureIsHeapObject()) {
156 heap_->mark_compact_collector()->RecordSlot(p, p, obj);
157 MarkObject(obj);
158 }
159 }
160
161 void VisitPointers(Object** start, Object** end) {
162 for (Object** p = start; p < end; p++) {
163 Object* obj = *p;
164 if (obj->NonFailureIsHeapObject()) {
165 heap_->mark_compact_collector()->RecordSlot(start, p, obj);
166 MarkObject(obj);
167 }
168 }
169 }
170
171 private:
172 // Mark object pointed to by p.
173 INLINE(void MarkObject(Object* obj)) {
174 HeapObject* heap_object = HeapObject::cast(obj);
175 MarkBit mark_bit = Marking::MarkBitFrom(heap_object);
176 if (mark_bit.data_only()) {
177 if (incremental_marking_->MarkBlackOrKeepGrey(mark_bit)) {
178 MemoryChunk::IncrementLiveBytes(heap_object->address(),
179 heap_object->Size());
180 }
181 } else if (Marking::IsWhite(mark_bit)) {
182 incremental_marking_->WhiteToGreyAndPush(heap_object, mark_bit);
183 }
184 }
185
186 Heap* heap_;
187 IncrementalMarking* incremental_marking_;
188};
189
190
191class IncrementalMarkingRootMarkingVisitor : public ObjectVisitor {
192 public:
193 IncrementalMarkingRootMarkingVisitor(Heap* heap,
194 IncrementalMarking* incremental_marking)
195 : heap_(heap),
196 incremental_marking_(incremental_marking) {
197 }
198
199 void VisitPointer(Object** p) {
200 MarkObjectByPointer(p);
201 }
202
203 void VisitPointers(Object** start, Object** end) {
204 for (Object** p = start; p < end; p++) MarkObjectByPointer(p);
205 }
206
207 private:
208 void MarkObjectByPointer(Object** p) {
209 Object* obj = *p;
210 if (!obj->IsHeapObject()) return;
211
212 HeapObject* heap_object = HeapObject::cast(obj);
213 MarkBit mark_bit = Marking::MarkBitFrom(heap_object);
214 if (mark_bit.data_only()) {
215 if (incremental_marking_->MarkBlackOrKeepGrey(mark_bit)) {
216 MemoryChunk::IncrementLiveBytes(heap_object->address(),
217 heap_object->Size());
218 }
219 } else {
220 if (Marking::IsWhite(mark_bit)) {
221 incremental_marking_->WhiteToGreyAndPush(heap_object, mark_bit);
222 }
223 }
224 }
225
226 Heap* heap_;
227 IncrementalMarking* incremental_marking_;
228};
229
230
231void IncrementalMarking::SetOldSpacePageFlags(MemoryChunk* chunk,
232 bool is_marking,
233 bool is_compacting) {
234 if (is_marking) {
235 chunk->SetFlag(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING);
236 chunk->SetFlag(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING);
237
238 // It's difficult to filter out slots recorded for large objects.
239 if (chunk->owner()->identity() == LO_SPACE &&
240 chunk->size() > static_cast<size_t>(Page::kPageSize) &&
241 is_compacting) {
242 chunk->SetFlag(MemoryChunk::RESCAN_ON_EVACUATION);
243 }
244 } else if (chunk->owner()->identity() == CELL_SPACE ||
245 chunk->scan_on_scavenge()) {
246 chunk->ClearFlag(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING);
247 chunk->ClearFlag(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING);
248 } else {
249 chunk->ClearFlag(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING);
250 chunk->SetFlag(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING);
251 }
252}
253
254
255void IncrementalMarking::SetNewSpacePageFlags(NewSpacePage* chunk,
256 bool is_marking) {
257 chunk->SetFlag(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING);
258 if (is_marking) {
259 chunk->SetFlag(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING);
260 } else {
261 chunk->ClearFlag(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING);
262 }
263 chunk->SetFlag(MemoryChunk::SCAN_ON_SCAVENGE);
264}
265
266
267void IncrementalMarking::DeactivateIncrementalWriteBarrierForSpace(
268 PagedSpace* space) {
269 PageIterator it(space);
270 while (it.has_next()) {
271 Page* p = it.next();
272 SetOldSpacePageFlags(p, false, false);
273 }
274}
275
276
277void IncrementalMarking::DeactivateIncrementalWriteBarrierForSpace(
278 NewSpace* space) {
279 NewSpacePageIterator it(space);
280 while (it.has_next()) {
281 NewSpacePage* p = it.next();
282 SetNewSpacePageFlags(p, false);
283 }
284}
285
286
287void IncrementalMarking::DeactivateIncrementalWriteBarrier() {
288 DeactivateIncrementalWriteBarrierForSpace(heap_->old_pointer_space());
289 DeactivateIncrementalWriteBarrierForSpace(heap_->old_data_space());
290 DeactivateIncrementalWriteBarrierForSpace(heap_->cell_space());
291 DeactivateIncrementalWriteBarrierForSpace(heap_->map_space());
292 DeactivateIncrementalWriteBarrierForSpace(heap_->code_space());
293 DeactivateIncrementalWriteBarrierForSpace(heap_->new_space());
294
295 LargePage* lop = heap_->lo_space()->first_page();
296 while (lop->is_valid()) {
297 SetOldSpacePageFlags(lop, false, false);
298 lop = lop->next_page();
299 }
300}
301
302
303void IncrementalMarking::ActivateIncrementalWriteBarrier(PagedSpace* space) {
304 PageIterator it(space);
305 while (it.has_next()) {
306 Page* p = it.next();
307 SetOldSpacePageFlags(p, true, is_compacting_);
308 }
309}
310
311
312void IncrementalMarking::ActivateIncrementalWriteBarrier(NewSpace* space) {
313 NewSpacePageIterator it(space->ToSpaceStart(), space->ToSpaceEnd());
314 while (it.has_next()) {
315 NewSpacePage* p = it.next();
316 SetNewSpacePageFlags(p, true);
317 }
318}
319
320
321void IncrementalMarking::ActivateIncrementalWriteBarrier() {
322 ActivateIncrementalWriteBarrier(heap_->old_pointer_space());
323 ActivateIncrementalWriteBarrier(heap_->old_data_space());
324 ActivateIncrementalWriteBarrier(heap_->cell_space());
325 ActivateIncrementalWriteBarrier(heap_->map_space());
326 ActivateIncrementalWriteBarrier(heap_->code_space());
327 ActivateIncrementalWriteBarrier(heap_->new_space());
328
329 LargePage* lop = heap_->lo_space()->first_page();
330 while (lop->is_valid()) {
331 SetOldSpacePageFlags(lop, true, is_compacting_);
332 lop = lop->next_page();
333 }
334}
335
336
337bool IncrementalMarking::WorthActivating() {
338#ifndef DEBUG
339 static const intptr_t kActivationThreshold = 8 * MB;
340#else
341 // TODO(gc) consider setting this to some low level so that some
342 // debug tests run with incremental marking and some without.
343 static const intptr_t kActivationThreshold = 0;
344#endif
345
346 return FLAG_incremental_marking &&
347 heap_->PromotedSpaceSize() > kActivationThreshold;
348}
349
350
351void IncrementalMarking::ActivateGeneratedStub(Code* stub) {
352 ASSERT(RecordWriteStub::GetMode(stub) ==
353 RecordWriteStub::STORE_BUFFER_ONLY);
354
355 if (!IsMarking()) {
356 // Initially stub is generated in STORE_BUFFER_ONLY mode thus
357 // we don't need to do anything if incremental marking is
358 // not active.
359 } else if (IsCompacting()) {
360 RecordWriteStub::Patch(stub, RecordWriteStub::INCREMENTAL_COMPACTION);
361 } else {
362 RecordWriteStub::Patch(stub, RecordWriteStub::INCREMENTAL);
363 }
364}
365
366
367static void PatchIncrementalMarkingRecordWriteStubs(
368 Heap* heap, RecordWriteStub::Mode mode) {
369 NumberDictionary* stubs = heap->code_stubs();
370
371 int capacity = stubs->Capacity();
372 for (int i = 0; i < capacity; i++) {
373 Object* k = stubs->KeyAt(i);
374 if (stubs->IsKey(k)) {
375 uint32_t key = NumberToUint32(k);
376
377 if (CodeStub::MajorKeyFromKey(key) ==
378 CodeStub::RecordWrite) {
379 Object* e = stubs->ValueAt(i);
380 if (e->IsCode()) {
381 RecordWriteStub::Patch(Code::cast(e), mode);
382 }
383 }
384 }
385 }
386}
387
388
389void IncrementalMarking::EnsureMarkingDequeIsCommitted() {
390 if (marking_deque_memory_ == NULL) {
391 marking_deque_memory_ = new VirtualMemory(4 * MB);
392 marking_deque_memory_->Commit(
393 reinterpret_cast<Address>(marking_deque_memory_->address()),
394 marking_deque_memory_->size(),
395 false); // Not executable.
396 }
397}
398
399
400void IncrementalMarking::Start() {
401 if (FLAG_trace_incremental_marking) {
402 PrintF("[IncrementalMarking] Start\n");
403 }
404 ASSERT(FLAG_incremental_marking);
405 ASSERT(state_ == STOPPED);
406
407 ResetStepCounters();
408
409 if (heap_->old_pointer_space()->IsSweepingComplete() &&
410 heap_->old_data_space()->IsSweepingComplete()) {
411 StartMarking();
412 } else {
413 if (FLAG_trace_incremental_marking) {
414 PrintF("[IncrementalMarking] Start sweeping.\n");
415 }
416 state_ = SWEEPING;
417 }
418
419 heap_->new_space()->LowerInlineAllocationLimit(kAllocatedThreshold);
420}
421
422
423static void MarkObjectGreyDoNotEnqueue(Object* obj) {
424 if (obj->IsHeapObject()) {
425 HeapObject* heap_obj = HeapObject::cast(obj);
426 MarkBit mark_bit = Marking::MarkBitFrom(HeapObject::cast(obj));
427 if (Marking::IsBlack(mark_bit)) {
428 MemoryChunk::IncrementLiveBytes(heap_obj->address(),
429 -heap_obj->Size());
430 }
431 Marking::AnyToGrey(mark_bit);
432 }
433}
434
435
436void IncrementalMarking::StartMarking() {
437 if (FLAG_trace_incremental_marking) {
438 PrintF("[IncrementalMarking] Start marking\n");
439 }
440
441 is_compacting_ = !FLAG_never_compact &&
442 heap_->mark_compact_collector()->StartCompaction();
443
444 state_ = MARKING;
445
446 RecordWriteStub::Mode mode = is_compacting_ ?
447 RecordWriteStub::INCREMENTAL_COMPACTION : RecordWriteStub::INCREMENTAL;
448
449 PatchIncrementalMarkingRecordWriteStubs(heap_, mode);
450
451 EnsureMarkingDequeIsCommitted();
452
453 // Initialize marking stack.
454 Address addr = static_cast<Address>(marking_deque_memory_->address());
455 size_t size = marking_deque_memory_->size();
456 if (FLAG_force_marking_deque_overflows) size = 64 * kPointerSize;
457 marking_deque_.Initialize(addr, addr + size);
458
459 ActivateIncrementalWriteBarrier();
460
461#ifdef DEBUG
462 // Marking bits are cleared by the sweeper.
463 heap_->mark_compact_collector()->VerifyMarkbitsAreClean();
464#endif
465
466 heap_->CompletelyClearInstanceofCache();
467 heap_->isolate()->compilation_cache()->MarkCompactPrologue();
468
469 if (FLAG_cleanup_code_caches_at_gc) {
470 // We will mark cache black with a separate pass
471 // when we finish marking.
472 MarkObjectGreyDoNotEnqueue(heap_->polymorphic_code_cache());
473 }
474
475 // Mark strong roots grey.
476 IncrementalMarkingRootMarkingVisitor visitor(heap_, this);
477 heap_->IterateStrongRoots(&visitor, VISIT_ONLY_STRONG);
478
479 // Ready to start incremental marking.
480 if (FLAG_trace_incremental_marking) {
481 PrintF("[IncrementalMarking] Running\n");
482 }
483}
484
485
486void IncrementalMarking::PrepareForScavenge() {
487 if (!IsMarking()) return;
488 NewSpacePageIterator it(heap_->new_space()->FromSpaceStart(),
489 heap_->new_space()->FromSpaceEnd());
490 while (it.has_next()) {
491 Bitmap::Clear(it.next());
492 }
493}
494
495
496void IncrementalMarking::UpdateMarkingDequeAfterScavenge() {
497 if (!IsMarking()) return;
498
499 int current = marking_deque_.bottom();
500 int mask = marking_deque_.mask();
501 int limit = marking_deque_.top();
502 HeapObject** array = marking_deque_.array();
503 int new_top = current;
504
505 Map* filler_map = heap_->one_pointer_filler_map();
506
507 while (current != limit) {
508 HeapObject* obj = array[current];
509 ASSERT(obj->IsHeapObject());
510 current = ((current + 1) & mask);
511 if (heap_->InNewSpace(obj)) {
512 MapWord map_word = obj->map_word();
513 if (map_word.IsForwardingAddress()) {
514 HeapObject* dest = map_word.ToForwardingAddress();
515 array[new_top] = dest;
516 new_top = ((new_top + 1) & mask);
517 ASSERT(new_top != marking_deque_.bottom());
518 ASSERT(Marking::IsGrey(Marking::MarkBitFrom(obj)));
519 }
520 } else if (obj->map() != filler_map) {
521 // Skip one word filler objects that appear on the
522 // stack when we perform in place array shift.
523 array[new_top] = obj;
524 new_top = ((new_top + 1) & mask);
525 ASSERT(new_top != marking_deque_.bottom());
526 ASSERT(Marking::IsGrey(Marking::MarkBitFrom(obj)));
527 }
528 }
529 marking_deque_.set_top(new_top);
530
531 steps_took_since_last_gc_ = 0;
532 steps_count_since_last_gc_ = 0;
533 longest_step_ = 0.0;
534}
535
536
537void IncrementalMarking::VisitGlobalContext(Context* ctx, ObjectVisitor* v) {
538 v->VisitPointers(
539 HeapObject::RawField(
540 ctx, Context::MarkCompactBodyDescriptor::kStartOffset),
541 HeapObject::RawField(
542 ctx, Context::MarkCompactBodyDescriptor::kEndOffset));
543
544 MarkCompactCollector* collector = heap_->mark_compact_collector();
545 for (int idx = Context::FIRST_WEAK_SLOT;
546 idx < Context::GLOBAL_CONTEXT_SLOTS;
547 ++idx) {
548 Object** slot =
549 HeapObject::RawField(ctx, FixedArray::OffsetOfElementAt(idx));
550 collector->RecordSlot(slot, slot, *slot);
551 }
552}
553
554
555void IncrementalMarking::Hurry() {
556 if (state() == MARKING) {
557 double start = 0.0;
558 if (FLAG_trace_incremental_marking) {
559 PrintF("[IncrementalMarking] Hurry\n");
560 start = OS::TimeCurrentMillis();
561 }
562 // TODO(gc) hurry can mark objects it encounters black as mutator
563 // was stopped.
564 Map* filler_map = heap_->one_pointer_filler_map();
565 Map* global_context_map = heap_->global_context_map();
566 IncrementalMarkingMarkingVisitor marking_visitor(heap_, this);
567 while (!marking_deque_.IsEmpty()) {
568 HeapObject* obj = marking_deque_.Pop();
569
570 // Explicitly skip one word fillers. Incremental markbit patterns are
571 // correct only for objects that occupy at least two words.
572 Map* map = obj->map();
573 if (map == filler_map) {
574 continue;
575 } else if (map == global_context_map) {
576 // Global contexts have weak fields.
577 VisitGlobalContext(Context::cast(obj), &marking_visitor);
578 } else {
579 obj->Iterate(&marking_visitor);
580 }
581
582 MarkBit mark_bit = Marking::MarkBitFrom(obj);
583 ASSERT(!Marking::IsBlack(mark_bit));
584 Marking::MarkBlack(mark_bit);
585 MemoryChunk::IncrementLiveBytes(obj->address(), obj->Size());
586 }
587 state_ = COMPLETE;
588 if (FLAG_trace_incremental_marking) {
589 double end = OS::TimeCurrentMillis();
590 PrintF("[IncrementalMarking] Complete (hurry), spent %d ms.\n",
591 static_cast<int>(end - start));
592 }
593 }
594
595 if (FLAG_cleanup_code_caches_at_gc) {
596 PolymorphicCodeCache* poly_cache = heap_->polymorphic_code_cache();
597 Marking::GreyToBlack(Marking::MarkBitFrom(poly_cache));
598 MemoryChunk::IncrementLiveBytes(poly_cache->address(),
599 PolymorphicCodeCache::kSize);
600 }
601
602 Object* context = heap_->global_contexts_list();
603 while (!context->IsUndefined()) {
604 NormalizedMapCache* cache = Context::cast(context)->normalized_map_cache();
605 MarkBit mark_bit = Marking::MarkBitFrom(cache);
606 if (Marking::IsGrey(mark_bit)) {
607 Marking::GreyToBlack(mark_bit);
608 MemoryChunk::IncrementLiveBytes(cache->address(), cache->Size());
609 }
610 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
611 }
612}
613
614
615void IncrementalMarking::Abort() {
616 if (IsStopped()) return;
617 if (FLAG_trace_incremental_marking) {
618 PrintF("[IncrementalMarking] Aborting.\n");
619 }
620 heap_->new_space()->LowerInlineAllocationLimit(0);
621 IncrementalMarking::set_should_hurry(false);
622 ResetStepCounters();
623 if (IsMarking()) {
624 PatchIncrementalMarkingRecordWriteStubs(heap_,
625 RecordWriteStub::STORE_BUFFER_ONLY);
626 DeactivateIncrementalWriteBarrier();
627
628 if (is_compacting_) {
629 LargeObjectIterator it(heap_->lo_space());
630 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
631 Page* p = Page::FromAddress(obj->address());
632 if (p->IsFlagSet(Page::RESCAN_ON_EVACUATION)) {
633 p->ClearFlag(Page::RESCAN_ON_EVACUATION);
634 }
635 }
636 }
637 }
638 heap_->isolate()->stack_guard()->Continue(GC_REQUEST);
639 state_ = STOPPED;
640 is_compacting_ = false;
641}
642
643
644void IncrementalMarking::Finalize() {
645 Hurry();
646 state_ = STOPPED;
647 is_compacting_ = false;
648 heap_->new_space()->LowerInlineAllocationLimit(0);
649 IncrementalMarking::set_should_hurry(false);
650 ResetStepCounters();
651 PatchIncrementalMarkingRecordWriteStubs(heap_,
652 RecordWriteStub::STORE_BUFFER_ONLY);
653 DeactivateIncrementalWriteBarrier();
654 ASSERT(marking_deque_.IsEmpty());
655 heap_->isolate()->stack_guard()->Continue(GC_REQUEST);
656}
657
658
659void IncrementalMarking::MarkingComplete() {
660 state_ = COMPLETE;
661 // We will set the stack guard to request a GC now. This will mean the rest
662 // of the GC gets performed as soon as possible (we can't do a GC here in a
663 // record-write context). If a few things get allocated between now and then
664 // that shouldn't make us do a scavenge and keep being incremental, so we set
665 // the should-hurry flag to indicate that there can't be much work left to do.
666 set_should_hurry(true);
667 if (FLAG_trace_incremental_marking) {
668 PrintF("[IncrementalMarking] Complete (normal).\n");
669 }
670 heap_->isolate()->stack_guard()->RequestGC();
671}
672
673
674void IncrementalMarking::Step(intptr_t allocated_bytes) {
675 if (heap_->gc_state() != Heap::NOT_IN_GC ||
676 !FLAG_incremental_marking ||
677 !FLAG_incremental_marking_steps ||
678 (state_ != SWEEPING && state_ != MARKING)) {
679 return;
680 }
681
682 allocated_ += allocated_bytes;
683
684 if (allocated_ < kAllocatedThreshold) return;
685
686 intptr_t bytes_to_process = allocated_ * allocation_marking_factor_;
687
688 double start = 0;
689
690 if (FLAG_trace_incremental_marking || FLAG_trace_gc) {
691 start = OS::TimeCurrentMillis();
692 }
693
694 if (state_ == SWEEPING) {
695 if (heap_->old_pointer_space()->AdvanceSweeper(bytes_to_process) &&
696 heap_->old_data_space()->AdvanceSweeper(bytes_to_process)) {
697 StartMarking();
698 }
699 } else if (state_ == MARKING) {
700 Map* filler_map = heap_->one_pointer_filler_map();
701 Map* global_context_map = heap_->global_context_map();
702 IncrementalMarkingMarkingVisitor marking_visitor(heap_, this);
703 while (!marking_deque_.IsEmpty() && bytes_to_process > 0) {
704 HeapObject* obj = marking_deque_.Pop();
705
706 // Explicitly skip one word fillers. Incremental markbit patterns are
707 // correct only for objects that occupy at least two words.
708 Map* map = obj->map();
709 if (map == filler_map) continue;
710
711 ASSERT(Marking::IsGrey(Marking::MarkBitFrom(obj)));
712 int size = obj->SizeFromMap(map);
713 bytes_to_process -= size;
714 MarkBit map_mark_bit = Marking::MarkBitFrom(map);
715 if (Marking::IsWhite(map_mark_bit)) {
716 WhiteToGreyAndPush(map, map_mark_bit);
717 }
718
719 // TODO(gc) switch to static visitor instead of normal visitor.
720 if (map == global_context_map) {
721 // Global contexts have weak fields.
722 Context* ctx = Context::cast(obj);
723
724 // We will mark cache black with a separate pass
725 // when we finish marking.
726 MarkObjectGreyDoNotEnqueue(ctx->normalized_map_cache());
727
728 VisitGlobalContext(ctx, &marking_visitor);
729 } else {
730 obj->IterateBody(map->instance_type(), size, &marking_visitor);
731 }
732
733 MarkBit obj_mark_bit = Marking::MarkBitFrom(obj);
734 ASSERT(!Marking::IsBlack(obj_mark_bit));
735 Marking::MarkBlack(obj_mark_bit);
736 MemoryChunk::IncrementLiveBytes(obj->address(), size);
737 }
738 if (marking_deque_.IsEmpty()) MarkingComplete();
739 }
740
741 allocated_ = 0;
742
743 steps_count_++;
744 steps_count_since_last_gc_++;
745
746 bool speed_up = false;
747
748 if (old_generation_space_available_at_start_of_incremental_ < 10 * MB ||
749 SpaceLeftInOldSpace() <
750 old_generation_space_available_at_start_of_incremental_ >> 1) {
751 // Half of the space that was available is gone while we were
752 // incrementally marking.
753 speed_up = true;
754 old_generation_space_available_at_start_of_incremental_ =
755 SpaceLeftInOldSpace();
756 }
757
758 if (heap_->PromotedTotalSize() >
759 old_generation_space_used_at_start_of_incremental_ << 1) {
760 // Size of old space doubled while we were incrementally marking.
761 speed_up = true;
762 old_generation_space_used_at_start_of_incremental_ =
763 heap_->PromotedTotalSize();
764 }
765
766 if ((steps_count_ % kAllocationMarkingFactorSpeedupInterval) == 0 &&
767 allocation_marking_factor_ < kMaxAllocationMarkingFactor) {
768 speed_up = true;
769 }
770
771 if (speed_up && 0) {
772 allocation_marking_factor_ += kAllocationMarkingFactorSpeedup;
773 allocation_marking_factor_ =
774 static_cast<int>(allocation_marking_factor_ * 1.3);
775 if (FLAG_trace_gc) {
776 PrintF("Marking speed increased to %d\n", allocation_marking_factor_);
777 }
778 }
779
780 if (FLAG_trace_incremental_marking || FLAG_trace_gc) {
781 double end = OS::TimeCurrentMillis();
782 double delta = (end - start);
783 longest_step_ = Max(longest_step_, delta);
784 steps_took_ += delta;
785 steps_took_since_last_gc_ += delta;
786 }
787}
788
789
790void IncrementalMarking::ResetStepCounters() {
791 steps_count_ = 0;
792 steps_took_ = 0;
793 longest_step_ = 0.0;
794 old_generation_space_available_at_start_of_incremental_ =
795 SpaceLeftInOldSpace();
796 old_generation_space_used_at_start_of_incremental_ =
797 heap_->PromotedTotalSize();
798 steps_count_since_last_gc_ = 0;
799 steps_took_since_last_gc_ = 0;
800 bytes_rescanned_ = 0;
801 allocation_marking_factor_ = kInitialAllocationMarkingFactor;
802}
803
804
805int64_t IncrementalMarking::SpaceLeftInOldSpace() {
806 return heap_->MaxOldGenerationSize() - heap_->PromotedSpaceSize();
807}
808
809} } // namespace v8::internal