Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "heap.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/wait.h> |
| 21 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 22 | #include <limits> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 25 | #include "card_table.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 26 | #include "debugger.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 27 | #include "image.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 28 | #include "mark_sweep.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "object_utils.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 31 | #include "os.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 32 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 33 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 34 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 35 | #include "timing_logger.h" |
| 36 | #include "UniquePtr.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 40 | std::vector<Space*> Heap::spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 41 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 42 | AllocSpace* Heap::alloc_space_ = NULL; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 43 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 44 | size_t Heap::num_bytes_allocated_ = 0; |
| 45 | |
| 46 | size_t Heap::num_objects_allocated_ = 0; |
| 47 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 48 | bool Heap::is_gc_running_ = false; |
| 49 | |
| 50 | HeapBitmap* Heap::mark_bitmap_ = NULL; |
| 51 | |
| 52 | HeapBitmap* Heap::live_bitmap_ = NULL; |
| 53 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 54 | CardTable* Heap::card_table_ = NULL; |
| 55 | |
| 56 | bool Heap::card_marking_disabled_ = false; |
| 57 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 58 | Class* Heap::java_lang_ref_FinalizerReference_ = NULL; |
| 59 | Class* Heap::java_lang_ref_ReferenceQueue_ = NULL; |
| 60 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 61 | MemberOffset Heap::reference_referent_offset_ = MemberOffset(0); |
| 62 | MemberOffset Heap::reference_queue_offset_ = MemberOffset(0); |
| 63 | MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0); |
| 64 | MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0); |
| 65 | MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 66 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 67 | float Heap::target_utilization_ = 0.5; |
| 68 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 69 | Mutex* Heap::lock_ = NULL; |
| 70 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 71 | bool Heap::verify_objects_ = false; |
| 72 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 73 | static void UpdateFirstAndLastSpace(Space** first_space, Space** last_space, Space* space) { |
| 74 | if (*first_space == NULL) { |
| 75 | *first_space = space; |
| 76 | *last_space = space; |
| 77 | } else { |
| 78 | if ((*first_space)->Begin() > space->Begin()) { |
| 79 | *first_space = space; |
| 80 | } else if (space->Begin() > (*last_space)->Begin()) { |
| 81 | *last_space = space; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 86 | bool GenerateImage(const std::string image_file_name) { |
| 87 | const std::string boot_class_path_string = Runtime::Current()->GetBootClassPath(); |
| 88 | std::vector<std::string> boot_class_path; |
| 89 | Split(boot_class_path_string, ':', boot_class_path); |
| 90 | |
| 91 | std::vector<char*> arg_vector; |
| 92 | |
| 93 | std::string dex2oat_string(GetAndroidRoot()); |
| 94 | dex2oat_string += "/bin/dex2oat"; |
| 95 | #ifndef NDEBUG |
| 96 | dex2oat_string += 'd'; |
| 97 | #endif |
| 98 | const char* dex2oat = dex2oat_string.c_str(); |
| 99 | arg_vector.push_back(strdup(dex2oat)); |
| 100 | |
| 101 | std::string image_option_string("--image="); |
| 102 | image_option_string += image_file_name; |
| 103 | const char* image_option = image_option_string.c_str(); |
| 104 | arg_vector.push_back(strdup(image_option)); |
| 105 | |
| 106 | arg_vector.push_back(strdup("--runtime-arg")); |
| 107 | arg_vector.push_back(strdup("-Xms64m")); |
| 108 | |
| 109 | arg_vector.push_back(strdup("--runtime-arg")); |
| 110 | arg_vector.push_back(strdup("-Xmx64m")); |
| 111 | |
| 112 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 113 | std::string dex_file_option_string("--dex-file="); |
| 114 | dex_file_option_string += boot_class_path[i]; |
| 115 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 116 | arg_vector.push_back(strdup(dex_file_option)); |
| 117 | } |
| 118 | |
| 119 | std::string oat_file_option_string("--oat-file="); |
| 120 | oat_file_option_string += image_file_name; |
| 121 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 122 | oat_file_option_string += "oat"; |
| 123 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 124 | arg_vector.push_back(strdup(oat_file_option)); |
| 125 | |
| 126 | arg_vector.push_back(strdup("--base=0x60000000")); |
| 127 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame^] | 128 | std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 129 | LOG(INFO) << command_line; |
| 130 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame^] | 131 | arg_vector.push_back(NULL); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 132 | char** argv = &arg_vector[0]; |
| 133 | |
| 134 | // fork and exec dex2oat |
| 135 | pid_t pid = fork(); |
| 136 | if (pid == 0) { |
| 137 | // no allocation allowed between fork and exec |
| 138 | |
| 139 | // change process groups, so we don't get reaped by ProcessManager |
| 140 | setpgid(0, 0); |
| 141 | |
| 142 | execv(dex2oat, argv); |
| 143 | |
| 144 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 145 | return false; |
| 146 | } else { |
| 147 | STLDeleteElements(&arg_vector); |
| 148 | |
| 149 | // wait for dex2oat to finish |
| 150 | int status; |
| 151 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 152 | if (got_pid != pid) { |
| 153 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 154 | return false; |
| 155 | } |
| 156 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 157 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 164 | void Heap::Init(size_t initial_size, size_t growth_limit, size_t capacity, |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 165 | const std::string& original_image_file_name) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 166 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 167 | LOG(INFO) << "Heap::Init entering"; |
| 168 | } |
| 169 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 170 | // Compute the bounds of all spaces for allocating live and mark bitmaps |
| 171 | // there will be at least one space (the alloc space) |
| 172 | Space* first_space = NULL; |
| 173 | Space* last_space = NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 174 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 175 | // Requested begin for the alloc space, to follow the mapped image and oat files |
| 176 | byte* requested_begin = NULL; |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 177 | std::string image_file_name(original_image_file_name); |
| 178 | if (!image_file_name.empty()) { |
| 179 | ImageSpace* space = NULL; |
| 180 | if (OS::FileExists(image_file_name.c_str())) { |
| 181 | // If the /system file exists, it should be up-to-date, don't try to generate |
| 182 | space = Space::CreateImageSpace(image_file_name); |
| 183 | } else { |
| 184 | // If the /system file didn't exist, we need to use one from the art-cache. |
| 185 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 186 | // If it does not exist, generate. |
| 187 | image_file_name = GetArtCacheFilenameOrDie(image_file_name); |
| 188 | if (OS::FileExists(image_file_name.c_str())) { |
| 189 | space = Space::CreateImageSpace(image_file_name); |
| 190 | } |
| 191 | if (space == NULL) { |
| 192 | if (!GenerateImage(image_file_name)) { |
| 193 | LOG(FATAL) << "Failed to generate image: " << image_file_name; |
| 194 | } |
| 195 | space = Space::CreateImageSpace(image_file_name); |
| 196 | } |
| 197 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 198 | if (space == NULL) { |
Brian Carlstrom | 223f20f | 2012-02-04 23:06:55 -0800 | [diff] [blame] | 199 | LOG(FATAL) << "Failed to create space from " << image_file_name; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 200 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 201 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 202 | AddSpace(space); |
| 203 | UpdateFirstAndLastSpace(&first_space, &last_space, space); |
| 204 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 205 | // isn't going to get in the middle |
| 206 | byte* oat_end_addr = space->GetImageHeader().GetOatEnd(); |
| 207 | CHECK(oat_end_addr > space->End()); |
| 208 | if (oat_end_addr > requested_begin) { |
| 209 | requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), |
| 210 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 211 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 214 | alloc_space_ = Space::CreateAllocSpace("alloc space", initial_size, growth_limit, capacity, |
| 215 | requested_begin); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 216 | if (alloc_space_ == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 217 | LOG(FATAL) << "Failed to create alloc space"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 218 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 219 | AddSpace(alloc_space_); |
| 220 | UpdateFirstAndLastSpace(&first_space, &last_space, alloc_space_); |
| 221 | byte* heap_begin = first_space->Begin(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 222 | size_t heap_capacity = (last_space->Begin() - first_space->Begin()) + last_space->NonGrowthLimitCapacity(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 223 | |
| 224 | // Allocate the initial live bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 225 | UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create("dalvik-bitmap-1", heap_begin, heap_capacity)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 226 | if (live_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 227 | LOG(FATAL) << "Failed to create live bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 230 | // Mark image objects in the live bitmap |
| 231 | for (size_t i = 0; i < spaces_.size(); i++) { |
| 232 | Space* space = spaces_[i]; |
| 233 | if (space->IsImageSpace()) { |
| 234 | space->AsImageSpace()->RecordImageAllocations(live_bitmap.get()); |
| 235 | } |
| 236 | } |
| 237 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 238 | // Allocate the initial mark bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 239 | UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create("dalvik-bitmap-2", heap_begin, heap_capacity)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 240 | if (mark_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 241 | LOG(FATAL) << "Failed to create mark bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 244 | // Allocate the card table. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 245 | UniquePtr<CardTable> card_table(CardTable::Create(heap_begin, heap_capacity)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 246 | if (card_table.get() == NULL) { |
| 247 | LOG(FATAL) << "Failed to create card table"; |
| 248 | } |
| 249 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 250 | live_bitmap_ = live_bitmap.release(); |
| 251 | mark_bitmap_ = mark_bitmap.release(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 252 | card_table_ = card_table.release(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 253 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 254 | num_bytes_allocated_ = 0; |
| 255 | num_objects_allocated_ = 0; |
| 256 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 257 | // It's still to early to take a lock because there are no threads yet, |
| 258 | // but we can create the heap lock now. We don't create it earlier to |
| 259 | // make it clear that you can't use locks during heap initialization. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 260 | lock_ = new Mutex("Heap lock"); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 261 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 262 | Heap::EnableObjectValidation(); |
| 263 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 264 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 265 | LOG(INFO) << "Heap::Init exiting"; |
| 266 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void Heap::Destroy() { |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 270 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 271 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 272 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 273 | // those threads can't resume. We're the only running thread, and we can do whatever we like... |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 274 | STLDeleteElements(&spaces_); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 275 | delete mark_bitmap_; |
| 276 | delete live_bitmap_; |
| 277 | delete card_table_; |
| 278 | delete lock_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 281 | Object* Heap::AllocObject(Class* klass, size_t byte_count) { |
| 282 | { |
| 283 | ScopedHeapLock lock; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 284 | DCHECK(klass == NULL || (klass->IsClassClass() && byte_count >= sizeof(Class)) || |
| 285 | (klass->IsVariableSize() || klass->GetObjectSize() == byte_count) || |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 286 | strlen(ClassHelper(klass).GetDescriptor()) == 0); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 287 | DCHECK_GE(byte_count, sizeof(Object)); |
| 288 | Object* obj = AllocateLocked(byte_count); |
| 289 | if (obj != NULL) { |
| 290 | obj->SetClass(klass); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 291 | if (Dbg::IsAllocTrackingEnabled()) { |
| 292 | Dbg::RecordAllocation(klass, byte_count); |
| 293 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 294 | return obj; |
| 295 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 296 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 297 | |
| 298 | Thread::Current()->ThrowOutOfMemoryError(klass, byte_count); |
| 299 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 302 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 303 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 304 | // require taking the lock. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 305 | if (obj == NULL || !IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 306 | return false; |
| 307 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 308 | for (size_t i = 0; i < spaces_.size(); i++) { |
| 309 | if (spaces_[i]->Contains(obj)) { |
| 310 | return true; |
| 311 | } |
| 312 | } |
| 313 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 316 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
| 317 | lock_->AssertHeld(); |
| 318 | return IsHeapAddress(obj) && live_bitmap_->Test(obj); |
| 319 | } |
| 320 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 321 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 322 | void Heap::VerifyObject(const Object* obj) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 323 | if (!verify_objects_) { |
| 324 | return; |
| 325 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 326 | ScopedHeapLock lock; |
| 327 | Heap::VerifyObjectLocked(obj); |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | void Heap::VerifyObjectLocked(const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 332 | lock_->AssertHeld(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 333 | if (obj != NULL) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 334 | if (!IsAligned<kObjectAlignment>(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 335 | LOG(FATAL) << "Object isn't aligned: " << obj; |
| 336 | } else if (!live_bitmap_->Test(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 337 | LOG(FATAL) << "Object is dead: " << obj; |
| 338 | } |
| 339 | // Ignore early dawn of the universe verifications |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 340 | if (num_objects_allocated_ > 10) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 341 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 342 | Object::ClassOffset().Int32Value(); |
| 343 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 344 | if (c == NULL) { |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 345 | LOG(FATAL) << "Null class in object: " << obj; |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 346 | } else if (!IsAligned<kObjectAlignment>(c)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 347 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 348 | } else if (!live_bitmap_->Test(c)) { |
| 349 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 350 | } |
| 351 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 352 | // Note: we don't use the accessors here as they have internal sanity checks |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 353 | // that we don't want to run |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 354 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 355 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 356 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 357 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 358 | CHECK_EQ(c_c, c_c_c); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 363 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 364 | DCHECK(obj != NULL); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 365 | Heap::VerifyObjectLocked(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void Heap::VerifyHeap() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 369 | ScopedHeapLock lock; |
| 370 | live_bitmap_->Walk(Heap::VerificationCallback, NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 373 | void Heap::RecordAllocationLocked(AllocSpace* space, const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 374 | #ifndef NDEBUG |
| 375 | if (Runtime::Current()->IsStarted()) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 376 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 377 | } |
| 378 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 379 | size_t size = space->AllocationSize(obj); |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 380 | DCHECK_GT(size, 0u); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 381 | num_bytes_allocated_ += size; |
| 382 | num_objects_allocated_ += 1; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 383 | |
| 384 | if (Runtime::Current()->HasStatsEnabled()) { |
| 385 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 386 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 387 | ++global_stats->allocated_objects; |
| 388 | ++thread_stats->allocated_objects; |
| 389 | global_stats->allocated_bytes += size; |
| 390 | thread_stats->allocated_bytes += size; |
| 391 | } |
| 392 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 393 | live_bitmap_->Set(obj); |
| 394 | } |
| 395 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 396 | void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 397 | lock_->AssertHeld(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 398 | |
| 399 | if (freed_objects < num_objects_allocated_) { |
| 400 | num_objects_allocated_ -= freed_objects; |
| 401 | } else { |
| 402 | num_objects_allocated_ = 0; |
| 403 | } |
| 404 | if (freed_bytes < num_bytes_allocated_) { |
| 405 | num_bytes_allocated_ -= freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 406 | } else { |
| 407 | num_bytes_allocated_ = 0; |
| 408 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 409 | |
| 410 | if (Runtime::Current()->HasStatsEnabled()) { |
| 411 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 412 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 413 | ++global_stats->freed_objects; |
| 414 | ++thread_stats->freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 415 | global_stats->freed_bytes += freed_bytes; |
| 416 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 417 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 420 | Object* Heap::AllocateLocked(size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 421 | lock_->AssertHeld(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 422 | DCHECK(alloc_space_ != NULL); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 423 | AllocSpace* space = alloc_space_; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 424 | Object* obj = AllocateLocked(space, size); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 425 | if (obj != NULL) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 426 | RecordAllocationLocked(space, obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 427 | } |
| 428 | return obj; |
| 429 | } |
| 430 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 431 | Object* Heap::AllocateLocked(AllocSpace* space, size_t alloc_size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 432 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 433 | |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 434 | // Since allocation can cause a GC which will need to SuspendAll, |
| 435 | // make sure all allocators are in the kRunnable state. |
Ian Rogers | f45b154 | 2012-02-03 18:03:48 -0800 | [diff] [blame] | 436 | CHECK_EQ(Thread::Current()->GetState(), Thread::kRunnable); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 437 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 438 | // Fail impossible allocations |
| 439 | if (alloc_size > space->Capacity()) { |
| 440 | // On failure collect soft references |
| 441 | CollectGarbageInternal(true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 442 | return NULL; |
| 443 | } |
| 444 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 445 | Object* ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 446 | if (ptr != NULL) { |
| 447 | return ptr; |
| 448 | } |
| 449 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 450 | // The allocation failed. If the GC is running, block until it completes and retry. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 451 | if (is_gc_running_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 452 | // The GC is concurrently tracing the heap. Release the heap lock, wait for the GC to |
| 453 | // complete, and retrying allocating. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 454 | WaitForConcurrentGcToComplete(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 455 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 456 | if (ptr != NULL) { |
| 457 | return ptr; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // Another failure. Our thread was starved or there may be too many |
| 462 | // live objects. Try a foreground GC. This will have no effect if |
| 463 | // the concurrent GC is already running. |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 464 | if (Runtime::Current()->HasStatsEnabled()) { |
| 465 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 466 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 467 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 468 | CollectGarbageInternal(false); |
| 469 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 470 | if (ptr != NULL) { |
| 471 | return ptr; |
| 472 | } |
| 473 | |
| 474 | // Even that didn't work; this is an exceptional state. |
| 475 | // Try harder, growing the heap if necessary. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 476 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 477 | if (ptr != NULL) { |
| 478 | //size_t new_footprint = dvmHeapSourceGetIdealFootprint(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 479 | size_t new_footprint = space->GetFootprintLimit(); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 480 | // OLD-TODO: may want to grow a little bit more so that the amount of |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 481 | // free space is equal to the old free space + the |
| 482 | // utilization slop for the new allocation. |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 483 | VLOG(gc) << "Grow heap (frag case) to " << PrettySize(new_footprint) |
Ian Rogers | 162a31c | 2012-01-31 16:14:31 -0800 | [diff] [blame] | 484 | << " for a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 485 | return ptr; |
| 486 | } |
| 487 | |
| 488 | // Most allocations should have succeeded by now, so the heap is |
| 489 | // really full, really fragmented, or the requested size is really |
| 490 | // big. Do another GC, collecting SoftReferences this time. The VM |
| 491 | // spec requires that all SoftReferences have been collected and |
| 492 | // cleared before throwing an OOME. |
| 493 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 494 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 495 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) << " allocation"; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 496 | CollectGarbageInternal(true); |
| 497 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 498 | if (ptr != NULL) { |
| 499 | return ptr; |
| 500 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 501 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 502 | LOG(ERROR) << "Out of memory on a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 503 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 504 | // TODO: tell the HeapSource to dump its state |
| 505 | // TODO: dump stack traces for all threads |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 506 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 507 | return NULL; |
| 508 | } |
| 509 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 510 | int64_t Heap::GetMaxMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 511 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | int64_t Heap::GetTotalMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 515 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | int64_t Heap::GetFreeMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 519 | return alloc_space_->Capacity() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 522 | class InstanceCounter { |
| 523 | public: |
| 524 | InstanceCounter(Class* c, bool count_assignable) |
| 525 | : class_(c), count_assignable_(count_assignable), count_(0) { |
| 526 | } |
| 527 | |
| 528 | size_t GetCount() { |
| 529 | return count_; |
| 530 | } |
| 531 | |
| 532 | static void Callback(Object* o, void* arg) { |
| 533 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 534 | } |
| 535 | |
| 536 | private: |
| 537 | void VisitInstance(Object* o) { |
| 538 | Class* instance_class = o->GetClass(); |
| 539 | if (count_assignable_) { |
| 540 | if (instance_class == class_) { |
| 541 | ++count_; |
| 542 | } |
| 543 | } else { |
| 544 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 545 | ++count_; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | Class* class_; |
| 551 | bool count_assignable_; |
| 552 | size_t count_; |
| 553 | }; |
| 554 | |
| 555 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
| 556 | ScopedHeapLock lock; |
| 557 | InstanceCounter counter(c, count_assignable); |
| 558 | live_bitmap_->Walk(InstanceCounter::Callback, &counter); |
| 559 | return counter.GetCount(); |
| 560 | } |
| 561 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 562 | void Heap::CollectGarbage(bool clear_soft_references) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 563 | ScopedHeapLock lock; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 564 | CollectGarbageInternal(clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 567 | void Heap::CollectGarbageInternal(bool clear_soft_references) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 568 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 569 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 570 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 571 | thread_list->SuspendAll(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 572 | |
| 573 | size_t initial_size = num_bytes_allocated_; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 574 | TimingLogger timings("CollectGarbageInternal"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 575 | uint64_t t0 = NanoTime(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 576 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 577 | { |
| 578 | MarkSweep mark_sweep; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 579 | timings.AddSplit("ctor"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 580 | |
| 581 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 582 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 583 | |
| 584 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 585 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 586 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 587 | mark_sweep.ScanDirtyImageRoots(); |
| 588 | timings.AddSplit("DirtyImageRoots"); |
| 589 | |
| 590 | // Roots are marked on the bitmap and the mark_stack is empty |
| 591 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 592 | |
| 593 | // TODO: if concurrent |
| 594 | // unlock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 595 | // thread_list->ResumeAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 596 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 597 | // Recursively mark all bits set in the non-image mark bitmap |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 598 | mark_sweep.RecursiveMark(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 599 | timings.AddSplit("RecursiveMark"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 600 | |
| 601 | // TODO: if concurrent |
| 602 | // lock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 603 | // thread_list->SuspendAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 604 | // re-mark root set |
| 605 | // scan dirty objects |
| 606 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 607 | mark_sweep.ProcessReferences(clear_soft_references); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 608 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 609 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 610 | // TODO: if concurrent |
| 611 | // swap bitmaps |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 612 | |
| 613 | mark_sweep.Sweep(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 614 | timings.AddSplit("Sweep"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 615 | |
| 616 | cleared_references = mark_sweep.GetClearedReferences(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 620 | timings.AddSplit("GrowForUtilization"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 621 | uint64_t t1 = NanoTime(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 622 | thread_list->ResumeAll(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 623 | |
| 624 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 625 | RequestHeapTrim(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 626 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 627 | uint64_t duration_ns = t1 - t0; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 628 | bool gc_was_particularly_slow = duration_ns > MsToNs(50); // TODO: crank this down for concurrent. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 629 | if (VLOG_IS_ON(gc) || gc_was_particularly_slow) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 630 | // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging. |
| 631 | size_t bytes_freed = initial_size - num_bytes_allocated_; |
| 632 | if (bytes_freed > KB) { // ignore freed bytes in output if > 1KB |
| 633 | bytes_freed = RoundDown(bytes_freed, KB); |
| 634 | } |
| 635 | size_t bytes_allocated = RoundUp(num_bytes_allocated_, KB); |
| 636 | // lose low nanoseconds in duration. TODO: make this part of PrettyDuration |
| 637 | duration_ns = (duration_ns / 1000) * 1000; |
| 638 | size_t total = GetTotalMemory(); |
| 639 | size_t percentFree = 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total); |
| 640 | LOG(INFO) << "GC freed " << PrettySize(bytes_freed) << ", " << percentFree << "% free, " |
| 641 | << PrettySize(bytes_allocated) << "/" << PrettySize(total) << ", " |
| 642 | << "paused " << PrettyDuration(duration_ns); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 643 | } |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 644 | Dbg::GcDidFinish(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 645 | if (VLOG_IS_ON(heap)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 646 | timings.Dump(); |
| 647 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void Heap::WaitForConcurrentGcToComplete() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 651 | lock_->AssertHeld(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 654 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 655 | size_t alloc_space_capacity = alloc_space_->Capacity(); |
| 656 | if (max_allowed_footprint > alloc_space_capacity) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 657 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) |
| 658 | << " to " << PrettySize(alloc_space_capacity); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 659 | max_allowed_footprint = alloc_space_capacity; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 660 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 661 | alloc_space_->SetFootprintLimit(max_allowed_footprint); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 664 | // kHeapIdealFree is the ideal maximum free size, when we grow the heap for utilization. |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 665 | static const size_t kHeapIdealFree = 2 * MB; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 666 | // kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization, |
| 667 | // regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 668 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 669 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 670 | void Heap::GrowForUtilization() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 671 | lock_->AssertHeld(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 672 | |
| 673 | // We know what our utilization is at this moment. |
| 674 | // This doesn't actually resize any memory. It just lets the heap grow more |
| 675 | // when necessary. |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 676 | size_t target_size(num_bytes_allocated_ / Heap::GetTargetHeapUtilization()); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 677 | |
| 678 | if (target_size > num_bytes_allocated_ + kHeapIdealFree) { |
| 679 | target_size = num_bytes_allocated_ + kHeapIdealFree; |
| 680 | } else if (target_size < num_bytes_allocated_ + kHeapMinFree) { |
| 681 | target_size = num_bytes_allocated_ + kHeapMinFree; |
| 682 | } |
| 683 | |
| 684 | SetIdealFootprint(target_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 685 | } |
| 686 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 687 | void Heap::ClearGrowthLimit() { |
| 688 | ScopedHeapLock lock; |
| 689 | WaitForConcurrentGcToComplete(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 690 | alloc_space_->ClearGrowthLimit(); |
| 691 | } |
| 692 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 693 | pid_t Heap::GetLockOwner() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 694 | return lock_->GetOwner(); |
| 695 | } |
| 696 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 697 | void Heap::Lock() { |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 698 | // Grab the lock, but put ourselves into Thread::kVmWait if it looks |
| 699 | // like we're going to have to wait on the mutex. This prevents |
| 700 | // deadlock if another thread is calling CollectGarbageInternal, |
| 701 | // since they will have the heap lock and be waiting for mutators to |
| 702 | // suspend. |
| 703 | if (!lock_->TryLock()) { |
| 704 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 705 | lock_->Lock(); |
| 706 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void Heap::Unlock() { |
| 710 | lock_->Unlock(); |
| 711 | } |
| 712 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 713 | void Heap::SetWellKnownClasses(Class* java_lang_ref_FinalizerReference, |
| 714 | Class* java_lang_ref_ReferenceQueue) { |
| 715 | java_lang_ref_FinalizerReference_ = java_lang_ref_FinalizerReference; |
| 716 | java_lang_ref_ReferenceQueue_ = java_lang_ref_ReferenceQueue; |
| 717 | CHECK(java_lang_ref_FinalizerReference_ != NULL); |
| 718 | CHECK(java_lang_ref_ReferenceQueue_ != NULL); |
| 719 | } |
| 720 | |
| 721 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 722 | MemberOffset reference_queue_offset, |
| 723 | MemberOffset reference_queueNext_offset, |
| 724 | MemberOffset reference_pendingNext_offset, |
| 725 | MemberOffset finalizer_reference_zombie_offset) { |
| 726 | reference_referent_offset_ = reference_referent_offset; |
| 727 | reference_queue_offset_ = reference_queue_offset; |
| 728 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 729 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 730 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 731 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 732 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 733 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 734 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 735 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 736 | } |
| 737 | |
| 738 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 739 | DCHECK(reference != NULL); |
| 740 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 741 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 742 | } |
| 743 | |
| 744 | void Heap::ClearReferenceReferent(Object* reference) { |
| 745 | DCHECK(reference != NULL); |
| 746 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 747 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 748 | } |
| 749 | |
| 750 | // Returns true if the reference object has not yet been enqueued. |
| 751 | bool Heap::IsEnqueuable(const Object* ref) { |
| 752 | DCHECK(ref != NULL); |
| 753 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 754 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 755 | return (queue != NULL) && (queue_next == NULL); |
| 756 | } |
| 757 | |
| 758 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 759 | DCHECK(ref != NULL); |
| 760 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 761 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 762 | EnqueuePendingReference(ref, cleared_reference_list); |
| 763 | } |
| 764 | |
| 765 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 766 | DCHECK(ref != NULL); |
| 767 | DCHECK(list != NULL); |
| 768 | |
| 769 | if (*list == NULL) { |
| 770 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 771 | *list = ref; |
| 772 | } else { |
| 773 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 774 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 775 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | Object* Heap::DequeuePendingReference(Object** list) { |
| 780 | DCHECK(list != NULL); |
| 781 | DCHECK(*list != NULL); |
| 782 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 783 | Object* ref; |
| 784 | if (*list == head) { |
| 785 | ref = *list; |
| 786 | *list = NULL; |
| 787 | } else { |
| 788 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 789 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 790 | ref = head; |
| 791 | } |
| 792 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 793 | return ref; |
| 794 | } |
| 795 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 796 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
| 797 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 798 | static Method* FinalizerReference_add = |
| 799 | java_lang_ref_FinalizerReference_->FindDirectMethod("add", "(Ljava/lang/Object;)V"); |
| 800 | DCHECK(FinalizerReference_add != NULL); |
| 801 | Object* args[] = { object }; |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 802 | FinalizerReference_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 806 | DCHECK(cleared != NULL); |
| 807 | if (*cleared != NULL) { |
| 808 | static Method* ReferenceQueue_add = |
| 809 | java_lang_ref_ReferenceQueue_->FindDirectMethod("add", "(Ljava/lang/ref/Reference;)V"); |
| 810 | DCHECK(ReferenceQueue_add != NULL); |
| 811 | |
| 812 | Thread* self = Thread::Current(); |
| 813 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 814 | Object* args[] = { *cleared }; |
| 815 | ReferenceQueue_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL); |
| 816 | *cleared = NULL; |
| 817 | } |
| 818 | } |
| 819 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 820 | void Heap::RequestHeapTrim() { |
| 821 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 822 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 823 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 824 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 825 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 826 | // not how much use we're making of those pages. |
| 827 | float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size(); |
| 828 | if (utilization > 0.75f) { |
| 829 | // Don't bother trimming the heap if it's more than 75% utilized. |
| 830 | // (This percentage was picked arbitrarily.) |
| 831 | return; |
| 832 | } |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 833 | if (!Runtime::Current()->IsStarted()) { |
| 834 | // Heap trimming isn't supported without a Java runtime (such as at dex2oat time) |
| 835 | return; |
| 836 | } |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 837 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 838 | static jclass Daemons_class = CacheClass(env, "java/lang/Daemons"); |
| 839 | static jmethodID Daemons_requestHeapTrim = env->GetStaticMethodID(Daemons_class, "requestHeapTrim", "()V"); |
| 840 | env->CallStaticVoidMethod(Daemons_class, Daemons_requestHeapTrim); |
| 841 | CHECK(!env->ExceptionCheck()); |
| 842 | } |
| 843 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 844 | } // namespace art |