Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 17 | #include "semi_space.h" |
| 18 | |
| 19 | #include <functional> |
| 20 | #include <numeric> |
| 21 | #include <climits> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "base/logging.h" |
| 25 | #include "base/macros.h" |
| 26 | #include "base/mutex-inl.h" |
| 27 | #include "base/timing_logger.h" |
| 28 | #include "gc/accounting/heap_bitmap.h" |
| 29 | #include "gc/accounting/mod_union_table.h" |
| 30 | #include "gc/accounting/space_bitmap-inl.h" |
| 31 | #include "gc/heap.h" |
| 32 | #include "gc/space/bump_pointer_space.h" |
| 33 | #include "gc/space/bump_pointer_space-inl.h" |
| 34 | #include "gc/space/image_space.h" |
| 35 | #include "gc/space/large_object_space.h" |
| 36 | #include "gc/space/space-inl.h" |
| 37 | #include "indirect_reference_table.h" |
| 38 | #include "intern_table.h" |
| 39 | #include "jni_internal.h" |
| 40 | #include "mark_sweep-inl.h" |
| 41 | #include "monitor.h" |
| 42 | #include "mirror/art_field.h" |
| 43 | #include "mirror/art_field-inl.h" |
| 44 | #include "mirror/class-inl.h" |
| 45 | #include "mirror/class_loader.h" |
| 46 | #include "mirror/dex_cache.h" |
| 47 | #include "mirror/object-inl.h" |
| 48 | #include "mirror/object_array.h" |
| 49 | #include "mirror/object_array-inl.h" |
| 50 | #include "runtime.h" |
| 51 | #include "semi_space-inl.h" |
| 52 | #include "thread-inl.h" |
| 53 | #include "thread_list.h" |
| 54 | #include "verifier/method_verifier.h" |
| 55 | |
| 56 | using ::art::mirror::Class; |
| 57 | using ::art::mirror::Object; |
| 58 | |
| 59 | namespace art { |
| 60 | namespace gc { |
| 61 | namespace collector { |
| 62 | |
| 63 | static constexpr bool kProtectFromSpace = true; |
| 64 | static constexpr bool kResetFromSpace = true; |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 65 | // TODO: move this to a new file as a new garbage collector? |
| 66 | static constexpr bool kEnableSimplePromo = false; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 67 | |
| 68 | // TODO: Unduplicate logic. |
| 69 | void SemiSpace::ImmuneSpace(space::ContinuousSpace* space) { |
| 70 | // Bind live to mark bitmap if necessary. |
| 71 | if (space->GetLiveBitmap() != space->GetMarkBitmap()) { |
| 72 | BindLiveToMarkBitmap(space); |
| 73 | } |
| 74 | // Add the space to the immune region. |
| 75 | if (immune_begin_ == nullptr) { |
| 76 | DCHECK(immune_end_ == nullptr); |
| 77 | immune_begin_ = reinterpret_cast<Object*>(space->Begin()); |
| 78 | immune_end_ = reinterpret_cast<Object*>(space->End()); |
| 79 | } else { |
| 80 | const space::ContinuousSpace* prev_space = nullptr; |
| 81 | // Find out if the previous space is immune. |
| 82 | for (space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) { |
| 83 | if (cur_space == space) { |
| 84 | break; |
| 85 | } |
| 86 | prev_space = cur_space; |
| 87 | } |
| 88 | // If previous space was immune, then extend the immune region. Relies on continuous spaces |
| 89 | // being sorted by Heap::AddContinuousSpace. |
| 90 | if (prev_space != nullptr && IsImmuneSpace(prev_space)) { |
| 91 | immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_); |
| 92 | immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void SemiSpace::BindBitmaps() { |
| 98 | timings_.StartSplit("BindBitmaps"); |
| 99 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 100 | // Mark all of the spaces we never collect as immune. |
| 101 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 102 | if (space->GetLiveBitmap() != nullptr) { |
| 103 | if (space == to_space_) { |
| 104 | BindLiveToMarkBitmap(to_space_); |
| 105 | } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect |
| 106 | || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) { |
| 107 | ImmuneSpace(space); |
| 108 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | timings_.EndSplit(); |
| 112 | } |
| 113 | |
| 114 | SemiSpace::SemiSpace(Heap* heap, const std::string& name_prefix) |
| 115 | : GarbageCollector(heap, |
| 116 | name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"), |
| 117 | mark_stack_(nullptr), |
| 118 | immune_begin_(nullptr), |
| 119 | immune_end_(nullptr), |
| 120 | to_space_(nullptr), |
| 121 | from_space_(nullptr), |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 122 | self_(nullptr), |
| 123 | last_gc_to_space_end_(nullptr), |
| 124 | bytes_promoted_(0) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void SemiSpace::InitializePhase() { |
| 128 | timings_.Reset(); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 129 | TimingLogger::ScopedSplit split("InitializePhase", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 130 | mark_stack_ = heap_->mark_stack_.get(); |
| 131 | DCHECK(mark_stack_ != nullptr); |
| 132 | immune_begin_ = nullptr; |
| 133 | immune_end_ = nullptr; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 134 | self_ = Thread::Current(); |
| 135 | // Do any pre GC verification. |
| 136 | timings_.NewSplit("PreGcVerification"); |
| 137 | heap_->PreGcVerification(this); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 138 | // Set the initial bitmap. |
| 139 | to_space_live_bitmap_ = to_space_->GetLiveBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void SemiSpace::ProcessReferences(Thread* self) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 143 | TimingLogger::ScopedSplit split("ProcessReferences", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 144 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 145 | GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback, |
| 146 | &RecursiveMarkObjectCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void SemiSpace::MarkingPhase() { |
| 150 | Thread* self = Thread::Current(); |
| 151 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 152 | TimingLogger::ScopedSplit split("MarkingPhase", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 153 | // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the |
| 154 | // wrong space. |
| 155 | heap_->SwapSemiSpaces(); |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 156 | if (kEnableSimplePromo) { |
| 157 | // If last_gc_to_space_end_ is out of the bounds of the from-space |
| 158 | // (the to-space from last GC), then point it to the beginning of |
| 159 | // the from-space. For example, the very first GC or the |
| 160 | // pre-zygote compaction. |
| 161 | if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) { |
| 162 | last_gc_to_space_end_ = from_space_->Begin(); |
| 163 | } |
| 164 | // Reset this before the marking starts below. |
| 165 | bytes_promoted_ = 0; |
| 166 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 167 | // Assume the cleared space is already empty. |
| 168 | BindBitmaps(); |
| 169 | // Process dirty cards and add dirty cards to mod-union tables. |
| 170 | heap_->ProcessCards(timings_); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 171 | // Clear the whole card table since we can not get any additional dirty cards during the |
| 172 | // paused GC. This saves memory but only works for pause the world collectors. |
| 173 | timings_.NewSplit("ClearCardTable"); |
| 174 | heap_->GetCardTable()->ClearCardTable(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 175 | // Need to do this before the checkpoint since we don't want any threads to add references to |
| 176 | // the live stack during the recursive mark. |
| 177 | timings_.NewSplit("SwapStacks"); |
| 178 | heap_->SwapStacks(); |
| 179 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 180 | MarkRoots(); |
| 181 | // Mark roots of immune spaces. |
| 182 | UpdateAndMarkModUnion(); |
| 183 | // Recursively mark remaining objects. |
| 184 | MarkReachableObjects(); |
| 185 | } |
| 186 | |
| 187 | bool SemiSpace::IsImmuneSpace(const space::ContinuousSpace* space) const { |
| 188 | return |
| 189 | immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) && |
| 190 | immune_end_ >= reinterpret_cast<Object*>(space->End()); |
| 191 | } |
| 192 | |
| 193 | void SemiSpace::UpdateAndMarkModUnion() { |
| 194 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 195 | // If the space is immune then we need to mark the references to other spaces. |
| 196 | if (IsImmuneSpace(space)) { |
| 197 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
| 198 | CHECK(table != nullptr); |
| 199 | // TODO: Improve naming. |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 200 | TimingLogger::ScopedSplit split( |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 201 | space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" : |
| 202 | "UpdateAndMarkImageModUnionTable", |
| 203 | &timings_); |
| 204 | table->UpdateAndMarkReferences(MarkRootCallback, this); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void SemiSpace::MarkReachableObjects() { |
| 210 | timings_.StartSplit("MarkStackAsLive"); |
| 211 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 212 | heap_->MarkAllocStackAsLive(live_stack); |
| 213 | live_stack->Reset(); |
| 214 | timings_.EndSplit(); |
| 215 | // Recursively process the mark stack. |
| 216 | ProcessMarkStack(true); |
| 217 | } |
| 218 | |
| 219 | void SemiSpace::ReclaimPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 220 | TimingLogger::ScopedSplit split("ReclaimPhase", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 221 | Thread* self = Thread::Current(); |
| 222 | ProcessReferences(self); |
| 223 | { |
| 224 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 225 | SweepSystemWeaks(); |
| 226 | } |
| 227 | // Record freed memory. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 228 | uint64_t from_bytes = from_space_->GetBytesAllocated(); |
| 229 | uint64_t to_bytes = to_space_->GetBytesAllocated(); |
| 230 | uint64_t from_objects = from_space_->GetObjectsAllocated(); |
| 231 | uint64_t to_objects = to_space_->GetObjectsAllocated(); |
| 232 | CHECK_LE(to_objects, from_objects); |
| 233 | int64_t freed_bytes = from_bytes - to_bytes; |
| 234 | int64_t freed_objects = from_objects - to_objects; |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 235 | freed_bytes_.FetchAndAdd(freed_bytes); |
| 236 | freed_objects_.FetchAndAdd(freed_objects); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 237 | // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed |
| 238 | // space. |
| 239 | heap_->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 240 | timings_.StartSplit("PreSweepingGcVerification"); |
| 241 | heap_->PreSweepingGcVerification(this); |
| 242 | timings_.EndSplit(); |
| 243 | |
| 244 | { |
| 245 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 246 | // Reclaim unmarked objects. |
| 247 | Sweep(false); |
| 248 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 249 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 250 | // bitmaps. |
| 251 | timings_.StartSplit("SwapBitmaps"); |
| 252 | SwapBitmaps(); |
| 253 | timings_.EndSplit(); |
| 254 | // Unbind the live and mark bitmaps. |
| 255 | UnBindBitmaps(); |
| 256 | } |
| 257 | // Release the memory used by the from space. |
| 258 | if (kResetFromSpace) { |
| 259 | // Clearing from space. |
| 260 | from_space_->Clear(); |
| 261 | } |
| 262 | // Protect the from space. |
| 263 | VLOG(heap) |
| 264 | << "mprotect region " << reinterpret_cast<void*>(from_space_->Begin()) << " - " |
| 265 | << reinterpret_cast<void*>(from_space_->Limit()); |
| 266 | if (kProtectFromSpace) { |
| 267 | mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_NONE); |
| 268 | } else { |
| 269 | mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_READ); |
| 270 | } |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 271 | |
| 272 | if (kEnableSimplePromo) { |
| 273 | // Record the end (top) of the to space so we can distinguish |
| 274 | // between objects that were allocated since the last GC and the |
| 275 | // older objects. |
| 276 | last_gc_to_space_end_ = to_space_->End(); |
| 277 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void SemiSpace::ResizeMarkStack(size_t new_size) { |
| 281 | std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End()); |
| 282 | CHECK_LE(mark_stack_->Size(), new_size); |
| 283 | mark_stack_->Resize(new_size); |
| 284 | for (const auto& obj : temp) { |
| 285 | mark_stack_->PushBack(obj); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | inline void SemiSpace::MarkStackPush(Object* obj) { |
| 290 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 291 | ResizeMarkStack(mark_stack_->Capacity() * 2); |
| 292 | } |
| 293 | // The object must be pushed on to the mark stack. |
| 294 | mark_stack_->PushBack(obj); |
| 295 | } |
| 296 | |
| 297 | // Rare case, probably not worth inlining since it will increase instruction cache miss rate. |
| 298 | bool SemiSpace::MarkLargeObject(const Object* obj) { |
| 299 | // TODO: support >1 discontinuous space. |
| 300 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 301 | accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects(); |
| 302 | if (UNLIKELY(!large_objects->Test(obj))) { |
| 303 | large_objects->Set(obj); |
| 304 | return true; |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 309 | mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) { |
| 310 | size_t object_size = obj->SizeOf(); |
| 311 | size_t bytes_allocated = 0; |
| 312 | mirror::Object* forward_address = nullptr; |
| 313 | if (kEnableSimplePromo && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) { |
| 314 | // If it's allocated before the last GC (older), move (pseudo-promote) it to |
| 315 | // the non-moving space (as sort of an old generation). |
| 316 | size_t bytes_promoted; |
| 317 | space::MallocSpace* non_moving_space = GetHeap()->GetNonMovingSpace(); |
| 318 | forward_address = non_moving_space->Alloc(self_, object_size, &bytes_promoted); |
| 319 | if (forward_address == nullptr) { |
| 320 | // If out of space, fall back to the to-space. |
| 321 | forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated); |
| 322 | } else { |
| 323 | GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted); |
| 324 | bytes_promoted_ += bytes_promoted; |
| 325 | // Mark forward_address on the live bit map. |
| 326 | accounting::SpaceBitmap* live_bitmap = non_moving_space->GetLiveBitmap(); |
| 327 | DCHECK(live_bitmap != nullptr); |
| 328 | DCHECK(!live_bitmap->Test(forward_address)); |
| 329 | live_bitmap->Set(forward_address); |
| 330 | // Mark forward_address on the mark bit map. |
| 331 | accounting::SpaceBitmap* mark_bitmap = non_moving_space->GetMarkBitmap(); |
| 332 | DCHECK(mark_bitmap != nullptr); |
| 333 | DCHECK(!mark_bitmap->Test(forward_address)); |
| 334 | mark_bitmap->Set(forward_address); |
| 335 | } |
| 336 | DCHECK(forward_address != nullptr); |
| 337 | } else { |
| 338 | // If it's allocated after the last GC (younger), copy it to the to-space. |
| 339 | forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated); |
| 340 | } |
| 341 | // Copy over the object and add it to the mark stack since we still need to update its |
| 342 | // references. |
| 343 | memcpy(reinterpret_cast<void*>(forward_address), obj, object_size); |
| 344 | if (to_space_live_bitmap_ != nullptr) { |
| 345 | to_space_live_bitmap_->Set(forward_address); |
| 346 | } |
| 347 | return forward_address; |
| 348 | } |
| 349 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 350 | // Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to |
| 351 | // the to-space and have their forward address updated. Objects which have been newly marked are |
| 352 | // pushed on the mark stack. |
| 353 | Object* SemiSpace::MarkObject(Object* obj) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 354 | Object* forward_address = obj; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 355 | if (obj != nullptr && !IsImmune(obj)) { |
| 356 | if (from_space_->HasAddress(obj)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 357 | forward_address = GetForwardingAddressInFromSpace(obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 358 | // If the object has already been moved, return the new forward address. |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 359 | if (forward_address == nullptr) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 360 | forward_address = MarkNonForwardedObject(obj); |
| 361 | DCHECK(forward_address != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 362 | // Make sure to only update the forwarding address AFTER you copy the object so that the |
| 363 | // monitor word doesn't get stomped over. |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 364 | obj->SetLockWord(LockWord::FromForwardingAddress( |
| 365 | reinterpret_cast<size_t>(forward_address))); |
| 366 | // Push the object onto the mark stack for later processing. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 367 | MarkStackPush(forward_address); |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 368 | } else { |
| 369 | DCHECK(to_space_->HasAddress(forward_address) || |
| 370 | (kEnableSimplePromo && GetHeap()->GetNonMovingSpace()->HasAddress(forward_address))); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 371 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 372 | // TODO: Do we need this if in the else statement? |
| 373 | } else { |
| 374 | accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj); |
| 375 | if (LIKELY(object_bitmap != nullptr)) { |
| 376 | // This object was not previously marked. |
| 377 | if (!object_bitmap->Test(obj)) { |
| 378 | object_bitmap->Set(obj); |
| 379 | MarkStackPush(obj); |
| 380 | } |
| 381 | } else { |
| 382 | DCHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_"; |
| 383 | if (MarkLargeObject(obj)) { |
| 384 | MarkStackPush(obj); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 389 | return forward_address; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 392 | Object* SemiSpace::RecursiveMarkObjectCallback(Object* root, void* arg) { |
| 393 | DCHECK(root != nullptr); |
| 394 | DCHECK(arg != nullptr); |
| 395 | SemiSpace* semi_space = reinterpret_cast<SemiSpace*>(arg); |
| 396 | mirror::Object* ret = semi_space->MarkObject(root); |
| 397 | semi_space->ProcessMarkStack(true); |
| 398 | return ret; |
| 399 | } |
| 400 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 401 | Object* SemiSpace::MarkRootCallback(Object* root, void* arg) { |
| 402 | DCHECK(root != nullptr); |
| 403 | DCHECK(arg != nullptr); |
| 404 | return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root); |
| 405 | } |
| 406 | |
| 407 | // Marks all objects in the root set. |
| 408 | void SemiSpace::MarkRoots() { |
| 409 | timings_.StartSplit("MarkRoots"); |
| 410 | // TODO: Visit up image roots as well? |
| 411 | Runtime::Current()->VisitRoots(MarkRootCallback, this, false, true); |
| 412 | timings_.EndSplit(); |
| 413 | } |
| 414 | |
| 415 | void SemiSpace::BindLiveToMarkBitmap(space::ContinuousSpace* space) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 416 | CHECK(space->IsMallocSpace()); |
| 417 | space::MallocSpace* alloc_space = space->AsMallocSpace(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 418 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 419 | accounting::SpaceBitmap* mark_bitmap = alloc_space->BindLiveToMarkBitmap(); |
| 420 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 421 | } |
| 422 | |
| 423 | mirror::Object* SemiSpace::GetForwardingAddress(mirror::Object* obj) { |
| 424 | if (from_space_->HasAddress(obj)) { |
| 425 | LOG(FATAL) << "Shouldn't happen!"; |
| 426 | return GetForwardingAddressInFromSpace(obj); |
| 427 | } |
| 428 | return obj; |
| 429 | } |
| 430 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 431 | mirror::Object* SemiSpace::MarkedForwardingAddressCallback(Object* object, void* arg) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 432 | return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object); |
| 433 | } |
| 434 | |
| 435 | void SemiSpace::SweepSystemWeaks() { |
| 436 | timings_.StartSplit("SweepSystemWeaks"); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 437 | Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 438 | timings_.EndSplit(); |
| 439 | } |
| 440 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 441 | bool SemiSpace::ShouldSweepSpace(space::MallocSpace* space) const { |
| 442 | return space != from_space_ && space != to_space_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | void SemiSpace::Sweep(bool swap_bitmaps) { |
| 446 | DCHECK(mark_stack_->IsEmpty()); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 447 | TimingLogger::ScopedSplit("Sweep", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 448 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 449 | if (space->IsMallocSpace()) { |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 450 | space::MallocSpace* malloc_space = space->AsMallocSpace(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 451 | if (!ShouldSweepSpace(malloc_space)) { |
| 452 | continue; |
| 453 | } |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 454 | TimingLogger::ScopedSplit split( |
| 455 | malloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_); |
| 456 | size_t freed_objects = 0; |
| 457 | size_t freed_bytes = 0; |
| 458 | malloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
| 459 | heap_->RecordFree(freed_objects, freed_bytes); |
| 460 | freed_objects_.FetchAndAdd(freed_objects); |
| 461 | freed_bytes_.FetchAndAdd(freed_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 464 | SweepLargeObjects(swap_bitmaps); |
| 465 | } |
| 466 | |
| 467 | void SemiSpace::SweepLargeObjects(bool swap_bitmaps) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 468 | TimingLogger::ScopedSplit("SweepLargeObjects", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 469 | // Sweep large objects |
| 470 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 471 | accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 472 | accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
| 473 | if (swap_bitmaps) { |
| 474 | std::swap(large_live_objects, large_mark_objects); |
| 475 | } |
| 476 | // O(n*log(n)) but hopefully there are not too many large objects. |
| 477 | size_t freed_objects = 0; |
| 478 | size_t freed_bytes = 0; |
| 479 | Thread* self = Thread::Current(); |
| 480 | for (const Object* obj : large_live_objects->GetObjects()) { |
| 481 | if (!large_mark_objects->Test(obj)) { |
| 482 | freed_bytes += large_object_space->Free(self, const_cast<Object*>(obj)); |
| 483 | ++freed_objects; |
| 484 | } |
| 485 | } |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 486 | freed_large_objects_.FetchAndAdd(freed_objects); |
| 487 | freed_large_object_bytes_.FetchAndAdd(freed_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 488 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
| 489 | } |
| 490 | |
| 491 | // Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been |
| 492 | // marked, put it on the appropriate list in the heap for later processing. |
| 493 | void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 494 | heap_->DelayReferenceReferent(klass, obj, MarkedForwardingAddressCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Visit all of the references of an object and update. |
| 498 | void SemiSpace::ScanObject(Object* obj) { |
| 499 | DCHECK(obj != NULL); |
| 500 | DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space"; |
| 501 | MarkSweep::VisitObjectReferences(obj, [this](Object* obj, Object* ref, const MemberOffset& offset, |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 502 | bool /* is_static */) ALWAYS_INLINE_LAMBDA NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 503 | mirror::Object* new_address = MarkObject(ref); |
| 504 | if (new_address != ref) { |
| 505 | DCHECK(new_address != nullptr); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 506 | // Don't need to mark the card since we updating the object address and not changing the |
| 507 | // actual objects its pointing to. Using SetFieldPtr is better in this case since it does not |
| 508 | // dirty cards and use additional memory. |
| 509 | obj->SetFieldPtr(offset, new_address, false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 510 | } |
| 511 | }, kMovingClasses); |
| 512 | mirror::Class* klass = obj->GetClass(); |
| 513 | if (UNLIKELY(klass->IsReferenceClass())) { |
| 514 | DelayReferenceReferent(klass, obj); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // Scan anything that's on the mark stack. |
| 519 | void SemiSpace::ProcessMarkStack(bool paused) { |
| 520 | timings_.StartSplit(paused ? "(paused)ProcessMarkStack" : "ProcessMarkStack"); |
| 521 | while (!mark_stack_->IsEmpty()) { |
| 522 | ScanObject(mark_stack_->PopBack()); |
| 523 | } |
| 524 | timings_.EndSplit(); |
| 525 | } |
| 526 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 527 | inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const |
| 528 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 529 | // All immune objects are assumed marked. |
| 530 | if (IsImmune(obj)) { |
| 531 | return obj; |
| 532 | } |
| 533 | if (from_space_->HasAddress(obj)) { |
| 534 | mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj)); |
| 535 | // If the object is forwarded then it MUST be marked. |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 536 | DCHECK(forwarding_address == nullptr || to_space_->HasAddress(forwarding_address) || |
| 537 | (kEnableSimplePromo && GetHeap()->GetNonMovingSpace()->HasAddress(forwarding_address))); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 538 | return forwarding_address; // Returns either the forwarding address or nullptr. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 539 | } else if (to_space_->HasAddress(obj)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame^] | 540 | // Should be unlikely. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 541 | // Already forwarded, must be marked. |
| 542 | return obj; |
| 543 | } |
| 544 | return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr; |
| 545 | } |
| 546 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 547 | void SemiSpace::UnBindBitmaps() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 548 | TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 549 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 550 | if (space->IsMallocSpace()) { |
| 551 | space::MallocSpace* alloc_space = space->AsMallocSpace(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 552 | if (alloc_space->HasBoundBitmaps()) { |
| 553 | alloc_space->UnBindBitmaps(); |
| 554 | heap_->GetMarkBitmap()->ReplaceBitmap(alloc_space->GetLiveBitmap(), |
| 555 | alloc_space->GetMarkBitmap()); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) { |
| 562 | DCHECK(to_space != nullptr); |
| 563 | to_space_ = to_space; |
| 564 | } |
| 565 | |
| 566 | void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) { |
| 567 | DCHECK(from_space != nullptr); |
| 568 | from_space_ = from_space; |
| 569 | } |
| 570 | |
| 571 | void SemiSpace::FinishPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 572 | TimingLogger::ScopedSplit split("FinishPhase", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 573 | // Can't enqueue references if we hold the mutator lock. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 574 | Heap* heap = GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 575 | timings_.NewSplit("PostGcVerification"); |
| 576 | heap->PostGcVerification(this); |
| 577 | |
| 578 | // Null the "to" and "from" spaces since compacting from one to the other isn't valid until |
| 579 | // further action is done by the heap. |
| 580 | to_space_ = nullptr; |
| 581 | from_space_ = nullptr; |
| 582 | |
| 583 | // Update the cumulative statistics |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 584 | total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects(); |
| 585 | total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes(); |
| 586 | |
| 587 | // Ensure that the mark stack is empty. |
| 588 | CHECK(mark_stack_->IsEmpty()); |
| 589 | |
| 590 | // Update the cumulative loggers. |
| 591 | cumulative_timings_.Start(); |
| 592 | cumulative_timings_.AddLogger(timings_); |
| 593 | cumulative_timings_.End(); |
| 594 | |
| 595 | // Clear all of the spaces' mark bitmaps. |
| 596 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
| 597 | accounting::SpaceBitmap* bitmap = space->GetMarkBitmap(); |
| 598 | if (bitmap != nullptr && |
| 599 | space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) { |
| 600 | bitmap->Clear(); |
| 601 | } |
| 602 | } |
| 603 | mark_stack_->Reset(); |
| 604 | |
| 605 | // Reset the marked large objects. |
| 606 | space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
| 607 | large_objects->GetMarkObjects()->Clear(); |
| 608 | } |
| 609 | |
| 610 | } // namespace collector |
| 611 | } // namespace gc |
| 612 | } // namespace art |