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