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