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