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