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