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 | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 17 | #include "semi_space-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 18 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 19 | #include <climits> |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <numeric> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 22 | #include <sstream> |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #include "base/logging.h" |
| 26 | #include "base/macros.h" |
| 27 | #include "base/mutex-inl.h" |
| 28 | #include "base/timing_logger.h" |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 29 | #include "gc/accounting/heap_bitmap-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 30 | #include "gc/accounting/mod_union_table.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 31 | #include "gc/accounting/remembered_set.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 32 | #include "gc/accounting/space_bitmap-inl.h" |
| 33 | #include "gc/heap.h" |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 34 | #include "gc/reference_processor.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 35 | #include "gc/space/bump_pointer_space.h" |
| 36 | #include "gc/space/bump_pointer_space-inl.h" |
| 37 | #include "gc/space/image_space.h" |
| 38 | #include "gc/space/large_object_space.h" |
| 39 | #include "gc/space/space-inl.h" |
| 40 | #include "indirect_reference_table.h" |
| 41 | #include "intern_table.h" |
| 42 | #include "jni_internal.h" |
| 43 | #include "mark_sweep-inl.h" |
| 44 | #include "monitor.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 45 | #include "mirror/reference-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 46 | #include "mirror/object-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | #include "runtime.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 48 | #include "thread-inl.h" |
| 49 | #include "thread_list.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 50 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 51 | using ::art::mirror::Object; |
| 52 | |
| 53 | namespace art { |
| 54 | namespace gc { |
| 55 | namespace collector { |
| 56 | |
| 57 | static constexpr bool kProtectFromSpace = true; |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 58 | static constexpr bool kStoreStackTraces = false; |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 59 | static constexpr size_t kBytesPromotedThreshold = 4 * MB; |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 60 | static constexpr size_t kLargeObjectBytesAllocatedThreshold = 16 * MB; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 61 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 62 | void SemiSpace::BindBitmaps() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 63 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 64 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 65 | // Mark all of the spaces we never collect as immune. |
| 66 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 67 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect || |
| 68 | space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) { |
| 69 | CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space; |
| 70 | } else if (space->GetLiveBitmap() != nullptr) { |
| 71 | if (space == to_space_ || collect_from_space_only_) { |
| 72 | if (collect_from_space_only_) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 73 | // Bind the bitmaps of the main free list space and the non-moving space we are doing a |
| 74 | // bump pointer space only collection. |
| 75 | CHECK(space == GetHeap()->GetPrimaryFreeListSpace() || |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 76 | space == GetHeap()->GetNonMovingSpace()); |
| 77 | } |
| 78 | CHECK(space->IsContinuousMemMapAllocSpace()); |
| 79 | space->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 80 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 83 | if (collect_from_space_only_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 84 | // We won't collect the large object space if a bump pointer space only collection. |
| 85 | is_large_object_space_immune_ = true; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -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 | SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 90 | : GarbageCollector(heap, |
| 91 | name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"), |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame] | 92 | mark_stack_(nullptr), |
| 93 | is_large_object_space_immune_(false), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 94 | to_space_(nullptr), |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame] | 95 | to_space_live_bitmap_(nullptr), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 96 | from_space_(nullptr), |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame] | 97 | mark_bitmap_(nullptr), |
| 98 | self_(nullptr), |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 99 | generational_(generational), |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 100 | last_gc_to_space_end_(nullptr), |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 101 | bytes_promoted_(0), |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 102 | bytes_promoted_since_last_whole_heap_collection_(0), |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 103 | large_object_bytes_allocated_at_last_whole_heap_collection_(0), |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 104 | collect_from_space_only_(generational), |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame] | 105 | promo_dest_space_(nullptr), |
| 106 | fallback_space_(nullptr), |
| 107 | bytes_moved_(0U), |
| 108 | objects_moved_(0U), |
| 109 | saved_bytes_(0U), |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 110 | collector_name_(name_), |
| 111 | swap_semi_spaces_(true) { |
| 112 | } |
| 113 | |
| 114 | void SemiSpace::RunPhases() { |
| 115 | Thread* self = Thread::Current(); |
| 116 | InitializePhase(); |
| 117 | // Semi-space collector is special since it is sometimes called with the mutators suspended |
| 118 | // during the zygote creation and collector transitions. If we already exclusively hold the |
| 119 | // mutator lock, then we can't lock it again since it will cause a deadlock. |
| 120 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 121 | GetHeap()->PreGcVerificationPaused(this); |
| 122 | GetHeap()->PrePauseRosAllocVerification(this); |
| 123 | MarkingPhase(); |
| 124 | ReclaimPhase(); |
| 125 | GetHeap()->PostGcVerificationPaused(this); |
| 126 | } else { |
| 127 | Locks::mutator_lock_->AssertNotHeld(self); |
| 128 | { |
| 129 | ScopedPause pause(this); |
| 130 | GetHeap()->PreGcVerificationPaused(this); |
| 131 | GetHeap()->PrePauseRosAllocVerification(this); |
| 132 | MarkingPhase(); |
| 133 | } |
| 134 | { |
| 135 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 136 | ReclaimPhase(); |
| 137 | } |
| 138 | GetHeap()->PostGcVerification(this); |
| 139 | } |
| 140 | FinishPhase(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void SemiSpace::InitializePhase() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 144 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 145 | mark_stack_ = heap_->GetMarkStack(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 146 | DCHECK(mark_stack_ != nullptr); |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 147 | immune_region_.Reset(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 148 | is_large_object_space_immune_ = false; |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 149 | saved_bytes_ = 0; |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 150 | bytes_moved_ = 0; |
| 151 | objects_moved_ = 0; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 152 | self_ = Thread::Current(); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 153 | CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 154 | // Set the initial bitmap. |
| 155 | to_space_live_bitmap_ = to_space_->GetLiveBitmap(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 156 | { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 157 | // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap. |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 158 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 159 | mark_bitmap_ = heap_->GetMarkBitmap(); |
| 160 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 161 | if (generational_) { |
| 162 | promo_dest_space_ = GetHeap()->GetPrimaryFreeListSpace(); |
| 163 | } |
| 164 | fallback_space_ = GetHeap()->GetNonMovingSpace(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void SemiSpace::ProcessReferences(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 168 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 169 | GetHeap()->GetReferenceProcessor()->ProcessReferences( |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 170 | false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void SemiSpace::MarkingPhase() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 174 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 175 | CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_)); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 176 | if (kStoreStackTraces) { |
| 177 | Locks::mutator_lock_->AssertExclusiveHeld(self_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 178 | // Store the stack traces into the runtime fault string in case we Get a heap corruption |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 179 | // related crash later. |
| 180 | ThreadState old_state = self_->SetStateUnsafe(kRunnable); |
| 181 | std::ostringstream oss; |
| 182 | Runtime* runtime = Runtime::Current(); |
| 183 | runtime->GetThreadList()->DumpForSigQuit(oss); |
| 184 | runtime->GetThreadList()->DumpNativeStacks(oss); |
| 185 | runtime->SetFaultMessage(oss.str()); |
| 186 | CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable); |
| 187 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 188 | // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps |
| 189 | // to prevent fragmentation. |
| 190 | RevokeAllThreadLocalBuffers(); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 191 | if (generational_) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 192 | if (GetCurrentIteration()->GetGcCause() == kGcCauseExplicit || |
| 193 | GetCurrentIteration()->GetGcCause() == kGcCauseForNativeAlloc || |
| 194 | GetCurrentIteration()->GetClearSoftReferences()) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 195 | // If an explicit, native allocation-triggered, or last attempt |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 196 | // collection, collect the whole heap. |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 197 | collect_from_space_only_ = false; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 198 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 199 | if (!collect_from_space_only_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 200 | VLOG(heap) << "Whole heap collection"; |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 201 | name_ = collector_name_ + " whole"; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 202 | } else { |
| 203 | VLOG(heap) << "Bump pointer space only collection"; |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 204 | name_ = collector_name_ + " bps"; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 207 | |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 208 | if (!collect_from_space_only_) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 209 | // If non-generational, always clear soft references. |
| 210 | // If generational, clear soft references if a whole heap collection. |
| 211 | GetCurrentIteration()->SetClearSoftReferences(true); |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 212 | } |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 213 | Locks::mutator_lock_->AssertExclusiveHeld(self_); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 214 | if (generational_) { |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 215 | // If last_gc_to_space_end_ is out of the bounds of the from-space |
| 216 | // (the to-space from last GC), then point it to the beginning of |
| 217 | // the from-space. For example, the very first GC or the |
| 218 | // pre-zygote compaction. |
| 219 | if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) { |
| 220 | last_gc_to_space_end_ = from_space_->Begin(); |
| 221 | } |
| 222 | // Reset this before the marking starts below. |
| 223 | bytes_promoted_ = 0; |
| 224 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 225 | // Assume the cleared space is already empty. |
| 226 | BindBitmaps(); |
| 227 | // Process dirty cards and add dirty cards to mod-union tables. |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 228 | heap_->ProcessCards(GetTimings(), kUseRememberedSet && generational_, false, true); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 229 | // Clear the whole card table since we can not Get any additional dirty cards during the |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 230 | // paused GC. This saves memory but only works for pause the world collectors. |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 231 | t.NewTiming("ClearCardTable"); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 232 | heap_->GetCardTable()->ClearCardTable(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 233 | // Need to do this before the checkpoint since we don't want any threads to add references to |
| 234 | // the live stack during the recursive mark. |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 235 | if (kUseThreadLocalAllocationStack) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 236 | TimingLogger::ScopedTiming t2("RevokeAllThreadLocalAllocationStacks", GetTimings()); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 237 | heap_->RevokeAllThreadLocalAllocationStacks(self_); |
| 238 | } |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 239 | heap_->SwapStacks(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 240 | { |
| 241 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
| 242 | MarkRoots(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 243 | // Recursively mark remaining objects. |
| 244 | MarkReachableObjects(); |
| 245 | } |
| 246 | ProcessReferences(self_); |
| 247 | { |
| 248 | ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
| 249 | SweepSystemWeaks(); |
| 250 | } |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame^] | 251 | Runtime::Current()->GetClassLinker()->CleanupClassLoaders(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 252 | // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked |
| 253 | // before they are properly counted. |
| 254 | RevokeAllThreadLocalBuffers(); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 255 | GetHeap()->RecordFreeRevoke(); // this is for the non-moving rosalloc space used by GSS. |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 256 | // Record freed memory. |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 257 | const int64_t from_bytes = from_space_->GetBytesAllocated(); |
| 258 | const int64_t to_bytes = bytes_moved_; |
| 259 | const uint64_t from_objects = from_space_->GetObjectsAllocated(); |
| 260 | const uint64_t to_objects = objects_moved_; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 261 | CHECK_LE(to_objects, from_objects); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 262 | // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed |
| 263 | // space. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 264 | RecordFree(ObjectBytePair(from_objects - to_objects, from_bytes - to_bytes)); |
Hiroshi Yamauchi | 312baf1 | 2015-01-12 12:11:05 -0800 | [diff] [blame] | 265 | // Clear and protect the from space. |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 266 | from_space_->Clear(); |
Hiroshi Yamauchi | 312baf1 | 2015-01-12 12:11:05 -0800 | [diff] [blame] | 267 | if (kProtectFromSpace && !from_space_->IsRosAllocSpace()) { |
| 268 | // Protect with PROT_NONE. |
| 269 | VLOG(heap) << "Protecting from_space_ : " << *from_space_; |
| 270 | from_space_->GetMemMap()->Protect(PROT_NONE); |
| 271 | } else { |
| 272 | // If RosAllocSpace, we'll leave it as PROT_READ here so the |
| 273 | // rosaloc verification can read the metadata magic number and |
| 274 | // protect it with PROT_NONE later in FinishPhase(). |
| 275 | VLOG(heap) << "Protecting from_space_ with PROT_READ : " << *from_space_; |
| 276 | from_space_->GetMemMap()->Protect(PROT_READ); |
| 277 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 278 | heap_->PreSweepingGcVerification(this); |
Mathieu Chartier | 4240c51 | 2014-05-27 10:10:11 -0700 | [diff] [blame] | 279 | if (swap_semi_spaces_) { |
| 280 | heap_->SwapSemiSpaces(); |
| 281 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 284 | class SemiSpaceScanObjectVisitor { |
| 285 | public: |
| 286 | explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {} |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 287 | void operator()(Object* obj) const REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 288 | DCHECK(obj != nullptr); |
| 289 | semi_space_->ScanObject(obj); |
| 290 | } |
| 291 | private: |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 292 | SemiSpace* const semi_space_; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 293 | }; |
| 294 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 295 | // Used to verify that there's no references to the from-space. |
| 296 | class SemiSpaceVerifyNoFromSpaceReferencesVisitor { |
| 297 | public: |
| 298 | explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) : |
| 299 | from_space_(from_space) {} |
| 300 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 301 | void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 302 | SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 303 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 304 | if (from_space_->HasAddress(ref)) { |
| 305 | Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 306 | LOG(FATAL) << ref << " found in from space"; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 307 | } |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 308 | } |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 309 | |
| 310 | // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors. |
| 311 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 312 | NO_THREAD_SAFETY_ANALYSIS { |
| 313 | if (!root->IsNull()) { |
| 314 | VisitRoot(root); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 319 | NO_THREAD_SAFETY_ANALYSIS { |
| 320 | if (kIsDebugBuild) { |
| 321 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 322 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 323 | } |
| 324 | CHECK(!from_space_->HasAddress(root->AsMirrorPtr())); |
| 325 | } |
| 326 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 327 | private: |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 328 | space::ContinuousMemMapAllocSpace* const from_space_; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 332 | DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space"; |
| 333 | SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 334 | obj->VisitReferences(visitor, VoidFunctor()); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor { |
| 338 | public: |
| 339 | explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {} |
| 340 | void operator()(Object* obj) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 341 | SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 342 | DCHECK(obj != nullptr); |
| 343 | semi_space_->VerifyNoFromSpaceReferences(obj); |
| 344 | } |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 345 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 346 | private: |
| 347 | SemiSpace* const semi_space_; |
| 348 | }; |
| 349 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 350 | void SemiSpace::MarkReachableObjects() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 351 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
| 352 | { |
| 353 | TimingLogger::ScopedTiming t2("MarkStackAsLive", GetTimings()); |
| 354 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 355 | heap_->MarkAllocStackAsLive(live_stack); |
| 356 | live_stack->Reset(); |
| 357 | } |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 358 | for (auto& space : heap_->GetContinuousSpaces()) { |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 359 | // If the space is immune then we need to mark the references to other spaces. |
| 360 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
| 361 | if (table != nullptr) { |
| 362 | // TODO: Improve naming. |
| 363 | TimingLogger::ScopedTiming t2( |
| 364 | space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" : |
| 365 | "UpdateAndMarkImageModUnionTable", |
| 366 | GetTimings()); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 367 | table->UpdateAndMarkReferences(this); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 368 | DCHECK(GetHeap()->FindRememberedSetFromSpace(space) == nullptr); |
| 369 | } else if (collect_from_space_only_ && space->GetLiveBitmap() != nullptr) { |
| 370 | // If the space has no mod union table (the non-moving space and main spaces when the bump |
| 371 | // pointer space only collection is enabled,) then we need to scan its live bitmap or dirty |
| 372 | // cards as roots (including the objects on the live stack which have just marked in the live |
| 373 | // bitmap above in MarkAllocStackAsLive().) |
| 374 | DCHECK(space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()) |
| 375 | << "Space " << space->GetName() << " " |
| 376 | << "generational_=" << generational_ << " " |
| 377 | << "collect_from_space_only_=" << collect_from_space_only_; |
| 378 | accounting::RememberedSet* rem_set = GetHeap()->FindRememberedSetFromSpace(space); |
| 379 | CHECK_EQ(rem_set != nullptr, kUseRememberedSet); |
| 380 | if (rem_set != nullptr) { |
| 381 | TimingLogger::ScopedTiming t2("UpdateAndMarkRememberedSet", GetTimings()); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 382 | rem_set->UpdateAndMarkReferences(from_space_, this); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 383 | if (kIsDebugBuild) { |
| 384 | // Verify that there are no from-space references that |
| 385 | // remain in the space, that is, the remembered set (and the |
| 386 | // card table) didn't miss any from-space references in the |
| 387 | // space. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 388 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 389 | SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this); |
| 390 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 391 | reinterpret_cast<uintptr_t>(space->End()), |
| 392 | visitor); |
| 393 | } |
| 394 | } else { |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 395 | TimingLogger::ScopedTiming t2("VisitLiveBits", GetTimings()); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 396 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 397 | SemiSpaceScanObjectVisitor visitor(this); |
| 398 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 399 | reinterpret_cast<uintptr_t>(space->End()), |
| 400 | visitor); |
| 401 | } |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 405 | CHECK_EQ(is_large_object_space_immune_, collect_from_space_only_); |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 406 | space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace(); |
| 407 | if (is_large_object_space_immune_ && los != nullptr) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 408 | TimingLogger::ScopedTiming t2("VisitLargeObjects", GetTimings()); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 409 | DCHECK(collect_from_space_only_); |
Hiroshi Yamauchi | ba5870d | 2014-01-29 15:31:03 -0800 | [diff] [blame] | 410 | // Delay copying the live set to the marked set until here from |
| 411 | // BindBitmaps() as the large objects on the allocation stack may |
| 412 | // be newly added to the live set above in MarkAllocStackAsLive(). |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 413 | los->CopyLiveToMarked(); |
Hiroshi Yamauchi | ba5870d | 2014-01-29 15:31:03 -0800 | [diff] [blame] | 414 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 415 | // When the large object space is immune, we need to scan the |
| 416 | // large object space as roots as they contain references to their |
| 417 | // classes (primitive array classes) that could move though they |
| 418 | // don't contain any other references. |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 419 | accounting::LargeObjectBitmap* large_live_bitmap = los->GetLiveBitmap(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 420 | SemiSpaceScanObjectVisitor visitor(this); |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 421 | large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(los->Begin()), |
| 422 | reinterpret_cast<uintptr_t>(los->End()), |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 423 | visitor); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 424 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 425 | // Recursively process the mark stack. |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 426 | ProcessMarkStack(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void SemiSpace::ReclaimPhase() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 430 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
| 431 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
| 432 | // Reclaim unmarked objects. |
| 433 | Sweep(false); |
| 434 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 435 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 436 | // bitmaps. |
| 437 | SwapBitmaps(); |
| 438 | // Unbind the live and mark bitmaps. |
| 439 | GetHeap()->UnBindBitmaps(); |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 440 | if (saved_bytes_ > 0) { |
| 441 | VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_); |
| 442 | } |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 443 | if (generational_) { |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 444 | // Record the end (top) of the to space so we can distinguish |
| 445 | // between objects that were allocated since the last GC and the |
| 446 | // older objects. |
| 447 | last_gc_to_space_end_ = to_space_->End(); |
| 448 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void SemiSpace::ResizeMarkStack(size_t new_size) { |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 452 | std::vector<StackReference<Object>> temp(mark_stack_->Begin(), mark_stack_->End()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 453 | CHECK_LE(mark_stack_->Size(), new_size); |
| 454 | mark_stack_->Resize(new_size); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 455 | for (auto& obj : temp) { |
| 456 | mark_stack_->PushBack(obj.AsMirrorPtr()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | inline void SemiSpace::MarkStackPush(Object* obj) { |
| 461 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 462 | ResizeMarkStack(mark_stack_->Capacity() * 2); |
| 463 | } |
| 464 | // The object must be pushed on to the mark stack. |
| 465 | mark_stack_->PushBack(obj); |
| 466 | } |
| 467 | |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 468 | static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) { |
| 469 | if (LIKELY(size <= static_cast<size_t>(kPageSize))) { |
| 470 | // We will dirty the current page and somewhere in the middle of the next page. This means |
| 471 | // that the next object copied will also dirty that page. |
| 472 | // TODO: Worth considering the last object copied? We may end up dirtying one page which is |
| 473 | // not necessary per GC. |
| 474 | memcpy(dest, src, size); |
| 475 | return 0; |
| 476 | } |
| 477 | size_t saved_bytes = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 478 | uint8_t* byte_dest = reinterpret_cast<uint8_t*>(dest); |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 479 | if (kIsDebugBuild) { |
| 480 | for (size_t i = 0; i < size; ++i) { |
| 481 | CHECK_EQ(byte_dest[i], 0U); |
| 482 | } |
| 483 | } |
| 484 | // Process the start of the page. The page must already be dirty, don't bother with checking. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 485 | const uint8_t* byte_src = reinterpret_cast<const uint8_t*>(src); |
| 486 | const uint8_t* limit = byte_src + size; |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 487 | size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest; |
| 488 | // Copy the bytes until the start of the next page. |
| 489 | memcpy(dest, src, page_remain); |
| 490 | byte_src += page_remain; |
| 491 | byte_dest += page_remain; |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 492 | DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize); |
| 493 | DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t)); |
| 494 | DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t)); |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 495 | while (byte_src + kPageSize < limit) { |
| 496 | bool all_zero = true; |
| 497 | uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest); |
| 498 | const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src); |
| 499 | for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) { |
| 500 | // Assumes the destination of the copy is all zeros. |
| 501 | if (word_src[i] != 0) { |
| 502 | all_zero = false; |
| 503 | word_dest[i] = word_src[i]; |
| 504 | } |
| 505 | } |
| 506 | if (all_zero) { |
| 507 | // Avoided copying into the page since it was all zeros. |
| 508 | saved_bytes += kPageSize; |
| 509 | } |
| 510 | byte_src += kPageSize; |
| 511 | byte_dest += kPageSize; |
| 512 | } |
| 513 | // Handle the part of the page at the end. |
| 514 | memcpy(byte_dest, byte_src, limit - byte_src); |
| 515 | return saved_bytes; |
| 516 | } |
| 517 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 518 | mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) { |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 519 | const size_t object_size = obj->SizeOf(); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 520 | size_t bytes_allocated, dummy; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 521 | mirror::Object* forward_address = nullptr; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 522 | if (generational_ && reinterpret_cast<uint8_t*>(obj) < last_gc_to_space_end_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 523 | // If it's allocated before the last GC (older), move |
| 524 | // (pseudo-promote) it to the main free list space (as sort |
| 525 | // of an old generation.) |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 526 | forward_address = promo_dest_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 527 | nullptr, &dummy); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 528 | if (UNLIKELY(forward_address == nullptr)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 529 | // If out of space, fall back to the to-space. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 530 | forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr, |
| 531 | &dummy); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 532 | // No logic for marking the bitmap, so it must be null. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 533 | DCHECK(to_space_live_bitmap_ == nullptr); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 534 | } else { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 535 | bytes_promoted_ += bytes_allocated; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 536 | // Dirty the card at the destionation as it may contain |
| 537 | // references (including the class pointer) to the bump pointer |
| 538 | // space. |
| 539 | GetHeap()->WriteBarrierEveryFieldOf(forward_address); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 540 | // Handle the bitmaps marking. |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 541 | accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space_->GetLiveBitmap(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 542 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 543 | accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 544 | DCHECK(mark_bitmap != nullptr); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 545 | DCHECK(!live_bitmap->Test(forward_address)); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 546 | if (collect_from_space_only_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 547 | // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap. |
| 548 | DCHECK_EQ(live_bitmap, mark_bitmap); |
| 549 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 550 | // If a bump pointer space only collection, delay the live |
| 551 | // bitmap marking of the promoted object until it's popped off |
| 552 | // the mark stack (ProcessMarkStack()). The rationale: we may |
| 553 | // be in the middle of scanning the objects in the promo |
| 554 | // destination space for |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 555 | // non-moving-space-to-bump-pointer-space references by |
| 556 | // iterating over the marked bits of the live bitmap |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 557 | // (MarkReachableObjects()). If we don't delay it (and instead |
| 558 | // mark the promoted object here), the above promo destination |
| 559 | // space scan could encounter the just-promoted object and |
| 560 | // forward the references in the promoted object's fields even |
| 561 | // through it is pushed onto the mark stack. If this happens, |
| 562 | // the promoted object would be in an inconsistent state, that |
| 563 | // is, it's on the mark stack (gray) but its fields are |
| 564 | // already forwarded (black), which would cause a |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 565 | // DCHECK(!to_space_->HasAddress(obj)) failure below. |
| 566 | } else { |
| 567 | // Mark forward_address on the live bit map. |
| 568 | live_bitmap->Set(forward_address); |
| 569 | // Mark forward_address on the mark bit map. |
| 570 | DCHECK(!mark_bitmap->Test(forward_address)); |
| 571 | mark_bitmap->Set(forward_address); |
| 572 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 573 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 574 | } else { |
| 575 | // If it's allocated after the last GC (younger), copy it to the to-space. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 576 | forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr, |
| 577 | &dummy); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 578 | if (forward_address != nullptr && to_space_live_bitmap_ != nullptr) { |
| 579 | to_space_live_bitmap_->Set(forward_address); |
| 580 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 581 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 582 | // If it's still null, attempt to use the fallback space. |
| 583 | if (UNLIKELY(forward_address == nullptr)) { |
| 584 | forward_address = fallback_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 585 | nullptr, &dummy); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 586 | CHECK(forward_address != nullptr) << "Out of memory in the to-space and fallback space."; |
| 587 | accounting::ContinuousSpaceBitmap* bitmap = fallback_space_->GetLiveBitmap(); |
| 588 | if (bitmap != nullptr) { |
| 589 | bitmap->Set(forward_address); |
| 590 | } |
| 591 | } |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 592 | ++objects_moved_; |
| 593 | bytes_moved_ += bytes_allocated; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 594 | // Copy over the object and add it to the mark stack since we still need to update its |
| 595 | // references. |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 596 | saved_bytes_ += |
| 597 | CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 598 | if (kUseBakerOrBrooksReadBarrier) { |
| 599 | obj->AssertReadBarrierPointer(); |
| 600 | if (kUseBrooksReadBarrier) { |
| 601 | DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj); |
| 602 | forward_address->SetReadBarrierPointer(forward_address); |
| 603 | } |
| 604 | forward_address->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 605 | } |
Mathieu Chartier | 5dc08a6 | 2014-01-10 10:10:23 -0800 | [diff] [blame] | 606 | DCHECK(to_space_->HasAddress(forward_address) || |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 607 | fallback_space_->HasAddress(forward_address) || |
| 608 | (generational_ && promo_dest_space_->HasAddress(forward_address))) |
| 609 | << forward_address << "\n" << GetHeap()->DumpSpaces(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 610 | return forward_address; |
| 611 | } |
| 612 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 613 | mirror::Object* SemiSpace::MarkObject(mirror::Object* root) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 614 | auto ref = StackReference<mirror::Object>::FromMirrorPtr(root); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 615 | MarkObject(&ref); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 616 | return ref.AsMirrorPtr(); |
| 617 | } |
| 618 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 619 | void SemiSpace::MarkHeapReference(mirror::HeapReference<mirror::Object>* obj_ptr) { |
| 620 | MarkObject(obj_ptr); |
Hiroshi Yamauchi | 4db7449 | 2014-04-22 17:10:48 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 623 | void SemiSpace::VisitRoots(mirror::Object*** roots, size_t count, |
| 624 | const RootInfo& info ATTRIBUTE_UNUSED) { |
| 625 | for (size_t i = 0; i < count; ++i) { |
| 626 | auto* root = roots[i]; |
| 627 | auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root); |
Mathieu Chartier | b18e827 | 2015-08-14 10:37:28 -0700 | [diff] [blame] | 628 | // The root can be in the to-space since we may visit the declaring class of an ArtMethod |
| 629 | // multiple times if it is on the call stack. |
| 630 | MarkObjectIfNotInToSpace(&ref); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 631 | if (*root != ref.AsMirrorPtr()) { |
| 632 | *root = ref.AsMirrorPtr(); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void SemiSpace::VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 638 | const RootInfo& info ATTRIBUTE_UNUSED) { |
| 639 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | b18e827 | 2015-08-14 10:37:28 -0700 | [diff] [blame] | 640 | MarkObjectIfNotInToSpace(roots[i]); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 641 | } |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 644 | // Marks all objects in the root set. |
| 645 | void SemiSpace::MarkRoots() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 646 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 647 | Runtime::Current()->VisitRoots(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 650 | void SemiSpace::SweepSystemWeaks() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 651 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 652 | Runtime::Current()->SweepSystemWeaks(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 655 | bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const { |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 656 | return space != from_space_ && space != to_space_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | void SemiSpace::Sweep(bool swap_bitmaps) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 660 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 661 | DCHECK(mark_stack_->IsEmpty()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 662 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 663 | if (space->IsContinuousMemMapAllocSpace()) { |
| 664 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
| 665 | if (!ShouldSweepSpace(alloc_space)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 666 | continue; |
| 667 | } |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 668 | TimingLogger::ScopedTiming split( |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 669 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings()); |
| 670 | RecordFree(alloc_space->Sweep(swap_bitmaps)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 673 | if (!is_large_object_space_immune_) { |
| 674 | SweepLargeObjects(swap_bitmaps); |
| 675 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | void SemiSpace::SweepLargeObjects(bool swap_bitmaps) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 679 | DCHECK(!is_large_object_space_immune_); |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 680 | space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace(); |
| 681 | if (los != nullptr) { |
| 682 | TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings()); |
| 683 | RecordFreeLOS(los->Sweep(swap_bitmaps)); |
| 684 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been |
| 688 | // marked, put it on the appropriate list in the heap for later processing. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 689 | void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 690 | heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 693 | class SemiSpaceMarkObjectVisitor { |
| 694 | public: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 695 | explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) { |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 698 | void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 699 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 580a8df | 2014-03-26 15:15:57 -0700 | [diff] [blame] | 700 | // Object was already verified when we scanned it. |
| 701 | collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset)); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 702 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 703 | |
| 704 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 705 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 706 | collector_->DelayReferenceReferent(klass, ref); |
| 707 | } |
| 708 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 709 | // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors. |
| 710 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 711 | NO_THREAD_SAFETY_ANALYSIS { |
| 712 | if (!root->IsNull()) { |
| 713 | VisitRoot(root); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 718 | NO_THREAD_SAFETY_ANALYSIS { |
| 719 | if (kIsDebugBuild) { |
| 720 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 721 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 722 | } |
Mathieu Chartier | c621106 | 2015-07-24 16:05:55 -0700 | [diff] [blame] | 723 | // We may visit the same root multiple times, so avoid marking things in the to-space since |
| 724 | // this is not handled by the GC. |
| 725 | collector_->MarkObjectIfNotInToSpace(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 728 | private: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 729 | SemiSpace* const collector_; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 730 | }; |
| 731 | |
| 732 | // Visit all of the references of an object and update. |
| 733 | void SemiSpace::ScanObject(Object* obj) { |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 734 | DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space"; |
| 735 | SemiSpaceMarkObjectVisitor visitor(this); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 736 | obj->VisitReferences(visitor, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // Scan anything that's on the mark stack. |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 740 | void SemiSpace::ProcessMarkStack() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 741 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 742 | accounting::ContinuousSpaceBitmap* live_bitmap = nullptr; |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 743 | if (collect_from_space_only_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 744 | // If a bump pointer space only collection (and the promotion is |
| 745 | // enabled,) we delay the live-bitmap marking of promoted objects |
| 746 | // from MarkObject() until this function. |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 747 | live_bitmap = promo_dest_space_->GetLiveBitmap(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 748 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 749 | accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 750 | DCHECK(mark_bitmap != nullptr); |
| 751 | DCHECK_EQ(live_bitmap, mark_bitmap); |
| 752 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 753 | while (!mark_stack_->IsEmpty()) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 754 | Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 755 | if (collect_from_space_only_ && promo_dest_space_->HasAddress(obj)) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 756 | // obj has just been promoted. Mark the live bitmap for it, |
| 757 | // which is delayed from MarkObject(). |
| 758 | DCHECK(!live_bitmap->Test(obj)); |
| 759 | live_bitmap->Set(obj); |
| 760 | } |
| 761 | ScanObject(obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 762 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 765 | mirror::Object* SemiSpace::IsMarked(mirror::Object* obj) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 766 | // All immune objects are assumed marked. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 767 | if (from_space_->HasAddress(obj)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 768 | // Returns either the forwarding address or null. |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 769 | return GetForwardingAddressInFromSpace(obj); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 770 | } else if (collect_from_space_only_ || immune_region_.ContainsObject(obj) || |
| 771 | to_space_->HasAddress(obj)) { |
| 772 | return obj; // Already forwarded, must be marked. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 773 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 774 | return mark_bitmap_->Test(obj) ? obj : nullptr; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 777 | bool SemiSpace::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* object) { |
| 778 | mirror::Object* obj = object->AsMirrorPtr(); |
| 779 | mirror::Object* new_obj = IsMarked(obj); |
| 780 | if (new_obj == nullptr) { |
| 781 | return false; |
| 782 | } |
| 783 | if (new_obj != obj) { |
| 784 | // Write barrier is not necessary since it still points to the same object, just at a different |
| 785 | // address. |
| 786 | object->Assign(new_obj); |
| 787 | } |
| 788 | return true; |
| 789 | } |
| 790 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 791 | void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) { |
| 792 | DCHECK(to_space != nullptr); |
| 793 | to_space_ = to_space; |
| 794 | } |
| 795 | |
| 796 | void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) { |
| 797 | DCHECK(from_space != nullptr); |
| 798 | from_space_ = from_space; |
| 799 | } |
| 800 | |
| 801 | void SemiSpace::FinishPhase() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 802 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Hiroshi Yamauchi | 312baf1 | 2015-01-12 12:11:05 -0800 | [diff] [blame] | 803 | if (kProtectFromSpace && from_space_->IsRosAllocSpace()) { |
Hiroshi Yamauchi | a233e03 | 2015-01-09 17:48:00 -0800 | [diff] [blame] | 804 | VLOG(heap) << "Protecting from_space_ with PROT_NONE : " << *from_space_; |
| 805 | from_space_->GetMemMap()->Protect(PROT_NONE); |
| 806 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 807 | // Null the "to" and "from" spaces since compacting from one to the other isn't valid until |
| 808 | // further action is done by the heap. |
| 809 | to_space_ = nullptr; |
| 810 | from_space_ = nullptr; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 811 | CHECK(mark_stack_->IsEmpty()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 812 | mark_stack_->Reset(); |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 813 | space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace(); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 814 | if (generational_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 815 | // Decide whether to do a whole heap collection or a bump pointer |
| 816 | // only space collection at the next collection by updating |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 817 | // collect_from_space_only_. |
| 818 | if (collect_from_space_only_) { |
| 819 | // Disable collect_from_space_only_ if the bytes promoted since the |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 820 | // last whole heap collection or the large object bytes |
| 821 | // allocated exceeds a threshold. |
| 822 | bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_; |
| 823 | bool bytes_promoted_threshold_exceeded = |
| 824 | bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold; |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 825 | uint64_t current_los_bytes_allocated = los != nullptr ? los->GetBytesAllocated() : 0U; |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 826 | uint64_t last_los_bytes_allocated = |
| 827 | large_object_bytes_allocated_at_last_whole_heap_collection_; |
| 828 | bool large_object_bytes_threshold_exceeded = |
| 829 | current_los_bytes_allocated >= |
| 830 | last_los_bytes_allocated + kLargeObjectBytesAllocatedThreshold; |
| 831 | if (bytes_promoted_threshold_exceeded || large_object_bytes_threshold_exceeded) { |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 832 | collect_from_space_only_ = false; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 833 | } |
| 834 | } else { |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 835 | // Reset the counters. |
| 836 | bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_; |
| 837 | large_object_bytes_allocated_at_last_whole_heap_collection_ = |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 838 | los != nullptr ? los->GetBytesAllocated() : 0U; |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 839 | collect_from_space_only_ = true; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 840 | } |
| 841 | } |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 842 | // Clear all of the spaces' mark bitmaps. |
| 843 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 844 | heap_->ClearMarkedObjects(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 847 | void SemiSpace::RevokeAllThreadLocalBuffers() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 848 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 849 | GetHeap()->RevokeAllThreadLocalBuffers(); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 852 | } // namespace collector |
| 853 | } // namespace gc |
| 854 | } // namespace art |