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