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