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