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