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" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 37 | #include "sirt_ref.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 38 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 39 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 40 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 41 | #include "timing_logger.h" |
| 42 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 43 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 47 | static bool GenerateImage(const std::string& image_file_name) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 48 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 49 | std::vector<std::string> boot_class_path; |
| 50 | Split(boot_class_path_string, ':', boot_class_path); |
Brian Carlstrom | b279337 | 2012-03-17 18:27:16 -0700 | [diff] [blame] | 51 | if (boot_class_path.empty()) { |
| 52 | LOG(FATAL) << "Failed to generate image because no boot class path specified"; |
| 53 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 54 | |
| 55 | std::vector<char*> arg_vector; |
| 56 | |
| 57 | std::string dex2oat_string(GetAndroidRoot()); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 58 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 59 | const char* dex2oat = dex2oat_string.c_str(); |
| 60 | arg_vector.push_back(strdup(dex2oat)); |
| 61 | |
| 62 | std::string image_option_string("--image="); |
| 63 | image_option_string += image_file_name; |
| 64 | const char* image_option = image_option_string.c_str(); |
| 65 | arg_vector.push_back(strdup(image_option)); |
| 66 | |
| 67 | arg_vector.push_back(strdup("--runtime-arg")); |
| 68 | arg_vector.push_back(strdup("-Xms64m")); |
| 69 | |
| 70 | arg_vector.push_back(strdup("--runtime-arg")); |
| 71 | arg_vector.push_back(strdup("-Xmx64m")); |
| 72 | |
| 73 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 74 | std::string dex_file_option_string("--dex-file="); |
| 75 | dex_file_option_string += boot_class_path[i]; |
| 76 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 77 | arg_vector.push_back(strdup(dex_file_option)); |
| 78 | } |
| 79 | |
| 80 | std::string oat_file_option_string("--oat-file="); |
| 81 | oat_file_option_string += image_file_name; |
| 82 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 83 | oat_file_option_string += "oat"; |
| 84 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 85 | arg_vector.push_back(strdup(oat_file_option)); |
| 86 | |
| 87 | arg_vector.push_back(strdup("--base=0x60000000")); |
| 88 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 89 | std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 90 | LOG(INFO) << command_line; |
| 91 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 92 | arg_vector.push_back(NULL); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 93 | char** argv = &arg_vector[0]; |
| 94 | |
| 95 | // fork and exec dex2oat |
| 96 | pid_t pid = fork(); |
| 97 | if (pid == 0) { |
| 98 | // no allocation allowed between fork and exec |
| 99 | |
| 100 | // change process groups, so we don't get reaped by ProcessManager |
| 101 | setpgid(0, 0); |
| 102 | |
| 103 | execv(dex2oat, argv); |
| 104 | |
| 105 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 106 | return false; |
| 107 | } else { |
| 108 | STLDeleteElements(&arg_vector); |
| 109 | |
| 110 | // wait for dex2oat to finish |
| 111 | int status; |
| 112 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 113 | if (got_pid != pid) { |
| 114 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 115 | return false; |
| 116 | } |
| 117 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 118 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 125 | 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] | 126 | const std::string& original_image_file_name, bool concurrent_gc) |
| 127 | : alloc_space_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 128 | card_table_(NULL), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | concurrent_gc_(concurrent_gc), |
| 130 | have_zygote_space_(false), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 131 | card_marking_disabled_(false), |
| 132 | is_gc_running_(false), |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 133 | last_gc_type_(kGcTypeNone), |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 134 | concurrent_start_bytes_(std::numeric_limits<size_t>::max()), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 135 | concurrent_start_size_(128 * KB), |
| 136 | concurrent_min_free_(256 * KB), |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 137 | bytes_since_last_gc_(0), |
| 138 | concurrent_gc_start_rate_(3 * MB / 2), |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 139 | sticky_gc_count_(0), |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 140 | large_object_threshold_(12 * KB), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 141 | num_bytes_allocated_(0), |
| 142 | num_objects_allocated_(0), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 143 | verify_missing_card_marks_(false), |
| 144 | verify_system_weaks_(false), |
| 145 | verify_pre_gc_heap_(false), |
| 146 | verify_post_gc_heap_(false), |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 147 | verify_mod_union_table_(false), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 148 | partial_gc_frequency_(10), |
| 149 | min_alloc_space_size_for_sticky_gc_(4 * MB), |
| 150 | min_remaining_space_for_sticky_gc_(1 * MB), |
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 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 165 | live_bitmap_.reset(new HeapBitmap(this)); |
| 166 | mark_bitmap_.reset(new HeapBitmap(this)); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 167 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 168 | // Requested begin for the alloc space, to follow the mapped image and oat files |
| 169 | byte* requested_begin = NULL; |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 170 | std::string image_file_name(original_image_file_name); |
| 171 | if (!image_file_name.empty()) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 172 | Space* image_space = NULL; |
| 173 | |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 174 | if (OS::FileExists(image_file_name.c_str())) { |
| 175 | // 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] | 176 | image_space = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 177 | } else { |
| 178 | // If the /system file didn't exist, we need to use one from the art-cache. |
| 179 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 180 | // If it does not exist, generate. |
| 181 | image_file_name = GetArtCacheFilenameOrDie(image_file_name); |
| 182 | if (OS::FileExists(image_file_name.c_str())) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 183 | image_space = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 184 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 185 | if (image_space == NULL) { |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 186 | if (!GenerateImage(image_file_name)) { |
| 187 | LOG(FATAL) << "Failed to generate image: " << image_file_name; |
| 188 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 189 | image_space = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 192 | if (image_space == NULL) { |
Brian Carlstrom | 223f20f | 2012-02-04 23:06:55 -0800 | [diff] [blame] | 193 | LOG(FATAL) << "Failed to create space from " << image_file_name; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 194 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 195 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 196 | AddSpace(image_space); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 197 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 198 | // isn't going to get in the middle |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 199 | byte* oat_end_addr = GetImageSpace()->GetImageHeader().GetOatEnd(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 200 | CHECK_GT(oat_end_addr, GetImageSpace()->End()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 201 | if (oat_end_addr > requested_begin) { |
| 202 | requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 203 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 204 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 207 | UniquePtr<AllocSpace> alloc_space(Space::CreateAllocSpace( |
| 208 | "alloc space", initial_size, growth_limit, capacity, requested_begin)); |
| 209 | alloc_space_ = alloc_space.release(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 210 | CHECK(alloc_space_ != NULL) << "Failed to create alloc space"; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 211 | AddSpace(alloc_space_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 212 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 213 | // Spaces are sorted in order of Begin(). |
| 214 | byte* heap_begin = spaces_.front()->Begin(); |
| 215 | size_t heap_capacity = (spaces_.back()->Begin() - spaces_.front()->Begin()) + spaces_.back()->NonGrowthLimitCapacity(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 216 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 217 | // Mark image objects in the live bitmap |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 218 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 219 | Space* space = spaces_[i]; |
| 220 | if (space->IsImageSpace()) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 221 | space->AsImageSpace()->RecordImageAllocations(space->GetLiveBitmap()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 225 | // Allocate the large object space. |
| 226 | large_object_space_.reset(Space::CreateLargeObjectSpace("large object space")); |
| 227 | live_bitmap_->SetLargeObjects(large_object_space_->GetLiveObjects()); |
| 228 | mark_bitmap_->SetLargeObjects(large_object_space_->GetMarkObjects()); |
| 229 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 230 | // Allocate the card table. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 231 | card_table_.reset(CardTable::Create(heap_begin, heap_capacity)); |
| 232 | CHECK(card_table_.get() != NULL) << "Failed to create card table"; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 233 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 234 | mod_union_table_.reset(new ModUnionTableToZygoteAllocspace<ModUnionTableReferenceCache>(this)); |
| 235 | CHECK(mod_union_table_.get() != NULL) << "Failed to create mod-union table"; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 236 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 237 | zygote_mod_union_table_.reset(new ModUnionTableCardCache(this)); |
| 238 | 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] | 239 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 240 | // TODO: Count objects in the image space here. |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 241 | num_bytes_allocated_ = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 242 | num_objects_allocated_ = 0; |
| 243 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 244 | // Max stack size in bytes. |
| 245 | static const size_t max_stack_size = capacity / SpaceBitmap::kAlignment * kWordSize; |
| 246 | |
| 247 | // TODO: Rename MarkStack to a more generic name? |
| 248 | mark_stack_.reset(MarkStack::Create("dalvik-mark-stack", max_stack_size)); |
| 249 | allocation_stack_.reset(MarkStack::Create("dalvik-allocation-stack", max_stack_size)); |
| 250 | live_stack_.reset(MarkStack::Create("dalvik-live-stack", max_stack_size)); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 251 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 252 | // 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] | 253 | // but we can create the heap lock now. We don't create it earlier to |
| 254 | // make it clear that you can't use locks during heap initialization. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 255 | gc_complete_lock_ = new Mutex("GC complete lock"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 256 | gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable")); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 257 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 258 | // Set up the cumulative timing loggers. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 259 | for (size_t i = static_cast<size_t>(kGcTypeSticky); i < static_cast<size_t>(kGcTypeMax); |
| 260 | ++i) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 261 | std::ostringstream name; |
| 262 | name << static_cast<GcType>(i); |
| 263 | cumulative_timings_.Put(static_cast<GcType>(i), |
| 264 | new CumulativeLogger(name.str().c_str(), true)); |
| 265 | } |
| 266 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 267 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 268 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 269 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 272 | // Sort spaces based on begin address |
| 273 | class SpaceSorter { |
| 274 | public: |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 275 | bool operator ()(const Space* a, const Space* b) const { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 276 | return a->Begin() < b->Begin(); |
| 277 | } |
| 278 | }; |
| 279 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 280 | void Heap::AddSpace(Space* space) { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 281 | WriterMutexLock mu(*Locks::heap_bitmap_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 282 | DCHECK(space != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 283 | DCHECK(space->GetLiveBitmap() != NULL); |
| 284 | live_bitmap_->AddSpaceBitmap(space->GetLiveBitmap()); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 285 | DCHECK(space->GetMarkBitmap() != NULL); |
| 286 | mark_bitmap_->AddSpaceBitmap(space->GetMarkBitmap()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 287 | spaces_.push_back(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 288 | if (space->IsAllocSpace()) { |
| 289 | alloc_space_ = space->AsAllocSpace(); |
| 290 | } |
| 291 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 292 | // Ensure that spaces remain sorted in increasing order of start address (required for CMS finger) |
| 293 | std::sort(spaces_.begin(), spaces_.end(), SpaceSorter()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 294 | |
| 295 | // Ensure that ImageSpaces < ZygoteSpaces < AllocSpaces so that we can do address based checks to |
| 296 | // avoid redundant marking. |
| 297 | bool seen_zygote = false, seen_alloc = false; |
| 298 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 299 | Space* space = *it; |
| 300 | if (space->IsImageSpace()) { |
| 301 | DCHECK(!seen_zygote); |
| 302 | DCHECK(!seen_alloc); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 303 | } else if (space->IsZygoteSpace()) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 304 | DCHECK(!seen_alloc); |
| 305 | seen_zygote = true; |
| 306 | } else if (space->IsAllocSpace()) { |
| 307 | seen_alloc = true; |
| 308 | } |
| 309 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | Heap::~Heap() { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 313 | // If we don't reset then the mark stack complains in it's destructor. |
| 314 | allocation_stack_->Reset(); |
| 315 | live_stack_->Reset(); |
| 316 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 317 | VLOG(heap) << "~Heap()"; |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 318 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 319 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 320 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 321 | // 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] | 322 | STLDeleteElements(&spaces_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 323 | delete gc_complete_lock_; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 324 | STLDeleteValues(&cumulative_timings_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 327 | Space* Heap::FindSpaceFromObject(const Object* obj) const { |
| 328 | // TODO: C++0x auto |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 329 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 330 | if ((*it)->Contains(obj)) { |
| 331 | return *it; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | ImageSpace* Heap::GetImageSpace() { |
| 339 | // TODO: C++0x auto |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 340 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 341 | if ((*it)->IsImageSpace()) { |
| 342 | return (*it)->AsImageSpace(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | AllocSpace* Heap::GetAllocSpace() { |
| 349 | return alloc_space_; |
| 350 | } |
| 351 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 352 | static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 353 | size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg); |
| 354 | |
| 355 | size_t chunk_size = static_cast<size_t>(reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start)); |
| 356 | size_t chunk_free_bytes = 0; |
| 357 | if (used_bytes < chunk_size) { |
| 358 | chunk_free_bytes = chunk_size - used_bytes; |
| 359 | } |
| 360 | |
| 361 | if (chunk_free_bytes > max_contiguous_allocation) { |
| 362 | max_contiguous_allocation = chunk_free_bytes; |
| 363 | } |
| 364 | } |
| 365 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 366 | bool Heap::CanAllocateBytes(size_t bytes) const { |
| 367 | return num_bytes_allocated_ + large_object_space_->GetNumBytesAllocated() + bytes <= |
| 368 | alloc_space_->Capacity(); |
| 369 | } |
| 370 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 371 | Object* Heap::AllocObject(Class* c, size_t byte_count) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 372 | DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(Class)) || |
| 373 | (c->IsVariableSize() || c->GetObjectSize() == byte_count) || |
| 374 | strlen(ClassHelper(c).GetDescriptor()) == 0); |
| 375 | DCHECK_GE(byte_count, sizeof(Object)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 376 | |
| 377 | Object* obj = NULL; |
| 378 | size_t size = 0; |
| 379 | |
| 380 | // We need to have a zygote space or else our newly allocated large object can end up in the |
| 381 | // Zygote resulting in it being prematurely freed. |
| 382 | // We can only do this for primive objects since large objects will not be within the card table |
| 383 | // range. This also means that we rely on SetClass not dirtying the object's card. |
| 384 | if (byte_count >= large_object_threshold_ && have_zygote_space_ && c->IsPrimitiveArray()) { |
| 385 | obj = Allocate(NULL, RoundUp(byte_count, kPageSize)); |
| 386 | size = 0; |
| 387 | |
| 388 | if (obj != NULL) { |
| 389 | // Make sure that our large object didn't get placed anywhere within the space interval or else |
| 390 | // it breaks the immune range. |
| 391 | DCHECK(reinterpret_cast<byte*>(obj) < spaces_.front()->Begin() || |
| 392 | reinterpret_cast<byte*>(obj) >= spaces_.back()->End()); |
| 393 | } |
| 394 | } else { |
| 395 | obj = Allocate(alloc_space_, byte_count); |
| 396 | size = alloc_space_->AllocationSize(obj); |
| 397 | |
| 398 | if (obj != NULL) { |
| 399 | // Additional verification to ensure that we did not allocate into a zygote space. |
| 400 | DCHECK(!have_zygote_space_ || !FindSpaceFromObject(obj)->IsZygoteSpace()); |
| 401 | } |
| 402 | } |
| 403 | |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 404 | if (LIKELY(obj != NULL)) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 405 | android_atomic_add(byte_count, reinterpret_cast<volatile int32_t*>( |
| 406 | reinterpret_cast<size_t>(&bytes_since_last_gc_))); |
| 407 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 408 | obj->SetClass(c); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 409 | |
| 410 | // Record allocation after since we want to use the atomic add for the atomic fence to guard |
| 411 | // the SetClass since we do not want the class to appear NULL in another thread. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 412 | RecordAllocation(size, obj); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 413 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 414 | if (Dbg::IsAllocTrackingEnabled()) { |
| 415 | Dbg::RecordAllocation(c, byte_count); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 416 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 417 | if (bytes_since_last_gc_ >= concurrent_gc_start_rate_ || |
| 418 | num_bytes_allocated_ >= concurrent_start_bytes_) { |
| 419 | // We already have a request pending, no reason to start more until we update |
| 420 | // concurrent_start_bytes_. |
| 421 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 422 | bytes_since_last_gc_ = 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 423 | // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint. |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 424 | Thread* self = Thread::Current(); |
| 425 | SirtRef<Object> ref(self, obj); |
| 426 | RequestConcurrentGC(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 427 | } |
| 428 | VerifyObject(obj); |
| 429 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 430 | return obj; |
| 431 | } |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 432 | int64_t total_bytes_free = GetFreeMemory(); |
| 433 | size_t max_contiguous_allocation = 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 434 | // TODO: C++0x auto |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 435 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 436 | if ((*it)->IsAllocSpace()) { |
| 437 | (*it)->AsAllocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 438 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 439 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 440 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 441 | std::string msg(StringPrintf("Failed to allocate a %zd-byte %s (%lld total bytes free; largest possible contiguous allocation %zd bytes)", |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 442 | byte_count, PrettyDescriptor(c).c_str(), total_bytes_free, max_contiguous_allocation)); |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 443 | Thread::Current()->ThrowOutOfMemoryError(msg.c_str()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 444 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 447 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 448 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 449 | // require taking the lock. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 450 | if (obj == NULL) { |
| 451 | return true; |
| 452 | } |
| 453 | if (!IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 454 | return false; |
| 455 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 456 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 457 | if (spaces_[i]->Contains(obj)) { |
| 458 | return true; |
| 459 | } |
| 460 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 461 | // TODO: Find a way to check large object space without using a lock. |
| 462 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 465 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 466 | Locks::heap_bitmap_lock_->AssertReaderHeld(Thread::Current()); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 467 | return IsHeapAddress(obj) && GetLiveBitmap()->Test(obj); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 470 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 471 | void Heap::VerifyObject(const Object* obj) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 472 | if (obj == NULL || this == NULL || !verify_objects_ || Runtime::Current()->IsShuttingDown() || |
Ian Rogers | 141d622 | 2012-04-05 12:23:06 -0700 | [diff] [blame] | 473 | Thread::Current() == NULL || |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 474 | Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 475 | return; |
| 476 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 477 | VerifyObjectBody(obj); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 478 | } |
| 479 | #endif |
| 480 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 481 | void Heap::DumpSpaces() { |
| 482 | // TODO: C++0x auto |
| 483 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 484 | Space* space = *it; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 485 | LOG(INFO) << *space << "\n" |
| 486 | << *space->GetLiveBitmap() << "\n" |
| 487 | << *space->GetMarkBitmap(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 488 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 489 | // TODO: Dump large object space? |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 492 | void Heap::VerifyObjectBody(const Object* obj) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 493 | if (!IsAligned<kObjectAlignment>(obj)) { |
| 494 | LOG(FATAL) << "Object isn't aligned: " << obj; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 497 | if (!GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 498 | // Check the allocation stack / live stack. |
| 499 | if (!std::binary_search(live_stack_->Begin(), live_stack_->End(), obj) && |
| 500 | std::find(allocation_stack_->Begin(), allocation_stack_->End(), obj) == |
| 501 | allocation_stack_->End()) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 502 | ReaderMutexLock mu(*Locks::heap_bitmap_lock_); |
| 503 | if (large_object_space_->GetLiveObjects()->Test(obj)) { |
| 504 | DumpSpaces(); |
| 505 | LOG(FATAL) << "Object is dead: " << obj; |
| 506 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 507 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 508 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 509 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 510 | // Ignore early dawn of the universe verifications |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 511 | if (!VERIFY_OBJECT_FAST && num_objects_allocated_ > 10) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 512 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 513 | Object::ClassOffset().Int32Value(); |
| 514 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 515 | if (c == NULL) { |
| 516 | LOG(FATAL) << "Null class in object: " << obj; |
| 517 | } else if (!IsAligned<kObjectAlignment>(c)) { |
| 518 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 519 | } else if (!GetLiveBitmap()->Test(c)) { |
| 520 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 521 | } |
| 522 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
| 523 | // Note: we don't use the accessors here as they have internal sanity checks |
| 524 | // that we don't want to run |
| 525 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
| 526 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 527 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
| 528 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 529 | CHECK_EQ(c_c, c_c_c); |
| 530 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 533 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 534 | DCHECK(obj != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 535 | reinterpret_cast<Heap*>(arg)->VerifyObjectBody(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void Heap::VerifyHeap() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 539 | ReaderMutexLock mu(*Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 540 | GetLiveBitmap()->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 543 | void Heap::RecordAllocation(size_t size, const Object* obj) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 544 | DCHECK(obj != NULL); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 545 | DCHECK_GT(size, 0u); |
| 546 | COMPILE_ASSERT(sizeof(size_t) == sizeof(int32_t), |
| 547 | int32_t_must_be_same_size_as_size_t_for_used_atomic_operations); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 548 | android_atomic_add(size, reinterpret_cast<volatile int32_t*>( |
| 549 | reinterpret_cast<size_t>(&num_bytes_allocated_))); |
| 550 | android_atomic_add(1, reinterpret_cast<volatile int32_t*>( |
| 551 | reinterpret_cast<size_t>(&num_objects_allocated_))); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 552 | |
| 553 | if (Runtime::Current()->HasStatsEnabled()) { |
| 554 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 555 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 556 | ++global_stats->allocated_objects; |
| 557 | ++thread_stats->allocated_objects; |
| 558 | global_stats->allocated_bytes += size; |
| 559 | thread_stats->allocated_bytes += size; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 560 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 561 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 562 | allocation_stack_->AtomicPush(obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 565 | void Heap::RecordFree(size_t freed_objects, size_t freed_bytes) { |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 566 | COMPILE_ASSERT(sizeof(size_t) == sizeof(int32_t), |
| 567 | 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] | 568 | DCHECK_LE(freed_objects, num_objects_allocated_); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 569 | android_atomic_add(-static_cast<int32_t>(freed_objects), |
Mathieu Chartier | 556fad3 | 2012-08-20 16:13:20 -0700 | [diff] [blame] | 570 | reinterpret_cast<volatile int32_t*>( |
| 571 | reinterpret_cast<size_t>(&num_objects_allocated_))); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 572 | |
| 573 | DCHECK_LE(freed_bytes, num_bytes_allocated_); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 574 | android_atomic_add(-static_cast<int32_t>(freed_bytes), |
Mathieu Chartier | 556fad3 | 2012-08-20 16:13:20 -0700 | [diff] [blame] | 575 | reinterpret_cast<volatile int32_t*>( |
| 576 | reinterpret_cast<size_t>(&num_bytes_allocated_))); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 577 | |
| 578 | if (Runtime::Current()->HasStatsEnabled()) { |
| 579 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 580 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 581 | global_stats->freed_objects += freed_objects; |
| 582 | thread_stats->freed_objects += freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 583 | global_stats->freed_bytes += freed_bytes; |
| 584 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 585 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 588 | Object* Heap::TryToAllocate(AllocSpace* space, size_t alloc_size, bool grow) { |
Mathieu Chartier | 83cf328 | 2012-09-26 17:54:27 -0700 | [diff] [blame] | 589 | if (UNLIKELY(space == NULL)) { |
| 590 | if (CanAllocateBytes(alloc_size)) { |
| 591 | // TODO: This is racy, but is it worth fixing? |
| 592 | return large_object_space_->Alloc(alloc_size); |
| 593 | } else { |
| 594 | // Application ran out of heap space. |
| 595 | return NULL; |
| 596 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 597 | } else if (grow) { |
| 598 | return space->AllocWithGrowth(alloc_size); |
| 599 | } else { |
| 600 | return space->AllocWithoutGrowth(alloc_size); |
| 601 | } |
| 602 | } |
| 603 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 604 | Object* Heap::Allocate(AllocSpace* space, size_t alloc_size) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 605 | // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are |
| 606 | // done in the runnable state where suspension is expected. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 607 | #ifndef NDEBUG |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 608 | Thread* self = Thread::Current(); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 609 | DCHECK_EQ(self->GetState(), kRunnable); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 610 | self->AssertThreadSuspensionIsAllowable(); |
| 611 | #endif |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 612 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 613 | Object* ptr = TryToAllocate(space, alloc_size, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 614 | if (ptr != NULL) { |
| 615 | return ptr; |
| 616 | } |
| 617 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 618 | // The allocation failed. If the GC is running, block until it completes, and then retry the |
| 619 | // allocation. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 620 | #ifdef NDEBUG |
| 621 | Thread* self = Thread::Current(); |
| 622 | #endif |
| 623 | GcType last_gc = WaitForConcurrentGcToComplete(self); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 624 | if (last_gc != kGcTypeNone) { |
| 625 | // A GC was in progress and we blocked, retry allocation now that memory has been freed. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 626 | ptr = TryToAllocate(space, alloc_size, false); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 627 | if (ptr != NULL) { |
| 628 | return ptr; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 632 | // Loop through our different Gc types and try to Gc until we get enough free memory. |
| 633 | for (size_t i = static_cast<size_t>(last_gc) + 1; i < static_cast<size_t>(kGcTypeMax); ++i) { |
| 634 | bool run_gc = false; |
| 635 | GcType gc_type = static_cast<GcType>(i); |
| 636 | switch (gc_type) { |
| 637 | case kGcTypeSticky: { |
| 638 | const size_t alloc_space_size = alloc_space_->Size(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 639 | run_gc = alloc_space_size > min_alloc_space_size_for_sticky_gc_ && |
| 640 | alloc_space_->Capacity() - alloc_space_size >= min_remaining_space_for_sticky_gc_; |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 641 | break; |
| 642 | } |
| 643 | case kGcTypePartial: |
| 644 | run_gc = have_zygote_space_; |
| 645 | break; |
| 646 | case kGcTypeFull: |
| 647 | run_gc = true; |
| 648 | break; |
| 649 | default: |
| 650 | break; |
| 651 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 652 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 653 | if (run_gc) { |
| 654 | if (Runtime::Current()->HasStatsEnabled()) { |
| 655 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 656 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 657 | } |
| 658 | self->TransitionFromRunnableToSuspended(kWaitingPerformingGc); |
| 659 | |
| 660 | // If we actually ran a different type of Gc than requested, we can skip the index forwards. |
| 661 | GcType gc_type_ran = CollectGarbageInternal(gc_type, false); |
| 662 | DCHECK(static_cast<size_t>(gc_type_ran) >= i); |
| 663 | i = static_cast<size_t>(gc_type_ran); |
| 664 | self->TransitionFromSuspendedToRunnable(); |
| 665 | |
| 666 | // Did we free sufficient memory for the allocation to succeed? |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 667 | ptr = TryToAllocate(space, alloc_size, false); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 668 | if (ptr != NULL) { |
| 669 | return ptr; |
| 670 | } |
| 671 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // Allocations have failed after GCs; this is an exceptional state. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 675 | // Try harder, growing the heap if necessary. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 676 | ptr = TryToAllocate(space, alloc_size, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 677 | if (ptr != NULL) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 678 | if (space != NULL) { |
| 679 | size_t new_footprint = space->GetFootprintLimit(); |
| 680 | // OLD-TODO: may want to grow a little bit more so that the amount of |
| 681 | // free space is equal to the old free space + the |
| 682 | // utilization slop for the new allocation. |
| 683 | VLOG(gc) << "Grow alloc space (frag case) to " << PrettySize(new_footprint) |
| 684 | << " for a " << PrettySize(alloc_size) << " allocation"; |
| 685 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 686 | return ptr; |
| 687 | } |
| 688 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 689 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 690 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 691 | // 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] | 692 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 693 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 694 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) |
| 695 | << " allocation"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 696 | |
| 697 | if (Runtime::Current()->HasStatsEnabled()) { |
| 698 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 699 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 700 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 701 | // We don't need a WaitForConcurrentGcToComplete here either. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 702 | self->TransitionFromRunnableToSuspended(kWaitingPerformingGc); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 703 | CollectGarbageInternal(kGcTypeFull, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 704 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 705 | return TryToAllocate(space, alloc_size, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 708 | int64_t Heap::GetMaxMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 709 | size_t total = 0; |
| 710 | // TODO: C++0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 711 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 712 | Space* space = *it; |
| 713 | if (space->IsAllocSpace()) { |
| 714 | total += space->AsAllocSpace()->Capacity(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | return total; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | int64_t Heap::GetTotalMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 721 | return GetMaxMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | int64_t Heap::GetFreeMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 725 | return GetMaxMemory() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 728 | class InstanceCounter { |
| 729 | public: |
| 730 | InstanceCounter(Class* c, bool count_assignable) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 731 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 732 | : class_(c), count_assignable_(count_assignable), count_(0) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 733 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | size_t GetCount() { |
| 737 | return count_; |
| 738 | } |
| 739 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 740 | static void Callback(Object* o, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 741 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 742 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 743 | } |
| 744 | |
| 745 | private: |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 746 | void VisitInstance(Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 747 | Class* instance_class = o->GetClass(); |
| 748 | if (count_assignable_) { |
| 749 | if (instance_class == class_) { |
| 750 | ++count_; |
| 751 | } |
| 752 | } else { |
| 753 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 754 | ++count_; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | Class* class_; |
| 760 | bool count_assignable_; |
| 761 | size_t count_; |
| 762 | }; |
| 763 | |
| 764 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 765 | ReaderMutexLock mu(*Locks::heap_bitmap_lock_); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 766 | InstanceCounter counter(c, count_assignable); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 767 | GetLiveBitmap()->Walk(InstanceCounter::Callback, &counter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 768 | return counter.GetCount(); |
| 769 | } |
| 770 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 771 | void Heap::CollectGarbage(bool clear_soft_references) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 772 | // Even if we waited for a GC we still need to do another GC since weaks allocated during the |
| 773 | // last GC will not have necessarily been cleared. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 774 | Thread* self = Thread::Current(); |
| 775 | WaitForConcurrentGcToComplete(self); |
| 776 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 777 | CollectGarbageInternal(have_zygote_space_ ? kGcTypePartial : kGcTypeFull, clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 780 | void Heap::PreZygoteFork() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 781 | static Mutex zygote_creation_lock_("zygote creation lock", kZygoteCreationLock); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 782 | Thread* self = Thread::Current(); |
| 783 | MutexLock mu(self, zygote_creation_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 784 | |
| 785 | // Try to see if we have any Zygote spaces. |
| 786 | if (have_zygote_space_) { |
| 787 | return; |
| 788 | } |
| 789 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 790 | VLOG(heap) << "Starting PreZygoteFork with alloc space size " << PrettySize(alloc_space_->Size()); |
| 791 | |
| 792 | { |
| 793 | // Flush the alloc stack. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 794 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 795 | FlushAllocStack(); |
| 796 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 797 | |
| 798 | // Replace the first alloc space we find with a zygote space. |
| 799 | // TODO: C++0x auto |
| 800 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 801 | if ((*it)->IsAllocSpace()) { |
| 802 | AllocSpace* zygote_space = (*it)->AsAllocSpace(); |
| 803 | |
| 804 | // Turns the current alloc space into a Zygote space and obtain the new alloc space composed |
| 805 | // of the remaining available heap memory. |
| 806 | alloc_space_ = zygote_space->CreateZygoteSpace(); |
| 807 | |
| 808 | // Change the GC retention policy of the zygote space to only collect when full. |
| 809 | zygote_space->SetGcRetentionPolicy(GCRP_FULL_COLLECT); |
| 810 | AddSpace(alloc_space_); |
| 811 | have_zygote_space_ = true; |
| 812 | break; |
| 813 | } |
| 814 | } |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 815 | |
Ian Rogers | 5f5a2c0 | 2012-09-17 10:52:08 -0700 | [diff] [blame] | 816 | // Reset the cumulative loggers since we now have a few additional timing phases. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 817 | // TODO: C++0x |
| 818 | for (CumulativeTimings::iterator it = cumulative_timings_.begin(); |
| 819 | it != cumulative_timings_.end(); ++it) { |
| 820 | it->second->Reset(); |
| 821 | } |
| 822 | |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 823 | // Reset this since we now count the ZygoteSpace in the total heap size. |
| 824 | num_bytes_allocated_ = 0; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 827 | void Heap::FlushAllocStack() { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 828 | MarkAllocStack(alloc_space_->GetLiveBitmap(), large_object_space_->GetLiveObjects(), |
| 829 | allocation_stack_.get()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 830 | allocation_stack_->Reset(); |
| 831 | } |
| 832 | |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 833 | size_t Heap::GetUsedMemorySize() const { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 834 | size_t total = num_bytes_allocated_ + large_object_space_->GetNumBytesAllocated(); |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 835 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 836 | if ((*it)->IsZygoteSpace()) { |
| 837 | total += (*it)->AsAllocSpace()->Size(); |
| 838 | } |
| 839 | } |
| 840 | return total; |
| 841 | } |
| 842 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 843 | void Heap::MarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, MarkStack* stack) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 844 | const size_t count = stack->Size(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 845 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 846 | const Object* obj = stack->Get(i); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 847 | DCHECK(obj != NULL); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 848 | if (LIKELY(bitmap->HasAddress(obj))) { |
| 849 | bitmap->Set(obj); |
| 850 | } else { |
| 851 | large_objects->Set(obj); |
| 852 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 856 | void Heap::UnMarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, MarkStack* stack) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 857 | size_t count = stack->Size(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 858 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 859 | const Object* obj = stack->Get(i); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 860 | DCHECK(obj != NULL); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 861 | if (LIKELY(bitmap->HasAddress(obj))) { |
| 862 | bitmap->Clear(obj); |
| 863 | } else { |
| 864 | large_objects->Clear(obj); |
| 865 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 869 | GcType Heap::CollectGarbageInternal(GcType gc_type, bool clear_soft_references) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 870 | Thread* self = Thread::Current(); |
| 871 | Locks::mutator_lock_->AssertNotHeld(self); |
| 872 | DCHECK_EQ(self->GetState(), kWaitingPerformingGc); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 873 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 874 | if (self->IsHandlingStackOverflow()) { |
| 875 | LOG(WARNING) << "Performing GC on a thread that is handling a stack overflow."; |
| 876 | } |
| 877 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 878 | // Ensure there is only one GC at a time. |
| 879 | bool start_collect = false; |
| 880 | while (!start_collect) { |
| 881 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 882 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 883 | if (!is_gc_running_) { |
| 884 | is_gc_running_ = true; |
| 885 | start_collect = true; |
| 886 | } |
| 887 | } |
| 888 | if (!start_collect) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 889 | WaitForConcurrentGcToComplete(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 890 | // TODO: if another thread beat this one to do the GC, perhaps we should just return here? |
| 891 | // Not doing at the moment to ensure soft references are cleared. |
| 892 | } |
| 893 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 894 | gc_complete_lock_->AssertNotHeld(self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 895 | |
| 896 | // We need to do partial GCs every now and then to avoid the heap growing too much and |
| 897 | // fragmenting. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 898 | if (gc_type == kGcTypeSticky && ++sticky_gc_count_ > partial_gc_frequency_) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 899 | gc_type = kGcTypePartial; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 900 | } |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 901 | if (gc_type != kGcTypeSticky) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 902 | sticky_gc_count_ = 0; |
| 903 | } |
| 904 | |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 905 | if (concurrent_gc_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 906 | CollectGarbageConcurrentMarkSweepPlan(self, gc_type, clear_soft_references); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 907 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 908 | CollectGarbageMarkSweepPlan(self, gc_type, clear_soft_references); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 909 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 910 | bytes_since_last_gc_ = 0; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 911 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 912 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 913 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 914 | is_gc_running_ = false; |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 915 | last_gc_type_ = gc_type; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 916 | // Wake anyone who may have been waiting for the GC to complete. |
| 917 | gc_complete_cond_->Broadcast(); |
| 918 | } |
| 919 | // Inform DDMS that a GC completed. |
| 920 | Dbg::GcDidFinish(); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 921 | return gc_type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 922 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 923 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 924 | void Heap::CollectGarbageMarkSweepPlan(Thread* self, GcType gc_type, bool clear_soft_references) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 925 | TimingLogger timings("CollectGarbageInternal", true); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 926 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 927 | std::stringstream gc_type_str; |
| 928 | gc_type_str << gc_type << " "; |
| 929 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 930 | // Suspend all threads are get exclusive access to the heap. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 931 | uint64_t start_time = NanoTime(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 932 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 933 | thread_list->SuspendAll(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 934 | timings.AddSplit("SuspendAll"); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 935 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 936 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 937 | size_t bytes_freed = 0; |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 938 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 939 | { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 940 | MarkSweep mark_sweep(mark_stack_.get()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 941 | |
| 942 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 943 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 944 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 945 | if (verify_pre_gc_heap_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 946 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 947 | if (!VerifyHeapReferences()) { |
| 948 | LOG(FATAL) << "Pre " << gc_type_str.str() << "Gc verification failed"; |
| 949 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 950 | timings.AddSplit("VerifyHeapReferencesPreGC"); |
| 951 | } |
| 952 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 953 | // Make sure that the tables have the correct pointer for the mark sweep. |
| 954 | mod_union_table_->Init(&mark_sweep); |
| 955 | zygote_mod_union_table_->Init(&mark_sweep); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 956 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 957 | // Swap allocation stack and live stack, enabling us to have new allocations during this GC. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 958 | SwapStacks(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 959 | |
| 960 | // We will need to know which cards were dirty for doing concurrent processing of dirty cards. |
| 961 | // TODO: Investigate using a mark stack instead of a vector. |
| 962 | std::vector<byte*> dirty_cards; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 963 | if (gc_type == kGcTypeSticky) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 964 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 965 | card_table_->GetDirtyCards(*it, dirty_cards); |
| 966 | } |
| 967 | } |
| 968 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 969 | // 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] | 970 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 971 | Space* space = *it; |
| 972 | if (space->IsImageSpace()) { |
| 973 | mod_union_table_->ClearCards(*it); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 974 | timings.AddSplit("ClearModUnionCards"); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 975 | } else if (space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 976 | zygote_mod_union_table_->ClearCards(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 977 | timings.AddSplit("ClearZygoteCards"); |
| 978 | } else { |
| 979 | card_table_->ClearSpaceCards(space); |
| 980 | timings.AddSplit("ClearCards"); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 983 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 984 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 985 | if (gc_type == kGcTypePartial) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 986 | // Copy the mark bits over from the live bits, do this as early as possible or else we can |
| 987 | // accidentally un-mark roots. |
| 988 | // Needed for scanning dirty objects. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 989 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 990 | if ((*it)->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 991 | mark_sweep.CopyMarkBits(*it); |
| 992 | } |
| 993 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 994 | timings.AddSplit("CopyMarkBits"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 995 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 996 | // We can assume that everything from the start of the first space to the alloc space is marked. |
| 997 | mark_sweep.SetImmuneRange(reinterpret_cast<Object*>(spaces_[0]->Begin()), |
| 998 | reinterpret_cast<Object*>(alloc_space_->Begin())); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 999 | } else if (gc_type == kGcTypeSticky) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1000 | for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1001 | if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) { |
| 1002 | mark_sweep.CopyMarkBits(*it); |
| 1003 | } |
| 1004 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1005 | large_object_space_->CopyLiveToMarked(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1006 | timings.AddSplit("CopyMarkBits"); |
| 1007 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1008 | mark_sweep.SetImmuneRange(reinterpret_cast<Object*>(spaces_[0]->Begin()), |
| 1009 | reinterpret_cast<Object*>(alloc_space_->Begin())); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1010 | } |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1011 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1012 | MarkAllocStack(alloc_space_->GetLiveBitmap(), large_object_space_->GetLiveObjects(), |
| 1013 | live_stack_.get()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1014 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1015 | if (gc_type != kGcTypeSticky) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1016 | live_stack_->Reset(); |
| 1017 | } |
| 1018 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1019 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1020 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1021 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1022 | // Roots are marked on the bitmap and the mark_stack is empty. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1023 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1024 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1025 | UpdateAndMarkModUnion(timings, gc_type); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1026 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1027 | if (verify_mod_union_table_) { |
| 1028 | zygote_mod_union_table_->Update(); |
| 1029 | zygote_mod_union_table_->Verify(); |
| 1030 | mod_union_table_->Update(); |
| 1031 | mod_union_table_->Verify(); |
| 1032 | } |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1033 | |
| 1034 | // Recursively mark all the non-image bits set in the mark bitmap. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1035 | if (gc_type != kGcTypeSticky) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1036 | live_stack_->Reset(); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1037 | mark_sweep.RecursiveMark(gc_type == kGcTypePartial, timings); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1038 | } else { |
| 1039 | mark_sweep.RecursiveMarkCards(card_table_.get(), dirty_cards, timings); |
| 1040 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1041 | mark_sweep.DisableFinger(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1042 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1043 | // Need to process references before the swap since it uses IsMarked. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1044 | mark_sweep.ProcessReferences(clear_soft_references); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1045 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1046 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1047 | // This doesn't work with mutators unpaused for some reason, TODO: Fix. |
| 1048 | mark_sweep.SweepSystemWeaks(false); |
| 1049 | timings.AddSplit("SweepSystemWeaks"); |
| 1050 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1051 | const bool swap = true; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1052 | if (swap) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1053 | SwapBitmaps(self); |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 1054 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1055 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1056 | #ifndef NDEBUG |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1057 | // Verify that we only reach marked objects from the image space |
| 1058 | mark_sweep.VerifyImageRoots(); |
| 1059 | timings.AddSplit("VerifyImageRoots"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1060 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1061 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1062 | if (gc_type != kGcTypeSticky) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1063 | mark_sweep.SweepLargeObjects(swap); |
| 1064 | timings.AddSplit("SweepLargeObjects"); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1065 | mark_sweep.Sweep(gc_type == kGcTypePartial, swap); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1066 | } else { |
| 1067 | mark_sweep.SweepArray(timings, live_stack_.get(), swap); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1068 | timings.AddSplit("SweepArray"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1069 | } |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1070 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1071 | if (verify_system_weaks_) { |
| 1072 | mark_sweep.VerifySystemWeaks(); |
| 1073 | timings.AddSplit("VerifySystemWeaks"); |
| 1074 | } |
| 1075 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1076 | cleared_references = mark_sweep.GetClearedReferences(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1077 | bytes_freed = mark_sweep.GetFreedBytes(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1080 | if (verify_post_gc_heap_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1081 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1082 | if (!VerifyHeapReferences()) { |
| 1083 | LOG(FATAL) << "Post " + gc_type_str.str() + "Gc verification failed"; |
| 1084 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1085 | timings.AddSplit("VerifyHeapReferencesPostGC"); |
| 1086 | } |
| 1087 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1088 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1089 | timings.AddSplit("GrowForUtilization"); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1090 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1091 | thread_list->ResumeAll(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1092 | timings.AddSplit("ResumeAll"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1093 | |
| 1094 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1095 | RequestHeapTrim(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 1096 | timings.AddSplit("Finish"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 1097 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1098 | // If the GC was slow, then print timings in the log. |
| 1099 | uint64_t duration = (NanoTime() - start_time) / 1000 * 1000; |
| 1100 | if (duration > MsToNs(50)) { |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1101 | const size_t percent_free = GetPercentFree(); |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1102 | const size_t current_heap_size = GetUsedMemorySize(); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1103 | const size_t total_memory = GetTotalMemory(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1104 | LOG(INFO) << gc_type_str.str() << " " |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1105 | << "GC freed " << PrettySize(bytes_freed) << ", " << percent_free << "% free, " |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1106 | << PrettySize(current_heap_size) << "/" << PrettySize(total_memory) << ", " |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1107 | << "paused " << PrettyDuration(duration); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1108 | if (VLOG_IS_ON(heap)) { |
| 1109 | timings.Dump(); |
| 1110 | } |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1111 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1112 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1113 | CumulativeLogger* logger = cumulative_timings_.Get(gc_type); |
| 1114 | logger->Start(); |
| 1115 | logger->AddLogger(timings); |
| 1116 | logger->End(); // Next iteration. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1117 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1118 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1119 | void Heap::UpdateAndMarkModUnion(TimingLogger& timings, GcType gc_type) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1120 | if (gc_type == kGcTypeSticky) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1121 | // Don't need to do anythign for mod union table in this case since we are only scanning dirty |
| 1122 | // cards. |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | // Update zygote mod union table. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1127 | if (gc_type == kGcTypePartial) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1128 | zygote_mod_union_table_->Update(); |
| 1129 | timings.AddSplit("UpdateZygoteModUnionTable"); |
| 1130 | |
| 1131 | zygote_mod_union_table_->MarkReferences(); |
| 1132 | timings.AddSplit("ZygoteMarkReferences"); |
| 1133 | } |
| 1134 | |
| 1135 | // Processes the cards we cleared earlier and adds their objects into the mod-union table. |
| 1136 | mod_union_table_->Update(); |
| 1137 | timings.AddSplit("UpdateModUnionTable"); |
| 1138 | |
| 1139 | // Scans all objects in the mod-union table. |
| 1140 | mod_union_table_->MarkReferences(); |
| 1141 | timings.AddSplit("MarkImageToAllocSpaceReferences"); |
| 1142 | } |
| 1143 | |
| 1144 | void Heap::RootMatchesObjectVisitor(const Object* root, void* arg) { |
| 1145 | Object* obj = reinterpret_cast<Object*>(arg); |
| 1146 | if (root == obj) { |
| 1147 | LOG(INFO) << "Object " << obj << " is a root"; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | class ScanVisitor { |
| 1152 | public: |
| 1153 | void operator ()(const Object* obj) const { |
| 1154 | LOG(INFO) << "Would have rescanned object " << obj; |
| 1155 | } |
| 1156 | }; |
| 1157 | |
| 1158 | class VerifyReferenceVisitor { |
| 1159 | public: |
| 1160 | VerifyReferenceVisitor(Heap* heap, bool* failed) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1161 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 1162 | Locks::heap_bitmap_lock_) |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1163 | : heap_(heap), |
| 1164 | failed_(failed) { |
| 1165 | } |
| 1166 | |
| 1167 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for smarter |
| 1168 | // analysis. |
| 1169 | void operator ()(const Object* obj, const Object* ref, const MemberOffset& /* offset */, |
| 1170 | bool /* is_static */) const NO_THREAD_SAFETY_ANALYSIS { |
| 1171 | // Verify that the reference is live. |
| 1172 | if (ref != NULL && !IsLive(ref)) { |
| 1173 | CardTable* card_table = heap_->GetCardTable(); |
| 1174 | MarkStack* alloc_stack = heap_->allocation_stack_.get(); |
| 1175 | MarkStack* live_stack = heap_->live_stack_.get(); |
| 1176 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1177 | byte* card_addr = card_table->CardFromAddr(obj); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1178 | LOG(ERROR) << "Object " << obj << " references dead object " << ref << " on IsDirty = " |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1179 | << (*card_addr == GC_CARD_DIRTY); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1180 | LOG(ERROR) << "Obj type " << PrettyTypeOf(obj); |
| 1181 | LOG(ERROR) << "Ref type " << PrettyTypeOf(ref); |
| 1182 | card_table->CheckAddrIsInCardTable(reinterpret_cast<const byte*>(obj)); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1183 | void* cover_begin = card_table->AddrFromCard(card_addr); |
| 1184 | void* cover_end = reinterpret_cast<void*>(reinterpret_cast<size_t>(cover_begin) + |
| 1185 | GC_CARD_SIZE); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1186 | LOG(ERROR) << "Card " << reinterpret_cast<void*>(card_addr) << " covers " << cover_begin |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1187 | << "-" << cover_end; |
| 1188 | SpaceBitmap* bitmap = heap_->GetLiveBitmap()->GetSpaceBitmap(obj); |
| 1189 | |
| 1190 | // Print out how the object is live. |
| 1191 | if (bitmap->Test(obj)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1192 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 1193 | } |
| 1194 | |
| 1195 | if (std::binary_search(alloc_stack->Begin(), alloc_stack->End(), obj)) { |
| 1196 | LOG(ERROR) << "Object " << obj << " found in allocation stack"; |
| 1197 | } |
| 1198 | |
| 1199 | if (std::binary_search(live_stack->Begin(), live_stack->End(), obj)) { |
| 1200 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | if (std::binary_search(live_stack->Begin(), live_stack->End(), ref)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1204 | LOG(ERROR) << "Reference " << ref << " found in live stack!"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | // Attempt to see if the card table missed the reference. |
| 1208 | ScanVisitor scan_visitor; |
| 1209 | byte* byte_cover_begin = reinterpret_cast<byte*>(card_table->AddrFromCard(card_addr)); |
| 1210 | card_table->Scan(bitmap, byte_cover_begin, byte_cover_begin + GC_CARD_SIZE, scan_visitor, |
| 1211 | IdentityFunctor()); |
| 1212 | |
| 1213 | // Try and see if a mark sweep collector scans the reference. |
| 1214 | MarkStack* mark_stack = heap_->mark_stack_.get(); |
| 1215 | MarkSweep ms(mark_stack); |
| 1216 | ms.Init(); |
| 1217 | mark_stack->Reset(); |
| 1218 | ms.SetFinger(reinterpret_cast<Object*>(~size_t(0))); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1219 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1220 | // All the references should end up in the mark stack. |
| 1221 | ms.ScanRoot(obj); |
| 1222 | if (std::find(mark_stack->Begin(), mark_stack->End(), ref)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1223 | LOG(ERROR) << "Ref found in the mark_stack when rescanning the object!"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1224 | } else { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1225 | LOG(ERROR) << "Dumping mark stack contents"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1226 | for (Object** it = mark_stack->Begin(); it != mark_stack->End(); ++it) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1227 | LOG(ERROR) << *it; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | mark_stack->Reset(); |
| 1231 | |
| 1232 | // Search to see if any of the roots reference our object. |
| 1233 | void* arg = const_cast<void*>(reinterpret_cast<const void*>(obj)); |
| 1234 | Runtime::Current()->VisitRoots(&Heap::RootMatchesObjectVisitor, arg); |
| 1235 | *failed_ = true; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | bool IsLive(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
| 1240 | SpaceBitmap* bitmap = heap_->GetLiveBitmap()->GetSpaceBitmap(obj); |
| 1241 | if (bitmap != NULL) { |
| 1242 | if (bitmap->Test(obj)) { |
| 1243 | return true; |
| 1244 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1245 | } else if (heap_->GetLargeObjectsSpace()->GetLiveObjects()->Test(obj)) { |
| 1246 | return true; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1247 | } else { |
| 1248 | heap_->DumpSpaces(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1249 | LOG(ERROR) << "Object " << obj << " not found in any spaces"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1250 | } |
| 1251 | MarkStack* alloc_stack = heap_->allocation_stack_.get(); |
| 1252 | // At this point we need to search the allocation since things in the live stack may get swept. |
| 1253 | if (std::binary_search(alloc_stack->Begin(), alloc_stack->End(), const_cast<Object*>(obj))) { |
| 1254 | return true; |
| 1255 | } |
| 1256 | // Not either in the live bitmap or allocation stack, so the object must be dead. |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
| 1260 | private: |
| 1261 | Heap* heap_; |
| 1262 | bool* failed_; |
| 1263 | }; |
| 1264 | |
| 1265 | class VerifyObjectVisitor { |
| 1266 | public: |
| 1267 | VerifyObjectVisitor(Heap* heap) |
| 1268 | : heap_(heap), |
| 1269 | failed_(false) { |
| 1270 | |
| 1271 | } |
| 1272 | |
| 1273 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1274 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1275 | VerifyReferenceVisitor visitor(heap_, const_cast<bool*>(&failed_)); |
| 1276 | MarkSweep::VisitObjectReferences(obj, visitor); |
| 1277 | } |
| 1278 | |
| 1279 | bool Failed() const { |
| 1280 | return failed_; |
| 1281 | } |
| 1282 | |
| 1283 | private: |
| 1284 | Heap* heap_; |
| 1285 | bool failed_; |
| 1286 | }; |
| 1287 | |
| 1288 | // Must do this with mutators suspended since we are directly accessing the allocation stacks. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1289 | bool Heap::VerifyHeapReferences() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1290 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1291 | // Lets sort our allocation stacks so that we can efficiently binary search them. |
| 1292 | std::sort(allocation_stack_->Begin(), allocation_stack_->End()); |
| 1293 | std::sort(live_stack_->Begin(), live_stack_->End()); |
| 1294 | // Perform the verification. |
| 1295 | VerifyObjectVisitor visitor(this); |
| 1296 | GetLiveBitmap()->Visit(visitor); |
| 1297 | // We don't want to verify the objects in the allocation stack since they themselves may be |
| 1298 | // pointing to dead objects if they are not reachable. |
| 1299 | if (visitor.Failed()) { |
| 1300 | DumpSpaces(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1301 | return false; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1302 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1303 | return true; |
| 1304 | } |
| 1305 | |
| 1306 | class VerifyReferenceCardVisitor { |
| 1307 | public: |
| 1308 | VerifyReferenceCardVisitor(Heap* heap, bool* failed) |
| 1309 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 1310 | Locks::heap_bitmap_lock_) |
| 1311 | : heap_(heap), |
| 1312 | failed_(failed) { |
| 1313 | } |
| 1314 | |
| 1315 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for smarter |
| 1316 | // analysis. |
| 1317 | void operator ()(const Object* obj, const Object* ref, const MemberOffset& offset, |
| 1318 | bool is_static) const NO_THREAD_SAFETY_ANALYSIS { |
| 1319 | if (ref != NULL) { |
| 1320 | CardTable* card_table = heap_->GetCardTable(); |
| 1321 | // If the object is not dirty and it is referencing something in the live stack other than |
| 1322 | // class, then it must be on a dirty card. |
| 1323 | if (!card_table->IsDirty(obj)) { |
| 1324 | MarkStack* live_stack = heap_->live_stack_.get(); |
| 1325 | if (std::binary_search(live_stack->Begin(), live_stack->End(), ref) && !ref->IsClass()) { |
| 1326 | if (std::binary_search(live_stack->Begin(), live_stack->End(), obj)) { |
| 1327 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 1328 | } |
| 1329 | if (heap_->GetLiveBitmap()->Test(obj)) { |
| 1330 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 1331 | } |
| 1332 | LOG(ERROR) << "Object " << obj << " " << PrettyTypeOf(obj) |
| 1333 | << " references " << ref << " " << PrettyTypeOf(ref) << " in live stack"; |
| 1334 | |
| 1335 | // Print which field of the object is dead. |
| 1336 | if (!obj->IsObjectArray()) { |
| 1337 | const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 1338 | CHECK(klass != NULL); |
| 1339 | const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 1340 | CHECK(fields != NULL); |
| 1341 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
| 1342 | const Field* cur = fields->Get(i); |
| 1343 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 1344 | LOG(ERROR) << (is_static ? "Static " : "") << "field in the live stack is " |
| 1345 | << PrettyField(cur); |
| 1346 | break; |
| 1347 | } |
| 1348 | } |
| 1349 | } else { |
| 1350 | const ObjectArray<Object>* object_array = obj->AsObjectArray<Object>(); |
| 1351 | for (int32_t i = 0; i < object_array->GetLength(); ++i) { |
| 1352 | if (object_array->Get(i) == ref) { |
| 1353 | LOG(ERROR) << (is_static ? "Static " : "") << "obj[" << i << "] = ref"; |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | *failed_ = true; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | private: |
| 1365 | Heap* heap_; |
| 1366 | bool* failed_; |
| 1367 | }; |
| 1368 | |
| 1369 | class VerifyLiveStackReferences { |
| 1370 | public: |
| 1371 | VerifyLiveStackReferences(Heap* heap) |
| 1372 | : heap_(heap), |
| 1373 | failed_(false) { |
| 1374 | |
| 1375 | } |
| 1376 | |
| 1377 | void operator ()(const Object* obj) const |
| 1378 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1379 | VerifyReferenceCardVisitor visitor(heap_, const_cast<bool*>(&failed_)); |
| 1380 | MarkSweep::VisitObjectReferences(obj, visitor); |
| 1381 | } |
| 1382 | |
| 1383 | bool Failed() const { |
| 1384 | return failed_; |
| 1385 | } |
| 1386 | |
| 1387 | private: |
| 1388 | Heap* heap_; |
| 1389 | bool failed_; |
| 1390 | }; |
| 1391 | |
| 1392 | bool Heap::VerifyMissingCardMarks() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1393 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1394 | |
| 1395 | VerifyLiveStackReferences visitor(this); |
| 1396 | GetLiveBitmap()->Visit(visitor); |
| 1397 | |
| 1398 | // We can verify objects in the live stack since none of these should reference dead objects. |
| 1399 | for (Object** it = live_stack_->Begin(); it != live_stack_->End(); ++it) { |
| 1400 | visitor(*it); |
| 1401 | } |
| 1402 | |
| 1403 | if (visitor.Failed()) { |
| 1404 | DumpSpaces(); |
| 1405 | return false; |
| 1406 | } |
| 1407 | return true; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1410 | void Heap::SwapBitmaps(Thread* self) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1411 | // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps |
| 1412 | // these bitmaps. Doing this enables us to sweep with the heap unlocked since new allocations |
| 1413 | // set the live bit, but since we have the bitmaps reversed at this point, this sets the mark bit |
| 1414 | // instead, resulting in no new allocated objects being incorrectly freed by sweep. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1415 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1416 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1417 | Space* space = *it; |
| 1418 | // We never allocate into zygote spaces. |
| 1419 | if (space->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) { |
| 1420 | live_bitmap_->ReplaceBitmap(space->GetLiveBitmap(), space->GetMarkBitmap()); |
| 1421 | mark_bitmap_->ReplaceBitmap(space->GetMarkBitmap(), space->GetLiveBitmap()); |
| 1422 | space->AsAllocSpace()->SwapBitmaps(); |
| 1423 | } |
| 1424 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1425 | |
| 1426 | large_object_space_->SwapBitmaps(); |
| 1427 | live_bitmap_->SetLargeObjects(large_object_space_->GetLiveObjects()); |
| 1428 | mark_bitmap_->SetLargeObjects(large_object_space_->GetMarkObjects()); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1431 | void Heap::SwapStacks() { |
| 1432 | MarkStack* temp = allocation_stack_.release(); |
| 1433 | allocation_stack_.reset(live_stack_.release()); |
| 1434 | live_stack_.reset(temp); |
| 1435 | |
| 1436 | // Sort the live stack so that we can quickly binary search it later. |
| 1437 | if (VERIFY_OBJECT_ENABLED) { |
| 1438 | std::sort(live_stack_->Begin(), live_stack_->End()); |
| 1439 | } |
| 1440 | } |
| 1441 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1442 | void Heap::CollectGarbageConcurrentMarkSweepPlan(Thread* self, GcType gc_type, bool clear_soft_references) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1443 | TimingLogger timings("ConcurrentCollectGarbageInternal", true); |
| 1444 | uint64_t root_begin = NanoTime(), root_end = 0, dirty_begin = 0, dirty_end = 0; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1445 | std::stringstream gc_type_str; |
| 1446 | gc_type_str << gc_type << " "; |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1447 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1448 | // Suspend all threads are get exclusive access to the heap. |
| 1449 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1450 | thread_list->SuspendAll(); |
| 1451 | timings.AddSplit("SuspendAll"); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1452 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1453 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1454 | size_t bytes_freed = 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1455 | Object* cleared_references = NULL; |
| 1456 | { |
| 1457 | MarkSweep mark_sweep(mark_stack_.get()); |
| 1458 | timings.AddSplit("ctor"); |
| 1459 | |
| 1460 | mark_sweep.Init(); |
| 1461 | timings.AddSplit("Init"); |
| 1462 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1463 | if (verify_pre_gc_heap_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1464 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1465 | if (!VerifyHeapReferences()) { |
| 1466 | LOG(FATAL) << "Pre " << gc_type_str.str() << "Gc verification failed"; |
| 1467 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1468 | timings.AddSplit("VerifyHeapReferencesPreGC"); |
| 1469 | } |
| 1470 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1471 | // Swap the stacks, this is safe since all the mutators are suspended at this point. |
| 1472 | SwapStacks(); |
| 1473 | |
| 1474 | // Check that all objects which reference things in the live stack are on dirty cards. |
| 1475 | if (verify_missing_card_marks_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1476 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1477 | // Sort the live stack so that we can quickly binary search it later. |
| 1478 | std::sort(live_stack_->Begin(), live_stack_->End()); |
| 1479 | if (!VerifyMissingCardMarks()) { |
| 1480 | LOG(FATAL) << "Pre GC verification of missing card marks failed"; |
| 1481 | } |
| 1482 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1483 | |
| 1484 | // We will need to know which cards were dirty for doing concurrent processing of dirty cards. |
| 1485 | // TODO: Investigate using a mark stack instead of a vector. |
| 1486 | std::vector<byte*> dirty_cards; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1487 | if (gc_type == kGcTypeSticky) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1488 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1489 | card_table_->GetDirtyCards(*it, dirty_cards); |
| 1490 | } |
| 1491 | } |
| 1492 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1493 | // Make sure that the tables have the correct pointer for the mark sweep. |
| 1494 | mod_union_table_->Init(&mark_sweep); |
| 1495 | zygote_mod_union_table_->Init(&mark_sweep); |
| 1496 | |
| 1497 | // Clear image space cards and keep track of cards we cleared in the mod-union table. |
| 1498 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1499 | Space* space = *it; |
| 1500 | if (space->IsImageSpace()) { |
| 1501 | mod_union_table_->ClearCards(*it); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1502 | timings.AddSplit("ModUnionClearCards"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1503 | } else if (space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 1504 | zygote_mod_union_table_->ClearCards(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1505 | timings.AddSplit("ZygoteModUnionClearCards"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1506 | } else { |
| 1507 | card_table_->ClearSpaceCards(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1508 | timings.AddSplit("ClearCards"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1509 | } |
| 1510 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1511 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1512 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1513 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1514 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1515 | for (Object** it = live_stack_->Begin(); it != live_stack_->End(); ++it) { |
| 1516 | CHECK(!GetLiveBitmap()->Test(*it)); |
| 1517 | } |
| 1518 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1519 | if (gc_type == kGcTypePartial) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1520 | // Copy the mark bits over from the live bits, do this as early as possible or else we can |
| 1521 | // accidentally un-mark roots. |
| 1522 | // Needed for scanning dirty objects. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1523 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1524 | if ((*it)->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) { |
| 1525 | mark_sweep.CopyMarkBits(*it); |
| 1526 | } |
| 1527 | } |
| 1528 | timings.AddSplit("CopyMarkBits"); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1529 | mark_sweep.SetImmuneRange(reinterpret_cast<Object*>(spaces_.front()->Begin()), |
| 1530 | reinterpret_cast<Object*>(alloc_space_->Begin())); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1531 | } else if (gc_type == kGcTypeSticky) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1532 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1533 | if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) { |
| 1534 | mark_sweep.CopyMarkBits(*it); |
| 1535 | } |
| 1536 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1537 | large_object_space_->CopyLiveToMarked(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1538 | timings.AddSplit("CopyMarkBits"); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1539 | mark_sweep.SetImmuneRange(reinterpret_cast<Object*>(spaces_.front()->Begin()), |
| 1540 | reinterpret_cast<Object*>(alloc_space_->Begin())); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1543 | // Marking roots is not necessary for sticky mark bits since we only actually require the |
| 1544 | // remarking of roots. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1545 | if (gc_type != kGcTypeSticky) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1546 | mark_sweep.MarkRoots(); |
| 1547 | timings.AddSplit("MarkRoots"); |
| 1548 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1549 | |
| 1550 | if (verify_mod_union_table_) { |
| 1551 | zygote_mod_union_table_->Update(); |
| 1552 | zygote_mod_union_table_->Verify(); |
| 1553 | mod_union_table_->Update(); |
| 1554 | mod_union_table_->Verify(); |
| 1555 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | // Roots are marked on the bitmap and the mark_stack is empty. |
| 1559 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
| 1560 | |
| 1561 | // Allow mutators to go again, acquire share on mutator_lock_ to continue. |
| 1562 | thread_list->ResumeAll(); |
| 1563 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1564 | ReaderMutexLock reader_lock(self, *Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1565 | root_end = NanoTime(); |
| 1566 | timings.AddSplit("RootEnd"); |
| 1567 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1568 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1569 | UpdateAndMarkModUnion(timings, gc_type); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1570 | |
| 1571 | // Mark everything as live so that sweeping system weak works correctly for sticky mark bit |
| 1572 | // GCs. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1573 | MarkAllocStack(alloc_space_->GetLiveBitmap(), large_object_space_->GetLiveObjects(), |
| 1574 | live_stack_.get()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1575 | timings.AddSplit("MarkStackAsLive"); |
| 1576 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1577 | if (gc_type != kGcTypeSticky) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1578 | // Recursively mark all the non-image bits set in the mark bitmap. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1579 | mark_sweep.RecursiveMark(gc_type == kGcTypePartial, timings); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1580 | } else { |
| 1581 | mark_sweep.RecursiveMarkCards(card_table_.get(), dirty_cards, timings); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1582 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1583 | mark_sweep.DisableFinger(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1584 | } |
| 1585 | // Release share on mutator_lock_ and then get exclusive access. |
| 1586 | dirty_begin = NanoTime(); |
| 1587 | thread_list->SuspendAll(); |
| 1588 | timings.AddSplit("ReSuspend"); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1589 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1590 | |
| 1591 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1592 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1593 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1594 | // Re-mark root set. |
| 1595 | mark_sweep.ReMarkRoots(); |
| 1596 | timings.AddSplit("ReMarkRoots"); |
| 1597 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1598 | if (verify_missing_card_marks_) { |
| 1599 | // Since verify missing card marks uses a sweep array to empty the allocation stack, we |
| 1600 | // need to make sure that we don't free weaks which wont get swept by SweepSystemWeaks. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1601 | MarkAllocStack(alloc_space_->GetLiveBitmap(), large_object_space_->GetLiveObjects(), |
| 1602 | allocation_stack_.get()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1605 | // 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] | 1606 | mark_sweep.RecursiveMarkDirtyObjects(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1607 | timings.AddSplit("RecursiveMarkDirtyObjects"); |
| 1608 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1609 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1610 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1611 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1612 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1613 | mark_sweep.ProcessReferences(clear_soft_references); |
| 1614 | timings.AddSplit("ProcessReferences"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1615 | |
| 1616 | // This doesn't work with mutators unpaused for some reason, TODO: Fix. |
| 1617 | mark_sweep.SweepSystemWeaks(false); |
| 1618 | timings.AddSplit("SweepSystemWeaks"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1619 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1620 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1621 | // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps |
| 1622 | // these bitmaps. Doing this enables us to sweep with the heap unlocked since new allocations |
| 1623 | // set the live bit, but since we have the bitmaps reversed at this point, this sets the mark |
| 1624 | // bit instead, resulting in no new allocated objects being incorrectly freed by sweep. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1625 | const bool swap = true; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1626 | if (swap) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1627 | SwapBitmaps(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1630 | // Only need to do this if we have the card mark verification on, and only during concurrent GC. |
| 1631 | if (verify_missing_card_marks_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1632 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1633 | mark_sweep.SweepArray(timings, allocation_stack_.get(), swap); |
| 1634 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1635 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1636 | // We only sweep over the live stack, and the live stack should not intersect with the |
| 1637 | // allocation stack, so it should be safe to UnMark anything in the allocation stack as live. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1638 | UnMarkAllocStack(alloc_space_->GetLiveBitmap(), large_object_space_->GetLiveObjects(), |
| 1639 | allocation_stack_.get()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1640 | timings.AddSplit("UnMarkAllocStack"); |
| 1641 | } |
| 1642 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1643 | if (kIsDebugBuild) { |
| 1644 | // Verify that we only reach marked objects from the image space. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1645 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1646 | mark_sweep.VerifyImageRoots(); |
| 1647 | timings.AddSplit("VerifyImageRoots"); |
| 1648 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1649 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1650 | if (verify_post_gc_heap_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1651 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1652 | if (!VerifyHeapReferences()) { |
| 1653 | LOG(FATAL) << "Post " << gc_type_str.str() << "Gc verification failed"; |
| 1654 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1655 | timings.AddSplit("VerifyHeapReferencesPostGC"); |
| 1656 | } |
| 1657 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1658 | thread_list->ResumeAll(); |
| 1659 | dirty_end = NanoTime(); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1660 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1661 | |
| 1662 | { |
| 1663 | // TODO: this lock shouldn't be necessary (it's why we did the bitmap flip above). |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1664 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1665 | if (gc_type != kGcTypeSticky) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1666 | mark_sweep.SweepLargeObjects(swap); |
| 1667 | timings.AddSplit("SweepLargeObjects"); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1668 | mark_sweep.Sweep(gc_type == kGcTypePartial, swap); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1669 | } else { |
| 1670 | mark_sweep.SweepArray(timings, live_stack_.get(), swap); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1671 | timings.AddSplit("SweepArray"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1672 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1673 | live_stack_->Reset(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1674 | timings.AddSplit("Sweep"); |
| 1675 | } |
| 1676 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1677 | if (verify_system_weaks_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1678 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1679 | mark_sweep.VerifySystemWeaks(); |
| 1680 | timings.AddSplit("VerifySystemWeaks"); |
| 1681 | } |
| 1682 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1683 | cleared_references = mark_sweep.GetClearedReferences(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1684 | bytes_freed = mark_sweep.GetFreedBytes(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | GrowForUtilization(); |
| 1688 | timings.AddSplit("GrowForUtilization"); |
| 1689 | |
| 1690 | EnqueueClearedReferences(&cleared_references); |
| 1691 | RequestHeapTrim(); |
| 1692 | timings.AddSplit("Finish"); |
| 1693 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1694 | // If the GC was slow, then print timings in the log. |
| 1695 | uint64_t pause_roots = (root_end - root_begin) / 1000 * 1000; |
| 1696 | uint64_t pause_dirty = (dirty_end - dirty_begin) / 1000 * 1000; |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1697 | uint64_t duration = (NanoTime() - root_begin) / 1000 * 1000; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1698 | if (pause_roots > MsToNs(5) || pause_dirty > MsToNs(5)) { |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1699 | const size_t percent_free = GetPercentFree(); |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1700 | const size_t current_heap_size = GetUsedMemorySize(); |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1701 | const size_t total_memory = GetTotalMemory(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1702 | LOG(INFO) << gc_type_str.str() |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1703 | << "Concurrent GC freed " << PrettySize(bytes_freed) << ", " << percent_free |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1704 | << "% free, " << PrettySize(current_heap_size) << "/" |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1705 | << PrettySize(total_memory) << ", " << "paused " << PrettyDuration(pause_roots) |
| 1706 | << "+" << PrettyDuration(pause_dirty) << " total " << PrettyDuration(duration); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1707 | if (VLOG_IS_ON(heap)) { |
| 1708 | timings.Dump(); |
| 1709 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1710 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1711 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1712 | CumulativeLogger* logger = cumulative_timings_.Get(gc_type); |
| 1713 | logger->Start(); |
| 1714 | logger->AddLogger(timings); |
| 1715 | logger->End(); // Next iteration. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1718 | GcType Heap::WaitForConcurrentGcToComplete(Thread* self) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1719 | GcType last_gc_type = kGcTypeNone; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1720 | if (concurrent_gc_) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1721 | bool do_wait; |
| 1722 | uint64_t wait_start = NanoTime(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1723 | { |
| 1724 | // Check if GC is running holding gc_complete_lock_. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1725 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1726 | do_wait = is_gc_running_; |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1727 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1728 | if (do_wait) { |
| 1729 | // We must wait, change thread state then sleep on gc_complete_cond_; |
| 1730 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingForGcToComplete); |
| 1731 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1732 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1733 | while (is_gc_running_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1734 | gc_complete_cond_->Wait(self, *gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1735 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1736 | last_gc_type = last_gc_type_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1737 | } |
| 1738 | uint64_t wait_time = NanoTime() - wait_start; |
| 1739 | if (wait_time > MsToNs(5)) { |
| 1740 | LOG(INFO) << "WaitForConcurrentGcToComplete blocked for " << PrettyDuration(wait_time); |
| 1741 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1742 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1743 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1744 | return last_gc_type; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1747 | void Heap::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1748 | os << "Heap: " << GetPercentFree() << "% free, " << PrettySize(num_bytes_allocated_) << "/" |
| 1749 | << PrettySize(GetTotalMemory()) << "; " << num_objects_allocated_ << " objects\n"; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1750 | // Dump cumulative timings. |
| 1751 | LOG(INFO) << "Dumping cumulative Gc timings"; |
| 1752 | for (CumulativeTimings::iterator it = cumulative_timings_.begin(); |
| 1753 | it != cumulative_timings_.end(); ++it) { |
| 1754 | it->second->Dump(); |
| 1755 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | size_t Heap::GetPercentFree() { |
| 1759 | size_t total = GetTotalMemory(); |
| 1760 | return 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total); |
| 1761 | } |
| 1762 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1763 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1764 | AllocSpace* alloc_space = alloc_space_; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1765 | size_t alloc_space_capacity = alloc_space->Capacity(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1766 | alloc_space_capacity -= large_object_space_->GetNumBytesAllocated(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1767 | if (max_allowed_footprint > alloc_space_capacity) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1768 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) << " to " |
| 1769 | << PrettySize(alloc_space_capacity); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1770 | max_allowed_footprint = alloc_space_capacity; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1771 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1772 | alloc_space->SetFootprintLimit(max_allowed_footprint); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 1775 | // 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] | 1776 | static const size_t kHeapIdealFree = 2 * MB; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 1777 | // kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization, |
| 1778 | // regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1779 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 1780 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1781 | void Heap::GrowForUtilization() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1782 | size_t target_size; |
| 1783 | bool use_footprint_limit = false; |
| 1784 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1785 | // We know what our utilization is at this moment. |
| 1786 | // This doesn't actually resize any memory. It just lets the heap grow more when necessary. |
| 1787 | target_size = num_bytes_allocated_ / Heap::GetTargetHeapUtilization(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1788 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1789 | if (target_size > num_bytes_allocated_ + kHeapIdealFree) { |
| 1790 | target_size = num_bytes_allocated_ + kHeapIdealFree; |
| 1791 | } else if (target_size < num_bytes_allocated_ + kHeapMinFree) { |
| 1792 | target_size = num_bytes_allocated_ + kHeapMinFree; |
| 1793 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1794 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1795 | // Calculate when to perform the next ConcurrentGC. |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 1796 | if (GetTotalMemory() - GetUsedMemorySize() < concurrent_min_free_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1797 | // Not enough free memory to perform concurrent GC. |
| 1798 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 1799 | } else { |
| 1800 | // Compute below to avoid holding both the statistics and the alloc space lock |
| 1801 | use_footprint_limit = true; |
| 1802 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1803 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1804 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1805 | if (use_footprint_limit) { |
| 1806 | size_t foot_print_limit = alloc_space_->GetFootprintLimit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1807 | concurrent_start_bytes_ = foot_print_limit - concurrent_start_size_; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1808 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1809 | SetIdealFootprint(target_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1812 | void Heap::ClearGrowthLimit() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1813 | WaitForConcurrentGcToComplete(Thread::Current()); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1814 | alloc_space_->ClearGrowthLimit(); |
| 1815 | } |
| 1816 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1817 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1818 | MemberOffset reference_queue_offset, |
| 1819 | MemberOffset reference_queueNext_offset, |
| 1820 | MemberOffset reference_pendingNext_offset, |
| 1821 | MemberOffset finalizer_reference_zombie_offset) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1822 | reference_referent_offset_ = reference_referent_offset; |
| 1823 | reference_queue_offset_ = reference_queue_offset; |
| 1824 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 1825 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 1826 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 1827 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1828 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 1829 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 1830 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 1831 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 1832 | } |
| 1833 | |
| 1834 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 1835 | DCHECK(reference != NULL); |
| 1836 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1837 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 1838 | } |
| 1839 | |
| 1840 | void Heap::ClearReferenceReferent(Object* reference) { |
| 1841 | DCHECK(reference != NULL); |
| 1842 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1843 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 1844 | } |
| 1845 | |
| 1846 | // Returns true if the reference object has not yet been enqueued. |
| 1847 | bool Heap::IsEnqueuable(const Object* ref) { |
| 1848 | DCHECK(ref != NULL); |
| 1849 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 1850 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 1851 | return (queue != NULL) && (queue_next == NULL); |
| 1852 | } |
| 1853 | |
| 1854 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 1855 | DCHECK(ref != NULL); |
| 1856 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 1857 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 1858 | EnqueuePendingReference(ref, cleared_reference_list); |
| 1859 | } |
| 1860 | |
| 1861 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 1862 | DCHECK(ref != NULL); |
| 1863 | DCHECK(list != NULL); |
| 1864 | |
| 1865 | if (*list == NULL) { |
| 1866 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1867 | *list = ref; |
| 1868 | } else { |
| 1869 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1870 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 1871 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | Object* Heap::DequeuePendingReference(Object** list) { |
| 1876 | DCHECK(list != NULL); |
| 1877 | DCHECK(*list != NULL); |
| 1878 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1879 | Object* ref; |
| 1880 | if (*list == head) { |
| 1881 | ref = *list; |
| 1882 | *list = NULL; |
| 1883 | } else { |
| 1884 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1885 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 1886 | ref = head; |
| 1887 | } |
| 1888 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 1889 | return ref; |
| 1890 | } |
| 1891 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 1892 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1893 | ScopedObjectAccess soa(self); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1894 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1895 | args[0].SetL(object); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1896 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_FinalizerReference_add)->Invoke(self, NULL, args, |
| 1897 | NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | size_t Heap::GetBytesAllocated() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1901 | return num_bytes_allocated_; |
| 1902 | } |
| 1903 | |
| 1904 | size_t Heap::GetObjectsAllocated() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1905 | return num_objects_allocated_; |
| 1906 | } |
| 1907 | |
| 1908 | size_t Heap::GetConcurrentStartSize() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1909 | return concurrent_start_size_; |
| 1910 | } |
| 1911 | |
| 1912 | size_t Heap::GetConcurrentMinFree() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1913 | return concurrent_min_free_; |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 1917 | DCHECK(cleared != NULL); |
| 1918 | if (*cleared != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1919 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1920 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1921 | args[0].SetL(*cleared); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1922 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_ReferenceQueue_add)->Invoke(soa.Self(), NULL, |
| 1923 | args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1924 | *cleared = NULL; |
| 1925 | } |
| 1926 | } |
| 1927 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 1928 | void Heap::RequestConcurrentGC(Thread* self) { |
Mathieu Chartier | 069387a | 2012-06-18 12:01:01 -0700 | [diff] [blame] | 1929 | // Make sure that we can do a concurrent GC. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1930 | Runtime* runtime = Runtime::Current(); |
| 1931 | if (requesting_gc_ || runtime == NULL || !runtime->IsFinishedStarting() || |
| 1932 | !runtime->IsConcurrentGcEnabled()) { |
| 1933 | return; |
| 1934 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1935 | { |
| 1936 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 1937 | if (runtime->IsShuttingDown()) { |
| 1938 | return; |
| 1939 | } |
| 1940 | } |
| 1941 | if (self->IsHandlingStackOverflow()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | requesting_gc_ = true; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1946 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1947 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1948 | DCHECK(WellKnownClasses::java_lang_Daemons_requestGC != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1949 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 1950 | WellKnownClasses::java_lang_Daemons_requestGC); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1951 | CHECK(!env->ExceptionCheck()); |
| 1952 | requesting_gc_ = false; |
| 1953 | } |
| 1954 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1955 | void Heap::ConcurrentGC(Thread* self) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1956 | { |
| 1957 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 1958 | if (Runtime::Current()->IsShuttingDown() || !concurrent_gc_) { |
| 1959 | return; |
| 1960 | } |
Mathieu Chartier | 2542d66 | 2012-06-21 17:14:11 -0700 | [diff] [blame] | 1961 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1962 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1963 | // TODO: We shouldn't need a WaitForConcurrentGcToComplete here since only |
| 1964 | // concurrent GC resumes threads before the GC is completed and this function |
| 1965 | // is only called within the GC daemon thread. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1966 | if (WaitForConcurrentGcToComplete(self) == kGcTypeNone) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1967 | // Start a concurrent GC as one wasn't in progress |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1968 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1969 | if (alloc_space_->Size() > min_alloc_space_size_for_sticky_gc_) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1970 | CollectGarbageInternal(kGcTypeSticky, false); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1971 | } else { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1972 | CollectGarbageInternal(kGcTypePartial, false); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1973 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1974 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1977 | void Heap::Trim(Thread* self) { |
| 1978 | WaitForConcurrentGcToComplete(self); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1979 | alloc_space_->Trim(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1982 | void Heap::RequestHeapTrim() { |
| 1983 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 1984 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 1985 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 1986 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 1987 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 1988 | // not how much use we're making of those pages. |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1989 | uint64_t ms_time = NsToMs(NanoTime()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1990 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1991 | float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size(); |
| 1992 | if ((utilization > 0.75f) || ((ms_time - last_trim_time_) < 2 * 1000)) { |
| 1993 | // Don't bother trimming the heap if it's more than 75% utilized, or if a |
| 1994 | // heap trim occurred in the last two seconds. |
| 1995 | return; |
| 1996 | } |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1997 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1998 | |
| 1999 | Thread* self = Thread::Current(); |
| 2000 | { |
| 2001 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 2002 | Runtime* runtime = Runtime::Current(); |
| 2003 | if (runtime == NULL || !runtime->IsFinishedStarting() || runtime->IsShuttingDown()) { |
| 2004 | // Heap trimming isn't supported without a Java runtime or Daemons (such as at dex2oat time) |
| 2005 | // Also: we do not wish to start a heap trim if the runtime is shutting down (a racy check |
| 2006 | // as we don't hold the lock while requesting the trim). |
| 2007 | return; |
| 2008 | } |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 2009 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 2010 | last_trim_time_ = ms_time; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 2011 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 2012 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 2013 | DCHECK(WellKnownClasses::java_lang_Daemons_requestHeapTrim != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2014 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 2015 | WellKnownClasses::java_lang_Daemons_requestHeapTrim); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 2016 | CHECK(!env->ExceptionCheck()); |
| 2017 | } |
| 2018 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2019 | } // namespace art |