Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "heap.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 18 | |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 20 | #include <cutils/trace.h> |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 21 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 22 | #include <limits> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 24 | #include <unwind.h> // For GC verification. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 27 | #include "art_field-inl.h" |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 28 | #include "base/allocator.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 29 | #include "base/dumpable.h" |
Mathieu Chartier | b2f9936 | 2013-11-20 17:26:00 -0800 | [diff] [blame] | 30 | #include "base/histogram-inl.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 32 | #include "base/time_utils.h" |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 33 | #include "common_throws.h" |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 34 | #include "cutils/sched_policy.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 35 | #include "debugger.h" |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 36 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 37 | #include "gc/accounting/atomic_stack.h" |
| 38 | #include "gc/accounting/card_table-inl.h" |
| 39 | #include "gc/accounting/heap_bitmap-inl.h" |
| 40 | #include "gc/accounting/mod_union_table-inl.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 41 | #include "gc/accounting/remembered_set.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 42 | #include "gc/accounting/space_bitmap-inl.h" |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 43 | #include "gc/collector/concurrent_copying.h" |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 44 | #include "gc/collector/mark_compact.h" |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 45 | #include "gc/collector/mark_sweep.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | #include "gc/collector/partial_mark_sweep.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | #include "gc/collector/semi_space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | #include "gc/collector/sticky_mark_sweep.h" |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 49 | #include "gc/reference_processor.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 50 | #include "gc/space/bump_pointer_space.h" |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 51 | #include "gc/space/dlmalloc_space-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | #include "gc/space/image_space.h" |
| 53 | #include "gc/space/large_object_space.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 54 | #include "gc/space/region_space.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 55 | #include "gc/space/rosalloc_space-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 56 | #include "gc/space/space-inl.h" |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 57 | #include "gc/space/zygote_space.h" |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 58 | #include "gc/task_processor.h" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 59 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 60 | #include "heap-inl.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 61 | #include "image.h" |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 62 | #include "intern_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | #include "mirror/object-inl.h" |
| 65 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 66 | #include "mirror/reference-inl.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 67 | #include "os.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 68 | #include "reflection.h" |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 69 | #include "runtime.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 70 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 71 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 72 | #include "handle_scope-inl.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 73 | #include "thread_list.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 74 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 75 | |
| 76 | namespace art { |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 77 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 78 | namespace gc { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 79 | |
Mathieu Chartier | 91e3063 | 2014-03-25 15:58:50 -0700 | [diff] [blame] | 80 | static constexpr size_t kCollectorTransitionStressIterations = 0; |
| 81 | static constexpr size_t kCollectorTransitionStressWait = 10 * 1000; // Microseconds |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 82 | // Minimum amount of remaining bytes before a concurrent GC is triggered. |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 83 | static constexpr size_t kMinConcurrentRemainingBytes = 128 * KB; |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 84 | static constexpr size_t kMaxConcurrentRemainingBytes = 512 * KB; |
Mathieu Chartier | df86d1f | 2014-04-08 13:44:04 -0700 | [diff] [blame] | 85 | // Sticky GC throughput adjustment, divided by 4. Increasing this causes sticky GC to occur more |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 86 | // relative to partial/full GC. This may be desirable since sticky GCs interfere less with mutator |
Mathieu Chartier | df86d1f | 2014-04-08 13:44:04 -0700 | [diff] [blame] | 87 | // threads (lower pauses, use less memory bandwidth). |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 88 | static constexpr double kStickyGcThroughputAdjustment = 1.0; |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 89 | // Whether or not we compact the zygote in PreZygoteFork. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 90 | static constexpr bool kCompactZygote = kMovingCollector; |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 91 | // How many reserve entries are at the end of the allocation stack, these are only needed if the |
| 92 | // allocation stack overflows. |
| 93 | static constexpr size_t kAllocationStackReserveSize = 1024; |
| 94 | // Default mark stack size in bytes. |
| 95 | static const size_t kDefaultMarkStackSize = 64 * KB; |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 96 | // Define space name. |
| 97 | static const char* kDlMallocSpaceName[2] = {"main dlmalloc space", "main dlmalloc space 1"}; |
| 98 | static const char* kRosAllocSpaceName[2] = {"main rosalloc space", "main rosalloc space 1"}; |
| 99 | static const char* kMemMapSpaceName[2] = {"main space", "main space 1"}; |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 100 | static const char* kNonMovingSpaceName = "non moving space"; |
| 101 | static const char* kZygoteSpaceName = "zygote space"; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 102 | static constexpr size_t kGSSBumpPointerSpaceCapacity = 32 * MB; |
Mathieu Chartier | 95a505c | 2014-12-10 18:45:30 -0800 | [diff] [blame] | 103 | static constexpr bool kGCALotMode = false; |
| 104 | // GC alot mode uses a small allocation stack to stress test a lot of GC. |
| 105 | static constexpr size_t kGcAlotAllocationStackSize = 4 * KB / |
| 106 | sizeof(mirror::HeapReference<mirror::Object>); |
| 107 | // Verify objet has a small allocation stack size since searching the allocation stack is slow. |
| 108 | static constexpr size_t kVerifyObjectAllocationStackSize = 16 * KB / |
| 109 | sizeof(mirror::HeapReference<mirror::Object>); |
| 110 | static constexpr size_t kDefaultAllocationStackSize = 8 * MB / |
| 111 | sizeof(mirror::HeapReference<mirror::Object>); |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 112 | // System.runFinalization can deadlock with native allocations, to deal with this, we have a |
| 113 | // timeout on how long we wait for finalizers to run. b/21544853 |
| 114 | static constexpr uint64_t kNativeAllocationFinalizeTimeout = MsToNs(250u); |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 115 | |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 116 | Heap::Heap(size_t initial_size, size_t growth_limit, size_t min_free, size_t max_free, |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 117 | double target_utilization, double foreground_heap_growth_multiplier, |
| 118 | size_t capacity, size_t non_moving_space_capacity, const std::string& image_file_name, |
| 119 | const InstructionSet image_instruction_set, CollectorType foreground_collector_type, |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 120 | CollectorType background_collector_type, |
| 121 | space::LargeObjectSpaceType large_object_space_type, size_t large_object_threshold, |
| 122 | size_t parallel_gc_threads, size_t conc_gc_threads, bool low_memory_mode, |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 123 | size_t long_pause_log_threshold, size_t long_gc_log_threshold, |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 124 | bool ignore_max_footprint, bool use_tlab, |
| 125 | bool verify_pre_gc_heap, bool verify_pre_sweeping_heap, bool verify_post_gc_heap, |
| 126 | bool verify_pre_gc_rosalloc, bool verify_pre_sweeping_rosalloc, |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 127 | bool verify_post_gc_rosalloc, bool gc_stress_mode, |
| 128 | bool use_homogeneous_space_compaction_for_oom, |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 129 | uint64_t min_interval_homogeneous_space_compaction_by_oom) |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 130 | : non_moving_space_(nullptr), |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 131 | rosalloc_space_(nullptr), |
| 132 | dlmalloc_space_(nullptr), |
Mathieu Chartier | fc5b528 | 2014-01-09 16:15:36 -0800 | [diff] [blame] | 133 | main_space_(nullptr), |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 134 | collector_type_(kCollectorTypeNone), |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 135 | foreground_collector_type_(foreground_collector_type), |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 136 | background_collector_type_(background_collector_type), |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 137 | desired_collector_type_(foreground_collector_type_), |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 138 | pending_task_lock_(nullptr), |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 139 | parallel_gc_threads_(parallel_gc_threads), |
| 140 | conc_gc_threads_(conc_gc_threads), |
Mathieu Chartier | e0a53e9 | 2013-08-05 10:17:40 -0700 | [diff] [blame] | 141 | low_memory_mode_(low_memory_mode), |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 142 | long_pause_log_threshold_(long_pause_log_threshold), |
| 143 | long_gc_log_threshold_(long_gc_log_threshold), |
| 144 | ignore_max_footprint_(ignore_max_footprint), |
Mathieu Chartier | 8e4a96d | 2014-05-21 10:44:32 -0700 | [diff] [blame] | 145 | zygote_creation_lock_("zygote creation lock", kZygoteCreationLock), |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 146 | zygote_space_(nullptr), |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 147 | large_object_threshold_(large_object_threshold), |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 148 | collector_type_running_(kCollectorTypeNone), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 149 | last_gc_type_(collector::kGcTypeNone), |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 150 | next_gc_type_(collector::kGcTypePartial), |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 151 | capacity_(capacity), |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 152 | growth_limit_(growth_limit), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 153 | max_allowed_footprint_(initial_size), |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 154 | native_footprint_gc_watermark_(initial_size), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 155 | native_need_to_run_finalization_(false), |
Mathieu Chartier | ca2a24d | 2013-11-25 15:12:12 -0800 | [diff] [blame] | 156 | // Initially assume we perceive jank in case the process state is never updated. |
| 157 | process_state_(kProcessStateJankPerceptible), |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 158 | concurrent_start_bytes_(std::numeric_limits<size_t>::max()), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 159 | total_bytes_freed_ever_(0), |
| 160 | total_objects_freed_ever_(0), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 161 | num_bytes_allocated_(0), |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 162 | native_bytes_allocated_(0), |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 163 | num_bytes_freed_revoke_(0), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 164 | verify_missing_card_marks_(false), |
| 165 | verify_system_weaks_(false), |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 166 | verify_pre_gc_heap_(verify_pre_gc_heap), |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 167 | verify_pre_sweeping_heap_(verify_pre_sweeping_heap), |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 168 | verify_post_gc_heap_(verify_post_gc_heap), |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 169 | verify_mod_union_table_(false), |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 170 | verify_pre_gc_rosalloc_(verify_pre_gc_rosalloc), |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 171 | verify_pre_sweeping_rosalloc_(verify_pre_sweeping_rosalloc), |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 172 | verify_post_gc_rosalloc_(verify_post_gc_rosalloc), |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 173 | gc_stress_mode_(gc_stress_mode), |
Mathieu Chartier | 0418ae2 | 2013-07-31 13:35:46 -0700 | [diff] [blame] | 174 | /* For GC a lot mode, we limit the allocations stacks to be kGcAlotInterval allocations. This |
| 175 | * causes a lot of GC since we do a GC for alloc whenever the stack is full. When heap |
| 176 | * verification is enabled, we limit the size of allocation stacks to speed up their |
| 177 | * searching. |
| 178 | */ |
Mathieu Chartier | 95a505c | 2014-12-10 18:45:30 -0800 | [diff] [blame] | 179 | max_allocation_stack_size_(kGCALotMode ? kGcAlotAllocationStackSize |
| 180 | : (kVerifyObjectSupport > kVerifyObjectModeFast) ? kVerifyObjectAllocationStackSize : |
| 181 | kDefaultAllocationStackSize), |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 182 | current_allocator_(kAllocatorTypeDlMalloc), |
| 183 | current_non_moving_allocator_(kAllocatorTypeNonMoving), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 184 | bump_pointer_space_(nullptr), |
| 185 | temp_space_(nullptr), |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 186 | region_space_(nullptr), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 187 | min_free_(min_free), |
| 188 | max_free_(max_free), |
| 189 | target_utilization_(target_utilization), |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 190 | foreground_heap_growth_multiplier_(foreground_heap_growth_multiplier), |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 191 | total_wait_time_(0), |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 192 | total_allocation_time_(0), |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 193 | verify_object_mode_(kVerifyObjectModeDisabled), |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 194 | disable_moving_gc_count_(0), |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 195 | is_running_on_memory_tool_(Runtime::Current()->IsRunningOnMemoryTool()), |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 196 | use_tlab_(use_tlab), |
| 197 | main_space_backup_(nullptr), |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 198 | min_interval_homogeneous_space_compaction_by_oom_( |
| 199 | min_interval_homogeneous_space_compaction_by_oom), |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 200 | last_time_homogeneous_space_compaction_by_oom_(NanoTime()), |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 201 | pending_collector_transition_(nullptr), |
| 202 | pending_heap_trim_(nullptr), |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 203 | use_homogeneous_space_compaction_for_oom_(use_homogeneous_space_compaction_for_oom), |
| 204 | running_collection_is_blocking_(false), |
| 205 | blocking_gc_count_(0U), |
| 206 | blocking_gc_time_(0U), |
| 207 | last_update_time_gc_count_rate_histograms_( // Round down by the window duration. |
| 208 | (NanoTime() / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration), |
| 209 | gc_count_last_window_(0U), |
| 210 | blocking_gc_count_last_window_(0U), |
| 211 | gc_count_rate_histogram_("gc count rate histogram", 1U, kGcCountRateMaxBucketCount), |
| 212 | blocking_gc_count_rate_histogram_("blocking gc count rate histogram", 1U, |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 213 | kGcCountRateMaxBucketCount), |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 214 | alloc_tracking_enabled_(false), |
| 215 | backtrace_lock_(nullptr), |
| 216 | seen_backtrace_count_(0u), |
| 217 | unique_backtrace_count_(0u) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 218 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 219 | LOG(INFO) << "Heap() entering"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 220 | } |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 221 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 222 | // If we aren't the zygote, switch to the default non zygote allocator. This may update the |
| 223 | // entrypoints. |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 224 | const bool is_zygote = runtime->IsZygote(); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 225 | if (!is_zygote) { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 226 | // Background compaction is currently not supported for command line runs. |
| 227 | if (background_collector_type_ != foreground_collector_type_) { |
Mathieu Chartier | 52ba199 | 2014-05-07 14:39:21 -0700 | [diff] [blame] | 228 | VLOG(heap) << "Disabling background compaction for non zygote"; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 229 | background_collector_type_ = foreground_collector_type_; |
Mathieu Chartier | bd0a653 | 2014-02-27 11:14:21 -0800 | [diff] [blame] | 230 | } |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 231 | } |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 232 | ChangeCollector(desired_collector_type_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 233 | live_bitmap_.reset(new accounting::HeapBitmap(this)); |
| 234 | mark_bitmap_.reset(new accounting::HeapBitmap(this)); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 235 | // Requested begin for the alloc space, to follow the mapped image and oat files |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 236 | uint8_t* requested_alloc_space_begin = nullptr; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 237 | if (foreground_collector_type_ == kCollectorTypeCC) { |
| 238 | // Need to use a low address so that we can allocate a contiguous |
| 239 | // 2 * Xmx space when there's no image (dex2oat for target). |
| 240 | CHECK_GE(300 * MB, non_moving_space_capacity); |
| 241 | requested_alloc_space_begin = reinterpret_cast<uint8_t*>(300 * MB) - non_moving_space_capacity; |
| 242 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 243 | if (!image_file_name.empty()) { |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 244 | ATRACE_BEGIN("ImageSpace::Create"); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 245 | std::string error_msg; |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 246 | auto* image_space = space::ImageSpace::Create(image_file_name.c_str(), image_instruction_set, |
| 247 | &error_msg); |
| 248 | ATRACE_END(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 249 | if (image_space != nullptr) { |
| 250 | AddSpace(image_space); |
| 251 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 252 | // isn't going to get in the middle |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 253 | uint8_t* oat_file_end_addr = image_space->GetImageHeader().GetOatFileEnd(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 254 | CHECK_GT(oat_file_end_addr, image_space->End()); |
| 255 | requested_alloc_space_begin = AlignUp(oat_file_end_addr, kPageSize); |
| 256 | } else { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 257 | LOG(ERROR) << "Could not create image space with image file '" << image_file_name << "'. " |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 258 | << "Attempting to fall back to imageless running. Error was: " << error_msg; |
| 259 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 260 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 261 | /* |
| 262 | requested_alloc_space_begin -> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 263 | +- nonmoving space (non_moving_space_capacity)+- |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 264 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 265 | +-????????????????????????????????????????????+- |
| 266 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 267 | +-main alloc space / bump space 1 (capacity_) +- |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 268 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 269 | +-????????????????????????????????????????????+- |
| 270 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
| 271 | +-main alloc space2 / bump space 2 (capacity_)+- |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 272 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
| 273 | */ |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 274 | // We don't have hspace compaction enabled with GSS or CC. |
| 275 | if (foreground_collector_type_ == kCollectorTypeGSS || |
| 276 | foreground_collector_type_ == kCollectorTypeCC) { |
Hiroshi Yamauchi | 20ed5af | 2014-11-17 18:05:44 -0800 | [diff] [blame] | 277 | use_homogeneous_space_compaction_for_oom_ = false; |
| 278 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 279 | bool support_homogeneous_space_compaction = |
Mathieu Chartier | 0deeb81 | 2014-08-21 18:28:20 -0700 | [diff] [blame] | 280 | background_collector_type_ == gc::kCollectorTypeHomogeneousSpaceCompact || |
Hiroshi Yamauchi | 20ed5af | 2014-11-17 18:05:44 -0800 | [diff] [blame] | 281 | use_homogeneous_space_compaction_for_oom_; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 282 | // We may use the same space the main space for the non moving space if we don't need to compact |
| 283 | // from the main space. |
| 284 | // This is not the case if we support homogeneous compaction or have a moving background |
| 285 | // collector type. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 286 | bool separate_non_moving_space = is_zygote || |
| 287 | support_homogeneous_space_compaction || IsMovingGc(foreground_collector_type_) || |
| 288 | IsMovingGc(background_collector_type_); |
| 289 | if (foreground_collector_type == kCollectorTypeGSS) { |
| 290 | separate_non_moving_space = false; |
| 291 | } |
| 292 | std::unique_ptr<MemMap> main_mem_map_1; |
| 293 | std::unique_ptr<MemMap> main_mem_map_2; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 294 | uint8_t* request_begin = requested_alloc_space_begin; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 295 | if (request_begin != nullptr && separate_non_moving_space) { |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 296 | request_begin += non_moving_space_capacity; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 297 | } |
| 298 | std::string error_str; |
| 299 | std::unique_ptr<MemMap> non_moving_space_mem_map; |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 300 | ATRACE_BEGIN("Create heap maps"); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 301 | if (separate_non_moving_space) { |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 302 | // If we are the zygote, the non moving space becomes the zygote space when we run |
| 303 | // PreZygoteFork the first time. In this case, call the map "zygote space" since we can't |
| 304 | // rename the mem map later. |
| 305 | const char* space_name = is_zygote ? kZygoteSpaceName: kNonMovingSpaceName; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 306 | // Reserve the non moving mem map before the other two since it needs to be at a specific |
| 307 | // address. |
| 308 | non_moving_space_mem_map.reset( |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 309 | MemMap::MapAnonymous(space_name, requested_alloc_space_begin, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 310 | non_moving_space_capacity, PROT_READ | PROT_WRITE, true, false, |
| 311 | &error_str)); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 312 | CHECK(non_moving_space_mem_map != nullptr) << error_str; |
Mathieu Chartier | c44ce2e | 2014-08-25 16:32:41 -0700 | [diff] [blame] | 313 | // Try to reserve virtual memory at a lower address if we have a separate non moving space. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 314 | request_begin = reinterpret_cast<uint8_t*>(300 * MB); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 315 | } |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 316 | // Attempt to create 2 mem maps at or after the requested begin. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 317 | if (foreground_collector_type_ != kCollectorTypeCC) { |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 318 | if (separate_non_moving_space) { |
| 319 | main_mem_map_1.reset(MapAnonymousPreferredAddress(kMemMapSpaceName[0], request_begin, |
| 320 | capacity_, &error_str)); |
| 321 | } else { |
| 322 | // If no separate non-moving space, the main space must come |
| 323 | // right after the image space to avoid a gap. |
| 324 | main_mem_map_1.reset(MemMap::MapAnonymous(kMemMapSpaceName[0], request_begin, capacity_, |
| 325 | PROT_READ | PROT_WRITE, true, false, |
| 326 | &error_str)); |
| 327 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 328 | CHECK(main_mem_map_1.get() != nullptr) << error_str; |
| 329 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 330 | if (support_homogeneous_space_compaction || |
| 331 | background_collector_type_ == kCollectorTypeSS || |
| 332 | foreground_collector_type_ == kCollectorTypeSS) { |
| 333 | main_mem_map_2.reset(MapAnonymousPreferredAddress(kMemMapSpaceName[1], main_mem_map_1->End(), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 334 | capacity_, &error_str)); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 335 | CHECK(main_mem_map_2.get() != nullptr) << error_str; |
| 336 | } |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 337 | ATRACE_END(); |
| 338 | ATRACE_BEGIN("Create spaces"); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 339 | // Create the non moving space first so that bitmaps don't take up the address range. |
| 340 | if (separate_non_moving_space) { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 341 | // Non moving space is always dlmalloc since we currently don't have support for multiple |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 342 | // active rosalloc spaces. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 343 | const size_t size = non_moving_space_mem_map->Size(); |
| 344 | non_moving_space_ = space::DlMallocSpace::CreateFromMemMap( |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 345 | non_moving_space_mem_map.release(), "zygote / non moving space", kDefaultStartingSize, |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 346 | initial_size, size, size, false); |
Mathieu Chartier | 7840888 | 2014-04-11 18:06:01 -0700 | [diff] [blame] | 347 | non_moving_space_->SetFootprintLimit(non_moving_space_->Capacity()); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 348 | CHECK(non_moving_space_ != nullptr) << "Failed creating non moving space " |
| 349 | << requested_alloc_space_begin; |
| 350 | AddSpace(non_moving_space_); |
| 351 | } |
| 352 | // Create other spaces based on whether or not we have a moving GC. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 353 | if (foreground_collector_type_ == kCollectorTypeCC) { |
| 354 | region_space_ = space::RegionSpace::Create("Region space", capacity_ * 2, request_begin); |
| 355 | AddSpace(region_space_); |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 356 | } else if (IsMovingGc(foreground_collector_type_) && |
| 357 | foreground_collector_type_ != kCollectorTypeGSS) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 358 | // Create bump pointer spaces. |
| 359 | // We only to create the bump pointer if the foreground collector is a compacting GC. |
| 360 | // TODO: Place bump-pointer spaces somewhere to minimize size of card table. |
| 361 | bump_pointer_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 1", |
| 362 | main_mem_map_1.release()); |
| 363 | CHECK(bump_pointer_space_ != nullptr) << "Failed to create bump pointer space"; |
| 364 | AddSpace(bump_pointer_space_); |
| 365 | temp_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 2", |
| 366 | main_mem_map_2.release()); |
| 367 | CHECK(temp_space_ != nullptr) << "Failed to create bump pointer space"; |
| 368 | AddSpace(temp_space_); |
| 369 | CHECK(separate_non_moving_space); |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 370 | } else { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 371 | CreateMainMallocSpace(main_mem_map_1.release(), initial_size, growth_limit_, capacity_); |
| 372 | CHECK(main_space_ != nullptr); |
| 373 | AddSpace(main_space_); |
| 374 | if (!separate_non_moving_space) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 375 | non_moving_space_ = main_space_; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 376 | CHECK(!non_moving_space_->CanMoveObjects()); |
| 377 | } |
| 378 | if (foreground_collector_type_ == kCollectorTypeGSS) { |
| 379 | CHECK_EQ(foreground_collector_type_, background_collector_type_); |
| 380 | // Create bump pointer spaces instead of a backup space. |
| 381 | main_mem_map_2.release(); |
| 382 | bump_pointer_space_ = space::BumpPointerSpace::Create("Bump pointer space 1", |
| 383 | kGSSBumpPointerSpaceCapacity, nullptr); |
| 384 | CHECK(bump_pointer_space_ != nullptr); |
| 385 | AddSpace(bump_pointer_space_); |
| 386 | temp_space_ = space::BumpPointerSpace::Create("Bump pointer space 2", |
| 387 | kGSSBumpPointerSpaceCapacity, nullptr); |
| 388 | CHECK(temp_space_ != nullptr); |
| 389 | AddSpace(temp_space_); |
| 390 | } else if (main_mem_map_2.get() != nullptr) { |
| 391 | const char* name = kUseRosAlloc ? kRosAllocSpaceName[1] : kDlMallocSpaceName[1]; |
| 392 | main_space_backup_.reset(CreateMallocSpaceFromMemMap(main_mem_map_2.release(), initial_size, |
| 393 | growth_limit_, capacity_, name, true)); |
| 394 | CHECK(main_space_backup_.get() != nullptr); |
| 395 | // Add the space so its accounted for in the heap_begin and heap_end. |
| 396 | AddSpace(main_space_backup_.get()); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 397 | } |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 398 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 399 | CHECK(non_moving_space_ != nullptr); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 400 | CHECK(!non_moving_space_->CanMoveObjects()); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 401 | // Allocate the large object space. |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 402 | if (large_object_space_type == space::LargeObjectSpaceType::kFreeList) { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 403 | large_object_space_ = space::FreeListSpace::Create("free list large object space", nullptr, |
| 404 | capacity_); |
| 405 | CHECK(large_object_space_ != nullptr) << "Failed to create large object space"; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 406 | } else if (large_object_space_type == space::LargeObjectSpaceType::kMap) { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 407 | large_object_space_ = space::LargeObjectMapSpace::Create("mem map large object space"); |
| 408 | CHECK(large_object_space_ != nullptr) << "Failed to create large object space"; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 409 | } else { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 410 | // Disable the large object space by making the cutoff excessively large. |
| 411 | large_object_threshold_ = std::numeric_limits<size_t>::max(); |
| 412 | large_object_space_ = nullptr; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 413 | } |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 414 | if (large_object_space_ != nullptr) { |
| 415 | AddSpace(large_object_space_); |
| 416 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 417 | // Compute heap capacity. Continuous spaces are sorted in order of Begin(). |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 418 | CHECK(!continuous_spaces_.empty()); |
| 419 | // Relies on the spaces being sorted. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 420 | uint8_t* heap_begin = continuous_spaces_.front()->Begin(); |
| 421 | uint8_t* heap_end = continuous_spaces_.back()->Limit(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 422 | size_t heap_capacity = heap_end - heap_begin; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 423 | // Remove the main backup space since it slows down the GC to have unused extra spaces. |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 424 | // TODO: Avoid needing to do this. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 425 | if (main_space_backup_.get() != nullptr) { |
| 426 | RemoveSpace(main_space_backup_.get()); |
| 427 | } |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 428 | ATRACE_END(); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 429 | // Allocate the card table. |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 430 | ATRACE_BEGIN("Create card table"); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 431 | card_table_.reset(accounting::CardTable::Create(heap_begin, heap_capacity)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 432 | CHECK(card_table_.get() != nullptr) << "Failed to create card table"; |
Richard Uhler | 054a078 | 2015-04-07 10:56:50 -0700 | [diff] [blame] | 433 | ATRACE_END(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 434 | if (foreground_collector_type_ == kCollectorTypeCC && kUseTableLookupReadBarrier) { |
| 435 | rb_table_.reset(new accounting::ReadBarrierTable()); |
| 436 | DCHECK(rb_table_->IsAllCleared()); |
| 437 | } |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 438 | if (GetImageSpace() != nullptr) { |
| 439 | // Don't add the image mod union table if we are running without an image, this can crash if |
| 440 | // we use the CardCache implementation. |
| 441 | accounting::ModUnionTable* mod_union_table = new accounting::ModUnionTableToZygoteAllocspace( |
| 442 | "Image mod-union table", this, GetImageSpace()); |
| 443 | CHECK(mod_union_table != nullptr) << "Failed to create image mod-union table"; |
| 444 | AddModUnionTable(mod_union_table); |
| 445 | } |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 446 | if (collector::SemiSpace::kUseRememberedSet && non_moving_space_ != main_space_) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 447 | accounting::RememberedSet* non_moving_space_rem_set = |
| 448 | new accounting::RememberedSet("Non-moving space remembered set", this, non_moving_space_); |
| 449 | CHECK(non_moving_space_rem_set != nullptr) << "Failed to create non-moving space remembered set"; |
| 450 | AddRememberedSet(non_moving_space_rem_set); |
| 451 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 452 | // TODO: Count objects in the image space here? |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 453 | num_bytes_allocated_.StoreRelaxed(0); |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 454 | mark_stack_.reset(accounting::ObjectStack::Create("mark stack", kDefaultMarkStackSize, |
| 455 | kDefaultMarkStackSize)); |
| 456 | const size_t alloc_stack_capacity = max_allocation_stack_size_ + kAllocationStackReserveSize; |
| 457 | allocation_stack_.reset(accounting::ObjectStack::Create( |
| 458 | "allocation stack", max_allocation_stack_size_, alloc_stack_capacity)); |
| 459 | live_stack_.reset(accounting::ObjectStack::Create( |
| 460 | "live stack", max_allocation_stack_size_, alloc_stack_capacity)); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 461 | // It's still too early to take a lock because there are no threads yet, but we can create locks |
| 462 | // now. We don't create it earlier to make it clear that you can't use locks during heap |
| 463 | // initialization. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 464 | gc_complete_lock_ = new Mutex("GC complete lock"); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 465 | gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable", |
| 466 | *gc_complete_lock_)); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 467 | task_processor_.reset(new TaskProcessor()); |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 468 | reference_processor_.reset(new ReferenceProcessor()); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 469 | pending_task_lock_ = new Mutex("Pending task lock"); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 470 | if (ignore_max_footprint_) { |
| 471 | SetIdealFootprint(std::numeric_limits<size_t>::max()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 472 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 473 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 474 | CHECK_NE(max_allowed_footprint_, 0U); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 475 | // Create our garbage collectors. |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 476 | for (size_t i = 0; i < 2; ++i) { |
| 477 | const bool concurrent = i != 0; |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 478 | if ((MayUseCollector(kCollectorTypeCMS) && concurrent) || |
| 479 | (MayUseCollector(kCollectorTypeMS) && !concurrent)) { |
| 480 | garbage_collectors_.push_back(new collector::MarkSweep(this, concurrent)); |
| 481 | garbage_collectors_.push_back(new collector::PartialMarkSweep(this, concurrent)); |
| 482 | garbage_collectors_.push_back(new collector::StickyMarkSweep(this, concurrent)); |
| 483 | } |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 484 | } |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 485 | if (kMovingCollector) { |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 486 | if (MayUseCollector(kCollectorTypeSS) || MayUseCollector(kCollectorTypeGSS) || |
| 487 | MayUseCollector(kCollectorTypeHomogeneousSpaceCompact) || |
| 488 | use_homogeneous_space_compaction_for_oom_) { |
| 489 | // TODO: Clean this up. |
| 490 | const bool generational = foreground_collector_type_ == kCollectorTypeGSS; |
| 491 | semi_space_collector_ = new collector::SemiSpace(this, generational, |
| 492 | generational ? "generational" : ""); |
| 493 | garbage_collectors_.push_back(semi_space_collector_); |
| 494 | } |
| 495 | if (MayUseCollector(kCollectorTypeCC)) { |
| 496 | concurrent_copying_collector_ = new collector::ConcurrentCopying(this); |
| 497 | garbage_collectors_.push_back(concurrent_copying_collector_); |
| 498 | } |
| 499 | if (MayUseCollector(kCollectorTypeMC)) { |
| 500 | mark_compact_collector_ = new collector::MarkCompact(this); |
| 501 | garbage_collectors_.push_back(mark_compact_collector_); |
| 502 | } |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 503 | } |
Andreas Gampe | e1cb298 | 2014-08-27 11:01:09 -0700 | [diff] [blame] | 504 | if (GetImageSpace() != nullptr && non_moving_space_ != nullptr && |
| 505 | (is_zygote || separate_non_moving_space || foreground_collector_type_ == kCollectorTypeGSS)) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 506 | // Check that there's no gap between the image space and the non moving space so that the |
Andreas Gampe | e1cb298 | 2014-08-27 11:01:09 -0700 | [diff] [blame] | 507 | // immune region won't break (eg. due to a large object allocated in the gap). This is only |
| 508 | // required when we're the zygote or using GSS. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 509 | bool no_gap = MemMap::CheckNoGaps(GetImageSpace()->GetMemMap(), |
| 510 | non_moving_space_->GetMemMap()); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 511 | if (!no_gap) { |
David Srbecky | 5dedb80 | 2015-06-17 00:08:02 +0100 | [diff] [blame] | 512 | PrintFileToLog("/proc/self/maps", LogSeverity::ERROR); |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 513 | MemMap::DumpMaps(LOG(ERROR), true); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 514 | LOG(FATAL) << "There's a gap between the image space and the non-moving space"; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 517 | instrumentation::Instrumentation* const instrumentation = runtime->GetInstrumentation(); |
| 518 | if (gc_stress_mode_) { |
| 519 | backtrace_lock_ = new Mutex("GC complete lock"); |
| 520 | } |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 521 | if (is_running_on_memory_tool_ || gc_stress_mode_) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 522 | instrumentation->InstrumentQuickAllocEntryPoints(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 523 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 524 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 525 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 526 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 529 | MemMap* Heap::MapAnonymousPreferredAddress(const char* name, uint8_t* request_begin, |
| 530 | size_t capacity, std::string* out_error_str) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 531 | while (true) { |
Kyungmin Lee | ef32b8f | 2014-10-23 09:32:05 +0900 | [diff] [blame] | 532 | MemMap* map = MemMap::MapAnonymous(name, request_begin, capacity, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 533 | PROT_READ | PROT_WRITE, true, false, out_error_str); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 534 | if (map != nullptr || request_begin == nullptr) { |
| 535 | return map; |
| 536 | } |
| 537 | // Retry a second time with no specified request begin. |
| 538 | request_begin = nullptr; |
| 539 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Mathieu Chartier | dfe3083 | 2015-03-06 15:28:34 -0800 | [diff] [blame] | 542 | bool Heap::MayUseCollector(CollectorType type) const { |
| 543 | return foreground_collector_type_ == type || background_collector_type_ == type; |
| 544 | } |
| 545 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 546 | space::MallocSpace* Heap::CreateMallocSpaceFromMemMap(MemMap* mem_map, size_t initial_size, |
| 547 | size_t growth_limit, size_t capacity, |
| 548 | const char* name, bool can_move_objects) { |
| 549 | space::MallocSpace* malloc_space = nullptr; |
| 550 | if (kUseRosAlloc) { |
| 551 | // Create rosalloc space. |
| 552 | malloc_space = space::RosAllocSpace::CreateFromMemMap(mem_map, name, kDefaultStartingSize, |
| 553 | initial_size, growth_limit, capacity, |
| 554 | low_memory_mode_, can_move_objects); |
| 555 | } else { |
| 556 | malloc_space = space::DlMallocSpace::CreateFromMemMap(mem_map, name, kDefaultStartingSize, |
| 557 | initial_size, growth_limit, capacity, |
| 558 | can_move_objects); |
| 559 | } |
| 560 | if (collector::SemiSpace::kUseRememberedSet) { |
| 561 | accounting::RememberedSet* rem_set = |
| 562 | new accounting::RememberedSet(std::string(name) + " remembered set", this, malloc_space); |
| 563 | CHECK(rem_set != nullptr) << "Failed to create main space remembered set"; |
| 564 | AddRememberedSet(rem_set); |
| 565 | } |
| 566 | CHECK(malloc_space != nullptr) << "Failed to create " << name; |
| 567 | malloc_space->SetFootprintLimit(malloc_space->Capacity()); |
| 568 | return malloc_space; |
| 569 | } |
| 570 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 571 | void Heap::CreateMainMallocSpace(MemMap* mem_map, size_t initial_size, size_t growth_limit, |
| 572 | size_t capacity) { |
| 573 | // Is background compaction is enabled? |
| 574 | bool can_move_objects = IsMovingGc(background_collector_type_) != |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 575 | IsMovingGc(foreground_collector_type_) || use_homogeneous_space_compaction_for_oom_; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 576 | // If we are the zygote and don't yet have a zygote space, it means that the zygote fork will |
| 577 | // happen in the future. If this happens and we have kCompactZygote enabled we wish to compact |
| 578 | // from the main space to the zygote space. If background compaction is enabled, always pass in |
| 579 | // that we can move objets. |
| 580 | if (kCompactZygote && Runtime::Current()->IsZygote() && !can_move_objects) { |
| 581 | // After the zygote we want this to be false if we don't have background compaction enabled so |
| 582 | // that getting primitive array elements is faster. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 583 | // We never have homogeneous compaction with GSS and don't need a space with movable objects. |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 584 | can_move_objects = !HasZygoteSpace() && foreground_collector_type_ != kCollectorTypeGSS; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 585 | } |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 586 | if (collector::SemiSpace::kUseRememberedSet && main_space_ != nullptr) { |
| 587 | RemoveRememberedSet(main_space_); |
| 588 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 589 | const char* name = kUseRosAlloc ? kRosAllocSpaceName[0] : kDlMallocSpaceName[0]; |
| 590 | main_space_ = CreateMallocSpaceFromMemMap(mem_map, initial_size, growth_limit, capacity, name, |
| 591 | can_move_objects); |
| 592 | SetSpaceAsDefault(main_space_); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 593 | VLOG(heap) << "Created main space " << main_space_; |
| 594 | } |
| 595 | |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 596 | void Heap::ChangeAllocator(AllocatorType allocator) { |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 597 | if (current_allocator_ != allocator) { |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 598 | // These two allocators are only used internally and don't have any entrypoints. |
| 599 | CHECK_NE(allocator, kAllocatorTypeLOS); |
| 600 | CHECK_NE(allocator, kAllocatorTypeNonMoving); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 601 | current_allocator_ = allocator; |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 602 | MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 603 | SetQuickAllocEntryPointsAllocator(current_allocator_); |
| 604 | Runtime::Current()->GetInstrumentation()->ResetQuickAllocEntryPoints(); |
| 605 | } |
| 606 | } |
| 607 | |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 608 | void Heap::DisableMovingGc() { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 609 | if (IsMovingGc(foreground_collector_type_)) { |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 610 | foreground_collector_type_ = kCollectorTypeCMS; |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 611 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 612 | if (IsMovingGc(background_collector_type_)) { |
| 613 | background_collector_type_ = foreground_collector_type_; |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 614 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 615 | TransitionCollector(foreground_collector_type_); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 616 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
| 617 | Thread* self = Thread::Current(); |
| 618 | ScopedThreadStateChange tsc(self, kSuspended); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 619 | tl->SuspendAll(__FUNCTION__); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 620 | // Something may have caused the transition to fail. |
Mathieu Chartier | e4927f6 | 2014-08-23 13:56:03 -0700 | [diff] [blame] | 621 | if (!IsMovingGc(collector_type_) && non_moving_space_ != main_space_) { |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 622 | CHECK(main_space_ != nullptr); |
| 623 | // The allocation stack may have non movable objects in it. We need to flush it since the GC |
| 624 | // can't only handle marking allocation stack objects of one non moving space and one main |
| 625 | // space. |
| 626 | { |
| 627 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 628 | FlushAllocStack(); |
| 629 | } |
| 630 | main_space_->DisableMovingObjects(); |
| 631 | non_moving_space_ = main_space_; |
| 632 | CHECK(!non_moving_space_->CanMoveObjects()); |
| 633 | } |
| 634 | tl->ResumeAll(); |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 637 | std::string Heap::SafeGetClassDescriptor(mirror::Class* klass) { |
| 638 | if (!IsValidContinuousSpaceObjectAddress(klass)) { |
| 639 | return StringPrintf("<non heap address klass %p>", klass); |
| 640 | } |
| 641 | mirror::Class* component_type = klass->GetComponentType<kVerifyNone>(); |
| 642 | if (IsValidContinuousSpaceObjectAddress(component_type) && klass->IsArrayClass<kVerifyNone>()) { |
| 643 | std::string result("["); |
| 644 | result += SafeGetClassDescriptor(component_type); |
| 645 | return result; |
| 646 | } else if (UNLIKELY(klass->IsPrimitive<kVerifyNone>())) { |
| 647 | return Primitive::Descriptor(klass->GetPrimitiveType<kVerifyNone>()); |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 648 | } else if (UNLIKELY(klass->IsProxyClass<kVerifyNone>())) { |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 649 | return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(klass); |
| 650 | } else { |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 651 | mirror::DexCache* dex_cache = klass->GetDexCache<kVerifyNone>(); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 652 | if (!IsValidContinuousSpaceObjectAddress(dex_cache)) { |
| 653 | return StringPrintf("<non heap address dex_cache %p>", dex_cache); |
| 654 | } |
| 655 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 656 | uint16_t class_def_idx = klass->GetDexClassDefIndex(); |
| 657 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 658 | return "<class def not found>"; |
| 659 | } |
| 660 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx); |
| 661 | const DexFile::TypeId& type_id = dex_file->GetTypeId(class_def.class_idx_); |
| 662 | return dex_file->GetTypeDescriptor(type_id); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | std::string Heap::SafePrettyTypeOf(mirror::Object* obj) { |
| 667 | if (obj == nullptr) { |
| 668 | return "null"; |
| 669 | } |
| 670 | mirror::Class* klass = obj->GetClass<kVerifyNone>(); |
| 671 | if (klass == nullptr) { |
| 672 | return "(class=null)"; |
| 673 | } |
| 674 | std::string result(SafeGetClassDescriptor(klass)); |
| 675 | if (obj->IsClass()) { |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 676 | result += "<" + SafeGetClassDescriptor(obj->AsClass<kVerifyNone>()) + ">"; |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 677 | } |
| 678 | return result; |
| 679 | } |
| 680 | |
| 681 | void Heap::DumpObject(std::ostream& stream, mirror::Object* obj) { |
| 682 | if (obj == nullptr) { |
| 683 | stream << "(obj=null)"; |
| 684 | return; |
| 685 | } |
| 686 | if (IsAligned<kObjectAlignment>(obj)) { |
| 687 | space::Space* space = nullptr; |
| 688 | // Don't use find space since it only finds spaces which actually contain objects instead of |
| 689 | // spaces which may contain objects (e.g. cleared bump pointer spaces). |
| 690 | for (const auto& cur_space : continuous_spaces_) { |
| 691 | if (cur_space->HasAddress(obj)) { |
| 692 | space = cur_space; |
| 693 | break; |
| 694 | } |
| 695 | } |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 696 | // Unprotect all the spaces. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 697 | for (const auto& con_space : continuous_spaces_) { |
| 698 | mprotect(con_space->Begin(), con_space->Capacity(), PROT_READ | PROT_WRITE); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 699 | } |
| 700 | stream << "Object " << obj; |
| 701 | if (space != nullptr) { |
| 702 | stream << " in space " << *space; |
| 703 | } |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 704 | mirror::Class* klass = obj->GetClass<kVerifyNone>(); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 705 | stream << "\nclass=" << klass; |
| 706 | if (klass != nullptr) { |
| 707 | stream << " type= " << SafePrettyTypeOf(obj); |
| 708 | } |
| 709 | // Re-protect the address we faulted on. |
| 710 | mprotect(AlignDown(obj, kPageSize), kPageSize, PROT_NONE); |
| 711 | } |
| 712 | } |
| 713 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 714 | bool Heap::IsCompilingBoot() const { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 715 | if (!Runtime::Current()->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 716 | return false; |
| 717 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 718 | for (const auto& space : continuous_spaces_) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 719 | if (space->IsImageSpace() || space->IsZygoteSpace()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 720 | return false; |
| 721 | } |
| 722 | } |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | bool Heap::HasImageSpace() const { |
| 727 | for (const auto& space : continuous_spaces_) { |
| 728 | if (space->IsImageSpace()) { |
| 729 | return true; |
| 730 | } |
| 731 | } |
| 732 | return false; |
| 733 | } |
| 734 | |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 735 | void Heap::IncrementDisableMovingGC(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 736 | // Need to do this holding the lock to prevent races where the GC is about to run / running when |
| 737 | // we attempt to disable it. |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 738 | ScopedThreadStateChange tsc(self, kWaitingForGcToComplete); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 739 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 740 | ++disable_moving_gc_count_; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 741 | if (IsMovingGc(collector_type_running_)) { |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 742 | WaitForGcToCompleteLocked(kGcCauseDisableMovingGc, self); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 743 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 746 | void Heap::DecrementDisableMovingGC(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 747 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 748 | CHECK_GT(disable_moving_gc_count_, 0U); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 749 | --disable_moving_gc_count_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Mathieu Chartier | ca2a24d | 2013-11-25 15:12:12 -0800 | [diff] [blame] | 752 | void Heap::UpdateProcessState(ProcessState process_state) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 753 | if (process_state_ != process_state) { |
| 754 | process_state_ = process_state; |
Mathieu Chartier | 91e3063 | 2014-03-25 15:58:50 -0700 | [diff] [blame] | 755 | for (size_t i = 1; i <= kCollectorTransitionStressIterations; ++i) { |
| 756 | // Start at index 1 to avoid "is always false" warning. |
| 757 | // Have iteration 1 always transition the collector. |
| 758 | TransitionCollector((((i & 1) == 1) == (process_state_ == kProcessStateJankPerceptible)) |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 759 | ? foreground_collector_type_ : background_collector_type_); |
Mathieu Chartier | 91e3063 | 2014-03-25 15:58:50 -0700 | [diff] [blame] | 760 | usleep(kCollectorTransitionStressWait); |
| 761 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 762 | if (process_state_ == kProcessStateJankPerceptible) { |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 763 | // Transition back to foreground right away to prevent jank. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 764 | RequestCollectorTransition(foreground_collector_type_, 0); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 765 | } else { |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 766 | // Don't delay for debug builds since we may want to stress test the GC. |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 767 | // If background_collector_type_ is kCollectorTypeHomogeneousSpaceCompact then we have |
| 768 | // special handling which does a homogenous space compaction once but then doesn't transition |
| 769 | // the collector. |
| 770 | RequestCollectorTransition(background_collector_type_, |
| 771 | kIsDebugBuild ? 0 : kCollectorTransitionWait); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 772 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 773 | } |
Mathieu Chartier | ca2a24d | 2013-11-25 15:12:12 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 776 | void Heap::CreateThreadPool() { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 777 | const size_t num_threads = std::max(parallel_gc_threads_, conc_gc_threads_); |
| 778 | if (num_threads != 0) { |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 779 | thread_pool_.reset(new ThreadPool("Heap thread pool", num_threads)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 780 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 783 | // Visit objects when threads aren't suspended. If concurrent moving |
| 784 | // GC, disable moving GC and suspend threads and then visit objects. |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 785 | void Heap::VisitObjects(ObjectCallback callback, void* arg) { |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 786 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 787 | Locks::mutator_lock_->AssertSharedHeld(self); |
| 788 | DCHECK(!Locks::mutator_lock_->IsExclusiveHeld(self)) << "Call VisitObjectsPaused() instead"; |
| 789 | if (IsGcConcurrentAndMoving()) { |
| 790 | // Concurrent moving GC. Just suspending threads isn't sufficient |
| 791 | // because a collection isn't one big pause and we could suspend |
| 792 | // threads in the middle (between phases) of a concurrent moving |
| 793 | // collection where it's not easily known which objects are alive |
| 794 | // (both the region space and the non-moving space) or which |
| 795 | // copies of objects to visit, and the to-space invariant could be |
| 796 | // easily broken. Visit objects while GC isn't running by using |
| 797 | // IncrementDisableMovingGC() and threads are suspended. |
| 798 | IncrementDisableMovingGC(self); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 799 | self->TransitionFromRunnableToSuspended(kWaitingForVisitObjects); |
| 800 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 801 | tl->SuspendAll(__FUNCTION__); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 802 | VisitObjectsInternalRegionSpace(callback, arg); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 803 | VisitObjectsInternal(callback, arg); |
| 804 | tl->ResumeAll(); |
| 805 | self->TransitionFromSuspendedToRunnable(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 806 | DecrementDisableMovingGC(self); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 807 | } else { |
| 808 | // GCs can move objects, so don't allow this. |
| 809 | ScopedAssertNoThreadSuspension ants(self, "Visiting objects"); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 810 | DCHECK(region_space_ == nullptr); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 811 | VisitObjectsInternal(callback, arg); |
| 812 | } |
| 813 | } |
| 814 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 815 | // Visit objects when threads are already suspended. |
| 816 | void Heap::VisitObjectsPaused(ObjectCallback callback, void* arg) { |
| 817 | Thread* self = Thread::Current(); |
| 818 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 819 | VisitObjectsInternalRegionSpace(callback, arg); |
| 820 | VisitObjectsInternal(callback, arg); |
| 821 | } |
| 822 | |
| 823 | // Visit objects in the region spaces. |
| 824 | void Heap::VisitObjectsInternalRegionSpace(ObjectCallback callback, void* arg) { |
| 825 | Thread* self = Thread::Current(); |
| 826 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 827 | if (region_space_ != nullptr) { |
| 828 | DCHECK(IsGcConcurrentAndMoving()); |
| 829 | if (!zygote_creation_lock_.IsExclusiveHeld(self)) { |
| 830 | // Exclude the pre-zygote fork time where the semi-space collector |
| 831 | // calls VerifyHeapReferences() as part of the zygote compaction |
| 832 | // which then would call here without the moving GC disabled, |
| 833 | // which is fine. |
| 834 | DCHECK(IsMovingGCDisabled(self)); |
| 835 | } |
| 836 | region_space_->Walk(callback, arg); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | // Visit objects in the other spaces. |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 841 | void Heap::VisitObjectsInternal(ObjectCallback callback, void* arg) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 842 | if (bump_pointer_space_ != nullptr) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 843 | // Visit objects in bump pointer space. |
| 844 | bump_pointer_space_->Walk(callback, arg); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 845 | } |
| 846 | // TODO: Switch to standard begin and end to use ranged a based loop. |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 847 | for (auto* it = allocation_stack_->Begin(), *end = allocation_stack_->End(); it < end; ++it) { |
| 848 | mirror::Object* const obj = it->AsMirrorPtr(); |
Mathieu Chartier | ebdf3f3 | 2014-02-13 10:23:27 -0800 | [diff] [blame] | 849 | if (obj != nullptr && obj->GetClass() != nullptr) { |
| 850 | // Avoid the race condition caused by the object not yet being written into the allocation |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 851 | // stack or the class not yet being written in the object. Or, if |
| 852 | // kUseThreadLocalAllocationStack, there can be nulls on the allocation stack. |
Mathieu Chartier | ebdf3f3 | 2014-02-13 10:23:27 -0800 | [diff] [blame] | 853 | callback(obj, arg); |
| 854 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 855 | } |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 856 | { |
| 857 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 858 | GetLiveBitmap()->Walk(callback, arg); |
| 859 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | void Heap::MarkAllocStackAsLive(accounting::ObjectStack* stack) { |
Mathieu Chartier | 00b5915 | 2014-07-25 10:13:51 -0700 | [diff] [blame] | 863 | space::ContinuousSpace* space1 = main_space_ != nullptr ? main_space_ : non_moving_space_; |
| 864 | space::ContinuousSpace* space2 = non_moving_space_; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 865 | // TODO: Generalize this to n bitmaps? |
Mathieu Chartier | 00b5915 | 2014-07-25 10:13:51 -0700 | [diff] [blame] | 866 | CHECK(space1 != nullptr); |
| 867 | CHECK(space2 != nullptr); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 868 | MarkAllocStack(space1->GetLiveBitmap(), space2->GetLiveBitmap(), |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 869 | (large_object_space_ != nullptr ? large_object_space_->GetLiveBitmap() : nullptr), |
| 870 | stack); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 873 | void Heap::DeleteThreadPool() { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 874 | thread_pool_.reset(nullptr); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 877 | void Heap::AddSpace(space::Space* space) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 878 | CHECK(space != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 879 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 880 | if (space->IsContinuousSpace()) { |
| 881 | DCHECK(!space->IsDiscontinuousSpace()); |
| 882 | space::ContinuousSpace* continuous_space = space->AsContinuousSpace(); |
| 883 | // Continuous spaces don't necessarily have bitmaps. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 884 | accounting::ContinuousSpaceBitmap* live_bitmap = continuous_space->GetLiveBitmap(); |
| 885 | accounting::ContinuousSpaceBitmap* mark_bitmap = continuous_space->GetMarkBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 886 | if (live_bitmap != nullptr) { |
Mathieu Chartier | 2796a16 | 2014-07-25 11:50:47 -0700 | [diff] [blame] | 887 | CHECK(mark_bitmap != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 888 | live_bitmap_->AddContinuousSpaceBitmap(live_bitmap); |
| 889 | mark_bitmap_->AddContinuousSpaceBitmap(mark_bitmap); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 890 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 891 | continuous_spaces_.push_back(continuous_space); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 892 | // Ensure that spaces remain sorted in increasing order of start address. |
| 893 | std::sort(continuous_spaces_.begin(), continuous_spaces_.end(), |
| 894 | [](const space::ContinuousSpace* a, const space::ContinuousSpace* b) { |
| 895 | return a->Begin() < b->Begin(); |
| 896 | }); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 897 | } else { |
Mathieu Chartier | 2796a16 | 2014-07-25 11:50:47 -0700 | [diff] [blame] | 898 | CHECK(space->IsDiscontinuousSpace()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 899 | space::DiscontinuousSpace* discontinuous_space = space->AsDiscontinuousSpace(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 900 | live_bitmap_->AddLargeObjectBitmap(discontinuous_space->GetLiveBitmap()); |
| 901 | mark_bitmap_->AddLargeObjectBitmap(discontinuous_space->GetMarkBitmap()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 902 | discontinuous_spaces_.push_back(discontinuous_space); |
| 903 | } |
| 904 | if (space->IsAllocSpace()) { |
| 905 | alloc_spaces_.push_back(space->AsAllocSpace()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 906 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 909 | void Heap::SetSpaceAsDefault(space::ContinuousSpace* continuous_space) { |
| 910 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 911 | if (continuous_space->IsDlMallocSpace()) { |
| 912 | dlmalloc_space_ = continuous_space->AsDlMallocSpace(); |
| 913 | } else if (continuous_space->IsRosAllocSpace()) { |
| 914 | rosalloc_space_ = continuous_space->AsRosAllocSpace(); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | void Heap::RemoveSpace(space::Space* space) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 919 | DCHECK(space != nullptr); |
| 920 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 921 | if (space->IsContinuousSpace()) { |
| 922 | DCHECK(!space->IsDiscontinuousSpace()); |
| 923 | space::ContinuousSpace* continuous_space = space->AsContinuousSpace(); |
| 924 | // Continuous spaces don't necessarily have bitmaps. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 925 | accounting::ContinuousSpaceBitmap* live_bitmap = continuous_space->GetLiveBitmap(); |
| 926 | accounting::ContinuousSpaceBitmap* mark_bitmap = continuous_space->GetMarkBitmap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 927 | if (live_bitmap != nullptr) { |
| 928 | DCHECK(mark_bitmap != nullptr); |
| 929 | live_bitmap_->RemoveContinuousSpaceBitmap(live_bitmap); |
| 930 | mark_bitmap_->RemoveContinuousSpaceBitmap(mark_bitmap); |
| 931 | } |
| 932 | auto it = std::find(continuous_spaces_.begin(), continuous_spaces_.end(), continuous_space); |
| 933 | DCHECK(it != continuous_spaces_.end()); |
| 934 | continuous_spaces_.erase(it); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 935 | } else { |
| 936 | DCHECK(space->IsDiscontinuousSpace()); |
| 937 | space::DiscontinuousSpace* discontinuous_space = space->AsDiscontinuousSpace(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 938 | live_bitmap_->RemoveLargeObjectBitmap(discontinuous_space->GetLiveBitmap()); |
| 939 | mark_bitmap_->RemoveLargeObjectBitmap(discontinuous_space->GetMarkBitmap()); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 940 | auto it = std::find(discontinuous_spaces_.begin(), discontinuous_spaces_.end(), |
| 941 | discontinuous_space); |
| 942 | DCHECK(it != discontinuous_spaces_.end()); |
| 943 | discontinuous_spaces_.erase(it); |
| 944 | } |
| 945 | if (space->IsAllocSpace()) { |
| 946 | auto it = std::find(alloc_spaces_.begin(), alloc_spaces_.end(), space->AsAllocSpace()); |
| 947 | DCHECK(it != alloc_spaces_.end()); |
| 948 | alloc_spaces_.erase(it); |
| 949 | } |
| 950 | } |
| 951 | |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 952 | void Heap::DumpGcPerformanceInfo(std::ostream& os) { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 953 | // Dump cumulative timings. |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 954 | os << "Dumping cumulative Gc timings\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 955 | uint64_t total_duration = 0; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 956 | // Dump cumulative loggers for each GC type. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 957 | uint64_t total_paused_time = 0; |
Mathieu Chartier | 5a48719 | 2014-04-08 11:14:54 -0700 | [diff] [blame] | 958 | for (auto& collector : garbage_collectors_) { |
Mathieu Chartier | 104fa0c | 2014-08-07 14:26:27 -0700 | [diff] [blame] | 959 | total_duration += collector->GetCumulativeTimings().GetTotalNs(); |
| 960 | total_paused_time += collector->GetTotalPausedTimeNs(); |
| 961 | collector->DumpPerformanceInfo(os); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 962 | } |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 963 | uint64_t allocation_time = |
| 964 | static_cast<uint64_t>(total_allocation_time_.LoadRelaxed()) * kTimeAdjust; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 965 | if (total_duration != 0) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 966 | const double total_seconds = static_cast<double>(total_duration / 1000) / 1000000.0; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 967 | os << "Total time spent in GC: " << PrettyDuration(total_duration) << "\n"; |
| 968 | os << "Mean GC size throughput: " |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 969 | << PrettySize(GetBytesFreedEver() / total_seconds) << "/s\n"; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 970 | os << "Mean GC object throughput: " |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 971 | << (GetObjectsFreedEver() / total_seconds) << " objects/s\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 972 | } |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 973 | uint64_t total_objects_allocated = GetObjectsAllocatedEver(); |
Mathieu Chartier | c30a725 | 2014-08-12 10:13:48 -0700 | [diff] [blame] | 974 | os << "Total number of allocations " << total_objects_allocated << "\n"; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 975 | os << "Total bytes allocated " << PrettySize(GetBytesAllocatedEver()) << "\n"; |
| 976 | os << "Total bytes freed " << PrettySize(GetBytesFreedEver()) << "\n"; |
Mathieu Chartier | c30a725 | 2014-08-12 10:13:48 -0700 | [diff] [blame] | 977 | os << "Free memory " << PrettySize(GetFreeMemory()) << "\n"; |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 978 | os << "Free memory until GC " << PrettySize(GetFreeMemoryUntilGC()) << "\n"; |
| 979 | os << "Free memory until OOME " << PrettySize(GetFreeMemoryUntilOOME()) << "\n"; |
Mathieu Chartier | c30a725 | 2014-08-12 10:13:48 -0700 | [diff] [blame] | 980 | os << "Total memory " << PrettySize(GetTotalMemory()) << "\n"; |
| 981 | os << "Max memory " << PrettySize(GetMaxMemory()) << "\n"; |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 982 | if (kMeasureAllocationTime) { |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 983 | os << "Total time spent allocating: " << PrettyDuration(allocation_time) << "\n"; |
| 984 | os << "Mean allocation time: " << PrettyDuration(allocation_time / total_objects_allocated) |
| 985 | << "\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 986 | } |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 987 | if (HasZygoteSpace()) { |
| 988 | os << "Zygote space size " << PrettySize(zygote_space_->Size()) << "\n"; |
| 989 | } |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 990 | os << "Total mutator paused time: " << PrettyDuration(total_paused_time) << "\n"; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 991 | os << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_) << "\n"; |
| 992 | os << "Total GC count: " << GetGcCount() << "\n"; |
| 993 | os << "Total GC time: " << PrettyDuration(GetGcTime()) << "\n"; |
| 994 | os << "Total blocking GC count: " << GetBlockingGcCount() << "\n"; |
| 995 | os << "Total blocking GC time: " << PrettyDuration(GetBlockingGcTime()) << "\n"; |
| 996 | |
| 997 | { |
| 998 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 999 | if (gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1000 | os << "Histogram of GC count per " << NsToMs(kGcCountRateHistogramWindowDuration) << " ms: "; |
| 1001 | gc_count_rate_histogram_.DumpBins(os); |
| 1002 | os << "\n"; |
| 1003 | } |
| 1004 | if (blocking_gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1005 | os << "Histogram of blocking GC count per " |
| 1006 | << NsToMs(kGcCountRateHistogramWindowDuration) << " ms: "; |
| 1007 | blocking_gc_count_rate_histogram_.DumpBins(os); |
| 1008 | os << "\n"; |
| 1009 | } |
| 1010 | } |
| 1011 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1012 | BaseMutex::DumpAll(os); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Hiroshi Yamauchi | 3767017 | 2015-06-10 17:20:54 -0700 | [diff] [blame] | 1015 | void Heap::ResetGcPerformanceInfo() { |
| 1016 | for (auto& collector : garbage_collectors_) { |
| 1017 | collector->ResetMeasurements(); |
| 1018 | } |
| 1019 | total_allocation_time_.StoreRelaxed(0); |
| 1020 | total_bytes_freed_ever_ = 0; |
| 1021 | total_objects_freed_ever_ = 0; |
| 1022 | total_wait_time_ = 0; |
| 1023 | blocking_gc_count_ = 0; |
| 1024 | blocking_gc_time_ = 0; |
| 1025 | gc_count_last_window_ = 0; |
| 1026 | blocking_gc_count_last_window_ = 0; |
| 1027 | last_update_time_gc_count_rate_histograms_ = // Round down by the window duration. |
| 1028 | (NanoTime() / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration; |
| 1029 | { |
| 1030 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 1031 | gc_count_rate_histogram_.Reset(); |
| 1032 | blocking_gc_count_rate_histogram_.Reset(); |
| 1033 | } |
| 1034 | } |
| 1035 | |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 1036 | uint64_t Heap::GetGcCount() const { |
| 1037 | uint64_t gc_count = 0U; |
| 1038 | for (auto& collector : garbage_collectors_) { |
| 1039 | gc_count += collector->GetCumulativeTimings().GetIterations(); |
| 1040 | } |
| 1041 | return gc_count; |
| 1042 | } |
| 1043 | |
| 1044 | uint64_t Heap::GetGcTime() const { |
| 1045 | uint64_t gc_time = 0U; |
| 1046 | for (auto& collector : garbage_collectors_) { |
| 1047 | gc_time += collector->GetCumulativeTimings().GetTotalNs(); |
| 1048 | } |
| 1049 | return gc_time; |
| 1050 | } |
| 1051 | |
| 1052 | uint64_t Heap::GetBlockingGcCount() const { |
| 1053 | return blocking_gc_count_; |
| 1054 | } |
| 1055 | |
| 1056 | uint64_t Heap::GetBlockingGcTime() const { |
| 1057 | return blocking_gc_time_; |
| 1058 | } |
| 1059 | |
| 1060 | void Heap::DumpGcCountRateHistogram(std::ostream& os) const { |
| 1061 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 1062 | if (gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1063 | gc_count_rate_histogram_.DumpBins(os); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | void Heap::DumpBlockingGcCountRateHistogram(std::ostream& os) const { |
| 1068 | MutexLock mu(Thread::Current(), *gc_complete_lock_); |
| 1069 | if (blocking_gc_count_rate_histogram_.SampleSize() > 0U) { |
| 1070 | blocking_gc_count_rate_histogram_.DumpBins(os); |
| 1071 | } |
| 1072 | } |
| 1073 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1074 | Heap::~Heap() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1075 | VLOG(heap) << "Starting ~Heap()"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1076 | STLDeleteElements(&garbage_collectors_); |
| 1077 | // If we don't reset then the mark stack complains in its destructor. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1078 | allocation_stack_->Reset(); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1079 | allocation_records_.reset(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1080 | live_stack_->Reset(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 1081 | STLDeleteValues(&mod_union_tables_); |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 1082 | STLDeleteValues(&remembered_sets_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1083 | STLDeleteElements(&continuous_spaces_); |
| 1084 | STLDeleteElements(&discontinuous_spaces_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1085 | delete gc_complete_lock_; |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1086 | delete pending_task_lock_; |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1087 | delete backtrace_lock_; |
| 1088 | if (unique_backtrace_count_.LoadRelaxed() != 0 || seen_backtrace_count_.LoadRelaxed() != 0) { |
| 1089 | LOG(INFO) << "gc stress unique=" << unique_backtrace_count_.LoadRelaxed() |
| 1090 | << " total=" << seen_backtrace_count_.LoadRelaxed() + |
| 1091 | unique_backtrace_count_.LoadRelaxed(); |
| 1092 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1093 | VLOG(heap) << "Finished ~Heap()"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1096 | space::ContinuousSpace* Heap::FindContinuousSpaceFromObject(const mirror::Object* obj, |
| 1097 | bool fail_ok) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1098 | for (const auto& space : continuous_spaces_) { |
| 1099 | if (space->Contains(obj)) { |
| 1100 | return space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1103 | if (!fail_ok) { |
| 1104 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 1105 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1106 | return nullptr; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1109 | space::DiscontinuousSpace* Heap::FindDiscontinuousSpaceFromObject(const mirror::Object* obj, |
| 1110 | bool fail_ok) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1111 | for (const auto& space : discontinuous_spaces_) { |
| 1112 | if (space->Contains(obj)) { |
| 1113 | return space; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | if (!fail_ok) { |
| 1117 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 1118 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1119 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | space::Space* Heap::FindSpaceFromObject(const mirror::Object* obj, bool fail_ok) const { |
| 1123 | space::Space* result = FindContinuousSpaceFromObject(obj, true); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1124 | if (result != nullptr) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1125 | return result; |
| 1126 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1127 | return FindDiscontinuousSpaceFromObject(obj, fail_ok); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | space::ImageSpace* Heap::GetImageSpace() const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1131 | for (const auto& space : continuous_spaces_) { |
| 1132 | if (space->IsImageSpace()) { |
| 1133 | return space->AsImageSpace(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1134 | } |
| 1135 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1136 | return nullptr; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1139 | void Heap::ThrowOutOfMemoryError(Thread* self, size_t byte_count, AllocatorType allocator_type) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1140 | std::ostringstream oss; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1141 | size_t total_bytes_free = GetFreeMemory(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1142 | oss << "Failed to allocate a " << byte_count << " byte allocation with " << total_bytes_free |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1143 | << " free bytes and " << PrettySize(GetFreeMemoryUntilOOME()) << " until OOM"; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1144 | // If the allocation failed due to fragmentation, print out the largest continuous allocation. |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1145 | if (total_bytes_free >= byte_count) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1146 | space::AllocSpace* space = nullptr; |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1147 | if (allocator_type == kAllocatorTypeNonMoving) { |
| 1148 | space = non_moving_space_; |
| 1149 | } else if (allocator_type == kAllocatorTypeRosAlloc || |
| 1150 | allocator_type == kAllocatorTypeDlMalloc) { |
| 1151 | space = main_space_; |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1152 | } else if (allocator_type == kAllocatorTypeBumpPointer || |
| 1153 | allocator_type == kAllocatorTypeTLAB) { |
| 1154 | space = bump_pointer_space_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1155 | } else if (allocator_type == kAllocatorTypeRegion || |
| 1156 | allocator_type == kAllocatorTypeRegionTLAB) { |
| 1157 | space = region_space_; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1158 | } |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1159 | if (space != nullptr) { |
| 1160 | space->LogFragmentationAllocFailure(oss, byte_count); |
| 1161 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1162 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1163 | self->ThrowOutOfMemoryError(oss.str().c_str()); |
| 1164 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1165 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1166 | void Heap::DoPendingCollectorTransition() { |
| 1167 | CollectorType desired_collector_type = desired_collector_type_; |
Mathieu Chartier | b272855 | 2014-09-08 20:08:41 +0000 | [diff] [blame] | 1168 | // Launch homogeneous space compaction if it is desired. |
| 1169 | if (desired_collector_type == kCollectorTypeHomogeneousSpaceCompact) { |
| 1170 | if (!CareAboutPauseTimes()) { |
| 1171 | PerformHomogeneousSpaceCompact(); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1172 | } else { |
| 1173 | VLOG(gc) << "Homogeneous compaction ignored due to jank perceptible process state"; |
Mathieu Chartier | b272855 | 2014-09-08 20:08:41 +0000 | [diff] [blame] | 1174 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1175 | } else { |
| 1176 | TransitionCollector(desired_collector_type); |
Mathieu Chartier | b272855 | 2014-09-08 20:08:41 +0000 | [diff] [blame] | 1177 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | void Heap::Trim(Thread* self) { |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1181 | if (!CareAboutPauseTimes()) { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1182 | ATRACE_BEGIN("Deflating monitors"); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1183 | // Deflate the monitors, this can cause a pause but shouldn't matter since we don't care |
| 1184 | // about pauses. |
| 1185 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 1186 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1187 | uint64_t start_time = NanoTime(); |
| 1188 | size_t count = runtime->GetMonitorList()->DeflateMonitors(); |
| 1189 | VLOG(heap) << "Deflating " << count << " monitors took " |
| 1190 | << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1191 | runtime->GetThreadList()->ResumeAll(); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1192 | ATRACE_END(); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1193 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1194 | TrimIndirectReferenceTables(self); |
| 1195 | TrimSpaces(self); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 1198 | class TrimIndirectReferenceTableClosure : public Closure { |
| 1199 | public: |
| 1200 | explicit TrimIndirectReferenceTableClosure(Barrier* barrier) : barrier_(barrier) { |
| 1201 | } |
| 1202 | virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
| 1203 | ATRACE_BEGIN("Trimming reference table"); |
| 1204 | thread->GetJniEnv()->locals.Trim(); |
| 1205 | ATRACE_END(); |
Lei Li | dd9943d | 2015-02-02 14:24:44 +0800 | [diff] [blame] | 1206 | // If thread is a running mutator, then act on behalf of the trim thread. |
| 1207 | // See the code in ThreadList::RunCheckpoint. |
| 1208 | if (thread->GetState() == kRunnable) { |
| 1209 | barrier_->Pass(Thread::Current()); |
| 1210 | } |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | private: |
| 1214 | Barrier* const barrier_; |
| 1215 | }; |
| 1216 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1217 | void Heap::TrimIndirectReferenceTables(Thread* self) { |
| 1218 | ScopedObjectAccess soa(self); |
| 1219 | ATRACE_BEGIN(__FUNCTION__); |
| 1220 | JavaVMExt* vm = soa.Vm(); |
| 1221 | // Trim globals indirect reference table. |
| 1222 | vm->TrimGlobals(); |
| 1223 | // Trim locals indirect reference tables. |
| 1224 | Barrier barrier(0); |
| 1225 | TrimIndirectReferenceTableClosure closure(&barrier); |
| 1226 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 1227 | size_t barrier_count = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure); |
Lei Li | dd9943d | 2015-02-02 14:24:44 +0800 | [diff] [blame] | 1228 | if (barrier_count != 0) { |
| 1229 | barrier.Increment(self, barrier_count); |
| 1230 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1231 | ATRACE_END(); |
| 1232 | } |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 1233 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1234 | void Heap::TrimSpaces(Thread* self) { |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 1235 | { |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 1236 | // Need to do this before acquiring the locks since we don't want to get suspended while |
| 1237 | // holding any locks. |
| 1238 | ScopedThreadStateChange tsc(self, kWaitingForGcToComplete); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1239 | // Pretend we are doing a GC to prevent background compaction from deleting the space we are |
| 1240 | // trimming. |
| 1241 | MutexLock mu(self, *gc_complete_lock_); |
| 1242 | // Ensure there is only one GC at a time. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 1243 | WaitForGcToCompleteLocked(kGcCauseTrim, self); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1244 | collector_type_running_ = kCollectorTypeHeapTrim; |
| 1245 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1246 | ATRACE_BEGIN(__FUNCTION__); |
| 1247 | const uint64_t start_ns = NanoTime(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1248 | // Trim the managed spaces. |
| 1249 | uint64_t total_alloc_space_allocated = 0; |
| 1250 | uint64_t total_alloc_space_size = 0; |
| 1251 | uint64_t managed_reclaimed = 0; |
| 1252 | for (const auto& space : continuous_spaces_) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 1253 | if (space->IsMallocSpace()) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1254 | gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 1255 | if (malloc_space->IsRosAllocSpace() || !CareAboutPauseTimes()) { |
| 1256 | // Don't trim dlmalloc spaces if we care about pauses since this can hold the space lock |
| 1257 | // for a long period of time. |
| 1258 | managed_reclaimed += malloc_space->Trim(); |
| 1259 | } |
| 1260 | total_alloc_space_size += malloc_space->Size(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 1263 | total_alloc_space_allocated = GetBytesAllocated(); |
| 1264 | if (large_object_space_ != nullptr) { |
| 1265 | total_alloc_space_allocated -= large_object_space_->GetBytesAllocated(); |
| 1266 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1267 | if (bump_pointer_space_ != nullptr) { |
| 1268 | total_alloc_space_allocated -= bump_pointer_space_->Size(); |
| 1269 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1270 | if (region_space_ != nullptr) { |
| 1271 | total_alloc_space_allocated -= region_space_->GetBytesAllocated(); |
| 1272 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1273 | const float managed_utilization = static_cast<float>(total_alloc_space_allocated) / |
| 1274 | static_cast<float>(total_alloc_space_size); |
| 1275 | uint64_t gc_heap_end_ns = NanoTime(); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1276 | // We never move things in the native heap, so we can finish the GC at this point. |
| 1277 | FinishGC(self, collector::kGcTypeNone); |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 1278 | size_t native_reclaimed = 0; |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 1279 | |
| 1280 | #ifdef HAVE_ANDROID_OS |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1281 | // Only trim the native heap if we don't care about pauses. |
| 1282 | if (!CareAboutPauseTimes()) { |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 1283 | #if defined(USE_DLMALLOC) |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1284 | // Trim the native heap. |
| 1285 | dlmalloc_trim(0); |
| 1286 | dlmalloc_inspect_all(DlmallocMadviseCallback, &native_reclaimed); |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 1287 | #elif defined(USE_JEMALLOC) |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1288 | // Jemalloc does it's own internal trimming. |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 1289 | #else |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1290 | UNIMPLEMENTED(WARNING) << "Add trimming support"; |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 1291 | #endif |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1292 | } |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 1293 | #endif // HAVE_ANDROID_OS |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1294 | uint64_t end_ns = NanoTime(); |
| 1295 | VLOG(heap) << "Heap trim of managed (duration=" << PrettyDuration(gc_heap_end_ns - start_ns) |
| 1296 | << ", advised=" << PrettySize(managed_reclaimed) << ") and native (duration=" |
| 1297 | << PrettyDuration(end_ns - gc_heap_end_ns) << ", advised=" << PrettySize(native_reclaimed) |
| 1298 | << ") heaps. Managed heap utilization of " << static_cast<int>(100 * managed_utilization) |
| 1299 | << "%."; |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1300 | ATRACE_END(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | bool Heap::IsValidObjectAddress(const mirror::Object* obj) const { |
| 1304 | // Note: we deliberately don't take the lock here, and mustn't test anything that would require |
| 1305 | // taking the lock. |
| 1306 | if (obj == nullptr) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 1307 | return true; |
| 1308 | } |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 1309 | return IsAligned<kObjectAlignment>(obj) && FindSpaceFromObject(obj, true) != nullptr; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Mathieu Chartier | d68ac70 | 2014-02-11 14:50:51 -0800 | [diff] [blame] | 1312 | bool Heap::IsNonDiscontinuousSpaceHeapAddress(const mirror::Object* obj) const { |
| 1313 | return FindContinuousSpaceFromObject(obj, true) != nullptr; |
| 1314 | } |
| 1315 | |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 1316 | bool Heap::IsValidContinuousSpaceObjectAddress(const mirror::Object* obj) const { |
| 1317 | if (obj == nullptr || !IsAligned<kObjectAlignment>(obj)) { |
| 1318 | return false; |
| 1319 | } |
| 1320 | for (const auto& space : continuous_spaces_) { |
| 1321 | if (space->HasAddress(obj)) { |
| 1322 | return true; |
| 1323 | } |
| 1324 | } |
| 1325 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1328 | bool Heap::IsLiveObjectLocked(mirror::Object* obj, bool search_allocation_stack, |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1329 | bool search_live_stack, bool sorted) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1330 | if (UNLIKELY(!IsAligned<kObjectAlignment>(obj))) { |
| 1331 | return false; |
| 1332 | } |
| 1333 | if (bump_pointer_space_ != nullptr && bump_pointer_space_->HasAddress(obj)) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1334 | mirror::Class* klass = obj->GetClass<kVerifyNone>(); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1335 | if (obj == klass) { |
Mathieu Chartier | 9be9a7a | 2014-01-24 14:07:33 -0800 | [diff] [blame] | 1336 | // This case happens for java.lang.Class. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1337 | return true; |
| 1338 | } |
| 1339 | return VerifyClassClass(klass) && IsLiveObjectLocked(klass); |
| 1340 | } else if (temp_space_ != nullptr && temp_space_->HasAddress(obj)) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1341 | // If we are in the allocated region of the temp space, then we are probably live (e.g. during |
| 1342 | // a GC). When a GC isn't running End() - Begin() is 0 which means no objects are contained. |
| 1343 | return temp_space_->Contains(obj); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1344 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1345 | if (region_space_ != nullptr && region_space_->HasAddress(obj)) { |
| 1346 | return true; |
| 1347 | } |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1348 | space::ContinuousSpace* c_space = FindContinuousSpaceFromObject(obj, true); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1349 | space::DiscontinuousSpace* d_space = nullptr; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1350 | if (c_space != nullptr) { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1351 | if (c_space->GetLiveBitmap()->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1352 | return true; |
| 1353 | } |
| 1354 | } else { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1355 | d_space = FindDiscontinuousSpaceFromObject(obj, true); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1356 | if (d_space != nullptr) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1357 | if (d_space->GetLiveBitmap()->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1358 | return true; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1362 | // This is covering the allocation/live stack swapping that is done without mutators suspended. |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1363 | for (size_t i = 0; i < (sorted ? 1 : 5); ++i) { |
| 1364 | if (i > 0) { |
| 1365 | NanoSleep(MsToNs(10)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1366 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1367 | if (search_allocation_stack) { |
| 1368 | if (sorted) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1369 | if (allocation_stack_->ContainsSorted(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1370 | return true; |
| 1371 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1372 | } else if (allocation_stack_->Contains(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1373 | return true; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | if (search_live_stack) { |
| 1378 | if (sorted) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1379 | if (live_stack_->ContainsSorted(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1380 | return true; |
| 1381 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1382 | } else if (live_stack_->Contains(obj)) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1383 | return true; |
| 1384 | } |
| 1385 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1386 | } |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1387 | // We need to check the bitmaps again since there is a race where we mark something as live and |
| 1388 | // then clear the stack containing it. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1389 | if (c_space != nullptr) { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1390 | if (c_space->GetLiveBitmap()->Test(obj)) { |
| 1391 | return true; |
| 1392 | } |
| 1393 | } else { |
| 1394 | d_space = FindDiscontinuousSpaceFromObject(obj, true); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1395 | if (d_space != nullptr && d_space->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | f082d3c | 2013-07-29 17:04:07 -0700 | [diff] [blame] | 1396 | return true; |
| 1397 | } |
| 1398 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1399 | return false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 1402 | std::string Heap::DumpSpaces() const { |
| 1403 | std::ostringstream oss; |
| 1404 | DumpSpaces(oss); |
| 1405 | return oss.str(); |
| 1406 | } |
| 1407 | |
| 1408 | void Heap::DumpSpaces(std::ostream& stream) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1409 | for (const auto& space : continuous_spaces_) { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 1410 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 1411 | accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1412 | stream << space << " " << *space << "\n"; |
| 1413 | if (live_bitmap != nullptr) { |
| 1414 | stream << live_bitmap << " " << *live_bitmap << "\n"; |
| 1415 | } |
| 1416 | if (mark_bitmap != nullptr) { |
| 1417 | stream << mark_bitmap << " " << *mark_bitmap << "\n"; |
| 1418 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1419 | } |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1420 | for (const auto& space : discontinuous_spaces_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1421 | stream << space << " " << *space << "\n"; |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 1422 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1425 | void Heap::VerifyObjectBody(mirror::Object* obj) { |
Stephen Hines | 22c6a81 | 2014-07-16 11:03:43 -0700 | [diff] [blame] | 1426 | if (verify_object_mode_ == kVerifyObjectModeDisabled) { |
| 1427 | return; |
| 1428 | } |
| 1429 | |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 1430 | // Ignore early dawn of the universe verifications. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 1431 | if (UNLIKELY(static_cast<size_t>(num_bytes_allocated_.LoadRelaxed()) < 10 * KB)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1432 | return; |
| 1433 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1434 | CHECK(IsAligned<kObjectAlignment>(obj)) << "Object isn't aligned: " << obj; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1435 | mirror::Class* c = obj->GetFieldObject<mirror::Class, kVerifyNone>(mirror::Object::ClassOffset()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1436 | CHECK(c != nullptr) << "Null class in object " << obj; |
| 1437 | CHECK(IsAligned<kObjectAlignment>(c)) << "Class " << c << " not aligned in object " << obj; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 1438 | CHECK(VerifyClassClass(c)); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 1439 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1440 | if (verify_object_mode_ > kVerifyObjectModeFast) { |
| 1441 | // Note: the bitmap tests below are racy since we don't hold the heap bitmap lock. |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 1442 | CHECK(IsLiveObjectLocked(obj)) << "Object is dead " << obj << "\n" << DumpSpaces(); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 1443 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1446 | void Heap::VerificationCallback(mirror::Object* obj, void* arg) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1447 | reinterpret_cast<Heap*>(arg)->VerifyObjectBody(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | void Heap::VerifyHeap() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1451 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1452 | GetLiveBitmap()->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 1455 | void Heap::RecordFree(uint64_t freed_objects, int64_t freed_bytes) { |
Mathieu Chartier | 601276a | 2014-03-20 15:12:30 -0700 | [diff] [blame] | 1456 | // Use signed comparison since freed bytes can be negative when background compaction foreground |
| 1457 | // transitions occurs. This is caused by the moving objects from a bump pointer space to a |
| 1458 | // free list backed space typically increasing memory footprint due to padding and binning. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 1459 | DCHECK_LE(freed_bytes, static_cast<int64_t>(num_bytes_allocated_.LoadRelaxed())); |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 1460 | // Note: This relies on 2s complement for handling negative freed_bytes. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 1461 | num_bytes_allocated_.FetchAndSubSequentiallyConsistent(static_cast<ssize_t>(freed_bytes)); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1462 | if (Runtime::Current()->HasStatsEnabled()) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1463 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1464 | thread_stats->freed_objects += freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1465 | thread_stats->freed_bytes += freed_bytes; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1466 | // TODO: Do this concurrently. |
| 1467 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 1468 | global_stats->freed_objects += freed_objects; |
| 1469 | global_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1470 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1473 | void Heap::RecordFreeRevoke() { |
| 1474 | // Subtract num_bytes_freed_revoke_ from num_bytes_allocated_ to cancel out the |
| 1475 | // the ahead-of-time, bulk counting of bytes allocated in rosalloc thread-local buffers. |
| 1476 | // If there's a concurrent revoke, ok to not necessarily reset num_bytes_freed_revoke_ |
| 1477 | // all the way to zero exactly as the remainder will be subtracted at the next GC. |
| 1478 | size_t bytes_freed = num_bytes_freed_revoke_.LoadSequentiallyConsistent(); |
| 1479 | CHECK_GE(num_bytes_freed_revoke_.FetchAndSubSequentiallyConsistent(bytes_freed), |
| 1480 | bytes_freed) << "num_bytes_freed_revoke_ underflow"; |
| 1481 | CHECK_GE(num_bytes_allocated_.FetchAndSubSequentiallyConsistent(bytes_freed), |
| 1482 | bytes_freed) << "num_bytes_allocated_ underflow"; |
| 1483 | GetCurrentGcIteration()->SetFreedRevoke(bytes_freed); |
| 1484 | } |
| 1485 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1486 | space::RosAllocSpace* Heap::GetRosAllocSpace(gc::allocator::RosAlloc* rosalloc) const { |
| 1487 | for (const auto& space : continuous_spaces_) { |
| 1488 | if (space->AsContinuousSpace()->IsRosAllocSpace()) { |
| 1489 | if (space->AsContinuousSpace()->AsRosAllocSpace()->GetRosAlloc() == rosalloc) { |
| 1490 | return space->AsContinuousSpace()->AsRosAllocSpace(); |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | return nullptr; |
| 1495 | } |
| 1496 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1497 | mirror::Object* Heap::AllocateInternalWithGc(Thread* self, AllocatorType allocator, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1498 | size_t alloc_size, size_t* bytes_allocated, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1499 | size_t* usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1500 | size_t* bytes_tl_bulk_allocated, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1501 | mirror::Class** klass) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1502 | bool was_default_allocator = allocator == GetCurrentAllocator(); |
Mathieu Chartier | f4f3843 | 2014-09-03 11:21:08 -0700 | [diff] [blame] | 1503 | // Make sure there is no pending exception since we may need to throw an OOME. |
| 1504 | self->AssertNoPendingException(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1505 | DCHECK(klass != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1506 | StackHandleScope<1> hs(self); |
| 1507 | HandleWrapper<mirror::Class> h(hs.NewHandleWrapper(klass)); |
| 1508 | klass = nullptr; // Invalidate for safety. |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1509 | // The allocation failed. If the GC is running, block until it completes, and then retry the |
| 1510 | // allocation. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 1511 | collector::GcType last_gc = WaitForGcToComplete(kGcCauseForAlloc, self); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1512 | if (last_gc != collector::kGcTypeNone) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1513 | // If we were the default allocator but the allocator changed while we were suspended, |
| 1514 | // abort the allocation. |
| 1515 | if (was_default_allocator && allocator != GetCurrentAllocator()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1516 | return nullptr; |
| 1517 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1518 | // A GC was in progress and we blocked, retry allocation now that memory has been freed. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1519 | mirror::Object* ptr = TryToAllocate<true, false>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1520 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1521 | if (ptr != nullptr) { |
| 1522 | return ptr; |
| 1523 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Mathieu Chartier | 5ae2c93 | 2014-03-28 16:22:20 -0700 | [diff] [blame] | 1526 | collector::GcType tried_type = next_gc_type_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1527 | const bool gc_ran = |
| 1528 | CollectGarbageInternal(tried_type, kGcCauseForAlloc, false) != collector::kGcTypeNone; |
| 1529 | if (was_default_allocator && allocator != GetCurrentAllocator()) { |
| 1530 | return nullptr; |
| 1531 | } |
| 1532 | if (gc_ran) { |
| 1533 | mirror::Object* ptr = TryToAllocate<true, false>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1534 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1535 | if (ptr != nullptr) { |
| 1536 | return ptr; |
Mathieu Chartier | 5ae2c93 | 2014-03-28 16:22:20 -0700 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1540 | // Loop through our different Gc types and try to Gc until we get enough free memory. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1541 | for (collector::GcType gc_type : gc_plan_) { |
Mathieu Chartier | 5ae2c93 | 2014-03-28 16:22:20 -0700 | [diff] [blame] | 1542 | if (gc_type == tried_type) { |
| 1543 | continue; |
| 1544 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1545 | // Attempt to run the collector, if we succeed, re-try the allocation. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1546 | const bool plan_gc_ran = |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1547 | CollectGarbageInternal(gc_type, kGcCauseForAlloc, false) != collector::kGcTypeNone; |
| 1548 | if (was_default_allocator && allocator != GetCurrentAllocator()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1549 | return nullptr; |
| 1550 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1551 | if (plan_gc_ran) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1552 | // Did we free sufficient memory for the allocation to succeed? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1553 | mirror::Object* ptr = TryToAllocate<true, false>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1554 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1555 | if (ptr != nullptr) { |
| 1556 | return ptr; |
| 1557 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1558 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1559 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1560 | // Allocations have failed after GCs; this is an exceptional state. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1561 | // Try harder, growing the heap if necessary. |
| 1562 | mirror::Object* ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1563 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1564 | if (ptr != nullptr) { |
| 1565 | return ptr; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1566 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1567 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 1568 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 1569 | // VM spec requires that all SoftReferences have been collected and cleared before throwing |
| 1570 | // OOME. |
| 1571 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) |
| 1572 | << " allocation"; |
| 1573 | // TODO: Run finalization, but this may cause more allocations to occur. |
| 1574 | // We don't need a WaitForGcToComplete here either. |
| 1575 | DCHECK(!gc_plan_.empty()); |
| 1576 | CollectGarbageInternal(gc_plan_.back(), kGcCauseForAlloc, true); |
| 1577 | if (was_default_allocator && allocator != GetCurrentAllocator()) { |
| 1578 | return nullptr; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1579 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1580 | ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, usable_size, |
| 1581 | bytes_tl_bulk_allocated); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1582 | if (ptr == nullptr) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1583 | const uint64_t current_time = NanoTime(); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1584 | switch (allocator) { |
| 1585 | case kAllocatorTypeRosAlloc: |
| 1586 | // Fall-through. |
| 1587 | case kAllocatorTypeDlMalloc: { |
| 1588 | if (use_homogeneous_space_compaction_for_oom_ && |
| 1589 | current_time - last_time_homogeneous_space_compaction_by_oom_ > |
| 1590 | min_interval_homogeneous_space_compaction_by_oom_) { |
| 1591 | last_time_homogeneous_space_compaction_by_oom_ = current_time; |
| 1592 | HomogeneousSpaceCompactResult result = PerformHomogeneousSpaceCompact(); |
| 1593 | switch (result) { |
| 1594 | case HomogeneousSpaceCompactResult::kSuccess: |
| 1595 | // If the allocation succeeded, we delayed an oom. |
| 1596 | ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1597 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1598 | if (ptr != nullptr) { |
| 1599 | count_delayed_oom_++; |
| 1600 | } |
| 1601 | break; |
| 1602 | case HomogeneousSpaceCompactResult::kErrorReject: |
| 1603 | // Reject due to disabled moving GC. |
| 1604 | break; |
| 1605 | case HomogeneousSpaceCompactResult::kErrorVMShuttingDown: |
| 1606 | // Throw OOM by default. |
| 1607 | break; |
| 1608 | default: { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1609 | UNIMPLEMENTED(FATAL) << "homogeneous space compaction result: " |
| 1610 | << static_cast<size_t>(result); |
| 1611 | UNREACHABLE(); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1612 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1613 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1614 | // Always print that we ran homogeneous space compation since this can cause jank. |
| 1615 | VLOG(heap) << "Ran heap homogeneous space compaction, " |
| 1616 | << " requested defragmentation " |
| 1617 | << count_requested_homogeneous_space_compaction_.LoadSequentiallyConsistent() |
| 1618 | << " performed defragmentation " |
| 1619 | << count_performed_homogeneous_space_compaction_.LoadSequentiallyConsistent() |
| 1620 | << " ignored homogeneous space compaction " |
| 1621 | << count_ignored_homogeneous_space_compaction_.LoadSequentiallyConsistent() |
| 1622 | << " delayed count = " |
| 1623 | << count_delayed_oom_.LoadSequentiallyConsistent(); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1624 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1625 | break; |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1626 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1627 | case kAllocatorTypeNonMoving: { |
| 1628 | // Try to transition the heap if the allocation failure was due to the space being full. |
| 1629 | if (!IsOutOfMemoryOnAllocation<false>(allocator, alloc_size)) { |
| 1630 | // If we aren't out of memory then the OOM was probably from the non moving space being |
| 1631 | // full. Attempt to disable compaction and turn the main space into a non moving space. |
| 1632 | DisableMovingGc(); |
| 1633 | // If we are still a moving GC then something must have caused the transition to fail. |
| 1634 | if (IsMovingGc(collector_type_)) { |
| 1635 | MutexLock mu(self, *gc_complete_lock_); |
| 1636 | // If we couldn't disable moving GC, just throw OOME and return null. |
| 1637 | LOG(WARNING) << "Couldn't disable moving GC with disable GC count " |
| 1638 | << disable_moving_gc_count_; |
| 1639 | } else { |
| 1640 | LOG(WARNING) << "Disabled moving GC due to the non moving space being full"; |
| 1641 | ptr = TryToAllocate<true, true>(self, allocator, alloc_size, bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1642 | usable_size, bytes_tl_bulk_allocated); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | break; |
| 1646 | } |
| 1647 | default: { |
| 1648 | // Do nothing for others allocators. |
| 1649 | } |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | // If the allocation hasn't succeeded by this point, throw an OOM error. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1653 | if (ptr == nullptr) { |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 1654 | ThrowOutOfMemoryError(self, alloc_size, allocator); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1655 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1656 | return ptr; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1659 | void Heap::SetTargetHeapUtilization(float target) { |
| 1660 | DCHECK_GT(target, 0.0f); // asserted in Java code |
| 1661 | DCHECK_LT(target, 1.0f); |
| 1662 | target_utilization_ = target; |
| 1663 | } |
| 1664 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1665 | size_t Heap::GetObjectsAllocated() const { |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 1666 | Thread* self = Thread::Current(); |
| 1667 | ScopedThreadStateChange tsc(self, kWaitingForGetObjectsAllocated); |
| 1668 | auto* tl = Runtime::Current()->GetThreadList(); |
| 1669 | // Need SuspendAll here to prevent lock violation if RosAlloc does it during InspectAll. |
| 1670 | tl->SuspendAll(__FUNCTION__); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1671 | size_t total = 0; |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 1672 | { |
| 1673 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1674 | for (space::AllocSpace* space : alloc_spaces_) { |
| 1675 | total += space->GetObjectsAllocated(); |
| 1676 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1677 | } |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 1678 | tl->ResumeAll(); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1679 | return total; |
| 1680 | } |
| 1681 | |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1682 | uint64_t Heap::GetObjectsAllocatedEver() const { |
Mathieu Chartier | 4edd847 | 2015-06-01 10:47:36 -0700 | [diff] [blame] | 1683 | uint64_t total = GetObjectsFreedEver(); |
| 1684 | // If we are detached, we can't use GetObjectsAllocated since we can't change thread states. |
| 1685 | if (Thread::Current() != nullptr) { |
| 1686 | total += GetObjectsAllocated(); |
| 1687 | } |
| 1688 | return total; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 1691 | uint64_t Heap::GetBytesAllocatedEver() const { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 1692 | return GetBytesFreedEver() + GetBytesAllocated(); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1695 | class InstanceCounter { |
| 1696 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1697 | InstanceCounter(const std::vector<mirror::Class*>& classes, bool use_is_assignable_from, uint64_t* counts) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1698 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1699 | : classes_(classes), use_is_assignable_from_(use_is_assignable_from), counts_(counts) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1700 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1701 | static void Callback(mirror::Object* obj, void* arg) |
| 1702 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1703 | InstanceCounter* instance_counter = reinterpret_cast<InstanceCounter*>(arg); |
| 1704 | mirror::Class* instance_class = obj->GetClass(); |
| 1705 | CHECK(instance_class != nullptr); |
| 1706 | for (size_t i = 0; i < instance_counter->classes_.size(); ++i) { |
Mathieu Chartier | f182085 | 2015-07-10 13:19:51 -0700 | [diff] [blame] | 1707 | mirror::Class* klass = instance_counter->classes_[i]; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1708 | if (instance_counter->use_is_assignable_from_) { |
Mathieu Chartier | f182085 | 2015-07-10 13:19:51 -0700 | [diff] [blame] | 1709 | if (klass != nullptr && klass->IsAssignableFrom(instance_class)) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1710 | ++instance_counter->counts_[i]; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1711 | } |
Mathieu Chartier | f182085 | 2015-07-10 13:19:51 -0700 | [diff] [blame] | 1712 | } else if (instance_class == klass) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1713 | ++instance_counter->counts_[i]; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1718 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1719 | const std::vector<mirror::Class*>& classes_; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1720 | bool use_is_assignable_from_; |
| 1721 | uint64_t* const counts_; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1722 | DISALLOW_COPY_AND_ASSIGN(InstanceCounter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1723 | }; |
| 1724 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1725 | void Heap::CountInstances(const std::vector<mirror::Class*>& classes, bool use_is_assignable_from, |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1726 | uint64_t* counts) { |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 1727 | InstanceCounter counter(classes, use_is_assignable_from, counts); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1728 | VisitObjects(InstanceCounter::Callback, &counter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1731 | class InstanceCollector { |
| 1732 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1733 | InstanceCollector(mirror::Class* c, int32_t max_count, std::vector<mirror::Object*>& instances) |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1734 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1735 | : class_(c), max_count_(max_count), instances_(instances) { |
| 1736 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1737 | static void Callback(mirror::Object* obj, void* arg) |
| 1738 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1739 | DCHECK(arg != nullptr); |
| 1740 | InstanceCollector* instance_collector = reinterpret_cast<InstanceCollector*>(arg); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1741 | if (obj->GetClass() == instance_collector->class_) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1742 | if (instance_collector->max_count_ == 0 || |
| 1743 | instance_collector->instances_.size() < instance_collector->max_count_) { |
| 1744 | instance_collector->instances_.push_back(obj); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | private: |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1750 | const mirror::Class* const class_; |
| 1751 | const uint32_t max_count_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1752 | std::vector<mirror::Object*>& instances_; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1753 | DISALLOW_COPY_AND_ASSIGN(InstanceCollector); |
| 1754 | }; |
| 1755 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1756 | void Heap::GetInstances(mirror::Class* c, int32_t max_count, |
| 1757 | std::vector<mirror::Object*>& instances) { |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1758 | InstanceCollector collector(c, max_count, instances); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1759 | VisitObjects(&InstanceCollector::Callback, &collector); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 1760 | } |
| 1761 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1762 | class ReferringObjectsFinder { |
| 1763 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1764 | ReferringObjectsFinder(mirror::Object* object, int32_t max_count, |
| 1765 | std::vector<mirror::Object*>& referring_objects) |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1766 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1767 | : object_(object), max_count_(max_count), referring_objects_(referring_objects) { |
| 1768 | } |
| 1769 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1770 | static void Callback(mirror::Object* obj, void* arg) |
| 1771 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1772 | reinterpret_cast<ReferringObjectsFinder*>(arg)->operator()(obj); |
| 1773 | } |
| 1774 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1775 | // For bitmap Visit. |
| 1776 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for |
| 1777 | // annotalysis on visitors. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 1778 | void operator()(mirror::Object* o) const NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1779 | o->VisitReferences<true>(*this, VoidFunctor()); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1782 | // For Object::VisitReferences. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1783 | void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) const |
| 1784 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1785 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1786 | if (ref == object_ && (max_count_ == 0 || referring_objects_.size() < max_count_)) { |
| 1787 | referring_objects_.push_back(obj); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | private: |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1792 | const mirror::Object* const object_; |
| 1793 | const uint32_t max_count_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1794 | std::vector<mirror::Object*>& referring_objects_; |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1795 | DISALLOW_COPY_AND_ASSIGN(ReferringObjectsFinder); |
| 1796 | }; |
| 1797 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1798 | void Heap::GetReferringObjects(mirror::Object* o, int32_t max_count, |
| 1799 | std::vector<mirror::Object*>& referring_objects) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1800 | ReferringObjectsFinder finder(o, max_count, referring_objects); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 1801 | VisitObjects(&ReferringObjectsFinder::Callback, &finder); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1804 | void Heap::CollectGarbage(bool clear_soft_references) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1805 | // Even if we waited for a GC we still need to do another GC since weaks allocated during the |
| 1806 | // last GC will not have necessarily been cleared. |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 1807 | CollectGarbageInternal(gc_plan_.back(), kGcCauseExplicit, clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1810 | HomogeneousSpaceCompactResult Heap::PerformHomogeneousSpaceCompact() { |
| 1811 | Thread* self = Thread::Current(); |
| 1812 | // Inc requested homogeneous space compaction. |
| 1813 | count_requested_homogeneous_space_compaction_++; |
| 1814 | // Store performed homogeneous space compaction at a new request arrival. |
| 1815 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
| 1816 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
| 1817 | Locks::mutator_lock_->AssertNotHeld(self); |
| 1818 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1819 | ScopedThreadStateChange tsc2(self, kWaitingForGcToComplete); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1820 | MutexLock mu(self, *gc_complete_lock_); |
| 1821 | // Ensure there is only one GC at a time. |
| 1822 | WaitForGcToCompleteLocked(kGcCauseHomogeneousSpaceCompact, self); |
| 1823 | // Homogeneous space compaction is a copying transition, can't run it if the moving GC disable count |
| 1824 | // is non zero. |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1825 | // If the collector type changed to something which doesn't benefit from homogeneous space compaction, |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1826 | // exit. |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 1827 | if (disable_moving_gc_count_ != 0 || IsMovingGc(collector_type_) || |
| 1828 | !main_space_->CanMoveObjects()) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1829 | return HomogeneousSpaceCompactResult::kErrorReject; |
| 1830 | } |
| 1831 | collector_type_running_ = kCollectorTypeHomogeneousSpaceCompact; |
| 1832 | } |
| 1833 | if (Runtime::Current()->IsShuttingDown(self)) { |
| 1834 | // Don't allow heap transitions to happen if the runtime is shutting down since these can |
| 1835 | // cause objects to get finalized. |
| 1836 | FinishGC(self, collector::kGcTypeNone); |
| 1837 | return HomogeneousSpaceCompactResult::kErrorVMShuttingDown; |
| 1838 | } |
| 1839 | // Suspend all threads. |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 1840 | tl->SuspendAll(__FUNCTION__); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1841 | uint64_t start_time = NanoTime(); |
| 1842 | // Launch compaction. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1843 | space::MallocSpace* to_space = main_space_backup_.release(); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1844 | space::MallocSpace* from_space = main_space_; |
| 1845 | to_space->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 1846 | const uint64_t space_size_before_compaction = from_space->Size(); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1847 | AddSpace(to_space); |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 1848 | // Make sure that we will have enough room to copy. |
| 1849 | CHECK_GE(to_space->GetFootprintLimit(), from_space->GetFootprintLimit()); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 1850 | collector::GarbageCollector* collector = Compact(to_space, from_space, |
| 1851 | kGcCauseHomogeneousSpaceCompact); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1852 | const uint64_t space_size_after_compaction = to_space->Size(); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1853 | main_space_ = to_space; |
| 1854 | main_space_backup_.reset(from_space); |
| 1855 | RemoveSpace(from_space); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1856 | SetSpaceAsDefault(main_space_); // Set as default to reset the proper dlmalloc space. |
| 1857 | // Update performed homogeneous space compaction count. |
| 1858 | count_performed_homogeneous_space_compaction_++; |
| 1859 | // Print statics log and resume all threads. |
| 1860 | uint64_t duration = NanoTime() - start_time; |
Mathieu Chartier | 98172a6 | 2014-09-02 12:33:25 -0700 | [diff] [blame] | 1861 | VLOG(heap) << "Heap homogeneous space compaction took " << PrettyDuration(duration) << " size: " |
| 1862 | << PrettySize(space_size_before_compaction) << " -> " |
| 1863 | << PrettySize(space_size_after_compaction) << " compact-ratio: " |
| 1864 | << std::fixed << static_cast<double>(space_size_after_compaction) / |
| 1865 | static_cast<double>(space_size_before_compaction); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1866 | tl->ResumeAll(); |
| 1867 | // Finish GC. |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 1868 | reference_processor_->EnqueueClearedReferences(self); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1869 | GrowForUtilization(semi_space_collector_); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 1870 | LogGC(kGcCauseHomogeneousSpaceCompact, collector); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 1871 | FinishGC(self, collector::kGcTypeFull); |
| 1872 | return HomogeneousSpaceCompactResult::kSuccess; |
| 1873 | } |
| 1874 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1875 | void Heap::TransitionCollector(CollectorType collector_type) { |
| 1876 | if (collector_type == collector_type_) { |
| 1877 | return; |
| 1878 | } |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1879 | VLOG(heap) << "TransitionCollector: " << static_cast<int>(collector_type_) |
| 1880 | << " -> " << static_cast<int>(collector_type); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1881 | uint64_t start_time = NanoTime(); |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 1882 | uint32_t before_allocated = num_bytes_allocated_.LoadSequentiallyConsistent(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1883 | Runtime* const runtime = Runtime::Current(); |
| 1884 | ThreadList* const tl = runtime->GetThreadList(); |
| 1885 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1886 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
| 1887 | Locks::mutator_lock_->AssertNotHeld(self); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 1888 | // Busy wait until we can GC (StartGC can fail if we have a non-zero |
| 1889 | // compacting_gc_disable_count_, this should rarely occurs). |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1890 | for (;;) { |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 1891 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1892 | ScopedThreadStateChange tsc2(self, kWaitingForGcToComplete); |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 1893 | MutexLock mu(self, *gc_complete_lock_); |
| 1894 | // Ensure there is only one GC at a time. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 1895 | WaitForGcToCompleteLocked(kGcCauseCollectorTransition, self); |
Mathieu Chartier | e4927f6 | 2014-08-23 13:56:03 -0700 | [diff] [blame] | 1896 | // Currently we only need a heap transition if we switch from a moving collector to a |
| 1897 | // non-moving one, or visa versa. |
| 1898 | const bool copying_transition = IsMovingGc(collector_type_) != IsMovingGc(collector_type); |
Mathieu Chartier | b38d483 | 2014-04-10 10:56:55 -0700 | [diff] [blame] | 1899 | // If someone else beat us to it and changed the collector before we could, exit. |
| 1900 | // This is safe to do before the suspend all since we set the collector_type_running_ before |
| 1901 | // we exit the loop. If another thread attempts to do the heap transition before we exit, |
| 1902 | // then it would get blocked on WaitForGcToCompleteLocked. |
| 1903 | if (collector_type == collector_type_) { |
| 1904 | return; |
| 1905 | } |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 1906 | // GC can be disabled if someone has a used GetPrimitiveArrayCritical but not yet released. |
| 1907 | if (!copying_transition || disable_moving_gc_count_ == 0) { |
| 1908 | // TODO: Not hard code in semi-space collector? |
| 1909 | collector_type_running_ = copying_transition ? kCollectorTypeSS : collector_type; |
| 1910 | break; |
| 1911 | } |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 1912 | } |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 1913 | usleep(1000); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1914 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1915 | if (runtime->IsShuttingDown(self)) { |
Hiroshi Yamauchi | a6a8d14 | 2014-05-12 16:57:33 -0700 | [diff] [blame] | 1916 | // Don't allow heap transitions to happen if the runtime is shutting down since these can |
| 1917 | // cause objects to get finalized. |
| 1918 | FinishGC(self, collector::kGcTypeNone); |
| 1919 | return; |
| 1920 | } |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 1921 | collector::GarbageCollector* collector = nullptr; |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 1922 | tl->SuspendAll(__FUNCTION__); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1923 | switch (collector_type) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1924 | case kCollectorTypeSS: { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1925 | if (!IsMovingGc(collector_type_)) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1926 | // Create the bump pointer space from the backup space. |
| 1927 | CHECK(main_space_backup_ != nullptr); |
| 1928 | std::unique_ptr<MemMap> mem_map(main_space_backup_->ReleaseMemMap()); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1929 | // We are transitioning from non moving GC -> moving GC, since we copied from the bump |
| 1930 | // pointer space last transition it will be protected. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1931 | CHECK(mem_map != nullptr); |
| 1932 | mem_map->Protect(PROT_READ | PROT_WRITE); |
| 1933 | bump_pointer_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space", |
| 1934 | mem_map.release()); |
| 1935 | AddSpace(bump_pointer_space_); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 1936 | collector = Compact(bump_pointer_space_, main_space_, kGcCauseCollectorTransition); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1937 | // Use the now empty main space mem map for the bump pointer temp space. |
| 1938 | mem_map.reset(main_space_->ReleaseMemMap()); |
Mathieu Chartier | 00b5915 | 2014-07-25 10:13:51 -0700 | [diff] [blame] | 1939 | // Unset the pointers just in case. |
| 1940 | if (dlmalloc_space_ == main_space_) { |
| 1941 | dlmalloc_space_ = nullptr; |
| 1942 | } else if (rosalloc_space_ == main_space_) { |
| 1943 | rosalloc_space_ = nullptr; |
| 1944 | } |
Mathieu Chartier | 2796a16 | 2014-07-25 11:50:47 -0700 | [diff] [blame] | 1945 | // Remove the main space so that we don't try to trim it, this doens't work for debug |
| 1946 | // builds since RosAlloc attempts to read the magic number from a protected page. |
| 1947 | RemoveSpace(main_space_); |
Mathieu Chartier | c5a8347 | 2014-07-23 18:45:17 -0700 | [diff] [blame] | 1948 | RemoveRememberedSet(main_space_); |
Mathieu Chartier | 2796a16 | 2014-07-25 11:50:47 -0700 | [diff] [blame] | 1949 | delete main_space_; // Delete the space since it has been removed. |
Mathieu Chartier | c5a8347 | 2014-07-23 18:45:17 -0700 | [diff] [blame] | 1950 | main_space_ = nullptr; |
Mathieu Chartier | 2796a16 | 2014-07-25 11:50:47 -0700 | [diff] [blame] | 1951 | RemoveRememberedSet(main_space_backup_.get()); |
| 1952 | main_space_backup_.reset(nullptr); // Deletes the space. |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1953 | temp_space_ = space::BumpPointerSpace::CreateFromMemMap("Bump pointer space 2", |
| 1954 | mem_map.release()); |
| 1955 | AddSpace(temp_space_); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1956 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | case kCollectorTypeMS: |
| 1960 | // Fall through. |
| 1961 | case kCollectorTypeCMS: { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1962 | if (IsMovingGc(collector_type_)) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1963 | CHECK(temp_space_ != nullptr); |
| 1964 | std::unique_ptr<MemMap> mem_map(temp_space_->ReleaseMemMap()); |
| 1965 | RemoveSpace(temp_space_); |
| 1966 | temp_space_ = nullptr; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 1967 | mem_map->Protect(PROT_READ | PROT_WRITE); |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 1968 | CreateMainMallocSpace(mem_map.get(), kDefaultInitialSize, |
| 1969 | std::min(mem_map->Size(), growth_limit_), mem_map->Size()); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1970 | mem_map.release(); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 1971 | // Compact to the main space from the bump pointer space, don't need to swap semispaces. |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 1972 | AddSpace(main_space_); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 1973 | collector = Compact(main_space_, bump_pointer_space_, kGcCauseCollectorTransition); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1974 | mem_map.reset(bump_pointer_space_->ReleaseMemMap()); |
| 1975 | RemoveSpace(bump_pointer_space_); |
| 1976 | bump_pointer_space_ = nullptr; |
| 1977 | const char* name = kUseRosAlloc ? kRosAllocSpaceName[1] : kDlMallocSpaceName[1]; |
Hiroshi Yamauchi | c1276c8 | 2014-08-07 10:27:17 -0700 | [diff] [blame] | 1978 | // Temporarily unprotect the backup mem map so rosalloc can write the debug magic number. |
| 1979 | if (kIsDebugBuild && kUseRosAlloc) { |
| 1980 | mem_map->Protect(PROT_READ | PROT_WRITE); |
| 1981 | } |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 1982 | main_space_backup_.reset(CreateMallocSpaceFromMemMap( |
| 1983 | mem_map.get(), kDefaultInitialSize, std::min(mem_map->Size(), growth_limit_), |
| 1984 | mem_map->Size(), name, true)); |
Hiroshi Yamauchi | c1276c8 | 2014-08-07 10:27:17 -0700 | [diff] [blame] | 1985 | if (kIsDebugBuild && kUseRosAlloc) { |
| 1986 | mem_map->Protect(PROT_NONE); |
| 1987 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 1988 | mem_map.release(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1989 | } |
| 1990 | break; |
| 1991 | } |
| 1992 | default: { |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 1993 | LOG(FATAL) << "Attempted to transition to invalid collector type " |
| 1994 | << static_cast<size_t>(collector_type); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | ChangeCollector(collector_type); |
| 1999 | tl->ResumeAll(); |
| 2000 | // Can't call into java code with all threads suspended. |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 2001 | reference_processor_->EnqueueClearedReferences(self); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2002 | uint64_t duration = NanoTime() - start_time; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 2003 | GrowForUtilization(semi_space_collector_); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2004 | DCHECK(collector != nullptr); |
| 2005 | LogGC(kGcCauseCollectorTransition, collector); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2006 | FinishGC(self, collector::kGcTypeFull); |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 2007 | int32_t after_allocated = num_bytes_allocated_.LoadSequentiallyConsistent(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2008 | int32_t delta_allocated = before_allocated - after_allocated; |
Mathieu Chartier | 19d46b4 | 2014-06-17 15:04:40 -0700 | [diff] [blame] | 2009 | std::string saved_str; |
| 2010 | if (delta_allocated >= 0) { |
| 2011 | saved_str = " saved at least " + PrettySize(delta_allocated); |
| 2012 | } else { |
| 2013 | saved_str = " expanded " + PrettySize(-delta_allocated); |
| 2014 | } |
Mathieu Chartier | 98172a6 | 2014-09-02 12:33:25 -0700 | [diff] [blame] | 2015 | VLOG(heap) << "Heap transition to " << process_state_ << " took " |
Mathieu Chartier | 19d46b4 | 2014-06-17 15:04:40 -0700 | [diff] [blame] | 2016 | << PrettyDuration(duration) << saved_str; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2017 | } |
| 2018 | |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 2019 | void Heap::ChangeCollector(CollectorType collector_type) { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2020 | // TODO: Only do this with all mutators suspended to avoid races. |
| 2021 | if (collector_type != collector_type_) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2022 | if (collector_type == kCollectorTypeMC) { |
| 2023 | // Don't allow mark compact unless support is compiled in. |
| 2024 | CHECK(kMarkCompactSupport); |
| 2025 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2026 | collector_type_ = collector_type; |
| 2027 | gc_plan_.clear(); |
| 2028 | switch (collector_type_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2029 | case kCollectorTypeCC: { |
| 2030 | gc_plan_.push_back(collector::kGcTypeFull); |
| 2031 | if (use_tlab_) { |
| 2032 | ChangeAllocator(kAllocatorTypeRegionTLAB); |
| 2033 | } else { |
| 2034 | ChangeAllocator(kAllocatorTypeRegion); |
| 2035 | } |
| 2036 | break; |
| 2037 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2038 | case kCollectorTypeMC: // Fall-through. |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 2039 | case kCollectorTypeSS: // Fall-through. |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 2040 | case kCollectorTypeGSS: { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2041 | gc_plan_.push_back(collector::kGcTypeFull); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 2042 | if (use_tlab_) { |
| 2043 | ChangeAllocator(kAllocatorTypeTLAB); |
| 2044 | } else { |
| 2045 | ChangeAllocator(kAllocatorTypeBumpPointer); |
| 2046 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2047 | break; |
| 2048 | } |
| 2049 | case kCollectorTypeMS: { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2050 | gc_plan_.push_back(collector::kGcTypeSticky); |
| 2051 | gc_plan_.push_back(collector::kGcTypePartial); |
| 2052 | gc_plan_.push_back(collector::kGcTypeFull); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2053 | ChangeAllocator(kUseRosAlloc ? kAllocatorTypeRosAlloc : kAllocatorTypeDlMalloc); |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2054 | break; |
| 2055 | } |
| 2056 | case kCollectorTypeCMS: { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2057 | gc_plan_.push_back(collector::kGcTypeSticky); |
| 2058 | gc_plan_.push_back(collector::kGcTypePartial); |
| 2059 | gc_plan_.push_back(collector::kGcTypeFull); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2060 | ChangeAllocator(kUseRosAlloc ? kAllocatorTypeRosAlloc : kAllocatorTypeDlMalloc); |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2061 | break; |
| 2062 | } |
| 2063 | default: { |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 2064 | UNIMPLEMENTED(FATAL); |
| 2065 | UNREACHABLE(); |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2066 | } |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 2067 | } |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 2068 | if (IsGcConcurrent()) { |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2069 | concurrent_start_bytes_ = |
| 2070 | std::max(max_allowed_footprint_, kMinConcurrentRemainingBytes) - kMinConcurrentRemainingBytes; |
| 2071 | } else { |
| 2072 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
Mathieu Chartier | 0de9f73 | 2013-11-22 17:58:48 -0800 | [diff] [blame] | 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2077 | // Special compacting collector which uses sub-optimal bin packing to reduce zygote space size. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2078 | class ZygoteCompactingCollector FINAL : public collector::SemiSpace { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2079 | public: |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2080 | explicit ZygoteCompactingCollector(gc::Heap* heap, |
| 2081 | bool is_running_on_memory_tool) |
| 2082 | : SemiSpace(heap, false, "zygote collector"), |
| 2083 | bin_live_bitmap_(nullptr), |
| 2084 | bin_mark_bitmap_(nullptr), |
| 2085 | is_running_on_memory_tool_(is_running_on_memory_tool) {} |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2086 | |
| 2087 | void BuildBins(space::ContinuousSpace* space) { |
| 2088 | bin_live_bitmap_ = space->GetLiveBitmap(); |
| 2089 | bin_mark_bitmap_ = space->GetMarkBitmap(); |
| 2090 | BinContext context; |
| 2091 | context.prev_ = reinterpret_cast<uintptr_t>(space->Begin()); |
| 2092 | context.collector_ = this; |
| 2093 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 2094 | // Note: This requires traversing the space in increasing order of object addresses. |
| 2095 | bin_live_bitmap_->Walk(Callback, reinterpret_cast<void*>(&context)); |
| 2096 | // Add the last bin which spans after the last object to the end of the space. |
| 2097 | AddBin(reinterpret_cast<uintptr_t>(space->End()) - context.prev_, context.prev_); |
| 2098 | } |
| 2099 | |
| 2100 | private: |
| 2101 | struct BinContext { |
| 2102 | uintptr_t prev_; // The end of the previous object. |
| 2103 | ZygoteCompactingCollector* collector_; |
| 2104 | }; |
| 2105 | // Maps from bin sizes to locations. |
| 2106 | std::multimap<size_t, uintptr_t> bins_; |
| 2107 | // Live bitmap of the space which contains the bins. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2108 | accounting::ContinuousSpaceBitmap* bin_live_bitmap_; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2109 | // Mark bitmap of the space which contains the bins. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2110 | accounting::ContinuousSpaceBitmap* bin_mark_bitmap_; |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2111 | const bool is_running_on_memory_tool_; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2112 | |
| 2113 | static void Callback(mirror::Object* obj, void* arg) |
| 2114 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2115 | DCHECK(arg != nullptr); |
| 2116 | BinContext* context = reinterpret_cast<BinContext*>(arg); |
| 2117 | ZygoteCompactingCollector* collector = context->collector_; |
| 2118 | uintptr_t object_addr = reinterpret_cast<uintptr_t>(obj); |
| 2119 | size_t bin_size = object_addr - context->prev_; |
| 2120 | // Add the bin consisting of the end of the previous object to the start of the current object. |
| 2121 | collector->AddBin(bin_size, context->prev_); |
| 2122 | context->prev_ = object_addr + RoundUp(obj->SizeOf(), kObjectAlignment); |
| 2123 | } |
| 2124 | |
| 2125 | void AddBin(size_t size, uintptr_t position) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2126 | if (is_running_on_memory_tool_) { |
| 2127 | MEMORY_TOOL_MAKE_DEFINED(reinterpret_cast<void*>(position), size); |
| 2128 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2129 | if (size != 0) { |
| 2130 | bins_.insert(std::make_pair(size, position)); |
| 2131 | } |
| 2132 | } |
| 2133 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2134 | virtual bool ShouldSweepSpace(space::ContinuousSpace* space) const { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2135 | // Don't sweep any spaces since we probably blasted the internal accounting of the free list |
| 2136 | // allocator. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2137 | UNUSED(space); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2138 | return false; |
| 2139 | } |
| 2140 | |
| 2141 | virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj) |
| 2142 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2143 | size_t obj_size = obj->SizeOf(); |
| 2144 | size_t alloc_size = RoundUp(obj_size, kObjectAlignment); |
Mathieu Chartier | 5dc08a6 | 2014-01-10 10:10:23 -0800 | [diff] [blame] | 2145 | mirror::Object* forward_address; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2146 | // Find the smallest bin which we can move obj in. |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2147 | auto it = bins_.lower_bound(alloc_size); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2148 | if (it == bins_.end()) { |
| 2149 | // No available space in the bins, place it in the target space instead (grows the zygote |
| 2150 | // space). |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2151 | size_t bytes_allocated, dummy; |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2152 | forward_address = to_space_->Alloc(self_, alloc_size, &bytes_allocated, nullptr, &dummy); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2153 | if (to_space_live_bitmap_ != nullptr) { |
| 2154 | to_space_live_bitmap_->Set(forward_address); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2155 | } else { |
| 2156 | GetHeap()->GetNonMovingSpace()->GetLiveBitmap()->Set(forward_address); |
| 2157 | GetHeap()->GetNonMovingSpace()->GetMarkBitmap()->Set(forward_address); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2158 | } |
| 2159 | } else { |
| 2160 | size_t size = it->first; |
| 2161 | uintptr_t pos = it->second; |
| 2162 | bins_.erase(it); // Erase the old bin which we replace with the new smaller bin. |
| 2163 | forward_address = reinterpret_cast<mirror::Object*>(pos); |
| 2164 | // Set the live and mark bits so that sweeping system weaks works properly. |
| 2165 | bin_live_bitmap_->Set(forward_address); |
| 2166 | bin_mark_bitmap_->Set(forward_address); |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2167 | DCHECK_GE(size, alloc_size); |
| 2168 | // Add a new bin with the remaining space. |
| 2169 | AddBin(size - alloc_size, pos + alloc_size); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2170 | } |
Hiroshi Yamauchi | 8711d1f | 2015-03-13 16:48:55 -0700 | [diff] [blame] | 2171 | // Copy the object over to its new location. Don't use alloc_size to avoid valgrind error. |
| 2172 | memcpy(reinterpret_cast<void*>(forward_address), obj, obj_size); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 2173 | if (kUseBakerOrBrooksReadBarrier) { |
| 2174 | obj->AssertReadBarrierPointer(); |
| 2175 | if (kUseBrooksReadBarrier) { |
| 2176 | DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj); |
| 2177 | forward_address->SetReadBarrierPointer(forward_address); |
| 2178 | } |
| 2179 | forward_address->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 2180 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2181 | return forward_address; |
| 2182 | } |
| 2183 | }; |
| 2184 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2185 | void Heap::UnBindBitmaps() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 2186 | TimingLogger::ScopedTiming t("UnBindBitmaps", GetCurrentGcIteration()->GetTimings()); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2187 | for (const auto& space : GetContinuousSpaces()) { |
| 2188 | if (space->IsContinuousMemMapAllocSpace()) { |
| 2189 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
| 2190 | if (alloc_space->HasBoundBitmaps()) { |
| 2191 | alloc_space->UnBindBitmaps(); |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | } |
| 2196 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2197 | void Heap::PreZygoteFork() { |
Mathieu Chartier | faed995 | 2015-03-31 16:28:53 -0700 | [diff] [blame] | 2198 | if (!HasZygoteSpace()) { |
| 2199 | // We still want to GC in case there is some unreachable non moving objects that could cause a |
| 2200 | // suboptimal bin packing when we compact the zygote space. |
| 2201 | CollectGarbageInternal(collector::kGcTypeFull, kGcCauseBackground, false); |
| 2202 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 2203 | Thread* self = Thread::Current(); |
| 2204 | MutexLock mu(self, zygote_creation_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2205 | // Try to see if we have any Zygote spaces. |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2206 | if (HasZygoteSpace()) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2207 | return; |
| 2208 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 2209 | Runtime::Current()->GetInternTable()->SwapPostZygoteWithPreZygote(); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2210 | Runtime::Current()->GetClassLinker()->MoveClassTableToPreZygote(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2211 | VLOG(heap) << "Starting PreZygoteFork"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2212 | // Trim the pages at the end of the non moving space. |
| 2213 | non_moving_space_->Trim(); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2214 | // The end of the non-moving space may be protected, unprotect it so that we can copy the zygote |
| 2215 | // there. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2216 | non_moving_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 2217 | const bool same_space = non_moving_space_ == main_space_; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2218 | if (kCompactZygote) { |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2219 | // Temporarily disable rosalloc verification because the zygote |
| 2220 | // compaction will mess up the rosalloc internal metadata. |
| 2221 | ScopedDisableRosAllocVerification disable_rosalloc_verif(this); |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2222 | ZygoteCompactingCollector zygote_collector(this, is_running_on_memory_tool_); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2223 | zygote_collector.BuildBins(non_moving_space_); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 2224 | // Create a new bump pointer space which we will compact into. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2225 | space::BumpPointerSpace target_space("zygote bump space", non_moving_space_->End(), |
| 2226 | non_moving_space_->Limit()); |
| 2227 | // Compact the bump pointer space to a new zygote bump pointer space. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2228 | bool reset_main_space = false; |
| 2229 | if (IsMovingGc(collector_type_)) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2230 | if (collector_type_ == kCollectorTypeCC) { |
| 2231 | zygote_collector.SetFromSpace(region_space_); |
| 2232 | } else { |
| 2233 | zygote_collector.SetFromSpace(bump_pointer_space_); |
| 2234 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2235 | } else { |
| 2236 | CHECK(main_space_ != nullptr); |
Hiroshi Yamauchi | d04495e | 2015-03-11 19:09:07 -0700 | [diff] [blame] | 2237 | CHECK_NE(main_space_, non_moving_space_) |
| 2238 | << "Does not make sense to compact within the same space"; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2239 | // Copy from the main space. |
| 2240 | zygote_collector.SetFromSpace(main_space_); |
| 2241 | reset_main_space = true; |
| 2242 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 2243 | zygote_collector.SetToSpace(&target_space); |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 2244 | zygote_collector.SetSwapSemiSpaces(false); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 2245 | zygote_collector.Run(kGcCauseCollectorTransition, false); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2246 | if (reset_main_space) { |
| 2247 | main_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2248 | madvise(main_space_->Begin(), main_space_->Capacity(), MADV_DONTNEED); |
| 2249 | MemMap* mem_map = main_space_->ReleaseMemMap(); |
| 2250 | RemoveSpace(main_space_); |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 2251 | space::Space* old_main_space = main_space_; |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 2252 | CreateMainMallocSpace(mem_map, kDefaultInitialSize, std::min(mem_map->Size(), growth_limit_), |
| 2253 | mem_map->Size()); |
Mathieu Chartier | 96bcd45 | 2014-06-17 09:50:02 -0700 | [diff] [blame] | 2254 | delete old_main_space; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2255 | AddSpace(main_space_); |
| 2256 | } else { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2257 | if (collector_type_ == kCollectorTypeCC) { |
| 2258 | region_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2259 | } else { |
| 2260 | bump_pointer_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2261 | } |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2262 | } |
| 2263 | if (temp_space_ != nullptr) { |
| 2264 | CHECK(temp_space_->IsEmpty()); |
| 2265 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2266 | total_objects_freed_ever_ += GetCurrentGcIteration()->GetFreedObjects(); |
| 2267 | total_bytes_freed_ever_ += GetCurrentGcIteration()->GetFreedBytes(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2268 | // Update the end and write out image. |
| 2269 | non_moving_space_->SetEnd(target_space.End()); |
| 2270 | non_moving_space_->SetLimit(target_space.Limit()); |
Mathieu Chartier | faed995 | 2015-03-31 16:28:53 -0700 | [diff] [blame] | 2271 | VLOG(heap) << "Create zygote space with size=" << non_moving_space_->Size() << " bytes"; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2272 | } |
Mathieu Chartier | 6a7824d | 2014-08-22 14:53:04 -0700 | [diff] [blame] | 2273 | // Change the collector to the post zygote one. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2274 | ChangeCollector(foreground_collector_type_); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2275 | // Save the old space so that we can remove it after we complete creating the zygote space. |
| 2276 | space::MallocSpace* old_alloc_space = non_moving_space_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2277 | // Turn the current alloc space into a zygote space and obtain the new alloc space composed of |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2278 | // the remaining available space. |
| 2279 | // Remove the old space before creating the zygote space since creating the zygote space sets |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2280 | // the old alloc space's bitmaps to null. |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2281 | RemoveSpace(old_alloc_space); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 2282 | if (collector::SemiSpace::kUseRememberedSet) { |
| 2283 | // Sanity bound check. |
| 2284 | FindRememberedSetFromSpace(old_alloc_space)->AssertAllDirtyCardsAreWithinSpace(); |
| 2285 | // Remove the remembered set for the now zygote space (the old |
| 2286 | // non-moving space). Note now that we have compacted objects into |
| 2287 | // the zygote space, the data in the remembered set is no longer |
| 2288 | // needed. The zygote space will instead have a mod-union table |
| 2289 | // from this point on. |
| 2290 | RemoveRememberedSet(old_alloc_space); |
| 2291 | } |
Mathieu Chartier | 7247af5 | 2014-11-19 10:51:42 -0800 | [diff] [blame] | 2292 | // Remaining space becomes the new non moving space. |
| 2293 | zygote_space_ = old_alloc_space->CreateZygoteSpace(kNonMovingSpaceName, low_memory_mode_, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2294 | &non_moving_space_); |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 2295 | CHECK(!non_moving_space_->CanMoveObjects()); |
| 2296 | if (same_space) { |
| 2297 | main_space_ = non_moving_space_; |
| 2298 | SetSpaceAsDefault(main_space_); |
| 2299 | } |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 2300 | delete old_alloc_space; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2301 | CHECK(HasZygoteSpace()) << "Failed creating zygote space"; |
| 2302 | AddSpace(zygote_space_); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2303 | non_moving_space_->SetFootprintLimit(non_moving_space_->Capacity()); |
| 2304 | AddSpace(non_moving_space_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2305 | // Create the zygote space mod union table. |
| 2306 | accounting::ModUnionTable* mod_union_table = |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2307 | new accounting::ModUnionTableCardCache("zygote space mod-union table", this, |
| 2308 | zygote_space_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2309 | CHECK(mod_union_table != nullptr) << "Failed to create zygote space mod-union table"; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2310 | // Set all the cards in the mod-union table since we don't know which objects contain references |
| 2311 | // to large objects. |
| 2312 | mod_union_table->SetCards(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2313 | AddModUnionTable(mod_union_table); |
Mathieu Chartier | f6c2a27 | 2015-06-03 17:32:42 -0700 | [diff] [blame] | 2314 | large_object_space_->SetAllLargeObjectsAsZygoteObjects(self); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 2315 | if (collector::SemiSpace::kUseRememberedSet) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 2316 | // Add a new remembered set for the post-zygote non-moving space. |
| 2317 | accounting::RememberedSet* post_zygote_non_moving_space_rem_set = |
| 2318 | new accounting::RememberedSet("Post-zygote non-moving space remembered set", this, |
| 2319 | non_moving_space_); |
| 2320 | CHECK(post_zygote_non_moving_space_rem_set != nullptr) |
| 2321 | << "Failed to create post-zygote non-moving space remembered set"; |
| 2322 | AddRememberedSet(post_zygote_non_moving_space_rem_set); |
| 2323 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2326 | void Heap::FlushAllocStack() { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2327 | MarkAllocStackAsLive(allocation_stack_.get()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2328 | allocation_stack_->Reset(); |
| 2329 | } |
| 2330 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2331 | void Heap::MarkAllocStack(accounting::ContinuousSpaceBitmap* bitmap1, |
| 2332 | accounting::ContinuousSpaceBitmap* bitmap2, |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 2333 | accounting::LargeObjectBitmap* large_objects, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2334 | accounting::ObjectStack* stack) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2335 | DCHECK(bitmap1 != nullptr); |
| 2336 | DCHECK(bitmap2 != nullptr); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 2337 | const auto* limit = stack->End(); |
| 2338 | for (auto* it = stack->Begin(); it != limit; ++it) { |
| 2339 | const mirror::Object* obj = it->AsMirrorPtr(); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2340 | if (!kUseThreadLocalAllocationStack || obj != nullptr) { |
| 2341 | if (bitmap1->HasAddress(obj)) { |
| 2342 | bitmap1->Set(obj); |
| 2343 | } else if (bitmap2->HasAddress(obj)) { |
| 2344 | bitmap2->Set(obj); |
| 2345 | } else { |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 2346 | DCHECK(large_objects != nullptr); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2347 | large_objects->Set(obj); |
| 2348 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 2349 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2353 | void Heap::SwapSemiSpaces() { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2354 | CHECK(bump_pointer_space_ != nullptr); |
| 2355 | CHECK(temp_space_ != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2356 | std::swap(bump_pointer_space_, temp_space_); |
| 2357 | } |
| 2358 | |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2359 | collector::GarbageCollector* Heap::Compact(space::ContinuousMemMapAllocSpace* target_space, |
| 2360 | space::ContinuousMemMapAllocSpace* source_space, |
| 2361 | GcCause gc_cause) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2362 | CHECK(kMovingCollector); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2363 | if (target_space != source_space) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 2364 | // Don't swap spaces since this isn't a typical semi space collection. |
| 2365 | semi_space_collector_->SetSwapSemiSpaces(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2366 | semi_space_collector_->SetFromSpace(source_space); |
| 2367 | semi_space_collector_->SetToSpace(target_space); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 2368 | semi_space_collector_->Run(gc_cause, false); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2369 | return semi_space_collector_; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2370 | } else { |
| 2371 | CHECK(target_space->IsBumpPointerSpace()) |
| 2372 | << "In-place compaction is only supported for bump pointer spaces"; |
| 2373 | mark_compact_collector_->SetSpace(target_space->AsBumpPointerSpace()); |
| 2374 | mark_compact_collector_->Run(kGcCauseCollectorTransition, false); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2375 | return mark_compact_collector_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2376 | } |
| 2377 | } |
Anwar Ghuloum | 67f9941 | 2013-08-12 14:19:48 -0700 | [diff] [blame] | 2378 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2379 | collector::GcType Heap::CollectGarbageInternal(collector::GcType gc_type, GcCause gc_cause, |
| 2380 | bool clear_soft_references) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 2381 | Thread* self = Thread::Current(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2382 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2383 | // If the heap can't run the GC, silently fail and return that no GC was run. |
| 2384 | switch (gc_type) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2385 | case collector::kGcTypePartial: { |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 2386 | if (!HasZygoteSpace()) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2387 | return collector::kGcTypeNone; |
| 2388 | } |
| 2389 | break; |
| 2390 | } |
| 2391 | default: { |
| 2392 | // Other GC types don't have any special cases which makes them not runnable. The main case |
| 2393 | // here is full GC. |
| 2394 | } |
| 2395 | } |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 2396 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 2397 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 2398 | if (self->IsHandlingStackOverflow()) { |
Mathieu Chartier | 50c138f | 2015-01-07 16:00:03 -0800 | [diff] [blame] | 2399 | // If we are throwing a stack overflow error we probably don't have enough remaining stack |
| 2400 | // space to run the GC. |
| 2401 | return collector::kGcTypeNone; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 2402 | } |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2403 | bool compacting_gc; |
| 2404 | { |
| 2405 | gc_complete_lock_->AssertNotHeld(self); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2406 | ScopedThreadStateChange tsc2(self, kWaitingForGcToComplete); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2407 | MutexLock mu(self, *gc_complete_lock_); |
| 2408 | // Ensure there is only one GC at a time. |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 2409 | WaitForGcToCompleteLocked(gc_cause, self); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 2410 | compacting_gc = IsMovingGc(collector_type_); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2411 | // GC can be disabled if someone has a used GetPrimitiveArrayCritical. |
| 2412 | if (compacting_gc && disable_moving_gc_count_ != 0) { |
| 2413 | LOG(WARNING) << "Skipping GC due to disable moving GC count " << disable_moving_gc_count_; |
| 2414 | return collector::kGcTypeNone; |
| 2415 | } |
| 2416 | collector_type_running_ = collector_type_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2417 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2418 | if (gc_cause == kGcCauseForAlloc && runtime->HasStatsEnabled()) { |
| 2419 | ++runtime->GetStats()->gc_for_alloc_count; |
| 2420 | ++self->GetStats()->gc_for_alloc_count; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 2421 | } |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 2422 | const uint64_t bytes_allocated_before_gc = GetBytesAllocated(); |
| 2423 | // Approximate heap size. |
| 2424 | ATRACE_INT("Heap size (KB)", bytes_allocated_before_gc / KB); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 2425 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2426 | DCHECK_LT(gc_type, collector::kGcTypeMax); |
| 2427 | DCHECK_NE(gc_type, collector::kGcTypeNone); |
Anwar Ghuloum | 67f9941 | 2013-08-12 14:19:48 -0700 | [diff] [blame] | 2428 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2429 | collector::GarbageCollector* collector = nullptr; |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 2430 | // TODO: Clean this up. |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 2431 | if (compacting_gc) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 2432 | DCHECK(current_allocator_ == kAllocatorTypeBumpPointer || |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2433 | current_allocator_ == kAllocatorTypeTLAB || |
| 2434 | current_allocator_ == kAllocatorTypeRegion || |
| 2435 | current_allocator_ == kAllocatorTypeRegionTLAB); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2436 | switch (collector_type_) { |
| 2437 | case kCollectorTypeSS: |
| 2438 | // Fall-through. |
| 2439 | case kCollectorTypeGSS: |
| 2440 | semi_space_collector_->SetFromSpace(bump_pointer_space_); |
| 2441 | semi_space_collector_->SetToSpace(temp_space_); |
| 2442 | semi_space_collector_->SetSwapSemiSpaces(true); |
| 2443 | collector = semi_space_collector_; |
| 2444 | break; |
| 2445 | case kCollectorTypeCC: |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2446 | concurrent_copying_collector_->SetRegionSpace(region_space_); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2447 | collector = concurrent_copying_collector_; |
| 2448 | break; |
| 2449 | case kCollectorTypeMC: |
| 2450 | mark_compact_collector_->SetSpace(bump_pointer_space_); |
| 2451 | collector = mark_compact_collector_; |
| 2452 | break; |
| 2453 | default: |
| 2454 | LOG(FATAL) << "Invalid collector type " << static_cast<size_t>(collector_type_); |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 2455 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2456 | if (collector != mark_compact_collector_ && collector != concurrent_copying_collector_) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2457 | temp_space_->GetMemMap()->Protect(PROT_READ | PROT_WRITE); |
| 2458 | CHECK(temp_space_->IsEmpty()); |
| 2459 | } |
| 2460 | gc_type = collector::kGcTypeFull; // TODO: Not hard code this in. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2461 | } else if (current_allocator_ == kAllocatorTypeRosAlloc || |
| 2462 | current_allocator_ == kAllocatorTypeDlMalloc) { |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 2463 | collector = FindCollectorByGcType(gc_type); |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 2464 | } else { |
| 2465 | LOG(FATAL) << "Invalid current allocator " << current_allocator_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2466 | } |
Mathieu Chartier | 08cef22 | 2014-10-22 17:18:34 -0700 | [diff] [blame] | 2467 | if (IsGcConcurrent()) { |
| 2468 | // Disable concurrent GC check so that we don't have spammy JNI requests. |
| 2469 | // This gets recalculated in GrowForUtilization. It is important that it is disabled / |
| 2470 | // calculated in the same thread so that there aren't any races that can cause it to become |
| 2471 | // permanantly disabled. b/17942071 |
| 2472 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 2473 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 2474 | CHECK(collector != nullptr) |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 2475 | << "Could not find garbage collector with collector_type=" |
| 2476 | << static_cast<size_t>(collector_type_) << " and gc_type=" << gc_type; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 2477 | collector->Run(gc_cause, clear_soft_references || runtime->IsZygote()); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2478 | total_objects_freed_ever_ += GetCurrentGcIteration()->GetFreedObjects(); |
| 2479 | total_bytes_freed_ever_ += GetCurrentGcIteration()->GetFreedBytes(); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 2480 | RequestTrim(self); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 2481 | // Enqueue cleared references. |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 2482 | reference_processor_->EnqueueClearedReferences(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2483 | // Grow the heap so that we know when to perform the next GC. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 2484 | GrowForUtilization(collector, bytes_allocated_before_gc); |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2485 | LogGC(gc_cause, collector); |
| 2486 | FinishGC(self, gc_type); |
| 2487 | // Inform DDMS that a GC completed. |
| 2488 | Dbg::GcDidFinish(); |
| 2489 | return gc_type; |
| 2490 | } |
| 2491 | |
| 2492 | void Heap::LogGC(GcCause gc_cause, collector::GarbageCollector* collector) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2493 | const size_t duration = GetCurrentGcIteration()->GetDurationNs(); |
| 2494 | const std::vector<uint64_t>& pause_times = GetCurrentGcIteration()->GetPauseTimes(); |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2495 | // Print the GC if it is an explicit GC (e.g. Runtime.gc()) or a slow GC |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 2496 | // (mutator time blocked >= long_pause_log_threshold_). |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2497 | bool log_gc = gc_cause == kGcCauseExplicit; |
| 2498 | if (!log_gc && CareAboutPauseTimes()) { |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 2499 | // GC for alloc pauses the allocating thread, so consider it as a pause. |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2500 | log_gc = duration > long_gc_log_threshold_ || |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2501 | (gc_cause == kGcCauseForAlloc && duration > long_pause_log_threshold_); |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2502 | for (uint64_t pause : pause_times) { |
| 2503 | log_gc = log_gc || pause >= long_pause_log_threshold_; |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 2504 | } |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2505 | } |
| 2506 | if (log_gc) { |
| 2507 | const size_t percent_free = GetPercentFree(); |
| 2508 | const size_t current_heap_size = GetBytesAllocated(); |
| 2509 | const size_t total_memory = GetTotalMemory(); |
| 2510 | std::ostringstream pause_string; |
| 2511 | for (size_t i = 0; i < pause_times.size(); ++i) { |
Hiroshi Yamauchi | e4d9987 | 2015-02-26 12:53:45 -0800 | [diff] [blame] | 2512 | pause_string << PrettyDuration((pause_times[i] / 1000) * 1000) |
| 2513 | << ((i != pause_times.size() - 1) ? "," : ""); |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 2514 | } |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2515 | LOG(INFO) << gc_cause << " " << collector->GetName() |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 2516 | << " GC freed " << current_gc_iteration_.GetFreedObjects() << "(" |
| 2517 | << PrettySize(current_gc_iteration_.GetFreedBytes()) << ") AllocSpace objects, " |
| 2518 | << current_gc_iteration_.GetFreedLargeObjects() << "(" |
| 2519 | << PrettySize(current_gc_iteration_.GetFreedLargeObjectBytes()) << ") LOS objects, " |
Mathieu Chartier | 62ab87b | 2014-04-28 12:22:07 -0700 | [diff] [blame] | 2520 | << percent_free << "% free, " << PrettySize(current_heap_size) << "/" |
| 2521 | << PrettySize(total_memory) << ", " << "paused " << pause_string.str() |
| 2522 | << " total " << PrettyDuration((duration / 1000) * 1000); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 2523 | VLOG(heap) << Dumpable<TimingLogger>(*current_gc_iteration_.GetTimings()); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 2524 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2525 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 2526 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2527 | void Heap::FinishGC(Thread* self, collector::GcType gc_type) { |
| 2528 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2529 | collector_type_running_ = kCollectorTypeNone; |
| 2530 | if (gc_type != collector::kGcTypeNone) { |
| 2531 | last_gc_type_ = gc_type; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 2532 | |
| 2533 | // Update stats. |
| 2534 | ++gc_count_last_window_; |
| 2535 | if (running_collection_is_blocking_) { |
| 2536 | // If the currently running collection was a blocking one, |
| 2537 | // increment the counters and reset the flag. |
| 2538 | ++blocking_gc_count_; |
| 2539 | blocking_gc_time_ += GetCurrentGcIteration()->GetDurationNs(); |
| 2540 | ++blocking_gc_count_last_window_; |
| 2541 | } |
| 2542 | // Update the gc count rate histograms if due. |
| 2543 | UpdateGcCountRateHistograms(); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 2544 | } |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 2545 | // Reset. |
| 2546 | running_collection_is_blocking_ = false; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2547 | // Wake anyone who may have been waiting for the GC to complete. |
| 2548 | gc_complete_cond_->Broadcast(self); |
| 2549 | } |
| 2550 | |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 2551 | void Heap::UpdateGcCountRateHistograms() { |
| 2552 | // Invariant: if the time since the last update includes more than |
| 2553 | // one windows, all the GC runs (if > 0) must have happened in first |
| 2554 | // window because otherwise the update must have already taken place |
| 2555 | // at an earlier GC run. So, we report the non-first windows with |
| 2556 | // zero counts to the histograms. |
| 2557 | DCHECK_EQ(last_update_time_gc_count_rate_histograms_ % kGcCountRateHistogramWindowDuration, 0U); |
| 2558 | uint64_t now = NanoTime(); |
| 2559 | DCHECK_GE(now, last_update_time_gc_count_rate_histograms_); |
| 2560 | uint64_t time_since_last_update = now - last_update_time_gc_count_rate_histograms_; |
| 2561 | uint64_t num_of_windows = time_since_last_update / kGcCountRateHistogramWindowDuration; |
| 2562 | if (time_since_last_update >= kGcCountRateHistogramWindowDuration) { |
| 2563 | // Record the first window. |
| 2564 | gc_count_rate_histogram_.AddValue(gc_count_last_window_ - 1); // Exclude the current run. |
| 2565 | blocking_gc_count_rate_histogram_.AddValue(running_collection_is_blocking_ ? |
| 2566 | blocking_gc_count_last_window_ - 1 : blocking_gc_count_last_window_); |
| 2567 | // Record the other windows (with zero counts). |
| 2568 | for (uint64_t i = 0; i < num_of_windows - 1; ++i) { |
| 2569 | gc_count_rate_histogram_.AddValue(0); |
| 2570 | blocking_gc_count_rate_histogram_.AddValue(0); |
| 2571 | } |
| 2572 | // Update the last update time and reset the counters. |
| 2573 | last_update_time_gc_count_rate_histograms_ = |
| 2574 | (now / kGcCountRateHistogramWindowDuration) * kGcCountRateHistogramWindowDuration; |
| 2575 | gc_count_last_window_ = 1; // Include the current run. |
| 2576 | blocking_gc_count_last_window_ = running_collection_is_blocking_ ? 1 : 0; |
| 2577 | } |
| 2578 | DCHECK_EQ(last_update_time_gc_count_rate_histograms_ % kGcCountRateHistogramWindowDuration, 0U); |
| 2579 | } |
| 2580 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2581 | class RootMatchesObjectVisitor : public SingleRootVisitor { |
| 2582 | public: |
| 2583 | explicit RootMatchesObjectVisitor(const mirror::Object* obj) : obj_(obj) { } |
| 2584 | |
| 2585 | void VisitRoot(mirror::Object* root, const RootInfo& info) |
| 2586 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2587 | if (root == obj_) { |
| 2588 | LOG(INFO) << "Object " << obj_ << " is a root " << info.ToString(); |
| 2589 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2590 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2591 | |
| 2592 | private: |
| 2593 | const mirror::Object* const obj_; |
| 2594 | }; |
| 2595 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2596 | |
| 2597 | class ScanVisitor { |
| 2598 | public: |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2599 | void operator()(const mirror::Object* obj) const { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2600 | LOG(ERROR) << "Would have rescanned object " << obj; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2601 | } |
| 2602 | }; |
| 2603 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2604 | // Verify a reference from an object. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2605 | class VerifyReferenceVisitor : public SingleRootVisitor { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2606 | public: |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2607 | explicit VerifyReferenceVisitor(Heap* heap, Atomic<size_t>* fail_count, bool verify_referent) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2608 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2609 | : heap_(heap), fail_count_(fail_count), verify_referent_(verify_referent) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2610 | |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2611 | size_t GetFailureCount() const { |
Mathieu Chartier | e9e55ac | 2014-05-21 17:48:25 -0700 | [diff] [blame] | 2612 | return fail_count_->LoadSequentiallyConsistent(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2615 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
| 2616 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2617 | UNUSED(klass); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 2618 | if (verify_referent_) { |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2619 | VerifyReference(ref, ref->GetReferent(), mirror::Reference::ReferentOffset()); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 2620 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2621 | } |
| 2622 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 2623 | void operator()(mirror::Object* obj, MemberOffset offset, bool /*is_static*/) const |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2624 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2625 | VerifyReference(obj, obj->GetFieldObject<mirror::Object>(offset), offset); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2626 | } |
| 2627 | |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2628 | bool IsLive(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
| 2629 | return heap_->IsLiveObjectLocked(obj, true, false, true); |
| 2630 | } |
| 2631 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2632 | void VisitRoot(mirror::Object* root, const RootInfo& root_info) OVERRIDE |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 2633 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2634 | if (root == nullptr) { |
| 2635 | LOG(ERROR) << "Root is null with info " << root_info.GetType(); |
| 2636 | } else if (!VerifyReference(nullptr, root, MemberOffset(0))) { |
| 2637 | LOG(ERROR) << "Root " << root << " is dead with type " << PrettyTypeOf(root) |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 2638 | << " thread_id= " << root_info.GetThreadId() << " root_type= " << root_info.GetType(); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | private: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2643 | // TODO: Fix the no thread safety analysis. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2644 | // Returns false on failure. |
| 2645 | bool VerifyReference(mirror::Object* obj, mirror::Object* ref, MemberOffset offset) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2646 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2647 | if (ref == nullptr || IsLive(ref)) { |
| 2648 | // Verify that the reference is live. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2649 | return true; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2650 | } |
Mathieu Chartier | e9e55ac | 2014-05-21 17:48:25 -0700 | [diff] [blame] | 2651 | if (fail_count_->FetchAndAddSequentiallyConsistent(1) == 0) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2652 | // Print message on only on first failure to prevent spam. |
| 2653 | LOG(ERROR) << "!!!!!!!!!!!!!!Heap corruption detected!!!!!!!!!!!!!!!!!!!"; |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2654 | } |
| 2655 | if (obj != nullptr) { |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2656 | // Only do this part for non roots. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2657 | accounting::CardTable* card_table = heap_->GetCardTable(); |
| 2658 | accounting::ObjectStack* alloc_stack = heap_->allocation_stack_.get(); |
| 2659 | accounting::ObjectStack* live_stack = heap_->live_stack_.get(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2660 | uint8_t* card_addr = card_table->CardFromAddr(obj); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2661 | LOG(ERROR) << "Object " << obj << " references dead object " << ref << " at offset " |
| 2662 | << offset << "\n card value = " << static_cast<int>(*card_addr); |
| 2663 | if (heap_->IsValidObjectAddress(obj->GetClass())) { |
| 2664 | LOG(ERROR) << "Obj type " << PrettyTypeOf(obj); |
| 2665 | } else { |
| 2666 | LOG(ERROR) << "Object " << obj << " class(" << obj->GetClass() << ") not a heap address"; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2667 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2668 | |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 2669 | // Attempt to find the class inside of the recently freed objects. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2670 | space::ContinuousSpace* ref_space = heap_->FindContinuousSpaceFromObject(ref, true); |
| 2671 | if (ref_space != nullptr && ref_space->IsMallocSpace()) { |
| 2672 | space::MallocSpace* space = ref_space->AsMallocSpace(); |
| 2673 | mirror::Class* ref_class = space->FindRecentFreedObject(ref); |
| 2674 | if (ref_class != nullptr) { |
| 2675 | LOG(ERROR) << "Reference " << ref << " found as a recently freed object with class " |
| 2676 | << PrettyClass(ref_class); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2677 | } else { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2678 | LOG(ERROR) << "Reference " << ref << " not found as a recently freed object"; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2679 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2680 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2681 | |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2682 | if (ref->GetClass() != nullptr && heap_->IsValidObjectAddress(ref->GetClass()) && |
| 2683 | ref->GetClass()->IsClass()) { |
| 2684 | LOG(ERROR) << "Ref type " << PrettyTypeOf(ref); |
| 2685 | } else { |
| 2686 | LOG(ERROR) << "Ref " << ref << " class(" << ref->GetClass() |
| 2687 | << ") is not a valid heap address"; |
| 2688 | } |
| 2689 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2690 | card_table->CheckAddrIsInCardTable(reinterpret_cast<const uint8_t*>(obj)); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2691 | void* cover_begin = card_table->AddrFromCard(card_addr); |
| 2692 | void* cover_end = reinterpret_cast<void*>(reinterpret_cast<size_t>(cover_begin) + |
| 2693 | accounting::CardTable::kCardSize); |
| 2694 | LOG(ERROR) << "Card " << reinterpret_cast<void*>(card_addr) << " covers " << cover_begin |
| 2695 | << "-" << cover_end; |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 2696 | accounting::ContinuousSpaceBitmap* bitmap = |
| 2697 | heap_->GetLiveBitmap()->GetContinuousSpaceBitmap(obj); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2698 | |
| 2699 | if (bitmap == nullptr) { |
| 2700 | LOG(ERROR) << "Object " << obj << " has no bitmap"; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 2701 | if (!VerifyClassClass(obj->GetClass())) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2702 | LOG(ERROR) << "Object " << obj << " failed class verification!"; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2703 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2704 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2705 | // Print out how the object is live. |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2706 | if (bitmap->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2707 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 2708 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2709 | if (alloc_stack->Contains(const_cast<mirror::Object*>(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2710 | LOG(ERROR) << "Object " << obj << " found in allocation stack"; |
| 2711 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2712 | if (live_stack->Contains(const_cast<mirror::Object*>(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2713 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 2714 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2715 | if (alloc_stack->Contains(const_cast<mirror::Object*>(ref))) { |
| 2716 | LOG(ERROR) << "Ref " << ref << " found in allocation stack"; |
| 2717 | } |
| 2718 | if (live_stack->Contains(const_cast<mirror::Object*>(ref))) { |
| 2719 | LOG(ERROR) << "Ref " << ref << " found in live stack"; |
| 2720 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2721 | // Attempt to see if the card table missed the reference. |
| 2722 | ScanVisitor scan_visitor; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2723 | uint8_t* byte_cover_begin = reinterpret_cast<uint8_t*>(card_table->AddrFromCard(card_addr)); |
Lei Li | 727b294 | 2015-01-15 11:26:34 +0800 | [diff] [blame] | 2724 | card_table->Scan<false>(bitmap, byte_cover_begin, |
| 2725 | byte_cover_begin + accounting::CardTable::kCardSize, scan_visitor); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2726 | } |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2727 | |
| 2728 | // Search to see if any of the roots reference our object. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2729 | RootMatchesObjectVisitor visitor1(obj); |
| 2730 | Runtime::Current()->VisitRoots(&visitor1); |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2731 | // Search to see if any of the roots reference our reference. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2732 | RootMatchesObjectVisitor visitor2(ref); |
| 2733 | Runtime::Current()->VisitRoots(&visitor2); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2734 | } |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2735 | return false; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2736 | } |
| 2737 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2738 | Heap* const heap_; |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2739 | Atomic<size_t>* const fail_count_; |
| 2740 | const bool verify_referent_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2741 | }; |
| 2742 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2743 | // Verify all references within an object, for use with HeapBitmap::Visit. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2744 | class VerifyObjectVisitor { |
| 2745 | public: |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2746 | explicit VerifyObjectVisitor(Heap* heap, Atomic<size_t>* fail_count, bool verify_referent) |
| 2747 | : heap_(heap), fail_count_(fail_count), verify_referent_(verify_referent) { |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 2748 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2749 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2750 | void operator()(mirror::Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2751 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2752 | // Note: we are verifying the references in obj but not obj itself, this is because obj must |
| 2753 | // be live or else how did we find it in the live bitmap? |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2754 | VerifyReferenceVisitor visitor(heap_, fail_count_, verify_referent_); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2755 | // The class doesn't count as a reference but we should verify it anyways. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2756 | obj->VisitReferences<true>(visitor, visitor); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2759 | static void VisitCallback(mirror::Object* obj, void* arg) |
| 2760 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 2761 | VerifyObjectVisitor* visitor = reinterpret_cast<VerifyObjectVisitor*>(arg); |
| 2762 | visitor->operator()(obj); |
| 2763 | } |
| 2764 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2765 | void VerifyRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 2766 | LOCKS_EXCLUDED(Locks::heap_bitmap_lock_) { |
| 2767 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 2768 | VerifyReferenceVisitor visitor(heap_, fail_count_, verify_referent_); |
| 2769 | Runtime::Current()->VisitRoots(&visitor); |
| 2770 | } |
| 2771 | |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2772 | size_t GetFailureCount() const { |
Mathieu Chartier | e9e55ac | 2014-05-21 17:48:25 -0700 | [diff] [blame] | 2773 | return fail_count_->LoadSequentiallyConsistent(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2777 | Heap* const heap_; |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2778 | Atomic<size_t>* const fail_count_; |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 2779 | const bool verify_referent_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2780 | }; |
| 2781 | |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 2782 | void Heap::PushOnAllocationStackWithInternalGC(Thread* self, mirror::Object** obj) { |
| 2783 | // Slow path, the allocation stack push back must have already failed. |
| 2784 | DCHECK(!allocation_stack_->AtomicPushBack(*obj)); |
| 2785 | do { |
| 2786 | // TODO: Add handle VerifyObject. |
| 2787 | StackHandleScope<1> hs(self); |
| 2788 | HandleWrapper<mirror::Object> wrapper(hs.NewHandleWrapper(obj)); |
| 2789 | // Push our object into the reserve region of the allocaiton stack. This is only required due |
| 2790 | // to heap verification requiring that roots are live (either in the live bitmap or in the |
| 2791 | // allocation stack). |
| 2792 | CHECK(allocation_stack_->AtomicPushBackIgnoreGrowthLimit(*obj)); |
| 2793 | CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false); |
| 2794 | } while (!allocation_stack_->AtomicPushBack(*obj)); |
| 2795 | } |
| 2796 | |
| 2797 | void Heap::PushOnThreadLocalAllocationStackWithInternalGC(Thread* self, mirror::Object** obj) { |
| 2798 | // Slow path, the allocation stack push back must have already failed. |
| 2799 | DCHECK(!self->PushOnThreadLocalAllocationStack(*obj)); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 2800 | StackReference<mirror::Object>* start_address; |
| 2801 | StackReference<mirror::Object>* end_address; |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 2802 | while (!allocation_stack_->AtomicBumpBack(kThreadLocalAllocationStackSize, &start_address, |
| 2803 | &end_address)) { |
| 2804 | // TODO: Add handle VerifyObject. |
| 2805 | StackHandleScope<1> hs(self); |
| 2806 | HandleWrapper<mirror::Object> wrapper(hs.NewHandleWrapper(obj)); |
| 2807 | // Push our object into the reserve region of the allocaiton stack. This is only required due |
| 2808 | // to heap verification requiring that roots are live (either in the live bitmap or in the |
| 2809 | // allocation stack). |
| 2810 | CHECK(allocation_stack_->AtomicPushBackIgnoreGrowthLimit(*obj)); |
| 2811 | // Push into the reserve allocation stack. |
| 2812 | CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false); |
| 2813 | } |
| 2814 | self->SetThreadLocalAllocationStack(start_address, end_address); |
| 2815 | // Retry on the new thread-local allocation stack. |
| 2816 | CHECK(self->PushOnThreadLocalAllocationStack(*obj)); // Must succeed. |
| 2817 | } |
| 2818 | |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2819 | // Must do this with mutators suspended since we are directly accessing the allocation stacks. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2820 | size_t Heap::VerifyHeapReferences(bool verify_referents) { |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 2821 | Thread* self = Thread::Current(); |
| 2822 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2823 | // Lets sort our allocation stacks so that we can efficiently binary search them. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2824 | allocation_stack_->Sort(); |
| 2825 | live_stack_->Sort(); |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 2826 | // Since we sorted the allocation stack content, need to revoke all |
| 2827 | // thread-local allocation stacks. |
| 2828 | RevokeAllThreadLocalAllocationStacks(self); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2829 | Atomic<size_t> fail_count_(0); |
| 2830 | VerifyObjectVisitor visitor(this, &fail_count_, verify_referents); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2831 | // Verify objects in the allocation stack since these will be objects which were: |
| 2832 | // 1. Allocated prior to the GC (pre GC verification). |
| 2833 | // 2. Allocated during the GC (pre sweep GC verification). |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2834 | // We don't want to verify the objects in the live stack since they themselves may be |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2835 | // pointing to dead objects if they are not reachable. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2836 | VisitObjectsPaused(VerifyObjectVisitor::VisitCallback, &visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2837 | // Verify the roots: |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2838 | visitor.VerifyRoots(); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2839 | if (visitor.GetFailureCount() > 0) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 2840 | // Dump mod-union tables. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2841 | for (const auto& table_pair : mod_union_tables_) { |
| 2842 | accounting::ModUnionTable* mod_union_table = table_pair.second; |
| 2843 | mod_union_table->Dump(LOG(ERROR) << mod_union_table->GetName() << ": "); |
| 2844 | } |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 2845 | // Dump remembered sets. |
| 2846 | for (const auto& table_pair : remembered_sets_) { |
| 2847 | accounting::RememberedSet* remembered_set = table_pair.second; |
| 2848 | remembered_set->Dump(LOG(ERROR) << remembered_set->GetName() << ": "); |
| 2849 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 2850 | DumpSpaces(LOG(ERROR)); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2851 | } |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 2852 | return visitor.GetFailureCount(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | class VerifyReferenceCardVisitor { |
| 2856 | public: |
| 2857 | VerifyReferenceCardVisitor(Heap* heap, bool* failed) |
| 2858 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 2859 | Locks::heap_bitmap_lock_) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2860 | : heap_(heap), failed_(failed) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2861 | } |
| 2862 | |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 2863 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for |
| 2864 | // annotalysis on visitors. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2865 | void operator()(mirror::Object* obj, MemberOffset offset, bool is_static) const |
| 2866 | NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 2867 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 2868 | // Filter out class references since changing an object's class does not mark the card as dirty. |
| 2869 | // Also handles large objects, since the only reference they hold is a class reference. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2870 | if (ref != nullptr && !ref->IsClass()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2871 | accounting::CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2872 | // If the object is not dirty and it is referencing something in the live stack other than |
| 2873 | // class, then it must be on a dirty card. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 2874 | if (!card_table->AddrIsInCardTable(obj)) { |
| 2875 | LOG(ERROR) << "Object " << obj << " is not in the address range of the card table"; |
| 2876 | *failed_ = true; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 2877 | } else if (!card_table->IsDirty(obj)) { |
Mathieu Chartier | 938a03b | 2014-01-16 15:10:31 -0800 | [diff] [blame] | 2878 | // TODO: Check mod-union tables. |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 2879 | // Card should be either kCardDirty if it got re-dirtied after we aged it, or |
| 2880 | // kCardDirty - 1 if it didnt get touched since we aged it. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2881 | accounting::ObjectStack* live_stack = heap_->live_stack_.get(); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 2882 | if (live_stack->ContainsSorted(ref)) { |
| 2883 | if (live_stack->ContainsSorted(obj)) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2884 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 2885 | } |
| 2886 | if (heap_->GetLiveBitmap()->Test(obj)) { |
| 2887 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 2888 | } |
| 2889 | LOG(ERROR) << "Object " << obj << " " << PrettyTypeOf(obj) |
| 2890 | << " references " << ref << " " << PrettyTypeOf(ref) << " in live stack"; |
| 2891 | |
| 2892 | // Print which field of the object is dead. |
| 2893 | if (!obj->IsObjectArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2894 | mirror::Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2895 | CHECK(klass != nullptr); |
| 2896 | auto* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 2897 | auto num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
| 2898 | CHECK_EQ(fields == nullptr, num_fields == 0u); |
| 2899 | for (size_t i = 0; i < num_fields; ++i) { |
| 2900 | ArtField* cur = &fields[i]; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2901 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 2902 | LOG(ERROR) << (is_static ? "Static " : "") << "field in the live stack is " |
| 2903 | << PrettyField(cur); |
| 2904 | break; |
| 2905 | } |
| 2906 | } |
| 2907 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2908 | mirror::ObjectArray<mirror::Object>* object_array = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2909 | obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2910 | for (int32_t i = 0; i < object_array->GetLength(); ++i) { |
| 2911 | if (object_array->Get(i) == ref) { |
| 2912 | LOG(ERROR) << (is_static ? "Static " : "") << "obj[" << i << "] = ref"; |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | *failed_ = true; |
| 2918 | } |
| 2919 | } |
| 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2924 | Heap* const heap_; |
| 2925 | bool* const failed_; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2926 | }; |
| 2927 | |
| 2928 | class VerifyLiveStackReferences { |
| 2929 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 2930 | explicit VerifyLiveStackReferences(Heap* heap) |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2931 | : heap_(heap), |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 2932 | failed_(false) {} |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2933 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 2934 | void operator()(mirror::Object* obj) const |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2935 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 2936 | VerifyReferenceCardVisitor visitor(heap_, const_cast<bool*>(&failed_)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 2937 | obj->VisitReferences<true>(visitor, VoidFunctor()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | bool Failed() const { |
| 2941 | return failed_; |
| 2942 | } |
| 2943 | |
| 2944 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2945 | Heap* const heap_; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2946 | bool failed_; |
| 2947 | }; |
| 2948 | |
| 2949 | bool Heap::VerifyMissingCardMarks() { |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 2950 | Thread* self = Thread::Current(); |
| 2951 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 2952 | // We need to sort the live stack since we binary search it. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2953 | live_stack_->Sort(); |
Hiroshi Yamauchi | 1ed9061 | 2014-02-14 15:00:51 -0800 | [diff] [blame] | 2954 | // Since we sorted the allocation stack content, need to revoke all |
| 2955 | // thread-local allocation stacks. |
| 2956 | RevokeAllThreadLocalAllocationStacks(self); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2957 | VerifyLiveStackReferences visitor(this); |
| 2958 | GetLiveBitmap()->Visit(visitor); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2959 | // We can verify objects in the live stack since none of these should reference dead objects. |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 2960 | for (auto* it = live_stack_->Begin(); it != live_stack_->End(); ++it) { |
| 2961 | if (!kUseThreadLocalAllocationStack || it->AsMirrorPtr() != nullptr) { |
| 2962 | visitor(it->AsMirrorPtr()); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2963 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2964 | } |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 2965 | return !visitor.Failed(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 2966 | } |
| 2967 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2968 | void Heap::SwapStacks(Thread* self) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2969 | UNUSED(self); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2970 | if (kUseThreadLocalAllocationStack) { |
| 2971 | live_stack_->AssertAllZero(); |
| 2972 | } |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 2973 | allocation_stack_.swap(live_stack_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2976 | void Heap::RevokeAllThreadLocalAllocationStacks(Thread* self) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2977 | // This must be called only during the pause. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2978 | DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self)); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 2979 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 2980 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 2981 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2982 | for (Thread* t : thread_list) { |
| 2983 | t->RevokeThreadLocalAllocationStack(); |
| 2984 | } |
| 2985 | } |
| 2986 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2987 | void Heap::AssertThreadLocalBuffersAreRevoked(Thread* thread) { |
| 2988 | if (kIsDebugBuild) { |
| 2989 | if (rosalloc_space_ != nullptr) { |
| 2990 | rosalloc_space_->AssertThreadLocalBuffersAreRevoked(thread); |
| 2991 | } |
| 2992 | if (bump_pointer_space_ != nullptr) { |
| 2993 | bump_pointer_space_->AssertThreadLocalBuffersAreRevoked(thread); |
| 2994 | } |
| 2995 | } |
| 2996 | } |
| 2997 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 2998 | void Heap::AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked() { |
| 2999 | if (kIsDebugBuild) { |
| 3000 | if (bump_pointer_space_ != nullptr) { |
| 3001 | bump_pointer_space_->AssertAllThreadLocalBuffersAreRevoked(); |
| 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3006 | accounting::ModUnionTable* Heap::FindModUnionTableFromSpace(space::Space* space) { |
| 3007 | auto it = mod_union_tables_.find(space); |
| 3008 | if (it == mod_union_tables_.end()) { |
| 3009 | return nullptr; |
| 3010 | } |
| 3011 | return it->second; |
| 3012 | } |
| 3013 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3014 | accounting::RememberedSet* Heap::FindRememberedSetFromSpace(space::Space* space) { |
| 3015 | auto it = remembered_sets_.find(space); |
| 3016 | if (it == remembered_sets_.end()) { |
| 3017 | return nullptr; |
| 3018 | } |
| 3019 | return it->second; |
| 3020 | } |
| 3021 | |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 3022 | void Heap::ProcessCards(TimingLogger* timings, bool use_rem_sets, bool process_alloc_space_cards, |
| 3023 | bool clear_alloc_space_cards) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3024 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3025 | // Clear cards and keep track of cards cleared in the mod-union table. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 3026 | for (const auto& space : continuous_spaces_) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3027 | accounting::ModUnionTable* table = FindModUnionTableFromSpace(space); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3028 | accounting::RememberedSet* rem_set = FindRememberedSetFromSpace(space); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3029 | if (table != nullptr) { |
| 3030 | const char* name = space->IsZygoteSpace() ? "ZygoteModUnionClearCards" : |
| 3031 | "ImageModUnionClearCards"; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3032 | TimingLogger::ScopedTiming t2(name, timings); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3033 | table->ClearCards(); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3034 | } else if (use_rem_sets && rem_set != nullptr) { |
| 3035 | DCHECK(collector::SemiSpace::kUseRememberedSet && collector_type_ == kCollectorTypeGSS) |
| 3036 | << static_cast<int>(collector_type_); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3037 | TimingLogger::ScopedTiming t2("AllocSpaceRemSetClearCards", timings); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3038 | rem_set->ClearCards(); |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 3039 | } else if (process_alloc_space_cards) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3040 | TimingLogger::ScopedTiming t2("AllocSpaceClearCards", timings); |
Lei Li | 4add3b4 | 2015-01-15 11:55:26 +0800 | [diff] [blame] | 3041 | if (clear_alloc_space_cards) { |
| 3042 | card_table_->ClearCardRange(space->Begin(), space->End()); |
| 3043 | } else { |
| 3044 | // No mod union table for the AllocSpace. Age the cards so that the GC knows that these |
| 3045 | // cards were dirty before the GC started. |
| 3046 | // TODO: Need to use atomic for the case where aged(cleaning thread) -> dirty(other thread) |
| 3047 | // -> clean(cleaning thread). |
| 3048 | // The races are we either end up with: Aged card, unaged card. Since we have the |
| 3049 | // checkpoint roots and then we scan / update mod union tables after. We will always |
| 3050 | // scan either card. If we end up with the non aged card, we scan it it in the pause. |
| 3051 | card_table_->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(), |
| 3052 | VoidFunctor()); |
| 3053 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame^] | 3058 | struct IdentityMarkHeapReferenceVisitor : public MarkObjectVisitor { |
| 3059 | virtual mirror::Object* MarkObject(mirror::Object* obj) OVERRIDE { |
| 3060 | return obj; |
| 3061 | } |
| 3062 | virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>*) OVERRIDE { |
| 3063 | } |
| 3064 | }; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3065 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3066 | void Heap::PreGcVerificationPaused(collector::GarbageCollector* gc) { |
| 3067 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3068 | TimingLogger* const timings = current_gc_iteration_.GetTimings(); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3069 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3070 | if (verify_pre_gc_heap_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3071 | TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyHeapReferences", timings); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3072 | size_t failures = VerifyHeapReferences(); |
| 3073 | if (failures > 0) { |
| 3074 | LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed with " << failures |
| 3075 | << " failures"; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3076 | } |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 3077 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3078 | // Check that all objects which reference things in the live stack are on dirty cards. |
| 3079 | if (verify_missing_card_marks_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3080 | TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyMissingCardMarks", timings); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3081 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 3082 | SwapStacks(self); |
| 3083 | // Sort the live stack so that we can quickly binary search it later. |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 3084 | CHECK(VerifyMissingCardMarks()) << "Pre " << gc->GetName() |
| 3085 | << " missing card mark verification failed\n" << DumpSpaces(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3086 | SwapStacks(self); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3087 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3088 | if (verify_mod_union_table_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3089 | TimingLogger::ScopedTiming t2("(Paused)PreGcVerifyModUnionTables", timings); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3090 | ReaderMutexLock reader_lock(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3091 | for (const auto& table_pair : mod_union_tables_) { |
| 3092 | accounting::ModUnionTable* mod_union_table = table_pair.second; |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame^] | 3093 | IdentityMarkHeapReferenceVisitor visitor; |
| 3094 | mod_union_table->UpdateAndMarkReferences(&visitor); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3095 | mod_union_table->Verify(); |
| 3096 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | void Heap::PreGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 3101 | if (verify_pre_gc_heap_ || verify_missing_card_marks_ || verify_mod_union_table_) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3102 | collector::GarbageCollector::ScopedPause pause(gc); |
| 3103 | PreGcVerificationPaused(gc); |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | void Heap::PrePauseRosAllocVerification(collector::GarbageCollector* gc) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3108 | UNUSED(gc); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3109 | // TODO: Add a new runtime option for this? |
| 3110 | if (verify_pre_gc_rosalloc_) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3111 | RosAllocVerification(current_gc_iteration_.GetTimings(), "PreGcRosAllocVerification"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3112 | } |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 3113 | } |
| 3114 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3115 | void Heap::PreSweepingGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3116 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3117 | TimingLogger* const timings = current_gc_iteration_.GetTimings(); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3118 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3119 | // Called before sweeping occurs since we want to make sure we are not going so reclaim any |
| 3120 | // reachable objects. |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3121 | if (verify_pre_sweeping_heap_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3122 | TimingLogger::ScopedTiming t2("(Paused)PostSweepingVerifyHeapReferences", timings); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3123 | CHECK_NE(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 3124 | { |
| 3125 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 3126 | // Swapping bound bitmaps does nothing. |
| 3127 | gc->SwapBitmaps(); |
| 3128 | } |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 3129 | // Pass in false since concurrent reference processing can mean that the reference referents |
| 3130 | // may point to dead objects at the point which PreSweepingGcVerification is called. |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3131 | size_t failures = VerifyHeapReferences(false); |
| 3132 | if (failures > 0) { |
| 3133 | LOG(FATAL) << "Pre sweeping " << gc->GetName() << " GC verification failed with " << failures |
| 3134 | << " failures"; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3135 | } |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 3136 | { |
| 3137 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 3138 | gc->SwapBitmaps(); |
| 3139 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3140 | } |
| 3141 | if (verify_pre_sweeping_rosalloc_) { |
| 3142 | RosAllocVerification(timings, "PreSweepingRosAllocVerification"); |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | void Heap::PostGcVerificationPaused(collector::GarbageCollector* gc) { |
| 3147 | // Only pause if we have to do some verification. |
| 3148 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3149 | TimingLogger* const timings = GetCurrentGcIteration()->GetTimings(); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3150 | TimingLogger::ScopedTiming t(__FUNCTION__, timings); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3151 | if (verify_system_weaks_) { |
| 3152 | ReaderMutexLock mu2(self, *Locks::heap_bitmap_lock_); |
| 3153 | collector::MarkSweep* mark_sweep = down_cast<collector::MarkSweep*>(gc); |
| 3154 | mark_sweep->VerifySystemWeaks(); |
| 3155 | } |
| 3156 | if (verify_post_gc_rosalloc_) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3157 | RosAllocVerification(timings, "(Paused)PostGcRosAllocVerification"); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3158 | } |
| 3159 | if (verify_post_gc_heap_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3160 | TimingLogger::ScopedTiming t2("(Paused)PostGcVerifyHeapReferences", timings); |
Mathieu Chartier | 8ab7e78 | 2014-05-19 16:55:27 -0700 | [diff] [blame] | 3161 | size_t failures = VerifyHeapReferences(); |
| 3162 | if (failures > 0) { |
| 3163 | LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed with " << failures |
| 3164 | << " failures"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3165 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3166 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 3167 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3168 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3169 | void Heap::PostGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3170 | if (verify_system_weaks_ || verify_post_gc_rosalloc_ || verify_post_gc_heap_) { |
| 3171 | collector::GarbageCollector::ScopedPause pause(gc); |
Mathieu Chartier | d35326f | 2014-08-18 15:02:59 -0700 | [diff] [blame] | 3172 | PostGcVerificationPaused(gc); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3173 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 3174 | } |
| 3175 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3176 | void Heap::RosAllocVerification(TimingLogger* timings, const char* name) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 3177 | TimingLogger::ScopedTiming t(name, timings); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 3178 | for (const auto& space : continuous_spaces_) { |
| 3179 | if (space->IsRosAllocSpace()) { |
| 3180 | VLOG(heap) << name << " : " << space->GetName(); |
| 3181 | space->AsRosAllocSpace()->Verify(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3186 | collector::GcType Heap::WaitForGcToComplete(GcCause cause, Thread* self) { |
Mathieu Chartier | caa82d6 | 2014-02-02 16:51:17 -0800 | [diff] [blame] | 3187 | ScopedThreadStateChange tsc(self, kWaitingForGcToComplete); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3188 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3189 | return WaitForGcToCompleteLocked(cause, self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3192 | collector::GcType Heap::WaitForGcToCompleteLocked(GcCause cause, Thread* self) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3193 | collector::GcType last_gc_type = collector::kGcTypeNone; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3194 | uint64_t wait_start = NanoTime(); |
Mathieu Chartier | d5a89ee | 2014-01-31 09:55:13 -0800 | [diff] [blame] | 3195 | while (collector_type_running_ != kCollectorTypeNone) { |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 3196 | if (self != task_processor_->GetRunningThread()) { |
| 3197 | // The current thread is about to wait for a currently running |
| 3198 | // collection to finish. If the waiting thread is not the heap |
| 3199 | // task daemon thread, the currently running collection is |
| 3200 | // considered as a blocking GC. |
| 3201 | running_collection_is_blocking_ = true; |
| 3202 | VLOG(gc) << "Waiting for a blocking GC " << cause; |
| 3203 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3204 | ATRACE_BEGIN("GC: Wait For Completion"); |
| 3205 | // We must wait, change thread state then sleep on gc_complete_cond_; |
| 3206 | gc_complete_cond_->Wait(self); |
| 3207 | last_gc_type = last_gc_type_; |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 3208 | ATRACE_END(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 3209 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3210 | uint64_t wait_time = NanoTime() - wait_start; |
| 3211 | total_wait_time_ += wait_time; |
| 3212 | if (wait_time > long_pause_log_threshold_) { |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3213 | LOG(INFO) << "WaitForGcToComplete blocked for " << PrettyDuration(wait_time) |
| 3214 | << " for cause " << cause; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3215 | } |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 3216 | if (self != task_processor_->GetRunningThread()) { |
| 3217 | // The current thread is about to run a collection. If the thread |
| 3218 | // is not the heap task daemon thread, it's considered as a |
| 3219 | // blocking GC (i.e., blocking itself). |
| 3220 | running_collection_is_blocking_ = true; |
| 3221 | VLOG(gc) << "Starting a blocking GC " << cause; |
| 3222 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 3223 | return last_gc_type; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 3226 | void Heap::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3227 | os << "Heap: " << GetPercentFree() << "% free, " << PrettySize(GetBytesAllocated()) << "/" |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3228 | << PrettySize(GetTotalMemory()) << "; " << GetObjectsAllocated() << " objects\n"; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 3229 | DumpGcPerformanceInfo(os); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | size_t Heap::GetPercentFree() { |
Mathieu Chartier | d30e1d6 | 2014-06-09 13:25:22 -0700 | [diff] [blame] | 3233 | return static_cast<size_t>(100.0f * static_cast<float>(GetFreeMemory()) / max_allowed_footprint_); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 3234 | } |
| 3235 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3236 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3237 | if (max_allowed_footprint > GetMaxMemory()) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3238 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) << " to " |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3239 | << PrettySize(GetMaxMemory()); |
| 3240 | max_allowed_footprint = GetMaxMemory(); |
| 3241 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 3242 | max_allowed_footprint_ = max_allowed_footprint; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3245 | bool Heap::IsMovableObject(const mirror::Object* obj) const { |
| 3246 | if (kMovingCollector) { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 3247 | space::Space* space = FindContinuousSpaceFromObject(obj, true); |
| 3248 | if (space != nullptr) { |
| 3249 | // TODO: Check large object? |
| 3250 | return space->CanMoveObjects(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3251 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3252 | } |
| 3253 | return false; |
| 3254 | } |
| 3255 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3256 | void Heap::UpdateMaxNativeFootprint() { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 3257 | size_t native_size = native_bytes_allocated_.LoadRelaxed(); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3258 | // TODO: Tune the native heap utilization to be a value other than the java heap utilization. |
| 3259 | size_t target_size = native_size / GetTargetHeapUtilization(); |
| 3260 | if (target_size > native_size + max_free_) { |
| 3261 | target_size = native_size + max_free_; |
| 3262 | } else if (target_size < native_size + min_free_) { |
| 3263 | target_size = native_size + min_free_; |
| 3264 | } |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3265 | native_footprint_gc_watermark_ = std::min(growth_limit_, target_size); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3268 | collector::GarbageCollector* Heap::FindCollectorByGcType(collector::GcType gc_type) { |
| 3269 | for (const auto& collector : garbage_collectors_) { |
| 3270 | if (collector->GetCollectorType() == collector_type_ && |
| 3271 | collector->GetGcType() == gc_type) { |
| 3272 | return collector; |
| 3273 | } |
| 3274 | } |
| 3275 | return nullptr; |
| 3276 | } |
| 3277 | |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3278 | double Heap::HeapGrowthMultiplier() const { |
| 3279 | // If we don't care about pause times we are background, so return 1.0. |
| 3280 | if (!CareAboutPauseTimes() || IsLowMemoryMode()) { |
| 3281 | return 1.0; |
| 3282 | } |
| 3283 | return foreground_heap_growth_multiplier_; |
| 3284 | } |
| 3285 | |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3286 | void Heap::GrowForUtilization(collector::GarbageCollector* collector_ran, |
| 3287 | uint64_t bytes_allocated_before_gc) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 3288 | // We know what our utilization is at this moment. |
| 3289 | // This doesn't actually resize any memory. It just lets the heap grow more when necessary. |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3290 | const uint64_t bytes_allocated = GetBytesAllocated(); |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3291 | uint64_t target_size; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3292 | collector::GcType gc_type = collector_ran->GetGcType(); |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3293 | const double multiplier = HeapGrowthMultiplier(); // Use the multiplier to grow more for |
| 3294 | // foreground. |
| 3295 | const uint64_t adjusted_min_free = static_cast<uint64_t>(min_free_ * multiplier); |
| 3296 | const uint64_t adjusted_max_free = static_cast<uint64_t>(max_free_ * multiplier); |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3297 | if (gc_type != collector::kGcTypeSticky) { |
| 3298 | // Grow the heap for non sticky GC. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3299 | ssize_t delta = bytes_allocated / GetTargetHeapUtilization() - bytes_allocated; |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3300 | CHECK_GE(delta, 0); |
| 3301 | target_size = bytes_allocated + delta * multiplier; |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3302 | target_size = std::min(target_size, bytes_allocated + adjusted_max_free); |
| 3303 | target_size = std::max(target_size, bytes_allocated + adjusted_min_free); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3304 | native_need_to_run_finalization_ = true; |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3305 | next_gc_type_ = collector::kGcTypeSticky; |
| 3306 | } else { |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3307 | collector::GcType non_sticky_gc_type = |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 3308 | HasZygoteSpace() ? collector::kGcTypePartial : collector::kGcTypeFull; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3309 | // Find what the next non sticky collector will be. |
| 3310 | collector::GarbageCollector* non_sticky_collector = FindCollectorByGcType(non_sticky_gc_type); |
| 3311 | // If the throughput of the current sticky GC >= throughput of the non sticky collector, then |
| 3312 | // do another sticky collection next. |
| 3313 | // We also check that the bytes allocated aren't over the footprint limit in order to prevent a |
| 3314 | // pathological case where dead objects which aren't reclaimed by sticky could get accumulated |
| 3315 | // if the sticky GC throughput always remained >= the full/partial throughput. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3316 | if (current_gc_iteration_.GetEstimatedThroughput() * kStickyGcThroughputAdjustment >= |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3317 | non_sticky_collector->GetEstimatedMeanThroughput() && |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3318 | non_sticky_collector->NumberOfIterations() > 0 && |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3319 | bytes_allocated <= max_allowed_footprint_) { |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3320 | next_gc_type_ = collector::kGcTypeSticky; |
| 3321 | } else { |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 3322 | next_gc_type_ = non_sticky_gc_type; |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3323 | } |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3324 | // If we have freed enough memory, shrink the heap back down. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3325 | if (bytes_allocated + adjusted_max_free < max_allowed_footprint_) { |
| 3326 | target_size = bytes_allocated + adjusted_max_free; |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3327 | } else { |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3328 | target_size = std::max(bytes_allocated, static_cast<uint64_t>(max_allowed_footprint_)); |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 3329 | } |
| 3330 | } |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3331 | if (!ignore_max_footprint_) { |
| 3332 | SetIdealFootprint(target_size); |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 3333 | if (IsGcConcurrent()) { |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3334 | const uint64_t freed_bytes = current_gc_iteration_.GetFreedBytes() + |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3335 | current_gc_iteration_.GetFreedLargeObjectBytes() + |
| 3336 | current_gc_iteration_.GetFreedRevokeBytes(); |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3337 | // Bytes allocated will shrink by freed_bytes after the GC runs, so if we want to figure out |
| 3338 | // how many bytes were allocated during the GC we need to add freed_bytes back on. |
| 3339 | CHECK_GE(bytes_allocated + freed_bytes, bytes_allocated_before_gc); |
| 3340 | const uint64_t bytes_allocated_during_gc = bytes_allocated + freed_bytes - |
| 3341 | bytes_allocated_before_gc; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3342 | // Calculate when to perform the next ConcurrentGC. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3343 | // Calculate the estimated GC duration. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 3344 | const double gc_duration_seconds = NsToMs(current_gc_iteration_.GetDurationNs()) / 1000.0; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3345 | // Estimate how many remaining bytes we will have when we need to start the next GC. |
Mathieu Chartier | e2c2f6e | 2014-12-16 18:49:31 -0800 | [diff] [blame] | 3346 | size_t remaining_bytes = bytes_allocated_during_gc * gc_duration_seconds; |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3347 | remaining_bytes = std::min(remaining_bytes, kMaxConcurrentRemainingBytes); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3348 | remaining_bytes = std::max(remaining_bytes, kMinConcurrentRemainingBytes); |
| 3349 | if (UNLIKELY(remaining_bytes > max_allowed_footprint_)) { |
| 3350 | // A never going to happen situation that from the estimated allocation rate we will exceed |
| 3351 | // the applications entire footprint with the given estimated allocation rate. Schedule |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3352 | // another GC nearly straight away. |
| 3353 | remaining_bytes = kMinConcurrentRemainingBytes; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 3354 | } |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3355 | DCHECK_LE(remaining_bytes, max_allowed_footprint_); |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 3356 | DCHECK_LE(max_allowed_footprint_, GetMaxMemory()); |
Mathieu Chartier | 7476280 | 2014-01-24 10:21:35 -0800 | [diff] [blame] | 3357 | // Start a concurrent GC when we get close to the estimated remaining bytes. When the |
| 3358 | // allocation rate is very high, remaining_bytes could tell us that we should start a GC |
| 3359 | // right away. |
Mathieu Chartier | 2f8da3e | 2014-04-15 15:37:02 -0700 | [diff] [blame] | 3360 | concurrent_start_bytes_ = std::max(max_allowed_footprint_ - remaining_bytes, |
| 3361 | static_cast<size_t>(bytes_allocated)); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 3362 | } |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 3363 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 3364 | } |
| 3365 | |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 3366 | void Heap::ClampGrowthLimit() { |
Mathieu Chartier | ddac423 | 2015-04-02 10:08:03 -0700 | [diff] [blame] | 3367 | // Use heap bitmap lock to guard against races with BindLiveToMarkBitmap. |
| 3368 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 3369 | capacity_ = growth_limit_; |
| 3370 | for (const auto& space : continuous_spaces_) { |
| 3371 | if (space->IsMallocSpace()) { |
| 3372 | gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 3373 | malloc_space->ClampGrowthLimit(); |
| 3374 | } |
| 3375 | } |
| 3376 | // This space isn't added for performance reasons. |
| 3377 | if (main_space_backup_.get() != nullptr) { |
| 3378 | main_space_backup_->ClampGrowthLimit(); |
| 3379 | } |
| 3380 | } |
| 3381 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 3382 | void Heap::ClearGrowthLimit() { |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 3383 | growth_limit_ = capacity_; |
Mathieu Chartier | 0310da5 | 2014-12-01 13:40:48 -0800 | [diff] [blame] | 3384 | for (const auto& space : continuous_spaces_) { |
| 3385 | if (space->IsMallocSpace()) { |
| 3386 | gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 3387 | malloc_space->ClearGrowthLimit(); |
| 3388 | malloc_space->SetFootprintLimit(malloc_space->Capacity()); |
| 3389 | } |
| 3390 | } |
| 3391 | // This space isn't added for performance reasons. |
| 3392 | if (main_space_backup_.get() != nullptr) { |
| 3393 | main_space_backup_->ClearGrowthLimit(); |
| 3394 | main_space_backup_->SetFootprintLimit(main_space_backup_->Capacity()); |
| 3395 | } |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 3398 | void Heap::AddFinalizerReference(Thread* self, mirror::Object** object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3399 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 3400 | ScopedLocalRef<jobject> arg(self->GetJniEnv(), soa.AddLocalReference<jobject>(*object)); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 3401 | jvalue args[1]; |
| 3402 | args[0].l = arg.get(); |
| 3403 | InvokeWithJValues(soa, nullptr, WellKnownClasses::java_lang_ref_FinalizerReference_add, args); |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 3404 | // Restore object in case it gets moved. |
| 3405 | *object = soa.Decode<mirror::Object*>(arg.get()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3406 | } |
| 3407 | |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3408 | void Heap::RequestConcurrentGCAndSaveObject(Thread* self, bool force_full, mirror::Object** obj) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3409 | StackHandleScope<1> hs(self); |
| 3410 | HandleWrapper<mirror::Object> wrapper(hs.NewHandleWrapper(obj)); |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3411 | RequestConcurrentGC(self, force_full); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3412 | } |
| 3413 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3414 | class Heap::ConcurrentGCTask : public HeapTask { |
| 3415 | public: |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3416 | explicit ConcurrentGCTask(uint64_t target_time, bool force_full) |
| 3417 | : HeapTask(target_time), force_full_(force_full) { } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3418 | virtual void Run(Thread* self) OVERRIDE { |
| 3419 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3420 | heap->ConcurrentGC(self, force_full_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3421 | heap->ClearConcurrentGCRequest(); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 3422 | } |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3423 | |
| 3424 | private: |
| 3425 | const bool force_full_; // If true, force full (or partial) collection. |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3426 | }; |
| 3427 | |
| 3428 | static bool CanAddHeapTask(Thread* self) LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_) { |
| 3429 | Runtime* runtime = Runtime::Current(); |
| 3430 | return runtime != nullptr && runtime->IsFinishedStarting() && !runtime->IsShuttingDown(self) && |
| 3431 | !self->IsHandlingStackOverflow(); |
| 3432 | } |
| 3433 | |
| 3434 | void Heap::ClearConcurrentGCRequest() { |
| 3435 | concurrent_gc_pending_.StoreRelaxed(false); |
| 3436 | } |
| 3437 | |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3438 | void Heap::RequestConcurrentGC(Thread* self, bool force_full) { |
Mathieu Chartier | ac19516 | 2015-02-20 18:44:28 +0000 | [diff] [blame] | 3439 | if (CanAddHeapTask(self) && |
| 3440 | concurrent_gc_pending_.CompareExchangeStrongSequentiallyConsistent(false, true)) { |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3441 | task_processor_->AddTask(self, new ConcurrentGCTask(NanoTime(), // Start straight away. |
| 3442 | force_full)); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3443 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3446 | void Heap::ConcurrentGC(Thread* self, bool force_full) { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3447 | if (!Runtime::Current()->IsShuttingDown(self)) { |
| 3448 | // Wait for any GCs currently running to finish. |
| 3449 | if (WaitForGcToComplete(kGcCauseBackground, self) == collector::kGcTypeNone) { |
| 3450 | // If the we can't run the GC type we wanted to run, find the next appropriate one and try that |
| 3451 | // instead. E.g. can't do partial, so do full instead. |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3452 | collector::GcType next_gc_type = next_gc_type_; |
| 3453 | // If forcing full and next gc type is sticky, override with a non-sticky type. |
| 3454 | if (force_full && next_gc_type == collector::kGcTypeSticky) { |
| 3455 | next_gc_type = HasZygoteSpace() ? collector::kGcTypePartial : collector::kGcTypeFull; |
| 3456 | } |
| 3457 | if (CollectGarbageInternal(next_gc_type, kGcCauseBackground, false) == |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3458 | collector::kGcTypeNone) { |
| 3459 | for (collector::GcType gc_type : gc_plan_) { |
| 3460 | // Attempt to run the collector, if we succeed, we are done. |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3461 | if (gc_type > next_gc_type && |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3462 | CollectGarbageInternal(gc_type, kGcCauseBackground, false) != |
| 3463 | collector::kGcTypeNone) { |
| 3464 | break; |
| 3465 | } |
Mathieu Chartier | f9ed0d3 | 2013-11-21 16:42:47 -0800 | [diff] [blame] | 3466 | } |
| 3467 | } |
| 3468 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 3469 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3472 | class Heap::CollectorTransitionTask : public HeapTask { |
| 3473 | public: |
| 3474 | explicit CollectorTransitionTask(uint64_t target_time) : HeapTask(target_time) { } |
| 3475 | virtual void Run(Thread* self) OVERRIDE { |
| 3476 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 3477 | heap->DoPendingCollectorTransition(); |
| 3478 | heap->ClearPendingCollectorTransition(self); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3479 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3480 | }; |
| 3481 | |
| 3482 | void Heap::ClearPendingCollectorTransition(Thread* self) { |
| 3483 | MutexLock mu(self, *pending_task_lock_); |
| 3484 | pending_collector_transition_ = nullptr; |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3485 | } |
| 3486 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3487 | void Heap::RequestCollectorTransition(CollectorType desired_collector_type, uint64_t delta_time) { |
| 3488 | Thread* self = Thread::Current(); |
| 3489 | desired_collector_type_ = desired_collector_type; |
| 3490 | if (desired_collector_type_ == collector_type_ || !CanAddHeapTask(self)) { |
| 3491 | return; |
| 3492 | } |
| 3493 | CollectorTransitionTask* added_task = nullptr; |
| 3494 | const uint64_t target_time = NanoTime() + delta_time; |
| 3495 | { |
| 3496 | MutexLock mu(self, *pending_task_lock_); |
| 3497 | // If we have an existing collector transition, update the targe time to be the new target. |
| 3498 | if (pending_collector_transition_ != nullptr) { |
| 3499 | task_processor_->UpdateTargetRunTime(self, pending_collector_transition_, target_time); |
| 3500 | return; |
| 3501 | } |
| 3502 | added_task = new CollectorTransitionTask(target_time); |
| 3503 | pending_collector_transition_ = added_task; |
| 3504 | } |
| 3505 | task_processor_->AddTask(self, added_task); |
| 3506 | } |
| 3507 | |
| 3508 | class Heap::HeapTrimTask : public HeapTask { |
| 3509 | public: |
| 3510 | explicit HeapTrimTask(uint64_t delta_time) : HeapTask(NanoTime() + delta_time) { } |
| 3511 | virtual void Run(Thread* self) OVERRIDE { |
| 3512 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 3513 | heap->Trim(self); |
| 3514 | heap->ClearPendingTrim(self); |
| 3515 | } |
| 3516 | }; |
| 3517 | |
| 3518 | void Heap::ClearPendingTrim(Thread* self) { |
| 3519 | MutexLock mu(self, *pending_task_lock_); |
| 3520 | pending_heap_trim_ = nullptr; |
| 3521 | } |
| 3522 | |
| 3523 | void Heap::RequestTrim(Thread* self) { |
| 3524 | if (!CanAddHeapTask(self)) { |
| 3525 | return; |
| 3526 | } |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 3527 | // GC completed and now we must decide whether to request a heap trim (advising pages back to the |
| 3528 | // kernel) or not. Issuing a request will also cause trimming of the libc heap. As a trim scans |
| 3529 | // a space it will hold its lock and can become a cause of jank. |
| 3530 | // Note, the large object space self trims and the Zygote space was trimmed and unchanging since |
| 3531 | // forking. |
| 3532 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 3533 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 3534 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 3535 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 3536 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 3537 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 3538 | // not how much use we're making of those pages. |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3539 | HeapTrimTask* added_task = nullptr; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 3540 | { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3541 | MutexLock mu(self, *pending_task_lock_); |
| 3542 | if (pending_heap_trim_ != nullptr) { |
| 3543 | // Already have a heap trim request in task processor, ignore this request. |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 3544 | return; |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3545 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3546 | added_task = new HeapTrimTask(kHeapTrimWait); |
| 3547 | pending_heap_trim_ = added_task; |
Mathieu Chartier | c39e342 | 2013-08-07 16:41:36 -0700 | [diff] [blame] | 3548 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3549 | task_processor_->AddTask(self, added_task); |
Mathieu Chartier | a5f9de0 | 2014-02-28 16:48:42 -0800 | [diff] [blame] | 3550 | } |
| 3551 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3552 | void Heap::RevokeThreadLocalBuffers(Thread* thread) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3553 | if (rosalloc_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3554 | size_t freed_bytes_revoke = rosalloc_space_->RevokeThreadLocalBuffers(thread); |
| 3555 | if (freed_bytes_revoke > 0U) { |
| 3556 | num_bytes_freed_revoke_.FetchAndAddSequentiallyConsistent(freed_bytes_revoke); |
| 3557 | CHECK_GE(num_bytes_allocated_.LoadRelaxed(), num_bytes_freed_revoke_.LoadRelaxed()); |
| 3558 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3559 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3560 | if (bump_pointer_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3561 | CHECK_EQ(bump_pointer_space_->RevokeThreadLocalBuffers(thread), 0U); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3562 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3563 | if (region_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3564 | CHECK_EQ(region_space_->RevokeThreadLocalBuffers(thread), 0U); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3565 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3566 | } |
| 3567 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 3568 | void Heap::RevokeRosAllocThreadLocalBuffers(Thread* thread) { |
| 3569 | if (rosalloc_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3570 | size_t freed_bytes_revoke = rosalloc_space_->RevokeThreadLocalBuffers(thread); |
| 3571 | if (freed_bytes_revoke > 0U) { |
| 3572 | num_bytes_freed_revoke_.FetchAndAddSequentiallyConsistent(freed_bytes_revoke); |
| 3573 | CHECK_GE(num_bytes_allocated_.LoadRelaxed(), num_bytes_freed_revoke_.LoadRelaxed()); |
| 3574 | } |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 3575 | } |
| 3576 | } |
| 3577 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3578 | void Heap::RevokeAllThreadLocalBuffers() { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3579 | if (rosalloc_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3580 | size_t freed_bytes_revoke = rosalloc_space_->RevokeAllThreadLocalBuffers(); |
| 3581 | if (freed_bytes_revoke > 0U) { |
| 3582 | num_bytes_freed_revoke_.FetchAndAddSequentiallyConsistent(freed_bytes_revoke); |
| 3583 | CHECK_GE(num_bytes_allocated_.LoadRelaxed(), num_bytes_freed_revoke_.LoadRelaxed()); |
| 3584 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 3585 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3586 | if (bump_pointer_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3587 | CHECK_EQ(bump_pointer_space_->RevokeAllThreadLocalBuffers(), 0U); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 3588 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3589 | if (region_space_ != nullptr) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 3590 | CHECK_EQ(region_space_->RevokeAllThreadLocalBuffers(), 0U); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 3591 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 3592 | } |
| 3593 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3594 | bool Heap::IsGCRequestPending() const { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3595 | return concurrent_gc_pending_.LoadRelaxed(); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3596 | } |
| 3597 | |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3598 | void Heap::RunFinalization(JNIEnv* env, uint64_t timeout) { |
| 3599 | env->CallStaticVoidMethod(WellKnownClasses::dalvik_system_VMRuntime, |
| 3600 | WellKnownClasses::dalvik_system_VMRuntime_runFinalization, |
| 3601 | static_cast<jlong>(timeout)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3602 | } |
| 3603 | |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3604 | void Heap::RegisterNativeAllocation(JNIEnv* env, size_t bytes) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3605 | Thread* self = ThreadForEnv(env); |
| 3606 | if (native_need_to_run_finalization_) { |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3607 | RunFinalization(env, kNativeAllocationFinalizeTimeout); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3608 | UpdateMaxNativeFootprint(); |
| 3609 | native_need_to_run_finalization_ = false; |
| 3610 | } |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3611 | // Total number of native bytes allocated. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 3612 | size_t new_native_bytes_allocated = native_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes); |
| 3613 | new_native_bytes_allocated += bytes; |
| 3614 | if (new_native_bytes_allocated > native_footprint_gc_watermark_) { |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 3615 | collector::GcType gc_type = HasZygoteSpace() ? collector::kGcTypePartial : |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 3616 | collector::kGcTypeFull; |
| 3617 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3618 | // The second watermark is higher than the gc watermark. If you hit this it means you are |
| 3619 | // allocating native objects faster than the GC can keep up with. |
Mathieu Chartier | 0848745 | 2014-09-02 16:21:01 -0700 | [diff] [blame] | 3620 | if (new_native_bytes_allocated > growth_limit_) { |
Mathieu Chartier | 89a201e | 2014-05-02 10:27:26 -0700 | [diff] [blame] | 3621 | if (WaitForGcToComplete(kGcCauseForNativeAlloc, self) != collector::kGcTypeNone) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3622 | // Just finished a GC, attempt to run finalizers. |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3623 | RunFinalization(env, kNativeAllocationFinalizeTimeout); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3624 | CHECK(!env->ExceptionCheck()); |
Lin Zang | 60e2716 | 2015-03-10 18:53:21 +0800 | [diff] [blame] | 3625 | // Native bytes allocated may be updated by finalization, refresh it. |
| 3626 | new_native_bytes_allocated = native_bytes_allocated_.LoadRelaxed(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3627 | } |
| 3628 | // If we still are over the watermark, attempt a GC for alloc and run finalizers. |
Mathieu Chartier | 0848745 | 2014-09-02 16:21:01 -0700 | [diff] [blame] | 3629 | if (new_native_bytes_allocated > growth_limit_) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 3630 | CollectGarbageInternal(gc_type, kGcCauseForNativeAlloc, false); |
Mathieu Chartier | b5de3bb | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 3631 | RunFinalization(env, kNativeAllocationFinalizeTimeout); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3632 | native_need_to_run_finalization_ = false; |
| 3633 | CHECK(!env->ExceptionCheck()); |
| 3634 | } |
| 3635 | // We have just run finalizers, update the native watermark since it is very likely that |
| 3636 | // finalizers released native managed allocations. |
| 3637 | UpdateMaxNativeFootprint(); |
| 3638 | } else if (!IsGCRequestPending()) { |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 3639 | if (IsGcConcurrent()) { |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 3640 | RequestConcurrentGC(self, true); // Request non-sticky type. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3641 | } else { |
Hiroshi Yamauchi | d20aba1 | 2014-04-11 15:31:09 -0700 | [diff] [blame] | 3642 | CollectGarbageInternal(gc_type, kGcCauseForNativeAlloc, false); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3648 | void Heap::RegisterNativeFree(JNIEnv* env, size_t bytes) { |
| 3649 | size_t expected_size; |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3650 | do { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 3651 | expected_size = native_bytes_allocated_.LoadRelaxed(); |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3652 | if (UNLIKELY(bytes > expected_size)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3653 | ScopedObjectAccess soa(env); |
| 3654 | env->ThrowNew(WellKnownClasses::java_lang_RuntimeException, |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3655 | StringPrintf("Attempted to free %zd native bytes with only %zd native bytes " |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3656 | "registered as allocated", bytes, expected_size).c_str()); |
| 3657 | break; |
| 3658 | } |
Mathieu Chartier | 8ec31f9 | 2014-09-03 10:30:11 -0700 | [diff] [blame] | 3659 | } while (!native_bytes_allocated_.CompareExchangeWeakRelaxed(expected_size, |
| 3660 | expected_size - bytes)); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3663 | size_t Heap::GetTotalMemory() const { |
Mathieu Chartier | dd162fb | 2014-08-06 17:06:33 -0700 | [diff] [blame] | 3664 | return std::max(max_allowed_footprint_, GetBytesAllocated()); |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 3665 | } |
| 3666 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 3667 | void Heap::AddModUnionTable(accounting::ModUnionTable* mod_union_table) { |
| 3668 | DCHECK(mod_union_table != nullptr); |
| 3669 | mod_union_tables_.Put(mod_union_table->GetSpace(), mod_union_table); |
| 3670 | } |
| 3671 | |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 3672 | void Heap::CheckPreconditionsForAllocObject(mirror::Class* c, size_t byte_count) { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 3673 | CHECK(c == nullptr || (c->IsClassClass() && byte_count >= sizeof(mirror::Class)) || |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3674 | (c->IsVariableSize() || c->GetObjectSize() == byte_count)); |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 3675 | CHECK_GE(byte_count, sizeof(mirror::Object)); |
| 3676 | } |
| 3677 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3678 | void Heap::AddRememberedSet(accounting::RememberedSet* remembered_set) { |
| 3679 | CHECK(remembered_set != nullptr); |
| 3680 | space::Space* space = remembered_set->GetSpace(); |
| 3681 | CHECK(space != nullptr); |
Mathieu Chartier | 8e4a96d | 2014-05-21 10:44:32 -0700 | [diff] [blame] | 3682 | CHECK(remembered_sets_.find(space) == remembered_sets_.end()) << space; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3683 | remembered_sets_.Put(space, remembered_set); |
Mathieu Chartier | 8e4a96d | 2014-05-21 10:44:32 -0700 | [diff] [blame] | 3684 | CHECK(remembered_sets_.find(space) != remembered_sets_.end()) << space; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | void Heap::RemoveRememberedSet(space::Space* space) { |
| 3688 | CHECK(space != nullptr); |
| 3689 | auto it = remembered_sets_.find(space); |
| 3690 | CHECK(it != remembered_sets_.end()); |
Mathieu Chartier | 5189e24 | 2014-07-24 11:11:05 -0700 | [diff] [blame] | 3691 | delete it->second; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 3692 | remembered_sets_.erase(it); |
| 3693 | CHECK(remembered_sets_.find(space) == remembered_sets_.end()); |
| 3694 | } |
| 3695 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 3696 | void Heap::ClearMarkedObjects() { |
| 3697 | // Clear all of the spaces' mark bitmaps. |
| 3698 | for (const auto& space : GetContinuousSpaces()) { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 3699 | accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 3700 | if (space->GetLiveBitmap() != mark_bitmap) { |
| 3701 | mark_bitmap->Clear(); |
| 3702 | } |
| 3703 | } |
| 3704 | // Clear the marked objects in the discontinous space object sets. |
| 3705 | for (const auto& space : GetDiscontinuousSpaces()) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 3706 | space->GetMarkBitmap()->Clear(); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 3707 | } |
| 3708 | } |
| 3709 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 3710 | void Heap::SetAllocationRecords(AllocRecordObjectMap* records) { |
| 3711 | allocation_records_.reset(records); |
| 3712 | } |
| 3713 | |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 3714 | void Heap::VisitAllocationRecords(RootVisitor* visitor) const { |
| 3715 | if (IsAllocTrackingEnabled()) { |
| 3716 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 3717 | if (IsAllocTrackingEnabled()) { |
| 3718 | GetAllocationRecords()->VisitRoots(visitor); |
| 3719 | } |
| 3720 | } |
| 3721 | } |
| 3722 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame^] | 3723 | void Heap::SweepAllocationRecords(IsMarkedVisitor* visitor) const { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 3724 | if (IsAllocTrackingEnabled()) { |
| 3725 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 3726 | if (IsAllocTrackingEnabled()) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame^] | 3727 | GetAllocationRecords()->SweepAllocationRecords(visitor); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | } |
| 3731 | |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 3732 | void Heap::AllowNewAllocationRecords() const { |
| 3733 | if (IsAllocTrackingEnabled()) { |
| 3734 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 3735 | if (IsAllocTrackingEnabled()) { |
| 3736 | GetAllocationRecords()->AllowNewAllocationRecords(); |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | void Heap::DisallowNewAllocationRecords() const { |
| 3742 | if (IsAllocTrackingEnabled()) { |
| 3743 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 3744 | if (IsAllocTrackingEnabled()) { |
| 3745 | GetAllocationRecords()->DisallowNewAllocationRecords(); |
| 3746 | } |
| 3747 | } |
| 3748 | } |
| 3749 | |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 3750 | // Based on debug malloc logic from libc/bionic/debug_stacktrace.cpp. |
| 3751 | class StackCrawlState { |
| 3752 | public: |
| 3753 | StackCrawlState(uintptr_t* frames, size_t max_depth, size_t skip_count) |
| 3754 | : frames_(frames), frame_count_(0), max_depth_(max_depth), skip_count_(skip_count) { |
| 3755 | } |
| 3756 | size_t GetFrameCount() const { |
| 3757 | return frame_count_; |
| 3758 | } |
| 3759 | static _Unwind_Reason_Code Callback(_Unwind_Context* context, void* arg) { |
| 3760 | auto* const state = reinterpret_cast<StackCrawlState*>(arg); |
| 3761 | const uintptr_t ip = _Unwind_GetIP(context); |
| 3762 | // The first stack frame is get_backtrace itself. Skip it. |
| 3763 | if (ip != 0 && state->skip_count_ > 0) { |
| 3764 | --state->skip_count_; |
| 3765 | return _URC_NO_REASON; |
| 3766 | } |
| 3767 | // ip may be off for ARM but it shouldn't matter since we only use it for hashing. |
| 3768 | state->frames_[state->frame_count_] = ip; |
| 3769 | state->frame_count_++; |
| 3770 | return state->frame_count_ >= state->max_depth_ ? _URC_END_OF_STACK : _URC_NO_REASON; |
| 3771 | } |
| 3772 | |
| 3773 | private: |
| 3774 | uintptr_t* const frames_; |
| 3775 | size_t frame_count_; |
| 3776 | const size_t max_depth_; |
| 3777 | size_t skip_count_; |
| 3778 | }; |
| 3779 | |
| 3780 | static size_t get_backtrace(uintptr_t* frames, size_t max_depth) { |
| 3781 | StackCrawlState state(frames, max_depth, 0u); |
| 3782 | _Unwind_Backtrace(&StackCrawlState::Callback, &state); |
| 3783 | return state.GetFrameCount(); |
| 3784 | } |
| 3785 | |
| 3786 | void Heap::CheckGcStressMode(Thread* self, mirror::Object** obj) { |
| 3787 | auto* const runtime = Runtime::Current(); |
| 3788 | if (gc_stress_mode_ && runtime->GetClassLinker()->IsInitialized() && |
| 3789 | !runtime->IsActiveTransaction() && mirror::Class::HasJavaLangClass()) { |
| 3790 | // Check if we should GC. |
| 3791 | bool new_backtrace = false; |
| 3792 | { |
| 3793 | static constexpr size_t kMaxFrames = 16u; |
| 3794 | uintptr_t backtrace[kMaxFrames]; |
| 3795 | const size_t frames = get_backtrace(backtrace, kMaxFrames); |
| 3796 | uint64_t hash = 0; |
| 3797 | for (size_t i = 0; i < frames; ++i) { |
| 3798 | hash = hash * 2654435761 + backtrace[i]; |
| 3799 | hash += (hash >> 13) ^ (hash << 6); |
| 3800 | } |
| 3801 | MutexLock mu(self, *backtrace_lock_); |
| 3802 | new_backtrace = seen_backtraces_.find(hash) == seen_backtraces_.end(); |
| 3803 | if (new_backtrace) { |
| 3804 | seen_backtraces_.insert(hash); |
| 3805 | } |
| 3806 | } |
| 3807 | if (new_backtrace) { |
| 3808 | StackHandleScope<1> hs(self); |
| 3809 | auto h = hs.NewHandleWrapper(obj); |
| 3810 | CollectGarbage(false); |
| 3811 | unique_backtrace_count_.FetchAndAddSequentiallyConsistent(1); |
| 3812 | } else { |
| 3813 | seen_backtrace_count_.FetchAndAddSequentiallyConsistent(1); |
| 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3818 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 3819 | } // namespace art |