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 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 25 | #include "base/stl_util.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 26 | #include "debugger.h" |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 27 | #include "gc/atomic_stack.h" |
| 28 | #include "gc/card_table.h" |
| 29 | #include "gc/heap_bitmap.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 30 | #include "gc/large_object_space.h" |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 31 | #include "gc/mark_sweep.h" |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 32 | #include "gc/partial_mark_sweep.h" |
| 33 | #include "gc/sticky_mark_sweep.h" |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 34 | #include "gc/mod_union_table.h" |
| 35 | #include "gc/space.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 36 | #include "image.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 37 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 38 | #include "object_utils.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 39 | #include "os.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 40 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 41 | #include "scoped_thread_state_change.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 42 | #include "sirt_ref.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 43 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 44 | #include "timing_logger.h" |
| 45 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 46 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 47 | |
| 48 | namespace art { |
| 49 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 50 | static const uint64_t kSlowGcThreshold = MsToNs(100); |
| 51 | static const uint64_t kLongGcPauseThreshold = MsToNs(5); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 52 | static const bool kDumpGcPerformanceOnShutdown = false; |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 53 | const double Heap::kDefaultTargetUtilization = 0.5; |
| 54 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 55 | static bool GenerateImage(const std::string& image_file_name) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 56 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 57 | std::vector<std::string> boot_class_path; |
| 58 | Split(boot_class_path_string, ':', boot_class_path); |
Brian Carlstrom | b279337 | 2012-03-17 18:27:16 -0700 | [diff] [blame] | 59 | if (boot_class_path.empty()) { |
| 60 | LOG(FATAL) << "Failed to generate image because no boot class path specified"; |
| 61 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 62 | |
| 63 | std::vector<char*> arg_vector; |
| 64 | |
| 65 | std::string dex2oat_string(GetAndroidRoot()); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 66 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 67 | const char* dex2oat = dex2oat_string.c_str(); |
| 68 | arg_vector.push_back(strdup(dex2oat)); |
| 69 | |
| 70 | std::string image_option_string("--image="); |
| 71 | image_option_string += image_file_name; |
| 72 | const char* image_option = image_option_string.c_str(); |
| 73 | arg_vector.push_back(strdup(image_option)); |
| 74 | |
| 75 | arg_vector.push_back(strdup("--runtime-arg")); |
| 76 | arg_vector.push_back(strdup("-Xms64m")); |
| 77 | |
| 78 | arg_vector.push_back(strdup("--runtime-arg")); |
| 79 | arg_vector.push_back(strdup("-Xmx64m")); |
| 80 | |
| 81 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 82 | std::string dex_file_option_string("--dex-file="); |
| 83 | dex_file_option_string += boot_class_path[i]; |
| 84 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 85 | arg_vector.push_back(strdup(dex_file_option)); |
| 86 | } |
| 87 | |
| 88 | std::string oat_file_option_string("--oat-file="); |
| 89 | oat_file_option_string += image_file_name; |
| 90 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 91 | oat_file_option_string += "oat"; |
| 92 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 93 | arg_vector.push_back(strdup(oat_file_option)); |
| 94 | |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 95 | std::string base_option_string(StringPrintf("--base=0x%x", ART_BASE_ADDRESS)); |
| 96 | arg_vector.push_back(strdup(base_option_string.c_str())); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 97 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 98 | std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 99 | LOG(INFO) << command_line; |
| 100 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 101 | arg_vector.push_back(NULL); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 102 | char** argv = &arg_vector[0]; |
| 103 | |
| 104 | // fork and exec dex2oat |
| 105 | pid_t pid = fork(); |
| 106 | if (pid == 0) { |
| 107 | // no allocation allowed between fork and exec |
| 108 | |
| 109 | // change process groups, so we don't get reaped by ProcessManager |
| 110 | setpgid(0, 0); |
| 111 | |
| 112 | execv(dex2oat, argv); |
| 113 | |
| 114 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 115 | return false; |
| 116 | } else { |
| 117 | STLDeleteElements(&arg_vector); |
| 118 | |
| 119 | // wait for dex2oat to finish |
| 120 | int status; |
| 121 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 122 | if (got_pid != pid) { |
| 123 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 124 | return false; |
| 125 | } |
| 126 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 127 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 134 | void Heap::UnReserveOatFileAddressRange() { |
| 135 | oat_file_map_.reset(NULL); |
| 136 | } |
| 137 | |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 138 | Heap::Heap(size_t initial_size, size_t growth_limit, size_t min_free, size_t max_free, |
| 139 | double target_utilization, size_t capacity, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 140 | const std::string& original_image_file_name, bool concurrent_gc) |
| 141 | : alloc_space_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 142 | card_table_(NULL), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 143 | concurrent_gc_(concurrent_gc), |
| 144 | have_zygote_space_(false), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 145 | is_gc_running_(false), |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 146 | last_gc_type_(kGcTypeNone), |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 147 | enforce_heap_growth_rate_(false), |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 148 | capacity_(capacity), |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 149 | growth_limit_(growth_limit), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 150 | max_allowed_footprint_(initial_size), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 151 | concurrent_start_size_(128 * KB), |
| 152 | concurrent_min_free_(256 * KB), |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 153 | concurrent_start_bytes_(concurrent_gc ? initial_size - concurrent_start_size_ : |
| 154 | std::numeric_limits<size_t>::max()), |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 155 | sticky_gc_count_(0), |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 156 | total_bytes_freed_(0), |
| 157 | total_objects_freed_(0), |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 158 | large_object_threshold_(3 * kPageSize), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 159 | num_bytes_allocated_(0), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 160 | verify_missing_card_marks_(false), |
| 161 | verify_system_weaks_(false), |
| 162 | verify_pre_gc_heap_(false), |
| 163 | verify_post_gc_heap_(false), |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 164 | verify_mod_union_table_(false), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 165 | partial_gc_frequency_(10), |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 166 | min_alloc_space_size_for_sticky_gc_(2 * MB), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 167 | min_remaining_space_for_sticky_gc_(1 * MB), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 168 | last_trim_time_(0), |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 169 | allocation_rate_(0), |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 170 | max_allocation_stack_size_(MB), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 171 | reference_referent_offset_(0), |
| 172 | reference_queue_offset_(0), |
| 173 | reference_queueNext_offset_(0), |
| 174 | reference_pendingNext_offset_(0), |
| 175 | finalizer_reference_zombie_offset_(0), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 176 | min_free_(min_free), |
| 177 | max_free_(max_free), |
| 178 | target_utilization_(target_utilization), |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 179 | total_wait_time_(0), |
| 180 | measure_allocation_time_(false), |
| 181 | total_allocation_time_(0), |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 182 | verify_objects_(false) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 183 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 184 | LOG(INFO) << "Heap() entering"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 187 | live_bitmap_.reset(new HeapBitmap(this)); |
| 188 | mark_bitmap_.reset(new HeapBitmap(this)); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 189 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 190 | // Requested begin for the alloc space, to follow the mapped image and oat files |
| 191 | byte* requested_begin = NULL; |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 192 | std::string image_file_name(original_image_file_name); |
| 193 | if (!image_file_name.empty()) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 194 | ImageSpace* image_space = NULL; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 195 | |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 196 | if (OS::FileExists(image_file_name.c_str())) { |
| 197 | // If the /system file exists, it should be up-to-date, don't try to generate |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 198 | image_space = ImageSpace::Create(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 199 | } else { |
| 200 | // If the /system file didn't exist, we need to use one from the art-cache. |
| 201 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 202 | // If it does not exist, generate. |
| 203 | image_file_name = GetArtCacheFilenameOrDie(image_file_name); |
| 204 | if (OS::FileExists(image_file_name.c_str())) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 205 | image_space = ImageSpace::Create(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 206 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 207 | if (image_space == NULL) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 208 | CHECK(GenerateImage(image_file_name)) << "Failed to generate image: " << image_file_name; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 209 | image_space = ImageSpace::Create(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 212 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 213 | CHECK(image_space != NULL) << "Failed to create space from " << image_file_name; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 214 | AddSpace(image_space); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 215 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 216 | // isn't going to get in the middle |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 217 | byte* oat_end_addr = image_space->GetImageHeader().GetOatEnd(); |
| 218 | CHECK_GT(oat_end_addr, image_space->End()); |
| 219 | |
| 220 | // Reserve address range from image_space->End() to image_space->GetImageHeader().GetOatEnd() |
| 221 | uintptr_t reserve_begin = RoundUp(reinterpret_cast<uintptr_t>(image_space->End()), kPageSize); |
| 222 | uintptr_t reserve_end = RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), kPageSize); |
| 223 | oat_file_map_.reset(MemMap::MapAnonymous("oat file reserve", |
| 224 | reinterpret_cast<byte*>(reserve_begin), |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame^] | 225 | reserve_end - reserve_begin, PROT_NONE)); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 226 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 227 | if (oat_end_addr > requested_begin) { |
| 228 | 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] | 229 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 230 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 233 | // Allocate the large object space. |
| 234 | large_object_space_.reset(FreeListSpace::Create("large object space", NULL, capacity)); |
Mathieu Chartier | 8e9a149 | 2012-10-04 12:25:40 -0700 | [diff] [blame] | 235 | live_bitmap_->SetLargeObjects(large_object_space_->GetLiveObjects()); |
| 236 | mark_bitmap_->SetLargeObjects(large_object_space_->GetMarkObjects()); |
| 237 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 238 | UniquePtr<DlMallocSpace> alloc_space(DlMallocSpace::Create("alloc space", initial_size, |
| 239 | growth_limit, capacity, |
| 240 | requested_begin)); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 241 | alloc_space_ = alloc_space.release(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 242 | CHECK(alloc_space_ != NULL) << "Failed to create alloc space"; |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 243 | alloc_space_->SetFootprintLimit(alloc_space_->Capacity()); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 244 | AddSpace(alloc_space_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 245 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 246 | // Spaces are sorted in order of Begin(). |
| 247 | byte* heap_begin = spaces_.front()->Begin(); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 248 | size_t heap_capacity = spaces_.back()->End() - spaces_.front()->Begin(); |
| 249 | if (spaces_.back()->IsAllocSpace()) { |
| 250 | heap_capacity += spaces_.back()->AsAllocSpace()->NonGrowthLimitCapacity(); |
| 251 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 252 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 253 | // Mark image objects in the live bitmap |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 254 | // TODO: C++0x |
| 255 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 256 | Space* space = *it; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 257 | if (space->IsImageSpace()) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 258 | ImageSpace* image_space = space->AsImageSpace(); |
| 259 | image_space->RecordImageAllocations(image_space->GetLiveBitmap()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 263 | // Allocate the card table. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 264 | card_table_.reset(CardTable::Create(heap_begin, heap_capacity)); |
| 265 | CHECK(card_table_.get() != NULL) << "Failed to create card table"; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 266 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 267 | mod_union_table_.reset(new ModUnionTableToZygoteAllocspace<ModUnionTableReferenceCache>(this)); |
| 268 | CHECK(mod_union_table_.get() != NULL) << "Failed to create mod-union table"; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 269 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 270 | zygote_mod_union_table_.reset(new ModUnionTableCardCache(this)); |
| 271 | 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] | 272 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 273 | // TODO: Count objects in the image space here. |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 274 | num_bytes_allocated_ = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 275 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 276 | // Default mark stack size in bytes. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 277 | static const size_t default_mark_stack_size = 64 * KB; |
| 278 | mark_stack_.reset(ObjectStack::Create("dalvik-mark-stack", default_mark_stack_size)); |
| 279 | allocation_stack_.reset(ObjectStack::Create("dalvik-allocation-stack", |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 280 | max_allocation_stack_size_)); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 281 | live_stack_.reset(ObjectStack::Create("dalvik-live-stack", |
| 282 | max_allocation_stack_size_)); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 283 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 284 | // It's still too early to take a lock because there are no threads yet, but we can create locks |
| 285 | // now. We don't create it earlier to make it clear that you can't use locks during heap |
| 286 | // initialization. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 287 | gc_complete_lock_ = new Mutex("GC complete lock"); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 288 | gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable", |
| 289 | *gc_complete_lock_)); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 290 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 291 | // Create the reference queue lock, this is required so for parrallel object scanning in the GC. |
| 292 | reference_queue_lock_.reset(new Mutex("reference queue lock")); |
| 293 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 294 | last_gc_time_ = NanoTime(); |
| 295 | last_gc_size_ = GetBytesAllocated(); |
| 296 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 297 | // Create our garbage collectors. |
| 298 | for (size_t i = 0; i < 2; ++i) { |
| 299 | const bool concurrent = i != 0; |
| 300 | mark_sweep_collectors_.push_back(new MarkSweep(this, concurrent)); |
| 301 | mark_sweep_collectors_.push_back(new PartialMarkSweep(this, concurrent)); |
| 302 | mark_sweep_collectors_.push_back(new StickyMarkSweep(this, concurrent)); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 305 | CHECK(max_allowed_footprint_ != 0); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 306 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 307 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 308 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 311 | void Heap::CreateThreadPool() { |
| 312 | // TODO: Make sysconf(_SC_NPROCESSORS_CONF) be a helper function? |
| 313 | // Use the number of processors - 1 since the thread doing the GC does work while its waiting for |
| 314 | // workers to complete. |
| 315 | thread_pool_.reset(new ThreadPool(sysconf(_SC_NPROCESSORS_CONF) - 1)); |
| 316 | } |
| 317 | |
| 318 | void Heap::DeleteThreadPool() { |
| 319 | thread_pool_.reset(NULL); |
| 320 | } |
| 321 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 322 | // Sort spaces based on begin address |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 323 | struct SpaceSorter { |
| 324 | bool operator ()(const ContinuousSpace* a, const ContinuousSpace* b) const { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 325 | return a->Begin() < b->Begin(); |
| 326 | } |
| 327 | }; |
| 328 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 329 | void Heap::AddSpace(ContinuousSpace* space) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 330 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 331 | DCHECK(space != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 332 | DCHECK(space->GetLiveBitmap() != NULL); |
| 333 | live_bitmap_->AddSpaceBitmap(space->GetLiveBitmap()); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 334 | DCHECK(space->GetMarkBitmap() != NULL); |
| 335 | mark_bitmap_->AddSpaceBitmap(space->GetMarkBitmap()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 336 | spaces_.push_back(space); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 337 | if (space->IsAllocSpace()) { |
| 338 | alloc_space_ = space->AsAllocSpace(); |
| 339 | } |
| 340 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 341 | // Ensure that spaces remain sorted in increasing order of start address (required for CMS finger) |
| 342 | std::sort(spaces_.begin(), spaces_.end(), SpaceSorter()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 343 | |
| 344 | // Ensure that ImageSpaces < ZygoteSpaces < AllocSpaces so that we can do address based checks to |
| 345 | // avoid redundant marking. |
| 346 | bool seen_zygote = false, seen_alloc = false; |
| 347 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 348 | Space* space = *it; |
| 349 | if (space->IsImageSpace()) { |
| 350 | DCHECK(!seen_zygote); |
| 351 | DCHECK(!seen_alloc); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 352 | } else if (space->IsZygoteSpace()) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 353 | DCHECK(!seen_alloc); |
| 354 | seen_zygote = true; |
| 355 | } else if (space->IsAllocSpace()) { |
| 356 | seen_alloc = true; |
| 357 | } |
| 358 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 361 | void Heap::DumpGcPerformanceInfo() { |
| 362 | // Dump cumulative timings. |
| 363 | LOG(INFO) << "Dumping cumulative Gc timings"; |
| 364 | uint64_t total_duration = 0; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 365 | |
| 366 | // Dump cumulative loggers for each GC type. |
| 367 | // TODO: C++0x |
| 368 | uint64_t total_paused_time = 0; |
| 369 | for (Collectors::const_iterator it = mark_sweep_collectors_.begin(); |
| 370 | it != mark_sweep_collectors_.end(); ++it) { |
| 371 | MarkSweep* collector = *it; |
| 372 | const CumulativeLogger& logger = collector->GetCumulativeTimings(); |
| 373 | if (logger.GetTotalNs() != 0) { |
| 374 | logger.Dump(); |
| 375 | const uint64_t total_ns = logger.GetTotalNs(); |
| 376 | const uint64_t total_pause_ns = (*it)->GetTotalPausedTime(); |
| 377 | double seconds = NsToMs(logger.GetTotalNs()) / 1000.0; |
| 378 | const uint64_t freed_bytes = collector->GetTotalFreedBytes(); |
| 379 | const uint64_t freed_objects = collector->GetTotalFreedObjects(); |
| 380 | LOG(INFO) |
| 381 | << collector->GetName() << " total time: " << PrettyDuration(total_ns) << "\n" |
| 382 | << collector->GetName() << " paused time: " << PrettyDuration(total_pause_ns) << "\n" |
| 383 | << collector->GetName() << " freed: " << freed_objects |
| 384 | << " objects with total size " << PrettySize(freed_bytes) << "\n" |
| 385 | << collector->GetName() << " throughput: " << freed_objects / seconds << "/s / " |
| 386 | << PrettySize(freed_bytes / seconds) << "/s\n"; |
| 387 | total_duration += total_ns; |
| 388 | total_paused_time += total_pause_ns; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | uint64_t allocation_time = static_cast<uint64_t>(total_allocation_time_) * kTimeAdjust; |
| 392 | size_t total_objects_allocated = GetTotalObjectsAllocated(); |
| 393 | size_t total_bytes_allocated = GetTotalBytesAllocated(); |
| 394 | if (total_duration != 0) { |
| 395 | const double total_seconds = double(total_duration / 1000) / 1000000.0; |
| 396 | LOG(INFO) << "Total time spent in GC: " << PrettyDuration(total_duration); |
| 397 | LOG(INFO) << "Mean GC size throughput: " |
| 398 | << PrettySize(GetTotalBytesFreed() / total_seconds) << "/s"; |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 399 | LOG(INFO) << "Mean GC object throughput: " |
| 400 | << (GetTotalObjectsFreed() / total_seconds) << " objects/s"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 401 | } |
| 402 | LOG(INFO) << "Total number of allocations: " << total_objects_allocated; |
| 403 | LOG(INFO) << "Total bytes allocated " << PrettySize(total_bytes_allocated); |
| 404 | if (measure_allocation_time_) { |
| 405 | LOG(INFO) << "Total time spent allocating: " << PrettyDuration(allocation_time); |
| 406 | LOG(INFO) << "Mean allocation time: " |
| 407 | << PrettyDuration(allocation_time / total_objects_allocated); |
| 408 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 409 | LOG(INFO) << "Total mutator paused time: " << PrettyDuration(total_paused_time); |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 410 | LOG(INFO) << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 413 | Heap::~Heap() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 414 | if (kDumpGcPerformanceOnShutdown) { |
| 415 | DumpGcPerformanceInfo(); |
| 416 | } |
| 417 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 418 | STLDeleteElements(&mark_sweep_collectors_); |
| 419 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 420 | // If we don't reset then the mark stack complains in it's destructor. |
| 421 | allocation_stack_->Reset(); |
| 422 | live_stack_->Reset(); |
| 423 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 424 | VLOG(heap) << "~Heap()"; |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 425 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 426 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 427 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 428 | // 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] | 429 | STLDeleteElements(&spaces_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 430 | delete gc_complete_lock_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 433 | ContinuousSpace* Heap::FindSpaceFromObject(const Object* obj) const { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 434 | // TODO: C++0x auto |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 435 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 436 | if ((*it)->Contains(obj)) { |
| 437 | return *it; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | ImageSpace* Heap::GetImageSpace() { |
| 445 | // TODO: C++0x auto |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 446 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 447 | if ((*it)->IsImageSpace()) { |
| 448 | return (*it)->AsImageSpace(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | return NULL; |
| 452 | } |
| 453 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 454 | DlMallocSpace* Heap::GetAllocSpace() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 455 | return alloc_space_; |
| 456 | } |
| 457 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 458 | static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 459 | size_t chunk_size = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start); |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 460 | if (used_bytes < chunk_size) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 461 | size_t chunk_free_bytes = chunk_size - used_bytes; |
| 462 | size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg); |
| 463 | max_contiguous_allocation = std::max(max_contiguous_allocation, chunk_free_bytes); |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 464 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 467 | Object* Heap::AllocObject(Thread* self, Class* c, size_t byte_count) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 468 | DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(Class)) || |
| 469 | (c->IsVariableSize() || c->GetObjectSize() == byte_count) || |
| 470 | strlen(ClassHelper(c).GetDescriptor()) == 0); |
| 471 | DCHECK_GE(byte_count, sizeof(Object)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 472 | |
| 473 | Object* obj = NULL; |
| 474 | size_t size = 0; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 475 | uint64_t allocation_start = 0; |
| 476 | if (measure_allocation_time_) { |
| 477 | allocation_start = NanoTime(); |
| 478 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 479 | |
| 480 | // We need to have a zygote space or else our newly allocated large object can end up in the |
| 481 | // Zygote resulting in it being prematurely freed. |
| 482 | // We can only do this for primive objects since large objects will not be within the card table |
| 483 | // range. This also means that we rely on SetClass not dirtying the object's card. |
| 484 | if (byte_count >= large_object_threshold_ && have_zygote_space_ && c->IsPrimitiveArray()) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 485 | size = RoundUp(byte_count, kPageSize); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 486 | obj = Allocate(self, large_object_space_.get(), size); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 487 | // Make sure that our large object didn't get placed anywhere within the space interval or else |
| 488 | // it breaks the immune range. |
| 489 | DCHECK(obj == NULL || |
| 490 | reinterpret_cast<byte*>(obj) < spaces_.front()->Begin() || |
| 491 | reinterpret_cast<byte*>(obj) >= spaces_.back()->End()); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 492 | } else { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 493 | obj = Allocate(self, alloc_space_, byte_count); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 494 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 495 | // Ensure that we did not allocate into a zygote space. |
| 496 | DCHECK(obj == NULL || !have_zygote_space_ || !FindSpaceFromObject(obj)->IsZygoteSpace()); |
| 497 | size = alloc_space_->AllocationSize(obj); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 500 | if (LIKELY(obj != NULL)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 501 | obj->SetClass(c); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 502 | |
| 503 | // Record allocation after since we want to use the atomic add for the atomic fence to guard |
| 504 | // 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] | 505 | RecordAllocation(size, obj); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 506 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 507 | if (Dbg::IsAllocTrackingEnabled()) { |
| 508 | Dbg::RecordAllocation(c, byte_count); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 509 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 510 | if (static_cast<size_t>(num_bytes_allocated_) >= concurrent_start_bytes_) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 511 | // We already have a request pending, no reason to start more until we update |
| 512 | // concurrent_start_bytes_. |
| 513 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 514 | // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint. |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 515 | SirtRef<Object> ref(self, obj); |
| 516 | RequestConcurrentGC(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 517 | } |
| 518 | VerifyObject(obj); |
| 519 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 520 | if (measure_allocation_time_) { |
| 521 | total_allocation_time_ += (NanoTime() - allocation_start) / kTimeAdjust; |
| 522 | } |
| 523 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 524 | return obj; |
| 525 | } |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 526 | std::ostringstream oss; |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 527 | int64_t total_bytes_free = GetFreeMemory(); |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 528 | uint64_t alloc_space_size = alloc_space_->GetNumBytesAllocated(); |
| 529 | uint64_t large_object_size = large_object_space_->GetNumObjectsAllocated(); |
| 530 | oss << "Failed to allocate a " << byte_count << " byte allocation with " << total_bytes_free |
| 531 | << " free bytes; allocation space size " << alloc_space_size |
| 532 | << "; large object space size " << large_object_size; |
| 533 | // If the allocation failed due to fragmentation, print out the largest continuous allocation. |
| 534 | if (total_bytes_free >= byte_count) { |
| 535 | size_t max_contiguous_allocation = 0; |
| 536 | // TODO: C++0x auto |
| 537 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 538 | if ((*it)->IsAllocSpace()) { |
| 539 | (*it)->AsAllocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation); |
| 540 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 541 | } |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 542 | oss << "; failed due to fragmentation (largest possible contiguous allocation " |
| 543 | << max_contiguous_allocation << " bytes)"; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 544 | } |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 545 | self->ThrowOutOfMemoryError(oss.str().c_str()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 546 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 549 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 550 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 551 | // require taking the lock. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 552 | if (obj == NULL) { |
| 553 | return true; |
| 554 | } |
| 555 | if (!IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 556 | return false; |
| 557 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 558 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 559 | if (spaces_[i]->Contains(obj)) { |
| 560 | return true; |
| 561 | } |
| 562 | } |
Mathieu Chartier | 0b0b515 | 2012-10-15 13:53:46 -0700 | [diff] [blame] | 563 | // Note: Doing this only works for the free list version of the large object space since the |
| 564 | // multiple memory map version uses a lock to do the contains check. |
| 565 | return large_object_space_->Contains(obj); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 568 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 569 | Locks::heap_bitmap_lock_->AssertReaderHeld(Thread::Current()); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 570 | return IsHeapAddress(obj) && GetLiveBitmap()->Test(obj); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 573 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 574 | void Heap::VerifyObject(const Object* obj) { |
jeffhao | 4eb68ed | 2012-10-17 16:41:07 -0700 | [diff] [blame] | 575 | if (obj == NULL || this == NULL || !verify_objects_ || Thread::Current() == NULL || |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 576 | Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 577 | return; |
| 578 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 579 | VerifyObjectBody(obj); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 580 | } |
| 581 | #endif |
| 582 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 583 | void Heap::DumpSpaces() { |
| 584 | // TODO: C++0x auto |
| 585 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 586 | ContinuousSpace* space = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 587 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 588 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 589 | LOG(INFO) << space << " " << *space << "\n" |
| 590 | << live_bitmap << " " << *live_bitmap << "\n" |
| 591 | << mark_bitmap << " " << *mark_bitmap; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 592 | } |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 593 | if (large_object_space_.get() != NULL) { |
| 594 | large_object_space_->Dump(LOG(INFO)); |
| 595 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 598 | void Heap::VerifyObjectBody(const Object* obj) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 599 | if (!IsAligned<kObjectAlignment>(obj)) { |
| 600 | LOG(FATAL) << "Object isn't aligned: " << obj; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Ian Rogers | f0bbeab | 2012-10-10 18:26:27 -0700 | [diff] [blame] | 603 | // TODO: the bitmap tests below are racy if VerifyObjectBody is called without the |
| 604 | // heap_bitmap_lock_. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 605 | if (!GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 606 | // Check the allocation stack / live stack. |
| 607 | if (!std::binary_search(live_stack_->Begin(), live_stack_->End(), obj) && |
| 608 | std::find(allocation_stack_->Begin(), allocation_stack_->End(), obj) == |
| 609 | allocation_stack_->End()) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 610 | if (large_object_space_->GetLiveObjects()->Test(obj)) { |
| 611 | DumpSpaces(); |
| 612 | LOG(FATAL) << "Object is dead: " << obj; |
| 613 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 614 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 615 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 616 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 617 | // Ignore early dawn of the universe verifications |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 618 | if (!VERIFY_OBJECT_FAST && GetObjectsAllocated() > 10) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 619 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 620 | Object::ClassOffset().Int32Value(); |
| 621 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 622 | if (c == NULL) { |
| 623 | LOG(FATAL) << "Null class in object: " << obj; |
| 624 | } else if (!IsAligned<kObjectAlignment>(c)) { |
| 625 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 626 | } else if (!GetLiveBitmap()->Test(c)) { |
| 627 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 628 | } |
| 629 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
| 630 | // Note: we don't use the accessors here as they have internal sanity checks |
| 631 | // that we don't want to run |
| 632 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
| 633 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 634 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
| 635 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 636 | CHECK_EQ(c_c, c_c_c); |
| 637 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 640 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 641 | DCHECK(obj != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 642 | reinterpret_cast<Heap*>(arg)->VerifyObjectBody(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void Heap::VerifyHeap() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 646 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 647 | GetLiveBitmap()->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 650 | void Heap::RecordAllocation(size_t size, Object* obj) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 651 | DCHECK(obj != NULL); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 652 | DCHECK_GT(size, 0u); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 653 | num_bytes_allocated_ += size; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 654 | |
| 655 | if (Runtime::Current()->HasStatsEnabled()) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 656 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 657 | ++thread_stats->allocated_objects; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 658 | thread_stats->allocated_bytes += size; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 659 | |
| 660 | // TODO: Update these atomically. |
| 661 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 662 | ++global_stats->allocated_objects; |
| 663 | global_stats->allocated_bytes += size; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 664 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 665 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 666 | // This is safe to do since the GC will never free objects which are neither in the allocation |
| 667 | // stack or the live bitmap. |
| 668 | while (!allocation_stack_->AtomicPushBack(obj)) { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 669 | CollectGarbageInternal(kGcTypeSticky, kGcCauseForAlloc, false); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 670 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 673 | void Heap::RecordFree(size_t freed_objects, size_t freed_bytes) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 674 | DCHECK_LE(freed_bytes, static_cast<size_t>(num_bytes_allocated_)); |
| 675 | num_bytes_allocated_ -= freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 676 | |
| 677 | if (Runtime::Current()->HasStatsEnabled()) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 678 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 679 | thread_stats->freed_objects += freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 680 | thread_stats->freed_bytes += freed_bytes; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 681 | |
| 682 | // TODO: Do this concurrently. |
| 683 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 684 | global_stats->freed_objects += freed_objects; |
| 685 | global_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 686 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 689 | Object* Heap::TryToAllocate(Thread* self, AllocSpace* space, size_t alloc_size, bool grow) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 690 | // Should we try to use a CAS here and fix up num_bytes_allocated_ later with AllocationSize? |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 691 | if (num_bytes_allocated_ + alloc_size > max_allowed_footprint_) { |
| 692 | // max_allowed_footprint_ <= growth_limit_ so it is safe to check in here. |
| 693 | if (num_bytes_allocated_ + alloc_size > growth_limit_) { |
| 694 | // Completely out of memory. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 695 | return NULL; |
| 696 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 697 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 698 | if (enforce_heap_growth_rate_) { |
| 699 | if (grow) { |
| 700 | // Grow the heap by alloc_size extra bytes. |
| 701 | max_allowed_footprint_ = std::min(max_allowed_footprint_ + alloc_size, growth_limit_); |
| 702 | VLOG(gc) << "Grow heap to " << PrettySize(max_allowed_footprint_) |
| 703 | << " for a " << PrettySize(alloc_size) << " allocation"; |
| 704 | } else { |
| 705 | return NULL; |
| 706 | } |
| 707 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 710 | return space->Alloc(self, alloc_size); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 713 | Object* Heap::Allocate(Thread* self, AllocSpace* space, size_t alloc_size) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 714 | // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are |
| 715 | // done in the runnable state where suspension is expected. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 716 | DCHECK_EQ(self->GetState(), kRunnable); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 717 | self->AssertThreadSuspensionIsAllowable(); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 718 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 719 | Object* ptr = TryToAllocate(self, space, alloc_size, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 720 | if (ptr != NULL) { |
| 721 | return ptr; |
| 722 | } |
| 723 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 724 | // The allocation failed. If the GC is running, block until it completes, and then retry the |
| 725 | // allocation. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 726 | GcType last_gc = WaitForConcurrentGcToComplete(self); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 727 | if (last_gc != kGcTypeNone) { |
| 728 | // A GC was in progress and we blocked, retry allocation now that memory has been freed. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 729 | ptr = TryToAllocate(self, space, alloc_size, false); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 730 | if (ptr != NULL) { |
| 731 | return ptr; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 735 | // Loop through our different Gc types and try to Gc until we get enough free memory. |
| 736 | for (size_t i = static_cast<size_t>(last_gc) + 1; i < static_cast<size_t>(kGcTypeMax); ++i) { |
| 737 | bool run_gc = false; |
| 738 | GcType gc_type = static_cast<GcType>(i); |
| 739 | switch (gc_type) { |
| 740 | case kGcTypeSticky: { |
| 741 | const size_t alloc_space_size = alloc_space_->Size(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 742 | run_gc = alloc_space_size > min_alloc_space_size_for_sticky_gc_ && |
| 743 | alloc_space_->Capacity() - alloc_space_size >= min_remaining_space_for_sticky_gc_; |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 744 | break; |
| 745 | } |
| 746 | case kGcTypePartial: |
| 747 | run_gc = have_zygote_space_; |
| 748 | break; |
| 749 | case kGcTypeFull: |
| 750 | run_gc = true; |
| 751 | break; |
| 752 | default: |
| 753 | break; |
| 754 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 755 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 756 | if (run_gc) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 757 | // If we actually ran a different type of Gc than requested, we can skip the index forwards. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 758 | GcType gc_type_ran = CollectGarbageInternal(gc_type, kGcCauseForAlloc, false); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 759 | DCHECK_GE(static_cast<size_t>(gc_type_ran), i); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 760 | i = static_cast<size_t>(gc_type_ran); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 761 | |
| 762 | // Did we free sufficient memory for the allocation to succeed? |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 763 | ptr = TryToAllocate(self, space, alloc_size, false); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 764 | if (ptr != NULL) { |
| 765 | return ptr; |
| 766 | } |
| 767 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // Allocations have failed after GCs; this is an exceptional state. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 771 | // Try harder, growing the heap if necessary. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 772 | ptr = TryToAllocate(self, space, alloc_size, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 773 | if (ptr != NULL) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 774 | return ptr; |
| 775 | } |
| 776 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 777 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 778 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 779 | // 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] | 780 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 781 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 782 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) |
| 783 | << " allocation"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 784 | |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 785 | // We don't need a WaitForConcurrentGcToComplete here either. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 786 | CollectGarbageInternal(kGcTypeFull, kGcCauseForAlloc, true); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 787 | return TryToAllocate(self, space, alloc_size, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 790 | void Heap::SetTargetHeapUtilization(float target) { |
| 791 | DCHECK_GT(target, 0.0f); // asserted in Java code |
| 792 | DCHECK_LT(target, 1.0f); |
| 793 | target_utilization_ = target; |
| 794 | } |
| 795 | |
| 796 | int64_t Heap::GetMaxMemory() const { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 797 | return growth_limit_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 800 | int64_t Heap::GetTotalMemory() const { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 801 | return GetMaxMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 804 | int64_t Heap::GetFreeMemory() const { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 805 | return GetMaxMemory() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 808 | size_t Heap::GetTotalBytesFreed() const { |
| 809 | return total_bytes_freed_; |
| 810 | } |
| 811 | |
| 812 | size_t Heap::GetTotalObjectsFreed() const { |
| 813 | return total_objects_freed_; |
| 814 | } |
| 815 | |
| 816 | size_t Heap::GetTotalObjectsAllocated() const { |
| 817 | size_t total = large_object_space_->GetTotalObjectsAllocated(); |
| 818 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 819 | Space* space = *it; |
| 820 | if (space->IsAllocSpace()) { |
| 821 | total += space->AsAllocSpace()->GetTotalObjectsAllocated(); |
| 822 | } |
| 823 | } |
| 824 | return total; |
| 825 | } |
| 826 | |
| 827 | size_t Heap::GetTotalBytesAllocated() const { |
| 828 | size_t total = large_object_space_->GetTotalBytesAllocated(); |
| 829 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 830 | Space* space = *it; |
| 831 | if (space->IsAllocSpace()) { |
| 832 | total += space->AsAllocSpace()->GetTotalBytesAllocated(); |
| 833 | } |
| 834 | } |
| 835 | return total; |
| 836 | } |
| 837 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 838 | class InstanceCounter { |
| 839 | public: |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 840 | InstanceCounter(Class* c, bool count_assignable, size_t* const count) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 841 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 842 | : class_(c), count_assignable_(count_assignable), count_(count) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 843 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 846 | void operator()(const Object* o) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 847 | const Class* instance_class = o->GetClass(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 848 | if (count_assignable_) { |
| 849 | if (instance_class == class_) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 850 | ++*count_; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 851 | } |
| 852 | } else { |
| 853 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 854 | ++*count_; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 859 | private: |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 860 | Class* class_; |
| 861 | bool count_assignable_; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 862 | size_t* const count_; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 863 | }; |
| 864 | |
| 865 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 866 | size_t count = 0; |
| 867 | InstanceCounter counter(c, count_assignable, &count); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 868 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 869 | GetLiveBitmap()->Visit(counter); |
| 870 | return count; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 873 | void Heap::CollectGarbage(bool clear_soft_references) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 874 | // Even if we waited for a GC we still need to do another GC since weaks allocated during the |
| 875 | // last GC will not have necessarily been cleared. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 876 | Thread* self = Thread::Current(); |
| 877 | WaitForConcurrentGcToComplete(self); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 878 | CollectGarbageInternal(kGcTypeFull, kGcCauseExplicit, clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 881 | void Heap::PreZygoteFork() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 882 | static Mutex zygote_creation_lock_("zygote creation lock", kZygoteCreationLock); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 883 | // Do this before acquiring the zygote creation lock so that we don't get lock order violations. |
| 884 | CollectGarbage(false); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 885 | Thread* self = Thread::Current(); |
| 886 | MutexLock mu(self, zygote_creation_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 887 | |
| 888 | // Try to see if we have any Zygote spaces. |
| 889 | if (have_zygote_space_) { |
| 890 | return; |
| 891 | } |
| 892 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 893 | VLOG(heap) << "Starting PreZygoteFork with alloc space size " << PrettySize(alloc_space_->Size()); |
| 894 | |
| 895 | { |
| 896 | // Flush the alloc stack. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 897 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 898 | FlushAllocStack(); |
| 899 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 900 | |
| 901 | // Replace the first alloc space we find with a zygote space. |
| 902 | // TODO: C++0x auto |
| 903 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 904 | if ((*it)->IsAllocSpace()) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 905 | DlMallocSpace* zygote_space = (*it)->AsAllocSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 906 | |
| 907 | // Turns the current alloc space into a Zygote space and obtain the new alloc space composed |
| 908 | // of the remaining available heap memory. |
| 909 | alloc_space_ = zygote_space->CreateZygoteSpace(); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 910 | alloc_space_->SetFootprintLimit(alloc_space_->Capacity()); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 911 | |
| 912 | // Change the GC retention policy of the zygote space to only collect when full. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 913 | zygote_space->SetGcRetentionPolicy(kGcRetentionPolicyFullCollect); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 914 | AddSpace(alloc_space_); |
| 915 | have_zygote_space_ = true; |
| 916 | break; |
| 917 | } |
| 918 | } |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 919 | |
Ian Rogers | 5f5a2c0 | 2012-09-17 10:52:08 -0700 | [diff] [blame] | 920 | // 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] | 921 | // TODO: C++0x |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 922 | for (Collectors::const_iterator it = mark_sweep_collectors_.begin(); |
| 923 | it != mark_sweep_collectors_.end(); ++it) { |
| 924 | (*it)->ResetCumulativeStatistics(); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 925 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 928 | void Heap::FlushAllocStack() { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 929 | MarkAllocStack(alloc_space_->GetLiveBitmap(), large_object_space_->GetLiveObjects(), |
| 930 | allocation_stack_.get()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 931 | allocation_stack_->Reset(); |
| 932 | } |
| 933 | |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 934 | size_t Heap::GetUsedMemorySize() const { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 935 | return num_bytes_allocated_; |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 938 | void Heap::MarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, ObjectStack* stack) { |
| 939 | Object** limit = stack->End(); |
| 940 | for (Object** it = stack->Begin(); it != limit; ++it) { |
| 941 | const Object* obj = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 942 | DCHECK(obj != NULL); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 943 | if (LIKELY(bitmap->HasAddress(obj))) { |
| 944 | bitmap->Set(obj); |
| 945 | } else { |
| 946 | large_objects->Set(obj); |
| 947 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 951 | void Heap::UnMarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, ObjectStack* stack) { |
| 952 | Object** limit = stack->End(); |
| 953 | for (Object** it = stack->Begin(); it != limit; ++it) { |
| 954 | const Object* obj = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 955 | DCHECK(obj != NULL); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 956 | if (LIKELY(bitmap->HasAddress(obj))) { |
| 957 | bitmap->Clear(obj); |
| 958 | } else { |
| 959 | large_objects->Clear(obj); |
| 960 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 964 | GcType Heap::CollectGarbageInternal(GcType gc_type, GcCause gc_cause, bool clear_soft_references) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 965 | Thread* self = Thread::Current(); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 966 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 967 | Locks::mutator_lock_->AssertNotHeld(self); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 968 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 969 | if (self->IsHandlingStackOverflow()) { |
| 970 | LOG(WARNING) << "Performing GC on a thread that is handling a stack overflow."; |
| 971 | } |
| 972 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 973 | // Ensure there is only one GC at a time. |
| 974 | bool start_collect = false; |
| 975 | while (!start_collect) { |
| 976 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 977 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 978 | if (!is_gc_running_) { |
| 979 | is_gc_running_ = true; |
| 980 | start_collect = true; |
| 981 | } |
| 982 | } |
| 983 | if (!start_collect) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 984 | WaitForConcurrentGcToComplete(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 985 | // TODO: if another thread beat this one to do the GC, perhaps we should just return here? |
| 986 | // Not doing at the moment to ensure soft references are cleared. |
| 987 | } |
| 988 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 989 | gc_complete_lock_->AssertNotHeld(self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 990 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 991 | if (gc_cause == kGcCauseForAlloc && Runtime::Current()->HasStatsEnabled()) { |
| 992 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 993 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 994 | } |
| 995 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 996 | // We need to do partial GCs every now and then to avoid the heap growing too much and |
| 997 | // fragmenting. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 998 | if (gc_type == kGcTypeSticky && ++sticky_gc_count_ > partial_gc_frequency_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 999 | gc_type = have_zygote_space_ ? kGcTypePartial : kGcTypeFull; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1000 | } |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1001 | if (gc_type != kGcTypeSticky) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1002 | sticky_gc_count_ = 0; |
| 1003 | } |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1004 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1005 | uint64_t gc_start_time = NanoTime(); |
| 1006 | uint64_t gc_start_size = GetBytesAllocated(); |
| 1007 | // Approximate allocation rate in bytes / second. |
| 1008 | DCHECK_NE(gc_start_time, last_gc_time_); |
| 1009 | uint64_t ms_delta = NsToMs(gc_start_time - last_gc_time_); |
| 1010 | if (ms_delta != 0) { |
| 1011 | allocation_rate_ = (gc_start_size - last_gc_size_) * 1000 / ms_delta; |
| 1012 | VLOG(heap) << "Allocation rate: " << PrettySize(allocation_rate_) << "/s"; |
| 1013 | } |
| 1014 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1015 | DCHECK_LT(gc_type, kGcTypeMax); |
| 1016 | DCHECK_NE(gc_type, kGcTypeNone); |
| 1017 | MarkSweep* collector = NULL; |
| 1018 | for (Collectors::iterator it = mark_sweep_collectors_.begin(); |
| 1019 | it != mark_sweep_collectors_.end(); ++it) { |
| 1020 | MarkSweep* cur_collector = *it; |
| 1021 | if (cur_collector->IsConcurrent() == concurrent_gc_ && cur_collector->GetGcType() == gc_type) { |
| 1022 | collector = cur_collector; |
| 1023 | break; |
| 1024 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1025 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1026 | CHECK(collector != NULL) |
| 1027 | << "Could not find garbage collector with concurrent=" << concurrent_gc_ |
| 1028 | << " and type=" << gc_type; |
| 1029 | collector->clear_soft_references_ = clear_soft_references; |
| 1030 | collector->Run(); |
| 1031 | total_objects_freed_ += collector->GetFreedObjects(); |
| 1032 | total_bytes_freed_ += collector->GetFreedBytes(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1033 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1034 | const size_t duration = collector->GetDuration(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1035 | std::vector<uint64_t> pauses = collector->GetPauseTimes(); |
| 1036 | bool was_slow = duration > kSlowGcThreshold || |
| 1037 | (gc_cause == kGcCauseForAlloc && duration > kLongGcPauseThreshold); |
| 1038 | for (size_t i = 0; i < pauses.size(); ++i) { |
| 1039 | if (pauses[i] > kLongGcPauseThreshold) { |
| 1040 | was_slow = true; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | if (was_slow) { |
| 1045 | const size_t percent_free = GetPercentFree(); |
| 1046 | const size_t current_heap_size = GetUsedMemorySize(); |
| 1047 | const size_t total_memory = GetTotalMemory(); |
| 1048 | std::ostringstream pause_string; |
| 1049 | for (size_t i = 0; i < pauses.size(); ++i) { |
| 1050 | pause_string << PrettyDuration((pauses[i] / 1000) * 1000) |
| 1051 | << ((i != pauses.size() - 1) ? ", " : ""); |
| 1052 | } |
| 1053 | LOG(INFO) << gc_cause << " " << collector->GetName() |
| 1054 | << "GC freed " << PrettySize(collector->GetFreedBytes()) << ", " |
| 1055 | << percent_free << "% free, " << PrettySize(current_heap_size) << "/" |
| 1056 | << PrettySize(total_memory) << ", " << "paused " << pause_string.str() |
| 1057 | << " total " << PrettyDuration((duration / 1000) * 1000); |
| 1058 | if (VLOG_IS_ON(heap)) { |
| 1059 | collector->GetTimings().Dump(); |
| 1060 | } |
| 1061 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1062 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1063 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1064 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1065 | is_gc_running_ = false; |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1066 | last_gc_type_ = gc_type; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1067 | // Wake anyone who may have been waiting for the GC to complete. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1068 | gc_complete_cond_->Broadcast(self); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1069 | } |
| 1070 | // Inform DDMS that a GC completed. |
| 1071 | Dbg::GcDidFinish(); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1072 | return gc_type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1073 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1074 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1075 | void Heap::UpdateAndMarkModUnion(MarkSweep* mark_sweep, TimingLogger& timings, GcType gc_type) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1076 | if (gc_type == kGcTypeSticky) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1077 | // Don't need to do anything for mod union table in this case since we are only scanning dirty |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1078 | // cards. |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | // Update zygote mod union table. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1083 | if (gc_type == kGcTypePartial) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1084 | zygote_mod_union_table_->Update(); |
| 1085 | timings.AddSplit("UpdateZygoteModUnionTable"); |
| 1086 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1087 | zygote_mod_union_table_->MarkReferences(mark_sweep); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1088 | timings.AddSplit("ZygoteMarkReferences"); |
| 1089 | } |
| 1090 | |
| 1091 | // Processes the cards we cleared earlier and adds their objects into the mod-union table. |
| 1092 | mod_union_table_->Update(); |
| 1093 | timings.AddSplit("UpdateModUnionTable"); |
| 1094 | |
| 1095 | // Scans all objects in the mod-union table. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1096 | mod_union_table_->MarkReferences(mark_sweep); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1097 | timings.AddSplit("MarkImageToAllocSpaceReferences"); |
| 1098 | } |
| 1099 | |
| 1100 | void Heap::RootMatchesObjectVisitor(const Object* root, void* arg) { |
| 1101 | Object* obj = reinterpret_cast<Object*>(arg); |
| 1102 | if (root == obj) { |
| 1103 | LOG(INFO) << "Object " << obj << " is a root"; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | class ScanVisitor { |
| 1108 | public: |
| 1109 | void operator ()(const Object* obj) const { |
| 1110 | LOG(INFO) << "Would have rescanned object " << obj; |
| 1111 | } |
| 1112 | }; |
| 1113 | |
| 1114 | class VerifyReferenceVisitor { |
| 1115 | public: |
| 1116 | VerifyReferenceVisitor(Heap* heap, bool* failed) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1117 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 1118 | Locks::heap_bitmap_lock_) |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1119 | : heap_(heap), |
| 1120 | failed_(failed) { |
| 1121 | } |
| 1122 | |
| 1123 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for smarter |
| 1124 | // analysis. |
| 1125 | void operator ()(const Object* obj, const Object* ref, const MemberOffset& /* offset */, |
| 1126 | bool /* is_static */) const NO_THREAD_SAFETY_ANALYSIS { |
| 1127 | // Verify that the reference is live. |
| 1128 | if (ref != NULL && !IsLive(ref)) { |
| 1129 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1130 | ObjectStack* alloc_stack = heap_->allocation_stack_.get(); |
| 1131 | ObjectStack* live_stack = heap_->live_stack_.get(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1132 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1133 | byte* card_addr = card_table->CardFromAddr(obj); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1134 | LOG(ERROR) << "Object " << obj << " references dead object " << ref << "\n" |
| 1135 | << "IsDirty = " << (*card_addr == CardTable::kCardDirty) << "\n" |
| 1136 | << "Obj type " << PrettyTypeOf(obj) << "\n" |
| 1137 | << "Ref type " << PrettyTypeOf(ref); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1138 | card_table->CheckAddrIsInCardTable(reinterpret_cast<const byte*>(obj)); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1139 | void* cover_begin = card_table->AddrFromCard(card_addr); |
| 1140 | void* cover_end = reinterpret_cast<void*>(reinterpret_cast<size_t>(cover_begin) + |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1141 | CardTable::kCardSize); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1142 | LOG(ERROR) << "Card " << reinterpret_cast<void*>(card_addr) << " covers " << cover_begin |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1143 | << "-" << cover_end; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1144 | SpaceBitmap* bitmap = heap_->GetLiveBitmap()->GetSpaceBitmap(obj); |
| 1145 | |
| 1146 | // Print out how the object is live. |
| 1147 | if (bitmap->Test(obj)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1148 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 1149 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1150 | if (std::binary_search(alloc_stack->Begin(), alloc_stack->End(), obj)) { |
| 1151 | LOG(ERROR) << "Object " << obj << " found in allocation stack"; |
| 1152 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1153 | if (std::binary_search(live_stack->Begin(), live_stack->End(), obj)) { |
| 1154 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1155 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1156 | if (std::binary_search(live_stack->Begin(), live_stack->End(), ref)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1157 | LOG(ERROR) << "Reference " << ref << " found in live stack!"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | // Attempt to see if the card table missed the reference. |
| 1161 | ScanVisitor scan_visitor; |
| 1162 | byte* byte_cover_begin = reinterpret_cast<byte*>(card_table->AddrFromCard(card_addr)); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1163 | card_table->Scan(bitmap, byte_cover_begin, byte_cover_begin + CardTable::kCardSize, |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1164 | scan_visitor, VoidFunctor()); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1165 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1166 | // Search to see if any of the roots reference our object. |
| 1167 | void* arg = const_cast<void*>(reinterpret_cast<const void*>(obj)); |
| 1168 | Runtime::Current()->VisitRoots(&Heap::RootMatchesObjectVisitor, arg); |
| 1169 | *failed_ = true; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | bool IsLive(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1174 | if (heap_->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1175 | return true; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1176 | } |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1177 | ObjectStack* alloc_stack = heap_->allocation_stack_.get(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1178 | // At this point we need to search the allocation since things in the live stack may get swept. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1179 | // If the object is not either in the live bitmap or allocation stack, so the object must be |
| 1180 | // dead. |
| 1181 | return std::binary_search(alloc_stack->Begin(), alloc_stack->End(), obj); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | private: |
| 1185 | Heap* heap_; |
| 1186 | bool* failed_; |
| 1187 | }; |
| 1188 | |
| 1189 | class VerifyObjectVisitor { |
| 1190 | public: |
| 1191 | VerifyObjectVisitor(Heap* heap) |
| 1192 | : heap_(heap), |
| 1193 | failed_(false) { |
| 1194 | |
| 1195 | } |
| 1196 | |
| 1197 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1198 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1199 | VerifyReferenceVisitor visitor(heap_, const_cast<bool*>(&failed_)); |
| 1200 | MarkSweep::VisitObjectReferences(obj, visitor); |
| 1201 | } |
| 1202 | |
| 1203 | bool Failed() const { |
| 1204 | return failed_; |
| 1205 | } |
| 1206 | |
| 1207 | private: |
| 1208 | Heap* heap_; |
| 1209 | bool failed_; |
| 1210 | }; |
| 1211 | |
| 1212 | // 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] | 1213 | bool Heap::VerifyHeapReferences() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1214 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1215 | // Lets sort our allocation stacks so that we can efficiently binary search them. |
| 1216 | std::sort(allocation_stack_->Begin(), allocation_stack_->End()); |
| 1217 | std::sort(live_stack_->Begin(), live_stack_->End()); |
| 1218 | // Perform the verification. |
| 1219 | VerifyObjectVisitor visitor(this); |
| 1220 | GetLiveBitmap()->Visit(visitor); |
| 1221 | // We don't want to verify the objects in the allocation stack since they themselves may be |
| 1222 | // pointing to dead objects if they are not reachable. |
| 1223 | if (visitor.Failed()) { |
| 1224 | DumpSpaces(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1225 | return false; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1226 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1227 | return true; |
| 1228 | } |
| 1229 | |
| 1230 | class VerifyReferenceCardVisitor { |
| 1231 | public: |
| 1232 | VerifyReferenceCardVisitor(Heap* heap, bool* failed) |
| 1233 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 1234 | Locks::heap_bitmap_lock_) |
| 1235 | : heap_(heap), |
| 1236 | failed_(failed) { |
| 1237 | } |
| 1238 | |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1239 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for |
| 1240 | // annotalysis on visitors. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1241 | void operator ()(const Object* obj, const Object* ref, const MemberOffset& offset, |
| 1242 | bool is_static) const NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1243 | // Filter out class references since changing an object's class does not mark the card as dirty. |
| 1244 | // Also handles large objects, since the only reference they hold is a class reference. |
| 1245 | if (ref != NULL && !ref->IsClass()) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1246 | CardTable* card_table = heap_->GetCardTable(); |
| 1247 | // If the object is not dirty and it is referencing something in the live stack other than |
| 1248 | // class, then it must be on a dirty card. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1249 | if (!card_table->AddrIsInCardTable(obj)) { |
| 1250 | LOG(ERROR) << "Object " << obj << " is not in the address range of the card table"; |
| 1251 | *failed_ = true; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1252 | } else if (!card_table->IsDirty(obj)) { |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1253 | // Card should be either kCardDirty if it got re-dirtied after we aged it, or |
| 1254 | // kCardDirty - 1 if it didnt get touched since we aged it. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1255 | ObjectStack* live_stack = heap_->live_stack_.get(); |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1256 | if (std::binary_search(live_stack->Begin(), live_stack->End(), ref)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1257 | if (std::binary_search(live_stack->Begin(), live_stack->End(), obj)) { |
| 1258 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 1259 | } |
| 1260 | if (heap_->GetLiveBitmap()->Test(obj)) { |
| 1261 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 1262 | } |
| 1263 | LOG(ERROR) << "Object " << obj << " " << PrettyTypeOf(obj) |
| 1264 | << " references " << ref << " " << PrettyTypeOf(ref) << " in live stack"; |
| 1265 | |
| 1266 | // Print which field of the object is dead. |
| 1267 | if (!obj->IsObjectArray()) { |
| 1268 | const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 1269 | CHECK(klass != NULL); |
| 1270 | const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 1271 | CHECK(fields != NULL); |
| 1272 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
| 1273 | const Field* cur = fields->Get(i); |
| 1274 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 1275 | LOG(ERROR) << (is_static ? "Static " : "") << "field in the live stack is " |
| 1276 | << PrettyField(cur); |
| 1277 | break; |
| 1278 | } |
| 1279 | } |
| 1280 | } else { |
| 1281 | const ObjectArray<Object>* object_array = obj->AsObjectArray<Object>(); |
| 1282 | for (int32_t i = 0; i < object_array->GetLength(); ++i) { |
| 1283 | if (object_array->Get(i) == ref) { |
| 1284 | LOG(ERROR) << (is_static ? "Static " : "") << "obj[" << i << "] = ref"; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | *failed_ = true; |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | private: |
| 1296 | Heap* heap_; |
| 1297 | bool* failed_; |
| 1298 | }; |
| 1299 | |
| 1300 | class VerifyLiveStackReferences { |
| 1301 | public: |
| 1302 | VerifyLiveStackReferences(Heap* heap) |
| 1303 | : heap_(heap), |
| 1304 | failed_(false) { |
| 1305 | |
| 1306 | } |
| 1307 | |
| 1308 | void operator ()(const Object* obj) const |
| 1309 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1310 | VerifyReferenceCardVisitor visitor(heap_, const_cast<bool*>(&failed_)); |
| 1311 | MarkSweep::VisitObjectReferences(obj, visitor); |
| 1312 | } |
| 1313 | |
| 1314 | bool Failed() const { |
| 1315 | return failed_; |
| 1316 | } |
| 1317 | |
| 1318 | private: |
| 1319 | Heap* heap_; |
| 1320 | bool failed_; |
| 1321 | }; |
| 1322 | |
| 1323 | bool Heap::VerifyMissingCardMarks() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1324 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1325 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1326 | // We need to sort the live stack since we binary search it. |
| 1327 | std::sort(live_stack_->Begin(), live_stack_->End()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1328 | VerifyLiveStackReferences visitor(this); |
| 1329 | GetLiveBitmap()->Visit(visitor); |
| 1330 | |
| 1331 | // We can verify objects in the live stack since none of these should reference dead objects. |
| 1332 | for (Object** it = live_stack_->Begin(); it != live_stack_->End(); ++it) { |
| 1333 | visitor(*it); |
| 1334 | } |
| 1335 | |
| 1336 | if (visitor.Failed()) { |
| 1337 | DumpSpaces(); |
| 1338 | return false; |
| 1339 | } |
| 1340 | return true; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1343 | void Heap::SwapStacks() { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1344 | allocation_stack_.swap(live_stack_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1345 | |
| 1346 | // Sort the live stack so that we can quickly binary search it later. |
| 1347 | if (VERIFY_OBJECT_ENABLED) { |
| 1348 | std::sort(live_stack_->Begin(), live_stack_->End()); |
| 1349 | } |
| 1350 | } |
| 1351 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1352 | void Heap::ProcessCards(TimingLogger& timings) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1353 | // Clear image space cards and keep track of cards we cleared in the mod-union table. |
| 1354 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1355 | ContinuousSpace* space = *it; |
| 1356 | if (space->IsImageSpace()) { |
| 1357 | mod_union_table_->ClearCards(*it); |
| 1358 | timings.AddSplit("ModUnionClearCards"); |
| 1359 | } else if (space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) { |
| 1360 | zygote_mod_union_table_->ClearCards(space); |
| 1361 | timings.AddSplit("ZygoteModUnionClearCards"); |
| 1362 | } else { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1363 | // No mod union table for the AllocSpace. Age the cards so that the GC knows that these cards |
| 1364 | // were dirty before the GC started. |
| 1365 | card_table_->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(), VoidFunctor()); |
| 1366 | timings.AddSplit("AllocSpaceClearCards"); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1371 | void Heap::PreGcVerification(GarbageCollector* gc) { |
| 1372 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1373 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1374 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1375 | if (verify_pre_gc_heap_) { |
| 1376 | thread_list->SuspendAll(); |
| 1377 | { |
| 1378 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1379 | if (!VerifyHeapReferences()) { |
| 1380 | LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed"; |
| 1381 | } |
| 1382 | } |
| 1383 | thread_list->ResumeAll(); |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1384 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1385 | |
| 1386 | // Check that all objects which reference things in the live stack are on dirty cards. |
| 1387 | if (verify_missing_card_marks_) { |
| 1388 | thread_list->SuspendAll(); |
| 1389 | { |
| 1390 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1391 | SwapStacks(); |
| 1392 | // Sort the live stack so that we can quickly binary search it later. |
| 1393 | if (!VerifyMissingCardMarks()) { |
| 1394 | LOG(FATAL) << "Pre " << gc->GetName() << " missing card mark verification failed"; |
| 1395 | } |
| 1396 | SwapStacks(); |
| 1397 | } |
| 1398 | thread_list->ResumeAll(); |
| 1399 | } |
| 1400 | |
| 1401 | if (verify_mod_union_table_) { |
| 1402 | thread_list->SuspendAll(); |
| 1403 | ReaderMutexLock reader_lock(self, *Locks::heap_bitmap_lock_); |
| 1404 | zygote_mod_union_table_->Update(); |
| 1405 | zygote_mod_union_table_->Verify(); |
| 1406 | mod_union_table_->Update(); |
| 1407 | mod_union_table_->Verify(); |
| 1408 | thread_list->ResumeAll(); |
| 1409 | } |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1412 | void Heap::PreSweepingGcVerification(GarbageCollector* gc) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1413 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1414 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1415 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1416 | // Called before sweeping occurs since we want to make sure we are not going so reclaim any |
| 1417 | // reachable objects. |
| 1418 | if (verify_post_gc_heap_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1419 | thread_list->SuspendAll(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1420 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1421 | // Swapping bound bitmaps does nothing. |
| 1422 | live_bitmap_.swap(mark_bitmap_); |
| 1423 | if (!VerifyHeapReferences()) { |
| 1424 | LOG(FATAL) << "Post " << gc->GetName() << "Gc verification failed"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1425 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1426 | live_bitmap_.swap(mark_bitmap_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1427 | thread_list->ResumeAll(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1428 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1429 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1430 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1431 | void Heap::PostGcVerification(GarbageCollector* gc) { |
| 1432 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1433 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1434 | if (verify_system_weaks_) { |
| 1435 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1436 | MarkSweep* mark_sweep = down_cast<MarkSweep*>(gc); |
| 1437 | mark_sweep->VerifySystemWeaks(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1438 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1441 | GcType Heap::WaitForConcurrentGcToComplete(Thread* self) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1442 | GcType last_gc_type = kGcTypeNone; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1443 | if (concurrent_gc_) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1444 | bool do_wait; |
| 1445 | uint64_t wait_start = NanoTime(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1446 | { |
| 1447 | // Check if GC is running holding gc_complete_lock_. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1448 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1449 | do_wait = is_gc_running_; |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1450 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1451 | if (do_wait) { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1452 | uint64_t wait_time; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1453 | // We must wait, change thread state then sleep on gc_complete_cond_; |
| 1454 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingForGcToComplete); |
| 1455 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1456 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1457 | while (is_gc_running_) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1458 | gc_complete_cond_->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1459 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1460 | last_gc_type = last_gc_type_; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1461 | wait_time = NanoTime() - wait_start;; |
| 1462 | total_wait_time_ += wait_time; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1463 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1464 | if (wait_time > kLongGcPauseThreshold) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1465 | LOG(INFO) << "WaitForConcurrentGcToComplete blocked for " << PrettyDuration(wait_time); |
| 1466 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1467 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1468 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1469 | return last_gc_type; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1472 | void Heap::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1473 | os << "Heap: " << GetPercentFree() << "% free, " << PrettySize(GetUsedMemorySize()) << "/" |
| 1474 | << PrettySize(GetTotalMemory()) << "; " << GetObjectsAllocated() << " objects\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1475 | DumpGcPerformanceInfo(); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | size_t Heap::GetPercentFree() { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1479 | return static_cast<size_t>(100.0f * static_cast<float>(GetFreeMemory()) / GetTotalMemory()); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1482 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1483 | if (max_allowed_footprint > GetMaxMemory()) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1484 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) << " to " |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1485 | << PrettySize(GetMaxMemory()); |
| 1486 | max_allowed_footprint = GetMaxMemory(); |
| 1487 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1488 | max_allowed_footprint_ = max_allowed_footprint; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1491 | void Heap::GrowForUtilization(uint64_t gc_duration) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1492 | // We know what our utilization is at this moment. |
| 1493 | // This doesn't actually resize any memory. It just lets the heap grow more when necessary. |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1494 | const size_t bytes_allocated = GetBytesAllocated(); |
| 1495 | last_gc_size_ = bytes_allocated; |
| 1496 | last_gc_time_ = NanoTime(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1497 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1498 | size_t target_size = bytes_allocated / GetTargetHeapUtilization(); |
| 1499 | if (target_size > bytes_allocated + max_free_) { |
| 1500 | target_size = bytes_allocated + max_free_; |
| 1501 | } else if (target_size < bytes_allocated + min_free_) { |
| 1502 | target_size = bytes_allocated + min_free_; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1503 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1504 | |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1505 | SetIdealFootprint(target_size); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1506 | |
| 1507 | // Calculate when to perform the next ConcurrentGC if we have enough free memory. |
| 1508 | if (concurrent_gc_ && GetFreeMemory() >= concurrent_min_free_) { |
| 1509 | // Calculate the estimated GC duration. |
| 1510 | double gc_duration_seconds = NsToMs(gc_duration) / 1000.0; |
| 1511 | // Estimate how many remaining bytes we will have when we need to start the next GC. |
| 1512 | size_t remaining_bytes = allocation_rate_ * gc_duration_seconds; |
| 1513 | if (remaining_bytes < max_allowed_footprint_) { |
| 1514 | // Start a concurrent GC when we get close to the estimated remaining bytes. When the |
| 1515 | // allocation rate is very high, remaining_bytes could tell us that we should start a GC |
| 1516 | // right away. |
| 1517 | concurrent_start_bytes_ = std::max(max_allowed_footprint_ - remaining_bytes, bytes_allocated); |
| 1518 | } else { |
| 1519 | // The estimated rate is so high that we should request another GC straight away. |
| 1520 | concurrent_start_bytes_ = bytes_allocated; |
| 1521 | } |
| 1522 | DCHECK_LE(concurrent_start_bytes_, max_allowed_footprint_); |
| 1523 | DCHECK_LE(max_allowed_footprint_, growth_limit_); |
| 1524 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1527 | void Heap::ClearGrowthLimit() { |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 1528 | growth_limit_ = capacity_; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1529 | alloc_space_->ClearGrowthLimit(); |
| 1530 | } |
| 1531 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1532 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1533 | MemberOffset reference_queue_offset, |
| 1534 | MemberOffset reference_queueNext_offset, |
| 1535 | MemberOffset reference_pendingNext_offset, |
| 1536 | MemberOffset finalizer_reference_zombie_offset) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1537 | reference_referent_offset_ = reference_referent_offset; |
| 1538 | reference_queue_offset_ = reference_queue_offset; |
| 1539 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 1540 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 1541 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 1542 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1543 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 1544 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 1545 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 1546 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 1547 | } |
| 1548 | |
| 1549 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 1550 | DCHECK(reference != NULL); |
| 1551 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1552 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 1553 | } |
| 1554 | |
| 1555 | void Heap::ClearReferenceReferent(Object* reference) { |
| 1556 | DCHECK(reference != NULL); |
| 1557 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1558 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 1559 | } |
| 1560 | |
| 1561 | // Returns true if the reference object has not yet been enqueued. |
| 1562 | bool Heap::IsEnqueuable(const Object* ref) { |
| 1563 | DCHECK(ref != NULL); |
| 1564 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 1565 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 1566 | return (queue != NULL) && (queue_next == NULL); |
| 1567 | } |
| 1568 | |
| 1569 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 1570 | DCHECK(ref != NULL); |
| 1571 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 1572 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 1573 | EnqueuePendingReference(ref, cleared_reference_list); |
| 1574 | } |
| 1575 | |
| 1576 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 1577 | DCHECK(ref != NULL); |
| 1578 | DCHECK(list != NULL); |
| 1579 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1580 | // TODO: Remove this lock, use atomic stacks for storing references. |
| 1581 | MutexLock mu(Thread::Current(), *reference_queue_lock_); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1582 | if (*list == NULL) { |
| 1583 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1584 | *list = ref; |
| 1585 | } else { |
| 1586 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1587 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 1588 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | Object* Heap::DequeuePendingReference(Object** list) { |
| 1593 | DCHECK(list != NULL); |
| 1594 | DCHECK(*list != NULL); |
| 1595 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1596 | Object* ref; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1597 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1598 | // Note: the following code is thread-safe because it is only called from ProcessReferences which |
| 1599 | // is single threaded. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1600 | if (*list == head) { |
| 1601 | ref = *list; |
| 1602 | *list = NULL; |
| 1603 | } else { |
| 1604 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 1605 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 1606 | ref = head; |
| 1607 | } |
| 1608 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 1609 | return ref; |
| 1610 | } |
| 1611 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 1612 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1613 | ScopedObjectAccess soa(self); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1614 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1615 | args[0].SetL(object); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1616 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_FinalizerReference_add)->Invoke(self, NULL, args, |
| 1617 | NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | size_t Heap::GetBytesAllocated() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1621 | return num_bytes_allocated_; |
| 1622 | } |
| 1623 | |
| 1624 | size_t Heap::GetObjectsAllocated() const { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1625 | size_t total = 0; |
| 1626 | // TODO: C++0x |
| 1627 | for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 1628 | Space* space = *it; |
| 1629 | if (space->IsAllocSpace()) { |
| 1630 | total += space->AsAllocSpace()->GetNumObjectsAllocated(); |
| 1631 | } |
| 1632 | } |
| 1633 | return total; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | size_t Heap::GetConcurrentStartSize() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1637 | return concurrent_start_size_; |
| 1638 | } |
| 1639 | |
| 1640 | size_t Heap::GetConcurrentMinFree() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1641 | return concurrent_min_free_; |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 1645 | DCHECK(cleared != NULL); |
| 1646 | if (*cleared != NULL) { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 1647 | // When a runtime isn't started there are no reference queues to care about so ignore. |
| 1648 | if (LIKELY(Runtime::Current()->IsStarted())) { |
| 1649 | ScopedObjectAccess soa(Thread::Current()); |
| 1650 | JValue args[1]; |
| 1651 | args[0].SetL(*cleared); |
| 1652 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_ReferenceQueue_add)->Invoke(soa.Self(), NULL, |
| 1653 | args, NULL); |
| 1654 | } |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1655 | *cleared = NULL; |
| 1656 | } |
| 1657 | } |
| 1658 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1659 | void Heap::RequestConcurrentGC(Thread* self) { |
Mathieu Chartier | 069387a | 2012-06-18 12:01:01 -0700 | [diff] [blame] | 1660 | // Make sure that we can do a concurrent GC. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1661 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1662 | DCHECK(concurrent_gc_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1663 | if (runtime == NULL || !runtime->IsFinishedStarting() || |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1664 | !runtime->IsConcurrentGcEnabled()) { |
| 1665 | return; |
| 1666 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1667 | { |
| 1668 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 1669 | if (runtime->IsShuttingDown()) { |
| 1670 | return; |
| 1671 | } |
| 1672 | } |
| 1673 | if (self->IsHandlingStackOverflow()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1674 | return; |
| 1675 | } |
| 1676 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1677 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1678 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1679 | DCHECK(WellKnownClasses::java_lang_Daemons_requestGC != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1680 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 1681 | WellKnownClasses::java_lang_Daemons_requestGC); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1682 | CHECK(!env->ExceptionCheck()); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1685 | void Heap::ConcurrentGC(Thread* self) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1686 | { |
| 1687 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1688 | if (Runtime::Current()->IsShuttingDown()) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1689 | return; |
| 1690 | } |
Mathieu Chartier | 2542d66 | 2012-06-21 17:14:11 -0700 | [diff] [blame] | 1691 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1692 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1693 | // Wait for any GCs currently running to finish. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1694 | if (WaitForConcurrentGcToComplete(self) == kGcTypeNone) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1695 | if (alloc_space_->Size() > min_alloc_space_size_for_sticky_gc_) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1696 | CollectGarbageInternal(kGcTypeSticky, kGcCauseBackground, false); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1697 | } else { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1698 | CollectGarbageInternal(kGcTypePartial, kGcCauseBackground, false); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1699 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1700 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
Mathieu Chartier | 3056d0c | 2012-10-19 10:49:56 -0700 | [diff] [blame] | 1703 | void Heap::Trim() { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1704 | alloc_space_->Trim(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1707 | void Heap::RequestHeapTrim() { |
| 1708 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 1709 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 1710 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 1711 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 1712 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 1713 | // not how much use we're making of those pages. |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1714 | uint64_t ms_time = NsToMs(NanoTime()); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1715 | float utilization = |
| 1716 | static_cast<float>(alloc_space_->GetNumBytesAllocated()) / alloc_space_->Size(); |
| 1717 | if ((utilization > 0.75f) || ((ms_time - last_trim_time_) < 2 * 1000)) { |
| 1718 | // Don't bother trimming the alloc space if it's more than 75% utilized, or if a |
| 1719 | // heap trim occurred in the last two seconds. |
| 1720 | return; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1721 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1722 | |
| 1723 | Thread* self = Thread::Current(); |
| 1724 | { |
| 1725 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 1726 | Runtime* runtime = Runtime::Current(); |
| 1727 | if (runtime == NULL || !runtime->IsFinishedStarting() || runtime->IsShuttingDown()) { |
| 1728 | // Heap trimming isn't supported without a Java runtime or Daemons (such as at dex2oat time) |
| 1729 | // Also: we do not wish to start a heap trim if the runtime is shutting down (a racy check |
| 1730 | // as we don't hold the lock while requesting the trim). |
| 1731 | return; |
| 1732 | } |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 1733 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1734 | last_trim_time_ = ms_time; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1735 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1736 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1737 | DCHECK(WellKnownClasses::java_lang_Daemons_requestHeapTrim != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1738 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 1739 | WellKnownClasses::java_lang_Daemons_requestHeapTrim); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1740 | CHECK(!env->ExceptionCheck()); |
| 1741 | } |
| 1742 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1743 | } // namespace art |