Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "heap.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 19 | #include <limits> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 21 | #include <unwind.h> // For GC verification. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 25 | #include "base/allocator.h" |
Mathieu Chartier | 8d44725 | 2015-10-26 10:21:14 -0700 | [diff] [blame] | 26 | #include "base/arena_allocator.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 27 | #include "base/dumpable.h" |
Mathieu Chartier | b2f9936 | 2013-11-20 17:26:00 -0800 | [diff] [blame] | 28 | #include "base/histogram-inl.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 30 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 31 | #include "base/time_utils.h" |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 32 | #include "common_throws.h" |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 33 | #include "cutils/sched_policy.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 34 | #include "debugger.h" |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 35 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | #include "gc/accounting/atomic_stack.h" |
| 37 | #include "gc/accounting/card_table-inl.h" |
| 38 | #include "gc/accounting/heap_bitmap-inl.h" |
| 39 | #include "gc/accounting/mod_union_table-inl.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 40 | #include "gc/accounting/remembered_set.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 41 | #include "gc/accounting/space_bitmap-inl.h" |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 42 | #include "gc/collector/concurrent_copying.h" |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 43 | #include "gc/collector/mark_compact.h" |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 44 | #include "gc/collector/mark_sweep.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 45 | #include "gc/collector/partial_mark_sweep.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 46 | #include "gc/collector/semi_space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 47 | #include "gc/collector/sticky_mark_sweep.h" |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 48 | #include "gc/reference_processor.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 49 | #include "gc/space/bump_pointer_space.h" |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 50 | #include "gc/space/dlmalloc_space-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 51 | #include "gc/space/image_space.h" |
| 52 | #include "gc/space/large_object_space.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 53 | #include "gc/space/region_space.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 54 | #include "gc/space/rosalloc_space-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 55 | #include "gc/space/space-inl.h" |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 56 | #include "gc/space/zygote_space.h" |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 57 | #include "gc/task_processor.h" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 58 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 59 | #include "heap-inl.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 60 | #include "image.h" |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 61 | #include "intern_table.h" |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 62 | #include "jit/jit.h" |
| 63 | #include "jit/jit_code_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | #include "mirror/object-inl.h" |
| 66 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 67 | #include "mirror/reference-inl.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 68 | #include "os.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 69 | #include "reflection.h" |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 70 | #include "runtime.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 71 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 72 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 73 | #include "handle_scope-inl.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 74 | #include "thread_list.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 75 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 76 | |
| 77 | namespace art { |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 78 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 79 | namespace gc { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 80 | |
Mathieu Chartier | 91e3063 | 2014-03-25 15:58:50 -0700 | [diff] [blame] | 81 | static constexpr size_t kCollectorTransitionStressIterations = 0; |
| 82 | static constexpr size_t kCollectorTransitionStressWait = 10 * 1000; // Microseconds |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 83 | // Minimum amount of remaining bytes before a concurrent GC is triggered. |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 84 | static constexpr size_t kMinConcurrentRemainingBytes = 128 * KB; |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 85 | static constexpr size_t kMaxConcurrentRemainingBytes = 512 * KB; |
Mathieu Chartier | df86d1f | 2014-04-08 13:44:04 -0700 | [diff] [blame] | 86 | // Sticky GC throughput adjustment, divided by 4. Increasing this causes sticky GC to occur more |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 87 | // relative to partial/full GC. This may be desirable since sticky GCs interfere less with mutator |
Mathieu Chartier | df86d1f | 2014-04-08 13:44:04 -0700 | [diff] [blame] | 88 | // threads (lower pauses, use less memory bandwidth). |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 89 | static constexpr double kStickyGcThroughputAdjustment = 1.0; |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 90 | // Whether or not we compact the zygote in PreZygoteFork. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 91 | static constexpr bool kCompactZygote = kMovingCollector; |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 92 | // How many reserve entries are at the end of the allocation stack, these are only needed if the |
| 93 | // allocation stack overflows. |
| 94 | static constexpr size_t kAllocationStackReserveSize = 1024; |
| 95 | // Default mark stack size in bytes. |
| 96 | static const size_t kDefaultMarkStackSize = 64 * KB; |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 97 | // Define space name. |
| 98 | static const char* kDlMallocSpaceName[2] = {"main dlmalloc space", "main dlmalloc space 1"}; |
| 99 | static const char* kRosAllocSpaceName[2] = {"main rosalloc space", "main rosalloc space 1"}; |
| 100 | static const char* kMemMapSpaceName[2] = {"main space", "main space 1"}; |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 101 | static const char* kNonMovingSpaceName = "non moving space"; |
| 102 | static const char* kZygoteSpaceName = "zygote space"; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 103 | static constexpr size_t kGSSBumpPointerSpaceCapacity = 32 * MB; |
Mathieu Chartier | 95a505c | 2014-12-10 18:45:30 -0800 | [diff] [blame] | 104 | static constexpr bool kGCALotMode = false; |
| 105 | // GC alot mode uses a small allocation stack to stress test a lot of GC. |
| 106 | static constexpr size_t kGcAlotAllocationStackSize = 4 * KB / |
| 107 | sizeof(mirror::HeapReference<mirror::Object>); |
| 108 | // Verify objet has a small allocation stack size since searching the allocation stack is slow. |
| 109 | static constexpr size_t kVerifyObjectAllocationStackSize = 16 * KB / |
| 110 | sizeof(mirror::HeapReference<mirror::Object>); |
| 111 | static constexpr size_t kDefaultAllocationStackSize = 8 * MB / |
| 112 | sizeof(mirror::HeapReference<mirror::Object>); |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 113 | // System.runFinalization can deadlock with native allocations, to deal with this, we have a |
| 114 | // timeout on how long we wait for finalizers to run. b/21544853 |
| 115 | static constexpr uint64_t kNativeAllocationFinalizeTimeout = MsToNs(250u); |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 116 | |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 117 | // For deterministic compilation, we need the heap to be at a well-known address. |
| 118 | static constexpr uint32_t kAllocSpaceBeginForDeterministicAoT = 0x40000000; |
Hiroshi Yamauchi | b62f2e6 | 2016-03-23 15:51:24 -0700 | [diff] [blame] | 119 | // Dump the rosalloc stats on SIGQUIT. |
| 120 | static constexpr bool kDumpRosAllocStatsOnSigQuit = false; |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 121 | |
Mathieu Chartier | 5d2a3f7 | 2016-05-11 11:35:39 -0700 | [diff] [blame] | 122 | static constexpr size_t kNativeAllocationHistogramBuckets = 16; |
| 123 | |
Hiroshi Yamauchi | b6bab0f | 2016-07-18 17:07:26 -0700 | [diff] [blame] | 124 | // Extra added to the heap growth multiplier. Used to adjust the GC ergonomics for the read barrier |
| 125 | // config. |
| 126 | static constexpr double kExtraHeapGrowthMultiplier = kUseReadBarrier ? 1.0 : 0.0; |
| 127 | |
Mathieu Chartier | f8cb178 | 2016-03-18 18:45:41 -0700 | [diff] [blame] | 128 | static inline bool CareAboutPauseTimes() { |
| 129 | return Runtime::Current()->InJankPerceptibleProcessState(); |
| 130 | } |
| 131 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 132 | Heap::Heap(size_t initial_size, |
| 133 | size_t growth_limit, |
| 134 | size_t min_free, |
| 135 | size_t max_free, |
| 136 | double target_utilization, |
| 137 | double foreground_heap_growth_multiplier, |
| 138 | size_t capacity, |
| 139 | size_t non_moving_space_capacity, |
| 140 | const std::string& image_file_name, |
| 141 | const InstructionSet image_instruction_set, |
| 142 | CollectorType foreground_collector_type, |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 143 | CollectorType background_collector_type, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 144 | space::LargeObjectSpaceType large_object_space_type, |
| 145 | size_t large_object_threshold, |
| 146 | size_t parallel_gc_threads, |
| 147 | size_t conc_gc_threads, |
| 148 | bool low_memory_mode, |
| 149 | size_t long_pause_log_threshold, |
| 150 | size_t long_gc_log_threshold, |
| 151 | bool ignore_max_footprint, |
| 152 | bool use_tlab, |
| 153 | bool verify_pre_gc_heap, |
| 154 | bool verify_pre_sweeping_heap, |
| 155 | bool verify_post_gc_heap, |
| 156 | bool verify_pre_gc_rosalloc, |
| 157 | bool verify_pre_sweeping_rosalloc, |
| 158 | bool verify_post_gc_rosalloc, |
| 159 | bool gc_stress_mode, |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame^] | 160 | bool measure_gc_performance, |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 161 | bool use_homogeneous_space_compaction_for_oom, |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 162 | uint64_t min_interval_homogeneous_space_compaction_by_oom) |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 163 | : non_moving_space_(nullptr), |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 164 | rosalloc_space_(nullptr), |
| 165 | dlmalloc_space_(nullptr), |
Mathieu Chartier | fc5b528 | 2014-01-09 16:15:36 -0800 | [diff] [blame] | 166 | main_space_(nullptr), |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 167 | collector_type_(kCollectorTypeNone), |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 168 | foreground_collector_type_(foreground_collector_type), |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 169 | background_collector_type_(background_collector_type), |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 170 | desired_collector_type_(foreground_collector_type_), |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 171 | pending_task_lock_(nullptr), |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 172 | parallel_gc_threads_(parallel_gc_threads), |
| 173 | conc_gc_threads_(conc_gc_threads), |
Mathieu Chartier | e0a53e9 | 2013-08-05 10:17:40 -0700 | [diff] [blame] | 174 | low_memory_mode_(low_memory_mode), |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 175 | long_pause_log_threshold_(long_pause_log_threshold), |
| 176 | long_gc_log_threshold_(long_gc_log_threshold), |
| 177 | ignore_max_footprint_(ignore_max_footprint), |
Mathieu Chartier | 8e4a96d | 2014-05-21 10:44:32 -0700 | [diff] [blame] | 178 | zygote_creation_lock_("zygote creation lock", kZygoteCreationLock), |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 179 | zygote_space_(nullptr), |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 180 | large_object_threshold_(large_object_threshold), |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 181 | disable_thread_flip_count_(0), |
| 182 | thread_flip_running_(false), |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 183 | collector_type_running_(kCollectorTypeNone), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 184 | last_gc_type_(collector::kGcTypeNone), |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 185 | next_gc_type_(collector::kGcTypePartial), |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 186 | capacity_(capacity), |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 187 | growth_limit_(growth_limit), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 188 | max_allowed_footprint_(initial_size), |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 189 | native_footprint_gc_watermark_(initial_size), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 190 | native_need_to_run_finalization_(false), |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 191 | concurrent_start_bytes_(std::numeric_limits<size_t>::max()), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 192 | total_bytes_freed_ever_(0), |
| 193 | total_objects_freed_ever_(0), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 194 | num_bytes_allocated_(0), |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 195 | native_bytes_allocated_(0), |
Mathieu Chartier | 5d2a3f7 | 2016-05-11 11:35:39 -0700 | [diff] [blame] | 196 | native_histogram_lock_("Native allocation lock"), |
| 197 | native_allocation_histogram_("Native allocation sizes", |
| 198 | 1U, |
| 199 | kNativeAllocationHistogramBuckets), |
| 200 | native_free_histogram_("Native free sizes", 1U, kNativeAllocationHistogramBuckets), |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 201 | num_bytes_freed_revoke_(0), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 202 | verify_missing_card_marks_(false), |
| 203 | verify_system_weaks_(false), |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 204 | verify_pre_gc_heap_(verify_pre_gc_heap), |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 205 | verify_pre_sweeping_heap_(verify_pre_sweeping_heap), |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 206 | verify_post_gc_heap_(verify_post_gc_heap), |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 207 | verify_mod_union_table_(false), |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 208 | verify_pre_gc_rosalloc_(verify_pre_gc_rosalloc), |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 209 | verify_pre_sweeping_rosalloc_(verify_pre_sweeping_rosalloc), |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 210 | verify_post_gc_rosalloc_(verify_post_gc_rosalloc), |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 211 | gc_stress_mode_(gc_stress_mode), |
Mathieu Chartier | 0418ae2 | 2013-07-31 13:35:46 -0700 | [diff] [blame] | 212 | /* For GC a lot mode, we limit the allocations stacks to be kGcAlotInterval allocations. This |
| 213 | * causes a lot of GC since we do a GC for alloc whenever the stack is full. When heap |
| 214 | * verification is enabled, we limit the size of allocation stacks to speed up their |
| 215 | * searching. |
| 216 | */ |
Mathieu Chartier | 95a505c | 2014-12-10 18:45:30 -0800 | [diff] [blame] | 217 | max_allocation_stack_size_(kGCALotMode ? kGcAlotAllocationStackSize |
| 218 | : (kVerifyObjectSupport > kVerifyObjectModeFast) ? kVerifyObjectAllocationStackSize : |
| 219 | kDefaultAllocationStackSize), |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 220 | current_allocator_(kAllocatorTypeDlMalloc), |
| 221 | current_non_moving_allocator_(kAllocatorTypeNonMoving), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 222 | bump_pointer_space_(nullptr), |
| 223 | temp_space_(nullptr), |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 224 | region_space_(nullptr), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 225 | min_free_(min_free), |
| 226 | max_free_(max_free), |
| 227 | target_utilization_(target_utilization), |
Hiroshi Yamauchi | b6bab0f | 2016-07-18 17:07:26 -0700 | [diff] [blame] | 228 | foreground_heap_growth_multiplier_( |
| 229 | foreground_heap_growth_multiplier + kExtraHeapGrowthMultiplier), |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 230 | total_wait_time_(0), |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 231 | verify_object_mode_(kVerifyObjectModeDisabled), |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 232 | disable_moving_gc_count_(0), |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 233 | is_running_on_memory_tool_(Runtime::Current()->IsRunningOnMemoryTool()), |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 234 | use_tlab_(use_tlab), |
| 235 | main_space_backup_(nullptr), |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 236 | min_interval_homogeneous_space_compaction_by_oom_( |
| 237 | min_interval_homogeneous_space_compaction_by_oom), |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 238 | last_time_homogeneous_space_compaction_by_oom_(NanoTime()), |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 239 | pending_collector_transition_(nullptr), |
| 240 | pending_heap_trim_(nullptr), |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 241 | use_homogeneous_space_compaction_for_oom_(use_homogeneous_space_compaction_for_oom), |
| 242 | running_collection_is_blocking_(false), |
| 243 | blocking_gc_count_(0U), |
| 244 | blocking_gc_time_(0U), |
| 245 | last_update_time_gc_count_rate_histograms_( // Round down by the window duration. |
| 246 | (NanoTime() / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration), |
| 247 | gc_count_last_window_(0U), |
| 248 | blocking_gc_count_last_window_(0U), |
| 249 | gc_count_rate_histogram_("gc count rate histogram", 1U, kGcCountRateMaxBucketCount), |
| 250 | blocking_gc_count_rate_histogram_("blocking gc count rate histogram", 1U, |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 251 | kGcCountRateMaxBucketCount), |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 252 | alloc_tracking_enabled_(false), |
| 253 | backtrace_lock_(nullptr), |
| 254 | seen_backtrace_count_(0u), |
Mathieu Chartier | 5116837 | 2015-08-12 16:40:32 -0700 | [diff] [blame] | 255 | unique_backtrace_count_(0u), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 256 | gc_disabled_for_shutdown_(false) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 257 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 258 | LOG(INFO) << "Heap() entering"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 259 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 260 | ScopedTrace trace(__FUNCTION__); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 261 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 262 | // If we aren't the zygote, switch to the default non zygote allocator. This may update the |
| 263 | // entrypoints. |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 264 | const bool is_zygote = runtime->IsZygote(); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 265 | if (!is_zygote) { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 266 | // Background compaction is currently not supported for command line runs. |
| 267 | if (background_collector_type_ != foreground_collector_type_) { |
Mathieu Chartier | 52ba199 | 2014-05-07 14:39:21 -0700 | [diff] [blame] | 268 | VLOG(heap) << "Disabling background compaction for non zygote"; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 269 | background_collector_type_ = foreground_collector_type_; |
Mathieu Chartier | bd0a653 | 2014-02-27 11:14:21 -0800 | [diff] [blame] | 270 | } |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 271 | } |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 272 | ChangeCollector(desired_collector_type_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 273 | live_bitmap_.reset(new accounting::HeapBitmap(this)); |
| 274 | mark_bitmap_.reset(new accounting::HeapBitmap(this)); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 275 | // Requested begin for the alloc space, to follow the mapped image and oat files |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 276 | uint8_t* requested_alloc_space_begin = nullptr; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 277 | if (foreground_collector_type_ == kCollectorTypeCC) { |
| 278 | // Need to use a low address so that we can allocate a contiguous |
| 279 | // 2 * Xmx space when there's no image (dex2oat for target). |
| 280 | CHECK_GE(300 * MB, non_moving_space_capacity); |
| 281 | requested_alloc_space_begin = reinterpret_cast<uint8_t*>(300 * MB) - non_moving_space_capacity; |
| 282 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 283 | |
| 284 | // Load image space(s). |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 285 | if (!image_file_name.empty()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 286 | // For code reuse, handle this like a work queue. |
| 287 | std::vector<std::string> image_file_names; |
| 288 | image_file_names.push_back(image_file_name); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 289 | // The loaded spaces. Secondary images may fail to load, in which case we need to remove |
| 290 | // already added spaces. |
| 291 | std::vector<space::Space*> added_image_spaces; |
Mathieu Chartier | 582b68f | 2016-02-04 09:50:22 -0800 | [diff] [blame] | 292 | uint8_t* const original_requested_alloc_space_begin = requested_alloc_space_begin; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 293 | for (size_t index = 0; index < image_file_names.size(); ++index) { |
| 294 | std::string& image_name = image_file_names[index]; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 295 | std::string error_msg; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 296 | space::ImageSpace* boot_image_space = space::ImageSpace::CreateBootImage( |
| 297 | image_name.c_str(), |
| 298 | image_instruction_set, |
| 299 | index > 0, |
| 300 | &error_msg); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 301 | if (boot_image_space != nullptr) { |
| 302 | AddSpace(boot_image_space); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 303 | added_image_spaces.push_back(boot_image_space); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 304 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 305 | // isn't going to get in the middle |
| 306 | uint8_t* oat_file_end_addr = boot_image_space->GetImageHeader().GetOatFileEnd(); |
| 307 | CHECK_GT(oat_file_end_addr, boot_image_space->End()); |
| 308 | requested_alloc_space_begin = AlignUp(oat_file_end_addr, kPageSize); |
| 309 | boot_image_spaces_.push_back(boot_image_space); |
| 310 | |
| 311 | if (index == 0) { |
| 312 | // If this was the first space, check whether there are more images to load. |
| 313 | const OatFile* boot_oat_file = boot_image_space->GetOatFile(); |
| 314 | if (boot_oat_file == nullptr) { |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | const OatHeader& boot_oat_header = boot_oat_file->GetOatHeader(); |
| 319 | const char* boot_classpath = |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 320 | boot_oat_header.GetStoreValueByKey(OatHeader::kBootClassPathKey); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 321 | if (boot_classpath == nullptr) { |
| 322 | continue; |
| 323 | } |
| 324 | |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 325 | space::ImageSpace::CreateMultiImageLocations(image_file_name, |
| 326 | boot_classpath, |
| 327 | &image_file_names); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 328 | } |
| 329 | } else { |
| 330 | LOG(ERROR) << "Could not create image space with image file '" << image_file_name << "'. " |
| 331 | << "Attempting to fall back to imageless running. Error was: " << error_msg |
| 332 | << "\nAttempted image: " << image_name; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 333 | // Remove already loaded spaces. |
| 334 | for (space::Space* loaded_space : added_image_spaces) { |
| 335 | RemoveSpace(loaded_space); |
Mathieu Chartier | b08f305 | 2016-02-02 17:24:39 -0800 | [diff] [blame] | 336 | delete loaded_space; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 337 | } |
Mathieu Chartier | b08f305 | 2016-02-02 17:24:39 -0800 | [diff] [blame] | 338 | boot_image_spaces_.clear(); |
Mathieu Chartier | 582b68f | 2016-02-04 09:50:22 -0800 | [diff] [blame] | 339 | requested_alloc_space_begin = original_requested_alloc_space_begin; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 340 | break; |
| 341 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 342 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 343 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 344 | /* |
| 345 | requested_alloc_space_begin -> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 346 | +- nonmoving space (non_moving_space_capacity)+- |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 347 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 348 | +-????????????????????????????????????????????+- |
| 349 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 350 | +-main alloc space / bump space 1 (capacity_) +- |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 351 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 352 | +-????????????????????????????????????????????+- |
| 353 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
| 354 | +-main alloc space2 / bump space 2 (capacity_)+- |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 355 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
| 356 | */ |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 357 | // We don't have hspace compaction enabled with GSS or CC. |
| 358 | if (foreground_collector_type_ == kCollectorTypeGSS || |
| 359 | foreground_collector_type_ == kCollectorTypeCC) { |
Hiroshi Yamauchi | 20ed5af | 2014-11-17 18:05:44 -0800 | [diff] [blame] | 360 | use_homogeneous_space_compaction_for_oom_ = false; |
| 361 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 362 | bool support_homogeneous_space_compaction = |
Mathieu Chartier | 0deeb81 | 2014-08-21 18:28:20 -0700 | [diff] [blame] | 363 | background_collector_type_ == gc::kCollectorTypeHomogeneousSpaceCompact || |
Hiroshi Yamauchi | 20ed5af | 2014-11-17 18:05:44 -0800 | [diff] [blame] | 364 | use_homogeneous_space_compaction_for_oom_; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 365 | // We may use the same space the main space for the non moving space if we don't need to compact |
| 366 | // from the main space. |
| 367 | // This is not the case if we support homogeneous compaction or have a moving background |
| 368 | // collector type. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 369 | bool separate_non_moving_space = is_zygote || |
| 370 | support_homogeneous_space_compaction || IsMovingGc(foreground_collector_type_) || |
| 371 | IsMovingGc(background_collector_type_); |
Mathieu Chartier | 76ce917 | 2016-01-27 10:44:20 -0800 | [diff] [blame] | 372 | if (foreground_collector_type_ == kCollectorTypeGSS) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 373 | separate_non_moving_space = false; |
| 374 | } |
| 375 | std::unique_ptr<MemMap> main_mem_map_1; |
| 376 | std::unique_ptr<MemMap> main_mem_map_2; |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 377 | |
| 378 | // Gross hack to make dex2oat deterministic. |
Mathieu Chartier | c68e77b | 2016-01-28 09:49:55 -0800 | [diff] [blame] | 379 | if (foreground_collector_type_ == kCollectorTypeMS && |
| 380 | requested_alloc_space_begin == nullptr && |
| 381 | Runtime::Current()->IsAotCompiler()) { |
| 382 | // Currently only enabled for MS collector since that is what the deterministic dex2oat uses. |
| 383 | // b/26849108 |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 384 | requested_alloc_space_begin = reinterpret_cast<uint8_t*>(kAllocSpaceBeginForDeterministicAoT); |
| 385 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 386 | uint8_t* request_begin = requested_alloc_space_begin; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 387 | if (request_begin != nullptr && separate_non_moving_space) { |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 388 | request_begin += non_moving_space_capacity; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 389 | } |
| 390 | std::string error_str; |
| 391 | std::unique_ptr<MemMap> non_moving_space_mem_map; |
| 392 | if (separate_non_moving_space) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 393 | ScopedTrace trace2("Create separate non moving space"); |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 394 | // If we are the zygote, the non moving space becomes the zygote space when we run |
| 395 | // PreZygoteFork the first time. In this case, call the map "zygote space" since we can't |
| 396 | // rename the mem map later. |
| 397 | const char* space_name = is_zygote ? kZygoteSpaceName: kNonMovingSpaceName; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 398 | // Reserve the non moving mem map before the other two since it needs to be at a specific |
| 399 | // address. |
| 400 | non_moving_space_mem_map.reset( |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 401 | MemMap::MapAnonymous(space_name, requested_alloc_space_begin, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 402 | non_moving_space_capacity, PROT_READ | PROT_WRITE, true, false, |
| 403 | &error_str)); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 404 | CHECK(non_moving_space_mem_map != nullptr) << error_str; |
Mathieu Chartier | c44ce2e | 2014-08-25 16:32:41 -0700 | [diff] [blame] | 405 | // Try to reserve virtual memory at a lower address if we have a separate non moving space. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 406 | request_begin = reinterpret_cast<uint8_t*>(300 * MB); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 407 | } |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 408 | // Attempt to create 2 mem maps at or after the requested begin. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 409 | if (foreground_collector_type_ != kCollectorTypeCC) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 410 | ScopedTrace trace2("Create main mem map"); |
Mathieu Chartier | 966f533 | 2016-01-25 12:53:03 -0800 | [diff] [blame] | 411 | if (separate_non_moving_space || !is_zygote) { |
| 412 | main_mem_map_1.reset(MapAnonymousPreferredAddress(kMemMapSpaceName[0], |
| 413 | request_begin, |
| 414 | capacity_, |
| 415 | &error_str)); |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 416 | } else { |
Mathieu Chartier | 966f533 | 2016-01-25 12:53:03 -0800 | [diff] [blame] | 417 | // If no separate non-moving space and we are the zygote, the main space must come right |
| 418 | // after the image space to avoid a gap. This is required since we want the zygote space to |
| 419 | // be adjacent to the image space. |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 420 | main_mem_map_1.reset(MemMap::MapAnonymous(kMemMapSpaceName[0], request_begin, capacity_, |
| 421 | PROT_READ | PROT_WRITE, true, false, |
| 422 | &error_str)); |
| 423 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 424 | CHECK(main_mem_map_1.get() != nullptr) << error_str; |
| 425 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 426 | if (support_homogeneous_space_compaction || |
| 427 | background_collector_type_ == kCollectorTypeSS || |
| 428 | foreground_collector_type_ == kCollectorTypeSS) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 429 | ScopedTrace trace2("Create main mem map 2"); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 430 | main_mem_map_2.reset(MapAnonymousPreferredAddress(kMemMapSpaceName[1], main_mem_map_1->End(), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 431 | capacity_, &error_str)); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 432 | CHECK(main_mem_map_2.get() != nullptr) << error_str; |
| 433 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 434 | |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 435 | // Create the non moving space first so that bitmaps don't take up the address range. |
| 436 | if (separate_non_moving_space) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 437 | ScopedTrace trace2("Add non moving space"); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 438 | // Non moving space is always dlmalloc since we currently don't have support for multiple |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 439 | // active rosalloc spaces. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 440 | const size_t size = non_moving_space_mem_map->Size(); |
| 441 | non_moving_space_ = space::DlMallocSpace::CreateFromMemMap( |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 442 | non_moving_space_mem_map.release(), "zygote / non moving space", kDefaultStartingSize, |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 443 | initial_size, size, size, false); |
Mathieu Chartier | 7840888 | 2014-04-11 18:06:01 -0700 | [diff] [blame] | 444 | non_moving_space_->SetFootprintLimit(non_moving_space_->Capacity()); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 445 | CHECK(non_moving_space_ != nullptr) << "Failed creating non moving space " |
| 446 | << requested_alloc_space_begin; |
| 447 | AddSpace(non_moving_space_); |
| 448 | } |
| 449 | // Create other spaces based on whether or not we have a moving GC. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 450 | if (foreground_collector_type_ == kCollectorTypeCC) { |
Hiroshi Yamauchi | 4799530 | 2016-06-10 14:59:43 -0700 | [diff] [blame] | 451 | region_space_ = space::RegionSpace::Create("main space (region space)", capacity_ * 2, request_begin); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 452 | AddSpace(region_space_); |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 453 | } else if (IsMovingGc(foreground_collector_type_) && |
| 454 | foreground_collector_type_ != kCollectorTypeGSS) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 455 | // Create bump pointer spaces. |
| 456 | // We only to create the bump pointer if the foreground collector is a compacting GC. |
| 457 | // TODO: Place bump-pointer spaces somewhere to minimize size of card table. |
| 458 | bump_pointer_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 1", |
| 459 | main_mem_map_1.release()); |
| 460 | CHECK(bump_pointer_space_ != nullptr) << "Failed to create bump pointer space"; |
| 461 | AddSpace(bump_pointer_space_); |
| 462 | temp_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 2", |
| 463 | main_mem_map_2.release()); |
| 464 | CHECK(temp_space_ != nullptr) << "Failed to create bump pointer space"; |
| 465 | AddSpace(temp_space_); |
| 466 | CHECK(separate_non_moving_space); |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 467 | } else { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 468 | CreateMainMallocSpace(main_mem_map_1.release(), initial_size, growth_limit_, capacity_); |
| 469 | CHECK(main_space_ != nullptr); |
| 470 | AddSpace(main_space_); |
| 471 | if (!separate_non_moving_space) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 472 | non_moving_space_ = main_space_; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 473 | CHECK(!non_moving_space_->CanMoveObjects()); |
| 474 | } |
| 475 | if (foreground_collector_type_ == kCollectorTypeGSS) { |
| 476 | CHECK_EQ(foreground_collector_type_, background_collector_type_); |
| 477 | // Create bump pointer spaces instead of a backup space. |
| 478 | main_mem_map_2.release(); |
| 479 | bump_pointer_space_ = space::BumpPointerSpace::Create("Bump pointer space 1", |
| 480 | kGSSBumpPointerSpaceCapacity, nullptr); |
| 481 | CHECK(bump_pointer_space_ != nullptr); |
| 482 | AddSpace(bump_pointer_space_); |
| 483 | temp_space_ = space::BumpPointerSpace::Create("Bump pointer space 2", |
| 484 | kGSSBumpPointerSpaceCapacity, nullptr); |
| 485 | CHECK(temp_space_ != nullptr); |
| 486 | AddSpace(temp_space_); |
| 487 | } else if (main_mem_map_2.get() != nullptr) { |
| 488 | const char* name = kUseRosAlloc ? kRosAllocSpaceName[1] : kDlMallocSpaceName[1]; |
| 489 | main_space_backup_.reset(CreateMallocSpaceFromMemMap(main_mem_map_2.release(), initial_size, |
| 490 | growth_limit_, capacity_, name, true)); |
| 491 | CHECK(main_space_backup_.get() != nullptr); |
| 492 | // Add the space so its accounted for in the heap_begin and heap_end. |
| 493 | AddSpace(main_space_backup_.get()); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 494 | } |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 495 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 496 | CHECK(non_moving_space_ != nullptr); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 497 | CHECK(!non_moving_space_->CanMoveObjects()); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 498 | // Allocate the large object space. |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 499 | if (large_object_space_type == space::LargeObjectSpaceType::kFreeList) { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 500 | large_object_space_ = space::FreeListSpace::Create("free list large object space", nullptr, |
| 501 | capacity_); |
| 502 | CHECK(large_object_space_ != nullptr) << "Failed to create large object space"; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 503 | } else if (large_object_space_type == space::LargeObjectSpaceType::kMap) { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 504 | large_object_space_ = space::LargeObjectMapSpace::Create("mem map large object space"); |
| 505 | CHECK(large_object_space_ != nullptr) << "Failed to create large object space"; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 506 | } else { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 507 | // Disable the large object space by making the cutoff excessively large. |
| 508 | large_object_threshold_ = std::numeric_limits<size_t>::max(); |
| 509 | large_object_space_ = nullptr; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 510 | } |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 511 | if (large_object_space_ != nullptr) { |
| 512 | AddSpace(large_object_space_); |
| 513 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 514 | // Compute heap capacity. Continuous spaces are sorted in order of Begin(). |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 515 | CHECK(!continuous_spaces_.empty()); |
| 516 | // Relies on the spaces being sorted. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 517 | uint8_t* heap_begin = continuous_spaces_.front()->Begin(); |
| 518 | uint8_t* heap_end = continuous_spaces_.back()->Limit(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 519 | size_t heap_capacity = heap_end - heap_begin; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 520 | // Remove the main backup space since it slows down the GC to have unused extra spaces. |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 521 | // TODO: Avoid needing to do this. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 522 | if (main_space_backup_.get() != nullptr) { |
| 523 | RemoveSpace(main_space_backup_.get()); |
| 524 | } |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 525 | // Allocate the card table. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 526 | // We currently don't support dynamically resizing the card table. |
| 527 | // Since we don't know where in the low_4gb the app image will be located, make the card table |
| 528 | // cover the whole low_4gb. TODO: Extend the card table in AddSpace. |
| 529 | UNUSED(heap_capacity); |
| 530 | // Start at 64 KB, we can be sure there are no spaces mapped this low since the address range is |
| 531 | // reserved by the kernel. |
| 532 | static constexpr size_t kMinHeapAddress = 4 * KB; |
| 533 | card_table_.reset(accounting::CardTable::Create(reinterpret_cast<uint8_t*>(kMinHeapAddress), |
| 534 | 4 * GB - kMinHeapAddress)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 535 | CHECK(card_table_.get() != nullptr) << "Failed to create card table"; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 536 | if (foreground_collector_type_ == kCollectorTypeCC && kUseTableLookupReadBarrier) { |
| 537 | rb_table_.reset(new accounting::ReadBarrierTable()); |
| 538 | DCHECK(rb_table_->IsAllCleared()); |
| 539 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 540 | if (HasBootImageSpace()) { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 541 | // Don't add the image mod union table if we are running without an image, this can crash if |
| 542 | // we use the CardCache implementation. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 543 | for (space::ImageSpace* image_space : GetBootImageSpaces()) { |
| 544 | accounting::ModUnionTable* mod_union_table = new accounting::ModUnionTableToZygoteAllocspace( |
| 545 | "Image mod-union table", this, image_space); |
| 546 | CHECK(mod_union_table != nullptr) << "Failed to create image mod-union table"; |
| 547 | AddModUnionTable(mod_union_table); |
| 548 | } |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 549 | } |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 550 | if (collector::SemiSpace::kUseRememberedSet && non_moving_space_ != main_space_) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 551 | accounting::RememberedSet* non_moving_space_rem_set = |
| 552 | new accounting::RememberedSet("Non-moving space remembered set", this, non_moving_space_); |
| 553 | CHECK(non_moving_space_rem_set != nullptr) << "Failed to create non-moving space remembered set"; |
| 554 | AddRememberedSet(non_moving_space_rem_set); |
| 555 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 556 | // TODO: Count objects in the image space here? |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 557 | num_bytes_allocated_.StoreRelaxed(0); |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 558 | mark_stack_.reset(accounting::ObjectStack::Create("mark stack", kDefaultMarkStackSize, |
| 559 | kDefaultMarkStackSize)); |
| 560 | const size_t alloc_stack_capacity = max_allocation_stack_size_ + kAllocationStackReserveSize; |
| 561 | allocation_stack_.reset(accounting::ObjectStack::Create( |
| 562 | "allocation stack", max_allocation_stack_size_, alloc_stack_capacity)); |
| 563 | live_stack_.reset(accounting::ObjectStack::Create( |
| 564 | "live stack", max_allocation_stack_size_, alloc_stack_capacity)); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 565 | // It's still too early to take a lock because there are no threads yet, but we can create locks |
| 566 | // now. We don't create it earlier to make it clear that you can't use locks during heap |
| 567 | // initialization. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 568 | gc_complete_lock_ = new Mutex("GC complete lock"); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 569 | gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable", |
| 570 | *gc_complete_lock_)); |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 571 | thread_flip_lock_ = new Mutex("GC thread flip lock"); |
| 572 | thread_flip_cond_.reset(new ConditionVariable("GC thread flip condition variable", |
| 573 | *thread_flip_lock_)); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 574 | task_processor_.reset(new TaskProcessor()); |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 575 | reference_processor_.reset(new ReferenceProcessor()); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 576 | pending_task_lock_ = new Mutex("Pending task lock"); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 577 | if (ignore_max_footprint_) { |
| 578 | SetIdealFootprint(std::numeric_limits<size_t>::max()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 579 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 580 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 581 | CHECK_NE(max_allowed_footprint_, 0U); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 582 | // Create our garbage collectors. |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 583 | for (size_t i = 0; i < 2; ++i) { |
| 584 | const bool concurrent = i != 0; |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 585 | if ((MayUseCollector(kCollectorTypeCMS) && concurrent) || |
| 586 | (MayUseCollector(kCollectorTypeMS) && !concurrent)) { |
| 587 | garbage_collectors_.push_back(new collector::MarkSweep(this, concurrent)); |
| 588 | garbage_collectors_.push_back(new collector::PartialMarkSweep(this, concurrent)); |
| 589 | garbage_collectors_.push_back(new collector::StickyMarkSweep(this, concurrent)); |
| 590 | } |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 591 | } |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 592 | if (kMovingCollector) { |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 593 | if (MayUseCollector(kCollectorTypeSS) || MayUseCollector(kCollectorTypeGSS) || |
| 594 | MayUseCollector(kCollectorTypeHomogeneousSpaceCompact) || |
| 595 | use_homogeneous_space_compaction_for_oom_) { |
| 596 | // TODO: Clean this up. |
| 597 | const bool generational = foreground_collector_type_ == kCollectorTypeGSS; |
| 598 | semi_space_collector_ = new collector::SemiSpace(this, generational, |
| 599 | generational ? "generational" : ""); |
| 600 | garbage_collectors_.push_back(semi_space_collector_); |
| 601 | } |
| 602 | if (MayUseCollector(kCollectorTypeCC)) { |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame^] | 603 | concurrent_copying_collector_ = new collector::ConcurrentCopying(this, |
| 604 | "", |
| 605 | measure_gc_performance); |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 606 | garbage_collectors_.push_back(concurrent_copying_collector_); |
| 607 | } |
| 608 | if (MayUseCollector(kCollectorTypeMC)) { |
| 609 | mark_compact_collector_ = new collector::MarkCompact(this); |
| 610 | garbage_collectors_.push_back(mark_compact_collector_); |
| 611 | } |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 612 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 613 | if (!GetBootImageSpaces().empty() && non_moving_space_ != nullptr && |
Andreas Gampe | e1cb298 | 2014-08-27 11:01:09 -0700 | [diff] [blame] | 614 | (is_zygote || separate_non_moving_space || foreground_collector_type_ == kCollectorTypeGSS)) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 615 | // Check that there's no gap between the image space and the non moving space so that the |
Andreas Gampe | e1cb298 | 2014-08-27 11:01:09 -0700 | [diff] [blame] | 616 | // immune region won't break (eg. due to a large object allocated in the gap). This is only |
| 617 | // required when we're the zygote or using GSS. |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 618 | // Space with smallest Begin(). |
| 619 | space::ImageSpace* first_space = nullptr; |
| 620 | for (space::ImageSpace* space : boot_image_spaces_) { |
| 621 | if (first_space == nullptr || space->Begin() < first_space->Begin()) { |
| 622 | first_space = space; |
| 623 | } |
| 624 | } |
| 625 | bool no_gap = MemMap::CheckNoGaps(first_space->GetMemMap(), non_moving_space_->GetMemMap()); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 626 | if (!no_gap) { |
David Srbecky | 5dedb80 | 2015-06-17 00:08:02 +0100 | [diff] [blame] | 627 | PrintFileToLog("/proc/self/maps", LogSeverity::ERROR); |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 628 | MemMap::DumpMaps(LOG(ERROR), true); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 629 | LOG(FATAL) << "There's a gap between the image space and the non-moving space"; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 632 | instrumentation::Instrumentation* const instrumentation = runtime->GetInstrumentation(); |
| 633 | if (gc_stress_mode_) { |
| 634 | backtrace_lock_ = new Mutex("GC complete lock"); |
| 635 | } |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 636 | if (is_running_on_memory_tool_ || gc_stress_mode_) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 637 | instrumentation->InstrumentQuickAllocEntryPoints(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 638 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 639 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 640 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 641 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 644 | MemMap* Heap::MapAnonymousPreferredAddress(const char* name, |
| 645 | uint8_t* request_begin, |
| 646 | size_t capacity, |
| 647 | std::string* out_error_str) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 648 | while (true) { |
Kyungmin Lee | ef32b8f | 2014-10-23 09:32:05 +0900 | [diff] [blame] | 649 | MemMap* map = MemMap::MapAnonymous(name, request_begin, capacity, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 650 | PROT_READ | PROT_WRITE, true, false, out_error_str); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 651 | if (map != nullptr || request_begin == nullptr) { |
| 652 | return map; |
| 653 | } |
| 654 | // Retry a second time with no specified request begin. |
| 655 | request_begin = nullptr; |
| 656 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 659 | bool Heap::MayUseCollector(CollectorType type) const { |
| 660 | return foreground_collector_type_ == type || background_collector_type_ == type; |
| 661 | } |
| 662 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 663 | space::MallocSpace* Heap::CreateMallocSpaceFromMemMap(MemMap* mem_map, |
| 664 | size_t initial_size, |
| 665 | size_t growth_limit, |
| 666 | size_t capacity, |
| 667 | const char* name, |
| 668 | bool can_move_objects) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 669 | space::MallocSpace* malloc_space = nullptr; |
| 670 | if (kUseRosAlloc) { |
| 671 | // Create rosalloc space. |
| 672 | malloc_space = space::RosAllocSpace::CreateFromMemMap(mem_map, name, kDefaultStartingSize, |
| 673 | initial_size, growth_limit, capacity, |
| 674 | low_memory_mode_, can_move_objects); |
| 675 | } else { |
| 676 | malloc_space = space::DlMallocSpace::CreateFromMemMap(mem_map, name, kDefaultStartingSize, |
| 677 | initial_size, growth_limit, capacity, |
| 678 | can_move_objects); |
| 679 | } |
| 680 | if (collector::SemiSpace::kUseRememberedSet) { |
| 681 | accounting::RememberedSet* rem_set = |
| 682 | new accounting::RememberedSet(std::string(name) + " remembered set", this, malloc_space); |
| 683 | CHECK(rem_set != nullptr) << "Failed to create main space remembered set"; |
| 684 | AddRememberedSet(rem_set); |
| 685 | } |
| 686 | CHECK(malloc_space != nullptr) << "Failed to create " << name; |
| 687 | malloc_space->SetFootprintLimit(malloc_space->Capacity()); |
| 688 | return malloc_space; |
| 689 | } |
| 690 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 691 | void Heap::CreateMainMallocSpace(MemMap* mem_map, size_t initial_size, size_t growth_limit, |
| 692 | size_t capacity) { |
| 693 | // Is background compaction is enabled? |
| 694 | bool can_move_objects = IsMovingGc(background_collector_type_) != |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 695 | IsMovingGc(foreground_collector_type_) || use_homogeneous_space_compaction_for_oom_; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 696 | // If we are the zygote and don't yet have a zygote space, it means that the zygote fork will |
| 697 | // happen in the future. If this happens and we have kCompactZygote enabled we wish to compact |
| 698 | // from the main space to the zygote space. If background compaction is enabled, always pass in |
| 699 | // that we can move objets. |
| 700 | if (kCompactZygote && Runtime::Current()->IsZygote() && !can_move_objects) { |
| 701 | // After the zygote we want this to be false if we don't have background compaction enabled so |
| 702 | // that getting primitive array elements is faster. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 703 | // We never have homogeneous compaction with GSS and don't need a space with movable objects. |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 704 | can_move_objects = !HasZygoteSpace() && foreground_collector_type_ != kCollectorTypeGSS; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 705 | } |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 706 | if (collector::SemiSpace::kUseRememberedSet && main_space_ != nullptr) { |
| 707 | RemoveRememberedSet(main_space_); |
| 708 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 709 | const char* name = kUseRosAlloc ? kRosAllocSpaceName[0] : kDlMallocSpaceName[0]; |
| 710 | main_space_ = CreateMallocSpaceFromMemMap(mem_map, initial_size, growth_limit, capacity, name, |
| 711 | can_move_objects); |
| 712 | SetSpaceAsDefault(main_space_); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 713 | VLOG(heap) << "Created main space " << main_space_; |
| 714 | } |
| 715 | |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 716 | void Heap::ChangeAllocator(AllocatorType allocator) { |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 717 | if (current_allocator_ != allocator) { |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 718 | // These two allocators are only used internally and don't have any entrypoints. |
| 719 | CHECK_NE(allocator, kAllocatorTypeLOS); |
| 720 | CHECK_NE(allocator, kAllocatorTypeNonMoving); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 721 | current_allocator_ = allocator; |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 722 | MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 723 | SetQuickAllocEntryPointsAllocator(current_allocator_); |
| 724 | Runtime::Current()->GetInstrumentation()->ResetQuickAllocEntryPoints(); |
| 725 | } |
| 726 | } |
| 727 | |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 728 | void Heap::DisableMovingGc() { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 729 | if (IsMovingGc(foreground_collector_type_)) { |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 730 | foreground_collector_type_ = kCollectorTypeCMS; |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 731 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 732 | if (IsMovingGc(background_collector_type_)) { |
| 733 | background_collector_type_ = foreground_collector_type_; |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 734 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 735 | TransitionCollector(foreground_collector_type_); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 736 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 737 | ScopedThreadStateChange tsc(self, kSuspended); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 738 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 739 | // Something may have caused the transition to fail. |
Mathieu Chartier | e4927f6 | 2014-08-23 13:56:03 -0700 | [diff] [blame] | 740 | if (!IsMovingGc(collector_type_) && non_moving_space_ != main_space_) { |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 741 | CHECK(main_space_ != nullptr); |
| 742 | // The allocation stack may have non movable objects in it. We need to flush it since the GC |
| 743 | // can't only handle marking allocation stack objects of one non moving space and one main |
| 744 | // space. |
| 745 | { |
| 746 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 747 | FlushAllocStack(); |
| 748 | } |
| 749 | main_space_->DisableMovingObjects(); |
| 750 | non_moving_space_ = main_space_; |
| 751 | CHECK(!non_moving_space_->CanMoveObjects()); |
| 752 | } |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 755 | std::string Heap::SafeGetClassDescriptor(mirror::Class* klass) { |
| 756 | if (!IsValidContinuousSpaceObjectAddress(klass)) { |
| 757 | return StringPrintf("<non heap address klass %p>", klass); |
| 758 | } |
| 759 | mirror::Class* component_type = klass->GetComponentType<kVerifyNone>(); |
| 760 | if (IsValidContinuousSpaceObjectAddress(component_type) && klass->IsArrayClass<kVerifyNone>()) { |
| 761 | std::string result("["); |
| 762 | result += SafeGetClassDescriptor(component_type); |
| 763 | return result; |
| 764 | } else if (UNLIKELY(klass->IsPrimitive<kVerifyNone>())) { |
| 765 | return Primitive::Descriptor(klass->GetPrimitiveType<kVerifyNone>()); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 766 | } else if (UNLIKELY(klass->IsProxyClass<kVerifyNone>())) { |
| 767 | return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(klass); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 768 | } else { |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 769 | mirror::DexCache* dex_cache = klass->GetDexCache<kVerifyNone>(); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 770 | if (!IsValidContinuousSpaceObjectAddress(dex_cache)) { |
| 771 | return StringPrintf("<non heap address dex_cache %p>", dex_cache); |
| 772 | } |
| 773 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 774 | uint16_t class_def_idx = klass->GetDexClassDefIndex(); |
| 775 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 776 | return "<class def not found>"; |
| 777 | } |
| 778 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx); |
| 779 | const DexFile::TypeId& type_id = dex_file->GetTypeId(class_def.class_idx_); |
| 780 | return dex_file->GetTypeDescriptor(type_id); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | std::string Heap::SafePrettyTypeOf(mirror::Object* obj) { |
| 785 | if (obj == nullptr) { |
| 786 | return "null"; |
| 787 | } |
| 788 | mirror::Class* klass = obj->GetClass<kVerifyNone>(); |
| 789 | if (klass == nullptr) { |
| 790 | return "(class=null)"; |
| 791 | } |
| 792 | std::string result(SafeGetClassDescriptor(klass)); |
| 793 | if (obj->IsClass()) { |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 794 | result += "<" + SafeGetClassDescriptor(obj->AsClass<kVerifyNone>()) + ">"; |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 795 | } |
| 796 | return result; |
| 797 | } |
| 798 | |
| 799 | void Heap::DumpObject(std::ostream& stream, mirror::Object* obj) { |
| 800 | if (obj == nullptr) { |
| 801 | stream << "(obj=null)"; |
| 802 | return; |
| 803 | } |
| 804 | if (IsAligned<kObjectAlignment>(obj)) { |
| 805 | space::Space* space = nullptr; |
| 806 | // Don't use find space since it only finds spaces which actually contain objects instead of |
| 807 | // spaces which may contain objects (e.g. cleared bump pointer spaces). |
| 808 | for (const auto& cur_space : continuous_spaces_) { |
| 809 | if (cur_space->HasAddress(obj)) { |
| 810 | space = cur_space; |
| 811 | break; |
| 812 | } |
| 813 | } |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 814 | // Unprotect all the spaces. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 815 | for (const auto& con_space : continuous_spaces_) { |
| 816 | mprotect(con_space->Begin(), con_space->Capacity(), PROT_READ | PROT_WRITE); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 817 | } |
| 818 | stream << "Object " << obj; |
| 819 | if (space != nullptr) { |
| 820 | stream << " in space " << *space; |
| 821 | } |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 822 | mirror::Class* klass = obj->GetClass<kVerifyNone>(); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 823 | stream << "\nclass=" << klass; |
| 824 | if (klass != nullptr) { |
| 825 | stream << " type= " << SafePrettyTypeOf(obj); |
| 826 | } |
| 827 | // Re-protect the address we faulted on. |
| 828 | mprotect(AlignDown(obj, kPageSize), kPageSize, PROT_NONE); |
| 829 | } |
| 830 | } |
| 831 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 832 | bool Heap::IsCompilingBoot() const { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 833 | if (!Runtime::Current()->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 834 | return false; |
| 835 | } |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 836 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 837 | for (const auto& space : continuous_spaces_) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 838 | if (space->IsImageSpace() || space->IsZygoteSpace()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 839 | return false; |
| 840 | } |
| 841 | } |
| 842 | return true; |
| 843 | } |
| 844 | |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 845 | void Heap::IncrementDisableMovingGC(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 846 | // Need to do this holding the lock to prevent races where the GC is about to run / running when |
| 847 | // we attempt to disable it. |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 848 | ScopedThreadStateChange tsc(self, kWaitingForGcToComplete); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 849 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 850 | ++disable_moving_gc_count_; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 851 | if (IsMovingGc(collector_type_running_)) { |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 852 | WaitForGcToCompleteLocked(kGcCauseDisableMovingGc, self); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 853 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 856 | void Heap::DecrementDisableMovingGC(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 857 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 858 | CHECK_GT(disable_moving_gc_count_, 0U); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 859 | --disable_moving_gc_count_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 862 | void Heap::IncrementDisableThreadFlip(Thread* self) { |
| 863 | // Supposed to be called by mutators. If thread_flip_running_ is true, block. Otherwise, go ahead. |
| 864 | CHECK(kUseReadBarrier); |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 865 | bool is_nested = self->GetDisableThreadFlipCount() > 0; |
| 866 | self->IncrementDisableThreadFlipCount(); |
| 867 | if (is_nested) { |
| 868 | // If this is a nested JNI critical section enter, we don't need to wait or increment the global |
| 869 | // counter. The global counter is incremented only once for a thread for the outermost enter. |
| 870 | return; |
| 871 | } |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 872 | ScopedThreadStateChange tsc(self, kWaitingForGcThreadFlip); |
| 873 | MutexLock mu(self, *thread_flip_lock_); |
| 874 | bool has_waited = false; |
| 875 | uint64_t wait_start = NanoTime(); |
| 876 | while (thread_flip_running_) { |
| 877 | has_waited = true; |
| 878 | thread_flip_cond_->Wait(self); |
| 879 | } |
| 880 | ++disable_thread_flip_count_; |
| 881 | if (has_waited) { |
| 882 | uint64_t wait_time = NanoTime() - wait_start; |
| 883 | total_wait_time_ += wait_time; |
| 884 | if (wait_time > long_pause_log_threshold_) { |
| 885 | LOG(INFO) << __FUNCTION__ << " blocked for " << PrettyDuration(wait_time); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | void Heap::DecrementDisableThreadFlip(Thread* self) { |
| 891 | // Supposed to be called by mutators. Decrement disable_thread_flip_count_ and potentially wake up |
| 892 | // the GC waiting before doing a thread flip. |
| 893 | CHECK(kUseReadBarrier); |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 894 | self->DecrementDisableThreadFlipCount(); |
| 895 | bool is_outermost = self->GetDisableThreadFlipCount() == 0; |
| 896 | if (!is_outermost) { |
| 897 | // If this is not an outermost JNI critical exit, we don't need to decrement the global counter. |
| 898 | // The global counter is decremented only once for a thread for the outermost exit. |
| 899 | return; |
| 900 | } |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 901 | MutexLock mu(self, *thread_flip_lock_); |
| 902 | CHECK_GT(disable_thread_flip_count_, 0U); |
| 903 | --disable_thread_flip_count_; |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 904 | if (disable_thread_flip_count_ == 0) { |
| 905 | // Potentially notify the GC thread blocking to begin a thread flip. |
| 906 | thread_flip_cond_->Broadcast(self); |
| 907 | } |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | void Heap::ThreadFlipBegin(Thread* self) { |
| 911 | // Supposed to be called by GC. Set thread_flip_running_ to be true. If disable_thread_flip_count_ |
| 912 | // > 0, block. Otherwise, go ahead. |
| 913 | CHECK(kUseReadBarrier); |
| 914 | ScopedThreadStateChange tsc(self, kWaitingForGcThreadFlip); |
| 915 | MutexLock mu(self, *thread_flip_lock_); |
| 916 | bool has_waited = false; |
| 917 | uint64_t wait_start = NanoTime(); |
| 918 | CHECK(!thread_flip_running_); |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 919 | // Set this to true before waiting so that frequent JNI critical enter/exits won't starve |
| 920 | // GC. This like a writer preference of a reader-writer lock. |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 921 | thread_flip_running_ = true; |
| 922 | while (disable_thread_flip_count_ > 0) { |
| 923 | has_waited = true; |
| 924 | thread_flip_cond_->Wait(self); |
| 925 | } |
| 926 | if (has_waited) { |
| 927 | uint64_t wait_time = NanoTime() - wait_start; |
| 928 | total_wait_time_ += wait_time; |
| 929 | if (wait_time > long_pause_log_threshold_) { |
| 930 | LOG(INFO) << __FUNCTION__ << " blocked for " << PrettyDuration(wait_time); |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | void Heap::ThreadFlipEnd(Thread* self) { |
| 936 | // Supposed to be called by GC. Set thread_flip_running_ to false and potentially wake up mutators |
| 937 | // waiting before doing a JNI critical. |
| 938 | CHECK(kUseReadBarrier); |
| 939 | MutexLock mu(self, *thread_flip_lock_); |
| 940 | CHECK(thread_flip_running_); |
| 941 | thread_flip_running_ = false; |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 942 | // Potentially notify mutator threads blocking to enter a JNI critical section. |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 943 | thread_flip_cond_->Broadcast(self); |
| 944 | } |
| 945 | |
Mathieu Chartier | f8cb178 | 2016-03-18 18:45:41 -0700 | [diff] [blame] | 946 | void Heap::UpdateProcessState(ProcessState old_process_state, ProcessState new_process_state) { |
| 947 | if (old_process_state != new_process_state) { |
| 948 | const bool jank_perceptible = new_process_state == kProcessStateJankPerceptible; |
Mathieu Chartier | 91e3063 | 2014-03-25 15:58:50 -0700 | [diff] [blame] | 949 | for (size_t i = 1; i <= kCollectorTransitionStressIterations; ++i) { |
| 950 | // Start at index 1 to avoid "is always false" warning. |
| 951 | // Have iteration 1 always transition the collector. |
Mathieu Chartier | f8cb178 | 2016-03-18 18:45:41 -0700 | [diff] [blame] | 952 | TransitionCollector((((i & 1) == 1) == jank_perceptible) |
| 953 | ? foreground_collector_type_ |
| 954 | : background_collector_type_); |
Mathieu Chartier | 91e3063 | 2014-03-25 15:58:50 -0700 | [diff] [blame] | 955 | usleep(kCollectorTransitionStressWait); |
| 956 | } |
Mathieu Chartier | f8cb178 | 2016-03-18 18:45:41 -0700 | [diff] [blame] | 957 | if (jank_perceptible) { |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 958 | // Transition back to foreground right away to prevent jank. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 959 | RequestCollectorTransition(foreground_collector_type_, 0); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 960 | } else { |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 961 | // Don't delay for debug builds since we may want to stress test the GC. |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 962 | // If background_collector_type_ is kCollectorTypeHomogeneousSpaceCompact then we have |
| 963 | // special handling which does a homogenous space compaction once but then doesn't transition |
| 964 | // the collector. |
| 965 | RequestCollectorTransition(background_collector_type_, |
| 966 | kIsDebugBuild ? 0 : kCollectorTransitionWait); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 967 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 968 | } |
Mathieu Chartier | ca2a24d | 2013-11-25 15:12:12 -0800 | [diff] [blame] | 969 | } |
| 970 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 971 | void Heap::CreateThreadPool() { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 972 | const size_t num_threads = std::max(parallel_gc_threads_, conc_gc_threads_); |
| 973 | if (num_threads != 0) { |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 974 | thread_pool_.reset(new ThreadPool("Heap thread pool", num_threads)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 975 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 978 | // Visit objects when threads aren't suspended. If concurrent moving |
| 979 | // GC, disable moving GC and suspend threads and then visit objects. |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 980 | void Heap::VisitObjects(ObjectCallback callback, void* arg) { |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 981 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 982 | Locks::mutator_lock_->AssertSharedHeld(self); |
| 983 | DCHECK(!Locks::mutator_lock_->IsExclusiveHeld(self)) << "Call VisitObjectsPaused() instead"; |
| 984 | if (IsGcConcurrentAndMoving()) { |
| 985 | // Concurrent moving GC. Just suspending threads isn't sufficient |
| 986 | // because a collection isn't one big pause and we could suspend |
| 987 | // threads in the middle (between phases) of a concurrent moving |
| 988 | // collection where it's not easily known which objects are alive |
| 989 | // (both the region space and the non-moving space) or which |
| 990 | // copies of objects to visit, and the to-space invariant could be |
| 991 | // easily broken. Visit objects while GC isn't running by using |
| 992 | // IncrementDisableMovingGC() and threads are suspended. |
| 993 | IncrementDisableMovingGC(self); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 994 | { |
| 995 | ScopedThreadSuspension sts(self, kWaitingForVisitObjects); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 996 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 997 | VisitObjectsInternalRegionSpace(callback, arg); |
| 998 | VisitObjectsInternal(callback, arg); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 999 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1000 | DecrementDisableMovingGC(self); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1001 | } else { |
| 1002 | // GCs can move objects, so don't allow this. |
| 1003 | ScopedAssertNoThreadSuspension ants(self, "Visiting objects"); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1004 | DCHECK(region_space_ == nullptr); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1005 | VisitObjectsInternal(callback, arg); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1009 | // Visit objects when threads are already suspended. |
| 1010 | void Heap::VisitObjectsPaused(ObjectCallback callback, void* arg) { |
| 1011 | Thread* self = Thread::Current(); |
| 1012 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 1013 | VisitObjectsInternalRegionSpace(callback, arg); |
| 1014 | VisitObjectsInternal(callback, arg); |
| 1015 | } |
| 1016 | |
| 1017 | // Visit objects in the region spaces. |
| 1018 | void Heap::VisitObjectsInternalRegionSpace(ObjectCallback callback, void* arg) { |
| 1019 | Thread* self = Thread::Current(); |
| 1020 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 1021 | if (region_space_ != nullptr) { |
| 1022 | DCHECK(IsGcConcurrentAndMoving()); |
| 1023 | if (!zygote_creation_lock_.IsExclusiveHeld(self)) { |
| 1024 | // Exclude the pre-zygote fork time where the semi-space collector |
| 1025 | // calls VerifyHeapReferences() as part of the zygote compaction |
| 1026 | // which then would call here without the moving GC disabled, |
| 1027 | // which is fine. |
| 1028 | DCHECK(IsMovingGCDisabled(self)); |
| 1029 | } |
| 1030 | region_space_->Walk(callback, arg); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | // Visit objects in the other spaces. |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1035 | void Heap::VisitObjectsInternal(ObjectCallback callback, void* arg) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1036 | if (bump_pointer_space_ != nullptr) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 1037 | // Visit objects in bump pointer space. |
| 1038 | bump_pointer_space_->Walk(callback, arg); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1039 | } |
| 1040 | // TODO: Switch to standard begin and end to use ranged a based loop. |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 1041 | for (auto* it = allocation_stack_->Begin(), *end = allocation_stack_->End(); it < end; ++it) { |
| 1042 | mirror::Object* const obj = it->AsMirrorPtr(); |
Mathieu Chartier | ebdf3f3 | 2014-02-13 10:23:27 -0800 | [diff] [blame] | 1043 | if (obj != nullptr && obj->GetClass() != nullptr) { |
| 1044 | // Avoid the race condition caused by the object not yet being written into the allocation |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1045 | // stack or the class not yet being written in the object. Or, if |
| 1046 | // kUseThreadLocalAllocationStack, there can be nulls on the allocation stack. |
Mathieu Chartier | ebdf3f3 | 2014-02-13 10:23:27 -0800 | [diff] [blame] | 1047 | callback(obj, arg); |
| 1048 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1049 | } |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1050 | { |
| 1051 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 1052 | GetLiveBitmap()->Walk(callback, arg); |
| 1053 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | void Heap::MarkAllocStackAsLive(accounting::ObjectStack* stack) { |
Mathieu Chartier | 00b5915 | 2014-07-25 10:13:51 -0700 | [diff] [blame] | 1057 | space::ContinuousSpace* space1 = main_space_ != nullptr ? main_space_ : non_moving_space_; |
| 1058 | space::ContinuousSpace* space2 = non_moving_space_; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1059 | // TODO: Generalize this to n bitmaps? |
Mathieu Chartier | 00b5915 | 2014-07-25 10:13:51 -0700 | [diff] [blame] | 1060 | CHECK(space1 != nullptr); |
| 1061 | CHECK(space2 != nullptr); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1062 | MarkAllocStack(space1->GetLiveBitmap(), space2->GetLiveBitmap(), |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 1063 | (large_object_space_ != nullptr ? large_object_space_->GetLiveBitmap() : nullptr), |
| 1064 | stack); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1067 | void Heap::DeleteThreadPool() { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1068 | thread_pool_.reset(nullptr); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 1071 | void Heap::AddSpace(space::Space* space) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1072 | CHECK(space != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1073 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 1074 | if (space->IsContinuousSpace()) { |
| 1075 | DCHECK(!space->IsDiscontinuousSpace()); |
| 1076 | space::ContinuousSpace* continuous_space = space->AsContinuousSpace(); |
| 1077 | // Continuous spaces don't necessarily have bitmaps. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 1078 | accounting::ContinuousSpaceBitmap* live_bitmap = continuous_space->GetLiveBitmap(); |
| 1079 | accounting::ContinuousSpaceBitmap* mark_bitmap = continuous_space->GetMarkBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1080 | if (live_bitmap != nullptr) { |
Mathieu Chartier | 2796a16 | 2014-07-25 11:50:47 -0700 | [diff] [blame] | 1081 | CHECK(mark_bitmap != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1082 | live_bitmap_->AddContinuousSpaceBitmap(live_bitmap); |
| 1083 | mark_bitmap_->AddContinuousSpaceBitmap(mark_bitmap); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1084 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1085 | continuous_spaces_.push_back(continuous_space); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1086 | // Ensure that spaces remain sorted in increasing order of start address. |
| 1087 | std::sort(continuous_spaces_.begin(), continuous_spaces_.end(), |
| 1088 | [](const space::ContinuousSpace* a, const space::ContinuousSpace* b) { |
| 1089 | return a->Begin() < b->Begin(); |
| 1090 | }); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1091 | } else { |
Mathieu Chartier | 2796a16 | 2014-07-25 11:50:47 -0700 | [diff] [blame] | 1092 | CHECK(space->IsDiscontinuousSpace()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1093 | space::DiscontinuousSpace* discontinuous_space = space->AsDiscontinuousSpace(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1094 | live_bitmap_->AddLargeObjectBitmap(discontinuous_space->GetLiveBitmap()); |
| 1095 | mark_bitmap_->AddLargeObjectBitmap(discontinuous_space->GetMarkBitmap()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1096 | discontinuous_spaces_.push_back(discontinuous_space); |
| 1097 | } |
| 1098 | if (space->IsAllocSpace()) { |
| 1099 | alloc_spaces_.push_back(space->AsAllocSpace()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1100 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 1103 | void Heap::SetSpaceAsDefault(space::ContinuousSpace* continuous_space) { |
| 1104 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 1105 | if (continuous_space->IsDlMallocSpace()) { |
| 1106 | dlmalloc_space_ = continuous_space->AsDlMallocSpace(); |
| 1107 | } else if (continuous_space->IsRosAllocSpace()) { |
| 1108 | rosalloc_space_ = continuous_space->AsRosAllocSpace(); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | void Heap::RemoveSpace(space::Space* space) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1113 | DCHECK(space != nullptr); |
| 1114 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 1115 | if (space->IsContinuousSpace()) { |
| 1116 | DCHECK(!space->IsDiscontinuousSpace()); |
| 1117 | space::ContinuousSpace* continuous_space = space->AsContinuousSpace(); |
| 1118 | // Continuous spaces don't necessarily have bitmaps. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 1119 | accounting::ContinuousSpaceBitmap* live_bitmap = continuous_space->GetLiveBitmap(); |
| 1120 | accounting::ContinuousSpaceBitmap* mark_bitmap = continuous_space->GetMarkBitmap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1121 | if (live_bitmap != nullptr) { |
| 1122 | DCHECK(mark_bitmap != nullptr); |
| 1123 | live_bitmap_->RemoveContinuousSpaceBitmap(live_bitmap); |
| 1124 | mark_bitmap_->RemoveContinuousSpaceBitmap(mark_bitmap); |
| 1125 | } |
| 1126 | auto it = std::find(continuous_spaces_.begin(), continuous_spaces_.end(), continuous_space); |
| 1127 | DCHECK(it != continuous_spaces_.end()); |
| 1128 | continuous_spaces_.erase(it); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1129 | } else { |
| 1130 | DCHECK(space->IsDiscontinuousSpace()); |
| 1131 | space::DiscontinuousSpace* discontinuous_space = space->AsDiscontinuousSpace(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1132 | live_bitmap_->RemoveLargeObjectBitmap(discontinuous_space->GetLiveBitmap()); |
| 1133 | mark_bitmap_->RemoveLargeObjectBitmap(discontinuous_space->GetMarkBitmap()); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1134 | auto it = std::find(discontinuous_spaces_.begin(), discontinuous_spaces_.end(), |
| 1135 | discontinuous_space); |
| 1136 | DCHECK(it != discontinuous_spaces_.end()); |
| 1137 | discontinuous_spaces_.erase(it); |
| 1138 | } |
| 1139 | if (space->IsAllocSpace()) { |
| 1140 | auto it = std::find(alloc_spaces_.begin(), alloc_spaces_.end(), space->AsAllocSpace()); |
| 1141 | DCHECK(it != alloc_spaces_.end()); |
| 1142 | alloc_spaces_.erase(it); |
| 1143 | } |
| 1144 | } |
| 1145 | |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 1146 | void Heap::DumpGcPerformanceInfo(std::ostream& os) { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1147 | // Dump cumulative timings. |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 1148 | os << "Dumping cumulative Gc timings\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1149 | uint64_t total_duration = 0; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1150 | // Dump cumulative loggers for each GC type. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1151 | uint64_t total_paused_time = 0; |
Mathieu Chartier | 5a48719 | 2014-04-08 11:14:54 -0700 | [diff] [blame] | 1152 | for (auto& collector : garbage_collectors_) { |
Mathieu Chartier | 104fa0c | 2014-08-07 14:26:27 -0700 | [diff] [blame] | 1153 | total_duration += collector->GetCumulativeTimings().GetTotalNs(); |
| 1154 | total_paused_time += collector->GetTotalPausedTimeNs(); |
| 1155 | collector->DumpPerformanceInfo(os); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1156 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1157 | if (total_duration != 0) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 1158 | const double total_seconds = static_cast<double>(total_duration / 1000) / 1000000.0; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 1159 | os << "Total time spent in GC: " << PrettyDuration(total_duration) << "\n"; |
| 1160 | os << "Mean GC size throughput: " |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1161 | << PrettySize(GetBytesFreedEver() / total_seconds) << "/s\n"; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 1162 | os << "Mean GC object throughput: " |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1163 | << (GetObjectsFreedEver() / total_seconds) << " objects/s\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1164 | } |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1165 | uint64_t total_objects_allocated = GetObjectsAllocatedEver(); |
Mathieu Chartier | c30a725 | 2014-08-12 10:13:48 -0700 | [diff] [blame] | 1166 | os << "Total number of allocations " << total_objects_allocated << "\n"; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 1167 | os << "Total bytes allocated " << PrettySize(GetBytesAllocatedEver()) << "\n"; |
| 1168 | os << "Total bytes freed " << PrettySize(GetBytesFreedEver()) << "\n"; |
Mathieu Chartier | c30a725 | 2014-08-12 10:13:48 -0700 | [diff] [blame] | 1169 | os << "Free memory " << PrettySize(GetFreeMemory()) << "\n"; |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1170 | os << "Free memory until GC " << PrettySize(GetFreeMemoryUntilGC()) << "\n"; |
| 1171 | os << "Free memory until OOME " << PrettySize(GetFreeMemoryUntilOOME()) << "\n"; |
Mathieu Chartier | c30a725 | 2014-08-12 10:13:48 -0700 | [diff] [blame] | 1172 | os << "Total memory " << PrettySize(GetTotalMemory()) << "\n"; |
| 1173 | os << "Max memory " << PrettySize(GetMaxMemory()) << "\n"; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 1174 | if (HasZygoteSpace()) { |
| 1175 | os << "Zygote space size " << PrettySize(zygote_space_->Size()) << "\n"; |
| 1176 | } |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 1177 | os << "Total mutator paused time: " << PrettyDuration(total_paused_time) << "\n"; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 1178 | os << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_) << "\n"; |
| 1179 | os << "Total GC count: " << GetGcCount() << "\n"; |
| 1180 | os << "Total GC time: " << PrettyDuration(GetGcTime()) << "\n"; |
| 1181 | os << "Total blocking GC count: " << GetBlockingGcCount() << "\n"; |
| 1182 | os << "Total blocking GC time: " << PrettyDuration(GetBlockingGcTime()) << "\n"; |
| 1183 | |
| 1184 | { |
| 1185 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 1186 | if (gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1187 | os << "Histogram of GC count per " << NsToMs(kGcCountRateHistogramWindowDuration) << " ms: "; |
| 1188 | gc_count_rate_histogram_.DumpBins(os); |
| 1189 | os << "\n"; |
| 1190 | } |
| 1191 | if (blocking_gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1192 | os << "Histogram of blocking GC count per " |
| 1193 | << NsToMs(kGcCountRateHistogramWindowDuration) << " ms: "; |
| 1194 | blocking_gc_count_rate_histogram_.DumpBins(os); |
| 1195 | os << "\n"; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Hiroshi Yamauchi | b62f2e6 | 2016-03-23 15:51:24 -0700 | [diff] [blame] | 1199 | if (kDumpRosAllocStatsOnSigQuit && rosalloc_space_ != nullptr) { |
| 1200 | rosalloc_space_->DumpStats(os); |
| 1201 | } |
| 1202 | |
Mathieu Chartier | 5d2a3f7 | 2016-05-11 11:35:39 -0700 | [diff] [blame] | 1203 | { |
| 1204 | MutexLock mu(Thread::Current(), native_histogram_lock_); |
| 1205 | if (native_allocation_histogram_.SampleSize() > 0u) { |
| 1206 | os << "Histogram of native allocation "; |
| 1207 | native_allocation_histogram_.DumpBins(os); |
| 1208 | os << " bucket size " << native_allocation_histogram_.BucketWidth() << "\n"; |
| 1209 | } |
| 1210 | if (native_free_histogram_.SampleSize() > 0u) { |
| 1211 | os << "Histogram of native free "; |
| 1212 | native_free_histogram_.DumpBins(os); |
| 1213 | os << " bucket size " << native_free_histogram_.BucketWidth() << "\n"; |
| 1214 | } |
| 1215 | } |
| 1216 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1217 | BaseMutex::DumpAll(os); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Hiroshi Yamauchi | 3767017 | 2015-06-10 17:20:54 -0700 | [diff] [blame] | 1220 | void Heap::ResetGcPerformanceInfo() { |
| 1221 | for (auto& collector : garbage_collectors_) { |
| 1222 | collector->ResetMeasurements(); |
| 1223 | } |
Hiroshi Yamauchi | 3767017 | 2015-06-10 17:20:54 -0700 | [diff] [blame] | 1224 | total_bytes_freed_ever_ = 0; |
| 1225 | total_objects_freed_ever_ = 0; |
| 1226 | total_wait_time_ = 0; |
| 1227 | blocking_gc_count_ = 0; |
| 1228 | blocking_gc_time_ = 0; |
| 1229 | gc_count_last_window_ = 0; |
| 1230 | blocking_gc_count_last_window_ = 0; |
| 1231 | last_update_time_gc_count_rate_histograms_ = // Round down by the window duration. |
| 1232 | (NanoTime() / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration; |
| 1233 | { |
| 1234 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 1235 | gc_count_rate_histogram_.Reset(); |
| 1236 | blocking_gc_count_rate_histogram_.Reset(); |
| 1237 | } |
| 1238 | } |
| 1239 | |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 1240 | uint64_t Heap::GetGcCount() const { |
| 1241 | uint64_t gc_count = 0U; |
| 1242 | for (auto& collector : garbage_collectors_) { |
| 1243 | gc_count += collector->GetCumulativeTimings().GetIterations(); |
| 1244 | } |
| 1245 | return gc_count; |
| 1246 | } |
| 1247 | |
| 1248 | uint64_t Heap::GetGcTime() const { |
| 1249 | uint64_t gc_time = 0U; |
| 1250 | for (auto& collector : garbage_collectors_) { |
| 1251 | gc_time += collector->GetCumulativeTimings().GetTotalNs(); |
| 1252 | } |
| 1253 | return gc_time; |
| 1254 | } |
| 1255 | |
| 1256 | uint64_t Heap::GetBlockingGcCount() const { |
| 1257 | return blocking_gc_count_; |
| 1258 | } |
| 1259 | |
| 1260 | uint64_t Heap::GetBlockingGcTime() const { |
| 1261 | return blocking_gc_time_; |
| 1262 | } |
| 1263 | |
| 1264 | void Heap::DumpGcCountRateHistogram(std::ostream& os) const { |
| 1265 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 1266 | if (gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1267 | gc_count_rate_histogram_.DumpBins(os); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | void Heap::DumpBlockingGcCountRateHistogram(std::ostream& os) const { |
| 1272 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 1273 | if (blocking_gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1274 | blocking_gc_count_rate_histogram_.DumpBins(os); |
| 1275 | } |
| 1276 | } |
| 1277 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1278 | Heap::~Heap() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1279 | VLOG(heap) << "Starting ~Heap()"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1280 | STLDeleteElements(&garbage_collectors_); |
| 1281 | // 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] | 1282 | allocation_stack_->Reset(); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1283 | allocation_records_.reset(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1284 | live_stack_->Reset(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 1285 | STLDeleteValues(&mod_union_tables_); |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 1286 | STLDeleteValues(&remembered_sets_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1287 | STLDeleteElements(&continuous_spaces_); |
| 1288 | STLDeleteElements(&discontinuous_spaces_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1289 | delete gc_complete_lock_; |
Andreas Gampe | 6be4f2a | 2015-11-10 13:34:17 -0800 | [diff] [blame] | 1290 | delete thread_flip_lock_; |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1291 | delete pending_task_lock_; |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1292 | delete backtrace_lock_; |
| 1293 | if (unique_backtrace_count_.LoadRelaxed() != 0 || seen_backtrace_count_.LoadRelaxed() != 0) { |
| 1294 | LOG(INFO) << "gc stress unique=" << unique_backtrace_count_.LoadRelaxed() |
| 1295 | << " total=" << seen_backtrace_count_.LoadRelaxed() + |
| 1296 | unique_backtrace_count_.LoadRelaxed(); |
| 1297 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1298 | VLOG(heap) << "Finished ~Heap()"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1301 | space::ContinuousSpace* Heap::FindContinuousSpaceFromObject(const mirror::Object* obj, |
| 1302 | bool fail_ok) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1303 | for (const auto& space : continuous_spaces_) { |
| 1304 | if (space->Contains(obj)) { |
| 1305 | return space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1308 | if (!fail_ok) { |
| 1309 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 1310 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1311 | return nullptr; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1314 | space::DiscontinuousSpace* Heap::FindDiscontinuousSpaceFromObject(const mirror::Object* obj, |
| 1315 | bool fail_ok) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1316 | for (const auto& space : discontinuous_spaces_) { |
| 1317 | if (space->Contains(obj)) { |
| 1318 | return space; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | if (!fail_ok) { |
| 1322 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 1323 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1324 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | space::Space* Heap::FindSpaceFromObject(const mirror::Object* obj, bool fail_ok) const { |
| 1328 | space::Space* result = FindContinuousSpaceFromObject(obj, true); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1329 | if (result != nullptr) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1330 | return result; |
| 1331 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1332 | return FindDiscontinuousSpaceFromObject(obj, fail_ok); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1335 | void Heap::ThrowOutOfMemoryError(Thread* self, size_t byte_count, AllocatorType allocator_type) { |
Mathieu Chartier | e8f3f03 | 2016-04-04 16:49:44 -0700 | [diff] [blame] | 1336 | // If we're in a stack overflow, do not create a new exception. It would require running the |
| 1337 | // constructor, which will of course still be in a stack overflow. |
| 1338 | if (self->IsHandlingStackOverflow()) { |
| 1339 | self->SetException(Runtime::Current()->GetPreAllocatedOutOfMemoryError()); |
| 1340 | return; |
| 1341 | } |
| 1342 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1343 | std::ostringstream oss; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1344 | size_t total_bytes_free = GetFreeMemory(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1345 | oss << "Failed to allocate a " << byte_count << " byte allocation with " << total_bytes_free |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1346 | << " free bytes and " << PrettySize(GetFreeMemoryUntilOOME()) << " until OOM"; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1347 | // If the allocation failed due to fragmentation, print out the largest continuous allocation. |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1348 | if (total_bytes_free >= byte_count) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1349 | space::AllocSpace* space = nullptr; |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1350 | if (allocator_type == kAllocatorTypeNonMoving) { |
| 1351 | space = non_moving_space_; |
| 1352 | } else if (allocator_type == kAllocatorTypeRosAlloc || |
| 1353 | allocator_type == kAllocatorTypeDlMalloc) { |
| 1354 | space = main_space_; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1355 | } else if (allocator_type == kAllocatorTypeBumpPointer || |
| 1356 | allocator_type == kAllocatorTypeTLAB) { |
| 1357 | space = bump_pointer_space_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1358 | } else if (allocator_type == kAllocatorTypeRegion || |
| 1359 | allocator_type == kAllocatorTypeRegionTLAB) { |
| 1360 | space = region_space_; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1361 | } |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1362 | if (space != nullptr) { |
| 1363 | space->LogFragmentationAllocFailure(oss, byte_count); |
| 1364 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1365 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1366 | self->ThrowOutOfMemoryError(oss.str().c_str()); |
| 1367 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1368 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1369 | void Heap::DoPendingCollectorTransition() { |
| 1370 | CollectorType desired_collector_type = desired_collector_type_; |
Mathieu Chartier | b272855 | 2014-09-08 20:08:41 +0000 | [diff] [blame] | 1371 | // Launch homogeneous space compaction if it is desired. |
| 1372 | if (desired_collector_type == kCollectorTypeHomogeneousSpaceCompact) { |
| 1373 | if (!CareAboutPauseTimes()) { |
| 1374 | PerformHomogeneousSpaceCompact(); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1375 | } else { |
| 1376 | VLOG(gc) << "Homogeneous compaction ignored due to jank perceptible process state"; |
Mathieu Chartier | b272855 | 2014-09-08 20:08:41 +0000 | [diff] [blame] | 1377 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1378 | } else { |
| 1379 | TransitionCollector(desired_collector_type); |
Mathieu Chartier | b272855 | 2014-09-08 20:08:41 +0000 | [diff] [blame] | 1380 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | void Heap::Trim(Thread* self) { |
Mathieu Chartier | 8d44725 | 2015-10-26 10:21:14 -0700 | [diff] [blame] | 1384 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1385 | if (!CareAboutPauseTimes()) { |
| 1386 | // Deflate the monitors, this can cause a pause but shouldn't matter since we don't care |
| 1387 | // about pauses. |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1388 | ScopedTrace trace("Deflating monitors"); |
| 1389 | ScopedSuspendAll ssa(__FUNCTION__); |
| 1390 | uint64_t start_time = NanoTime(); |
| 1391 | size_t count = runtime->GetMonitorList()->DeflateMonitors(); |
| 1392 | VLOG(heap) << "Deflating " << count << " monitors took " |
| 1393 | << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1394 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1395 | TrimIndirectReferenceTables(self); |
| 1396 | TrimSpaces(self); |
Mathieu Chartier | 8d44725 | 2015-10-26 10:21:14 -0700 | [diff] [blame] | 1397 | // Trim arenas that may have been used by JIT or verifier. |
Mathieu Chartier | 8d44725 | 2015-10-26 10:21:14 -0700 | [diff] [blame] | 1398 | runtime->GetArenaPool()->TrimMaps(); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 1401 | class TrimIndirectReferenceTableClosure : public Closure { |
| 1402 | public: |
| 1403 | explicit TrimIndirectReferenceTableClosure(Barrier* barrier) : barrier_(barrier) { |
| 1404 | } |
| 1405 | virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 1406 | thread->GetJniEnv()->locals.Trim(); |
Lei Li | dd9943d | 2015-02-02 14:24:44 +0800 | [diff] [blame] | 1407 | // If thread is a running mutator, then act on behalf of the trim thread. |
| 1408 | // See the code in ThreadList::RunCheckpoint. |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 1409 | barrier_->Pass(Thread::Current()); |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | private: |
| 1413 | Barrier* const barrier_; |
| 1414 | }; |
| 1415 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1416 | void Heap::TrimIndirectReferenceTables(Thread* self) { |
| 1417 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1418 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1419 | JavaVMExt* vm = soa.Vm(); |
| 1420 | // Trim globals indirect reference table. |
| 1421 | vm->TrimGlobals(); |
| 1422 | // Trim locals indirect reference tables. |
| 1423 | Barrier barrier(0); |
| 1424 | TrimIndirectReferenceTableClosure closure(&barrier); |
| 1425 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 1426 | size_t barrier_count = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure); |
Lei Li | dd9943d | 2015-02-02 14:24:44 +0800 | [diff] [blame] | 1427 | if (barrier_count != 0) { |
| 1428 | barrier.Increment(self, barrier_count); |
| 1429 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1430 | } |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 1431 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 1432 | void Heap::StartGC(Thread* self, GcCause cause, CollectorType collector_type) { |
| 1433 | MutexLock mu(self, *gc_complete_lock_); |
| 1434 | // Ensure there is only one GC at a time. |
| 1435 | WaitForGcToCompleteLocked(cause, self); |
| 1436 | collector_type_running_ = collector_type; |
| 1437 | } |
| 1438 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1439 | void Heap::TrimSpaces(Thread* self) { |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 1440 | { |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 1441 | // Need to do this before acquiring the locks since we don't want to get suspended while |
| 1442 | // holding any locks. |
| 1443 | ScopedThreadStateChange tsc(self, kWaitingForGcToComplete); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1444 | // Pretend we are doing a GC to prevent background compaction from deleting the space we are |
| 1445 | // trimming. |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 1446 | StartGC(self, kGcCauseTrim, kCollectorTypeHeapTrim); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1447 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1448 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1449 | const uint64_t start_ns = NanoTime(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1450 | // Trim the managed spaces. |
| 1451 | uint64_t total_alloc_space_allocated = 0; |
| 1452 | uint64_t total_alloc_space_size = 0; |
| 1453 | uint64_t managed_reclaimed = 0; |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 1454 | { |
| 1455 | ScopedObjectAccess soa(self); |
| 1456 | for (const auto& space : continuous_spaces_) { |
| 1457 | if (space->IsMallocSpace()) { |
| 1458 | gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 1459 | if (malloc_space->IsRosAllocSpace() || !CareAboutPauseTimes()) { |
| 1460 | // Don't trim dlmalloc spaces if we care about pauses since this can hold the space lock |
| 1461 | // for a long period of time. |
| 1462 | managed_reclaimed += malloc_space->Trim(); |
| 1463 | } |
| 1464 | total_alloc_space_size += malloc_space->Size(); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1465 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1466 | } |
| 1467 | } |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 1468 | total_alloc_space_allocated = GetBytesAllocated(); |
| 1469 | if (large_object_space_ != nullptr) { |
| 1470 | total_alloc_space_allocated -= large_object_space_->GetBytesAllocated(); |
| 1471 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1472 | if (bump_pointer_space_ != nullptr) { |
| 1473 | total_alloc_space_allocated -= bump_pointer_space_->Size(); |
| 1474 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1475 | if (region_space_ != nullptr) { |
| 1476 | total_alloc_space_allocated -= region_space_->GetBytesAllocated(); |
| 1477 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1478 | const float managed_utilization = static_cast<float>(total_alloc_space_allocated) / |
| 1479 | static_cast<float>(total_alloc_space_size); |
| 1480 | uint64_t gc_heap_end_ns = NanoTime(); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1481 | // We never move things in the native heap, so we can finish the GC at this point. |
| 1482 | FinishGC(self, collector::kGcTypeNone); |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 1483 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1484 | VLOG(heap) << "Heap trim of managed (duration=" << PrettyDuration(gc_heap_end_ns - start_ns) |
Dimitry Ivanov | e6465bc | 2015-12-14 18:55:02 -0800 | [diff] [blame] | 1485 | << ", advised=" << PrettySize(managed_reclaimed) << ") heap. Managed heap utilization of " |
| 1486 | << static_cast<int>(100 * managed_utilization) << "%."; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | bool Heap::IsValidObjectAddress(const mirror::Object* obj) const { |
| 1490 | // Note: we deliberately don't take the lock here, and mustn't test anything that would require |
| 1491 | // taking the lock. |
| 1492 | if (obj == nullptr) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 1493 | return true; |
| 1494 | } |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 1495 | return IsAligned<kObjectAlignment>(obj) && FindSpaceFromObject(obj, true) != nullptr; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Mathieu Chartier | d68ac70 | 2014-02-11 14:50:51 -0800 | [diff] [blame] | 1498 | bool Heap::IsNonDiscontinuousSpaceHeapAddress(const mirror::Object* obj) const { |
| 1499 | return FindContinuousSpaceFromObject(obj, true) != nullptr; |
| 1500 | } |
| 1501 | |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 1502 | bool Heap::IsValidContinuousSpaceObjectAddress(const mirror::Object* obj) const { |
| 1503 | if (obj == nullptr || !IsAligned<kObjectAlignment>(obj)) { |
| 1504 | return false; |
| 1505 | } |
| 1506 | for (const auto& space : continuous_spaces_) { |
| 1507 | if (space->HasAddress(obj)) { |
| 1508 | return true; |
| 1509 | } |
| 1510 | } |
| 1511 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1514 | bool Heap::IsLiveObjectLocked(mirror::Object* obj, bool search_allocation_stack, |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1515 | bool search_live_stack, bool sorted) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1516 | if (UNLIKELY(!IsAligned<kObjectAlignment>(obj))) { |
| 1517 | return false; |
| 1518 | } |
| 1519 | if (bump_pointer_space_ != nullptr && bump_pointer_space_->HasAddress(obj)) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1520 | mirror::Class* klass = obj->GetClass<kVerifyNone>(); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1521 | if (obj == klass) { |
Mathieu Chartier | 9be9a7a | 2014-01-24 14:07:33 -0800 | [diff] [blame] | 1522 | // This case happens for java.lang.Class. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1523 | return true; |
| 1524 | } |
| 1525 | return VerifyClassClass(klass) && IsLiveObjectLocked(klass); |
| 1526 | } else if (temp_space_ != nullptr && temp_space_->HasAddress(obj)) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1527 | // If we are in the allocated region of the temp space, then we are probably live (e.g. during |
| 1528 | // a GC). When a GC isn't running End() - Begin() is 0 which means no objects are contained. |
| 1529 | return temp_space_->Contains(obj); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1530 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1531 | if (region_space_ != nullptr && region_space_->HasAddress(obj)) { |
| 1532 | return true; |
| 1533 | } |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1534 | space::ContinuousSpace* c_space = FindContinuousSpaceFromObject(obj, true); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1535 | space::DiscontinuousSpace* d_space = nullptr; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1536 | if (c_space != nullptr) { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1537 | if (c_space->GetLiveBitmap()->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1538 | return true; |
| 1539 | } |
| 1540 | } else { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1541 | d_space = FindDiscontinuousSpaceFromObject(obj, true); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1542 | if (d_space != nullptr) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1543 | if (d_space->GetLiveBitmap()->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1544 | return true; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1548 | // 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] | 1549 | for (size_t i = 0; i < (sorted ? 1 : 5); ++i) { |
| 1550 | if (i > 0) { |
| 1551 | NanoSleep(MsToNs(10)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1552 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1553 | if (search_allocation_stack) { |
| 1554 | if (sorted) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1555 | if (allocation_stack_->ContainsSorted(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1556 | return true; |
| 1557 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1558 | } else if (allocation_stack_->Contains(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1559 | return true; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | if (search_live_stack) { |
| 1564 | if (sorted) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1565 | if (live_stack_->ContainsSorted(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1566 | return true; |
| 1567 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1568 | } else if (live_stack_->Contains(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1569 | return true; |
| 1570 | } |
| 1571 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1572 | } |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1573 | // We need to check the bitmaps again since there is a race where we mark something as live and |
| 1574 | // then clear the stack containing it. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1575 | if (c_space != nullptr) { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1576 | if (c_space->GetLiveBitmap()->Test(obj)) { |
| 1577 | return true; |
| 1578 | } |
| 1579 | } else { |
| 1580 | d_space = FindDiscontinuousSpaceFromObject(obj, true); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1581 | if (d_space != nullptr && d_space->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1582 | return true; |
| 1583 | } |
| 1584 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1585 | return false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 1588 | std::string Heap::DumpSpaces() const { |
| 1589 | std::ostringstream oss; |
| 1590 | DumpSpaces(oss); |
| 1591 | return oss.str(); |
| 1592 | } |
| 1593 | |
| 1594 | void Heap::DumpSpaces(std::ostream& stream) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1595 | for (const auto& space : continuous_spaces_) { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 1596 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 1597 | accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1598 | stream << space << " " << *space << "\n"; |
| 1599 | if (live_bitmap != nullptr) { |
| 1600 | stream << live_bitmap << " " << *live_bitmap << "\n"; |
| 1601 | } |
| 1602 | if (mark_bitmap != nullptr) { |
| 1603 | stream << mark_bitmap << " " << *mark_bitmap << "\n"; |
| 1604 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1605 | } |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1606 | for (const auto& space : discontinuous_spaces_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1607 | stream << space << " " << *space << "\n"; |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 1608 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1611 | void Heap::VerifyObjectBody(mirror::Object* obj) { |
Stephen Hines | 22c6a81 | 2014-07-16 11:03:43 -0700 | [diff] [blame] | 1612 | if (verify_object_mode_ == kVerifyObjectModeDisabled) { |
| 1613 | return; |
| 1614 | } |
| 1615 | |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1616 | // Ignore early dawn of the universe verifications. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 1617 | if (UNLIKELY(static_cast<size_t>(num_bytes_allocated_.LoadRelaxed()) < 10 * KB)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1618 | return; |
| 1619 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1620 | CHECK_ALIGNED(obj, kObjectAlignment) << "Object isn't aligned"; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1621 | mirror::Class* c = obj->GetFieldObject<mirror::Class, kVerifyNone>(mirror::Object::ClassOffset()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1622 | CHECK(c != nullptr) << "Null class in object " << obj; |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1623 | CHECK_ALIGNED(c, kObjectAlignment) << "Class " << c << " not aligned in object " << obj; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1624 | CHECK(VerifyClassClass(c)); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1625 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1626 | if (verify_object_mode_ > kVerifyObjectModeFast) { |
| 1627 | // Note: the bitmap tests below are racy since we don't hold the heap bitmap lock. |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 1628 | CHECK(IsLiveObjectLocked(obj)) << "Object is dead " << obj << "\n" << DumpSpaces(); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 1629 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1632 | void Heap::VerificationCallback(mirror::Object* obj, void* arg) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1633 | reinterpret_cast<Heap*>(arg)->VerifyObjectBody(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | void Heap::VerifyHeap() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1637 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1638 | GetLiveBitmap()->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 1641 | void Heap::RecordFree(uint64_t freed_objects, int64_t freed_bytes) { |
Mathieu Chartier | 601276a | 2014-03-20 15:12:30 -0700 | [diff] [blame] | 1642 | // Use signed comparison since freed bytes can be negative when background compaction foreground |
| 1643 | // transitions occurs. This is caused by the moving objects from a bump pointer space to a |
| 1644 | // free list backed space typically increasing memory footprint due to padding and binning. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 1645 | DCHECK_LE(freed_bytes, static_cast<int64_t>(num_bytes_allocated_.LoadRelaxed())); |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 1646 | // Note: This relies on 2s complement for handling negative freed_bytes. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 1647 | num_bytes_allocated_.FetchAndSubSequentiallyConsistent(static_cast<ssize_t>(freed_bytes)); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1648 | if (Runtime::Current()->HasStatsEnabled()) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1649 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1650 | thread_stats->freed_objects += freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1651 | thread_stats->freed_bytes += freed_bytes; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1652 | // TODO: Do this concurrently. |
| 1653 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 1654 | global_stats->freed_objects += freed_objects; |
| 1655 | global_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1656 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1659 | void Heap::RecordFreeRevoke() { |
| 1660 | // Subtract num_bytes_freed_revoke_ from num_bytes_allocated_ to cancel out the |
| 1661 | // the ahead-of-time, bulk counting of bytes allocated in rosalloc thread-local buffers. |
| 1662 | // If there's a concurrent revoke, ok to not necessarily reset num_bytes_freed_revoke_ |
| 1663 | // all the way to zero exactly as the remainder will be subtracted at the next GC. |
| 1664 | size_t bytes_freed = num_bytes_freed_revoke_.LoadSequentiallyConsistent(); |
| 1665 | CHECK_GE(num_bytes_freed_revoke_.FetchAndSubSequentiallyConsistent(bytes_freed), |
| 1666 | bytes_freed) << "num_bytes_freed_revoke_ underflow"; |
| 1667 | CHECK_GE(num_bytes_allocated_.FetchAndSubSequentiallyConsistent(bytes_freed), |
| 1668 | bytes_freed) << "num_bytes_allocated_ underflow"; |
| 1669 | GetCurrentGcIteration()->SetFreedRevoke(bytes_freed); |
| 1670 | } |
| 1671 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1672 | space::RosAllocSpace* Heap::GetRosAllocSpace(gc::allocator::RosAlloc* rosalloc) const { |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 1673 | if (rosalloc_space_ != nullptr && rosalloc_space_->GetRosAlloc() == rosalloc) { |
| 1674 | return rosalloc_space_; |
| 1675 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1676 | for (const auto& space : continuous_spaces_) { |
| 1677 | if (space->AsContinuousSpace()->IsRosAllocSpace()) { |
| 1678 | if (space->AsContinuousSpace()->AsRosAllocSpace()->GetRosAlloc() == rosalloc) { |
| 1679 | return space->AsContinuousSpace()->AsRosAllocSpace(); |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | return nullptr; |
| 1684 | } |
| 1685 | |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1686 | static inline bool EntrypointsInstrumented() SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1687 | instrumentation::Instrumentation* const instrumentation = |
| 1688 | Runtime::Current()->GetInstrumentation(); |
| 1689 | return instrumentation != nullptr && instrumentation->AllocEntrypointsInstrumented(); |
| 1690 | } |
| 1691 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1692 | mirror::Object* Heap::AllocateInternalWithGc(Thread* self, |
| 1693 | AllocatorType allocator, |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1694 | bool instrumented, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1695 | size_t alloc_size, |
| 1696 | size_t* bytes_allocated, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1697 | size_t* usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1698 | size_t* bytes_tl_bulk_allocated, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1699 | mirror::Class** klass) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1700 | bool was_default_allocator = allocator == GetCurrentAllocator(); |
Mathieu Chartier | f4f3843 | 2014-09-03 11:21:08 -0700 | [diff] [blame] | 1701 | // Make sure there is no pending exception since we may need to throw an OOME. |
| 1702 | self->AssertNoPendingException(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1703 | DCHECK(klass != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1704 | StackHandleScope<1> hs(self); |
| 1705 | HandleWrapper<mirror::Class> h(hs.NewHandleWrapper(klass)); |
| 1706 | klass = nullptr; // Invalidate for safety. |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1707 | // The allocation failed. If the GC is running, block until it completes, and then retry the |
| 1708 | // allocation. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 1709 | collector::GcType last_gc = WaitForGcToComplete(kGcCauseForAlloc, self); |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1710 | // If we were the default allocator but the allocator changed while we were suspended, |
| 1711 | // abort the allocation. |
| 1712 | if ((was_default_allocator && allocator != GetCurrentAllocator()) || |
| 1713 | (!instrumented && EntrypointsInstrumented())) { |
| 1714 | return nullptr; |
| 1715 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1716 | if (last_gc != collector::kGcTypeNone) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1717 | // A GC was in progress and we blocked, retry allocation now that memory has been freed. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1718 | mirror::Object* ptr = TryToAllocate<true, false>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1719 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1720 | if (ptr != nullptr) { |
| 1721 | return ptr; |
| 1722 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Mathieu Chartier | 5ae2c93 | 2014-03-28 16:22:20 -0700 | [diff] [blame] | 1725 | collector::GcType tried_type = next_gc_type_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1726 | const bool gc_ran = |
| 1727 | CollectGarbageInternal(tried_type, kGcCauseForAlloc, false) != collector::kGcTypeNone; |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1728 | if ((was_default_allocator && allocator != GetCurrentAllocator()) || |
| 1729 | (!instrumented && EntrypointsInstrumented())) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1730 | return nullptr; |
| 1731 | } |
| 1732 | if (gc_ran) { |
| 1733 | mirror::Object* ptr = TryToAllocate<true, false>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1734 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1735 | if (ptr != nullptr) { |
| 1736 | return ptr; |
Mathieu Chartier | 5ae2c93 | 2014-03-28 16:22:20 -0700 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1740 | // 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] | 1741 | for (collector::GcType gc_type : gc_plan_) { |
Mathieu Chartier | 5ae2c93 | 2014-03-28 16:22:20 -0700 | [diff] [blame] | 1742 | if (gc_type == tried_type) { |
| 1743 | continue; |
| 1744 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1745 | // Attempt to run the collector, if we succeed, re-try the allocation. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1746 | const bool plan_gc_ran = |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1747 | CollectGarbageInternal(gc_type, kGcCauseForAlloc, false) != collector::kGcTypeNone; |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1748 | if ((was_default_allocator && allocator != GetCurrentAllocator()) || |
| 1749 | (!instrumented && EntrypointsInstrumented())) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1750 | return nullptr; |
| 1751 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1752 | if (plan_gc_ran) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1753 | // Did we free sufficient memory for the allocation to succeed? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1754 | mirror::Object* ptr = TryToAllocate<true, false>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1755 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1756 | if (ptr != nullptr) { |
| 1757 | return ptr; |
| 1758 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1759 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1760 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1761 | // Allocations have failed after GCs; this is an exceptional state. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1762 | // Try harder, growing the heap if necessary. |
| 1763 | mirror::Object* ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1764 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1765 | if (ptr != nullptr) { |
| 1766 | return ptr; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1767 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1768 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 1769 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 1770 | // VM spec requires that all SoftReferences have been collected and cleared before throwing |
| 1771 | // OOME. |
| 1772 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) |
| 1773 | << " allocation"; |
| 1774 | // TODO: Run finalization, but this may cause more allocations to occur. |
| 1775 | // We don't need a WaitForGcToComplete here either. |
| 1776 | DCHECK(!gc_plan_.empty()); |
| 1777 | CollectGarbageInternal(gc_plan_.back(), kGcCauseForAlloc, true); |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1778 | if ((was_default_allocator && allocator != GetCurrentAllocator()) || |
| 1779 | (!instrumented && EntrypointsInstrumented())) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1780 | return nullptr; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1781 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1782 | ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, usable_size, |
| 1783 | bytes_tl_bulk_allocated); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1784 | if (ptr == nullptr) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1785 | const uint64_t current_time = NanoTime(); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1786 | switch (allocator) { |
| 1787 | case kAllocatorTypeRosAlloc: |
| 1788 | // Fall-through. |
| 1789 | case kAllocatorTypeDlMalloc: { |
| 1790 | if (use_homogeneous_space_compaction_for_oom_ && |
| 1791 | current_time - last_time_homogeneous_space_compaction_by_oom_ > |
| 1792 | min_interval_homogeneous_space_compaction_by_oom_) { |
| 1793 | last_time_homogeneous_space_compaction_by_oom_ = current_time; |
| 1794 | HomogeneousSpaceCompactResult result = PerformHomogeneousSpaceCompact(); |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1795 | // Thread suspension could have occurred. |
| 1796 | if ((was_default_allocator && allocator != GetCurrentAllocator()) || |
| 1797 | (!instrumented && EntrypointsInstrumented())) { |
| 1798 | return nullptr; |
| 1799 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1800 | switch (result) { |
| 1801 | case HomogeneousSpaceCompactResult::kSuccess: |
| 1802 | // If the allocation succeeded, we delayed an oom. |
| 1803 | ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1804 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1805 | if (ptr != nullptr) { |
| 1806 | count_delayed_oom_++; |
| 1807 | } |
| 1808 | break; |
| 1809 | case HomogeneousSpaceCompactResult::kErrorReject: |
| 1810 | // Reject due to disabled moving GC. |
| 1811 | break; |
| 1812 | case HomogeneousSpaceCompactResult::kErrorVMShuttingDown: |
| 1813 | // Throw OOM by default. |
| 1814 | break; |
| 1815 | default: { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1816 | UNIMPLEMENTED(FATAL) << "homogeneous space compaction result: " |
| 1817 | << static_cast<size_t>(result); |
| 1818 | UNREACHABLE(); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1819 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1820 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1821 | // Always print that we ran homogeneous space compation since this can cause jank. |
| 1822 | VLOG(heap) << "Ran heap homogeneous space compaction, " |
| 1823 | << " requested defragmentation " |
| 1824 | << count_requested_homogeneous_space_compaction_.LoadSequentiallyConsistent() |
| 1825 | << " performed defragmentation " |
| 1826 | << count_performed_homogeneous_space_compaction_.LoadSequentiallyConsistent() |
| 1827 | << " ignored homogeneous space compaction " |
| 1828 | << count_ignored_homogeneous_space_compaction_.LoadSequentiallyConsistent() |
| 1829 | << " delayed count = " |
| 1830 | << count_delayed_oom_.LoadSequentiallyConsistent(); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1831 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1832 | break; |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1833 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1834 | case kAllocatorTypeNonMoving: { |
| 1835 | // Try to transition the heap if the allocation failure was due to the space being full. |
| 1836 | if (!IsOutOfMemoryOnAllocation<false>(allocator, alloc_size)) { |
| 1837 | // If we aren't out of memory then the OOM was probably from the non moving space being |
| 1838 | // full. Attempt to disable compaction and turn the main space into a non moving space. |
| 1839 | DisableMovingGc(); |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1840 | // Thread suspension could have occurred. |
| 1841 | if ((was_default_allocator && allocator != GetCurrentAllocator()) || |
| 1842 | (!instrumented && EntrypointsInstrumented())) { |
| 1843 | return nullptr; |
| 1844 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1845 | // If we are still a moving GC then something must have caused the transition to fail. |
| 1846 | if (IsMovingGc(collector_type_)) { |
| 1847 | MutexLock mu(self, *gc_complete_lock_); |
| 1848 | // If we couldn't disable moving GC, just throw OOME and return null. |
| 1849 | LOG(WARNING) << "Couldn't disable moving GC with disable GC count " |
| 1850 | << disable_moving_gc_count_; |
| 1851 | } else { |
| 1852 | LOG(WARNING) << "Disabled moving GC due to the non moving space being full"; |
| 1853 | ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1854 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1855 | } |
| 1856 | } |
| 1857 | break; |
| 1858 | } |
| 1859 | default: { |
| 1860 | // Do nothing for others allocators. |
| 1861 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | // If the allocation hasn't succeeded by this point, throw an OOM error. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1865 | if (ptr == nullptr) { |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1866 | ThrowOutOfMemoryError(self, alloc_size, allocator); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1867 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1868 | return ptr; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1871 | void Heap::SetTargetHeapUtilization(float target) { |
| 1872 | DCHECK_GT(target, 0.0f); // asserted in Java code |
| 1873 | DCHECK_LT(target, 1.0f); |
| 1874 | target_utilization_ = target; |
| 1875 | } |
| 1876 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1877 | size_t Heap::GetObjectsAllocated() const { |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 1878 | Thread* const self = Thread::Current(); |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 1879 | ScopedThreadStateChange tsc(self, kWaitingForGetObjectsAllocated); |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 1880 | // Need SuspendAll here to prevent lock violation if RosAlloc does it during InspectAll. |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 1881 | ScopedSuspendAll ssa(__FUNCTION__); |
| 1882 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1883 | size_t total = 0; |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 1884 | for (space::AllocSpace* space : alloc_spaces_) { |
| 1885 | total += space->GetObjectsAllocated(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1886 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1887 | return total; |
| 1888 | } |
| 1889 | |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1890 | uint64_t Heap::GetObjectsAllocatedEver() const { |
Mathieu Chartier | 4edd847 | 2015-06-01 10:47:36 -0700 | [diff] [blame] | 1891 | uint64_t total = GetObjectsFreedEver(); |
| 1892 | // If we are detached, we can't use GetObjectsAllocated since we can't change thread states. |
| 1893 | if (Thread::Current() != nullptr) { |
| 1894 | total += GetObjectsAllocated(); |
| 1895 | } |
| 1896 | return total; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1899 | uint64_t Heap::GetBytesAllocatedEver() const { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 1900 | return GetBytesFreedEver() + GetBytesAllocated(); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1903 | class InstanceCounter { |
| 1904 | public: |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1905 | InstanceCounter(const std::vector<mirror::Class*>& classes, |
| 1906 | bool use_is_assignable_from, |
| 1907 | uint64_t* counts) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1908 | SHARED_REQUIRES(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1909 | : classes_(classes), use_is_assignable_from_(use_is_assignable_from), counts_(counts) {} |
| 1910 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1911 | static void Callback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1912 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1913 | InstanceCounter* instance_counter = reinterpret_cast<InstanceCounter*>(arg); |
| 1914 | mirror::Class* instance_class = obj->GetClass(); |
| 1915 | CHECK(instance_class != nullptr); |
| 1916 | for (size_t i = 0; i < instance_counter->classes_.size(); ++i) { |
Mathieu Chartier | f182085 | 2015-07-10 13:19:51 -0700 | [diff] [blame] | 1917 | mirror::Class* klass = instance_counter->classes_[i]; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1918 | if (instance_counter->use_is_assignable_from_) { |
Mathieu Chartier | f182085 | 2015-07-10 13:19:51 -0700 | [diff] [blame] | 1919 | if (klass != nullptr && klass->IsAssignableFrom(instance_class)) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1920 | ++instance_counter->counts_[i]; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1921 | } |
Mathieu Chartier | f182085 | 2015-07-10 13:19:51 -0700 | [diff] [blame] | 1922 | } else if (instance_class == klass) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1923 | ++instance_counter->counts_[i]; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1928 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1929 | const std::vector<mirror::Class*>& classes_; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1930 | bool use_is_assignable_from_; |
| 1931 | uint64_t* const counts_; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1932 | DISALLOW_COPY_AND_ASSIGN(InstanceCounter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1933 | }; |
| 1934 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1935 | 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] | 1936 | uint64_t* counts) { |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1937 | InstanceCounter counter(classes, use_is_assignable_from, counts); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1938 | VisitObjects(InstanceCounter::Callback, &counter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1939 | } |
| 1940 | |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1941 | class InstanceCollector { |
| 1942 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1943 | InstanceCollector(mirror::Class* c, int32_t max_count, std::vector<mirror::Object*>& instances) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1944 | SHARED_REQUIRES(Locks::mutator_lock_) |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1945 | : class_(c), max_count_(max_count), instances_(instances) { |
| 1946 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1947 | static void Callback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1948 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1949 | DCHECK(arg != nullptr); |
| 1950 | InstanceCollector* instance_collector = reinterpret_cast<InstanceCollector*>(arg); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1951 | if (obj->GetClass() == instance_collector->class_) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1952 | if (instance_collector->max_count_ == 0 || |
| 1953 | instance_collector->instances_.size() < instance_collector->max_count_) { |
| 1954 | instance_collector->instances_.push_back(obj); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | private: |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1960 | const mirror::Class* const class_; |
| 1961 | const uint32_t max_count_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1962 | std::vector<mirror::Object*>& instances_; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1963 | DISALLOW_COPY_AND_ASSIGN(InstanceCollector); |
| 1964 | }; |
| 1965 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1966 | void Heap::GetInstances(mirror::Class* c, |
| 1967 | int32_t max_count, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1968 | std::vector<mirror::Object*>& instances) { |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1969 | InstanceCollector collector(c, max_count, instances); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1970 | VisitObjects(&InstanceCollector::Callback, &collector); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1971 | } |
| 1972 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1973 | class ReferringObjectsFinder { |
| 1974 | public: |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1975 | ReferringObjectsFinder(mirror::Object* object, |
| 1976 | int32_t max_count, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1977 | std::vector<mirror::Object*>& referring_objects) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1978 | SHARED_REQUIRES(Locks::mutator_lock_) |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1979 | : object_(object), max_count_(max_count), referring_objects_(referring_objects) { |
| 1980 | } |
| 1981 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1982 | static void Callback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1983 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1984 | reinterpret_cast<ReferringObjectsFinder*>(arg)->operator()(obj); |
| 1985 | } |
| 1986 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1987 | // For bitmap Visit. |
| 1988 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for |
| 1989 | // annotalysis on visitors. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 1990 | void operator()(mirror::Object* o) const NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1991 | o->VisitReferences(*this, VoidFunctor()); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1994 | // For Object::VisitReferences. |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1995 | void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1996 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1997 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1998 | if (ref == object_ && (max_count_ == 0 || referring_objects_.size() < max_count_)) { |
| 1999 | referring_objects_.push_back(obj); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2003 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 2004 | const {} |
| 2005 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 2006 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 2007 | private: |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2008 | const mirror::Object* const object_; |
| 2009 | const uint32_t max_count_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2010 | std::vector<mirror::Object*>& referring_objects_; |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 2011 | DISALLOW_COPY_AND_ASSIGN(ReferringObjectsFinder); |
| 2012 | }; |
| 2013 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2014 | void Heap::GetReferringObjects(mirror::Object* o, int32_t max_count, |
| 2015 | std::vector<mirror::Object*>& referring_objects) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 2016 | ReferringObjectsFinder finder(o, max_count, referring_objects); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 2017 | VisitObjects(&ReferringObjectsFinder::Callback, &finder); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 2018 | } |
| 2019 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2020 | void Heap::CollectGarbage(bool clear_soft_references) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 2021 | // Even if we waited for a GC we still need to do another GC since weaks allocated during the |
| 2022 | // last GC will not have necessarily been cleared. |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2023 | CollectGarbageInternal(gc_plan_.back(), kGcCauseExplicit, clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
Mathieu Chartier | db00eaf | 2015-08-31 17:10:05 -0700 | [diff] [blame] | 2026 | bool Heap::SupportHomogeneousSpaceCompactAndCollectorTransitions() const { |
| 2027 | return main_space_backup_.get() != nullptr && main_space_ != nullptr && |
| 2028 | foreground_collector_type_ == kCollectorTypeCMS; |
| 2029 | } |
| 2030 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2031 | HomogeneousSpaceCompactResult Heap::PerformHomogeneousSpaceCompact() { |
| 2032 | Thread* self = Thread::Current(); |
| 2033 | // Inc requested homogeneous space compaction. |
| 2034 | count_requested_homogeneous_space_compaction_++; |
| 2035 | // Store performed homogeneous space compaction at a new request arrival. |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2036 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
| 2037 | Locks::mutator_lock_->AssertNotHeld(self); |
| 2038 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2039 | ScopedThreadStateChange tsc2(self, kWaitingForGcToComplete); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2040 | MutexLock mu(self, *gc_complete_lock_); |
| 2041 | // Ensure there is only one GC at a time. |
| 2042 | WaitForGcToCompleteLocked(kGcCauseHomogeneousSpaceCompact, self); |
| 2043 | // Homogeneous space compaction is a copying transition, can't run it if the moving GC disable count |
| 2044 | // is non zero. |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 2045 | // If the collector type changed to something which doesn't benefit from homogeneous space compaction, |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2046 | // exit. |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 2047 | if (disable_moving_gc_count_ != 0 || IsMovingGc(collector_type_) || |
| 2048 | !main_space_->CanMoveObjects()) { |
Mathieu Chartier | db00eaf | 2015-08-31 17:10:05 -0700 | [diff] [blame] | 2049 | return kErrorReject; |
| 2050 | } |
| 2051 | if (!SupportHomogeneousSpaceCompactAndCollectorTransitions()) { |
| 2052 | return kErrorUnsupported; |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2053 | } |
| 2054 | collector_type_running_ = kCollectorTypeHomogeneousSpaceCompact; |
| 2055 | } |
| 2056 | if (Runtime::Current()->IsShuttingDown(self)) { |
| 2057 | // Don't allow heap transitions to happen if the runtime is shutting down since these can |
| 2058 | // cause objects to get finalized. |
| 2059 | FinishGC(self, collector::kGcTypeNone); |
| 2060 | return HomogeneousSpaceCompactResult::kErrorVMShuttingDown; |
| 2061 | } |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 2062 | collector::GarbageCollector* collector; |
| 2063 | { |
| 2064 | ScopedSuspendAll ssa(__FUNCTION__); |
| 2065 | uint64_t start_time = NanoTime(); |
| 2066 | // Launch compaction. |
| 2067 | space::MallocSpace* to_space = main_space_backup_.release(); |
| 2068 | space::MallocSpace* from_space = main_space_; |
| 2069 | to_space->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2070 | const uint64_t space_size_before_compaction = from_space->Size(); |
| 2071 | AddSpace(to_space); |
| 2072 | // Make sure that we will have enough room to copy. |
| 2073 | CHECK_GE(to_space->GetFootprintLimit(), from_space->GetFootprintLimit()); |
| 2074 | collector = Compact(to_space, from_space, kGcCauseHomogeneousSpaceCompact); |
| 2075 | const uint64_t space_size_after_compaction = to_space->Size(); |
| 2076 | main_space_ = to_space; |
| 2077 | main_space_backup_.reset(from_space); |
| 2078 | RemoveSpace(from_space); |
| 2079 | SetSpaceAsDefault(main_space_); // Set as default to reset the proper dlmalloc space. |
| 2080 | // Update performed homogeneous space compaction count. |
| 2081 | count_performed_homogeneous_space_compaction_++; |
| 2082 | // Print statics log and resume all threads. |
| 2083 | uint64_t duration = NanoTime() - start_time; |
| 2084 | VLOG(heap) << "Heap homogeneous space compaction took " << PrettyDuration(duration) << " size: " |
| 2085 | << PrettySize(space_size_before_compaction) << " -> " |
| 2086 | << PrettySize(space_size_after_compaction) << " compact-ratio: " |
| 2087 | << std::fixed << static_cast<double>(space_size_after_compaction) / |
| 2088 | static_cast<double>(space_size_before_compaction); |
| 2089 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2090 | // Finish GC. |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 2091 | reference_processor_->EnqueueClearedReferences(self); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2092 | GrowForUtilization(semi_space_collector_); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2093 | LogGC(kGcCauseHomogeneousSpaceCompact, collector); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2094 | FinishGC(self, collector::kGcTypeFull); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 2095 | { |
| 2096 | ScopedObjectAccess soa(self); |
| 2097 | soa.Vm()->UnloadNativeLibraries(); |
| 2098 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2099 | return HomogeneousSpaceCompactResult::kSuccess; |
| 2100 | } |
| 2101 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2102 | void Heap::TransitionCollector(CollectorType collector_type) { |
| 2103 | if (collector_type == collector_type_) { |
| 2104 | return; |
| 2105 | } |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2106 | VLOG(heap) << "TransitionCollector: " << static_cast<int>(collector_type_) |
| 2107 | << " -> " << static_cast<int>(collector_type); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2108 | uint64_t start_time = NanoTime(); |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 2109 | uint32_t before_allocated = num_bytes_allocated_.LoadSequentiallyConsistent(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2110 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2111 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2112 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
| 2113 | Locks::mutator_lock_->AssertNotHeld(self); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 2114 | // Busy wait until we can GC (StartGC can fail if we have a non-zero |
| 2115 | // compacting_gc_disable_count_, this should rarely occurs). |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2116 | for (;;) { |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 2117 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2118 | ScopedThreadStateChange tsc2(self, kWaitingForGcToComplete); |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 2119 | MutexLock mu(self, *gc_complete_lock_); |
| 2120 | // Ensure there is only one GC at a time. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 2121 | WaitForGcToCompleteLocked(kGcCauseCollectorTransition, self); |
Mathieu Chartier | e4927f6 | 2014-08-23 13:56:03 -0700 | [diff] [blame] | 2122 | // Currently we only need a heap transition if we switch from a moving collector to a |
| 2123 | // non-moving one, or visa versa. |
| 2124 | const bool copying_transition = IsMovingGc(collector_type_) != IsMovingGc(collector_type); |
Mathieu Chartier | b38d483 | 2014-04-10 10:56:55 -0700 | [diff] [blame] | 2125 | // If someone else beat us to it and changed the collector before we could, exit. |
| 2126 | // This is safe to do before the suspend all since we set the collector_type_running_ before |
| 2127 | // we exit the loop. If another thread attempts to do the heap transition before we exit, |
| 2128 | // then it would get blocked on WaitForGcToCompleteLocked. |
| 2129 | if (collector_type == collector_type_) { |
| 2130 | return; |
| 2131 | } |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 2132 | // GC can be disabled if someone has a used GetPrimitiveArrayCritical but not yet released. |
| 2133 | if (!copying_transition || disable_moving_gc_count_ == 0) { |
| 2134 | // TODO: Not hard code in semi-space collector? |
| 2135 | collector_type_running_ = copying_transition ? kCollectorTypeSS : collector_type; |
| 2136 | break; |
| 2137 | } |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2138 | } |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 2139 | usleep(1000); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2140 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2141 | if (runtime->IsShuttingDown(self)) { |
Hiroshi Yamauchi | a6a8d14 | 2014-05-12 16:57:33 -0700 | [diff] [blame] | 2142 | // Don't allow heap transitions to happen if the runtime is shutting down since these can |
| 2143 | // cause objects to get finalized. |
| 2144 | FinishGC(self, collector::kGcTypeNone); |
| 2145 | return; |
| 2146 | } |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2147 | collector::GarbageCollector* collector = nullptr; |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 2148 | { |
| 2149 | ScopedSuspendAll ssa(__FUNCTION__); |
| 2150 | switch (collector_type) { |
| 2151 | case kCollectorTypeSS: { |
| 2152 | if (!IsMovingGc(collector_type_)) { |
| 2153 | // Create the bump pointer space from the backup space. |
| 2154 | CHECK(main_space_backup_ != nullptr); |
| 2155 | std::unique_ptr<MemMap> mem_map(main_space_backup_->ReleaseMemMap()); |
| 2156 | // We are transitioning from non moving GC -> moving GC, since we copied from the bump |
| 2157 | // pointer space last transition it will be protected. |
| 2158 | CHECK(mem_map != nullptr); |
Hiroshi Yamauchi | c1276c8 | 2014-08-07 10:27:17 -0700 | [diff] [blame] | 2159 | mem_map->Protect(PROT_READ | PROT_WRITE); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 2160 | bump_pointer_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space", |
| 2161 | mem_map.release()); |
| 2162 | AddSpace(bump_pointer_space_); |
| 2163 | collector = Compact(bump_pointer_space_, main_space_, kGcCauseCollectorTransition); |
| 2164 | // Use the now empty main space mem map for the bump pointer temp space. |
| 2165 | mem_map.reset(main_space_->ReleaseMemMap()); |
| 2166 | // Unset the pointers just in case. |
| 2167 | if (dlmalloc_space_ == main_space_) { |
| 2168 | dlmalloc_space_ = nullptr; |
| 2169 | } else if (rosalloc_space_ == main_space_) { |
| 2170 | rosalloc_space_ = nullptr; |
| 2171 | } |
| 2172 | // Remove the main space so that we don't try to trim it, this doens't work for debug |
| 2173 | // builds since RosAlloc attempts to read the magic number from a protected page. |
| 2174 | RemoveSpace(main_space_); |
| 2175 | RemoveRememberedSet(main_space_); |
| 2176 | delete main_space_; // Delete the space since it has been removed. |
| 2177 | main_space_ = nullptr; |
| 2178 | RemoveRememberedSet(main_space_backup_.get()); |
| 2179 | main_space_backup_.reset(nullptr); // Deletes the space. |
| 2180 | temp_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 2", |
| 2181 | mem_map.release()); |
| 2182 | AddSpace(temp_space_); |
Hiroshi Yamauchi | c1276c8 | 2014-08-07 10:27:17 -0700 | [diff] [blame] | 2183 | } |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 2184 | break; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2185 | } |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 2186 | case kCollectorTypeMS: |
| 2187 | // Fall through. |
| 2188 | case kCollectorTypeCMS: { |
| 2189 | if (IsMovingGc(collector_type_)) { |
| 2190 | CHECK(temp_space_ != nullptr); |
| 2191 | std::unique_ptr<MemMap> mem_map(temp_space_->ReleaseMemMap()); |
| 2192 | RemoveSpace(temp_space_); |
| 2193 | temp_space_ = nullptr; |
| 2194 | mem_map->Protect(PROT_READ | PROT_WRITE); |
| 2195 | CreateMainMallocSpace(mem_map.get(), |
| 2196 | kDefaultInitialSize, |
| 2197 | std::min(mem_map->Size(), growth_limit_), |
| 2198 | mem_map->Size()); |
| 2199 | mem_map.release(); |
| 2200 | // Compact to the main space from the bump pointer space, don't need to swap semispaces. |
| 2201 | AddSpace(main_space_); |
| 2202 | collector = Compact(main_space_, bump_pointer_space_, kGcCauseCollectorTransition); |
| 2203 | mem_map.reset(bump_pointer_space_->ReleaseMemMap()); |
| 2204 | RemoveSpace(bump_pointer_space_); |
| 2205 | bump_pointer_space_ = nullptr; |
| 2206 | const char* name = kUseRosAlloc ? kRosAllocSpaceName[1] : kDlMallocSpaceName[1]; |
| 2207 | // Temporarily unprotect the backup mem map so rosalloc can write the debug magic number. |
| 2208 | if (kIsDebugBuild && kUseRosAlloc) { |
| 2209 | mem_map->Protect(PROT_READ | PROT_WRITE); |
| 2210 | } |
| 2211 | main_space_backup_.reset(CreateMallocSpaceFromMemMap( |
| 2212 | mem_map.get(), |
| 2213 | kDefaultInitialSize, |
| 2214 | std::min(mem_map->Size(), growth_limit_), |
| 2215 | mem_map->Size(), |
| 2216 | name, |
| 2217 | true)); |
| 2218 | if (kIsDebugBuild && kUseRosAlloc) { |
| 2219 | mem_map->Protect(PROT_NONE); |
| 2220 | } |
| 2221 | mem_map.release(); |
| 2222 | } |
| 2223 | break; |
| 2224 | } |
| 2225 | default: { |
| 2226 | LOG(FATAL) << "Attempted to transition to invalid collector type " |
| 2227 | << static_cast<size_t>(collector_type); |
| 2228 | break; |
| 2229 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2230 | } |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 2231 | ChangeCollector(collector_type); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2232 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2233 | // Can't call into java code with all threads suspended. |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 2234 | reference_processor_->EnqueueClearedReferences(self); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2235 | uint64_t duration = NanoTime() - start_time; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 2236 | GrowForUtilization(semi_space_collector_); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2237 | DCHECK(collector != nullptr); |
| 2238 | LogGC(kGcCauseCollectorTransition, collector); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2239 | FinishGC(self, collector::kGcTypeFull); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 2240 | { |
| 2241 | ScopedObjectAccess soa(self); |
| 2242 | soa.Vm()->UnloadNativeLibraries(); |
| 2243 | } |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 2244 | int32_t after_allocated = num_bytes_allocated_.LoadSequentiallyConsistent(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2245 | int32_t delta_allocated = before_allocated - after_allocated; |
Mathieu Chartier | 19d46b4 | 2014-06-17 15:04:40 -0700 | [diff] [blame] | 2246 | std::string saved_str; |
| 2247 | if (delta_allocated >= 0) { |
| 2248 | saved_str = " saved at least " + PrettySize(delta_allocated); |
| 2249 | } else { |
| 2250 | saved_str = " expanded " + PrettySize(-delta_allocated); |
| 2251 | } |
Mathieu Chartier | f8cb178 | 2016-03-18 18:45:41 -0700 | [diff] [blame] | 2252 | VLOG(heap) << "Collector transition to " << collector_type << " took " |
| 2253 | << PrettyDuration(duration) << saved_str; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2254 | } |
| 2255 | |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 2256 | void Heap::ChangeCollector(CollectorType collector_type) { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2257 | // TODO: Only do this with all mutators suspended to avoid races. |
| 2258 | if (collector_type != collector_type_) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2259 | if (collector_type == kCollectorTypeMC) { |
| 2260 | // Don't allow mark compact unless support is compiled in. |
| 2261 | CHECK(kMarkCompactSupport); |
| 2262 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2263 | collector_type_ = collector_type; |
| 2264 | gc_plan_.clear(); |
| 2265 | switch (collector_type_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2266 | case kCollectorTypeCC: { |
| 2267 | gc_plan_.push_back(collector::kGcTypeFull); |
| 2268 | if (use_tlab_) { |
| 2269 | ChangeAllocator(kAllocatorTypeRegionTLAB); |
| 2270 | } else { |
| 2271 | ChangeAllocator(kAllocatorTypeRegion); |
| 2272 | } |
| 2273 | break; |
| 2274 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2275 | case kCollectorTypeMC: // Fall-through. |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 2276 | case kCollectorTypeSS: // Fall-through. |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 2277 | case kCollectorTypeGSS: { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2278 | gc_plan_.push_back(collector::kGcTypeFull); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 2279 | if (use_tlab_) { |
| 2280 | ChangeAllocator(kAllocatorTypeTLAB); |
| 2281 | } else { |
| 2282 | ChangeAllocator(kAllocatorTypeBumpPointer); |
| 2283 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2284 | break; |
| 2285 | } |
| 2286 | case kCollectorTypeMS: { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2287 | gc_plan_.push_back(collector::kGcTypeSticky); |
| 2288 | gc_plan_.push_back(collector::kGcTypePartial); |
| 2289 | gc_plan_.push_back(collector::kGcTypeFull); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2290 | ChangeAllocator(kUseRosAlloc ? kAllocatorTypeRosAlloc : kAllocatorTypeDlMalloc); |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2291 | break; |
| 2292 | } |
| 2293 | case kCollectorTypeCMS: { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2294 | gc_plan_.push_back(collector::kGcTypeSticky); |
| 2295 | gc_plan_.push_back(collector::kGcTypePartial); |
| 2296 | gc_plan_.push_back(collector::kGcTypeFull); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2297 | ChangeAllocator(kUseRosAlloc ? kAllocatorTypeRosAlloc : kAllocatorTypeDlMalloc); |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2298 | break; |
| 2299 | } |
| 2300 | default: { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 2301 | UNIMPLEMENTED(FATAL); |
| 2302 | UNREACHABLE(); |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2303 | } |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 2304 | } |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 2305 | if (IsGcConcurrent()) { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2306 | concurrent_start_bytes_ = |
| 2307 | std::max(max_allowed_footprint_, kMinConcurrentRemainingBytes) - kMinConcurrentRemainingBytes; |
| 2308 | } else { |
| 2309 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2314 | // Special compacting collector which uses sub-optimal bin packing to reduce zygote space size. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2315 | class ZygoteCompactingCollector FINAL : public collector::SemiSpace { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2316 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 2317 | ZygoteCompactingCollector(gc::Heap* heap, bool is_running_on_memory_tool) |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2318 | : SemiSpace(heap, false, "zygote collector"), |
| 2319 | bin_live_bitmap_(nullptr), |
| 2320 | bin_mark_bitmap_(nullptr), |
| 2321 | is_running_on_memory_tool_(is_running_on_memory_tool) {} |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2322 | |
| 2323 | void BuildBins(space::ContinuousSpace* space) { |
| 2324 | bin_live_bitmap_ = space->GetLiveBitmap(); |
| 2325 | bin_mark_bitmap_ = space->GetMarkBitmap(); |
| 2326 | BinContext context; |
| 2327 | context.prev_ = reinterpret_cast<uintptr_t>(space->Begin()); |
| 2328 | context.collector_ = this; |
| 2329 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 2330 | // Note: This requires traversing the space in increasing order of object addresses. |
| 2331 | bin_live_bitmap_->Walk(Callback, reinterpret_cast<void*>(&context)); |
| 2332 | // Add the last bin which spans after the last object to the end of the space. |
| 2333 | AddBin(reinterpret_cast<uintptr_t>(space->End()) - context.prev_, context.prev_); |
| 2334 | } |
| 2335 | |
| 2336 | private: |
| 2337 | struct BinContext { |
| 2338 | uintptr_t prev_; // The end of the previous object. |
| 2339 | ZygoteCompactingCollector* collector_; |
| 2340 | }; |
| 2341 | // Maps from bin sizes to locations. |
| 2342 | std::multimap<size_t, uintptr_t> bins_; |
| 2343 | // Live bitmap of the space which contains the bins. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2344 | accounting::ContinuousSpaceBitmap* bin_live_bitmap_; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2345 | // Mark bitmap of the space which contains the bins. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2346 | accounting::ContinuousSpaceBitmap* bin_mark_bitmap_; |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2347 | const bool is_running_on_memory_tool_; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2348 | |
| 2349 | static void Callback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2350 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2351 | DCHECK(arg != nullptr); |
| 2352 | BinContext* context = reinterpret_cast<BinContext*>(arg); |
| 2353 | ZygoteCompactingCollector* collector = context->collector_; |
| 2354 | uintptr_t object_addr = reinterpret_cast<uintptr_t>(obj); |
| 2355 | size_t bin_size = object_addr - context->prev_; |
| 2356 | // Add the bin consisting of the end of the previous object to the start of the current object. |
| 2357 | collector->AddBin(bin_size, context->prev_); |
| 2358 | context->prev_ = object_addr + RoundUp(obj->SizeOf(), kObjectAlignment); |
| 2359 | } |
| 2360 | |
| 2361 | void AddBin(size_t size, uintptr_t position) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2362 | if (is_running_on_memory_tool_) { |
| 2363 | MEMORY_TOOL_MAKE_DEFINED(reinterpret_cast<void*>(position), size); |
| 2364 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2365 | if (size != 0) { |
| 2366 | bins_.insert(std::make_pair(size, position)); |
| 2367 | } |
| 2368 | } |
| 2369 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 2370 | virtual bool ShouldSweepSpace(space::ContinuousSpace* space ATTRIBUTE_UNUSED) const { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2371 | // Don't sweep any spaces since we probably blasted the internal accounting of the free list |
| 2372 | // allocator. |
| 2373 | return false; |
| 2374 | } |
| 2375 | |
| 2376 | virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2377 | REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2378 | size_t obj_size = obj->SizeOf(); |
| 2379 | size_t alloc_size = RoundUp(obj_size, kObjectAlignment); |
Mathieu Chartier | 5dc08a6 | 2014-01-10 10:10:23 -0800 | [diff] [blame] | 2380 | mirror::Object* forward_address; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2381 | // Find the smallest bin which we can move obj in. |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2382 | auto it = bins_.lower_bound(alloc_size); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2383 | if (it == bins_.end()) { |
| 2384 | // No available space in the bins, place it in the target space instead (grows the zygote |
| 2385 | // space). |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2386 | size_t bytes_allocated, dummy; |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2387 | forward_address = to_space_->Alloc(self_, alloc_size, &bytes_allocated, nullptr, &dummy); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2388 | if (to_space_live_bitmap_ != nullptr) { |
| 2389 | to_space_live_bitmap_->Set(forward_address); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2390 | } else { |
| 2391 | GetHeap()->GetNonMovingSpace()->GetLiveBitmap()->Set(forward_address); |
| 2392 | GetHeap()->GetNonMovingSpace()->GetMarkBitmap()->Set(forward_address); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2393 | } |
| 2394 | } else { |
| 2395 | size_t size = it->first; |
| 2396 | uintptr_t pos = it->second; |
| 2397 | bins_.erase(it); // Erase the old bin which we replace with the new smaller bin. |
| 2398 | forward_address = reinterpret_cast<mirror::Object*>(pos); |
| 2399 | // Set the live and mark bits so that sweeping system weaks works properly. |
| 2400 | bin_live_bitmap_->Set(forward_address); |
| 2401 | bin_mark_bitmap_->Set(forward_address); |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2402 | DCHECK_GE(size, alloc_size); |
| 2403 | // Add a new bin with the remaining space. |
| 2404 | AddBin(size - alloc_size, pos + alloc_size); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2405 | } |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2406 | // Copy the object over to its new location. Don't use alloc_size to avoid valgrind error. |
| 2407 | memcpy(reinterpret_cast<void*>(forward_address), obj, obj_size); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 2408 | if (kUseBakerOrBrooksReadBarrier) { |
| 2409 | obj->AssertReadBarrierPointer(); |
| 2410 | if (kUseBrooksReadBarrier) { |
| 2411 | DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj); |
| 2412 | forward_address->SetReadBarrierPointer(forward_address); |
| 2413 | } |
| 2414 | forward_address->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 2415 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2416 | return forward_address; |
| 2417 | } |
| 2418 | }; |
| 2419 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2420 | void Heap::UnBindBitmaps() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 2421 | TimingLogger::ScopedTiming t("UnBindBitmaps", GetCurrentGcIteration()->GetTimings()); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2422 | for (const auto& space : GetContinuousSpaces()) { |
| 2423 | if (space->IsContinuousMemMapAllocSpace()) { |
| 2424 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
| 2425 | if (alloc_space->HasBoundBitmaps()) { |
| 2426 | alloc_space->UnBindBitmaps(); |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2432 | void Heap::PreZygoteFork() { |
Mathieu Chartier | faed995 | 2015-03-31 16:28:53 -0700 | [diff] [blame] | 2433 | if (!HasZygoteSpace()) { |
| 2434 | // We still want to GC in case there is some unreachable non moving objects that could cause a |
| 2435 | // suboptimal bin packing when we compact the zygote space. |
| 2436 | CollectGarbageInternal(collector::kGcTypeFull, kGcCauseBackground, false); |
Mathieu Chartier | 76ce917 | 2016-01-27 10:44:20 -0800 | [diff] [blame] | 2437 | // Trim the pages at the end of the non moving space. Trim while not holding zygote lock since |
| 2438 | // the trim process may require locking the mutator lock. |
| 2439 | non_moving_space_->Trim(); |
Mathieu Chartier | faed995 | 2015-03-31 16:28:53 -0700 | [diff] [blame] | 2440 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 2441 | Thread* self = Thread::Current(); |
| 2442 | MutexLock mu(self, zygote_creation_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2443 | // Try to see if we have any Zygote spaces. |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2444 | if (HasZygoteSpace()) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2445 | return; |
| 2446 | } |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2447 | Runtime::Current()->GetInternTable()->AddNewTable(); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2448 | Runtime::Current()->GetClassLinker()->MoveClassTableToPreZygote(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2449 | VLOG(heap) << "Starting PreZygoteFork"; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2450 | // The end of the non-moving space may be protected, unprotect it so that we can copy the zygote |
| 2451 | // there. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2452 | non_moving_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 2453 | const bool same_space = non_moving_space_ == main_space_; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2454 | if (kCompactZygote) { |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2455 | // Temporarily disable rosalloc verification because the zygote |
| 2456 | // compaction will mess up the rosalloc internal metadata. |
| 2457 | ScopedDisableRosAllocVerification disable_rosalloc_verif(this); |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2458 | ZygoteCompactingCollector zygote_collector(this, is_running_on_memory_tool_); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2459 | zygote_collector.BuildBins(non_moving_space_); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 2460 | // Create a new bump pointer space which we will compact into. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2461 | space::BumpPointerSpace target_space("zygote bump space", non_moving_space_->End(), |
| 2462 | non_moving_space_->Limit()); |
| 2463 | // Compact the bump pointer space to a new zygote bump pointer space. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2464 | bool reset_main_space = false; |
| 2465 | if (IsMovingGc(collector_type_)) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2466 | if (collector_type_ == kCollectorTypeCC) { |
| 2467 | zygote_collector.SetFromSpace(region_space_); |
| 2468 | } else { |
| 2469 | zygote_collector.SetFromSpace(bump_pointer_space_); |
| 2470 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2471 | } else { |
| 2472 | CHECK(main_space_ != nullptr); |
Hiroshi Yamauchi | d04495e | 2015-03-11 19:09:07 -0700 | [diff] [blame] | 2473 | CHECK_NE(main_space_, non_moving_space_) |
| 2474 | << "Does not make sense to compact within the same space"; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2475 | // Copy from the main space. |
| 2476 | zygote_collector.SetFromSpace(main_space_); |
| 2477 | reset_main_space = true; |
| 2478 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2479 | zygote_collector.SetToSpace(&target_space); |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 2480 | zygote_collector.SetSwapSemiSpaces(false); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 2481 | zygote_collector.Run(kGcCauseCollectorTransition, false); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2482 | if (reset_main_space) { |
| 2483 | main_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2484 | madvise(main_space_->Begin(), main_space_->Capacity(), MADV_DONTNEED); |
| 2485 | MemMap* mem_map = main_space_->ReleaseMemMap(); |
| 2486 | RemoveSpace(main_space_); |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 2487 | space::Space* old_main_space = main_space_; |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 2488 | CreateMainMallocSpace(mem_map, kDefaultInitialSize, std::min(mem_map->Size(), growth_limit_), |
| 2489 | mem_map->Size()); |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 2490 | delete old_main_space; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2491 | AddSpace(main_space_); |
| 2492 | } else { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2493 | if (collector_type_ == kCollectorTypeCC) { |
| 2494 | region_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2495 | } else { |
| 2496 | bump_pointer_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2497 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2498 | } |
| 2499 | if (temp_space_ != nullptr) { |
| 2500 | CHECK(temp_space_->IsEmpty()); |
| 2501 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2502 | total_objects_freed_ever_ += GetCurrentGcIteration()->GetFreedObjects(); |
| 2503 | total_bytes_freed_ever_ += GetCurrentGcIteration()->GetFreedBytes(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2504 | // Update the end and write out image. |
| 2505 | non_moving_space_->SetEnd(target_space.End()); |
| 2506 | non_moving_space_->SetLimit(target_space.Limit()); |
Mathieu Chartier | faed995 | 2015-03-31 16:28:53 -0700 | [diff] [blame] | 2507 | VLOG(heap) << "Create zygote space with size=" << non_moving_space_->Size() << " bytes"; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2508 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 2509 | // Change the collector to the post zygote one. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2510 | ChangeCollector(foreground_collector_type_); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2511 | // Save the old space so that we can remove it after we complete creating the zygote space. |
| 2512 | space::MallocSpace* old_alloc_space = non_moving_space_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2513 | // Turn the current alloc space into a zygote space and obtain the new alloc space composed of |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2514 | // the remaining available space. |
| 2515 | // Remove the old space before creating the zygote space since creating the zygote space sets |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2516 | // the old alloc space's bitmaps to null. |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2517 | RemoveSpace(old_alloc_space); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 2518 | if (collector::SemiSpace::kUseRememberedSet) { |
| 2519 | // Sanity bound check. |
| 2520 | FindRememberedSetFromSpace(old_alloc_space)->AssertAllDirtyCardsAreWithinSpace(); |
| 2521 | // Remove the remembered set for the now zygote space (the old |
| 2522 | // non-moving space). Note now that we have compacted objects into |
| 2523 | // the zygote space, the data in the remembered set is no longer |
| 2524 | // needed. The zygote space will instead have a mod-union table |
| 2525 | // from this point on. |
| 2526 | RemoveRememberedSet(old_alloc_space); |
| 2527 | } |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 2528 | // Remaining space becomes the new non moving space. |
| 2529 | zygote_space_ = old_alloc_space->CreateZygoteSpace(kNonMovingSpaceName, low_memory_mode_, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2530 | &non_moving_space_); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 2531 | CHECK(!non_moving_space_->CanMoveObjects()); |
| 2532 | if (same_space) { |
| 2533 | main_space_ = non_moving_space_; |
| 2534 | SetSpaceAsDefault(main_space_); |
| 2535 | } |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2536 | delete old_alloc_space; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2537 | CHECK(HasZygoteSpace()) << "Failed creating zygote space"; |
| 2538 | AddSpace(zygote_space_); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2539 | non_moving_space_->SetFootprintLimit(non_moving_space_->Capacity()); |
| 2540 | AddSpace(non_moving_space_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2541 | // Create the zygote space mod union table. |
| 2542 | accounting::ModUnionTable* mod_union_table = |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2543 | new accounting::ModUnionTableCardCache("zygote space mod-union table", this, |
| 2544 | zygote_space_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2545 | CHECK(mod_union_table != nullptr) << "Failed to create zygote space mod-union table"; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2546 | // Set all the cards in the mod-union table since we don't know which objects contain references |
| 2547 | // to large objects. |
| 2548 | mod_union_table->SetCards(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2549 | AddModUnionTable(mod_union_table); |
Mathieu Chartier | f6c2a27 | 2015-06-03 17:32:42 -0700 | [diff] [blame] | 2550 | large_object_space_->SetAllLargeObjectsAsZygoteObjects(self); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 2551 | if (collector::SemiSpace::kUseRememberedSet) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 2552 | // Add a new remembered set for the post-zygote non-moving space. |
| 2553 | accounting::RememberedSet* post_zygote_non_moving_space_rem_set = |
| 2554 | new accounting::RememberedSet("Post-zygote non-moving space remembered set", this, |
| 2555 | non_moving_space_); |
| 2556 | CHECK(post_zygote_non_moving_space_rem_set != nullptr) |
| 2557 | << "Failed to create post-zygote non-moving space remembered set"; |
| 2558 | AddRememberedSet(post_zygote_non_moving_space_rem_set); |
| 2559 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2562 | void Heap::FlushAllocStack() { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2563 | MarkAllocStackAsLive(allocation_stack_.get()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2564 | allocation_stack_->Reset(); |
| 2565 | } |
| 2566 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2567 | void Heap::MarkAllocStack(accounting::ContinuousSpaceBitmap* bitmap1, |
| 2568 | accounting::ContinuousSpaceBitmap* bitmap2, |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 2569 | accounting::LargeObjectBitmap* large_objects, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2570 | accounting::ObjectStack* stack) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2571 | DCHECK(bitmap1 != nullptr); |
| 2572 | DCHECK(bitmap2 != nullptr); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 2573 | const auto* limit = stack->End(); |
| 2574 | for (auto* it = stack->Begin(); it != limit; ++it) { |
| 2575 | const mirror::Object* obj = it->AsMirrorPtr(); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2576 | if (!kUseThreadLocalAllocationStack || obj != nullptr) { |
| 2577 | if (bitmap1->HasAddress(obj)) { |
| 2578 | bitmap1->Set(obj); |
| 2579 | } else if (bitmap2->HasAddress(obj)) { |
| 2580 | bitmap2->Set(obj); |
| 2581 | } else { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 2582 | DCHECK(large_objects != nullptr); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2583 | large_objects->Set(obj); |
| 2584 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 2585 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2589 | void Heap::SwapSemiSpaces() { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2590 | CHECK(bump_pointer_space_ != nullptr); |
| 2591 | CHECK(temp_space_ != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2592 | std::swap(bump_pointer_space_, temp_space_); |
| 2593 | } |
| 2594 | |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2595 | collector::GarbageCollector* Heap::Compact(space::ContinuousMemMapAllocSpace* target_space, |
| 2596 | space::ContinuousMemMapAllocSpace* source_space, |
| 2597 | GcCause gc_cause) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2598 | CHECK(kMovingCollector); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2599 | if (target_space != source_space) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 2600 | // Don't swap spaces since this isn't a typical semi space collection. |
| 2601 | semi_space_collector_->SetSwapSemiSpaces(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2602 | semi_space_collector_->SetFromSpace(source_space); |
| 2603 | semi_space_collector_->SetToSpace(target_space); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2604 | semi_space_collector_->Run(gc_cause, false); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2605 | return semi_space_collector_; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2606 | } else { |
| 2607 | CHECK(target_space->IsBumpPointerSpace()) |
| 2608 | << "In-place compaction is only supported for bump pointer spaces"; |
| 2609 | mark_compact_collector_->SetSpace(target_space->AsBumpPointerSpace()); |
| 2610 | mark_compact_collector_->Run(kGcCauseCollectorTransition, false); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2611 | return mark_compact_collector_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2612 | } |
| 2613 | } |
Anwar Ghuloum | 67f9941 | 2013-08-12 14:19:48 -0700 | [diff] [blame] | 2614 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 2615 | collector::GcType Heap::CollectGarbageInternal(collector::GcType gc_type, |
| 2616 | GcCause gc_cause, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2617 | bool clear_soft_references) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 2618 | Thread* self = Thread::Current(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2619 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2620 | // If the heap can't run the GC, silently fail and return that no GC was run. |
| 2621 | switch (gc_type) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2622 | case collector::kGcTypePartial: { |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2623 | if (!HasZygoteSpace()) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2624 | return collector::kGcTypeNone; |
| 2625 | } |
| 2626 | break; |
| 2627 | } |
| 2628 | default: { |
| 2629 | // Other GC types don't have any special cases which makes them not runnable. The main case |
| 2630 | // here is full GC. |
| 2631 | } |
| 2632 | } |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 2633 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 2634 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 2635 | if (self->IsHandlingStackOverflow()) { |
Mathieu Chartier | 50c138f | 2015-01-07 16:00:03 -0800 | [diff] [blame] | 2636 | // If we are throwing a stack overflow error we probably don't have enough remaining stack |
| 2637 | // space to run the GC. |
| 2638 | return collector::kGcTypeNone; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 2639 | } |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2640 | bool compacting_gc; |
| 2641 | { |
| 2642 | gc_complete_lock_->AssertNotHeld(self); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2643 | ScopedThreadStateChange tsc2(self, kWaitingForGcToComplete); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2644 | MutexLock mu(self, *gc_complete_lock_); |
| 2645 | // Ensure there is only one GC at a time. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 2646 | WaitForGcToCompleteLocked(gc_cause, self); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2647 | compacting_gc = IsMovingGc(collector_type_); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2648 | // GC can be disabled if someone has a used GetPrimitiveArrayCritical. |
| 2649 | if (compacting_gc && disable_moving_gc_count_ != 0) { |
| 2650 | LOG(WARNING) << "Skipping GC due to disable moving GC count " << disable_moving_gc_count_; |
| 2651 | return collector::kGcTypeNone; |
| 2652 | } |
Mathieu Chartier | 5116837 | 2015-08-12 16:40:32 -0700 | [diff] [blame] | 2653 | if (gc_disabled_for_shutdown_) { |
| 2654 | return collector::kGcTypeNone; |
| 2655 | } |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2656 | collector_type_running_ = collector_type_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2657 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2658 | if (gc_cause == kGcCauseForAlloc && runtime->HasStatsEnabled()) { |
| 2659 | ++runtime->GetStats()->gc_for_alloc_count; |
| 2660 | ++self->GetStats()->gc_for_alloc_count; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 2661 | } |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 2662 | const uint64_t bytes_allocated_before_gc = GetBytesAllocated(); |
| 2663 | // Approximate heap size. |
| 2664 | ATRACE_INT("Heap size (KB)", bytes_allocated_before_gc / KB); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 2665 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2666 | DCHECK_LT(gc_type, collector::kGcTypeMax); |
| 2667 | DCHECK_NE(gc_type, collector::kGcTypeNone); |
Anwar Ghuloum | 67f9941 | 2013-08-12 14:19:48 -0700 | [diff] [blame] | 2668 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2669 | collector::GarbageCollector* collector = nullptr; |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 2670 | // TODO: Clean this up. |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 2671 | if (compacting_gc) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 2672 | DCHECK(current_allocator_ == kAllocatorTypeBumpPointer || |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2673 | current_allocator_ == kAllocatorTypeTLAB || |
| 2674 | current_allocator_ == kAllocatorTypeRegion || |
| 2675 | current_allocator_ == kAllocatorTypeRegionTLAB); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2676 | switch (collector_type_) { |
| 2677 | case kCollectorTypeSS: |
| 2678 | // Fall-through. |
| 2679 | case kCollectorTypeGSS: |
| 2680 | semi_space_collector_->SetFromSpace(bump_pointer_space_); |
| 2681 | semi_space_collector_->SetToSpace(temp_space_); |
| 2682 | semi_space_collector_->SetSwapSemiSpaces(true); |
| 2683 | collector = semi_space_collector_; |
| 2684 | break; |
| 2685 | case kCollectorTypeCC: |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2686 | concurrent_copying_collector_->SetRegionSpace(region_space_); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2687 | collector = concurrent_copying_collector_; |
| 2688 | break; |
| 2689 | case kCollectorTypeMC: |
| 2690 | mark_compact_collector_->SetSpace(bump_pointer_space_); |
| 2691 | collector = mark_compact_collector_; |
| 2692 | break; |
| 2693 | default: |
| 2694 | LOG(FATAL) << "Invalid collector type " << static_cast<size_t>(collector_type_); |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 2695 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2696 | if (collector != mark_compact_collector_ && collector != concurrent_copying_collector_) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2697 | temp_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
Hiroshi Yamauchi | 6edb9ae | 2016-02-08 14:18:21 -0800 | [diff] [blame] | 2698 | if (kIsDebugBuild) { |
| 2699 | // Try to read each page of the memory map in case mprotect didn't work properly b/19894268. |
| 2700 | temp_space_->GetMemMap()->TryReadable(); |
| 2701 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2702 | CHECK(temp_space_->IsEmpty()); |
| 2703 | } |
| 2704 | gc_type = collector::kGcTypeFull; // TODO: Not hard code this in. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2705 | } else if (current_allocator_ == kAllocatorTypeRosAlloc || |
| 2706 | current_allocator_ == kAllocatorTypeDlMalloc) { |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 2707 | collector = FindCollectorByGcType(gc_type); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 2708 | } else { |
| 2709 | LOG(FATAL) << "Invalid current allocator " << current_allocator_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2710 | } |
Mathieu Chartier | 08cef22 | 2014-10-22 17:18:34 -0700 | [diff] [blame] | 2711 | if (IsGcConcurrent()) { |
| 2712 | // Disable concurrent GC check so that we don't have spammy JNI requests. |
| 2713 | // This gets recalculated in GrowForUtilization. It is important that it is disabled / |
| 2714 | // calculated in the same thread so that there aren't any races that can cause it to become |
| 2715 | // permanantly disabled. b/17942071 |
| 2716 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 2717 | } |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 2718 | |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2719 | // It's time to clear all inline caches, in case some classes can be unloaded. |
| 2720 | if ((gc_type == collector::kGcTypeFull) && (runtime->GetJit() != nullptr)) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 2721 | runtime->GetJit()->GetCodeCache()->ClearGcRootsInInlineCaches(self); |
| 2722 | } |
| 2723 | |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2724 | CHECK(collector != nullptr) |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 2725 | << "Could not find garbage collector with collector_type=" |
| 2726 | << static_cast<size_t>(collector_type_) << " and gc_type=" << gc_type; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 2727 | collector->Run(gc_cause, clear_soft_references || runtime->IsZygote()); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2728 | total_objects_freed_ever_ += GetCurrentGcIteration()->GetFreedObjects(); |
| 2729 | total_bytes_freed_ever_ += GetCurrentGcIteration()->GetFreedBytes(); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 2730 | RequestTrim(self); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 2731 | // Enqueue cleared references. |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 2732 | reference_processor_->EnqueueClearedReferences(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2733 | // Grow the heap so that we know when to perform the next GC. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 2734 | GrowForUtilization(collector, bytes_allocated_before_gc); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2735 | LogGC(gc_cause, collector); |
| 2736 | FinishGC(self, gc_type); |
| 2737 | // Inform DDMS that a GC completed. |
| 2738 | Dbg::GcDidFinish(); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 2739 | // Unload native libraries for class unloading. We do this after calling FinishGC to prevent |
| 2740 | // deadlocks in case the JNI_OnUnload function does allocations. |
| 2741 | { |
| 2742 | ScopedObjectAccess soa(self); |
| 2743 | soa.Vm()->UnloadNativeLibraries(); |
| 2744 | } |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2745 | return gc_type; |
| 2746 | } |
| 2747 | |
| 2748 | void Heap::LogGC(GcCause gc_cause, collector::GarbageCollector* collector) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2749 | const size_t duration = GetCurrentGcIteration()->GetDurationNs(); |
| 2750 | const std::vector<uint64_t>& pause_times = GetCurrentGcIteration()->GetPauseTimes(); |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2751 | // Print the GC if it is an explicit GC (e.g. Runtime.gc()) or a slow GC |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 2752 | // (mutator time blocked >= long_pause_log_threshold_). |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2753 | bool log_gc = gc_cause == kGcCauseExplicit; |
| 2754 | if (!log_gc && CareAboutPauseTimes()) { |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 2755 | // GC for alloc pauses the allocating thread, so consider it as a pause. |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2756 | log_gc = duration > long_gc_log_threshold_ || |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2757 | (gc_cause == kGcCauseForAlloc && duration > long_pause_log_threshold_); |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2758 | for (uint64_t pause : pause_times) { |
| 2759 | log_gc = log_gc || pause >= long_pause_log_threshold_; |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 2760 | } |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2761 | } |
| 2762 | if (log_gc) { |
| 2763 | const size_t percent_free = GetPercentFree(); |
| 2764 | const size_t current_heap_size = GetBytesAllocated(); |
| 2765 | const size_t total_memory = GetTotalMemory(); |
| 2766 | std::ostringstream pause_string; |
| 2767 | for (size_t i = 0; i < pause_times.size(); ++i) { |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2768 | pause_string << PrettyDuration((pause_times[i] / 1000) * 1000) |
| 2769 | << ((i != pause_times.size() - 1) ? "," : ""); |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 2770 | } |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2771 | LOG(INFO) << gc_cause << " " << collector->GetName() |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2772 | << " GC freed " << current_gc_iteration_.GetFreedObjects() << "(" |
| 2773 | << PrettySize(current_gc_iteration_.GetFreedBytes()) << ") AllocSpace objects, " |
| 2774 | << current_gc_iteration_.GetFreedLargeObjects() << "(" |
| 2775 | << PrettySize(current_gc_iteration_.GetFreedLargeObjectBytes()) << ") LOS objects, " |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2776 | << percent_free << "% free, " << PrettySize(current_heap_size) << "/" |
| 2777 | << PrettySize(total_memory) << ", " << "paused " << pause_string.str() |
| 2778 | << " total " << PrettyDuration((duration / 1000) * 1000); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 2779 | VLOG(heap) << Dumpable<TimingLogger>(*current_gc_iteration_.GetTimings()); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 2780 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2781 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 2782 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2783 | void Heap::FinishGC(Thread* self, collector::GcType gc_type) { |
| 2784 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2785 | collector_type_running_ = kCollectorTypeNone; |
| 2786 | if (gc_type != collector::kGcTypeNone) { |
| 2787 | last_gc_type_ = gc_type; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 2788 | |
| 2789 | // Update stats. |
| 2790 | ++gc_count_last_window_; |
| 2791 | if (running_collection_is_blocking_) { |
| 2792 | // If the currently running collection was a blocking one, |
| 2793 | // increment the counters and reset the flag. |
| 2794 | ++blocking_gc_count_; |
| 2795 | blocking_gc_time_ += GetCurrentGcIteration()->GetDurationNs(); |
| 2796 | ++blocking_gc_count_last_window_; |
| 2797 | } |
| 2798 | // Update the gc count rate histograms if due. |
| 2799 | UpdateGcCountRateHistograms(); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2800 | } |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 2801 | // Reset. |
| 2802 | running_collection_is_blocking_ = false; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2803 | // Wake anyone who may have been waiting for the GC to complete. |
| 2804 | gc_complete_cond_->Broadcast(self); |
| 2805 | } |
| 2806 | |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 2807 | void Heap::UpdateGcCountRateHistograms() { |
| 2808 | // Invariant: if the time since the last update includes more than |
| 2809 | // one windows, all the GC runs (if > 0) must have happened in first |
| 2810 | // window because otherwise the update must have already taken place |
| 2811 | // at an earlier GC run. So, we report the non-first windows with |
| 2812 | // zero counts to the histograms. |
| 2813 | DCHECK_EQ(last_update_time_gc_count_rate_histograms_ % kGcCountRateHistogramWindowDuration, 0U); |
| 2814 | uint64_t now = NanoTime(); |
| 2815 | DCHECK_GE(now, last_update_time_gc_count_rate_histograms_); |
| 2816 | uint64_t time_since_last_update = now - last_update_time_gc_count_rate_histograms_; |
| 2817 | uint64_t num_of_windows = time_since_last_update / kGcCountRateHistogramWindowDuration; |
| 2818 | if (time_since_last_update >= kGcCountRateHistogramWindowDuration) { |
| 2819 | // Record the first window. |
| 2820 | gc_count_rate_histogram_.AddValue(gc_count_last_window_ - 1); // Exclude the current run. |
| 2821 | blocking_gc_count_rate_histogram_.AddValue(running_collection_is_blocking_ ? |
| 2822 | blocking_gc_count_last_window_ - 1 : blocking_gc_count_last_window_); |
| 2823 | // Record the other windows (with zero counts). |
| 2824 | for (uint64_t i = 0; i < num_of_windows - 1; ++i) { |
| 2825 | gc_count_rate_histogram_.AddValue(0); |
| 2826 | blocking_gc_count_rate_histogram_.AddValue(0); |
| 2827 | } |
| 2828 | // Update the last update time and reset the counters. |
| 2829 | last_update_time_gc_count_rate_histograms_ = |
| 2830 | (now / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration; |
| 2831 | gc_count_last_window_ = 1; // Include the current run. |
| 2832 | blocking_gc_count_last_window_ = running_collection_is_blocking_ ? 1 : 0; |
| 2833 | } |
| 2834 | DCHECK_EQ(last_update_time_gc_count_rate_histograms_ % kGcCountRateHistogramWindowDuration, 0U); |
| 2835 | } |
| 2836 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2837 | class RootMatchesObjectVisitor : public SingleRootVisitor { |
| 2838 | public: |
| 2839 | explicit RootMatchesObjectVisitor(const mirror::Object* obj) : obj_(obj) { } |
| 2840 | |
| 2841 | void VisitRoot(mirror::Object* root, const RootInfo& info) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2842 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2843 | if (root == obj_) { |
| 2844 | LOG(INFO) << "Object " << obj_ << " is a root " << info.ToString(); |
| 2845 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2846 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2847 | |
| 2848 | private: |
| 2849 | const mirror::Object* const obj_; |
| 2850 | }; |
| 2851 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2852 | |
| 2853 | class ScanVisitor { |
| 2854 | public: |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2855 | void operator()(const mirror::Object* obj) const { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2856 | LOG(ERROR) << "Would have rescanned object " << obj; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2857 | } |
| 2858 | }; |
| 2859 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2860 | // Verify a reference from an object. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2861 | class VerifyReferenceVisitor : public SingleRootVisitor { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2862 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 2863 | VerifyReferenceVisitor(Heap* heap, Atomic<size_t>* fail_count, bool verify_referent) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2864 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2865 | : heap_(heap), fail_count_(fail_count), verify_referent_(verify_referent) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2866 | |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2867 | size_t GetFailureCount() const { |
Mathieu Chartier | e9e55ac | 2014-05-21 17:48:25 -0700 | [diff] [blame] | 2868 | return fail_count_->LoadSequentiallyConsistent(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2871 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, mirror::Reference* ref) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2872 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 2873 | if (verify_referent_) { |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2874 | VerifyReference(ref, ref->GetReferent(), mirror::Reference::ReferentOffset()); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 2875 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2876 | } |
| 2877 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2878 | void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2879 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2880 | VerifyReference(obj, obj->GetFieldObject<mirror::Object>(offset), offset); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2881 | } |
| 2882 | |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2883 | bool IsLive(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
| 2884 | return heap_->IsLiveObjectLocked(obj, true, false, true); |
| 2885 | } |
| 2886 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2887 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 2888 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 2889 | if (!root->IsNull()) { |
| 2890 | VisitRoot(root); |
| 2891 | } |
| 2892 | } |
| 2893 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 2894 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 2895 | const_cast<VerifyReferenceVisitor*>(this)->VisitRoot( |
| 2896 | root->AsMirrorPtr(), RootInfo(kRootVMInternal)); |
| 2897 | } |
| 2898 | |
| 2899 | virtual void VisitRoot(mirror::Object* root, const RootInfo& root_info) OVERRIDE |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2900 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2901 | if (root == nullptr) { |
| 2902 | LOG(ERROR) << "Root is null with info " << root_info.GetType(); |
| 2903 | } else if (!VerifyReference(nullptr, root, MemberOffset(0))) { |
| 2904 | LOG(ERROR) << "Root " << root << " is dead with type " << PrettyTypeOf(root) |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 2905 | << " thread_id= " << root_info.GetThreadId() << " root_type= " << root_info.GetType(); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | private: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2910 | // TODO: Fix the no thread safety analysis. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2911 | // Returns false on failure. |
| 2912 | bool VerifyReference(mirror::Object* obj, mirror::Object* ref, MemberOffset offset) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2913 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2914 | if (ref == nullptr || IsLive(ref)) { |
| 2915 | // Verify that the reference is live. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2916 | return true; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2917 | } |
Mathieu Chartier | e9e55ac | 2014-05-21 17:48:25 -0700 | [diff] [blame] | 2918 | if (fail_count_->FetchAndAddSequentiallyConsistent(1) == 0) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2919 | // Print message on only on first failure to prevent spam. |
| 2920 | LOG(ERROR) << "!!!!!!!!!!!!!!Heap corruption detected!!!!!!!!!!!!!!!!!!!"; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2921 | } |
| 2922 | if (obj != nullptr) { |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2923 | // Only do this part for non roots. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2924 | accounting::CardTable* card_table = heap_->GetCardTable(); |
| 2925 | accounting::ObjectStack* alloc_stack = heap_->allocation_stack_.get(); |
| 2926 | accounting::ObjectStack* live_stack = heap_->live_stack_.get(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2927 | uint8_t* card_addr = card_table->CardFromAddr(obj); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2928 | LOG(ERROR) << "Object " << obj << " references dead object " << ref << " at offset " |
| 2929 | << offset << "\n card value = " << static_cast<int>(*card_addr); |
| 2930 | if (heap_->IsValidObjectAddress(obj->GetClass())) { |
| 2931 | LOG(ERROR) << "Obj type " << PrettyTypeOf(obj); |
| 2932 | } else { |
| 2933 | LOG(ERROR) << "Object " << obj << " class(" << obj->GetClass() << ") not a heap address"; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2934 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2935 | |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 2936 | // Attempt to find the class inside of the recently freed objects. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2937 | space::ContinuousSpace* ref_space = heap_->FindContinuousSpaceFromObject(ref, true); |
| 2938 | if (ref_space != nullptr && ref_space->IsMallocSpace()) { |
| 2939 | space::MallocSpace* space = ref_space->AsMallocSpace(); |
| 2940 | mirror::Class* ref_class = space->FindRecentFreedObject(ref); |
| 2941 | if (ref_class != nullptr) { |
| 2942 | LOG(ERROR) << "Reference " << ref << " found as a recently freed object with class " |
| 2943 | << PrettyClass(ref_class); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2944 | } else { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2945 | LOG(ERROR) << "Reference " << ref << " not found as a recently freed object"; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2946 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2947 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2948 | |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2949 | if (ref->GetClass() != nullptr && heap_->IsValidObjectAddress(ref->GetClass()) && |
| 2950 | ref->GetClass()->IsClass()) { |
| 2951 | LOG(ERROR) << "Ref type " << PrettyTypeOf(ref); |
| 2952 | } else { |
| 2953 | LOG(ERROR) << "Ref " << ref << " class(" << ref->GetClass() |
| 2954 | << ") is not a valid heap address"; |
| 2955 | } |
| 2956 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2957 | card_table->CheckAddrIsInCardTable(reinterpret_cast<const uint8_t*>(obj)); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2958 | void* cover_begin = card_table->AddrFromCard(card_addr); |
| 2959 | void* cover_end = reinterpret_cast<void*>(reinterpret_cast<size_t>(cover_begin) + |
| 2960 | accounting::CardTable::kCardSize); |
| 2961 | LOG(ERROR) << "Card " << reinterpret_cast<void*>(card_addr) << " covers " << cover_begin |
| 2962 | << "-" << cover_end; |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2963 | accounting::ContinuousSpaceBitmap* bitmap = |
| 2964 | heap_->GetLiveBitmap()->GetContinuousSpaceBitmap(obj); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2965 | |
| 2966 | if (bitmap == nullptr) { |
| 2967 | LOG(ERROR) << "Object " << obj << " has no bitmap"; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 2968 | if (!VerifyClassClass(obj->GetClass())) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2969 | LOG(ERROR) << "Object " << obj << " failed class verification!"; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2970 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2971 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2972 | // Print out how the object is live. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2973 | if (bitmap->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2974 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 2975 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2976 | if (alloc_stack->Contains(const_cast<mirror::Object*>(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2977 | LOG(ERROR) << "Object " << obj << " found in allocation stack"; |
| 2978 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2979 | if (live_stack->Contains(const_cast<mirror::Object*>(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2980 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 2981 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2982 | if (alloc_stack->Contains(const_cast<mirror::Object*>(ref))) { |
| 2983 | LOG(ERROR) << "Ref " << ref << " found in allocation stack"; |
| 2984 | } |
| 2985 | if (live_stack->Contains(const_cast<mirror::Object*>(ref))) { |
| 2986 | LOG(ERROR) << "Ref " << ref << " found in live stack"; |
| 2987 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2988 | // Attempt to see if the card table missed the reference. |
| 2989 | ScanVisitor scan_visitor; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2990 | uint8_t* byte_cover_begin = reinterpret_cast<uint8_t*>(card_table->AddrFromCard(card_addr)); |
Lei Li | 727b294 | 2015-01-15 11:26:34 +0800 | [diff] [blame] | 2991 | card_table->Scan<false>(bitmap, byte_cover_begin, |
| 2992 | byte_cover_begin + accounting::CardTable::kCardSize, scan_visitor); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2993 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2994 | |
| 2995 | // Search to see if any of the roots reference our object. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2996 | RootMatchesObjectVisitor visitor1(obj); |
| 2997 | Runtime::Current()->VisitRoots(&visitor1); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2998 | // Search to see if any of the roots reference our reference. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2999 | RootMatchesObjectVisitor visitor2(ref); |
| 3000 | Runtime::Current()->VisitRoots(&visitor2); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3001 | } |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3002 | return false; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3003 | } |
| 3004 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3005 | Heap* const heap_; |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3006 | Atomic<size_t>* const fail_count_; |
| 3007 | const bool verify_referent_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3008 | }; |
| 3009 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3010 | // Verify all references within an object, for use with HeapBitmap::Visit. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3011 | class VerifyObjectVisitor { |
| 3012 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 3013 | VerifyObjectVisitor(Heap* heap, Atomic<size_t>* fail_count, bool verify_referent) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 3014 | : heap_(heap), fail_count_(fail_count), verify_referent_(verify_referent) {} |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3015 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 3016 | void operator()(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3017 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3018 | // Note: we are verifying the references in obj but not obj itself, this is because obj must |
| 3019 | // be live or else how did we find it in the live bitmap? |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3020 | VerifyReferenceVisitor visitor(heap_, fail_count_, verify_referent_); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 3021 | // The class doesn't count as a reference but we should verify it anyways. |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 3022 | obj->VisitReferences(visitor, visitor); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3023 | } |
| 3024 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3025 | static void VisitCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3026 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3027 | VerifyObjectVisitor* visitor = reinterpret_cast<VerifyObjectVisitor*>(arg); |
| 3028 | visitor->operator()(obj); |
| 3029 | } |
| 3030 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 3031 | void VerifyRoots() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 3032 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 3033 | VerifyReferenceVisitor visitor(heap_, fail_count_, verify_referent_); |
| 3034 | Runtime::Current()->VisitRoots(&visitor); |
| 3035 | } |
| 3036 | |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3037 | size_t GetFailureCount() const { |
Mathieu Chartier | e9e55ac | 2014-05-21 17:48:25 -0700 | [diff] [blame] | 3038 | return fail_count_->LoadSequentiallyConsistent(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3042 | Heap* const heap_; |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3043 | Atomic<size_t>* const fail_count_; |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 3044 | const bool verify_referent_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3045 | }; |
| 3046 | |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 3047 | void Heap::PushOnAllocationStackWithInternalGC(Thread* self, mirror::Object** obj) { |
| 3048 | // Slow path, the allocation stack push back must have already failed. |
| 3049 | DCHECK(!allocation_stack_->AtomicPushBack(*obj)); |
| 3050 | do { |
| 3051 | // TODO: Add handle VerifyObject. |
| 3052 | StackHandleScope<1> hs(self); |
| 3053 | HandleWrapper<mirror::Object> wrapper(hs.NewHandleWrapper(obj)); |
| 3054 | // Push our object into the reserve region of the allocaiton stack. This is only required due |
| 3055 | // to heap verification requiring that roots are live (either in the live bitmap or in the |
| 3056 | // allocation stack). |
| 3057 | CHECK(allocation_stack_->AtomicPushBackIgnoreGrowthLimit(*obj)); |
| 3058 | CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false); |
| 3059 | } while (!allocation_stack_->AtomicPushBack(*obj)); |
| 3060 | } |
| 3061 | |
| 3062 | void Heap::PushOnThreadLocalAllocationStackWithInternalGC(Thread* self, mirror::Object** obj) { |
| 3063 | // Slow path, the allocation stack push back must have already failed. |
| 3064 | DCHECK(!self->PushOnThreadLocalAllocationStack(*obj)); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 3065 | StackReference<mirror::Object>* start_address; |
| 3066 | StackReference<mirror::Object>* end_address; |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 3067 | while (!allocation_stack_->AtomicBumpBack(kThreadLocalAllocationStackSize, &start_address, |
| 3068 | &end_address)) { |
| 3069 | // TODO: Add handle VerifyObject. |
| 3070 | StackHandleScope<1> hs(self); |
| 3071 | HandleWrapper<mirror::Object> wrapper(hs.NewHandleWrapper(obj)); |
| 3072 | // Push our object into the reserve region of the allocaiton stack. This is only required due |
| 3073 | // to heap verification requiring that roots are live (either in the live bitmap or in the |
| 3074 | // allocation stack). |
| 3075 | CHECK(allocation_stack_->AtomicPushBackIgnoreGrowthLimit(*obj)); |
| 3076 | // Push into the reserve allocation stack. |
| 3077 | CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false); |
| 3078 | } |
| 3079 | self->SetThreadLocalAllocationStack(start_address, end_address); |
| 3080 | // Retry on the new thread-local allocation stack. |
| 3081 | CHECK(self->PushOnThreadLocalAllocationStack(*obj)); // Must succeed. |
| 3082 | } |
| 3083 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3084 | // Must do this with mutators suspended since we are directly accessing the allocation stacks. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3085 | size_t Heap::VerifyHeapReferences(bool verify_referents) { |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 3086 | Thread* self = Thread::Current(); |
| 3087 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3088 | // 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] | 3089 | allocation_stack_->Sort(); |
| 3090 | live_stack_->Sort(); |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 3091 | // Since we sorted the allocation stack content, need to revoke all |
| 3092 | // thread-local allocation stacks. |
| 3093 | RevokeAllThreadLocalAllocationStacks(self); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3094 | Atomic<size_t> fail_count_(0); |
| 3095 | VerifyObjectVisitor visitor(this, &fail_count_, verify_referents); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 3096 | // Verify objects in the allocation stack since these will be objects which were: |
| 3097 | // 1. Allocated prior to the GC (pre GC verification). |
| 3098 | // 2. Allocated during the GC (pre sweep GC verification). |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 3099 | // 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] | 3100 | // pointing to dead objects if they are not reachable. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3101 | VisitObjectsPaused(VerifyObjectVisitor::VisitCallback, &visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3102 | // Verify the roots: |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 3103 | visitor.VerifyRoots(); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3104 | if (visitor.GetFailureCount() > 0) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 3105 | // Dump mod-union tables. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3106 | for (const auto& table_pair : mod_union_tables_) { |
| 3107 | accounting::ModUnionTable* mod_union_table = table_pair.second; |
| 3108 | mod_union_table->Dump(LOG(ERROR) << mod_union_table->GetName() << ": "); |
| 3109 | } |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3110 | // Dump remembered sets. |
| 3111 | for (const auto& table_pair : remembered_sets_) { |
| 3112 | accounting::RememberedSet* remembered_set = table_pair.second; |
| 3113 | remembered_set->Dump(LOG(ERROR) << remembered_set->GetName() << ": "); |
| 3114 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 3115 | DumpSpaces(LOG(ERROR)); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3116 | } |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3117 | return visitor.GetFailureCount(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | class VerifyReferenceCardVisitor { |
| 3121 | public: |
| 3122 | VerifyReferenceCardVisitor(Heap* heap, bool* failed) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3123 | SHARED_REQUIRES(Locks::mutator_lock_, |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3124 | Locks::heap_bitmap_lock_) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3125 | : heap_(heap), failed_(failed) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3126 | } |
| 3127 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 3128 | // There is no card marks for native roots on a class. |
| 3129 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 3130 | const {} |
| 3131 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 3132 | |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 3133 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for |
| 3134 | // annotalysis on visitors. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 3135 | void operator()(mirror::Object* obj, MemberOffset offset, bool is_static) const |
| 3136 | NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 3137 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 3138 | // Filter out class references since changing an object's class does not mark the card as dirty. |
| 3139 | // Also handles large objects, since the only reference they hold is a class reference. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 3140 | if (ref != nullptr && !ref->IsClass()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3141 | accounting::CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3142 | // If the object is not dirty and it is referencing something in the live stack other than |
| 3143 | // class, then it must be on a dirty card. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 3144 | if (!card_table->AddrIsInCardTable(obj)) { |
| 3145 | LOG(ERROR) << "Object " << obj << " is not in the address range of the card table"; |
| 3146 | *failed_ = true; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3147 | } else if (!card_table->IsDirty(obj)) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 3148 | // TODO: Check mod-union tables. |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 3149 | // Card should be either kCardDirty if it got re-dirtied after we aged it, or |
| 3150 | // kCardDirty - 1 if it didnt get touched since we aged it. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3151 | accounting::ObjectStack* live_stack = heap_->live_stack_.get(); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 3152 | if (live_stack->ContainsSorted(ref)) { |
| 3153 | if (live_stack->ContainsSorted(obj)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3154 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 3155 | } |
| 3156 | if (heap_->GetLiveBitmap()->Test(obj)) { |
| 3157 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 3158 | } |
| 3159 | LOG(ERROR) << "Object " << obj << " " << PrettyTypeOf(obj) |
| 3160 | << " references " << ref << " " << PrettyTypeOf(ref) << " in live stack"; |
| 3161 | |
| 3162 | // Print which field of the object is dead. |
| 3163 | if (!obj->IsObjectArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3164 | mirror::Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3165 | CHECK(klass != nullptr); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 3166 | for (ArtField& field : (is_static ? klass->GetSFields() : klass->GetIFields())) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3167 | if (field.GetOffset().Int32Value() == offset.Int32Value()) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3168 | LOG(ERROR) << (is_static ? "Static " : "") << "field in the live stack is " |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3169 | << PrettyField(&field); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3170 | break; |
| 3171 | } |
| 3172 | } |
| 3173 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3174 | mirror::ObjectArray<mirror::Object>* object_array = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3175 | obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3176 | for (int32_t i = 0; i < object_array->GetLength(); ++i) { |
| 3177 | if (object_array->Get(i) == ref) { |
| 3178 | LOG(ERROR) << (is_static ? "Static " : "") << "obj[" << i << "] = ref"; |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | *failed_ = true; |
| 3184 | } |
| 3185 | } |
| 3186 | } |
| 3187 | } |
| 3188 | |
| 3189 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3190 | Heap* const heap_; |
| 3191 | bool* const failed_; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3192 | }; |
| 3193 | |
| 3194 | class VerifyLiveStackReferences { |
| 3195 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 3196 | explicit VerifyLiveStackReferences(Heap* heap) |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3197 | : heap_(heap), |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 3198 | failed_(false) {} |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3199 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3200 | void operator()(mirror::Object* obj) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3201 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3202 | VerifyReferenceCardVisitor visitor(heap_, const_cast<bool*>(&failed_)); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 3203 | obj->VisitReferences(visitor, VoidFunctor()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | bool Failed() const { |
| 3207 | return failed_; |
| 3208 | } |
| 3209 | |
| 3210 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3211 | Heap* const heap_; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3212 | bool failed_; |
| 3213 | }; |
| 3214 | |
| 3215 | bool Heap::VerifyMissingCardMarks() { |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 3216 | Thread* self = Thread::Current(); |
| 3217 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3218 | // We need to sort the live stack since we binary search it. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3219 | live_stack_->Sort(); |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 3220 | // Since we sorted the allocation stack content, need to revoke all |
| 3221 | // thread-local allocation stacks. |
| 3222 | RevokeAllThreadLocalAllocationStacks(self); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3223 | VerifyLiveStackReferences visitor(this); |
| 3224 | GetLiveBitmap()->Visit(visitor); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3225 | // We can verify objects in the live stack since none of these should reference dead objects. |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 3226 | for (auto* it = live_stack_->Begin(); it != live_stack_->End(); ++it) { |
| 3227 | if (!kUseThreadLocalAllocationStack || it->AsMirrorPtr() != nullptr) { |
| 3228 | visitor(it->AsMirrorPtr()); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 3229 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3230 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 3231 | return !visitor.Failed(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3232 | } |
| 3233 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 3234 | void Heap::SwapStacks() { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 3235 | if (kUseThreadLocalAllocationStack) { |
| 3236 | live_stack_->AssertAllZero(); |
| 3237 | } |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 3238 | allocation_stack_.swap(live_stack_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 3239 | } |
| 3240 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 3241 | void Heap::RevokeAllThreadLocalAllocationStacks(Thread* self) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 3242 | // This must be called only during the pause. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3243 | DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self)); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 3244 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 3245 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 3246 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 3247 | for (Thread* t : thread_list) { |
| 3248 | t->RevokeThreadLocalAllocationStack(); |
| 3249 | } |
| 3250 | } |
| 3251 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3252 | void Heap::AssertThreadLocalBuffersAreRevoked(Thread* thread) { |
| 3253 | if (kIsDebugBuild) { |
| 3254 | if (rosalloc_space_ != nullptr) { |
| 3255 | rosalloc_space_->AssertThreadLocalBuffersAreRevoked(thread); |
| 3256 | } |
| 3257 | if (bump_pointer_space_ != nullptr) { |
| 3258 | bump_pointer_space_->AssertThreadLocalBuffersAreRevoked(thread); |
| 3259 | } |
| 3260 | } |
| 3261 | } |
| 3262 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 3263 | void Heap::AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked() { |
| 3264 | if (kIsDebugBuild) { |
| 3265 | if (bump_pointer_space_ != nullptr) { |
| 3266 | bump_pointer_space_->AssertAllThreadLocalBuffersAreRevoked(); |
| 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3271 | accounting::ModUnionTable* Heap::FindModUnionTableFromSpace(space::Space* space) { |
| 3272 | auto it = mod_union_tables_.find(space); |
| 3273 | if (it == mod_union_tables_.end()) { |
| 3274 | return nullptr; |
| 3275 | } |
| 3276 | return it->second; |
| 3277 | } |
| 3278 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3279 | accounting::RememberedSet* Heap::FindRememberedSetFromSpace(space::Space* space) { |
| 3280 | auto it = remembered_sets_.find(space); |
| 3281 | if (it == remembered_sets_.end()) { |
| 3282 | return nullptr; |
| 3283 | } |
| 3284 | return it->second; |
| 3285 | } |
| 3286 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 3287 | void Heap::ProcessCards(TimingLogger* timings, |
| 3288 | bool use_rem_sets, |
| 3289 | bool process_alloc_space_cards, |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 3290 | bool clear_alloc_space_cards) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3291 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3292 | // 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] | 3293 | for (const auto& space : continuous_spaces_) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3294 | accounting::ModUnionTable* table = FindModUnionTableFromSpace(space); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3295 | accounting::RememberedSet* rem_set = FindRememberedSetFromSpace(space); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3296 | if (table != nullptr) { |
| 3297 | const char* name = space->IsZygoteSpace() ? "ZygoteModUnionClearCards" : |
| 3298 | "ImageModUnionClearCards"; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3299 | TimingLogger::ScopedTiming t2(name, timings); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3300 | table->ClearCards(); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3301 | } else if (use_rem_sets && rem_set != nullptr) { |
| 3302 | DCHECK(collector::SemiSpace::kUseRememberedSet && collector_type_ == kCollectorTypeGSS) |
| 3303 | << static_cast<int>(collector_type_); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3304 | TimingLogger::ScopedTiming t2("AllocSpaceRemSetClearCards", timings); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3305 | rem_set->ClearCards(); |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 3306 | } else if (process_alloc_space_cards) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3307 | TimingLogger::ScopedTiming t2("AllocSpaceClearCards", timings); |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 3308 | if (clear_alloc_space_cards) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3309 | uint8_t* end = space->End(); |
| 3310 | if (space->IsImageSpace()) { |
| 3311 | // Image space end is the end of the mirror objects, it is not necessarily page or card |
| 3312 | // aligned. Align up so that the check in ClearCardRange does not fail. |
| 3313 | end = AlignUp(end, accounting::CardTable::kCardSize); |
| 3314 | } |
| 3315 | card_table_->ClearCardRange(space->Begin(), end); |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 3316 | } else { |
| 3317 | // No mod union table for the AllocSpace. Age the cards so that the GC knows that these |
| 3318 | // cards were dirty before the GC started. |
| 3319 | // TODO: Need to use atomic for the case where aged(cleaning thread) -> dirty(other thread) |
| 3320 | // -> clean(cleaning thread). |
| 3321 | // The races are we either end up with: Aged card, unaged card. Since we have the |
| 3322 | // checkpoint roots and then we scan / update mod union tables after. We will always |
| 3323 | // scan either card. If we end up with the non aged card, we scan it it in the pause. |
| 3324 | card_table_->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(), |
| 3325 | VoidFunctor()); |
| 3326 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 3331 | struct IdentityMarkHeapReferenceVisitor : public MarkObjectVisitor { |
| 3332 | virtual mirror::Object* MarkObject(mirror::Object* obj) OVERRIDE { |
| 3333 | return obj; |
| 3334 | } |
| 3335 | virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>*) OVERRIDE { |
| 3336 | } |
| 3337 | }; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3338 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3339 | void Heap::PreGcVerificationPaused(collector::GarbageCollector* gc) { |
| 3340 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3341 | TimingLogger* const timings = current_gc_iteration_.GetTimings(); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3342 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3343 | if (verify_pre_gc_heap_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3344 | TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyHeapReferences", timings); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3345 | size_t failures = VerifyHeapReferences(); |
| 3346 | if (failures > 0) { |
| 3347 | LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed with " << failures |
| 3348 | << " failures"; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3349 | } |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 3350 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3351 | // Check that all objects which reference things in the live stack are on dirty cards. |
| 3352 | if (verify_missing_card_marks_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3353 | TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyMissingCardMarks", timings); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3354 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 3355 | SwapStacks(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3356 | // Sort the live stack so that we can quickly binary search it later. |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 3357 | CHECK(VerifyMissingCardMarks()) << "Pre " << gc->GetName() |
| 3358 | << " missing card mark verification failed\n" << DumpSpaces(); |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 3359 | SwapStacks(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3360 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3361 | if (verify_mod_union_table_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3362 | TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyModUnionTables", timings); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3363 | ReaderMutexLock reader_lock(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3364 | for (const auto& table_pair : mod_union_tables_) { |
| 3365 | accounting::ModUnionTable* mod_union_table = table_pair.second; |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 3366 | IdentityMarkHeapReferenceVisitor visitor; |
| 3367 | mod_union_table->UpdateAndMarkReferences(&visitor); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3368 | mod_union_table->Verify(); |
| 3369 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | void Heap::PreGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 3374 | if (verify_pre_gc_heap_ || verify_missing_card_marks_ || verify_mod_union_table_) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3375 | collector::GarbageCollector::ScopedPause pause(gc); |
| 3376 | PreGcVerificationPaused(gc); |
| 3377 | } |
| 3378 | } |
| 3379 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 3380 | void Heap::PrePauseRosAllocVerification(collector::GarbageCollector* gc ATTRIBUTE_UNUSED) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3381 | // TODO: Add a new runtime option for this? |
| 3382 | if (verify_pre_gc_rosalloc_) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3383 | RosAllocVerification(current_gc_iteration_.GetTimings(), "PreGcRosAllocVerification"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3384 | } |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 3385 | } |
| 3386 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3387 | void Heap::PreSweepingGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3388 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3389 | TimingLogger* const timings = current_gc_iteration_.GetTimings(); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3390 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3391 | // Called before sweeping occurs since we want to make sure we are not going so reclaim any |
| 3392 | // reachable objects. |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3393 | if (verify_pre_sweeping_heap_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3394 | TimingLogger::ScopedTiming t2("(Paused)PostSweepingVerifyHeapReferences", timings); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3395 | CHECK_NE(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 3396 | { |
| 3397 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 3398 | // Swapping bound bitmaps does nothing. |
| 3399 | gc->SwapBitmaps(); |
| 3400 | } |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 3401 | // Pass in false since concurrent reference processing can mean that the reference referents |
| 3402 | // may point to dead objects at the point which PreSweepingGcVerification is called. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3403 | size_t failures = VerifyHeapReferences(false); |
| 3404 | if (failures > 0) { |
| 3405 | LOG(FATAL) << "Pre sweeping " << gc->GetName() << " GC verification failed with " << failures |
| 3406 | << " failures"; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3407 | } |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 3408 | { |
| 3409 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 3410 | gc->SwapBitmaps(); |
| 3411 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3412 | } |
| 3413 | if (verify_pre_sweeping_rosalloc_) { |
| 3414 | RosAllocVerification(timings, "PreSweepingRosAllocVerification"); |
| 3415 | } |
| 3416 | } |
| 3417 | |
| 3418 | void Heap::PostGcVerificationPaused(collector::GarbageCollector* gc) { |
| 3419 | // Only pause if we have to do some verification. |
| 3420 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3421 | TimingLogger* const timings = GetCurrentGcIteration()->GetTimings(); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3422 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3423 | if (verify_system_weaks_) { |
| 3424 | ReaderMutexLock mu2(self, *Locks::heap_bitmap_lock_); |
| 3425 | collector::MarkSweep* mark_sweep = down_cast<collector::MarkSweep*>(gc); |
| 3426 | mark_sweep->VerifySystemWeaks(); |
| 3427 | } |
| 3428 | if (verify_post_gc_rosalloc_) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3429 | RosAllocVerification(timings, "(Paused)PostGcRosAllocVerification"); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3430 | } |
| 3431 | if (verify_post_gc_heap_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3432 | TimingLogger::ScopedTiming t2("(Paused)PostGcVerifyHeapReferences", timings); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3433 | size_t failures = VerifyHeapReferences(); |
| 3434 | if (failures > 0) { |
| 3435 | LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed with " << failures |
| 3436 | << " failures"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3437 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3438 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3439 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3440 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3441 | void Heap::PostGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3442 | if (verify_system_weaks_ || verify_post_gc_rosalloc_ || verify_post_gc_heap_) { |
| 3443 | collector::GarbageCollector::ScopedPause pause(gc); |
Mathieu Chartier | d35326f | 2014-08-18 15:02:59 -0700 | [diff] [blame] | 3444 | PostGcVerificationPaused(gc); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3445 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 3446 | } |
| 3447 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3448 | void Heap::RosAllocVerification(TimingLogger* timings, const char* name) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3449 | TimingLogger::ScopedTiming t(name, timings); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3450 | for (const auto& space : continuous_spaces_) { |
| 3451 | if (space->IsRosAllocSpace()) { |
| 3452 | VLOG(heap) << name << " : " << space->GetName(); |
| 3453 | space->AsRosAllocSpace()->Verify(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | } |
| 3457 | |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3458 | collector::GcType Heap::WaitForGcToComplete(GcCause cause, Thread* self) { |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 3459 | ScopedThreadStateChange tsc(self, kWaitingForGcToComplete); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3460 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3461 | return WaitForGcToCompleteLocked(cause, self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3464 | collector::GcType Heap::WaitForGcToCompleteLocked(GcCause cause, Thread* self) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3465 | collector::GcType last_gc_type = collector::kGcTypeNone; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3466 | uint64_t wait_start = NanoTime(); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 3467 | while (collector_type_running_ != kCollectorTypeNone) { |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 3468 | if (self != task_processor_->GetRunningThread()) { |
| 3469 | // The current thread is about to wait for a currently running |
| 3470 | // collection to finish. If the waiting thread is not the heap |
| 3471 | // task daemon thread, the currently running collection is |
| 3472 | // considered as a blocking GC. |
| 3473 | running_collection_is_blocking_ = true; |
| 3474 | VLOG(gc) << "Waiting for a blocking GC " << cause; |
| 3475 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 3476 | ScopedTrace trace("GC: Wait For Completion"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3477 | // We must wait, change thread state then sleep on gc_complete_cond_; |
| 3478 | gc_complete_cond_->Wait(self); |
| 3479 | last_gc_type = last_gc_type_; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 3480 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3481 | uint64_t wait_time = NanoTime() - wait_start; |
| 3482 | total_wait_time_ += wait_time; |
| 3483 | if (wait_time > long_pause_log_threshold_) { |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3484 | LOG(INFO) << "WaitForGcToComplete blocked for " << PrettyDuration(wait_time) |
| 3485 | << " for cause " << cause; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3486 | } |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 3487 | if (self != task_processor_->GetRunningThread()) { |
| 3488 | // The current thread is about to run a collection. If the thread |
| 3489 | // is not the heap task daemon thread, it's considered as a |
| 3490 | // blocking GC (i.e., blocking itself). |
| 3491 | running_collection_is_blocking_ = true; |
| 3492 | VLOG(gc) << "Starting a blocking GC " << cause; |
| 3493 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 3494 | return last_gc_type; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 3497 | void Heap::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3498 | os << "Heap: " << GetPercentFree() << "% free, " << PrettySize(GetBytesAllocated()) << "/" |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3499 | << PrettySize(GetTotalMemory()) << "; " << GetObjectsAllocated() << " objects\n"; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 3500 | DumpGcPerformanceInfo(os); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | size_t Heap::GetPercentFree() { |
Mathieu Chartier | d30e1d6 | 2014-06-09 13:25:22 -0700 | [diff] [blame] | 3504 | return static_cast<size_t>(100.0f * static_cast<float>(GetFreeMemory()) / max_allowed_footprint_); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3507 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3508 | if (max_allowed_footprint > GetMaxMemory()) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3509 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) << " to " |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3510 | << PrettySize(GetMaxMemory()); |
| 3511 | max_allowed_footprint = GetMaxMemory(); |
| 3512 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 3513 | max_allowed_footprint_ = max_allowed_footprint; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3516 | bool Heap::IsMovableObject(const mirror::Object* obj) const { |
| 3517 | if (kMovingCollector) { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 3518 | space::Space* space = FindContinuousSpaceFromObject(obj, true); |
| 3519 | if (space != nullptr) { |
| 3520 | // TODO: Check large object? |
| 3521 | return space->CanMoveObjects(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3522 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3523 | } |
| 3524 | return false; |
| 3525 | } |
| 3526 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3527 | void Heap::UpdateMaxNativeFootprint() { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 3528 | size_t native_size = native_bytes_allocated_.LoadRelaxed(); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3529 | // TODO: Tune the native heap utilization to be a value other than the java heap utilization. |
| 3530 | size_t target_size = native_size / GetTargetHeapUtilization(); |
| 3531 | if (target_size > native_size + max_free_) { |
| 3532 | target_size = native_size + max_free_; |
| 3533 | } else if (target_size < native_size + min_free_) { |
| 3534 | target_size = native_size + min_free_; |
| 3535 | } |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3536 | native_footprint_gc_watermark_ = std::min(growth_limit_, target_size); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3539 | collector::GarbageCollector* Heap::FindCollectorByGcType(collector::GcType gc_type) { |
| 3540 | for (const auto& collector : garbage_collectors_) { |
| 3541 | if (collector->GetCollectorType() == collector_type_ && |
| 3542 | collector->GetGcType() == gc_type) { |
| 3543 | return collector; |
| 3544 | } |
| 3545 | } |
| 3546 | return nullptr; |
| 3547 | } |
| 3548 | |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3549 | double Heap::HeapGrowthMultiplier() const { |
| 3550 | // If we don't care about pause times we are background, so return 1.0. |
| 3551 | if (!CareAboutPauseTimes() || IsLowMemoryMode()) { |
| 3552 | return 1.0; |
| 3553 | } |
| 3554 | return foreground_heap_growth_multiplier_; |
| 3555 | } |
| 3556 | |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3557 | void Heap::GrowForUtilization(collector::GarbageCollector* collector_ran, |
| 3558 | uint64_t bytes_allocated_before_gc) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3559 | // We know what our utilization is at this moment. |
| 3560 | // This doesn't actually resize any memory. It just lets the heap grow more when necessary. |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3561 | const uint64_t bytes_allocated = GetBytesAllocated(); |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3562 | uint64_t target_size; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3563 | collector::GcType gc_type = collector_ran->GetGcType(); |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3564 | const double multiplier = HeapGrowthMultiplier(); // Use the multiplier to grow more for |
| 3565 | // foreground. |
| 3566 | const uint64_t adjusted_min_free = static_cast<uint64_t>(min_free_ * multiplier); |
| 3567 | const uint64_t adjusted_max_free = static_cast<uint64_t>(max_free_ * multiplier); |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3568 | if (gc_type != collector::kGcTypeSticky) { |
| 3569 | // Grow the heap for non sticky GC. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3570 | ssize_t delta = bytes_allocated / GetTargetHeapUtilization() - bytes_allocated; |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3571 | CHECK_GE(delta, 0); |
| 3572 | target_size = bytes_allocated + delta * multiplier; |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3573 | target_size = std::min(target_size, bytes_allocated + adjusted_max_free); |
| 3574 | target_size = std::max(target_size, bytes_allocated + adjusted_min_free); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3575 | native_need_to_run_finalization_ = true; |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3576 | next_gc_type_ = collector::kGcTypeSticky; |
| 3577 | } else { |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3578 | collector::GcType non_sticky_gc_type = |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 3579 | HasZygoteSpace() ? collector::kGcTypePartial : collector::kGcTypeFull; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3580 | // Find what the next non sticky collector will be. |
| 3581 | collector::GarbageCollector* non_sticky_collector = FindCollectorByGcType(non_sticky_gc_type); |
| 3582 | // If the throughput of the current sticky GC >= throughput of the non sticky collector, then |
| 3583 | // do another sticky collection next. |
| 3584 | // We also check that the bytes allocated aren't over the footprint limit in order to prevent a |
| 3585 | // pathological case where dead objects which aren't reclaimed by sticky could get accumulated |
| 3586 | // if the sticky GC throughput always remained >= the full/partial throughput. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3587 | if (current_gc_iteration_.GetEstimatedThroughput() * kStickyGcThroughputAdjustment >= |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3588 | non_sticky_collector->GetEstimatedMeanThroughput() && |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3589 | non_sticky_collector->NumberOfIterations() > 0 && |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3590 | bytes_allocated <= max_allowed_footprint_) { |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3591 | next_gc_type_ = collector::kGcTypeSticky; |
| 3592 | } else { |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3593 | next_gc_type_ = non_sticky_gc_type; |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3594 | } |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3595 | // If we have freed enough memory, shrink the heap back down. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3596 | if (bytes_allocated + adjusted_max_free < max_allowed_footprint_) { |
| 3597 | target_size = bytes_allocated + adjusted_max_free; |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3598 | } else { |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3599 | target_size = std::max(bytes_allocated, static_cast<uint64_t>(max_allowed_footprint_)); |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3602 | if (!ignore_max_footprint_) { |
| 3603 | SetIdealFootprint(target_size); |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 3604 | if (IsGcConcurrent()) { |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3605 | const uint64_t freed_bytes = current_gc_iteration_.GetFreedBytes() + |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3606 | current_gc_iteration_.GetFreedLargeObjectBytes() + |
| 3607 | current_gc_iteration_.GetFreedRevokeBytes(); |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3608 | // Bytes allocated will shrink by freed_bytes after the GC runs, so if we want to figure out |
| 3609 | // how many bytes were allocated during the GC we need to add freed_bytes back on. |
| 3610 | CHECK_GE(bytes_allocated + freed_bytes, bytes_allocated_before_gc); |
| 3611 | const uint64_t bytes_allocated_during_gc = bytes_allocated + freed_bytes - |
| 3612 | bytes_allocated_before_gc; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3613 | // Calculate when to perform the next ConcurrentGC. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3614 | // Calculate the estimated GC duration. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3615 | const double gc_duration_seconds = NsToMs(current_gc_iteration_.GetDurationNs()) / 1000.0; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3616 | // Estimate how many remaining bytes we will have when we need to start the next GC. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3617 | size_t remaining_bytes = bytes_allocated_during_gc * gc_duration_seconds; |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3618 | remaining_bytes = std::min(remaining_bytes, kMaxConcurrentRemainingBytes); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3619 | remaining_bytes = std::max(remaining_bytes, kMinConcurrentRemainingBytes); |
| 3620 | if (UNLIKELY(remaining_bytes > max_allowed_footprint_)) { |
| 3621 | // A never going to happen situation that from the estimated allocation rate we will exceed |
| 3622 | // the applications entire footprint with the given estimated allocation rate. Schedule |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3623 | // another GC nearly straight away. |
| 3624 | remaining_bytes = kMinConcurrentRemainingBytes; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3625 | } |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3626 | DCHECK_LE(remaining_bytes, max_allowed_footprint_); |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 3627 | DCHECK_LE(max_allowed_footprint_, GetMaxMemory()); |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3628 | // Start a concurrent GC when we get close to the estimated remaining bytes. When the |
| 3629 | // allocation rate is very high, remaining_bytes could tell us that we should start a GC |
| 3630 | // right away. |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3631 | concurrent_start_bytes_ = std::max(max_allowed_footprint_ - remaining_bytes, |
| 3632 | static_cast<size_t>(bytes_allocated)); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 3633 | } |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 3634 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 3635 | } |
| 3636 | |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 3637 | void Heap::ClampGrowthLimit() { |
Mathieu Chartier | ddac423 | 2015-04-02 10:08:03 -0700 | [diff] [blame] | 3638 | // Use heap bitmap lock to guard against races with BindLiveToMarkBitmap. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 3639 | ScopedObjectAccess soa(Thread::Current()); |
| 3640 | WriterMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 3641 | capacity_ = growth_limit_; |
| 3642 | for (const auto& space : continuous_spaces_) { |
| 3643 | if (space->IsMallocSpace()) { |
| 3644 | gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 3645 | malloc_space->ClampGrowthLimit(); |
| 3646 | } |
| 3647 | } |
| 3648 | // This space isn't added for performance reasons. |
| 3649 | if (main_space_backup_.get() != nullptr) { |
| 3650 | main_space_backup_->ClampGrowthLimit(); |
| 3651 | } |
| 3652 | } |
| 3653 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 3654 | void Heap::ClearGrowthLimit() { |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 3655 | growth_limit_ = capacity_; |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 3656 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 3657 | for (const auto& space : continuous_spaces_) { |
| 3658 | if (space->IsMallocSpace()) { |
| 3659 | gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 3660 | malloc_space->ClearGrowthLimit(); |
| 3661 | malloc_space->SetFootprintLimit(malloc_space->Capacity()); |
| 3662 | } |
| 3663 | } |
| 3664 | // This space isn't added for performance reasons. |
| 3665 | if (main_space_backup_.get() != nullptr) { |
| 3666 | main_space_backup_->ClearGrowthLimit(); |
| 3667 | main_space_backup_->SetFootprintLimit(main_space_backup_->Capacity()); |
| 3668 | } |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 3669 | } |
| 3670 | |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 3671 | void Heap::AddFinalizerReference(Thread* self, mirror::Object** object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3672 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 3673 | ScopedLocalRef<jobject> arg(self->GetJniEnv(), soa.AddLocalReference<jobject>(*object)); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 3674 | jvalue args[1]; |
| 3675 | args[0].l = arg.get(); |
| 3676 | InvokeWithJValues(soa, nullptr, WellKnownClasses::java_lang_ref_FinalizerReference_add, args); |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 3677 | // Restore object in case it gets moved. |
| 3678 | *object = soa.Decode<mirror::Object*>(arg.get()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3679 | } |
| 3680 | |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3681 | void Heap::RequestConcurrentGCAndSaveObject(Thread* self, bool force_full, mirror::Object** obj) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3682 | StackHandleScope<1> hs(self); |
| 3683 | HandleWrapper<mirror::Object> wrapper(hs.NewHandleWrapper(obj)); |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3684 | RequestConcurrentGC(self, force_full); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3687 | class Heap::ConcurrentGCTask : public HeapTask { |
| 3688 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 3689 | ConcurrentGCTask(uint64_t target_time, bool force_full) |
| 3690 | : HeapTask(target_time), force_full_(force_full) { } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3691 | virtual void Run(Thread* self) OVERRIDE { |
| 3692 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3693 | heap->ConcurrentGC(self, force_full_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3694 | heap->ClearConcurrentGCRequest(); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 3695 | } |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3696 | |
| 3697 | private: |
| 3698 | const bool force_full_; // If true, force full (or partial) collection. |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3699 | }; |
| 3700 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3701 | static bool CanAddHeapTask(Thread* self) REQUIRES(!Locks::runtime_shutdown_lock_) { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3702 | Runtime* runtime = Runtime::Current(); |
| 3703 | return runtime != nullptr && runtime->IsFinishedStarting() && !runtime->IsShuttingDown(self) && |
| 3704 | !self->IsHandlingStackOverflow(); |
| 3705 | } |
| 3706 | |
| 3707 | void Heap::ClearConcurrentGCRequest() { |
| 3708 | concurrent_gc_pending_.StoreRelaxed(false); |
| 3709 | } |
| 3710 | |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3711 | void Heap::RequestConcurrentGC(Thread* self, bool force_full) { |
Mathieu Chartier | ac19516 | 2015-02-20 18:44:28 +0000 | [diff] [blame] | 3712 | if (CanAddHeapTask(self) && |
| 3713 | concurrent_gc_pending_.CompareExchangeStrongSequentiallyConsistent(false, true)) { |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3714 | task_processor_->AddTask(self, new ConcurrentGCTask(NanoTime(), // Start straight away. |
| 3715 | force_full)); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3716 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3719 | void Heap::ConcurrentGC(Thread* self, bool force_full) { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3720 | if (!Runtime::Current()->IsShuttingDown(self)) { |
| 3721 | // Wait for any GCs currently running to finish. |
| 3722 | if (WaitForGcToComplete(kGcCauseBackground, self) == collector::kGcTypeNone) { |
| 3723 | // If the we can't run the GC type we wanted to run, find the next appropriate one and try that |
| 3724 | // instead. E.g. can't do partial, so do full instead. |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3725 | collector::GcType next_gc_type = next_gc_type_; |
| 3726 | // If forcing full and next gc type is sticky, override with a non-sticky type. |
| 3727 | if (force_full && next_gc_type == collector::kGcTypeSticky) { |
| 3728 | next_gc_type = HasZygoteSpace() ? collector::kGcTypePartial : collector::kGcTypeFull; |
| 3729 | } |
| 3730 | if (CollectGarbageInternal(next_gc_type, kGcCauseBackground, false) == |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3731 | collector::kGcTypeNone) { |
| 3732 | for (collector::GcType gc_type : gc_plan_) { |
| 3733 | // Attempt to run the collector, if we succeed, we are done. |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3734 | if (gc_type > next_gc_type && |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3735 | CollectGarbageInternal(gc_type, kGcCauseBackground, false) != |
| 3736 | collector::kGcTypeNone) { |
| 3737 | break; |
| 3738 | } |
Mathieu Chartier | f9ed0d3 | 2013-11-21 16:42:47 -0800 | [diff] [blame] | 3739 | } |
| 3740 | } |
| 3741 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 3742 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3745 | class Heap::CollectorTransitionTask : public HeapTask { |
| 3746 | public: |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 3747 | explicit CollectorTransitionTask(uint64_t target_time) : HeapTask(target_time) {} |
| 3748 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3749 | virtual void Run(Thread* self) OVERRIDE { |
| 3750 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 3751 | heap->DoPendingCollectorTransition(); |
| 3752 | heap->ClearPendingCollectorTransition(self); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3753 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3754 | }; |
| 3755 | |
| 3756 | void Heap::ClearPendingCollectorTransition(Thread* self) { |
| 3757 | MutexLock mu(self, *pending_task_lock_); |
| 3758 | pending_collector_transition_ = nullptr; |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3759 | } |
| 3760 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3761 | void Heap::RequestCollectorTransition(CollectorType desired_collector_type, uint64_t delta_time) { |
| 3762 | Thread* self = Thread::Current(); |
| 3763 | desired_collector_type_ = desired_collector_type; |
| 3764 | if (desired_collector_type_ == collector_type_ || !CanAddHeapTask(self)) { |
| 3765 | return; |
| 3766 | } |
| 3767 | CollectorTransitionTask* added_task = nullptr; |
| 3768 | const uint64_t target_time = NanoTime() + delta_time; |
| 3769 | { |
| 3770 | MutexLock mu(self, *pending_task_lock_); |
| 3771 | // If we have an existing collector transition, update the targe time to be the new target. |
| 3772 | if (pending_collector_transition_ != nullptr) { |
| 3773 | task_processor_->UpdateTargetRunTime(self, pending_collector_transition_, target_time); |
| 3774 | return; |
| 3775 | } |
| 3776 | added_task = new CollectorTransitionTask(target_time); |
| 3777 | pending_collector_transition_ = added_task; |
| 3778 | } |
| 3779 | task_processor_->AddTask(self, added_task); |
| 3780 | } |
| 3781 | |
| 3782 | class Heap::HeapTrimTask : public HeapTask { |
| 3783 | public: |
| 3784 | explicit HeapTrimTask(uint64_t delta_time) : HeapTask(NanoTime() + delta_time) { } |
| 3785 | virtual void Run(Thread* self) OVERRIDE { |
| 3786 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 3787 | heap->Trim(self); |
| 3788 | heap->ClearPendingTrim(self); |
| 3789 | } |
| 3790 | }; |
| 3791 | |
| 3792 | void Heap::ClearPendingTrim(Thread* self) { |
| 3793 | MutexLock mu(self, *pending_task_lock_); |
| 3794 | pending_heap_trim_ = nullptr; |
| 3795 | } |
| 3796 | |
| 3797 | void Heap::RequestTrim(Thread* self) { |
| 3798 | if (!CanAddHeapTask(self)) { |
| 3799 | return; |
| 3800 | } |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 3801 | // GC completed and now we must decide whether to request a heap trim (advising pages back to the |
| 3802 | // kernel) or not. Issuing a request will also cause trimming of the libc heap. As a trim scans |
| 3803 | // a space it will hold its lock and can become a cause of jank. |
| 3804 | // Note, the large object space self trims and the Zygote space was trimmed and unchanging since |
| 3805 | // forking. |
| 3806 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 3807 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 3808 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 3809 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 3810 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 3811 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 3812 | // not how much use we're making of those pages. |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3813 | HeapTrimTask* added_task = nullptr; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 3814 | { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3815 | MutexLock mu(self, *pending_task_lock_); |
| 3816 | if (pending_heap_trim_ != nullptr) { |
| 3817 | // Already have a heap trim request in task processor, ignore this request. |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 3818 | return; |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3819 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3820 | added_task = new HeapTrimTask(kHeapTrimWait); |
| 3821 | pending_heap_trim_ = added_task; |
Mathieu Chartier | c39e342 | 2013-08-07 16:41:36 -0700 | [diff] [blame] | 3822 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3823 | task_processor_->AddTask(self, added_task); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3824 | } |
| 3825 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3826 | void Heap::RevokeThreadLocalBuffers(Thread* thread) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3827 | if (rosalloc_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3828 | size_t freed_bytes_revoke = rosalloc_space_->RevokeThreadLocalBuffers(thread); |
| 3829 | if (freed_bytes_revoke > 0U) { |
| 3830 | num_bytes_freed_revoke_.FetchAndAddSequentiallyConsistent(freed_bytes_revoke); |
| 3831 | CHECK_GE(num_bytes_allocated_.LoadRelaxed(), num_bytes_freed_revoke_.LoadRelaxed()); |
| 3832 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3833 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3834 | if (bump_pointer_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3835 | CHECK_EQ(bump_pointer_space_->RevokeThreadLocalBuffers(thread), 0U); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3836 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3837 | if (region_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3838 | CHECK_EQ(region_space_->RevokeThreadLocalBuffers(thread), 0U); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3839 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3840 | } |
| 3841 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 3842 | void Heap::RevokeRosAllocThreadLocalBuffers(Thread* thread) { |
| 3843 | if (rosalloc_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3844 | size_t freed_bytes_revoke = rosalloc_space_->RevokeThreadLocalBuffers(thread); |
| 3845 | if (freed_bytes_revoke > 0U) { |
| 3846 | num_bytes_freed_revoke_.FetchAndAddSequentiallyConsistent(freed_bytes_revoke); |
| 3847 | CHECK_GE(num_bytes_allocated_.LoadRelaxed(), num_bytes_freed_revoke_.LoadRelaxed()); |
| 3848 | } |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 3849 | } |
| 3850 | } |
| 3851 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3852 | void Heap::RevokeAllThreadLocalBuffers() { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3853 | if (rosalloc_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3854 | size_t freed_bytes_revoke = rosalloc_space_->RevokeAllThreadLocalBuffers(); |
| 3855 | if (freed_bytes_revoke > 0U) { |
| 3856 | num_bytes_freed_revoke_.FetchAndAddSequentiallyConsistent(freed_bytes_revoke); |
| 3857 | CHECK_GE(num_bytes_allocated_.LoadRelaxed(), num_bytes_freed_revoke_.LoadRelaxed()); |
| 3858 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3859 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3860 | if (bump_pointer_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3861 | CHECK_EQ(bump_pointer_space_->RevokeAllThreadLocalBuffers(), 0U); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3862 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3863 | if (region_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3864 | CHECK_EQ(region_space_->RevokeAllThreadLocalBuffers(), 0U); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3865 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3866 | } |
| 3867 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3868 | bool Heap::IsGCRequestPending() const { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3869 | return concurrent_gc_pending_.LoadRelaxed(); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3870 | } |
| 3871 | |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3872 | void Heap::RunFinalization(JNIEnv* env, uint64_t timeout) { |
| 3873 | env->CallStaticVoidMethod(WellKnownClasses::dalvik_system_VMRuntime, |
| 3874 | WellKnownClasses::dalvik_system_VMRuntime_runFinalization, |
| 3875 | static_cast<jlong>(timeout)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3876 | } |
| 3877 | |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3878 | void Heap::RegisterNativeAllocation(JNIEnv* env, size_t bytes) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3879 | Thread* self = ThreadForEnv(env); |
Mathieu Chartier | 5d2a3f7 | 2016-05-11 11:35:39 -0700 | [diff] [blame] | 3880 | { |
| 3881 | MutexLock mu(self, native_histogram_lock_); |
| 3882 | native_allocation_histogram_.AddValue(bytes); |
| 3883 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3884 | if (native_need_to_run_finalization_) { |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3885 | RunFinalization(env, kNativeAllocationFinalizeTimeout); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3886 | UpdateMaxNativeFootprint(); |
| 3887 | native_need_to_run_finalization_ = false; |
| 3888 | } |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3889 | // Total number of native bytes allocated. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 3890 | size_t new_native_bytes_allocated = native_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes); |
| 3891 | new_native_bytes_allocated += bytes; |
| 3892 | if (new_native_bytes_allocated > native_footprint_gc_watermark_) { |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 3893 | collector::GcType gc_type = HasZygoteSpace() ? collector::kGcTypePartial : |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 3894 | collector::kGcTypeFull; |
| 3895 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3896 | // The second watermark is higher than the gc watermark. If you hit this it means you are |
| 3897 | // allocating native objects faster than the GC can keep up with. |
Mathieu Chartier | 0848745 | 2014-09-02 16:21:01 -0700 | [diff] [blame] | 3898 | if (new_native_bytes_allocated > growth_limit_) { |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3899 | if (WaitForGcToComplete(kGcCauseForNativeAlloc, self) != collector::kGcTypeNone) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3900 | // Just finished a GC, attempt to run finalizers. |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3901 | RunFinalization(env, kNativeAllocationFinalizeTimeout); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3902 | CHECK(!env->ExceptionCheck()); |
Lin Zang | 60e2716 | 2015-03-10 18:53:21 +0800 | [diff] [blame] | 3903 | // Native bytes allocated may be updated by finalization, refresh it. |
| 3904 | new_native_bytes_allocated = native_bytes_allocated_.LoadRelaxed(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3905 | } |
| 3906 | // If we still are over the watermark, attempt a GC for alloc and run finalizers. |
Mathieu Chartier | 0848745 | 2014-09-02 16:21:01 -0700 | [diff] [blame] | 3907 | if (new_native_bytes_allocated > growth_limit_) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 3908 | CollectGarbageInternal(gc_type, kGcCauseForNativeAlloc, false); |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3909 | RunFinalization(env, kNativeAllocationFinalizeTimeout); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3910 | native_need_to_run_finalization_ = false; |
| 3911 | CHECK(!env->ExceptionCheck()); |
| 3912 | } |
| 3913 | // We have just run finalizers, update the native watermark since it is very likely that |
| 3914 | // finalizers released native managed allocations. |
| 3915 | UpdateMaxNativeFootprint(); |
| 3916 | } else if (!IsGCRequestPending()) { |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 3917 | if (IsGcConcurrent()) { |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3918 | RequestConcurrentGC(self, true); // Request non-sticky type. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3919 | } else { |
Hiroshi Yamauchi | d20aba1 | 2014-04-11 15:31:09 -0700 | [diff] [blame] | 3920 | CollectGarbageInternal(gc_type, kGcCauseForNativeAlloc, false); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3921 | } |
| 3922 | } |
| 3923 | } |
| 3924 | } |
| 3925 | |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3926 | void Heap::RegisterNativeFree(JNIEnv* env, size_t bytes) { |
| 3927 | size_t expected_size; |
Mathieu Chartier | 5d2a3f7 | 2016-05-11 11:35:39 -0700 | [diff] [blame] | 3928 | { |
| 3929 | MutexLock mu(Thread::Current(), native_histogram_lock_); |
| 3930 | native_free_histogram_.AddValue(bytes); |
| 3931 | } |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3932 | do { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 3933 | expected_size = native_bytes_allocated_.LoadRelaxed(); |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3934 | if (UNLIKELY(bytes > expected_size)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3935 | ScopedObjectAccess soa(env); |
| 3936 | env->ThrowNew(WellKnownClasses::java_lang_RuntimeException, |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3937 | StringPrintf("Attempted to free %zd native bytes with only %zd native bytes " |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3938 | "registered as allocated", bytes, expected_size).c_str()); |
| 3939 | break; |
| 3940 | } |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3941 | } while (!native_bytes_allocated_.CompareExchangeWeakRelaxed(expected_size, |
| 3942 | expected_size - bytes)); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3943 | } |
| 3944 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3945 | size_t Heap::GetTotalMemory() const { |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 3946 | return std::max(max_allowed_footprint_, GetBytesAllocated()); |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 3947 | } |
| 3948 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3949 | void Heap::AddModUnionTable(accounting::ModUnionTable* mod_union_table) { |
| 3950 | DCHECK(mod_union_table != nullptr); |
| 3951 | mod_union_tables_.Put(mod_union_table->GetSpace(), mod_union_table); |
| 3952 | } |
| 3953 | |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 3954 | void Heap::CheckPreconditionsForAllocObject(mirror::Class* c, size_t byte_count) { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3955 | CHECK(c == nullptr || (c->IsClassClass() && byte_count >= sizeof(mirror::Class)) || |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 3956 | (c->IsVariableSize() || c->GetObjectSize() == byte_count)) << c->GetClassFlags(); |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 3957 | CHECK_GE(byte_count, sizeof(mirror::Object)); |
| 3958 | } |
| 3959 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3960 | void Heap::AddRememberedSet(accounting::RememberedSet* remembered_set) { |
| 3961 | CHECK(remembered_set != nullptr); |
| 3962 | space::Space* space = remembered_set->GetSpace(); |
| 3963 | CHECK(space != nullptr); |
Mathieu Chartier | 8e4a96d | 2014-05-21 10:44:32 -0700 | [diff] [blame] | 3964 | CHECK(remembered_sets_.find(space) == remembered_sets_.end()) << space; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3965 | remembered_sets_.Put(space, remembered_set); |
Mathieu Chartier | 8e4a96d | 2014-05-21 10:44:32 -0700 | [diff] [blame] | 3966 | CHECK(remembered_sets_.find(space) != remembered_sets_.end()) << space; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3967 | } |
| 3968 | |
| 3969 | void Heap::RemoveRememberedSet(space::Space* space) { |
| 3970 | CHECK(space != nullptr); |
| 3971 | auto it = remembered_sets_.find(space); |
| 3972 | CHECK(it != remembered_sets_.end()); |
Mathieu Chartier | 5189e24 | 2014-07-24 11:11:05 -0700 | [diff] [blame] | 3973 | delete it->second; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3974 | remembered_sets_.erase(it); |
| 3975 | CHECK(remembered_sets_.find(space) == remembered_sets_.end()); |
| 3976 | } |
| 3977 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 3978 | void Heap::ClearMarkedObjects() { |
| 3979 | // Clear all of the spaces' mark bitmaps. |
| 3980 | for (const auto& space : GetContinuousSpaces()) { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 3981 | accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 3982 | if (space->GetLiveBitmap() != mark_bitmap) { |
| 3983 | mark_bitmap->Clear(); |
| 3984 | } |
| 3985 | } |
| 3986 | // Clear the marked objects in the discontinous space object sets. |
| 3987 | for (const auto& space : GetDiscontinuousSpaces()) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 3988 | space->GetMarkBitmap()->Clear(); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 3989 | } |
| 3990 | } |
| 3991 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 3992 | void Heap::SetAllocationRecords(AllocRecordObjectMap* records) { |
| 3993 | allocation_records_.reset(records); |
| 3994 | } |
| 3995 | |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 3996 | void Heap::VisitAllocationRecords(RootVisitor* visitor) const { |
| 3997 | if (IsAllocTrackingEnabled()) { |
| 3998 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 3999 | if (IsAllocTrackingEnabled()) { |
| 4000 | GetAllocationRecords()->VisitRoots(visitor); |
| 4001 | } |
| 4002 | } |
| 4003 | } |
| 4004 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 4005 | void Heap::SweepAllocationRecords(IsMarkedVisitor* visitor) const { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4006 | if (IsAllocTrackingEnabled()) { |
| 4007 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 4008 | if (IsAllocTrackingEnabled()) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 4009 | GetAllocationRecords()->SweepAllocationRecords(visitor); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | } |
| 4013 | |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 4014 | void Heap::AllowNewAllocationRecords() const { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 4015 | CHECK(!kUseReadBarrier); |
Hiroshi Yamauchi | 6f0c6cd | 2016-03-18 17:17:52 -0700 | [diff] [blame] | 4016 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 4017 | AllocRecordObjectMap* allocation_records = GetAllocationRecords(); |
| 4018 | if (allocation_records != nullptr) { |
| 4019 | allocation_records->AllowNewAllocationRecords(); |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | void Heap::DisallowNewAllocationRecords() const { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 4024 | CHECK(!kUseReadBarrier); |
Hiroshi Yamauchi | 6f0c6cd | 2016-03-18 17:17:52 -0700 | [diff] [blame] | 4025 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 4026 | AllocRecordObjectMap* allocation_records = GetAllocationRecords(); |
| 4027 | if (allocation_records != nullptr) { |
| 4028 | allocation_records->DisallowNewAllocationRecords(); |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 4032 | void Heap::BroadcastForNewAllocationRecords() const { |
| 4033 | CHECK(kUseReadBarrier); |
Hiroshi Yamauchi | 6f0c6cd | 2016-03-18 17:17:52 -0700 | [diff] [blame] | 4034 | // Always broadcast without checking IsAllocTrackingEnabled() because IsAllocTrackingEnabled() may |
| 4035 | // be set to false while some threads are waiting for system weak access in |
| 4036 | // AllocRecordObjectMap::RecordAllocation() and we may fail to wake them up. b/27467554. |
| 4037 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 4038 | AllocRecordObjectMap* allocation_records = GetAllocationRecords(); |
| 4039 | if (allocation_records != nullptr) { |
| 4040 | allocation_records->BroadcastForNewAllocationRecords(); |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 4041 | } |
| 4042 | } |
| 4043 | |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 4044 | // Based on debug malloc logic from libc/bionic/debug_stacktrace.cpp. |
| 4045 | class StackCrawlState { |
| 4046 | public: |
| 4047 | StackCrawlState(uintptr_t* frames, size_t max_depth, size_t skip_count) |
| 4048 | : frames_(frames), frame_count_(0), max_depth_(max_depth), skip_count_(skip_count) { |
| 4049 | } |
| 4050 | size_t GetFrameCount() const { |
| 4051 | return frame_count_; |
| 4052 | } |
| 4053 | static _Unwind_Reason_Code Callback(_Unwind_Context* context, void* arg) { |
| 4054 | auto* const state = reinterpret_cast<StackCrawlState*>(arg); |
| 4055 | const uintptr_t ip = _Unwind_GetIP(context); |
| 4056 | // The first stack frame is get_backtrace itself. Skip it. |
| 4057 | if (ip != 0 && state->skip_count_ > 0) { |
| 4058 | --state->skip_count_; |
| 4059 | return _URC_NO_REASON; |
| 4060 | } |
| 4061 | // ip may be off for ARM but it shouldn't matter since we only use it for hashing. |
| 4062 | state->frames_[state->frame_count_] = ip; |
| 4063 | state->frame_count_++; |
| 4064 | return state->frame_count_ >= state->max_depth_ ? _URC_END_OF_STACK : _URC_NO_REASON; |
| 4065 | } |
| 4066 | |
| 4067 | private: |
| 4068 | uintptr_t* const frames_; |
| 4069 | size_t frame_count_; |
| 4070 | const size_t max_depth_; |
| 4071 | size_t skip_count_; |
| 4072 | }; |
| 4073 | |
| 4074 | static size_t get_backtrace(uintptr_t* frames, size_t max_depth) { |
| 4075 | StackCrawlState state(frames, max_depth, 0u); |
| 4076 | _Unwind_Backtrace(&StackCrawlState::Callback, &state); |
| 4077 | return state.GetFrameCount(); |
| 4078 | } |
| 4079 | |
| 4080 | void Heap::CheckGcStressMode(Thread* self, mirror::Object** obj) { |
| 4081 | auto* const runtime = Runtime::Current(); |
| 4082 | if (gc_stress_mode_ && runtime->GetClassLinker()->IsInitialized() && |
| 4083 | !runtime->IsActiveTransaction() && mirror::Class::HasJavaLangClass()) { |
| 4084 | // Check if we should GC. |
| 4085 | bool new_backtrace = false; |
| 4086 | { |
| 4087 | static constexpr size_t kMaxFrames = 16u; |
| 4088 | uintptr_t backtrace[kMaxFrames]; |
| 4089 | const size_t frames = get_backtrace(backtrace, kMaxFrames); |
| 4090 | uint64_t hash = 0; |
| 4091 | for (size_t i = 0; i < frames; ++i) { |
| 4092 | hash = hash * 2654435761 + backtrace[i]; |
| 4093 | hash += (hash >> 13) ^ (hash << 6); |
| 4094 | } |
| 4095 | MutexLock mu(self, *backtrace_lock_); |
| 4096 | new_backtrace = seen_backtraces_.find(hash) == seen_backtraces_.end(); |
| 4097 | if (new_backtrace) { |
| 4098 | seen_backtraces_.insert(hash); |
| 4099 | } |
| 4100 | } |
| 4101 | if (new_backtrace) { |
| 4102 | StackHandleScope<1> hs(self); |
| 4103 | auto h = hs.NewHandleWrapper(obj); |
| 4104 | CollectGarbage(false); |
| 4105 | unique_backtrace_count_.FetchAndAddSequentiallyConsistent(1); |
| 4106 | } else { |
| 4107 | seen_backtrace_count_.FetchAndAddSequentiallyConsistent(1); |
| 4108 | } |
| 4109 | } |
| 4110 | } |
| 4111 | |
Mathieu Chartier | 5116837 | 2015-08-12 16:40:32 -0700 | [diff] [blame] | 4112 | void Heap::DisableGCForShutdown() { |
| 4113 | Thread* const self = Thread::Current(); |
| 4114 | CHECK(Runtime::Current()->IsShuttingDown(self)); |
| 4115 | MutexLock mu(self, *gc_complete_lock_); |
| 4116 | gc_disabled_for_shutdown_ = true; |
| 4117 | } |
| 4118 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 4119 | bool Heap::ObjectIsInBootImageSpace(mirror::Object* obj) const { |
| 4120 | for (gc::space::ImageSpace* space : boot_image_spaces_) { |
| 4121 | if (space->HasAddress(obj)) { |
| 4122 | return true; |
| 4123 | } |
| 4124 | } |
| 4125 | return false; |
| 4126 | } |
| 4127 | |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 4128 | bool Heap::IsInBootImageOatFile(const void* p) const { |
| 4129 | for (gc::space::ImageSpace* space : boot_image_spaces_) { |
| 4130 | if (space->GetOatFile()->Contains(p)) { |
| 4131 | return true; |
| 4132 | } |
| 4133 | } |
| 4134 | return false; |
| 4135 | } |
| 4136 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 4137 | void Heap::GetBootImagesSize(uint32_t* boot_image_begin, |
| 4138 | uint32_t* boot_image_end, |
| 4139 | uint32_t* boot_oat_begin, |
| 4140 | uint32_t* boot_oat_end) { |
| 4141 | DCHECK(boot_image_begin != nullptr); |
| 4142 | DCHECK(boot_image_end != nullptr); |
| 4143 | DCHECK(boot_oat_begin != nullptr); |
| 4144 | DCHECK(boot_oat_end != nullptr); |
| 4145 | *boot_image_begin = 0u; |
| 4146 | *boot_image_end = 0u; |
| 4147 | *boot_oat_begin = 0u; |
| 4148 | *boot_oat_end = 0u; |
| 4149 | for (gc::space::ImageSpace* space_ : GetBootImageSpaces()) { |
| 4150 | const uint32_t image_begin = PointerToLowMemUInt32(space_->Begin()); |
| 4151 | const uint32_t image_size = space_->GetImageHeader().GetImageSize(); |
| 4152 | if (*boot_image_begin == 0 || image_begin < *boot_image_begin) { |
| 4153 | *boot_image_begin = image_begin; |
| 4154 | } |
| 4155 | *boot_image_end = std::max(*boot_image_end, image_begin + image_size); |
| 4156 | const OatFile* boot_oat_file = space_->GetOatFile(); |
| 4157 | const uint32_t oat_begin = PointerToLowMemUInt32(boot_oat_file->Begin()); |
| 4158 | const uint32_t oat_size = boot_oat_file->Size(); |
| 4159 | if (*boot_oat_begin == 0 || oat_begin < *boot_oat_begin) { |
| 4160 | *boot_oat_begin = oat_begin; |
| 4161 | } |
| 4162 | *boot_oat_end = std::max(*boot_oat_end, oat_begin + oat_size); |
| 4163 | } |
| 4164 | } |
| 4165 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4166 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 4167 | } // namespace art |