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" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 32 | #include "scoped_heap_lock.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 33 | #include "ScopedLocalRef.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 34 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 35 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 36 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 37 | #include "timing_logger.h" |
| 38 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 39 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 40 | |
| 41 | namespace art { |
| 42 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 43 | static void UpdateFirstAndLastSpace(Space** first_space, Space** last_space, Space* space) { |
| 44 | if (*first_space == NULL) { |
| 45 | *first_space = space; |
| 46 | *last_space = space; |
| 47 | } else { |
| 48 | if ((*first_space)->Begin() > space->Begin()) { |
| 49 | *first_space = space; |
| 50 | } else if (space->Begin() > (*last_space)->Begin()) { |
| 51 | *last_space = space; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 56 | static bool GenerateImage(const std::string& image_file_name) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 57 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 58 | std::vector<std::string> boot_class_path; |
| 59 | Split(boot_class_path_string, ':', boot_class_path); |
Brian Carlstrom | b279337 | 2012-03-17 18:27:16 -0700 | [diff] [blame] | 60 | if (boot_class_path.empty()) { |
| 61 | LOG(FATAL) << "Failed to generate image because no boot class path specified"; |
| 62 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 63 | |
| 64 | std::vector<char*> arg_vector; |
| 65 | |
| 66 | std::string dex2oat_string(GetAndroidRoot()); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 67 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 68 | const char* dex2oat = dex2oat_string.c_str(); |
| 69 | arg_vector.push_back(strdup(dex2oat)); |
| 70 | |
| 71 | std::string image_option_string("--image="); |
| 72 | image_option_string += image_file_name; |
| 73 | const char* image_option = image_option_string.c_str(); |
| 74 | arg_vector.push_back(strdup(image_option)); |
| 75 | |
| 76 | arg_vector.push_back(strdup("--runtime-arg")); |
| 77 | arg_vector.push_back(strdup("-Xms64m")); |
| 78 | |
| 79 | arg_vector.push_back(strdup("--runtime-arg")); |
| 80 | arg_vector.push_back(strdup("-Xmx64m")); |
| 81 | |
| 82 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 83 | std::string dex_file_option_string("--dex-file="); |
| 84 | dex_file_option_string += boot_class_path[i]; |
| 85 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 86 | arg_vector.push_back(strdup(dex_file_option)); |
| 87 | } |
| 88 | |
| 89 | std::string oat_file_option_string("--oat-file="); |
| 90 | oat_file_option_string += image_file_name; |
| 91 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 92 | oat_file_option_string += "oat"; |
| 93 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 94 | arg_vector.push_back(strdup(oat_file_option)); |
| 95 | |
| 96 | arg_vector.push_back(strdup("--base=0x60000000")); |
| 97 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 98 | std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 99 | LOG(INFO) << command_line; |
| 100 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 101 | arg_vector.push_back(NULL); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 102 | char** argv = &arg_vector[0]; |
| 103 | |
| 104 | // fork and exec dex2oat |
| 105 | pid_t pid = fork(); |
| 106 | if (pid == 0) { |
| 107 | // no allocation allowed between fork and exec |
| 108 | |
| 109 | // change process groups, so we don't get reaped by ProcessManager |
| 110 | setpgid(0, 0); |
| 111 | |
| 112 | execv(dex2oat, argv); |
| 113 | |
| 114 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 115 | return false; |
| 116 | } else { |
| 117 | STLDeleteElements(&arg_vector); |
| 118 | |
| 119 | // wait for dex2oat to finish |
| 120 | int status; |
| 121 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 122 | if (got_pid != pid) { |
| 123 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 124 | return false; |
| 125 | } |
| 126 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 127 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 134 | Heap::Heap(size_t initial_size, size_t growth_limit, size_t capacity, |
| 135 | const std::string& original_image_file_name) |
| 136 | : lock_(NULL), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 137 | image_space_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 138 | alloc_space_(NULL), |
| 139 | mark_bitmap_(NULL), |
| 140 | live_bitmap_(NULL), |
| 141 | card_table_(NULL), |
| 142 | card_marking_disabled_(false), |
| 143 | is_gc_running_(false), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 144 | concurrent_start_size_(128 * KB), |
| 145 | concurrent_min_free_(256 * KB), |
| 146 | try_running_gc_(false), |
| 147 | requesting_gc_(false), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 148 | num_bytes_allocated_(0), |
| 149 | num_objects_allocated_(0), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 150 | last_trim_time_(0), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 151 | reference_referent_offset_(0), |
| 152 | reference_queue_offset_(0), |
| 153 | reference_queueNext_offset_(0), |
| 154 | reference_pendingNext_offset_(0), |
| 155 | finalizer_reference_zombie_offset_(0), |
| 156 | target_utilization_(0.5), |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 157 | verify_objects_(false) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 158 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 159 | LOG(INFO) << "Heap() entering"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 162 | // Compute the bounds of all spaces for allocating live and mark bitmaps |
| 163 | // there will be at least one space (the alloc space) |
| 164 | Space* first_space = NULL; |
| 165 | Space* last_space = NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 166 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 167 | // Requested begin for the alloc space, to follow the mapped image and oat files |
| 168 | byte* requested_begin = NULL; |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 169 | std::string image_file_name(original_image_file_name); |
| 170 | if (!image_file_name.empty()) { |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 171 | if (OS::FileExists(image_file_name.c_str())) { |
| 172 | // If the /system file exists, it should be up-to-date, don't try to generate |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 173 | image_space_ = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 174 | } else { |
| 175 | // If the /system file didn't exist, we need to use one from the art-cache. |
| 176 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 177 | // If it does not exist, generate. |
| 178 | image_file_name = GetArtCacheFilenameOrDie(image_file_name); |
| 179 | if (OS::FileExists(image_file_name.c_str())) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 180 | image_space_ = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 181 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 182 | if (image_space_ == NULL) { |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 183 | if (!GenerateImage(image_file_name)) { |
| 184 | LOG(FATAL) << "Failed to generate image: " << image_file_name; |
| 185 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 186 | image_space_ = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 189 | if (image_space_ == NULL) { |
Brian Carlstrom | 223f20f | 2012-02-04 23:06:55 -0800 | [diff] [blame] | 190 | LOG(FATAL) << "Failed to create space from " << image_file_name; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 191 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 192 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 193 | AddSpace(image_space_); |
| 194 | UpdateFirstAndLastSpace(&first_space, &last_space, image_space_); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 195 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 196 | // isn't going to get in the middle |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 197 | byte* oat_end_addr = image_space_->GetImageHeader().GetOatEnd(); |
| 198 | CHECK(oat_end_addr > image_space_->End()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 199 | if (oat_end_addr > requested_begin) { |
| 200 | requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), |
| 201 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 202 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 205 | alloc_space_ = Space::CreateAllocSpace("alloc space", initial_size, growth_limit, capacity, |
| 206 | requested_begin); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 207 | if (alloc_space_ == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 208 | LOG(FATAL) << "Failed to create alloc space"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 209 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 210 | AddSpace(alloc_space_); |
| 211 | UpdateFirstAndLastSpace(&first_space, &last_space, alloc_space_); |
| 212 | byte* heap_begin = first_space->Begin(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 213 | 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] | 214 | |
| 215 | // Allocate the initial live bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 216 | 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] | 217 | if (live_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 218 | LOG(FATAL) << "Failed to create live bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 221 | // Mark image objects in the live bitmap |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 222 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 223 | Space* space = spaces_[i]; |
| 224 | if (space->IsImageSpace()) { |
| 225 | space->AsImageSpace()->RecordImageAllocations(live_bitmap.get()); |
| 226 | } |
| 227 | } |
| 228 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 229 | // Allocate the initial mark bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 230 | 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] | 231 | if (mark_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 232 | LOG(FATAL) << "Failed to create mark bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 235 | // Allocate the card table. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 236 | UniquePtr<CardTable> card_table(CardTable::Create(heap_begin, heap_capacity)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 237 | if (card_table.get() == NULL) { |
| 238 | LOG(FATAL) << "Failed to create card table"; |
| 239 | } |
| 240 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 241 | live_bitmap_ = live_bitmap.release(); |
| 242 | mark_bitmap_ = mark_bitmap.release(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 243 | card_table_ = card_table.release(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 244 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 245 | num_bytes_allocated_ = 0; |
| 246 | num_objects_allocated_ = 0; |
| 247 | |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 248 | mark_stack_ = MarkStack::Create(); |
| 249 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 250 | // It's still too early to take a lock because there are no threads yet, |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 251 | // but we can create the heap lock now. We don't create it earlier to |
| 252 | // make it clear that you can't use locks during heap initialization. |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 253 | lock_ = new Mutex("Heap lock", kHeapLock); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 254 | condition_ = new ConditionVariable("Heap condition variable"); |
| 255 | |
| 256 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 257 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 258 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 259 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 260 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 263 | void Heap::AddSpace(Space* space) { |
| 264 | spaces_.push_back(space); |
| 265 | } |
| 266 | |
| 267 | Heap::~Heap() { |
| 268 | VLOG(heap) << "~Heap()"; |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 269 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 270 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 271 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 272 | // 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] | 273 | STLDeleteElements(&spaces_); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 274 | delete mark_bitmap_; |
| 275 | delete live_bitmap_; |
| 276 | delete card_table_; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 277 | delete mark_stack_; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 278 | delete condition_; |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 279 | delete lock_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 282 | static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 283 | size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg); |
| 284 | |
| 285 | size_t chunk_size = static_cast<size_t>(reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start)); |
| 286 | size_t chunk_free_bytes = 0; |
| 287 | if (used_bytes < chunk_size) { |
| 288 | chunk_free_bytes = chunk_size - used_bytes; |
| 289 | } |
| 290 | |
| 291 | if (chunk_free_bytes > max_contiguous_allocation) { |
| 292 | max_contiguous_allocation = chunk_free_bytes; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | Object* Heap::AllocObject(Class* c, size_t byte_count) { |
| 297 | // Used in the detail message if we throw an OOME. |
| 298 | int64_t total_bytes_free; |
| 299 | size_t max_contiguous_allocation; |
| 300 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 301 | { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 302 | ScopedHeapLock heap_lock; |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 303 | DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(Class)) || |
| 304 | (c->IsVariableSize() || c->GetObjectSize() == byte_count) || |
| 305 | strlen(ClassHelper(c).GetDescriptor()) == 0); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 306 | DCHECK_GE(byte_count, sizeof(Object)); |
| 307 | Object* obj = AllocateLocked(byte_count); |
| 308 | if (obj != NULL) { |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 309 | obj->SetClass(c); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 310 | if (Dbg::IsAllocTrackingEnabled()) { |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 311 | Dbg::RecordAllocation(c, byte_count); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 312 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 313 | |
| 314 | if (!is_gc_running_ && num_bytes_allocated_ >= concurrent_start_bytes_) { |
| 315 | // The SirtRef is necessary since the calls in RequestConcurrentGC |
| 316 | // are a safepoint. |
| 317 | SirtRef<Object> ref(obj); |
| 318 | RequestConcurrentGC(); |
| 319 | } |
| 320 | VerifyObject(obj); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 321 | return obj; |
| 322 | } |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 323 | total_bytes_free = GetFreeMemory(); |
| 324 | max_contiguous_allocation = 0; |
| 325 | GetAllocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 326 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 327 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 328 | std::string msg(StringPrintf("Failed to allocate a %zd-byte %s (%lld total bytes free; largest possible contiguous allocation %zd bytes)", |
| 329 | byte_count, |
| 330 | PrettyDescriptor(c).c_str(), |
| 331 | total_bytes_free, max_contiguous_allocation)); |
| 332 | Thread::Current()->ThrowOutOfMemoryError(msg.c_str()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 333 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 336 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 337 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 338 | // require taking the lock. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 339 | if (obj == NULL) { |
| 340 | return true; |
| 341 | } |
| 342 | if (!IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 345 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 346 | if (spaces_[i]->Contains(obj)) { |
| 347 | return true; |
| 348 | } |
| 349 | } |
| 350 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 353 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
| 354 | lock_->AssertHeld(); |
| 355 | return IsHeapAddress(obj) && live_bitmap_->Test(obj); |
| 356 | } |
| 357 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 358 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 359 | void Heap::VerifyObject(const Object* obj) { |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 360 | if (this == NULL || !verify_objects_ || Runtime::Current()->IsShuttingDown() || |
Ian Rogers | 141d622 | 2012-04-05 12:23:06 -0700 | [diff] [blame] | 361 | Thread::Current() == NULL || |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 362 | Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 363 | return; |
| 364 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 365 | ScopedHeapLock heap_lock; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 366 | Heap::VerifyObjectLocked(obj); |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | void Heap::VerifyObjectLocked(const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 371 | lock_->AssertHeld(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 372 | if (obj != NULL) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 373 | if (!IsAligned<kObjectAlignment>(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 374 | LOG(FATAL) << "Object isn't aligned: " << obj; |
| 375 | } else if (!live_bitmap_->Test(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 376 | LOG(FATAL) << "Object is dead: " << obj; |
| 377 | } |
| 378 | // Ignore early dawn of the universe verifications |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 379 | if (num_objects_allocated_ > 10) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 380 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 381 | Object::ClassOffset().Int32Value(); |
| 382 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 383 | if (c == NULL) { |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 384 | LOG(FATAL) << "Null class in object: " << obj; |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 385 | } else if (!IsAligned<kObjectAlignment>(c)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 386 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 387 | } else if (!live_bitmap_->Test(c)) { |
| 388 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 389 | } |
| 390 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 391 | // 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] | 392 | // that we don't want to run |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 393 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 394 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 395 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 396 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 397 | CHECK_EQ(c_c, c_c_c); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 402 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 403 | DCHECK(obj != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 404 | reinterpret_cast<Heap*>(arg)->VerifyObjectLocked(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void Heap::VerifyHeap() { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 408 | ScopedHeapLock heap_lock; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 409 | live_bitmap_->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 412 | void Heap::RecordAllocationLocked(AllocSpace* space, const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 413 | #ifndef NDEBUG |
| 414 | if (Runtime::Current()->IsStarted()) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 415 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 416 | } |
| 417 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 418 | size_t size = space->AllocationSize(obj); |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 419 | DCHECK_GT(size, 0u); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 420 | num_bytes_allocated_ += size; |
| 421 | num_objects_allocated_ += 1; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 422 | |
| 423 | if (Runtime::Current()->HasStatsEnabled()) { |
| 424 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 425 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 426 | ++global_stats->allocated_objects; |
| 427 | ++thread_stats->allocated_objects; |
| 428 | global_stats->allocated_bytes += size; |
| 429 | thread_stats->allocated_bytes += size; |
| 430 | } |
| 431 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 432 | live_bitmap_->Set(obj); |
| 433 | } |
| 434 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 435 | void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 436 | lock_->AssertHeld(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 437 | |
| 438 | if (freed_objects < num_objects_allocated_) { |
| 439 | num_objects_allocated_ -= freed_objects; |
| 440 | } else { |
| 441 | num_objects_allocated_ = 0; |
| 442 | } |
| 443 | if (freed_bytes < num_bytes_allocated_) { |
| 444 | num_bytes_allocated_ -= freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 445 | } else { |
| 446 | num_bytes_allocated_ = 0; |
| 447 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 448 | |
| 449 | if (Runtime::Current()->HasStatsEnabled()) { |
| 450 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 451 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 452 | ++global_stats->freed_objects; |
| 453 | ++thread_stats->freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 454 | global_stats->freed_bytes += freed_bytes; |
| 455 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 456 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 459 | Object* Heap::AllocateLocked(size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 460 | lock_->AssertHeld(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 461 | DCHECK(alloc_space_ != NULL); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 462 | AllocSpace* space = alloc_space_; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 463 | Object* obj = AllocateLocked(space, size); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 464 | if (obj != NULL) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 465 | RecordAllocationLocked(space, obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 466 | } |
| 467 | return obj; |
| 468 | } |
| 469 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 470 | Object* Heap::AllocateLocked(AllocSpace* space, size_t alloc_size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 471 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 472 | |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 473 | // Since allocation can cause a GC which will need to SuspendAll, |
| 474 | // make sure all allocators are in the kRunnable state. |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 475 | CHECK_EQ(Thread::Current()->GetState(), kRunnable); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 476 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 477 | // Fail impossible allocations |
| 478 | if (alloc_size > space->Capacity()) { |
| 479 | // On failure collect soft references |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 480 | WaitForConcurrentGcToComplete(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 481 | CollectGarbageInternal(false, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
| 484 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 485 | Object* ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 486 | if (ptr != NULL) { |
| 487 | return ptr; |
| 488 | } |
| 489 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 490 | // 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] | 491 | if (is_gc_running_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 492 | // The GC is concurrently tracing the heap. Release the heap lock, wait for the GC to |
| 493 | // complete, and retrying allocating. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 494 | WaitForConcurrentGcToComplete(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 495 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 496 | if (ptr != NULL) { |
| 497 | return ptr; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Another failure. Our thread was starved or there may be too many |
| 502 | // live objects. Try a foreground GC. This will have no effect if |
| 503 | // the concurrent GC is already running. |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 504 | if (Runtime::Current()->HasStatsEnabled()) { |
| 505 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 506 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 507 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 508 | // We don't need a WaitForConcurrentGcToComplete here since we checked |
| 509 | // is_gc_running_ earlier and we are in a heap lock. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 510 | CollectGarbageInternal(false, false); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 511 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 512 | if (ptr != NULL) { |
| 513 | return ptr; |
| 514 | } |
| 515 | |
| 516 | // Even that didn't work; this is an exceptional state. |
| 517 | // Try harder, growing the heap if necessary. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 518 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 519 | if (ptr != NULL) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 520 | size_t new_footprint = space->GetFootprintLimit(); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 521 | // 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] | 522 | // free space is equal to the old free space + the |
| 523 | // utilization slop for the new allocation. |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 524 | VLOG(gc) << "Grow heap (frag case) to " << PrettySize(new_footprint) |
Ian Rogers | 162a31c | 2012-01-31 16:14:31 -0800 | [diff] [blame] | 525 | << " for a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 526 | return ptr; |
| 527 | } |
| 528 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 529 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 530 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 531 | // VM spec requires that all SoftReferences have been collected and cleared before throwing OOME. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 532 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 533 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 534 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) << " allocation"; |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 535 | // We don't need a WaitForConcurrentGcToComplete here either. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 536 | CollectGarbageInternal(false, true); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 537 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 538 | if (ptr != NULL) { |
| 539 | return ptr; |
| 540 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 541 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 542 | return NULL; |
| 543 | } |
| 544 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 545 | int64_t Heap::GetMaxMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 546 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | int64_t Heap::GetTotalMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 550 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | int64_t Heap::GetFreeMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 554 | return alloc_space_->Capacity() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 557 | class InstanceCounter { |
| 558 | public: |
| 559 | InstanceCounter(Class* c, bool count_assignable) |
| 560 | : class_(c), count_assignable_(count_assignable), count_(0) { |
| 561 | } |
| 562 | |
| 563 | size_t GetCount() { |
| 564 | return count_; |
| 565 | } |
| 566 | |
| 567 | static void Callback(Object* o, void* arg) { |
| 568 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 569 | } |
| 570 | |
| 571 | private: |
| 572 | void VisitInstance(Object* o) { |
| 573 | Class* instance_class = o->GetClass(); |
| 574 | if (count_assignable_) { |
| 575 | if (instance_class == class_) { |
| 576 | ++count_; |
| 577 | } |
| 578 | } else { |
| 579 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 580 | ++count_; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | Class* class_; |
| 586 | bool count_assignable_; |
| 587 | size_t count_; |
| 588 | }; |
| 589 | |
| 590 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 591 | ScopedHeapLock heap_lock; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 592 | InstanceCounter counter(c, count_assignable); |
| 593 | live_bitmap_->Walk(InstanceCounter::Callback, &counter); |
| 594 | return counter.GetCount(); |
| 595 | } |
| 596 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 597 | void Heap::CollectGarbage(bool clear_soft_references) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 598 | ScopedHeapLock heap_lock; |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 599 | // If we just waited for a GC to complete then we do not need to do another |
| 600 | // GC unless we clear soft references. |
| 601 | if (!WaitForConcurrentGcToComplete() || clear_soft_references) { |
| 602 | CollectGarbageInternal(false, clear_soft_references); |
| 603 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 606 | void Heap::CollectGarbageInternal(bool concurrent, bool clear_soft_references) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 607 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 608 | |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 609 | DCHECK(!is_gc_running_); |
| 610 | is_gc_running_ = true; |
| 611 | |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 612 | TimingLogger timings("CollectGarbageInternal"); |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 613 | uint64_t t0 = NanoTime(), root_end = 0, dirty_begin = 0, dirty_end = 0; |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 614 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 615 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 616 | thread_list->SuspendAll(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 617 | timings.AddSplit("SuspendAll"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 618 | |
| 619 | size_t initial_size = num_bytes_allocated_; |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 620 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 621 | { |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 622 | MarkSweep mark_sweep(mark_stack_); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 623 | timings.AddSplit("ctor"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 624 | |
| 625 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 626 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 627 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 628 | if (concurrent) { |
| 629 | card_table_->ClearNonImageSpaceCards(this); |
| 630 | } |
| 631 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 632 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 633 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 634 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 635 | if (!concurrent) { |
| 636 | mark_sweep.ScanDirtyImageRoots(); |
| 637 | timings.AddSplit("ScanDirtyImageRoots"); |
| 638 | } |
| 639 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 640 | // Roots are marked on the bitmap and the mark_stack is empty |
| 641 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 642 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 643 | if (concurrent) { |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 644 | // We need to resume before unlocking or else a thread waiting for the |
| 645 | // heap lock would re-suspend since we have not yet called ResumeAll. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 646 | thread_list->ResumeAll(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 647 | Unlock(); |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 648 | root_end = NanoTime(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 649 | timings.AddSplit("RootEnd"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 650 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 651 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 652 | // Recursively mark all bits set in the non-image mark bitmap |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 653 | mark_sweep.RecursiveMark(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 654 | timings.AddSplit("RecursiveMark"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 655 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 656 | if (concurrent) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 657 | dirty_begin = NanoTime(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 658 | Lock(); |
| 659 | thread_list->SuspendAll(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 660 | timings.AddSplit("ReSuspend"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 661 | |
| 662 | // Re-mark root set. |
| 663 | mark_sweep.ReMarkRoots(); |
| 664 | timings.AddSplit("ReMarkRoots"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 665 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 666 | // Scan dirty objects, this is required even if we are not doing a |
| 667 | // concurrent GC since we use the card table to locate image roots. |
| 668 | mark_sweep.RecursiveMarkDirtyObjects(); |
| 669 | timings.AddSplit("RecursiveMarkDirtyObjects"); |
| 670 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 671 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 672 | mark_sweep.ProcessReferences(clear_soft_references); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 673 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 674 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 675 | // TODO: swap live and marked bitmaps |
| 676 | // Note: Need to be careful about image spaces if we do this since not |
| 677 | // everything image space will be marked, resulting in things not being |
| 678 | // marked as live anymore. |
| 679 | |
| 680 | // Verify that we only reach marked objects from the image space |
| 681 | mark_sweep.VerifyImageRoots(); |
| 682 | timings.AddSplit("VerifyImageRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 683 | |
| 684 | mark_sweep.Sweep(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 685 | timings.AddSplit("Sweep"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 686 | |
| 687 | cleared_references = mark_sweep.GetClearedReferences(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 691 | timings.AddSplit("GrowForUtilization"); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 692 | thread_list->ResumeAll(); |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 693 | dirty_end = NanoTime(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 694 | |
| 695 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 696 | RequestHeapTrim(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 697 | timings.AddSplit("Finish"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 698 | |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 699 | uint64_t t1 = NanoTime(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 700 | uint64_t duration_ns = t1 - t0; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 701 | 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] | 702 | if (VLOG_IS_ON(gc) || gc_was_particularly_slow) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 703 | // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging. |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 704 | // Reason: For CMS sometimes initial_size < num_bytes_allocated_ results in overflow (3GB freed message). |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 705 | size_t bytes_freed = initial_size - num_bytes_allocated_; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 706 | // lose low nanoseconds in duration. TODO: make this part of PrettyDuration |
| 707 | duration_ns = (duration_ns / 1000) * 1000; |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 708 | if (concurrent) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 709 | uint64_t pause_roots_time = (root_end - t0) / 1000 * 1000; |
| 710 | uint64_t pause_dirty_time = (dirty_end - dirty_begin) / 1000 * 1000; |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 711 | LOG(INFO) << "GC freed " << PrettySize(bytes_freed) << ", " << GetPercentFree() << "% free, " |
| 712 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) << ", " |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 713 | << "paused " << PrettyDuration(pause_roots_time) << "+" << PrettyDuration(pause_dirty_time) |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 714 | << ", total " << PrettyDuration(duration_ns); |
| 715 | } else { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 716 | uint64_t markSweepTime = (dirty_end - t0) / 1000 * 1000; |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 717 | LOG(INFO) << "GC freed " << PrettySize(bytes_freed) << ", " << GetPercentFree() << "% free, " |
| 718 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) << ", " |
| 719 | << "paused " << PrettyDuration(markSweepTime) |
| 720 | << ", total " << PrettyDuration(duration_ns); |
| 721 | } |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 722 | } |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 723 | Dbg::GcDidFinish(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 724 | if (VLOG_IS_ON(heap)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 725 | timings.Dump(); |
| 726 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 727 | |
| 728 | is_gc_running_ = false; |
| 729 | |
| 730 | // Wake anyone who may have been waiting for the GC to complete. |
| 731 | condition_->Broadcast(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 734 | bool Heap::WaitForConcurrentGcToComplete() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 735 | lock_->AssertHeld(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 736 | |
| 737 | // Busy wait for GC to finish |
| 738 | if (is_gc_running_) { |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 739 | uint64_t wait_start = NanoTime(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 740 | do { |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 741 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 742 | condition_->Wait(*lock_); |
| 743 | } while (is_gc_running_); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 744 | uint64_t wait_time = NanoTime() - wait_start; |
| 745 | if (wait_time > MsToNs(5)) { |
| 746 | LOG(INFO) << "WaitForConcurrentGcToComplete blocked for " << PrettyDuration(wait_time); |
| 747 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 748 | DCHECK(!is_gc_running_); |
| 749 | return true; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 750 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 751 | return false; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 754 | void Heap::DumpForSigQuit(std::ostream& os) { |
| 755 | os << "Heap: " << GetPercentFree() << "% free, " |
| 756 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 757 | << "; " << num_objects_allocated_ << " objects\n"; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | size_t Heap::GetPercentFree() { |
| 761 | size_t total = GetTotalMemory(); |
| 762 | return 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total); |
| 763 | } |
| 764 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 765 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 766 | size_t alloc_space_capacity = alloc_space_->Capacity(); |
| 767 | if (max_allowed_footprint > alloc_space_capacity) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 768 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) |
| 769 | << " to " << PrettySize(alloc_space_capacity); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 770 | max_allowed_footprint = alloc_space_capacity; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 771 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 772 | alloc_space_->SetFootprintLimit(max_allowed_footprint); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 775 | // 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] | 776 | static const size_t kHeapIdealFree = 2 * MB; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 777 | // kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization, |
| 778 | // regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 779 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 780 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 781 | void Heap::GrowForUtilization() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 782 | lock_->AssertHeld(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 783 | |
| 784 | // We know what our utilization is at this moment. |
| 785 | // This doesn't actually resize any memory. It just lets the heap grow more |
| 786 | // when necessary. |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 787 | size_t target_size(num_bytes_allocated_ / Heap::GetTargetHeapUtilization()); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 788 | |
| 789 | if (target_size > num_bytes_allocated_ + kHeapIdealFree) { |
| 790 | target_size = num_bytes_allocated_ + kHeapIdealFree; |
| 791 | } else if (target_size < num_bytes_allocated_ + kHeapMinFree) { |
| 792 | target_size = num_bytes_allocated_ + kHeapMinFree; |
| 793 | } |
| 794 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 795 | // Calculate when to perform the next ConcurrentGC. |
| 796 | if (GetTotalMemory() - num_bytes_allocated_ < concurrent_min_free_) { |
| 797 | // Not enough free memory to perform concurrent GC. |
| 798 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 799 | } else { |
| 800 | concurrent_start_bytes_ = alloc_space_->GetFootprintLimit() - concurrent_start_size_; |
| 801 | } |
| 802 | |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 803 | SetIdealFootprint(target_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 804 | } |
| 805 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 806 | void Heap::ClearGrowthLimit() { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 807 | ScopedHeapLock heap_lock; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 808 | WaitForConcurrentGcToComplete(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 809 | alloc_space_->ClearGrowthLimit(); |
| 810 | } |
| 811 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 812 | pid_t Heap::GetLockOwner() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 813 | return lock_->GetOwner(); |
| 814 | } |
| 815 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 816 | void Heap::Lock() { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 817 | // Grab the lock, but put ourselves into kVmWait if it looks |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 818 | // like we're going to have to wait on the mutex. This prevents |
| 819 | // deadlock if another thread is calling CollectGarbageInternal, |
| 820 | // since they will have the heap lock and be waiting for mutators to |
| 821 | // suspend. |
| 822 | if (!lock_->TryLock()) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 823 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 824 | lock_->Lock(); |
| 825 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | void Heap::Unlock() { |
| 829 | lock_->Unlock(); |
| 830 | } |
| 831 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 832 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 833 | MemberOffset reference_queue_offset, |
| 834 | MemberOffset reference_queueNext_offset, |
| 835 | MemberOffset reference_pendingNext_offset, |
| 836 | MemberOffset finalizer_reference_zombie_offset) { |
| 837 | reference_referent_offset_ = reference_referent_offset; |
| 838 | reference_queue_offset_ = reference_queue_offset; |
| 839 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 840 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 841 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 842 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 843 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 844 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 845 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 846 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 847 | } |
| 848 | |
| 849 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 850 | DCHECK(reference != NULL); |
| 851 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 852 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 853 | } |
| 854 | |
| 855 | void Heap::ClearReferenceReferent(Object* reference) { |
| 856 | DCHECK(reference != NULL); |
| 857 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 858 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 859 | } |
| 860 | |
| 861 | // Returns true if the reference object has not yet been enqueued. |
| 862 | bool Heap::IsEnqueuable(const Object* ref) { |
| 863 | DCHECK(ref != NULL); |
| 864 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 865 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 866 | return (queue != NULL) && (queue_next == NULL); |
| 867 | } |
| 868 | |
| 869 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 870 | DCHECK(ref != NULL); |
| 871 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 872 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 873 | EnqueuePendingReference(ref, cleared_reference_list); |
| 874 | } |
| 875 | |
| 876 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 877 | DCHECK(ref != NULL); |
| 878 | DCHECK(list != NULL); |
| 879 | |
| 880 | if (*list == NULL) { |
| 881 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 882 | *list = ref; |
| 883 | } else { |
| 884 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 885 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 886 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | Object* Heap::DequeuePendingReference(Object** list) { |
| 891 | DCHECK(list != NULL); |
| 892 | DCHECK(*list != NULL); |
| 893 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 894 | Object* ref; |
| 895 | if (*list == head) { |
| 896 | ref = *list; |
| 897 | *list = NULL; |
| 898 | } else { |
| 899 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 900 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 901 | ref = head; |
| 902 | } |
| 903 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 904 | return ref; |
| 905 | } |
| 906 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 907 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 908 | ScopedThreadStateChange tsc(self, kRunnable); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 909 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 910 | args[0].SetL(object); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 911 | DecodeMethod(WellKnownClasses::java_lang_ref_FinalizerReference_add)->Invoke(self, NULL, args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 915 | DCHECK(cleared != NULL); |
| 916 | if (*cleared != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 917 | Thread* self = Thread::Current(); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 918 | ScopedThreadStateChange tsc(self, kRunnable); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 919 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 920 | args[0].SetL(*cleared); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 921 | DecodeMethod(WellKnownClasses::java_lang_ref_ReferenceQueue_add)->Invoke(self, NULL, args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 922 | *cleared = NULL; |
| 923 | } |
| 924 | } |
| 925 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 926 | void Heap::RequestConcurrentGC() { |
Mathieu Chartier | 069387a | 2012-06-18 12:01:01 -0700 | [diff] [blame] | 927 | // Make sure that we can do a concurrent GC. |
| 928 | if (requesting_gc_ || |
| 929 | !Runtime::Current()->IsFinishedStarting() || |
| 930 | Runtime::Current()->IsShuttingDown() || |
| 931 | !Runtime::Current()->IsConcurrentGcEnabled()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | |
| 935 | requesting_gc_ = true; |
| 936 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 937 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 938 | DCHECK(WellKnownClasses::java_lang_Daemons_requestGC != NULL); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 939 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_requestGC); |
| 940 | CHECK(!env->ExceptionCheck()); |
| 941 | requesting_gc_ = false; |
| 942 | } |
| 943 | |
| 944 | void Heap::ConcurrentGC() { |
| 945 | ScopedHeapLock heap_lock; |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 946 | // We shouldn't need a WaitForConcurrentGcToComplete here since only |
| 947 | // concurrent GC resumes threads before the GC is completed and this function |
| 948 | // is only called within the GC daemon thread. |
| 949 | CHECK(!is_gc_running_); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 950 | // Current thread needs to be runnable or else we can't suspend all threads. |
| 951 | ScopedThreadStateChange tsc(Thread::Current(), kRunnable); |
| 952 | CollectGarbageInternal(true, false); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | void Heap::Trim() { |
Mathieu Chartier | 5dbf829 | 2012-06-11 13:51:41 -0700 | [diff] [blame] | 956 | lock_->AssertHeld(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 957 | WaitForConcurrentGcToComplete(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 958 | GetAllocSpace()->Trim(); |
| 959 | } |
| 960 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 961 | void Heap::RequestHeapTrim() { |
| 962 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 963 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 964 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 965 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 966 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 967 | // not how much use we're making of those pages. |
| 968 | float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 969 | uint64_t ms_time = NsToMs(NanoTime()); |
| 970 | if (utilization > 0.75f || ms_time - last_trim_time_ < 2 * 1000) { |
| 971 | // Don't bother trimming the heap if it's more than 75% utilized, or if a |
| 972 | // heap trim occurred in the last two seconds. |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 973 | return; |
| 974 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 975 | if (!Runtime::Current()->IsFinishedStarting() || Runtime::Current()->IsShuttingDown()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 976 | // Heap trimming isn't supported without a Java runtime or Daemons (such as at dex2oat time) |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 977 | // Also: we do not wish to start a heap trim if the runtime is shutting down. |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 978 | return; |
| 979 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 980 | last_trim_time_ = ms_time; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 981 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 982 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 983 | DCHECK(WellKnownClasses::java_lang_Daemons_requestHeapTrim != NULL); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 984 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_requestHeapTrim); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 985 | CHECK(!env->ExceptionCheck()); |
| 986 | } |
| 987 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 988 | } // namespace art |