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 | |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 25 | #include "atomic.h" |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 26 | #include "card_table.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 27 | #include "debugger.h" |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 28 | #include "heap_bitmap.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 29 | #include "image.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 30 | #include "mark_sweep.h" |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 31 | #include "mod_union_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 32 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 33 | #include "object_utils.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 34 | #include "os.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 35 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 36 | #include "scoped_thread_state_change.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 37 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 38 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 39 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 40 | #include "timing_logger.h" |
| 41 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 42 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 46 | static void UpdateFirstAndLastSpace(Space** first_space, Space** last_space, Space* space) { |
| 47 | if (*first_space == NULL) { |
| 48 | *first_space = space; |
| 49 | *last_space = space; |
| 50 | } else { |
| 51 | if ((*first_space)->Begin() > space->Begin()) { |
| 52 | *first_space = space; |
| 53 | } else if (space->Begin() > (*last_space)->Begin()) { |
| 54 | *last_space = space; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 59 | static bool GenerateImage(const std::string& image_file_name) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 60 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 61 | std::vector<std::string> boot_class_path; |
| 62 | Split(boot_class_path_string, ':', boot_class_path); |
Brian Carlstrom | b279337 | 2012-03-17 18:27:16 -0700 | [diff] [blame] | 63 | if (boot_class_path.empty()) { |
| 64 | LOG(FATAL) << "Failed to generate image because no boot class path specified"; |
| 65 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 66 | |
| 67 | std::vector<char*> arg_vector; |
| 68 | |
| 69 | std::string dex2oat_string(GetAndroidRoot()); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 70 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 71 | const char* dex2oat = dex2oat_string.c_str(); |
| 72 | arg_vector.push_back(strdup(dex2oat)); |
| 73 | |
| 74 | std::string image_option_string("--image="); |
| 75 | image_option_string += image_file_name; |
| 76 | const char* image_option = image_option_string.c_str(); |
| 77 | arg_vector.push_back(strdup(image_option)); |
| 78 | |
| 79 | arg_vector.push_back(strdup("--runtime-arg")); |
| 80 | arg_vector.push_back(strdup("-Xms64m")); |
| 81 | |
| 82 | arg_vector.push_back(strdup("--runtime-arg")); |
| 83 | arg_vector.push_back(strdup("-Xmx64m")); |
| 84 | |
| 85 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 86 | std::string dex_file_option_string("--dex-file="); |
| 87 | dex_file_option_string += boot_class_path[i]; |
| 88 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 89 | arg_vector.push_back(strdup(dex_file_option)); |
| 90 | } |
| 91 | |
| 92 | std::string oat_file_option_string("--oat-file="); |
| 93 | oat_file_option_string += image_file_name; |
| 94 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 95 | oat_file_option_string += "oat"; |
| 96 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 97 | arg_vector.push_back(strdup(oat_file_option)); |
| 98 | |
| 99 | arg_vector.push_back(strdup("--base=0x60000000")); |
| 100 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 101 | std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 102 | LOG(INFO) << command_line; |
| 103 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 104 | arg_vector.push_back(NULL); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 105 | char** argv = &arg_vector[0]; |
| 106 | |
| 107 | // fork and exec dex2oat |
| 108 | pid_t pid = fork(); |
| 109 | if (pid == 0) { |
| 110 | // no allocation allowed between fork and exec |
| 111 | |
| 112 | // change process groups, so we don't get reaped by ProcessManager |
| 113 | setpgid(0, 0); |
| 114 | |
| 115 | execv(dex2oat, argv); |
| 116 | |
| 117 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 118 | return false; |
| 119 | } else { |
| 120 | STLDeleteElements(&arg_vector); |
| 121 | |
| 122 | // wait for dex2oat to finish |
| 123 | int status; |
| 124 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 125 | if (got_pid != pid) { |
| 126 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 127 | return false; |
| 128 | } |
| 129 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 130 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 137 | Heap::Heap(size_t initial_size, size_t growth_limit, size_t capacity, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 138 | const std::string& original_image_file_name, bool concurrent_gc) |
| 139 | : alloc_space_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 140 | card_table_(NULL), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 141 | concurrent_gc_(concurrent_gc), |
| 142 | have_zygote_space_(false), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 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), |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 148 | sticky_gc_count_(0), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 149 | num_bytes_allocated_(0), |
| 150 | num_objects_allocated_(0), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 151 | last_trim_time_(0), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 152 | try_running_gc_(false), |
| 153 | requesting_gc_(false), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 154 | reference_referent_offset_(0), |
| 155 | reference_queue_offset_(0), |
| 156 | reference_queueNext_offset_(0), |
| 157 | reference_pendingNext_offset_(0), |
| 158 | finalizer_reference_zombie_offset_(0), |
| 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 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 241 | // TODO: Count objects in the image space here. |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 242 | num_bytes_allocated_ = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 243 | num_objects_allocated_ = 0; |
| 244 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 245 | // Max stack size in bytes. |
| 246 | static const size_t max_stack_size = capacity / SpaceBitmap::kAlignment * kWordSize; |
| 247 | |
| 248 | // TODO: Rename MarkStack to a more generic name? |
| 249 | mark_stack_.reset(MarkStack::Create("dalvik-mark-stack", max_stack_size)); |
| 250 | allocation_stack_.reset(MarkStack::Create("dalvik-allocation-stack", max_stack_size)); |
| 251 | live_stack_.reset(MarkStack::Create("dalvik-live-stack", max_stack_size)); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 253 | // 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] | 254 | // but we can create the heap lock now. We don't create it earlier to |
| 255 | // make it clear that you can't use locks during heap initialization. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 256 | gc_complete_lock_ = new Mutex("GC complete lock"); |
| 257 | gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable")); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 258 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 259 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 260 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 261 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 264 | // Sort spaces based on begin address |
| 265 | class SpaceSorter { |
| 266 | public: |
| 267 | bool operator () (const Space* a, const Space* b) const { |
| 268 | return a->Begin() < b->Begin(); |
| 269 | } |
| 270 | }; |
| 271 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 272 | void Heap::AddSpace(Space* space) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 273 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 274 | DCHECK(space != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 275 | DCHECK(space->GetLiveBitmap() != NULL); |
| 276 | live_bitmap_->AddSpaceBitmap(space->GetLiveBitmap()); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 277 | DCHECK(space->GetMarkBitmap() != NULL); |
| 278 | mark_bitmap_->AddSpaceBitmap(space->GetMarkBitmap()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 279 | spaces_.push_back(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 280 | if (space->IsAllocSpace()) { |
| 281 | alloc_space_ = space->AsAllocSpace(); |
| 282 | } |
| 283 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 284 | // Ensure that spaces remain sorted in increasing order of start address (required for CMS finger) |
| 285 | std::sort(spaces_.begin(), spaces_.end(), SpaceSorter()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 286 | |
| 287 | // Ensure that ImageSpaces < ZygoteSpaces < AllocSpaces so that we can do address based checks to |
| 288 | // avoid redundant marking. |
| 289 | bool seen_zygote = false, seen_alloc = false; |
| 290 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 291 | Space* space = *it; |
| 292 | if (space->IsImageSpace()) { |
| 293 | DCHECK(!seen_zygote); |
| 294 | DCHECK(!seen_alloc); |
| 295 | } if (space->IsZygoteSpace()) { |
| 296 | DCHECK(!seen_alloc); |
| 297 | seen_zygote = true; |
| 298 | } else if (space->IsAllocSpace()) { |
| 299 | seen_alloc = true; |
| 300 | } |
| 301 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | Heap::~Heap() { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 305 | // If we don't reset then the mark stack complains in it's destructor. |
| 306 | allocation_stack_->Reset(); |
| 307 | live_stack_->Reset(); |
| 308 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 309 | VLOG(heap) << "~Heap()"; |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 310 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 311 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 312 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 313 | // 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] | 314 | STLDeleteElements(&spaces_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 315 | delete gc_complete_lock_; |
| 316 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 319 | Space* Heap::FindSpaceFromObject(const Object* obj) const { |
| 320 | // TODO: C++0x auto |
| 321 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 322 | if ((*cur)->Contains(obj)) { |
| 323 | return *cur; |
| 324 | } |
| 325 | } |
| 326 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | ImageSpace* Heap::GetImageSpace() { |
| 331 | // TODO: C++0x auto |
| 332 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 333 | if ((*cur)->IsImageSpace()) { |
| 334 | return (*cur)->AsImageSpace(); |
| 335 | } |
| 336 | } |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | AllocSpace* Heap::GetAllocSpace() { |
| 341 | return alloc_space_; |
| 342 | } |
| 343 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 344 | static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 345 | size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg); |
| 346 | |
| 347 | size_t chunk_size = static_cast<size_t>(reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start)); |
| 348 | size_t chunk_free_bytes = 0; |
| 349 | if (used_bytes < chunk_size) { |
| 350 | chunk_free_bytes = chunk_size - used_bytes; |
| 351 | } |
| 352 | |
| 353 | if (chunk_free_bytes > max_contiguous_allocation) { |
| 354 | max_contiguous_allocation = chunk_free_bytes; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | Object* Heap::AllocObject(Class* c, size_t byte_count) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 359 | DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(Class)) || |
| 360 | (c->IsVariableSize() || c->GetObjectSize() == byte_count) || |
| 361 | strlen(ClassHelper(c).GetDescriptor()) == 0); |
| 362 | DCHECK_GE(byte_count, sizeof(Object)); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 363 | Object* obj = Allocate(alloc_space_, byte_count); |
| 364 | if (LIKELY(obj != NULL)) { |
| 365 | #if VERIFY_OBJECT_ENABLED |
| 366 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
| 367 | // Verify objects doesn't like objects in allocation stack not being marked as live. |
| 368 | live_bitmap_->Set(obj); |
| 369 | #endif |
| 370 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 371 | obj->SetClass(c); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 372 | |
| 373 | // Record allocation after since we want to use the atomic add for the atomic fence to guard |
| 374 | // the SetClass since we do not want the class to appear NULL in another thread. |
| 375 | RecordAllocation(alloc_space_, obj); |
| 376 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 377 | if (Dbg::IsAllocTrackingEnabled()) { |
| 378 | Dbg::RecordAllocation(c, byte_count); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 379 | } |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 380 | const bool request_concurrent_gc = num_bytes_allocated_ >= concurrent_start_bytes_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 381 | if (request_concurrent_gc) { |
| 382 | // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint. |
| 383 | SirtRef<Object> ref(obj); |
| 384 | RequestConcurrentGC(); |
| 385 | } |
| 386 | VerifyObject(obj); |
| 387 | |
| 388 | // Additional verification to ensure that we did not allocate into a zygote space. |
| 389 | DCHECK(!have_zygote_space_ || !FindSpaceFromObject(obj)->IsZygoteSpace()); |
| 390 | |
| 391 | return obj; |
| 392 | } |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 393 | int64_t total_bytes_free = GetFreeMemory(); |
| 394 | size_t max_contiguous_allocation = 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 395 | // TODO: C++0x auto |
| 396 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 397 | if ((*cur)->IsAllocSpace()) { |
| 398 | (*cur)->AsAllocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 399 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 400 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 401 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 402 | std::string msg(StringPrintf("Failed to allocate a %zd-byte %s (%lld total bytes free; largest possible contiguous allocation %zd bytes)", |
| 403 | byte_count, |
| 404 | PrettyDescriptor(c).c_str(), |
| 405 | total_bytes_free, max_contiguous_allocation)); |
| 406 | Thread::Current()->ThrowOutOfMemoryError(msg.c_str()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 407 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 410 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 411 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 412 | // require taking the lock. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 413 | if (obj == NULL) { |
| 414 | return true; |
| 415 | } |
| 416 | if (!IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 417 | return false; |
| 418 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 419 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 420 | if (spaces_[i]->Contains(obj)) { |
| 421 | return true; |
| 422 | } |
| 423 | } |
| 424 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 427 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 428 | GlobalSynchronization::heap_bitmap_lock_->AssertReaderHeld(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 429 | return IsHeapAddress(obj) && GetLiveBitmap()->Test(obj); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 432 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 433 | void Heap::VerifyObject(const Object* obj) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 434 | if (obj == NULL || this == NULL || !verify_objects_ || Runtime::Current()->IsShuttingDown() || |
Ian Rogers | 141d622 | 2012-04-05 12:23:06 -0700 | [diff] [blame] | 435 | Thread::Current() == NULL || |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 436 | Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 437 | return; |
| 438 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 439 | VerifyObjectBody(obj); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 440 | } |
| 441 | #endif |
| 442 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 443 | void Heap::DumpSpaces() { |
| 444 | // TODO: C++0x auto |
| 445 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 446 | Space* space = *it; |
| 447 | LOG(INFO) << *space; |
| 448 | LOG(INFO) << *space->GetLiveBitmap(); |
| 449 | LOG(INFO) << *space->GetMarkBitmap(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 453 | // We want to avoid bit rotting. |
| 454 | void Heap::VerifyObjectBody(const Object* obj) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 455 | if (!IsAligned<kObjectAlignment>(obj)) { |
| 456 | LOG(FATAL) << "Object isn't aligned: " << obj; |
| 457 | } else if (!GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 458 | DumpSpaces(); |
| 459 | LOG(FATAL) << "Object is dead: " << obj; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 460 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 461 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 462 | // Ignore early dawn of the universe verifications |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 463 | if (!VERIFY_OBJECT_FAST && num_objects_allocated_ > 10) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 464 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 465 | Object::ClassOffset().Int32Value(); |
| 466 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 467 | if (c == NULL) { |
| 468 | LOG(FATAL) << "Null class in object: " << obj; |
| 469 | } else if (!IsAligned<kObjectAlignment>(c)) { |
| 470 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 471 | } else if (!GetLiveBitmap()->Test(c)) { |
| 472 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 473 | } |
| 474 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
| 475 | // Note: we don't use the accessors here as they have internal sanity checks |
| 476 | // that we don't want to run |
| 477 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
| 478 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 479 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
| 480 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 481 | CHECK_EQ(c_c, c_c_c); |
| 482 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 485 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 486 | DCHECK(obj != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 487 | reinterpret_cast<Heap*>(arg)->VerifyObjectBody(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void Heap::VerifyHeap() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 491 | ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 492 | GetLiveBitmap()->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 495 | void Heap::RecordAllocation(AllocSpace* space, const Object* obj) { |
| 496 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 497 | size_t size = space->AllocationSize(obj); |
| 498 | DCHECK_GT(size, 0u); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 499 | COMPILE_ASSERT(sizeof(size_t) == sizeof(int32_t), |
| 500 | int32_t_must_be_same_size_as_size_t_for_used_atomic_operations); |
Mathieu Chartier | 556fad3 | 2012-08-20 16:13:20 -0700 | [diff] [blame] | 501 | android_atomic_add(size, reinterpret_cast<volatile int32_t*>( |
| 502 | reinterpret_cast<size_t>(&num_bytes_allocated_))); |
| 503 | android_atomic_add(1, reinterpret_cast<volatile int32_t*>( |
| 504 | reinterpret_cast<size_t>(&num_objects_allocated_))); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 505 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 506 | if (Runtime::Current()->HasStatsEnabled()) { |
| 507 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 508 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 509 | ++global_stats->allocated_objects; |
| 510 | ++thread_stats->allocated_objects; |
| 511 | global_stats->allocated_bytes += size; |
| 512 | thread_stats->allocated_bytes += size; |
| 513 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 514 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 515 | |
| 516 | DCHECK(obj); |
| 517 | |
| 518 | allocation_stack_->AtomicPush(obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 521 | void Heap::RecordFree(size_t freed_objects, size_t freed_bytes) { |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 522 | COMPILE_ASSERT(sizeof(size_t) == sizeof(int32_t), |
| 523 | int32_t_must_be_same_size_as_size_t_for_used_atomic_operations); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 524 | DCHECK_LE(freed_objects, num_objects_allocated_); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 525 | android_atomic_add(-static_cast<int32_t>(freed_objects), |
Mathieu Chartier | 556fad3 | 2012-08-20 16:13:20 -0700 | [diff] [blame] | 526 | reinterpret_cast<volatile int32_t*>( |
| 527 | reinterpret_cast<size_t>(&num_objects_allocated_))); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 528 | |
| 529 | DCHECK_LE(freed_bytes, num_bytes_allocated_); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 530 | android_atomic_add(-static_cast<int32_t>(freed_bytes), |
Mathieu Chartier | 556fad3 | 2012-08-20 16:13:20 -0700 | [diff] [blame] | 531 | reinterpret_cast<volatile int32_t*>( |
| 532 | reinterpret_cast<size_t>(&num_bytes_allocated_))); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 533 | |
| 534 | if (Runtime::Current()->HasStatsEnabled()) { |
| 535 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 536 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 537 | global_stats->freed_objects += freed_objects; |
| 538 | thread_stats->freed_objects += freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 539 | global_stats->freed_bytes += freed_bytes; |
| 540 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 541 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 544 | Object* Heap::Allocate(AllocSpace* space, size_t alloc_size) { |
| 545 | Thread* self = Thread::Current(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 546 | // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are |
| 547 | // done in the runnable state where suspension is expected. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 548 | #ifndef NDEBUG |
| 549 | { |
| 550 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 551 | CHECK_EQ(self->GetState(), kRunnable); |
| 552 | } |
| 553 | self->AssertThreadSuspensionIsAllowable(); |
| 554 | #endif |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 555 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 556 | Object* ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 557 | if (ptr != NULL) { |
| 558 | return ptr; |
| 559 | } |
| 560 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 561 | // The allocation failed. If the GC is running, block until it completes else request a |
| 562 | // foreground partial collection. |
| 563 | if (!WaitForConcurrentGcToComplete()) { |
| 564 | // No concurrent GC so perform a foreground collection. |
| 565 | if (Runtime::Current()->HasStatsEnabled()) { |
| 566 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 567 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 568 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 569 | self->TransitionFromRunnableToSuspended(kWaitingPerformingGc); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 570 | CollectGarbageInternal(have_zygote_space_ ? GC_PARTIAL : GC_FULL, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 571 | self->TransitionFromSuspendedToRunnable(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 574 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 575 | if (ptr != NULL) { |
| 576 | return ptr; |
| 577 | } |
| 578 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 579 | const size_t alloc_space_size = alloc_space_->Size(); |
| 580 | if (alloc_space_size > kMinAllocSpaceSizeForStickyGC && |
| 581 | alloc_space_->Capacity() - alloc_space_size < kMinRemainingSpaceForStickyGC) { |
| 582 | // Partial GC didn't free enough memory, try a full GC. |
| 583 | if (Runtime::Current()->HasStatsEnabled()) { |
| 584 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 585 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 586 | } |
| 587 | |
| 588 | // Don't bother trying a young GC unless we have a few MB AllocSpace. |
| 589 | self->TransitionFromRunnableToSuspended(kWaitingPerformingGc); |
| 590 | CollectGarbageInternal(GC_STICKY, false); |
| 591 | self->TransitionFromSuspendedToRunnable(); |
| 592 | |
| 593 | ptr = space->AllocWithoutGrowth(alloc_size); |
| 594 | if (ptr != NULL) { |
| 595 | return ptr; |
| 596 | } |
| 597 | } |
| 598 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 599 | if (!have_zygote_space_) { |
| 600 | // Partial GC didn't free enough memory, try a full GC. |
| 601 | if (Runtime::Current()->HasStatsEnabled()) { |
| 602 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 603 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 604 | } |
| 605 | self->TransitionFromRunnableToSuspended(kWaitingPerformingGc); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 606 | CollectGarbageInternal(GC_PARTIAL, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 607 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 608 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 609 | ptr = space->AllocWithoutGrowth(alloc_size); |
| 610 | if (ptr != NULL) { |
| 611 | return ptr; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Allocations have failed after GCs; this is an exceptional state. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 616 | // Try harder, growing the heap if necessary. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 617 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 618 | if (ptr != NULL) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 619 | size_t new_footprint = space->GetFootprintLimit(); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 620 | // 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] | 621 | // free space is equal to the old free space + the |
| 622 | // utilization slop for the new allocation. |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 623 | VLOG(gc) << "Grow heap (frag case) to " << PrettySize(new_footprint) |
Ian Rogers | 162a31c | 2012-01-31 16:14:31 -0800 | [diff] [blame] | 624 | << " for a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 625 | return ptr; |
| 626 | } |
| 627 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 628 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 629 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 630 | // 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] | 631 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 632 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 633 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) << " allocation"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 634 | |
| 635 | if (Runtime::Current()->HasStatsEnabled()) { |
| 636 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 637 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 638 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 639 | // We don't need a WaitForConcurrentGcToComplete here either. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 640 | self->TransitionFromRunnableToSuspended(kWaitingPerformingGc); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 641 | CollectGarbageInternal(GC_FULL, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 642 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 643 | return space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 646 | int64_t Heap::GetMaxMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 647 | size_t total = 0; |
| 648 | // TODO: C++0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 649 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 650 | Space* space = *it; |
| 651 | if (space->IsAllocSpace()) { |
| 652 | total += space->AsAllocSpace()->Capacity(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | return total; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | int64_t Heap::GetTotalMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 659 | return GetMaxMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | int64_t Heap::GetFreeMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 663 | return GetMaxMemory() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 666 | class InstanceCounter { |
| 667 | public: |
| 668 | InstanceCounter(Class* c, bool count_assignable) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 669 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 670 | : class_(c), count_assignable_(count_assignable), count_(0) { |
| 671 | } |
| 672 | |
| 673 | size_t GetCount() { |
| 674 | return count_; |
| 675 | } |
| 676 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 677 | static void Callback(Object* o, void* arg) |
| 678 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 679 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 680 | } |
| 681 | |
| 682 | private: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 683 | void VisitInstance(Object* o) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 684 | Class* instance_class = o->GetClass(); |
| 685 | if (count_assignable_) { |
| 686 | if (instance_class == class_) { |
| 687 | ++count_; |
| 688 | } |
| 689 | } else { |
| 690 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 691 | ++count_; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | Class* class_; |
| 697 | bool count_assignable_; |
| 698 | size_t count_; |
| 699 | }; |
| 700 | |
| 701 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 702 | ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 703 | InstanceCounter counter(c, count_assignable); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 704 | GetLiveBitmap()->Walk(InstanceCounter::Callback, &counter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 705 | return counter.GetCount(); |
| 706 | } |
| 707 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 708 | void Heap::CollectGarbage(bool clear_soft_references) { |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 709 | // If we just waited for a GC to complete then we do not need to do another |
| 710 | // GC unless we clear soft references. |
| 711 | if (!WaitForConcurrentGcToComplete() || clear_soft_references) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 712 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingPerformingGc); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 713 | CollectGarbageInternal(have_zygote_space_ ? GC_PARTIAL : GC_FULL, clear_soft_references); |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 714 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 717 | void Heap::PreZygoteFork() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 718 | static Mutex zygote_creation_lock_("zygote creation lock", kZygoteCreationLock); |
| 719 | MutexLock mu(zygote_creation_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 720 | |
| 721 | // Try to see if we have any Zygote spaces. |
| 722 | if (have_zygote_space_) { |
| 723 | return; |
| 724 | } |
| 725 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 726 | VLOG(heap) << "Starting PreZygoteFork with alloc space size " << PrettySize(alloc_space_->Size()); |
| 727 | |
| 728 | { |
| 729 | // Flush the alloc stack. |
| 730 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
| 731 | FlushAllocStack(); |
| 732 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 733 | |
| 734 | // Replace the first alloc space we find with a zygote space. |
| 735 | // TODO: C++0x auto |
| 736 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 737 | if ((*it)->IsAllocSpace()) { |
| 738 | AllocSpace* zygote_space = (*it)->AsAllocSpace(); |
| 739 | |
| 740 | // Turns the current alloc space into a Zygote space and obtain the new alloc space composed |
| 741 | // of the remaining available heap memory. |
| 742 | alloc_space_ = zygote_space->CreateZygoteSpace(); |
| 743 | |
| 744 | // Change the GC retention policy of the zygote space to only collect when full. |
| 745 | zygote_space->SetGcRetentionPolicy(GCRP_FULL_COLLECT); |
| 746 | AddSpace(alloc_space_); |
| 747 | have_zygote_space_ = true; |
| 748 | break; |
| 749 | } |
| 750 | } |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 751 | |
| 752 | // Reset this since we now count the ZygoteSpace in the total heap size. |
| 753 | num_bytes_allocated_ = 0; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 756 | void Heap::FlushAllocStack() { |
| 757 | MarkStackAsLive(allocation_stack_.get()); |
| 758 | allocation_stack_->Reset(); |
| 759 | } |
| 760 | |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 761 | size_t Heap::GetUsedMemorySize() const { |
| 762 | size_t total = num_bytes_allocated_; |
| 763 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 764 | if ((*it)->IsZygoteSpace()) { |
| 765 | total += (*it)->AsAllocSpace()->Size(); |
| 766 | } |
| 767 | } |
| 768 | return total; |
| 769 | } |
| 770 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 771 | void Heap::MarkStackAsLive(MarkStack* alloc_stack) { |
| 772 | // We can just assume everything is inside the alloc_space_'s bitmap since we should only have |
| 773 | // fresh allocations. |
| 774 | SpaceBitmap* live_bitmap = alloc_space_->GetLiveBitmap(); |
| 775 | |
| 776 | // Empty the allocation stack. |
| 777 | const size_t count = alloc_stack->Size(); |
| 778 | for (size_t i = 0; i < count; ++i) { |
| 779 | const Object* obj = alloc_stack->Get(i); |
| 780 | DCHECK(obj != NULL); |
| 781 | live_bitmap->Set(obj); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void Heap::UnMarkStack(MarkStack* alloc_stack) { |
| 786 | SpaceBitmap* mark_bitmap = alloc_space_->GetMarkBitmap(); |
| 787 | |
| 788 | // Clear all of the things in the AllocStack. |
| 789 | size_t count = alloc_stack->Size(); |
| 790 | for (size_t i = 0;i < count;++i) { |
| 791 | const Object* obj = alloc_stack->Get(i); |
| 792 | DCHECK(obj != NULL); |
| 793 | if (mark_bitmap->Test(obj)) { |
| 794 | mark_bitmap->Clear(obj); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | void Heap::CollectGarbageInternal(GcType gc_type, bool clear_soft_references) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 800 | GlobalSynchronization::mutator_lock_->AssertNotHeld(); |
| 801 | #ifndef NDEBUG |
| 802 | { |
| 803 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 804 | CHECK_EQ(Thread::Current()->GetState(), kWaitingPerformingGc); |
| 805 | } |
| 806 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 807 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 808 | // Ensure there is only one GC at a time. |
| 809 | bool start_collect = false; |
| 810 | while (!start_collect) { |
| 811 | { |
| 812 | MutexLock mu(*gc_complete_lock_); |
| 813 | if (!is_gc_running_) { |
| 814 | is_gc_running_ = true; |
| 815 | start_collect = true; |
| 816 | } |
| 817 | } |
| 818 | if (!start_collect) { |
| 819 | WaitForConcurrentGcToComplete(); |
| 820 | // TODO: if another thread beat this one to do the GC, perhaps we should just return here? |
| 821 | // Not doing at the moment to ensure soft references are cleared. |
| 822 | } |
| 823 | } |
| 824 | gc_complete_lock_->AssertNotHeld(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 825 | |
| 826 | // We need to do partial GCs every now and then to avoid the heap growing too much and |
| 827 | // fragmenting. |
| 828 | if (gc_type == GC_STICKY && ++sticky_gc_count_ > kPartialGCFrequency) { |
| 829 | gc_type = GC_PARTIAL; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 830 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 831 | if (gc_type != GC_STICKY) { |
| 832 | sticky_gc_count_ = 0; |
| 833 | } |
| 834 | |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 835 | if (concurrent_gc_) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 836 | CollectGarbageConcurrentMarkSweepPlan(gc_type, clear_soft_references); |
| 837 | } else { |
| 838 | CollectGarbageMarkSweepPlan(gc_type, clear_soft_references); |
| 839 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 840 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 841 | gc_complete_lock_->AssertNotHeld(); |
| 842 | MutexLock mu(*gc_complete_lock_); |
| 843 | is_gc_running_ = false; |
| 844 | // Wake anyone who may have been waiting for the GC to complete. |
| 845 | gc_complete_cond_->Broadcast(); |
| 846 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 847 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 848 | void Heap::CollectGarbageMarkSweepPlan(GcType gc_type, bool clear_soft_references) { |
| 849 | TimingLogger timings("CollectGarbageInternal", true); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 850 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 851 | // Suspend all threads are get exclusive access to the heap. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 852 | uint64_t start_time = NanoTime(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 853 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 854 | thread_list->SuspendAll(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 855 | timings.AddSplit("SuspendAll"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 856 | GlobalSynchronization::mutator_lock_->AssertExclusiveHeld(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 857 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 858 | size_t bytes_freed = 0; |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 859 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 860 | { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 861 | MarkSweep mark_sweep(mark_stack_.get()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 862 | |
| 863 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 864 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 865 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 866 | // Make sure that the tables have the correct pointer for the mark sweep. |
| 867 | mod_union_table_->Init(&mark_sweep); |
| 868 | zygote_mod_union_table_->Init(&mark_sweep); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 869 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 870 | // Swap allocation stack and live stack, enabling us to have new allocations during this GC. |
| 871 | MarkStack* temp = allocation_stack_.release(); |
| 872 | allocation_stack_.reset(live_stack_.release()); |
| 873 | live_stack_.reset(temp); |
| 874 | |
| 875 | // We will need to know which cards were dirty for doing concurrent processing of dirty cards. |
| 876 | // TODO: Investigate using a mark stack instead of a vector. |
| 877 | std::vector<byte*> dirty_cards; |
| 878 | if (gc_type == GC_STICKY) { |
| 879 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 880 | card_table_->GetDirtyCards(*it, dirty_cards); |
| 881 | } |
| 882 | } |
| 883 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 884 | // 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] | 885 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 886 | Space* space = *it; |
| 887 | if (space->IsImageSpace()) { |
| 888 | mod_union_table_->ClearCards(*it); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 889 | timings.AddSplit("ClearModUnionCards"); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 890 | } else if (space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 891 | zygote_mod_union_table_->ClearCards(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 892 | timings.AddSplit("ClearZygoteCards"); |
| 893 | } else { |
| 894 | card_table_->ClearSpaceCards(space); |
| 895 | timings.AddSplit("ClearCards"); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 898 | |
| 899 | #if VERIFY_MOD_UNION |
| 900 | mod_union_table_->Verify(); |
| 901 | zygote_mod_union_table_->Verify(); |
| 902 | #endif |
| 903 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 904 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 905 | if (gc_type == GC_PARTIAL) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 906 | // Copy the mark bits over from the live bits, do this as early as possible or else we can |
| 907 | // accidentally un-mark roots. |
| 908 | // Needed for scanning dirty objects. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 909 | for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) { |
| 910 | if ((*it)->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 911 | mark_sweep.CopyMarkBits(*it); |
| 912 | } |
| 913 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 914 | timings.AddSplit("CopyMarkBits"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 915 | |
| 916 | // We can assume that everything < alloc_space_ start is marked at this point. |
| 917 | mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin())); |
| 918 | } else if (gc_type == GC_STICKY) { |
| 919 | for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) { |
| 920 | if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) { |
| 921 | mark_sweep.CopyMarkBits(*it); |
| 922 | } |
| 923 | } |
| 924 | timings.AddSplit("CopyMarkBits"); |
| 925 | |
| 926 | if (VERIFY_OBJECT_ENABLED) { |
| 927 | UnMarkStack(live_stack_.get()); |
| 928 | } |
| 929 | |
| 930 | mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin())); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 931 | } |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 932 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 933 | MarkStackAsLive(live_stack_.get()); |
| 934 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 935 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 936 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 937 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 938 | // Roots are marked on the bitmap and the mark_stack is empty. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 939 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 940 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 941 | // Update zygote mod union table. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 942 | zygote_mod_union_table_->Update(); |
| 943 | timings.AddSplit("UpdateZygoteModUnionTable"); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 944 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 945 | zygote_mod_union_table_->MarkReferences(); |
| 946 | timings.AddSplit("ZygoteMarkReferences"); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 947 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 948 | // 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] | 949 | mod_union_table_->Update(); |
Mathieu Chartier | e6e0651 | 2012-06-26 15:00:26 -0700 | [diff] [blame] | 950 | timings.AddSplit("UpdateModUnionTable"); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 951 | |
| 952 | // Scans all objects in the mod-union table. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 953 | mod_union_table_->MarkReferences(); |
Mathieu Chartier | e6e0651 | 2012-06-26 15:00:26 -0700 | [diff] [blame] | 954 | timings.AddSplit("MarkImageToAllocSpaceReferences"); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 955 | |
| 956 | // Recursively mark all the non-image bits set in the mark bitmap. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 957 | if (gc_type != GC_STICKY) { |
| 958 | live_stack_->Reset(); |
| 959 | mark_sweep.RecursiveMark(gc_type == GC_PARTIAL, timings); |
| 960 | } else { |
| 961 | mark_sweep.RecursiveMarkCards(card_table_.get(), dirty_cards, timings); |
| 962 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 963 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 964 | // Need to process references the swap since it uses IsMarked. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 965 | mark_sweep.ProcessReferences(clear_soft_references); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 966 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 967 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 968 | // This doesn't work with mutators unpaused for some reason, TODO: Fix. |
| 969 | mark_sweep.SweepSystemWeaks(false); |
| 970 | timings.AddSplit("SweepSystemWeaks"); |
| 971 | |
| 972 | // Need to swap for VERIFY_OBJECT_ENABLED since we put things in the live bitmap after they |
| 973 | // have been allocated. |
| 974 | const bool swap = true; |
| 975 | |
| 976 | if (swap) { |
| 977 | // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps |
| 978 | // these bitmaps. Doing this enables us to sweep with the heap unlocked since new allocations |
| 979 | // set the live bit, but since we have the bitmaps reversed at this point, this sets the mark bit |
| 980 | // instead, resulting in no new allocated objects being incorrectly freed by sweep. |
| 981 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 982 | Space* space = *it; |
| 983 | // We only allocate into AllocSpace, so we only need to swap AllocSpaces. |
| 984 | if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) { |
| 985 | live_bitmap_->ReplaceBitmap(space->GetLiveBitmap(), space->GetMarkBitmap()); |
| 986 | mark_bitmap_->ReplaceBitmap(space->GetMarkBitmap(), space->GetLiveBitmap()); |
| 987 | space->AsAllocSpace()->SwapBitmaps(); |
| 988 | } |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 989 | } |
| 990 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 991 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 992 | #ifndef NDEBUG |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 993 | // Verify that we only reach marked objects from the image space |
| 994 | mark_sweep.VerifyImageRoots(); |
| 995 | timings.AddSplit("VerifyImageRoots"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 996 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 997 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 998 | if (gc_type != GC_STICKY) { |
| 999 | mark_sweep.Sweep(gc_type == GC_PARTIAL, swap); |
| 1000 | } else { |
| 1001 | mark_sweep.SweepArray(timings, live_stack_.get(), swap); |
| 1002 | } |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1003 | timings.AddSplit("Sweep"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1004 | |
| 1005 | cleared_references = mark_sweep.GetClearedReferences(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1006 | bytes_freed = mark_sweep.GetFreedBytes(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1010 | timings.AddSplit("GrowForUtilization"); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1011 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1012 | thread_list->ResumeAll(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1013 | timings.AddSplit("ResumeAll"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1014 | |
| 1015 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1016 | RequestHeapTrim(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 1017 | timings.AddSplit("Finish"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 1018 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1019 | // If the GC was slow, then print timings in the log. |
| 1020 | uint64_t duration = (NanoTime() - start_time) / 1000 * 1000; |
| 1021 | if (duration > MsToNs(50)) { |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1022 | const size_t percent_free = GetPercentFree(); |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1023 | const size_t current_heap_size = GetUsedMemorySize(); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1024 | const size_t total_memory = GetTotalMemory(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1025 | LOG(INFO) << (gc_type == GC_PARTIAL ? "Partial " : (gc_type == GC_STICKY ? "Sticky " : "")) |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1026 | << "GC freed " << PrettySize(bytes_freed) << ", " << percent_free << "% free, " |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1027 | << PrettySize(current_heap_size) << "/" << PrettySize(total_memory) << ", " |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1028 | << "paused " << PrettyDuration(duration); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1029 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1030 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1031 | if (VLOG_IS_ON(heap)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1032 | timings.Dump(); |
| 1033 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1034 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1035 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1036 | void Heap::CollectGarbageConcurrentMarkSweepPlan(GcType gc_type, bool clear_soft_references) { |
| 1037 | TimingLogger timings("ConcurrentCollectGarbageInternal", true); |
| 1038 | uint64_t root_begin = NanoTime(), root_end = 0, dirty_begin = 0, dirty_end = 0; |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1039 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1040 | // Suspend all threads are get exclusive access to the heap. |
| 1041 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1042 | thread_list->SuspendAll(); |
| 1043 | timings.AddSplit("SuspendAll"); |
| 1044 | GlobalSynchronization::mutator_lock_->AssertExclusiveHeld(); |
| 1045 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1046 | size_t bytes_freed = 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1047 | Object* cleared_references = NULL; |
| 1048 | { |
| 1049 | MarkSweep mark_sweep(mark_stack_.get()); |
| 1050 | timings.AddSplit("ctor"); |
| 1051 | |
| 1052 | mark_sweep.Init(); |
| 1053 | timings.AddSplit("Init"); |
| 1054 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1055 | // Swap the stacks, this is safe sunce all the mutators are suspended at this point. |
| 1056 | MarkStack* temp = allocation_stack_.release(); |
| 1057 | allocation_stack_.reset(live_stack_.release()); |
| 1058 | live_stack_.reset(temp); |
| 1059 | |
| 1060 | // We will need to know which cards were dirty for doing concurrent processing of dirty cards. |
| 1061 | // TODO: Investigate using a mark stack instead of a vector. |
| 1062 | std::vector<byte*> dirty_cards; |
| 1063 | if (gc_type == GC_STICKY) { |
| 1064 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1065 | card_table_->GetDirtyCards(*it, dirty_cards); |
| 1066 | } |
| 1067 | } |
| 1068 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1069 | // Make sure that the tables have the correct pointer for the mark sweep. |
| 1070 | mod_union_table_->Init(&mark_sweep); |
| 1071 | zygote_mod_union_table_->Init(&mark_sweep); |
| 1072 | |
| 1073 | // Clear image space cards and keep track of cards we cleared in the mod-union table. |
| 1074 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1075 | Space* space = *it; |
| 1076 | if (space->IsImageSpace()) { |
| 1077 | mod_union_table_->ClearCards(*it); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1078 | timings.AddSplit("ModUnionClearCards"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1079 | } else if (space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 1080 | zygote_mod_union_table_->ClearCards(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1081 | timings.AddSplit("ZygoteModUnionClearCards"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1082 | } else { |
| 1083 | card_table_->ClearSpaceCards(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1084 | timings.AddSplit("ClearCards"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1085 | } |
| 1086 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1087 | |
| 1088 | #if VERIFY_MOD_UNION |
| 1089 | mod_union_table_->Verify(); |
| 1090 | zygote_mod_union_table_->Verify(); |
| 1091 | #endif |
| 1092 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1093 | |
| 1094 | { |
| 1095 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (gc_type == GC_PARTIAL) { |
| 1098 | // Copy the mark bits over from the live bits, do this as early as possible or else we can |
| 1099 | // accidentally un-mark roots. |
| 1100 | // Needed for scanning dirty objects. |
| 1101 | for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) { |
| 1102 | if ((*it)->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 1103 | mark_sweep.CopyMarkBits(*it); |
| 1104 | } |
| 1105 | } |
| 1106 | timings.AddSplit("CopyMarkBits"); |
| 1107 | mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin())); |
| 1108 | } else if (gc_type == GC_STICKY) { |
| 1109 | for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) { |
| 1110 | if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) { |
| 1111 | mark_sweep.CopyMarkBits(*it); |
| 1112 | } |
| 1113 | } |
| 1114 | timings.AddSplit("CopyMarkBits"); |
| 1115 | // We need to unmark the new objects since we marked them as live earlier to avoid verify |
| 1116 | // objects failing. |
| 1117 | if (VERIFY_OBJECT_ENABLED) { |
| 1118 | UnMarkStack(live_stack_.get()); |
| 1119 | } |
| 1120 | mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin())); |
| 1121 | } |
| 1122 | |
| 1123 | // TODO: Investigate whether or not this is really necessary for sticky mark bits. |
| 1124 | MarkStackAsLive(live_stack_.get()); |
| 1125 | |
| 1126 | if (gc_type != GC_STICKY) { |
| 1127 | live_stack_->Reset(); |
| 1128 | mark_sweep.MarkRoots(); |
| 1129 | timings.AddSplit("MarkRoots"); |
| 1130 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | // Roots are marked on the bitmap and the mark_stack is empty. |
| 1134 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
| 1135 | |
| 1136 | // Allow mutators to go again, acquire share on mutator_lock_ to continue. |
| 1137 | thread_list->ResumeAll(); |
| 1138 | { |
| 1139 | ReaderMutexLock reader_lock(*GlobalSynchronization::mutator_lock_); |
| 1140 | root_end = NanoTime(); |
| 1141 | timings.AddSplit("RootEnd"); |
| 1142 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1143 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
| 1144 | if (gc_type != GC_STICKY) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1145 | // Update zygote mod union table. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1146 | if (gc_type == GC_PARTIAL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1147 | zygote_mod_union_table_->Update(); |
| 1148 | timings.AddSplit("UpdateZygoteModUnionTable"); |
| 1149 | |
| 1150 | zygote_mod_union_table_->MarkReferences(); |
| 1151 | timings.AddSplit("ZygoteMarkReferences"); |
| 1152 | } |
| 1153 | |
| 1154 | // Processes the cards we cleared earlier and adds their objects into the mod-union table. |
| 1155 | mod_union_table_->Update(); |
| 1156 | timings.AddSplit("UpdateModUnionTable"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1157 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1158 | // Scans all objects in the mod-union table. |
| 1159 | mod_union_table_->MarkReferences(); |
| 1160 | timings.AddSplit("MarkImageToAllocSpaceReferences"); |
| 1161 | |
| 1162 | // Recursively mark all the non-image bits set in the mark bitmap. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1163 | mark_sweep.RecursiveMark(gc_type == GC_PARTIAL, timings); |
| 1164 | } else { |
| 1165 | mark_sweep.RecursiveMarkCards(card_table_.get(), dirty_cards, timings); |
| 1166 | mark_sweep.DisableFinger(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | // Release share on mutator_lock_ and then get exclusive access. |
| 1170 | dirty_begin = NanoTime(); |
| 1171 | thread_list->SuspendAll(); |
| 1172 | timings.AddSplit("ReSuspend"); |
| 1173 | GlobalSynchronization::mutator_lock_->AssertExclusiveHeld(); |
| 1174 | |
| 1175 | { |
| 1176 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1177 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1178 | // Re-mark root set. |
| 1179 | mark_sweep.ReMarkRoots(); |
| 1180 | timings.AddSplit("ReMarkRoots"); |
| 1181 | |
| 1182 | // Scan dirty objects, this is only required if we are not doing concurrent GC. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1183 | mark_sweep.RecursiveMarkDirtyObjects(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1184 | timings.AddSplit("RecursiveMarkDirtyObjects"); |
| 1185 | } |
| 1186 | { |
| 1187 | ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
| 1188 | mark_sweep.ProcessReferences(clear_soft_references); |
| 1189 | timings.AddSplit("ProcessReferences"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1190 | |
| 1191 | // This doesn't work with mutators unpaused for some reason, TODO: Fix. |
| 1192 | mark_sweep.SweepSystemWeaks(false); |
| 1193 | timings.AddSplit("SweepSystemWeaks"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1194 | } |
| 1195 | // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps |
| 1196 | // these bitmaps. Doing this enables us to sweep with the heap unlocked since new allocations |
| 1197 | // set the live bit, but since we have the bitmaps reversed at this point, this sets the mark |
| 1198 | // bit instead, resulting in no new allocated objects being incorrectly freed by sweep. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1199 | bool swap = true; |
| 1200 | if (swap) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1201 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
| 1202 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1203 | Space* space = *it; |
| 1204 | // We never allocate into zygote spaces. |
| 1205 | if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) { |
| 1206 | live_bitmap_->ReplaceBitmap(space->GetLiveBitmap(), space->GetMarkBitmap()); |
| 1207 | mark_bitmap_->ReplaceBitmap(space->GetMarkBitmap(), space->GetLiveBitmap()); |
| 1208 | space->AsAllocSpace()->SwapBitmaps(); |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | if (kIsDebugBuild) { |
| 1214 | // Verify that we only reach marked objects from the image space. |
| 1215 | ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
| 1216 | mark_sweep.VerifyImageRoots(); |
| 1217 | timings.AddSplit("VerifyImageRoots"); |
| 1218 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1219 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1220 | thread_list->ResumeAll(); |
| 1221 | dirty_end = NanoTime(); |
| 1222 | GlobalSynchronization::mutator_lock_->AssertNotHeld(); |
| 1223 | |
| 1224 | { |
| 1225 | // TODO: this lock shouldn't be necessary (it's why we did the bitmap flip above). |
| 1226 | WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1227 | if (gc_type != GC_STICKY) { |
| 1228 | mark_sweep.Sweep(gc_type == GC_PARTIAL, swap); |
| 1229 | } else { |
| 1230 | mark_sweep.SweepArray(timings, live_stack_.get(), swap); |
| 1231 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1232 | timings.AddSplit("Sweep"); |
| 1233 | } |
| 1234 | |
| 1235 | cleared_references = mark_sweep.GetClearedReferences(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1236 | bytes_freed = mark_sweep.GetFreedBytes(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | GrowForUtilization(); |
| 1240 | timings.AddSplit("GrowForUtilization"); |
| 1241 | |
| 1242 | EnqueueClearedReferences(&cleared_references); |
| 1243 | RequestHeapTrim(); |
| 1244 | timings.AddSplit("Finish"); |
| 1245 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1246 | // If the GC was slow, then print timings in the log. |
| 1247 | uint64_t pause_roots = (root_end - root_begin) / 1000 * 1000; |
| 1248 | uint64_t pause_dirty = (dirty_end - dirty_begin) / 1000 * 1000; |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1249 | uint64_t duration = (NanoTime() - root_begin) / 1000 * 1000; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1250 | if (pause_roots > MsToNs(5) || pause_dirty > MsToNs(5)) { |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1251 | const size_t percent_free = GetPercentFree(); |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1252 | const size_t current_heap_size = GetUsedMemorySize(); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1253 | const size_t total_memory = GetTotalMemory(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1254 | LOG(INFO) << (gc_type == GC_PARTIAL ? "Partial " : (gc_type == GC_STICKY ? "Sticky " : "")) |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1255 | << "Concurrent GC freed " << PrettySize(bytes_freed) << ", " << percent_free |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1256 | << "% free, " << PrettySize(current_heap_size) << "/" |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1257 | << PrettySize(total_memory) << ", " << "paused " << PrettyDuration(pause_roots) |
| 1258 | << "+" << PrettyDuration(pause_dirty) << " total " << PrettyDuration(duration); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1259 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1260 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1261 | if (VLOG_IS_ON(heap)) { |
| 1262 | timings.Dump(); |
| 1263 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 1266 | bool Heap::WaitForConcurrentGcToComplete() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1267 | if (concurrent_gc_) { |
| 1268 | bool do_wait = false; |
| 1269 | uint64_t wait_start; |
| 1270 | { |
| 1271 | // Check if GC is running holding gc_complete_lock_. |
| 1272 | MutexLock mu(*gc_complete_lock_); |
| 1273 | if (is_gc_running_) { |
| 1274 | wait_start = NanoTime(); |
| 1275 | do_wait = true; |
| 1276 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1277 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1278 | if (do_wait) { |
| 1279 | // We must wait, change thread state then sleep on gc_complete_cond_; |
| 1280 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingForGcToComplete); |
| 1281 | { |
| 1282 | MutexLock mu(*gc_complete_lock_); |
| 1283 | while (is_gc_running_) { |
| 1284 | gc_complete_cond_->Wait(*gc_complete_lock_); |
| 1285 | } |
| 1286 | } |
| 1287 | uint64_t wait_time = NanoTime() - wait_start; |
| 1288 | if (wait_time > MsToNs(5)) { |
| 1289 | LOG(INFO) << "WaitForConcurrentGcToComplete blocked for " << PrettyDuration(wait_time); |
| 1290 | } |
| 1291 | return true; |
| 1292 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1293 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 1294 | return false; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1297 | void Heap::DumpForSigQuit(std::ostream& os) { |
| 1298 | os << "Heap: " << GetPercentFree() << "% free, " |
| 1299 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 1300 | << "; " << num_objects_allocated_ << " objects\n"; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | size_t Heap::GetPercentFree() { |
| 1304 | size_t total = GetTotalMemory(); |
| 1305 | return 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total); |
| 1306 | } |
| 1307 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1308 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1309 | AllocSpace* alloc_space = alloc_space_; |
| 1310 | // TODO: Behavior for multiple alloc spaces? |
| 1311 | size_t alloc_space_capacity = alloc_space->Capacity(); |
| 1312 | if (max_allowed_footprint > alloc_space_capacity) { |
| 1313 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) |
| 1314 | << " to " << PrettySize(alloc_space_capacity); |
| 1315 | max_allowed_footprint = alloc_space_capacity; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1316 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1317 | alloc_space->SetFootprintLimit(max_allowed_footprint); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 1320 | // 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] | 1321 | static const size_t kHeapIdealFree = 2 * MB; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 1322 | // kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization, |
| 1323 | // regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1324 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 1325 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1326 | void Heap::GrowForUtilization() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1327 | size_t target_size; |
| 1328 | bool use_footprint_limit = false; |
| 1329 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1330 | // We know what our utilization is at this moment. |
| 1331 | // This doesn't actually resize any memory. It just lets the heap grow more when necessary. |
| 1332 | target_size = num_bytes_allocated_ / Heap::GetTargetHeapUtilization(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1333 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1334 | if (target_size > num_bytes_allocated_ + kHeapIdealFree) { |
| 1335 | target_size = num_bytes_allocated_ + kHeapIdealFree; |
| 1336 | } else if (target_size < num_bytes_allocated_ + kHeapMinFree) { |
| 1337 | target_size = num_bytes_allocated_ + kHeapMinFree; |
| 1338 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1339 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1340 | // Calculate when to perform the next ConcurrentGC. |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1341 | if (GetTotalMemory() - GetUsedMemorySize() < concurrent_min_free_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1342 | // Not enough free memory to perform concurrent GC. |
| 1343 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 1344 | } else { |
| 1345 | // Compute below to avoid holding both the statistics and the alloc space lock |
| 1346 | use_footprint_limit = true; |
| 1347 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1348 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1349 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1350 | if (use_footprint_limit) { |
| 1351 | size_t foot_print_limit = alloc_space_->GetFootprintLimit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1352 | concurrent_start_bytes_ = foot_print_limit - concurrent_start_size_; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1353 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1354 | SetIdealFootprint(target_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1357 | void Heap::ClearGrowthLimit() { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1358 | WaitForConcurrentGcToComplete(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1359 | alloc_space_->ClearGrowthLimit(); |
| 1360 | } |
| 1361 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1362 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 1363 | MemberOffset reference_queue_offset, |
| 1364 | MemberOffset reference_queueNext_offset, |
| 1365 | MemberOffset reference_pendingNext_offset, |
| 1366 | MemberOffset finalizer_reference_zombie_offset) { |
| 1367 | reference_referent_offset_ = reference_referent_offset; |
| 1368 | reference_queue_offset_ = reference_queue_offset; |
| 1369 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 1370 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 1371 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 1372 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1373 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 1374 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 1375 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 1376 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 1377 | } |
| 1378 | |
| 1379 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 1380 | DCHECK(reference != NULL); |
| 1381 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1382 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 1383 | } |
| 1384 | |
| 1385 | void Heap::ClearReferenceReferent(Object* reference) { |
| 1386 | DCHECK(reference != NULL); |
| 1387 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1388 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 1389 | } |
| 1390 | |
| 1391 | // Returns true if the reference object has not yet been enqueued. |
| 1392 | bool Heap::IsEnqueuable(const Object* ref) { |
| 1393 | DCHECK(ref != NULL); |
| 1394 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 1395 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 1396 | return (queue != NULL) && (queue_next == NULL); |
| 1397 | } |
| 1398 | |
| 1399 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 1400 | DCHECK(ref != NULL); |
| 1401 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 1402 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 1403 | EnqueuePendingReference(ref, cleared_reference_list); |
| 1404 | } |
| 1405 | |
| 1406 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 1407 | DCHECK(ref != NULL); |
| 1408 | DCHECK(list != NULL); |
| 1409 | |
| 1410 | if (*list == NULL) { |
| 1411 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1412 | *list = ref; |
| 1413 | } else { |
| 1414 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1415 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 1416 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | Object* Heap::DequeuePendingReference(Object** list) { |
| 1421 | DCHECK(list != NULL); |
| 1422 | DCHECK(*list != NULL); |
| 1423 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1424 | Object* ref; |
| 1425 | if (*list == head) { |
| 1426 | ref = *list; |
| 1427 | *list = NULL; |
| 1428 | } else { |
| 1429 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1430 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 1431 | ref = head; |
| 1432 | } |
| 1433 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 1434 | return ref; |
| 1435 | } |
| 1436 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 1437 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1438 | ScopedObjectAccess soa(self); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1439 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1440 | args[0].SetL(object); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1441 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_FinalizerReference_add)->Invoke(self, |
| 1442 | NULL, args, NULL); |
| 1443 | } |
| 1444 | |
| 1445 | size_t Heap::GetBytesAllocated() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1446 | return num_bytes_allocated_; |
| 1447 | } |
| 1448 | |
| 1449 | size_t Heap::GetObjectsAllocated() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1450 | return num_objects_allocated_; |
| 1451 | } |
| 1452 | |
| 1453 | size_t Heap::GetConcurrentStartSize() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1454 | return concurrent_start_size_; |
| 1455 | } |
| 1456 | |
| 1457 | size_t Heap::GetConcurrentMinFree() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1458 | return concurrent_min_free_; |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 1462 | DCHECK(cleared != NULL); |
| 1463 | if (*cleared != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1464 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1465 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1466 | args[0].SetL(*cleared); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1467 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_ReferenceQueue_add)->Invoke(soa.Self(), |
| 1468 | NULL, args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1469 | *cleared = NULL; |
| 1470 | } |
| 1471 | } |
| 1472 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1473 | void Heap::RequestConcurrentGC() { |
Mathieu Chartier | 069387a | 2012-06-18 12:01:01 -0700 | [diff] [blame] | 1474 | // Make sure that we can do a concurrent GC. |
| 1475 | if (requesting_gc_ || |
| 1476 | !Runtime::Current()->IsFinishedStarting() || |
| 1477 | Runtime::Current()->IsShuttingDown() || |
| 1478 | !Runtime::Current()->IsConcurrentGcEnabled()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1479 | return; |
| 1480 | } |
| 1481 | |
| 1482 | requesting_gc_ = true; |
| 1483 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1484 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1485 | DCHECK(WellKnownClasses::java_lang_Daemons_requestGC != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1486 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 1487 | WellKnownClasses::java_lang_Daemons_requestGC); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1488 | CHECK(!env->ExceptionCheck()); |
| 1489 | requesting_gc_ = false; |
| 1490 | } |
| 1491 | |
| 1492 | void Heap::ConcurrentGC() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1493 | if (Runtime::Current()->IsShuttingDown() || !concurrent_gc_) { |
Mathieu Chartier | 2542d66 | 2012-06-21 17:14:11 -0700 | [diff] [blame] | 1494 | return; |
| 1495 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1496 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1497 | // TODO: We shouldn't need a WaitForConcurrentGcToComplete here since only |
| 1498 | // concurrent GC resumes threads before the GC is completed and this function |
| 1499 | // is only called within the GC daemon thread. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1500 | if (!WaitForConcurrentGcToComplete()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1501 | // Start a concurrent GC as one wasn't in progress |
| 1502 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingPerformingGc); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1503 | if (alloc_space_->Size() > kMinAllocSpaceSizeForStickyGC) { |
| 1504 | CollectGarbageInternal(GC_STICKY, false); |
| 1505 | } else { |
| 1506 | CollectGarbageInternal(GC_PARTIAL, false); |
| 1507 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1508 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1511 | void Heap::Trim(AllocSpace* alloc_space) { |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1512 | WaitForConcurrentGcToComplete(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1513 | alloc_space->Trim(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1516 | void Heap::RequestHeapTrim() { |
| 1517 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 1518 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 1519 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 1520 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 1521 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 1522 | // not how much use we're making of those pages. |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1523 | uint64_t ms_time = NsToMs(NanoTime()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1524 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1525 | float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size(); |
| 1526 | if ((utilization > 0.75f) || ((ms_time - last_trim_time_) < 2 * 1000)) { |
| 1527 | // Don't bother trimming the heap if it's more than 75% utilized, or if a |
| 1528 | // heap trim occurred in the last two seconds. |
| 1529 | return; |
| 1530 | } |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1531 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1532 | if (!Runtime::Current()->IsFinishedStarting() || Runtime::Current()->IsShuttingDown()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1533 | // 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] | 1534 | // 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] | 1535 | return; |
| 1536 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1537 | last_trim_time_ = ms_time; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1538 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1539 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1540 | DCHECK(WellKnownClasses::java_lang_Daemons_requestHeapTrim != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1541 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 1542 | WellKnownClasses::java_lang_Daemons_requestHeapTrim); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1543 | CHECK(!env->ExceptionCheck()); |
| 1544 | } |
| 1545 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1546 | } // namespace art |