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