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