blob: ce0e0f36308c89000d576bd154f6a8778bd485e4 [file] [log] [blame]
Mathieu Chartier590fee92013-09-13 13:46:47 -07001/*
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
17#include "bump_pointer_space.h"
18#include "bump_pointer_space-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070019#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include "mirror/object-inl.h"
Mathieu Chartier692fafd2013-11-29 17:24:40 -080021#include "thread_list.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070022
23namespace art {
24namespace gc {
25namespace space {
26
27BumpPointerSpace* BumpPointerSpace::Create(const std::string& name, size_t capacity,
Ian Rogers13735952014-10-08 12:43:28 -070028 uint8_t* requested_begin) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070029 capacity = RoundUp(capacity, kPageSize);
30 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070031 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity,
Vladimir Marko5c42c292015-02-25 12:02:49 +000032 PROT_READ | PROT_WRITE, true, false,
33 &error_msg));
Mathieu Chartier590fee92013-09-13 13:46:47 -070034 if (mem_map.get() == nullptr) {
35 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
36 << PrettySize(capacity) << " with message " << error_msg;
37 return nullptr;
38 }
39 return new BumpPointerSpace(name, mem_map.release());
40}
41
Mathieu Chartier31f44142014-04-08 14:40:03 -070042BumpPointerSpace* BumpPointerSpace::CreateFromMemMap(const std::string& name, MemMap* mem_map) {
43 return new BumpPointerSpace(name, mem_map);
44}
45
Ian Rogers13735952014-10-08 12:43:28 -070046BumpPointerSpace::BumpPointerSpace(const std::string& name, uint8_t* begin, uint8_t* limit)
Mathieu Chartier590fee92013-09-13 13:46:47 -070047 : ContinuousMemMapAllocSpace(name, nullptr, begin, begin, limit,
48 kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080049 growth_end_(limit),
50 objects_allocated_(0), bytes_allocated_(0),
51 block_lock_("Block lock"),
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080052 main_block_size_(0),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080053 num_blocks_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070054}
55
56BumpPointerSpace::BumpPointerSpace(const std::string& name, MemMap* mem_map)
57 : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->Begin(), mem_map->End(),
58 kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080059 growth_end_(mem_map->End()),
60 objects_allocated_(0), bytes_allocated_(0),
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080061 block_lock_("Block lock", kBumpPointerSpaceBlockLock),
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080062 main_block_size_(0),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080063 num_blocks_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070064}
65
Mathieu Chartier590fee92013-09-13 13:46:47 -070066void BumpPointerSpace::Clear() {
67 // Release the pages back to the operating system.
Ian Rogersc5f17732014-06-05 20:48:42 -070068 if (!kMadviseZeroes) {
69 memset(Begin(), 0, Limit() - Begin());
70 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 CHECK_NE(madvise(Begin(), Limit() - Begin(), MADV_DONTNEED), -1) << "madvise failed";
72 // Reset the end of the space back to the beginning, we move the end forward as we allocate
73 // objects.
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080074 SetEnd(Begin());
Ian Rogers3e5cf302014-05-20 16:40:37 -070075 objects_allocated_.StoreRelaxed(0);
76 bytes_allocated_.StoreRelaxed(0);
Mathieu Chartier590fee92013-09-13 13:46:47 -070077 growth_end_ = Limit();
Mathieu Chartier692fafd2013-11-29 17:24:40 -080078 {
79 MutexLock mu(Thread::Current(), block_lock_);
80 num_blocks_ = 0;
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080081 main_block_size_ = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -080082 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070083}
84
85void BumpPointerSpace::Dump(std::ostream& os) const {
Mathieu Chartier15d34022014-02-26 17:16:38 -080086 os << GetName() << " "
87 << reinterpret_cast<void*>(Begin()) << "-" << reinterpret_cast<void*>(End()) << " - "
88 << reinterpret_cast<void*>(Limit());
Mathieu Chartier590fee92013-09-13 13:46:47 -070089}
90
91mirror::Object* BumpPointerSpace::GetNextObject(mirror::Object* obj) {
92 const uintptr_t position = reinterpret_cast<uintptr_t>(obj) + obj->SizeOf();
93 return reinterpret_cast<mirror::Object*>(RoundUp(position, kAlignment));
94}
95
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070096size_t BumpPointerSpace::RevokeThreadLocalBuffers(Thread* thread) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -080097 MutexLock mu(Thread::Current(), block_lock_);
98 RevokeThreadLocalBuffersLocked(thread);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070099 return 0U;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800100}
101
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700102size_t BumpPointerSpace::RevokeAllThreadLocalBuffers() {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800103 Thread* self = Thread::Current();
104 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
105 MutexLock mu2(self, *Locks::thread_list_lock_);
106 // TODO: Not do a copy of the thread list?
107 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
108 for (Thread* thread : thread_list) {
109 RevokeThreadLocalBuffers(thread);
110 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700111 return 0U;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800112}
113
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700114void BumpPointerSpace::AssertThreadLocalBuffersAreRevoked(Thread* thread) {
115 if (kIsDebugBuild) {
116 MutexLock mu(Thread::Current(), block_lock_);
117 DCHECK(!thread->HasTlab());
118 }
119}
120
121void BumpPointerSpace::AssertAllThreadLocalBuffersAreRevoked() {
122 if (kIsDebugBuild) {
123 Thread* self = Thread::Current();
124 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
125 MutexLock mu2(self, *Locks::thread_list_lock_);
126 // TODO: Not do a copy of the thread list?
127 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
128 for (Thread* thread : thread_list) {
129 AssertThreadLocalBuffersAreRevoked(thread);
130 }
131 }
132}
133
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800134void BumpPointerSpace::UpdateMainBlock() {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800135 DCHECK_EQ(num_blocks_, 0U);
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800136 main_block_size_ = Size();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800137}
138
139// Returns the start of the storage.
Ian Rogers13735952014-10-08 12:43:28 -0700140uint8_t* BumpPointerSpace::AllocBlock(size_t bytes) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800141 bytes = RoundUp(bytes, kAlignment);
142 if (!num_blocks_) {
143 UpdateMainBlock();
144 }
Ian Rogers13735952014-10-08 12:43:28 -0700145 uint8_t* storage = reinterpret_cast<uint8_t*>(
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800146 AllocNonvirtualWithoutAccounting(bytes + sizeof(BlockHeader)));
147 if (LIKELY(storage != nullptr)) {
148 BlockHeader* header = reinterpret_cast<BlockHeader*>(storage);
149 header->size_ = bytes; // Write out the block header.
150 storage += sizeof(BlockHeader);
151 ++num_blocks_;
152 }
153 return storage;
154}
155
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700156accounting::ContinuousSpaceBitmap::SweepCallback* BumpPointerSpace::GetSweepCallback() {
Ian Rogers2c4257b2014-10-24 14:20:06 -0700157 UNIMPLEMENTED(FATAL);
158 UNREACHABLE();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800159}
160
161uint64_t BumpPointerSpace::GetBytesAllocated() {
162 // Start out pre-determined amount (blocks which are not being allocated into).
Ian Rogers3e5cf302014-05-20 16:40:37 -0700163 uint64_t total = static_cast<uint64_t>(bytes_allocated_.LoadRelaxed());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800164 Thread* self = Thread::Current();
165 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
166 MutexLock mu2(self, *Locks::thread_list_lock_);
167 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
168 MutexLock mu3(Thread::Current(), block_lock_);
169 // If we don't have any blocks, we don't have any thread local buffers. This check is required
170 // since there can exist multiple bump pointer spaces which exist at the same time.
171 if (num_blocks_ > 0) {
172 for (Thread* thread : thread_list) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700173 total += thread->GetThreadLocalBytesAllocated();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800174 }
175 }
176 return total;
177}
178
179uint64_t BumpPointerSpace::GetObjectsAllocated() {
180 // Start out pre-determined amount (blocks which are not being allocated into).
Ian Rogers3e5cf302014-05-20 16:40:37 -0700181 uint64_t total = static_cast<uint64_t>(objects_allocated_.LoadRelaxed());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800182 Thread* self = Thread::Current();
183 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
184 MutexLock mu2(self, *Locks::thread_list_lock_);
185 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
186 MutexLock mu3(Thread::Current(), block_lock_);
187 // If we don't have any blocks, we don't have any thread local buffers. This check is required
188 // since there can exist multiple bump pointer spaces which exist at the same time.
189 if (num_blocks_ > 0) {
190 for (Thread* thread : thread_list) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700191 total += thread->GetThreadLocalObjectsAllocated();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800192 }
193 }
194 return total;
195}
196
197void BumpPointerSpace::RevokeThreadLocalBuffersLocked(Thread* thread) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700198 objects_allocated_.FetchAndAddSequentiallyConsistent(thread->GetThreadLocalObjectsAllocated());
199 bytes_allocated_.FetchAndAddSequentiallyConsistent(thread->GetThreadLocalBytesAllocated());
Mathieu Chartier6bc77742017-04-18 17:46:23 -0700200 thread->SetTlab(nullptr, nullptr, nullptr);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800201}
202
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800203bool BumpPointerSpace::AllocNewTlab(Thread* self, size_t bytes) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800204 MutexLock mu(Thread::Current(), block_lock_);
205 RevokeThreadLocalBuffersLocked(self);
Ian Rogers13735952014-10-08 12:43:28 -0700206 uint8_t* start = AllocBlock(bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800207 if (start == nullptr) {
208 return false;
209 }
Mathieu Chartier6bc77742017-04-18 17:46:23 -0700210 self->SetTlab(start, start + bytes, start + bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800211 return true;
212}
213
Mathieu Chartierb363f662014-07-16 13:28:58 -0700214void BumpPointerSpace::LogFragmentationAllocFailure(std::ostream& os,
215 size_t /* failed_alloc_bytes */) {
216 size_t max_contiguous_allocation = Limit() - End();
217 os << "; failed due to fragmentation (largest possible contiguous allocation "
218 << max_contiguous_allocation << " bytes)";
219 // Caller's job to print failed_alloc_bytes.
220}
221
Andreas Gamped4901292017-05-30 18:41:34 -0700222size_t BumpPointerSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
223 size_t num_bytes = obj->SizeOf();
224 if (usable_size != nullptr) {
225 *usable_size = RoundUp(num_bytes, kAlignment);
226 }
227 return num_bytes;
228}
229
Mathieu Chartier590fee92013-09-13 13:46:47 -0700230} // namespace space
231} // namespace gc
232} // namespace art