blob: 43674eacffdc90e98a149eff640f7cf3c82a30e2 [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
Mathieu Chartier590fee92013-09-13 13:46:47 -070061void BumpPointerSpace::Clear() {
62 // Release the pages back to the operating system.
63 CHECK_NE(madvise(Begin(), Limit() - Begin(), MADV_DONTNEED), -1) << "madvise failed";
64 // Reset the end of the space back to the beginning, we move the end forward as we allocate
65 // objects.
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080066 SetEnd(Begin());
Mathieu Chartier692fafd2013-11-29 17:24:40 -080067 objects_allocated_ = 0;
68 bytes_allocated_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -070069 growth_end_ = Limit();
Mathieu Chartier692fafd2013-11-29 17:24:40 -080070 {
71 MutexLock mu(Thread::Current(), block_lock_);
72 num_blocks_ = 0;
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -080073 main_block_size_ = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -080074 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070075}
76
77void BumpPointerSpace::Dump(std::ostream& os) const {
78 os << reinterpret_cast<void*>(Begin()) << "-" << reinterpret_cast<void*>(End()) << " - "
79 << reinterpret_cast<void*>(Limit());
80}
81
82mirror::Object* BumpPointerSpace::GetNextObject(mirror::Object* obj) {
83 const uintptr_t position = reinterpret_cast<uintptr_t>(obj) + obj->SizeOf();
84 return reinterpret_cast<mirror::Object*>(RoundUp(position, kAlignment));
85}
86
Mathieu Chartier692fafd2013-11-29 17:24:40 -080087void BumpPointerSpace::RevokeThreadLocalBuffers(Thread* thread) {
88 MutexLock mu(Thread::Current(), block_lock_);
89 RevokeThreadLocalBuffersLocked(thread);
90}
91
92void BumpPointerSpace::RevokeAllThreadLocalBuffers() {
93 Thread* self = Thread::Current();
94 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
95 MutexLock mu2(self, *Locks::thread_list_lock_);
96 // TODO: Not do a copy of the thread list?
97 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
98 for (Thread* thread : thread_list) {
99 RevokeThreadLocalBuffers(thread);
100 }
101}
102
103void BumpPointerSpace::UpdateMainBlock() {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800104 DCHECK_EQ(num_blocks_, 0U);
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800105 main_block_size_ = Size();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800106}
107
108// Returns the start of the storage.
109byte* BumpPointerSpace::AllocBlock(size_t bytes) {
110 bytes = RoundUp(bytes, kAlignment);
111 if (!num_blocks_) {
112 UpdateMainBlock();
113 }
114 byte* storage = reinterpret_cast<byte*>(
115 AllocNonvirtualWithoutAccounting(bytes + sizeof(BlockHeader)));
116 if (LIKELY(storage != nullptr)) {
117 BlockHeader* header = reinterpret_cast<BlockHeader*>(storage);
118 header->size_ = bytes; // Write out the block header.
119 storage += sizeof(BlockHeader);
120 ++num_blocks_;
121 }
122 return storage;
123}
124
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800125void BumpPointerSpace::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800126 byte* pos = Begin();
Mathieu Chartier8544b462014-02-12 17:47:42 -0800127 byte* end = End();
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800128 byte* main_end = pos;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800129 {
130 MutexLock mu(Thread::Current(), block_lock_);
131 // If we have 0 blocks then we need to update the main header since we have bump pointer style
132 // allocation into an unbounded region (actually bounded by Capacity()).
133 if (num_blocks_ == 0) {
134 UpdateMainBlock();
135 }
Mathieu Chartier8544b462014-02-12 17:47:42 -0800136 main_end = Begin() + main_block_size_;
137 if (num_blocks_ == 0) {
138 // We don't have any other blocks, this means someone else may be allocating into the main
139 // block. In this case, we don't want to try and visit the other blocks after the main block
140 // since these could actually be part of the main block.
141 end = main_end;
142 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800143 }
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800144 // Walk all of the objects in the main block first.
145 while (pos < main_end) {
146 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
Mathieu Chartier8544b462014-02-12 17:47:42 -0800147 if (obj->GetClass() == nullptr) {
148 // There is a race condition where a thread has just allocated an object but not set the
149 // class. We can't know the size of this object, so we don't visit it and exit the function
150 // since there is guaranteed to be not other blocks.
151 return;
152 } else {
153 callback(obj, arg);
154 pos = reinterpret_cast<byte*>(GetNextObject(obj));
155 }
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800156 }
157 // Walk the other blocks (currently only TLABs).
Mathieu Chartier8544b462014-02-12 17:47:42 -0800158 while (pos < end) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800159 BlockHeader* header = reinterpret_cast<BlockHeader*>(pos);
160 size_t block_size = header->size_;
161 pos += sizeof(BlockHeader); // Skip the header so that we know where the objects
162 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
163 const mirror::Object* end = reinterpret_cast<const mirror::Object*>(pos + block_size);
164 CHECK_LE(reinterpret_cast<const byte*>(end), End());
165 // We don't know how many objects are allocated in the current block. When we hit a null class
166 // assume its the end. TODO: Have a thread update the header when it flushes the block?
167 while (obj < end && obj->GetClass() != nullptr) {
168 callback(obj, arg);
169 obj = GetNextObject(obj);
170 }
171 pos += block_size;
172 }
173}
174
Ian Rogers6fac4472014-02-25 17:01:10 -0800175accounting::SpaceBitmap::SweepCallback* BumpPointerSpace::GetSweepCallback() {
176 LOG(FATAL) << "Unimplemented";
177 return nullptr;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800178}
179
180uint64_t BumpPointerSpace::GetBytesAllocated() {
181 // Start out pre-determined amount (blocks which are not being allocated into).
Ian Rogersb122a4b2013-11-19 18:00:50 -0800182 uint64_t total = static_cast<uint64_t>(bytes_allocated_.Load());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800183 Thread* self = Thread::Current();
184 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
185 MutexLock mu2(self, *Locks::thread_list_lock_);
186 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
187 MutexLock mu3(Thread::Current(), block_lock_);
188 // If we don't have any blocks, we don't have any thread local buffers. This check is required
189 // since there can exist multiple bump pointer spaces which exist at the same time.
190 if (num_blocks_ > 0) {
191 for (Thread* thread : thread_list) {
192 total += thread->thread_local_pos_ - thread->thread_local_start_;
193 }
194 }
195 return total;
196}
197
198uint64_t BumpPointerSpace::GetObjectsAllocated() {
199 // Start out pre-determined amount (blocks which are not being allocated into).
Ian Rogersb122a4b2013-11-19 18:00:50 -0800200 uint64_t total = static_cast<uint64_t>(objects_allocated_.Load());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800201 Thread* self = Thread::Current();
202 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
203 MutexLock mu2(self, *Locks::thread_list_lock_);
204 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
205 MutexLock mu3(Thread::Current(), block_lock_);
206 // If we don't have any blocks, we don't have any thread local buffers. This check is required
207 // since there can exist multiple bump pointer spaces which exist at the same time.
208 if (num_blocks_ > 0) {
209 for (Thread* thread : thread_list) {
210 total += thread->thread_local_objects_;
211 }
212 }
213 return total;
214}
215
216void BumpPointerSpace::RevokeThreadLocalBuffersLocked(Thread* thread) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800217 objects_allocated_.FetchAndAdd(thread->thread_local_objects_);
218 bytes_allocated_.FetchAndAdd(thread->thread_local_pos_ - thread->thread_local_start_);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800219 thread->SetTlab(nullptr, nullptr);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800220}
221
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800222bool BumpPointerSpace::AllocNewTlab(Thread* self, size_t bytes) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800223 MutexLock mu(Thread::Current(), block_lock_);
224 RevokeThreadLocalBuffersLocked(self);
225 byte* start = AllocBlock(bytes);
226 if (start == nullptr) {
227 return false;
228 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800229 self->SetTlab(start, start + bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800230 return true;
231}
232
Mathieu Chartier590fee92013-09-13 13:46:47 -0700233} // namespace space
234} // namespace gc
235} // namespace art