ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 30 | #include "code-stubs.h" |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 31 | #include "compilation-cache.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 32 | #include "cpu-profiler.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 33 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | #include "execution.h" |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 35 | #include "gdb-jit.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "global-handles.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 37 | #include "heap-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | #include "ic-inl.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 39 | #include "incremental-marking.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | #include "mark-compact.h" |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 41 | #include "objects-visiting.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 42 | #include "objects-visiting-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | #include "stub-cache.h" |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 44 | #include "sweeper-thread.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 46 | namespace v8 { |
| 47 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 49 | |
| 50 | const char* Marking::kWhiteBitPattern = "00"; |
| 51 | const char* Marking::kBlackBitPattern = "10"; |
| 52 | const char* Marking::kGreyBitPattern = "11"; |
| 53 | const char* Marking::kImpossibleBitPattern = "01"; |
| 54 | |
| 55 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 56 | // ------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | // MarkCompactCollector |
| 58 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 59 | MarkCompactCollector::MarkCompactCollector() : // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 61 | state_(IDLE), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 63 | sweep_precisely_(false), |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 64 | reduce_memory_footprint_(false), |
| 65 | abort_incremental_marking_(false), |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 66 | marking_parity_(ODD_MARKING_PARITY), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 67 | compacting_(false), |
| 68 | was_marked_incrementally_(false), |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 69 | sweeping_pending_(false), |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 70 | sequential_sweeping_(false), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 71 | tracer_(NULL), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 72 | migration_slots_buffer_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 73 | heap_(NULL), |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 74 | code_flusher_(NULL), |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 75 | encountered_weak_collections_(NULL), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 76 | have_code_to_deoptimize_(false) { } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 77 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 78 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 79 | class VerifyMarkingVisitor: public ObjectVisitor { |
| 80 | public: |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 81 | explicit VerifyMarkingVisitor(Heap* heap) : heap_(heap) {} |
| 82 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 83 | void VisitPointers(Object** start, Object** end) { |
| 84 | for (Object** current = start; current < end; current++) { |
| 85 | if ((*current)->IsHeapObject()) { |
| 86 | HeapObject* object = HeapObject::cast(*current); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 87 | CHECK(heap_->mark_compact_collector()->IsMarked(object)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 91 | |
| 92 | void VisitEmbeddedPointer(RelocInfo* rinfo) { |
| 93 | ASSERT(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 94 | if (!Code::IsWeakEmbeddedObject(rinfo->host()->kind(), |
| 95 | rinfo->target_object())) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 96 | Object* p = rinfo->target_object(); |
| 97 | VisitPointer(&p); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | Heap* heap_; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 106 | static void VerifyMarking(Heap* heap, Address bottom, Address top) { |
| 107 | VerifyMarkingVisitor visitor(heap); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 108 | HeapObject* object; |
| 109 | Address next_object_must_be_here_or_later = bottom; |
| 110 | |
| 111 | for (Address current = bottom; |
| 112 | current < top; |
| 113 | current += kPointerSize) { |
| 114 | object = HeapObject::FromAddress(current); |
| 115 | if (MarkCompactCollector::IsMarked(object)) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 116 | CHECK(current >= next_object_must_be_here_or_later); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 117 | object->Iterate(&visitor); |
| 118 | next_object_must_be_here_or_later = current + object->Size(); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static void VerifyMarking(NewSpace* space) { |
| 125 | Address end = space->top(); |
| 126 | NewSpacePageIterator it(space->bottom(), end); |
| 127 | // The bottom position is at the start of its page. Allows us to use |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 128 | // page->area_start() as start of range on all pages. |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 129 | CHECK_EQ(space->bottom(), |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 130 | NewSpacePage::FromAddress(space->bottom())->area_start()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 131 | while (it.has_next()) { |
| 132 | NewSpacePage* page = it.next(); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 133 | Address limit = it.has_next() ? page->area_end() : end; |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 134 | CHECK(limit == end || !page->Contains(end)); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 135 | VerifyMarking(space->heap(), page->area_start(), limit); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | static void VerifyMarking(PagedSpace* space) { |
| 141 | PageIterator it(space); |
| 142 | |
| 143 | while (it.has_next()) { |
| 144 | Page* p = it.next(); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 145 | VerifyMarking(space->heap(), p->area_start(), p->area_end()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static void VerifyMarking(Heap* heap) { |
| 151 | VerifyMarking(heap->old_pointer_space()); |
| 152 | VerifyMarking(heap->old_data_space()); |
| 153 | VerifyMarking(heap->code_space()); |
| 154 | VerifyMarking(heap->cell_space()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 155 | VerifyMarking(heap->property_cell_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 156 | VerifyMarking(heap->map_space()); |
| 157 | VerifyMarking(heap->new_space()); |
| 158 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 159 | VerifyMarkingVisitor visitor(heap); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 160 | |
| 161 | LargeObjectIterator it(heap->lo_space()); |
| 162 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
| 163 | if (MarkCompactCollector::IsMarked(obj)) { |
| 164 | obj->Iterate(&visitor); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | heap->IterateStrongRoots(&visitor, VISIT_ONLY_STRONG); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | class VerifyEvacuationVisitor: public ObjectVisitor { |
| 173 | public: |
| 174 | void VisitPointers(Object** start, Object** end) { |
| 175 | for (Object** current = start; current < end; current++) { |
| 176 | if ((*current)->IsHeapObject()) { |
| 177 | HeapObject* object = HeapObject::cast(*current); |
| 178 | CHECK(!MarkCompactCollector::IsOnEvacuationCandidate(object)); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | static void VerifyEvacuation(Address bottom, Address top) { |
| 186 | VerifyEvacuationVisitor visitor; |
| 187 | HeapObject* object; |
| 188 | Address next_object_must_be_here_or_later = bottom; |
| 189 | |
| 190 | for (Address current = bottom; |
| 191 | current < top; |
| 192 | current += kPointerSize) { |
| 193 | object = HeapObject::FromAddress(current); |
| 194 | if (MarkCompactCollector::IsMarked(object)) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 195 | CHECK(current >= next_object_must_be_here_or_later); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 196 | object->Iterate(&visitor); |
| 197 | next_object_must_be_here_or_later = current + object->Size(); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static void VerifyEvacuation(NewSpace* space) { |
| 204 | NewSpacePageIterator it(space->bottom(), space->top()); |
| 205 | VerifyEvacuationVisitor visitor; |
| 206 | |
| 207 | while (it.has_next()) { |
| 208 | NewSpacePage* page = it.next(); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 209 | Address current = page->area_start(); |
| 210 | Address limit = it.has_next() ? page->area_end() : space->top(); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 211 | CHECK(limit == space->top() || !page->Contains(space->top())); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 212 | while (current < limit) { |
| 213 | HeapObject* object = HeapObject::FromAddress(current); |
| 214 | object->Iterate(&visitor); |
| 215 | current += object->Size(); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | static void VerifyEvacuation(PagedSpace* space) { |
| 222 | PageIterator it(space); |
| 223 | |
| 224 | while (it.has_next()) { |
| 225 | Page* p = it.next(); |
| 226 | if (p->IsEvacuationCandidate()) continue; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 227 | VerifyEvacuation(p->area_start(), p->area_end()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static void VerifyEvacuation(Heap* heap) { |
| 233 | VerifyEvacuation(heap->old_pointer_space()); |
| 234 | VerifyEvacuation(heap->old_data_space()); |
| 235 | VerifyEvacuation(heap->code_space()); |
| 236 | VerifyEvacuation(heap->cell_space()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 237 | VerifyEvacuation(heap->property_cell_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 238 | VerifyEvacuation(heap->map_space()); |
| 239 | VerifyEvacuation(heap->new_space()); |
| 240 | |
| 241 | VerifyEvacuationVisitor visitor; |
| 242 | heap->IterateStrongRoots(&visitor, VISIT_ALL); |
| 243 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 244 | #endif // VERIFY_HEAP |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 245 | |
| 246 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 247 | #ifdef DEBUG |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 248 | class VerifyNativeContextSeparationVisitor: public ObjectVisitor { |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 249 | public: |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 250 | VerifyNativeContextSeparationVisitor() : current_native_context_(NULL) {} |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 251 | |
| 252 | void VisitPointers(Object** start, Object** end) { |
| 253 | for (Object** current = start; current < end; current++) { |
| 254 | if ((*current)->IsHeapObject()) { |
| 255 | HeapObject* object = HeapObject::cast(*current); |
| 256 | if (object->IsString()) continue; |
| 257 | switch (object->map()->instance_type()) { |
| 258 | case JS_FUNCTION_TYPE: |
| 259 | CheckContext(JSFunction::cast(object)->context()); |
| 260 | break; |
| 261 | case JS_GLOBAL_PROXY_TYPE: |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 262 | CheckContext(JSGlobalProxy::cast(object)->native_context()); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 263 | break; |
| 264 | case JS_GLOBAL_OBJECT_TYPE: |
| 265 | case JS_BUILTINS_OBJECT_TYPE: |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 266 | CheckContext(GlobalObject::cast(object)->native_context()); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 267 | break; |
| 268 | case JS_ARRAY_TYPE: |
| 269 | case JS_DATE_TYPE: |
| 270 | case JS_OBJECT_TYPE: |
| 271 | case JS_REGEXP_TYPE: |
| 272 | VisitPointer(HeapObject::RawField(object, JSObject::kMapOffset)); |
| 273 | break; |
| 274 | case MAP_TYPE: |
| 275 | VisitPointer(HeapObject::RawField(object, Map::kPrototypeOffset)); |
| 276 | VisitPointer(HeapObject::RawField(object, Map::kConstructorOffset)); |
| 277 | break; |
| 278 | case FIXED_ARRAY_TYPE: |
| 279 | if (object->IsContext()) { |
| 280 | CheckContext(object); |
| 281 | } else { |
| 282 | FixedArray* array = FixedArray::cast(object); |
| 283 | int length = array->length(); |
| 284 | // Set array length to zero to prevent cycles while iterating |
| 285 | // over array bodies, this is easier than intrusive marking. |
| 286 | array->set_length(0); |
| 287 | array->IterateBody( |
| 288 | FIXED_ARRAY_TYPE, FixedArray::SizeFor(length), this); |
| 289 | array->set_length(length); |
| 290 | } |
| 291 | break; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 292 | case CELL_TYPE: |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 293 | case JS_PROXY_TYPE: |
| 294 | case JS_VALUE_TYPE: |
| 295 | case TYPE_FEEDBACK_INFO_TYPE: |
| 296 | object->Iterate(this); |
| 297 | break; |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 298 | case DECLARED_ACCESSOR_INFO_TYPE: |
| 299 | case EXECUTABLE_ACCESSOR_INFO_TYPE: |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 300 | case BYTE_ARRAY_TYPE: |
| 301 | case CALL_HANDLER_INFO_TYPE: |
| 302 | case CODE_TYPE: |
| 303 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 304 | case HEAP_NUMBER_TYPE: |
| 305 | case INTERCEPTOR_INFO_TYPE: |
| 306 | case ODDBALL_TYPE: |
| 307 | case SCRIPT_TYPE: |
| 308 | case SHARED_FUNCTION_INFO_TYPE: |
| 309 | break; |
| 310 | default: |
| 311 | UNREACHABLE(); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private: |
| 318 | void CheckContext(Object* context) { |
| 319 | if (!context->IsContext()) return; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 320 | Context* native_context = Context::cast(context)->native_context(); |
| 321 | if (current_native_context_ == NULL) { |
| 322 | current_native_context_ = native_context; |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 323 | } else { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 324 | CHECK_EQ(current_native_context_, native_context); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 328 | Context* current_native_context_; |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 332 | static void VerifyNativeContextSeparation(Heap* heap) { |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 333 | HeapObjectIterator it(heap->code_space()); |
| 334 | |
| 335 | for (Object* object = it.Next(); object != NULL; object = it.Next()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 336 | VerifyNativeContextSeparationVisitor visitor; |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 337 | Code::cast(object)->CodeIterateBody(&visitor); |
| 338 | } |
| 339 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 340 | #endif |
| 341 | |
| 342 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 343 | void MarkCompactCollector::TearDown() { |
| 344 | AbortCompaction(); |
| 345 | } |
| 346 | |
| 347 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 348 | void MarkCompactCollector::AddEvacuationCandidate(Page* p) { |
| 349 | p->MarkEvacuationCandidate(); |
| 350 | evacuation_candidates_.Add(p); |
| 351 | } |
| 352 | |
| 353 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 354 | static void TraceFragmentation(PagedSpace* space) { |
| 355 | int number_of_pages = space->CountTotalPages(); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 356 | intptr_t reserved = (number_of_pages * space->AreaSize()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 357 | intptr_t free = reserved - space->SizeOfObjects(); |
| 358 | PrintF("[%s]: %d pages, %d (%.1f%%) free\n", |
| 359 | AllocationSpaceName(space->identity()), |
| 360 | number_of_pages, |
| 361 | static_cast<int>(free), |
| 362 | static_cast<double>(free) * 100 / reserved); |
| 363 | } |
| 364 | |
| 365 | |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 366 | bool MarkCompactCollector::StartCompaction(CompactionMode mode) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 367 | if (!compacting_) { |
| 368 | ASSERT(evacuation_candidates_.length() == 0); |
| 369 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 370 | #ifdef ENABLE_GDB_JIT_INTERFACE |
| 371 | // If GDBJIT interface is active disable compaction. |
| 372 | if (FLAG_gdbjit) return false; |
| 373 | #endif |
| 374 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 375 | CollectEvacuationCandidates(heap()->old_pointer_space()); |
| 376 | CollectEvacuationCandidates(heap()->old_data_space()); |
| 377 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 378 | if (FLAG_compact_code_space && |
| 379 | (mode == NON_INCREMENTAL_COMPACTION || |
| 380 | FLAG_incremental_code_compaction)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 381 | CollectEvacuationCandidates(heap()->code_space()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 382 | } else if (FLAG_trace_fragmentation) { |
| 383 | TraceFragmentation(heap()->code_space()); |
| 384 | } |
| 385 | |
| 386 | if (FLAG_trace_fragmentation) { |
| 387 | TraceFragmentation(heap()->map_space()); |
| 388 | TraceFragmentation(heap()->cell_space()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 389 | TraceFragmentation(heap()->property_cell_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | heap()->old_pointer_space()->EvictEvacuationCandidatesFromFreeLists(); |
| 393 | heap()->old_data_space()->EvictEvacuationCandidatesFromFreeLists(); |
| 394 | heap()->code_space()->EvictEvacuationCandidatesFromFreeLists(); |
| 395 | |
| 396 | compacting_ = evacuation_candidates_.length() > 0; |
| 397 | } |
| 398 | |
| 399 | return compacting_; |
| 400 | } |
| 401 | |
| 402 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 403 | void MarkCompactCollector::CollectGarbage() { |
| 404 | // Make sure that Prepare() has been called. The individual steps below will |
| 405 | // update the state as they proceed. |
| 406 | ASSERT(state_ == PREPARE_GC); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 407 | ASSERT(encountered_weak_collections_ == Smi::FromInt(0)); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 408 | |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 409 | heap()->allocation_mementos_found_ = 0; |
| 410 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 411 | MarkLiveObjects(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 412 | ASSERT(heap_->incremental_marking()->IsStopped()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 414 | if (FLAG_collect_maps) ClearNonLiveReferences(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 415 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 416 | ClearWeakCollections(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 417 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 418 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 419 | if (FLAG_verify_heap) { |
| 420 | VerifyMarking(heap_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 422 | #endif |
| 423 | |
| 424 | SweepSpaces(); |
| 425 | |
yangguo@chromium.org | 5f0b8ea | 2012-05-16 12:37:04 +0000 | [diff] [blame] | 426 | if (!FLAG_collect_maps) ReattachInitialMaps(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 427 | |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 428 | #ifdef DEBUG |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 429 | if (FLAG_verify_native_context_separation) { |
| 430 | VerifyNativeContextSeparation(heap_); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 431 | } |
| 432 | #endif |
| 433 | |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 434 | #ifdef VERIFY_HEAP |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 435 | if (heap()->weak_embedded_objects_verification_enabled()) { |
| 436 | VerifyWeakEmbeddedObjectsInOptimizedCode(); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 437 | } |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 438 | if (FLAG_collect_maps && FLAG_omit_map_checks_for_leaf_maps) { |
| 439 | VerifyOmittedMapChecks(); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 440 | } |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 441 | #endif |
| 442 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | Finish(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 444 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 445 | if (marking_parity_ == EVEN_MARKING_PARITY) { |
| 446 | marking_parity_ = ODD_MARKING_PARITY; |
| 447 | } else { |
| 448 | ASSERT(marking_parity_ == ODD_MARKING_PARITY); |
| 449 | marking_parity_ = EVEN_MARKING_PARITY; |
| 450 | } |
| 451 | |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 452 | if (FLAG_trace_track_allocation_sites && |
| 453 | heap()->allocation_mementos_found_ > 0) { |
| 454 | PrintF("AllocationMementos found during mark-sweep = %d\n", |
| 455 | heap()->allocation_mementos_found_); |
| 456 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 457 | tracer_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 461 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 462 | void MarkCompactCollector::VerifyMarkbitsAreClean(PagedSpace* space) { |
| 463 | PageIterator it(space); |
| 464 | |
| 465 | while (it.has_next()) { |
| 466 | Page* p = it.next(); |
| 467 | CHECK(p->markbits()->IsClean()); |
| 468 | CHECK_EQ(0, p->LiveBytes()); |
| 469 | } |
| 470 | } |
| 471 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 472 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 473 | void MarkCompactCollector::VerifyMarkbitsAreClean(NewSpace* space) { |
| 474 | NewSpacePageIterator it(space->bottom(), space->top()); |
| 475 | |
| 476 | while (it.has_next()) { |
| 477 | NewSpacePage* p = it.next(); |
| 478 | CHECK(p->markbits()->IsClean()); |
| 479 | CHECK_EQ(0, p->LiveBytes()); |
| 480 | } |
| 481 | } |
| 482 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 483 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 484 | void MarkCompactCollector::VerifyMarkbitsAreClean() { |
| 485 | VerifyMarkbitsAreClean(heap_->old_pointer_space()); |
| 486 | VerifyMarkbitsAreClean(heap_->old_data_space()); |
| 487 | VerifyMarkbitsAreClean(heap_->code_space()); |
| 488 | VerifyMarkbitsAreClean(heap_->cell_space()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 489 | VerifyMarkbitsAreClean(heap_->property_cell_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 490 | VerifyMarkbitsAreClean(heap_->map_space()); |
| 491 | VerifyMarkbitsAreClean(heap_->new_space()); |
| 492 | |
| 493 | LargeObjectIterator it(heap_->lo_space()); |
| 494 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
| 495 | MarkBit mark_bit = Marking::MarkBitFrom(obj); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 496 | CHECK(Marking::IsWhite(mark_bit)); |
| 497 | CHECK_EQ(0, Page::FromAddress(obj->address())->LiveBytes()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 500 | |
| 501 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 502 | void MarkCompactCollector::VerifyWeakEmbeddedObjectsInOptimizedCode() { |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 503 | HeapObjectIterator code_iterator(heap()->code_space()); |
| 504 | for (HeapObject* obj = code_iterator.Next(); |
| 505 | obj != NULL; |
| 506 | obj = code_iterator.Next()) { |
| 507 | Code* code = Code::cast(obj); |
| 508 | if (code->kind() != Code::OPTIMIZED_FUNCTION) continue; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 509 | if (WillBeDeoptimized(code)) continue; |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 510 | code->VerifyEmbeddedObjectsDependency(); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 513 | |
| 514 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 515 | void MarkCompactCollector::VerifyOmittedMapChecks() { |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 516 | HeapObjectIterator iterator(heap()->map_space()); |
| 517 | for (HeapObject* obj = iterator.Next(); |
| 518 | obj != NULL; |
| 519 | obj = iterator.Next()) { |
| 520 | Map* map = Map::cast(obj); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 521 | map->VerifyOmittedMapChecks(); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 524 | #endif // VERIFY_HEAP |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 525 | |
| 526 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 527 | static void ClearMarkbitsInPagedSpace(PagedSpace* space) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 528 | PageIterator it(space); |
| 529 | |
| 530 | while (it.has_next()) { |
| 531 | Bitmap::Clear(it.next()); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 536 | static void ClearMarkbitsInNewSpace(NewSpace* space) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 537 | NewSpacePageIterator it(space->ToSpaceStart(), space->ToSpaceEnd()); |
| 538 | |
| 539 | while (it.has_next()) { |
| 540 | Bitmap::Clear(it.next()); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 545 | void MarkCompactCollector::ClearMarkbits() { |
| 546 | ClearMarkbitsInPagedSpace(heap_->code_space()); |
| 547 | ClearMarkbitsInPagedSpace(heap_->map_space()); |
| 548 | ClearMarkbitsInPagedSpace(heap_->old_pointer_space()); |
| 549 | ClearMarkbitsInPagedSpace(heap_->old_data_space()); |
| 550 | ClearMarkbitsInPagedSpace(heap_->cell_space()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 551 | ClearMarkbitsInPagedSpace(heap_->property_cell_space()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 552 | ClearMarkbitsInNewSpace(heap_->new_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 553 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 554 | LargeObjectIterator it(heap_->lo_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 555 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
| 556 | MarkBit mark_bit = Marking::MarkBitFrom(obj); |
| 557 | mark_bit.Clear(); |
| 558 | mark_bit.Next().Clear(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 559 | Page::FromAddress(obj->address())->ResetProgressBar(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 560 | Page::FromAddress(obj->address())->ResetLiveBytes(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 565 | void MarkCompactCollector::StartSweeperThreads() { |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 566 | sweeping_pending_ = true; |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 567 | for (int i = 0; i < FLAG_sweeper_threads; i++) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 568 | isolate()->sweeper_threads()[i]->StartSweeping(); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | |
| 573 | void MarkCompactCollector::WaitUntilSweepingCompleted() { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 574 | ASSERT(sweeping_pending_ == true); |
| 575 | for (int i = 0; i < FLAG_sweeper_threads; i++) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 576 | isolate()->sweeper_threads()[i]->WaitForSweeperThread(); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 577 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 578 | sweeping_pending_ = false; |
| 579 | StealMemoryFromSweeperThreads(heap()->paged_space(OLD_DATA_SPACE)); |
| 580 | StealMemoryFromSweeperThreads(heap()->paged_space(OLD_POINTER_SPACE)); |
| 581 | heap()->paged_space(OLD_DATA_SPACE)->ResetUnsweptFreeBytes(); |
| 582 | heap()->paged_space(OLD_POINTER_SPACE)->ResetUnsweptFreeBytes(); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | |
| 586 | intptr_t MarkCompactCollector:: |
| 587 | StealMemoryFromSweeperThreads(PagedSpace* space) { |
| 588 | intptr_t freed_bytes = 0; |
| 589 | for (int i = 0; i < FLAG_sweeper_threads; i++) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 590 | freed_bytes += isolate()->sweeper_threads()[i]->StealMemory(space); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 591 | } |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 592 | space->AddToAccountingStats(freed_bytes); |
| 593 | space->DecrementUnsweptFreeBytes(freed_bytes); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 594 | return freed_bytes; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | bool MarkCompactCollector::AreSweeperThreadsActivated() { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 599 | return isolate()->sweeper_threads() != NULL; |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 603 | bool MarkCompactCollector::IsConcurrentSweepingInProgress() { |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 604 | return sweeping_pending_; |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 608 | bool Marking::TransferMark(Address old_start, Address new_start) { |
| 609 | // This is only used when resizing an object. |
| 610 | ASSERT(MemoryChunk::FromAddress(old_start) == |
| 611 | MemoryChunk::FromAddress(new_start)); |
| 612 | |
| 613 | // If the mark doesn't move, we don't check the color of the object. |
| 614 | // It doesn't matter whether the object is black, since it hasn't changed |
| 615 | // size, so the adjustment to the live data count will be zero anyway. |
| 616 | if (old_start == new_start) return false; |
| 617 | |
| 618 | MarkBit new_mark_bit = MarkBitFrom(new_start); |
| 619 | MarkBit old_mark_bit = MarkBitFrom(old_start); |
| 620 | |
| 621 | #ifdef DEBUG |
| 622 | ObjectColor old_color = Color(old_mark_bit); |
| 623 | #endif |
| 624 | |
| 625 | if (Marking::IsBlack(old_mark_bit)) { |
| 626 | old_mark_bit.Clear(); |
| 627 | ASSERT(IsWhite(old_mark_bit)); |
| 628 | Marking::MarkBlack(new_mark_bit); |
| 629 | return true; |
| 630 | } else if (Marking::IsGrey(old_mark_bit)) { |
| 631 | ASSERT(heap_->incremental_marking()->IsMarking()); |
| 632 | old_mark_bit.Clear(); |
| 633 | old_mark_bit.Next().Clear(); |
| 634 | ASSERT(IsWhite(old_mark_bit)); |
| 635 | heap_->incremental_marking()->WhiteToGreyAndPush( |
| 636 | HeapObject::FromAddress(new_start), new_mark_bit); |
| 637 | heap_->incremental_marking()->RestartIfNotMarking(); |
| 638 | } |
| 639 | |
| 640 | #ifdef DEBUG |
| 641 | ObjectColor new_color = Color(new_mark_bit); |
| 642 | ASSERT(new_color == old_color); |
| 643 | #endif |
| 644 | |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | const char* AllocationSpaceName(AllocationSpace space) { |
| 650 | switch (space) { |
| 651 | case NEW_SPACE: return "NEW_SPACE"; |
| 652 | case OLD_POINTER_SPACE: return "OLD_POINTER_SPACE"; |
| 653 | case OLD_DATA_SPACE: return "OLD_DATA_SPACE"; |
| 654 | case CODE_SPACE: return "CODE_SPACE"; |
| 655 | case MAP_SPACE: return "MAP_SPACE"; |
| 656 | case CELL_SPACE: return "CELL_SPACE"; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 657 | case PROPERTY_CELL_SPACE: |
| 658 | return "PROPERTY_CELL_SPACE"; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 659 | case LO_SPACE: return "LO_SPACE"; |
| 660 | default: |
| 661 | UNREACHABLE(); |
| 662 | } |
| 663 | |
| 664 | return NULL; |
| 665 | } |
| 666 | |
| 667 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 668 | // Returns zero for pages that have so little fragmentation that it is not |
| 669 | // worth defragmenting them. Otherwise a positive integer that gives an |
| 670 | // estimate of fragmentation on an arbitrary scale. |
| 671 | static int FreeListFragmentation(PagedSpace* space, Page* p) { |
| 672 | // If page was not swept then there are no free list items on it. |
| 673 | if (!p->WasSwept()) { |
| 674 | if (FLAG_trace_fragmentation) { |
| 675 | PrintF("%p [%s]: %d bytes live (unswept)\n", |
| 676 | reinterpret_cast<void*>(p), |
| 677 | AllocationSpaceName(space->identity()), |
| 678 | p->LiveBytes()); |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 683 | PagedSpace::SizeStats sizes; |
| 684 | space->ObtainFreeListStatistics(p, &sizes); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 685 | |
| 686 | intptr_t ratio; |
| 687 | intptr_t ratio_threshold; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 688 | intptr_t area_size = space->AreaSize(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 689 | if (space->identity() == CODE_SPACE) { |
| 690 | ratio = (sizes.medium_size_ * 10 + sizes.large_size_ * 2) * 100 / |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 691 | area_size; |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 692 | ratio_threshold = 10; |
| 693 | } else { |
| 694 | ratio = (sizes.small_size_ * 5 + sizes.medium_size_) * 100 / |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 695 | area_size; |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 696 | ratio_threshold = 15; |
| 697 | } |
| 698 | |
| 699 | if (FLAG_trace_fragmentation) { |
| 700 | PrintF("%p [%s]: %d (%.2f%%) %d (%.2f%%) %d (%.2f%%) %d (%.2f%%) %s\n", |
| 701 | reinterpret_cast<void*>(p), |
| 702 | AllocationSpaceName(space->identity()), |
| 703 | static_cast<int>(sizes.small_size_), |
| 704 | static_cast<double>(sizes.small_size_ * 100) / |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 705 | area_size, |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 706 | static_cast<int>(sizes.medium_size_), |
| 707 | static_cast<double>(sizes.medium_size_ * 100) / |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 708 | area_size, |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 709 | static_cast<int>(sizes.large_size_), |
| 710 | static_cast<double>(sizes.large_size_ * 100) / |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 711 | area_size, |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 712 | static_cast<int>(sizes.huge_size_), |
| 713 | static_cast<double>(sizes.huge_size_ * 100) / |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 714 | area_size, |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 715 | (ratio > ratio_threshold) ? "[fragmented]" : ""); |
| 716 | } |
| 717 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 718 | if (FLAG_always_compact && sizes.Total() != area_size) { |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | if (ratio <= ratio_threshold) return 0; // Not fragmented. |
| 723 | |
| 724 | return static_cast<int>(ratio - ratio_threshold); |
| 725 | } |
| 726 | |
| 727 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 728 | void MarkCompactCollector::CollectEvacuationCandidates(PagedSpace* space) { |
| 729 | ASSERT(space->identity() == OLD_POINTER_SPACE || |
| 730 | space->identity() == OLD_DATA_SPACE || |
| 731 | space->identity() == CODE_SPACE); |
| 732 | |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 733 | static const int kMaxMaxEvacuationCandidates = 1000; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 734 | int number_of_pages = space->CountTotalPages(); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 735 | int max_evacuation_candidates = |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 736 | static_cast<int>(sqrt(number_of_pages / 2.0) + 1); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 737 | |
| 738 | if (FLAG_stress_compaction || FLAG_always_compact) { |
| 739 | max_evacuation_candidates = kMaxMaxEvacuationCandidates; |
| 740 | } |
| 741 | |
| 742 | class Candidate { |
| 743 | public: |
| 744 | Candidate() : fragmentation_(0), page_(NULL) { } |
| 745 | Candidate(int f, Page* p) : fragmentation_(f), page_(p) { } |
| 746 | |
| 747 | int fragmentation() { return fragmentation_; } |
| 748 | Page* page() { return page_; } |
| 749 | |
| 750 | private: |
| 751 | int fragmentation_; |
| 752 | Page* page_; |
| 753 | }; |
| 754 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 755 | enum CompactionMode { |
| 756 | COMPACT_FREE_LISTS, |
| 757 | REDUCE_MEMORY_FOOTPRINT |
| 758 | }; |
| 759 | |
| 760 | CompactionMode mode = COMPACT_FREE_LISTS; |
| 761 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 762 | intptr_t reserved = number_of_pages * space->AreaSize(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 763 | intptr_t over_reserved = reserved - space->SizeOfObjects(); |
| 764 | static const intptr_t kFreenessThreshold = 50; |
| 765 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 766 | if (reduce_memory_footprint_ && over_reserved >= space->AreaSize()) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 767 | // If reduction of memory footprint was requested, we are aggressive |
| 768 | // about choosing pages to free. We expect that half-empty pages |
| 769 | // are easier to compact so slightly bump the limit. |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 770 | mode = REDUCE_MEMORY_FOOTPRINT; |
| 771 | max_evacuation_candidates += 2; |
| 772 | } |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 773 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 774 | |
| 775 | if (over_reserved > reserved / 3 && over_reserved >= 2 * space->AreaSize()) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 776 | // If over-usage is very high (more than a third of the space), we |
| 777 | // try to free all mostly empty pages. We expect that almost empty |
| 778 | // pages are even easier to compact so bump the limit even more. |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 779 | mode = REDUCE_MEMORY_FOOTPRINT; |
| 780 | max_evacuation_candidates *= 2; |
| 781 | } |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 782 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 783 | if (FLAG_trace_fragmentation && mode == REDUCE_MEMORY_FOOTPRINT) { |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 784 | PrintF("Estimated over reserved memory: %.1f / %.1f MB (threshold %d), " |
| 785 | "evacuation candidate limit: %d\n", |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 786 | static_cast<double>(over_reserved) / MB, |
| 787 | static_cast<double>(reserved) / MB, |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 788 | static_cast<int>(kFreenessThreshold), |
| 789 | max_evacuation_candidates); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | intptr_t estimated_release = 0; |
| 793 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 794 | Candidate candidates[kMaxMaxEvacuationCandidates]; |
| 795 | |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 796 | max_evacuation_candidates = |
| 797 | Min(kMaxMaxEvacuationCandidates, max_evacuation_candidates); |
| 798 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 799 | int count = 0; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 800 | int fragmentation = 0; |
| 801 | Candidate* least = NULL; |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 802 | |
| 803 | PageIterator it(space); |
| 804 | if (it.has_next()) it.next(); // Never compact the first page. |
| 805 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 806 | while (it.has_next()) { |
| 807 | Page* p = it.next(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 808 | p->ClearEvacuationCandidate(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 809 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 810 | if (FLAG_stress_compaction) { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 811 | unsigned int counter = space->heap()->ms_count(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 812 | uintptr_t page_number = reinterpret_cast<uintptr_t>(p) >> kPageSizeBits; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 813 | if ((counter & 1) == (page_number & 1)) fragmentation = 1; |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 814 | } else if (mode == REDUCE_MEMORY_FOOTPRINT) { |
| 815 | // Don't try to release too many pages. |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 816 | if (estimated_release >= over_reserved) { |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 817 | continue; |
| 818 | } |
| 819 | |
| 820 | intptr_t free_bytes = 0; |
| 821 | |
| 822 | if (!p->WasSwept()) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 823 | free_bytes = (p->area_size() - p->LiveBytes()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 824 | } else { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 825 | PagedSpace::SizeStats sizes; |
| 826 | space->ObtainFreeListStatistics(p, &sizes); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 827 | free_bytes = sizes.Total(); |
| 828 | } |
| 829 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 830 | int free_pct = static_cast<int>(free_bytes * 100) / p->area_size(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 831 | |
| 832 | if (free_pct >= kFreenessThreshold) { |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 833 | estimated_release += free_bytes; |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 834 | fragmentation = free_pct; |
| 835 | } else { |
| 836 | fragmentation = 0; |
| 837 | } |
| 838 | |
| 839 | if (FLAG_trace_fragmentation) { |
| 840 | PrintF("%p [%s]: %d (%.2f%%) free %s\n", |
| 841 | reinterpret_cast<void*>(p), |
| 842 | AllocationSpaceName(space->identity()), |
| 843 | static_cast<int>(free_bytes), |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 844 | static_cast<double>(free_bytes * 100) / p->area_size(), |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 845 | (fragmentation > 0) ? "[fragmented]" : ""); |
| 846 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 847 | } else { |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 848 | fragmentation = FreeListFragmentation(space, p); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 849 | } |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 850 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 851 | if (fragmentation != 0) { |
| 852 | if (count < max_evacuation_candidates) { |
| 853 | candidates[count++] = Candidate(fragmentation, p); |
| 854 | } else { |
| 855 | if (least == NULL) { |
| 856 | for (int i = 0; i < max_evacuation_candidates; i++) { |
| 857 | if (least == NULL || |
| 858 | candidates[i].fragmentation() < least->fragmentation()) { |
| 859 | least = candidates + i; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | if (least->fragmentation() < fragmentation) { |
| 864 | *least = Candidate(fragmentation, p); |
| 865 | least = NULL; |
| 866 | } |
| 867 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 870 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 871 | for (int i = 0; i < count; i++) { |
| 872 | AddEvacuationCandidate(candidates[i].page()); |
| 873 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 874 | |
| 875 | if (count > 0 && FLAG_trace_fragmentation) { |
| 876 | PrintF("Collected %d evacuation candidates for space %s\n", |
| 877 | count, |
| 878 | AllocationSpaceName(space->identity())); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | |
| 883 | void MarkCompactCollector::AbortCompaction() { |
| 884 | if (compacting_) { |
| 885 | int npages = evacuation_candidates_.length(); |
| 886 | for (int i = 0; i < npages; i++) { |
| 887 | Page* p = evacuation_candidates_[i]; |
| 888 | slots_buffer_allocator_.DeallocateChain(p->slots_buffer_address()); |
| 889 | p->ClearEvacuationCandidate(); |
| 890 | p->ClearFlag(MemoryChunk::RESCAN_ON_EVACUATION); |
| 891 | } |
| 892 | compacting_ = false; |
| 893 | evacuation_candidates_.Rewind(0); |
| 894 | invalidated_code_.Rewind(0); |
| 895 | } |
| 896 | ASSERT_EQ(0, evacuation_candidates_.length()); |
| 897 | } |
| 898 | |
| 899 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 900 | void MarkCompactCollector::Prepare(GCTracer* tracer) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 901 | was_marked_incrementally_ = heap()->incremental_marking()->IsMarking(); |
| 902 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 903 | // Rather than passing the tracer around we stash it in a static member |
| 904 | // variable. |
| 905 | tracer_ = tracer; |
| 906 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 907 | #ifdef DEBUG |
| 908 | ASSERT(state_ == IDLE); |
| 909 | state_ = PREPARE_GC; |
| 910 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 911 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 912 | ASSERT(!FLAG_never_compact || !FLAG_always_compact); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 913 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 914 | if (IsConcurrentSweepingInProgress()) { |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 915 | // Instead of waiting we could also abort the sweeper threads here. |
| 916 | WaitUntilSweepingCompleted(); |
| 917 | } |
| 918 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 919 | // Clear marking bits if incremental marking is aborted. |
| 920 | if (was_marked_incrementally_ && abort_incremental_marking_) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 921 | heap()->incremental_marking()->Abort(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 922 | ClearMarkbits(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 923 | AbortCompaction(); |
| 924 | was_marked_incrementally_ = false; |
| 925 | } |
| 926 | |
| 927 | // Don't start compaction if we are in the middle of incremental |
| 928 | // marking cycle. We did not collect any slots. |
| 929 | if (!FLAG_never_compact && !was_marked_incrementally_) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 930 | StartCompaction(NON_INCREMENTAL_COMPACTION); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 931 | } |
| 932 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 933 | PagedSpaces spaces(heap()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 934 | for (PagedSpace* space = spaces.next(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 935 | space != NULL; |
| 936 | space = spaces.next()) { |
| 937 | space->PrepareForMarkCompact(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 938 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 939 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 940 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 941 | if (!was_marked_incrementally_ && FLAG_verify_heap) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 942 | VerifyMarkbitsAreClean(); |
| 943 | } |
| 944 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 945 | } |
| 946 | |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 947 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 948 | void MarkCompactCollector::Finish() { |
| 949 | #ifdef DEBUG |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 950 | ASSERT(state_ == SWEEP_SPACES || state_ == RELOCATE_OBJECTS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 951 | state_ = IDLE; |
| 952 | #endif |
| 953 | // The stub cache is not traversed during GC; clear the cache to |
| 954 | // force lazy re-initialization of it. This must be done after the |
| 955 | // GC, because it relies on the new address of certain old space |
| 956 | // objects (empty string, illegal builtin). |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 957 | isolate()->stub_cache()->Clear(); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 958 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 959 | if (have_code_to_deoptimize_) { |
| 960 | // Some code objects were marked for deoptimization during the GC. |
| 961 | Deoptimizer::DeoptimizeMarkedCode(isolate()); |
| 962 | have_code_to_deoptimize_ = false; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 963 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 967 | // ------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 968 | // Phase 1: tracing and marking live objects. |
| 969 | // before: all objects are in normal state. |
| 970 | // after: a live object's map pointer is marked as '00'. |
| 971 | |
| 972 | // Marking all live objects in the heap as part of mark-sweep or mark-compact |
| 973 | // collection. Before marking, all objects are in their normal state. After |
| 974 | // marking, live objects' map pointers are marked indicating that the object |
| 975 | // has been found reachable. |
| 976 | // |
| 977 | // The marking algorithm is a (mostly) depth-first (because of possible stack |
| 978 | // overflow) traversal of the graph of objects reachable from the roots. It |
| 979 | // uses an explicit stack of pointers rather than recursion. The young |
| 980 | // generation's inactive ('from') space is used as a marking stack. The |
| 981 | // objects in the marking stack are the ones that have been reached and marked |
| 982 | // but their children have not yet been visited. |
| 983 | // |
| 984 | // The marking stack can overflow during traversal. In that case, we set an |
| 985 | // overflow flag. When the overflow flag is set, we continue marking objects |
| 986 | // reachable from the objects on the marking stack, but no longer push them on |
| 987 | // the marking stack. Instead, we mark them as both marked and overflowed. |
| 988 | // When the stack is in the overflowed state, objects marked as overflowed |
| 989 | // have been reached and marked but their children have not been visited yet. |
| 990 | // After emptying the marking stack, we clear the overflow flag and traverse |
| 991 | // the heap looking for objects marked as overflowed, push them on the stack, |
| 992 | // and continue with marking. This process repeats until all reachable |
| 993 | // objects have been marked. |
| 994 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 995 | void CodeFlusher::ProcessJSFunctionCandidates() { |
| 996 | Code* lazy_compile = isolate_->builtins()->builtin(Builtins::kLazyCompile); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 997 | Object* undefined = isolate_->heap()->undefined_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 998 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 999 | JSFunction* candidate = jsfunction_candidates_head_; |
| 1000 | JSFunction* next_candidate; |
| 1001 | while (candidate != NULL) { |
| 1002 | next_candidate = GetNextCandidate(candidate); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1003 | ClearNextCandidate(candidate, undefined); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1004 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1005 | SharedFunctionInfo* shared = candidate->shared(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1006 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1007 | Code* code = shared->code(); |
| 1008 | MarkBit code_mark = Marking::MarkBitFrom(code); |
| 1009 | if (!code_mark.Get()) { |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1010 | if (FLAG_trace_code_flushing && shared->is_compiled()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1011 | PrintF("[code-flushing clears: "); |
| 1012 | shared->ShortPrint(); |
| 1013 | PrintF(" - age: %d]\n", code->GetAge()); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1014 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1015 | shared->set_code(lazy_compile); |
| 1016 | candidate->set_code(lazy_compile); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 1017 | } else { |
| 1018 | candidate->set_code(code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1021 | // We are in the middle of a GC cycle so the write barrier in the code |
| 1022 | // setter did not record the slot update and we have to do that manually. |
| 1023 | Address slot = candidate->address() + JSFunction::kCodeEntryOffset; |
| 1024 | Code* target = Code::cast(Code::GetObjectFromEntryAddress(slot)); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1025 | isolate_->heap()->mark_compact_collector()-> |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1026 | RecordCodeEntrySlot(slot, target); |
| 1027 | |
| 1028 | Object** shared_code_slot = |
| 1029 | HeapObject::RawField(shared, SharedFunctionInfo::kCodeOffset); |
| 1030 | isolate_->heap()->mark_compact_collector()-> |
| 1031 | RecordSlot(shared_code_slot, shared_code_slot, *shared_code_slot); |
| 1032 | |
| 1033 | candidate = next_candidate; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1036 | jsfunction_candidates_head_ = NULL; |
| 1037 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1038 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1039 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1040 | void CodeFlusher::ProcessSharedFunctionInfoCandidates() { |
| 1041 | Code* lazy_compile = isolate_->builtins()->builtin(Builtins::kLazyCompile); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1042 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1043 | SharedFunctionInfo* candidate = shared_function_info_candidates_head_; |
| 1044 | SharedFunctionInfo* next_candidate; |
| 1045 | while (candidate != NULL) { |
| 1046 | next_candidate = GetNextCandidate(candidate); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1047 | ClearNextCandidate(candidate); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1048 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1049 | Code* code = candidate->code(); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1050 | MarkBit code_mark = Marking::MarkBitFrom(code); |
| 1051 | if (!code_mark.Get()) { |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1052 | if (FLAG_trace_code_flushing && candidate->is_compiled()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1053 | PrintF("[code-flushing clears: "); |
| 1054 | candidate->ShortPrint(); |
| 1055 | PrintF(" - age: %d]\n", code->GetAge()); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1056 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1057 | candidate->set_code(lazy_compile); |
| 1058 | } |
| 1059 | |
| 1060 | Object** code_slot = |
| 1061 | HeapObject::RawField(candidate, SharedFunctionInfo::kCodeOffset); |
| 1062 | isolate_->heap()->mark_compact_collector()-> |
| 1063 | RecordSlot(code_slot, code_slot, *code_slot); |
| 1064 | |
| 1065 | candidate = next_candidate; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1068 | shared_function_info_candidates_head_ = NULL; |
| 1069 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1070 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1071 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 1072 | void CodeFlusher::ProcessOptimizedCodeMaps() { |
| 1073 | static const int kEntriesStart = SharedFunctionInfo::kEntriesStart; |
| 1074 | static const int kEntryLength = SharedFunctionInfo::kEntryLength; |
| 1075 | static const int kContextOffset = 0; |
| 1076 | static const int kCodeOffset = 1; |
| 1077 | static const int kLiteralsOffset = 2; |
| 1078 | STATIC_ASSERT(kEntryLength == 3); |
| 1079 | |
| 1080 | SharedFunctionInfo* holder = optimized_code_map_holder_head_; |
| 1081 | SharedFunctionInfo* next_holder; |
| 1082 | while (holder != NULL) { |
| 1083 | next_holder = GetNextCodeMap(holder); |
| 1084 | ClearNextCodeMap(holder); |
| 1085 | |
| 1086 | FixedArray* code_map = FixedArray::cast(holder->optimized_code_map()); |
| 1087 | int new_length = kEntriesStart; |
| 1088 | int old_length = code_map->length(); |
| 1089 | for (int i = kEntriesStart; i < old_length; i += kEntryLength) { |
| 1090 | Code* code = Code::cast(code_map->get(i + kCodeOffset)); |
| 1091 | MarkBit code_mark = Marking::MarkBitFrom(code); |
| 1092 | if (!code_mark.Get()) { |
| 1093 | continue; |
| 1094 | } |
| 1095 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1096 | // Update and record the context slot in the optimized code map. |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 1097 | Object** context_slot = HeapObject::RawField(code_map, |
| 1098 | FixedArray::OffsetOfElementAt(new_length)); |
| 1099 | code_map->set(new_length++, code_map->get(i + kContextOffset)); |
| 1100 | ASSERT(Marking::IsBlack( |
| 1101 | Marking::MarkBitFrom(HeapObject::cast(*context_slot)))); |
| 1102 | isolate_->heap()->mark_compact_collector()-> |
| 1103 | RecordSlot(context_slot, context_slot, *context_slot); |
| 1104 | |
| 1105 | // Update and record the code slot in the optimized code map. |
| 1106 | Object** code_slot = HeapObject::RawField(code_map, |
| 1107 | FixedArray::OffsetOfElementAt(new_length)); |
| 1108 | code_map->set(new_length++, code_map->get(i + kCodeOffset)); |
| 1109 | ASSERT(Marking::IsBlack( |
| 1110 | Marking::MarkBitFrom(HeapObject::cast(*code_slot)))); |
| 1111 | isolate_->heap()->mark_compact_collector()-> |
| 1112 | RecordSlot(code_slot, code_slot, *code_slot); |
| 1113 | |
| 1114 | // Update and record the literals slot in the optimized code map. |
| 1115 | Object** literals_slot = HeapObject::RawField(code_map, |
| 1116 | FixedArray::OffsetOfElementAt(new_length)); |
| 1117 | code_map->set(new_length++, code_map->get(i + kLiteralsOffset)); |
| 1118 | ASSERT(Marking::IsBlack( |
| 1119 | Marking::MarkBitFrom(HeapObject::cast(*literals_slot)))); |
| 1120 | isolate_->heap()->mark_compact_collector()-> |
| 1121 | RecordSlot(literals_slot, literals_slot, *literals_slot); |
| 1122 | } |
| 1123 | |
| 1124 | // Trim the optimized code map if entries have been removed. |
| 1125 | if (new_length < old_length) { |
| 1126 | holder->TrimOptimizedCodeMap(old_length - new_length); |
| 1127 | } |
| 1128 | |
| 1129 | holder = next_holder; |
| 1130 | } |
| 1131 | |
| 1132 | optimized_code_map_holder_head_ = NULL; |
| 1133 | } |
| 1134 | |
| 1135 | |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 1136 | void CodeFlusher::EvictCandidate(SharedFunctionInfo* shared_info) { |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 1137 | // Make sure previous flushing decisions are revisited. |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 1138 | isolate_->heap()->incremental_marking()->RecordWrites(shared_info); |
| 1139 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1140 | if (FLAG_trace_code_flushing) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1141 | PrintF("[code-flushing abandons function-info: "); |
| 1142 | shared_info->ShortPrint(); |
| 1143 | PrintF("]\n"); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 1146 | SharedFunctionInfo* candidate = shared_function_info_candidates_head_; |
| 1147 | SharedFunctionInfo* next_candidate; |
| 1148 | if (candidate == shared_info) { |
| 1149 | next_candidate = GetNextCandidate(shared_info); |
| 1150 | shared_function_info_candidates_head_ = next_candidate; |
| 1151 | ClearNextCandidate(shared_info); |
| 1152 | } else { |
| 1153 | while (candidate != NULL) { |
| 1154 | next_candidate = GetNextCandidate(candidate); |
| 1155 | |
| 1156 | if (next_candidate == shared_info) { |
| 1157 | next_candidate = GetNextCandidate(shared_info); |
| 1158 | SetNextCandidate(candidate, next_candidate); |
| 1159 | ClearNextCandidate(shared_info); |
| 1160 | break; |
| 1161 | } |
| 1162 | |
| 1163 | candidate = next_candidate; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1169 | void CodeFlusher::EvictCandidate(JSFunction* function) { |
| 1170 | ASSERT(!function->next_function_link()->IsUndefined()); |
| 1171 | Object* undefined = isolate_->heap()->undefined_value(); |
| 1172 | |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 1173 | // Make sure previous flushing decisions are revisited. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 1174 | isolate_->heap()->incremental_marking()->RecordWrites(function); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 1175 | isolate_->heap()->incremental_marking()->RecordWrites(function->shared()); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 1176 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1177 | if (FLAG_trace_code_flushing) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1178 | PrintF("[code-flushing abandons closure: "); |
| 1179 | function->shared()->ShortPrint(); |
| 1180 | PrintF("]\n"); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1183 | JSFunction* candidate = jsfunction_candidates_head_; |
| 1184 | JSFunction* next_candidate; |
| 1185 | if (candidate == function) { |
| 1186 | next_candidate = GetNextCandidate(function); |
| 1187 | jsfunction_candidates_head_ = next_candidate; |
| 1188 | ClearNextCandidate(function, undefined); |
| 1189 | } else { |
| 1190 | while (candidate != NULL) { |
| 1191 | next_candidate = GetNextCandidate(candidate); |
| 1192 | |
| 1193 | if (next_candidate == function) { |
| 1194 | next_candidate = GetNextCandidate(function); |
| 1195 | SetNextCandidate(candidate, next_candidate); |
| 1196 | ClearNextCandidate(function, undefined); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 1197 | break; |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | candidate = next_candidate; |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 1206 | void CodeFlusher::EvictOptimizedCodeMap(SharedFunctionInfo* code_map_holder) { |
| 1207 | ASSERT(!FixedArray::cast(code_map_holder->optimized_code_map())-> |
| 1208 | get(SharedFunctionInfo::kNextMapIndex)->IsUndefined()); |
| 1209 | |
| 1210 | // Make sure previous flushing decisions are revisited. |
| 1211 | isolate_->heap()->incremental_marking()->RecordWrites(code_map_holder); |
| 1212 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1213 | if (FLAG_trace_code_flushing) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1214 | PrintF("[code-flushing abandons code-map: "); |
| 1215 | code_map_holder->ShortPrint(); |
| 1216 | PrintF("]\n"); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 1219 | SharedFunctionInfo* holder = optimized_code_map_holder_head_; |
| 1220 | SharedFunctionInfo* next_holder; |
| 1221 | if (holder == code_map_holder) { |
| 1222 | next_holder = GetNextCodeMap(code_map_holder); |
| 1223 | optimized_code_map_holder_head_ = next_holder; |
| 1224 | ClearNextCodeMap(code_map_holder); |
| 1225 | } else { |
| 1226 | while (holder != NULL) { |
| 1227 | next_holder = GetNextCodeMap(holder); |
| 1228 | |
| 1229 | if (next_holder == code_map_holder) { |
| 1230 | next_holder = GetNextCodeMap(code_map_holder); |
| 1231 | SetNextCodeMap(holder, next_holder); |
| 1232 | ClearNextCodeMap(code_map_holder); |
| 1233 | break; |
| 1234 | } |
| 1235 | |
| 1236 | holder = next_holder; |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1242 | void CodeFlusher::EvictJSFunctionCandidates() { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1243 | JSFunction* candidate = jsfunction_candidates_head_; |
| 1244 | JSFunction* next_candidate; |
| 1245 | while (candidate != NULL) { |
| 1246 | next_candidate = GetNextCandidate(candidate); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 1247 | EvictCandidate(candidate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1248 | candidate = next_candidate; |
| 1249 | } |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 1250 | ASSERT(jsfunction_candidates_head_ == NULL); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | void CodeFlusher::EvictSharedFunctionInfoCandidates() { |
| 1255 | SharedFunctionInfo* candidate = shared_function_info_candidates_head_; |
| 1256 | SharedFunctionInfo* next_candidate; |
| 1257 | while (candidate != NULL) { |
| 1258 | next_candidate = GetNextCandidate(candidate); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 1259 | EvictCandidate(candidate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1260 | candidate = next_candidate; |
| 1261 | } |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 1262 | ASSERT(shared_function_info_candidates_head_ == NULL); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 1266 | void CodeFlusher::EvictOptimizedCodeMaps() { |
| 1267 | SharedFunctionInfo* holder = optimized_code_map_holder_head_; |
| 1268 | SharedFunctionInfo* next_holder; |
| 1269 | while (holder != NULL) { |
| 1270 | next_holder = GetNextCodeMap(holder); |
| 1271 | EvictOptimizedCodeMap(holder); |
| 1272 | holder = next_holder; |
| 1273 | } |
| 1274 | ASSERT(optimized_code_map_holder_head_ == NULL); |
| 1275 | } |
| 1276 | |
| 1277 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1278 | void CodeFlusher::IteratePointersToFromSpace(ObjectVisitor* v) { |
| 1279 | Heap* heap = isolate_->heap(); |
| 1280 | |
| 1281 | JSFunction** slot = &jsfunction_candidates_head_; |
| 1282 | JSFunction* candidate = jsfunction_candidates_head_; |
| 1283 | while (candidate != NULL) { |
| 1284 | if (heap->InFromSpace(candidate)) { |
| 1285 | v->VisitPointer(reinterpret_cast<Object**>(slot)); |
| 1286 | } |
| 1287 | candidate = GetNextCandidate(*slot); |
| 1288 | slot = GetNextCandidateSlot(*slot); |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1293 | MarkCompactCollector::~MarkCompactCollector() { |
| 1294 | if (code_flusher_ != NULL) { |
| 1295 | delete code_flusher_; |
| 1296 | code_flusher_ = NULL; |
| 1297 | } |
| 1298 | } |
| 1299 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1300 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1301 | static inline HeapObject* ShortCircuitConsString(Object** p) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1302 | // Optimization: If the heap object pointed to by p is a non-internalized |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1303 | // cons string whose right substring is HEAP->empty_string, update |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1304 | // it in place to its left substring. Return the updated value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1305 | // |
| 1306 | // Here we assume that if we change *p, we replace it with a heap object |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1307 | // (i.e., the left substring of a cons string is always a heap object). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1308 | // |
| 1309 | // The check performed is: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1310 | // object->IsConsString() && !object->IsInternalizedString() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1311 | // (ConsString::cast(object)->second() == HEAP->empty_string()) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1312 | // except the maps for the object and its possible substrings might be |
| 1313 | // marked. |
| 1314 | HeapObject* object = HeapObject::cast(*p); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1315 | if (!FLAG_clever_optimizations) return object; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1316 | Map* map = object->map(); |
| 1317 | InstanceType type = map->instance_type(); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1318 | if ((type & kShortcutTypeMask) != kShortcutTypeTag) return object; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1319 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1320 | Object* second = reinterpret_cast<ConsString*>(object)->second(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1321 | Heap* heap = map->GetHeap(); |
| 1322 | if (second != heap->empty_string()) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1323 | return object; |
| 1324 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1325 | |
| 1326 | // Since we don't have the object's start, it is impossible to update the |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1327 | // page dirty marks. Therefore, we only replace the string with its left |
| 1328 | // substring when page dirty marks do not change. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1329 | Object* first = reinterpret_cast<ConsString*>(object)->first(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1330 | if (!heap->InNewSpace(object) && heap->InNewSpace(first)) return object; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1331 | |
| 1332 | *p = first; |
| 1333 | return HeapObject::cast(first); |
| 1334 | } |
| 1335 | |
| 1336 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1337 | class MarkCompactMarkingVisitor |
| 1338 | : public StaticMarkingVisitor<MarkCompactMarkingVisitor> { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1339 | public: |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1340 | static void ObjectStatsVisitBase(StaticVisitorBase::VisitorId id, |
| 1341 | Map* map, HeapObject* obj); |
| 1342 | |
| 1343 | static void ObjectStatsCountFixedArray( |
| 1344 | FixedArrayBase* fixed_array, |
| 1345 | FixedArraySubInstanceType fast_type, |
| 1346 | FixedArraySubInstanceType dictionary_type); |
| 1347 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1348 | template<MarkCompactMarkingVisitor::VisitorId id> |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1349 | class ObjectStatsTracker { |
| 1350 | public: |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1351 | static inline void Visit(Map* map, HeapObject* obj); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1352 | }; |
| 1353 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1354 | static void Initialize(); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1355 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1356 | INLINE(static void VisitPointer(Heap* heap, Object** p)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1357 | MarkObjectByPointer(heap->mark_compact_collector(), p, p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1360 | INLINE(static void VisitPointers(Heap* heap, Object** start, Object** end)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1361 | // Mark all objects pointed to in [start, end). |
| 1362 | const int kMinRangeForMarkingRecursion = 64; |
| 1363 | if (end - start >= kMinRangeForMarkingRecursion) { |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1364 | if (VisitUnmarkedObjects(heap, start, end)) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1365 | // We are close to a stack overflow, so just mark the objects. |
| 1366 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1367 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
| 1368 | for (Object** p = start; p < end; p++) { |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1369 | MarkObjectByPointer(collector, start, p); |
jkummerow@chromium.org | 7a96c2a | 2012-10-01 16:24:39 +0000 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 1373 | // Marks the object black and pushes it on the marking stack. |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1374 | INLINE(static void MarkObject(Heap* heap, HeapObject* object)) { |
| 1375 | MarkBit mark = Marking::MarkBitFrom(object); |
| 1376 | heap->mark_compact_collector()->MarkObject(object, mark); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 1379 | // Marks the object black without pushing it on the marking stack. |
| 1380 | // Returns true if object needed marking and false otherwise. |
| 1381 | INLINE(static bool MarkObjectWithoutPush(Heap* heap, HeapObject* object)) { |
| 1382 | MarkBit mark_bit = Marking::MarkBitFrom(object); |
| 1383 | if (!mark_bit.Get()) { |
| 1384 | heap->mark_compact_collector()->SetMark(object, mark_bit); |
| 1385 | return true; |
| 1386 | } |
| 1387 | return false; |
| 1388 | } |
| 1389 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | // Mark object pointed to by p. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1391 | INLINE(static void MarkObjectByPointer(MarkCompactCollector* collector, |
| 1392 | Object** anchor_slot, |
| 1393 | Object** p)) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1394 | if (!(*p)->IsHeapObject()) return; |
| 1395 | HeapObject* object = ShortCircuitConsString(p); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1396 | collector->RecordSlot(anchor_slot, p, object); |
| 1397 | MarkBit mark = Marking::MarkBitFrom(object); |
| 1398 | collector->MarkObject(object, mark); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1401 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1402 | // Visit an unmarked object. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1403 | INLINE(static void VisitUnmarkedObject(MarkCompactCollector* collector, |
| 1404 | HeapObject* obj)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1405 | #ifdef DEBUG |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 1406 | ASSERT(collector->heap()->Contains(obj)); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 1407 | ASSERT(!collector->heap()->mark_compact_collector()->IsMarked(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1408 | #endif |
| 1409 | Map* map = obj->map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1410 | Heap* heap = obj->GetHeap(); |
| 1411 | MarkBit mark = Marking::MarkBitFrom(obj); |
| 1412 | heap->mark_compact_collector()->SetMark(obj, mark); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1413 | // Mark the map pointer and the body. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1414 | MarkBit map_mark = Marking::MarkBitFrom(map); |
| 1415 | heap->mark_compact_collector()->MarkObject(map, map_mark); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1416 | IterateBody(map, obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1419 | // Visit all unmarked objects pointed to by [start, end). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1420 | // Returns false if the operation fails (lack of stack space). |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 1421 | INLINE(static bool VisitUnmarkedObjects(Heap* heap, |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1422 | Object** start, |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 1423 | Object** end)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1424 | // Return false is we are close to the stack limit. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1425 | StackLimitCheck check(heap->isolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1426 | if (check.HasOverflowed()) return false; |
| 1427 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1428 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1429 | // Visit the unmarked objects. |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1430 | for (Object** p = start; p < end; p++) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1431 | Object* o = *p; |
| 1432 | if (!o->IsHeapObject()) continue; |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1433 | collector->RecordSlot(start, p, o); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1434 | HeapObject* obj = HeapObject::cast(o); |
| 1435 | MarkBit mark = Marking::MarkBitFrom(obj); |
| 1436 | if (mark.Get()) continue; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1437 | VisitUnmarkedObject(collector, obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1438 | } |
| 1439 | return true; |
| 1440 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1441 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1442 | INLINE(static void BeforeVisitingSharedFunctionInfo(HeapObject* object)) { |
| 1443 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(object); |
| 1444 | shared->BeforeVisitingPointers(); |
| 1445 | } |
| 1446 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1447 | static void VisitWeakCollection(Map* map, HeapObject* object) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1448 | MarkCompactCollector* collector = map->GetHeap()->mark_compact_collector(); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1449 | JSWeakCollection* weak_collection = |
| 1450 | reinterpret_cast<JSWeakCollection*>(object); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1451 | |
| 1452 | // Enqueue weak map in linked list of encountered weak maps. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1453 | if (weak_collection->next() == Smi::FromInt(0)) { |
| 1454 | weak_collection->set_next(collector->encountered_weak_collections()); |
| 1455 | collector->set_encountered_weak_collections(weak_collection); |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1456 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1457 | |
| 1458 | // Skip visiting the backing hash table containing the mappings. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1459 | int object_size = JSWeakCollection::BodyDescriptor::SizeOf(map, object); |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1460 | BodyVisitorBase<MarkCompactMarkingVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1461 | map->GetHeap(), |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1462 | object, |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1463 | JSWeakCollection::BodyDescriptor::kStartOffset, |
| 1464 | JSWeakCollection::kTableOffset); |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1465 | BodyVisitorBase<MarkCompactMarkingVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1466 | map->GetHeap(), |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1467 | object, |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1468 | JSWeakCollection::kTableOffset + kPointerSize, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1469 | object_size); |
| 1470 | |
| 1471 | // Mark the backing hash table without pushing it on the marking stack. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1472 | Object* table_object = weak_collection->table(); |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1473 | if (!table_object->IsHashTable()) return; |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 1474 | WeakHashTable* table = WeakHashTable::cast(table_object); |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1475 | Object** table_slot = |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1476 | HeapObject::RawField(weak_collection, JSWeakCollection::kTableOffset); |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1477 | MarkBit table_mark = Marking::MarkBitFrom(table); |
| 1478 | collector->RecordSlot(table_slot, table_slot, table); |
| 1479 | if (!table_mark.Get()) collector->SetMark(table, table_mark); |
| 1480 | // Recording the map slot can be skipped, because maps are not compacted. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1481 | collector->MarkObject(table->map(), Marking::MarkBitFrom(table->map())); |
| 1482 | ASSERT(MarkCompactCollector::IsMarked(table->map())); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1485 | private: |
| 1486 | template<int id> |
| 1487 | static inline void TrackObjectStatsAndVisit(Map* map, HeapObject* obj); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1488 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1489 | // Code flushing support. |
| 1490 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1491 | static const int kRegExpCodeThreshold = 5; |
| 1492 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1493 | static void UpdateRegExpCodeAgeAndFlush(Heap* heap, |
| 1494 | JSRegExp* re, |
| 1495 | bool is_ascii) { |
| 1496 | // Make sure that the fixed array is in fact initialized on the RegExp. |
| 1497 | // We could potentially trigger a GC when initializing the RegExp. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1498 | if (HeapObject::cast(re->data())->map()->instance_type() != |
| 1499 | FIXED_ARRAY_TYPE) return; |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1500 | |
| 1501 | // Make sure this is a RegExp that actually contains code. |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1502 | if (re->TypeTag() != JSRegExp::IRREGEXP) return; |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1503 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1504 | Object* code = re->DataAt(JSRegExp::code_index(is_ascii)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1505 | if (!code->IsSmi() && |
| 1506 | HeapObject::cast(code)->map()->instance_type() == CODE_TYPE) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1507 | // Save a copy that can be reinstated if we need the code again. |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1508 | re->SetDataAt(JSRegExp::saved_code_index(is_ascii), code); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Saving a copy might create a pointer into compaction candidate |
| 1511 | // that was not observed by marker. This might happen if JSRegExp data |
| 1512 | // was marked through the compilation cache before marker reached JSRegExp |
| 1513 | // object. |
| 1514 | FixedArray* data = FixedArray::cast(re->data()); |
| 1515 | Object** slot = data->data_start() + JSRegExp::saved_code_index(is_ascii); |
| 1516 | heap->mark_compact_collector()-> |
| 1517 | RecordSlot(slot, slot, code); |
| 1518 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1519 | // Set a number in the 0-255 range to guarantee no smi overflow. |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1520 | re->SetDataAt(JSRegExp::code_index(is_ascii), |
| 1521 | Smi::FromInt(heap->sweep_generation() & 0xff)); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1522 | } else if (code->IsSmi()) { |
| 1523 | int value = Smi::cast(code)->value(); |
| 1524 | // The regexp has not been compiled yet or there was a compilation error. |
| 1525 | if (value == JSRegExp::kUninitializedValue || |
| 1526 | value == JSRegExp::kCompilationErrorValue) { |
| 1527 | return; |
| 1528 | } |
| 1529 | |
| 1530 | // Check if we should flush now. |
| 1531 | if (value == ((heap->sweep_generation() - kRegExpCodeThreshold) & 0xff)) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1532 | re->SetDataAt(JSRegExp::code_index(is_ascii), |
| 1533 | Smi::FromInt(JSRegExp::kUninitializedValue)); |
| 1534 | re->SetDataAt(JSRegExp::saved_code_index(is_ascii), |
| 1535 | Smi::FromInt(JSRegExp::kUninitializedValue)); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | // Works by setting the current sweep_generation (as a smi) in the |
| 1542 | // code object place in the data array of the RegExp and keeps a copy |
| 1543 | // around that can be reinstated if we reuse the RegExp before flushing. |
| 1544 | // If we did not use the code for kRegExpCodeThreshold mark sweep GCs |
| 1545 | // we flush the code. |
| 1546 | static void VisitRegExpAndFlushCode(Map* map, HeapObject* object) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1547 | Heap* heap = map->GetHeap(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1548 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
| 1549 | if (!collector->is_code_flushing_enabled()) { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1550 | VisitJSRegExp(map, object); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1551 | return; |
| 1552 | } |
| 1553 | JSRegExp* re = reinterpret_cast<JSRegExp*>(object); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1554 | // Flush code or set age on both ASCII and two byte code. |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1555 | UpdateRegExpCodeAgeAndFlush(heap, re, true); |
| 1556 | UpdateRegExpCodeAgeAndFlush(heap, re, false); |
| 1557 | // Visit the fields of the RegExp, including the updated FixedArray. |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1558 | VisitJSRegExp(map, object); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1561 | static VisitorDispatchTable<Callback> non_count_table_; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1562 | }; |
| 1563 | |
| 1564 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1565 | void MarkCompactMarkingVisitor::ObjectStatsCountFixedArray( |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1566 | FixedArrayBase* fixed_array, |
| 1567 | FixedArraySubInstanceType fast_type, |
| 1568 | FixedArraySubInstanceType dictionary_type) { |
| 1569 | Heap* heap = fixed_array->map()->GetHeap(); |
| 1570 | if (fixed_array->map() != heap->fixed_cow_array_map() && |
| 1571 | fixed_array->map() != heap->fixed_double_array_map() && |
| 1572 | fixed_array != heap->empty_fixed_array()) { |
| 1573 | if (fixed_array->IsDictionary()) { |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1574 | heap->RecordFixedArraySubTypeStats(dictionary_type, |
| 1575 | fixed_array->Size()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1576 | } else { |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1577 | heap->RecordFixedArraySubTypeStats(fast_type, |
| 1578 | fixed_array->Size()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1584 | void MarkCompactMarkingVisitor::ObjectStatsVisitBase( |
| 1585 | MarkCompactMarkingVisitor::VisitorId id, Map* map, HeapObject* obj) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1586 | Heap* heap = map->GetHeap(); |
| 1587 | int object_size = obj->Size(); |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1588 | heap->RecordObjectStats(map->instance_type(), object_size); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1589 | non_count_table_.GetVisitorById(id)(map, obj); |
| 1590 | if (obj->IsJSObject()) { |
| 1591 | JSObject* object = JSObject::cast(obj); |
| 1592 | ObjectStatsCountFixedArray(object->elements(), |
| 1593 | DICTIONARY_ELEMENTS_SUB_TYPE, |
| 1594 | FAST_ELEMENTS_SUB_TYPE); |
| 1595 | ObjectStatsCountFixedArray(object->properties(), |
| 1596 | DICTIONARY_PROPERTIES_SUB_TYPE, |
| 1597 | FAST_PROPERTIES_SUB_TYPE); |
| 1598 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1602 | template<MarkCompactMarkingVisitor::VisitorId id> |
| 1603 | void MarkCompactMarkingVisitor::ObjectStatsTracker<id>::Visit( |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1604 | Map* map, HeapObject* obj) { |
| 1605 | ObjectStatsVisitBase(id, map, obj); |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | template<> |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1610 | class MarkCompactMarkingVisitor::ObjectStatsTracker< |
| 1611 | MarkCompactMarkingVisitor::kVisitMap> { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1612 | public: |
| 1613 | static inline void Visit(Map* map, HeapObject* obj) { |
| 1614 | Heap* heap = map->GetHeap(); |
| 1615 | Map* map_obj = Map::cast(obj); |
| 1616 | ASSERT(map->instance_type() == MAP_TYPE); |
| 1617 | DescriptorArray* array = map_obj->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 1618 | if (map_obj->owns_descriptors() && |
| 1619 | array != heap->empty_descriptor_array()) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1620 | int fixed_array_size = array->Size(); |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1621 | heap->RecordFixedArraySubTypeStats(DESCRIPTOR_ARRAY_SUB_TYPE, |
| 1622 | fixed_array_size); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1623 | } |
| 1624 | if (map_obj->HasTransitionArray()) { |
| 1625 | int fixed_array_size = map_obj->transitions()->Size(); |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1626 | heap->RecordFixedArraySubTypeStats(TRANSITION_ARRAY_SUB_TYPE, |
| 1627 | fixed_array_size); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1628 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1629 | if (map_obj->has_code_cache()) { |
| 1630 | CodeCache* cache = CodeCache::cast(map_obj->code_cache()); |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1631 | heap->RecordFixedArraySubTypeStats(MAP_CODE_CACHE_SUB_TYPE, |
| 1632 | cache->default_cache()->Size()); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1633 | if (!cache->normal_type_cache()->IsUndefined()) { |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1634 | heap->RecordFixedArraySubTypeStats( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1635 | MAP_CODE_CACHE_SUB_TYPE, |
| 1636 | FixedArray::cast(cache->normal_type_cache())->Size()); |
| 1637 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1638 | } |
| 1639 | ObjectStatsVisitBase(kVisitMap, map, obj); |
| 1640 | } |
| 1641 | }; |
| 1642 | |
| 1643 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1644 | template<> |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1645 | class MarkCompactMarkingVisitor::ObjectStatsTracker< |
| 1646 | MarkCompactMarkingVisitor::kVisitCode> { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1647 | public: |
| 1648 | static inline void Visit(Map* map, HeapObject* obj) { |
| 1649 | Heap* heap = map->GetHeap(); |
| 1650 | int object_size = obj->Size(); |
| 1651 | ASSERT(map->instance_type() == CODE_TYPE); |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1652 | Code* code_obj = Code::cast(obj); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 1653 | heap->RecordCodeSubTypeStats(code_obj->kind(), code_obj->GetRawAge(), |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1654 | object_size); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1655 | ObjectStatsVisitBase(kVisitCode, map, obj); |
| 1656 | } |
| 1657 | }; |
| 1658 | |
| 1659 | |
| 1660 | template<> |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1661 | class MarkCompactMarkingVisitor::ObjectStatsTracker< |
| 1662 | MarkCompactMarkingVisitor::kVisitSharedFunctionInfo> { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1663 | public: |
| 1664 | static inline void Visit(Map* map, HeapObject* obj) { |
| 1665 | Heap* heap = map->GetHeap(); |
| 1666 | SharedFunctionInfo* sfi = SharedFunctionInfo::cast(obj); |
| 1667 | if (sfi->scope_info() != heap->empty_fixed_array()) { |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1668 | heap->RecordFixedArraySubTypeStats( |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1669 | SCOPE_INFO_SUB_TYPE, |
| 1670 | FixedArray::cast(sfi->scope_info())->Size()); |
| 1671 | } |
| 1672 | ObjectStatsVisitBase(kVisitSharedFunctionInfo, map, obj); |
| 1673 | } |
| 1674 | }; |
| 1675 | |
| 1676 | |
| 1677 | template<> |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1678 | class MarkCompactMarkingVisitor::ObjectStatsTracker< |
| 1679 | MarkCompactMarkingVisitor::kVisitFixedArray> { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1680 | public: |
| 1681 | static inline void Visit(Map* map, HeapObject* obj) { |
| 1682 | Heap* heap = map->GetHeap(); |
| 1683 | FixedArray* fixed_array = FixedArray::cast(obj); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1684 | if (fixed_array == heap->string_table()) { |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 1685 | heap->RecordFixedArraySubTypeStats( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1686 | STRING_TABLE_SUB_TYPE, |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1687 | fixed_array->Size()); |
| 1688 | } |
| 1689 | ObjectStatsVisitBase(kVisitFixedArray, map, obj); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1690 | } |
| 1691 | }; |
| 1692 | |
| 1693 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1694 | void MarkCompactMarkingVisitor::Initialize() { |
| 1695 | StaticMarkingVisitor<MarkCompactMarkingVisitor>::Initialize(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1696 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1697 | table_.Register(kVisitJSRegExp, |
| 1698 | &VisitRegExpAndFlushCode); |
| 1699 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1700 | if (FLAG_track_gc_object_stats) { |
| 1701 | // Copy the visitor table to make call-through possible. |
| 1702 | non_count_table_.CopyFrom(&table_); |
| 1703 | #define VISITOR_ID_COUNT_FUNCTION(id) \ |
| 1704 | table_.Register(kVisit##id, ObjectStatsTracker<kVisit##id>::Visit); |
| 1705 | VISITOR_ID_LIST(VISITOR_ID_COUNT_FUNCTION) |
| 1706 | #undef VISITOR_ID_COUNT_FUNCTION |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1711 | VisitorDispatchTable<MarkCompactMarkingVisitor::Callback> |
| 1712 | MarkCompactMarkingVisitor::non_count_table_; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1713 | |
| 1714 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1715 | class CodeMarkingVisitor : public ThreadVisitor { |
| 1716 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1717 | explicit CodeMarkingVisitor(MarkCompactCollector* collector) |
| 1718 | : collector_(collector) {} |
| 1719 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1720 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1721 | collector_->PrepareThreadForCodeFlushing(isolate, top); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1722 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1723 | |
| 1724 | private: |
| 1725 | MarkCompactCollector* collector_; |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1726 | }; |
| 1727 | |
| 1728 | |
| 1729 | class SharedFunctionInfoMarkingVisitor : public ObjectVisitor { |
| 1730 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1731 | explicit SharedFunctionInfoMarkingVisitor(MarkCompactCollector* collector) |
| 1732 | : collector_(collector) {} |
| 1733 | |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1734 | void VisitPointers(Object** start, Object** end) { |
| 1735 | for (Object** p = start; p < end; p++) VisitPointer(p); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | void VisitPointer(Object** slot) { |
| 1739 | Object* obj = *slot; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1740 | if (obj->IsSharedFunctionInfo()) { |
| 1741 | SharedFunctionInfo* shared = reinterpret_cast<SharedFunctionInfo*>(obj); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1742 | MarkBit shared_mark = Marking::MarkBitFrom(shared); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1743 | MarkBit code_mark = Marking::MarkBitFrom(shared->code()); |
| 1744 | collector_->MarkObject(shared->code(), code_mark); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1745 | collector_->MarkObject(shared, shared_mark); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1746 | } |
| 1747 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1748 | |
| 1749 | private: |
| 1750 | MarkCompactCollector* collector_; |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1751 | }; |
| 1752 | |
| 1753 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1754 | void MarkCompactCollector::PrepareThreadForCodeFlushing(Isolate* isolate, |
| 1755 | ThreadLocalTop* top) { |
| 1756 | for (StackFrameIterator it(isolate, top); !it.done(); it.Advance()) { |
| 1757 | // Note: for the frame that has a pending lazy deoptimization |
| 1758 | // StackFrame::unchecked_code will return a non-optimized code object for |
| 1759 | // the outermost function and StackFrame::LookupCode will return |
| 1760 | // actual optimized code object. |
| 1761 | StackFrame* frame = it.frame(); |
| 1762 | Code* code = frame->unchecked_code(); |
| 1763 | MarkBit code_mark = Marking::MarkBitFrom(code); |
| 1764 | MarkObject(code, code_mark); |
| 1765 | if (frame->is_optimized()) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1766 | MarkCompactMarkingVisitor::MarkInlinedFunctionsCode(heap(), |
| 1767 | frame->LookupCode()); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1773 | void MarkCompactCollector::PrepareForCodeFlushing() { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1774 | // Enable code flushing for non-incremental cycles. |
| 1775 | if (FLAG_flush_code && !FLAG_flush_code_incrementally) { |
| 1776 | EnableCodeFlushing(!was_marked_incrementally_); |
mstarzinger@chromium.org | b101611 | 2012-11-02 15:55:00 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1779 | // If code flushing is disabled, there is no need to prepare for it. |
| 1780 | if (!is_code_flushing_enabled()) return; |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1781 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 1782 | // Ensure that empty descriptor array is marked. Method MarkDescriptorArray |
| 1783 | // relies on it being marked before any other descriptor array. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1784 | HeapObject* descriptor_array = heap()->empty_descriptor_array(); |
| 1785 | MarkBit descriptor_array_mark = Marking::MarkBitFrom(descriptor_array); |
| 1786 | MarkObject(descriptor_array, descriptor_array_mark); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 1787 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1788 | // Make sure we are not referencing the code from the stack. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1789 | ASSERT(this == heap()->mark_compact_collector()); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1790 | PrepareThreadForCodeFlushing(heap()->isolate(), |
| 1791 | heap()->isolate()->thread_local_top()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1792 | |
| 1793 | // Iterate the archived stacks in all threads to check if |
| 1794 | // the code is referenced. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1795 | CodeMarkingVisitor code_marking_visitor(this); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1796 | heap()->isolate()->thread_manager()->IterateArchivedThreads( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1797 | &code_marking_visitor); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1798 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1799 | SharedFunctionInfoMarkingVisitor visitor(this); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1800 | heap()->isolate()->compilation_cache()->IterateFunctions(&visitor); |
| 1801 | heap()->isolate()->handle_scope_implementer()->Iterate(&visitor); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1802 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1803 | ProcessMarkingDeque(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1807 | // Visitor class for marking heap roots. |
| 1808 | class RootMarkingVisitor : public ObjectVisitor { |
| 1809 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1810 | explicit RootMarkingVisitor(Heap* heap) |
| 1811 | : collector_(heap->mark_compact_collector()) { } |
| 1812 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1813 | void VisitPointer(Object** p) { |
| 1814 | MarkObjectByPointer(p); |
| 1815 | } |
| 1816 | |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1817 | void VisitPointers(Object** start, Object** end) { |
| 1818 | for (Object** p = start; p < end; p++) MarkObjectByPointer(p); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1821 | private: |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1822 | void MarkObjectByPointer(Object** p) { |
| 1823 | if (!(*p)->IsHeapObject()) return; |
| 1824 | |
| 1825 | // Replace flat cons strings in place. |
| 1826 | HeapObject* object = ShortCircuitConsString(p); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1827 | MarkBit mark_bit = Marking::MarkBitFrom(object); |
| 1828 | if (mark_bit.Get()) return; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1829 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1830 | Map* map = object->map(); |
| 1831 | // Mark the object. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1832 | collector_->SetMark(object, mark_bit); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1833 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1834 | // Mark the map pointer and body, and push them on the marking stack. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1835 | MarkBit map_mark = Marking::MarkBitFrom(map); |
| 1836 | collector_->MarkObject(map, map_mark); |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1837 | MarkCompactMarkingVisitor::IterateBody(map, object); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Mark all the objects reachable from the map and body. May leave |
| 1840 | // overflowed objects in the heap. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1841 | collector_->EmptyMarkingDeque(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1842 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1843 | |
| 1844 | MarkCompactCollector* collector_; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1845 | }; |
| 1846 | |
| 1847 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1848 | // Helper class for pruning the string table. |
| 1849 | class StringTableCleaner : public ObjectVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1850 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1851 | explicit StringTableCleaner(Heap* heap) |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1852 | : heap_(heap), pointers_removed_(0) { } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1853 | |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 1854 | virtual void VisitPointers(Object** start, Object** end) { |
| 1855 | // Visit all HeapObject pointers in [start, end). |
| 1856 | for (Object** p = start; p < end; p++) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1857 | Object* o = *p; |
| 1858 | if (o->IsHeapObject() && |
| 1859 | !Marking::MarkBitFrom(HeapObject::cast(o)).Get()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1860 | // Check if the internalized string being pruned is external. We need to |
| 1861 | // delete the associated external data as this string is going away. |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1862 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1863 | // Since no objects have yet been moved we can safely access the map of |
| 1864 | // the object. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1865 | if (o->IsExternalString()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1866 | heap_->FinalizeExternalString(String::cast(*p)); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1867 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1868 | // Set the entry to the_hole_value (as deleted). |
| 1869 | *p = heap_->the_hole_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1870 | pointers_removed_++; |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | int PointersRemoved() { |
| 1876 | return pointers_removed_; |
| 1877 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1878 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | private: |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1880 | Heap* heap_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1881 | int pointers_removed_; |
| 1882 | }; |
| 1883 | |
| 1884 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1885 | // Implementation of WeakObjectRetainer for mark compact GCs. All marked objects |
| 1886 | // are retained. |
| 1887 | class MarkCompactWeakObjectRetainer : public WeakObjectRetainer { |
| 1888 | public: |
| 1889 | virtual Object* RetainAs(Object* object) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1890 | if (Marking::MarkBitFrom(HeapObject::cast(object)).Get()) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1891 | return object; |
| 1892 | } else { |
| 1893 | return NULL; |
| 1894 | } |
| 1895 | } |
| 1896 | }; |
| 1897 | |
| 1898 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1899 | // Fill the marking stack with overflowed objects returned by the given |
| 1900 | // iterator. Stop when the marking stack is filled or the end of the space |
| 1901 | // is reached, whichever comes first. |
| 1902 | template<class T> |
| 1903 | static void DiscoverGreyObjectsWithIterator(Heap* heap, |
| 1904 | MarkingDeque* marking_deque, |
| 1905 | T* it) { |
| 1906 | // The caller should ensure that the marking stack is initially not full, |
| 1907 | // so that we don't waste effort pointlessly scanning for objects. |
| 1908 | ASSERT(!marking_deque->IsFull()); |
| 1909 | |
| 1910 | Map* filler_map = heap->one_pointer_filler_map(); |
| 1911 | for (HeapObject* object = it->Next(); |
| 1912 | object != NULL; |
| 1913 | object = it->Next()) { |
| 1914 | MarkBit markbit = Marking::MarkBitFrom(object); |
| 1915 | if ((object->map() != filler_map) && Marking::IsGrey(markbit)) { |
| 1916 | Marking::GreyToBlack(markbit); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1917 | MemoryChunk::IncrementLiveBytesFromGC(object->address(), object->Size()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1918 | marking_deque->PushBlack(object); |
| 1919 | if (marking_deque->IsFull()) return; |
| 1920 | } |
| 1921 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1925 | static inline int MarkWordToObjectStarts(uint32_t mark_bits, int* starts); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1926 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1927 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 1928 | static void DiscoverGreyObjectsOnPage(MarkingDeque* marking_deque, |
| 1929 | MemoryChunk* p) { |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 1930 | ASSERT(!marking_deque->IsFull()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1931 | ASSERT(strcmp(Marking::kWhiteBitPattern, "00") == 0); |
| 1932 | ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0); |
| 1933 | ASSERT(strcmp(Marking::kGreyBitPattern, "11") == 0); |
| 1934 | ASSERT(strcmp(Marking::kImpossibleBitPattern, "01") == 0); |
| 1935 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 1936 | for (MarkBitCellIterator it(p); !it.Done(); it.Advance()) { |
| 1937 | Address cell_base = it.CurrentCellBase(); |
| 1938 | MarkBit::CellType* cell = it.CurrentCell(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1939 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 1940 | const MarkBit::CellType current_cell = *cell; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1941 | if (current_cell == 0) continue; |
| 1942 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 1943 | MarkBit::CellType grey_objects; |
| 1944 | if (it.HasNext()) { |
| 1945 | const MarkBit::CellType next_cell = *(cell+1); |
| 1946 | grey_objects = current_cell & |
| 1947 | ((current_cell >> 1) | (next_cell << (Bitmap::kBitsPerCell - 1))); |
| 1948 | } else { |
| 1949 | grey_objects = current_cell & (current_cell >> 1); |
| 1950 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1951 | |
| 1952 | int offset = 0; |
| 1953 | while (grey_objects != 0) { |
| 1954 | int trailing_zeros = CompilerIntrinsics::CountTrailingZeros(grey_objects); |
| 1955 | grey_objects >>= trailing_zeros; |
| 1956 | offset += trailing_zeros; |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 1957 | MarkBit markbit(cell, 1 << offset, false); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1958 | ASSERT(Marking::IsGrey(markbit)); |
| 1959 | Marking::GreyToBlack(markbit); |
| 1960 | Address addr = cell_base + offset * kPointerSize; |
| 1961 | HeapObject* object = HeapObject::FromAddress(addr); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1962 | MemoryChunk::IncrementLiveBytesFromGC(object->address(), object->Size()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1963 | marking_deque->PushBlack(object); |
| 1964 | if (marking_deque->IsFull()) return; |
| 1965 | offset += 2; |
| 1966 | grey_objects >>= 2; |
| 1967 | } |
| 1968 | |
| 1969 | grey_objects >>= (Bitmap::kBitsPerCell - 1); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 1974 | int MarkCompactCollector::DiscoverAndPromoteBlackObjectsOnPage( |
| 1975 | NewSpace* new_space, |
| 1976 | NewSpacePage* p) { |
| 1977 | ASSERT(strcmp(Marking::kWhiteBitPattern, "00") == 0); |
| 1978 | ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0); |
| 1979 | ASSERT(strcmp(Marking::kGreyBitPattern, "11") == 0); |
| 1980 | ASSERT(strcmp(Marking::kImpossibleBitPattern, "01") == 0); |
| 1981 | |
| 1982 | MarkBit::CellType* cells = p->markbits()->cells(); |
| 1983 | int survivors_size = 0; |
| 1984 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 1985 | for (MarkBitCellIterator it(p); !it.Done(); it.Advance()) { |
| 1986 | Address cell_base = it.CurrentCellBase(); |
| 1987 | MarkBit::CellType* cell = it.CurrentCell(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 1988 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 1989 | MarkBit::CellType current_cell = *cell; |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 1990 | if (current_cell == 0) continue; |
| 1991 | |
| 1992 | int offset = 0; |
| 1993 | while (current_cell != 0) { |
| 1994 | int trailing_zeros = CompilerIntrinsics::CountTrailingZeros(current_cell); |
| 1995 | current_cell >>= trailing_zeros; |
| 1996 | offset += trailing_zeros; |
| 1997 | Address address = cell_base + offset * kPointerSize; |
| 1998 | HeapObject* object = HeapObject::FromAddress(address); |
| 1999 | |
| 2000 | int size = object->Size(); |
| 2001 | survivors_size += size; |
| 2002 | |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 2003 | if (FLAG_trace_track_allocation_sites && object->IsJSObject()) { |
| 2004 | if (AllocationMemento::FindForJSObject(JSObject::cast(object), true) |
| 2005 | != NULL) { |
| 2006 | heap()->allocation_mementos_found_++; |
| 2007 | } |
| 2008 | } |
| 2009 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 2010 | offset++; |
| 2011 | current_cell >>= 1; |
| 2012 | // Aggressively promote young survivors to the old space. |
| 2013 | if (TryPromoteObject(object, size)) { |
| 2014 | continue; |
| 2015 | } |
| 2016 | |
| 2017 | // Promotion failed. Just migrate object to another semispace. |
| 2018 | MaybeObject* allocation = new_space->AllocateRaw(size); |
| 2019 | if (allocation->IsFailure()) { |
| 2020 | if (!new_space->AddFreshPage()) { |
| 2021 | // Shouldn't happen. We are sweeping linearly, and to-space |
| 2022 | // has the same number of pages as from-space, so there is |
| 2023 | // always room. |
| 2024 | UNREACHABLE(); |
| 2025 | } |
| 2026 | allocation = new_space->AllocateRaw(size); |
| 2027 | ASSERT(!allocation->IsFailure()); |
| 2028 | } |
| 2029 | Object* target = allocation->ToObjectUnchecked(); |
| 2030 | |
| 2031 | MigrateObject(HeapObject::cast(target)->address(), |
| 2032 | object->address(), |
| 2033 | size, |
| 2034 | NEW_SPACE); |
| 2035 | } |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 2036 | *cells = 0; |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 2037 | } |
| 2038 | return survivors_size; |
| 2039 | } |
| 2040 | |
| 2041 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2042 | static void DiscoverGreyObjectsInSpace(Heap* heap, |
| 2043 | MarkingDeque* marking_deque, |
| 2044 | PagedSpace* space) { |
| 2045 | if (!space->was_swept_conservatively()) { |
| 2046 | HeapObjectIterator it(space); |
| 2047 | DiscoverGreyObjectsWithIterator(heap, marking_deque, &it); |
| 2048 | } else { |
| 2049 | PageIterator it(space); |
| 2050 | while (it.has_next()) { |
| 2051 | Page* p = it.next(); |
| 2052 | DiscoverGreyObjectsOnPage(marking_deque, p); |
| 2053 | if (marking_deque->IsFull()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2054 | } |
| 2055 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2056 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | |
| 2058 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 2059 | static void DiscoverGreyObjectsInNewSpace(Heap* heap, |
| 2060 | MarkingDeque* marking_deque) { |
| 2061 | NewSpace* space = heap->new_space(); |
| 2062 | NewSpacePageIterator it(space->bottom(), space->top()); |
| 2063 | while (it.has_next()) { |
| 2064 | NewSpacePage* page = it.next(); |
| 2065 | DiscoverGreyObjectsOnPage(marking_deque, page); |
| 2066 | if (marking_deque->IsFull()) return; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2071 | bool MarkCompactCollector::IsUnmarkedHeapObject(Object** p) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2072 | Object* o = *p; |
| 2073 | if (!o->IsHeapObject()) return false; |
| 2074 | HeapObject* heap_object = HeapObject::cast(o); |
| 2075 | MarkBit mark = Marking::MarkBitFrom(heap_object); |
| 2076 | return !mark.Get(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 2080 | bool MarkCompactCollector::IsUnmarkedHeapObjectWithHeap(Heap* heap, |
| 2081 | Object** p) { |
| 2082 | Object* o = *p; |
| 2083 | ASSERT(o->IsHeapObject()); |
| 2084 | HeapObject* heap_object = HeapObject::cast(o); |
| 2085 | MarkBit mark = Marking::MarkBitFrom(heap_object); |
| 2086 | return !mark.Get(); |
| 2087 | } |
| 2088 | |
| 2089 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2090 | void MarkCompactCollector::MarkStringTable(RootMarkingVisitor* visitor) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2091 | StringTable* string_table = heap()->string_table(); |
| 2092 | // Mark the string table itself. |
| 2093 | MarkBit string_table_mark = Marking::MarkBitFrom(string_table); |
| 2094 | SetMark(string_table, string_table_mark); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2095 | // Explicitly mark the prefix. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2096 | string_table->IteratePrefix(visitor); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2097 | ProcessMarkingDeque(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | |
| 2101 | void MarkCompactCollector::MarkRoots(RootMarkingVisitor* visitor) { |
| 2102 | // Mark the heap roots including global variables, stack variables, |
| 2103 | // etc., and all objects reachable from them. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2104 | heap()->IterateStrongRoots(visitor, VISIT_ONLY_STRONG); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2105 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2106 | // Handle the string table specially. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2107 | MarkStringTable(visitor); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2108 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 2109 | MarkWeakObjectToCodeTable(); |
| 2110 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2111 | // There may be overflowed objects in the heap. Visit them now. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2112 | while (marking_deque_.overflowed()) { |
| 2113 | RefillMarkingDeque(); |
| 2114 | EmptyMarkingDeque(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2115 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2116 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2117 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2118 | |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2119 | void MarkCompactCollector::MarkImplicitRefGroups() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2120 | List<ImplicitRefGroup*>* ref_groups = |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 2121 | isolate()->global_handles()->implicit_ref_groups(); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2122 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2123 | int last = 0; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2124 | for (int i = 0; i < ref_groups->length(); i++) { |
| 2125 | ImplicitRefGroup* entry = ref_groups->at(i); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2126 | ASSERT(entry != NULL); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2127 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2128 | if (!IsMarked(*entry->parent)) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2129 | (*ref_groups)[last++] = entry; |
| 2130 | continue; |
| 2131 | } |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2132 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2133 | Object*** children = entry->children; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2134 | // A parent object is marked, so mark all child heap objects. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2135 | for (size_t j = 0; j < entry->length; ++j) { |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2136 | if ((*children[j])->IsHeapObject()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2137 | HeapObject* child = HeapObject::cast(*children[j]); |
| 2138 | MarkBit mark = Marking::MarkBitFrom(child); |
| 2139 | MarkObject(child, mark); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2143 | // Once the entire group has been marked, dispose it because it's |
| 2144 | // not needed anymore. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2145 | delete entry; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2146 | } |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2147 | ref_groups->Rewind(last); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 2151 | void MarkCompactCollector::MarkWeakObjectToCodeTable() { |
| 2152 | HeapObject* weak_object_to_code_table = |
| 2153 | HeapObject::cast(heap()->weak_object_to_code_table()); |
| 2154 | if (!IsMarked(weak_object_to_code_table)) { |
| 2155 | MarkBit mark = Marking::MarkBitFrom(weak_object_to_code_table); |
| 2156 | SetMark(weak_object_to_code_table, mark); |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2161 | // Mark all objects reachable from the objects on the marking stack. |
| 2162 | // Before: the marking stack contains zero or more heap object pointers. |
| 2163 | // After: the marking stack is empty, and all objects reachable from the |
| 2164 | // marking stack have been marked, or are overflowed in the heap. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2165 | void MarkCompactCollector::EmptyMarkingDeque() { |
| 2166 | while (!marking_deque_.IsEmpty()) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 2167 | HeapObject* object = marking_deque_.Pop(); |
| 2168 | ASSERT(object->IsHeapObject()); |
| 2169 | ASSERT(heap()->Contains(object)); |
| 2170 | ASSERT(Marking::IsBlack(Marking::MarkBitFrom(object))); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2171 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 2172 | Map* map = object->map(); |
| 2173 | MarkBit map_mark = Marking::MarkBitFrom(map); |
| 2174 | MarkObject(map, map_mark); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 2175 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 2176 | MarkCompactMarkingVisitor::IterateBody(map, object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2177 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2178 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2179 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2180 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2181 | // Sweep the heap for overflowed objects, clear their overflow bits, and |
| 2182 | // push them on the marking stack. Stop early if the marking stack fills |
| 2183 | // before sweeping completes. If sweeping completes, there are no remaining |
| 2184 | // overflowed objects in the heap so the overflow flag on the markings stack |
| 2185 | // is cleared. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2186 | void MarkCompactCollector::RefillMarkingDeque() { |
| 2187 | ASSERT(marking_deque_.overflowed()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2188 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 2189 | DiscoverGreyObjectsInNewSpace(heap(), &marking_deque_); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2190 | if (marking_deque_.IsFull()) return; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2191 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2192 | DiscoverGreyObjectsInSpace(heap(), |
| 2193 | &marking_deque_, |
| 2194 | heap()->old_pointer_space()); |
| 2195 | if (marking_deque_.IsFull()) return; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2196 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2197 | DiscoverGreyObjectsInSpace(heap(), |
| 2198 | &marking_deque_, |
| 2199 | heap()->old_data_space()); |
| 2200 | if (marking_deque_.IsFull()) return; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2201 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2202 | DiscoverGreyObjectsInSpace(heap(), |
| 2203 | &marking_deque_, |
| 2204 | heap()->code_space()); |
| 2205 | if (marking_deque_.IsFull()) return; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2206 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2207 | DiscoverGreyObjectsInSpace(heap(), |
| 2208 | &marking_deque_, |
| 2209 | heap()->map_space()); |
| 2210 | if (marking_deque_.IsFull()) return; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2211 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2212 | DiscoverGreyObjectsInSpace(heap(), |
| 2213 | &marking_deque_, |
| 2214 | heap()->cell_space()); |
| 2215 | if (marking_deque_.IsFull()) return; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2216 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2217 | DiscoverGreyObjectsInSpace(heap(), |
| 2218 | &marking_deque_, |
| 2219 | heap()->property_cell_space()); |
| 2220 | if (marking_deque_.IsFull()) return; |
| 2221 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2222 | LargeObjectIterator lo_it(heap()->lo_space()); |
| 2223 | DiscoverGreyObjectsWithIterator(heap(), |
| 2224 | &marking_deque_, |
| 2225 | &lo_it); |
| 2226 | if (marking_deque_.IsFull()) return; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2227 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2228 | marking_deque_.ClearOverflowed(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | |
| 2232 | // Mark all objects reachable (transitively) from objects on the marking |
| 2233 | // stack. Before: the marking stack contains zero or more heap object |
| 2234 | // pointers. After: the marking stack is empty and there are no overflowed |
| 2235 | // objects in the heap. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2236 | void MarkCompactCollector::ProcessMarkingDeque() { |
| 2237 | EmptyMarkingDeque(); |
| 2238 | while (marking_deque_.overflowed()) { |
| 2239 | RefillMarkingDeque(); |
| 2240 | EmptyMarkingDeque(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 2245 | // Mark all objects reachable (transitively) from objects on the marking |
| 2246 | // stack including references only considered in the atomic marking pause. |
| 2247 | void MarkCompactCollector::ProcessEphemeralMarking(ObjectVisitor* visitor) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2248 | bool work_to_do = true; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2249 | ASSERT(marking_deque_.IsEmpty()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2250 | while (work_to_do) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 2251 | isolate()->global_handles()->IterateObjectGroups( |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 2252 | visitor, &IsUnmarkedHeapObjectWithHeap); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2253 | MarkImplicitRefGroups(); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 2254 | ProcessWeakCollections(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2255 | work_to_do = !marking_deque_.IsEmpty(); |
| 2256 | ProcessMarkingDeque(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 2261 | void MarkCompactCollector::ProcessTopOptimizedFrame(ObjectVisitor* visitor) { |
| 2262 | for (StackFrameIterator it(isolate(), isolate()->thread_local_top()); |
| 2263 | !it.done(); it.Advance()) { |
| 2264 | if (it.frame()->type() == StackFrame::JAVA_SCRIPT) { |
| 2265 | return; |
| 2266 | } |
| 2267 | if (it.frame()->type() == StackFrame::OPTIMIZED) { |
| 2268 | Code* code = it.frame()->LookupCode(); |
| 2269 | if (!code->CanDeoptAt(it.frame()->pc())) { |
| 2270 | code->CodeIterateBody(visitor); |
| 2271 | } |
| 2272 | ProcessMarkingDeque(); |
| 2273 | return; |
| 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2279 | void MarkCompactCollector::MarkLiveObjects() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 2280 | GCTracer::Scope gc_scope(tracer_, GCTracer::Scope::MC_MARK); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2281 | // The recursive GC marker detects when it is nearing stack overflow, |
| 2282 | // and switches to a different marking system. JS interrupts interfere |
| 2283 | // with the C stack limit check. |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 2284 | PostponeInterruptsScope postpone(isolate()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2285 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2286 | bool incremental_marking_overflowed = false; |
| 2287 | IncrementalMarking* incremental_marking = heap_->incremental_marking(); |
| 2288 | if (was_marked_incrementally_) { |
| 2289 | // Finalize the incremental marking and check whether we had an overflow. |
| 2290 | // Both markers use grey color to mark overflowed objects so |
| 2291 | // non-incremental marker can deal with them as if overflow |
| 2292 | // occured during normal marking. |
| 2293 | // But incremental marker uses a separate marking deque |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 2294 | // so we have to explicitly copy its overflow state. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2295 | incremental_marking->Finalize(); |
| 2296 | incremental_marking_overflowed = |
| 2297 | incremental_marking->marking_deque()->overflowed(); |
| 2298 | incremental_marking->marking_deque()->ClearOverflowed(); |
| 2299 | } else { |
| 2300 | // Abort any pending incremental activities e.g. incremental sweeping. |
| 2301 | incremental_marking->Abort(); |
| 2302 | } |
| 2303 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2304 | #ifdef DEBUG |
| 2305 | ASSERT(state_ == PREPARE_GC); |
| 2306 | state_ = MARK_LIVE_OBJECTS; |
| 2307 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2308 | // The to space contains live objects, a page in from space is used as a |
| 2309 | // marking stack. |
| 2310 | Address marking_deque_start = heap()->new_space()->FromSpacePageLow(); |
| 2311 | Address marking_deque_end = heap()->new_space()->FromSpacePageHigh(); |
| 2312 | if (FLAG_force_marking_deque_overflows) { |
| 2313 | marking_deque_end = marking_deque_start + 64 * kPointerSize; |
| 2314 | } |
| 2315 | marking_deque_.Initialize(marking_deque_start, |
| 2316 | marking_deque_end); |
| 2317 | ASSERT(!marking_deque_.overflowed()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2318 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2319 | if (incremental_marking_overflowed) { |
| 2320 | // There are overflowed objects left in the heap after incremental marking. |
| 2321 | marking_deque_.SetOverflowed(); |
| 2322 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2323 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 2324 | PrepareForCodeFlushing(); |
| 2325 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2326 | if (was_marked_incrementally_) { |
| 2327 | // There is no write barrier on cells so we have to scan them now at the end |
| 2328 | // of the incremental marking. |
| 2329 | { |
| 2330 | HeapObjectIterator cell_iterator(heap()->cell_space()); |
| 2331 | HeapObject* cell; |
| 2332 | while ((cell = cell_iterator.Next()) != NULL) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2333 | ASSERT(cell->IsCell()); |
| 2334 | if (IsMarked(cell)) { |
| 2335 | int offset = Cell::kValueOffset; |
| 2336 | MarkCompactMarkingVisitor::VisitPointer( |
| 2337 | heap(), |
| 2338 | reinterpret_cast<Object**>(cell->address() + offset)); |
| 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | { |
| 2343 | HeapObjectIterator js_global_property_cell_iterator( |
| 2344 | heap()->property_cell_space()); |
| 2345 | HeapObject* cell; |
| 2346 | while ((cell = js_global_property_cell_iterator.Next()) != NULL) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2347 | ASSERT(cell->IsPropertyCell()); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2348 | if (IsMarked(cell)) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2349 | MarkCompactMarkingVisitor::VisitPropertyCell(cell->map(), cell); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | } |
| 2354 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2355 | RootMarkingVisitor root_visitor(heap()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2356 | MarkRoots(&root_visitor); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2357 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 2358 | ProcessTopOptimizedFrame(&root_visitor); |
| 2359 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2360 | // The objects reachable from the roots are marked, yet unreachable |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 2361 | // objects are unmarked. Mark objects reachable due to host |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 2362 | // application specific logic or through Harmony weak maps. |
| 2363 | ProcessEphemeralMarking(&root_visitor); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2364 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 2365 | // The objects reachable from the roots, weak maps or object groups |
| 2366 | // are marked, yet unreachable objects are unmarked. Mark objects |
| 2367 | // reachable only from weak global handles. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2368 | // |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2369 | // First we identify nonlive weak handles and mark them as pending |
| 2370 | // destruction. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2371 | heap()->isolate()->global_handles()->IdentifyWeakHandles( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2372 | &IsUnmarkedHeapObject); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2373 | // Then we mark the objects and process the transitive closure. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2374 | heap()->isolate()->global_handles()->IterateWeakRoots(&root_visitor); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2375 | while (marking_deque_.overflowed()) { |
| 2376 | RefillMarkingDeque(); |
| 2377 | EmptyMarkingDeque(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2378 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2379 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 2380 | // Repeat host application specific and Harmony weak maps marking to |
| 2381 | // mark unmarked objects reachable from the weak roots. |
| 2382 | ProcessEphemeralMarking(&root_visitor); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2383 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2384 | AfterMarking(); |
| 2385 | } |
| 2386 | |
| 2387 | |
| 2388 | void MarkCompactCollector::AfterMarking() { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2389 | // Object literal map caches reference strings (cache keys) and maps |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2390 | // (cache values). At this point still useful maps have already been |
| 2391 | // marked. Mark the keys for the alive values before we process the |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2392 | // string table. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2393 | ProcessMapCaches(); |
| 2394 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2395 | // Prune the string table removing all strings only pointed to by the |
| 2396 | // string table. Cannot use string_table() here because the string |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2397 | // table is marked. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2398 | StringTable* string_table = heap()->string_table(); |
| 2399 | StringTableCleaner v(heap()); |
| 2400 | string_table->IterateElements(&v); |
| 2401 | string_table->ElementsRemoved(v.PointersRemoved()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2402 | heap()->external_string_table_.Iterate(&v); |
| 2403 | heap()->external_string_table_.CleanUp(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2404 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 2405 | // Process the weak references. |
| 2406 | MarkCompactWeakObjectRetainer mark_compact_object_retainer; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2407 | heap()->ProcessWeakReferences(&mark_compact_object_retainer); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 2408 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2409 | // Remove object groups after marking phase. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2410 | heap()->isolate()->global_handles()->RemoveObjectGroups(); |
| 2411 | heap()->isolate()->global_handles()->RemoveImplicitRefGroups(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2412 | |
| 2413 | // Flush code from collected candidates. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2414 | if (is_code_flushing_enabled()) { |
| 2415 | code_flusher_->ProcessCandidates(); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 2416 | // If incremental marker does not support code flushing, we need to |
| 2417 | // disable it before incremental marking steps for next cycle. |
| 2418 | if (FLAG_flush_code && !FLAG_flush_code_incrementally) { |
| 2419 | EnableCodeFlushing(false); |
| 2420 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2421 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2422 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 2423 | if (!FLAG_watch_ic_patching) { |
| 2424 | // Clean up dead objects from the runtime profiler. |
| 2425 | heap()->isolate()->runtime_profiler()->RemoveDeadSamples(); |
| 2426 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2427 | |
| 2428 | if (FLAG_track_gc_object_stats) { |
| 2429 | heap()->CheckpointObjectStats(); |
| 2430 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2434 | void MarkCompactCollector::ProcessMapCaches() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2435 | Object* raw_context = heap()->native_contexts_list_; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2436 | while (raw_context != heap()->undefined_value()) { |
| 2437 | Context* context = reinterpret_cast<Context*>(raw_context); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2438 | if (IsMarked(context)) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2439 | HeapObject* raw_map_cache = |
| 2440 | HeapObject::cast(context->get(Context::MAP_CACHE_INDEX)); |
| 2441 | // A map cache may be reachable from the stack. In this case |
| 2442 | // it's already transitively marked and it's too late to clean |
| 2443 | // up its parts. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2444 | if (!IsMarked(raw_map_cache) && |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2445 | raw_map_cache != heap()->undefined_value()) { |
| 2446 | MapCache* map_cache = reinterpret_cast<MapCache*>(raw_map_cache); |
| 2447 | int existing_elements = map_cache->NumberOfElements(); |
| 2448 | int used_elements = 0; |
| 2449 | for (int i = MapCache::kElementsStartIndex; |
| 2450 | i < map_cache->length(); |
| 2451 | i += MapCache::kEntrySize) { |
| 2452 | Object* raw_key = map_cache->get(i); |
| 2453 | if (raw_key == heap()->undefined_value() || |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2454 | raw_key == heap()->the_hole_value()) continue; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2455 | STATIC_ASSERT(MapCache::kEntrySize == 2); |
| 2456 | Object* raw_map = map_cache->get(i + 1); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2457 | if (raw_map->IsHeapObject() && IsMarked(raw_map)) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2458 | ++used_elements; |
| 2459 | } else { |
| 2460 | // Delete useless entries with unmarked maps. |
| 2461 | ASSERT(raw_map->IsMap()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2462 | map_cache->set_the_hole(i); |
| 2463 | map_cache->set_the_hole(i + 1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | if (used_elements == 0) { |
| 2467 | context->set(Context::MAP_CACHE_INDEX, heap()->undefined_value()); |
| 2468 | } else { |
| 2469 | // Note: we don't actually shrink the cache here to avoid |
| 2470 | // extra complexity during GC. We rely on subsequent cache |
| 2471 | // usages (EnsureCapacity) to do this. |
| 2472 | map_cache->ElementsRemoved(existing_elements - used_elements); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2473 | MarkBit map_cache_markbit = Marking::MarkBitFrom(map_cache); |
| 2474 | MarkObject(map_cache, map_cache_markbit); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | // Move to next element in the list. |
| 2479 | raw_context = context->get(Context::NEXT_CONTEXT_LINK); |
| 2480 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2481 | ProcessMarkingDeque(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2485 | void MarkCompactCollector::ReattachInitialMaps() { |
| 2486 | HeapObjectIterator map_iterator(heap()->map_space()); |
| 2487 | for (HeapObject* obj = map_iterator.Next(); |
| 2488 | obj != NULL; |
| 2489 | obj = map_iterator.Next()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2490 | Map* map = Map::cast(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2491 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2492 | STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE); |
| 2493 | if (map->instance_type() < FIRST_JS_RECEIVER_TYPE) continue; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2494 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2495 | if (map->attached_to_shared_function_info()) { |
| 2496 | JSFunction::cast(map->constructor())->shared()->AttachInitialMap(map); |
| 2497 | } |
| 2498 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2501 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2502 | void MarkCompactCollector::ClearNonLiveReferences() { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2503 | // Iterate over the map space, setting map transitions that go from |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2504 | // a marked map to an unmarked map to null transitions. This action |
| 2505 | // is carried out only on maps of JSObjects and related subtypes. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2506 | HeapObjectIterator map_iterator(heap()->map_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2507 | for (HeapObject* obj = map_iterator.Next(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2508 | obj != NULL; |
| 2509 | obj = map_iterator.Next()) { |
| 2510 | Map* map = Map::cast(obj); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2511 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2512 | if (!map->CanTransition()) continue; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2513 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2514 | MarkBit map_mark = Marking::MarkBitFrom(map); |
| 2515 | if (map_mark.Get() && map->attached_to_shared_function_info()) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2516 | // This map is used for inobject slack tracking and has been detached |
| 2517 | // from SharedFunctionInfo during the mark phase. |
| 2518 | // Since it survived the GC, reattach it now. |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2519 | JSFunction::cast(map->constructor())->shared()->AttachInitialMap(map); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2522 | ClearNonLivePrototypeTransitions(map); |
| 2523 | ClearNonLiveMapTransitions(map, map_mark); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2524 | |
| 2525 | if (map_mark.Get()) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2526 | ClearNonLiveDependentCode(map->dependent_code()); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2527 | } else { |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 2528 | ClearAndDeoptimizeDependentCode(map->dependent_code()); |
| 2529 | map->set_dependent_code(DependentCode::cast(heap()->empty_fixed_array())); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2530 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2531 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2532 | |
| 2533 | // Iterate over property cell space, removing dependent code that is not |
| 2534 | // otherwise kept alive by strong references. |
| 2535 | HeapObjectIterator cell_iterator(heap_->property_cell_space()); |
| 2536 | for (HeapObject* cell = cell_iterator.Next(); |
| 2537 | cell != NULL; |
| 2538 | cell = cell_iterator.Next()) { |
| 2539 | if (IsMarked(cell)) { |
| 2540 | ClearNonLiveDependentCode(PropertyCell::cast(cell)->dependent_code()); |
| 2541 | } |
| 2542 | } |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 2543 | |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 2544 | // Iterate over allocation sites, removing dependent code that is not |
| 2545 | // otherwise kept alive by strong references. |
| 2546 | Object* undefined = heap()->undefined_value(); |
| 2547 | for (Object* site = heap()->allocation_sites_list(); |
| 2548 | site != undefined; |
| 2549 | site = AllocationSite::cast(site)->weak_next()) { |
| 2550 | if (IsMarked(site)) { |
| 2551 | ClearNonLiveDependentCode(AllocationSite::cast(site)->dependent_code()); |
| 2552 | } |
| 2553 | } |
| 2554 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 2555 | if (heap_->weak_object_to_code_table()->IsHashTable()) { |
| 2556 | WeakHashTable* table = |
| 2557 | WeakHashTable::cast(heap_->weak_object_to_code_table()); |
| 2558 | uint32_t capacity = table->Capacity(); |
| 2559 | for (uint32_t i = 0; i < capacity; i++) { |
| 2560 | uint32_t key_index = table->EntryToIndex(i); |
| 2561 | Object* key = table->get(key_index); |
| 2562 | if (!table->IsKey(key)) continue; |
| 2563 | uint32_t value_index = table->EntryToValueIndex(i); |
| 2564 | Object* value = table->get(value_index); |
| 2565 | if (IsMarked(key)) { |
| 2566 | if (!IsMarked(value)) { |
| 2567 | HeapObject* obj = HeapObject::cast(value); |
| 2568 | MarkBit mark = Marking::MarkBitFrom(obj); |
| 2569 | SetMark(obj, mark); |
| 2570 | } |
| 2571 | ClearNonLiveDependentCode(DependentCode::cast(value)); |
| 2572 | } else { |
| 2573 | ClearAndDeoptimizeDependentCode(DependentCode::cast(value)); |
| 2574 | table->set(key_index, heap_->the_hole_value()); |
| 2575 | table->set(value_index, heap_->the_hole_value()); |
| 2576 | } |
| 2577 | } |
| 2578 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2579 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2580 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2581 | |
| 2582 | void MarkCompactCollector::ClearNonLivePrototypeTransitions(Map* map) { |
| 2583 | int number_of_transitions = map->NumberOfProtoTransitions(); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 2584 | FixedArray* prototype_transitions = map->GetPrototypeTransitions(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2585 | |
| 2586 | int new_number_of_transitions = 0; |
| 2587 | const int header = Map::kProtoTransitionHeaderSize; |
| 2588 | const int proto_offset = header + Map::kProtoTransitionPrototypeOffset; |
| 2589 | const int map_offset = header + Map::kProtoTransitionMapOffset; |
| 2590 | const int step = Map::kProtoTransitionElementsPerEntry; |
| 2591 | for (int i = 0; i < number_of_transitions; i++) { |
| 2592 | Object* prototype = prototype_transitions->get(proto_offset + i * step); |
| 2593 | Object* cached_map = prototype_transitions->get(map_offset + i * step); |
| 2594 | if (IsMarked(prototype) && IsMarked(cached_map)) { |
| 2595 | int proto_index = proto_offset + new_number_of_transitions * step; |
| 2596 | int map_index = map_offset + new_number_of_transitions * step; |
| 2597 | if (new_number_of_transitions != i) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2598 | prototype_transitions->set( |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2599 | proto_index, |
| 2600 | prototype, |
| 2601 | UPDATE_WRITE_BARRIER); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2602 | prototype_transitions->set( |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2603 | map_index, |
| 2604 | cached_map, |
| 2605 | SKIP_WRITE_BARRIER); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2606 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2607 | Object** slot = |
| 2608 | HeapObject::RawField(prototype_transitions, |
| 2609 | FixedArray::OffsetOfElementAt(proto_index)); |
| 2610 | RecordSlot(slot, slot, prototype); |
| 2611 | new_number_of_transitions++; |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | if (new_number_of_transitions != number_of_transitions) { |
| 2616 | map->SetNumberOfProtoTransitions(new_number_of_transitions); |
| 2617 | } |
| 2618 | |
| 2619 | // Fill slots that became free with undefined value. |
| 2620 | for (int i = new_number_of_transitions * step; |
| 2621 | i < number_of_transitions * step; |
| 2622 | i++) { |
mstarzinger@chromium.org | 1f410f9 | 2013-08-29 08:13:16 +0000 | [diff] [blame] | 2623 | prototype_transitions->set_undefined(header + i); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | |
| 2628 | void MarkCompactCollector::ClearNonLiveMapTransitions(Map* map, |
| 2629 | MarkBit map_mark) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2630 | Object* potential_parent = map->GetBackPointer(); |
| 2631 | if (!potential_parent->IsMap()) return; |
| 2632 | Map* parent = Map::cast(potential_parent); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2633 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2634 | // Follow back pointer, check whether we are dealing with a map transition |
| 2635 | // from a live map to a dead path and in case clear transitions of parent. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2636 | bool current_is_alive = map_mark.Get(); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2637 | bool parent_is_alive = Marking::MarkBitFrom(parent).Get(); |
| 2638 | if (!current_is_alive && parent_is_alive) { |
| 2639 | parent->ClearNonLiveTransitions(heap()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2640 | } |
| 2641 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2642 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2643 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 2644 | void MarkCompactCollector::ClearAndDeoptimizeDependentCode( |
| 2645 | DependentCode* entries) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 2646 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2647 | DependentCode::GroupStartIndexes starts(entries); |
| 2648 | int number_of_entries = starts.number_of_entries(); |
| 2649 | if (number_of_entries == 0) return; |
| 2650 | for (int i = 0; i < number_of_entries; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2651 | // If the entry is compilation info then the map must be alive, |
| 2652 | // and ClearAndDeoptimizeDependentCode shouldn't be called. |
| 2653 | ASSERT(entries->is_code_at(i)); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2654 | Code* code = entries->code_at(i); |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 2655 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 2656 | if (IsMarked(code) && !code->marked_for_deoptimization()) { |
| 2657 | code->set_marked_for_deoptimization(true); |
| 2658 | have_code_to_deoptimize_ = true; |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2659 | } |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2660 | entries->clear_at(i); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2661 | } |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2665 | void MarkCompactCollector::ClearNonLiveDependentCode(DependentCode* entries) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 2666 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2667 | DependentCode::GroupStartIndexes starts(entries); |
| 2668 | int number_of_entries = starts.number_of_entries(); |
| 2669 | if (number_of_entries == 0) return; |
| 2670 | int new_number_of_entries = 0; |
| 2671 | // Go through all groups, remove dead codes and compact. |
| 2672 | for (int g = 0; g < DependentCode::kGroupCount; g++) { |
| 2673 | int group_number_of_entries = 0; |
| 2674 | for (int i = starts.at(g); i < starts.at(g + 1); i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2675 | Object* obj = entries->object_at(i); |
| 2676 | ASSERT(obj->IsCode() || IsMarked(obj)); |
| 2677 | if (IsMarked(obj) && |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 2678 | (!obj->IsCode() || !WillBeDeoptimized(Code::cast(obj)))) { |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2679 | if (new_number_of_entries + group_number_of_entries != i) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2680 | entries->set_object_at( |
| 2681 | new_number_of_entries + group_number_of_entries, obj); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2682 | } |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2683 | Object** slot = entries->slot_at(new_number_of_entries + |
| 2684 | group_number_of_entries); |
| 2685 | RecordSlot(slot, slot, obj); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2686 | group_number_of_entries++; |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2687 | } |
| 2688 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2689 | entries->set_number_of_entries( |
| 2690 | static_cast<DependentCode::DependencyGroup>(g), |
| 2691 | group_number_of_entries); |
| 2692 | new_number_of_entries += group_number_of_entries; |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2693 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 2694 | for (int i = new_number_of_entries; i < number_of_entries; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2695 | entries->clear_at(i); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2696 | } |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 2700 | void MarkCompactCollector::ProcessWeakCollections() { |
| 2701 | GCTracer::Scope gc_scope(tracer_, GCTracer::Scope::MC_WEAKCOLLECTION_PROCESS); |
| 2702 | Object* weak_collection_obj = encountered_weak_collections(); |
| 2703 | while (weak_collection_obj != Smi::FromInt(0)) { |
| 2704 | ASSERT(MarkCompactCollector::IsMarked( |
| 2705 | HeapObject::cast(weak_collection_obj))); |
| 2706 | JSWeakCollection* weak_collection = |
| 2707 | reinterpret_cast<JSWeakCollection*>(weak_collection_obj); |
| 2708 | ObjectHashTable* table = ObjectHashTable::cast(weak_collection->table()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2709 | Object** anchor = reinterpret_cast<Object**>(table->address()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2710 | for (int i = 0; i < table->Capacity(); i++) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2711 | if (MarkCompactCollector::IsMarked(HeapObject::cast(table->KeyAt(i)))) { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2712 | Object** key_slot = |
| 2713 | HeapObject::RawField(table, FixedArray::OffsetOfElementAt( |
| 2714 | ObjectHashTable::EntryToIndex(i))); |
| 2715 | RecordSlot(anchor, key_slot, *key_slot); |
| 2716 | Object** value_slot = |
| 2717 | HeapObject::RawField(table, FixedArray::OffsetOfElementAt( |
| 2718 | ObjectHashTable::EntryToValueIndex(i))); |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 2719 | MarkCompactMarkingVisitor::MarkObjectByPointer( |
| 2720 | this, anchor, value_slot); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2721 | } |
| 2722 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 2723 | weak_collection_obj = weak_collection->next(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 2728 | void MarkCompactCollector::ClearWeakCollections() { |
| 2729 | GCTracer::Scope gc_scope(tracer_, GCTracer::Scope::MC_WEAKCOLLECTION_CLEAR); |
| 2730 | Object* weak_collection_obj = encountered_weak_collections(); |
| 2731 | while (weak_collection_obj != Smi::FromInt(0)) { |
| 2732 | ASSERT(MarkCompactCollector::IsMarked( |
| 2733 | HeapObject::cast(weak_collection_obj))); |
| 2734 | JSWeakCollection* weak_collection = |
| 2735 | reinterpret_cast<JSWeakCollection*>(weak_collection_obj); |
| 2736 | ObjectHashTable* table = ObjectHashTable::cast(weak_collection->table()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2737 | for (int i = 0; i < table->Capacity(); i++) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2738 | if (!MarkCompactCollector::IsMarked(HeapObject::cast(table->KeyAt(i)))) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2739 | table->RemoveEntry(i); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2740 | } |
| 2741 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 2742 | weak_collection_obj = weak_collection->next(); |
| 2743 | weak_collection->set_next(Smi::FromInt(0)); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2744 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 2745 | set_encountered_weak_collections(Smi::FromInt(0)); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2748 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2749 | // We scavange new space simultaneously with sweeping. This is done in two |
| 2750 | // passes. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2751 | // |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2752 | // The first pass migrates all alive objects from one semispace to another or |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2753 | // promotes them to old space. Forwarding address is written directly into |
| 2754 | // first word of object without any encoding. If object is dead we write |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2755 | // NULL as a forwarding address. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2756 | // |
| 2757 | // The second pass updates pointers to new space in all spaces. It is possible |
| 2758 | // to encounter pointers to dead new space objects during traversal of pointers |
| 2759 | // to new space. We should clear them to avoid encountering them during next |
| 2760 | // pointer iteration. This is an issue if the store buffer overflows and we |
| 2761 | // have to scan the entire old space, including dead objects, looking for |
| 2762 | // pointers to new space. |
| 2763 | void MarkCompactCollector::MigrateObject(Address dst, |
| 2764 | Address src, |
| 2765 | int size, |
| 2766 | AllocationSpace dest) { |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 2767 | HeapProfiler* heap_profiler = heap()->isolate()->heap_profiler(); |
| 2768 | if (heap_profiler->is_profiling()) { |
| 2769 | heap_profiler->ObjectMoveEvent(src, dst, size); |
| 2770 | } |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 2771 | ASSERT(heap()->AllowedToBeMigrated(HeapObject::FromAddress(src), dest)); |
| 2772 | ASSERT(dest != LO_SPACE && size <= Page::kMaxNonCodeHeapObjectSize); |
hpayer@chromium.org | 83fa61b | 2013-07-24 09:36:58 +0000 | [diff] [blame] | 2773 | if (dest == OLD_POINTER_SPACE) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2774 | Address src_slot = src; |
| 2775 | Address dst_slot = dst; |
| 2776 | ASSERT(IsAligned(size, kPointerSize)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2777 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2778 | for (int remaining = size / kPointerSize; remaining > 0; remaining--) { |
| 2779 | Object* value = Memory::Object_at(src_slot); |
| 2780 | |
| 2781 | Memory::Object_at(dst_slot) = value; |
| 2782 | |
| 2783 | if (heap_->InNewSpace(value)) { |
| 2784 | heap_->store_buffer()->Mark(dst_slot); |
| 2785 | } else if (value->IsHeapObject() && IsOnEvacuationCandidate(value)) { |
| 2786 | SlotsBuffer::AddTo(&slots_buffer_allocator_, |
| 2787 | &migration_slots_buffer_, |
| 2788 | reinterpret_cast<Object**>(dst_slot), |
| 2789 | SlotsBuffer::IGNORE_OVERFLOW); |
| 2790 | } |
| 2791 | |
| 2792 | src_slot += kPointerSize; |
| 2793 | dst_slot += kPointerSize; |
| 2794 | } |
| 2795 | |
| 2796 | if (compacting_ && HeapObject::FromAddress(dst)->IsJSFunction()) { |
| 2797 | Address code_entry_slot = dst + JSFunction::kCodeEntryOffset; |
| 2798 | Address code_entry = Memory::Address_at(code_entry_slot); |
| 2799 | |
| 2800 | if (Page::FromAddress(code_entry)->IsEvacuationCandidate()) { |
| 2801 | SlotsBuffer::AddTo(&slots_buffer_allocator_, |
| 2802 | &migration_slots_buffer_, |
| 2803 | SlotsBuffer::CODE_ENTRY_SLOT, |
| 2804 | code_entry_slot, |
| 2805 | SlotsBuffer::IGNORE_OVERFLOW); |
| 2806 | } |
| 2807 | } |
| 2808 | } else if (dest == CODE_SPACE) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 2809 | PROFILE(isolate(), CodeMoveEvent(src, dst)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2810 | heap()->MoveBlock(dst, src, size); |
| 2811 | SlotsBuffer::AddTo(&slots_buffer_allocator_, |
| 2812 | &migration_slots_buffer_, |
| 2813 | SlotsBuffer::RELOCATED_CODE_OBJECT, |
| 2814 | dst, |
| 2815 | SlotsBuffer::IGNORE_OVERFLOW); |
| 2816 | Code::cast(HeapObject::FromAddress(dst))->Relocate(dst - src); |
| 2817 | } else { |
| 2818 | ASSERT(dest == OLD_DATA_SPACE || dest == NEW_SPACE); |
| 2819 | heap()->MoveBlock(dst, src, size); |
| 2820 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2821 | Memory::Address_at(src) = dst; |
| 2822 | } |
| 2823 | |
| 2824 | |
| 2825 | // Visitor for updating pointers from live objects in old spaces to new space. |
| 2826 | // It does not expect to encounter pointers to dead objects. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2827 | class PointersUpdatingVisitor: public ObjectVisitor { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2828 | public: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2829 | explicit PointersUpdatingVisitor(Heap* heap) : heap_(heap) { } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2830 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2831 | void VisitPointer(Object** p) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2832 | UpdatePointer(p); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 2835 | void VisitPointers(Object** start, Object** end) { |
| 2836 | for (Object** p = start; p < end; p++) UpdatePointer(p); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 2839 | void VisitEmbeddedPointer(RelocInfo* rinfo) { |
| 2840 | ASSERT(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
| 2841 | Object* target = rinfo->target_object(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2842 | Object* old_target = target; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 2843 | VisitPointer(&target); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2844 | // Avoid unnecessary changes that might unnecessary flush the instruction |
| 2845 | // cache. |
| 2846 | if (target != old_target) { |
| 2847 | rinfo->set_target_object(target); |
| 2848 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | void VisitCodeTarget(RelocInfo* rinfo) { |
| 2852 | ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode())); |
| 2853 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2854 | Object* old_target = target; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2855 | VisitPointer(&target); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2856 | if (target != old_target) { |
| 2857 | rinfo->set_target_address(Code::cast(target)->instruction_start()); |
| 2858 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2861 | void VisitCodeAgeSequence(RelocInfo* rinfo) { |
| 2862 | ASSERT(RelocInfo::IsCodeAgeSequence(rinfo->rmode())); |
| 2863 | Object* stub = rinfo->code_age_stub(); |
| 2864 | ASSERT(stub != NULL); |
| 2865 | VisitPointer(&stub); |
| 2866 | if (stub != rinfo->code_age_stub()) { |
| 2867 | rinfo->set_code_age_stub(Code::cast(stub)); |
| 2868 | } |
| 2869 | } |
| 2870 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2871 | void VisitDebugTarget(RelocInfo* rinfo) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2872 | ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) && |
| 2873 | rinfo->IsPatchedReturnSequence()) || |
| 2874 | (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 2875 | rinfo->IsPatchedDebugBreakSlotSequence())); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2876 | Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address()); |
| 2877 | VisitPointer(&target); |
| 2878 | rinfo->set_call_address(Code::cast(target)->instruction_start()); |
| 2879 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2880 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2881 | static inline void UpdateSlot(Heap* heap, Object** slot) { |
| 2882 | Object* obj = *slot; |
| 2883 | |
| 2884 | if (!obj->IsHeapObject()) return; |
| 2885 | |
| 2886 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 2887 | |
| 2888 | MapWord map_word = heap_obj->map_word(); |
| 2889 | if (map_word.IsForwardingAddress()) { |
| 2890 | ASSERT(heap->InFromSpace(heap_obj) || |
| 2891 | MarkCompactCollector::IsOnEvacuationCandidate(heap_obj)); |
| 2892 | HeapObject* target = map_word.ToForwardingAddress(); |
| 2893 | *slot = target; |
| 2894 | ASSERT(!heap->InFromSpace(target) && |
| 2895 | !MarkCompactCollector::IsOnEvacuationCandidate(target)); |
| 2896 | } |
| 2897 | } |
| 2898 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2899 | private: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2900 | inline void UpdatePointer(Object** p) { |
| 2901 | UpdateSlot(heap_, p); |
| 2902 | } |
| 2903 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2904 | Heap* heap_; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2905 | }; |
| 2906 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2907 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2908 | static void UpdatePointer(HeapObject** p, HeapObject* object) { |
| 2909 | ASSERT(*p == object); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2910 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2911 | Address old_addr = object->address(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2912 | |
| 2913 | Address new_addr = Memory::Address_at(old_addr); |
| 2914 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2915 | // The new space sweep will overwrite the map word of dead objects |
| 2916 | // with NULL. In this case we do not need to transfer this entry to |
| 2917 | // the store buffer which we are rebuilding. |
| 2918 | if (new_addr != NULL) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2919 | *p = HeapObject::FromAddress(new_addr); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2920 | } else { |
| 2921 | // We have to zap this pointer, because the store buffer may overflow later, |
| 2922 | // and then we have to scan the entire heap and we don't want to find |
| 2923 | // spurious newspace pointers in the old space. |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 2924 | // TODO(mstarzinger): This was changed to a sentinel value to track down |
| 2925 | // rare crashes, change it back to Smi::FromInt(0) later. |
| 2926 | *p = reinterpret_cast<HeapObject*>(Smi::FromInt(0x0f100d00 >> 1)); // flood |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2927 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2931 | static String* UpdateReferenceInExternalStringTableEntry(Heap* heap, |
| 2932 | Object** p) { |
| 2933 | MapWord map_word = HeapObject::cast(*p)->map_word(); |
| 2934 | |
| 2935 | if (map_word.IsForwardingAddress()) { |
| 2936 | return String::cast(map_word.ToForwardingAddress()); |
| 2937 | } |
| 2938 | |
| 2939 | return String::cast(*p); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2943 | bool MarkCompactCollector::TryPromoteObject(HeapObject* object, |
| 2944 | int object_size) { |
hpayer@chromium.org | 83fa61b | 2013-07-24 09:36:58 +0000 | [diff] [blame] | 2945 | // TODO(hpayer): Replace that check with an assert. |
| 2946 | CHECK(object_size <= Page::kMaxNonCodeHeapObjectSize); |
| 2947 | |
| 2948 | OldSpace* target_space = heap()->TargetSpace(object); |
| 2949 | |
| 2950 | ASSERT(target_space == heap()->old_pointer_space() || |
| 2951 | target_space == heap()->old_data_space()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2952 | Object* result; |
machenbach@chromium.org | 935a779 | 2013-11-12 09:05:18 +0000 | [diff] [blame] | 2953 | MaybeObject* maybe_result = target_space->AllocateRaw(object_size); |
hpayer@chromium.org | 83fa61b | 2013-07-24 09:36:58 +0000 | [diff] [blame] | 2954 | if (maybe_result->ToObject(&result)) { |
| 2955 | HeapObject* target = HeapObject::cast(result); |
| 2956 | MigrateObject(target->address(), |
| 2957 | object->address(), |
| 2958 | object_size, |
| 2959 | target_space->identity()); |
| 2960 | heap()->mark_compact_collector()->tracer()-> |
| 2961 | increment_promoted_objects_size(object_size); |
| 2962 | return true; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | return false; |
| 2966 | } |
| 2967 | |
| 2968 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2969 | void MarkCompactCollector::EvacuateNewSpace() { |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 2970 | // There are soft limits in the allocation code, designed trigger a mark |
| 2971 | // sweep collection by failing allocations. But since we are already in |
| 2972 | // a mark-sweep allocation, there is no sense in trying to trigger one. |
| 2973 | AlwaysAllocateScope scope; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2974 | heap()->CheckNewSpaceExpansionCriteria(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2975 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2976 | NewSpace* new_space = heap()->new_space(); |
| 2977 | |
| 2978 | // Store allocation range before flipping semispaces. |
| 2979 | Address from_bottom = new_space->bottom(); |
| 2980 | Address from_top = new_space->top(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2981 | |
| 2982 | // Flip the semispaces. After flipping, to space is empty, from space has |
| 2983 | // live objects. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2984 | new_space->Flip(); |
| 2985 | new_space->ResetAllocationInfo(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2986 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2987 | int survivors_size = 0; |
| 2988 | |
| 2989 | // First pass: traverse all objects in inactive semispace, remove marks, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2990 | // migrate live objects and write forwarding addresses. This stage puts |
| 2991 | // new entries in the store buffer and may cause some pages to be marked |
| 2992 | // scan-on-scavenge. |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 2993 | NewSpacePageIterator it(from_bottom, from_top); |
| 2994 | while (it.has_next()) { |
| 2995 | NewSpacePage* p = it.next(); |
| 2996 | survivors_size += DiscoverAndPromoteBlackObjectsOnPage(new_space, p); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2999 | heap_->IncrementYoungSurvivorsCounter(survivors_size); |
| 3000 | new_space->set_age_mark(new_space->top()); |
| 3001 | } |
| 3002 | |
| 3003 | |
| 3004 | void MarkCompactCollector::EvacuateLiveObjectsFromPage(Page* p) { |
| 3005 | AlwaysAllocateScope always_allocate; |
| 3006 | PagedSpace* space = static_cast<PagedSpace*>(p->owner()); |
| 3007 | ASSERT(p->IsEvacuationCandidate() && !p->WasSwept()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3008 | p->MarkSweptPrecisely(); |
| 3009 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3010 | int offsets[16]; |
| 3011 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3012 | for (MarkBitCellIterator it(p); !it.Done(); it.Advance()) { |
| 3013 | Address cell_base = it.CurrentCellBase(); |
| 3014 | MarkBit::CellType* cell = it.CurrentCell(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3015 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3016 | if (*cell == 0) continue; |
| 3017 | |
| 3018 | int live_objects = MarkWordToObjectStarts(*cell, offsets); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3019 | for (int i = 0; i < live_objects; i++) { |
| 3020 | Address object_addr = cell_base + offsets[i] * kPointerSize; |
| 3021 | HeapObject* object = HeapObject::FromAddress(object_addr); |
| 3022 | ASSERT(Marking::IsBlack(Marking::MarkBitFrom(object))); |
| 3023 | |
| 3024 | int size = object->Size(); |
| 3025 | |
machenbach@chromium.org | 935a779 | 2013-11-12 09:05:18 +0000 | [diff] [blame] | 3026 | MaybeObject* target = space->AllocateRaw(size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3027 | if (target->IsFailure()) { |
| 3028 | // OS refused to give us memory. |
| 3029 | V8::FatalProcessOutOfMemory("Evacuation"); |
| 3030 | return; |
| 3031 | } |
| 3032 | |
| 3033 | Object* target_object = target->ToObjectUnchecked(); |
| 3034 | |
| 3035 | MigrateObject(HeapObject::cast(target_object)->address(), |
| 3036 | object_addr, |
| 3037 | size, |
| 3038 | space->identity()); |
| 3039 | ASSERT(object->map_word().IsForwardingAddress()); |
| 3040 | } |
| 3041 | |
| 3042 | // Clear marking bits for current cell. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3043 | *cell = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3044 | } |
| 3045 | p->ResetLiveBytes(); |
| 3046 | } |
| 3047 | |
| 3048 | |
| 3049 | void MarkCompactCollector::EvacuatePages() { |
| 3050 | int npages = evacuation_candidates_.length(); |
| 3051 | for (int i = 0; i < npages; i++) { |
| 3052 | Page* p = evacuation_candidates_[i]; |
| 3053 | ASSERT(p->IsEvacuationCandidate() || |
| 3054 | p->IsFlagSet(Page::RESCAN_ON_EVACUATION)); |
| 3055 | if (p->IsEvacuationCandidate()) { |
| 3056 | // During compaction we might have to request a new page. |
| 3057 | // Check that space still have room for that. |
| 3058 | if (static_cast<PagedSpace*>(p->owner())->CanExpand()) { |
| 3059 | EvacuateLiveObjectsFromPage(p); |
| 3060 | } else { |
| 3061 | // Without room for expansion evacuation is not guaranteed to succeed. |
| 3062 | // Pessimistically abandon unevacuated pages. |
| 3063 | for (int j = i; j < npages; j++) { |
| 3064 | Page* page = evacuation_candidates_[j]; |
| 3065 | slots_buffer_allocator_.DeallocateChain(page->slots_buffer_address()); |
| 3066 | page->ClearEvacuationCandidate(); |
| 3067 | page->SetFlag(Page::RESCAN_ON_EVACUATION); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 3068 | page->InsertAfter(static_cast<PagedSpace*>(page->owner())->anchor()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3069 | } |
| 3070 | return; |
| 3071 | } |
| 3072 | } |
| 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | |
| 3077 | class EvacuationWeakObjectRetainer : public WeakObjectRetainer { |
| 3078 | public: |
| 3079 | virtual Object* RetainAs(Object* object) { |
| 3080 | if (object->IsHeapObject()) { |
| 3081 | HeapObject* heap_object = HeapObject::cast(object); |
| 3082 | MapWord map_word = heap_object->map_word(); |
| 3083 | if (map_word.IsForwardingAddress()) { |
| 3084 | return map_word.ToForwardingAddress(); |
| 3085 | } |
| 3086 | } |
| 3087 | return object; |
| 3088 | } |
| 3089 | }; |
| 3090 | |
| 3091 | |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 3092 | static inline void UpdateSlot(Isolate* isolate, |
| 3093 | ObjectVisitor* v, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3094 | SlotsBuffer::SlotType slot_type, |
| 3095 | Address addr) { |
| 3096 | switch (slot_type) { |
| 3097 | case SlotsBuffer::CODE_TARGET_SLOT: { |
| 3098 | RelocInfo rinfo(addr, RelocInfo::CODE_TARGET, 0, NULL); |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 3099 | rinfo.Visit(isolate, v); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3100 | break; |
| 3101 | } |
| 3102 | case SlotsBuffer::CODE_ENTRY_SLOT: { |
| 3103 | v->VisitCodeEntry(addr); |
| 3104 | break; |
| 3105 | } |
| 3106 | case SlotsBuffer::RELOCATED_CODE_OBJECT: { |
| 3107 | HeapObject* obj = HeapObject::FromAddress(addr); |
| 3108 | Code::cast(obj)->CodeIterateBody(v); |
| 3109 | break; |
| 3110 | } |
| 3111 | case SlotsBuffer::DEBUG_TARGET_SLOT: { |
| 3112 | RelocInfo rinfo(addr, RelocInfo::DEBUG_BREAK_SLOT, 0, NULL); |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 3113 | if (rinfo.IsPatchedDebugBreakSlotSequence()) rinfo.Visit(isolate, v); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3114 | break; |
| 3115 | } |
| 3116 | case SlotsBuffer::JS_RETURN_SLOT: { |
| 3117 | RelocInfo rinfo(addr, RelocInfo::JS_RETURN, 0, NULL); |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 3118 | if (rinfo.IsPatchedReturnSequence()) rinfo.Visit(isolate, v); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3119 | break; |
| 3120 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3121 | case SlotsBuffer::EMBEDDED_OBJECT_SLOT: { |
| 3122 | RelocInfo rinfo(addr, RelocInfo::EMBEDDED_OBJECT, 0, NULL); |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 3123 | rinfo.Visit(isolate, v); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3124 | break; |
| 3125 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3126 | default: |
| 3127 | UNREACHABLE(); |
| 3128 | break; |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | |
| 3133 | enum SweepingMode { |
| 3134 | SWEEP_ONLY, |
| 3135 | SWEEP_AND_VISIT_LIVE_OBJECTS |
| 3136 | }; |
| 3137 | |
| 3138 | |
| 3139 | enum SkipListRebuildingMode { |
| 3140 | REBUILD_SKIP_LIST, |
| 3141 | IGNORE_SKIP_LIST |
| 3142 | }; |
| 3143 | |
| 3144 | |
| 3145 | // Sweep a space precisely. After this has been done the space can |
| 3146 | // be iterated precisely, hitting only the live objects. Code space |
| 3147 | // is always swept precisely because we want to be able to iterate |
| 3148 | // over it. Map space is swept precisely, because it is not compacted. |
| 3149 | // Slots in live objects pointing into evacuation candidates are updated |
| 3150 | // if requested. |
| 3151 | template<SweepingMode sweeping_mode, SkipListRebuildingMode skip_list_mode> |
| 3152 | static void SweepPrecisely(PagedSpace* space, |
| 3153 | Page* p, |
| 3154 | ObjectVisitor* v) { |
| 3155 | ASSERT(!p->IsEvacuationCandidate() && !p->WasSwept()); |
| 3156 | ASSERT_EQ(skip_list_mode == REBUILD_SKIP_LIST, |
| 3157 | space->identity() == CODE_SPACE); |
| 3158 | ASSERT((p->skip_list() == NULL) || (skip_list_mode == REBUILD_SKIP_LIST)); |
| 3159 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3160 | double start_time = 0.0; |
| 3161 | if (FLAG_print_cumulative_gc_stat) { |
| 3162 | start_time = OS::TimeCurrentMillis(); |
| 3163 | } |
| 3164 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3165 | p->MarkSweptPrecisely(); |
| 3166 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 3167 | Address free_start = p->area_start(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3168 | ASSERT(reinterpret_cast<intptr_t>(free_start) % (32 * kPointerSize) == 0); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3169 | int offsets[16]; |
| 3170 | |
| 3171 | SkipList* skip_list = p->skip_list(); |
| 3172 | int curr_region = -1; |
| 3173 | if ((skip_list_mode == REBUILD_SKIP_LIST) && skip_list) { |
| 3174 | skip_list->Clear(); |
| 3175 | } |
| 3176 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3177 | for (MarkBitCellIterator it(p); !it.Done(); it.Advance()) { |
| 3178 | Address cell_base = it.CurrentCellBase(); |
| 3179 | MarkBit::CellType* cell = it.CurrentCell(); |
| 3180 | int live_objects = MarkWordToObjectStarts(*cell, offsets); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3181 | int live_index = 0; |
| 3182 | for ( ; live_objects != 0; live_objects--) { |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3183 | Address free_end = cell_base + offsets[live_index++] * kPointerSize; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3184 | if (free_end != free_start) { |
| 3185 | space->Free(free_start, static_cast<int>(free_end - free_start)); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 3186 | #ifdef ENABLE_GDB_JIT_INTERFACE |
| 3187 | if (FLAG_gdbjit && space->identity() == CODE_SPACE) { |
| 3188 | GDBJITInterface::RemoveCodeRange(free_start, free_end); |
| 3189 | } |
| 3190 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3191 | } |
| 3192 | HeapObject* live_object = HeapObject::FromAddress(free_end); |
| 3193 | ASSERT(Marking::IsBlack(Marking::MarkBitFrom(live_object))); |
| 3194 | Map* map = live_object->map(); |
| 3195 | int size = live_object->SizeFromMap(map); |
| 3196 | if (sweeping_mode == SWEEP_AND_VISIT_LIVE_OBJECTS) { |
| 3197 | live_object->IterateBody(map->instance_type(), size, v); |
| 3198 | } |
| 3199 | if ((skip_list_mode == REBUILD_SKIP_LIST) && skip_list != NULL) { |
| 3200 | int new_region_start = |
| 3201 | SkipList::RegionNumber(free_end); |
| 3202 | int new_region_end = |
| 3203 | SkipList::RegionNumber(free_end + size - kPointerSize); |
| 3204 | if (new_region_start != curr_region || |
| 3205 | new_region_end != curr_region) { |
| 3206 | skip_list->AddObject(free_end, size); |
| 3207 | curr_region = new_region_end; |
| 3208 | } |
| 3209 | } |
| 3210 | free_start = free_end + size; |
| 3211 | } |
| 3212 | // Clear marking bits for current cell. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3213 | *cell = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3214 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 3215 | if (free_start != p->area_end()) { |
| 3216 | space->Free(free_start, static_cast<int>(p->area_end() - free_start)); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 3217 | #ifdef ENABLE_GDB_JIT_INTERFACE |
| 3218 | if (FLAG_gdbjit && space->identity() == CODE_SPACE) { |
| 3219 | GDBJITInterface::RemoveCodeRange(free_start, p->area_end()); |
| 3220 | } |
| 3221 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3222 | } |
| 3223 | p->ResetLiveBytes(); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3224 | if (FLAG_print_cumulative_gc_stat) { |
| 3225 | space->heap()->AddSweepingTime(OS::TimeCurrentMillis() - start_time); |
| 3226 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | |
| 3230 | static bool SetMarkBitsUnderInvalidatedCode(Code* code, bool value) { |
| 3231 | Page* p = Page::FromAddress(code->address()); |
| 3232 | |
| 3233 | if (p->IsEvacuationCandidate() || |
| 3234 | p->IsFlagSet(Page::RESCAN_ON_EVACUATION)) { |
| 3235 | return false; |
| 3236 | } |
| 3237 | |
| 3238 | Address code_start = code->address(); |
| 3239 | Address code_end = code_start + code->Size(); |
| 3240 | |
| 3241 | uint32_t start_index = MemoryChunk::FastAddressToMarkbitIndex(code_start); |
| 3242 | uint32_t end_index = |
| 3243 | MemoryChunk::FastAddressToMarkbitIndex(code_end - kPointerSize); |
| 3244 | |
| 3245 | Bitmap* b = p->markbits(); |
| 3246 | |
| 3247 | MarkBit start_mark_bit = b->MarkBitFromIndex(start_index); |
| 3248 | MarkBit end_mark_bit = b->MarkBitFromIndex(end_index); |
| 3249 | |
| 3250 | MarkBit::CellType* start_cell = start_mark_bit.cell(); |
| 3251 | MarkBit::CellType* end_cell = end_mark_bit.cell(); |
| 3252 | |
| 3253 | if (value) { |
| 3254 | MarkBit::CellType start_mask = ~(start_mark_bit.mask() - 1); |
| 3255 | MarkBit::CellType end_mask = (end_mark_bit.mask() << 1) - 1; |
| 3256 | |
| 3257 | if (start_cell == end_cell) { |
| 3258 | *start_cell |= start_mask & end_mask; |
| 3259 | } else { |
| 3260 | *start_cell |= start_mask; |
| 3261 | for (MarkBit::CellType* cell = start_cell + 1; cell < end_cell; cell++) { |
| 3262 | *cell = ~0; |
| 3263 | } |
| 3264 | *end_cell |= end_mask; |
| 3265 | } |
| 3266 | } else { |
| 3267 | for (MarkBit::CellType* cell = start_cell ; cell <= end_cell; cell++) { |
| 3268 | *cell = 0; |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | return true; |
| 3273 | } |
| 3274 | |
| 3275 | |
| 3276 | static bool IsOnInvalidatedCodeObject(Address addr) { |
| 3277 | // We did not record any slots in large objects thus |
| 3278 | // we can safely go to the page from the slot address. |
| 3279 | Page* p = Page::FromAddress(addr); |
| 3280 | |
| 3281 | // First check owner's identity because old pointer and old data spaces |
| 3282 | // are swept lazily and might still have non-zero mark-bits on some |
| 3283 | // pages. |
| 3284 | if (p->owner()->identity() != CODE_SPACE) return false; |
| 3285 | |
| 3286 | // In code space only bits on evacuation candidates (but we don't record |
| 3287 | // any slots on them) and under invalidated code objects are non-zero. |
| 3288 | MarkBit mark_bit = |
| 3289 | p->markbits()->MarkBitFromIndex(Page::FastAddressToMarkbitIndex(addr)); |
| 3290 | |
| 3291 | return mark_bit.Get(); |
| 3292 | } |
| 3293 | |
| 3294 | |
| 3295 | void MarkCompactCollector::InvalidateCode(Code* code) { |
| 3296 | if (heap_->incremental_marking()->IsCompacting() && |
| 3297 | !ShouldSkipEvacuationSlotRecording(code)) { |
| 3298 | ASSERT(compacting_); |
| 3299 | |
| 3300 | // If the object is white than no slots were recorded on it yet. |
| 3301 | MarkBit mark_bit = Marking::MarkBitFrom(code); |
| 3302 | if (Marking::IsWhite(mark_bit)) return; |
| 3303 | |
| 3304 | invalidated_code_.Add(code); |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 3309 | // Return true if the given code is deoptimized or will be deoptimized. |
| 3310 | bool MarkCompactCollector::WillBeDeoptimized(Code* code) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3311 | return code->marked_for_deoptimization(); |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3315 | bool MarkCompactCollector::MarkInvalidatedCode() { |
| 3316 | bool code_marked = false; |
| 3317 | |
| 3318 | int length = invalidated_code_.length(); |
| 3319 | for (int i = 0; i < length; i++) { |
| 3320 | Code* code = invalidated_code_[i]; |
| 3321 | |
| 3322 | if (SetMarkBitsUnderInvalidatedCode(code, true)) { |
| 3323 | code_marked = true; |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | return code_marked; |
| 3328 | } |
| 3329 | |
| 3330 | |
| 3331 | void MarkCompactCollector::RemoveDeadInvalidatedCode() { |
| 3332 | int length = invalidated_code_.length(); |
| 3333 | for (int i = 0; i < length; i++) { |
| 3334 | if (!IsMarked(invalidated_code_[i])) invalidated_code_[i] = NULL; |
| 3335 | } |
| 3336 | } |
| 3337 | |
| 3338 | |
| 3339 | void MarkCompactCollector::ProcessInvalidatedCode(ObjectVisitor* visitor) { |
| 3340 | int length = invalidated_code_.length(); |
| 3341 | for (int i = 0; i < length; i++) { |
| 3342 | Code* code = invalidated_code_[i]; |
| 3343 | if (code != NULL) { |
| 3344 | code->Iterate(visitor); |
| 3345 | SetMarkBitsUnderInvalidatedCode(code, false); |
| 3346 | } |
| 3347 | } |
| 3348 | invalidated_code_.Rewind(0); |
| 3349 | } |
| 3350 | |
| 3351 | |
| 3352 | void MarkCompactCollector::EvacuateNewSpaceAndCandidates() { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3353 | Heap::RelocationLock relocation_lock(heap()); |
| 3354 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3355 | bool code_slots_filtering_required; |
| 3356 | { GCTracer::Scope gc_scope(tracer_, GCTracer::Scope::MC_SWEEP_NEWSPACE); |
| 3357 | code_slots_filtering_required = MarkInvalidatedCode(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3358 | EvacuateNewSpace(); |
| 3359 | } |
| 3360 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3361 | { GCTracer::Scope gc_scope(tracer_, GCTracer::Scope::MC_EVACUATE_PAGES); |
| 3362 | EvacuatePages(); |
| 3363 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3364 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3365 | // Second pass: find pointers to new space and update them. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3366 | PointersUpdatingVisitor updating_visitor(heap()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3367 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3368 | { GCTracer::Scope gc_scope(tracer_, |
| 3369 | GCTracer::Scope::MC_UPDATE_NEW_TO_NEW_POINTERS); |
| 3370 | // Update pointers in to space. |
| 3371 | SemiSpaceIterator to_it(heap()->new_space()->bottom(), |
| 3372 | heap()->new_space()->top()); |
| 3373 | for (HeapObject* object = to_it.Next(); |
| 3374 | object != NULL; |
| 3375 | object = to_it.Next()) { |
| 3376 | Map* map = object->map(); |
| 3377 | object->IterateBody(map->instance_type(), |
| 3378 | object->SizeFromMap(map), |
| 3379 | &updating_visitor); |
| 3380 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3381 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3382 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3383 | { GCTracer::Scope gc_scope(tracer_, |
| 3384 | GCTracer::Scope::MC_UPDATE_ROOT_TO_NEW_POINTERS); |
| 3385 | // Update roots. |
| 3386 | heap_->IterateRoots(&updating_visitor, VISIT_ALL_IN_SWEEP_NEWSPACE); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3387 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3388 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3389 | { GCTracer::Scope gc_scope(tracer_, |
| 3390 | GCTracer::Scope::MC_UPDATE_OLD_TO_NEW_POINTERS); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3391 | StoreBufferRebuildScope scope(heap_, |
| 3392 | heap_->store_buffer(), |
| 3393 | &Heap::ScavengeStoreBufferCallback); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 3394 | heap_->store_buffer()->IteratePointersToNewSpaceAndClearMaps( |
| 3395 | &UpdatePointer); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3396 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3397 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3398 | { GCTracer::Scope gc_scope(tracer_, |
| 3399 | GCTracer::Scope::MC_UPDATE_POINTERS_TO_EVACUATED); |
| 3400 | SlotsBuffer::UpdateSlotsRecordedIn(heap_, |
| 3401 | migration_slots_buffer_, |
| 3402 | code_slots_filtering_required); |
| 3403 | if (FLAG_trace_fragmentation) { |
| 3404 | PrintF(" migration slots buffer: %d\n", |
| 3405 | SlotsBuffer::SizeOfChain(migration_slots_buffer_)); |
| 3406 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3407 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3408 | if (compacting_ && was_marked_incrementally_) { |
| 3409 | // It's difficult to filter out slots recorded for large objects. |
| 3410 | LargeObjectIterator it(heap_->lo_space()); |
| 3411 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
| 3412 | // LargeObjectSpace is not swept yet thus we have to skip |
| 3413 | // dead objects explicitly. |
| 3414 | if (!IsMarked(obj)) continue; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3415 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3416 | Page* p = Page::FromAddress(obj->address()); |
| 3417 | if (p->IsFlagSet(Page::RESCAN_ON_EVACUATION)) { |
| 3418 | obj->Iterate(&updating_visitor); |
| 3419 | p->ClearFlag(Page::RESCAN_ON_EVACUATION); |
| 3420 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | int npages = evacuation_candidates_.length(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3426 | { GCTracer::Scope gc_scope( |
| 3427 | tracer_, GCTracer::Scope::MC_UPDATE_POINTERS_BETWEEN_EVACUATED); |
| 3428 | for (int i = 0; i < npages; i++) { |
| 3429 | Page* p = evacuation_candidates_[i]; |
| 3430 | ASSERT(p->IsEvacuationCandidate() || |
| 3431 | p->IsFlagSet(Page::RESCAN_ON_EVACUATION)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3432 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3433 | if (p->IsEvacuationCandidate()) { |
| 3434 | SlotsBuffer::UpdateSlotsRecordedIn(heap_, |
| 3435 | p->slots_buffer(), |
| 3436 | code_slots_filtering_required); |
| 3437 | if (FLAG_trace_fragmentation) { |
| 3438 | PrintF(" page %p slots buffer: %d\n", |
| 3439 | reinterpret_cast<void*>(p), |
| 3440 | SlotsBuffer::SizeOfChain(p->slots_buffer())); |
| 3441 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3442 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3443 | // Important: skip list should be cleared only after roots were updated |
| 3444 | // because root iteration traverses the stack and might have to find |
| 3445 | // code objects from non-updated pc pointing into evacuation candidate. |
| 3446 | SkipList* list = p->skip_list(); |
| 3447 | if (list != NULL) list->Clear(); |
| 3448 | } else { |
| 3449 | if (FLAG_gc_verbose) { |
| 3450 | PrintF("Sweeping 0x%" V8PRIxPTR " during evacuation.\n", |
| 3451 | reinterpret_cast<intptr_t>(p)); |
| 3452 | } |
| 3453 | PagedSpace* space = static_cast<PagedSpace*>(p->owner()); |
| 3454 | p->ClearFlag(MemoryChunk::RESCAN_ON_EVACUATION); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3455 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3456 | switch (space->identity()) { |
| 3457 | case OLD_DATA_SPACE: |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3458 | SweepConservatively<SWEEP_SEQUENTIALLY>(space, NULL, p); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3459 | break; |
| 3460 | case OLD_POINTER_SPACE: |
| 3461 | SweepPrecisely<SWEEP_AND_VISIT_LIVE_OBJECTS, IGNORE_SKIP_LIST>( |
| 3462 | space, p, &updating_visitor); |
| 3463 | break; |
| 3464 | case CODE_SPACE: |
| 3465 | SweepPrecisely<SWEEP_AND_VISIT_LIVE_OBJECTS, REBUILD_SKIP_LIST>( |
| 3466 | space, p, &updating_visitor); |
| 3467 | break; |
| 3468 | default: |
| 3469 | UNREACHABLE(); |
| 3470 | break; |
| 3471 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3472 | } |
| 3473 | } |
| 3474 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3475 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3476 | GCTracer::Scope gc_scope(tracer_, GCTracer::Scope::MC_UPDATE_MISC_POINTERS); |
| 3477 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3478 | // Update pointers from cells. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3479 | HeapObjectIterator cell_iterator(heap_->cell_space()); |
| 3480 | for (HeapObject* cell = cell_iterator.Next(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3481 | cell != NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3482 | cell = cell_iterator.Next()) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 3483 | if (cell->IsCell()) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3484 | Cell::BodyDescriptor::IterateBody(cell, &updating_visitor); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | HeapObjectIterator js_global_property_cell_iterator( |
| 3489 | heap_->property_cell_space()); |
| 3490 | for (HeapObject* cell = js_global_property_cell_iterator.Next(); |
| 3491 | cell != NULL; |
| 3492 | cell = js_global_property_cell_iterator.Next()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 3493 | if (cell->IsPropertyCell()) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3494 | PropertyCell::BodyDescriptor::IterateBody(cell, &updating_visitor); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3495 | } |
| 3496 | } |
| 3497 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3498 | // Update the head of the native contexts list in the heap. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 3499 | updating_visitor.VisitPointer(heap_->native_contexts_list_address()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3500 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3501 | heap_->string_table()->Iterate(&updating_visitor); |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 3502 | updating_visitor.VisitPointer(heap_->weak_object_to_code_table_address()); |
| 3503 | if (heap_->weak_object_to_code_table()->IsHashTable()) { |
| 3504 | WeakHashTable* table = |
| 3505 | WeakHashTable::cast(heap_->weak_object_to_code_table()); |
| 3506 | table->Iterate(&updating_visitor); |
| 3507 | table->Rehash(heap_->undefined_value()); |
| 3508 | } |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 3509 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 3510 | // Update pointers from external string table. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3511 | heap_->UpdateReferencesInExternalStringTable( |
| 3512 | &UpdateReferenceInExternalStringTableEntry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3513 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 3514 | if (!FLAG_watch_ic_patching) { |
| 3515 | // Update JSFunction pointers from the runtime profiler. |
| 3516 | heap()->isolate()->runtime_profiler()->UpdateSamplesAfterCompact( |
| 3517 | &updating_visitor); |
| 3518 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3519 | |
| 3520 | EvacuationWeakObjectRetainer evacuation_object_retainer; |
| 3521 | heap()->ProcessWeakReferences(&evacuation_object_retainer); |
| 3522 | |
| 3523 | // Visit invalidated code (we ignored all slots on it) and clear mark-bits |
| 3524 | // under it. |
| 3525 | ProcessInvalidatedCode(&updating_visitor); |
| 3526 | |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 3527 | heap_->isolate()->inner_pointer_to_code_cache()->Flush(); |
| 3528 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3529 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3530 | if (FLAG_verify_heap) { |
| 3531 | VerifyEvacuation(heap_); |
| 3532 | } |
| 3533 | #endif |
| 3534 | |
| 3535 | slots_buffer_allocator_.DeallocateChain(&migration_slots_buffer_); |
| 3536 | ASSERT(migration_slots_buffer_ == NULL); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 3540 | void MarkCompactCollector::UnlinkEvacuationCandidates() { |
| 3541 | int npages = evacuation_candidates_.length(); |
| 3542 | for (int i = 0; i < npages; i++) { |
| 3543 | Page* p = evacuation_candidates_[i]; |
| 3544 | if (!p->IsEvacuationCandidate()) continue; |
| 3545 | p->Unlink(); |
| 3546 | p->ClearSweptPrecisely(); |
| 3547 | p->ClearSweptConservatively(); |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 3552 | void MarkCompactCollector::ReleaseEvacuationCandidates() { |
| 3553 | int npages = evacuation_candidates_.length(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3554 | for (int i = 0; i < npages; i++) { |
| 3555 | Page* p = evacuation_candidates_[i]; |
| 3556 | if (!p->IsEvacuationCandidate()) continue; |
| 3557 | PagedSpace* space = static_cast<PagedSpace*>(p->owner()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 3558 | space->Free(p->area_start(), p->area_size()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3559 | p->set_scan_on_scavenge(false); |
| 3560 | slots_buffer_allocator_.DeallocateChain(p->slots_buffer_address()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 3561 | p->ResetLiveBytes(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 3562 | space->ReleasePage(p, false); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3563 | } |
| 3564 | evacuation_candidates_.Rewind(0); |
| 3565 | compacting_ = false; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 3566 | heap()->FreeQueuedChunks(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3570 | static const int kStartTableEntriesPerLine = 5; |
| 3571 | static const int kStartTableLines = 171; |
| 3572 | static const int kStartTableInvalidLine = 127; |
| 3573 | static const int kStartTableUnusedEntry = 126; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3574 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3575 | #define _ kStartTableUnusedEntry |
| 3576 | #define X kStartTableInvalidLine |
| 3577 | // Mark-bit to object start offset table. |
| 3578 | // |
| 3579 | // The line is indexed by the mark bits in a byte. The first number on |
| 3580 | // the line describes the number of live object starts for the line and the |
| 3581 | // other numbers on the line describe the offsets (in words) of the object |
| 3582 | // starts. |
| 3583 | // |
| 3584 | // Since objects are at least 2 words large we don't have entries for two |
| 3585 | // consecutive 1 bits. All entries after 170 have at least 2 consecutive bits. |
| 3586 | char kStartTable[kStartTableLines * kStartTableEntriesPerLine] = { |
| 3587 | 0, _, _, _, _, // 0 |
| 3588 | 1, 0, _, _, _, // 1 |
| 3589 | 1, 1, _, _, _, // 2 |
| 3590 | X, _, _, _, _, // 3 |
| 3591 | 1, 2, _, _, _, // 4 |
| 3592 | 2, 0, 2, _, _, // 5 |
| 3593 | X, _, _, _, _, // 6 |
| 3594 | X, _, _, _, _, // 7 |
| 3595 | 1, 3, _, _, _, // 8 |
| 3596 | 2, 0, 3, _, _, // 9 |
| 3597 | 2, 1, 3, _, _, // 10 |
| 3598 | X, _, _, _, _, // 11 |
| 3599 | X, _, _, _, _, // 12 |
| 3600 | X, _, _, _, _, // 13 |
| 3601 | X, _, _, _, _, // 14 |
| 3602 | X, _, _, _, _, // 15 |
| 3603 | 1, 4, _, _, _, // 16 |
| 3604 | 2, 0, 4, _, _, // 17 |
| 3605 | 2, 1, 4, _, _, // 18 |
| 3606 | X, _, _, _, _, // 19 |
| 3607 | 2, 2, 4, _, _, // 20 |
| 3608 | 3, 0, 2, 4, _, // 21 |
| 3609 | X, _, _, _, _, // 22 |
| 3610 | X, _, _, _, _, // 23 |
| 3611 | X, _, _, _, _, // 24 |
| 3612 | X, _, _, _, _, // 25 |
| 3613 | X, _, _, _, _, // 26 |
| 3614 | X, _, _, _, _, // 27 |
| 3615 | X, _, _, _, _, // 28 |
| 3616 | X, _, _, _, _, // 29 |
| 3617 | X, _, _, _, _, // 30 |
| 3618 | X, _, _, _, _, // 31 |
| 3619 | 1, 5, _, _, _, // 32 |
| 3620 | 2, 0, 5, _, _, // 33 |
| 3621 | 2, 1, 5, _, _, // 34 |
| 3622 | X, _, _, _, _, // 35 |
| 3623 | 2, 2, 5, _, _, // 36 |
| 3624 | 3, 0, 2, 5, _, // 37 |
| 3625 | X, _, _, _, _, // 38 |
| 3626 | X, _, _, _, _, // 39 |
| 3627 | 2, 3, 5, _, _, // 40 |
| 3628 | 3, 0, 3, 5, _, // 41 |
| 3629 | 3, 1, 3, 5, _, // 42 |
| 3630 | X, _, _, _, _, // 43 |
| 3631 | X, _, _, _, _, // 44 |
| 3632 | X, _, _, _, _, // 45 |
| 3633 | X, _, _, _, _, // 46 |
| 3634 | X, _, _, _, _, // 47 |
| 3635 | X, _, _, _, _, // 48 |
| 3636 | X, _, _, _, _, // 49 |
| 3637 | X, _, _, _, _, // 50 |
| 3638 | X, _, _, _, _, // 51 |
| 3639 | X, _, _, _, _, // 52 |
| 3640 | X, _, _, _, _, // 53 |
| 3641 | X, _, _, _, _, // 54 |
| 3642 | X, _, _, _, _, // 55 |
| 3643 | X, _, _, _, _, // 56 |
| 3644 | X, _, _, _, _, // 57 |
| 3645 | X, _, _, _, _, // 58 |
| 3646 | X, _, _, _, _, // 59 |
| 3647 | X, _, _, _, _, // 60 |
| 3648 | X, _, _, _, _, // 61 |
| 3649 | X, _, _, _, _, // 62 |
| 3650 | X, _, _, _, _, // 63 |
| 3651 | 1, 6, _, _, _, // 64 |
| 3652 | 2, 0, 6, _, _, // 65 |
| 3653 | 2, 1, 6, _, _, // 66 |
| 3654 | X, _, _, _, _, // 67 |
| 3655 | 2, 2, 6, _, _, // 68 |
| 3656 | 3, 0, 2, 6, _, // 69 |
| 3657 | X, _, _, _, _, // 70 |
| 3658 | X, _, _, _, _, // 71 |
| 3659 | 2, 3, 6, _, _, // 72 |
| 3660 | 3, 0, 3, 6, _, // 73 |
| 3661 | 3, 1, 3, 6, _, // 74 |
| 3662 | X, _, _, _, _, // 75 |
| 3663 | X, _, _, _, _, // 76 |
| 3664 | X, _, _, _, _, // 77 |
| 3665 | X, _, _, _, _, // 78 |
| 3666 | X, _, _, _, _, // 79 |
| 3667 | 2, 4, 6, _, _, // 80 |
| 3668 | 3, 0, 4, 6, _, // 81 |
| 3669 | 3, 1, 4, 6, _, // 82 |
| 3670 | X, _, _, _, _, // 83 |
| 3671 | 3, 2, 4, 6, _, // 84 |
| 3672 | 4, 0, 2, 4, 6, // 85 |
| 3673 | X, _, _, _, _, // 86 |
| 3674 | X, _, _, _, _, // 87 |
| 3675 | X, _, _, _, _, // 88 |
| 3676 | X, _, _, _, _, // 89 |
| 3677 | X, _, _, _, _, // 90 |
| 3678 | X, _, _, _, _, // 91 |
| 3679 | X, _, _, _, _, // 92 |
| 3680 | X, _, _, _, _, // 93 |
| 3681 | X, _, _, _, _, // 94 |
| 3682 | X, _, _, _, _, // 95 |
| 3683 | X, _, _, _, _, // 96 |
| 3684 | X, _, _, _, _, // 97 |
| 3685 | X, _, _, _, _, // 98 |
| 3686 | X, _, _, _, _, // 99 |
| 3687 | X, _, _, _, _, // 100 |
| 3688 | X, _, _, _, _, // 101 |
| 3689 | X, _, _, _, _, // 102 |
| 3690 | X, _, _, _, _, // 103 |
| 3691 | X, _, _, _, _, // 104 |
| 3692 | X, _, _, _, _, // 105 |
| 3693 | X, _, _, _, _, // 106 |
| 3694 | X, _, _, _, _, // 107 |
| 3695 | X, _, _, _, _, // 108 |
| 3696 | X, _, _, _, _, // 109 |
| 3697 | X, _, _, _, _, // 110 |
| 3698 | X, _, _, _, _, // 111 |
| 3699 | X, _, _, _, _, // 112 |
| 3700 | X, _, _, _, _, // 113 |
| 3701 | X, _, _, _, _, // 114 |
| 3702 | X, _, _, _, _, // 115 |
| 3703 | X, _, _, _, _, // 116 |
| 3704 | X, _, _, _, _, // 117 |
| 3705 | X, _, _, _, _, // 118 |
| 3706 | X, _, _, _, _, // 119 |
| 3707 | X, _, _, _, _, // 120 |
| 3708 | X, _, _, _, _, // 121 |
| 3709 | X, _, _, _, _, // 122 |
| 3710 | X, _, _, _, _, // 123 |
| 3711 | X, _, _, _, _, // 124 |
| 3712 | X, _, _, _, _, // 125 |
| 3713 | X, _, _, _, _, // 126 |
| 3714 | X, _, _, _, _, // 127 |
| 3715 | 1, 7, _, _, _, // 128 |
| 3716 | 2, 0, 7, _, _, // 129 |
| 3717 | 2, 1, 7, _, _, // 130 |
| 3718 | X, _, _, _, _, // 131 |
| 3719 | 2, 2, 7, _, _, // 132 |
| 3720 | 3, 0, 2, 7, _, // 133 |
| 3721 | X, _, _, _, _, // 134 |
| 3722 | X, _, _, _, _, // 135 |
| 3723 | 2, 3, 7, _, _, // 136 |
| 3724 | 3, 0, 3, 7, _, // 137 |
| 3725 | 3, 1, 3, 7, _, // 138 |
| 3726 | X, _, _, _, _, // 139 |
| 3727 | X, _, _, _, _, // 140 |
| 3728 | X, _, _, _, _, // 141 |
| 3729 | X, _, _, _, _, // 142 |
| 3730 | X, _, _, _, _, // 143 |
| 3731 | 2, 4, 7, _, _, // 144 |
| 3732 | 3, 0, 4, 7, _, // 145 |
| 3733 | 3, 1, 4, 7, _, // 146 |
| 3734 | X, _, _, _, _, // 147 |
| 3735 | 3, 2, 4, 7, _, // 148 |
| 3736 | 4, 0, 2, 4, 7, // 149 |
| 3737 | X, _, _, _, _, // 150 |
| 3738 | X, _, _, _, _, // 151 |
| 3739 | X, _, _, _, _, // 152 |
| 3740 | X, _, _, _, _, // 153 |
| 3741 | X, _, _, _, _, // 154 |
| 3742 | X, _, _, _, _, // 155 |
| 3743 | X, _, _, _, _, // 156 |
| 3744 | X, _, _, _, _, // 157 |
| 3745 | X, _, _, _, _, // 158 |
| 3746 | X, _, _, _, _, // 159 |
| 3747 | 2, 5, 7, _, _, // 160 |
| 3748 | 3, 0, 5, 7, _, // 161 |
| 3749 | 3, 1, 5, 7, _, // 162 |
| 3750 | X, _, _, _, _, // 163 |
| 3751 | 3, 2, 5, 7, _, // 164 |
| 3752 | 4, 0, 2, 5, 7, // 165 |
| 3753 | X, _, _, _, _, // 166 |
| 3754 | X, _, _, _, _, // 167 |
| 3755 | 3, 3, 5, 7, _, // 168 |
| 3756 | 4, 0, 3, 5, 7, // 169 |
| 3757 | 4, 1, 3, 5, 7 // 170 |
| 3758 | }; |
| 3759 | #undef _ |
| 3760 | #undef X |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3761 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3762 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3763 | // Takes a word of mark bits. Returns the number of objects that start in the |
| 3764 | // range. Puts the offsets of the words in the supplied array. |
| 3765 | static inline int MarkWordToObjectStarts(uint32_t mark_bits, int* starts) { |
| 3766 | int objects = 0; |
| 3767 | int offset = 0; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3768 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3769 | // No consecutive 1 bits. |
| 3770 | ASSERT((mark_bits & 0x180) != 0x180); |
| 3771 | ASSERT((mark_bits & 0x18000) != 0x18000); |
| 3772 | ASSERT((mark_bits & 0x1800000) != 0x1800000); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3773 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3774 | while (mark_bits != 0) { |
| 3775 | int byte = (mark_bits & 0xff); |
| 3776 | mark_bits >>= 8; |
| 3777 | if (byte != 0) { |
| 3778 | ASSERT(byte < kStartTableLines); // No consecutive 1 bits. |
| 3779 | char* table = kStartTable + byte * kStartTableEntriesPerLine; |
| 3780 | int objects_in_these_8_words = table[0]; |
| 3781 | ASSERT(objects_in_these_8_words != kStartTableInvalidLine); |
| 3782 | ASSERT(objects_in_these_8_words < kStartTableEntriesPerLine); |
| 3783 | for (int i = 0; i < objects_in_these_8_words; i++) { |
| 3784 | starts[objects++] = offset + table[1 + i]; |
| 3785 | } |
| 3786 | } |
| 3787 | offset += 8; |
| 3788 | } |
| 3789 | return objects; |
| 3790 | } |
| 3791 | |
| 3792 | |
| 3793 | static inline Address DigestFreeStart(Address approximate_free_start, |
| 3794 | uint32_t free_start_cell) { |
| 3795 | ASSERT(free_start_cell != 0); |
| 3796 | |
| 3797 | // No consecutive 1 bits. |
| 3798 | ASSERT((free_start_cell & (free_start_cell << 1)) == 0); |
| 3799 | |
| 3800 | int offsets[16]; |
| 3801 | uint32_t cell = free_start_cell; |
| 3802 | int offset_of_last_live; |
| 3803 | if ((cell & 0x80000000u) != 0) { |
| 3804 | // This case would overflow below. |
| 3805 | offset_of_last_live = 31; |
| 3806 | } else { |
| 3807 | // Remove all but one bit, the most significant. This is an optimization |
| 3808 | // that may or may not be worthwhile. |
| 3809 | cell |= cell >> 16; |
| 3810 | cell |= cell >> 8; |
| 3811 | cell |= cell >> 4; |
| 3812 | cell |= cell >> 2; |
| 3813 | cell |= cell >> 1; |
| 3814 | cell = (cell + 1) >> 1; |
| 3815 | int live_objects = MarkWordToObjectStarts(cell, offsets); |
| 3816 | ASSERT(live_objects == 1); |
| 3817 | offset_of_last_live = offsets[live_objects - 1]; |
| 3818 | } |
| 3819 | Address last_live_start = |
| 3820 | approximate_free_start + offset_of_last_live * kPointerSize; |
| 3821 | HeapObject* last_live = HeapObject::FromAddress(last_live_start); |
| 3822 | Address free_start = last_live_start + last_live->Size(); |
| 3823 | return free_start; |
| 3824 | } |
| 3825 | |
| 3826 | |
| 3827 | static inline Address StartOfLiveObject(Address block_address, uint32_t cell) { |
| 3828 | ASSERT(cell != 0); |
| 3829 | |
| 3830 | // No consecutive 1 bits. |
| 3831 | ASSERT((cell & (cell << 1)) == 0); |
| 3832 | |
| 3833 | int offsets[16]; |
| 3834 | if (cell == 0x80000000u) { // Avoid overflow below. |
| 3835 | return block_address + 31 * kPointerSize; |
| 3836 | } |
| 3837 | uint32_t first_set_bit = ((cell ^ (cell - 1)) + 1) >> 1; |
| 3838 | ASSERT((first_set_bit & cell) == first_set_bit); |
| 3839 | int live_objects = MarkWordToObjectStarts(first_set_bit, offsets); |
| 3840 | ASSERT(live_objects == 1); |
| 3841 | USE(live_objects); |
| 3842 | return block_address + offsets[0] * kPointerSize; |
| 3843 | } |
| 3844 | |
| 3845 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3846 | template<MarkCompactCollector::SweepingParallelism mode> |
| 3847 | static intptr_t Free(PagedSpace* space, |
| 3848 | FreeList* free_list, |
| 3849 | Address start, |
| 3850 | int size) { |
| 3851 | if (mode == MarkCompactCollector::SWEEP_SEQUENTIALLY) { |
| 3852 | return space->Free(start, size); |
| 3853 | } else { |
| 3854 | return size - free_list->Free(start, size); |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | |
| 3859 | // Force instantiation of templatized SweepConservatively method for |
| 3860 | // SWEEP_SEQUENTIALLY mode. |
| 3861 | template intptr_t MarkCompactCollector:: |
| 3862 | SweepConservatively<MarkCompactCollector::SWEEP_SEQUENTIALLY>( |
| 3863 | PagedSpace*, FreeList*, Page*); |
| 3864 | |
| 3865 | |
| 3866 | // Force instantiation of templatized SweepConservatively method for |
| 3867 | // SWEEP_IN_PARALLEL mode. |
| 3868 | template intptr_t MarkCompactCollector:: |
| 3869 | SweepConservatively<MarkCompactCollector::SWEEP_IN_PARALLEL>( |
| 3870 | PagedSpace*, FreeList*, Page*); |
| 3871 | |
| 3872 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3873 | // Sweeps a space conservatively. After this has been done the larger free |
| 3874 | // spaces have been put on the free list and the smaller ones have been |
| 3875 | // ignored and left untouched. A free space is always either ignored or put |
| 3876 | // on the free list, never split up into two parts. This is important |
| 3877 | // because it means that any FreeSpace maps left actually describe a region of |
| 3878 | // memory that can be ignored when scanning. Dead objects other than free |
| 3879 | // spaces will not contain the free space map. |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3880 | template<MarkCompactCollector::SweepingParallelism mode> |
| 3881 | intptr_t MarkCompactCollector::SweepConservatively(PagedSpace* space, |
| 3882 | FreeList* free_list, |
| 3883 | Page* p) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3884 | ASSERT(!p->IsEvacuationCandidate() && !p->WasSwept()); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3885 | ASSERT((mode == MarkCompactCollector::SWEEP_IN_PARALLEL && |
| 3886 | free_list != NULL) || |
| 3887 | (mode == MarkCompactCollector::SWEEP_SEQUENTIALLY && |
| 3888 | free_list == NULL)); |
| 3889 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3890 | p->MarkSweptConservatively(); |
| 3891 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3892 | intptr_t freed_bytes = 0; |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3893 | size_t size = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3894 | |
| 3895 | // Skip over all the dead objects at the start of the page and mark them free. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3896 | Address cell_base = 0; |
| 3897 | MarkBit::CellType* cell = NULL; |
| 3898 | MarkBitCellIterator it(p); |
| 3899 | for (; !it.Done(); it.Advance()) { |
| 3900 | cell_base = it.CurrentCellBase(); |
| 3901 | cell = it.CurrentCell(); |
| 3902 | if (*cell != 0) break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3903 | } |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3904 | |
| 3905 | if (it.Done()) { |
| 3906 | size = p->area_end() - p->area_start(); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3907 | freed_bytes += Free<mode>(space, free_list, p->area_start(), |
| 3908 | static_cast<int>(size)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3909 | ASSERT_EQ(0, p->LiveBytes()); |
| 3910 | return freed_bytes; |
| 3911 | } |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3912 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3913 | // Grow the size of the start-of-page free space a little to get up to the |
| 3914 | // first live object. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3915 | Address free_end = StartOfLiveObject(cell_base, *cell); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3916 | // Free the first free space. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 3917 | size = free_end - p->area_start(); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3918 | freed_bytes += Free<mode>(space, free_list, p->area_start(), |
| 3919 | static_cast<int>(size)); |
| 3920 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3921 | // The start of the current free area is represented in undigested form by |
| 3922 | // the address of the last 32-word section that contained a live object and |
| 3923 | // the marking bitmap for that cell, which describes where the live object |
| 3924 | // started. Unless we find a large free space in the bitmap we will not |
| 3925 | // digest this pair into a real address. We start the iteration here at the |
| 3926 | // first word in the marking bit map that indicates a live object. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3927 | Address free_start = cell_base; |
| 3928 | MarkBit::CellType free_start_cell = *cell; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3929 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3930 | for (; !it.Done(); it.Advance()) { |
| 3931 | cell_base = it.CurrentCellBase(); |
| 3932 | cell = it.CurrentCell(); |
| 3933 | if (*cell != 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3934 | // We have a live object. Check approximately whether it is more than 32 |
| 3935 | // words since the last live object. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3936 | if (cell_base - free_start > 32 * kPointerSize) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3937 | free_start = DigestFreeStart(free_start, free_start_cell); |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3938 | if (cell_base - free_start > 32 * kPointerSize) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3939 | // Now that we know the exact start of the free space it still looks |
| 3940 | // like we have a large enough free space to be worth bothering with. |
| 3941 | // so now we need to find the start of the first live object at the |
| 3942 | // end of the free space. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3943 | free_end = StartOfLiveObject(cell_base, *cell); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3944 | freed_bytes += Free<mode>(space, free_list, free_start, |
| 3945 | static_cast<int>(free_end - free_start)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3946 | } |
| 3947 | } |
| 3948 | // Update our undigested record of where the current free area started. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3949 | free_start = cell_base; |
| 3950 | free_start_cell = *cell; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3951 | // Clear marking bits for current cell. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3952 | *cell = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3953 | } |
| 3954 | } |
| 3955 | |
| 3956 | // Handle the free space at the end of the page. |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3957 | if (cell_base - free_start > 32 * kPointerSize) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3958 | free_start = DigestFreeStart(free_start, free_start_cell); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3959 | freed_bytes += Free<mode>(space, free_list, free_start, |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 3960 | static_cast<int>(p->area_end() - free_start)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | p->ResetLiveBytes(); |
| 3964 | return freed_bytes; |
| 3965 | } |
| 3966 | |
| 3967 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3968 | void MarkCompactCollector::SweepInParallel(PagedSpace* space, |
| 3969 | FreeList* private_free_list, |
| 3970 | FreeList* free_list) { |
| 3971 | PageIterator it(space); |
| 3972 | while (it.has_next()) { |
| 3973 | Page* p = it.next(); |
| 3974 | |
| 3975 | if (p->TryParallelSweeping()) { |
| 3976 | SweepConservatively<SWEEP_IN_PARALLEL>(space, private_free_list, p); |
| 3977 | free_list->Concatenate(private_free_list); |
| 3978 | } |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3983 | void MarkCompactCollector::SweepSpace(PagedSpace* space, SweeperType sweeper) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3984 | space->set_was_swept_conservatively(sweeper == CONSERVATIVE || |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 3985 | sweeper == LAZY_CONSERVATIVE || |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 3986 | sweeper == PARALLEL_CONSERVATIVE || |
| 3987 | sweeper == CONCURRENT_CONSERVATIVE); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3988 | space->ClearStats(); |
| 3989 | |
| 3990 | PageIterator it(space); |
| 3991 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3992 | int pages_swept = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3993 | bool lazy_sweeping_active = false; |
| 3994 | bool unused_page_present = false; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3995 | bool parallel_sweeping_active = false; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3996 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3997 | while (it.has_next()) { |
| 3998 | Page* p = it.next(); |
| 3999 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4000 | ASSERT(p->parallel_sweeping() == 0); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4001 | ASSERT(!p->IsEvacuationCandidate()); |
| 4002 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4003 | // Clear sweeping flags indicating that marking bits are still intact. |
| 4004 | p->ClearSweptPrecisely(); |
| 4005 | p->ClearSweptConservatively(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4006 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4007 | if (p->IsFlagSet(Page::RESCAN_ON_EVACUATION)) { |
| 4008 | // Will be processed in EvacuateNewSpaceAndCandidates. |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4009 | ASSERT(evacuation_candidates_.length() > 0); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4010 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4011 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 4012 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4013 | // One unused page is kept, all further are released before sweeping them. |
| 4014 | if (p->LiveBytes() == 0) { |
| 4015 | if (unused_page_present) { |
| 4016 | if (FLAG_gc_verbose) { |
| 4017 | PrintF("Sweeping 0x%" V8PRIxPTR " released page.\n", |
| 4018 | reinterpret_cast<intptr_t>(p)); |
| 4019 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4020 | // Adjust unswept free bytes because releasing a page expects said |
| 4021 | // counter to be accurate for unswept pages. |
| 4022 | space->IncreaseUnsweptFreeBytes(p); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4023 | space->ReleasePage(p, true); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4024 | continue; |
| 4025 | } |
| 4026 | unused_page_present = true; |
| 4027 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4028 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4029 | switch (sweeper) { |
| 4030 | case CONSERVATIVE: { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4031 | if (FLAG_gc_verbose) { |
| 4032 | PrintF("Sweeping 0x%" V8PRIxPTR " conservatively.\n", |
| 4033 | reinterpret_cast<intptr_t>(p)); |
| 4034 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4035 | SweepConservatively<SWEEP_SEQUENTIALLY>(space, NULL, p); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4036 | pages_swept++; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4037 | break; |
| 4038 | } |
| 4039 | case LAZY_CONSERVATIVE: { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4040 | if (lazy_sweeping_active) { |
| 4041 | if (FLAG_gc_verbose) { |
| 4042 | PrintF("Sweeping 0x%" V8PRIxPTR " lazily postponed.\n", |
| 4043 | reinterpret_cast<intptr_t>(p)); |
| 4044 | } |
| 4045 | space->IncreaseUnsweptFreeBytes(p); |
| 4046 | } else { |
| 4047 | if (FLAG_gc_verbose) { |
| 4048 | PrintF("Sweeping 0x%" V8PRIxPTR " conservatively.\n", |
| 4049 | reinterpret_cast<intptr_t>(p)); |
| 4050 | } |
| 4051 | SweepConservatively<SWEEP_SEQUENTIALLY>(space, NULL, p); |
| 4052 | pages_swept++; |
| 4053 | space->SetPagesToSweep(p->next_page()); |
| 4054 | lazy_sweeping_active = true; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4055 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4056 | break; |
| 4057 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 4058 | case CONCURRENT_CONSERVATIVE: |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4059 | case PARALLEL_CONSERVATIVE: { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4060 | if (!parallel_sweeping_active) { |
| 4061 | if (FLAG_gc_verbose) { |
| 4062 | PrintF("Sweeping 0x%" V8PRIxPTR " conservatively.\n", |
| 4063 | reinterpret_cast<intptr_t>(p)); |
| 4064 | } |
| 4065 | SweepConservatively<SWEEP_SEQUENTIALLY>(space, NULL, p); |
| 4066 | pages_swept++; |
| 4067 | parallel_sweeping_active = true; |
| 4068 | } else { |
| 4069 | if (FLAG_gc_verbose) { |
| 4070 | PrintF("Sweeping 0x%" V8PRIxPTR " conservatively in parallel.\n", |
| 4071 | reinterpret_cast<intptr_t>(p)); |
| 4072 | } |
| 4073 | p->set_parallel_sweeping(1); |
| 4074 | space->IncreaseUnsweptFreeBytes(p); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4075 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4076 | break; |
| 4077 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4078 | case PRECISE: { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4079 | if (FLAG_gc_verbose) { |
| 4080 | PrintF("Sweeping 0x%" V8PRIxPTR " precisely.\n", |
| 4081 | reinterpret_cast<intptr_t>(p)); |
| 4082 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4083 | if (space->identity() == CODE_SPACE) { |
| 4084 | SweepPrecisely<SWEEP_ONLY, REBUILD_SKIP_LIST>(space, p, NULL); |
| 4085 | } else { |
| 4086 | SweepPrecisely<SWEEP_ONLY, IGNORE_SKIP_LIST>(space, p, NULL); |
| 4087 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4088 | pages_swept++; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4089 | break; |
| 4090 | } |
| 4091 | default: { |
| 4092 | UNREACHABLE(); |
| 4093 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4094 | } |
| 4095 | } |
| 4096 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4097 | if (FLAG_gc_verbose) { |
| 4098 | PrintF("SweepSpace: %s (%d pages swept)\n", |
| 4099 | AllocationSpaceName(space->identity()), |
| 4100 | pages_swept); |
| 4101 | } |
| 4102 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4103 | // Give pages that are queued to be freed back to the OS. |
| 4104 | heap()->FreeQueuedChunks(); |
| 4105 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4106 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4107 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4108 | void MarkCompactCollector::SweepSpaces() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4109 | GCTracer::Scope gc_scope(tracer_, GCTracer::Scope::MC_SWEEP); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4110 | #ifdef DEBUG |
| 4111 | state_ = SWEEP_SPACES; |
| 4112 | #endif |
| 4113 | SweeperType how_to_sweep = |
| 4114 | FLAG_lazy_sweeping ? LAZY_CONSERVATIVE : CONSERVATIVE; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 4115 | if (FLAG_parallel_sweeping) how_to_sweep = PARALLEL_CONSERVATIVE; |
| 4116 | if (FLAG_concurrent_sweeping) how_to_sweep = CONCURRENT_CONSERVATIVE; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4117 | if (FLAG_expose_gc) how_to_sweep = CONSERVATIVE; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4118 | if (sweep_precisely_) how_to_sweep = PRECISE; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4119 | |
| 4120 | // Unlink evacuation candidates before sweeper threads access the list of |
| 4121 | // pages to avoid race condition. |
| 4122 | UnlinkEvacuationCandidates(); |
| 4123 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4124 | // Noncompacting collections simply sweep the spaces to clear the mark |
| 4125 | // bits and free the nonlive blocks (for old and map spaces). We sweep |
| 4126 | // the map space last because freeing non-live maps overwrites them and |
| 4127 | // the other spaces rely on possibly non-live maps to get the sizes for |
| 4128 | // non-live objects. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4129 | SequentialSweepingScope scope(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4130 | SweepSpace(heap()->old_pointer_space(), how_to_sweep); |
| 4131 | SweepSpace(heap()->old_data_space(), how_to_sweep); |
| 4132 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 4133 | if (how_to_sweep == PARALLEL_CONSERVATIVE || |
| 4134 | how_to_sweep == CONCURRENT_CONSERVATIVE) { |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 4135 | // TODO(hpayer): fix race with concurrent sweeper |
| 4136 | StartSweeperThreads(); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | if (how_to_sweep == PARALLEL_CONSERVATIVE) { |
| 4140 | WaitUntilSweepingCompleted(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4143 | RemoveDeadInvalidatedCode(); |
| 4144 | SweepSpace(heap()->code_space(), PRECISE); |
| 4145 | |
| 4146 | SweepSpace(heap()->cell_space(), PRECISE); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 4147 | SweepSpace(heap()->property_cell_space(), PRECISE); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4148 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4149 | EvacuateNewSpaceAndCandidates(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4150 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4151 | // ClearNonLiveTransitions depends on precise sweeping of map space to |
| 4152 | // detect whether unmarked map became dead in this collection or in one |
| 4153 | // of the previous ones. |
| 4154 | SweepSpace(heap()->map_space(), PRECISE); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 4155 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4156 | // Deallocate unmarked objects and clear marked bits for marked objects. |
| 4157 | heap_->lo_space()->FreeUnmarkedObjects(); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 4158 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4159 | // Deallocate evacuated candidate pages. |
| 4160 | ReleaseEvacuationCandidates(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4164 | void MarkCompactCollector::EnableCodeFlushing(bool enable) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 4165 | #ifdef ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 4166 | if (isolate()->debug()->IsLoaded() || |
| 4167 | isolate()->debug()->has_break_points()) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 4168 | enable = false; |
| 4169 | } |
| 4170 | #endif |
| 4171 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4172 | if (enable) { |
| 4173 | if (code_flusher_ != NULL) return; |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 4174 | code_flusher_ = new CodeFlusher(isolate()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4175 | } else { |
| 4176 | if (code_flusher_ == NULL) return; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4177 | code_flusher_->EvictAllCandidates(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4178 | delete code_flusher_; |
| 4179 | code_flusher_ = NULL; |
| 4180 | } |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 4181 | |
| 4182 | if (FLAG_trace_code_flushing) { |
| 4183 | PrintF("[code-flushing is now %s]\n", enable ? "on" : "off"); |
| 4184 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4188 | // TODO(1466) ReportDeleteIfNeeded is not called currently. |
| 4189 | // Our profiling tools do not expect intersections between |
| 4190 | // code objects. We should either reenable it or change our tools. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4191 | void MarkCompactCollector::ReportDeleteIfNeeded(HeapObject* obj, |
| 4192 | Isolate* isolate) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 4193 | #ifdef ENABLE_GDB_JIT_INTERFACE |
| 4194 | if (obj->IsCode()) { |
| 4195 | GDBJITInterface::RemoveCode(reinterpret_cast<Code*>(obj)); |
| 4196 | } |
| 4197 | #endif |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4198 | if (obj->IsCode()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4199 | PROFILE(isolate, CodeDeleteEvent(obj->address())); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4200 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 4203 | |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 4204 | Isolate* MarkCompactCollector::isolate() const { |
| 4205 | return heap_->isolate(); |
| 4206 | } |
| 4207 | |
| 4208 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4209 | void MarkCompactCollector::Initialize() { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 4210 | MarkCompactMarkingVisitor::Initialize(); |
| 4211 | IncrementalMarking::Initialize(); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4215 | bool SlotsBuffer::IsTypedSlot(ObjectSlot slot) { |
| 4216 | return reinterpret_cast<uintptr_t>(slot) < NUMBER_OF_SLOT_TYPES; |
| 4217 | } |
| 4218 | |
| 4219 | |
| 4220 | bool SlotsBuffer::AddTo(SlotsBufferAllocator* allocator, |
| 4221 | SlotsBuffer** buffer_address, |
| 4222 | SlotType type, |
| 4223 | Address addr, |
| 4224 | AdditionMode mode) { |
| 4225 | SlotsBuffer* buffer = *buffer_address; |
| 4226 | if (buffer == NULL || !buffer->HasSpaceForTypedSlot()) { |
| 4227 | if (mode == FAIL_ON_OVERFLOW && ChainLengthThresholdReached(buffer)) { |
| 4228 | allocator->DeallocateChain(buffer_address); |
| 4229 | return false; |
| 4230 | } |
| 4231 | buffer = allocator->AllocateBuffer(buffer); |
| 4232 | *buffer_address = buffer; |
| 4233 | } |
| 4234 | ASSERT(buffer->HasSpaceForTypedSlot()); |
| 4235 | buffer->Add(reinterpret_cast<ObjectSlot>(type)); |
| 4236 | buffer->Add(reinterpret_cast<ObjectSlot>(addr)); |
| 4237 | return true; |
| 4238 | } |
| 4239 | |
| 4240 | |
| 4241 | static inline SlotsBuffer::SlotType SlotTypeForRMode(RelocInfo::Mode rmode) { |
| 4242 | if (RelocInfo::IsCodeTarget(rmode)) { |
| 4243 | return SlotsBuffer::CODE_TARGET_SLOT; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 4244 | } else if (RelocInfo::IsEmbeddedObject(rmode)) { |
| 4245 | return SlotsBuffer::EMBEDDED_OBJECT_SLOT; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4246 | } else if (RelocInfo::IsDebugBreakSlot(rmode)) { |
| 4247 | return SlotsBuffer::DEBUG_TARGET_SLOT; |
| 4248 | } else if (RelocInfo::IsJSReturn(rmode)) { |
| 4249 | return SlotsBuffer::JS_RETURN_SLOT; |
| 4250 | } |
| 4251 | UNREACHABLE(); |
| 4252 | return SlotsBuffer::NUMBER_OF_SLOT_TYPES; |
| 4253 | } |
| 4254 | |
| 4255 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 4256 | void MarkCompactCollector::RecordRelocSlot(RelocInfo* rinfo, Object* target) { |
| 4257 | Page* target_page = Page::FromAddress(reinterpret_cast<Address>(target)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4258 | if (target_page->IsEvacuationCandidate() && |
| 4259 | (rinfo->host() == NULL || |
| 4260 | !ShouldSkipEvacuationSlotRecording(rinfo->host()))) { |
| 4261 | if (!SlotsBuffer::AddTo(&slots_buffer_allocator_, |
| 4262 | target_page->slots_buffer_address(), |
| 4263 | SlotTypeForRMode(rinfo->rmode()), |
| 4264 | rinfo->pc(), |
| 4265 | SlotsBuffer::FAIL_ON_OVERFLOW)) { |
| 4266 | EvictEvacuationCandidate(target_page); |
| 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | |
| 4272 | void MarkCompactCollector::RecordCodeEntrySlot(Address slot, Code* target) { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 4273 | Page* target_page = Page::FromAddress(reinterpret_cast<Address>(target)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4274 | if (target_page->IsEvacuationCandidate() && |
| 4275 | !ShouldSkipEvacuationSlotRecording(reinterpret_cast<Object**>(slot))) { |
| 4276 | if (!SlotsBuffer::AddTo(&slots_buffer_allocator_, |
| 4277 | target_page->slots_buffer_address(), |
| 4278 | SlotsBuffer::CODE_ENTRY_SLOT, |
| 4279 | slot, |
| 4280 | SlotsBuffer::FAIL_ON_OVERFLOW)) { |
| 4281 | EvictEvacuationCandidate(target_page); |
| 4282 | } |
| 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 4287 | void MarkCompactCollector::RecordCodeTargetPatch(Address pc, Code* target) { |
| 4288 | ASSERT(heap()->gc_state() == Heap::MARK_COMPACT); |
| 4289 | if (is_compacting()) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 4290 | Code* host = isolate()->inner_pointer_to_code_cache()-> |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 4291 | GcSafeFindCodeForInnerPointer(pc); |
| 4292 | MarkBit mark_bit = Marking::MarkBitFrom(host); |
| 4293 | if (Marking::IsBlack(mark_bit)) { |
| 4294 | RelocInfo rinfo(pc, RelocInfo::CODE_TARGET, 0, host); |
| 4295 | RecordRelocSlot(&rinfo, target); |
| 4296 | } |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4301 | static inline SlotsBuffer::SlotType DecodeSlotType( |
| 4302 | SlotsBuffer::ObjectSlot slot) { |
| 4303 | return static_cast<SlotsBuffer::SlotType>(reinterpret_cast<intptr_t>(slot)); |
| 4304 | } |
| 4305 | |
| 4306 | |
| 4307 | void SlotsBuffer::UpdateSlots(Heap* heap) { |
| 4308 | PointersUpdatingVisitor v(heap); |
| 4309 | |
| 4310 | for (int slot_idx = 0; slot_idx < idx_; ++slot_idx) { |
| 4311 | ObjectSlot slot = slots_[slot_idx]; |
| 4312 | if (!IsTypedSlot(slot)) { |
| 4313 | PointersUpdatingVisitor::UpdateSlot(heap, slot); |
| 4314 | } else { |
| 4315 | ++slot_idx; |
| 4316 | ASSERT(slot_idx < idx_); |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 4317 | UpdateSlot(heap->isolate(), |
| 4318 | &v, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4319 | DecodeSlotType(slot), |
| 4320 | reinterpret_cast<Address>(slots_[slot_idx])); |
| 4321 | } |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | |
| 4326 | void SlotsBuffer::UpdateSlotsWithFilter(Heap* heap) { |
| 4327 | PointersUpdatingVisitor v(heap); |
| 4328 | |
| 4329 | for (int slot_idx = 0; slot_idx < idx_; ++slot_idx) { |
| 4330 | ObjectSlot slot = slots_[slot_idx]; |
| 4331 | if (!IsTypedSlot(slot)) { |
| 4332 | if (!IsOnInvalidatedCodeObject(reinterpret_cast<Address>(slot))) { |
| 4333 | PointersUpdatingVisitor::UpdateSlot(heap, slot); |
| 4334 | } |
| 4335 | } else { |
| 4336 | ++slot_idx; |
| 4337 | ASSERT(slot_idx < idx_); |
| 4338 | Address pc = reinterpret_cast<Address>(slots_[slot_idx]); |
| 4339 | if (!IsOnInvalidatedCodeObject(pc)) { |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 4340 | UpdateSlot(heap->isolate(), |
| 4341 | &v, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4342 | DecodeSlotType(slot), |
| 4343 | reinterpret_cast<Address>(slots_[slot_idx])); |
| 4344 | } |
| 4345 | } |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | |
| 4350 | SlotsBuffer* SlotsBufferAllocator::AllocateBuffer(SlotsBuffer* next_buffer) { |
| 4351 | return new SlotsBuffer(next_buffer); |
| 4352 | } |
| 4353 | |
| 4354 | |
| 4355 | void SlotsBufferAllocator::DeallocateBuffer(SlotsBuffer* buffer) { |
| 4356 | delete buffer; |
| 4357 | } |
| 4358 | |
| 4359 | |
| 4360 | void SlotsBufferAllocator::DeallocateChain(SlotsBuffer** buffer_address) { |
| 4361 | SlotsBuffer* buffer = *buffer_address; |
| 4362 | while (buffer != NULL) { |
| 4363 | SlotsBuffer* next_buffer = buffer->next(); |
| 4364 | DeallocateBuffer(buffer); |
| 4365 | buffer = next_buffer; |
| 4366 | } |
| 4367 | *buffer_address = NULL; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4371 | } } // namespace v8::internal |