blob: f7bdc4cdb012680b8817d68935eaa7230f52ca55 [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"
19#include "mirror/object-inl.h"
20#include "mirror/class-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,
28 byte* requested_begin) {
29 capacity = RoundUp(capacity, kPageSize);
30 std::string error_msg;
31 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity,
Ian Rogersef7d42f2014-01-06 12:55:46 -080032 PROT_READ | PROT_WRITE, true, &error_msg));
Mathieu Chartier590fee92013-09-13 13:46:47 -070033 if (mem_map.get() == nullptr) {
34 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
35 << PrettySize(capacity) << " with message " << error_msg;
36 return nullptr;
37 }
38 return new BumpPointerSpace(name, mem_map.release());
39}
40
41BumpPointerSpace::BumpPointerSpace(const std::string& name, byte* begin, byte* limit)
42 : ContinuousMemMapAllocSpace(name, nullptr, begin, begin, limit,
43 kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080044 growth_end_(limit),
45 objects_allocated_(0), bytes_allocated_(0),
46 block_lock_("Block lock"),
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080047 main_block_size_(0),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080048 num_blocks_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070049}
50
51BumpPointerSpace::BumpPointerSpace(const std::string& name, MemMap* mem_map)
52 : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->Begin(), mem_map->End(),
53 kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080054 growth_end_(mem_map->End()),
55 objects_allocated_(0), bytes_allocated_(0),
56 block_lock_("Block lock"),
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080057 main_block_size_(0),
Mathieu Chartier692fafd2013-11-29 17:24:40 -080058 num_blocks_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070059}
60
61mirror::Object* BumpPointerSpace::Alloc(Thread*, size_t num_bytes, size_t* bytes_allocated) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -080062 num_bytes = RoundUp(num_bytes, kAlignment);
Mathieu Chartier590fee92013-09-13 13:46:47 -070063 mirror::Object* ret = AllocNonvirtual(num_bytes);
64 if (LIKELY(ret != nullptr)) {
65 *bytes_allocated = num_bytes;
66 }
67 return ret;
68}
69
Ian Rogersef7d42f2014-01-06 12:55:46 -080070size_t BumpPointerSpace::AllocationSize(mirror::Object* obj) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 return AllocationSizeNonvirtual(obj);
72}
73
74void BumpPointerSpace::Clear() {
75 // Release the pages back to the operating system.
76 CHECK_NE(madvise(Begin(), Limit() - Begin(), MADV_DONTNEED), -1) << "madvise failed";
77 // Reset the end of the space back to the beginning, we move the end forward as we allocate
78 // objects.
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080079 SetEnd(Begin());
Mathieu Chartier692fafd2013-11-29 17:24:40 -080080 objects_allocated_ = 0;
81 bytes_allocated_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -070082 growth_end_ = Limit();
Mathieu Chartier692fafd2013-11-29 17:24:40 -080083 {
84 MutexLock mu(Thread::Current(), block_lock_);
85 num_blocks_ = 0;
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080086 main_block_size_ = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -080087 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070088}
89
90void BumpPointerSpace::Dump(std::ostream& os) const {
91 os << reinterpret_cast<void*>(Begin()) << "-" << reinterpret_cast<void*>(End()) << " - "
92 << reinterpret_cast<void*>(Limit());
93}
94
95mirror::Object* BumpPointerSpace::GetNextObject(mirror::Object* obj) {
96 const uintptr_t position = reinterpret_cast<uintptr_t>(obj) + obj->SizeOf();
97 return reinterpret_cast<mirror::Object*>(RoundUp(position, kAlignment));
98}
99
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800100void BumpPointerSpace::RevokeThreadLocalBuffers(Thread* thread) {
101 MutexLock mu(Thread::Current(), block_lock_);
102 RevokeThreadLocalBuffersLocked(thread);
103}
104
105void BumpPointerSpace::RevokeAllThreadLocalBuffers() {
106 Thread* self = Thread::Current();
107 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
108 MutexLock mu2(self, *Locks::thread_list_lock_);
109 // TODO: Not do a copy of the thread list?
110 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
111 for (Thread* thread : thread_list) {
112 RevokeThreadLocalBuffers(thread);
113 }
114}
115
116void BumpPointerSpace::UpdateMainBlock() {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800117 DCHECK_EQ(num_blocks_, 0U);
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800118 main_block_size_ = Size();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800119}
120
121// Returns the start of the storage.
122byte* BumpPointerSpace::AllocBlock(size_t bytes) {
123 bytes = RoundUp(bytes, kAlignment);
124 if (!num_blocks_) {
125 UpdateMainBlock();
126 }
127 byte* storage = reinterpret_cast<byte*>(
128 AllocNonvirtualWithoutAccounting(bytes + sizeof(BlockHeader)));
129 if (LIKELY(storage != nullptr)) {
130 BlockHeader* header = reinterpret_cast<BlockHeader*>(storage);
131 header->size_ = bytes; // Write out the block header.
132 storage += sizeof(BlockHeader);
133 ++num_blocks_;
134 }
135 return storage;
136}
137
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800138void BumpPointerSpace::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800139 byte* pos = Begin();
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800140 byte* main_end = pos;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800141 {
142 MutexLock mu(Thread::Current(), block_lock_);
143 // If we have 0 blocks then we need to update the main header since we have bump pointer style
144 // allocation into an unbounded region (actually bounded by Capacity()).
145 if (num_blocks_ == 0) {
146 UpdateMainBlock();
147 }
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800148 main_end += main_block_size_;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800149 }
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800150 // Walk all of the objects in the main block first.
151 while (pos < main_end) {
152 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
153 callback(obj, arg);
154 pos = reinterpret_cast<byte*>(GetNextObject(obj));
155 }
156 // Walk the other blocks (currently only TLABs).
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800157 while (pos < End()) {
158 BlockHeader* header = reinterpret_cast<BlockHeader*>(pos);
159 size_t block_size = header->size_;
160 pos += sizeof(BlockHeader); // Skip the header so that we know where the objects
161 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
162 const mirror::Object* end = reinterpret_cast<const mirror::Object*>(pos + block_size);
163 CHECK_LE(reinterpret_cast<const byte*>(end), End());
164 // We don't know how many objects are allocated in the current block. When we hit a null class
165 // assume its the end. TODO: Have a thread update the header when it flushes the block?
166 while (obj < end && obj->GetClass() != nullptr) {
167 callback(obj, arg);
168 obj = GetNextObject(obj);
169 }
170 pos += block_size;
171 }
172}
173
174bool BumpPointerSpace::IsEmpty() const {
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800175 return Begin() == End();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800176}
177
178uint64_t BumpPointerSpace::GetBytesAllocated() {
179 // Start out pre-determined amount (blocks which are not being allocated into).
Ian Rogersb122a4b2013-11-19 18:00:50 -0800180 uint64_t total = static_cast<uint64_t>(bytes_allocated_.Load());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800181 Thread* self = Thread::Current();
182 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
183 MutexLock mu2(self, *Locks::thread_list_lock_);
184 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
185 MutexLock mu3(Thread::Current(), block_lock_);
186 // If we don't have any blocks, we don't have any thread local buffers. This check is required
187 // since there can exist multiple bump pointer spaces which exist at the same time.
188 if (num_blocks_ > 0) {
189 for (Thread* thread : thread_list) {
190 total += thread->thread_local_pos_ - thread->thread_local_start_;
191 }
192 }
193 return total;
194}
195
196uint64_t BumpPointerSpace::GetObjectsAllocated() {
197 // Start out pre-determined amount (blocks which are not being allocated into).
Ian Rogersb122a4b2013-11-19 18:00:50 -0800198 uint64_t total = static_cast<uint64_t>(objects_allocated_.Load());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800199 Thread* self = Thread::Current();
200 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
201 MutexLock mu2(self, *Locks::thread_list_lock_);
202 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
203 MutexLock mu3(Thread::Current(), block_lock_);
204 // If we don't have any blocks, we don't have any thread local buffers. This check is required
205 // since there can exist multiple bump pointer spaces which exist at the same time.
206 if (num_blocks_ > 0) {
207 for (Thread* thread : thread_list) {
208 total += thread->thread_local_objects_;
209 }
210 }
211 return total;
212}
213
214void BumpPointerSpace::RevokeThreadLocalBuffersLocked(Thread* thread) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800215 objects_allocated_.FetchAndAdd(thread->thread_local_objects_);
216 bytes_allocated_.FetchAndAdd(thread->thread_local_pos_ - thread->thread_local_start_);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800217 thread->SetTlab(nullptr, nullptr);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800218}
219
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800220bool BumpPointerSpace::AllocNewTlab(Thread* self, size_t bytes) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800221 MutexLock mu(Thread::Current(), block_lock_);
222 RevokeThreadLocalBuffersLocked(self);
223 byte* start = AllocBlock(bytes);
224 if (start == nullptr) {
225 return false;
226 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800227 self->SetTlab(start, start + bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800228 return true;
229}
230
Mathieu Chartier590fee92013-09-13 13:46:47 -0700231} // namespace space
232} // namespace gc
233} // namespace art