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