blob: 6421188f815958a9b03676a363fa38e416fd81dd [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07004
5#include <sys/mman.h>
6
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "UniquePtr.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07008#include "file.h"
9#include "image.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070011#include "os.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070013
14namespace art {
15
jeffhaoc1160702011-10-27 15:48:45 -070016Space* Space::Create(const std::string& name, size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base) {
Elliott Hughes307f75d2011-10-12 18:04:40 -070017 UniquePtr<Space> space(new Space(name));
jeffhaoc1160702011-10-27 15:48:45 -070018 bool success = space->Init(initial_size, maximum_size, growth_size, requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -070019 if (!success) {
20 return NULL;
21 } else {
22 return space.release();
23 }
24}
25
Brian Carlstrom58ae9412011-10-04 00:56:06 -070026Space* Space::CreateFromImage(const std::string& image_file_name) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070027 CHECK(image_file_name != NULL);
Elliott Hughes307f75d2011-10-12 18:04:40 -070028 UniquePtr<Space> space(new Space(image_file_name));
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070029 bool success = space->InitFromImage(image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070030 if (!success) {
31 return NULL;
32 } else {
33 return space.release();
34 }
35}
36
37Space::~Space() {}
38
Carl Shapiro69759ea2011-07-21 18:13:35 -070039void* Space::CreateMallocSpace(void* base,
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070040 size_t initial_size,
Carl Shapiro69759ea2011-07-21 18:13:35 -070041 size_t maximum_size) {
42 errno = 0;
43 bool is_locked = false;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070044 size_t commit_size = initial_size / 2;
Carl Shapiro69759ea2011-07-21 18:13:35 -070045 void* msp = create_contiguous_mspace_with_base(commit_size, maximum_size,
46 is_locked, base);
47 if (msp != NULL) {
48 // Do not permit the heap grow past the starting size without our
49 // intervention.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070050 mspace_set_max_allowed_footprint(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -070051 } else {
52 // There is no guarantee that errno has meaning when the call
53 // fails, but it often does.
54 PLOG(ERROR) << "create_contiguous_mspace_with_base failed";
55 }
56 return msp;
57}
58
jeffhaoc1160702011-10-27 15:48:45 -070059bool Space::Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070060 const Runtime* runtime = Runtime::Current();
61 if (runtime->IsVerboseStartup()) {
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080062 LOG(INFO) << "Space::Init entering " << name_
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070063 << " initial_size=" << initial_size
64 << " maximum_size=" << maximum_size
jeffhaoc1160702011-10-27 15:48:45 -070065 << " growth_size=" << growth_size
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070066 << " requested_base=" << reinterpret_cast<void*>(requested_base);
67 }
jeffhaoc1160702011-10-27 15:48:45 -070068 if (initial_size > growth_size) {
69 LOG(ERROR) << "Failed to create space with initial size > growth size ("
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080070 << initial_size << ">" << growth_size << "): " << name_;
jeffhaoc1160702011-10-27 15:48:45 -070071 return false;
72 }
73 if (growth_size > maximum_size) {
74 LOG(ERROR) << "Failed to create space with growth size > maximum size ("
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080075 << growth_size << ">" << maximum_size << "): " << name_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 return false;
77 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070078 size_t length = RoundUp(maximum_size, kPageSize);
Carl Shapiro69759ea2011-07-21 18:13:35 -070079 int prot = PROT_READ | PROT_WRITE;
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080080 UniquePtr<MemMap> mem_map(MemMap::Map(name_.c_str(), requested_base, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070081 if (mem_map.get() == NULL) {
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080082 LOG(WARNING) << "Failed to allocate " << length << " bytes for space: " << name_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070083 return false;
84 }
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080085 InitFromMemMap(mem_map.release());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070086 maximum_size_ = maximum_size;
jeffhaoc1160702011-10-27 15:48:45 -070087 size_t growth_length = RoundUp(growth_size, kPageSize);
88 growth_size_ = growth_size;
89 growth_limit_ = base_ + growth_length;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070090 mspace_ = CreateMallocSpace(base_, initial_size, maximum_size);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070091 if (mspace_ == NULL) {
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080092 LOG(WARNING) << "Failed to create mspace for space: " << name_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070093 return false;
94 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070095 if (runtime->IsVerboseStartup()) {
96 LOG(INFO) << "Space::Init exiting";
97 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070098 return true;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070099}
100
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800101void Space::InitFromMemMap(MemMap* mem_map) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700102 mem_map_.reset(mem_map);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700103 base_ = mem_map_->GetAddress();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700104 limit_ = base_ + mem_map->GetLength();
105}
106
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700107bool Space::InitFromImage(const std::string& image_file_name) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700108 Runtime* runtime = Runtime::Current();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700109 if (runtime->IsVerboseStartup()) {
110 LOG(INFO) << "Space::InitFromImage entering"
111 << " image_file_name=" << image_file_name;
112 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700113 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700114 if (file.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700115 LOG(WARNING) << "Failed to open " << image_file_name;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700116 return false;
117 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700118 ImageHeader image_header;
119 bool success = file->ReadFully(&image_header, sizeof(image_header));
120 if (!success || !image_header.IsValid()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700121 LOG(WARNING) << "Invalid image header " << image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700122 return false;
123 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124 UniquePtr<MemMap> map(MemMap::Map(image_header.GetImageBaseAddr(),
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700125 file->Length(),
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700126 // TODO: selectively PROT_EXEC an image subset containing stubs
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700127 PROT_READ | PROT_WRITE | PROT_EXEC,
128 MAP_PRIVATE | MAP_FIXED,
129 file->Fd(),
130 0));
Elliott Hughes90a33692011-08-30 13:27:07 -0700131 if (map.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700132 LOG(WARNING) << "Failed to map " << image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700133 return false;
134 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 CHECK_EQ(image_header.GetImageBaseAddr(), map->GetAddress());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700136 image_header_ = reinterpret_cast<ImageHeader*>(map->GetAddress());
137 DCHECK_EQ(0, memcmp(&image_header, image_header_, sizeof(ImageHeader)));
138
Brian Carlstrom16192862011-09-12 17:50:06 -0700139 Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray);
Ian Rogers169c9a72011-11-13 20:13:17 -0800140 runtime->SetJniDlsymLookupStub(down_cast<ByteArray*>(jni_stub_array));
Brian Carlstrom16192862011-09-12 17:50:06 -0700141
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700143 runtime->SetAbstractMethodErrorStubArray(down_cast<ByteArray*>(ame_stub_array));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144
Ian Rogersad25ac52011-10-04 19:13:33 -0700145 Object* resolution_stub_array = image_header.GetImageRoot(ImageHeader::kInstanceResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700146 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700147 down_cast<ByteArray*>(resolution_stub_array), Runtime::kInstanceMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700148 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700149 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700150 down_cast<ByteArray*>(resolution_stub_array), Runtime::kStaticMethod);
151 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700152 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700153 down_cast<ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700154
Ian Rogersff1ed472011-09-20 13:46:24 -0700155 Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700156 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kSaveAll);
157 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
158 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsOnly);
159 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
160 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogersff1ed472011-09-20 13:46:24 -0700161
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800162 InitFromMemMap(map.release());
jeffhaoc1160702011-10-27 15:48:45 -0700163 growth_limit_ = limit_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700164 if (runtime->IsVerboseStartup()) {
165 LOG(INFO) << "Space::InitFromImage exiting";
166 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700167 return true;
168}
169
Carl Shapiro69759ea2011-07-21 18:13:35 -0700170Object* Space::AllocWithoutGrowth(size_t num_bytes) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700171 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700172 return reinterpret_cast<Object*>(mspace_calloc(mspace_, 1, num_bytes));
173}
174
175Object* Space::AllocWithGrowth(size_t num_bytes) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700176 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177 // Grow as much as possible within the mspace.
jeffhaoc1160702011-10-27 15:48:45 -0700178 size_t max_allowed = growth_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700179 mspace_set_max_allowed_footprint(mspace_, max_allowed);
180 // Try the allocation.
181 void* ptr = AllocWithoutGrowth(num_bytes);
182 // Shrink back down as small as possible.
183 size_t footprint = mspace_footprint(mspace_);
184 mspace_set_max_allowed_footprint(mspace_, footprint);
185 // Return the new allocation or NULL.
186 return reinterpret_cast<Object*>(ptr);
187}
188
189size_t Space::Free(void* ptr) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700190 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700191 DCHECK(ptr != NULL);
192 size_t num_bytes = mspace_usable_size(mspace_, ptr);
193 mspace_free(mspace_, ptr);
194 return num_bytes;
195}
196
Ian Rogers5d76c432011-10-31 21:42:49 -0700197size_t Space::FreeList(size_t num_ptrs, void** ptrs) {
198 DCHECK(mspace_ != NULL);
199 DCHECK(ptrs != NULL);
200 void* merged = ptrs[0];
201 size_t num_bytes = 0;
202 for (size_t i = 1; i < num_ptrs; i++) {
203 num_bytes += mspace_usable_size(mspace_, ptrs[i]);
204 if (mspace_merge_objects(mspace_, merged, ptrs[i]) == NULL) {
205 mspace_free(mspace_, merged);
206 merged = ptrs[i];
207 }
208 }
209 CHECK(merged != NULL);
210 mspace_free(mspace_, merged);
211 return num_bytes;
212}
213
Carl Shapiro58551df2011-07-24 03:09:51 -0700214size_t Space::AllocationSize(const Object* obj) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700215 DCHECK(mspace_ != NULL);
Carl Shapiro58551df2011-07-24 03:09:51 -0700216 return mspace_usable_size(mspace_, obj) + kChunkOverhead;
217}
218
Carl Shapiro69759ea2011-07-21 18:13:35 -0700219void Space::DontNeed(void* start, void* end, void* num_bytes) {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700220 start = (void*)RoundUp((uintptr_t)start, kPageSize);
221 end = (void*)RoundDown((uintptr_t)end, kPageSize);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222 if (start >= end) {
223 return;
224 }
225 size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start);
226 int result = madvise(start, length, MADV_DONTNEED);
227 if (result == -1) {
228 PLOG(WARNING) << "madvise failed";
229 } else {
230 *reinterpret_cast<size_t*>(num_bytes) += length;
231 }
232}
233
234void Space::Trim() {
235 CHECK(mspace_ != NULL);
236 mspace_trim(mspace_, 0);
237 size_t num_bytes_released = 0;
238 mspace_walk_free_pages(mspace_, DontNeed, &num_bytes_released);
239}
240
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700241void Space::Walk(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg) {
242 if (mspace_ != NULL) {
243 mspace_walk_heap(mspace_, callback, arg);
244 }
245}
246
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700247size_t Space::GetMaxAllowedFootprint() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700248 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700249 return mspace_max_allowed_footprint(mspace_);
250}
251
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700252void Space::SetMaxAllowedFootprint(size_t limit) {
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700253 DCHECK(mspace_ != NULL);
254
255 // Compare against the actual footprint, rather than the
256 // max_allowed, because the heap may not have grown all the
257 // way to the allowed size yet.
258 //
259 size_t current_space_size = mspace_footprint(mspace_);
260 if (limit < current_space_size) {
261 // Don't let the space grow any more.
262 mspace_set_max_allowed_footprint(mspace_, current_space_size);
263 } else {
264 // Let the heap grow to the requested limit.
265 mspace_set_max_allowed_footprint(mspace_, limit);
266 }
267}
268
Carl Shapiro69759ea2011-07-21 18:13:35 -0700269void Space::Grow(size_t new_size) {
Elliott Hughes53b61312011-08-12 18:28:20 -0700270 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700271}
272
273} // namespace art