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