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