blob: d10d14dfc0709afe164363407cc1da282c51fd6f [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()) {
62 LOG(INFO) << "Space::Init entering"
63 << " 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 ("
70 << initial_size << ">" << growth_size << ")";
71 return false;
72 }
73 if (growth_size > maximum_size) {
74 LOG(ERROR) << "Failed to create space with growth size > maximum size ("
75 << growth_size << ">" << maximum_size << ")";
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 Hughes90a33692011-08-30 13:27:07 -070080 UniquePtr<MemMap> mem_map(MemMap::Map(requested_base, length, prot));
81 if (mem_map.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070082 LOG(WARNING) << "Failed to allocate " << length << " bytes for space";
Carl Shapiro69759ea2011-07-21 18:13:35 -070083 return false;
84 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070085 Init(mem_map.release());
86 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) {
92 LOG(WARNING) << "Failed to create mspace for space";
93 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
101void Space::Init(MemMap* mem_map) {
102 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
107
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700108bool Space::InitFromImage(const std::string& image_file_name) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700109 Runtime* runtime = Runtime::Current();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700110 if (runtime->IsVerboseStartup()) {
111 LOG(INFO) << "Space::InitFromImage entering"
112 << " image_file_name=" << image_file_name;
113 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700114 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700115 if (file.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700116 LOG(WARNING) << "Failed to open " << image_file_name;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117 return false;
118 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700119 ImageHeader image_header;
120 bool success = file->ReadFully(&image_header, sizeof(image_header));
121 if (!success || !image_header.IsValid()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700122 LOG(WARNING) << "Invalid image header " << image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700123 return false;
124 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125 UniquePtr<MemMap> map(MemMap::Map(image_header.GetImageBaseAddr(),
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700126 file->Length(),
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127 // TODO: selectively PROT_EXEC an image subset containing stubs
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700128 PROT_READ | PROT_WRITE | PROT_EXEC,
129 MAP_PRIVATE | MAP_FIXED,
130 file->Fd(),
131 0));
Elliott Hughes90a33692011-08-30 13:27:07 -0700132 if (map.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700133 LOG(WARNING) << "Failed to map " << image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700134 return false;
135 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700136 CHECK_EQ(image_header.GetImageBaseAddr(), map->GetAddress());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700137 image_header_ = reinterpret_cast<ImageHeader*>(map->GetAddress());
138 DCHECK_EQ(0, memcmp(&image_header, image_header_, sizeof(ImageHeader)));
139
Brian Carlstrom16192862011-09-12 17:50:06 -0700140 Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700141 runtime->SetJniStubArray(down_cast<ByteArray*>(jni_stub_array));
Brian Carlstrom16192862011-09-12 17:50:06 -0700142
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700144 runtime->SetAbstractMethodErrorStubArray(down_cast<ByteArray*>(ame_stub_array));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145
Ian Rogersad25ac52011-10-04 19:13:33 -0700146 Object* resolution_stub_array = image_header.GetImageRoot(ImageHeader::kInstanceResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700147 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700148 down_cast<ByteArray*>(resolution_stub_array), Runtime::kInstanceMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700149 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700150 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700151 down_cast<ByteArray*>(resolution_stub_array), Runtime::kStaticMethod);
152 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700153 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700154 down_cast<ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700155
Ian Rogersff1ed472011-09-20 13:46:24 -0700156 Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700157 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kSaveAll);
158 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
159 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsOnly);
160 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
161 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogersff1ed472011-09-20 13:46:24 -0700162
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700163 Init(map.release());
jeffhaoc1160702011-10-27 15:48:45 -0700164 growth_limit_ = limit_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700165 if (runtime->IsVerboseStartup()) {
166 LOG(INFO) << "Space::InitFromImage exiting";
167 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168 return true;
169}
170
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171Object* Space::AllocWithoutGrowth(size_t num_bytes) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700172 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700173 return reinterpret_cast<Object*>(mspace_calloc(mspace_, 1, num_bytes));
174}
175
176Object* Space::AllocWithGrowth(size_t num_bytes) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700177 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700178 // Grow as much as possible within the mspace.
jeffhaoc1160702011-10-27 15:48:45 -0700179 size_t max_allowed = growth_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700180 mspace_set_max_allowed_footprint(mspace_, max_allowed);
181 // Try the allocation.
182 void* ptr = AllocWithoutGrowth(num_bytes);
183 // Shrink back down as small as possible.
184 size_t footprint = mspace_footprint(mspace_);
185 mspace_set_max_allowed_footprint(mspace_, footprint);
186 // Return the new allocation or NULL.
187 return reinterpret_cast<Object*>(ptr);
188}
189
190size_t Space::Free(void* ptr) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700191 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700192 DCHECK(ptr != NULL);
193 size_t num_bytes = mspace_usable_size(mspace_, ptr);
194 mspace_free(mspace_, ptr);
195 return num_bytes;
196}
197
Ian Rogers5d76c432011-10-31 21:42:49 -0700198size_t Space::FreeList(size_t num_ptrs, void** ptrs) {
199 DCHECK(mspace_ != NULL);
200 DCHECK(ptrs != NULL);
201 void* merged = ptrs[0];
202 size_t num_bytes = 0;
203 for (size_t i = 1; i < num_ptrs; i++) {
204 num_bytes += mspace_usable_size(mspace_, ptrs[i]);
205 if (mspace_merge_objects(mspace_, merged, ptrs[i]) == NULL) {
206 mspace_free(mspace_, merged);
207 merged = ptrs[i];
208 }
209 }
210 CHECK(merged != NULL);
211 mspace_free(mspace_, merged);
212 return num_bytes;
213}
214
Carl Shapiro58551df2011-07-24 03:09:51 -0700215size_t Space::AllocationSize(const Object* obj) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700216 DCHECK(mspace_ != NULL);
Carl Shapiro58551df2011-07-24 03:09:51 -0700217 return mspace_usable_size(mspace_, obj) + kChunkOverhead;
218}
219
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220void Space::DontNeed(void* start, void* end, void* num_bytes) {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700221 start = (void*)RoundUp((uintptr_t)start, kPageSize);
222 end = (void*)RoundDown((uintptr_t)end, kPageSize);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223 if (start >= end) {
224 return;
225 }
226 size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start);
227 int result = madvise(start, length, MADV_DONTNEED);
228 if (result == -1) {
229 PLOG(WARNING) << "madvise failed";
230 } else {
231 *reinterpret_cast<size_t*>(num_bytes) += length;
232 }
233}
234
235void Space::Trim() {
236 CHECK(mspace_ != NULL);
237 mspace_trim(mspace_, 0);
238 size_t num_bytes_released = 0;
239 mspace_walk_free_pages(mspace_, DontNeed, &num_bytes_released);
240}
241
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700242void Space::Walk(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg) {
243 if (mspace_ != NULL) {
244 mspace_walk_heap(mspace_, callback, arg);
245 }
246}
247
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700248size_t Space::GetMaxAllowedFootprint() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700249 DCHECK(mspace_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250 return mspace_max_allowed_footprint(mspace_);
251}
252
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700253void Space::SetMaxAllowedFootprint(size_t limit) {
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700254 DCHECK(mspace_ != NULL);
255
256 // Compare against the actual footprint, rather than the
257 // max_allowed, because the heap may not have grown all the
258 // way to the allowed size yet.
259 //
260 size_t current_space_size = mspace_footprint(mspace_);
261 if (limit < current_space_size) {
262 // Don't let the space grow any more.
263 mspace_set_max_allowed_footprint(mspace_, current_space_size);
264 } else {
265 // Let the heap grow to the requested limit.
266 mspace_set_max_allowed_footprint(mspace_, limit);
267 }
268}
269
Carl Shapiro69759ea2011-07-21 18:13:35 -0700270void Space::Grow(size_t new_size) {
Elliott Hughes53b61312011-08-12 18:28:20 -0700271 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272}
273
274} // namespace art