blob: a589e67ed9bd3ed883ff0944ae4a27736d4b714b [file] [log] [blame]
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001/*
2 * Copyright (C) 2012 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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include "base/logging.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080018#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070019#include "large_object_space.h"
20#include "UniquePtr.h"
21#include "dlmalloc.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070022#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070023#include "os.h"
24#include "space_bitmap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "thread.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070026#include "utils.h"
27
28namespace art {
29
30void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080031 live_objects_.swap(mark_objects_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070032 // Swap names to get more descriptive diagnostics.
33 std::string temp_name = live_objects_->GetName();
34 live_objects_->SetName(mark_objects_->GetName());
35 mark_objects_->SetName(temp_name);
36}
37
38LargeObjectSpace::LargeObjectSpace(const std::string& name)
39 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
40 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
41 total_objects_allocated_(0) {
42 live_objects_.reset(new SpaceSetMap("large live objects"));
43 mark_objects_.reset(new SpaceSetMap("large marked objects"));
44}
45
46
47void LargeObjectSpace::CopyLiveToMarked() {
48 mark_objects_->CopyFrom(*live_objects_.get());
49}
50
51LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
52 : LargeObjectSpace(name),
53 lock_("large object space lock", kAllocSpaceLock)
54{
55
56}
57
58LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
59 return new LargeObjectMapSpace(name);
60}
61
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes) {
Ian Rogersa40307e2013-02-22 11:32:44 -080063 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", NULL, num_bytes,
64 PROT_READ | PROT_WRITE);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070065 if (mem_map == NULL) {
66 return NULL;
67 }
68 MutexLock mu(self, lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 mirror::Object* obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070070 large_objects_.push_back(obj);
71 mem_maps_.Put(obj, mem_map);
72 size_t allocation_size = mem_map->Size();
73 num_bytes_allocated_ += allocation_size;
74 total_bytes_allocated_ += allocation_size;
75 ++num_objects_allocated_;
76 ++total_objects_allocated_;
77 return obj;
78}
79
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070081 MutexLock mu(self, lock_);
82 MemMaps::iterator found = mem_maps_.find(ptr);
83 CHECK(found != mem_maps_.end()) << "Attempted to free large object which was not live";
84 DCHECK_GE(num_bytes_allocated_, found->second->Size());
85 size_t allocation_size = found->second->Size();
86 num_bytes_allocated_ -= allocation_size;
87 --num_objects_allocated_;
88 delete found->second;
89 mem_maps_.erase(found);
90 return allocation_size;
91}
92
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093size_t LargeObjectMapSpace::AllocationSize(const mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070094 MutexLock mu(Thread::Current(), lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 MemMaps::iterator found = mem_maps_.find(const_cast<mirror::Object*>(obj));
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070096 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
97 return found->second->Size();
98}
99
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700101 size_t total = 0;
102 for (size_t i = 0; i < num_ptrs; ++i) {
103 if (kDebugSpaces) {
104 CHECK(Contains(ptrs[i]));
105 }
106 total += Free(self, ptrs[i]);
107 }
108 return total;
109}
110
111void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
112 MutexLock mu(Thread::Current(), lock_);
113 for (MemMaps::iterator it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
114 MemMap* mem_map = it->second;
115 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
116 callback(NULL, NULL, 0, arg);
117 }
118}
119
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700121 MutexLock mu(Thread::Current(), lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700123}
124
125FreeListSpace* FreeListSpace::Create(const std::string& name, byte* requested_begin, size_t size) {
126 CHECK(size % kAlignment == 0);
127 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
128 PROT_READ | PROT_WRITE);
129 CHECK(mem_map != NULL) << "Failed to allocate large object space mem map";
130 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
131}
132
133FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end)
134 : LargeObjectSpace(name),
135 begin_(begin),
136 end_(end),
137 mem_map_(mem_map),
138 lock_("free list space lock", kAllocSpaceLock) {
139 chunks_.resize(Size() / kAlignment + 1);
140 // Add a dummy chunk so we don't need to handle chunks having no next chunk.
141 chunks_.back().SetSize(kAlignment, false);
142 // Start out with one large free chunk.
143 AddFreeChunk(begin_, end_ - begin_, NULL);
144}
145
146FreeListSpace::~FreeListSpace() {
147
148}
149
150void FreeListSpace::AddFreeChunk(void* address, size_t size, Chunk* previous) {
151 Chunk* chunk = ChunkFromAddr(address);
152 chunk->SetSize(size, true);
153 chunk->SetPrevious(previous);
154 Chunk* next_chunk = GetNextChunk(chunk);
155 next_chunk->SetPrevious(chunk);
156 free_chunks_.insert(chunk);
157}
158
159FreeListSpace::Chunk* FreeListSpace::ChunkFromAddr(void* address) {
160 size_t offset = reinterpret_cast<byte*>(address) - Begin();
161 DCHECK(IsAligned<kAlignment>(offset));
162 DCHECK_LT(offset, Size());
163 return &chunks_[offset / kAlignment];
164}
165
166void* FreeListSpace::AddrFromChunk(Chunk* chunk) {
167 return reinterpret_cast<void*>(Begin() + (chunk - &chunks_.front()) * kAlignment);
168}
169
170void FreeListSpace::RemoveFreeChunk(Chunk* chunk) {
171 // TODO: C++0x
172 // TODO: Improve performance, this might be slow.
173 std::pair<FreeChunks::iterator, FreeChunks::iterator> range = free_chunks_.equal_range(chunk);
174 for (FreeChunks::iterator it = range.first; it != range.second; ++it) {
175 if (*it == chunk) {
176 free_chunks_.erase(it);
177 return;
178 }
179 }
180}
181
182void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
183 MutexLock mu(Thread::Current(), lock_);
184 for (Chunk* chunk = &chunks_.front(); chunk < &chunks_.back(); ) {
185 if (!chunk->IsFree()) {
186 size_t size = chunk->GetSize();
187 void* begin = AddrFromChunk(chunk);
188 void* end = reinterpret_cast<void*>(reinterpret_cast<byte*>(begin) + size);
189 callback(begin, end, size, arg);
190 callback(NULL, NULL, 0, arg);
191 }
192 chunk = GetNextChunk(chunk);
193 }
194}
195
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700197 MutexLock mu(self, lock_);
198 CHECK(Contains(obj));
199 // Check adjacent chunks to see if we need to combine.
200 Chunk* chunk = ChunkFromAddr(obj);
201 CHECK(!chunk->IsFree());
202
203 size_t allocation_size = chunk->GetSize();
204 madvise(obj, allocation_size, MADV_DONTNEED);
205 num_objects_allocated_--;
206 num_bytes_allocated_ -= allocation_size;
207 Chunk* prev = chunk->GetPrevious();
208 Chunk* next = GetNextChunk(chunk);
209
210 // Combine any adjacent free chunks
211 size_t extra_size = chunk->GetSize();
212 if (next->IsFree()) {
213 extra_size += next->GetSize();
214 RemoveFreeChunk(next);
215 }
216 if (prev != NULL && prev->IsFree()) {
217 RemoveFreeChunk(prev);
218 AddFreeChunk(AddrFromChunk(prev), prev->GetSize() + extra_size, prev->GetPrevious());
219 } else {
220 AddFreeChunk(AddrFromChunk(chunk), extra_size, prev);
221 }
222 return allocation_size;
223}
224
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225bool FreeListSpace::Contains(const mirror::Object* obj) const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700226 return mem_map_->HasAddress(obj);
227}
228
229FreeListSpace::Chunk* FreeListSpace::GetNextChunk(Chunk* chunk) {
230 return chunk + chunk->GetSize() / kAlignment;
231}
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233size_t FreeListSpace::AllocationSize(const mirror::Object* obj) {
234 Chunk* chunk = ChunkFromAddr(const_cast<mirror::Object*>(obj));
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700235 CHECK(!chunk->IsFree());
236 return chunk->GetSize();
237}
238
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700240 MutexLock mu(self, lock_);
241 num_bytes = RoundUp(num_bytes, kAlignment);
242 Chunk temp;
243 temp.SetSize(num_bytes);
244 // Find the smallest chunk at least num_bytes in size.
245 FreeChunks::iterator found = free_chunks_.lower_bound(&temp);
246 if (found == free_chunks_.end()) {
247 // Out of memory, or too much fragmentation.
248 return NULL;
249 }
250 Chunk* chunk = *found;
251 free_chunks_.erase(found);
252 CHECK(chunk->IsFree());
253 void* addr = AddrFromChunk(chunk);
254 size_t chunk_size = chunk->GetSize();
255 chunk->SetSize(num_bytes);
256 if (chunk_size > num_bytes) {
257 // Split the chunk into two chunks.
258 Chunk* new_chunk = GetNextChunk(chunk);
259 AddFreeChunk(AddrFromChunk(new_chunk), chunk_size - num_bytes, chunk);
260 }
261
262 num_objects_allocated_++;
263 total_objects_allocated_++;
264 num_bytes_allocated_ += num_bytes;
265 total_bytes_allocated_ += num_bytes;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 return reinterpret_cast<mirror::Object*>(addr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700267}
268
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700269void FreeListSpace::Dump(std::ostream& os) const{
270 os << GetName() << " -"
271 << " begin: " << reinterpret_cast<void*>(Begin())
272 << " end: " << reinterpret_cast<void*>(End());
273}
274
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700275}