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