• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "heap.h"
18 
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include <limits>
23 #include <memory>
24 #include <random>
25 #include <sstream>
26 #include <vector>
27 
28 #include "allocation_listener.h"
29 #include "android-base/stringprintf.h"
30 #include "android-base/thread_annotations.h"
31 #include "art_field-inl.h"
32 #include "backtrace_helper.h"
33 #include "base/allocator.h"
34 #include "base/arena_allocator.h"
35 #include "base/dumpable.h"
36 #include "base/file_utils.h"
37 #include "base/histogram-inl.h"
38 #include "base/logging.h"  // For VLOG.
39 #include "base/memory_tool.h"
40 #include "base/mutex.h"
41 #include "base/os.h"
42 #include "base/stl_util.h"
43 #include "base/systrace.h"
44 #include "base/time_utils.h"
45 #include "base/utils.h"
46 #include "class_root-inl.h"
47 #include "common_throws.h"
48 #include "debugger.h"
49 #include "dex/dex_file-inl.h"
50 #include "entrypoints/quick/quick_alloc_entrypoints.h"
51 #include "gc/accounting/card_table-inl.h"
52 #include "gc/accounting/heap_bitmap-inl.h"
53 #include "gc/accounting/mod_union_table-inl.h"
54 #include "gc/accounting/read_barrier_table.h"
55 #include "gc/accounting/remembered_set.h"
56 #include "gc/accounting/space_bitmap-inl.h"
57 #include "gc/collector/concurrent_copying.h"
58 #include "gc/collector/mark_compact.h"
59 #include "gc/collector/mark_sweep.h"
60 #include "gc/collector/partial_mark_sweep.h"
61 #include "gc/collector/semi_space.h"
62 #include "gc/collector/sticky_mark_sweep.h"
63 #include "gc/racing_check.h"
64 #include "gc/reference_processor.h"
65 #include "gc/scoped_gc_critical_section.h"
66 #include "gc/space/bump_pointer_space.h"
67 #include "gc/space/dlmalloc_space-inl.h"
68 #include "gc/space/image_space.h"
69 #include "gc/space/large_object_space.h"
70 #include "gc/space/region_space.h"
71 #include "gc/space/rosalloc_space-inl.h"
72 #include "gc/space/space-inl.h"
73 #include "gc/space/zygote_space.h"
74 #include "gc/task_processor.h"
75 #include "gc/verification.h"
76 #include "gc_pause_listener.h"
77 #include "gc_root.h"
78 #include "handle_scope-inl.h"
79 #include "heap-inl.h"
80 #include "heap-visit-objects-inl.h"
81 #include "intern_table.h"
82 #include "jit/jit.h"
83 #include "jit/jit_code_cache.h"
84 #include "jni/java_vm_ext.h"
85 #include "mirror/class-inl.h"
86 #include "mirror/executable-inl.h"
87 #include "mirror/field.h"
88 #include "mirror/method_handle_impl.h"
89 #include "mirror/object-inl.h"
90 #include "mirror/object-refvisitor-inl.h"
91 #include "mirror/object_array-inl.h"
92 #include "mirror/reference-inl.h"
93 #include "mirror/var_handle.h"
94 #include "nativehelper/scoped_local_ref.h"
95 #include "oat/image.h"
96 #include "obj_ptr-inl.h"
97 #ifdef ART_TARGET_ANDROID
98 #include "perfetto/heap_profile.h"
99 #endif
100 #include "reflection.h"
101 #include "runtime.h"
102 #include "javaheapprof/javaheapsampler.h"
103 #include "scoped_thread_state_change-inl.h"
104 #include "thread-inl.h"
105 #include "thread_list.h"
106 #include "verify_object-inl.h"
107 #include "well_known_classes.h"
108 
109 #if defined(__BIONIC__) || defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
110 #include <malloc.h>  // For mallinfo()
111 #endif
112 
113 namespace art HIDDEN {
114 
115 #ifdef ART_TARGET_ANDROID
116 namespace {
117 
118 // Enable the heap sampler Callback function used by Perfetto.
EnableHeapSamplerCallback(void * enable_ptr,const AHeapProfileEnableCallbackInfo * enable_info_ptr)119 void EnableHeapSamplerCallback(void* enable_ptr,
120                                const AHeapProfileEnableCallbackInfo* enable_info_ptr) {
121   HeapSampler* sampler_self = reinterpret_cast<HeapSampler*>(enable_ptr);
122   // Set the ART profiler sampling interval to the value from Perfetto.
123   uint64_t interval = AHeapProfileEnableCallbackInfo_getSamplingInterval(enable_info_ptr);
124   if (interval > 0) {
125     sampler_self->SetSamplingInterval(interval);
126   }
127   // Else default is 4K sampling interval. However, default case shouldn't happen for Perfetto API.
128   // AHeapProfileEnableCallbackInfo_getSamplingInterval should always give the requested
129   // (non-negative) sampling interval. It is a uint64_t and gets checked for != 0
130   // Do not call heap as a temp here, it will build but test run will silently fail.
131   // Heap is not fully constructed yet in some cases.
132   sampler_self->EnableHeapSampler();
133 }
134 
135 // Disable the heap sampler Callback function used by Perfetto.
DisableHeapSamplerCallback(void * disable_ptr,const AHeapProfileDisableCallbackInfo * info_ptr)136 void DisableHeapSamplerCallback(void* disable_ptr,
137                                 [[maybe_unused]] const AHeapProfileDisableCallbackInfo* info_ptr) {
138   HeapSampler* sampler_self = reinterpret_cast<HeapSampler*>(disable_ptr);
139   sampler_self->DisableHeapSampler();
140 }
141 
142 }  // namespace
143 #endif
144 
145 namespace gc {
146 
147 DEFINE_RUNTIME_DEBUG_FLAG(Heap, kStressCollectorTransition);
148 
149 // Minimum amount of remaining bytes before a concurrent GC is triggered.
150 static constexpr size_t kMinConcurrentRemainingBytes = 128 * KB;
151 static constexpr size_t kMaxConcurrentRemainingBytes = 512 * KB;
152 // Sticky GC throughput adjustment. Increasing this causes sticky GC to occur more
153 // relative to partial/full GC. This may be desirable since sticky GCs interfere less
154 // with mutator threads (lower pauses, use less memory bandwidth). The value
155 // (1.0) for non-generational GC case is fixed and shall never change.
GetStickyGcThroughputAdjustment(bool use_generational_gc)156 static double GetStickyGcThroughputAdjustment(bool use_generational_gc) {
157   return use_generational_gc ? 0.5 : 1.0;
158 }
159 // Whether or not we compact the zygote in PreZygoteFork.
160 static constexpr bool kCompactZygote = kMovingCollector;
161 // How many reserve entries are at the end of the allocation stack, these are only needed if the
162 // allocation stack overflows.
163 static constexpr size_t kAllocationStackReserveSize = 1024;
164 // Default mark stack size in bytes.
165 static const size_t kDefaultMarkStackSize = 64 * KB;
166 // Define space name.
167 static const char* kDlMallocSpaceName[2] = {"main dlmalloc space", "main dlmalloc space 1"};
168 static const char* kRosAllocSpaceName[2] = {"main rosalloc space", "main rosalloc space 1"};
169 static const char* kMemMapSpaceName[2] = {"main space", "main space 1"};
170 static const char* kNonMovingSpaceName = "non moving space";
171 static const char* kZygoteSpaceName = "zygote space";
172 static constexpr bool kGCALotMode = false;
173 // GC alot mode uses a small allocation stack to stress test a lot of GC.
174 static constexpr size_t kGcAlotAllocationStackSize = 4 * KB /
175     sizeof(mirror::HeapReference<mirror::Object>);
176 // Verify objet has a small allocation stack size since searching the allocation stack is slow.
177 static constexpr size_t kVerifyObjectAllocationStackSize = 16 * KB /
178     sizeof(mirror::HeapReference<mirror::Object>);
179 static constexpr size_t kDefaultAllocationStackSize = 8 * MB /
180     sizeof(mirror::HeapReference<mirror::Object>);
181 
182 // If we violate BOTH of the following constraints, we throw OOME.
183 // They differ due to concurrent allocation.
184 // After a GC (due to allocation failure) we should retrieve at least this
185 // fraction of the current max heap size.
186 static constexpr double kMinFreedHeapAfterGcForAlloc = 0.05;
187 // After a GC (due to allocation failure), at least this fraction of the
188 // heap should be available.
189 static constexpr double kMinFreeHeapAfterGcForAlloc = 0.01;
190 
191 // For deterministic compilation, we need the heap to be at a well-known address.
192 static constexpr uint32_t kAllocSpaceBeginForDeterministicAoT = 0x40000000;
193 // Dump the rosalloc stats on SIGQUIT.
194 static constexpr bool kDumpRosAllocStatsOnSigQuit = false;
195 
196 static const char* kRegionSpaceName = "main space (region space)";
197 
198 // If true, we log all GCs in the both the foreground and background. Used for debugging.
199 static constexpr bool kLogAllGCs = false;
200 
201 // Use Max heap for 2 seconds, this is smaller than the usual 5s window since we don't want to leave
202 // allocate with relaxed ergonomics for that long.
203 static constexpr size_t kPostForkMaxHeapDurationMS = 2000;
204 
205 #if defined(__LP64__) || !defined(ADDRESS_SANITIZER)
206 // 32 MB (0x2000000) is picked to ensure it is aligned to the largest supported PMD
207 // size, which is 32mb with a 16k page size on AArch64.
__anonab01f3890202() 208 uint8_t* const Heap::kPreferredAllocSpaceBegin = reinterpret_cast<uint8_t*>(([]() constexpr {
209   constexpr size_t kBegin = 32 * MB;
210   constexpr int kMaxPMDSize = (kMaxPageSize / sizeof(uint64_t)) * kMaxPageSize;
211   static_assert(IsAligned<kMaxPMDSize>(kBegin),
212                 "Moving-space's begin should be aligned to the maximum supported PMD size.");
213   return kBegin;
214 })());
215 #else
216 #ifdef __ANDROID__
217 // For 32-bit Android, use 0x20000000 because asan reserves 0x04000000 - 0x20000000.
218 uint8_t* const Heap::kPreferredAllocSpaceBegin = reinterpret_cast<uint8_t*>(0x20000000);
219 #else
220 // For 32-bit host, use 0x40000000 because asan uses most of the space below this.
221 uint8_t* const Heap::kPreferredAllocSpaceBegin = reinterpret_cast<uint8_t*>(0x40000000);
222 #endif
223 #endif
224 
225 // Log GC on regular (but fairly large) intervals during GC stress mode.
226 // It is expected that the other runtime options will be used to reduce the usual logging.
227 // This allows us to make the logging much less verbose while still reporting some
228 // progress (biased towards expensive GCs), and while still reporting pathological cases.
229 static constexpr int64_t kGcStressModeGcLogSampleFrequencyNs = MsToNs(10000);
230 
CareAboutPauseTimes()231 static inline bool CareAboutPauseTimes() {
232   return Runtime::Current()->InJankPerceptibleProcessState();
233 }
234 
VerifyBootImagesContiguity(const std::vector<gc::space::ImageSpace * > & image_spaces)235 static void VerifyBootImagesContiguity(const std::vector<gc::space::ImageSpace*>& image_spaces) {
236   uint32_t boot_image_size = 0u;
237   for (size_t i = 0u, num_spaces = image_spaces.size(); i != num_spaces; ) {
238     const ImageHeader& image_header = image_spaces[i]->GetImageHeader();
239     uint32_t reservation_size = image_header.GetImageReservationSize();
240     uint32_t image_count = image_header.GetImageSpaceCount();
241 
242     CHECK_NE(image_count, 0u);
243     CHECK_LE(image_count, num_spaces - i);
244     CHECK_NE(reservation_size, 0u);
245     for (size_t j = 1u; j != image_count; ++j) {
246       CHECK_EQ(image_spaces[i + j]->GetImageHeader().GetComponentCount(), 0u);
247       CHECK_EQ(image_spaces[i + j]->GetImageHeader().GetImageReservationSize(), 0u);
248     }
249 
250     // Check the start of the heap.
251     CHECK_EQ(image_spaces[0]->Begin() + boot_image_size, image_spaces[i]->Begin());
252     // Check contiguous layout of images and oat files.
253     const uint8_t* current_heap = image_spaces[i]->Begin();
254     const uint8_t* current_oat = image_spaces[i]->GetImageHeader().GetOatFileBegin();
255     for (size_t j = 0u; j != image_count; ++j) {
256       const ImageHeader& current_header = image_spaces[i + j]->GetImageHeader();
257       CHECK_EQ(current_heap, image_spaces[i + j]->Begin());
258       CHECK_EQ(current_oat, current_header.GetOatFileBegin());
259       current_heap += RoundUp(current_header.GetImageSize(), kElfSegmentAlignment);
260       CHECK_GT(current_header.GetOatFileEnd(), current_header.GetOatFileBegin());
261       current_oat = current_header.GetOatFileEnd();
262     }
263     // Check that oat files start at the end of images.
264     CHECK_EQ(current_heap, image_spaces[i]->GetImageHeader().GetOatFileBegin());
265     // Check that the reservation size equals the size of images and oat files.
266     CHECK_EQ(reservation_size, static_cast<size_t>(current_oat - image_spaces[i]->Begin()));
267 
268     boot_image_size += reservation_size;
269     i += image_count;
270   }
271 }
272 
Heap(size_t initial_size,size_t growth_limit,size_t min_free,size_t max_free,double target_utilization,double foreground_heap_growth_multiplier,size_t stop_for_native_allocs,size_t capacity,size_t non_moving_space_capacity,const std::vector<std::string> & boot_class_path,const std::vector<std::string> & boot_class_path_locations,ArrayRef<File> boot_class_path_files,ArrayRef<File> boot_class_path_image_files,ArrayRef<File> boot_class_path_vdex_files,ArrayRef<File> boot_class_path_oat_files,const std::vector<std::string> & image_file_names,const InstructionSet image_instruction_set,CollectorType foreground_collector_type,CollectorType background_collector_type,space::LargeObjectSpaceType large_object_space_type,size_t large_object_threshold,size_t parallel_gc_threads,size_t conc_gc_threads,bool low_memory_mode,size_t long_pause_log_threshold,size_t long_gc_log_threshold,bool ignore_target_footprint,bool always_log_explicit_gcs,bool use_tlab,bool verify_pre_gc_heap,bool verify_pre_sweeping_heap,bool verify_post_gc_heap,bool verify_pre_gc_rosalloc,bool verify_pre_sweeping_rosalloc,bool verify_post_gc_rosalloc,bool gc_stress_mode,bool measure_gc_performance,bool use_homogeneous_space_compaction_for_oom,bool use_generational_gc,uint64_t min_interval_homogeneous_space_compaction_by_oom,bool dump_region_info_before_gc,bool dump_region_info_after_gc)273 Heap::Heap(size_t initial_size,
274            size_t growth_limit,
275            size_t min_free,
276            size_t max_free,
277            double target_utilization,
278            double foreground_heap_growth_multiplier,
279            size_t stop_for_native_allocs,
280            size_t capacity,
281            size_t non_moving_space_capacity,
282            const std::vector<std::string>& boot_class_path,
283            const std::vector<std::string>& boot_class_path_locations,
284            ArrayRef<File> boot_class_path_files,
285            ArrayRef<File> boot_class_path_image_files,
286            ArrayRef<File> boot_class_path_vdex_files,
287            ArrayRef<File> boot_class_path_oat_files,
288            const std::vector<std::string>& image_file_names,
289            const InstructionSet image_instruction_set,
290            CollectorType foreground_collector_type,
291            CollectorType background_collector_type,
292            space::LargeObjectSpaceType large_object_space_type,
293            size_t large_object_threshold,
294            size_t parallel_gc_threads,
295            size_t conc_gc_threads,
296            bool low_memory_mode,
297            size_t long_pause_log_threshold,
298            size_t long_gc_log_threshold,
299            bool ignore_target_footprint,
300            bool always_log_explicit_gcs,
301            bool use_tlab,
302            bool verify_pre_gc_heap,
303            bool verify_pre_sweeping_heap,
304            bool verify_post_gc_heap,
305            bool verify_pre_gc_rosalloc,
306            bool verify_pre_sweeping_rosalloc,
307            bool verify_post_gc_rosalloc,
308            bool gc_stress_mode,
309            bool measure_gc_performance,
310            bool use_homogeneous_space_compaction_for_oom,
311            bool use_generational_gc,
312            uint64_t min_interval_homogeneous_space_compaction_by_oom,
313            bool dump_region_info_before_gc,
314            bool dump_region_info_after_gc)
315     : non_moving_space_(nullptr),
316       rosalloc_space_(nullptr),
317       dlmalloc_space_(nullptr),
318       main_space_(nullptr),
319       collector_type_(kCollectorTypeNone),
320       foreground_collector_type_(foreground_collector_type),
321       background_collector_type_(background_collector_type),
322       desired_collector_type_(foreground_collector_type_),
323       pending_task_lock_(nullptr),
324       parallel_gc_threads_(parallel_gc_threads),
325       conc_gc_threads_(conc_gc_threads),
326       low_memory_mode_(low_memory_mode),
327       long_pause_log_threshold_(long_pause_log_threshold),
328       long_gc_log_threshold_(long_gc_log_threshold),
329       process_cpu_start_time_ns_(ProcessCpuNanoTime()),
330       pre_gc_last_process_cpu_time_ns_(process_cpu_start_time_ns_),
331       post_gc_last_process_cpu_time_ns_(process_cpu_start_time_ns_),
332       pre_gc_weighted_allocated_bytes_(0.0),
333       post_gc_weighted_allocated_bytes_(0.0),
334       ignore_target_footprint_(ignore_target_footprint),
335       always_log_explicit_gcs_(always_log_explicit_gcs),
336       zygote_creation_lock_("zygote creation lock", kZygoteCreationLock),
337       zygote_space_(nullptr),
338       large_object_threshold_(large_object_threshold),
339       disable_thread_flip_count_(0),
340       thread_flip_running_(false),
341       collector_type_running_(kCollectorTypeNone),
342       last_gc_cause_(kGcCauseNone),
343       thread_running_gc_(nullptr),
344       last_gc_type_(collector::kGcTypeNone),
345       next_gc_type_(collector::kGcTypePartial),
346       capacity_(capacity),
347       growth_limit_(growth_limit),
348       initial_heap_size_(initial_size),
349       target_footprint_(initial_size),
350       // Using kPostMonitorLock as a lock at kDefaultMutexLevel is acquired after
351       // this one.
352       process_state_update_lock_("process state update lock", kPostMonitorLock),
353       min_foreground_target_footprint_(0),
354       min_foreground_concurrent_start_bytes_(0),
355       concurrent_start_bytes_(std::numeric_limits<size_t>::max()),
356       total_bytes_freed_ever_(0),
357       total_objects_freed_ever_(0),
358       num_bytes_allocated_(0),
359       native_bytes_registered_(0),
360       old_native_bytes_allocated_(0),
361       native_objects_notified_(0),
362       num_bytes_freed_revoke_(0),
363       num_bytes_alive_after_gc_(0),
364       verify_missing_card_marks_(false),
365       verify_system_weaks_(false),
366       verify_pre_gc_heap_(verify_pre_gc_heap),
367       verify_pre_sweeping_heap_(verify_pre_sweeping_heap),
368       verify_post_gc_heap_(verify_post_gc_heap),
369       verify_mod_union_table_(false),
370       verify_pre_gc_rosalloc_(verify_pre_gc_rosalloc),
371       verify_pre_sweeping_rosalloc_(verify_pre_sweeping_rosalloc),
372       verify_post_gc_rosalloc_(verify_post_gc_rosalloc),
373       gc_stress_mode_(gc_stress_mode),
374       /* For GC a lot mode, we limit the allocation stacks to be kGcAlotInterval allocations. This
375        * causes a lot of GC since we do a GC for alloc whenever the stack is full. When heap
376        * verification is enabled, we limit the size of allocation stacks to speed up their
377        * searching.
378        */
379       max_allocation_stack_size_(kGCALotMode ? kGcAlotAllocationStackSize
380                                  : (kVerifyObjectSupport > kVerifyObjectModeFast)
381                                      ? kVerifyObjectAllocationStackSize
382                                      : kDefaultAllocationStackSize),
383       current_allocator_(kAllocatorTypeDlMalloc),
384       current_non_moving_allocator_(kAllocatorTypeNonMoving),
385       bump_pointer_space_(nullptr),
386       temp_space_(nullptr),
387       region_space_(nullptr),
388       min_free_(min_free),
389       max_free_(max_free),
390       target_utilization_(target_utilization),
391       foreground_heap_growth_multiplier_(foreground_heap_growth_multiplier),
392       stop_for_native_allocs_(stop_for_native_allocs),
393       total_wait_time_(0),
394       verify_object_mode_(kVerifyObjectModeDisabled),
395       disable_moving_gc_count_(0),
396       semi_space_collector_(nullptr),
397       active_concurrent_copying_collector_(nullptr),
398       young_concurrent_copying_collector_(nullptr),
399       concurrent_copying_collector_(nullptr),
400       is_running_on_memory_tool_(Runtime::Current()->IsRunningOnMemoryTool()),
401       use_tlab_(use_tlab),
402       main_space_backup_(nullptr),
403       min_interval_homogeneous_space_compaction_by_oom_(
404           min_interval_homogeneous_space_compaction_by_oom),
405       last_time_homogeneous_space_compaction_by_oom_(NanoTime()),
406       gcs_completed_(0u),
407       max_gc_requested_(0u),
408       pending_collector_transition_(nullptr),
409       pending_heap_trim_(nullptr),
410       use_homogeneous_space_compaction_for_oom_(use_homogeneous_space_compaction_for_oom),
411       use_generational_gc_(use_generational_gc),
412       running_collection_is_blocking_(false),
413       blocking_gc_count_(0U),
414       blocking_gc_time_(0U),
415       last_update_time_gc_count_rate_histograms_(  // Round down by the window duration.
416           (NanoTime() / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration),
417       gc_count_last_window_(0U),
418       blocking_gc_count_last_window_(0U),
419       gc_count_rate_histogram_("gc count rate histogram", 1U, kGcCountRateMaxBucketCount),
420       blocking_gc_count_rate_histogram_(
421           "blocking gc count rate histogram", 1U, kGcCountRateMaxBucketCount),
422       alloc_tracking_enabled_(false),
423       alloc_record_depth_(AllocRecordObjectMap::kDefaultAllocStackDepth),
424       backtrace_lock_(nullptr),
425       seen_backtrace_count_(0u),
426       unique_backtrace_count_(0u),
427       gc_disabled_for_shutdown_(false),
428       dump_region_info_before_gc_(dump_region_info_before_gc),
429       dump_region_info_after_gc_(dump_region_info_after_gc),
430       boot_image_spaces_(),
431       boot_images_start_address_(0u),
432       boot_images_size_(0u),
433       pre_oome_gc_count_(0u) {
434   if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
435     LOG(INFO) << "Heap() entering";
436   }
437 
438   LOG(INFO) << "Using " << foreground_collector_type_ << " GC.";
439   if (gUseUserfaultfd) {
440     CHECK_EQ(foreground_collector_type_, kCollectorTypeCMC);
441     CHECK_EQ(background_collector_type_, kCollectorTypeCMCBackground);
442   } else {
443     // This ensures that userfaultfd syscall is done before any seccomp filter is installed.
444     // TODO(b/266731037): Remove this when we no longer need to collect metric on userfaultfd
445     // support.
446     auto [uffd_supported, minor_fault_supported] = collector::MarkCompact::GetUffdAndMinorFault();
447     // The check is just to ensure that compiler doesn't eliminate the function call above.
448     // Userfaultfd support is certain to be there if its minor-fault feature is supported.
449     CHECK_IMPLIES(minor_fault_supported, uffd_supported);
450   }
451 
452   if (gUseReadBarrier) {
453     CHECK_EQ(foreground_collector_type_, kCollectorTypeCC);
454     CHECK_EQ(background_collector_type_, kCollectorTypeCCBackground);
455   } else if (background_collector_type_ != gc::kCollectorTypeHomogeneousSpaceCompact) {
456     CHECK_EQ(IsMovingGc(foreground_collector_type_), IsMovingGc(background_collector_type_))
457         << "Changing from " << foreground_collector_type_ << " to "
458         << background_collector_type_ << " (or visa versa) is not supported.";
459   }
460   verification_.reset(new Verification(this));
461   CHECK_GE(large_object_threshold, kMinLargeObjectThreshold);
462   ScopedTrace trace(__FUNCTION__);
463   Runtime* const runtime = Runtime::Current();
464   // If we aren't the zygote, switch to the default non zygote allocator. This may update the
465   // entrypoints.
466   const bool is_zygote = runtime->IsZygote();
467   if (!is_zygote) {
468     // Background compaction is currently not supported for command line runs.
469     if (background_collector_type_ != foreground_collector_type_) {
470       VLOG(heap) << "Disabling background compaction for non zygote";
471       background_collector_type_ = foreground_collector_type_;
472     }
473   }
474   ChangeCollector(desired_collector_type_);
475   live_bitmap_.reset(new accounting::HeapBitmap(this));
476   mark_bitmap_.reset(new accounting::HeapBitmap(this));
477 
478   // We don't have hspace compaction enabled with CC.
479   if (foreground_collector_type_ == kCollectorTypeCC
480       || foreground_collector_type_ == kCollectorTypeCMC) {
481     use_homogeneous_space_compaction_for_oom_ = false;
482   }
483   bool support_homogeneous_space_compaction =
484       background_collector_type_ == gc::kCollectorTypeHomogeneousSpaceCompact ||
485       use_homogeneous_space_compaction_for_oom_;
486   // We may use the same space the main space for the non moving space if we don't need to compact
487   // from the main space.
488   // This is not the case if we support homogeneous compaction or have a moving background
489   // collector type.
490   bool separate_non_moving_space = is_zygote ||
491       support_homogeneous_space_compaction || IsMovingGc(foreground_collector_type_) ||
492       IsMovingGc(background_collector_type_);
493 
494   // Requested begin for the alloc space, to follow the mapped image and oat files
495   uint8_t* request_begin = nullptr;
496   // Calculate the extra space required after the boot image, see allocations below.
497   size_t heap_reservation_size = 0u;
498   if (separate_non_moving_space) {
499     heap_reservation_size = non_moving_space_capacity;
500   } else if (foreground_collector_type_ != kCollectorTypeCC && is_zygote) {
501     heap_reservation_size = capacity_;
502   }
503   heap_reservation_size = RoundUp(heap_reservation_size, gPageSize);
504   // Load image space(s).
505   std::vector<std::unique_ptr<space::ImageSpace>> boot_image_spaces;
506   MemMap heap_reservation;
507   if (space::ImageSpace::LoadBootImage(boot_class_path,
508                                        boot_class_path_locations,
509                                        boot_class_path_files,
510                                        boot_class_path_image_files,
511                                        boot_class_path_vdex_files,
512                                        boot_class_path_oat_files,
513                                        image_file_names,
514                                        image_instruction_set,
515                                        runtime->ShouldRelocate(),
516                                        /*executable=*/!runtime->IsAotCompiler(),
517                                        heap_reservation_size,
518                                        runtime->AllowInMemoryCompilation(),
519                                        runtime->GetApexVersions(),
520                                        &boot_image_spaces,
521                                        &heap_reservation)) {
522     DCHECK_EQ(heap_reservation_size, heap_reservation.IsValid() ? heap_reservation.Size() : 0u);
523     DCHECK(!boot_image_spaces.empty());
524     request_begin = boot_image_spaces.back()->GetImageHeader().GetOatFileEnd();
525     DCHECK_IMPLIES(heap_reservation.IsValid(), request_begin == heap_reservation.Begin())
526         << "request_begin=" << static_cast<const void*>(request_begin)
527         << " heap_reservation.Begin()=" << static_cast<const void*>(heap_reservation.Begin());
528     for (std::unique_ptr<space::ImageSpace>& space : boot_image_spaces) {
529       boot_image_spaces_.push_back(space.get());
530       AddSpace(space.release());
531     }
532     boot_images_start_address_ = PointerToLowMemUInt32(boot_image_spaces_.front()->Begin());
533     uint32_t boot_images_end =
534         PointerToLowMemUInt32(boot_image_spaces_.back()->GetImageHeader().GetOatFileEnd());
535     boot_images_size_ = boot_images_end - boot_images_start_address_;
536     if (kIsDebugBuild) {
537       VerifyBootImagesContiguity(boot_image_spaces_);
538     }
539   } else {
540     if (foreground_collector_type_ == kCollectorTypeCC) {
541       // Need to use a low address so that we can allocate a contiguous 2 * Xmx space
542       // when there's no image (dex2oat for target).
543       request_begin = kPreferredAllocSpaceBegin;
544     }
545     // Gross hack to make dex2oat deterministic.
546     if (foreground_collector_type_ == kCollectorTypeMS && Runtime::Current()->IsAotCompiler()) {
547       // Currently only enabled for MS collector since that is what the deterministic dex2oat uses.
548       // b/26849108
549       request_begin = reinterpret_cast<uint8_t*>(kAllocSpaceBeginForDeterministicAoT);
550     }
551   }
552 
553   /*
554   requested_alloc_space_begin ->     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
555                                      +-  nonmoving space (non_moving_space_capacity)+-
556                                      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
557                                      +-????????????????????????????????????????????+-
558                                      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
559                                      +-main alloc space / bump space 1 (capacity_) +-
560                                      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
561                                      +-????????????????????????????????????????????+-
562                                      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
563                                      +-main alloc space2 / bump space 2 (capacity_)+-
564                                      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
565   */
566 
567   MemMap main_mem_map_1;
568   MemMap main_mem_map_2;
569 
570   std::string error_str;
571   MemMap non_moving_space_mem_map;
572   if (separate_non_moving_space) {
573     ScopedTrace trace2("Create separate non moving space");
574     // If we are the zygote, the non moving space becomes the zygote space when we run
575     // PreZygoteFork the first time. In this case, call the map "zygote space" since we can't
576     // rename the mem map later.
577     const char* space_name = is_zygote ? kZygoteSpaceName : kNonMovingSpaceName;
578     // Reserve the non moving mem map before the other two since it needs to be at a specific
579     // address.
580     DCHECK_EQ(heap_reservation.IsValid(), !boot_image_spaces_.empty());
581     if (heap_reservation.IsValid()) {
582       non_moving_space_mem_map = heap_reservation.RemapAtEnd(
583           heap_reservation.Begin(), space_name, PROT_READ | PROT_WRITE, &error_str);
584     } else {
585       non_moving_space_mem_map = MapAnonymousPreferredAddress(
586           space_name, request_begin, non_moving_space_capacity, &error_str);
587     }
588     CHECK(non_moving_space_mem_map.IsValid()) << error_str;
589     DCHECK(!heap_reservation.IsValid());
590     // Try to reserve virtual memory at a lower address if we have a separate non moving space.
591     request_begin = non_moving_space_mem_map.Begin() == kPreferredAllocSpaceBegin
592                         ? non_moving_space_mem_map.End()
593                         : kPreferredAllocSpaceBegin;
594   }
595   // Attempt to create 2 mem maps at or after the requested begin.
596   if (foreground_collector_type_ != kCollectorTypeCC) {
597     ScopedTrace trace2("Create main mem map");
598     if (separate_non_moving_space || !is_zygote) {
599       main_mem_map_1 = MapAnonymousPreferredAddress(
600           kMemMapSpaceName[0], request_begin, capacity_, &error_str);
601     } else {
602       // If no separate non-moving space and we are the zygote, the main space must come right after
603       // the image space to avoid a gap. This is required since we want the zygote space to be
604       // adjacent to the image space.
605       DCHECK_EQ(heap_reservation.IsValid(), !boot_image_spaces_.empty());
606       main_mem_map_1 = MemMap::MapAnonymous(
607           kMemMapSpaceName[0],
608           request_begin,
609           capacity_,
610           PROT_READ | PROT_WRITE,
611           /* low_4gb= */ true,
612           /* reuse= */ false,
613           heap_reservation.IsValid() ? &heap_reservation : nullptr,
614           &error_str);
615     }
616     CHECK(main_mem_map_1.IsValid()) << error_str;
617     DCHECK(!heap_reservation.IsValid());
618   }
619   if (support_homogeneous_space_compaction ||
620       background_collector_type_ == kCollectorTypeSS ||
621       foreground_collector_type_ == kCollectorTypeSS) {
622     ScopedTrace trace2("Create main mem map 2");
623     main_mem_map_2 = MapAnonymousPreferredAddress(
624         kMemMapSpaceName[1], main_mem_map_1.End(), capacity_, &error_str);
625     CHECK(main_mem_map_2.IsValid()) << error_str;
626   }
627 
628   // Create the non moving space first so that bitmaps don't take up the address range.
629   if (separate_non_moving_space) {
630     ScopedTrace trace2("Add non moving space");
631     // Non moving space is always dlmalloc since we currently don't have support for multiple
632     // active rosalloc spaces.
633     const size_t size = non_moving_space_mem_map.Size();
634     const void* non_moving_space_mem_map_begin = non_moving_space_mem_map.Begin();
635     non_moving_space_ = space::DlMallocSpace::CreateFromMemMap(std::move(non_moving_space_mem_map),
636                                                                "zygote / non moving space",
637                                                                GetDefaultStartingSize(),
638                                                                initial_size,
639                                                                size,
640                                                                size,
641                                                                /* can_move_objects= */ false);
642     CHECK(non_moving_space_ != nullptr) << "Failed creating non moving space "
643         << non_moving_space_mem_map_begin;
644     non_moving_space_->SetFootprintLimit(non_moving_space_->Capacity());
645     AddSpace(non_moving_space_);
646   }
647   // Create other spaces based on whether or not we have a moving GC.
648   if (foreground_collector_type_ == kCollectorTypeCC) {
649     CHECK(separate_non_moving_space);
650     // Reserve twice the capacity, to allow evacuating every region for explicit GCs.
651     MemMap region_space_mem_map =
652         space::RegionSpace::CreateMemMap(kRegionSpaceName, capacity_ * 2, request_begin);
653     CHECK(region_space_mem_map.IsValid()) << "No region space mem map";
654     region_space_ = space::RegionSpace::Create(
655         kRegionSpaceName, std::move(region_space_mem_map), use_generational_gc_);
656     AddSpace(region_space_);
657   } else if (IsMovingGc(foreground_collector_type_)) {
658     // Create bump pointer spaces.
659     // We only to create the bump pointer if the foreground collector is a compacting GC.
660     // TODO: Place bump-pointer spaces somewhere to minimize size of card table.
661     bump_pointer_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 1",
662                                                                     std::move(main_mem_map_1));
663     CHECK(bump_pointer_space_ != nullptr) << "Failed to create bump pointer space";
664     AddSpace(bump_pointer_space_);
665     // For Concurrent Mark-compact GC we don't need the temp space to be in
666     // lower 4GB. So its temp space will be created by the GC itself.
667     if (foreground_collector_type_ != kCollectorTypeCMC) {
668       temp_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 2",
669                                                               std::move(main_mem_map_2));
670       CHECK(temp_space_ != nullptr) << "Failed to create bump pointer space";
671       AddSpace(temp_space_);
672     }
673     CHECK(separate_non_moving_space);
674   } else {
675     CreateMainMallocSpace(std::move(main_mem_map_1), initial_size, growth_limit_, capacity_);
676     CHECK(main_space_ != nullptr);
677     AddSpace(main_space_);
678     if (!separate_non_moving_space) {
679       non_moving_space_ = main_space_;
680       CHECK(!non_moving_space_->CanMoveObjects());
681     }
682     if (main_mem_map_2.IsValid()) {
683       const char* name = kUseRosAlloc ? kRosAllocSpaceName[1] : kDlMallocSpaceName[1];
684       main_space_backup_.reset(CreateMallocSpaceFromMemMap(std::move(main_mem_map_2),
685                                                            initial_size,
686                                                            growth_limit_,
687                                                            capacity_,
688                                                            name,
689                                                            /* can_move_objects= */ true));
690       CHECK(main_space_backup_.get() != nullptr);
691       // Add the space so its accounted for in the heap_begin and heap_end.
692       AddSpace(main_space_backup_.get());
693     }
694   }
695   CHECK(non_moving_space_ != nullptr);
696   CHECK(!non_moving_space_->CanMoveObjects());
697   // Allocate the large object space.
698   if (large_object_space_type == space::LargeObjectSpaceType::kFreeList) {
699     large_object_space_ = space::FreeListSpace::Create("free list large object space", capacity_);
700     CHECK(large_object_space_ != nullptr) << "Failed to create large object space";
701   } else if (large_object_space_type == space::LargeObjectSpaceType::kMap) {
702     large_object_space_ = space::LargeObjectMapSpace::Create("mem map large object space");
703     CHECK(large_object_space_ != nullptr) << "Failed to create large object space";
704   } else {
705     // Disable the large object space by making the cutoff excessively large.
706     large_object_threshold_ = std::numeric_limits<size_t>::max();
707     large_object_space_ = nullptr;
708   }
709   if (large_object_space_ != nullptr) {
710     AddSpace(large_object_space_);
711   }
712   // Compute heap capacity. Continuous spaces are sorted in order of Begin().
713   CHECK(!continuous_spaces_.empty());
714   // Relies on the spaces being sorted.
715   uint8_t* heap_begin = continuous_spaces_.front()->Begin();
716   uint8_t* heap_end = continuous_spaces_.back()->Limit();
717   size_t heap_capacity = heap_end - heap_begin;
718   // Remove the main backup space since it slows down the GC to have unused extra spaces.
719   // TODO: Avoid needing to do this.
720   if (main_space_backup_.get() != nullptr) {
721     RemoveSpace(main_space_backup_.get());
722   }
723   // Allocate the card table.
724   // We currently don't support dynamically resizing the card table.
725   // Since we don't know where in the low_4gb the app image will be located, make the card table
726   // cover the whole low_4gb. TODO: Extend the card table in AddSpace.
727   UNUSED(heap_capacity);
728   // Start at 4 KB, we can be sure there are no spaces mapped this low since the address range is
729   // reserved by the kernel.
730   static constexpr size_t kMinHeapAddress = 4 * KB;
731   card_table_.reset(accounting::CardTable::Create(reinterpret_cast<uint8_t*>(kMinHeapAddress),
732                                                   4 * GB - kMinHeapAddress));
733   CHECK(card_table_.get() != nullptr) << "Failed to create card table";
734   if (foreground_collector_type_ == kCollectorTypeCC && kUseTableLookupReadBarrier) {
735     rb_table_.reset(new accounting::ReadBarrierTable());
736     DCHECK(rb_table_->IsAllCleared());
737   }
738   if (HasBootImageSpace()) {
739     // Don't add the image mod union table if we are running without an image, this can crash if
740     // we use the CardCache implementation.
741     for (space::ImageSpace* image_space : GetBootImageSpaces()) {
742       accounting::ModUnionTable* mod_union_table = new accounting::ModUnionTableToZygoteAllocspace(
743           "Image mod-union table", this, image_space);
744       CHECK(mod_union_table != nullptr) << "Failed to create image mod-union table";
745       AddModUnionTable(mod_union_table);
746     }
747   }
748   if (collector::SemiSpace::kUseRememberedSet && non_moving_space_ != main_space_) {
749     accounting::RememberedSet* non_moving_space_rem_set =
750         new accounting::RememberedSet("Non-moving space remembered set", this, non_moving_space_);
751     CHECK(non_moving_space_rem_set != nullptr) << "Failed to create non-moving space remembered set";
752     AddRememberedSet(non_moving_space_rem_set);
753   }
754   // TODO: Count objects in the image space here?
755   num_bytes_allocated_.store(0, std::memory_order_relaxed);
756   mark_stack_.reset(accounting::ObjectStack::Create("mark stack", kDefaultMarkStackSize,
757                                                     kDefaultMarkStackSize));
758   const size_t alloc_stack_capacity = max_allocation_stack_size_ + kAllocationStackReserveSize;
759   allocation_stack_.reset(accounting::ObjectStack::Create(
760       "allocation stack", max_allocation_stack_size_, alloc_stack_capacity));
761   live_stack_.reset(accounting::ObjectStack::Create(
762       "live stack", max_allocation_stack_size_, alloc_stack_capacity));
763   // It's still too early to take a lock because there are no threads yet, but we can create locks
764   // now. We don't create it earlier to make it clear that you can't use locks during heap
765   // initialization.
766   gc_complete_lock_ = new Mutex("GC complete lock");
767   gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable",
768                                                 *gc_complete_lock_));
769 
770   thread_flip_lock_ = new Mutex("GC thread flip lock");
771   thread_flip_cond_.reset(new ConditionVariable("GC thread flip condition variable",
772                                                 *thread_flip_lock_));
773   task_processor_.reset(new TaskProcessor());
774   reference_processor_.reset(new ReferenceProcessor());
775   pending_task_lock_ = new Mutex("Pending task lock");
776   if (ignore_target_footprint_) {
777     SetIdealFootprint(std::numeric_limits<size_t>::max());
778     concurrent_start_bytes_ = std::numeric_limits<size_t>::max();
779   }
780   CHECK_NE(target_footprint_.load(std::memory_order_relaxed), 0U);
781   CreateGarbageCollectors(measure_gc_performance);
782   if (!GetBootImageSpaces().empty() && non_moving_space_ != nullptr &&
783       (is_zygote || separate_non_moving_space)) {
784     // Check that there's no gap between the image space and the non moving space so that the
785     // immune region won't break (eg. due to a large object allocated in the gap). This is only
786     // required when we're the zygote.
787     // Space with smallest Begin().
788     space::ImageSpace* first_space = nullptr;
789     for (space::ImageSpace* space : boot_image_spaces_) {
790       if (first_space == nullptr || space->Begin() < first_space->Begin()) {
791         first_space = space;
792       }
793     }
794     bool no_gap = MemMap::CheckNoGaps(*first_space->GetMemMap(), *non_moving_space_->GetMemMap());
795     if (!no_gap) {
796       PrintFileToLog("/proc/self/maps", LogSeverity::ERROR);
797       MemMap::DumpMaps(LOG_STREAM(ERROR), /* terse= */ true);
798       LOG(FATAL) << "There's a gap between the image space and the non-moving space";
799     }
800   }
801   // Perfetto Java Heap Profiler Support.
802   if (runtime->IsPerfettoJavaHeapStackProfEnabled()) {
803     // Perfetto Plugin is loaded and enabled, initialize the Java Heap Profiler.
804     InitPerfettoJavaHeapProf();
805   } else {
806     // Disable the Java Heap Profiler.
807     GetHeapSampler().DisableHeapSampler();
808   }
809 
810   instrumentation::Instrumentation* const instrumentation = runtime->GetInstrumentation();
811   if (gc_stress_mode_) {
812     backtrace_lock_ = new Mutex("GC complete lock");
813   }
814   if (is_running_on_memory_tool_ || gc_stress_mode_) {
815     instrumentation->InstrumentQuickAllocEntryPoints();
816   }
817   if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
818     LOG(INFO) << "Heap() exiting";
819   }
820 }
821 
CreateGarbageCollectors(bool measure_gc_performance)822 void Heap::CreateGarbageCollectors(bool measure_gc_performance) {
823   for (size_t i = 0; i < 2; ++i) {
824     const bool concurrent = (i != 0);
825     if ((MayUseCollector(kCollectorTypeCMS) && concurrent) ||
826         (MayUseCollector(kCollectorTypeMS) && !concurrent)) {
827       garbage_collectors_.push_back(new collector::MarkSweep(this, concurrent));
828       garbage_collectors_.push_back(new collector::PartialMarkSweep(this, concurrent));
829       garbage_collectors_.push_back(new collector::StickyMarkSweep(this, concurrent));
830     }
831   }
832   if (kMovingCollector) {
833     if (MayUseCollector(kCollectorTypeSS) ||
834         MayUseCollector(kCollectorTypeHomogeneousSpaceCompact) ||
835         use_homogeneous_space_compaction_for_oom_) {
836       semi_space_collector_ = new collector::SemiSpace(this);
837       garbage_collectors_.push_back(semi_space_collector_);
838     }
839     if (MayUseCollector(kCollectorTypeCMC)) {
840       mark_compact_ = new collector::MarkCompact(this);
841       garbage_collectors_.push_back(mark_compact_);
842       if (use_generational_gc_) {
843         young_mark_compact_ = new collector::YoungMarkCompact(this, mark_compact_);
844         garbage_collectors_.push_back(young_mark_compact_);
845       }
846     }
847     if (MayUseCollector(kCollectorTypeCC)) {
848       concurrent_copying_collector_ = new collector::ConcurrentCopying(this,
849                                                                        /*young_gen=*/false,
850                                                                        use_generational_gc_,
851                                                                        "",
852                                                                        measure_gc_performance);
853       if (use_generational_gc_) {
854         young_concurrent_copying_collector_ =
855             new collector::ConcurrentCopying(this,
856                                              /*young_gen=*/true,
857                                              use_generational_gc_,
858                                              "young",
859                                              measure_gc_performance);
860       }
861       active_concurrent_copying_collector_.store(concurrent_copying_collector_,
862                                                  std::memory_order_relaxed);
863       DCHECK(region_space_ != nullptr);
864       concurrent_copying_collector_->SetRegionSpace(region_space_);
865       if (use_generational_gc_) {
866         young_concurrent_copying_collector_->SetRegionSpace(region_space_);
867         // At this point, non-moving space should be created.
868         DCHECK(non_moving_space_ != nullptr);
869         concurrent_copying_collector_->CreateInterRegionRefBitmaps();
870       }
871       garbage_collectors_.push_back(concurrent_copying_collector_);
872       if (use_generational_gc_) {
873         garbage_collectors_.push_back(young_concurrent_copying_collector_);
874       }
875     }
876   }
877 }
878 
MapAnonymousPreferredAddress(const char * name,uint8_t * request_begin,size_t capacity,std::string * out_error_str)879 MemMap Heap::MapAnonymousPreferredAddress(const char* name,
880                                           uint8_t* request_begin,
881                                           size_t capacity,
882                                           std::string* out_error_str) {
883   while (true) {
884     MemMap map = MemMap::MapAnonymous(name,
885                                       request_begin,
886                                       capacity,
887                                       PROT_READ | PROT_WRITE,
888                                       /*low_4gb=*/ true,
889                                       /*reuse=*/ false,
890                                       /*reservation=*/ nullptr,
891                                       out_error_str);
892     if (map.IsValid() || request_begin == nullptr) {
893       return map;
894     }
895     // Retry a  second time with no specified request begin.
896     request_begin = nullptr;
897   }
898 }
899 
MayUseCollector(CollectorType type) const900 bool Heap::MayUseCollector(CollectorType type) const {
901   return foreground_collector_type_ == type || background_collector_type_ == type;
902 }
903 
CreateMallocSpaceFromMemMap(MemMap && mem_map,size_t initial_size,size_t growth_limit,size_t capacity,const char * name,bool can_move_objects)904 space::MallocSpace* Heap::CreateMallocSpaceFromMemMap(MemMap&& mem_map,
905                                                       size_t initial_size,
906                                                       size_t growth_limit,
907                                                       size_t capacity,
908                                                       const char* name,
909                                                       bool can_move_objects) {
910   space::MallocSpace* malloc_space = nullptr;
911   if (kUseRosAlloc) {
912     // Create rosalloc space.
913     malloc_space = space::RosAllocSpace::CreateFromMemMap(std::move(mem_map),
914                                                           name,
915                                                           GetDefaultStartingSize(),
916                                                           initial_size,
917                                                           growth_limit,
918                                                           capacity,
919                                                           low_memory_mode_,
920                                                           can_move_objects);
921   } else {
922     malloc_space = space::DlMallocSpace::CreateFromMemMap(std::move(mem_map),
923                                                           name,
924                                                           GetDefaultStartingSize(),
925                                                           initial_size,
926                                                           growth_limit,
927                                                           capacity,
928                                                           can_move_objects);
929   }
930   if (collector::SemiSpace::kUseRememberedSet) {
931     accounting::RememberedSet* rem_set  =
932         new accounting::RememberedSet(std::string(name) + " remembered set", this, malloc_space);
933     CHECK(rem_set != nullptr) << "Failed to create main space remembered set";
934     AddRememberedSet(rem_set);
935   }
936   CHECK(malloc_space != nullptr) << "Failed to create " << name;
937   malloc_space->SetFootprintLimit(malloc_space->Capacity());
938   return malloc_space;
939 }
940 
CreateMainMallocSpace(MemMap && mem_map,size_t initial_size,size_t growth_limit,size_t capacity)941 void Heap::CreateMainMallocSpace(MemMap&& mem_map,
942                                  size_t initial_size,
943                                  size_t growth_limit,
944                                  size_t capacity) {
945   // Is background compaction is enabled?
946   bool can_move_objects = IsMovingGc(background_collector_type_) !=
947       IsMovingGc(foreground_collector_type_) || use_homogeneous_space_compaction_for_oom_;
948   // If we are the zygote and don't yet have a zygote space, it means that the zygote fork will
949   // happen in the future. If this happens and we have kCompactZygote enabled we wish to compact
950   // from the main space to the zygote space. If background compaction is enabled, always pass in
951   // that we can move objets.
952   if (kCompactZygote && Runtime::Current()->IsZygote() && !can_move_objects) {
953     // After the zygote we want this to be false if we don't have background compaction enabled so
954     // that getting primitive array elements is faster.
955     can_move_objects = !HasZygoteSpace();
956   }
957   if (collector::SemiSpace::kUseRememberedSet && main_space_ != nullptr) {
958     RemoveRememberedSet(main_space_);
959   }
960   const char* name = kUseRosAlloc ? kRosAllocSpaceName[0] : kDlMallocSpaceName[0];
961   main_space_ = CreateMallocSpaceFromMemMap(std::move(mem_map),
962                                             initial_size,
963                                             growth_limit,
964                                             capacity, name,
965                                             can_move_objects);
966   SetSpaceAsDefault(main_space_);
967   VLOG(heap) << "Created main space " << main_space_;
968 }
969 
ChangeAllocator(AllocatorType allocator)970 void Heap::ChangeAllocator(AllocatorType allocator) {
971   if (current_allocator_ != allocator) {
972     // These two allocators are only used internally and don't have any entrypoints.
973     CHECK_NE(allocator, kAllocatorTypeLOS);
974     CHECK_NE(allocator, kAllocatorTypeNonMoving);
975     current_allocator_ = allocator;
976     MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_);
977     SetQuickAllocEntryPointsAllocator(current_allocator_);
978     Runtime::Current()->GetInstrumentation()->ResetQuickAllocEntryPoints();
979   }
980 }
981 
IsCompilingBoot() const982 bool Heap::IsCompilingBoot() const {
983   if (!Runtime::Current()->IsAotCompiler()) {
984     return false;
985   }
986   ScopedObjectAccess soa(Thread::Current());
987   for (const auto& space : continuous_spaces_) {
988     if (space->IsImageSpace() || space->IsZygoteSpace()) {
989       return false;
990     }
991   }
992   return true;
993 }
994 
IncrementDisableMovingGC(Thread * self)995 void Heap::IncrementDisableMovingGC(Thread* self) {
996   // Need to do this holding the lock to prevent races where the GC is about to run / running when
997   // we attempt to disable it.
998   ScopedThreadStateChange tsc(self, ThreadState::kWaitingForGcToComplete);
999   MutexLock mu(self, *gc_complete_lock_);
1000   ++disable_moving_gc_count_;
1001   if (IsMovingGc(collector_type_running_)) {
1002     WaitForGcToCompleteLocked(kGcCauseDisableMovingGc, self);
1003   }
1004 }
1005 
DecrementDisableMovingGC(Thread * self)1006 void Heap::DecrementDisableMovingGC(Thread* self) {
1007   MutexLock mu(self, *gc_complete_lock_);
1008   CHECK_GT(disable_moving_gc_count_, 0U);
1009   --disable_moving_gc_count_;
1010 }
1011 
IncrementDisableThreadFlip(Thread * self)1012 void Heap::IncrementDisableThreadFlip(Thread* self) {
1013   // Supposed to be called by mutators. If thread_flip_running_ is true, block. Otherwise, go ahead.
1014   bool is_nested = self->GetDisableThreadFlipCount() > 0;
1015   self->IncrementDisableThreadFlipCount();
1016   if (is_nested) {
1017     // If this is a nested JNI critical section enter, we don't need to wait or increment the global
1018     // counter. The global counter is incremented only once for a thread for the outermost enter.
1019     return;
1020   }
1021   ScopedThreadStateChange tsc(self, ThreadState::kWaitingForGcThreadFlip);
1022   MutexLock mu(self, *thread_flip_lock_);
1023   thread_flip_cond_->CheckSafeToWait(self);
1024   bool has_waited = false;
1025   uint64_t wait_start = 0;
1026   if (thread_flip_running_) {
1027     wait_start = NanoTime();
1028     ScopedTrace trace("IncrementDisableThreadFlip");
1029     while (thread_flip_running_) {
1030       has_waited = true;
1031       thread_flip_cond_->Wait(self);
1032     }
1033   }
1034   ++disable_thread_flip_count_;
1035   if (has_waited) {
1036     uint64_t wait_time = NanoTime() - wait_start;
1037     total_wait_time_ += wait_time;
1038     if (wait_time > long_pause_log_threshold_) {
1039       LOG(INFO) << __FUNCTION__ << " blocked for " << PrettyDuration(wait_time);
1040     }
1041   }
1042 }
1043 
EnsureObjectUserfaulted(ObjPtr<mirror::Object> obj)1044 void Heap::EnsureObjectUserfaulted(ObjPtr<mirror::Object> obj) {
1045   if (gUseUserfaultfd) {
1046     // Use volatile to ensure that compiler loads from memory to trigger userfaults, if required.
1047     const uint8_t* start = reinterpret_cast<uint8_t*>(obj.Ptr());
1048     const uint8_t* end = AlignUp(start + obj->SizeOf(), gPageSize);
1049     // The first page is already touched by SizeOf().
1050     start += gPageSize;
1051     while (start < end) {
1052       ForceRead(start);
1053       start += gPageSize;
1054     }
1055   }
1056 }
1057 
DecrementDisableThreadFlip(Thread * self)1058 void Heap::DecrementDisableThreadFlip(Thread* self) {
1059   // Supposed to be called by mutators. Decrement disable_thread_flip_count_ and potentially wake up
1060   // the GC waiting before doing a thread flip.
1061   self->DecrementDisableThreadFlipCount();
1062   bool is_outermost = self->GetDisableThreadFlipCount() == 0;
1063   if (!is_outermost) {
1064     // If this is not an outermost JNI critical exit, we don't need to decrement the global counter.
1065     // The global counter is decremented only once for a thread for the outermost exit.
1066     return;
1067   }
1068   MutexLock mu(self, *thread_flip_lock_);
1069   CHECK_GT(disable_thread_flip_count_, 0U);
1070   --disable_thread_flip_count_;
1071   if (disable_thread_flip_count_ == 0) {
1072     // Potentially notify the GC thread blocking to begin a thread flip.
1073     thread_flip_cond_->Broadcast(self);
1074   }
1075 }
1076 
ThreadFlipBegin(Thread * self)1077 void Heap::ThreadFlipBegin(Thread* self) {
1078   // Supposed to be called by GC. Set thread_flip_running_ to be true. If disable_thread_flip_count_
1079   // > 0, block. Otherwise, go ahead.
1080   ScopedThreadStateChange tsc(self, ThreadState::kWaitingForGcThreadFlip);
1081   MutexLock mu(self, *thread_flip_lock_);
1082   thread_flip_cond_->CheckSafeToWait(self);
1083   bool has_waited = false;
1084   uint64_t wait_start = NanoTime();
1085   CHECK(!thread_flip_running_);
1086   // Set this to true before waiting so that frequent JNI critical enter/exits won't starve
1087   // GC. This like a writer preference of a reader-writer lock.
1088   thread_flip_running_ = true;
1089   while (disable_thread_flip_count_ > 0) {
1090     has_waited = true;
1091     thread_flip_cond_->Wait(self);
1092   }
1093   if (has_waited) {
1094     uint64_t wait_time = NanoTime() - wait_start;
1095     total_wait_time_ += wait_time;
1096     if (wait_time > long_pause_log_threshold_) {
1097       LOG(INFO) << __FUNCTION__ << " blocked for " << PrettyDuration(wait_time);
1098     }
1099   }
1100 }
1101 
ThreadFlipEnd(Thread * self)1102 void Heap::ThreadFlipEnd(Thread* self) {
1103   // Supposed to be called by GC. Set thread_flip_running_ to false and potentially wake up mutators
1104   // waiting before doing a JNI critical.
1105   MutexLock mu(self, *thread_flip_lock_);
1106   CHECK(thread_flip_running_);
1107   thread_flip_running_ = false;
1108   // Potentially notify mutator threads blocking to enter a JNI critical section.
1109   thread_flip_cond_->Broadcast(self);
1110 }
1111 
GrowHeapOnJankPerceptibleSwitch()1112 void Heap::GrowHeapOnJankPerceptibleSwitch() {
1113   MutexLock mu(Thread::Current(), process_state_update_lock_);
1114   size_t orig_target_footprint = target_footprint_.load(std::memory_order_relaxed);
1115   if (orig_target_footprint < min_foreground_target_footprint_) {
1116     target_footprint_.compare_exchange_strong(orig_target_footprint,
1117                                               min_foreground_target_footprint_,
1118                                               std::memory_order_relaxed);
1119   }
1120   if (IsGcConcurrent() && concurrent_start_bytes_ < min_foreground_concurrent_start_bytes_) {
1121     concurrent_start_bytes_ = min_foreground_concurrent_start_bytes_;
1122   }
1123 }
1124 
UpdateProcessState(ProcessState old_process_state,ProcessState new_process_state)1125 void Heap::UpdateProcessState(ProcessState old_process_state, ProcessState new_process_state) {
1126   if (old_process_state != new_process_state) {
1127     const bool jank_perceptible = new_process_state == kProcessStateJankPerceptible;
1128     if (jank_perceptible) {
1129       // Transition back to foreground right away to prevent jank.
1130       RequestCollectorTransition(foreground_collector_type_, 0);
1131       GrowHeapOnJankPerceptibleSwitch();
1132     } else {
1133       // If background_collector_type_ is kCollectorTypeHomogeneousSpaceCompact then we have
1134       // special handling which does a homogenous space compaction once but then doesn't transition
1135       // the collector. Similarly, we invoke a full compaction for kCollectorTypeCC but don't
1136       // transition the collector.
1137       RequestCollectorTransition(background_collector_type_, 0);
1138     }
1139   }
1140 }
1141 
CreateThreadPool(size_t num_threads)1142 void Heap::CreateThreadPool(size_t num_threads) {
1143   if (num_threads == 0) {
1144     num_threads = std::max(parallel_gc_threads_, conc_gc_threads_);
1145   }
1146   if (num_threads != 0) {
1147     thread_pool_.reset(ThreadPool::Create("Heap thread pool", num_threads));
1148   }
1149 }
1150 
WaitForWorkersToBeCreated()1151 void Heap::WaitForWorkersToBeCreated() {
1152   DCHECK(!Runtime::Current()->IsShuttingDown(Thread::Current()))
1153       << "Cannot create new threads during runtime shutdown";
1154   if (thread_pool_ != nullptr) {
1155     thread_pool_->WaitForWorkersToBeCreated();
1156   }
1157 }
1158 
MarkAllocStackAsLive(accounting::ObjectStack * stack)1159 void Heap::MarkAllocStackAsLive(accounting::ObjectStack* stack) {
1160   space::ContinuousSpace* space1 = main_space_ != nullptr ? main_space_ : non_moving_space_;
1161   space::ContinuousSpace* space2 = non_moving_space_;
1162   // TODO: Generalize this to n bitmaps?
1163   CHECK(space1 != nullptr);
1164   CHECK(space2 != nullptr);
1165   MarkAllocStack(space1->GetLiveBitmap(), space2->GetLiveBitmap(),
1166                  (large_object_space_ != nullptr ? large_object_space_->GetLiveBitmap() : nullptr),
1167                  stack);
1168 }
1169 
DeleteThreadPool()1170 void Heap::DeleteThreadPool() {
1171   thread_pool_.reset(nullptr);
1172 }
1173 
AddSpace(space::Space * space)1174 void Heap::AddSpace(space::Space* space) {
1175   CHECK(space != nullptr);
1176   WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
1177   if (space->IsContinuousSpace()) {
1178     DCHECK(!space->IsDiscontinuousSpace());
1179     space::ContinuousSpace* continuous_space = space->AsContinuousSpace();
1180     // Continuous spaces don't necessarily have bitmaps.
1181     accounting::ContinuousSpaceBitmap* live_bitmap = continuous_space->GetLiveBitmap();
1182     accounting::ContinuousSpaceBitmap* mark_bitmap = continuous_space->GetMarkBitmap();
1183     // The region space bitmap is not added since VisitObjects visits the region space objects with
1184     // special handling.
1185     if (live_bitmap != nullptr && !space->IsRegionSpace()) {
1186       CHECK(mark_bitmap != nullptr);
1187       live_bitmap_->AddContinuousSpaceBitmap(live_bitmap);
1188       mark_bitmap_->AddContinuousSpaceBitmap(mark_bitmap);
1189     }
1190     continuous_spaces_.push_back(continuous_space);
1191     // Ensure that spaces remain sorted in increasing order of start address.
1192     std::sort(continuous_spaces_.begin(), continuous_spaces_.end(),
1193               [](const space::ContinuousSpace* a, const space::ContinuousSpace* b) {
1194       return a->Begin() < b->Begin();
1195     });
1196   } else {
1197     CHECK(space->IsDiscontinuousSpace());
1198     space::DiscontinuousSpace* discontinuous_space = space->AsDiscontinuousSpace();
1199     live_bitmap_->AddLargeObjectBitmap(discontinuous_space->GetLiveBitmap());
1200     mark_bitmap_->AddLargeObjectBitmap(discontinuous_space->GetMarkBitmap());
1201     discontinuous_spaces_.push_back(discontinuous_space);
1202   }
1203   if (space->IsAllocSpace()) {
1204     alloc_spaces_.push_back(space->AsAllocSpace());
1205   }
1206 }
1207 
SetSpaceAsDefault(space::ContinuousSpace * continuous_space)1208 void Heap::SetSpaceAsDefault(space::ContinuousSpace* continuous_space) {
1209   WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
1210   if (continuous_space->IsDlMallocSpace()) {
1211     dlmalloc_space_ = continuous_space->AsDlMallocSpace();
1212   } else if (continuous_space->IsRosAllocSpace()) {
1213     rosalloc_space_ = continuous_space->AsRosAllocSpace();
1214   }
1215 }
1216 
RemoveSpace(space::Space * space)1217 void Heap::RemoveSpace(space::Space* space) {
1218   DCHECK(space != nullptr);
1219   WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
1220   if (space->IsContinuousSpace()) {
1221     DCHECK(!space->IsDiscontinuousSpace());
1222     space::ContinuousSpace* continuous_space = space->AsContinuousSpace();
1223     // Continuous spaces don't necessarily have bitmaps.
1224     accounting::ContinuousSpaceBitmap* live_bitmap = continuous_space->GetLiveBitmap();
1225     accounting::ContinuousSpaceBitmap* mark_bitmap = continuous_space->GetMarkBitmap();
1226     if (live_bitmap != nullptr && !space->IsRegionSpace()) {
1227       DCHECK(mark_bitmap != nullptr);
1228       live_bitmap_->RemoveContinuousSpaceBitmap(live_bitmap);
1229       mark_bitmap_->RemoveContinuousSpaceBitmap(mark_bitmap);
1230     }
1231     auto it = std::find(continuous_spaces_.begin(), continuous_spaces_.end(), continuous_space);
1232     DCHECK(it != continuous_spaces_.end());
1233     continuous_spaces_.erase(it);
1234   } else {
1235     DCHECK(space->IsDiscontinuousSpace());
1236     space::DiscontinuousSpace* discontinuous_space = space->AsDiscontinuousSpace();
1237     live_bitmap_->RemoveLargeObjectBitmap(discontinuous_space->GetLiveBitmap());
1238     mark_bitmap_->RemoveLargeObjectBitmap(discontinuous_space->GetMarkBitmap());
1239     auto it = std::find(discontinuous_spaces_.begin(), discontinuous_spaces_.end(),
1240                         discontinuous_space);
1241     DCHECK(it != discontinuous_spaces_.end());
1242     discontinuous_spaces_.erase(it);
1243   }
1244   if (space->IsAllocSpace()) {
1245     auto it = std::find(alloc_spaces_.begin(), alloc_spaces_.end(), space->AsAllocSpace());
1246     DCHECK(it != alloc_spaces_.end());
1247     alloc_spaces_.erase(it);
1248   }
1249 }
1250 
CalculateGcWeightedAllocatedBytes(uint64_t gc_last_process_cpu_time_ns,uint64_t current_process_cpu_time) const1251 double Heap::CalculateGcWeightedAllocatedBytes(uint64_t gc_last_process_cpu_time_ns,
1252                                                uint64_t current_process_cpu_time) const {
1253   uint64_t bytes_allocated = GetBytesAllocated();
1254   double weight = current_process_cpu_time - gc_last_process_cpu_time_ns;
1255   return weight * bytes_allocated;
1256 }
1257 
CalculatePreGcWeightedAllocatedBytes()1258 void Heap::CalculatePreGcWeightedAllocatedBytes() {
1259   uint64_t current_process_cpu_time = ProcessCpuNanoTime();
1260   pre_gc_weighted_allocated_bytes_ +=
1261     CalculateGcWeightedAllocatedBytes(pre_gc_last_process_cpu_time_ns_, current_process_cpu_time);
1262   pre_gc_last_process_cpu_time_ns_ = current_process_cpu_time;
1263 }
1264 
CalculatePostGcWeightedAllocatedBytes()1265 void Heap::CalculatePostGcWeightedAllocatedBytes() {
1266   uint64_t current_process_cpu_time = ProcessCpuNanoTime();
1267   post_gc_weighted_allocated_bytes_ +=
1268     CalculateGcWeightedAllocatedBytes(post_gc_last_process_cpu_time_ns_, current_process_cpu_time);
1269   post_gc_last_process_cpu_time_ns_ = current_process_cpu_time;
1270 }
1271 
GetTotalGcCpuTime()1272 uint64_t Heap::GetTotalGcCpuTime() {
1273   uint64_t sum = 0;
1274   for (auto* collector : garbage_collectors_) {
1275     sum += collector->GetTotalCpuTime();
1276   }
1277   return sum;
1278 }
1279 
DumpGcPerformanceInfo(std::ostream & os)1280 void Heap::DumpGcPerformanceInfo(std::ostream& os) {
1281   // Dump cumulative timings.
1282   os << "Dumping cumulative Gc timings\n";
1283   uint64_t total_duration = 0;
1284   // Dump cumulative loggers for each GC type.
1285   uint64_t total_paused_time = 0;
1286   for (auto* collector : garbage_collectors_) {
1287     total_duration += collector->GetCumulativeTimings().GetTotalNs();
1288     total_paused_time += collector->GetTotalPausedTimeNs();
1289     collector->DumpPerformanceInfo(os);
1290   }
1291   if (total_duration != 0) {
1292     const double total_seconds = total_duration / 1.0e9;
1293     const double total_cpu_seconds = GetTotalGcCpuTime() / 1.0e9;
1294     os << "Total time spent in GC: " << PrettyDuration(total_duration) << "\n";
1295     os << "Mean GC size throughput: "
1296        << PrettySize(GetBytesFreedEver() / total_seconds) << "/s"
1297        << " per cpu-time: "
1298        << PrettySize(GetBytesFreedEver() / total_cpu_seconds) << "/s\n";
1299   }
1300   os << "Total bytes allocated " << PrettySize(GetBytesAllocatedEver()) << "\n";
1301   os << "Total bytes freed " << PrettySize(GetBytesFreedEver()) << "\n";
1302   os << "Free memory " << PrettySize(GetFreeMemory()) << "\n";
1303   os << "Free memory until GC " << PrettySize(GetFreeMemoryUntilGC()) << "\n";
1304   os << "Free memory until OOME " << PrettySize(GetFreeMemoryUntilOOME()) << "\n";
1305   os << "Total memory " << PrettySize(GetTotalMemory()) << "\n";
1306   os << "Max memory " << PrettySize(GetMaxMemory()) << "\n";
1307   if (HasZygoteSpace()) {
1308     os << "Zygote space size " << PrettySize(zygote_space_->Size()) << "\n";
1309   }
1310   os << "Total mutator paused time: " << PrettyDuration(total_paused_time) << "\n";
1311   os << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_) << "\n";
1312   os << "Total GC count: " << GetGcCount() << "\n";
1313   os << "Total GC time: " << PrettyDuration(GetGcTime()) << "\n";
1314   os << "Total blocking GC count: " << GetBlockingGcCount() << "\n";
1315   os << "Total blocking GC time: " << PrettyDuration(GetBlockingGcTime()) << "\n";
1316   os << "Total pre-OOME GC count: " << GetPreOomeGcCount() << "\n";
1317   {
1318     MutexLock mu(Thread::Current(), *gc_complete_lock_);
1319     if (gc_count_rate_histogram_.SampleSize() > 0U) {
1320       os << "Histogram of GC count per " << NsToMs(kGcCountRateHistogramWindowDuration) << " ms: ";
1321       gc_count_rate_histogram_.DumpBins(os);
1322       os << "\n";
1323     }
1324     if (blocking_gc_count_rate_histogram_.SampleSize() > 0U) {
1325       os << "Histogram of blocking GC count per "
1326          << NsToMs(kGcCountRateHistogramWindowDuration) << " ms: ";
1327       blocking_gc_count_rate_histogram_.DumpBins(os);
1328       os << "\n";
1329     }
1330   }
1331 
1332   if (kDumpRosAllocStatsOnSigQuit && rosalloc_space_ != nullptr) {
1333     rosalloc_space_->DumpStats(os);
1334   }
1335 
1336   os << "Native bytes total: " << GetNativeBytes()
1337      << " registered: " << native_bytes_registered_.load(std::memory_order_relaxed) << "\n";
1338 
1339   os << "Total native bytes at last GC: "
1340      << old_native_bytes_allocated_.load(std::memory_order_relaxed) << "\n";
1341 
1342   BaseMutex::DumpAll(os);
1343 }
1344 
ResetGcPerformanceInfo()1345 void Heap::ResetGcPerformanceInfo() {
1346   for (auto* collector : garbage_collectors_) {
1347     collector->ResetMeasurements();
1348   }
1349 
1350   process_cpu_start_time_ns_ = ProcessCpuNanoTime();
1351 
1352   pre_gc_last_process_cpu_time_ns_ = process_cpu_start_time_ns_;
1353   pre_gc_weighted_allocated_bytes_ = 0u;
1354 
1355   post_gc_last_process_cpu_time_ns_ = process_cpu_start_time_ns_;
1356   post_gc_weighted_allocated_bytes_ = 0u;
1357 
1358   total_bytes_freed_ever_.store(0);
1359   total_objects_freed_ever_.store(0);
1360   total_wait_time_ = 0;
1361   blocking_gc_count_ = 0;
1362   blocking_gc_time_ = 0;
1363   pre_oome_gc_count_.store(0, std::memory_order_relaxed);
1364   gc_count_last_window_ = 0;
1365   blocking_gc_count_last_window_ = 0;
1366   last_update_time_gc_count_rate_histograms_ =  // Round down by the window duration.
1367       (NanoTime() / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration;
1368   {
1369     MutexLock mu(Thread::Current(), *gc_complete_lock_);
1370     gc_count_rate_histogram_.Reset();
1371     blocking_gc_count_rate_histogram_.Reset();
1372   }
1373 }
1374 
GetGcCount() const1375 uint64_t Heap::GetGcCount() const {
1376   uint64_t gc_count = 0U;
1377   for (auto* collector : garbage_collectors_) {
1378     gc_count += collector->GetCumulativeTimings().GetIterations();
1379   }
1380   return gc_count;
1381 }
1382 
GetGcTime() const1383 uint64_t Heap::GetGcTime() const {
1384   uint64_t gc_time = 0U;
1385   for (auto* collector : garbage_collectors_) {
1386     gc_time += collector->GetCumulativeTimings().GetTotalNs();
1387   }
1388   return gc_time;
1389 }
1390 
GetBlockingGcCount() const1391 uint64_t Heap::GetBlockingGcCount() const {
1392   return blocking_gc_count_;
1393 }
1394 
GetBlockingGcTime() const1395 uint64_t Heap::GetBlockingGcTime() const {
1396   return blocking_gc_time_;
1397 }
1398 
DumpGcCountRateHistogram(std::ostream & os) const1399 void Heap::DumpGcCountRateHistogram(std::ostream& os) const {
1400   MutexLock mu(Thread::Current(), *gc_complete_lock_);
1401   if (gc_count_rate_histogram_.SampleSize() > 0U) {
1402     gc_count_rate_histogram_.DumpBins(os);
1403   }
1404 }
1405 
DumpBlockingGcCountRateHistogram(std::ostream & os) const1406 void Heap::DumpBlockingGcCountRateHistogram(std::ostream& os) const {
1407   MutexLock mu(Thread::Current(), *gc_complete_lock_);
1408   if (blocking_gc_count_rate_histogram_.SampleSize() > 0U) {
1409     blocking_gc_count_rate_histogram_.DumpBins(os);
1410   }
1411 }
1412 
GetPreOomeGcCount() const1413 uint64_t Heap::GetPreOomeGcCount() const {
1414   return pre_oome_gc_count_.load(std::memory_order_relaxed);
1415 }
1416 
1417 ALWAYS_INLINE
GetAndOverwriteAllocationListener(Atomic<AllocationListener * > * storage,AllocationListener * new_value)1418 static inline AllocationListener* GetAndOverwriteAllocationListener(
1419     Atomic<AllocationListener*>* storage, AllocationListener* new_value) {
1420   return storage->exchange(new_value);
1421 }
1422 
~Heap()1423 Heap::~Heap() {
1424   VLOG(heap) << "Starting ~Heap()";
1425   STLDeleteElements(&garbage_collectors_);
1426   // If we don't reset then the mark stack complains in its destructor.
1427   allocation_stack_->Reset();
1428   allocation_records_.reset();
1429   live_stack_->Reset();
1430   STLDeleteValues(&mod_union_tables_);
1431   STLDeleteValues(&remembered_sets_);
1432   STLDeleteElements(&continuous_spaces_);
1433   STLDeleteElements(&discontinuous_spaces_);
1434   delete gc_complete_lock_;
1435   delete thread_flip_lock_;
1436   delete pending_task_lock_;
1437   delete backtrace_lock_;
1438   uint64_t unique_count = unique_backtrace_count_.load();
1439   uint64_t seen_count = seen_backtrace_count_.load();
1440   if (unique_count != 0 || seen_count != 0) {
1441     LOG(INFO) << "gc stress unique=" << unique_count << " total=" << (unique_count + seen_count);
1442   }
1443   VLOG(heap) << "Finished ~Heap()";
1444 }
1445 
1446 
FindContinuousSpaceFromAddress(const mirror::Object * addr) const1447 space::ContinuousSpace* Heap::FindContinuousSpaceFromAddress(const mirror::Object* addr) const {
1448   for (const auto& space : continuous_spaces_) {
1449     if (space->Contains(addr)) {
1450       return space;
1451     }
1452   }
1453   return nullptr;
1454 }
1455 
FindContinuousSpaceFromObject(ObjPtr<mirror::Object> obj,bool fail_ok) const1456 space::ContinuousSpace* Heap::FindContinuousSpaceFromObject(ObjPtr<mirror::Object> obj,
1457                                                             bool fail_ok) const {
1458   space::ContinuousSpace* space = FindContinuousSpaceFromAddress(obj.Ptr());
1459   if (space != nullptr) {
1460     return space;
1461   }
1462   if (!fail_ok) {
1463     LOG(FATAL) << "object " << obj << " not inside any spaces!";
1464   }
1465   return nullptr;
1466 }
1467 
FindDiscontinuousSpaceFromObject(ObjPtr<mirror::Object> obj,bool fail_ok) const1468 space::DiscontinuousSpace* Heap::FindDiscontinuousSpaceFromObject(ObjPtr<mirror::Object> obj,
1469                                                                   bool fail_ok) const {
1470   for (const auto& space : discontinuous_spaces_) {
1471     if (space->Contains(obj.Ptr())) {
1472       return space;
1473     }
1474   }
1475   if (!fail_ok) {
1476     LOG(FATAL) << "object " << obj << " not inside any spaces!";
1477   }
1478   return nullptr;
1479 }
1480 
FindSpaceFromObject(ObjPtr<mirror::Object> obj,bool fail_ok) const1481 space::Space* Heap::FindSpaceFromObject(ObjPtr<mirror::Object> obj, bool fail_ok) const {
1482   space::Space* result = FindContinuousSpaceFromObject(obj, true);
1483   if (result != nullptr) {
1484     return result;
1485   }
1486   return FindDiscontinuousSpaceFromObject(obj, fail_ok);
1487 }
1488 
FindSpaceFromAddress(const void * addr) const1489 space::Space* Heap::FindSpaceFromAddress(const void* addr) const {
1490   for (const auto& space : continuous_spaces_) {
1491     if (space->Contains(reinterpret_cast<const mirror::Object*>(addr))) {
1492       return space;
1493     }
1494   }
1495   for (const auto& space : discontinuous_spaces_) {
1496     if (space->Contains(reinterpret_cast<const mirror::Object*>(addr))) {
1497       return space;
1498     }
1499   }
1500   return nullptr;
1501 }
1502 
DumpSpaceNameFromAddress(const void * addr) const1503 std::string Heap::DumpSpaceNameFromAddress(const void* addr) const {
1504   space::Space* space = FindSpaceFromAddress(addr);
1505   return (space != nullptr) ? space->GetName() : "no space";
1506 }
1507 
ThrowOutOfMemoryError(Thread * self,size_t byte_count,AllocatorType allocator_type)1508 void Heap::ThrowOutOfMemoryError(Thread* self, size_t byte_count, AllocatorType allocator_type) {
1509   // If we're in a stack overflow, do not create a new exception. It would require running the
1510   // constructor, which will of course still be in a stack overflow. Note: we only care if the
1511   // native stack has overflowed. If the simulated stack overflows, it is still possible that the
1512   // native stack has room to create a new exception.
1513   if (self->IsHandlingStackOverflow<kNativeStackType>()) {
1514     self->SetException(
1515         Runtime::Current()->GetPreAllocatedOutOfMemoryErrorWhenHandlingStackOverflow());
1516     return;
1517   }
1518   // Allow plugins to intercept out of memory errors.
1519   Runtime::Current()->OutOfMemoryErrorHook();
1520 
1521   std::ostringstream oss;
1522   size_t total_bytes_free = GetFreeMemory();
1523   oss << "Failed to allocate a " << byte_count << " byte allocation with " << total_bytes_free
1524       << " free bytes and " << PrettySize(GetFreeMemoryUntilOOME()) << " until OOM,"
1525       << " target footprint " << target_footprint_.load(std::memory_order_relaxed)
1526       << ", growth limit "
1527       << growth_limit_;
1528   // If the allocation failed due to fragmentation, print out the largest continuous allocation.
1529   if (total_bytes_free >= byte_count) {
1530     space::AllocSpace* space = nullptr;
1531     if (allocator_type == kAllocatorTypeNonMoving) {
1532       space = non_moving_space_;
1533     } else if (allocator_type == kAllocatorTypeRosAlloc ||
1534                allocator_type == kAllocatorTypeDlMalloc) {
1535       space = main_space_;
1536     } else if (allocator_type == kAllocatorTypeBumpPointer ||
1537                allocator_type == kAllocatorTypeTLAB) {
1538       space = bump_pointer_space_;
1539     } else if (allocator_type == kAllocatorTypeRegion ||
1540                allocator_type == kAllocatorTypeRegionTLAB) {
1541       space = region_space_;
1542     }
1543 
1544     // There is no fragmentation info to log for large-object space.
1545     if (allocator_type != kAllocatorTypeLOS) {
1546       CHECK(space != nullptr) << "allocator_type:" << allocator_type
1547                               << " byte_count:" << byte_count
1548                               << " total_bytes_free:" << total_bytes_free;
1549       // LogFragmentationAllocFailure returns true if byte_count is greater than
1550       // the largest free contiguous chunk in the space. Return value false
1551       // means that we are throwing OOME because the amount of free heap after
1552       // GC is less than kMinFreeHeapAfterGcForAlloc in proportion of the heap-size.
1553       // Log an appropriate message in that case.
1554       if (!space->LogFragmentationAllocFailure(oss, byte_count)) {
1555         oss << "; giving up on allocation because <"
1556             << kMinFreeHeapAfterGcForAlloc * 100
1557             << "% of heap free after GC.";
1558       }
1559     }
1560   }
1561   self->ThrowOutOfMemoryError(oss.str().c_str());
1562 }
1563 
DoPendingCollectorTransition()1564 void Heap::DoPendingCollectorTransition() {
1565   CollectorType desired_collector_type = desired_collector_type_;
1566 
1567   if (collector_type_ == kCollectorTypeCC || collector_type_ == kCollectorTypeCMC) {
1568     // App's allocations (since last GC) more than the threshold then do TransitionGC
1569     // when the app was in background. If not then don't do TransitionGC.
1570     // num_bytes_allocated_since_gc should always be positive even if initially
1571     // num_bytes_alive_after_gc_ is coming from Zygote. This gives positive or zero value.
1572     size_t num_bytes_allocated_since_gc =
1573         UnsignedDifference(GetBytesAllocated(), num_bytes_alive_after_gc_);
1574     if (num_bytes_allocated_since_gc <
1575         (UnsignedDifference(target_footprint_.load(std::memory_order_relaxed),
1576                             num_bytes_alive_after_gc_)/4)
1577         && !kStressCollectorTransition
1578         && !IsLowMemoryMode()) {
1579       return;
1580     }
1581   }
1582 
1583   // Launch homogeneous space compaction if it is desired.
1584   if (desired_collector_type == kCollectorTypeHomogeneousSpaceCompact) {
1585     if (!CareAboutPauseTimes()) {
1586       PerformHomogeneousSpaceCompact();
1587     } else {
1588       VLOG(gc) << "Homogeneous compaction ignored due to jank perceptible process state";
1589     }
1590   } else if (desired_collector_type == kCollectorTypeCCBackground ||
1591              desired_collector_type == kCollectorTypeCMCBackground) {
1592     if (!CareAboutPauseTimes()) {
1593       // Invoke full compaction.
1594       CollectGarbageInternal(collector::kGcTypeFull,
1595                              kGcCauseCollectorTransition,
1596                              /*clear_soft_references=*/false, GetCurrentGcNum() + 1);
1597     } else {
1598       VLOG(gc) << "background compaction ignored due to jank perceptible process state";
1599     }
1600   } else {
1601     CHECK_EQ(desired_collector_type, collector_type_) << "Unsupported collector transition";
1602   }
1603 }
1604 
Trim(Thread * self)1605 void Heap::Trim(Thread* self) {
1606   Runtime* const runtime = Runtime::Current();
1607   if (!CareAboutPauseTimes()) {
1608     // Deflate the monitors, this can cause a pause but shouldn't matter since we don't care
1609     // about pauses.
1610     ScopedTrace trace("Deflating monitors");
1611     // Avoid race conditions on the lock word for CC.
1612     ScopedGCCriticalSection gcs(self, kGcCauseTrim, kCollectorTypeHeapTrim);
1613     ScopedSuspendAll ssa(__FUNCTION__);
1614     uint64_t start_time = NanoTime();
1615     size_t count = runtime->GetMonitorList()->DeflateMonitors();
1616     VLOG(heap) << "Deflating " << count << " monitors took "
1617         << PrettyDuration(NanoTime() - start_time);
1618   }
1619   TrimIndirectReferenceTables(self);
1620   TrimSpaces(self);
1621   // Trim arenas that may have been used by JIT or verifier.
1622   runtime->GetArenaPool()->TrimMaps();
1623 }
1624 
1625 class TrimIndirectReferenceTableClosure : public Closure {
1626  public:
TrimIndirectReferenceTableClosure(Barrier * barrier)1627   explicit TrimIndirectReferenceTableClosure(Barrier* barrier) : barrier_(barrier) {
1628   }
Run(Thread * thread)1629   void Run(Thread* thread) override REQUIRES_SHARED(Locks::mutator_lock_) {
1630     thread->GetJniEnv()->TrimLocals();
1631     // If thread is a running mutator, then act on behalf of the trim thread.
1632     // See the code in ThreadList::RunCheckpoint.
1633     barrier_->Pass(Thread::Current());
1634   }
1635 
1636  private:
1637   Barrier* const barrier_;
1638 };
1639 
TrimIndirectReferenceTables(Thread * self)1640 void Heap::TrimIndirectReferenceTables(Thread* self) {
1641   ScopedObjectAccess soa(self);
1642   ScopedTrace trace(__PRETTY_FUNCTION__);
1643   JavaVMExt* vm = soa.Vm();
1644   // Trim globals indirect reference table.
1645   vm->TrimGlobals();
1646   // Trim locals indirect reference tables.
1647   // TODO: May also want to look for entirely empty pages maintained by SmallIrtAllocator.
1648   Barrier barrier(0);
1649   TrimIndirectReferenceTableClosure closure(&barrier);
1650   size_t barrier_count = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
1651   ScopedThreadStateChange tsc(self, ThreadState::kWaitingForCheckPointsToRun);
1652   if (barrier_count != 0) {
1653     barrier.Increment(self, barrier_count);
1654   }
1655 }
1656 
StartGC(Thread * self,GcCause cause,CollectorType collector_type)1657 void Heap::StartGC(Thread* self, GcCause cause, CollectorType collector_type) {
1658   // This can be called in either kRunnable or suspended states.
1659   // TODO: Consider fixing that?
1660   ThreadState old_thread_state = self->GetState();
1661   if (old_thread_state == ThreadState::kRunnable) {
1662     Locks::mutator_lock_->AssertSharedHeld(self);
1663     // Manually inlining the following call breaks thread-safety analysis.
1664     StartGCRunnable(self, cause, collector_type);
1665     return;
1666   }
1667   Locks::mutator_lock_->AssertNotHeld(self);
1668   self->SetState(ThreadState::kWaitingForGcToComplete);
1669   MutexLock mu(self, *gc_complete_lock_);
1670   WaitForGcToCompleteLocked(cause, self);
1671   collector_type_running_ = collector_type;
1672   last_gc_cause_ = cause;
1673   thread_running_gc_ = self;
1674   self->SetState(old_thread_state);
1675 }
1676 
StartGCRunnable(Thread * self,GcCause cause,CollectorType collector_type)1677 void Heap::StartGCRunnable(Thread* self, GcCause cause, CollectorType collector_type) {
1678   Locks::mutator_lock_->AssertSharedHeld(self);
1679   while (true) {
1680     self->TransitionFromRunnableToSuspended(ThreadState::kWaitingForGcToComplete);
1681     {
1682       MutexLock mu(self, *gc_complete_lock_);
1683       // Ensure there is only one GC at a time.
1684       WaitForGcToCompleteLocked(cause, self);
1685       collector_type_running_ = collector_type;
1686       last_gc_cause_ = cause;
1687       thread_running_gc_ = self;
1688     }
1689     // We have to be careful returning to runnable state, since that could cause us to block.
1690     // That would be bad, since collector_type_running_ is set, and hence no GC is possible in this
1691     // state, allowing deadlock.
1692     if (LIKELY(self->TryTransitionFromSuspendedToRunnable())) {
1693       return;
1694     }
1695     {
1696       MutexLock mu(self, *gc_complete_lock_);
1697       collector_type_running_ = kCollectorTypeNone;
1698       thread_running_gc_ = nullptr;
1699     }
1700     self->TransitionFromSuspendedToRunnable();  // Will handle suspension request and block.
1701   }
1702 }
1703 
TrimSpaces(Thread * self)1704 void Heap::TrimSpaces(Thread* self) {
1705   // Pretend we are doing a GC to prevent background compaction from deleting the space we are
1706   // trimming.
1707   StartGC(self, kGcCauseTrim, kCollectorTypeHeapTrim);
1708   ScopedTrace trace(__PRETTY_FUNCTION__);
1709   const uint64_t start_ns = NanoTime();
1710   // Trim the managed spaces.
1711   uint64_t total_alloc_space_allocated = 0;
1712   uint64_t total_alloc_space_size = 0;
1713   uint64_t managed_reclaimed = 0;
1714   {
1715     ScopedObjectAccess soa(self);
1716     for (const auto& space : continuous_spaces_) {
1717       if (space->IsMallocSpace()) {
1718         gc::space::MallocSpace* malloc_space = space->AsMallocSpace();
1719         if (malloc_space->IsRosAllocSpace() || !CareAboutPauseTimes()) {
1720           // Don't trim dlmalloc spaces if we care about pauses since this can hold the space lock
1721           // for a long period of time.
1722           managed_reclaimed += malloc_space->Trim();
1723         }
1724         total_alloc_space_size += malloc_space->Size();
1725       }
1726     }
1727   }
1728   total_alloc_space_allocated = GetBytesAllocated();
1729   if (large_object_space_ != nullptr) {
1730     total_alloc_space_allocated -= large_object_space_->GetBytesAllocated();
1731   }
1732   if (bump_pointer_space_ != nullptr) {
1733     total_alloc_space_allocated -= bump_pointer_space_->Size();
1734   }
1735   if (region_space_ != nullptr) {
1736     total_alloc_space_allocated -= region_space_->GetBytesAllocated();
1737   }
1738   const float managed_utilization = static_cast<float>(total_alloc_space_allocated) /
1739       static_cast<float>(total_alloc_space_size);
1740   uint64_t gc_heap_end_ns = NanoTime();
1741   // We never move things in the native heap, so we can finish the GC at this point.
1742   FinishGC(self, collector::kGcTypeNone);
1743 
1744   VLOG(heap) << "Heap trim of managed (duration=" << PrettyDuration(gc_heap_end_ns - start_ns)
1745       << ", advised=" << PrettySize(managed_reclaimed) << ") heap. Managed heap utilization of "
1746       << static_cast<int>(100 * managed_utilization) << "%.";
1747 }
1748 
IsValidObjectAddress(const void * addr) const1749 bool Heap::IsValidObjectAddress(const void* addr) const {
1750   if (addr == nullptr) {
1751     return true;
1752   }
1753   return IsAligned<kObjectAlignment>(addr) && FindSpaceFromAddress(addr) != nullptr;
1754 }
1755 
IsNonDiscontinuousSpaceHeapAddress(const void * addr) const1756 bool Heap::IsNonDiscontinuousSpaceHeapAddress(const void* addr) const {
1757   return FindContinuousSpaceFromAddress(reinterpret_cast<const mirror::Object*>(addr)) != nullptr;
1758 }
1759 
IsLiveObjectLocked(ObjPtr<mirror::Object> obj,bool search_allocation_stack,bool search_live_stack,bool sorted)1760 bool Heap::IsLiveObjectLocked(ObjPtr<mirror::Object> obj,
1761                               bool search_allocation_stack,
1762                               bool search_live_stack,
1763                               bool sorted) {
1764   if (UNLIKELY(!IsAligned<kObjectAlignment>(obj.Ptr()))) {
1765     return false;
1766   }
1767   if (bump_pointer_space_ != nullptr && bump_pointer_space_->HasAddress(obj.Ptr())) {
1768     mirror::Class* klass = obj->GetClass<kVerifyNone>();
1769     if (obj == klass) {
1770       // This case happens for java.lang.Class.
1771       return true;
1772     }
1773     return VerifyClassClass(klass) && IsLiveObjectLocked(klass);
1774   } else if (temp_space_ != nullptr && temp_space_->HasAddress(obj.Ptr())) {
1775     // If we are in the allocated region of the temp space, then we are probably live (e.g. during
1776     // a GC). When a GC isn't running End() - Begin() is 0 which means no objects are contained.
1777     return temp_space_->Contains(obj.Ptr());
1778   }
1779   if (region_space_ != nullptr && region_space_->HasAddress(obj.Ptr())) {
1780     return true;
1781   }
1782   space::ContinuousSpace* c_space = FindContinuousSpaceFromObject(obj, true);
1783   space::DiscontinuousSpace* d_space = nullptr;
1784   if (c_space != nullptr) {
1785     if (c_space->GetLiveBitmap()->Test(obj.Ptr())) {
1786       return true;
1787     }
1788   } else {
1789     d_space = FindDiscontinuousSpaceFromObject(obj, true);
1790     if (d_space != nullptr) {
1791       if (d_space->GetLiveBitmap()->Test(obj.Ptr())) {
1792         return true;
1793       }
1794     }
1795   }
1796   // This is covering the allocation/live stack swapping that is done without mutators suspended.
1797   for (size_t i = 0; i < (sorted ? 1 : 5); ++i) {
1798     if (i > 0) {
1799       NanoSleep(MsToNs(10));
1800     }
1801     if (search_allocation_stack) {
1802       if (sorted) {
1803         if (allocation_stack_->ContainsSorted(obj.Ptr())) {
1804           return true;
1805         }
1806       } else if (allocation_stack_->Contains(obj.Ptr())) {
1807         return true;
1808       }
1809     }
1810 
1811     if (search_live_stack) {
1812       if (sorted) {
1813         if (live_stack_->ContainsSorted(obj.Ptr())) {
1814           return true;
1815         }
1816       } else if (live_stack_->Contains(obj.Ptr())) {
1817         return true;
1818       }
1819     }
1820   }
1821   // We need to check the bitmaps again since there is a race where we mark something as live and
1822   // then clear the stack containing it.
1823   if (c_space != nullptr) {
1824     if (c_space->GetLiveBitmap()->Test(obj.Ptr())) {
1825       return true;
1826     }
1827   } else {
1828     d_space = FindDiscontinuousSpaceFromObject(obj, true);
1829     if (d_space != nullptr && d_space->GetLiveBitmap()->Test(obj.Ptr())) {
1830       return true;
1831     }
1832   }
1833   return false;
1834 }
1835 
DumpSpaces() const1836 std::string Heap::DumpSpaces() const {
1837   std::ostringstream oss;
1838   DumpSpaces(oss);
1839   return oss.str();
1840 }
1841 
DumpSpaces(std::ostream & stream) const1842 void Heap::DumpSpaces(std::ostream& stream) const {
1843   for (const auto& space : continuous_spaces_) {
1844     accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
1845     accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap();
1846     stream << space << " " << *space << "\n";
1847     if (live_bitmap != nullptr) {
1848       stream << live_bitmap << " " << *live_bitmap << "\n";
1849     }
1850     if (mark_bitmap != nullptr) {
1851       stream << mark_bitmap << " " << *mark_bitmap << "\n";
1852     }
1853   }
1854   for (const auto& space : discontinuous_spaces_) {
1855     stream << space << " " << *space << "\n";
1856   }
1857 }
1858 
VerifyObjectBody(ObjPtr<mirror::Object> obj)1859 void Heap::VerifyObjectBody(ObjPtr<mirror::Object> obj) {
1860   if (verify_object_mode_ == kVerifyObjectModeDisabled) {
1861     return;
1862   }
1863 
1864   // Ignore early dawn of the universe verifications.
1865   if (UNLIKELY(num_bytes_allocated_.load(std::memory_order_relaxed) < 10 * KB)) {
1866     return;
1867   }
1868   CHECK_ALIGNED(obj.Ptr(), kObjectAlignment) << "Object isn't aligned";
1869   mirror::Class* c = obj->GetFieldObject<mirror::Class, kVerifyNone>(mirror::Object::ClassOffset());
1870   CHECK(c != nullptr) << "Null class in object " << obj;
1871   CHECK_ALIGNED(c, kObjectAlignment) << "Class " << c << " not aligned in object " << obj;
1872   CHECK(VerifyClassClass(c));
1873 
1874   if (verify_object_mode_ > kVerifyObjectModeFast) {
1875     // Note: the bitmap tests below are racy since we don't hold the heap bitmap lock.
1876     CHECK(IsLiveObjectLocked(obj)) << "Object is dead " << obj << "\n" << DumpSpaces();
1877   }
1878 }
1879 
VerifyHeap()1880 void Heap::VerifyHeap() {
1881   ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
1882   auto visitor = [&](mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1883     VerifyObjectBody(obj);
1884   };
1885   // Technically we need the mutator lock here to call Visit. However, VerifyObjectBody is already
1886   // NO_THREAD_SAFETY_ANALYSIS.
1887   auto no_thread_safety_analysis = [&]() NO_THREAD_SAFETY_ANALYSIS {
1888     GetLiveBitmap()->Visit(visitor);
1889   };
1890   no_thread_safety_analysis();
1891 }
1892 
RecordFree(uint64_t freed_objects,int64_t freed_bytes)1893 void Heap::RecordFree(uint64_t freed_objects, int64_t freed_bytes) {
1894   // Use signed comparison since freed bytes can be negative when background compaction foreground
1895   // transitions occurs. This is typically due to objects moving from a bump pointer space to a
1896   // free list backed space, which may increase memory footprint due to padding and binning.
1897   RACING_DCHECK_LE(freed_bytes,
1898                    static_cast<int64_t>(num_bytes_allocated_.load(std::memory_order_relaxed)));
1899   // Note: This relies on 2s complement for handling negative freed_bytes.
1900   num_bytes_allocated_.fetch_sub(static_cast<ssize_t>(freed_bytes), std::memory_order_relaxed);
1901   if (Runtime::Current()->HasStatsEnabled()) {
1902     RuntimeStats* thread_stats = Thread::Current()->GetStats();
1903     thread_stats->freed_objects += freed_objects;
1904     thread_stats->freed_bytes += freed_bytes;
1905     // TODO: Do this concurrently.
1906     RuntimeStats* global_stats = Runtime::Current()->GetStats();
1907     global_stats->freed_objects += freed_objects;
1908     global_stats->freed_bytes += freed_bytes;
1909   }
1910 }
1911 
RecordFreeRevoke()1912 void Heap::RecordFreeRevoke() {
1913   // Subtract num_bytes_freed_revoke_ from num_bytes_allocated_ to cancel out the
1914   // ahead-of-time, bulk counting of bytes allocated in rosalloc thread-local buffers.
1915   // If there's a concurrent revoke, ok to not necessarily reset num_bytes_freed_revoke_
1916   // all the way to zero exactly as the remainder will be subtracted at the next GC.
1917   size_t bytes_freed = num_bytes_freed_revoke_.load(std::memory_order_relaxed);
1918   CHECK_GE(num_bytes_freed_revoke_.fetch_sub(bytes_freed, std::memory_order_relaxed),
1919            bytes_freed) << "num_bytes_freed_revoke_ underflow";
1920   CHECK_GE(num_bytes_allocated_.fetch_sub(bytes_freed, std::memory_order_relaxed),
1921            bytes_freed) << "num_bytes_allocated_ underflow";
1922   GetCurrentGcIteration()->SetFreedRevoke(bytes_freed);
1923 }
1924 
GetRosAllocSpace(gc::allocator::RosAlloc * rosalloc) const1925 space::RosAllocSpace* Heap::GetRosAllocSpace(gc::allocator::RosAlloc* rosalloc) const {
1926   if (rosalloc_space_ != nullptr && rosalloc_space_->GetRosAlloc() == rosalloc) {
1927     return rosalloc_space_;
1928   }
1929   for (const auto& space : continuous_spaces_) {
1930     if (space->AsContinuousSpace()->IsRosAllocSpace()) {
1931       if (space->AsContinuousSpace()->AsRosAllocSpace()->GetRosAlloc() == rosalloc) {
1932         return space->AsContinuousSpace()->AsRosAllocSpace();
1933       }
1934     }
1935   }
1936   return nullptr;
1937 }
1938 
EntrypointsInstrumented()1939 static inline bool EntrypointsInstrumented() REQUIRES_SHARED(Locks::mutator_lock_) {
1940   instrumentation::Instrumentation* const instrumentation =
1941       Runtime::Current()->GetInstrumentation();
1942   return instrumentation != nullptr && instrumentation->AllocEntrypointsInstrumented();
1943 }
1944 
AllocateInternalWithGc(Thread * self,AllocatorType allocator,bool instrumented,size_t alloc_size,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated,ObjPtr<mirror::Class> * klass)1945 mirror::Object* Heap::AllocateInternalWithGc(Thread* self,
1946                                              AllocatorType allocator,
1947                                              bool instrumented,
1948                                              size_t alloc_size,
1949                                              size_t* bytes_allocated,
1950                                              size_t* usable_size,
1951                                              size_t* bytes_tl_bulk_allocated,
1952                                              ObjPtr<mirror::Class>* klass) {
1953   bool was_default_allocator = allocator == GetCurrentAllocator();
1954   // Make sure there is no pending exception since we may need to throw an OOME.
1955   self->AssertNoPendingException();
1956   DCHECK(klass != nullptr);
1957 
1958   StackHandleScope<1> hs(self);
1959   HandleWrapperObjPtr<mirror::Class> h_klass(hs.NewHandleWrapper(klass));
1960 
1961   auto send_object_pre_alloc =
1962       [&]() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_) {
1963         if (UNLIKELY(instrumented)) {
1964           AllocationListener* l = alloc_listener_.load(std::memory_order_seq_cst);
1965           if (UNLIKELY(l != nullptr) && UNLIKELY(l->HasPreAlloc())) {
1966             l->PreObjectAllocated(self, h_klass, &alloc_size);
1967           }
1968         }
1969       };
1970 #define PERFORM_SUSPENDING_OPERATION(op)                                          \
1971   [&]() REQUIRES(Roles::uninterruptible_) REQUIRES_SHARED(Locks::mutator_lock_) { \
1972     ScopedAllowThreadSuspension ats;                                              \
1973     auto res = (op);                                                              \
1974     send_object_pre_alloc();                                                      \
1975     return res;                                                                   \
1976   }()
1977 
1978   // The allocation failed. If the GC is running, block until it completes, and then retry the
1979   // allocation.
1980   collector::GcType last_gc =
1981       PERFORM_SUSPENDING_OPERATION(WaitForGcToComplete(kGcCauseForAlloc, self));
1982   // If we were the default allocator but the allocator changed while we were suspended,
1983   // abort the allocation.
1984   if ((was_default_allocator && allocator != GetCurrentAllocator()) ||
1985       (!instrumented && EntrypointsInstrumented())) {
1986     return nullptr;
1987   }
1988   uint32_t starting_gc_num = GetCurrentGcNum();
1989   if (last_gc != collector::kGcTypeNone) {
1990     // A GC was in progress and we blocked, retry allocation now that memory has been freed.
1991     mirror::Object* ptr = TryToAllocate<true, false>(self, allocator, alloc_size, bytes_allocated,
1992                                                      usable_size, bytes_tl_bulk_allocated);
1993     if (ptr != nullptr) {
1994       return ptr;
1995     }
1996   }
1997   if (IsGCDisabledForShutdown()) {
1998     // We're just shutting down and GCs don't work anymore. Try a different allocator.
1999     mirror::Object* ptr = TryToAllocate<true, false>(self,
2000                                                      kAllocatorTypeNonMoving,
2001                                                      alloc_size,
2002                                                      bytes_allocated,
2003                                                      usable_size,
2004                                                      bytes_tl_bulk_allocated);
2005     if (ptr != nullptr) {
2006       return ptr;
2007     }
2008   }
2009 
2010   int64_t bytes_freed_before = GetBytesFreedEver();
2011   auto have_reclaimed_enough = [&]() {
2012     size_t curr_bytes_allocated = GetBytesAllocated();
2013     size_t free_heap = UnsignedDifference(growth_limit_, curr_bytes_allocated);
2014     int64_t newly_freed = GetBytesFreedEver() - bytes_freed_before;
2015     double free_heap_ratio = static_cast<double>(free_heap) / growth_limit_;
2016     double newly_freed_ratio = static_cast<double>(newly_freed) / growth_limit_;
2017     return free_heap_ratio >= kMinFreeHeapAfterGcForAlloc ||
2018            newly_freed_ratio >= kMinFreedHeapAfterGcForAlloc;
2019   };
2020   // We perform one GC as per the next_gc_type_ (chosen in GrowForUtilization),
2021   // if it's not already tried. If that doesn't succeed then go for the most
2022   // exhaustive option. Perform a full-heap collection including clearing
2023   // SoftReferences. In case of ConcurrentCopying, it will also ensure that
2024   // all regions are evacuated. If allocation doesn't succeed even after that
2025   // then there is no hope, so we throw OOME.
2026   collector::GcType tried_type = next_gc_type_;
2027   if (last_gc < tried_type) {
2028     VLOG(gc) << "Starting a blocking GC " << kGcCauseForAlloc;
2029     PERFORM_SUSPENDING_OPERATION(
2030         CollectGarbageInternal(tried_type, kGcCauseForAlloc, false, starting_gc_num + 1));
2031 
2032     if ((was_default_allocator && allocator != GetCurrentAllocator()) ||
2033         (!instrumented && EntrypointsInstrumented())) {
2034       return nullptr;
2035     }
2036     // Check this even if we didn't actually run a GC; if we didn't someone else probably did.
2037     if (have_reclaimed_enough()) {
2038       mirror::Object* ptr = TryToAllocate<true, false>(self, allocator,
2039                                                        alloc_size, bytes_allocated,
2040                                                        usable_size, bytes_tl_bulk_allocated);
2041       if (ptr != nullptr) {
2042         return ptr;
2043       }
2044     }
2045   }
2046   // Most allocations should have succeeded by now, so the heap is really full, really fragmented,
2047   // or the requested size is really big. Do another GC, collecting SoftReferences this time. The
2048   // VM spec requires that all SoftReferences have been collected and cleared before throwing
2049   // OOME.
2050   VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size)
2051            << " allocation";
2052   // TODO: Run finalization, but this may cause more allocations to occur.
2053   // We don't need a WaitForGcToComplete here either.
2054   // TODO: Should check whether another thread already just ran a GC with soft
2055   // references.
2056 
2057   DCHECK(!gc_plan_.empty());
2058 
2059   int64_t min_freed_to_continue =
2060       static_cast<int64_t>(kMinFreedHeapAfterGcForAlloc * growth_limit_ + alloc_size);
2061   // Repeatedly collect the entire heap until either
2062   // (a) this was insufficiently productive at reclaiming memory and we should give upt to avoid
2063   // "GC thrashing", or
2064   // (b) GC was sufficiently productive (reclaimed min_freed_to_continue bytes) AND allowed us to
2065   // satisfy the allocation request.
2066   bool gc_ran;
2067   int gc_attempts = 0;
2068   // A requested GC can fail to run because either someone else beat us to it, or because we can't
2069   // run a GC in this state. In the latter case, we return quickly. Just try a small number of
2070   // times.
2071   static constexpr int kMaxGcAttempts = 5;
2072   do {
2073     bytes_freed_before = GetBytesFreedEver();
2074     pre_oome_gc_count_.fetch_add(1, std::memory_order_relaxed);
2075     // TODO(b/353333767): Do this only if nobody else beats us to it. If we're having trouble
2076     // allocating, probably other threads are in the same boat.
2077     starting_gc_num = GetCurrentGcNum();
2078     gc_ran = PERFORM_SUSPENDING_OPERATION(
2079         CollectGarbageInternal(gc_plan_.back(), kGcCauseForAlloc, true, starting_gc_num + 1) !=
2080         collector::kGcTypeNone);
2081     ++gc_attempts;
2082     if ((was_default_allocator && allocator != GetCurrentAllocator()) ||
2083         (!instrumented && EntrypointsInstrumented())) {
2084       return nullptr;
2085     }
2086     bool ran_homogeneous_space_compaction = false;
2087     bool immediately_reclaimed_enough = have_reclaimed_enough();
2088     if (!immediately_reclaimed_enough) {
2089       const uint64_t current_time = NanoTime();
2090       if (allocator == kAllocatorTypeRosAlloc || allocator == kAllocatorTypeDlMalloc) {
2091         if (use_homogeneous_space_compaction_for_oom_ &&
2092             current_time - last_time_homogeneous_space_compaction_by_oom_ >
2093             min_interval_homogeneous_space_compaction_by_oom_) {
2094           last_time_homogeneous_space_compaction_by_oom_ = current_time;
2095           ran_homogeneous_space_compaction =
2096               (PERFORM_SUSPENDING_OPERATION(PerformHomogeneousSpaceCompact()) ==
2097                HomogeneousSpaceCompactResult::kSuccess);
2098           // Thread suspension could have occurred.
2099           if ((was_default_allocator && allocator != GetCurrentAllocator()) ||
2100               (!instrumented && EntrypointsInstrumented())) {
2101             return nullptr;
2102           }
2103           // Always print that we ran homogeneous space compation since this can cause jank.
2104           VLOG(heap) << "Ran heap homogeneous space compaction, "
2105                     << " requested defragmentation "
2106                     << count_requested_homogeneous_space_compaction_.load()
2107                     << " performed defragmentation "
2108                     << count_performed_homogeneous_space_compaction_.load()
2109                     << " ignored homogeneous space compaction "
2110                     << count_ignored_homogeneous_space_compaction_.load()
2111                     << " delayed count = "
2112                     << count_delayed_oom_.load();
2113         }
2114       }
2115     }
2116     if (immediately_reclaimed_enough ||
2117         (ran_homogeneous_space_compaction && have_reclaimed_enough())) {
2118       mirror::Object* ptr = TryToAllocate<true, true>(
2119           self, allocator, alloc_size, bytes_allocated, usable_size, bytes_tl_bulk_allocated);
2120       if (ptr != nullptr) {
2121         if (ran_homogeneous_space_compaction) {
2122           count_delayed_oom_++;
2123         }
2124         return ptr;
2125       }
2126     }
2127     // This loops only if we reclaimed plenty of memory, but presumably some other thread beat us
2128     // to allocating it. In the very unlikely case that we're running into a serious fragmentation
2129     // issue, and there is no other thread allocating, GCs will quickly become unsuccessful, and we
2130     // will stop then. If another thread is allocating aggressively, this may go on for a while,
2131     // but we are still making progress somewhere.
2132   } while ((!gc_ran && gc_attempts < kMaxGcAttempts) ||
2133            GetBytesFreedEver() - bytes_freed_before > min_freed_to_continue);
2134 #undef PERFORM_SUSPENDING_OPERATION
2135   // Throw an OOM error.
2136   {
2137     ScopedAllowThreadSuspension ats;
2138     ThrowOutOfMemoryError(self, alloc_size, allocator);
2139   }
2140   return nullptr;
2141 }
2142 
SetTargetHeapUtilization(float target)2143 void Heap::SetTargetHeapUtilization(float target) {
2144   DCHECK_GT(target, 0.1f);  // asserted in Java code
2145   DCHECK_LT(target, 1.0f);
2146   target_utilization_ = target;
2147 }
2148 
GetObjectsAllocated() const2149 size_t Heap::GetObjectsAllocated() const {
2150   Thread* const self = Thread::Current();
2151   ScopedThreadStateChange tsc(self, ThreadState::kWaitingForGetObjectsAllocated);
2152   // Prevent GC running during GetObjectsAllocated since we may get a checkpoint request that tells
2153   // us to suspend while we are doing SuspendAll. b/35232978
2154   gc::ScopedGCCriticalSection gcs(Thread::Current(),
2155                                   gc::kGcCauseGetObjectsAllocated,
2156                                   gc::kCollectorTypeGetObjectsAllocated);
2157   // Need SuspendAll here to prevent lock violation if RosAlloc does it during InspectAll.
2158   ScopedSuspendAll ssa(__FUNCTION__);
2159   ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
2160   size_t total = 0;
2161   for (space::AllocSpace* space : alloc_spaces_) {
2162     total += space->GetObjectsAllocated();
2163   }
2164   return total;
2165 }
2166 
GetBytesAllocatedEver() const2167 uint64_t Heap::GetBytesAllocatedEver() const {
2168   // Force the returned value to be monotonically increasing, in the sense that if this is called
2169   // at A and B, such that A happens-before B, then the call at B returns a value no smaller than
2170   // that at A. This is not otherwise guaranteed, since num_bytes_allocated_ is decremented first,
2171   // and total_bytes_freed_ever_ is incremented later.
2172   static std::atomic<uint64_t> max_bytes_so_far(0);
2173   uint64_t so_far = max_bytes_so_far.load(std::memory_order_relaxed);
2174   uint64_t current_bytes = GetBytesFreedEver(std::memory_order_acquire) + GetBytesAllocated();
2175   DCHECK(current_bytes < (static_cast<uint64_t>(1) << 63));  // result is "positive".
2176   do {
2177     if (current_bytes <= so_far) {
2178       return so_far;
2179     }
2180   } while (!max_bytes_so_far.compare_exchange_weak(so_far /* updated */,
2181                                                    current_bytes, std::memory_order_relaxed));
2182   return current_bytes;
2183 }
2184 
2185 // Check whether the given object is an instance of the given class.
MatchesClass(mirror::Object * obj,Handle<mirror::Class> h_class,bool use_is_assignable_from)2186 static bool MatchesClass(mirror::Object* obj,
2187                          Handle<mirror::Class> h_class,
2188                          bool use_is_assignable_from) REQUIRES_SHARED(Locks::mutator_lock_) {
2189   mirror::Class* instance_class = obj->GetClass();
2190   CHECK(instance_class != nullptr);
2191   ObjPtr<mirror::Class> klass = h_class.Get();
2192   if (use_is_assignable_from) {
2193     return klass != nullptr && klass->IsAssignableFrom(instance_class);
2194   }
2195   return instance_class == klass;
2196 }
2197 
CountInstances(const std::vector<Handle<mirror::Class>> & classes,bool use_is_assignable_from,uint64_t * counts)2198 void Heap::CountInstances(const std::vector<Handle<mirror::Class>>& classes,
2199                           bool use_is_assignable_from,
2200                           uint64_t* counts) {
2201   auto instance_counter = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2202     for (size_t i = 0; i < classes.size(); ++i) {
2203       if (MatchesClass(obj, classes[i], use_is_assignable_from)) {
2204         ++counts[i];
2205       }
2206     }
2207   };
2208   VisitObjects(instance_counter);
2209 }
2210 
CollectGarbage(bool clear_soft_references,GcCause cause)2211 void Heap::CollectGarbage(bool clear_soft_references, GcCause cause) {
2212   // Even if we waited for a GC we still need to do another GC since weaks allocated during the
2213   // last GC will not have necessarily been cleared.
2214   CollectGarbageInternal(gc_plan_.back(), cause, clear_soft_references, GC_NUM_ANY);
2215 }
2216 
SupportHomogeneousSpaceCompactAndCollectorTransitions() const2217 bool Heap::SupportHomogeneousSpaceCompactAndCollectorTransitions() const {
2218   return main_space_backup_.get() != nullptr && main_space_ != nullptr &&
2219       foreground_collector_type_ == kCollectorTypeCMS;
2220 }
2221 
PerformHomogeneousSpaceCompact()2222 HomogeneousSpaceCompactResult Heap::PerformHomogeneousSpaceCompact() {
2223   Thread* self = Thread::Current();
2224   // Inc requested homogeneous space compaction.
2225   count_requested_homogeneous_space_compaction_++;
2226   // Store performed homogeneous space compaction at a new request arrival.
2227   ScopedThreadStateChange tsc(self, ThreadState::kWaitingPerformingGc);
2228   Locks::mutator_lock_->AssertNotHeld(self);
2229   {
2230     ScopedThreadStateChange tsc2(self, ThreadState::kWaitingForGcToComplete);
2231     MutexLock mu(self, *gc_complete_lock_);
2232     // Ensure there is only one GC at a time.
2233     WaitForGcToCompleteLocked(kGcCauseHomogeneousSpaceCompact, self);
2234     // Homogeneous space compaction is a copying transition, can't run it if the moving GC disable
2235     // count is non zero.
2236     // If the collector type changed to something which doesn't benefit from homogeneous space
2237     // compaction, exit.
2238     if (disable_moving_gc_count_ != 0 || IsMovingGc(collector_type_) ||
2239         !main_space_->CanMoveObjects()) {
2240       return kErrorReject;
2241     }
2242     if (!SupportHomogeneousSpaceCompactAndCollectorTransitions()) {
2243       return kErrorUnsupported;
2244     }
2245     collector_type_running_ = kCollectorTypeHomogeneousSpaceCompact;
2246   }
2247   if (Runtime::Current()->IsShuttingDown(self)) {
2248     // Don't allow heap transitions to happen if the runtime is shutting down since these can
2249     // cause objects to get finalized.
2250     FinishGC(self, collector::kGcTypeNone);
2251     return HomogeneousSpaceCompactResult::kErrorVMShuttingDown;
2252   }
2253   collector::GarbageCollector* collector;
2254   {
2255     ScopedSuspendAll ssa(__FUNCTION__);
2256     uint64_t start_time = NanoTime();
2257     // Launch compaction.
2258     space::MallocSpace* to_space = main_space_backup_.release();
2259     space::MallocSpace* from_space = main_space_;
2260     to_space->GetMemMap()->Protect(PROT_READ | PROT_WRITE);
2261     const uint64_t space_size_before_compaction = from_space->Size();
2262     AddSpace(to_space);
2263     // Make sure that we will have enough room to copy.
2264     CHECK_GE(to_space->GetFootprintLimit(), from_space->GetFootprintLimit());
2265     collector = Compact(to_space, from_space, kGcCauseHomogeneousSpaceCompact);
2266     const uint64_t space_size_after_compaction = to_space->Size();
2267     main_space_ = to_space;
2268     main_space_backup_.reset(from_space);
2269     RemoveSpace(from_space);
2270     SetSpaceAsDefault(main_space_);  // Set as default to reset the proper dlmalloc space.
2271     // Update performed homogeneous space compaction count.
2272     count_performed_homogeneous_space_compaction_++;
2273     // Print statics log and resume all threads.
2274     uint64_t duration = NanoTime() - start_time;
2275     VLOG(heap) << "Heap homogeneous space compaction took " << PrettyDuration(duration) << " size: "
2276                << PrettySize(space_size_before_compaction) << " -> "
2277                << PrettySize(space_size_after_compaction) << " compact-ratio: "
2278                << std::fixed << static_cast<double>(space_size_after_compaction) /
2279                static_cast<double>(space_size_before_compaction);
2280   }
2281   // Finish GC.
2282   // Get the references we need to enqueue.
2283   SelfDeletingTask* clear = reference_processor_->CollectClearedReferences(self);
2284   GrowForUtilization(semi_space_collector_);
2285   LogGC(kGcCauseHomogeneousSpaceCompact, collector);
2286   FinishGC(self, collector::kGcTypeFull);
2287   // Enqueue any references after losing the GC locks.
2288   clear->Run(self);
2289   clear->Finalize();
2290   {
2291     ScopedObjectAccess soa(self);
2292     soa.Vm()->UnloadNativeLibraries();
2293   }
2294   return HomogeneousSpaceCompactResult::kSuccess;
2295 }
2296 
SetDefaultConcurrentStartBytes()2297 void Heap::SetDefaultConcurrentStartBytes() {
2298   MutexLock mu(Thread::Current(), *gc_complete_lock_);
2299   if (collector_type_running_ != kCollectorTypeNone) {
2300     // If a collector is already running, just let it set concurrent_start_bytes_ .
2301     return;
2302   }
2303   SetDefaultConcurrentStartBytesLocked();
2304 }
2305 
SetDefaultConcurrentStartBytesLocked()2306 void Heap::SetDefaultConcurrentStartBytesLocked() {
2307   if (IsGcConcurrent()) {
2308     size_t target_footprint = target_footprint_.load(std::memory_order_relaxed);
2309     size_t reserve_bytes = target_footprint / 4;
2310     reserve_bytes = std::min(reserve_bytes, kMaxConcurrentRemainingBytes);
2311     reserve_bytes = std::max(reserve_bytes, kMinConcurrentRemainingBytes);
2312     concurrent_start_bytes_ = UnsignedDifference(target_footprint, reserve_bytes);
2313   } else {
2314     concurrent_start_bytes_ = std::numeric_limits<size_t>::max();
2315   }
2316 }
2317 
ChangeCollector(CollectorType collector_type)2318 void Heap::ChangeCollector(CollectorType collector_type) {
2319   // TODO: Only do this with all mutators suspended to avoid races.
2320   if (collector_type != collector_type_) {
2321     collector_type_ = collector_type;
2322     gc_plan_.clear();
2323     switch (collector_type_) {
2324       case kCollectorTypeCC: {
2325         if (use_generational_gc_) {
2326           gc_plan_.push_back(collector::kGcTypeSticky);
2327         }
2328         gc_plan_.push_back(collector::kGcTypeFull);
2329         if (use_tlab_) {
2330           ChangeAllocator(kAllocatorTypeRegionTLAB);
2331         } else {
2332           ChangeAllocator(kAllocatorTypeRegion);
2333         }
2334         break;
2335       }
2336       case kCollectorTypeCMC: {
2337         if (use_generational_gc_) {
2338           gc_plan_.push_back(collector::kGcTypeSticky);
2339         }
2340         gc_plan_.push_back(collector::kGcTypeFull);
2341         if (use_tlab_) {
2342           ChangeAllocator(kAllocatorTypeTLAB);
2343         } else {
2344           ChangeAllocator(kAllocatorTypeBumpPointer);
2345         }
2346         break;
2347       }
2348       case kCollectorTypeSS: {
2349         gc_plan_.push_back(collector::kGcTypeFull);
2350         if (use_tlab_) {
2351           ChangeAllocator(kAllocatorTypeTLAB);
2352         } else {
2353           ChangeAllocator(kAllocatorTypeBumpPointer);
2354         }
2355         break;
2356       }
2357       case kCollectorTypeMS: {
2358         gc_plan_.push_back(collector::kGcTypeSticky);
2359         gc_plan_.push_back(collector::kGcTypePartial);
2360         gc_plan_.push_back(collector::kGcTypeFull);
2361         ChangeAllocator(kUseRosAlloc ? kAllocatorTypeRosAlloc : kAllocatorTypeDlMalloc);
2362         break;
2363       }
2364       case kCollectorTypeCMS: {
2365         gc_plan_.push_back(collector::kGcTypeSticky);
2366         gc_plan_.push_back(collector::kGcTypePartial);
2367         gc_plan_.push_back(collector::kGcTypeFull);
2368         ChangeAllocator(kUseRosAlloc ? kAllocatorTypeRosAlloc : kAllocatorTypeDlMalloc);
2369         break;
2370       }
2371       default: {
2372         UNIMPLEMENTED(FATAL);
2373         UNREACHABLE();
2374       }
2375     }
2376     SetDefaultConcurrentStartBytesLocked();
2377   }
2378 }
2379 
2380 // Special compacting collector which uses sub-optimal bin packing to reduce zygote space size.
2381 class ZygoteCompactingCollector final : public collector::SemiSpace {
2382  public:
ZygoteCompactingCollector(gc::Heap * heap,bool is_running_on_memory_tool)2383   ZygoteCompactingCollector(gc::Heap* heap, bool is_running_on_memory_tool)
2384       : SemiSpace(heap, "zygote collector"),
2385         bin_live_bitmap_(nullptr),
2386         bin_mark_bitmap_(nullptr),
2387         is_running_on_memory_tool_(is_running_on_memory_tool) {}
2388 
BuildBins(space::ContinuousSpace * space)2389   void BuildBins(space::ContinuousSpace* space) REQUIRES_SHARED(Locks::mutator_lock_) {
2390     bin_live_bitmap_ = space->GetLiveBitmap();
2391     bin_mark_bitmap_ = space->GetMarkBitmap();
2392     uintptr_t prev = reinterpret_cast<uintptr_t>(space->Begin());
2393     WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
2394     // Note: This requires traversing the space in increasing order of object addresses.
2395     auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2396       uintptr_t object_addr = reinterpret_cast<uintptr_t>(obj);
2397       size_t bin_size = object_addr - prev;
2398       // Add the bin consisting of the end of the previous object to the start of the current object.
2399       AddBin(bin_size, prev);
2400       prev = object_addr + RoundUp(obj->SizeOf<kDefaultVerifyFlags>(), kObjectAlignment);
2401     };
2402     bin_live_bitmap_->Walk(visitor);
2403     // Add the last bin which spans after the last object to the end of the space.
2404     AddBin(reinterpret_cast<uintptr_t>(space->End()) - prev, prev);
2405   }
2406 
2407  private:
2408   // Maps from bin sizes to locations.
2409   std::multimap<size_t, uintptr_t> bins_;
2410   // Live bitmap of the space which contains the bins.
2411   accounting::ContinuousSpaceBitmap* bin_live_bitmap_;
2412   // Mark bitmap of the space which contains the bins.
2413   accounting::ContinuousSpaceBitmap* bin_mark_bitmap_;
2414   const bool is_running_on_memory_tool_;
2415 
AddBin(size_t size,uintptr_t position)2416   void AddBin(size_t size, uintptr_t position) {
2417     if (is_running_on_memory_tool_) {
2418       MEMORY_TOOL_MAKE_DEFINED(reinterpret_cast<void*>(position), size);
2419     }
2420     if (size != 0) {
2421       bins_.insert(std::make_pair(size, position));
2422     }
2423   }
2424 
ShouldSweepSpace(space::ContinuousSpace * space) const2425   bool ShouldSweepSpace([[maybe_unused]] space::ContinuousSpace* space) const override {
2426     // Don't sweep any spaces since we probably blasted the internal accounting of the free list
2427     // allocator.
2428     return false;
2429   }
2430 
MarkNonForwardedObject(mirror::Object * obj)2431   mirror::Object* MarkNonForwardedObject(mirror::Object* obj) override
2432       REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
2433     size_t obj_size = obj->SizeOf<kDefaultVerifyFlags>();
2434     size_t alloc_size = RoundUp(obj_size, kObjectAlignment);
2435     mirror::Object* forward_address;
2436     // Find the smallest bin which we can move obj in.
2437     auto it = bins_.lower_bound(alloc_size);
2438     if (it == bins_.end()) {
2439       // No available space in the bins, place it in the target space instead (grows the zygote
2440       // space).
2441       size_t bytes_allocated, unused_bytes_tl_bulk_allocated;
2442       forward_address = to_space_->Alloc(
2443           self_, alloc_size, &bytes_allocated, nullptr, &unused_bytes_tl_bulk_allocated);
2444       if (to_space_live_bitmap_ != nullptr) {
2445         to_space_live_bitmap_->Set(forward_address);
2446       } else {
2447         GetHeap()->GetNonMovingSpace()->GetLiveBitmap()->Set(forward_address);
2448         GetHeap()->GetNonMovingSpace()->GetMarkBitmap()->Set(forward_address);
2449       }
2450     } else {
2451       size_t size = it->first;
2452       uintptr_t pos = it->second;
2453       bins_.erase(it);  // Erase the old bin which we replace with the new smaller bin.
2454       forward_address = reinterpret_cast<mirror::Object*>(pos);
2455       // Set the live and mark bits so that sweeping system weaks works properly.
2456       bin_live_bitmap_->Set(forward_address);
2457       bin_mark_bitmap_->Set(forward_address);
2458       DCHECK_GE(size, alloc_size);
2459       // Add a new bin with the remaining space.
2460       AddBin(size - alloc_size, pos + alloc_size);
2461     }
2462     // Copy the object over to its new location.
2463     // Historical note: We did not use `alloc_size` to avoid a Valgrind error.
2464     memcpy(reinterpret_cast<void*>(forward_address), obj, obj_size);
2465     if (kUseBakerReadBarrier) {
2466       obj->AssertReadBarrierState();
2467       forward_address->AssertReadBarrierState();
2468     }
2469     return forward_address;
2470   }
2471 };
2472 
UnBindBitmaps()2473 void Heap::UnBindBitmaps() {
2474   TimingLogger::ScopedTiming t("UnBindBitmaps", GetCurrentGcIteration()->GetTimings());
2475   for (const auto& space : GetContinuousSpaces()) {
2476     if (space->IsContinuousMemMapAllocSpace()) {
2477       space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
2478       if (alloc_space->GetLiveBitmap() != nullptr && alloc_space->HasBoundBitmaps()) {
2479         alloc_space->UnBindBitmaps();
2480       }
2481     }
2482   }
2483 }
2484 
IncrementFreedEver()2485 void Heap::IncrementFreedEver() {
2486   // Counters are updated only by us, but may be read concurrently.
2487   // The updates should become visible after the corresponding live object info.
2488   total_objects_freed_ever_.store(total_objects_freed_ever_.load(std::memory_order_relaxed)
2489                                   + GetCurrentGcIteration()->GetFreedObjects()
2490                                   + GetCurrentGcIteration()->GetFreedLargeObjects(),
2491                                   std::memory_order_release);
2492   total_bytes_freed_ever_.store(total_bytes_freed_ever_.load(std::memory_order_relaxed)
2493                                 + GetCurrentGcIteration()->GetFreedBytes()
2494                                 + GetCurrentGcIteration()->GetFreedLargeObjectBytes(),
2495                                 std::memory_order_release);
2496 }
2497 
2498 #pragma clang diagnostic push
2499 #if !ART_USE_FUTEXES
2500 // Frame gets too large, perhaps due to Bionic pthread_mutex_lock size. We don't care.
2501 #  pragma clang diagnostic ignored "-Wframe-larger-than="
2502 #endif
2503 // This has a large frame, but shouldn't be run anywhere near the stack limit.
2504 // FIXME: BUT it did exceed... http://b/197647048
2505 #  pragma clang diagnostic ignored "-Wframe-larger-than="
PreZygoteFork()2506 void Heap::PreZygoteFork() {
2507   if (!HasZygoteSpace()) {
2508     // We still want to GC in case there is some unreachable non moving objects that could cause a
2509     // suboptimal bin packing when we compact the zygote space.
2510     CollectGarbageInternal(collector::kGcTypeFull, kGcCauseBackground, false, GC_NUM_ANY);
2511     // Trim the pages at the end of the non moving space. Trim while not holding zygote lock since
2512     // the trim process may require locking the mutator lock.
2513     non_moving_space_->Trim();
2514   }
2515   // We need to close userfaultfd fd for app/webview zygotes to avoid getattr
2516   // (stat) on the fd during fork.
2517   Thread* self = Thread::Current();
2518   MutexLock mu(self, zygote_creation_lock_);
2519   // Try to see if we have any Zygote spaces.
2520   if (HasZygoteSpace()) {
2521     return;
2522   }
2523   Runtime* runtime = Runtime::Current();
2524   // Setup linear-alloc pool for post-zygote fork allocations before freezing
2525   // snapshots of intern-table and class-table.
2526   runtime->SetupLinearAllocForPostZygoteFork(self);
2527   runtime->GetInternTable()->AddNewTable();
2528   runtime->GetClassLinker()->MoveClassTableToPreZygote();
2529   VLOG(heap) << "Starting PreZygoteFork";
2530   // The end of the non-moving space may be protected, unprotect it so that we can copy the zygote
2531   // there.
2532   non_moving_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE);
2533   const bool same_space = non_moving_space_ == main_space_;
2534   if (kCompactZygote) {
2535     // Temporarily disable rosalloc verification because the zygote
2536     // compaction will mess up the rosalloc internal metadata.
2537     ScopedDisableRosAllocVerification disable_rosalloc_verif(this);
2538     ZygoteCompactingCollector zygote_collector(this, is_running_on_memory_tool_);
2539     zygote_collector.BuildBins(non_moving_space_);
2540     // Create a new bump pointer space which we will compact into.
2541     space::BumpPointerSpace target_space("zygote bump space", non_moving_space_->End(),
2542                                          non_moving_space_->Limit());
2543     // Compact the bump pointer space to a new zygote bump pointer space.
2544     bool reset_main_space = false;
2545     if (IsMovingGc(collector_type_)) {
2546       if (collector_type_ == kCollectorTypeCC) {
2547         zygote_collector.SetFromSpace(region_space_);
2548       } else {
2549         zygote_collector.SetFromSpace(bump_pointer_space_);
2550       }
2551     } else {
2552       CHECK(main_space_ != nullptr);
2553       CHECK_NE(main_space_, non_moving_space_)
2554           << "Does not make sense to compact within the same space";
2555       // Copy from the main space.
2556       zygote_collector.SetFromSpace(main_space_);
2557       reset_main_space = true;
2558     }
2559     zygote_collector.SetToSpace(&target_space);
2560     zygote_collector.SetSwapSemiSpaces(false);
2561     zygote_collector.Run(kGcCauseCollectorTransition, false);
2562     if (reset_main_space) {
2563       main_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE);
2564       madvise(main_space_->Begin(), main_space_->Capacity(), MADV_DONTNEED);
2565       MemMap mem_map = main_space_->ReleaseMemMap();
2566       RemoveSpace(main_space_);
2567       space::Space* old_main_space = main_space_;
2568       CreateMainMallocSpace(std::move(mem_map),
2569                             kDefaultInitialSize,
2570                             std::min(mem_map.Size(), growth_limit_),
2571                             mem_map.Size());
2572       delete old_main_space;
2573       AddSpace(main_space_);
2574     } else {
2575       if (collector_type_ == kCollectorTypeCC) {
2576         region_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE);
2577         // Evacuated everything out of the region space, clear the mark bitmap.
2578         region_space_->GetMarkBitmap()->Clear();
2579       } else {
2580         bump_pointer_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE);
2581         if (gUseUserfaultfd && use_generational_gc_) {
2582           MarkCompactCollector()->ResetGenerationalState();
2583         }
2584       }
2585     }
2586     if (temp_space_ != nullptr) {
2587       CHECK(temp_space_->IsEmpty());
2588     }
2589     IncrementFreedEver();
2590     // Update the end and write out image.
2591     non_moving_space_->SetEnd(target_space.End());
2592     non_moving_space_->SetLimit(target_space.Limit());
2593     VLOG(heap) << "Create zygote space with size=" << non_moving_space_->Size() << " bytes";
2594   }
2595   // Change the collector to the post zygote one.
2596   ChangeCollector(foreground_collector_type_);
2597   // Save the old space so that we can remove it after we complete creating the zygote space.
2598   space::MallocSpace* old_alloc_space = non_moving_space_;
2599   // Turn the current alloc space into a zygote space and obtain the new alloc space composed of
2600   // the remaining available space.
2601   // Remove the old space before creating the zygote space since creating the zygote space sets
2602   // the old alloc space's bitmaps to null.
2603   RemoveSpace(old_alloc_space);
2604   if (collector::SemiSpace::kUseRememberedSet) {
2605     // Consistency bound check.
2606     FindRememberedSetFromSpace(old_alloc_space)->AssertAllDirtyCardsAreWithinSpace();
2607     // Remove the remembered set for the now zygote space (the old
2608     // non-moving space). Note now that we have compacted objects into
2609     // the zygote space, the data in the remembered set is no longer
2610     // needed. The zygote space will instead have a mod-union table
2611     // from this point on.
2612     RemoveRememberedSet(old_alloc_space);
2613   }
2614   // Remaining space becomes the new non moving space.
2615   zygote_space_ = old_alloc_space->CreateZygoteSpace(kNonMovingSpaceName, low_memory_mode_,
2616                                                      &non_moving_space_);
2617   CHECK(!non_moving_space_->CanMoveObjects());
2618   if (same_space) {
2619     main_space_ = non_moving_space_;
2620     SetSpaceAsDefault(main_space_);
2621   }
2622   delete old_alloc_space;
2623   CHECK(HasZygoteSpace()) << "Failed creating zygote space";
2624   AddSpace(zygote_space_);
2625   non_moving_space_->SetFootprintLimit(non_moving_space_->Capacity());
2626   AddSpace(non_moving_space_);
2627   constexpr bool set_mark_bit = kUseBakerReadBarrier
2628                                 && gc::collector::ConcurrentCopying::kGrayDirtyImmuneObjects;
2629   if (set_mark_bit) {
2630     // Treat all of the objects in the zygote as marked to avoid unnecessary dirty pages. This is
2631     // safe since we mark all of the objects that may reference non immune objects as gray.
2632     zygote_space_->SetMarkBitInLiveObjects();
2633   }
2634 
2635   // Create the zygote space mod union table.
2636   accounting::ModUnionTable* mod_union_table =
2637       new accounting::ModUnionTableCardCache("zygote space mod-union table", this, zygote_space_);
2638   CHECK(mod_union_table != nullptr) << "Failed to create zygote space mod-union table";
2639 
2640   if (collector_type_ != kCollectorTypeCC && collector_type_ != kCollectorTypeCMC) {
2641     // Set all the cards in the mod-union table since we don't know which objects contain references
2642     // to large objects.
2643     mod_union_table->SetCards();
2644   } else {
2645     // Make sure to clear the zygote space cards so that we don't dirty pages in the next GC. There
2646     // may be dirty cards from the zygote compaction or reference processing. These cards are not
2647     // necessary to have marked since the zygote space may not refer to any objects not in the
2648     // zygote or image spaces at this point.
2649     mod_union_table->ProcessCards();
2650     mod_union_table->ClearTable();
2651 
2652     // For CC and CMC we never collect zygote large objects. This means we do not need to set the
2653     // cards for the zygote mod-union table and we can also clear all of the existing image
2654     // mod-union tables. The existing mod-union tables are only for image spaces and may only
2655     // reference zygote and image objects.
2656     for (auto& pair : mod_union_tables_) {
2657       CHECK(pair.first->IsImageSpace());
2658       CHECK(!pair.first->AsImageSpace()->GetImageHeader().IsAppImage());
2659       accounting::ModUnionTable* table = pair.second;
2660       table->ClearTable();
2661     }
2662   }
2663   AddModUnionTable(mod_union_table);
2664   large_object_space_->SetAllLargeObjectsAsZygoteObjects(self, set_mark_bit);
2665   if (collector::SemiSpace::kUseRememberedSet) {
2666     // Add a new remembered set for the post-zygote non-moving space.
2667     accounting::RememberedSet* post_zygote_non_moving_space_rem_set =
2668         new accounting::RememberedSet("Post-zygote non-moving space remembered set", this,
2669                                       non_moving_space_);
2670     CHECK(post_zygote_non_moving_space_rem_set != nullptr)
2671         << "Failed to create post-zygote non-moving space remembered set";
2672     AddRememberedSet(post_zygote_non_moving_space_rem_set);
2673   }
2674 }
2675 #pragma clang diagnostic pop
2676 
FlushAllocStack()2677 void Heap::FlushAllocStack() {
2678   MarkAllocStackAsLive(allocation_stack_.get());
2679   allocation_stack_->Reset();
2680 }
2681 
MarkAllocStack(accounting::ContinuousSpaceBitmap * bitmap1,accounting::ContinuousSpaceBitmap * bitmap2,accounting::LargeObjectBitmap * large_objects,accounting::ObjectStack * stack)2682 void Heap::MarkAllocStack(accounting::ContinuousSpaceBitmap* bitmap1,
2683                           accounting::ContinuousSpaceBitmap* bitmap2,
2684                           accounting::LargeObjectBitmap* large_objects,
2685                           accounting::ObjectStack* stack) {
2686   DCHECK(bitmap1 != nullptr);
2687   DCHECK(bitmap2 != nullptr);
2688   const auto* limit = stack->End();
2689   for (auto* it = stack->Begin(); it != limit; ++it) {
2690     const mirror::Object* obj = it->AsMirrorPtr();
2691     if (obj != nullptr) {
2692       if (bitmap1->HasAddress(obj)) {
2693         bitmap1->Set(obj);
2694       } else if (bitmap2->HasAddress(obj)) {
2695         bitmap2->Set(obj);
2696       } else {
2697         DCHECK(large_objects != nullptr);
2698         large_objects->Set(obj);
2699       }
2700     }
2701   }
2702 }
2703 
SwapSemiSpaces()2704 void Heap::SwapSemiSpaces() {
2705   CHECK(bump_pointer_space_ != nullptr);
2706   CHECK(temp_space_ != nullptr);
2707   std::swap(bump_pointer_space_, temp_space_);
2708 }
2709 
Compact(space::ContinuousMemMapAllocSpace * target_space,space::ContinuousMemMapAllocSpace * source_space,GcCause gc_cause)2710 collector::GarbageCollector* Heap::Compact(space::ContinuousMemMapAllocSpace* target_space,
2711                                            space::ContinuousMemMapAllocSpace* source_space,
2712                                            GcCause gc_cause) {
2713   CHECK(kMovingCollector);
2714   if (target_space != source_space) {
2715     // Don't swap spaces since this isn't a typical semi space collection.
2716     semi_space_collector_->SetSwapSemiSpaces(false);
2717     semi_space_collector_->SetFromSpace(source_space);
2718     semi_space_collector_->SetToSpace(target_space);
2719     semi_space_collector_->Run(gc_cause, false);
2720     return semi_space_collector_;
2721   }
2722   LOG(FATAL) << "Unsupported";
2723   UNREACHABLE();
2724 }
2725 
TraceHeapSize(size_t heap_size)2726 void Heap::TraceHeapSize(size_t heap_size) {
2727   ATraceIntegerValue("Heap size (KB)", heap_size / KB);
2728 }
2729 
2730 #if defined(__GLIBC__)
2731 # define IF_GLIBC(x) x
2732 #else
2733 # define IF_GLIBC(x)
2734 #endif
2735 
GetNativeBytes()2736 size_t Heap::GetNativeBytes() {
2737   size_t malloc_bytes;
2738 #if defined(__BIONIC__) || defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
2739   IF_GLIBC(size_t mmapped_bytes;)
2740   struct mallinfo mi = mallinfo();
2741   // In spite of the documentation, the jemalloc version of this call seems to do what we want,
2742   // and it is thread-safe.
2743   if (sizeof(size_t) > sizeof(mi.uordblks) && sizeof(size_t) > sizeof(mi.hblkhd)) {
2744     // Shouldn't happen, but glibc declares uordblks as int.
2745     // Avoiding sign extension gets us correct behavior for another 2 GB.
2746     malloc_bytes = (unsigned int)mi.uordblks;
2747     IF_GLIBC(mmapped_bytes = (unsigned int)mi.hblkhd;)
2748   } else {
2749     malloc_bytes = mi.uordblks;
2750     IF_GLIBC(mmapped_bytes = mi.hblkhd;)
2751   }
2752   // From the spec, it appeared mmapped_bytes <= malloc_bytes. Reality was sometimes
2753   // dramatically different. (b/119580449 was an early bug.) If so, we try to fudge it.
2754   // However, malloc implementations seem to interpret hblkhd differently, namely as
2755   // mapped blocks backing the entire heap (e.g. jemalloc) vs. large objects directly
2756   // allocated via mmap (e.g. glibc). Thus we now only do this for glibc, where it
2757   // previously helped, and which appears to use a reading of the spec compatible
2758   // with our adjustment.
2759 #if defined(__GLIBC__)
2760   if (mmapped_bytes > malloc_bytes) {
2761     malloc_bytes = mmapped_bytes;
2762   }
2763 #endif  // GLIBC
2764 #else  // Neither Bionic nor Glibc
2765   // We should hit this case only in contexts in which GC triggering is not critical. Effectively
2766   // disable GC triggering based on malloc().
2767   malloc_bytes = 1000;
2768 #endif
2769   return malloc_bytes + native_bytes_registered_.load(std::memory_order_relaxed);
2770   // An alternative would be to get RSS from /proc/self/statm. Empirically, that's no
2771   // more expensive, and it would allow us to count memory allocated by means other than malloc.
2772   // However it would change as pages are unmapped and remapped due to memory pressure, among
2773   // other things. It seems risky to trigger GCs as a result of such changes.
2774 }
2775 
GCNumberLt(uint32_t gc_num1,uint32_t gc_num2)2776 static inline bool GCNumberLt(uint32_t gc_num1, uint32_t gc_num2) {
2777   // unsigned comparison, assuming a non-huge difference, but dealing correctly with wrapping.
2778   uint32_t difference = gc_num2 - gc_num1;
2779   bool completed_more_than_requested = difference > 0x80000000;
2780   return difference > 0 && !completed_more_than_requested;
2781 }
2782 
2783 
CollectGarbageInternal(collector::GcType gc_type,GcCause gc_cause,bool clear_soft_references,uint32_t requested_gc_num)2784 collector::GcType Heap::CollectGarbageInternal(collector::GcType gc_type,
2785                                                GcCause gc_cause,
2786                                                bool clear_soft_references,
2787                                                uint32_t requested_gc_num) {
2788   Thread* self = Thread::Current();
2789   Runtime* runtime = Runtime::Current();
2790   // If the heap can't run the GC, silently fail and return that no GC was run.
2791   switch (gc_type) {
2792     case collector::kGcTypePartial: {
2793       if (!HasZygoteSpace()) {
2794         // Do not increment gcs_completed_ . We should retry with kGcTypeFull.
2795         return collector::kGcTypeNone;
2796       }
2797       break;
2798     }
2799     default: {
2800       // Other GC types don't have any special cases which makes them not runnable. The main case
2801       // here is full GC.
2802     }
2803   }
2804   ScopedThreadStateChange tsc(self, ThreadState::kWaitingPerformingGc);
2805   Locks::mutator_lock_->AssertNotHeld(self);
2806   SelfDeletingTask* clear;  // Unconditionally set below.
2807   {
2808     // We should not ever become runnable and re-suspend while executing a GC.
2809     // This would likely cause a deadlock if we acted on a suspension request.
2810     // TODO: We really want to assert that we don't transition to kRunnable.
2811     ScopedAssertNoThreadSuspension scoped_assert("Performing GC");
2812     if (self->IsHandlingStackOverflow<kNativeStackType>()) {
2813       // If we are throwing a stack overflow error we probably don't have enough remaining stack
2814       // space to run the GC. Note: we only care if the native stack has overflowed. If the
2815       // simulated stack overflows it is still possible that the native stack has room to run the
2816       // GC.
2817 
2818       // Count this as a GC in case someone is waiting for it to complete.
2819       gcs_completed_.fetch_add(1, std::memory_order_release);
2820       return collector::kGcTypeNone;
2821     }
2822     bool compacting_gc;
2823     {
2824       gc_complete_lock_->AssertNotHeld(self);
2825       // Already not runnable; just switch suspended states. We remain in a suspended state until
2826       // FinishGC(). This avoids the complicated dance in StartGC().
2827       ScopedThreadStateChange tsc2(self, ThreadState::kWaitingForGcToComplete);
2828       MutexLock mu(self, *gc_complete_lock_);
2829       // Ensure there is only one GC at a time.
2830       WaitForGcToCompleteLocked(gc_cause, self);
2831       if (requested_gc_num != GC_NUM_ANY && !GCNumberLt(GetCurrentGcNum(), requested_gc_num)) {
2832         // The appropriate GC was already triggered elsewhere.
2833         return collector::kGcTypeNone;
2834       }
2835       compacting_gc = IsMovingGc(collector_type_);
2836       // GC can be disabled if someone has a used GetPrimitiveArrayCritical.
2837       if (compacting_gc && disable_moving_gc_count_ != 0) {
2838         LOG(WARNING) << "Skipping GC due to disable moving GC count " << disable_moving_gc_count_;
2839         // Again count this as a GC.
2840         gcs_completed_.fetch_add(1, std::memory_order_release);
2841         return collector::kGcTypeNone;
2842       }
2843       if (gc_disabled_for_shutdown_) {
2844         gcs_completed_.fetch_add(1, std::memory_order_release);
2845         return collector::kGcTypeNone;
2846       }
2847       collector_type_running_ = collector_type_;
2848       last_gc_cause_ = gc_cause;
2849     }
2850     if (gc_cause == kGcCauseForAlloc && runtime->HasStatsEnabled()) {
2851       ++runtime->GetStats()->gc_for_alloc_count;
2852       ++self->GetStats()->gc_for_alloc_count;
2853     }
2854     const size_t bytes_allocated_before_gc = GetBytesAllocated();
2855 
2856     DCHECK_LT(gc_type, collector::kGcTypeMax);
2857     DCHECK_NE(gc_type, collector::kGcTypeNone);
2858 
2859     collector::GarbageCollector* collector = nullptr;
2860     // TODO: Clean this up.
2861     if (compacting_gc) {
2862       DCHECK(current_allocator_ == kAllocatorTypeBumpPointer ||
2863              current_allocator_ == kAllocatorTypeTLAB ||
2864              current_allocator_ == kAllocatorTypeRegion ||
2865              current_allocator_ == kAllocatorTypeRegionTLAB);
2866       switch (collector_type_) {
2867         case kCollectorTypeSS:
2868           semi_space_collector_->SetFromSpace(bump_pointer_space_);
2869           semi_space_collector_->SetToSpace(temp_space_);
2870           semi_space_collector_->SetSwapSemiSpaces(true);
2871           collector = semi_space_collector_;
2872           break;
2873         case kCollectorTypeCMC:
2874           collector = mark_compact_;
2875           if (use_generational_gc_ && gc_type == collector::kGcTypeSticky) {
2876             collector = young_mark_compact_;
2877           }
2878           break;
2879         case kCollectorTypeCC:
2880           collector::ConcurrentCopying* active_cc_collector;
2881           if (use_generational_gc_) {
2882             // TODO: Other threads must do the flip checkpoint before they start poking at
2883             // active_concurrent_copying_collector_. So we should not concurrency here.
2884             active_cc_collector = (gc_type == collector::kGcTypeSticky) ?
2885                                       young_concurrent_copying_collector_ :
2886                                       concurrent_copying_collector_;
2887             active_concurrent_copying_collector_.store(active_cc_collector,
2888                                                        std::memory_order_relaxed);
2889             DCHECK(active_cc_collector->RegionSpace() == region_space_);
2890             collector = active_cc_collector;
2891           } else {
2892             collector = active_concurrent_copying_collector_.load(std::memory_order_relaxed);
2893           }
2894           break;
2895         default:
2896           LOG(FATAL) << "Invalid collector type " << static_cast<size_t>(collector_type_);
2897       }
2898       // temp_space_ will be null for kCollectorTypeCMC.
2899       if (temp_space_ != nullptr &&
2900           collector != active_concurrent_copying_collector_.load(std::memory_order_relaxed)) {
2901         temp_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE);
2902         if (kIsDebugBuild) {
2903           // Try to read each page of the memory map in case mprotect didn't work properly
2904           // b/19894268.
2905           temp_space_->GetMemMap()->TryReadable();
2906         }
2907         CHECK(temp_space_->IsEmpty());
2908       }
2909     } else if (current_allocator_ == kAllocatorTypeRosAlloc ||
2910                current_allocator_ == kAllocatorTypeDlMalloc) {
2911       collector = FindCollectorByGcType(gc_type);
2912     } else {
2913       LOG(FATAL) << "Invalid current allocator " << current_allocator_;
2914     }
2915 
2916     CHECK(collector != nullptr) << "Could not find garbage collector with collector_type="
2917                                 << static_cast<size_t>(collector_type_)
2918                                 << " and gc_type=" << gc_type;
2919     collector->Run(gc_cause, clear_soft_references || runtime->IsZygote());
2920     IncrementFreedEver();
2921     RequestTrim(self);
2922     // Collect cleared references.
2923     clear = reference_processor_->CollectClearedReferences(self);
2924     // Grow the heap so that we know when to perform the next GC.
2925     GrowForUtilization(collector, bytes_allocated_before_gc);
2926     old_native_bytes_allocated_.store(GetNativeBytes());
2927     LogGC(gc_cause, collector);
2928     FinishGC(self, gc_type);
2929     // We're suspended up to this point.
2930   }
2931   // Actually enqueue all cleared references. Do this after the GC has officially finished since
2932   // otherwise we can deadlock.
2933   clear->Run(self);
2934   clear->Finalize();
2935   // Inform DDMS that a GC completed.
2936   Dbg::GcDidFinish();
2937 
2938   // Unload native libraries for class unloading. We do this after calling FinishGC to prevent
2939   // deadlocks in case the JNI_OnUnload function does allocations.
2940   {
2941     ScopedObjectAccess soa(self);
2942     soa.Vm()->UnloadNativeLibraries();
2943   }
2944   return gc_type;
2945 }
2946 
LogGC(GcCause gc_cause,collector::GarbageCollector * collector)2947 void Heap::LogGC(GcCause gc_cause, collector::GarbageCollector* collector) {
2948   const size_t duration = GetCurrentGcIteration()->GetDurationNs();
2949   const std::vector<uint64_t>& pause_times = GetCurrentGcIteration()->GetPauseTimes();
2950   // Print the GC if it is an explicit GC (e.g. Runtime.gc()) or a slow GC
2951   // (mutator time blocked >= long_pause_log_threshold_).
2952   bool log_gc = kLogAllGCs || (gc_cause == kGcCauseExplicit && always_log_explicit_gcs_);
2953   if (!log_gc && CareAboutPauseTimes()) {
2954     // GC for alloc pauses the allocating thread, so consider it as a pause.
2955     log_gc = duration > long_gc_log_threshold_ ||
2956         (gc_cause == kGcCauseForAlloc && duration > long_pause_log_threshold_);
2957     for (uint64_t pause : pause_times) {
2958       log_gc = log_gc || pause >= long_pause_log_threshold_;
2959     }
2960   }
2961   bool is_sampled = false;
2962   if (UNLIKELY(gc_stress_mode_)) {
2963     static std::atomic_int64_t accumulated_duration_ns = 0;
2964     accumulated_duration_ns += duration;
2965     if (accumulated_duration_ns >= kGcStressModeGcLogSampleFrequencyNs) {
2966       accumulated_duration_ns -= kGcStressModeGcLogSampleFrequencyNs;
2967       log_gc = true;
2968       is_sampled = true;
2969     }
2970   }
2971   if (log_gc) {
2972     const size_t percent_free = GetPercentFree();
2973     const size_t current_heap_size = GetBytesAllocated();
2974     const size_t total_memory = GetTotalMemory();
2975     std::ostringstream pause_string;
2976     for (size_t i = 0; i < pause_times.size(); ++i) {
2977       pause_string << PrettyDuration((pause_times[i] / 1000) * 1000)
2978                    << ((i != pause_times.size() - 1) ? "," : "");
2979     }
2980     LOG(INFO) << gc_cause << " " << collector->GetName()
2981               << (is_sampled ? " (sampled)" : "")
2982               << " GC freed "
2983               << PrettySize(current_gc_iteration_.GetFreedBytes()) << " AllocSpace bytes, "
2984               << current_gc_iteration_.GetFreedLargeObjects() << "("
2985               << PrettySize(current_gc_iteration_.GetFreedLargeObjectBytes()) << ") LOS objects, "
2986               << percent_free << "% free, " << PrettySize(current_heap_size) << "/"
2987               << PrettySize(total_memory) << ", " << "paused " << pause_string.str()
2988               << " total " << PrettyDuration((duration / 1000) * 1000);
2989     VLOG(heap) << Dumpable<TimingLogger>(*current_gc_iteration_.GetTimings());
2990   }
2991 }
2992 
FinishGC(Thread * self,collector::GcType gc_type)2993 void Heap::FinishGC(Thread* self, collector::GcType gc_type) {
2994   MutexLock mu(self, *gc_complete_lock_);
2995   collector_type_running_ = kCollectorTypeNone;
2996   if (gc_type != collector::kGcTypeNone) {
2997     last_gc_type_ = gc_type;
2998 
2999     // Update stats.
3000     ++gc_count_last_window_;
3001     if (running_collection_is_blocking_) {
3002       // If the currently running collection was a blocking one,
3003       // increment the counters and reset the flag.
3004       ++blocking_gc_count_;
3005       blocking_gc_time_ += GetCurrentGcIteration()->GetDurationNs();
3006       ++blocking_gc_count_last_window_;
3007     }
3008     // Update the gc count rate histograms if due.
3009     UpdateGcCountRateHistograms();
3010   }
3011   // Reset.
3012   running_collection_is_blocking_ = false;
3013   thread_running_gc_ = nullptr;
3014   if (gc_type != collector::kGcTypeNone) {
3015     gcs_completed_.fetch_add(1, std::memory_order_release);
3016   }
3017   // Wake anyone who may have been waiting for the GC to complete.
3018   gc_complete_cond_->Broadcast(self);
3019 }
3020 
UpdateGcCountRateHistograms()3021 void Heap::UpdateGcCountRateHistograms() {
3022   // Invariant: if the time since the last update includes more than
3023   // one windows, all the GC runs (if > 0) must have happened in first
3024   // window because otherwise the update must have already taken place
3025   // at an earlier GC run. So, we report the non-first windows with
3026   // zero counts to the histograms.
3027   DCHECK_EQ(last_update_time_gc_count_rate_histograms_ % kGcCountRateHistogramWindowDuration, 0U);
3028   uint64_t now = NanoTime();
3029   DCHECK_GE(now, last_update_time_gc_count_rate_histograms_);
3030   uint64_t time_since_last_update = now - last_update_time_gc_count_rate_histograms_;
3031   uint64_t num_of_windows = time_since_last_update / kGcCountRateHistogramWindowDuration;
3032 
3033   // The computed number of windows can be incoherently high if NanoTime() is not monotonic.
3034   // Setting a limit on its maximum value reduces the impact on CPU time in such cases.
3035   if (num_of_windows > kGcCountRateHistogramMaxNumMissedWindows) {
3036     LOG(WARNING) << "Reducing the number of considered missed Gc histogram windows from "
3037                  << num_of_windows << " to " << kGcCountRateHistogramMaxNumMissedWindows;
3038     num_of_windows = kGcCountRateHistogramMaxNumMissedWindows;
3039   }
3040 
3041   if (time_since_last_update >= kGcCountRateHistogramWindowDuration) {
3042     // Record the first window.
3043     gc_count_rate_histogram_.AddValue(gc_count_last_window_ - 1);  // Exclude the current run.
3044     blocking_gc_count_rate_histogram_.AddValue(running_collection_is_blocking_ ?
3045         blocking_gc_count_last_window_ - 1 : blocking_gc_count_last_window_);
3046     // Record the other windows (with zero counts).
3047     for (uint64_t i = 0; i < num_of_windows - 1; ++i) {
3048       gc_count_rate_histogram_.AddValue(0);
3049       blocking_gc_count_rate_histogram_.AddValue(0);
3050     }
3051     // Update the last update time and reset the counters.
3052     last_update_time_gc_count_rate_histograms_ =
3053         (now / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration;
3054     gc_count_last_window_ = 1;  // Include the current run.
3055     blocking_gc_count_last_window_ = running_collection_is_blocking_ ? 1 : 0;
3056   }
3057   DCHECK_EQ(last_update_time_gc_count_rate_histograms_ % kGcCountRateHistogramWindowDuration, 0U);
3058 }
3059 
3060 class RootMatchesObjectVisitor : public SingleRootVisitor {
3061  public:
RootMatchesObjectVisitor(const mirror::Object * obj)3062   explicit RootMatchesObjectVisitor(const mirror::Object* obj) : obj_(obj) { }
3063 
VisitRoot(mirror::Object * root,const RootInfo & info)3064   void VisitRoot(mirror::Object* root, const RootInfo& info)
3065       override REQUIRES_SHARED(Locks::mutator_lock_) {
3066     if (root == obj_) {
3067       LOG(INFO) << "Object " << obj_ << " is a root " << info.ToString();
3068     }
3069   }
3070 
3071  private:
3072   const mirror::Object* const obj_;
3073 };
3074 
3075 
3076 class ScanVisitor {
3077  public:
operator ()(const mirror::Object * obj) const3078   void operator()(const mirror::Object* obj) const {
3079     LOG(ERROR) << "Would have rescanned object " << obj;
3080   }
3081 };
3082 
3083 // Verify a reference from an object.
3084 class VerifyReferenceVisitor : public SingleRootVisitor {
3085  public:
VerifyReferenceVisitor(Thread * self,Heap * heap,size_t * fail_count,bool verify_referent)3086   VerifyReferenceVisitor(Thread* self, Heap* heap, size_t* fail_count, bool verify_referent)
3087       REQUIRES_SHARED(Locks::mutator_lock_)
3088       : self_(self), heap_(heap), fail_count_(fail_count), verify_referent_(verify_referent) {
3089     CHECK_EQ(self_, Thread::Current());
3090   }
3091 
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const3092   void operator()([[maybe_unused]] ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
3093       REQUIRES_SHARED(Locks::mutator_lock_) {
3094     if (verify_referent_) {
3095       VerifyReference(ref.Ptr(), ref->GetReferent(), mirror::Reference::ReferentOffset());
3096     }
3097   }
3098 
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static) const3099   void operator()(ObjPtr<mirror::Object> obj,
3100                   MemberOffset offset,
3101                   [[maybe_unused]] bool is_static) const REQUIRES_SHARED(Locks::mutator_lock_) {
3102     VerifyReference(obj.Ptr(), obj->GetFieldObject<mirror::Object>(offset), offset);
3103   }
3104 
IsLive(ObjPtr<mirror::Object> obj) const3105   bool IsLive(ObjPtr<mirror::Object> obj) const NO_THREAD_SAFETY_ANALYSIS {
3106     return heap_->IsLiveObjectLocked(obj, true, false, true);
3107   }
3108 
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const3109   void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
3110       REQUIRES_SHARED(Locks::mutator_lock_) {
3111     if (!root->IsNull()) {
3112       VisitRoot(root);
3113     }
3114   }
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const3115   void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
3116       REQUIRES_SHARED(Locks::mutator_lock_) {
3117     const_cast<VerifyReferenceVisitor*>(this)->VisitRoot(
3118         root->AsMirrorPtr(), RootInfo(kRootVMInternal));
3119   }
3120 
VisitRoot(mirror::Object * root,const RootInfo & root_info)3121   void VisitRoot(mirror::Object* root, const RootInfo& root_info) override
3122       REQUIRES_SHARED(Locks::mutator_lock_) {
3123     if (root == nullptr) {
3124       LOG(ERROR) << "Root is null with info " << root_info.GetType();
3125     } else if (!VerifyReference(nullptr, root, MemberOffset(0))) {
3126       LOG(ERROR) << "Root " << root << " is dead with type " << mirror::Object::PrettyTypeOf(root)
3127           << " thread_id= " << root_info.GetThreadId() << " root_type= " << root_info.GetType();
3128     }
3129   }
3130 
3131  private:
3132   // TODO: Fix the no thread safety analysis.
3133   // Returns false on failure.
VerifyReference(mirror::Object * obj,mirror::Object * ref,MemberOffset offset) const3134   bool VerifyReference(mirror::Object* obj, mirror::Object* ref, MemberOffset offset) const
3135       NO_THREAD_SAFETY_ANALYSIS {
3136     if (ref == nullptr || IsLive(ref)) {
3137       // Verify that the reference is live.
3138       return true;
3139     }
3140     CHECK_EQ(self_, Thread::Current());  // fail_count_ is private to the calling thread.
3141     *fail_count_ += 1;
3142     if (*fail_count_ == 1) {
3143       // Only print message for the first failure to prevent spam.
3144       LOG(ERROR) << "!!!!!!!!!!!!!!Heap corruption detected!!!!!!!!!!!!!!!!!!!";
3145     }
3146     if (obj != nullptr) {
3147       // Only do this part for non roots.
3148       accounting::CardTable* card_table = heap_->GetCardTable();
3149       accounting::ObjectStack* alloc_stack = heap_->allocation_stack_.get();
3150       accounting::ObjectStack* live_stack = heap_->live_stack_.get();
3151       uint8_t* card_addr = card_table->CardFromAddr(obj);
3152       LOG(ERROR) << "Object " << obj << " references dead object " << ref << " at offset "
3153                  << offset << "\n card value = " << static_cast<int>(*card_addr);
3154       if (heap_->IsValidObjectAddress(obj->GetClass())) {
3155         LOG(ERROR) << "Obj type " << obj->PrettyTypeOf();
3156       } else {
3157         LOG(ERROR) << "Object " << obj << " class(" << obj->GetClass() << ") not a heap address";
3158       }
3159 
3160       // Attempt to find the class inside of the recently freed objects.
3161       space::ContinuousSpace* ref_space = heap_->FindContinuousSpaceFromObject(ref, true);
3162       if (ref_space != nullptr && ref_space->IsMallocSpace()) {
3163         space::MallocSpace* space = ref_space->AsMallocSpace();
3164         mirror::Class* ref_class = space->FindRecentFreedObject(ref);
3165         if (ref_class != nullptr) {
3166           LOG(ERROR) << "Reference " << ref << " found as a recently freed object with class "
3167                      << ref_class->PrettyClass();
3168         } else {
3169           LOG(ERROR) << "Reference " << ref << " not found as a recently freed object";
3170         }
3171       }
3172 
3173       if (ref->GetClass() != nullptr && heap_->IsValidObjectAddress(ref->GetClass()) &&
3174           ref->GetClass()->IsClass()) {
3175         LOG(ERROR) << "Ref type " << ref->PrettyTypeOf();
3176       } else {
3177         LOG(ERROR) << "Ref " << ref << " class(" << ref->GetClass()
3178                    << ") is not a valid heap address";
3179       }
3180 
3181       card_table->CheckAddrIsInCardTable(reinterpret_cast<const uint8_t*>(obj));
3182       void* cover_begin = card_table->AddrFromCard(card_addr);
3183       void* cover_end = reinterpret_cast<void*>(reinterpret_cast<size_t>(cover_begin) +
3184           accounting::CardTable::kCardSize);
3185       LOG(ERROR) << "Card " << reinterpret_cast<void*>(card_addr) << " covers " << cover_begin
3186           << "-" << cover_end;
3187       accounting::ContinuousSpaceBitmap* bitmap =
3188           heap_->GetLiveBitmap()->GetContinuousSpaceBitmap(obj);
3189 
3190       if (bitmap == nullptr) {
3191         LOG(ERROR) << "Object " << obj << " has no bitmap";
3192         if (!VerifyClassClass(obj->GetClass())) {
3193           LOG(ERROR) << "Object " << obj << " failed class verification!";
3194         }
3195       } else {
3196         // Print out how the object is live.
3197         if (bitmap->Test(obj)) {
3198           LOG(ERROR) << "Object " << obj << " found in live bitmap";
3199         }
3200         if (alloc_stack->Contains(const_cast<mirror::Object*>(obj))) {
3201           LOG(ERROR) << "Object " << obj << " found in allocation stack";
3202         }
3203         if (live_stack->Contains(const_cast<mirror::Object*>(obj))) {
3204           LOG(ERROR) << "Object " << obj << " found in live stack";
3205         }
3206         if (alloc_stack->Contains(const_cast<mirror::Object*>(ref))) {
3207           LOG(ERROR) << "Ref " << ref << " found in allocation stack";
3208         }
3209         if (live_stack->Contains(const_cast<mirror::Object*>(ref))) {
3210           LOG(ERROR) << "Ref " << ref << " found in live stack";
3211         }
3212         // Attempt to see if the card table missed the reference.
3213         ScanVisitor scan_visitor;
3214         uint8_t* byte_cover_begin = reinterpret_cast<uint8_t*>(card_table->AddrFromCard(card_addr));
3215         card_table->Scan<false>(bitmap, byte_cover_begin,
3216                                 byte_cover_begin + accounting::CardTable::kCardSize, scan_visitor);
3217       }
3218 
3219       // Search to see if any of the roots reference our object.
3220       RootMatchesObjectVisitor visitor1(obj);
3221       Runtime::Current()->VisitRoots(&visitor1);
3222       // Search to see if any of the roots reference our reference.
3223       RootMatchesObjectVisitor visitor2(ref);
3224       Runtime::Current()->VisitRoots(&visitor2);
3225     }
3226     return false;
3227   }
3228 
3229   Thread* const self_;
3230   Heap* const heap_;
3231   size_t* const fail_count_;
3232   const bool verify_referent_;
3233 };
3234 
3235 // Verify all references within an object, for use with HeapBitmap::Visit.
3236 class VerifyObjectVisitor {
3237  public:
VerifyObjectVisitor(Thread * self,Heap * heap,size_t * fail_count,bool verify_referent)3238   VerifyObjectVisitor(Thread* self, Heap* heap, size_t* fail_count, bool verify_referent)
3239       : self_(self), heap_(heap), fail_count_(fail_count), verify_referent_(verify_referent) {}
3240 
operator ()(mirror::Object * obj)3241   void operator()(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
3242     // Note: we are verifying the references in obj but not obj itself, this is because obj must
3243     // be live or else how did we find it in the live bitmap?
3244     VerifyReferenceVisitor visitor(self_, heap_, fail_count_, verify_referent_);
3245     // The class doesn't count as a reference but we should verify it anyways.
3246     obj->VisitReferences(visitor, visitor);
3247   }
3248 
VerifyRoots()3249   void VerifyRoots() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_) {
3250     ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
3251     VerifyReferenceVisitor visitor(self_, heap_, fail_count_, verify_referent_);
3252     Runtime::Current()->VisitRoots(&visitor);
3253   }
3254 
GetFailureCount() const3255   uint32_t GetFailureCount() const REQUIRES(Locks::mutator_lock_) {
3256     CHECK_EQ(self_, Thread::Current());
3257     return *fail_count_;
3258   }
3259 
3260  private:
3261   Thread* const self_;
3262   Heap* const heap_;
3263   size_t* const fail_count_;
3264   const bool verify_referent_;
3265 };
3266 
PushOnAllocationStackWithInternalGC(Thread * self,ObjPtr<mirror::Object> * obj)3267 void Heap::PushOnAllocationStackWithInternalGC(Thread* self, ObjPtr<mirror::Object>* obj) {
3268   // Slow path, the allocation stack push back must have already failed.
3269   DCHECK(!allocation_stack_->AtomicPushBack(obj->Ptr()));
3270   do {
3271     // TODO: Add handle VerifyObject.
3272     StackHandleScope<1> hs(self);
3273     HandleWrapperObjPtr<mirror::Object> wrapper(hs.NewHandleWrapper(obj));
3274     // Push our object into the reserve region of the allocation stack. This is only required due
3275     // to heap verification requiring that roots are live (either in the live bitmap or in the
3276     // allocation stack).
3277     CHECK(allocation_stack_->AtomicPushBackIgnoreGrowthLimit(obj->Ptr()));
3278     CollectGarbageInternal(collector::kGcTypeSticky,
3279                            kGcCauseForAlloc,
3280                            false,
3281                            GetCurrentGcNum() + 1);
3282   } while (!allocation_stack_->AtomicPushBack(obj->Ptr()));
3283 }
3284 
PushOnThreadLocalAllocationStackWithInternalGC(Thread * self,ObjPtr<mirror::Object> * obj)3285 void Heap::PushOnThreadLocalAllocationStackWithInternalGC(Thread* self,
3286                                                           ObjPtr<mirror::Object>* obj) {
3287   // Slow path, the allocation stack push back must have already failed.
3288   DCHECK(!self->PushOnThreadLocalAllocationStack(obj->Ptr()));
3289   StackReference<mirror::Object>* start_address;
3290   StackReference<mirror::Object>* end_address;
3291   while (!allocation_stack_->AtomicBumpBack(kThreadLocalAllocationStackSize, &start_address,
3292                                             &end_address)) {
3293     // TODO: Add handle VerifyObject.
3294     StackHandleScope<1> hs(self);
3295     HandleWrapperObjPtr<mirror::Object> wrapper(hs.NewHandleWrapper(obj));
3296     // Push our object into the reserve region of the allocaiton stack. This is only required due
3297     // to heap verification requiring that roots are live (either in the live bitmap or in the
3298     // allocation stack).
3299     CHECK(allocation_stack_->AtomicPushBackIgnoreGrowthLimit(obj->Ptr()));
3300     // Push into the reserve allocation stack.
3301     CollectGarbageInternal(collector::kGcTypeSticky,
3302                            kGcCauseForAlloc,
3303                            false,
3304                            GetCurrentGcNum() + 1);
3305   }
3306   self->SetThreadLocalAllocationStack(start_address, end_address);
3307   // Retry on the new thread-local allocation stack.
3308   CHECK(self->PushOnThreadLocalAllocationStack(obj->Ptr()));  // Must succeed.
3309 }
3310 
3311 // Must do this with mutators suspended since we are directly accessing the allocation stacks.
VerifyHeapReferences(bool verify_referents)3312 size_t Heap::VerifyHeapReferences(bool verify_referents) {
3313   Thread* self = Thread::Current();
3314   Locks::mutator_lock_->AssertExclusiveHeld(self);
3315   // Lets sort our allocation stacks so that we can efficiently binary search them.
3316   allocation_stack_->Sort();
3317   live_stack_->Sort();
3318   // Since we sorted the allocation stack content, need to revoke all
3319   // thread-local allocation stacks.
3320   RevokeAllThreadLocalAllocationStacks(self);
3321   size_t fail_count = 0;
3322   VerifyObjectVisitor visitor(self, this, &fail_count, verify_referents);
3323   // Verify objects in the allocation stack since these will be objects which were:
3324   // 1. Allocated prior to the GC (pre GC verification).
3325   // 2. Allocated during the GC (pre sweep GC verification).
3326   // We don't want to verify the objects in the live stack since they themselves may be
3327   // pointing to dead objects if they are not reachable.
3328   VisitObjectsPaused(visitor);
3329   // Verify the roots:
3330   visitor.VerifyRoots();
3331   if (visitor.GetFailureCount() > 0) {
3332     // Dump mod-union tables.
3333     for (const auto& table_pair : mod_union_tables_) {
3334       accounting::ModUnionTable* mod_union_table = table_pair.second;
3335       mod_union_table->Dump(LOG_STREAM(ERROR) << mod_union_table->GetName() << ": ");
3336     }
3337     // Dump remembered sets.
3338     for (const auto& table_pair : remembered_sets_) {
3339       accounting::RememberedSet* remembered_set = table_pair.second;
3340       remembered_set->Dump(LOG_STREAM(ERROR) << remembered_set->GetName() << ": ");
3341     }
3342     DumpSpaces(LOG_STREAM(ERROR));
3343   }
3344   return visitor.GetFailureCount();
3345 }
3346 
3347 class VerifyReferenceCardVisitor {
3348  public:
VerifyReferenceCardVisitor(Heap * heap,bool * failed)3349   VerifyReferenceCardVisitor(Heap* heap, bool* failed)
3350       REQUIRES_SHARED(Locks::mutator_lock_,
3351                             Locks::heap_bitmap_lock_)
3352       : heap_(heap), failed_(failed) {
3353   }
3354 
3355   // There is no card marks for native roots on a class.
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const3356   void VisitRootIfNonNull(
3357       [[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const3358   void VisitRoot([[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const {}
3359 
3360   // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for
3361   // annotalysis on visitors.
operator ()(mirror::Object * obj,MemberOffset offset,bool is_static) const3362   void operator()(mirror::Object* obj, MemberOffset offset, bool is_static) const
3363       NO_THREAD_SAFETY_ANALYSIS {
3364     mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
3365     // Filter out class references since changing an object's class does not mark the card as dirty.
3366     // Also handles large objects, since the only reference they hold is a class reference.
3367     if (ref != nullptr && !ref->IsClass()) {
3368       accounting::CardTable* card_table = heap_->GetCardTable();
3369       // If the object is not dirty and it is referencing something in the live stack other than
3370       // class, then it must be on a dirty card.
3371       if (!card_table->AddrIsInCardTable(obj)) {
3372         LOG(ERROR) << "Object " << obj << " is not in the address range of the card table";
3373         *failed_ = true;
3374       } else if (!card_table->IsDirty(obj)) {
3375         // TODO: Check mod-union tables.
3376         // Card should be either kCardDirty if it got re-dirtied after we aged it, or
3377         // kCardDirty - 1 if it didnt get touched since we aged it.
3378         accounting::ObjectStack* live_stack = heap_->live_stack_.get();
3379         if (live_stack->ContainsSorted(ref)) {
3380           if (live_stack->ContainsSorted(obj)) {
3381             LOG(ERROR) << "Object " << obj << " found in live stack";
3382           }
3383           if (heap_->GetLiveBitmap()->Test(obj)) {
3384             LOG(ERROR) << "Object " << obj << " found in live bitmap";
3385           }
3386           LOG(ERROR) << "Object " << obj << " " << mirror::Object::PrettyTypeOf(obj)
3387                     << " references " << ref << " " << mirror::Object::PrettyTypeOf(ref)
3388                     << " in live stack";
3389 
3390           // Print which field of the object is dead.
3391           if (!obj->IsObjectArray()) {
3392             ObjPtr<mirror::Class> klass = is_static ? obj->AsClass() : obj->GetClass();
3393             CHECK(klass != nullptr);
3394             for (ArtField& field : klass->GetFields()) {
3395               if (is_static == field.IsStatic() &&
3396                   field.GetOffset().Int32Value() == offset.Int32Value()) {
3397                 LOG(ERROR) << (is_static ? "Static " : "") << "field in the live stack is "
3398                            << field.PrettyField();
3399                 break;
3400               }
3401             }
3402           } else {
3403             ObjPtr<mirror::ObjectArray<mirror::Object>> object_array =
3404                 obj->AsObjectArray<mirror::Object>();
3405             for (int32_t i = 0; i < object_array->GetLength(); ++i) {
3406               if (object_array->Get(i) == ref) {
3407                 LOG(ERROR) << (is_static ? "Static " : "") << "obj[" << i << "] = ref";
3408               }
3409             }
3410           }
3411 
3412           *failed_ = true;
3413         }
3414       }
3415     }
3416   }
3417 
3418  private:
3419   Heap* const heap_;
3420   bool* const failed_;
3421 };
3422 
3423 class VerifyLiveStackReferences {
3424  public:
VerifyLiveStackReferences(Heap * heap)3425   explicit VerifyLiveStackReferences(Heap* heap)
3426       : heap_(heap),
3427         failed_(false) {}
3428 
operator ()(mirror::Object * obj) const3429   void operator()(mirror::Object* obj) const
3430       REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
3431     VerifyReferenceCardVisitor visitor(heap_, const_cast<bool*>(&failed_));
3432     obj->VisitReferences(visitor, VoidFunctor());
3433   }
3434 
Failed() const3435   bool Failed() const {
3436     return failed_;
3437   }
3438 
3439  private:
3440   Heap* const heap_;
3441   bool failed_;
3442 };
3443 
VerifyMissingCardMarks()3444 bool Heap::VerifyMissingCardMarks() {
3445   Thread* self = Thread::Current();
3446   Locks::mutator_lock_->AssertExclusiveHeld(self);
3447   // We need to sort the live stack since we binary search it.
3448   live_stack_->Sort();
3449   // Since we sorted the allocation stack content, need to revoke all
3450   // thread-local allocation stacks.
3451   RevokeAllThreadLocalAllocationStacks(self);
3452   VerifyLiveStackReferences visitor(this);
3453   GetLiveBitmap()->Visit(visitor);
3454   // We can verify objects in the live stack since none of these should reference dead objects.
3455   for (auto* it = live_stack_->Begin(); it != live_stack_->End(); ++it) {
3456     if (!kUseThreadLocalAllocationStack || it->AsMirrorPtr() != nullptr) {
3457       visitor(it->AsMirrorPtr());
3458     }
3459   }
3460   return !visitor.Failed();
3461 }
3462 
SwapStacks()3463 void Heap::SwapStacks() {
3464   if (kUseThreadLocalAllocationStack) {
3465     live_stack_->AssertAllZero();
3466   }
3467   allocation_stack_.swap(live_stack_);
3468 }
3469 
RevokeAllThreadLocalAllocationStacks(Thread * self)3470 void Heap::RevokeAllThreadLocalAllocationStacks(Thread* self) {
3471   // This must be called only during the pause.
3472   DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self));
3473   MutexLock mu(self, *Locks::runtime_shutdown_lock_);
3474   MutexLock mu2(self, *Locks::thread_list_lock_);
3475   std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
3476   for (Thread* t : thread_list) {
3477     t->RevokeThreadLocalAllocationStack();
3478   }
3479 }
3480 
AssertThreadLocalBuffersAreRevoked(Thread * thread)3481 void Heap::AssertThreadLocalBuffersAreRevoked(Thread* thread) {
3482   if (kIsDebugBuild) {
3483     if (rosalloc_space_ != nullptr) {
3484       rosalloc_space_->AssertThreadLocalBuffersAreRevoked(thread);
3485     }
3486     if (bump_pointer_space_ != nullptr) {
3487       bump_pointer_space_->AssertThreadLocalBuffersAreRevoked(thread);
3488     }
3489   }
3490 }
3491 
AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked()3492 void Heap::AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked() {
3493   if (kIsDebugBuild) {
3494     if (bump_pointer_space_ != nullptr) {
3495       bump_pointer_space_->AssertAllThreadLocalBuffersAreRevoked();
3496     }
3497   }
3498 }
3499 
FindModUnionTableFromSpace(space::Space * space)3500 accounting::ModUnionTable* Heap::FindModUnionTableFromSpace(space::Space* space) {
3501   auto it = mod_union_tables_.find(space);
3502   if (it == mod_union_tables_.end()) {
3503     return nullptr;
3504   }
3505   return it->second;
3506 }
3507 
FindRememberedSetFromSpace(space::Space * space)3508 accounting::RememberedSet* Heap::FindRememberedSetFromSpace(space::Space* space) {
3509   auto it = remembered_sets_.find(space);
3510   if (it == remembered_sets_.end()) {
3511     return nullptr;
3512   }
3513   return it->second;
3514 }
3515 
ProcessCards(TimingLogger * timings,bool use_rem_sets,bool process_alloc_space_cards,bool clear_alloc_space_cards)3516 void Heap::ProcessCards(TimingLogger* timings,
3517                         bool use_rem_sets,
3518                         bool process_alloc_space_cards,
3519                         bool clear_alloc_space_cards) {
3520   TimingLogger::ScopedTiming t(__FUNCTION__, timings);
3521   // Clear cards and keep track of cards cleared in the mod-union table.
3522   for (const auto& space : continuous_spaces_) {
3523     accounting::ModUnionTable* table = FindModUnionTableFromSpace(space);
3524     accounting::RememberedSet* rem_set = FindRememberedSetFromSpace(space);
3525     if (table != nullptr) {
3526       const char* name = space->IsZygoteSpace() ? "ZygoteModUnionClearCards" :
3527           "ImageModUnionClearCards";
3528       TimingLogger::ScopedTiming t2(name, timings);
3529       table->ProcessCards();
3530     } else if (use_rem_sets && rem_set != nullptr) {
3531       DCHECK(collector::SemiSpace::kUseRememberedSet) << static_cast<int>(collector_type_);
3532       TimingLogger::ScopedTiming t2("AllocSpaceRemSetClearCards", timings);
3533       rem_set->ClearCards();
3534     } else if (process_alloc_space_cards) {
3535       TimingLogger::ScopedTiming t2("AllocSpaceClearCards", timings);
3536       if (clear_alloc_space_cards) {
3537         uint8_t* end = space->End();
3538         if (space->IsImageSpace()) {
3539           // Image space end is the end of the mirror objects, it is not necessarily page or card
3540           // aligned. Align up so that the check in ClearCardRange does not fail.
3541           end = AlignUp(end, accounting::CardTable::kCardSize);
3542         }
3543         card_table_->ClearCardRange(space->Begin(), end);
3544       } else {
3545         // No mod union table for the AllocSpace. Age the cards so that the GC knows that these
3546         // cards were dirty before the GC started.
3547         // TODO: Need to use atomic for the case where aged(cleaning thread) -> dirty(other thread)
3548         // -> clean(cleaning thread).
3549         // The races are we either end up with: Aged card, unaged card. Since we have the
3550         // checkpoint roots and then we scan / update mod union tables after. We will always
3551         // scan either card. If we end up with the non aged card, we scan it it in the pause.
3552         card_table_->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(),
3553                                        VoidFunctor());
3554       }
3555     }
3556   }
3557 }
3558 
3559 struct IdentityMarkHeapReferenceVisitor : public MarkObjectVisitor {
MarkObjectart::gc::IdentityMarkHeapReferenceVisitor3560   mirror::Object* MarkObject(mirror::Object* obj) override {
3561     return obj;
3562   }
MarkHeapReferenceart::gc::IdentityMarkHeapReferenceVisitor3563   void MarkHeapReference(mirror::HeapReference<mirror::Object>*, bool) override {
3564   }
3565 };
3566 
PreGcVerificationPaused(collector::GarbageCollector * gc)3567 void Heap::PreGcVerificationPaused(collector::GarbageCollector* gc) {
3568   Thread* const self = Thread::Current();
3569   TimingLogger* const timings = current_gc_iteration_.GetTimings();
3570   TimingLogger::ScopedTiming t(__FUNCTION__, timings);
3571   if (verify_pre_gc_heap_) {
3572     TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyHeapReferences", timings);
3573     size_t failures = VerifyHeapReferences();
3574     if (failures > 0) {
3575       LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed with " << failures
3576           << " failures";
3577     }
3578   }
3579   // Check that all objects which reference things in the live stack are on dirty cards.
3580   if (verify_missing_card_marks_) {
3581     TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyMissingCardMarks", timings);
3582     ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
3583     SwapStacks();
3584     // Sort the live stack so that we can quickly binary search it later.
3585     CHECK(VerifyMissingCardMarks()) << "Pre " << gc->GetName()
3586                                     << " missing card mark verification failed\n" << DumpSpaces();
3587     SwapStacks();
3588   }
3589   if (verify_mod_union_table_) {
3590     TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyModUnionTables", timings);
3591     ReaderMutexLock reader_lock(self, *Locks::heap_bitmap_lock_);
3592     for (const auto& table_pair : mod_union_tables_) {
3593       accounting::ModUnionTable* mod_union_table = table_pair.second;
3594       IdentityMarkHeapReferenceVisitor visitor;
3595       mod_union_table->UpdateAndMarkReferences(&visitor);
3596       mod_union_table->Verify();
3597     }
3598   }
3599 }
3600 
PreGcVerification(collector::GarbageCollector * gc)3601 void Heap::PreGcVerification(collector::GarbageCollector* gc) {
3602   if (verify_pre_gc_heap_ || verify_missing_card_marks_ || verify_mod_union_table_) {
3603     collector::GarbageCollector::ScopedPause pause(gc, false);
3604     PreGcVerificationPaused(gc);
3605   }
3606 }
3607 
PrePauseRosAllocVerification(collector::GarbageCollector * gc)3608 void Heap::PrePauseRosAllocVerification([[maybe_unused]] collector::GarbageCollector* gc) {
3609   // TODO: Add a new runtime option for this?
3610   if (verify_pre_gc_rosalloc_) {
3611     RosAllocVerification(current_gc_iteration_.GetTimings(), "PreGcRosAllocVerification");
3612   }
3613 }
3614 
PreSweepingGcVerification(collector::GarbageCollector * gc)3615 void Heap::PreSweepingGcVerification(collector::GarbageCollector* gc) {
3616   Thread* const self = Thread::Current();
3617   TimingLogger* const timings = current_gc_iteration_.GetTimings();
3618   TimingLogger::ScopedTiming t(__FUNCTION__, timings);
3619   // Called before sweeping occurs since we want to make sure we are not going so reclaim any
3620   // reachable objects.
3621   if (verify_pre_sweeping_heap_) {
3622     TimingLogger::ScopedTiming t2("(Paused)PostSweepingVerifyHeapReferences", timings);
3623     CHECK_NE(self->GetState(), ThreadState::kRunnable);
3624     {
3625       WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
3626       // Swapping bound bitmaps does nothing.
3627       gc->SwapBitmaps();
3628     }
3629     // Pass in false since concurrent reference processing can mean that the reference referents
3630     // may point to dead objects at the point which PreSweepingGcVerification is called.
3631     size_t failures = VerifyHeapReferences(false);
3632     if (failures > 0) {
3633       LOG(FATAL) << "Pre sweeping " << gc->GetName() << " GC verification failed with " << failures
3634           << " failures";
3635     }
3636     {
3637       WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
3638       gc->SwapBitmaps();
3639     }
3640   }
3641   if (verify_pre_sweeping_rosalloc_) {
3642     RosAllocVerification(timings, "PreSweepingRosAllocVerification");
3643   }
3644 }
3645 
PostGcVerificationPaused(collector::GarbageCollector * gc)3646 void Heap::PostGcVerificationPaused(collector::GarbageCollector* gc) {
3647   // Only pause if we have to do some verification.
3648   Thread* const self = Thread::Current();
3649   TimingLogger* const timings = GetCurrentGcIteration()->GetTimings();
3650   TimingLogger::ScopedTiming t(__FUNCTION__, timings);
3651   if (verify_system_weaks_) {
3652     ReaderMutexLock mu2(self, *Locks::heap_bitmap_lock_);
3653     collector::MarkSweep* mark_sweep = down_cast<collector::MarkSweep*>(gc);
3654     mark_sweep->VerifySystemWeaks();
3655   }
3656   if (verify_post_gc_rosalloc_) {
3657     RosAllocVerification(timings, "(Paused)PostGcRosAllocVerification");
3658   }
3659   if (verify_post_gc_heap_) {
3660     TimingLogger::ScopedTiming t2("(Paused)PostGcVerifyHeapReferences", timings);
3661     size_t failures = VerifyHeapReferences();
3662     if (failures > 0) {
3663       LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed with " << failures
3664           << " failures";
3665     }
3666   }
3667 }
3668 
PostGcVerification(collector::GarbageCollector * gc)3669 void Heap::PostGcVerification(collector::GarbageCollector* gc) {
3670   if (verify_system_weaks_ || verify_post_gc_rosalloc_ || verify_post_gc_heap_) {
3671     collector::GarbageCollector::ScopedPause pause(gc, false);
3672     PostGcVerificationPaused(gc);
3673   }
3674 }
3675 
RosAllocVerification(TimingLogger * timings,const char * name)3676 void Heap::RosAllocVerification(TimingLogger* timings, const char* name) {
3677   TimingLogger::ScopedTiming t(name, timings);
3678   for (const auto& space : continuous_spaces_) {
3679     if (space->IsRosAllocSpace()) {
3680       VLOG(heap) << name << " : " << space->GetName();
3681       space->AsRosAllocSpace()->Verify();
3682     }
3683   }
3684 }
3685 
WaitForGcToComplete(GcCause cause,Thread * self)3686 collector::GcType Heap::WaitForGcToComplete(GcCause cause, Thread* self) {
3687   ScopedThreadStateChange tsc(self, ThreadState::kWaitingForGcToComplete);
3688   MutexLock mu(self, *gc_complete_lock_);
3689   return WaitForGcToCompleteLocked(cause, self, /* only_one= */ true);
3690 }
3691 
WaitForGcToCompleteLocked(GcCause cause,Thread * self,bool only_one)3692 collector::GcType Heap::WaitForGcToCompleteLocked(GcCause cause, Thread* self, bool only_one) {
3693   gc_complete_cond_->CheckSafeToWait(self);
3694   collector::GcType last_gc_type = collector::kGcTypeNone;
3695   GcCause last_gc_cause = kGcCauseNone;
3696   if (collector_type_running_ != kCollectorTypeNone) {
3697     uint64_t wait_start = NanoTime();
3698     uint32_t starting_gc_num = GetCurrentGcNum();
3699     while (collector_type_running_ != kCollectorTypeNone &&
3700            (!only_one || GCNumberLt(GetCurrentGcNum(), starting_gc_num + 1))) {
3701       if (!task_processor_->IsRunningThread(self)) {
3702         // The current thread is about to wait for a currently running
3703         // collection to finish. If the waiting thread is not the heap
3704         // task daemon thread, the currently running collection is
3705         // considered as a blocking GC.
3706         running_collection_is_blocking_ = true;
3707         VLOG(gc) << "Waiting for a blocking GC " << cause;
3708       }
3709       SCOPED_TRACE << "GC: Wait For Completion " << cause;
3710       // We must wait, change thread state then sleep on gc_complete_cond_;
3711       gc_complete_cond_->Wait(self);
3712       last_gc_type = last_gc_type_;
3713       last_gc_cause = last_gc_cause_;
3714     }
3715     uint64_t wait_time = NanoTime() - wait_start;
3716     total_wait_time_ += wait_time;
3717     if (wait_time > long_pause_log_threshold_) {
3718       LOG(INFO) << "WaitForGcToComplete blocked " << cause << " on " << last_gc_cause << " for "
3719                 << PrettyDuration(wait_time);
3720     }
3721   }
3722   if (!task_processor_->IsRunningThread(self)) {
3723     // The current thread is about to run a collection. If the thread
3724     // is not the heap task daemon thread, it's considered as a
3725     // blocking GC (i.e., blocking itself).
3726     running_collection_is_blocking_ = true;
3727   }
3728   DCHECK(only_one || collector_type_running_ == kCollectorTypeNone);
3729   return last_gc_type;
3730 }
3731 
DumpForSigQuit(std::ostream & os)3732 void Heap::DumpForSigQuit(std::ostream& os) {
3733   os << "Heap: " << GetPercentFree() << "% free, " << PrettySize(GetBytesAllocated()) << "/"
3734      << PrettySize(GetTotalMemory()) << "\n";
3735   {
3736     os << "Image spaces:\n";
3737     ScopedObjectAccess soa(Thread::Current());
3738     for (const auto& space : continuous_spaces_) {
3739       if (space->IsImageSpace()) {
3740         os << space->GetName() << "\n";
3741       }
3742     }
3743   }
3744   DumpGcPerformanceInfo(os);
3745 }
3746 
GetPercentFree()3747 size_t Heap::GetPercentFree() {
3748   return static_cast<size_t>(100.0f * static_cast<float>(
3749       GetFreeMemory()) / target_footprint_.load(std::memory_order_relaxed));
3750 }
3751 
SetIdealFootprint(size_t target_footprint)3752 void Heap::SetIdealFootprint(size_t target_footprint) {
3753   if (target_footprint > GetMaxMemory()) {
3754     VLOG(gc) << "Clamp target GC heap from " << PrettySize(target_footprint) << " to "
3755              << PrettySize(GetMaxMemory());
3756     target_footprint = GetMaxMemory();
3757   }
3758   target_footprint_.store(target_footprint, std::memory_order_relaxed);
3759 }
3760 
IsMovableObject(ObjPtr<mirror::Object> obj) const3761 bool Heap::IsMovableObject(ObjPtr<mirror::Object> obj) const {
3762   if (kMovingCollector) {
3763     space::Space* space = FindContinuousSpaceFromObject(obj.Ptr(), true);
3764     if (space != nullptr) {
3765       // TODO: Check large object?
3766       return space->CanMoveObjects();
3767     }
3768   }
3769   return false;
3770 }
3771 
FindCollectorByGcType(collector::GcType gc_type)3772 collector::GarbageCollector* Heap::FindCollectorByGcType(collector::GcType gc_type) {
3773   for (auto* collector : garbage_collectors_) {
3774     if (collector->GetCollectorType() == collector_type_ &&
3775         collector->GetGcType() == gc_type) {
3776       return collector;
3777     }
3778   }
3779   return nullptr;
3780 }
3781 
HeapGrowthMultiplier() const3782 double Heap::HeapGrowthMultiplier() const {
3783   // If we don't care about pause times we are background, so return 1.0.
3784   if (!CareAboutPauseTimes()) {
3785     return 1.0;
3786   }
3787   return foreground_heap_growth_multiplier_;
3788 }
3789 
GrowForUtilization(collector::GarbageCollector * collector_ran,size_t bytes_allocated_before_gc)3790 void Heap::GrowForUtilization(collector::GarbageCollector* collector_ran,
3791                               size_t bytes_allocated_before_gc) {
3792   // We're running in the thread that set collector_type_running_ to something other than none,
3793   // thus ensuring that there is only one of us running. Thus
3794   // collector_type_running_ != kCollectorTypeNone, but that's a little tricky to turn into a
3795   // DCHECK.
3796 
3797   // We know what our utilization is at this moment.
3798   // This doesn't actually resize any memory. It just lets the heap grow more when necessary.
3799   const size_t bytes_allocated = GetBytesAllocated();
3800   // Trace the new heap size after the GC is finished.
3801   TraceHeapSize(bytes_allocated);
3802   uint64_t target_size, grow_bytes;
3803   collector::GcType gc_type = collector_ran->GetGcType();
3804   MutexLock mu(Thread::Current(), process_state_update_lock_);
3805   // Use the multiplier to grow more for foreground.
3806   const double multiplier = HeapGrowthMultiplier();
3807   if (gc_type != collector::kGcTypeSticky) {
3808     // Grow the heap for non sticky GC.
3809     uint64_t delta = bytes_allocated * (1.0 / GetTargetHeapUtilization() - 1.0);
3810     DCHECK_LE(delta, std::numeric_limits<size_t>::max()) << "bytes_allocated=" << bytes_allocated
3811         << " target_utilization_=" << target_utilization_;
3812     grow_bytes = std::min(delta, static_cast<uint64_t>(max_free_));
3813     grow_bytes = std::max(grow_bytes, static_cast<uint64_t>(min_free_));
3814     target_size = bytes_allocated + static_cast<uint64_t>(grow_bytes * multiplier);
3815     next_gc_type_ = collector::kGcTypeSticky;
3816   } else {
3817     collector::GcType non_sticky_gc_type = NonStickyGcType();
3818     // Find what the next non sticky collector will be.
3819     collector::GarbageCollector* non_sticky_collector = FindCollectorByGcType(non_sticky_gc_type);
3820     if (use_generational_gc_) {
3821       if (non_sticky_collector == nullptr) {
3822         non_sticky_collector = FindCollectorByGcType(collector::kGcTypePartial);
3823       }
3824       CHECK(non_sticky_collector != nullptr);
3825     }
3826     double sticky_gc_throughput_adjustment = GetStickyGcThroughputAdjustment(use_generational_gc_);
3827 
3828     // If the throughput of the current sticky GC >= throughput of the non sticky collector, then
3829     // do another sticky collection next.
3830     // We also check that the bytes allocated aren't over the target_footprint, or
3831     // concurrent_start_bytes in case of concurrent GCs, in order to prevent a
3832     // pathological case where dead objects which aren't reclaimed by sticky could get accumulated
3833     // if the sticky GC throughput always remained >= the full/partial throughput.
3834     size_t target_footprint = target_footprint_.load(std::memory_order_relaxed);
3835     if (current_gc_iteration_.GetEstimatedThroughput() * sticky_gc_throughput_adjustment >=
3836         non_sticky_collector->GetEstimatedMeanThroughput() &&
3837         non_sticky_collector->NumberOfIterations() > 0 &&
3838         bytes_allocated <= (IsGcConcurrent() ? concurrent_start_bytes_ : target_footprint)) {
3839       next_gc_type_ = collector::kGcTypeSticky;
3840     } else {
3841       next_gc_type_ = non_sticky_gc_type;
3842     }
3843     // If we have freed enough memory, shrink the heap back down.
3844     const size_t adjusted_max_free = static_cast<size_t>(max_free_ * multiplier);
3845     if (bytes_allocated + adjusted_max_free < target_footprint) {
3846       target_size = bytes_allocated + adjusted_max_free;
3847       grow_bytes = max_free_;
3848     } else {
3849       target_size = std::max(bytes_allocated, target_footprint);
3850       // The same whether jank perceptible or not; just avoid the adjustment.
3851       grow_bytes = 0;
3852     }
3853   }
3854   CHECK_LE(target_size, std::numeric_limits<size_t>::max())
3855       << " bytes_allocated:" << bytes_allocated
3856       << " bytes_freed:" << current_gc_iteration_.GetFreedBytes()
3857       << " large_obj_bytes_freed:" << current_gc_iteration_.GetFreedLargeObjectBytes();
3858   if (!ignore_target_footprint_) {
3859     SetIdealFootprint(target_size);
3860     // Store target size (computed with foreground heap growth multiplier) for updating
3861     // target_footprint_ when process state switches to foreground.
3862     // target_size = 0 ensures that target_footprint_ is not updated on
3863     // process-state switch.
3864     min_foreground_target_footprint_ =
3865         (multiplier <= 1.0 && grow_bytes > 0)
3866         ? std::min(
3867           bytes_allocated + static_cast<size_t>(grow_bytes * foreground_heap_growth_multiplier_),
3868           GetMaxMemory())
3869         : 0;
3870 
3871     if (IsGcConcurrent()) {
3872       const uint64_t freed_bytes = current_gc_iteration_.GetFreedBytes() +
3873           current_gc_iteration_.GetFreedLargeObjectBytes() +
3874           current_gc_iteration_.GetFreedRevokeBytes();
3875       // Records the number of bytes allocated at the time of GC finish,excluding the number of
3876       // bytes allocated during GC.
3877       num_bytes_alive_after_gc_ = UnsignedDifference(bytes_allocated_before_gc, freed_bytes);
3878       // Bytes allocated will shrink by freed_bytes after the GC runs, so if we want to figure out
3879       // how many bytes were allocated during the GC we need to add freed_bytes back on.
3880       // Almost always bytes_allocated + freed_bytes >= bytes_allocated_before_gc.
3881       const size_t bytes_allocated_during_gc =
3882           UnsignedDifference(bytes_allocated + freed_bytes, bytes_allocated_before_gc);
3883       // Calculate when to perform the next ConcurrentGC.
3884       // Estimate how many remaining bytes we will have when we need to start the next GC.
3885       size_t remaining_bytes = bytes_allocated_during_gc;
3886       remaining_bytes = std::min(remaining_bytes, kMaxConcurrentRemainingBytes);
3887       remaining_bytes = std::max(remaining_bytes, kMinConcurrentRemainingBytes);
3888       size_t target_footprint = target_footprint_.load(std::memory_order_relaxed);
3889       if (UNLIKELY(remaining_bytes > target_footprint)) {
3890         // A never going to happen situation that from the estimated allocation rate we will exceed
3891         // the applications entire footprint with the given estimated allocation rate. Schedule
3892         // another GC nearly straight away.
3893         remaining_bytes = std::min(kMinConcurrentRemainingBytes, target_footprint);
3894       }
3895       DCHECK_LE(target_footprint_.load(std::memory_order_relaxed), GetMaxMemory());
3896       // Start a concurrent GC when we get close to the estimated remaining bytes. When the
3897       // allocation rate is very high, remaining_bytes could tell us that we should start a GC
3898       // right away.
3899       concurrent_start_bytes_ = std::max(target_footprint - remaining_bytes, bytes_allocated);
3900       // Store concurrent_start_bytes_ (computed with foreground heap growth multiplier) for update
3901       // itself when process state switches to foreground.
3902       min_foreground_concurrent_start_bytes_ =
3903           min_foreground_target_footprint_ != 0
3904           ? std::max(min_foreground_target_footprint_ - remaining_bytes, bytes_allocated)
3905           : 0;
3906     }
3907   }
3908 }
3909 
ClampGrowthLimit()3910 void Heap::ClampGrowthLimit() {
3911   // Use heap bitmap lock to guard against races with BindLiveToMarkBitmap.
3912   ScopedObjectAccess soa(Thread::Current());
3913   WriterMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_);
3914   capacity_ = growth_limit_;
3915   for (const auto& space : continuous_spaces_) {
3916     if (space->IsMallocSpace()) {
3917       gc::space::MallocSpace* malloc_space = space->AsMallocSpace();
3918       malloc_space->ClampGrowthLimit();
3919     }
3920   }
3921   if (large_object_space_ != nullptr) {
3922     large_object_space_->ClampGrowthLimit(capacity_);
3923   }
3924   if (collector_type_ == kCollectorTypeCC) {
3925     DCHECK(region_space_ != nullptr);
3926     // Twice the capacity as CC needs extra space for evacuating objects.
3927     region_space_->ClampGrowthLimit(2 * capacity_);
3928   } else if (collector_type_ == kCollectorTypeCMC) {
3929     DCHECK(gUseUserfaultfd);
3930     DCHECK_NE(mark_compact_, nullptr);
3931     DCHECK_NE(bump_pointer_space_, nullptr);
3932     mark_compact_->ClampGrowthLimit(capacity_);
3933   }
3934   // This space isn't added for performance reasons.
3935   if (main_space_backup_.get() != nullptr) {
3936     main_space_backup_->ClampGrowthLimit();
3937   }
3938 }
3939 
ClearGrowthLimit()3940 void Heap::ClearGrowthLimit() {
3941   if (target_footprint_.load(std::memory_order_relaxed) == growth_limit_
3942       && growth_limit_ < capacity_) {
3943     target_footprint_.store(capacity_, std::memory_order_relaxed);
3944     SetDefaultConcurrentStartBytes();
3945   }
3946   growth_limit_ = capacity_;
3947   ScopedObjectAccess soa(Thread::Current());
3948   for (const auto& space : continuous_spaces_) {
3949     if (space->IsMallocSpace()) {
3950       gc::space::MallocSpace* malloc_space = space->AsMallocSpace();
3951       malloc_space->ClearGrowthLimit();
3952       malloc_space->SetFootprintLimit(malloc_space->Capacity());
3953     }
3954   }
3955   // This space isn't added for performance reasons.
3956   if (main_space_backup_.get() != nullptr) {
3957     main_space_backup_->ClearGrowthLimit();
3958     main_space_backup_->SetFootprintLimit(main_space_backup_->Capacity());
3959   }
3960 }
3961 
AddFinalizerReference(Thread * self,ObjPtr<mirror::Object> * object)3962 void Heap::AddFinalizerReference(Thread* self, ObjPtr<mirror::Object>* object) {
3963   ScopedObjectAccess soa(self);
3964   StackHandleScope<1u> hs(self);
3965   // Use handle wrapper to update the `*object` if the object gets moved.
3966   HandleWrapperObjPtr<mirror::Object> h_object = hs.NewHandleWrapper(object);
3967   WellKnownClasses::java_lang_ref_FinalizerReference_add->InvokeStatic<'V', 'L'>(
3968       self, h_object.Get());
3969 }
3970 
RequestConcurrentGCAndSaveObject(Thread * self,bool force_full,uint32_t observed_gc_num,ObjPtr<mirror::Object> * obj)3971 void Heap::RequestConcurrentGCAndSaveObject(Thread* self,
3972                                             bool force_full,
3973                                             uint32_t observed_gc_num,
3974                                             ObjPtr<mirror::Object>* obj) {
3975   StackHandleScope<1> hs(self);
3976   HandleWrapperObjPtr<mirror::Object> wrapper(hs.NewHandleWrapper(obj));
3977   RequestConcurrentGC(self, kGcCauseBackground, force_full, observed_gc_num);
3978 }
3979 
3980 class Heap::ConcurrentGCTask : public HeapTask {
3981  public:
ConcurrentGCTask(uint64_t target_time,GcCause cause,bool force_full,uint32_t gc_num)3982   ConcurrentGCTask(uint64_t target_time, GcCause cause, bool force_full, uint32_t gc_num)
3983       : HeapTask(target_time), cause_(cause), force_full_(force_full), my_gc_num_(gc_num) {}
Run(Thread * self)3984   void Run(Thread* self) override {
3985     Runtime* runtime = Runtime::Current();
3986     gc::Heap* heap = runtime->GetHeap();
3987     DCHECK(GCNumberLt(my_gc_num_, heap->GetCurrentGcNum() + 2));  // <= current_gc_num + 1
3988     heap->ConcurrentGC(self, cause_, force_full_, my_gc_num_);
3989     CHECK_IMPLIES(GCNumberLt(heap->GetCurrentGcNum(), my_gc_num_), runtime->IsShuttingDown(self));
3990   }
3991 
3992  private:
3993   const GcCause cause_;
3994   const bool force_full_;  // If true, force full (or partial) collection.
3995   const uint32_t my_gc_num_;  // Sequence number of requested GC.
3996 };
3997 
CanAddHeapTask(Thread * self)3998 static bool CanAddHeapTask(Thread* self) REQUIRES(!Locks::runtime_shutdown_lock_) {
3999   Runtime* runtime = Runtime::Current();
4000   // We only care if the native stack has overflowed. If the simulated stack overflows, it is still
4001   // possible that the native stack has room to add a heap task.
4002   return runtime != nullptr && runtime->IsFinishedStarting() && !runtime->IsShuttingDown(self) &&
4003       !self->IsHandlingStackOverflow<kNativeStackType>();
4004 }
4005 
RequestConcurrentGC(Thread * self,GcCause cause,bool force_full,uint32_t observed_gc_num)4006 bool Heap::RequestConcurrentGC(Thread* self,
4007                                GcCause cause,
4008                                bool force_full,
4009                                uint32_t observed_gc_num) {
4010   uint32_t max_gc_requested = max_gc_requested_.load(std::memory_order_relaxed);
4011   if (!GCNumberLt(observed_gc_num, max_gc_requested)) {
4012     // observed_gc_num >= max_gc_requested: Nobody beat us to requesting the next gc.
4013     if (CanAddHeapTask(self)) {
4014       // Since observed_gc_num >= max_gc_requested, this increases max_gc_requested_, if successful.
4015       if (max_gc_requested_.CompareAndSetStrongRelaxed(max_gc_requested, observed_gc_num + 1)) {
4016         task_processor_->AddTask(self, new ConcurrentGCTask(NanoTime(),  // Start straight away.
4017                                                             cause,
4018                                                             force_full,
4019                                                             observed_gc_num + 1));
4020       }
4021       DCHECK(GCNumberLt(observed_gc_num, max_gc_requested_.load(std::memory_order_relaxed)));
4022       // If we increased max_gc_requested_, then we added a task that will eventually cause
4023       // gcs_completed_ to be incremented (to at least observed_gc_num + 1).
4024       // If the CAS failed, somebody else did.
4025       return true;
4026     }
4027     return false;
4028   }
4029   return true;  // Vacuously.
4030 }
4031 
ConcurrentGC(Thread * self,GcCause cause,bool force_full,uint32_t requested_gc_num)4032 void Heap::ConcurrentGC(Thread* self, GcCause cause, bool force_full, uint32_t requested_gc_num) {
4033   if (!Runtime::Current()->IsShuttingDown(self)) {
4034     // Wait for any GCs currently running to finish. If this incremented GC number, we're done.
4035     WaitForGcToComplete(cause, self);
4036     if (GCNumberLt(GetCurrentGcNum(), requested_gc_num)) {
4037       collector::GcType next_gc_type = next_gc_type_;
4038       // If forcing full and next gc type is sticky, override with a non-sticky type.
4039       if (force_full && next_gc_type == collector::kGcTypeSticky) {
4040         next_gc_type = NonStickyGcType();
4041       }
4042       // If we can't run the GC type we wanted to run, find the next appropriate one and try
4043       // that instead. E.g. can't do partial, so do full instead.
4044       // We must ensure that we run something that ends up incrementing gcs_completed_.
4045       // In the kGcTypePartial case, the initial CollectGarbageInternal call may not have that
4046       // effect, but the subsequent KGcTypeFull call will.
4047       if (CollectGarbageInternal(next_gc_type, cause, false, requested_gc_num)
4048           == collector::kGcTypeNone) {
4049         for (collector::GcType gc_type : gc_plan_) {
4050           if (!GCNumberLt(GetCurrentGcNum(), requested_gc_num)) {
4051             // Somebody did it for us.
4052             break;
4053           }
4054           // Attempt to run the collector, if we succeed, we are done.
4055           if (gc_type > next_gc_type &&
4056               CollectGarbageInternal(gc_type, cause, false, requested_gc_num)
4057               != collector::kGcTypeNone) {
4058             break;
4059           }
4060         }
4061       }
4062     }
4063   }
4064 }
4065 
4066 class Heap::CollectorTransitionTask : public HeapTask {
4067  public:
CollectorTransitionTask(uint64_t target_time)4068   explicit CollectorTransitionTask(uint64_t target_time) : HeapTask(target_time) {}
4069 
Run(Thread * self)4070   void Run(Thread* self) override {
4071     gc::Heap* heap = Runtime::Current()->GetHeap();
4072     heap->DoPendingCollectorTransition();
4073     heap->ClearPendingCollectorTransition(self);
4074   }
4075 };
4076 
ClearPendingCollectorTransition(Thread * self)4077 void Heap::ClearPendingCollectorTransition(Thread* self) {
4078   MutexLock mu(self, *pending_task_lock_);
4079   pending_collector_transition_ = nullptr;
4080 }
4081 
RequestCollectorTransition(CollectorType desired_collector_type,uint64_t delta_time)4082 void Heap::RequestCollectorTransition(CollectorType desired_collector_type, uint64_t delta_time) {
4083   Thread* self = Thread::Current();
4084   desired_collector_type_ = desired_collector_type;
4085   if (desired_collector_type_ == collector_type_ || !CanAddHeapTask(self)) {
4086     return;
4087   }
4088   if (collector_type_ == kCollectorTypeCC) {
4089     // For CC, we invoke a full compaction when going to the background, but the collector type
4090     // doesn't change.
4091     DCHECK_EQ(desired_collector_type_, kCollectorTypeCCBackground);
4092   }
4093   if (collector_type_ == kCollectorTypeCMC) {
4094     // For CMC collector type doesn't change.
4095     DCHECK_EQ(desired_collector_type_, kCollectorTypeCMCBackground);
4096   }
4097   DCHECK_NE(collector_type_, kCollectorTypeCCBackground);
4098   DCHECK_NE(collector_type_, kCollectorTypeCMCBackground);
4099   CollectorTransitionTask* added_task = nullptr;
4100   const uint64_t target_time = NanoTime() + delta_time;
4101   {
4102     MutexLock mu(self, *pending_task_lock_);
4103     // If we have an existing collector transition, update the target time to be the new target.
4104     if (pending_collector_transition_ != nullptr) {
4105       task_processor_->UpdateTargetRunTime(self, pending_collector_transition_, target_time);
4106       return;
4107     }
4108     added_task = new CollectorTransitionTask(target_time);
4109     pending_collector_transition_ = added_task;
4110   }
4111   task_processor_->AddTask(self, added_task);
4112 }
4113 
4114 class Heap::HeapTrimTask : public HeapTask {
4115  public:
HeapTrimTask(uint64_t delta_time)4116   explicit HeapTrimTask(uint64_t delta_time) : HeapTask(NanoTime() + delta_time) { }
Run(Thread * self)4117   void Run(Thread* self) override {
4118     gc::Heap* heap = Runtime::Current()->GetHeap();
4119     heap->Trim(self);
4120     heap->ClearPendingTrim(self);
4121   }
4122 };
4123 
ClearPendingTrim(Thread * self)4124 void Heap::ClearPendingTrim(Thread* self) {
4125   MutexLock mu(self, *pending_task_lock_);
4126   pending_heap_trim_ = nullptr;
4127 }
4128 
RequestTrim(Thread * self)4129 void Heap::RequestTrim(Thread* self) {
4130   if (!CanAddHeapTask(self)) {
4131     return;
4132   }
4133   // GC completed and now we must decide whether to request a heap trim (advising pages back to the
4134   // kernel) or not. Issuing a request will also cause trimming of the libc heap. As a trim scans
4135   // a space it will hold its lock and can become a cause of jank.
4136   // Note, the large object space self trims and the Zygote space was trimmed and unchanging since
4137   // forking.
4138 
4139   // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap
4140   // because that only marks object heads, so a large array looks like lots of empty space. We
4141   // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional
4142   // to utilization (which is probably inversely proportional to how much benefit we can expect).
4143   // We could try mincore(2) but that's only a measure of how many pages we haven't given away,
4144   // not how much use we're making of those pages.
4145   HeapTrimTask* added_task = nullptr;
4146   {
4147     MutexLock mu(self, *pending_task_lock_);
4148     if (pending_heap_trim_ != nullptr) {
4149       // Already have a heap trim request in task processor, ignore this request.
4150       return;
4151     }
4152     added_task = new HeapTrimTask(kHeapTrimWait);
4153     pending_heap_trim_ = added_task;
4154   }
4155   task_processor_->AddTask(self, added_task);
4156 }
4157 
IncrementNumberOfBytesFreedRevoke(size_t freed_bytes_revoke)4158 void Heap::IncrementNumberOfBytesFreedRevoke(size_t freed_bytes_revoke) {
4159   size_t previous_num_bytes_freed_revoke =
4160       num_bytes_freed_revoke_.fetch_add(freed_bytes_revoke, std::memory_order_relaxed);
4161   // Check the updated value is less than the number of bytes allocated. There is a risk of
4162   // execution being suspended between the increment above and the CHECK below, leading to
4163   // the use of previous_num_bytes_freed_revoke in the comparison.
4164   CHECK_GE(num_bytes_allocated_.load(std::memory_order_relaxed),
4165            previous_num_bytes_freed_revoke + freed_bytes_revoke);
4166 }
4167 
RevokeThreadLocalBuffers(Thread * thread)4168 void Heap::RevokeThreadLocalBuffers(Thread* thread) {
4169   if (rosalloc_space_ != nullptr) {
4170     size_t freed_bytes_revoke = rosalloc_space_->RevokeThreadLocalBuffers(thread);
4171     if (freed_bytes_revoke > 0U) {
4172       IncrementNumberOfBytesFreedRevoke(freed_bytes_revoke);
4173     }
4174   }
4175   if (bump_pointer_space_ != nullptr) {
4176     CHECK_EQ(bump_pointer_space_->RevokeThreadLocalBuffers(thread), 0U);
4177   }
4178   if (region_space_ != nullptr) {
4179     CHECK_EQ(region_space_->RevokeThreadLocalBuffers(thread), 0U);
4180   }
4181 }
4182 
RevokeRosAllocThreadLocalBuffers(Thread * thread)4183 void Heap::RevokeRosAllocThreadLocalBuffers(Thread* thread) {
4184   if (rosalloc_space_ != nullptr) {
4185     size_t freed_bytes_revoke = rosalloc_space_->RevokeThreadLocalBuffers(thread);
4186     if (freed_bytes_revoke > 0U) {
4187       IncrementNumberOfBytesFreedRevoke(freed_bytes_revoke);
4188     }
4189   }
4190 }
4191 
RevokeAllThreadLocalBuffers()4192 void Heap::RevokeAllThreadLocalBuffers() {
4193   if (rosalloc_space_ != nullptr) {
4194     size_t freed_bytes_revoke = rosalloc_space_->RevokeAllThreadLocalBuffers();
4195     if (freed_bytes_revoke > 0U) {
4196       IncrementNumberOfBytesFreedRevoke(freed_bytes_revoke);
4197     }
4198   }
4199   if (bump_pointer_space_ != nullptr) {
4200     CHECK_EQ(bump_pointer_space_->RevokeAllThreadLocalBuffers(), 0U);
4201   }
4202   if (region_space_ != nullptr) {
4203     CHECK_EQ(region_space_->RevokeAllThreadLocalBuffers(), 0U);
4204   }
4205 }
4206 
4207 // For GC triggering purposes, we count old (pre-last-GC) and new native allocations as
4208 // different fractions of Java allocations.
4209 // For now, we essentially do not count old native allocations at all, so that we can preserve the
4210 // existing behavior of not limiting native heap size. If we seriously considered it, we would
4211 // have to adjust collection thresholds when we encounter large amounts of old native memory,
4212 // and handle native out-of-memory situations.
4213 
4214 static constexpr size_t kOldNativeDiscountFactor = 65536;  // Approximately infinite for now.
4215 static constexpr size_t kNewNativeDiscountFactor = 2;
4216 
4217 // If weighted java + native memory use exceeds our target by kStopForNativeFactor, and
4218 // newly allocated memory exceeds stop_for_native_allocs_, we wait for GC to complete to avoid
4219 // running out of memory.
4220 static constexpr float kStopForNativeFactor = 4.0;
4221 
4222 // Return the ratio of the weighted native + java allocated bytes to its target value.
4223 // A return value > 1.0 means we should collect. Significantly larger values mean we're falling
4224 // behind.
NativeMemoryOverTarget(size_t current_native_bytes,bool is_gc_concurrent)4225 inline float Heap::NativeMemoryOverTarget(size_t current_native_bytes, bool is_gc_concurrent) {
4226   // Collection check for native allocation. Does not enforce Java heap bounds.
4227   // With adj_start_bytes defined below, effectively checks
4228   // <java bytes allocd> + c1*<old native allocd> + c2*<new native allocd) >= adj_start_bytes,
4229   // where c3 > 1, and currently c1 and c2 are 1 divided by the values defined above.
4230   size_t old_native_bytes = old_native_bytes_allocated_.load(std::memory_order_relaxed);
4231   if (old_native_bytes > current_native_bytes) {
4232     // Net decrease; skip the check, but update old value.
4233     // It's OK to lose an update if two stores race.
4234     old_native_bytes_allocated_.store(current_native_bytes, std::memory_order_relaxed);
4235     return 0.0;
4236   } else {
4237     size_t new_native_bytes = UnsignedDifference(current_native_bytes, old_native_bytes);
4238     size_t weighted_native_bytes = new_native_bytes / kNewNativeDiscountFactor
4239         + old_native_bytes / kOldNativeDiscountFactor;
4240     size_t add_bytes_allowed = static_cast<size_t>(
4241         NativeAllocationGcWatermark() * HeapGrowthMultiplier());
4242     size_t java_gc_start_bytes = is_gc_concurrent
4243         ? concurrent_start_bytes_
4244         : target_footprint_.load(std::memory_order_relaxed);
4245     size_t adj_start_bytes = UnsignedSum(java_gc_start_bytes,
4246                                          add_bytes_allowed / kNewNativeDiscountFactor);
4247     return static_cast<float>(GetBytesAllocated() + weighted_native_bytes)
4248          / static_cast<float>(adj_start_bytes);
4249   }
4250 }
4251 
CheckGCForNative(Thread * self)4252 inline void Heap::CheckGCForNative(Thread* self) {
4253   bool is_gc_concurrent = IsGcConcurrent();
4254   uint32_t starting_gc_num = GetCurrentGcNum();
4255   size_t current_native_bytes = GetNativeBytes();
4256   float gc_urgency = NativeMemoryOverTarget(current_native_bytes, is_gc_concurrent);
4257   if (UNLIKELY(gc_urgency >= 1.0)) {
4258     if (is_gc_concurrent) {
4259       bool requested =
4260           RequestConcurrentGC(self, kGcCauseForNativeAlloc, /*force_full=*/true, starting_gc_num);
4261       if (requested && gc_urgency > kStopForNativeFactor
4262           && current_native_bytes > stop_for_native_allocs_) {
4263         // We're in danger of running out of memory due to rampant native allocation.
4264         if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
4265           LOG(INFO) << "Stopping for native allocation, urgency: " << gc_urgency;
4266         }
4267         // Count how many times we do this, so we can warn if this becomes excessive.
4268         // Stop after a while, out of excessive caution.
4269         static constexpr int kGcWaitIters = 20;
4270         for (int i = 1; i <= kGcWaitIters; ++i) {
4271           if (!GCNumberLt(GetCurrentGcNum(), max_gc_requested_.load(std::memory_order_relaxed))
4272               || WaitForGcToComplete(kGcCauseForNativeAlloc, self) != collector::kGcTypeNone) {
4273             break;
4274           }
4275           CHECK(GCNumberLt(starting_gc_num, max_gc_requested_.load(std::memory_order_relaxed)));
4276           if (i % 10 == 0) {
4277             LOG(WARNING) << "Slept " << i << " times in native allocation, waiting for GC";
4278           }
4279           static constexpr int kGcWaitSleepMicros = 2000;
4280           usleep(kGcWaitSleepMicros);  // Encourage our requested GC to start.
4281         }
4282       }
4283     } else {
4284       CollectGarbageInternal(NonStickyGcType(), kGcCauseForNativeAlloc, false, starting_gc_num + 1);
4285     }
4286   }
4287 }
4288 
4289 // About kNotifyNativeInterval allocations have occurred. Check whether we should garbage collect.
NotifyNativeAllocations(JNIEnv * env)4290 void Heap::NotifyNativeAllocations(JNIEnv* env) {
4291   native_objects_notified_.fetch_add(kNotifyNativeInterval, std::memory_order_relaxed);
4292   CheckGCForNative(Thread::ForEnv(env));
4293 }
4294 
4295 // Register a native allocation with an explicit size.
4296 // This should only be done for large allocations of non-malloc memory, which we wouldn't
4297 // otherwise see.
RegisterNativeAllocation(JNIEnv * env,size_t bytes)4298 void Heap::RegisterNativeAllocation(JNIEnv* env, size_t bytes) {
4299   // Cautiously check for a wrapped negative bytes argument.
4300   DCHECK(sizeof(size_t) < 8 || bytes < (std::numeric_limits<size_t>::max() / 2));
4301   native_bytes_registered_.fetch_add(bytes, std::memory_order_relaxed);
4302   uint32_t objects_notified =
4303       native_objects_notified_.fetch_add(1, std::memory_order_relaxed);
4304   if (objects_notified % kNotifyNativeInterval == kNotifyNativeInterval - 1
4305       || bytes > kCheckImmediatelyThreshold) {
4306     CheckGCForNative(Thread::ForEnv(env));
4307   }
4308   // Heap profiler treats this as a Java allocation with a null object.
4309   if (GetHeapSampler().IsEnabled()) {
4310     JHPCheckNonTlabSampleAllocation(Thread::Current(), nullptr, bytes);
4311   }
4312 }
4313 
RegisterNativeFree(JNIEnv *,size_t bytes)4314 void Heap::RegisterNativeFree(JNIEnv*, size_t bytes) {
4315   size_t allocated;
4316   size_t new_freed_bytes;
4317   do {
4318     allocated = native_bytes_registered_.load(std::memory_order_relaxed);
4319     new_freed_bytes = std::min(allocated, bytes);
4320     // We should not be registering more free than allocated bytes.
4321     // But correctly keep going in non-debug builds.
4322     DCHECK_EQ(new_freed_bytes, bytes);
4323   } while (!native_bytes_registered_.CompareAndSetWeakRelaxed(allocated,
4324                                                               allocated - new_freed_bytes));
4325 }
4326 
GetTotalMemory() const4327 size_t Heap::GetTotalMemory() const {
4328   return std::max(target_footprint_.load(std::memory_order_relaxed), GetBytesAllocated());
4329 }
4330 
AddModUnionTable(accounting::ModUnionTable * mod_union_table)4331 void Heap::AddModUnionTable(accounting::ModUnionTable* mod_union_table) {
4332   DCHECK(mod_union_table != nullptr);
4333   mod_union_tables_.Put(mod_union_table->GetSpace(), mod_union_table);
4334 }
4335 
CheckPreconditionsForAllocObject(ObjPtr<mirror::Class> c,size_t byte_count)4336 void Heap::CheckPreconditionsForAllocObject(ObjPtr<mirror::Class> c, size_t byte_count) {
4337   // Compare rounded sizes since the allocation may have been retried after rounding the size.
4338   // See b/37885600
4339   CHECK(c == nullptr || (c->IsClassClass() && byte_count >= sizeof(mirror::Class)) ||
4340         (c->IsVariableSize() ||
4341             RoundUp(c->GetObjectSize(), kObjectAlignment) ==
4342                 RoundUp(byte_count, kObjectAlignment)))
4343       << "ClassFlags=" << c->GetClassFlags()
4344       << " IsClassClass=" << c->IsClassClass()
4345       << " byte_count=" << byte_count
4346       << " IsVariableSize=" << c->IsVariableSize()
4347       << " ObjectSize=" << c->GetObjectSize()
4348       << " sizeof(Class)=" << sizeof(mirror::Class)
4349       << " " << verification_->DumpObjectInfo(c.Ptr(), /*tag=*/ "klass");
4350   CHECK_GE(byte_count, sizeof(mirror::Object));
4351 }
4352 
AddRememberedSet(accounting::RememberedSet * remembered_set)4353 void Heap::AddRememberedSet(accounting::RememberedSet* remembered_set) {
4354   CHECK(remembered_set != nullptr);
4355   space::Space* space = remembered_set->GetSpace();
4356   CHECK(space != nullptr);
4357   CHECK(remembered_sets_.find(space) == remembered_sets_.end()) << space;
4358   remembered_sets_.Put(space, remembered_set);
4359   CHECK(remembered_sets_.find(space) != remembered_sets_.end()) << space;
4360 }
4361 
RemoveRememberedSet(space::Space * space)4362 void Heap::RemoveRememberedSet(space::Space* space) {
4363   CHECK(space != nullptr);
4364   auto it = remembered_sets_.find(space);
4365   CHECK(it != remembered_sets_.end());
4366   delete it->second;
4367   remembered_sets_.erase(it);
4368   CHECK(remembered_sets_.find(space) == remembered_sets_.end());
4369 }
4370 
ClearMarkedObjects(bool release_eagerly)4371 void Heap::ClearMarkedObjects(bool release_eagerly) {
4372   // Clear all of the spaces' mark bitmaps.
4373   for (const auto& space : GetContinuousSpaces()) {
4374     if (space->GetLiveBitmap() != nullptr && !space->HasBoundBitmaps()) {
4375       space->GetMarkBitmap()->Clear(release_eagerly);
4376     }
4377   }
4378   // Clear the marked objects in the discontinous space object sets.
4379   for (const auto& space : GetDiscontinuousSpaces()) {
4380     space->GetMarkBitmap()->Clear(release_eagerly);
4381   }
4382 }
4383 
SetAllocationRecords(AllocRecordObjectMap * records)4384 void Heap::SetAllocationRecords(AllocRecordObjectMap* records) {
4385   allocation_records_.reset(records);
4386 }
4387 
VisitAllocationRecords(RootVisitor * visitor) const4388 void Heap::VisitAllocationRecords(RootVisitor* visitor) const {
4389   if (IsAllocTrackingEnabled()) {
4390     MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
4391     if (IsAllocTrackingEnabled()) {
4392       GetAllocationRecords()->VisitRoots(visitor);
4393     }
4394   }
4395 }
4396 
SweepAllocationRecords(IsMarkedVisitor * visitor) const4397 void Heap::SweepAllocationRecords(IsMarkedVisitor* visitor) const {
4398   if (IsAllocTrackingEnabled()) {
4399     MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
4400     if (IsAllocTrackingEnabled()) {
4401       GetAllocationRecords()->SweepAllocationRecords(visitor);
4402     }
4403   }
4404 }
4405 
AllowNewAllocationRecords() const4406 void Heap::AllowNewAllocationRecords() const {
4407   CHECK(!gUseReadBarrier);
4408   MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
4409   AllocRecordObjectMap* allocation_records = GetAllocationRecords();
4410   if (allocation_records != nullptr) {
4411     allocation_records->AllowNewAllocationRecords();
4412   }
4413 }
4414 
DisallowNewAllocationRecords() const4415 void Heap::DisallowNewAllocationRecords() const {
4416   CHECK(!gUseReadBarrier);
4417   MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
4418   AllocRecordObjectMap* allocation_records = GetAllocationRecords();
4419   if (allocation_records != nullptr) {
4420     allocation_records->DisallowNewAllocationRecords();
4421   }
4422 }
4423 
BroadcastForNewAllocationRecords() const4424 void Heap::BroadcastForNewAllocationRecords() const {
4425   // Always broadcast without checking IsAllocTrackingEnabled() because IsAllocTrackingEnabled() may
4426   // be set to false while some threads are waiting for system weak access in
4427   // AllocRecordObjectMap::RecordAllocation() and we may fail to wake them up. b/27467554.
4428   MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
4429   AllocRecordObjectMap* allocation_records = GetAllocationRecords();
4430   if (allocation_records != nullptr) {
4431     allocation_records->BroadcastForNewAllocationRecords();
4432   }
4433 }
4434 
4435 // Perfetto Java Heap Profiler Support.
4436 
4437 // Perfetto initialization.
InitPerfettoJavaHeapProf()4438 void Heap::InitPerfettoJavaHeapProf() {
4439   // Initialize Perfetto Heap info and Heap id.
4440   uint32_t heap_id = 1;  // Initialize to 1, to be overwritten by Perfetto heap id.
4441 #ifdef ART_TARGET_ANDROID
4442   // Register the heap and create the heapid.
4443   // Use a Perfetto heap name = "com.android.art" for the Java Heap Profiler.
4444   AHeapInfo* info = AHeapInfo_create("com.android.art");
4445   // Set the Enable Callback, there is no callback data ("nullptr").
4446   AHeapInfo_setEnabledCallback(info, &EnableHeapSamplerCallback, &heap_sampler_);
4447   // Set the Disable Callback.
4448   AHeapInfo_setDisabledCallback(info, &DisableHeapSamplerCallback, &heap_sampler_);
4449   heap_id = AHeapProfile_registerHeap(info);
4450   // Do not enable the Java Heap Profiler in this case, wait for Perfetto to enable it through
4451   // the callback function.
4452 #else
4453   // This is the host case, enable the Java Heap Profiler for host testing.
4454   // Perfetto API is currently not available on host.
4455   heap_sampler_.EnableHeapSampler();
4456 #endif
4457   heap_sampler_.SetHeapID(heap_id);
4458   VLOG(heap) << "Java Heap Profiler Initialized";
4459 }
4460 
JHPCheckNonTlabSampleAllocation(Thread * self,mirror::Object * obj,size_t alloc_size)4461 void Heap::JHPCheckNonTlabSampleAllocation(Thread* self, mirror::Object* obj, size_t alloc_size) {
4462   bool take_sample = false;
4463   size_t bytes_until_sample = 0;
4464   HeapSampler& prof_heap_sampler = GetHeapSampler();
4465   // An allocation occurred, sample it, even if non-Tlab.
4466   // In case take_sample is already set from the previous GetSampleOffset
4467   // because we tried the Tlab allocation first, we will not use this value.
4468   // A new value is generated below. Also bytes_until_sample will be updated.
4469   // Note that we are not using the return value from the GetSampleOffset in
4470   // the NonTlab case here.
4471   prof_heap_sampler.GetSampleOffset(
4472       alloc_size, self->GetTlabPosOffset(), &take_sample, &bytes_until_sample);
4473   prof_heap_sampler.SetBytesUntilSample(bytes_until_sample);
4474   if (take_sample) {
4475     prof_heap_sampler.ReportSample(obj, alloc_size);
4476   }
4477   VLOG(heap) << "JHP:NonTlab Non-moving or Large Allocation or RegisterNativeAllocation";
4478 }
4479 
JHPCalculateNextTlabSize(Thread * self,size_t jhp_def_tlab_size,size_t alloc_size,bool * take_sample,size_t * bytes_until_sample)4480 size_t Heap::JHPCalculateNextTlabSize(Thread* self,
4481                                       size_t jhp_def_tlab_size,
4482                                       size_t alloc_size,
4483                                       bool* take_sample,
4484                                       size_t* bytes_until_sample) {
4485   size_t next_sample_point = GetHeapSampler().GetSampleOffset(
4486       alloc_size, self->GetTlabPosOffset(), take_sample, bytes_until_sample);
4487   return std::min(next_sample_point, jhp_def_tlab_size);
4488 }
4489 
AdjustSampleOffset(size_t adjustment)4490 void Heap::AdjustSampleOffset(size_t adjustment) {
4491   GetHeapSampler().AdjustSampleOffset(adjustment);
4492 }
4493 
CheckGcStressMode(Thread * self,ObjPtr<mirror::Object> * obj)4494 void Heap::CheckGcStressMode(Thread* self, ObjPtr<mirror::Object>* obj) {
4495   DCHECK(gc_stress_mode_);
4496   auto* const runtime = Runtime::Current();
4497   if (runtime->GetClassLinker()->IsInitialized() && !runtime->IsActiveTransaction()) {
4498     // Check if we should GC.
4499     bool new_backtrace = false;
4500     {
4501       static constexpr size_t kMaxFrames = 16u;
4502       MutexLock mu(self, *backtrace_lock_);
4503       FixedSizeBacktrace<kMaxFrames> backtrace;
4504       backtrace.Collect(/* skip_count= */ 2);
4505       uint64_t hash = backtrace.Hash();
4506       new_backtrace = seen_backtraces_.find(hash) == seen_backtraces_.end();
4507       if (new_backtrace) {
4508         seen_backtraces_.insert(hash);
4509       }
4510     }
4511     if (new_backtrace) {
4512       StackHandleScope<1> hs(self);
4513       auto h = hs.NewHandleWrapper(obj);
4514       CollectGarbage(/* clear_soft_references= */ false);
4515       unique_backtrace_count_.fetch_add(1);
4516     } else {
4517       seen_backtrace_count_.fetch_add(1);
4518     }
4519   }
4520 }
4521 
DisableGCForShutdown()4522 void Heap::DisableGCForShutdown() {
4523   MutexLock mu(Thread::Current(), *gc_complete_lock_);
4524   gc_disabled_for_shutdown_ = true;
4525 }
4526 
IsGCDisabledForShutdown() const4527 bool Heap::IsGCDisabledForShutdown() const {
4528   MutexLock mu(Thread::Current(), *gc_complete_lock_);
4529   return gc_disabled_for_shutdown_;
4530 }
4531 
ObjectIsInBootImageSpace(ObjPtr<mirror::Object> obj) const4532 bool Heap::ObjectIsInBootImageSpace(ObjPtr<mirror::Object> obj) const {
4533   DCHECK_EQ(IsBootImageAddress(obj.Ptr()),
4534             any_of(boot_image_spaces_.begin(),
4535                    boot_image_spaces_.end(),
4536                    [obj](gc::space::ImageSpace* space) REQUIRES_SHARED(Locks::mutator_lock_) {
4537                      return space->HasAddress(obj.Ptr());
4538                    }));
4539   return IsBootImageAddress(obj.Ptr());
4540 }
4541 
IsInBootImageOatFile(const void * p) const4542 bool Heap::IsInBootImageOatFile(const void* p) const {
4543   DCHECK_EQ(IsBootImageAddress(p),
4544             any_of(boot_image_spaces_.begin(),
4545                    boot_image_spaces_.end(),
4546                    [p](gc::space::ImageSpace* space) REQUIRES_SHARED(Locks::mutator_lock_) {
4547                      return space->GetOatFile()->Contains(p);
4548                    }));
4549   return IsBootImageAddress(p);
4550 }
4551 
SetAllocationListener(AllocationListener * l)4552 void Heap::SetAllocationListener(AllocationListener* l) {
4553   AllocationListener* old = GetAndOverwriteAllocationListener(&alloc_listener_, l);
4554 
4555   if (old == nullptr) {
4556     Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
4557   }
4558 }
4559 
RemoveAllocationListener()4560 void Heap::RemoveAllocationListener() {
4561   AllocationListener* old = GetAndOverwriteAllocationListener(&alloc_listener_, nullptr);
4562 
4563   if (old != nullptr) {
4564     Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
4565   }
4566 }
4567 
SetGcPauseListener(GcPauseListener * l)4568 void Heap::SetGcPauseListener(GcPauseListener* l) {
4569   gc_pause_listener_.store(l, std::memory_order_relaxed);
4570 }
4571 
RemoveGcPauseListener()4572 void Heap::RemoveGcPauseListener() {
4573   gc_pause_listener_.store(nullptr, std::memory_order_relaxed);
4574 }
4575 
AllocWithNewTLAB(Thread * self,AllocatorType allocator_type,size_t alloc_size,bool grow,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)4576 mirror::Object* Heap::AllocWithNewTLAB(Thread* self,
4577                                        AllocatorType allocator_type,
4578                                        size_t alloc_size,
4579                                        bool grow,
4580                                        size_t* bytes_allocated,
4581                                        size_t* usable_size,
4582                                        size_t* bytes_tl_bulk_allocated) {
4583   mirror::Object* ret = nullptr;
4584   bool take_sample = false;
4585   size_t bytes_until_sample = 0;
4586   bool jhp_enabled = GetHeapSampler().IsEnabled();
4587 
4588   if (kUsePartialTlabs && alloc_size <= self->TlabRemainingCapacity()) {
4589     DCHECK_GT(alloc_size, self->TlabSize());
4590     // There is enough space if we grow the TLAB. Lets do that. This increases the
4591     // TLAB bytes.
4592     const size_t min_expand_size = alloc_size - self->TlabSize();
4593     size_t next_tlab_size =
4594         jhp_enabled ? JHPCalculateNextTlabSize(
4595                           self, kPartialTlabSize, alloc_size, &take_sample, &bytes_until_sample) :
4596                       kPartialTlabSize;
4597     const size_t expand_bytes = std::max(
4598         min_expand_size,
4599         std::min(self->TlabRemainingCapacity() - self->TlabSize(), next_tlab_size));
4600     if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, expand_bytes, grow))) {
4601       return nullptr;
4602     }
4603     *bytes_tl_bulk_allocated = expand_bytes;
4604     self->ExpandTlab(expand_bytes);
4605     DCHECK_LE(alloc_size, self->TlabSize());
4606   } else if (allocator_type == kAllocatorTypeTLAB) {
4607     DCHECK(bump_pointer_space_ != nullptr);
4608     // Try to allocate a page-aligned TLAB (not necessary though).
4609     // TODO: for large allocations, which are rare, maybe we should allocate
4610     // that object and return. There is no need to revoke the current TLAB,
4611     // particularly if it's mostly unutilized.
4612     size_t next_tlab_size = RoundDown(alloc_size + kDefaultTLABSize, gPageSize) - alloc_size;
4613     if (jhp_enabled) {
4614       next_tlab_size = JHPCalculateNextTlabSize(
4615           self, next_tlab_size, alloc_size, &take_sample, &bytes_until_sample);
4616     }
4617     const size_t new_tlab_size = alloc_size + next_tlab_size;
4618     if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, new_tlab_size, grow))) {
4619       return nullptr;
4620     }
4621     // Try allocating a new thread local buffer, if the allocation fails the space must be
4622     // full so return null.
4623     if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size, bytes_tl_bulk_allocated)) {
4624       return nullptr;
4625     }
4626     if (jhp_enabled) {
4627       VLOG(heap) << "JHP:kAllocatorTypeTLAB, New Tlab bytes allocated= " << new_tlab_size;
4628     }
4629   } else {
4630     DCHECK(allocator_type == kAllocatorTypeRegionTLAB);
4631     DCHECK(region_space_ != nullptr);
4632     if (space::RegionSpace::kRegionSize >= alloc_size) {
4633       // Non-large. Check OOME for a tlab.
4634       if (LIKELY(!IsOutOfMemoryOnAllocation(allocator_type,
4635                                             space::RegionSpace::kRegionSize,
4636                                             grow))) {
4637         size_t next_pr_tlab_size =
4638             kUsePartialTlabs ? kPartialTlabSize : gc::space::RegionSpace::kRegionSize;
4639         if (jhp_enabled) {
4640           next_pr_tlab_size = JHPCalculateNextTlabSize(
4641               self, next_pr_tlab_size, alloc_size, &take_sample, &bytes_until_sample);
4642         }
4643         const size_t new_tlab_size = kUsePartialTlabs
4644             ? std::max(alloc_size, next_pr_tlab_size)
4645             : next_pr_tlab_size;
4646         // Try to allocate a tlab.
4647         if (!region_space_->AllocNewTlab(self, new_tlab_size, bytes_tl_bulk_allocated)) {
4648           // Failed to allocate a tlab. Try non-tlab.
4649           ret = region_space_->AllocNonvirtual<false>(alloc_size,
4650                                                       bytes_allocated,
4651                                                       usable_size,
4652                                                       bytes_tl_bulk_allocated);
4653           if (jhp_enabled) {
4654             JHPCheckNonTlabSampleAllocation(self, ret, alloc_size);
4655           }
4656           return ret;
4657         }
4658         // Fall-through to using the TLAB below.
4659       } else {
4660         // Check OOME for a non-tlab allocation.
4661         if (!IsOutOfMemoryOnAllocation(allocator_type, alloc_size, grow)) {
4662           ret = region_space_->AllocNonvirtual<false>(alloc_size,
4663                                                       bytes_allocated,
4664                                                       usable_size,
4665                                                       bytes_tl_bulk_allocated);
4666           if (jhp_enabled) {
4667             JHPCheckNonTlabSampleAllocation(self, ret, alloc_size);
4668           }
4669           return ret;
4670         }
4671         // Neither tlab or non-tlab works. Give up.
4672         return nullptr;
4673       }
4674     } else {
4675       // Large. Check OOME.
4676       if (LIKELY(!IsOutOfMemoryOnAllocation(allocator_type, alloc_size, grow))) {
4677         ret = region_space_->AllocNonvirtual<false>(alloc_size,
4678                                                     bytes_allocated,
4679                                                     usable_size,
4680                                                     bytes_tl_bulk_allocated);
4681         if (jhp_enabled) {
4682           JHPCheckNonTlabSampleAllocation(self, ret, alloc_size);
4683         }
4684         return ret;
4685       }
4686       return nullptr;
4687     }
4688   }
4689   // Refilled TLAB, return.
4690   ret = self->AllocTlab(alloc_size);
4691   DCHECK(ret != nullptr);
4692   *bytes_allocated = alloc_size;
4693   *usable_size = alloc_size;
4694 
4695   // JavaHeapProfiler: Send the thread information about this allocation in case a sample is
4696   // requested.
4697   // This is the fallthrough from both the if and else if above cases => Cases that use TLAB.
4698   if (jhp_enabled) {
4699     if (take_sample) {
4700       GetHeapSampler().ReportSample(ret, alloc_size);
4701       // Update the bytes_until_sample now that the allocation is already done.
4702       GetHeapSampler().SetBytesUntilSample(bytes_until_sample);
4703     }
4704     VLOG(heap) << "JHP:Fallthrough Tlab allocation";
4705   }
4706 
4707   return ret;
4708 }
4709 
GetVerification() const4710 const Verification* Heap::GetVerification() const {
4711   return verification_.get();
4712 }
4713 
VlogHeapGrowth(size_t old_footprint,size_t new_footprint,size_t alloc_size)4714 void Heap::VlogHeapGrowth(size_t old_footprint, size_t new_footprint, size_t alloc_size) {
4715   VLOG(heap) << "Growing heap from " << PrettySize(old_footprint) << " to "
4716              << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
4717 }
4718 
4719 // Run a gc if we haven't run one since initial_gc_num. This forces processes to
4720 // reclaim memory allocated during startup, even if they don't do much
4721 // allocation post startup. If the process is actively allocating and triggering
4722 // GCs, or has moved to the background and hence forced a GC, this does nothing.
4723 class Heap::TriggerPostForkCCGcTask : public HeapTask {
4724  public:
TriggerPostForkCCGcTask(uint64_t target_time,uint32_t initial_gc_num)4725   explicit TriggerPostForkCCGcTask(uint64_t target_time, uint32_t initial_gc_num) :
4726       HeapTask(target_time), initial_gc_num_(initial_gc_num) {}
Run(Thread * self)4727   void Run(Thread* self) override {
4728     gc::Heap* heap = Runtime::Current()->GetHeap();
4729     if (heap->GetCurrentGcNum() == initial_gc_num_) {
4730       if (kLogAllGCs) {
4731         LOG(INFO) << "Forcing GC for allocation-inactive process";
4732       }
4733       heap->RequestConcurrentGC(self, kGcCauseBackground, false, initial_gc_num_);
4734     }
4735   }
4736  private:
4737   uint32_t initial_gc_num_;
4738 };
4739 
4740 // Reduce target footprint, if no GC has occurred since initial_gc_num.
4741 // If a GC already occurred, it will have done this for us.
4742 class Heap::ReduceTargetFootprintTask : public HeapTask {
4743  public:
ReduceTargetFootprintTask(uint64_t target_time,size_t new_target_sz,uint32_t initial_gc_num)4744   explicit ReduceTargetFootprintTask(uint64_t target_time, size_t new_target_sz,
4745                                      uint32_t initial_gc_num) :
4746       HeapTask(target_time), new_target_sz_(new_target_sz), initial_gc_num_(initial_gc_num) {}
Run(Thread * self)4747   void Run(Thread* self) override {
4748     gc::Heap* heap = Runtime::Current()->GetHeap();
4749     MutexLock mu(self, *(heap->gc_complete_lock_));
4750     if (heap->GetCurrentGcNum() == initial_gc_num_
4751         && heap->collector_type_running_ == kCollectorTypeNone) {
4752       size_t target_footprint = heap->target_footprint_.load(std::memory_order_relaxed);
4753       if (target_footprint > new_target_sz_) {
4754         if (heap->target_footprint_.CompareAndSetStrongRelaxed(target_footprint, new_target_sz_)) {
4755           heap->SetDefaultConcurrentStartBytesLocked();
4756         }
4757       }
4758     }
4759   }
4760  private:
4761   size_t new_target_sz_;
4762   uint32_t initial_gc_num_;
4763 };
4764 
4765 // Return a pseudo-random integer between 0 and 19999, using the uid as a seed.  We want this to
4766 // be deterministic for a given process, but to vary randomly across processes. Empirically, the
4767 // uids for processes for which this matters are distinct.
GetPseudoRandomFromUid()4768 static uint32_t GetPseudoRandomFromUid() {
4769   std::default_random_engine rng(getuid());
4770   std::uniform_int_distribution<int> dist(0, 19999);
4771   return dist(rng);
4772 }
4773 
PostForkChildAction(Thread * self)4774 void Heap::PostForkChildAction(Thread* self) {
4775   uint32_t starting_gc_num = GetCurrentGcNum();
4776   uint64_t last_adj_time = NanoTime();
4777   next_gc_type_ = NonStickyGcType();  // Always start with a full gc.
4778 
4779   LOG(INFO) << "Using " << foreground_collector_type_ << " GC.";
4780   if (gUseUserfaultfd) {
4781     DCHECK_NE(mark_compact_, nullptr);
4782     mark_compact_->CreateUserfaultfd(/*post_fork*/true);
4783   }
4784 
4785   // Temporarily increase target_footprint_ and concurrent_start_bytes_ to
4786   // max values to avoid GC during app launch.
4787   // Set target_footprint_ to the largest allowed value.
4788   SetIdealFootprint(growth_limit_);
4789   SetDefaultConcurrentStartBytes();
4790 
4791   // Shrink heap after kPostForkMaxHeapDurationMS, to force a memory hog process to GC.
4792   // This remains high enough that many processes will continue without a GC.
4793   if (initial_heap_size_ < growth_limit_) {
4794     size_t first_shrink_size = std::max(growth_limit_ / 4, initial_heap_size_);
4795     last_adj_time += MsToNs(kPostForkMaxHeapDurationMS);
4796     GetTaskProcessor()->AddTask(
4797         self, new ReduceTargetFootprintTask(last_adj_time, first_shrink_size, starting_gc_num));
4798     // Shrink to a small value after a substantial time period. This will typically force a
4799     // GC if none has occurred yet. Has no effect if there was a GC before this anyway, which
4800     // is commonly the case, e.g. because of a process transition.
4801     if (initial_heap_size_ < first_shrink_size) {
4802       last_adj_time += MsToNs(4 * kPostForkMaxHeapDurationMS);
4803       GetTaskProcessor()->AddTask(
4804           self,
4805           new ReduceTargetFootprintTask(last_adj_time, initial_heap_size_, starting_gc_num));
4806     }
4807   }
4808   // Schedule a GC after a substantial period of time. This will become a no-op if another GC is
4809   // scheduled in the interim. If not, we want to avoid holding onto start-up garbage.
4810   uint64_t post_fork_gc_time = last_adj_time
4811       + MsToNs(4 * kPostForkMaxHeapDurationMS + GetPseudoRandomFromUid());
4812   GetTaskProcessor()->AddTask(self,
4813                               new TriggerPostForkCCGcTask(post_fork_gc_time, starting_gc_num));
4814 }
4815 
VisitReflectiveTargets(ReflectiveValueVisitor * visit)4816 void Heap::VisitReflectiveTargets(ReflectiveValueVisitor *visit) {
4817   VisitObjectsPaused([&visit](mirror::Object* ref) NO_THREAD_SAFETY_ANALYSIS {
4818     art::ObjPtr<mirror::Class> klass(ref->GetClass());
4819     // All these classes are in the BootstrapClassLoader.
4820     if (!klass->IsBootStrapClassLoaded()) {
4821       return;
4822     }
4823     if (GetClassRoot<mirror::Method>()->IsAssignableFrom(klass) ||
4824         GetClassRoot<mirror::Constructor>()->IsAssignableFrom(klass)) {
4825       down_cast<mirror::Executable*>(ref)->VisitTarget(visit);
4826     } else if (art::GetClassRoot<art::mirror::Field>() == klass) {
4827       down_cast<mirror::Field*>(ref)->VisitTarget(visit);
4828     } else if (art::GetClassRoot<art::mirror::MethodHandle>()->IsAssignableFrom(klass)) {
4829       down_cast<mirror::MethodHandle*>(ref)->VisitTarget(visit);
4830     } else if (art::GetClassRoot<art::mirror::StaticFieldVarHandle>()->IsAssignableFrom(klass)) {
4831       down_cast<mirror::StaticFieldVarHandle*>(ref)->VisitTarget(visit);
4832     } else if (art::GetClassRoot<art::mirror::FieldVarHandle>()->IsAssignableFrom(klass)) {
4833       down_cast<mirror::FieldVarHandle*>(ref)->VisitTarget(visit);
4834     } else if (art::GetClassRoot<art::mirror::DexCache>()->IsAssignableFrom(klass)) {
4835       down_cast<mirror::DexCache*>(ref)->VisitReflectiveTargets(visit);
4836     }
4837   });
4838 }
4839 
AddHeapTask(gc::HeapTask * task)4840 bool Heap::AddHeapTask(gc::HeapTask* task) {
4841   Thread* const self = Thread::Current();
4842   if (!CanAddHeapTask(self)) {
4843     return false;
4844   }
4845   GetTaskProcessor()->AddTask(self, task);
4846   return true;
4847 }
4848 
GetForegroundCollectorName()4849 std::string Heap::GetForegroundCollectorName() {
4850   std::ostringstream oss;
4851   oss << foreground_collector_type_;
4852   return oss.str();
4853 }
4854 
HasAppImageSpaceFor(const std::string & dex_location) const4855 bool Heap::HasAppImageSpaceFor(const std::string& dex_location) const {
4856   ScopedObjectAccess soa(Thread::Current());
4857   for (space::ContinuousSpace* space : continuous_spaces_) {
4858     // An image space is either a boot image space or an app image space.
4859     if (space->IsImageSpace() &&
4860         !IsBootImageAddress(space->Begin()) &&
4861         (space->AsImageSpace()->GetOatFile()->GetOatDexFiles()[0]->GetDexFileLocation() ==
4862               dex_location)) {
4863       return true;
4864     }
4865   }
4866   return false;
4867 }
4868 
4869 }  // namespace gc
4870 }  // namespace art
4871