Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_HEAP_H_ |
| 18 | #define ART_RUNTIME_GC_HEAP_H_ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 20 | #include <iosfwd> |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 21 | #include <string> |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 22 | #include <unordered_set> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 27 | #include "allocator_type.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 28 | #include "base/atomic.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include "base/macros.h" |
Andreas Gampe | 351c447 | 2017-07-12 19:32:55 -0700 | [diff] [blame] | 30 | #include "base/mutex.h" |
Andreas Gampe | dcc528d | 2017-12-07 13:37:10 -0800 | [diff] [blame] | 31 | #include "base/runtime_debug.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 32 | #include "base/safe_map.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 33 | #include "base/time_utils.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | #include "gc/collector/gc_type.h" |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 35 | #include "gc/collector/iteration.h" |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 36 | #include "gc/collector_type.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 37 | #include "gc/gc_cause.h" |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 38 | #include "gc/space/image_space_loading_order.h" |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 39 | #include "gc/space/large_object_space.h" |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 40 | #include "handle.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 41 | #include "obj_ptr.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 42 | #include "offsets.h" |
Mathieu Chartier | f8cb178 | 2016-03-18 18:45:41 -0700 | [diff] [blame] | 43 | #include "process_state.h" |
Andreas Gampe | 217488a | 2017-09-18 08:34:42 -0700 | [diff] [blame] | 44 | #include "read_barrier_config.h" |
Andreas Gampe | 5a0430d | 2019-01-04 14:33:57 -0800 | [diff] [blame] | 45 | #include "runtime_globals.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 46 | #include "verify_object.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 47 | |
| 48 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 49 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 50 | class ConditionVariable; |
Andreas Gampe | 639b2b1 | 2019-01-08 10:32:50 -0800 | [diff] [blame] | 51 | enum class InstructionSet; |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 52 | class IsMarkedVisitor; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 53 | class Mutex; |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 54 | class ReflectiveValueVisitor; |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 55 | class RootVisitor; |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 56 | class StackVisitor; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 57 | class Thread; |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 58 | class ThreadPool; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 59 | class TimingLogger; |
Mathieu Chartier | aea9bfb | 2016-10-12 19:19:56 -0700 | [diff] [blame] | 60 | class VariableSizedHandleScope; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 61 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 62 | namespace mirror { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 63 | class Class; |
| 64 | class Object; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 65 | } // namespace mirror |
| 66 | |
| 67 | namespace gc { |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 68 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 69 | class AllocationListener; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 70 | class AllocRecordObjectMap; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 71 | class GcPauseListener; |
Mathieu Chartier | ad390fa | 2019-10-16 20:03:00 -0700 | [diff] [blame] | 72 | class HeapTask; |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 73 | class ReferenceProcessor; |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 74 | class TaskProcessor; |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 75 | class Verification; |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 76 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 77 | namespace accounting { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 78 | template <typename T> class AtomicStack; |
| 79 | typedef AtomicStack<mirror::Object> ObjectStack; |
| 80 | class CardTable; |
| 81 | class HeapBitmap; |
| 82 | class ModUnionTable; |
| 83 | class ReadBarrierTable; |
| 84 | class RememberedSet; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 85 | } // namespace accounting |
| 86 | |
| 87 | namespace collector { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 88 | class ConcurrentCopying; |
| 89 | class GarbageCollector; |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 90 | class MarkSweep; |
| 91 | class SemiSpace; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 92 | } // namespace collector |
| 93 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 94 | namespace allocator { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 95 | class RosAlloc; |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 96 | } // namespace allocator |
| 97 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 98 | namespace space { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 99 | class AllocSpace; |
| 100 | class BumpPointerSpace; |
| 101 | class ContinuousMemMapAllocSpace; |
| 102 | class DiscontinuousSpace; |
| 103 | class DlMallocSpace; |
| 104 | class ImageSpace; |
| 105 | class LargeObjectSpace; |
| 106 | class MallocSpace; |
| 107 | class RegionSpace; |
| 108 | class RosAllocSpace; |
| 109 | class Space; |
| 110 | class ZygoteSpace; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 111 | } // namespace space |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 112 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 113 | enum HomogeneousSpaceCompactResult { |
| 114 | // Success. |
| 115 | kSuccess, |
| 116 | // Reject due to disabled moving GC. |
| 117 | kErrorReject, |
Mathieu Chartier | db00eaf | 2015-08-31 17:10:05 -0700 | [diff] [blame] | 118 | // Unsupported due to the current configuration. |
| 119 | kErrorUnsupported, |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 120 | // System is shutting down. |
| 121 | kErrorVMShuttingDown, |
| 122 | }; |
| 123 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 124 | // If true, use rosalloc/RosAllocSpace instead of dlmalloc/DlMallocSpace |
Hiroshi Yamauchi | d31fb97 | 2013-11-19 11:08:27 -0800 | [diff] [blame] | 125 | static constexpr bool kUseRosAlloc = true; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 126 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 127 | // If true, use thread-local allocation stack. |
| 128 | static constexpr bool kUseThreadLocalAllocationStack = true; |
| 129 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 130 | class Heap { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 131 | public: |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 132 | static constexpr size_t kDefaultStartingSize = kPageSize; |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 133 | static constexpr size_t kDefaultInitialSize = 2 * MB; |
Mathieu Chartier | 2f4f0a3 | 2014-07-10 17:50:34 -0700 | [diff] [blame] | 134 | static constexpr size_t kDefaultMaximumSize = 256 * MB; |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 135 | static constexpr size_t kDefaultNonMovingSpaceCapacity = 64 * MB; |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 136 | static constexpr size_t kDefaultMaxFree = 2 * MB; |
| 137 | static constexpr size_t kDefaultMinFree = kDefaultMaxFree / 4; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 138 | static constexpr size_t kDefaultLongPauseLogThreshold = MsToNs(5); |
| 139 | static constexpr size_t kDefaultLongGCLogThreshold = MsToNs(100); |
Mathieu Chartier | 9e03b2f | 2016-05-27 13:50:59 -0700 | [diff] [blame] | 140 | static constexpr size_t kDefaultTLABSize = 32 * KB; |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 141 | static constexpr double kDefaultTargetUtilization = 0.5; |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 142 | static constexpr double kDefaultHeapGrowthMultiplier = 2.0; |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 143 | // Primitive arrays larger than this size are put in the large object space. |
Mathieu Chartier | 8261d02 | 2016-08-08 09:41:04 -0700 | [diff] [blame] | 144 | static constexpr size_t kMinLargeObjectThreshold = 3 * kPageSize; |
| 145 | static constexpr size_t kDefaultLargeObjectThreshold = kMinLargeObjectThreshold; |
Mathieu Chartier | 10d6886 | 2015-04-15 14:21:33 -0700 | [diff] [blame] | 146 | // Whether or not parallel GC is enabled. If not, then we never create the thread pool. |
| 147 | static constexpr bool kDefaultEnableParallelGC = false; |
Mathieu Chartier | fa4ea82 | 2018-03-02 13:48:54 -0800 | [diff] [blame] | 148 | static uint8_t* const kPreferredAllocSpaceBegin; |
Igor Murashkin | 446ba4b | 2015-02-04 15:11:27 -0800 | [diff] [blame] | 149 | |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 150 | // Whether or not we use the free list large object space. Only use it if USE_ART_LOW_4G_ALLOCATOR |
| 151 | // since this means that we have to use the slow msync loop in MemMap::MapAnonymous. |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 152 | static constexpr space::LargeObjectSpaceType kDefaultLargeObjectSpaceType = |
Igor Murashkin | 446ba4b | 2015-02-04 15:11:27 -0800 | [diff] [blame] | 153 | USE_ART_LOW_4G_ALLOCATOR ? |
| 154 | space::LargeObjectSpaceType::kFreeList |
| 155 | : space::LargeObjectSpaceType::kMap; |
| 156 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 157 | // Used so that we don't overflow the allocation time atomic integer. |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 158 | static constexpr size_t kTimeAdjust = 1024; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 159 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 160 | // Client should call NotifyNativeAllocation every kNotifyNativeInterval allocations. |
| 161 | // Should be chosen so that time_to_call_mallinfo / kNotifyNativeInterval is on the same order |
| 162 | // as object allocation time. time_to_call_mallinfo seems to be on the order of 1 usec. |
Hans Boehm | 1575267 | 2018-12-18 17:01:00 -0800 | [diff] [blame] | 163 | #ifdef __ANDROID__ |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 164 | static constexpr uint32_t kNotifyNativeInterval = 32; |
Hans Boehm | 1575267 | 2018-12-18 17:01:00 -0800 | [diff] [blame] | 165 | #else |
| 166 | // Some host mallinfo() implementations are slow. And memory is less scarce. |
Hans Boehm | 82e347b | 2019-08-15 17:45:05 -0700 | [diff] [blame] | 167 | static constexpr uint32_t kNotifyNativeInterval = 512; |
Hans Boehm | 1575267 | 2018-12-18 17:01:00 -0800 | [diff] [blame] | 168 | #endif |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 169 | |
| 170 | // RegisterNativeAllocation checks immediately whether GC is needed if size exceeds the |
| 171 | // following. kCheckImmediatelyThreshold * kNotifyNativeInterval should be small enough to |
| 172 | // make it safe to allocate that many bytes between checks. |
| 173 | static constexpr size_t kCheckImmediatelyThreshold = 300000; |
| 174 | |
Mathieu Chartier | 7bf52d2 | 2014-03-13 14:46:09 -0700 | [diff] [blame] | 175 | // How often we allow heap trimming to happen (nanoseconds). |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 176 | static constexpr uint64_t kHeapTrimWait = MsToNs(5000); |
Mathieu Chartier | 7bf52d2 | 2014-03-13 14:46:09 -0700 | [diff] [blame] | 177 | // How long we wait after a transition request to perform a collector transition (nanoseconds). |
| 178 | static constexpr uint64_t kCollectorTransitionWait = MsToNs(5000); |
Andreas Gampe | ed56b5e | 2017-10-19 12:58:19 -0700 | [diff] [blame] | 179 | // Whether the transition-wait applies or not. Zero wait will stress the |
| 180 | // transition code and collector, but increases jank probability. |
| 181 | DECLARE_RUNTIME_DEBUG_FLAG(kStressCollectorTransition); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 182 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 183 | // Create a heap with the requested sizes. The possible empty |
| 184 | // image_file_names names specify Spaces to load based on |
| 185 | // ImageWriter output. |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 186 | Heap(size_t initial_size, |
| 187 | size_t growth_limit, |
| 188 | size_t min_free, |
| 189 | size_t max_free, |
| 190 | double target_utilization, |
| 191 | double foreground_heap_growth_multiplier, |
Hans Boehm | bb2467b | 2019-03-29 22:55:06 -0700 | [diff] [blame] | 192 | size_t stop_for_native_allocs, |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 193 | size_t capacity, |
| 194 | size_t non_moving_space_capacity, |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 195 | const std::vector<std::string>& boot_class_path, |
| 196 | const std::vector<std::string>& boot_class_path_locations, |
| 197 | const std::string& image_file_name, |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 198 | InstructionSet image_instruction_set, |
| 199 | CollectorType foreground_collector_type, |
| 200 | CollectorType background_collector_type, |
| 201 | space::LargeObjectSpaceType large_object_space_type, |
| 202 | size_t large_object_threshold, |
| 203 | size_t parallel_gc_threads, |
| 204 | size_t conc_gc_threads, |
| 205 | bool low_memory_mode, |
| 206 | size_t long_pause_threshold, |
| 207 | size_t long_gc_threshold, |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 208 | bool ignore_target_footprint, |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 209 | bool use_tlab, |
| 210 | bool verify_pre_gc_heap, |
| 211 | bool verify_pre_sweeping_heap, |
| 212 | bool verify_post_gc_heap, |
| 213 | bool verify_pre_gc_rosalloc, |
| 214 | bool verify_pre_sweeping_rosalloc, |
| 215 | bool verify_post_gc_rosalloc, |
| 216 | bool gc_stress_mode, |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 217 | bool measure_gc_performance, |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 218 | bool use_homogeneous_space_compaction, |
Albert Mingkun Yang | 0b4d146 | 2018-11-29 13:25:35 +0000 | [diff] [blame] | 219 | bool use_generational_cc, |
Albert Mingkun Yang | de94ea7 | 2018-11-16 10:15:49 +0000 | [diff] [blame] | 220 | uint64_t min_interval_homogeneous_space_compaction_by_oom, |
| 221 | bool dump_region_info_before_gc, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 222 | bool dump_region_info_after_gc, |
| 223 | space::ImageSpaceLoadingOrder image_space_loading_order); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 224 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 225 | ~Heap(); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 226 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 227 | // Allocates and initializes storage for an object instance. |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 228 | template <bool kInstrumented = true, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 229 | mirror::Object* AllocObject(Thread* self, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 230 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 231 | size_t num_bytes, |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 232 | const PreFenceVisitor& pre_fence_visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 233 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 234 | REQUIRES(!*gc_complete_lock_, |
| 235 | !*pending_task_lock_, |
| 236 | !*backtrace_lock_, |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 237 | !Roles::uninterruptible_) { |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 238 | return AllocObjectWithAllocator<kInstrumented>(self, |
| 239 | klass, |
| 240 | num_bytes, |
| 241 | GetCurrentAllocator(), |
| 242 | pre_fence_visitor); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 243 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 244 | |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 245 | template <bool kInstrumented = true, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 246 | mirror::Object* AllocNonMovableObject(Thread* self, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 247 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 248 | size_t num_bytes, |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 249 | const PreFenceVisitor& pre_fence_visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 250 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 251 | REQUIRES(!*gc_complete_lock_, |
| 252 | !*pending_task_lock_, |
| 253 | !*backtrace_lock_, |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 254 | !Roles::uninterruptible_) { |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 255 | return AllocObjectWithAllocator<kInstrumented>(self, |
| 256 | klass, |
| 257 | num_bytes, |
| 258 | GetCurrentNonMovingAllocator(), |
| 259 | pre_fence_visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 260 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 261 | |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 262 | template <bool kInstrumented = true, bool kCheckLargeObject = true, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 263 | ALWAYS_INLINE mirror::Object* AllocObjectWithAllocator(Thread* self, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 264 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 265 | size_t byte_count, |
| 266 | AllocatorType allocator, |
| 267 | const PreFenceVisitor& pre_fence_visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 268 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 269 | REQUIRES(!*gc_complete_lock_, |
| 270 | !*pending_task_lock_, |
| 271 | !*backtrace_lock_, |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 272 | !Roles::uninterruptible_); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 273 | |
| 274 | AllocatorType GetCurrentAllocator() const { |
| 275 | return current_allocator_; |
| 276 | } |
| 277 | |
| 278 | AllocatorType GetCurrentNonMovingAllocator() const { |
| 279 | return current_non_moving_allocator_; |
| 280 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 281 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 282 | // Visit all of the live objects in the heap. |
Andreas Gampe | 351c447 | 2017-07-12 19:32:55 -0700 | [diff] [blame] | 283 | template <typename Visitor> |
| 284 | ALWAYS_INLINE void VisitObjects(Visitor&& visitor) |
| 285 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 286 | REQUIRES(!Locks::heap_bitmap_lock_, !*gc_complete_lock_); |
| 287 | template <typename Visitor> |
| 288 | ALWAYS_INLINE void VisitObjectsPaused(Visitor&& visitor) |
| 289 | REQUIRES(Locks::mutator_lock_, !Locks::heap_bitmap_lock_, !*gc_complete_lock_); |
| 290 | |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 291 | void VisitReflectiveTargets(ReflectiveValueVisitor* visitor) |
| 292 | REQUIRES(Locks::mutator_lock_, !Locks::heap_bitmap_lock_, !*gc_complete_lock_); |
| 293 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 294 | void CheckPreconditionsForAllocObject(ObjPtr<mirror::Class> c, size_t byte_count) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 295 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 296 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 297 | // Inform the garbage collector of a non-malloc allocated native memory that might become |
| 298 | // reclaimable in the future as a result of Java garbage collection. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 299 | void RegisterNativeAllocation(JNIEnv* env, size_t bytes) |
Richard Uhler | f4eedfe | 2017-12-29 14:48:42 +0000 | [diff] [blame] | 300 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_); |
Richard Uhler | caaa2b0 | 2017-02-01 09:54:17 +0000 | [diff] [blame] | 301 | void RegisterNativeFree(JNIEnv* env, size_t bytes); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 302 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 303 | // Notify the garbage collector of malloc allocations that might be reclaimable |
| 304 | // as a result of Java garbage collection. Each such call represents approximately |
| 305 | // kNotifyNativeInterval such allocations. |
| 306 | void NotifyNativeAllocations(JNIEnv* env) |
| 307 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_); |
| 308 | |
| 309 | uint32_t GetNotifyNativeInterval() { |
| 310 | return kNotifyNativeInterval; |
| 311 | } |
| 312 | |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 313 | // Change the allocator, updates entrypoints. |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 314 | void ChangeAllocator(AllocatorType allocator) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 315 | REQUIRES(Locks::mutator_lock_, !Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 316 | |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 317 | // Change the collector to be one of the possible options (MS, CMS, SS). |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 318 | void ChangeCollector(CollectorType collector_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 319 | REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 320 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 321 | // The given reference is believed to be to an object in the Java heap, check the soundness of it. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 322 | // TODO: NO_THREAD_SAFETY_ANALYSIS since we call this everywhere and it is impossible to find a |
| 323 | // proper lock ordering for it. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 324 | void VerifyObjectBody(ObjPtr<mirror::Object> o) NO_THREAD_SAFETY_ANALYSIS; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 325 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 326 | // Check sanity of all live references. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 327 | void VerifyHeap() REQUIRES(!Locks::heap_bitmap_lock_); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 328 | // Returns how many failures occured. |
| 329 | size_t VerifyHeapReferences(bool verify_referents = true) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 330 | REQUIRES(Locks::mutator_lock_, !*gc_complete_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 331 | bool VerifyMissingCardMarks() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 332 | REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 333 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 334 | // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock, |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 335 | // and doesn't abort on error, allowing the caller to report more |
| 336 | // meaningful diagnostics. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 337 | bool IsValidObjectAddress(const void* obj) const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 338 | |
Mathieu Chartier | d68ac70 | 2014-02-11 14:50:51 -0800 | [diff] [blame] | 339 | // Faster alternative to IsHeapAddress since finding if an object is in the large object space is |
| 340 | // very slow. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 341 | bool IsNonDiscontinuousSpaceHeapAddress(const void* addr) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 342 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 343 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 344 | // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses). |
| 345 | // Requires the heap lock to be held. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 346 | bool IsLiveObjectLocked(ObjPtr<mirror::Object> obj, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 347 | bool search_allocation_stack = true, |
| 348 | bool search_live_stack = true, |
| 349 | bool sorted = false) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 350 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 351 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 352 | // Returns true if there is any chance that the object (obj) will move. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 353 | bool IsMovableObject(ObjPtr<mirror::Object> obj) const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 354 | |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 355 | // Enables us to compacting GC until objects are released. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 356 | void IncrementDisableMovingGC(Thread* self) REQUIRES(!*gc_complete_lock_); |
| 357 | void DecrementDisableMovingGC(Thread* self) REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 358 | |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 359 | // Temporarily disable thread flip for JNI critical calls. |
| 360 | void IncrementDisableThreadFlip(Thread* self) REQUIRES(!*thread_flip_lock_); |
| 361 | void DecrementDisableThreadFlip(Thread* self) REQUIRES(!*thread_flip_lock_); |
| 362 | void ThreadFlipBegin(Thread* self) REQUIRES(!*thread_flip_lock_); |
| 363 | void ThreadFlipEnd(Thread* self) REQUIRES(!*thread_flip_lock_); |
| 364 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 365 | // Clear all of the mark bits, doesn't clear bitmaps which have the same live bits as mark bits. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 366 | // Mutator lock is required for GetContinuousSpaces. |
| 367 | void ClearMarkedObjects() |
| 368 | REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 369 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 370 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 371 | // Initiates an explicit garbage collection. |
Andreas Gampe | 94c589d | 2017-12-27 12:43:01 -0800 | [diff] [blame] | 372 | void CollectGarbage(bool clear_soft_references, GcCause cause = kGcCauseExplicit) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 373 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 374 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 375 | // Does a concurrent GC, should only be called by the GC daemon thread |
| 376 | // through runtime. |
Mathieu Chartier | 35b59a2 | 2017-04-17 15:24:43 -0700 | [diff] [blame] | 377 | void ConcurrentGC(Thread* self, GcCause cause, bool force_full) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 378 | REQUIRES(!Locks::runtime_shutdown_lock_, !*gc_complete_lock_, !*pending_task_lock_); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 379 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 380 | // Implements VMDebug.countInstancesOfClass and JDWP VM_InstanceCount. |
| 381 | // The boolean decides whether to use IsAssignableFrom or == when comparing classes. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 382 | void CountInstances(const std::vector<Handle<mirror::Class>>& classes, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 383 | bool use_is_assignable_from, |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 384 | uint64_t* counts) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 385 | REQUIRES(!Locks::heap_bitmap_lock_, !*gc_complete_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 386 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 387 | |
Richard Uhler | 660be6f | 2017-11-22 16:12:29 +0000 | [diff] [blame] | 388 | // Implements VMDebug.getInstancesOfClasses and JDWP RT_Instances. |
Mathieu Chartier | 2d85595 | 2016-10-12 19:37:59 -0700 | [diff] [blame] | 389 | void GetInstances(VariableSizedHandleScope& scope, |
| 390 | Handle<mirror::Class> c, |
Richard Uhler | 660be6f | 2017-11-22 16:12:29 +0000 | [diff] [blame] | 391 | bool use_is_assignable_from, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 392 | int32_t max_count, |
Mathieu Chartier | 2d85595 | 2016-10-12 19:37:59 -0700 | [diff] [blame] | 393 | std::vector<Handle<mirror::Object>>& instances) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 394 | REQUIRES(!Locks::heap_bitmap_lock_, !*gc_complete_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 395 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 396 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 397 | // Implements JDWP OR_ReferringObjects. |
Mathieu Chartier | aea9bfb | 2016-10-12 19:19:56 -0700 | [diff] [blame] | 398 | void GetReferringObjects(VariableSizedHandleScope& scope, |
| 399 | Handle<mirror::Object> o, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 400 | int32_t max_count, |
Mathieu Chartier | aea9bfb | 2016-10-12 19:19:56 -0700 | [diff] [blame] | 401 | std::vector<Handle<mirror::Object>>& referring_objects) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 402 | REQUIRES(!Locks::heap_bitmap_lock_, !*gc_complete_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 403 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 404 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 405 | // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to |
| 406 | // implement dalvik.system.VMRuntime.clearGrowthLimit. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 407 | void ClearGrowthLimit(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 408 | |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 409 | // Make the current growth limit the new maximum capacity, unmaps pages at the end of spaces |
| 410 | // which will never be used. Used to implement dalvik.system.VMRuntime.clampGrowthLimit. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 411 | void ClampGrowthLimit() REQUIRES(!Locks::heap_bitmap_lock_); |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 412 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 413 | // Target ideal heap utilization ratio, implements |
| 414 | // dalvik.system.VMRuntime.getTargetHeapUtilization. |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 415 | double GetTargetHeapUtilization() const { |
| 416 | return target_utilization_; |
| 417 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 418 | |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 419 | // Data structure memory usage tracking. |
| 420 | void RegisterGCAllocation(size_t bytes); |
| 421 | void RegisterGCDeAllocation(size_t bytes); |
| 422 | |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 423 | // Set the heap's private space pointers to be the same as the space based on it's type. Public |
| 424 | // due to usage by tests. |
| 425 | void SetSpaceAsDefault(space::ContinuousSpace* continuous_space) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 426 | REQUIRES(!Locks::heap_bitmap_lock_); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 427 | void AddSpace(space::Space* space) |
| 428 | REQUIRES(!Locks::heap_bitmap_lock_) |
| 429 | REQUIRES(Locks::mutator_lock_); |
| 430 | void RemoveSpace(space::Space* space) |
| 431 | REQUIRES(!Locks::heap_bitmap_lock_) |
| 432 | REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 433 | |
Albert Mingkun Yang | 6e0d325 | 2018-12-10 15:22:45 +0000 | [diff] [blame] | 434 | double GetPreGcWeightedAllocatedBytes() const { |
| 435 | return pre_gc_weighted_allocated_bytes_; |
Albert Mingkun Yang | 2d7329b | 2018-11-30 19:58:18 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Albert Mingkun Yang | 6e0d325 | 2018-12-10 15:22:45 +0000 | [diff] [blame] | 438 | double GetPostGcWeightedAllocatedBytes() const { |
| 439 | return post_gc_weighted_allocated_bytes_; |
| 440 | } |
| 441 | |
| 442 | void CalculatePreGcWeightedAllocatedBytes(); |
| 443 | void CalculatePostGcWeightedAllocatedBytes(); |
Albert Mingkun Yang | d6e178e | 2018-11-19 12:58:30 +0000 | [diff] [blame] | 444 | uint64_t GetTotalGcCpuTime(); |
| 445 | |
Albert Mingkun Yang | 2d7329b | 2018-11-30 19:58:18 +0000 | [diff] [blame] | 446 | uint64_t GetProcessCpuStartTime() const { |
| 447 | return process_cpu_start_time_ns_; |
| 448 | } |
| 449 | |
Albert Mingkun Yang | 1c0ae84 | 2019-01-11 16:52:24 +0000 | [diff] [blame] | 450 | uint64_t GetPostGCLastProcessCpuTime() const { |
| 451 | return post_gc_last_process_cpu_time_ns_; |
| 452 | } |
| 453 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 454 | // Set target ideal heap utilization ratio, implements |
| 455 | // dalvik.system.VMRuntime.setTargetHeapUtilization. |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 456 | void SetTargetHeapUtilization(float target); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 457 | |
| 458 | // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate |
| 459 | // from the system. Doesn't allow the space to exceed its growth limit. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 460 | void SetIdealFootprint(size_t max_allowed_footprint); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 461 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 462 | // Blocks the caller until the garbage collector becomes idle and returns the type of GC we |
| 463 | // waited for. |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 464 | collector::GcType WaitForGcToComplete(GcCause cause, Thread* self) REQUIRES(!*gc_complete_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 465 | |
Mathieu Chartier | ca2a24d | 2013-11-25 15:12:12 -0800 | [diff] [blame] | 466 | // Update the heap's process state to a new value, may cause compaction to occur. |
Mathieu Chartier | f8cb178 | 2016-03-18 18:45:41 -0700 | [diff] [blame] | 467 | void UpdateProcessState(ProcessState old_process_state, ProcessState new_process_state) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 468 | REQUIRES(!*pending_task_lock_, !*gc_complete_lock_); |
Mathieu Chartier | ca2a24d | 2013-11-25 15:12:12 -0800 | [diff] [blame] | 469 | |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 470 | bool HaveContinuousSpaces() const NO_THREAD_SAFETY_ANALYSIS { |
| 471 | // No lock since vector empty is thread safe. |
| 472 | return !continuous_spaces_.empty(); |
| 473 | } |
| 474 | |
| 475 | const std::vector<space::ContinuousSpace*>& GetContinuousSpaces() const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 476 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 477 | return continuous_spaces_; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 480 | const std::vector<space::DiscontinuousSpace*>& GetDiscontinuousSpaces() const { |
| 481 | return discontinuous_spaces_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 482 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 483 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 484 | const collector::Iteration* GetCurrentGcIteration() const { |
| 485 | return ¤t_gc_iteration_; |
| 486 | } |
| 487 | collector::Iteration* GetCurrentGcIteration() { |
| 488 | return ¤t_gc_iteration_; |
| 489 | } |
| 490 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 491 | // Enable verification of object references when the runtime is sufficiently initialized. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 492 | void EnableObjectValidation() { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 493 | verify_object_mode_ = kVerifyObjectSupport; |
| 494 | if (verify_object_mode_ > kVerifyObjectModeDisabled) { |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 495 | VerifyHeap(); |
| 496 | } |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 499 | // Disable object reference verification for image writing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 500 | void DisableObjectValidation() { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 501 | verify_object_mode_ = kVerifyObjectModeDisabled; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 504 | // Other checks may be performed if we know the heap should be in a sane state. |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 505 | bool IsObjectValidationEnabled() const { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 506 | return verify_object_mode_ > kVerifyObjectModeDisabled; |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Mathieu Chartier | e0a53e9 | 2013-08-05 10:17:40 -0700 | [diff] [blame] | 509 | // Returns true if low memory mode is enabled. |
| 510 | bool IsLowMemoryMode() const { |
| 511 | return low_memory_mode_; |
| 512 | } |
| 513 | |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 514 | // Returns the heap growth multiplier, this affects how much we grow the heap after a GC. |
| 515 | // Scales heap growth, min free, and max free. |
| 516 | double HeapGrowthMultiplier() const; |
| 517 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 518 | // Freed bytes can be negative in cases where we copy objects from a compacted space to a |
| 519 | // free-list backed space. |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 520 | void RecordFree(uint64_t freed_objects, int64_t freed_bytes); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 521 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 522 | // Record the bytes freed by thread-local buffer revoke. |
| 523 | void RecordFreeRevoke(); |
| 524 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 525 | accounting::CardTable* GetCardTable() const { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 526 | return card_table_.get(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 529 | accounting::ReadBarrierTable* GetReadBarrierTable() const { |
| 530 | return rb_table_.get(); |
| 531 | } |
| 532 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 533 | void AddFinalizerReference(Thread* self, ObjPtr<mirror::Object>* object); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 534 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 535 | // Returns the number of bytes currently allocated. |
Hans Boehm | fb8b4e2 | 2018-09-05 16:45:42 -0700 | [diff] [blame] | 536 | // The result should be treated as an approximation, if it is being concurrently updated. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 537 | size_t GetBytesAllocated() const { |
Hans Boehm | fb8b4e2 | 2018-09-05 16:45:42 -0700 | [diff] [blame] | 538 | return num_bytes_allocated_.load(std::memory_order_relaxed); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Albert Mingkun Yang | 0b4d146 | 2018-11-29 13:25:35 +0000 | [diff] [blame] | 541 | bool GetUseGenerationalCC() const { |
| 542 | return use_generational_cc_; |
| 543 | } |
| 544 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 545 | // Returns the number of objects currently allocated. |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 546 | size_t GetObjectsAllocated() const |
| 547 | REQUIRES(!Locks::heap_bitmap_lock_); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 548 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 549 | // Returns the total number of objects allocated since the heap was created. |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 550 | uint64_t GetObjectsAllocatedEver() const; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 551 | |
| 552 | // Returns the total number of bytes allocated since the heap was created. |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 553 | uint64_t GetBytesAllocatedEver() const; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 554 | |
| 555 | // Returns the total number of objects freed since the heap was created. |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 556 | uint64_t GetObjectsFreedEver() const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 557 | return total_objects_freed_ever_; |
| 558 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 559 | |
| 560 | // Returns the total number of bytes freed since the heap was created. |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 561 | uint64_t GetBytesFreedEver() const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 562 | return total_bytes_freed_ever_; |
| 563 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 564 | |
Albert Mingkun Yang | f9f22f9 | 2018-12-14 19:54:34 +0000 | [diff] [blame] | 565 | space::RegionSpace* GetRegionSpace() const { |
| 566 | return region_space_; |
| 567 | } |
| 568 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 569 | // Implements java.lang.Runtime.maxMemory, returning the maximum amount of memory a program can |
| 570 | // consume. For a regular VM this would relate to the -Xmx option and would return -1 if no Xmx |
| 571 | // were specified. Android apps start with a growth limit (small heap size) which is |
| 572 | // cleared/extended for large apps. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 573 | size_t GetMaxMemory() const { |
Hans Boehm | fb8b4e2 | 2018-09-05 16:45:42 -0700 | [diff] [blame] | 574 | // There are some race conditions in the allocation code that can cause bytes allocated to |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 575 | // become larger than growth_limit_ in rare cases. |
| 576 | return std::max(GetBytesAllocated(), growth_limit_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 579 | // Implements java.lang.Runtime.totalMemory, returning approximate amount of memory currently |
| 580 | // consumed by an application. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 581 | size_t GetTotalMemory() const; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 582 | |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 583 | // Returns approximately how much free memory we have until the next GC happens. |
| 584 | size_t GetFreeMemoryUntilGC() const { |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 585 | return UnsignedDifference(target_footprint_.load(std::memory_order_relaxed), |
| 586 | GetBytesAllocated()); |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Returns approximately how much free memory we have until the next OOME happens. |
| 590 | size_t GetFreeMemoryUntilOOME() const { |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 591 | return UnsignedDifference(growth_limit_, GetBytesAllocated()); |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Returns how much free memory we have until we need to grow the heap to perform an allocation. |
| 595 | // Similar to GetFreeMemoryUntilGC. Implements java.lang.Runtime.freeMemory. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 596 | size_t GetFreeMemory() const { |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 597 | return UnsignedDifference(GetTotalMemory(), |
| 598 | num_bytes_allocated_.load(std::memory_order_relaxed)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Roland Levillain | ef01222 | 2017-06-21 16:28:06 +0100 | [diff] [blame] | 601 | // Get the space that corresponds to an object's address. Current implementation searches all |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 602 | // spaces in turn. If fail_ok is false then failing to find a space will cause an abort. |
| 603 | // TODO: consider using faster data structure like binary tree. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 604 | space::ContinuousSpace* FindContinuousSpaceFromObject(ObjPtr<mirror::Object>, bool fail_ok) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 605 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 606 | |
| 607 | space::ContinuousSpace* FindContinuousSpaceFromAddress(const mirror::Object* addr) const |
| 608 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 609 | |
| 610 | space::DiscontinuousSpace* FindDiscontinuousSpaceFromObject(ObjPtr<mirror::Object>, |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 611 | bool fail_ok) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 612 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 613 | |
| 614 | space::Space* FindSpaceFromObject(ObjPtr<mirror::Object> obj, bool fail_ok) const |
| 615 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 616 | |
| 617 | space::Space* FindSpaceFromAddress(const void* ptr) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 618 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 619 | |
Roland Levillain | 5fcf1ea | 2018-10-30 11:58:08 +0000 | [diff] [blame] | 620 | std::string DumpSpaceNameFromAddress(const void* addr) const |
| 621 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 622 | |
Richard Uhler | caaa2b0 | 2017-02-01 09:54:17 +0000 | [diff] [blame] | 623 | void DumpForSigQuit(std::ostream& os) REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 624 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 625 | // Do a pending collector transition. |
Hiroshi Yamauchi | 60985b7 | 2016-08-24 13:53:12 -0700 | [diff] [blame] | 626 | void DoPendingCollectorTransition() REQUIRES(!*gc_complete_lock_, !*pending_task_lock_); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 627 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 628 | // Deflate monitors, ... and trim the spaces. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 629 | void Trim(Thread* self) REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 630 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 631 | void RevokeThreadLocalBuffers(Thread* thread); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 632 | void RevokeRosAllocThreadLocalBuffers(Thread* thread); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 633 | void RevokeAllThreadLocalBuffers(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 634 | void AssertThreadLocalBuffersAreRevoked(Thread* thread); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 635 | void AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 636 | void RosAllocVerification(TimingLogger* timings, const char* name) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 637 | REQUIRES(Locks::mutator_lock_); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 638 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 639 | accounting::HeapBitmap* GetLiveBitmap() REQUIRES_SHARED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 640 | return live_bitmap_.get(); |
| 641 | } |
| 642 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 643 | accounting::HeapBitmap* GetMarkBitmap() REQUIRES_SHARED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 644 | return mark_bitmap_.get(); |
| 645 | } |
| 646 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 647 | accounting::ObjectStack* GetLiveStack() REQUIRES_SHARED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 648 | return live_stack_.get(); |
| 649 | } |
| 650 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 651 | void PreZygoteFork() NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 652 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 653 | // Mark and empty stack. |
| 654 | void FlushAllocStack() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 655 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 656 | REQUIRES(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 657 | |
Hiroshi Yamauchi | 90d7068 | 2014-02-20 16:17:30 -0800 | [diff] [blame] | 658 | // Revoke all the thread-local allocation stacks. |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 659 | void RevokeAllThreadLocalAllocationStacks(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 660 | REQUIRES(Locks::mutator_lock_, !Locks::runtime_shutdown_lock_, !Locks::thread_list_lock_); |
Hiroshi Yamauchi | 90d7068 | 2014-02-20 16:17:30 -0800 | [diff] [blame] | 661 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 662 | // Mark all the objects in the allocation stack in the specified bitmap. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 663 | // TODO: Refactor? |
| 664 | void MarkAllocStack(accounting::SpaceBitmap<kObjectAlignment>* bitmap1, |
| 665 | accounting::SpaceBitmap<kObjectAlignment>* bitmap2, |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 666 | accounting::SpaceBitmap<kLargeObjectAlignment>* large_objects, |
| 667 | accounting::ObjectStack* stack) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 668 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 669 | REQUIRES(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 670 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 671 | // Mark the specified allocation stack as live. |
| 672 | void MarkAllocStackAsLive(accounting::ObjectStack* stack) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 673 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 674 | REQUIRES(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 8235331 | 2013-07-18 10:47:51 -0700 | [diff] [blame] | 675 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 676 | // Unbind any bound bitmaps. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 677 | void UnBindBitmaps() |
| 678 | REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 679 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 680 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 681 | // Returns the boot image spaces. There may be multiple boot image spaces. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 682 | const std::vector<space::ImageSpace*>& GetBootImageSpaces() const { |
| 683 | return boot_image_spaces_; |
| 684 | } |
| 685 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 686 | bool ObjectIsInBootImageSpace(ObjPtr<mirror::Object> obj) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 687 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 688 | |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 689 | bool IsInBootImageOatFile(const void* p) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 690 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 691 | |
Vladimir Marko | 1aff1ef | 2019-07-02 15:12:50 +0100 | [diff] [blame] | 692 | // Get the start address of the boot images if any; otherwise returns 0. |
Vladimir Marko | 7cde458 | 2019-07-05 13:26:11 +0100 | [diff] [blame] | 693 | uint32_t GetBootImagesStartAddress() const { |
| 694 | return boot_images_start_address_; |
| 695 | } |
Vladimir Marko | 1aff1ef | 2019-07-02 15:12:50 +0100 | [diff] [blame] | 696 | |
| 697 | // Get the size of all boot images, including the heap and oat areas. |
Vladimir Marko | 7cde458 | 2019-07-05 13:26:11 +0100 | [diff] [blame] | 698 | uint32_t GetBootImagesSize() const { |
| 699 | return boot_images_size_; |
| 700 | } |
| 701 | |
| 702 | // Check if a pointer points to a boot image. |
| 703 | bool IsBootImageAddress(const void* p) const { |
| 704 | return reinterpret_cast<uintptr_t>(p) - boot_images_start_address_ < boot_images_size_; |
| 705 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 706 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 707 | space::DlMallocSpace* GetDlMallocSpace() const { |
| 708 | return dlmalloc_space_; |
| 709 | } |
| 710 | |
| 711 | space::RosAllocSpace* GetRosAllocSpace() const { |
| 712 | return rosalloc_space_; |
| 713 | } |
| 714 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 715 | // Return the corresponding rosalloc space. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 716 | space::RosAllocSpace* GetRosAllocSpace(gc::allocator::RosAlloc* rosalloc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 717 | REQUIRES_SHARED(Locks::mutator_lock_); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 718 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 719 | space::MallocSpace* GetNonMovingSpace() const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 720 | return non_moving_space_; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 721 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 722 | |
| 723 | space::LargeObjectSpace* GetLargeObjectsSpace() const { |
| 724 | return large_object_space_; |
| 725 | } |
| 726 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 727 | // Returns the free list space that may contain movable objects (the |
| 728 | // one that's not the non-moving space), either rosalloc_space_ or |
| 729 | // dlmalloc_space_. |
| 730 | space::MallocSpace* GetPrimaryFreeListSpace() { |
| 731 | if (kUseRosAlloc) { |
| 732 | DCHECK(rosalloc_space_ != nullptr); |
| 733 | // reinterpret_cast is necessary as the space class hierarchy |
| 734 | // isn't known (#included) yet here. |
| 735 | return reinterpret_cast<space::MallocSpace*>(rosalloc_space_); |
| 736 | } else { |
| 737 | DCHECK(dlmalloc_space_ != nullptr); |
| 738 | return reinterpret_cast<space::MallocSpace*>(dlmalloc_space_); |
| 739 | } |
| 740 | } |
| 741 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 742 | void DumpSpaces(std::ostream& stream) const REQUIRES_SHARED(Locks::mutator_lock_); |
| 743 | std::string DumpSpaces() const REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 744 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 745 | // GC performance measuring |
Mathieu Chartier | 5d2a3f7 | 2016-05-11 11:35:39 -0700 | [diff] [blame] | 746 | void DumpGcPerformanceInfo(std::ostream& os) |
Richard Uhler | caaa2b0 | 2017-02-01 09:54:17 +0000 | [diff] [blame] | 747 | REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 748 | void ResetGcPerformanceInfo() REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 749 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 750 | // Thread pool. |
| 751 | void CreateThreadPool(); |
| 752 | void DeleteThreadPool(); |
| 753 | ThreadPool* GetThreadPool() { |
| 754 | return thread_pool_.get(); |
| 755 | } |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 756 | size_t GetParallelGCThreadCount() const { |
| 757 | return parallel_gc_threads_; |
| 758 | } |
| 759 | size_t GetConcGCThreadCount() const { |
| 760 | return conc_gc_threads_; |
| 761 | } |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 762 | accounting::ModUnionTable* FindModUnionTableFromSpace(space::Space* space); |
| 763 | void AddModUnionTable(accounting::ModUnionTable* mod_union_table); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 764 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 765 | accounting::RememberedSet* FindRememberedSetFromSpace(space::Space* space); |
| 766 | void AddRememberedSet(accounting::RememberedSet* remembered_set); |
Mathieu Chartier | 5189e24 | 2014-07-24 11:11:05 -0700 | [diff] [blame] | 767 | // Also deletes the remebered set. |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 768 | void RemoveRememberedSet(space::Space* space); |
| 769 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 770 | bool IsCompilingBoot() const; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 771 | bool HasBootImageSpace() const { |
| 772 | return !boot_image_spaces_.empty(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 773 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 774 | |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 775 | ReferenceProcessor* GetReferenceProcessor() { |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 776 | return reference_processor_.get(); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 777 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 778 | TaskProcessor* GetTaskProcessor() { |
| 779 | return task_processor_.get(); |
| 780 | } |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 781 | |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 782 | bool HasZygoteSpace() const { |
| 783 | return zygote_space_ != nullptr; |
| 784 | } |
| 785 | |
Mathieu Chartier | 8d1a996 | 2016-08-17 16:39:45 -0700 | [diff] [blame] | 786 | // Returns the active concurrent copying collector. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 787 | collector::ConcurrentCopying* ConcurrentCopyingCollector() { |
Albert Mingkun Yang | 0b4d146 | 2018-11-29 13:25:35 +0000 | [diff] [blame] | 788 | if (use_generational_cc_) { |
Mathieu Chartier | 8d1a996 | 2016-08-17 16:39:45 -0700 | [diff] [blame] | 789 | DCHECK((active_concurrent_copying_collector_ == concurrent_copying_collector_) || |
| 790 | (active_concurrent_copying_collector_ == young_concurrent_copying_collector_)); |
| 791 | } else { |
| 792 | DCHECK_EQ(active_concurrent_copying_collector_, concurrent_copying_collector_); |
| 793 | } |
| 794 | return active_concurrent_copying_collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | CollectorType CurrentCollectorType() { |
| 798 | return collector_type_; |
| 799 | } |
| 800 | |
| 801 | bool IsGcConcurrentAndMoving() const { |
| 802 | if (IsGcConcurrent() && IsMovingGc(collector_type_)) { |
| 803 | // Assume no transition when a concurrent moving collector is used. |
| 804 | DCHECK_EQ(collector_type_, foreground_collector_type_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 805 | return true; |
| 806 | } |
| 807 | return false; |
| 808 | } |
| 809 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 810 | bool IsMovingGCDisabled(Thread* self) REQUIRES(!*gc_complete_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 811 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | d6b17d4 | 2017-02-17 12:50:39 -0800 | [diff] [blame] | 812 | return disable_moving_gc_count_ > 0; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 815 | // Request an asynchronous trim. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 816 | void RequestTrim(Thread* self) REQUIRES(!*pending_task_lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 817 | |
| 818 | // Request asynchronous GC. |
Mathieu Chartier | 35b59a2 | 2017-04-17 15:24:43 -0700 | [diff] [blame] | 819 | void RequestConcurrentGC(Thread* self, GcCause cause, bool force_full) |
| 820 | REQUIRES(!*pending_task_lock_); |
Mathieu Chartier | 079101a | 2014-12-15 14:23:10 -0800 | [diff] [blame] | 821 | |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 822 | // Whether or not we may use a garbage collector, used so that we only create collectors we need. |
| 823 | bool MayUseCollector(CollectorType type) const; |
| 824 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 825 | // Used by tests to reduce timinig-dependent flakiness in OOME behavior. |
| 826 | void SetMinIntervalHomogeneousSpaceCompactionByOom(uint64_t interval) { |
| 827 | min_interval_homogeneous_space_compaction_by_oom_ = interval; |
| 828 | } |
| 829 | |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 830 | // Helpers for android.os.Debug.getRuntimeStat(). |
| 831 | uint64_t GetGcCount() const; |
| 832 | uint64_t GetGcTime() const; |
| 833 | uint64_t GetBlockingGcCount() const; |
| 834 | uint64_t GetBlockingGcTime() const; |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 835 | void DumpGcCountRateHistogram(std::ostream& os) const REQUIRES(!*gc_complete_lock_); |
| 836 | void DumpBlockingGcCountRateHistogram(std::ostream& os) const REQUIRES(!*gc_complete_lock_); |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 837 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 838 | // Allocation tracking support |
| 839 | // Callers to this function use double-checked locking to ensure safety on allocation_records_ |
| 840 | bool IsAllocTrackingEnabled() const { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 841 | return alloc_tracking_enabled_.load(std::memory_order_relaxed); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 844 | void SetAllocTrackingEnabled(bool enabled) REQUIRES(Locks::alloc_tracker_lock_) { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 845 | alloc_tracking_enabled_.store(enabled, std::memory_order_relaxed); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Mathieu Chartier | 0a20607 | 2019-03-28 12:29:22 -0700 | [diff] [blame] | 848 | // Return the current stack depth of allocation records. |
| 849 | size_t GetAllocTrackerStackDepth() const { |
| 850 | return alloc_record_depth_; |
| 851 | } |
| 852 | |
| 853 | // Return the current stack depth of allocation records. |
| 854 | void SetAllocTrackerStackDepth(size_t alloc_record_depth) { |
| 855 | alloc_record_depth_ = alloc_record_depth; |
| 856 | } |
| 857 | |
| 858 | AllocRecordObjectMap* GetAllocationRecords() const REQUIRES(Locks::alloc_tracker_lock_) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 859 | return allocation_records_.get(); |
| 860 | } |
| 861 | |
| 862 | void SetAllocationRecords(AllocRecordObjectMap* records) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 863 | REQUIRES(Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 864 | |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 865 | void VisitAllocationRecords(RootVisitor* visitor) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 866 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 867 | REQUIRES(!Locks::alloc_tracker_lock_); |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 868 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 869 | void SweepAllocationRecords(IsMarkedVisitor* visitor) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 870 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 871 | REQUIRES(!Locks::alloc_tracker_lock_); |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 872 | |
| 873 | void DisallowNewAllocationRecords() const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 874 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 875 | REQUIRES(!Locks::alloc_tracker_lock_); |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 876 | |
| 877 | void AllowNewAllocationRecords() const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 878 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 879 | REQUIRES(!Locks::alloc_tracker_lock_); |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 880 | |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 881 | void BroadcastForNewAllocationRecords() const |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 882 | REQUIRES(!Locks::alloc_tracker_lock_); |
| 883 | |
Mathieu Chartier | 5116837 | 2015-08-12 16:40:32 -0700 | [diff] [blame] | 884 | void DisableGCForShutdown() REQUIRES(!*gc_complete_lock_); |
| 885 | |
Mathieu Chartier | db00eaf | 2015-08-31 17:10:05 -0700 | [diff] [blame] | 886 | // Create a new alloc space and compact default alloc space to it. |
| 887 | HomogeneousSpaceCompactResult PerformHomogeneousSpaceCompact() REQUIRES(!*gc_complete_lock_); |
| 888 | bool SupportHomogeneousSpaceCompactAndCollectorTransitions() const; |
| 889 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 890 | // Install an allocation listener. |
| 891 | void SetAllocationListener(AllocationListener* l); |
| 892 | // Remove an allocation listener. Note: the listener must not be deleted, as for performance |
| 893 | // reasons, we assume it stays valid when we read it (so that we don't require a lock). |
| 894 | void RemoveAllocationListener(); |
| 895 | |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 896 | // Install a gc pause listener. |
| 897 | void SetGcPauseListener(GcPauseListener* l); |
| 898 | // Get the currently installed gc pause listener, or null. |
| 899 | GcPauseListener* GetGcPauseListener() { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 900 | return gc_pause_listener_.load(std::memory_order_acquire); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 901 | } |
| 902 | // Remove a gc pause listener. Note: the listener must not be deleted, as for performance |
| 903 | // reasons, we assume it stays valid when we read it (so that we don't require a lock). |
| 904 | void RemoveGcPauseListener(); |
| 905 | |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 906 | const Verification* GetVerification() const; |
| 907 | |
Mathieu Chartier | a98a282 | 2017-05-24 16:14:10 -0700 | [diff] [blame] | 908 | void PostForkChildAction(Thread* self); |
| 909 | |
Lokesh Gidra | ea5b429 | 2019-08-08 16:27:21 -0700 | [diff] [blame] | 910 | void TraceHeapSize(size_t heap_size); |
| 911 | |
Mathieu Chartier | ad390fa | 2019-10-16 20:03:00 -0700 | [diff] [blame] | 912 | bool AddHeapTask(gc::HeapTask* task); |
| 913 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 914 | private: |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 915 | class ConcurrentGCTask; |
| 916 | class CollectorTransitionTask; |
| 917 | class HeapTrimTask; |
Mathieu Chartier | a98a282 | 2017-05-24 16:14:10 -0700 | [diff] [blame] | 918 | class TriggerPostForkCCGcTask; |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 919 | |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 920 | // Compact source space to target space. Returns the collector used. |
| 921 | collector::GarbageCollector* Compact(space::ContinuousMemMapAllocSpace* target_space, |
| 922 | space::ContinuousMemMapAllocSpace* source_space, |
| 923 | GcCause gc_cause) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 924 | REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 925 | |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 926 | void LogGC(GcCause gc_cause, collector::GarbageCollector* collector); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 927 | void StartGC(Thread* self, GcCause cause, CollectorType collector_type) |
| 928 | REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 929 | void FinishGC(Thread* self, collector::GcType gc_type) REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 930 | |
Albert Mingkun Yang | 6e0d325 | 2018-12-10 15:22:45 +0000 | [diff] [blame] | 931 | double CalculateGcWeightedAllocatedBytes(uint64_t gc_last_process_cpu_time_ns, |
| 932 | uint64_t current_process_cpu_time) const; |
| 933 | |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 934 | // Create a mem map with a preferred base address. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 935 | static MemMap MapAnonymousPreferredAddress(const char* name, |
| 936 | uint8_t* request_begin, |
| 937 | size_t capacity, |
| 938 | std::string* out_error_str); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 939 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 940 | bool SupportHSpaceCompaction() const { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 941 | // Returns true if we can do hspace compaction |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 942 | return main_space_backup_ != nullptr; |
| 943 | } |
| 944 | |
Hans Boehm | 7c73dd1 | 2019-02-06 00:20:18 +0000 | [diff] [blame] | 945 | // Size_t saturating arithmetic |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 946 | static ALWAYS_INLINE size_t UnsignedDifference(size_t x, size_t y) { |
| 947 | return x > y ? x - y : 0; |
| 948 | } |
Hans Boehm | 7c73dd1 | 2019-02-06 00:20:18 +0000 | [diff] [blame] | 949 | static ALWAYS_INLINE size_t UnsignedSum(size_t x, size_t y) { |
| 950 | return x + y >= x ? x + y : std::numeric_limits<size_t>::max(); |
| 951 | } |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 952 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 953 | static ALWAYS_INLINE bool AllocatorHasAllocationStack(AllocatorType allocator_type) { |
| 954 | return |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 955 | allocator_type != kAllocatorTypeRegionTLAB && |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 956 | allocator_type != kAllocatorTypeBumpPointer && |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 957 | allocator_type != kAllocatorTypeTLAB && |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 958 | allocator_type != kAllocatorTypeRegion; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 959 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 960 | static ALWAYS_INLINE bool AllocatorMayHaveConcurrentGC(AllocatorType allocator_type) { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 961 | if (kUseReadBarrier) { |
| 962 | // Read barrier may have the TLAB allocator but is always concurrent. TODO: clean this up. |
| 963 | return true; |
| 964 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 965 | return |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 966 | allocator_type != kAllocatorTypeTLAB && |
| 967 | allocator_type != kAllocatorTypeBumpPointer; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 968 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 969 | static bool IsMovingGc(CollectorType collector_type) { |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 970 | return |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 971 | collector_type == kCollectorTypeCC || |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 972 | collector_type == kCollectorTypeSS || |
Hiroshi Yamauchi | 60985b7 | 2016-08-24 13:53:12 -0700 | [diff] [blame] | 973 | collector_type == kCollectorTypeCCBackground || |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 974 | collector_type == kCollectorTypeHomogeneousSpaceCompact; |
Mathieu Chartier | 9be9a7a | 2014-01-24 14:07:33 -0800 | [diff] [blame] | 975 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 976 | bool ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 977 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 978 | |
| 979 | // Checks whether we should garbage collect: |
| 980 | ALWAYS_INLINE bool ShouldConcurrentGCForJava(size_t new_num_bytes_allocated); |
Hans Boehm | 7c73dd1 | 2019-02-06 00:20:18 +0000 | [diff] [blame] | 981 | float NativeMemoryOverTarget(size_t current_native_bytes, bool is_gc_concurrent); |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 982 | ALWAYS_INLINE void CheckConcurrentGCForJava(Thread* self, |
| 983 | size_t new_num_bytes_allocated, |
| 984 | ObjPtr<mirror::Object>* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 985 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 986 | REQUIRES(!*pending_task_lock_, !*gc_complete_lock_); |
Hans Boehm | 7c73dd1 | 2019-02-06 00:20:18 +0000 | [diff] [blame] | 987 | void CheckGCForNative(Thread* self) |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 988 | REQUIRES(!*pending_task_lock_, !*gc_complete_lock_); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 989 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 990 | accounting::ObjectStack* GetMarkStack() { |
| 991 | return mark_stack_.get(); |
| 992 | } |
| 993 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 994 | // We don't force this to be inlined since it is a slow path. |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 995 | template <bool kInstrumented, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 996 | mirror::Object* AllocLargeObject(Thread* self, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 997 | ObjPtr<mirror::Class>* klass, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 998 | size_t byte_count, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 999 | const PreFenceVisitor& pre_fence_visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1000 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1001 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_, !*backtrace_lock_); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1002 | |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 1003 | // Handles Allocate()'s slow allocation path with GC involved after |
| 1004 | // an initial allocation attempt failed. |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1005 | mirror::Object* AllocateInternalWithGc(Thread* self, |
| 1006 | AllocatorType allocator, |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1007 | bool instrumented, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1008 | size_t num_bytes, |
| 1009 | size_t* bytes_allocated, |
| 1010 | size_t* usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1011 | size_t* bytes_tl_bulk_allocated, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1012 | ObjPtr<mirror::Class>* klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1013 | REQUIRES(!Locks::thread_suspend_count_lock_, !*gc_complete_lock_, !*pending_task_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1014 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1015 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1016 | // Allocate into a specific space. |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1017 | mirror::Object* AllocateInto(Thread* self, |
| 1018 | space::AllocSpace* space, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1019 | ObjPtr<mirror::Class> c, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1020 | size_t bytes) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1021 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1022 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1023 | // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the |
| 1024 | // wrong space. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1025 | void SwapSemiSpaces() REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1026 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1027 | // Try to allocate a number of bytes, this function never does any GCs. Needs to be inlined so |
| 1028 | // that the switch statement is constant optimized in the entrypoints. |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1029 | template <const bool kInstrumented, const bool kGrow> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1030 | ALWAYS_INLINE mirror::Object* TryToAllocate(Thread* self, |
| 1031 | AllocatorType allocator_type, |
| 1032 | size_t alloc_size, |
| 1033 | size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1034 | size_t* usable_size, |
| 1035 | size_t* bytes_tl_bulk_allocated) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1036 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1037 | |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 1038 | mirror::Object* AllocWithNewTLAB(Thread* self, |
| 1039 | size_t alloc_size, |
| 1040 | bool grow, |
| 1041 | size_t* bytes_allocated, |
| 1042 | size_t* usable_size, |
| 1043 | size_t* bytes_tl_bulk_allocated) |
| 1044 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 1045 | |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1046 | void ThrowOutOfMemoryError(Thread* self, size_t byte_count, AllocatorType allocator_type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1047 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 1048 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1049 | // Are we out of memory, and thus should force a GC or fail? |
| 1050 | // For concurrent collectors, out of memory is defined by growth_limit_. |
| 1051 | // For nonconcurrent collectors it is defined by target_footprint_ unless grow is |
| 1052 | // set. If grow is set, the limit is growth_limit_ and we adjust target_footprint_ |
| 1053 | // to accomodate the allocation. |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 1054 | ALWAYS_INLINE bool IsOutOfMemoryOnAllocation(AllocatorType allocator_type, |
| 1055 | size_t alloc_size, |
| 1056 | bool grow); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 1057 | |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 1058 | // Run the finalizers. If timeout is non zero, then we use the VMRuntime version. |
| 1059 | void RunFinalization(JNIEnv* env, uint64_t timeout); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1060 | |
| 1061 | // Blocks the caller until the garbage collector becomes idle and returns the type of GC we |
| 1062 | // waited for. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 1063 | collector::GcType WaitForGcToCompleteLocked(GcCause cause, Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1064 | REQUIRES(gc_complete_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1065 | |
Mathieu Chartier | 7bf52d2 | 2014-03-13 14:46:09 -0700 | [diff] [blame] | 1066 | void RequestCollectorTransition(CollectorType desired_collector_type, uint64_t delta_time) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1067 | REQUIRES(!*pending_task_lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1068 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1069 | void RequestConcurrentGCAndSaveObject(Thread* self, bool force_full, ObjPtr<mirror::Object>* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1070 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1071 | REQUIRES(!*pending_task_lock_); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1072 | bool IsGCRequestPending() const; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1073 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1074 | // Sometimes CollectGarbageInternal decides to run a different Gc than you requested. Returns |
| 1075 | // which type of Gc was actually ran. |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1076 | collector::GcType CollectGarbageInternal(collector::GcType gc_plan, |
| 1077 | GcCause gc_cause, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1078 | bool clear_soft_references) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1079 | REQUIRES(!*gc_complete_lock_, !Locks::heap_bitmap_lock_, !Locks::thread_suspend_count_lock_, |
| 1080 | !*pending_task_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1081 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1082 | void PreGcVerification(collector::GarbageCollector* gc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1083 | REQUIRES(!Locks::mutator_lock_, !*gc_complete_lock_); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1084 | void PreGcVerificationPaused(collector::GarbageCollector* gc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1085 | REQUIRES(Locks::mutator_lock_, !*gc_complete_lock_); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1086 | void PrePauseRosAllocVerification(collector::GarbageCollector* gc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1087 | REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1088 | void PreSweepingGcVerification(collector::GarbageCollector* gc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1089 | REQUIRES(Locks::mutator_lock_, !Locks::heap_bitmap_lock_, !*gc_complete_lock_); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1090 | void PostGcVerification(collector::GarbageCollector* gc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1091 | REQUIRES(!Locks::mutator_lock_, !*gc_complete_lock_); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1092 | void PostGcVerificationPaused(collector::GarbageCollector* gc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1093 | REQUIRES(Locks::mutator_lock_, !*gc_complete_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1094 | |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 1095 | // Find a collector based on GC type. |
| 1096 | collector::GarbageCollector* FindCollectorByGcType(collector::GcType gc_type); |
| 1097 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1098 | // Create the main free list malloc space, either a RosAlloc space or DlMalloc space. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1099 | void CreateMainMallocSpace(MemMap&& mem_map, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1100 | size_t initial_size, |
| 1101 | size_t growth_limit, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1102 | size_t capacity); |
| 1103 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1104 | // Create a malloc space based on a mem map. Does not set the space as default. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1105 | space::MallocSpace* CreateMallocSpaceFromMemMap(MemMap&& mem_map, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1106 | size_t initial_size, |
| 1107 | size_t growth_limit, |
| 1108 | size_t capacity, |
| 1109 | const char* name, |
| 1110 | bool can_move_objects); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1111 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 1112 | // Given the current contents of the alloc space, increase the allowed heap footprint to match |
| 1113 | // the target utilization ratio. This should only be called immediately after a full garbage |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 1114 | // collection. bytes_allocated_before_gc is used to measure bytes / second for the period which |
| 1115 | // the GC was run. |
| 1116 | void GrowForUtilization(collector::GarbageCollector* collector_ran, |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1117 | size_t bytes_allocated_before_gc = 0); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1118 | |
Mathieu Chartier | 637e348 | 2012-08-17 10:41:32 -0700 | [diff] [blame] | 1119 | size_t GetPercentFree(); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1120 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1121 | // Swap the allocation stack with the live stack. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1122 | void SwapStacks() REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 1123 | |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 1124 | // Clear cards and update the mod union table. When process_alloc_space_cards is true, |
| 1125 | // if clear_alloc_space_cards is true, then we clear cards instead of ageing them. We do |
| 1126 | // not process the alloc space if process_alloc_space_cards is false. |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1127 | void ProcessCards(TimingLogger* timings, |
| 1128 | bool use_rem_sets, |
| 1129 | bool process_alloc_space_cards, |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 1130 | bool clear_alloc_space_cards) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1131 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1132 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 1133 | // Push an object onto the allocation stack. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1134 | void PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1135 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1136 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1137 | void PushOnAllocationStackWithInternalGC(Thread* self, ObjPtr<mirror::Object>* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1138 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1139 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1140 | void PushOnThreadLocalAllocationStackWithInternalGC(Thread* thread, ObjPtr<mirror::Object>* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1141 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 1142 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 1143 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1144 | void ClearConcurrentGCRequest(); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1145 | void ClearPendingTrim(Thread* self) REQUIRES(!*pending_task_lock_); |
| 1146 | void ClearPendingCollectorTransition(Thread* self) REQUIRES(!*pending_task_lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1147 | |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 1148 | // What kind of concurrency behavior is the runtime after? Currently true for concurrent mark |
| 1149 | // sweep GC, false for other GC types. |
| 1150 | bool IsGcConcurrent() const ALWAYS_INLINE { |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1151 | return collector_type_ == kCollectorTypeCC || |
| 1152 | collector_type_ == kCollectorTypeCMS || |
Hiroshi Yamauchi | 60985b7 | 2016-08-24 13:53:12 -0700 | [diff] [blame] | 1153 | collector_type_ == kCollectorTypeCCBackground; |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1156 | // Trim the managed and native spaces by releasing unused memory back to the OS. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1157 | void TrimSpaces(Thread* self) REQUIRES(!*gc_complete_lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1158 | |
| 1159 | // Trim 0 pages at the end of reference tables. |
| 1160 | void TrimIndirectReferenceTables(Thread* self); |
| 1161 | |
Andreas Gampe | 351c447 | 2017-07-12 19:32:55 -0700 | [diff] [blame] | 1162 | template <typename Visitor> |
| 1163 | ALWAYS_INLINE void VisitObjectsInternal(Visitor&& visitor) |
| 1164 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1165 | REQUIRES(!Locks::heap_bitmap_lock_, !*gc_complete_lock_); |
| 1166 | template <typename Visitor> |
| 1167 | ALWAYS_INLINE void VisitObjectsInternalRegionSpace(Visitor&& visitor) |
| 1168 | REQUIRES(Locks::mutator_lock_, !Locks::heap_bitmap_lock_, !*gc_complete_lock_); |
| 1169 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1170 | void UpdateGcCountRateHistograms() REQUIRES(gc_complete_lock_); |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 1171 | |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1172 | // GC stress mode attempts to do one GC per unique backtrace. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1173 | void CheckGcStressMode(Thread* self, ObjPtr<mirror::Object>* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1174 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1175 | REQUIRES(!*gc_complete_lock_, !*pending_task_lock_, !*backtrace_lock_); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1176 | |
Richard Uhler | caaa2b0 | 2017-02-01 09:54:17 +0000 | [diff] [blame] | 1177 | collector::GcType NonStickyGcType() const { |
| 1178 | return HasZygoteSpace() ? collector::kGcTypePartial : collector::kGcTypeFull; |
| 1179 | } |
| 1180 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1181 | // Return the amount of space we allow for native memory when deciding whether to |
| 1182 | // collect. We collect when a weighted sum of Java memory plus native memory exceeds |
| 1183 | // the similarly weighted sum of the Java heap size target and this value. |
Richard Uhler | caaa2b0 | 2017-02-01 09:54:17 +0000 | [diff] [blame] | 1184 | ALWAYS_INLINE size_t NativeAllocationGcWatermark() const { |
Hans Boehm | 1575267 | 2018-12-18 17:01:00 -0800 | [diff] [blame] | 1185 | // We keep the traditional limit of max_free_ in place for small heaps, |
| 1186 | // but allow it to be adjusted upward for large heaps to limit GC overhead. |
| 1187 | return target_footprint_.load(std::memory_order_relaxed) / 8 + max_free_; |
Richard Uhler | caaa2b0 | 2017-02-01 09:54:17 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Orion Hodson | 82cf9a2 | 2018-03-27 16:36:32 +0100 | [diff] [blame] | 1190 | ALWAYS_INLINE void IncrementNumberOfBytesFreedRevoke(size_t freed_bytes_revoke); |
| 1191 | |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 1192 | // Remove a vlog code from heap-inl.h which is transitively included in half the world. |
| 1193 | static void VlogHeapGrowth(size_t max_allowed_footprint, size_t new_footprint, size_t alloc_size); |
| 1194 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1195 | // Return our best approximation of the number of bytes of native memory that |
| 1196 | // are currently in use, and could possibly be reclaimed as an indirect result |
| 1197 | // of a garbage collection. |
| 1198 | size_t GetNativeBytes(); |
| 1199 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1200 | // All-known continuous spaces, where objects lie within fixed bounds. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 1201 | std::vector<space::ContinuousSpace*> continuous_spaces_ GUARDED_BY(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1202 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1203 | // All-known discontinuous spaces, where objects may be placed throughout virtual memory. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 1204 | std::vector<space::DiscontinuousSpace*> discontinuous_spaces_ GUARDED_BY(Locks::mutator_lock_); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1205 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1206 | // All-known alloc spaces, where objects may be or have been allocated. |
| 1207 | std::vector<space::AllocSpace*> alloc_spaces_; |
| 1208 | |
| 1209 | // A space where non-movable objects are allocated, when compaction is enabled it contains |
| 1210 | // Classes, ArtMethods, ArtFields, and non moving objects. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1211 | space::MallocSpace* non_moving_space_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1212 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1213 | // Space which we use for the kAllocatorTypeROSAlloc. |
| 1214 | space::RosAllocSpace* rosalloc_space_; |
| 1215 | |
| 1216 | // Space which we use for the kAllocatorTypeDlMalloc. |
| 1217 | space::DlMallocSpace* dlmalloc_space_; |
| 1218 | |
Mathieu Chartier | fc5b528 | 2014-01-09 16:15:36 -0800 | [diff] [blame] | 1219 | // The main space is the space which the GC copies to and from on process state updates. This |
| 1220 | // space is typically either the dlmalloc_space_ or the rosalloc_space_. |
| 1221 | space::MallocSpace* main_space_; |
| 1222 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1223 | // The large object space we are currently allocating into. |
| 1224 | space::LargeObjectSpace* large_object_space_; |
| 1225 | |
| 1226 | // The card table, dirtied by the write barrier. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1227 | std::unique_ptr<accounting::CardTable> card_table_; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 1228 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1229 | std::unique_ptr<accounting::ReadBarrierTable> rb_table_; |
| 1230 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 1231 | // A mod-union table remembers all of the references from the it's space to other spaces. |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 1232 | AllocationTrackingSafeMap<space::Space*, accounting::ModUnionTable*, kAllocatorTagHeap> |
| 1233 | mod_union_tables_; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1234 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 1235 | // A remembered set remembers all of the references from the it's space to the target space. |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 1236 | AllocationTrackingSafeMap<space::Space*, accounting::RememberedSet*, kAllocatorTagHeap> |
| 1237 | remembered_sets_; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 1238 | |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 1239 | // The current collector type. |
| 1240 | CollectorType collector_type_; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1241 | // Which collector we use when the app is in the foreground. |
| 1242 | CollectorType foreground_collector_type_; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1243 | // Which collector we will use when the app is notified of a transition to background. |
| 1244 | CollectorType background_collector_type_; |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 1245 | // Desired collector type, heap trimming daemon transitions the heap if it is != collector_type_. |
| 1246 | CollectorType desired_collector_type_; |
| 1247 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1248 | // Lock which guards pending tasks. |
| 1249 | Mutex* pending_task_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1250 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1251 | // How many GC threads we may use for paused parts of garbage collection. |
| 1252 | const size_t parallel_gc_threads_; |
| 1253 | |
| 1254 | // How many GC threads we may use for unpaused parts of garbage collection. |
| 1255 | const size_t conc_gc_threads_; |
Mathieu Chartier | 63a5434 | 2013-07-23 13:17:59 -0700 | [diff] [blame] | 1256 | |
Mathieu Chartier | e0a53e9 | 2013-08-05 10:17:40 -0700 | [diff] [blame] | 1257 | // Boolean for if we are in low memory mode. |
| 1258 | const bool low_memory_mode_; |
| 1259 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1260 | // If we get a pause longer than long pause log threshold, then we print out the GC after it |
| 1261 | // finishes. |
| 1262 | const size_t long_pause_log_threshold_; |
| 1263 | |
| 1264 | // If we get a GC longer than long GC log threshold, then we print out the GC after it finishes. |
| 1265 | const size_t long_gc_log_threshold_; |
| 1266 | |
Albert Mingkun Yang | 2d7329b | 2018-11-30 19:58:18 +0000 | [diff] [blame] | 1267 | // Starting time of the new process; meant to be used for measuring total process CPU time. |
| 1268 | uint64_t process_cpu_start_time_ns_; |
| 1269 | |
Albert Mingkun Yang | 6e0d325 | 2018-12-10 15:22:45 +0000 | [diff] [blame] | 1270 | // Last time (before and after) GC started; meant to be used to measure the |
| 1271 | // duration between two GCs. |
| 1272 | uint64_t pre_gc_last_process_cpu_time_ns_; |
| 1273 | uint64_t post_gc_last_process_cpu_time_ns_; |
Albert Mingkun Yang | 2d7329b | 2018-11-30 19:58:18 +0000 | [diff] [blame] | 1274 | |
Albert Mingkun Yang | 6e0d325 | 2018-12-10 15:22:45 +0000 | [diff] [blame] | 1275 | // allocated_bytes * (current_process_cpu_time - [pre|post]_gc_last_process_cpu_time) |
| 1276 | double pre_gc_weighted_allocated_bytes_; |
| 1277 | double post_gc_weighted_allocated_bytes_; |
Albert Mingkun Yang | 2d7329b | 2018-11-30 19:58:18 +0000 | [diff] [blame] | 1278 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1279 | // If we ignore the target footprint it lets the heap grow until it hits the heap capacity, this |
| 1280 | // is useful for benchmarking since it reduces time spent in GC to a low %. |
| 1281 | const bool ignore_target_footprint_; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1282 | |
Mathieu Chartier | 8e4a96d | 2014-05-21 10:44:32 -0700 | [diff] [blame] | 1283 | // Lock which guards zygote space creation. |
| 1284 | Mutex zygote_creation_lock_; |
| 1285 | |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 1286 | // Non-null iff we have a zygote space. Doesn't contain the large objects allocated before |
| 1287 | // zygote space creation. |
| 1288 | space::ZygoteSpace* zygote_space_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1289 | |
Mathieu Chartier | bd0a653 | 2014-02-27 11:14:21 -0800 | [diff] [blame] | 1290 | // Minimum allocation size of large object. |
| 1291 | size_t large_object_threshold_; |
| 1292 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1293 | // Guards access to the state of GC, associated conditional variable is used to signal when a GC |
| 1294 | // completes. |
| 1295 | Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1296 | std::unique_ptr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1297 | |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 1298 | // Used to synchronize between JNI critical calls and the thread flip of the CC collector. |
| 1299 | Mutex* thread_flip_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 1300 | std::unique_ptr<ConditionVariable> thread_flip_cond_ GUARDED_BY(thread_flip_lock_); |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 1301 | // This counter keeps track of how many threads are currently in a JNI critical section. This is |
| 1302 | // incremented once per thread even with nested enters. |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 1303 | size_t disable_thread_flip_count_ GUARDED_BY(thread_flip_lock_); |
| 1304 | bool thread_flip_running_ GUARDED_BY(thread_flip_lock_); |
| 1305 | |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 1306 | // Reference processor; |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 1307 | std::unique_ptr<ReferenceProcessor> reference_processor_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1308 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1309 | // Task processor, proxies heap trim requests to the daemon threads. |
| 1310 | std::unique_ptr<TaskProcessor> task_processor_; |
| 1311 | |
Mathieu Chartier | 40112dd | 2017-06-26 17:49:09 -0700 | [diff] [blame] | 1312 | // Collector type of the running GC. |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1313 | volatile CollectorType collector_type_running_ GUARDED_BY(gc_complete_lock_); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1314 | |
Mathieu Chartier | 40112dd | 2017-06-26 17:49:09 -0700 | [diff] [blame] | 1315 | // Cause of the last running GC. |
| 1316 | volatile GcCause last_gc_cause_ GUARDED_BY(gc_complete_lock_); |
| 1317 | |
Mathieu Chartier | 183009a | 2017-02-16 21:19:28 -0800 | [diff] [blame] | 1318 | // The thread currently running the GC. |
| 1319 | volatile Thread* thread_running_gc_ GUARDED_BY(gc_complete_lock_); |
| 1320 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1321 | // Last Gc type we ran. Used by WaitForConcurrentGc to know which Gc was waited on. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1322 | volatile collector::GcType last_gc_type_ GUARDED_BY(gc_complete_lock_); |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 1323 | collector::GcType next_gc_type_; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1324 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1325 | // Maximum size that the heap can reach. |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 1326 | size_t capacity_; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1327 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1328 | // The size the heap is limited to. This is initially smaller than capacity, but for largeHeap |
| 1329 | // programs it is "cleared" making it the same as capacity. |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1330 | // Only weakly enforced for simultaneous allocations. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1331 | size_t growth_limit_; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1332 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1333 | // Target size (as in maximum allocatable bytes) for the heap. Weakly enforced as a limit for |
| 1334 | // non-concurrent GC. Used as a guideline for computing concurrent_start_bytes_ in the |
| 1335 | // concurrent GC case. |
| 1336 | Atomic<size_t> target_footprint_; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1337 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1338 | // When num_bytes_allocated_ exceeds this amount then a concurrent GC should be requested so that |
| 1339 | // it completes ahead of an allocation failing. |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1340 | // A multiple of this is also used to determine when to trigger a GC in response to native |
| 1341 | // allocation. |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 1342 | size_t concurrent_start_bytes_; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1343 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1344 | // Since the heap was created, how many bytes have been freed. |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1345 | uint64_t total_bytes_freed_ever_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1346 | |
| 1347 | // Since the heap was created, how many objects have been freed. |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1348 | uint64_t total_objects_freed_ever_; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1349 | |
Hans Boehm | fb8b4e2 | 2018-09-05 16:45:42 -0700 | [diff] [blame] | 1350 | // Number of bytes currently allocated and not yet reclaimed. Includes active |
| 1351 | // TLABS in their entirety, even if they have not yet been parceled out. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1352 | Atomic<size_t> num_bytes_allocated_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1353 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1354 | // Number of registered native bytes allocated. Adjusted after each RegisterNativeAllocation and |
| 1355 | // RegisterNativeFree. Used to help determine when to trigger GC for native allocations. Should |
| 1356 | // not include bytes allocated through the system malloc, since those are implicitly included. |
| 1357 | Atomic<size_t> native_bytes_registered_; |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1358 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1359 | // Approximately the smallest value of GetNativeBytes() we've seen since the last GC. |
Richard Uhler | caaa2b0 | 2017-02-01 09:54:17 +0000 | [diff] [blame] | 1360 | Atomic<size_t> old_native_bytes_allocated_; |
| 1361 | |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1362 | // Total number of native objects of which we were notified since the beginning of time, mod 2^32. |
| 1363 | // Allows us to check for GC only roughly every kNotifyNativeInterval allocations. |
| 1364 | Atomic<uint32_t> native_objects_notified_; |
| 1365 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1366 | // Number of bytes freed by thread local buffer revokes. This will |
| 1367 | // cancel out the ahead-of-time bulk counting of bytes allocated in |
| 1368 | // rosalloc thread-local buffers. It is temporarily accumulated |
| 1369 | // here to be subtracted from num_bytes_allocated_ later at the next |
| 1370 | // GC. |
| 1371 | Atomic<size_t> num_bytes_freed_revoke_; |
| 1372 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 1373 | // Info related to the current or previous GC iteration. |
| 1374 | collector::Iteration current_gc_iteration_; |
| 1375 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1376 | // Heap verification flags. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1377 | const bool verify_missing_card_marks_; |
| 1378 | const bool verify_system_weaks_; |
| 1379 | const bool verify_pre_gc_heap_; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1380 | const bool verify_pre_sweeping_heap_; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1381 | const bool verify_post_gc_heap_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1382 | const bool verify_mod_union_table_; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1383 | bool verify_pre_gc_rosalloc_; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1384 | bool verify_pre_sweeping_rosalloc_; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1385 | bool verify_post_gc_rosalloc_; |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1386 | const bool gc_stress_mode_; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1387 | |
| 1388 | // RAII that temporarily disables the rosalloc verification during |
| 1389 | // the zygote fork. |
| 1390 | class ScopedDisableRosAllocVerification { |
| 1391 | private: |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1392 | Heap* const heap_; |
| 1393 | const bool orig_verify_pre_gc_; |
| 1394 | const bool orig_verify_pre_sweeping_; |
| 1395 | const bool orig_verify_post_gc_; |
| 1396 | |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1397 | public: |
| 1398 | explicit ScopedDisableRosAllocVerification(Heap* heap) |
| 1399 | : heap_(heap), |
| 1400 | orig_verify_pre_gc_(heap_->verify_pre_gc_rosalloc_), |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1401 | orig_verify_pre_sweeping_(heap_->verify_pre_sweeping_rosalloc_), |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1402 | orig_verify_post_gc_(heap_->verify_post_gc_rosalloc_) { |
| 1403 | heap_->verify_pre_gc_rosalloc_ = false; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1404 | heap_->verify_pre_sweeping_rosalloc_ = false; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1405 | heap_->verify_post_gc_rosalloc_ = false; |
| 1406 | } |
| 1407 | ~ScopedDisableRosAllocVerification() { |
| 1408 | heap_->verify_pre_gc_rosalloc_ = orig_verify_pre_gc_; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1409 | heap_->verify_pre_sweeping_rosalloc_ = orig_verify_pre_sweeping_; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1410 | heap_->verify_post_gc_rosalloc_ = orig_verify_post_gc_; |
| 1411 | } |
| 1412 | }; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1413 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1414 | // Parallel GC data structures. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1415 | std::unique_ptr<ThreadPool> thread_pool_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1416 | |
Roland Levillain | 8f7ea9a | 2018-01-26 17:27:59 +0000 | [diff] [blame] | 1417 | // A bitmap that is set corresponding to the known live objects since the last GC cycle. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1418 | std::unique_ptr<accounting::HeapBitmap> live_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_); |
Roland Levillain | 8f7ea9a | 2018-01-26 17:27:59 +0000 | [diff] [blame] | 1419 | // A bitmap that is set corresponding to the marked objects in the current GC cycle. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1420 | std::unique_ptr<accounting::HeapBitmap> mark_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1421 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1422 | // Mark stack that we reuse to avoid re-allocating the mark stack. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1423 | std::unique_ptr<accounting::ObjectStack> mark_stack_; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1424 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1425 | // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us |
| 1426 | // to use the live bitmap as the old mark bitmap. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1427 | const size_t max_allocation_stack_size_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1428 | std::unique_ptr<accounting::ObjectStack> allocation_stack_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1429 | |
| 1430 | // Second allocation stack so that we can process allocation with the heap unlocked. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1431 | std::unique_ptr<accounting::ObjectStack> live_stack_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1432 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1433 | // Allocator type. |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 1434 | AllocatorType current_allocator_; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1435 | const AllocatorType current_non_moving_allocator_; |
| 1436 | |
Roland Levillain | ef01222 | 2017-06-21 16:28:06 +0100 | [diff] [blame] | 1437 | // Which GCs we run in order when an allocation fails. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1438 | std::vector<collector::GcType> gc_plan_; |
| 1439 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1440 | // Bump pointer spaces. |
| 1441 | space::BumpPointerSpace* bump_pointer_space_; |
| 1442 | // Temp space is the space which the semispace collector copies to. |
| 1443 | space::BumpPointerSpace* temp_space_; |
| 1444 | |
Roland Levillain | 8f7ea9a | 2018-01-26 17:27:59 +0000 | [diff] [blame] | 1445 | // Region space, used by the concurrent collector. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1446 | space::RegionSpace* region_space_; |
| 1447 | |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 1448 | // Minimum free guarantees that you always have at least min_free_ free bytes after growing for |
| 1449 | // utilization, regardless of target utilization ratio. |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1450 | const size_t min_free_; |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 1451 | |
| 1452 | // The ideal maximum free size, when we grow the heap for utilization. |
Hans Boehm | c220f98 | 2018-10-12 16:15:45 -0700 | [diff] [blame] | 1453 | const size_t max_free_; |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 1454 | |
Roland Levillain | 99bd16b | 2018-02-21 14:18:15 +0000 | [diff] [blame] | 1455 | // Target ideal heap utilization ratio. |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 1456 | double target_utilization_; |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 1457 | |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 1458 | // How much more we grow the heap when we are a foreground app instead of background. |
| 1459 | double foreground_heap_growth_multiplier_; |
| 1460 | |
Hans Boehm | bb2467b | 2019-03-29 22:55:06 -0700 | [diff] [blame] | 1461 | // The amount of native memory allocation since the last GC required to cause us to wait for a |
| 1462 | // collection as a result of native allocation. Very large values can cause the device to run |
| 1463 | // out of memory, due to lack of finalization to reclaim native memory. Making it too small can |
| 1464 | // cause jank in apps like launcher that intentionally allocate large amounts of memory in rapid |
| 1465 | // succession. (b/122099093) 1/4 to 1/3 of physical memory seems to be a good number. |
| 1466 | const size_t stop_for_native_allocs_; |
| 1467 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1468 | // Total time which mutators are paused or waiting for GC to complete. |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1469 | uint64_t total_wait_time_; |
| 1470 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 1471 | // The current state of heap verification, may be enabled or disabled. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1472 | VerifyObjectMode verify_object_mode_; |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 1473 | |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 1474 | // Compacting GC disable count, prevents compacting GC from running iff > 0. |
| 1475 | size_t disable_moving_gc_count_ GUARDED_BY(gc_complete_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1476 | |
| 1477 | std::vector<collector::GarbageCollector*> garbage_collectors_; |
| 1478 | collector::SemiSpace* semi_space_collector_; |
Mathieu Chartier | 8d1a996 | 2016-08-17 16:39:45 -0700 | [diff] [blame] | 1479 | collector::ConcurrentCopying* active_concurrent_copying_collector_; |
| 1480 | collector::ConcurrentCopying* young_concurrent_copying_collector_; |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 1481 | collector::ConcurrentCopying* concurrent_copying_collector_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1482 | |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1483 | const bool is_running_on_memory_tool_; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 1484 | const bool use_tlab_; |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 1485 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1486 | // Pointer to the space which becomes the new main space when we do homogeneous space compaction. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1487 | // Use unique_ptr since the space is only added during the homogeneous compaction phase. |
| 1488 | std::unique_ptr<space::MallocSpace> main_space_backup_; |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1489 | |
| 1490 | // Minimal interval allowed between two homogeneous space compactions caused by OOM. |
| 1491 | uint64_t min_interval_homogeneous_space_compaction_by_oom_; |
| 1492 | |
| 1493 | // Times of the last homogeneous space compaction caused by OOM. |
| 1494 | uint64_t last_time_homogeneous_space_compaction_by_oom_; |
| 1495 | |
| 1496 | // Saved OOMs by homogeneous space compaction. |
| 1497 | Atomic<size_t> count_delayed_oom_; |
| 1498 | |
| 1499 | // Count for requested homogeneous space compaction. |
| 1500 | Atomic<size_t> count_requested_homogeneous_space_compaction_; |
| 1501 | |
| 1502 | // Count for ignored homogeneous space compaction. |
| 1503 | Atomic<size_t> count_ignored_homogeneous_space_compaction_; |
| 1504 | |
| 1505 | // Count for performed homogeneous space compaction. |
| 1506 | Atomic<size_t> count_performed_homogeneous_space_compaction_; |
| 1507 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1508 | // Whether or not a concurrent GC is pending. |
| 1509 | Atomic<bool> concurrent_gc_pending_; |
| 1510 | |
| 1511 | // Active tasks which we can modify (change target time, desired collector type, etc..). |
| 1512 | CollectorTransitionTask* pending_collector_transition_ GUARDED_BY(pending_task_lock_); |
| 1513 | HeapTrimTask* pending_heap_trim_ GUARDED_BY(pending_task_lock_); |
| 1514 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1515 | // Whether or not we use homogeneous space compaction to avoid OOM errors. |
| 1516 | bool use_homogeneous_space_compaction_for_oom_; |
| 1517 | |
Albert Mingkun Yang | 0b4d146 | 2018-11-29 13:25:35 +0000 | [diff] [blame] | 1518 | // If true, enable generational collection when using the Concurrent Copying |
| 1519 | // (CC) collector, i.e. use sticky-bit CC for minor collections and (full) CC |
| 1520 | // for major collections. Set in Heap constructor. |
| 1521 | const bool use_generational_cc_; |
| 1522 | |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 1523 | // True if the currently running collection has made some thread wait. |
| 1524 | bool running_collection_is_blocking_ GUARDED_BY(gc_complete_lock_); |
| 1525 | // The number of blocking GC runs. |
| 1526 | uint64_t blocking_gc_count_; |
| 1527 | // The total duration of blocking GC runs. |
| 1528 | uint64_t blocking_gc_time_; |
| 1529 | // The duration of the window for the GC count rate histograms. |
| 1530 | static constexpr uint64_t kGcCountRateHistogramWindowDuration = MsToNs(10 * 1000); // 10s. |
Vincent Palomares | cc17d07 | 2019-01-28 11:14:01 -0800 | [diff] [blame] | 1531 | // Maximum number of missed histogram windows for which statistics will be collected. |
| 1532 | static constexpr uint64_t kGcCountRateHistogramMaxNumMissedWindows = 100; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 1533 | // The last time when the GC count rate histograms were updated. |
| 1534 | // This is rounded by kGcCountRateHistogramWindowDuration (a multiple of 10s). |
| 1535 | uint64_t last_update_time_gc_count_rate_histograms_; |
| 1536 | // The running count of GC runs in the last window. |
| 1537 | uint64_t gc_count_last_window_; |
| 1538 | // The running count of blocking GC runs in the last window. |
| 1539 | uint64_t blocking_gc_count_last_window_; |
| 1540 | // The maximum number of buckets in the GC count rate histograms. |
| 1541 | static constexpr size_t kGcCountRateMaxBucketCount = 200; |
| 1542 | // The histogram of the number of GC invocations per window duration. |
| 1543 | Histogram<uint64_t> gc_count_rate_histogram_ GUARDED_BY(gc_complete_lock_); |
| 1544 | // The histogram of the number of blocking GC invocations per window duration. |
| 1545 | Histogram<uint64_t> blocking_gc_count_rate_histogram_ GUARDED_BY(gc_complete_lock_); |
| 1546 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1547 | // Allocation tracking support |
| 1548 | Atomic<bool> alloc_tracking_enabled_; |
Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 1549 | std::unique_ptr<AllocRecordObjectMap> allocation_records_; |
Mathieu Chartier | 0a20607 | 2019-03-28 12:29:22 -0700 | [diff] [blame] | 1550 | size_t alloc_record_depth_; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1551 | |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1552 | // GC stress related data structures. |
| 1553 | Mutex* backtrace_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 1554 | // Debugging variables, seen backtraces vs unique backtraces. |
| 1555 | Atomic<uint64_t> seen_backtrace_count_; |
| 1556 | Atomic<uint64_t> unique_backtrace_count_; |
| 1557 | // Stack trace hashes that we already saw, |
| 1558 | std::unordered_set<uint64_t> seen_backtraces_ GUARDED_BY(backtrace_lock_); |
| 1559 | |
Mathieu Chartier | 5116837 | 2015-08-12 16:40:32 -0700 | [diff] [blame] | 1560 | // We disable GC when we are shutting down the runtime in case there are daemon threads still |
| 1561 | // allocating. |
| 1562 | bool gc_disabled_for_shutdown_ GUARDED_BY(gc_complete_lock_); |
| 1563 | |
Albert Mingkun Yang | de94ea7 | 2018-11-16 10:15:49 +0000 | [diff] [blame] | 1564 | // Turned on by -XX:DumpRegionInfoBeforeGC and -XX:DumpRegionInfoAfterGC to |
| 1565 | // emit region info before and after each GC cycle. |
| 1566 | bool dump_region_info_before_gc_; |
| 1567 | bool dump_region_info_after_gc_; |
| 1568 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1569 | // Boot image spaces. |
| 1570 | std::vector<space::ImageSpace*> boot_image_spaces_; |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 1571 | |
Vladimir Marko | 7cde458 | 2019-07-05 13:26:11 +0100 | [diff] [blame] | 1572 | // Boot image address range. Includes images and oat files. |
| 1573 | uint32_t boot_images_start_address_; |
| 1574 | uint32_t boot_images_size_; |
| 1575 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1576 | // An installed allocation listener. |
| 1577 | Atomic<AllocationListener*> alloc_listener_; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1578 | // An installed GC Pause listener. |
| 1579 | Atomic<GcPauseListener*> gc_pause_listener_; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1580 | |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 1581 | std::unique_ptr<Verification> verification_; |
| 1582 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1583 | friend class CollectorTransitionTask; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 1584 | friend class collector::GarbageCollector; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1585 | friend class collector::ConcurrentCopying; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1586 | friend class collector::MarkSweep; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1587 | friend class collector::SemiSpace; |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 1588 | friend class GCCriticalSection; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 1589 | friend class ReferenceQueue; |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 1590 | friend class ScopedGCCriticalSection; |
Mathieu Chartier | ad390fa | 2019-10-16 20:03:00 -0700 | [diff] [blame] | 1591 | friend class ScopedInterruptibleGCCriticalSection; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1592 | friend class VerifyReferenceCardVisitor; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1593 | friend class VerifyReferenceVisitor; |
| 1594 | friend class VerifyObjectVisitor; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1595 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1596 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 1597 | }; |
| 1598 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1599 | } // namespace gc |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1600 | } // namespace art |
| 1601 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 1602 | #endif // ART_RUNTIME_GC_HEAP_H_ |