blob: 88ebd783ea8369a0839e9c1e39207596aafa8972 [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
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000120 void VisitEmbeddedPointer(RelocInfo* rinfo) {
121 ASSERT(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT);
122 Object* target = rinfo->target_object();
123 if (target->NonFailureIsHeapObject()) {
124 heap_->mark_compact_collector()->RecordRelocSlot(rinfo, target);
125 MarkObject(target);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000126 }
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 &&
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000347 !Serializer::enabled() &&
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000348 heap_->PromotedSpaceSize() > kActivationThreshold;
349}
350
351
352void IncrementalMarking::ActivateGeneratedStub(Code* stub) {
353 ASSERT(RecordWriteStub::GetMode(stub) ==
354 RecordWriteStub::STORE_BUFFER_ONLY);
355
356 if (!IsMarking()) {
357 // Initially stub is generated in STORE_BUFFER_ONLY mode thus
358 // we don't need to do anything if incremental marking is
359 // not active.
360 } else if (IsCompacting()) {
361 RecordWriteStub::Patch(stub, RecordWriteStub::INCREMENTAL_COMPACTION);
362 } else {
363 RecordWriteStub::Patch(stub, RecordWriteStub::INCREMENTAL);
364 }
365}
366
367
368static void PatchIncrementalMarkingRecordWriteStubs(
369 Heap* heap, RecordWriteStub::Mode mode) {
370 NumberDictionary* stubs = heap->code_stubs();
371
372 int capacity = stubs->Capacity();
373 for (int i = 0; i < capacity; i++) {
374 Object* k = stubs->KeyAt(i);
375 if (stubs->IsKey(k)) {
376 uint32_t key = NumberToUint32(k);
377
378 if (CodeStub::MajorKeyFromKey(key) ==
379 CodeStub::RecordWrite) {
380 Object* e = stubs->ValueAt(i);
381 if (e->IsCode()) {
382 RecordWriteStub::Patch(Code::cast(e), mode);
383 }
384 }
385 }
386 }
387}
388
389
390void IncrementalMarking::EnsureMarkingDequeIsCommitted() {
391 if (marking_deque_memory_ == NULL) {
392 marking_deque_memory_ = new VirtualMemory(4 * MB);
393 marking_deque_memory_->Commit(
394 reinterpret_cast<Address>(marking_deque_memory_->address()),
395 marking_deque_memory_->size(),
396 false); // Not executable.
397 }
398}
399
400
401void IncrementalMarking::Start() {
402 if (FLAG_trace_incremental_marking) {
403 PrintF("[IncrementalMarking] Start\n");
404 }
405 ASSERT(FLAG_incremental_marking);
406 ASSERT(state_ == STOPPED);
407
408 ResetStepCounters();
409
410 if (heap_->old_pointer_space()->IsSweepingComplete() &&
411 heap_->old_data_space()->IsSweepingComplete()) {
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000412 StartMarking(ALLOW_COMPACTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000413 } else {
414 if (FLAG_trace_incremental_marking) {
415 PrintF("[IncrementalMarking] Start sweeping.\n");
416 }
417 state_ = SWEEPING;
418 }
419
420 heap_->new_space()->LowerInlineAllocationLimit(kAllocatedThreshold);
421}
422
423
424static void MarkObjectGreyDoNotEnqueue(Object* obj) {
425 if (obj->IsHeapObject()) {
426 HeapObject* heap_obj = HeapObject::cast(obj);
427 MarkBit mark_bit = Marking::MarkBitFrom(HeapObject::cast(obj));
428 if (Marking::IsBlack(mark_bit)) {
429 MemoryChunk::IncrementLiveBytes(heap_obj->address(),
430 -heap_obj->Size());
431 }
432 Marking::AnyToGrey(mark_bit);
433 }
434}
435
436
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000437void IncrementalMarking::StartMarking(CompactionFlag flag) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000438 if (FLAG_trace_incremental_marking) {
439 PrintF("[IncrementalMarking] Start marking\n");
440 }
441
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000442 is_compacting_ = !FLAG_never_compact && (flag == ALLOW_COMPACTION) &&
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000443 heap_->mark_compact_collector()->StartCompaction();
444
445 state_ = MARKING;
446
447 RecordWriteStub::Mode mode = is_compacting_ ?
448 RecordWriteStub::INCREMENTAL_COMPACTION : RecordWriteStub::INCREMENTAL;
449
450 PatchIncrementalMarkingRecordWriteStubs(heap_, mode);
451
452 EnsureMarkingDequeIsCommitted();
453
454 // Initialize marking stack.
455 Address addr = static_cast<Address>(marking_deque_memory_->address());
456 size_t size = marking_deque_memory_->size();
457 if (FLAG_force_marking_deque_overflows) size = 64 * kPointerSize;
458 marking_deque_.Initialize(addr, addr + size);
459
460 ActivateIncrementalWriteBarrier();
461
462#ifdef DEBUG
463 // Marking bits are cleared by the sweeper.
464 heap_->mark_compact_collector()->VerifyMarkbitsAreClean();
465#endif
466
467 heap_->CompletelyClearInstanceofCache();
468 heap_->isolate()->compilation_cache()->MarkCompactPrologue();
469
470 if (FLAG_cleanup_code_caches_at_gc) {
471 // We will mark cache black with a separate pass
472 // when we finish marking.
473 MarkObjectGreyDoNotEnqueue(heap_->polymorphic_code_cache());
474 }
475
476 // Mark strong roots grey.
477 IncrementalMarkingRootMarkingVisitor visitor(heap_, this);
478 heap_->IterateStrongRoots(&visitor, VISIT_ONLY_STRONG);
479
480 // Ready to start incremental marking.
481 if (FLAG_trace_incremental_marking) {
482 PrintF("[IncrementalMarking] Running\n");
483 }
484}
485
486
487void IncrementalMarking::PrepareForScavenge() {
488 if (!IsMarking()) return;
489 NewSpacePageIterator it(heap_->new_space()->FromSpaceStart(),
490 heap_->new_space()->FromSpaceEnd());
491 while (it.has_next()) {
492 Bitmap::Clear(it.next());
493 }
494}
495
496
497void IncrementalMarking::UpdateMarkingDequeAfterScavenge() {
498 if (!IsMarking()) return;
499
500 int current = marking_deque_.bottom();
501 int mask = marking_deque_.mask();
502 int limit = marking_deque_.top();
503 HeapObject** array = marking_deque_.array();
504 int new_top = current;
505
506 Map* filler_map = heap_->one_pointer_filler_map();
507
508 while (current != limit) {
509 HeapObject* obj = array[current];
510 ASSERT(obj->IsHeapObject());
511 current = ((current + 1) & mask);
512 if (heap_->InNewSpace(obj)) {
513 MapWord map_word = obj->map_word();
514 if (map_word.IsForwardingAddress()) {
515 HeapObject* dest = map_word.ToForwardingAddress();
516 array[new_top] = dest;
517 new_top = ((new_top + 1) & mask);
518 ASSERT(new_top != marking_deque_.bottom());
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000519#ifdef DEBUG
520 MarkBit mark_bit = Marking::MarkBitFrom(obj);
521 ASSERT(Marking::IsGrey(mark_bit) ||
522 (obj->IsFiller() && Marking::IsWhite(mark_bit)));
523#endif
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000524 }
525 } else if (obj->map() != filler_map) {
526 // Skip one word filler objects that appear on the
527 // stack when we perform in place array shift.
528 array[new_top] = obj;
529 new_top = ((new_top + 1) & mask);
530 ASSERT(new_top != marking_deque_.bottom());
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000531#ifdef DEBUG
532 MarkBit mark_bit = Marking::MarkBitFrom(obj);
533 ASSERT(Marking::IsGrey(mark_bit) ||
534 (obj->IsFiller() && Marking::IsWhite(mark_bit)));
535#endif
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000536 }
537 }
538 marking_deque_.set_top(new_top);
539
540 steps_took_since_last_gc_ = 0;
541 steps_count_since_last_gc_ = 0;
542 longest_step_ = 0.0;
543}
544
545
546void IncrementalMarking::VisitGlobalContext(Context* ctx, ObjectVisitor* v) {
547 v->VisitPointers(
548 HeapObject::RawField(
549 ctx, Context::MarkCompactBodyDescriptor::kStartOffset),
550 HeapObject::RawField(
551 ctx, Context::MarkCompactBodyDescriptor::kEndOffset));
552
553 MarkCompactCollector* collector = heap_->mark_compact_collector();
554 for (int idx = Context::FIRST_WEAK_SLOT;
555 idx < Context::GLOBAL_CONTEXT_SLOTS;
556 ++idx) {
557 Object** slot =
558 HeapObject::RawField(ctx, FixedArray::OffsetOfElementAt(idx));
559 collector->RecordSlot(slot, slot, *slot);
560 }
561}
562
563
564void IncrementalMarking::Hurry() {
565 if (state() == MARKING) {
566 double start = 0.0;
567 if (FLAG_trace_incremental_marking) {
568 PrintF("[IncrementalMarking] Hurry\n");
569 start = OS::TimeCurrentMillis();
570 }
571 // TODO(gc) hurry can mark objects it encounters black as mutator
572 // was stopped.
573 Map* filler_map = heap_->one_pointer_filler_map();
574 Map* global_context_map = heap_->global_context_map();
575 IncrementalMarkingMarkingVisitor marking_visitor(heap_, this);
576 while (!marking_deque_.IsEmpty()) {
577 HeapObject* obj = marking_deque_.Pop();
578
579 // Explicitly skip one word fillers. Incremental markbit patterns are
580 // correct only for objects that occupy at least two words.
581 Map* map = obj->map();
582 if (map == filler_map) {
583 continue;
584 } else if (map == global_context_map) {
585 // Global contexts have weak fields.
586 VisitGlobalContext(Context::cast(obj), &marking_visitor);
587 } else {
588 obj->Iterate(&marking_visitor);
589 }
590
591 MarkBit mark_bit = Marking::MarkBitFrom(obj);
592 ASSERT(!Marking::IsBlack(mark_bit));
593 Marking::MarkBlack(mark_bit);
594 MemoryChunk::IncrementLiveBytes(obj->address(), obj->Size());
595 }
596 state_ = COMPLETE;
597 if (FLAG_trace_incremental_marking) {
598 double end = OS::TimeCurrentMillis();
599 PrintF("[IncrementalMarking] Complete (hurry), spent %d ms.\n",
600 static_cast<int>(end - start));
601 }
602 }
603
604 if (FLAG_cleanup_code_caches_at_gc) {
605 PolymorphicCodeCache* poly_cache = heap_->polymorphic_code_cache();
606 Marking::GreyToBlack(Marking::MarkBitFrom(poly_cache));
607 MemoryChunk::IncrementLiveBytes(poly_cache->address(),
608 PolymorphicCodeCache::kSize);
609 }
610
611 Object* context = heap_->global_contexts_list();
612 while (!context->IsUndefined()) {
613 NormalizedMapCache* cache = Context::cast(context)->normalized_map_cache();
614 MarkBit mark_bit = Marking::MarkBitFrom(cache);
615 if (Marking::IsGrey(mark_bit)) {
616 Marking::GreyToBlack(mark_bit);
617 MemoryChunk::IncrementLiveBytes(cache->address(), cache->Size());
618 }
619 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
620 }
621}
622
623
624void IncrementalMarking::Abort() {
625 if (IsStopped()) return;
626 if (FLAG_trace_incremental_marking) {
627 PrintF("[IncrementalMarking] Aborting.\n");
628 }
629 heap_->new_space()->LowerInlineAllocationLimit(0);
630 IncrementalMarking::set_should_hurry(false);
631 ResetStepCounters();
632 if (IsMarking()) {
633 PatchIncrementalMarkingRecordWriteStubs(heap_,
634 RecordWriteStub::STORE_BUFFER_ONLY);
635 DeactivateIncrementalWriteBarrier();
636
637 if (is_compacting_) {
638 LargeObjectIterator it(heap_->lo_space());
639 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
640 Page* p = Page::FromAddress(obj->address());
641 if (p->IsFlagSet(Page::RESCAN_ON_EVACUATION)) {
642 p->ClearFlag(Page::RESCAN_ON_EVACUATION);
643 }
644 }
645 }
646 }
647 heap_->isolate()->stack_guard()->Continue(GC_REQUEST);
648 state_ = STOPPED;
649 is_compacting_ = false;
650}
651
652
653void IncrementalMarking::Finalize() {
654 Hurry();
655 state_ = STOPPED;
656 is_compacting_ = false;
657 heap_->new_space()->LowerInlineAllocationLimit(0);
658 IncrementalMarking::set_should_hurry(false);
659 ResetStepCounters();
660 PatchIncrementalMarkingRecordWriteStubs(heap_,
661 RecordWriteStub::STORE_BUFFER_ONLY);
662 DeactivateIncrementalWriteBarrier();
663 ASSERT(marking_deque_.IsEmpty());
664 heap_->isolate()->stack_guard()->Continue(GC_REQUEST);
665}
666
667
668void IncrementalMarking::MarkingComplete() {
669 state_ = COMPLETE;
670 // We will set the stack guard to request a GC now. This will mean the rest
671 // of the GC gets performed as soon as possible (we can't do a GC here in a
672 // record-write context). If a few things get allocated between now and then
673 // that shouldn't make us do a scavenge and keep being incremental, so we set
674 // the should-hurry flag to indicate that there can't be much work left to do.
675 set_should_hurry(true);
676 if (FLAG_trace_incremental_marking) {
677 PrintF("[IncrementalMarking] Complete (normal).\n");
678 }
679 heap_->isolate()->stack_guard()->RequestGC();
680}
681
682
683void IncrementalMarking::Step(intptr_t allocated_bytes) {
684 if (heap_->gc_state() != Heap::NOT_IN_GC ||
685 !FLAG_incremental_marking ||
686 !FLAG_incremental_marking_steps ||
687 (state_ != SWEEPING && state_ != MARKING)) {
688 return;
689 }
690
691 allocated_ += allocated_bytes;
692
693 if (allocated_ < kAllocatedThreshold) return;
694
695 intptr_t bytes_to_process = allocated_ * allocation_marking_factor_;
696
697 double start = 0;
698
699 if (FLAG_trace_incremental_marking || FLAG_trace_gc) {
700 start = OS::TimeCurrentMillis();
701 }
702
703 if (state_ == SWEEPING) {
704 if (heap_->old_pointer_space()->AdvanceSweeper(bytes_to_process) &&
705 heap_->old_data_space()->AdvanceSweeper(bytes_to_process)) {
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000706 StartMarking(PREVENT_COMPACTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000707 }
708 } else if (state_ == MARKING) {
709 Map* filler_map = heap_->one_pointer_filler_map();
710 Map* global_context_map = heap_->global_context_map();
711 IncrementalMarkingMarkingVisitor marking_visitor(heap_, this);
712 while (!marking_deque_.IsEmpty() && bytes_to_process > 0) {
713 HeapObject* obj = marking_deque_.Pop();
714
715 // Explicitly skip one word fillers. Incremental markbit patterns are
716 // correct only for objects that occupy at least two words.
717 Map* map = obj->map();
718 if (map == filler_map) continue;
719
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000720 int size = obj->SizeFromMap(map);
721 bytes_to_process -= size;
722 MarkBit map_mark_bit = Marking::MarkBitFrom(map);
723 if (Marking::IsWhite(map_mark_bit)) {
724 WhiteToGreyAndPush(map, map_mark_bit);
725 }
726
727 // TODO(gc) switch to static visitor instead of normal visitor.
728 if (map == global_context_map) {
729 // Global contexts have weak fields.
730 Context* ctx = Context::cast(obj);
731
732 // We will mark cache black with a separate pass
733 // when we finish marking.
734 MarkObjectGreyDoNotEnqueue(ctx->normalized_map_cache());
735
736 VisitGlobalContext(ctx, &marking_visitor);
737 } else {
738 obj->IterateBody(map->instance_type(), size, &marking_visitor);
739 }
740
741 MarkBit obj_mark_bit = Marking::MarkBitFrom(obj);
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000742 ASSERT(Marking::IsGrey(obj_mark_bit) ||
743 (obj->IsFiller() && Marking::IsWhite(obj_mark_bit)));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000744 Marking::MarkBlack(obj_mark_bit);
745 MemoryChunk::IncrementLiveBytes(obj->address(), size);
746 }
747 if (marking_deque_.IsEmpty()) MarkingComplete();
748 }
749
750 allocated_ = 0;
751
752 steps_count_++;
753 steps_count_since_last_gc_++;
754
755 bool speed_up = false;
756
757 if (old_generation_space_available_at_start_of_incremental_ < 10 * MB ||
758 SpaceLeftInOldSpace() <
759 old_generation_space_available_at_start_of_incremental_ >> 1) {
760 // Half of the space that was available is gone while we were
761 // incrementally marking.
762 speed_up = true;
763 old_generation_space_available_at_start_of_incremental_ =
764 SpaceLeftInOldSpace();
765 }
766
767 if (heap_->PromotedTotalSize() >
768 old_generation_space_used_at_start_of_incremental_ << 1) {
769 // Size of old space doubled while we were incrementally marking.
770 speed_up = true;
771 old_generation_space_used_at_start_of_incremental_ =
772 heap_->PromotedTotalSize();
773 }
774
775 if ((steps_count_ % kAllocationMarkingFactorSpeedupInterval) == 0 &&
776 allocation_marking_factor_ < kMaxAllocationMarkingFactor) {
777 speed_up = true;
778 }
779
780 if (speed_up && 0) {
781 allocation_marking_factor_ += kAllocationMarkingFactorSpeedup;
782 allocation_marking_factor_ =
783 static_cast<int>(allocation_marking_factor_ * 1.3);
784 if (FLAG_trace_gc) {
785 PrintF("Marking speed increased to %d\n", allocation_marking_factor_);
786 }
787 }
788
789 if (FLAG_trace_incremental_marking || FLAG_trace_gc) {
790 double end = OS::TimeCurrentMillis();
791 double delta = (end - start);
792 longest_step_ = Max(longest_step_, delta);
793 steps_took_ += delta;
794 steps_took_since_last_gc_ += delta;
795 }
796}
797
798
799void IncrementalMarking::ResetStepCounters() {
800 steps_count_ = 0;
801 steps_took_ = 0;
802 longest_step_ = 0.0;
803 old_generation_space_available_at_start_of_incremental_ =
804 SpaceLeftInOldSpace();
805 old_generation_space_used_at_start_of_incremental_ =
806 heap_->PromotedTotalSize();
807 steps_count_since_last_gc_ = 0;
808 steps_took_since_last_gc_ = 0;
809 bytes_rescanned_ = 0;
810 allocation_marking_factor_ = kInitialAllocationMarkingFactor;
811}
812
813
814int64_t IncrementalMarking::SpaceLeftInOldSpace() {
815 return heap_->MaxOldGenerationSize() - heap_->PromotedSpaceSize();
816}
817
818} } // namespace v8::internal