• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/logging.h>
20 
21 #include "base/macros.h"
22 #include "gc/accounting/heap_bitmap.h"
23 #include "gc/accounting/space_bitmap-inl.h"
24 #include "gc/heap.h"
25 #include "runtime.h"
26 #include "thread-current-inl.h"
27 
28 namespace art {
29 namespace gc {
30 namespace space {
31 
Space(const std::string & name,GcRetentionPolicy gc_retention_policy)32 Space::Space(const std::string& name, GcRetentionPolicy gc_retention_policy)
33     : name_(name), gc_retention_policy_(gc_retention_policy) { }
34 
Dump(std::ostream & os) const35 void Space::Dump(std::ostream& os) const {
36   os << GetName() << ":" << GetGcRetentionPolicy();
37 }
38 
operator <<(std::ostream & os,const Space & space)39 std::ostream& operator<<(std::ostream& os, const Space& space) {
40   space.Dump(os);
41   return os;
42 }
43 
AsDlMallocSpace()44 DlMallocSpace* Space::AsDlMallocSpace() {
45   UNIMPLEMENTED(FATAL) << "Unreachable";
46   UNREACHABLE();
47 }
48 
AsRosAllocSpace()49 RosAllocSpace* Space::AsRosAllocSpace() {
50   UNIMPLEMENTED(FATAL) << "Unreachable";
51   UNREACHABLE();
52 }
53 
AsZygoteSpace()54 ZygoteSpace* Space::AsZygoteSpace() {
55   UNIMPLEMENTED(FATAL) << "Unreachable";
56   UNREACHABLE();
57 }
58 
AsBumpPointerSpace()59 BumpPointerSpace* Space::AsBumpPointerSpace() {
60   UNIMPLEMENTED(FATAL) << "Unreachable";
61   UNREACHABLE();
62 }
63 
AsRegionSpace()64 RegionSpace* Space::AsRegionSpace() {
65   LOG(FATAL) << "Unreachable";
66   UNREACHABLE();
67 }
68 
AsAllocSpace()69 AllocSpace* Space::AsAllocSpace() {
70   UNIMPLEMENTED(FATAL) << "Unreachable";
71   UNREACHABLE();
72 }
73 
AsContinuousMemMapAllocSpace()74 ContinuousMemMapAllocSpace* Space::AsContinuousMemMapAllocSpace() {
75   UNIMPLEMENTED(FATAL) << "Unreachable";
76   UNREACHABLE();
77 }
78 
DiscontinuousSpace(const std::string & name,GcRetentionPolicy gc_retention_policy)79 DiscontinuousSpace::DiscontinuousSpace(const std::string& name,
80                                        GcRetentionPolicy gc_retention_policy) :
81     Space(name, gc_retention_policy) {
82   // TODO: Fix this if we ever support objects not in the low 32 bit.
83   const size_t capacity = static_cast<size_t>(std::numeric_limits<uint32_t>::max());
84   live_bitmap_ = accounting::LargeObjectBitmap::Create("large live objects", nullptr, capacity);
85   CHECK(live_bitmap_.IsValid());
86   mark_bitmap_ = accounting::LargeObjectBitmap::Create("large marked objects", nullptr, capacity);
87   CHECK(mark_bitmap_.IsValid());
88 }
89 
Sweep(bool swap_bitmaps)90 collector::ObjectBytePair ContinuousMemMapAllocSpace::Sweep(bool swap_bitmaps) {
91   accounting::ContinuousSpaceBitmap* live_bitmap = GetLiveBitmap();
92   accounting::ContinuousSpaceBitmap* mark_bitmap = GetMarkBitmap();
93   // If the bitmaps are bound then sweeping this space clearly won't do anything.
94   if (live_bitmap == mark_bitmap) {
95     return collector::ObjectBytePair(0, 0);
96   }
97   SweepCallbackContext scc(swap_bitmaps, this);
98   if (swap_bitmaps) {
99     std::swap(live_bitmap, mark_bitmap);
100   }
101   // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
102   accounting::ContinuousSpaceBitmap::SweepWalk(
103       *live_bitmap, *mark_bitmap, reinterpret_cast<uintptr_t>(Begin()),
104       reinterpret_cast<uintptr_t>(End()), GetSweepCallback(), reinterpret_cast<void*>(&scc));
105   return scc.freed;
106 }
107 
BindLiveToMarkBitmap()108 void ContinuousMemMapAllocSpace::BindLiveToMarkBitmap() {
109   CHECK(!HasBoundBitmaps());
110   temp_bitmap_ = std::move(mark_bitmap_);
111   mark_bitmap_.CopyView(live_bitmap_);
112 }
113 
HasBoundBitmaps()114 bool ContinuousSpace::HasBoundBitmaps() {
115   DCHECK(GetLiveBitmap() != nullptr);
116   DCHECK(GetMarkBitmap() != nullptr);
117   // Check if the bitmaps are pointing to the same underlying data.
118   return GetLiveBitmap()->Begin() == GetMarkBitmap()->Begin();
119 }
120 
UnBindBitmaps()121 void ContinuousMemMapAllocSpace::UnBindBitmaps() {
122   CHECK(HasBoundBitmaps());
123   // At this point, `temp_bitmap_` holds our old mark bitmap.
124   mark_bitmap_ = std::move(temp_bitmap_);
125 }
126 
SwapBitmaps()127 void ContinuousMemMapAllocSpace::SwapBitmaps() {
128   CHECK(!HasBoundBitmaps());
129   std::swap(live_bitmap_, mark_bitmap_);
130   // Preserve names to get more descriptive diagnostics.
131   std::string temp_name(live_bitmap_.GetName());
132   live_bitmap_.SetName(mark_bitmap_.GetName());
133   mark_bitmap_.SetName(temp_name);
134 }
135 
SweepCallbackContext(bool swap_bitmaps_in,space::Space * space_in)136 AllocSpace::SweepCallbackContext::SweepCallbackContext(bool swap_bitmaps_in, space::Space* space_in)
137     : swap_bitmaps(swap_bitmaps_in), space(space_in), self(Thread::Current()) {
138 }
139 
140 }  // namespace space
141 }  // namespace gc
142 }  // namespace art
143