blob: 486d79ad1beaa2129daa8502aafa1ec3eec405e5 [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
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 Chartiera1602f22014-01-13 17:19:19 -080020#include "gc/accounting/heap_bitmap.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070021#include "gc/accounting/space_bitmap-inl.h"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080022#include "runtime.h"
23#include "thread-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024
25namespace art {
26namespace gc {
27namespace space {
28
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070029Space::Space(const std::string& name, GcRetentionPolicy gc_retention_policy)
30 : name_(name), gc_retention_policy_(gc_retention_policy) { }
Ian Rogers1d54e732013-05-02 21:10:01 -070031
32void Space::Dump(std::ostream& os) const {
33 os << GetName() << ":" << GetGcRetentionPolicy();
34}
35
36std::ostream& operator<<(std::ostream& os, const Space& space) {
37 space.Dump(os);
38 return os;
39}
40
Ian Rogers6fac4472014-02-25 17:01:10 -080041DlMallocSpace* Space::AsDlMallocSpace() {
Ian Rogers2c4257b2014-10-24 14:20:06 -070042 UNIMPLEMENTED(FATAL) << "Unreachable";
43 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080044}
45
46RosAllocSpace* Space::AsRosAllocSpace() {
Ian Rogers2c4257b2014-10-24 14:20:06 -070047 UNIMPLEMENTED(FATAL) << "Unreachable";
48 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080049}
50
51ZygoteSpace* Space::AsZygoteSpace() {
Ian Rogers2c4257b2014-10-24 14:20:06 -070052 UNIMPLEMENTED(FATAL) << "Unreachable";
53 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080054}
55
56BumpPointerSpace* Space::AsBumpPointerSpace() {
Ian Rogers2c4257b2014-10-24 14:20:06 -070057 UNIMPLEMENTED(FATAL) << "Unreachable";
58 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080059}
60
61AllocSpace* Space::AsAllocSpace() {
Ian Rogers2c4257b2014-10-24 14:20:06 -070062 UNIMPLEMENTED(FATAL) << "Unreachable";
63 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080064}
65
66ContinuousMemMapAllocSpace* Space::AsContinuousMemMapAllocSpace() {
Ian Rogers2c4257b2014-10-24 14:20:06 -070067 UNIMPLEMENTED(FATAL) << "Unreachable";
68 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080069}
70
Ian Rogers1d54e732013-05-02 21:10:01 -070071DiscontinuousSpace::DiscontinuousSpace(const std::string& name,
72 GcRetentionPolicy gc_retention_policy) :
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070073 Space(name, gc_retention_policy) {
74 // TODO: Fix this if we ever support objects not in the low 32 bit.
75 const size_t capacity = static_cast<size_t>(std::numeric_limits<uint32_t>::max());
76 live_bitmap_.reset(accounting::LargeObjectBitmap::Create("large live objects", nullptr,
77 capacity));
78 CHECK(live_bitmap_.get() != nullptr);
79 mark_bitmap_.reset(accounting::LargeObjectBitmap::Create("large marked objects", nullptr,
80 capacity));
81 CHECK(mark_bitmap_.get() != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -070082}
83
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070084collector::ObjectBytePair ContinuousMemMapAllocSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070085 accounting::ContinuousSpaceBitmap* live_bitmap = GetLiveBitmap();
86 accounting::ContinuousSpaceBitmap* mark_bitmap = GetMarkBitmap();
Mathieu Chartiera1602f22014-01-13 17:19:19 -080087 // If the bitmaps are bound then sweeping this space clearly won't do anything.
88 if (live_bitmap == mark_bitmap) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070089 return collector::ObjectBytePair(0, 0);
Mathieu Chartiera1602f22014-01-13 17:19:19 -080090 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070091 SweepCallbackContext scc(swap_bitmaps, this);
Mathieu Chartiera1602f22014-01-13 17:19:19 -080092 if (swap_bitmaps) {
93 std::swap(live_bitmap, mark_bitmap);
94 }
95 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070096 accounting::ContinuousSpaceBitmap::SweepWalk(
97 *live_bitmap, *mark_bitmap, reinterpret_cast<uintptr_t>(Begin()),
98 reinterpret_cast<uintptr_t>(End()), GetSweepCallback(), reinterpret_cast<void*>(&scc));
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070099 return scc.freed;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800100}
101
102// Returns the old mark bitmap.
103void ContinuousMemMapAllocSpace::BindLiveToMarkBitmap() {
104 CHECK(!HasBoundBitmaps());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700105 accounting::ContinuousSpaceBitmap* live_bitmap = GetLiveBitmap();
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800106 if (live_bitmap != mark_bitmap_.get()) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700107 accounting::ContinuousSpaceBitmap* mark_bitmap = mark_bitmap_.release();
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800108 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
109 temp_bitmap_.reset(mark_bitmap);
110 mark_bitmap_.reset(live_bitmap);
111 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800112}
113
114bool ContinuousMemMapAllocSpace::HasBoundBitmaps() const {
115 return temp_bitmap_.get() != nullptr;
116}
117
118void ContinuousMemMapAllocSpace::UnBindBitmaps() {
119 CHECK(HasBoundBitmaps());
120 // At this point, the temp_bitmap holds our old mark bitmap.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700121 accounting::ContinuousSpaceBitmap* new_bitmap = temp_bitmap_.release();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800122 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap_.get(), new_bitmap);
123 CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get());
124 mark_bitmap_.reset(new_bitmap);
125 DCHECK(temp_bitmap_.get() == nullptr);
126}
127
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800128void ContinuousMemMapAllocSpace::SwapBitmaps() {
129 live_bitmap_.swap(mark_bitmap_);
130 // Swap 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
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800136AllocSpace::SweepCallbackContext::SweepCallbackContext(bool swap_bitmaps_in, space::Space* space_in)
137 : swap_bitmaps(swap_bitmaps_in), space(space_in), self(Thread::Current()) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700138}
139
Ian Rogers1d54e732013-05-02 21:10:01 -0700140} // namespace space
141} // namespace gc
142} // namespace art