Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | #include "space.h" |
| 18 | |
| 19 | #include "base/logging.h" |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 20 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 21 | #include "gc/accounting/space_bitmap-inl.h" |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 22 | #include "runtime.h" |
| 23 | #include "thread-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace gc { |
| 27 | namespace space { |
| 28 | |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 29 | Space::Space(const std::string& name, GcRetentionPolicy gc_retention_policy) |
| 30 | : name_(name), gc_retention_policy_(gc_retention_policy) { } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | |
| 32 | void Space::Dump(std::ostream& os) const { |
| 33 | os << GetName() << ":" << GetGcRetentionPolicy(); |
| 34 | } |
| 35 | |
| 36 | std::ostream& operator<<(std::ostream& os, const Space& space) { |
| 37 | space.Dump(os); |
| 38 | return os; |
| 39 | } |
| 40 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 41 | DlMallocSpace* Space::AsDlMallocSpace() { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 42 | UNIMPLEMENTED(FATAL) << "Unreachable"; |
| 43 | UNREACHABLE(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | RosAllocSpace* Space::AsRosAllocSpace() { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 47 | UNIMPLEMENTED(FATAL) << "Unreachable"; |
| 48 | UNREACHABLE(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | ZygoteSpace* Space::AsZygoteSpace() { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 52 | UNIMPLEMENTED(FATAL) << "Unreachable"; |
| 53 | UNREACHABLE(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | BumpPointerSpace* Space::AsBumpPointerSpace() { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 57 | UNIMPLEMENTED(FATAL) << "Unreachable"; |
| 58 | UNREACHABLE(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 61 | RegionSpace* Space::AsRegionSpace() { |
| 62 | LOG(FATAL) << "Unreachable"; |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 66 | AllocSpace* Space::AsAllocSpace() { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 67 | UNIMPLEMENTED(FATAL) << "Unreachable"; |
| 68 | UNREACHABLE(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ContinuousMemMapAllocSpace* Space::AsContinuousMemMapAllocSpace() { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 72 | UNIMPLEMENTED(FATAL) << "Unreachable"; |
| 73 | UNREACHABLE(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 76 | DiscontinuousSpace::DiscontinuousSpace(const std::string& name, |
| 77 | GcRetentionPolicy gc_retention_policy) : |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 78 | Space(name, gc_retention_policy) { |
| 79 | // TODO: Fix this if we ever support objects not in the low 32 bit. |
| 80 | const size_t capacity = static_cast<size_t>(std::numeric_limits<uint32_t>::max()); |
| 81 | live_bitmap_.reset(accounting::LargeObjectBitmap::Create("large live objects", nullptr, |
| 82 | capacity)); |
| 83 | CHECK(live_bitmap_.get() != nullptr); |
| 84 | mark_bitmap_.reset(accounting::LargeObjectBitmap::Create("large marked objects", nullptr, |
| 85 | capacity)); |
| 86 | CHECK(mark_bitmap_.get() != nullptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 89 | collector::ObjectBytePair ContinuousMemMapAllocSpace::Sweep(bool swap_bitmaps) { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 90 | accounting::ContinuousSpaceBitmap* live_bitmap = GetLiveBitmap(); |
| 91 | accounting::ContinuousSpaceBitmap* mark_bitmap = GetMarkBitmap(); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 92 | // If the bitmaps are bound then sweeping this space clearly won't do anything. |
| 93 | if (live_bitmap == mark_bitmap) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 94 | return collector::ObjectBytePair(0, 0); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 95 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 96 | SweepCallbackContext scc(swap_bitmaps, this); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 97 | if (swap_bitmaps) { |
| 98 | std::swap(live_bitmap, mark_bitmap); |
| 99 | } |
| 100 | // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 101 | accounting::ContinuousSpaceBitmap::SweepWalk( |
| 102 | *live_bitmap, *mark_bitmap, reinterpret_cast<uintptr_t>(Begin()), |
| 103 | reinterpret_cast<uintptr_t>(End()), GetSweepCallback(), reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 104 | return scc.freed; |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Returns the old mark bitmap. |
| 108 | void ContinuousMemMapAllocSpace::BindLiveToMarkBitmap() { |
| 109 | CHECK(!HasBoundBitmaps()); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 110 | accounting::ContinuousSpaceBitmap* live_bitmap = GetLiveBitmap(); |
Mathieu Chartier | 1f3b535 | 2014-02-03 14:00:42 -0800 | [diff] [blame] | 111 | if (live_bitmap != mark_bitmap_.get()) { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 112 | accounting::ContinuousSpaceBitmap* mark_bitmap = mark_bitmap_.release(); |
Mathieu Chartier | 1f3b535 | 2014-02-03 14:00:42 -0800 | [diff] [blame] | 113 | Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 114 | temp_bitmap_.reset(mark_bitmap); |
| 115 | mark_bitmap_.reset(live_bitmap); |
| 116 | } |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool ContinuousMemMapAllocSpace::HasBoundBitmaps() const { |
| 120 | return temp_bitmap_.get() != nullptr; |
| 121 | } |
| 122 | |
| 123 | void ContinuousMemMapAllocSpace::UnBindBitmaps() { |
| 124 | CHECK(HasBoundBitmaps()); |
| 125 | // At this point, the temp_bitmap holds our old mark bitmap. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 126 | accounting::ContinuousSpaceBitmap* new_bitmap = temp_bitmap_.release(); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 127 | Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap_.get(), new_bitmap); |
| 128 | CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get()); |
| 129 | mark_bitmap_.reset(new_bitmap); |
| 130 | DCHECK(temp_bitmap_.get() == nullptr); |
| 131 | } |
| 132 | |
Mathieu Chartier | 1f3b535 | 2014-02-03 14:00:42 -0800 | [diff] [blame] | 133 | void ContinuousMemMapAllocSpace::SwapBitmaps() { |
| 134 | live_bitmap_.swap(mark_bitmap_); |
| 135 | // Swap names to get more descriptive diagnostics. |
| 136 | std::string temp_name(live_bitmap_->GetName()); |
| 137 | live_bitmap_->SetName(mark_bitmap_->GetName()); |
| 138 | mark_bitmap_->SetName(temp_name); |
| 139 | } |
| 140 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 141 | AllocSpace::SweepCallbackContext::SweepCallbackContext(bool swap_bitmaps_in, space::Space* space_in) |
| 142 | : swap_bitmaps(swap_bitmaps_in), space(space_in), self(Thread::Current()) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 145 | } // namespace space |
| 146 | } // namespace gc |
| 147 | } // namespace art |