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