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