Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "space.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 4 | |
| 5 | #include <sys/mman.h> |
| 6 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 7 | #include "UniquePtr.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 8 | #include "file.h" |
| 9 | #include "image.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "logging.h" |
| 11 | #include "mspace.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 12 | #include "os.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "utils.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 14 | |
| 15 | namespace art { |
| 16 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 17 | Space* Space::Create(size_t initial_size, size_t maximum_size, byte* requested_base) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 18 | UniquePtr<Space> space(new Space()); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 19 | bool success = space->Init(initial_size, maximum_size, requested_base); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 20 | if (!success) { |
| 21 | return NULL; |
| 22 | } else { |
| 23 | return space.release(); |
| 24 | } |
| 25 | } |
| 26 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 27 | Space* Space::Create(const char* image_file_name) { |
| 28 | CHECK(image_file_name != NULL); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 29 | UniquePtr<Space> space(new Space()); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 30 | bool success = space->Init(image_file_name); |
| 31 | if (!success) { |
| 32 | return NULL; |
| 33 | } else { |
| 34 | return space.release(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | Space::~Space() {} |
| 39 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 40 | void* Space::CreateMallocSpace(void* base, |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 41 | size_t initial_size, |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 42 | size_t maximum_size) { |
| 43 | errno = 0; |
| 44 | bool is_locked = false; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 45 | size_t commit_size = initial_size / 2; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 46 | void* msp = create_contiguous_mspace_with_base(commit_size, maximum_size, |
| 47 | is_locked, base); |
| 48 | if (msp != NULL) { |
| 49 | // Do not permit the heap grow past the starting size without our |
| 50 | // intervention. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 51 | mspace_set_max_allowed_footprint(msp, initial_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 52 | } else { |
| 53 | // There is no guarantee that errno has meaning when the call |
| 54 | // fails, but it often does. |
| 55 | PLOG(ERROR) << "create_contiguous_mspace_with_base failed"; |
| 56 | } |
| 57 | return msp; |
| 58 | } |
| 59 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 60 | bool Space::Init(size_t initial_size, size_t maximum_size, byte* requested_base) { |
| 61 | if (!(initial_size <= maximum_size)) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 62 | return false; |
| 63 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 64 | size_t length = RoundUp(maximum_size, kPageSize); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 65 | int prot = PROT_READ | PROT_WRITE; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 66 | UniquePtr<MemMap> mem_map(MemMap::Map(requested_base, length, prot)); |
| 67 | if (mem_map.get() == NULL) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 68 | return false; |
| 69 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 70 | Init(mem_map.release()); |
| 71 | maximum_size_ = maximum_size; |
| 72 | mspace_ = CreateMallocSpace(base_, initial_size, maximum_size); |
| 73 | return (mspace_ != NULL); |
| 74 | } |
| 75 | |
| 76 | void Space::Init(MemMap* mem_map) { |
| 77 | mem_map_.reset(mem_map); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 78 | base_ = mem_map_->GetAddress(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 79 | limit_ = base_ + mem_map->GetLength(); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | bool Space::Init(const char* image_file_name) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 84 | UniquePtr<File> file(OS::OpenFile(image_file_name, false)); |
| 85 | if (file.get() == NULL) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 86 | return false; |
| 87 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 88 | ImageHeader image_header; |
| 89 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 90 | if (!success || !image_header.IsValid()) { |
| 91 | return false; |
| 92 | } |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 93 | UniquePtr<MemMap> map(MemMap::Map(image_header.GetBaseAddr(), |
| 94 | file->Length(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, |
| 95 | file->Fd(), 0)); |
| 96 | if (map.get() == NULL) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | CHECK_EQ(image_header.GetBaseAddr(), map->GetAddress()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 100 | image_header_ = reinterpret_cast<ImageHeader*>(map->GetAddress()); |
| 101 | DCHECK_EQ(0, memcmp(&image_header, image_header_, sizeof(ImageHeader))); |
| 102 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 103 | Init(map.release()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 107 | Object* Space::AllocWithoutGrowth(size_t num_bytes) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 108 | DCHECK(mspace_ != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 109 | return reinterpret_cast<Object*>(mspace_calloc(mspace_, 1, num_bytes)); |
| 110 | } |
| 111 | |
| 112 | Object* Space::AllocWithGrowth(size_t num_bytes) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 113 | DCHECK(mspace_ != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 114 | // Grow as much as possible within the mspace. |
| 115 | size_t max_allowed = maximum_size_; |
| 116 | mspace_set_max_allowed_footprint(mspace_, max_allowed); |
| 117 | // Try the allocation. |
| 118 | void* ptr = AllocWithoutGrowth(num_bytes); |
| 119 | // Shrink back down as small as possible. |
| 120 | size_t footprint = mspace_footprint(mspace_); |
| 121 | mspace_set_max_allowed_footprint(mspace_, footprint); |
| 122 | // Return the new allocation or NULL. |
| 123 | return reinterpret_cast<Object*>(ptr); |
| 124 | } |
| 125 | |
| 126 | size_t Space::Free(void* ptr) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 127 | DCHECK(mspace_ != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 128 | DCHECK(ptr != NULL); |
| 129 | size_t num_bytes = mspace_usable_size(mspace_, ptr); |
| 130 | mspace_free(mspace_, ptr); |
| 131 | return num_bytes; |
| 132 | } |
| 133 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 134 | size_t Space::AllocationSize(const Object* obj) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 135 | DCHECK(mspace_ != NULL); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 136 | return mspace_usable_size(mspace_, obj) + kChunkOverhead; |
| 137 | } |
| 138 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 139 | void Space::DontNeed(void* start, void* end, void* num_bytes) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 140 | start = (void*)RoundUp((uintptr_t)start, kPageSize); |
| 141 | end = (void*)RoundDown((uintptr_t)end, kPageSize); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 142 | if (start >= end) { |
| 143 | return; |
| 144 | } |
| 145 | size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start); |
| 146 | int result = madvise(start, length, MADV_DONTNEED); |
| 147 | if (result == -1) { |
| 148 | PLOG(WARNING) << "madvise failed"; |
| 149 | } else { |
| 150 | *reinterpret_cast<size_t*>(num_bytes) += length; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void Space::Trim() { |
| 155 | CHECK(mspace_ != NULL); |
| 156 | mspace_trim(mspace_, 0); |
| 157 | size_t num_bytes_released = 0; |
| 158 | mspace_walk_free_pages(mspace_, DontNeed, &num_bytes_released); |
| 159 | } |
| 160 | |
| 161 | size_t Space::MaxAllowedFootprint() { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 162 | DCHECK(mspace_ != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 163 | return mspace_max_allowed_footprint(mspace_); |
| 164 | } |
| 165 | |
| 166 | void Space::Grow(size_t new_size) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 167 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | } // namespace art |