blob: 434c39efd777c0a15ed38e1c81744061d317eda9 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Elliott Hughes90a33692011-08-30 13:27:07 -070019#include "UniquePtr.h"
Ian Rogers30fab402012-01-23 15:43:46 -080020#include "dlmalloc.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070021#include "file.h"
22#include "image.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "logging.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024#include "os.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070025#include "space_bitmap.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070026#include "stl_util.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29namespace art {
30
Ian Rogers30fab402012-01-23 15:43:46 -080031#ifndef NDEBUG
32#define DEBUG_SPACES 1
33#endif
34
35#define CHECK_MEMORY_CALL(call, args, what) \
36 do { \
37 int rc = call args; \
38 if (UNLIKELY(rc != 0)) { \
39 errno = rc; \
40 PLOG(FATAL) << # call << " failed for " << what; \
41 } \
42 } while (false)
43
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070044size_t AllocSpace::bitmap_index_ = 0;
45
Mathieu Chartiercc236d72012-07-20 10:29:05 -070046AllocSpace::AllocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070047 size_t growth_limit)
Ian Rogers15bf2d32012-08-28 17:33:04 -070048 : Space(name, mem_map, begin, end, GCRP_ALWAYS_COLLECT),
49 lock_("allocation space lock", kAllocSpaceLock), mspace_(mspace),
50 growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070051 CHECK(mspace != NULL);
52
53 size_t bitmap_index = bitmap_index_++;
54
Mathieu Chartiercc236d72012-07-20 10:29:05 -070055 DCHECK(reinterpret_cast<uintptr_t>(mem_map->Begin()) % static_cast<uintptr_t>GC_CARD_SIZE == 0);
56 DCHECK(reinterpret_cast<uintptr_t>(mem_map->End()) % static_cast<uintptr_t>GC_CARD_SIZE == 0);
57
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070058 live_bitmap_.reset(SpaceBitmap::Create(
59 StringPrintf("allocspace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
60 Begin(), Capacity()));
61 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
62
63 mark_bitmap_.reset(SpaceBitmap::Create(
64 StringPrintf("allocspace-%s-mark-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
65 Begin(), Capacity()));
66 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
67}
68
Ian Rogers30fab402012-01-23 15:43:46 -080069AllocSpace* Space::CreateAllocSpace(const std::string& name, size_t initial_size,
70 size_t growth_limit, size_t capacity,
71 byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080072 // Memory we promise to dlmalloc before it asks for morecore.
73 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
74 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
75 // size of the large allocation) will be greater than the footprint limit.
76 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -080077 uint64_t start_time = 0;
78 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
79 start_time = NanoTime();
80 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -080081 << " initial_size=" << PrettySize(initial_size)
82 << " growth_limit=" << PrettySize(growth_limit)
83 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -080084 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -070085 }
Ian Rogers30fab402012-01-23 15:43:46 -080086
87 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -080088 if (starting_size > initial_size) {
89 initial_size = starting_size;
90 }
Ian Rogers30fab402012-01-23 15:43:46 -080091 if (initial_size > growth_limit) {
92 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -080093 << PrettySize(initial_size) << ") is larger than its capacity ("
94 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -080095 return NULL;
96 }
97 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080098 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
99 << PrettySize(growth_limit) << ") is larger than the capacity ("
100 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800101 return NULL;
102 }
103
104 // Page align growth limit and capacity which will be used to manage mmapped storage
105 growth_limit = RoundUp(growth_limit, kPageSize);
106 capacity = RoundUp(capacity, kPageSize);
107
108 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin,
109 capacity, PROT_READ | PROT_WRITE));
110 if (mem_map.get() == NULL) {
111 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers3bb17a62012-01-27 23:56:44 -0800112 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -0800113 return NULL;
114 }
115
Ian Rogers3bb17a62012-01-27 23:56:44 -0800116 void* mspace = AllocSpace::CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800117 if (mspace == NULL) {
118 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
119 return NULL;
120 }
121
Ian Rogers3bb17a62012-01-27 23:56:44 -0800122 // Protect memory beyond the initial size.
123 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800124 if (capacity - initial_size > 0) {
125 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
126 }
127
128 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700129 MemMap* mem_map_ptr = mem_map.release();
130 AllocSpace* space = new AllocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end, growth_limit);
Ian Rogers30fab402012-01-23 15:43:46 -0800131 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800132 LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time)
133 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800134 }
135 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700136}
137
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700138LargeObjectSpace* Space::CreateLargeObjectSpace(const std::string& name) {
139 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
140 VLOG(startup) << "Space::CreateLargeObjectSpace entering " << name;
141 }
142 return new LargeObjectSpace(name);
143}
144
Ian Rogers3bb17a62012-01-27 23:56:44 -0800145void* AllocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800146 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800148 // create mspace using our backing storage starting at begin and with a footprint of
149 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
150 // morecore_start bytes of memory is exhaused morecore will be called.
151 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700152 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800153 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800154 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800156 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700157 }
158 return msp;
159}
160
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700161void AllocSpace::SwapBitmaps() {
162 SpaceBitmap* temp_live_bitmap = live_bitmap_.release();
163 live_bitmap_.reset(mark_bitmap_.release());
164 mark_bitmap_.reset(temp_live_bitmap);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700165 // Swap names to get more descriptive diagnostics.
166 std::string temp_name = live_bitmap_->GetName();
167 live_bitmap_->SetName(mark_bitmap_->GetName());
168 mark_bitmap_->SetName(temp_name);
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700169}
170
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171Object* AllocSpace::AllocWithoutGrowthLocked(size_t num_bytes) {
Ian Rogers30fab402012-01-23 15:43:46 -0800172 Object* result = reinterpret_cast<Object*>(mspace_calloc(mspace_, 1, num_bytes));
173#if DEBUG_SPACES
174 if (result != NULL) {
175 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 << ") not in bounds of allocation space " << *this;
jeffhaoc1160702011-10-27 15:48:45 -0700177 }
Ian Rogers30fab402012-01-23 15:43:46 -0800178#endif
179 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700180}
181
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182Object* AllocSpace::AllocWithoutGrowth(size_t num_bytes) {
183 MutexLock mu(lock_);
184 return AllocWithoutGrowthLocked(num_bytes);
185}
186
Ian Rogers30fab402012-01-23 15:43:46 -0800187Object* AllocSpace::AllocWithGrowth(size_t num_bytes) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800189 // Grow as much as possible within the mspace.
190 size_t max_allowed = Capacity();
191 mspace_set_footprint_limit(mspace_, max_allowed);
192 // Try the allocation.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193 void* ptr = AllocWithoutGrowthLocked(num_bytes);
Ian Rogers30fab402012-01-23 15:43:46 -0800194 // Shrink back down as small as possible.
195 size_t footprint = mspace_footprint(mspace_);
196 mspace_set_footprint_limit(mspace_, footprint);
197 // Return the new allocation or NULL.
198 Object* result = reinterpret_cast<Object*>(ptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199#if DEBUG_SPACES
Ian Rogers30fab402012-01-23 15:43:46 -0800200 CHECK(result == NULL || Contains(result));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201#endif
Ian Rogers30fab402012-01-23 15:43:46 -0800202 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700203}
204
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700205void AllocSpace::SetGrowthLimit(size_t growth_limit) {
206 growth_limit = RoundUp(growth_limit, kPageSize);
207 growth_limit_ = growth_limit;
208 if (Size() > growth_limit_) {
209 end_ = begin_ + growth_limit;
210 }
211}
212
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700213AllocSpace* AllocSpace::CreateZygoteSpace() {
214 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
215 DCHECK(IsAligned<GC_CARD_SIZE>(begin_));
216 DCHECK(IsAligned<GC_CARD_SIZE>(end_));
217 DCHECK(IsAligned<kPageSize>(begin_));
218 DCHECK(IsAligned<kPageSize>(end_));
219 size_t size = RoundUp(Size(), kPageSize);
220 // Trim the heap so that we minimize the size of the Zygote space.
221 Trim();
222 // Trim our mem-map to free unused pages.
223 mem_map_->UnMapAtEnd(end_);
224 // TODO: Not hardcode these in?
225 const size_t starting_size = kPageSize;
226 const size_t initial_size = 2 * MB;
227 // Remaining size is for the new alloc space.
228 const size_t growth_limit = growth_limit_ - size;
229 const size_t capacity = Capacity() - size;
230 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_);
231 VLOG(heap) << "End " << reinterpret_cast<const void*>(end_);
232 VLOG(heap) << "Size " << size;
233 VLOG(heap) << "GrowthLimit " << growth_limit_;
234 VLOG(heap) << "Capacity " << Capacity();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700235 SetGrowthLimit(RoundUp(size, kPageSize));
236 SetFootprintLimit(RoundUp(size, kPageSize));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700237 // FIXME: Do we need reference counted pointers here?
238 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700239 VLOG(heap) << "Creating new AllocSpace: ";
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700240 VLOG(heap) << "Size " << mem_map_->Size();
241 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
242 VLOG(heap) << "Capacity " << PrettySize(capacity);
243 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name_.c_str(), end_, capacity, PROT_READ | PROT_WRITE));
244 void* mspace = CreateMallocSpace(end_, starting_size, initial_size);
245 // Protect memory beyond the initial size.
246 byte* end = mem_map->Begin() + starting_size;
247 if (capacity - initial_size > 0) {
248 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name_.c_str());
249 }
250 AllocSpace* alloc_space = new AllocSpace(name_, mem_map.release(), mspace, end_, end, growth_limit);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700251 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(end_));
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700252 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(end_));
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700253 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(end_));
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700254 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(end_));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700255 name_ += "-zygote-transformed";
256 VLOG(heap) << "zygote space creation done";
257 return alloc_space;
258}
259
Ian Rogers30fab402012-01-23 15:43:46 -0800260void AllocSpace::Free(Object* ptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800262#if DEBUG_SPACES
263 CHECK(ptr != NULL);
264 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
265#endif
266 mspace_free(mspace_, ptr);
267}
268
269void AllocSpace::FreeList(size_t num_ptrs, Object** ptrs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800271#if DEBUG_SPACES
272 CHECK(ptrs != NULL);
273 size_t num_broken_ptrs = 0;
274 for (size_t i = 0; i < num_ptrs; i++) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700275 if (!Contains(ptrs[i])) {
Ian Rogers30fab402012-01-23 15:43:46 -0800276 num_broken_ptrs++;
277 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
Mathieu Chartier0325e622012-09-05 14:22:51 -0700278 } else {
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700279 size_t size = mspace_usable_size(ptrs[i]);
280 memset(ptrs[i], 0xEF, size);
Ian Rogers30fab402012-01-23 15:43:46 -0800281 }
282 }
283 CHECK_EQ(num_broken_ptrs, 0u);
284#endif
285 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
286}
287
288// Callback from dlmalloc when it needs to increase the footprint
289extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700291 DCHECK_EQ(heap->GetAllocSpace()->GetMspace(), mspace);
292 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800293}
294
295void* AllocSpace::MoreCore(intptr_t increment) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 lock_.AssertHeld();
Ian Rogers30fab402012-01-23 15:43:46 -0800297 byte* original_end = end_;
298 if (increment != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800299 VLOG(heap) << "AllocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800300 byte* new_end = original_end + increment;
301 if (increment > 0) {
302#if DEBUG_SPACES
303 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
304 // by mspace_set_footprint_limit.
305 CHECK_LE(new_end, Begin() + Capacity());
306#endif
307 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetSpaceName());
308 } else {
309#if DEBUG_SPACES
310 // Should never be asked for negative footprint (ie before begin)
311 CHECK_GT(original_end + increment, Begin());
312#endif
313 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800314 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
315 // expensive (note the same isn't true for giving permissions to a page as the protected
316 // page shouldn't be in a TLB). We should investigate performance impact of just
317 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
318 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800319 size_t size = -increment;
320 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetSpaceName());
321 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetSpaceName());
322 }
323 // Update end_
324 end_ = new_end;
325 }
326 return original_end;
327}
328
329size_t AllocSpace::AllocationSize(const Object* obj) {
330 return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) + kChunkOverhead;
331}
332
Brian Carlstromb18e77a2012-08-21 14:20:03 -0700333void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* /* arg */) {
334 // Is this chunk in use?
335 if (used_bytes != 0) {
336 return;
337 }
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700338 // Do we have any whole pages to give back?
339 start = reinterpret_cast<void*>(RoundUp(reinterpret_cast<uintptr_t>(start), kPageSize));
340 end = reinterpret_cast<void*>(RoundDown(reinterpret_cast<uintptr_t>(end), kPageSize));
341 if (end > start) {
342 size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start);
343 CHECK_MEMORY_CALL(madvise, (start, length, MADV_DONTNEED), "trim");
Ian Rogers30fab402012-01-23 15:43:46 -0800344 }
345}
346
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700347void AllocSpace::Trim() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 MutexLock mu(lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700349 // Trim to release memory at the end of the space.
350 mspace_trim(mspace_, 0);
351 // Visit space looking for page-sized holes to advise the kernel we don't need.
352 mspace_inspect_all(mspace_, MspaceMadviseCallback, NULL);
353}
Ian Rogers30fab402012-01-23 15:43:46 -0800354
355void AllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
356 void* arg) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800358 mspace_inspect_all(mspace_, callback, arg);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700359 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800360}
361
362size_t AllocSpace::GetFootprintLimit() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800364 return mspace_footprint_limit(mspace_);
365}
366
367void AllocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 MutexLock mu(lock_);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800369 VLOG(heap) << "AllocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800370 // Compare against the actual footprint, rather than the Size(), because the heap may not have
371 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800372 size_t current_space_size = mspace_footprint(mspace_);
373 if (new_size < current_space_size) {
374 // Don't let the space grow any more.
375 new_size = current_space_size;
376 }
377 mspace_set_footprint_limit(mspace_, new_size);
378}
379
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700380size_t ImageSpace::bitmap_index_ = 0;
381
382ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700383 : Space(name, mem_map, mem_map->Begin(), mem_map->End(), GCRP_NEVER_COLLECT) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700384 const size_t bitmap_index = bitmap_index_++;
385 live_bitmap_.reset(SpaceBitmap::Create(
386 StringPrintf("imagespace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
387 Begin(), Capacity()));
388 DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index;
389}
390
Ian Rogers30fab402012-01-23 15:43:46 -0800391ImageSpace* Space::CreateImageSpace(const std::string& image_file_name) {
Brian Carlstrom5643b782012-02-05 12:32:53 -0800392 CHECK(!image_file_name.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800393
394 uint64_t start_time = 0;
395 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
396 start_time = NanoTime();
397 LOG(INFO) << "Space::CreateImageSpace entering" << " image_file_name=" << image_file_name;
398 }
399
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700400 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700401 if (file.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800402 LOG(ERROR) << "Failed to open " << image_file_name;
403 return NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700404 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700405 ImageHeader image_header;
406 bool success = file->ReadFully(&image_header, sizeof(image_header));
407 if (!success || !image_header.IsValid()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800408 LOG(ERROR) << "Invalid image header " << image_file_name;
409 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700410 }
Ian Rogers30fab402012-01-23 15:43:46 -0800411 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800412 file->Length(),
413 // TODO: selectively PROT_EXEC stubs
414 PROT_READ | PROT_WRITE | PROT_EXEC,
415 MAP_PRIVATE | MAP_FIXED,
416 file->Fd(),
417 0));
Elliott Hughes90a33692011-08-30 13:27:07 -0700418 if (map.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800419 LOG(ERROR) << "Failed to map " << image_file_name;
420 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700421 }
Ian Rogers30fab402012-01-23 15:43:46 -0800422 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
423 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700424
Ian Rogers30fab402012-01-23 15:43:46 -0800425 Runtime* runtime = Runtime::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -0700426 Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray);
Ian Rogers169c9a72011-11-13 20:13:17 -0800427 runtime->SetJniDlsymLookupStub(down_cast<ByteArray*>(jni_stub_array));
Brian Carlstrom16192862011-09-12 17:50:06 -0700428
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429 Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700430 runtime->SetAbstractMethodErrorStubArray(down_cast<ByteArray*>(ame_stub_array));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700431
Ian Rogersfb6adba2012-03-04 21:51:51 -0800432 Object* resolution_stub_array =
433 image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700434 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700435 down_cast<ByteArray*>(resolution_stub_array), Runtime::kStaticMethod);
436 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700437 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700438 down_cast<ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700439
Ian Rogers19846512012-02-24 11:42:47 -0800440 Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700441 runtime->SetResolutionMethod(down_cast<AbstractMethod*>(resolution_method));
Ian Rogers19846512012-02-24 11:42:47 -0800442
Ian Rogersff1ed472011-09-20 13:46:24 -0700443 Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700444 runtime->SetCalleeSaveMethod(down_cast<AbstractMethod*>(callee_save_method), Runtime::kSaveAll);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700445 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700446 runtime->SetCalleeSaveMethod(down_cast<AbstractMethod*>(callee_save_method), Runtime::kRefsOnly);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700447 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700448 runtime->SetCalleeSaveMethod(down_cast<AbstractMethod*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogersff1ed472011-09-20 13:46:24 -0700449
Ian Rogers30fab402012-01-23 15:43:46 -0800450 ImageSpace* space = new ImageSpace(image_file_name, map.release());
451 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800452 LOG(INFO) << "Space::CreateImageSpace exiting (" << PrettyDuration(NanoTime() - start_time)
453 << ") " << *space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700454 }
Ian Rogers30fab402012-01-23 15:43:46 -0800455 return space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700456}
457
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700458void ImageSpace::RecordImageAllocations(SpaceBitmap* live_bitmap) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800459 uint64_t start_time = 0;
460 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
461 LOG(INFO) << "ImageSpace::RecordImageAllocations entering";
462 start_time = NanoTime();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700463 }
Ian Rogers30fab402012-01-23 15:43:46 -0800464 DCHECK(!Runtime::Current()->IsStarted());
465 CHECK(live_bitmap != NULL);
466 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
467 byte* end = End();
468 while (current < end) {
469 DCHECK_ALIGNED(current, kObjectAlignment);
470 const Object* obj = reinterpret_cast<const Object*>(current);
471 live_bitmap->Set(obj);
472 current += RoundUp(obj->SizeOf(), kObjectAlignment);
473 }
474 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800475 LOG(INFO) << "ImageSpace::RecordImageAllocations exiting ("
476 << PrettyDuration(NanoTime() - start_time) << ")";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700477 }
478}
479
Ian Rogers30fab402012-01-23 15:43:46 -0800480std::ostream& operator<<(std::ostream& os, const Space& space) {
481 os << (space.IsImageSpace() ? "Image" : "Alloc") << "Space["
482 << "begin=" << reinterpret_cast<void*>(space.Begin())
483 << ",end=" << reinterpret_cast<void*>(space.End())
Ian Rogers3bb17a62012-01-27 23:56:44 -0800484 << ",size=" << PrettySize(space.Size()) << ",capacity=" << PrettySize(space.Capacity())
Ian Rogers30fab402012-01-23 15:43:46 -0800485 << ",name=\"" << space.GetSpaceName() << "\"]";
486 return os;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700487}
488
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700489LargeObjectSpace::LargeObjectSpace(const std::string& name)
490 : Space(name, NULL, NULL, NULL, GCRP_ALWAYS_COLLECT),
491 lock_("large object space lock", kAllocSpaceLock),
492 num_bytes_allocated_(0) {
493 live_objects_.reset(new SpaceSetMap("large live objects"));
494 mark_objects_.reset(new SpaceSetMap("large marked objects"));
495}
496
497void LargeObjectSpace::SwapBitmaps() {
498 MutexLock mu(lock_);
499 SpaceSetMap* temp_live_objects = live_objects_.release();
500 live_objects_.reset(mark_objects_.release());
501 mark_objects_.reset(temp_live_objects);
502 // Swap names to get more descriptive diagnostics.
503 std::string temp_name = live_objects_->GetName();
504 live_objects_->SetName(mark_objects_->GetName());
505 mark_objects_->SetName(temp_name);
506}
507
508Object* LargeObjectSpace::Alloc(size_t num_bytes) {
509 MutexLock mu(lock_);
510 MemMap* mem_map = MemMap::MapAnonymous("allocation", NULL, num_bytes, PROT_READ | PROT_WRITE);
511 if (mem_map == NULL) {
512 return NULL;
513 }
514 Object* obj = reinterpret_cast<Object*>(mem_map->Begin());
515 large_objects_.push_back(obj);
516 mem_maps_.Put(obj, mem_map);
517 num_bytes_allocated_ += mem_map->Size();
518 return obj;
519}
520
521void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700522 mark_objects_->CopyFrom(*live_objects_.get());
523}
524
525void LargeObjectSpace::Free(Object* ptr) {
526 MutexLock mu(lock_);
527 MemMaps::iterator found = mem_maps_.find(ptr);
528 CHECK(found != mem_maps_.end()) << "Attempted to free large object which was not live";
529 DCHECK_GE(num_bytes_allocated_, found->second->Size());
530 num_bytes_allocated_ -= found->second->Size();
531 delete found->second;
532 mem_maps_.erase(found);
533}
534
535size_t LargeObjectSpace::AllocationSize(const Object* obj) {
536 MutexLock mu(lock_);
537 MemMaps::iterator found = mem_maps_.find(const_cast<Object*>(obj));
538 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
539 return found->second->Size();
540}
541
542void LargeObjectSpace::Walk(AllocSpace::WalkCallback callback, void* arg) {
543 MutexLock mu(lock_);
544 for (MemMaps::iterator it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
545 MemMap* mem_map = it->second;
546 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
547 callback(NULL, NULL, 0, arg);
548 }
549}
550
Carl Shapiro69759ea2011-07-21 18:13:35 -0700551} // namespace art