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; |
| 65 | |
| 66 | // TODO: Unduplicate logic. |
| 67 | void SemiSpace::ImmuneSpace(space::ContinuousSpace* space) { |
| 68 | // Bind live to mark bitmap if necessary. |
| 69 | if (space->GetLiveBitmap() != space->GetMarkBitmap()) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 70 | CHECK(space->IsContinuousMemMapAllocSpace()); |
| 71 | space->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 72 | } |
| 73 | // Add the space to the immune region. |
| 74 | if (immune_begin_ == nullptr) { |
| 75 | DCHECK(immune_end_ == nullptr); |
| 76 | immune_begin_ = reinterpret_cast<Object*>(space->Begin()); |
| 77 | immune_end_ = reinterpret_cast<Object*>(space->End()); |
| 78 | } else { |
| 79 | const space::ContinuousSpace* prev_space = nullptr; |
| 80 | // Find out if the previous space is immune. |
| 81 | for (space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) { |
| 82 | if (cur_space == space) { |
| 83 | break; |
| 84 | } |
| 85 | prev_space = cur_space; |
| 86 | } |
| 87 | // If previous space was immune, then extend the immune region. Relies on continuous spaces |
| 88 | // being sorted by Heap::AddContinuousSpace. |
| 89 | if (prev_space != nullptr && IsImmuneSpace(prev_space)) { |
| 90 | immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 91 | // Use Limit() instead of End() because otherwise if the |
| 92 | // generational mode is enabled, the alloc space might expand |
| 93 | // due to promotion and the sense of immunity may change in the |
| 94 | // middle of a GC. |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 95 | immune_end_ = std::max(reinterpret_cast<Object*>(space->Limit()), immune_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void SemiSpace::BindBitmaps() { |
| 101 | timings_.StartSplit("BindBitmaps"); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 102 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 103 | // Mark all of the spaces we never collect as immune. |
| 104 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 105 | if (space->GetLiveBitmap() != nullptr) { |
| 106 | if (space == to_space_) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 107 | CHECK(to_space_->IsContinuousMemMapAllocSpace()); |
| 108 | to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 109 | } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 110 | || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect |
| 111 | // Add the main free list space and the non-moving |
| 112 | // space to the immune space if a bump pointer space |
| 113 | // only collection. |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 114 | || (generational_ && !whole_heap_collection_ && |
| 115 | (space == GetHeap()->GetNonMovingSpace() || |
| 116 | space == GetHeap()->GetPrimaryFreeListSpace()))) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 117 | ImmuneSpace(space); |
| 118 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 121 | if (generational_ && !whole_heap_collection_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 122 | // We won't collect the large object space if a bump pointer space only collection. |
| 123 | is_large_object_space_immune_ = true; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 124 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 125 | timings_.EndSplit(); |
| 126 | } |
| 127 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 128 | SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 129 | : GarbageCollector(heap, |
| 130 | name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"), |
| 131 | mark_stack_(nullptr), |
| 132 | immune_begin_(nullptr), |
| 133 | immune_end_(nullptr), |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 134 | is_large_object_space_immune_(false), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 135 | to_space_(nullptr), |
| 136 | from_space_(nullptr), |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 137 | self_(nullptr), |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 138 | generational_(generational), |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 139 | last_gc_to_space_end_(nullptr), |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 140 | bytes_promoted_(0), |
| 141 | whole_heap_collection_(true), |
| 142 | whole_heap_collection_interval_counter_(0) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void SemiSpace::InitializePhase() { |
| 146 | timings_.Reset(); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 147 | TimingLogger::ScopedSplit split("InitializePhase", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 148 | mark_stack_ = heap_->mark_stack_.get(); |
| 149 | DCHECK(mark_stack_ != nullptr); |
| 150 | immune_begin_ = nullptr; |
| 151 | immune_end_ = nullptr; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 152 | is_large_object_space_immune_ = false; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 153 | self_ = Thread::Current(); |
| 154 | // Do any pre GC verification. |
| 155 | timings_.NewSplit("PreGcVerification"); |
| 156 | heap_->PreGcVerification(this); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 157 | // Set the initial bitmap. |
| 158 | to_space_live_bitmap_ = to_space_->GetLiveBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void SemiSpace::ProcessReferences(Thread* self) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 162 | TimingLogger::ScopedSplit split("ProcessReferences", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 163 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 164 | GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback, |
| 165 | &RecursiveMarkObjectCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void SemiSpace::MarkingPhase() { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 169 | if (generational_) { |
| 170 | if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc || |
| 171 | clear_soft_references_) { |
| 172 | // If an explicit, native allocation-triggered, or last attempt |
| 173 | // collection, collect the whole heap (and reset the interval |
| 174 | // counter to be consistent.) |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 175 | whole_heap_collection_ = true; |
| 176 | whole_heap_collection_interval_counter_ = 0; |
| 177 | } |
| 178 | if (whole_heap_collection_) { |
| 179 | VLOG(heap) << "Whole heap collection"; |
| 180 | } else { |
| 181 | VLOG(heap) << "Bump pointer space only collection"; |
| 182 | } |
| 183 | } |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 184 | Locks::mutator_lock_->AssertExclusiveHeld(self_); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 185 | |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 186 | TimingLogger::ScopedSplit split("MarkingPhase", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 187 | // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the |
| 188 | // wrong space. |
| 189 | heap_->SwapSemiSpaces(); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 190 | if (generational_) { |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 191 | // If last_gc_to_space_end_ is out of the bounds of the from-space |
| 192 | // (the to-space from last GC), then point it to the beginning of |
| 193 | // the from-space. For example, the very first GC or the |
| 194 | // pre-zygote compaction. |
| 195 | if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) { |
| 196 | last_gc_to_space_end_ = from_space_->Begin(); |
| 197 | } |
| 198 | // Reset this before the marking starts below. |
| 199 | bytes_promoted_ = 0; |
| 200 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 201 | // Assume the cleared space is already empty. |
| 202 | BindBitmaps(); |
| 203 | // Process dirty cards and add dirty cards to mod-union tables. |
| 204 | heap_->ProcessCards(timings_); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 205 | // Clear the whole card table since we can not get any additional dirty cards during the |
| 206 | // paused GC. This saves memory but only works for pause the world collectors. |
| 207 | timings_.NewSplit("ClearCardTable"); |
| 208 | heap_->GetCardTable()->ClearCardTable(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 209 | // Need to do this before the checkpoint since we don't want any threads to add references to |
| 210 | // the live stack during the recursive mark. |
| 211 | timings_.NewSplit("SwapStacks"); |
| 212 | heap_->SwapStacks(); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 213 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 214 | MarkRoots(); |
| 215 | // Mark roots of immune spaces. |
| 216 | UpdateAndMarkModUnion(); |
| 217 | // Recursively mark remaining objects. |
| 218 | MarkReachableObjects(); |
| 219 | } |
| 220 | |
| 221 | bool SemiSpace::IsImmuneSpace(const space::ContinuousSpace* space) const { |
| 222 | return |
| 223 | immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) && |
| 224 | immune_end_ >= reinterpret_cast<Object*>(space->End()); |
| 225 | } |
| 226 | |
| 227 | void SemiSpace::UpdateAndMarkModUnion() { |
| 228 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 229 | // If the space is immune then we need to mark the references to other spaces. |
| 230 | if (IsImmuneSpace(space)) { |
| 231 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 232 | if (table != nullptr) { |
| 233 | // TODO: Improve naming. |
| 234 | TimingLogger::ScopedSplit split( |
| 235 | space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" : |
| 236 | "UpdateAndMarkImageModUnionTable", |
| 237 | &timings_); |
| 238 | table->UpdateAndMarkReferences(MarkRootCallback, this); |
| 239 | } else { |
| 240 | // If a bump pointer space only collection, the non-moving |
| 241 | // space is added to the immune space. But the non-moving |
| 242 | // space doesn't have a mod union table. Instead, its live |
| 243 | // bitmap will be scanned later in MarkReachableObjects(). |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 244 | DCHECK(generational_ && !whole_heap_collection_ && |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 245 | (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace())); |
| 246 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 251 | class SemiSpaceScanObjectVisitor { |
| 252 | public: |
| 253 | explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {} |
| 254 | void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
| 255 | // TODO: fix NO_THREAD_SAFETY_ANALYSIS. ScanObject() requires an |
| 256 | // exclusive lock on the mutator lock, but |
| 257 | // SpaceBitmap::VisitMarkedRange() only requires the shared lock. |
| 258 | DCHECK(obj != nullptr); |
| 259 | semi_space_->ScanObject(obj); |
| 260 | } |
| 261 | private: |
| 262 | SemiSpace* semi_space_; |
| 263 | }; |
| 264 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 265 | void SemiSpace::MarkReachableObjects() { |
| 266 | timings_.StartSplit("MarkStackAsLive"); |
| 267 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 268 | heap_->MarkAllocStackAsLive(live_stack); |
| 269 | live_stack->Reset(); |
| 270 | timings_.EndSplit(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 271 | |
| 272 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 273 | // If the space is immune and has no mod union table (the |
| 274 | // non-moving space when the bump pointer space only collection is |
| 275 | // enabled,) then we need to scan its live bitmap as roots |
| 276 | // (including the objects on the live stack which have just marked |
| 277 | // in the live bitmap above in MarkAllocStackAsLive().) |
| 278 | if (IsImmuneSpace(space) && heap_->FindModUnionTableFromSpace(space) == nullptr) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 279 | DCHECK(generational_ && !whole_heap_collection_ && |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 280 | (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace())); |
| 281 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 282 | SemiSpaceScanObjectVisitor visitor(this); |
| 283 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 284 | reinterpret_cast<uintptr_t>(space->End()), |
| 285 | visitor); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (is_large_object_space_immune_) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 290 | DCHECK(generational_ && !whole_heap_collection_); |
Hiroshi Yamauchi | ba5870d | 2014-01-29 15:31:03 -0800 | [diff] [blame] | 291 | // Delay copying the live set to the marked set until here from |
| 292 | // BindBitmaps() as the large objects on the allocation stack may |
| 293 | // be newly added to the live set above in MarkAllocStackAsLive(). |
| 294 | GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked(); |
| 295 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 296 | // When the large object space is immune, we need to scan the |
| 297 | // large object space as roots as they contain references to their |
| 298 | // classes (primitive array classes) that could move though they |
| 299 | // don't contain any other references. |
| 300 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 301 | accounting::ObjectSet* large_live_objects = large_object_space->GetLiveObjects(); |
| 302 | SemiSpaceScanObjectVisitor visitor(this); |
| 303 | for (const Object* obj : large_live_objects->GetObjects()) { |
| 304 | visitor(const_cast<Object*>(obj)); |
| 305 | } |
| 306 | } |
| 307 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 308 | // Recursively process the mark stack. |
| 309 | ProcessMarkStack(true); |
| 310 | } |
| 311 | |
| 312 | void SemiSpace::ReclaimPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 313 | TimingLogger::ScopedSplit split("ReclaimPhase", &timings_); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 314 | ProcessReferences(self_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 315 | { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 316 | ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 317 | SweepSystemWeaks(); |
| 318 | } |
| 319 | // Record freed memory. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 320 | uint64_t from_bytes = from_space_->GetBytesAllocated(); |
| 321 | uint64_t to_bytes = to_space_->GetBytesAllocated(); |
| 322 | uint64_t from_objects = from_space_->GetObjectsAllocated(); |
| 323 | uint64_t to_objects = to_space_->GetObjectsAllocated(); |
| 324 | CHECK_LE(to_objects, from_objects); |
| 325 | int64_t freed_bytes = from_bytes - to_bytes; |
| 326 | int64_t freed_objects = from_objects - to_objects; |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 327 | freed_bytes_.FetchAndAdd(freed_bytes); |
| 328 | freed_objects_.FetchAndAdd(freed_objects); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 329 | // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed |
| 330 | // space. |
| 331 | heap_->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 332 | timings_.StartSplit("PreSweepingGcVerification"); |
| 333 | heap_->PreSweepingGcVerification(this); |
| 334 | timings_.EndSplit(); |
| 335 | |
| 336 | { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 337 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 338 | // Reclaim unmarked objects. |
| 339 | Sweep(false); |
| 340 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 341 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 342 | // bitmaps. |
| 343 | timings_.StartSplit("SwapBitmaps"); |
| 344 | SwapBitmaps(); |
| 345 | timings_.EndSplit(); |
| 346 | // Unbind the live and mark bitmaps. |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 347 | TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_); |
| 348 | GetHeap()->UnBindBitmaps(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 349 | } |
| 350 | // Release the memory used by the from space. |
| 351 | if (kResetFromSpace) { |
| 352 | // Clearing from space. |
| 353 | from_space_->Clear(); |
| 354 | } |
| 355 | // Protect the from space. |
| 356 | VLOG(heap) |
| 357 | << "mprotect region " << reinterpret_cast<void*>(from_space_->Begin()) << " - " |
| 358 | << reinterpret_cast<void*>(from_space_->Limit()); |
| 359 | if (kProtectFromSpace) { |
| 360 | mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_NONE); |
| 361 | } else { |
| 362 | mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_READ); |
| 363 | } |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 364 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 365 | if (generational_) { |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 366 | // Record the end (top) of the to space so we can distinguish |
| 367 | // between objects that were allocated since the last GC and the |
| 368 | // older objects. |
| 369 | last_gc_to_space_end_ = to_space_->End(); |
| 370 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void SemiSpace::ResizeMarkStack(size_t new_size) { |
| 374 | std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End()); |
| 375 | CHECK_LE(mark_stack_->Size(), new_size); |
| 376 | mark_stack_->Resize(new_size); |
| 377 | for (const auto& obj : temp) { |
| 378 | mark_stack_->PushBack(obj); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | inline void SemiSpace::MarkStackPush(Object* obj) { |
| 383 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 384 | ResizeMarkStack(mark_stack_->Capacity() * 2); |
| 385 | } |
| 386 | // The object must be pushed on to the mark stack. |
| 387 | mark_stack_->PushBack(obj); |
| 388 | } |
| 389 | |
| 390 | // Rare case, probably not worth inlining since it will increase instruction cache miss rate. |
| 391 | bool SemiSpace::MarkLargeObject(const Object* obj) { |
| 392 | // TODO: support >1 discontinuous space. |
| 393 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 394 | DCHECK(large_object_space->Contains(obj)); |
Mathieu Chartier | db7f37d | 2014-01-10 11:09:06 -0800 | [diff] [blame] | 395 | accounting::ObjectSet* large_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 396 | if (UNLIKELY(!large_objects->Test(obj))) { |
| 397 | large_objects->Set(obj); |
| 398 | return true; |
| 399 | } |
| 400 | return false; |
| 401 | } |
| 402 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 403 | mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) { |
| 404 | size_t object_size = obj->SizeOf(); |
Mathieu Chartier | 5dc08a6 | 2014-01-10 10:10:23 -0800 | [diff] [blame] | 405 | size_t bytes_allocated; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 406 | mirror::Object* forward_address = nullptr; |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 407 | if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 408 | // If it's allocated before the last GC (older), move |
| 409 | // (pseudo-promote) it to the main free list space (as sort |
| 410 | // of an old generation.) |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 411 | size_t bytes_promoted; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 412 | space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace(); |
| 413 | forward_address = promo_dest_space->Alloc(self_, object_size, &bytes_promoted); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 414 | if (forward_address == nullptr) { |
| 415 | // If out of space, fall back to the to-space. |
| 416 | forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated); |
| 417 | } else { |
| 418 | GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted); |
| 419 | bytes_promoted_ += bytes_promoted; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 420 | // Handle the bitmaps marking. |
| 421 | accounting::SpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 422 | DCHECK(live_bitmap != nullptr); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 423 | accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 424 | DCHECK(mark_bitmap != nullptr); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 425 | DCHECK(!live_bitmap->Test(forward_address)); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 426 | if (!whole_heap_collection_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 427 | // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap. |
| 428 | DCHECK_EQ(live_bitmap, mark_bitmap); |
| 429 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 430 | // If a bump pointer space only collection, delay the live |
| 431 | // bitmap marking of the promoted object until it's popped off |
| 432 | // the mark stack (ProcessMarkStack()). The rationale: we may |
| 433 | // be in the middle of scanning the objects in the promo |
| 434 | // destination space for |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 435 | // non-moving-space-to-bump-pointer-space references by |
| 436 | // iterating over the marked bits of the live bitmap |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 437 | // (MarkReachableObjects()). If we don't delay it (and instead |
| 438 | // mark the promoted object here), the above promo destination |
| 439 | // space scan could encounter the just-promoted object and |
| 440 | // forward the references in the promoted object's fields even |
| 441 | // through it is pushed onto the mark stack. If this happens, |
| 442 | // the promoted object would be in an inconsistent state, that |
| 443 | // is, it's on the mark stack (gray) but its fields are |
| 444 | // already forwarded (black), which would cause a |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 445 | // DCHECK(!to_space_->HasAddress(obj)) failure below. |
| 446 | } else { |
| 447 | // Mark forward_address on the live bit map. |
| 448 | live_bitmap->Set(forward_address); |
| 449 | // Mark forward_address on the mark bit map. |
| 450 | DCHECK(!mark_bitmap->Test(forward_address)); |
| 451 | mark_bitmap->Set(forward_address); |
| 452 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 453 | } |
| 454 | DCHECK(forward_address != nullptr); |
| 455 | } else { |
| 456 | // If it's allocated after the last GC (younger), copy it to the to-space. |
| 457 | forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated); |
| 458 | } |
| 459 | // Copy over the object and add it to the mark stack since we still need to update its |
| 460 | // references. |
| 461 | memcpy(reinterpret_cast<void*>(forward_address), obj, object_size); |
| 462 | if (to_space_live_bitmap_ != nullptr) { |
| 463 | to_space_live_bitmap_->Set(forward_address); |
| 464 | } |
Mathieu Chartier | 5dc08a6 | 2014-01-10 10:10:23 -0800 | [diff] [blame] | 465 | DCHECK(to_space_->HasAddress(forward_address) || |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 466 | (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address))); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 467 | return forward_address; |
| 468 | } |
| 469 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 470 | // Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to |
| 471 | // the to-space and have their forward address updated. Objects which have been newly marked are |
| 472 | // pushed on the mark stack. |
| 473 | Object* SemiSpace::MarkObject(Object* obj) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 474 | Object* forward_address = obj; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 475 | if (obj != nullptr && !IsImmune(obj)) { |
| 476 | if (from_space_->HasAddress(obj)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 477 | forward_address = GetForwardingAddressInFromSpace(obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 478 | // If the object has already been moved, return the new forward address. |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 479 | if (forward_address == nullptr) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 480 | forward_address = MarkNonForwardedObject(obj); |
| 481 | DCHECK(forward_address != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 482 | // Make sure to only update the forwarding address AFTER you copy the object so that the |
| 483 | // monitor word doesn't get stomped over. |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 484 | obj->SetLockWord(LockWord::FromForwardingAddress( |
| 485 | reinterpret_cast<size_t>(forward_address))); |
| 486 | // Push the object onto the mark stack for later processing. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 487 | MarkStackPush(forward_address); |
| 488 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 489 | // TODO: Do we need this if in the else statement? |
| 490 | } else { |
| 491 | accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj); |
| 492 | if (LIKELY(object_bitmap != nullptr)) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 493 | if (generational_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 494 | // If a bump pointer space only collection, we should not |
| 495 | // reach here as we don't/won't mark the objects in the |
| 496 | // non-moving space (except for the promoted objects.) Note |
| 497 | // the non-moving space is added to the immune space. |
| 498 | DCHECK(whole_heap_collection_); |
| 499 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 500 | // This object was not previously marked. |
| 501 | if (!object_bitmap->Test(obj)) { |
| 502 | object_bitmap->Set(obj); |
| 503 | MarkStackPush(obj); |
| 504 | } |
| 505 | } else { |
Mathieu Chartier | d1e05bf | 2014-02-04 17:11:58 -0800 | [diff] [blame] | 506 | CHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 507 | if (MarkLargeObject(obj)) { |
| 508 | MarkStackPush(obj); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 513 | return forward_address; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 516 | Object* SemiSpace::RecursiveMarkObjectCallback(Object* root, void* arg) { |
| 517 | DCHECK(root != nullptr); |
| 518 | DCHECK(arg != nullptr); |
| 519 | SemiSpace* semi_space = reinterpret_cast<SemiSpace*>(arg); |
| 520 | mirror::Object* ret = semi_space->MarkObject(root); |
| 521 | semi_space->ProcessMarkStack(true); |
| 522 | return ret; |
| 523 | } |
| 524 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 525 | Object* SemiSpace::MarkRootCallback(Object* root, void* arg) { |
| 526 | DCHECK(root != nullptr); |
| 527 | DCHECK(arg != nullptr); |
| 528 | return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root); |
| 529 | } |
| 530 | |
| 531 | // Marks all objects in the root set. |
| 532 | void SemiSpace::MarkRoots() { |
| 533 | timings_.StartSplit("MarkRoots"); |
| 534 | // TODO: Visit up image roots as well? |
| 535 | Runtime::Current()->VisitRoots(MarkRootCallback, this, false, true); |
| 536 | timings_.EndSplit(); |
| 537 | } |
| 538 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 539 | mirror::Object* SemiSpace::MarkedForwardingAddressCallback(Object* object, void* arg) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 540 | return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object); |
| 541 | } |
| 542 | |
| 543 | void SemiSpace::SweepSystemWeaks() { |
| 544 | timings_.StartSplit("SweepSystemWeaks"); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 545 | Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 546 | timings_.EndSplit(); |
| 547 | } |
| 548 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 549 | bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 550 | return space != from_space_ && space != to_space_ && !IsImmuneSpace(space); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void SemiSpace::Sweep(bool swap_bitmaps) { |
| 554 | DCHECK(mark_stack_->IsEmpty()); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 555 | TimingLogger::ScopedSplit("Sweep", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 556 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 557 | if (space->IsContinuousMemMapAllocSpace()) { |
| 558 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
| 559 | if (!ShouldSweepSpace(alloc_space)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 560 | continue; |
| 561 | } |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 562 | TimingLogger::ScopedSplit split( |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 563 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_); |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 564 | size_t freed_objects = 0; |
| 565 | size_t freed_bytes = 0; |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 566 | alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 567 | heap_->RecordFree(freed_objects, freed_bytes); |
| 568 | freed_objects_.FetchAndAdd(freed_objects); |
| 569 | freed_bytes_.FetchAndAdd(freed_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 572 | if (!is_large_object_space_immune_) { |
| 573 | SweepLargeObjects(swap_bitmaps); |
| 574 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void SemiSpace::SweepLargeObjects(bool swap_bitmaps) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 578 | DCHECK(!is_large_object_space_immune_); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 579 | TimingLogger::ScopedSplit("SweepLargeObjects", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 580 | size_t freed_objects = 0; |
| 581 | size_t freed_bytes = 0; |
Mathieu Chartier | db7f37d | 2014-01-10 11:09:06 -0800 | [diff] [blame] | 582 | GetHeap()->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 583 | freed_large_objects_.FetchAndAdd(freed_objects); |
| 584 | freed_large_object_bytes_.FetchAndAdd(freed_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 585 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
| 586 | } |
| 587 | |
| 588 | // Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been |
| 589 | // marked, put it on the appropriate list in the heap for later processing. |
| 590 | void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 591 | heap_->DelayReferenceReferent(klass, obj, MarkedForwardingAddressCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Visit all of the references of an object and update. |
| 595 | void SemiSpace::ScanObject(Object* obj) { |
| 596 | DCHECK(obj != NULL); |
| 597 | DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space"; |
| 598 | MarkSweep::VisitObjectReferences(obj, [this](Object* obj, Object* ref, const MemberOffset& offset, |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 599 | bool /* is_static */) ALWAYS_INLINE_LAMBDA NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 600 | mirror::Object* new_address = MarkObject(ref); |
| 601 | if (new_address != ref) { |
| 602 | DCHECK(new_address != nullptr); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 603 | // Don't need to mark the card since we updating the object address and not changing the |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 604 | // actual objects its pointing to. Using SetFieldObjectWithoutWriteBarrier is better in this |
| 605 | // case since it does not dirty cards and use additional memory. |
| 606 | obj->SetFieldObjectWithoutWriteBarrier(offset, new_address, false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 607 | } |
| 608 | }, kMovingClasses); |
| 609 | mirror::Class* klass = obj->GetClass(); |
| 610 | if (UNLIKELY(klass->IsReferenceClass())) { |
| 611 | DelayReferenceReferent(klass, obj); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Scan anything that's on the mark stack. |
| 616 | void SemiSpace::ProcessMarkStack(bool paused) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 617 | space::MallocSpace* promo_dest_space = NULL; |
| 618 | accounting::SpaceBitmap* live_bitmap = NULL; |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 619 | if (generational_ && !whole_heap_collection_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 620 | // If a bump pointer space only collection (and the promotion is |
| 621 | // enabled,) we delay the live-bitmap marking of promoted objects |
| 622 | // from MarkObject() until this function. |
| 623 | promo_dest_space = GetHeap()->GetPrimaryFreeListSpace(); |
| 624 | live_bitmap = promo_dest_space->GetLiveBitmap(); |
| 625 | DCHECK(live_bitmap != nullptr); |
| 626 | accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap(); |
| 627 | DCHECK(mark_bitmap != nullptr); |
| 628 | DCHECK_EQ(live_bitmap, mark_bitmap); |
| 629 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 630 | timings_.StartSplit(paused ? "(paused)ProcessMarkStack" : "ProcessMarkStack"); |
| 631 | while (!mark_stack_->IsEmpty()) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 632 | Object* obj = mark_stack_->PopBack(); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 633 | if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 634 | // obj has just been promoted. Mark the live bitmap for it, |
| 635 | // which is delayed from MarkObject(). |
| 636 | DCHECK(!live_bitmap->Test(obj)); |
| 637 | live_bitmap->Set(obj); |
| 638 | } |
| 639 | ScanObject(obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 640 | } |
| 641 | timings_.EndSplit(); |
| 642 | } |
| 643 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 644 | inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const |
| 645 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 646 | // All immune objects are assumed marked. |
| 647 | if (IsImmune(obj)) { |
| 648 | return obj; |
| 649 | } |
| 650 | if (from_space_->HasAddress(obj)) { |
| 651 | mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj)); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 652 | return forwarding_address; // Returns either the forwarding address or nullptr. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 653 | } else if (to_space_->HasAddress(obj)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 654 | // Should be unlikely. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 655 | // Already forwarded, must be marked. |
| 656 | return obj; |
| 657 | } |
| 658 | return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr; |
| 659 | } |
| 660 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 661 | void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) { |
| 662 | DCHECK(to_space != nullptr); |
| 663 | to_space_ = to_space; |
| 664 | } |
| 665 | |
| 666 | void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) { |
| 667 | DCHECK(from_space != nullptr); |
| 668 | from_space_ = from_space; |
| 669 | } |
| 670 | |
| 671 | void SemiSpace::FinishPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 672 | TimingLogger::ScopedSplit split("FinishPhase", &timings_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 673 | Heap* heap = GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 674 | timings_.NewSplit("PostGcVerification"); |
| 675 | heap->PostGcVerification(this); |
| 676 | |
| 677 | // Null the "to" and "from" spaces since compacting from one to the other isn't valid until |
| 678 | // further action is done by the heap. |
| 679 | to_space_ = nullptr; |
| 680 | from_space_ = nullptr; |
| 681 | |
| 682 | // Update the cumulative statistics |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 683 | total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects(); |
| 684 | total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes(); |
| 685 | |
| 686 | // Ensure that the mark stack is empty. |
| 687 | CHECK(mark_stack_->IsEmpty()); |
| 688 | |
| 689 | // Update the cumulative loggers. |
| 690 | cumulative_timings_.Start(); |
| 691 | cumulative_timings_.AddLogger(timings_); |
| 692 | cumulative_timings_.End(); |
| 693 | |
| 694 | // Clear all of the spaces' mark bitmaps. |
| 695 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
| 696 | accounting::SpaceBitmap* bitmap = space->GetMarkBitmap(); |
| 697 | if (bitmap != nullptr && |
| 698 | space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) { |
| 699 | bitmap->Clear(); |
| 700 | } |
| 701 | } |
| 702 | mark_stack_->Reset(); |
| 703 | |
| 704 | // Reset the marked large objects. |
| 705 | space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
| 706 | large_objects->GetMarkObjects()->Clear(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 707 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 708 | if (generational_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 709 | // Decide whether to do a whole heap collection or a bump pointer |
| 710 | // only space collection at the next collection by updating |
| 711 | // whole_heap_collection. Enable whole_heap_collection once every |
| 712 | // kDefaultWholeHeapCollectionInterval collections. |
| 713 | if (!whole_heap_collection_) { |
| 714 | --whole_heap_collection_interval_counter_; |
| 715 | DCHECK_GE(whole_heap_collection_interval_counter_, 0); |
| 716 | if (whole_heap_collection_interval_counter_ == 0) { |
| 717 | whole_heap_collection_ = true; |
| 718 | } |
| 719 | } else { |
| 720 | DCHECK_EQ(whole_heap_collection_interval_counter_, 0); |
| 721 | whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval; |
| 722 | whole_heap_collection_ = false; |
| 723 | } |
| 724 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | } // namespace collector |
| 728 | } // namespace gc |
| 729 | } // namespace art |