blob: 1c6fac009b0daf26dad27b74ecb5ed54c2801cb8 [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()) {
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000413 StartMarking(ALLOW_COMPACTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000414 } 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
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000438void IncrementalMarking::StartMarking(CompactionFlag flag) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000439 if (FLAG_trace_incremental_marking) {
440 PrintF("[IncrementalMarking] Start marking\n");
441 }
442
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000443 is_compacting_ = !FLAG_never_compact && (flag == ALLOW_COMPACTION) &&
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000444 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());
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000520#ifdef DEBUG
521 MarkBit mark_bit = Marking::MarkBitFrom(obj);
522 ASSERT(Marking::IsGrey(mark_bit) ||
523 (obj->IsFiller() && Marking::IsWhite(mark_bit)));
524#endif
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000525 }
526 } else if (obj->map() != filler_map) {
527 // Skip one word filler objects that appear on the
528 // stack when we perform in place array shift.
529 array[new_top] = obj;
530 new_top = ((new_top + 1) & mask);
531 ASSERT(new_top != marking_deque_.bottom());
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000532#ifdef DEBUG
533 MarkBit mark_bit = Marking::MarkBitFrom(obj);
534 ASSERT(Marking::IsGrey(mark_bit) ||
535 (obj->IsFiller() && Marking::IsWhite(mark_bit)));
536#endif
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000537 }
538 }
539 marking_deque_.set_top(new_top);
540
541 steps_took_since_last_gc_ = 0;
542 steps_count_since_last_gc_ = 0;
543 longest_step_ = 0.0;
544}
545
546
547void IncrementalMarking::VisitGlobalContext(Context* ctx, ObjectVisitor* v) {
548 v->VisitPointers(
549 HeapObject::RawField(
550 ctx, Context::MarkCompactBodyDescriptor::kStartOffset),
551 HeapObject::RawField(
552 ctx, Context::MarkCompactBodyDescriptor::kEndOffset));
553
554 MarkCompactCollector* collector = heap_->mark_compact_collector();
555 for (int idx = Context::FIRST_WEAK_SLOT;
556 idx < Context::GLOBAL_CONTEXT_SLOTS;
557 ++idx) {
558 Object** slot =
559 HeapObject::RawField(ctx, FixedArray::OffsetOfElementAt(idx));
560 collector->RecordSlot(slot, slot, *slot);
561 }
562}
563
564
565void IncrementalMarking::Hurry() {
566 if (state() == MARKING) {
567 double start = 0.0;
568 if (FLAG_trace_incremental_marking) {
569 PrintF("[IncrementalMarking] Hurry\n");
570 start = OS::TimeCurrentMillis();
571 }
572 // TODO(gc) hurry can mark objects it encounters black as mutator
573 // was stopped.
574 Map* filler_map = heap_->one_pointer_filler_map();
575 Map* global_context_map = heap_->global_context_map();
576 IncrementalMarkingMarkingVisitor marking_visitor(heap_, this);
577 while (!marking_deque_.IsEmpty()) {
578 HeapObject* obj = marking_deque_.Pop();
579
580 // Explicitly skip one word fillers. Incremental markbit patterns are
581 // correct only for objects that occupy at least two words.
582 Map* map = obj->map();
583 if (map == filler_map) {
584 continue;
585 } else if (map == global_context_map) {
586 // Global contexts have weak fields.
587 VisitGlobalContext(Context::cast(obj), &marking_visitor);
588 } else {
589 obj->Iterate(&marking_visitor);
590 }
591
592 MarkBit mark_bit = Marking::MarkBitFrom(obj);
593 ASSERT(!Marking::IsBlack(mark_bit));
594 Marking::MarkBlack(mark_bit);
595 MemoryChunk::IncrementLiveBytes(obj->address(), obj->Size());
596 }
597 state_ = COMPLETE;
598 if (FLAG_trace_incremental_marking) {
599 double end = OS::TimeCurrentMillis();
600 PrintF("[IncrementalMarking] Complete (hurry), spent %d ms.\n",
601 static_cast<int>(end - start));
602 }
603 }
604
605 if (FLAG_cleanup_code_caches_at_gc) {
606 PolymorphicCodeCache* poly_cache = heap_->polymorphic_code_cache();
607 Marking::GreyToBlack(Marking::MarkBitFrom(poly_cache));
608 MemoryChunk::IncrementLiveBytes(poly_cache->address(),
609 PolymorphicCodeCache::kSize);
610 }
611
612 Object* context = heap_->global_contexts_list();
613 while (!context->IsUndefined()) {
614 NormalizedMapCache* cache = Context::cast(context)->normalized_map_cache();
615 MarkBit mark_bit = Marking::MarkBitFrom(cache);
616 if (Marking::IsGrey(mark_bit)) {
617 Marking::GreyToBlack(mark_bit);
618 MemoryChunk::IncrementLiveBytes(cache->address(), cache->Size());
619 }
620 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
621 }
622}
623
624
625void IncrementalMarking::Abort() {
626 if (IsStopped()) return;
627 if (FLAG_trace_incremental_marking) {
628 PrintF("[IncrementalMarking] Aborting.\n");
629 }
630 heap_->new_space()->LowerInlineAllocationLimit(0);
631 IncrementalMarking::set_should_hurry(false);
632 ResetStepCounters();
633 if (IsMarking()) {
634 PatchIncrementalMarkingRecordWriteStubs(heap_,
635 RecordWriteStub::STORE_BUFFER_ONLY);
636 DeactivateIncrementalWriteBarrier();
637
638 if (is_compacting_) {
639 LargeObjectIterator it(heap_->lo_space());
640 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
641 Page* p = Page::FromAddress(obj->address());
642 if (p->IsFlagSet(Page::RESCAN_ON_EVACUATION)) {
643 p->ClearFlag(Page::RESCAN_ON_EVACUATION);
644 }
645 }
646 }
647 }
648 heap_->isolate()->stack_guard()->Continue(GC_REQUEST);
649 state_ = STOPPED;
650 is_compacting_ = false;
651}
652
653
654void IncrementalMarking::Finalize() {
655 Hurry();
656 state_ = STOPPED;
657 is_compacting_ = false;
658 heap_->new_space()->LowerInlineAllocationLimit(0);
659 IncrementalMarking::set_should_hurry(false);
660 ResetStepCounters();
661 PatchIncrementalMarkingRecordWriteStubs(heap_,
662 RecordWriteStub::STORE_BUFFER_ONLY);
663 DeactivateIncrementalWriteBarrier();
664 ASSERT(marking_deque_.IsEmpty());
665 heap_->isolate()->stack_guard()->Continue(GC_REQUEST);
666}
667
668
669void IncrementalMarking::MarkingComplete() {
670 state_ = COMPLETE;
671 // We will set the stack guard to request a GC now. This will mean the rest
672 // of the GC gets performed as soon as possible (we can't do a GC here in a
673 // record-write context). If a few things get allocated between now and then
674 // that shouldn't make us do a scavenge and keep being incremental, so we set
675 // the should-hurry flag to indicate that there can't be much work left to do.
676 set_should_hurry(true);
677 if (FLAG_trace_incremental_marking) {
678 PrintF("[IncrementalMarking] Complete (normal).\n");
679 }
680 heap_->isolate()->stack_guard()->RequestGC();
681}
682
683
684void IncrementalMarking::Step(intptr_t allocated_bytes) {
685 if (heap_->gc_state() != Heap::NOT_IN_GC ||
686 !FLAG_incremental_marking ||
687 !FLAG_incremental_marking_steps ||
688 (state_ != SWEEPING && state_ != MARKING)) {
689 return;
690 }
691
692 allocated_ += allocated_bytes;
693
694 if (allocated_ < kAllocatedThreshold) return;
695
696 intptr_t bytes_to_process = allocated_ * allocation_marking_factor_;
697
698 double start = 0;
699
700 if (FLAG_trace_incremental_marking || FLAG_trace_gc) {
701 start = OS::TimeCurrentMillis();
702 }
703
704 if (state_ == SWEEPING) {
705 if (heap_->old_pointer_space()->AdvanceSweeper(bytes_to_process) &&
706 heap_->old_data_space()->AdvanceSweeper(bytes_to_process)) {
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000707 StartMarking(PREVENT_COMPACTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000708 }
709 } else if (state_ == MARKING) {
710 Map* filler_map = heap_->one_pointer_filler_map();
711 Map* global_context_map = heap_->global_context_map();
712 IncrementalMarkingMarkingVisitor marking_visitor(heap_, this);
713 while (!marking_deque_.IsEmpty() && bytes_to_process > 0) {
714 HeapObject* obj = marking_deque_.Pop();
715
716 // Explicitly skip one word fillers. Incremental markbit patterns are
717 // correct only for objects that occupy at least two words.
718 Map* map = obj->map();
719 if (map == filler_map) continue;
720
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000721 int size = obj->SizeFromMap(map);
722 bytes_to_process -= size;
723 MarkBit map_mark_bit = Marking::MarkBitFrom(map);
724 if (Marking::IsWhite(map_mark_bit)) {
725 WhiteToGreyAndPush(map, map_mark_bit);
726 }
727
728 // TODO(gc) switch to static visitor instead of normal visitor.
729 if (map == global_context_map) {
730 // Global contexts have weak fields.
731 Context* ctx = Context::cast(obj);
732
733 // We will mark cache black with a separate pass
734 // when we finish marking.
735 MarkObjectGreyDoNotEnqueue(ctx->normalized_map_cache());
736
737 VisitGlobalContext(ctx, &marking_visitor);
738 } else {
739 obj->IterateBody(map->instance_type(), size, &marking_visitor);
740 }
741
742 MarkBit obj_mark_bit = Marking::MarkBitFrom(obj);
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +0000743 ASSERT(Marking::IsGrey(obj_mark_bit) ||
744 (obj->IsFiller() && Marking::IsWhite(obj_mark_bit)));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000745 Marking::MarkBlack(obj_mark_bit);
746 MemoryChunk::IncrementLiveBytes(obj->address(), size);
747 }
748 if (marking_deque_.IsEmpty()) MarkingComplete();
749 }
750
751 allocated_ = 0;
752
753 steps_count_++;
754 steps_count_since_last_gc_++;
755
756 bool speed_up = false;
757
758 if (old_generation_space_available_at_start_of_incremental_ < 10 * MB ||
759 SpaceLeftInOldSpace() <
760 old_generation_space_available_at_start_of_incremental_ >> 1) {
761 // Half of the space that was available is gone while we were
762 // incrementally marking.
763 speed_up = true;
764 old_generation_space_available_at_start_of_incremental_ =
765 SpaceLeftInOldSpace();
766 }
767
768 if (heap_->PromotedTotalSize() >
769 old_generation_space_used_at_start_of_incremental_ << 1) {
770 // Size of old space doubled while we were incrementally marking.
771 speed_up = true;
772 old_generation_space_used_at_start_of_incremental_ =
773 heap_->PromotedTotalSize();
774 }
775
776 if ((steps_count_ % kAllocationMarkingFactorSpeedupInterval) == 0 &&
777 allocation_marking_factor_ < kMaxAllocationMarkingFactor) {
778 speed_up = true;
779 }
780
781 if (speed_up && 0) {
782 allocation_marking_factor_ += kAllocationMarkingFactorSpeedup;
783 allocation_marking_factor_ =
784 static_cast<int>(allocation_marking_factor_ * 1.3);
785 if (FLAG_trace_gc) {
786 PrintF("Marking speed increased to %d\n", allocation_marking_factor_);
787 }
788 }
789
790 if (FLAG_trace_incremental_marking || FLAG_trace_gc) {
791 double end = OS::TimeCurrentMillis();
792 double delta = (end - start);
793 longest_step_ = Max(longest_step_, delta);
794 steps_took_ += delta;
795 steps_took_since_last_gc_ += delta;
796 }
797}
798
799
800void IncrementalMarking::ResetStepCounters() {
801 steps_count_ = 0;
802 steps_took_ = 0;
803 longest_step_ = 0.0;
804 old_generation_space_available_at_start_of_incremental_ =
805 SpaceLeftInOldSpace();
806 old_generation_space_used_at_start_of_incremental_ =
807 heap_->PromotedTotalSize();
808 steps_count_since_last_gc_ = 0;
809 steps_took_since_last_gc_ = 0;
810 bytes_rescanned_ = 0;
811 allocation_marking_factor_ = kInitialAllocationMarkingFactor;
812}
813
814
815int64_t IncrementalMarking::SpaceLeftInOldSpace() {
816 return heap_->MaxOldGenerationSize() - heap_->PromotedSpaceSize();
817}
818
819} } // namespace v8::internal