blob: 3f532ead6265f0f07d8530a1696c7f5f35bf5d43 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/heap/scavenger.h"
6
7#include "src/contexts.h"
8#include "src/heap/heap.h"
9#include "src/heap/objects-visiting-inl.h"
10#include "src/heap/scavenger-inl.h"
11#include "src/isolate.h"
12#include "src/log.h"
13#include "src/profiler/cpu-profiler.h"
14
15namespace v8 {
16namespace internal {
17
18enum LoggingAndProfiling {
19 LOGGING_AND_PROFILING_ENABLED,
20 LOGGING_AND_PROFILING_DISABLED
21};
22
23
24enum MarksHandling { TRANSFER_MARKS, IGNORE_MARKS };
25
26
27template <MarksHandling marks_handling,
28 LoggingAndProfiling logging_and_profiling_mode>
29class ScavengingVisitor : public StaticVisitorBase {
30 public:
31 static void Initialize() {
32 table_.Register(kVisitSeqOneByteString, &EvacuateSeqOneByteString);
33 table_.Register(kVisitSeqTwoByteString, &EvacuateSeqTwoByteString);
34 table_.Register(kVisitShortcutCandidate, &EvacuateShortcutCandidate);
35 table_.Register(kVisitByteArray, &EvacuateByteArray);
36 table_.Register(kVisitFixedArray, &EvacuateFixedArray);
37 table_.Register(kVisitFixedDoubleArray, &EvacuateFixedDoubleArray);
38 table_.Register(kVisitFixedTypedArray, &EvacuateFixedTypedArray);
39 table_.Register(kVisitFixedFloat64Array, &EvacuateFixedFloat64Array);
40 table_.Register(kVisitJSArrayBuffer, &EvacuateJSArrayBuffer);
41
42 table_.Register(
43 kVisitNativeContext,
44 &ObjectEvacuationStrategy<POINTER_OBJECT>::template VisitSpecialized<
45 Context::kSize>);
46
47 table_.Register(
48 kVisitConsString,
49 &ObjectEvacuationStrategy<POINTER_OBJECT>::template VisitSpecialized<
50 ConsString::kSize>);
51
52 table_.Register(
53 kVisitSlicedString,
54 &ObjectEvacuationStrategy<POINTER_OBJECT>::template VisitSpecialized<
55 SlicedString::kSize>);
56
57 table_.Register(
58 kVisitSymbol,
59 &ObjectEvacuationStrategy<POINTER_OBJECT>::template VisitSpecialized<
60 Symbol::kSize>);
61
62 table_.Register(
63 kVisitSharedFunctionInfo,
64 &ObjectEvacuationStrategy<POINTER_OBJECT>::template VisitSpecialized<
65 SharedFunctionInfo::kSize>);
66
67 table_.Register(kVisitJSWeakCollection,
68 &ObjectEvacuationStrategy<POINTER_OBJECT>::Visit);
69
70 table_.Register(kVisitJSRegExp,
71 &ObjectEvacuationStrategy<POINTER_OBJECT>::Visit);
72
73 table_.Register(kVisitJSFunction, &EvacuateJSFunction);
74
75 table_.RegisterSpecializations<ObjectEvacuationStrategy<DATA_OBJECT>,
76 kVisitDataObject, kVisitDataObjectGeneric>();
77
78 table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>,
79 kVisitJSObject, kVisitJSObjectGeneric>();
80
81 table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>,
82 kVisitStruct, kVisitStructGeneric>();
83 }
84
85 static VisitorDispatchTable<ScavengingCallback>* GetTable() {
86 return &table_;
87 }
88
89 private:
90 enum ObjectContents { DATA_OBJECT, POINTER_OBJECT };
91
92 static void RecordCopiedObject(Heap* heap, HeapObject* obj) {
93 bool should_record = false;
94#ifdef DEBUG
95 should_record = FLAG_heap_stats;
96#endif
97 should_record = should_record || FLAG_log_gc;
98 if (should_record) {
99 if (heap->new_space()->Contains(obj)) {
100 heap->new_space()->RecordAllocation(obj);
101 } else {
102 heap->new_space()->RecordPromotion(obj);
103 }
104 }
105 }
106
107 // Helper function used by CopyObject to copy a source object to an
108 // allocated target object and update the forwarding pointer in the source
109 // object. Returns the target object.
110 INLINE(static void MigrateObject(Heap* heap, HeapObject* source,
111 HeapObject* target, int size)) {
112 // If we migrate into to-space, then the to-space top pointer should be
113 // right after the target object. Incorporate double alignment
114 // over-allocation.
115 DCHECK(!heap->InToSpace(target) ||
116 target->address() + size == heap->new_space()->top() ||
117 target->address() + size + kPointerSize == heap->new_space()->top());
118
119 // Make sure that we do not overwrite the promotion queue which is at
120 // the end of to-space.
121 DCHECK(!heap->InToSpace(target) ||
122 heap->promotion_queue()->IsBelowPromotionQueue(
123 heap->new_space()->top()));
124
125 // Copy the content of source to target.
126 heap->CopyBlock(target->address(), source->address(), size);
127
128 // Set the forwarding address.
129 source->set_map_word(MapWord::FromForwardingAddress(target));
130
131 if (logging_and_profiling_mode == LOGGING_AND_PROFILING_ENABLED) {
132 // Update NewSpace stats if necessary.
133 RecordCopiedObject(heap, target);
134 heap->OnMoveEvent(target, source, size);
135 }
136
137 if (marks_handling == TRANSFER_MARKS) {
138 if (Marking::TransferColor(source, target)) {
139 MemoryChunk::IncrementLiveBytesFromGC(target, size);
140 }
141 }
142 }
143
144 template <AllocationAlignment alignment>
145 static inline bool SemiSpaceCopyObject(Map* map, HeapObject** slot,
146 HeapObject* object, int object_size) {
147 Heap* heap = map->GetHeap();
148
149 DCHECK(heap->AllowedToBeMigrated(object, NEW_SPACE));
150 AllocationResult allocation =
151 heap->new_space()->AllocateRaw(object_size, alignment);
152
153 HeapObject* target = NULL; // Initialization to please compiler.
154 if (allocation.To(&target)) {
155 // Order is important here: Set the promotion limit before storing a
156 // filler for double alignment or migrating the object. Otherwise we
157 // may end up overwriting promotion queue entries when we migrate the
158 // object.
159 heap->promotion_queue()->SetNewLimit(heap->new_space()->top());
160
161 MigrateObject(heap, object, target, object_size);
162
163 // Update slot to new target.
164 *slot = target;
165
166 heap->IncrementSemiSpaceCopiedObjectSize(object_size);
167 return true;
168 }
169 return false;
170 }
171
172
173 template <ObjectContents object_contents, AllocationAlignment alignment>
174 static inline bool PromoteObject(Map* map, HeapObject** slot,
175 HeapObject* object, int object_size) {
176 Heap* heap = map->GetHeap();
177
178 AllocationResult allocation =
179 heap->old_space()->AllocateRaw(object_size, alignment);
180
181 HeapObject* target = NULL; // Initialization to please compiler.
182 if (allocation.To(&target)) {
183 MigrateObject(heap, object, target, object_size);
184
185 // Update slot to new target.
186 *slot = target;
187
188 if (object_contents == POINTER_OBJECT) {
Ben Murdochda12d292016-06-02 14:46:10 +0100189 heap->promotion_queue()->insert(
190 target, object_size,
191 Marking::IsBlack(Marking::MarkBitFrom(object)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 }
193 heap->IncrementPromotedObjectsSize(object_size);
194 return true;
195 }
196 return false;
197 }
198
199
200 template <ObjectContents object_contents, AllocationAlignment alignment>
201 static inline void EvacuateObject(Map* map, HeapObject** slot,
202 HeapObject* object, int object_size) {
203 SLOW_DCHECK(object_size <= Page::kAllocatableMemory);
204 SLOW_DCHECK(object->Size() == object_size);
205 Heap* heap = map->GetHeap();
206
207 if (!heap->ShouldBePromoted(object->address(), object_size)) {
208 // A semi-space copy may fail due to fragmentation. In that case, we
209 // try to promote the object.
210 if (SemiSpaceCopyObject<alignment>(map, slot, object, object_size)) {
211 return;
212 }
213 }
214
215 if (PromoteObject<object_contents, alignment>(map, slot, object,
216 object_size)) {
217 return;
218 }
219
220 // If promotion failed, we try to copy the object to the other semi-space
221 if (SemiSpaceCopyObject<alignment>(map, slot, object, object_size)) return;
222
223 FatalProcessOutOfMemory("Scavenger: semi-space copy\n");
224 }
225
226
227 static inline void EvacuateJSFunction(Map* map, HeapObject** slot,
228 HeapObject* object) {
229 ObjectEvacuationStrategy<POINTER_OBJECT>::Visit(map, slot, object);
230
231 if (marks_handling == IGNORE_MARKS) return;
232
233 MapWord map_word = object->map_word();
234 DCHECK(map_word.IsForwardingAddress());
235 HeapObject* target = map_word.ToForwardingAddress();
236
237 MarkBit mark_bit = Marking::MarkBitFrom(target);
238 if (Marking::IsBlack(mark_bit)) {
239 // This object is black and it might not be rescanned by marker.
240 // We should explicitly record code entry slot for compaction because
Ben Murdochda12d292016-06-02 14:46:10 +0100241 // promotion queue processing (IteratePromotedObjectPointers) will
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000242 // miss it as it is not HeapObject-tagged.
243 Address code_entry_slot =
244 target->address() + JSFunction::kCodeEntryOffset;
245 Code* code = Code::cast(Code::GetObjectFromEntryAddress(code_entry_slot));
246 map->GetHeap()->mark_compact_collector()->RecordCodeEntrySlot(
247 target, code_entry_slot, code);
248 }
249 }
250
251
252 static inline void EvacuateFixedArray(Map* map, HeapObject** slot,
253 HeapObject* object) {
254 int length = reinterpret_cast<FixedArray*>(object)->synchronized_length();
255 int object_size = FixedArray::SizeFor(length);
256 EvacuateObject<POINTER_OBJECT, kWordAligned>(map, slot, object,
257 object_size);
258 }
259
260
261 static inline void EvacuateFixedDoubleArray(Map* map, HeapObject** slot,
262 HeapObject* object) {
263 int length = reinterpret_cast<FixedDoubleArray*>(object)->length();
264 int object_size = FixedDoubleArray::SizeFor(length);
265 EvacuateObject<DATA_OBJECT, kDoubleAligned>(map, slot, object, object_size);
266 }
267
268
269 static inline void EvacuateFixedTypedArray(Map* map, HeapObject** slot,
270 HeapObject* object) {
271 int object_size = reinterpret_cast<FixedTypedArrayBase*>(object)->size();
272 EvacuateObject<POINTER_OBJECT, kWordAligned>(map, slot, object,
273 object_size);
274 }
275
276
277 static inline void EvacuateFixedFloat64Array(Map* map, HeapObject** slot,
278 HeapObject* object) {
279 int object_size = reinterpret_cast<FixedFloat64Array*>(object)->size();
280 EvacuateObject<POINTER_OBJECT, kDoubleAligned>(map, slot, object,
281 object_size);
282 }
283
284
285 static inline void EvacuateJSArrayBuffer(Map* map, HeapObject** slot,
286 HeapObject* object) {
287 ObjectEvacuationStrategy<POINTER_OBJECT>::Visit(map, slot, object);
288
289 Heap* heap = map->GetHeap();
290 MapWord map_word = object->map_word();
291 DCHECK(map_word.IsForwardingAddress());
292 HeapObject* target = map_word.ToForwardingAddress();
293 if (!heap->InNewSpace(target)) {
294 heap->array_buffer_tracker()->Promote(JSArrayBuffer::cast(target));
295 }
296 }
297
298
299 static inline void EvacuateByteArray(Map* map, HeapObject** slot,
300 HeapObject* object) {
301 int object_size = reinterpret_cast<ByteArray*>(object)->ByteArraySize();
302 EvacuateObject<DATA_OBJECT, kWordAligned>(map, slot, object, object_size);
303 }
304
305
306 static inline void EvacuateSeqOneByteString(Map* map, HeapObject** slot,
307 HeapObject* object) {
308 int object_size = SeqOneByteString::cast(object)
309 ->SeqOneByteStringSize(map->instance_type());
310 EvacuateObject<DATA_OBJECT, kWordAligned>(map, slot, object, object_size);
311 }
312
313
314 static inline void EvacuateSeqTwoByteString(Map* map, HeapObject** slot,
315 HeapObject* object) {
316 int object_size = SeqTwoByteString::cast(object)
317 ->SeqTwoByteStringSize(map->instance_type());
318 EvacuateObject<DATA_OBJECT, kWordAligned>(map, slot, object, object_size);
319 }
320
321
322 static inline void EvacuateShortcutCandidate(Map* map, HeapObject** slot,
323 HeapObject* object) {
324 DCHECK(IsShortcutCandidate(map->instance_type()));
325
326 Heap* heap = map->GetHeap();
327
328 if (marks_handling == IGNORE_MARKS &&
329 ConsString::cast(object)->unchecked_second() == heap->empty_string()) {
330 HeapObject* first =
331 HeapObject::cast(ConsString::cast(object)->unchecked_first());
332
333 *slot = first;
334
335 if (!heap->InNewSpace(first)) {
336 object->set_map_word(MapWord::FromForwardingAddress(first));
337 return;
338 }
339
340 MapWord first_word = first->map_word();
341 if (first_word.IsForwardingAddress()) {
342 HeapObject* target = first_word.ToForwardingAddress();
343
344 *slot = target;
345 object->set_map_word(MapWord::FromForwardingAddress(target));
346 return;
347 }
348
349 Scavenger::ScavengeObjectSlow(slot, first);
350 object->set_map_word(MapWord::FromForwardingAddress(*slot));
351 return;
352 }
353
354 int object_size = ConsString::kSize;
355 EvacuateObject<POINTER_OBJECT, kWordAligned>(map, slot, object,
356 object_size);
357 }
358
359 template <ObjectContents object_contents>
360 class ObjectEvacuationStrategy {
361 public:
362 template <int object_size>
363 static inline void VisitSpecialized(Map* map, HeapObject** slot,
364 HeapObject* object) {
365 EvacuateObject<object_contents, kWordAligned>(map, slot, object,
366 object_size);
367 }
368
369 static inline void Visit(Map* map, HeapObject** slot, HeapObject* object) {
370 int object_size = map->instance_size();
371 EvacuateObject<object_contents, kWordAligned>(map, slot, object,
372 object_size);
373 }
374 };
375
376 static VisitorDispatchTable<ScavengingCallback> table_;
377};
378
379
380template <MarksHandling marks_handling,
381 LoggingAndProfiling logging_and_profiling_mode>
382VisitorDispatchTable<ScavengingCallback>
383 ScavengingVisitor<marks_handling, logging_and_profiling_mode>::table_;
384
385
386// static
387void Scavenger::Initialize() {
388 ScavengingVisitor<TRANSFER_MARKS,
389 LOGGING_AND_PROFILING_DISABLED>::Initialize();
390 ScavengingVisitor<IGNORE_MARKS, LOGGING_AND_PROFILING_DISABLED>::Initialize();
391 ScavengingVisitor<TRANSFER_MARKS,
392 LOGGING_AND_PROFILING_ENABLED>::Initialize();
393 ScavengingVisitor<IGNORE_MARKS, LOGGING_AND_PROFILING_ENABLED>::Initialize();
394}
395
396
397// static
398void Scavenger::ScavengeObjectSlow(HeapObject** p, HeapObject* object) {
399 SLOW_DCHECK(object->GetIsolate()->heap()->InFromSpace(object));
400 MapWord first_word = object->map_word();
401 SLOW_DCHECK(!first_word.IsForwardingAddress());
402 Map* map = first_word.ToMap();
403 Scavenger* scavenger = map->GetHeap()->scavenge_collector_;
404 scavenger->scavenging_visitors_table_.GetVisitor(map)(map, p, object);
405}
406
407
408void Scavenger::SelectScavengingVisitorsTable() {
409 bool logging_and_profiling =
410 FLAG_verify_predictable || isolate()->logger()->is_logging() ||
411 isolate()->cpu_profiler()->is_profiling() ||
412 (isolate()->heap_profiler() != NULL &&
413 isolate()->heap_profiler()->is_tracking_object_moves());
414
415 if (!heap()->incremental_marking()->IsMarking()) {
416 if (!logging_and_profiling) {
417 scavenging_visitors_table_.CopyFrom(
418 ScavengingVisitor<IGNORE_MARKS,
419 LOGGING_AND_PROFILING_DISABLED>::GetTable());
420 } else {
421 scavenging_visitors_table_.CopyFrom(
422 ScavengingVisitor<IGNORE_MARKS,
423 LOGGING_AND_PROFILING_ENABLED>::GetTable());
424 }
425 } else {
426 if (!logging_and_profiling) {
427 scavenging_visitors_table_.CopyFrom(
428 ScavengingVisitor<TRANSFER_MARKS,
429 LOGGING_AND_PROFILING_DISABLED>::GetTable());
430 } else {
431 scavenging_visitors_table_.CopyFrom(
432 ScavengingVisitor<TRANSFER_MARKS,
433 LOGGING_AND_PROFILING_ENABLED>::GetTable());
434 }
435
436 if (heap()->incremental_marking()->IsCompacting()) {
437 // When compacting forbid short-circuiting of cons-strings.
438 // Scavenging code relies on the fact that new space object
439 // can't be evacuated into evacuation candidate but
440 // short-circuiting violates this assumption.
441 scavenging_visitors_table_.Register(
442 StaticVisitorBase::kVisitShortcutCandidate,
443 scavenging_visitors_table_.GetVisitorById(
444 StaticVisitorBase::kVisitConsString));
445 }
446 }
447}
448
449
450Isolate* Scavenger::isolate() { return heap()->isolate(); }
451
452
453void ScavengeVisitor::VisitPointer(Object** p) { ScavengePointer(p); }
454
455
456void ScavengeVisitor::VisitPointers(Object** start, Object** end) {
457 // Copy all HeapObject pointers in [start, end)
458 for (Object** p = start; p < end; p++) ScavengePointer(p);
459}
460
461
462void ScavengeVisitor::ScavengePointer(Object** p) {
463 Object* object = *p;
464 if (!heap_->InNewSpace(object)) return;
465 Scavenger::ScavengeObject(reinterpret_cast<HeapObject**>(p),
466 reinterpret_cast<HeapObject*>(object));
467}
468
469} // namespace internal
470} // namespace v8