Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_GC_HEAP_INL_H_ |
| 18 | #define ART_RUNTIME_GC_HEAP_INL_H_ |
| 19 | |
| 20 | #include "heap.h" |
| 21 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | #include "base/time_utils.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 23 | #include "gc/accounting/card_table-inl.h" |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 24 | #include "gc/allocation_record.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 25 | #include "gc/collector/semi_space.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 26 | #include "gc/space/bump_pointer_space-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 27 | #include "gc/space/dlmalloc_space-inl.h" |
| 28 | #include "gc/space/large_object_space.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 29 | #include "gc/space/region_space-inl.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 30 | #include "gc/space/rosalloc_space-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 31 | #include "runtime.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 32 | #include "handle_scope-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 33 | #include "thread-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 34 | #include "utils.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 35 | #include "verify_object-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | namespace gc { |
| 39 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 40 | template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 41 | inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, |
| 42 | mirror::Class* klass, |
| 43 | size_t byte_count, |
| 44 | AllocatorType allocator, |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 45 | const PreFenceVisitor& pre_fence_visitor) { |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 46 | if (kIsDebugBuild) { |
| 47 | CheckPreconditionsForAllocObject(klass, byte_count); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 48 | // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are |
| 49 | // done in the runnable state where suspension is expected. |
| 50 | CHECK_EQ(self->GetState(), kRunnable); |
| 51 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 52 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 53 | // Need to check that we arent the large object allocator since the large object allocation code |
| 54 | // path this function. If we didn't check we would have an infinite loop. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 55 | mirror::Object* obj; |
Mathieu Chartier | 446f9ee | 2014-12-01 15:00:27 -0800 | [diff] [blame] | 56 | if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) { |
| 57 | obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count, |
| 58 | pre_fence_visitor); |
| 59 | if (obj != nullptr) { |
| 60 | return obj; |
| 61 | } else { |
| 62 | // There should be an OOM exception, since we are retrying, clear it. |
| 63 | self->ClearException(); |
| 64 | } |
| 65 | // If the large object allocation failed, try to use the normal spaces (main space, |
| 66 | // non moving space). This can happen if there is significant virtual address space |
| 67 | // fragmentation. |
| 68 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 69 | // bytes allocated for the (individual) object. |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 70 | size_t bytes_allocated; |
| 71 | size_t usable_size; |
| 72 | size_t new_num_bytes_allocated = 0; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 73 | if (allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) { |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 74 | byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment); |
| 75 | } |
| 76 | // If we have a thread local allocation we don't need to update bytes allocated. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 77 | if ((allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) && |
| 78 | byte_count <= self->TlabSize()) { |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 79 | obj = self->AllocTlab(byte_count); |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 80 | DCHECK(obj != nullptr) << "AllocTlab can't fail"; |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 81 | obj->SetClass(klass); |
| 82 | if (kUseBakerOrBrooksReadBarrier) { |
| 83 | if (kUseBrooksReadBarrier) { |
| 84 | obj->SetReadBarrierPointer(obj); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 85 | } |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 86 | obj->AssertReadBarrierPointer(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 87 | } |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 88 | bytes_allocated = byte_count; |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 89 | usable_size = bytes_allocated; |
| 90 | pre_fence_visitor(obj, usable_size); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 91 | QuasiAtomic::ThreadFenceForConstructor(); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 92 | } else if (!kInstrumented && allocator == kAllocatorTypeRosAlloc && |
| 93 | (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) && |
| 94 | LIKELY(obj != nullptr)) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 95 | DCHECK(!is_running_on_memory_tool_); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 96 | obj->SetClass(klass); |
| 97 | if (kUseBakerOrBrooksReadBarrier) { |
| 98 | if (kUseBrooksReadBarrier) { |
| 99 | obj->SetReadBarrierPointer(obj); |
| 100 | } |
| 101 | obj->AssertReadBarrierPointer(); |
| 102 | } |
| 103 | usable_size = bytes_allocated; |
| 104 | pre_fence_visitor(obj, usable_size); |
| 105 | QuasiAtomic::ThreadFenceForConstructor(); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 106 | } else { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 107 | // bytes allocated that takes bulk thread-local buffer allocations into account. |
| 108 | size_t bytes_tl_bulk_allocated = 0; |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 109 | obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 110 | &usable_size, &bytes_tl_bulk_allocated); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 111 | if (UNLIKELY(obj == nullptr)) { |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame^] | 112 | // AllocateInternalWithGc can cause thread suspension, if someone instruments the entrypoints |
| 113 | // or changes the allocator in a suspend point here, we need to retry the allocation. |
| 114 | obj = AllocateInternalWithGc(self, |
| 115 | allocator, |
| 116 | kInstrumented, |
| 117 | byte_count, |
| 118 | &bytes_allocated, |
| 119 | &usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 120 | &bytes_tl_bulk_allocated, &klass); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 121 | if (obj == nullptr) { |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame^] | 122 | // The only way that we can get a null return if there is no pending exception is if the |
| 123 | // allocator or instrumentation changed. |
| 124 | if (!self->IsExceptionPending()) { |
| 125 | // AllocObject will pick up the new allocator type, and instrumented as true is the safe |
| 126 | // default. |
| 127 | return AllocObject</*kInstrumented*/true>(self, |
| 128 | klass, |
| 129 | byte_count, |
| 130 | pre_fence_visitor); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 131 | } |
| 132 | return nullptr; |
| 133 | } |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 134 | } |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 135 | DCHECK_GT(bytes_allocated, 0u); |
| 136 | DCHECK_GT(usable_size, 0u); |
| 137 | obj->SetClass(klass); |
| 138 | if (kUseBakerOrBrooksReadBarrier) { |
| 139 | if (kUseBrooksReadBarrier) { |
| 140 | obj->SetReadBarrierPointer(obj); |
| 141 | } |
| 142 | obj->AssertReadBarrierPointer(); |
| 143 | } |
| 144 | if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) { |
| 145 | // (Note this if statement will be constant folded away for the |
| 146 | // fast-path quick entry points.) Because SetClass() has no write |
| 147 | // barrier, if a non-moving space allocation, we need a write |
| 148 | // barrier as the class pointer may point to the bump pointer |
| 149 | // space (where the class pointer is an "old-to-young" reference, |
| 150 | // though rare) under the GSS collector with the remembered set |
| 151 | // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc |
| 152 | // cases because we don't directly allocate into the main alloc |
| 153 | // space (besides promotions) under the SS/GSS collector. |
| 154 | WriteBarrierField(obj, mirror::Object::ClassOffset(), klass); |
| 155 | } |
| 156 | pre_fence_visitor(obj, usable_size); |
Hans Boehm | b0171b9 | 2016-01-28 17:19:15 -0800 | [diff] [blame] | 157 | QuasiAtomic::ThreadFenceForConstructor(); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 158 | new_num_bytes_allocated = static_cast<size_t>( |
Hans Boehm | b0171b9 | 2016-01-28 17:19:15 -0800 | [diff] [blame] | 159 | num_bytes_allocated_.FetchAndAddRelaxed(bytes_tl_bulk_allocated)) + bytes_tl_bulk_allocated; |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 160 | } |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 161 | if (kIsDebugBuild && Runtime::Current()->IsStarted()) { |
| 162 | CHECK_LE(obj->SizeOf(), usable_size); |
| 163 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 164 | // TODO: Deprecate. |
| 165 | if (kInstrumented) { |
| 166 | if (Runtime::Current()->HasStatsEnabled()) { |
| 167 | RuntimeStats* thread_stats = self->GetStats(); |
| 168 | ++thread_stats->allocated_objects; |
| 169 | thread_stats->allocated_bytes += bytes_allocated; |
| 170 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 171 | ++global_stats->allocated_objects; |
| 172 | global_stats->allocated_bytes += bytes_allocated; |
| 173 | } |
| 174 | } else { |
| 175 | DCHECK(!Runtime::Current()->HasStatsEnabled()); |
| 176 | } |
| 177 | if (AllocatorHasAllocationStack(allocator)) { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 178 | PushOnAllocationStack(self, &obj); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 179 | } |
| 180 | if (kInstrumented) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 181 | if (IsAllocTrackingEnabled()) { |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 182 | // Use obj->GetClass() instead of klass, because PushOnAllocationStack() could move klass |
| 183 | AllocRecordObjectMap::RecordAllocation(self, obj, obj->GetClass(), bytes_allocated); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 184 | } |
| 185 | } else { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 186 | DCHECK(!IsAllocTrackingEnabled()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 187 | } |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 188 | if (kInstrumented) { |
| 189 | if (gc_stress_mode_) { |
| 190 | CheckGcStressMode(self, &obj); |
| 191 | } |
| 192 | } else { |
| 193 | DCHECK(!gc_stress_mode_); |
| 194 | } |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 195 | // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 196 | // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be |
| 197 | // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since |
| 198 | // the allocator_type should be constant propagated. |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 199 | if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) { |
Mathieu Chartier | f517f1a | 2014-03-06 15:52:27 -0800 | [diff] [blame] | 200 | CheckConcurrentGC(self, new_num_bytes_allocated, &obj); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 201 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 202 | VerifyObject(obj); |
| 203 | self->VerifyStack(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 204 | return obj; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 207 | // The size of a thread-local allocation stack in the number of references. |
| 208 | static constexpr size_t kThreadLocalAllocationStackSize = 128; |
| 209 | |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 210 | inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 211 | if (kUseThreadLocalAllocationStack) { |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 212 | if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) { |
| 213 | PushOnThreadLocalAllocationStackWithInternalGC(self, obj); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 214 | } |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 215 | } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) { |
| 216 | PushOnAllocationStackWithInternalGC(self, obj); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 220 | template <bool kInstrumented, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 221 | inline mirror::Object* Heap::AllocLargeObject(Thread* self, |
| 222 | mirror::Class** klass, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 223 | size_t byte_count, |
| 224 | const PreFenceVisitor& pre_fence_visitor) { |
Mathieu Chartier | 446f9ee | 2014-12-01 15:00:27 -0800 | [diff] [blame] | 225 | // Save and restore the class in case it moves. |
| 226 | StackHandleScope<1> hs(self); |
| 227 | auto klass_wrapper = hs.NewHandleWrapper(klass); |
| 228 | return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count, |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 229 | kAllocatorTypeLOS, |
| 230 | pre_fence_visitor); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | template <const bool kInstrumented, const bool kGrow> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 234 | inline mirror::Object* Heap::TryToAllocate(Thread* self, |
| 235 | AllocatorType allocator_type, |
| 236 | size_t alloc_size, |
| 237 | size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 238 | size_t* usable_size, |
| 239 | size_t* bytes_tl_bulk_allocated) { |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 240 | if (allocator_type != kAllocatorTypeTLAB && |
| 241 | allocator_type != kAllocatorTypeRegionTLAB && |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 242 | allocator_type != kAllocatorTypeRosAlloc && |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 243 | UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 244 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 245 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 246 | mirror::Object* ret; |
| 247 | switch (allocator_type) { |
| 248 | case kAllocatorTypeBumpPointer: { |
| 249 | DCHECK(bump_pointer_space_ != nullptr); |
| 250 | alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment); |
| 251 | ret = bump_pointer_space_->AllocNonvirtual(alloc_size); |
| 252 | if (LIKELY(ret != nullptr)) { |
| 253 | *bytes_allocated = alloc_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 254 | *usable_size = alloc_size; |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 255 | *bytes_tl_bulk_allocated = alloc_size; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 256 | } |
| 257 | break; |
| 258 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 259 | case kAllocatorTypeRosAlloc: { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 260 | if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) { |
| 261 | // If running on valgrind or asan, we should be using the instrumented path. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 262 | size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size); |
| 263 | if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, |
| 264 | max_bytes_tl_bulk_allocated))) { |
| 265 | return nullptr; |
| 266 | } |
| 267 | ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size, |
| 268 | bytes_tl_bulk_allocated); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 269 | } else { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 270 | DCHECK(!is_running_on_memory_tool_); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 271 | size_t max_bytes_tl_bulk_allocated = |
| 272 | rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size); |
| 273 | if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, |
| 274 | max_bytes_tl_bulk_allocated))) { |
| 275 | return nullptr; |
| 276 | } |
| 277 | if (!kInstrumented) { |
| 278 | DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size)); |
| 279 | } |
| 280 | ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size, |
| 281 | bytes_tl_bulk_allocated); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 282 | } |
| 283 | break; |
| 284 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 285 | case kAllocatorTypeDlMalloc: { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 286 | if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 287 | // If running on valgrind, we should be using the instrumented path. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 288 | ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size, |
| 289 | bytes_tl_bulk_allocated); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 290 | } else { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 291 | DCHECK(!is_running_on_memory_tool_); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 292 | ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size, |
| 293 | bytes_tl_bulk_allocated); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 294 | } |
| 295 | break; |
| 296 | } |
| 297 | case kAllocatorTypeNonMoving: { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 298 | ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size, |
| 299 | bytes_tl_bulk_allocated); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 300 | break; |
| 301 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 302 | case kAllocatorTypeLOS: { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 303 | ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size, |
| 304 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | 95a659f | 2013-11-22 14:43:45 -0800 | [diff] [blame] | 305 | // Note that the bump pointer spaces aren't necessarily next to |
| 306 | // the other continuous spaces like the non-moving alloc space or |
| 307 | // the zygote space. |
| 308 | DCHECK(ret == nullptr || large_object_space_->Contains(ret)); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 309 | break; |
| 310 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 311 | case kAllocatorTypeTLAB: { |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 312 | DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 313 | if (UNLIKELY(self->TlabSize() < alloc_size)) { |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 314 | const size_t new_tlab_size = alloc_size + kDefaultTLABSize; |
| 315 | if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 316 | return nullptr; |
| 317 | } |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 318 | // Try allocating a new thread local buffer, if the allocaiton fails the space must be |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 319 | // full so return null. |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 320 | if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) { |
| 321 | return nullptr; |
| 322 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 323 | *bytes_tl_bulk_allocated = new_tlab_size; |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 324 | } else { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 325 | *bytes_tl_bulk_allocated = 0; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 326 | } |
| 327 | // The allocation can't fail. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 328 | ret = self->AllocTlab(alloc_size); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 329 | DCHECK(ret != nullptr); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 330 | *bytes_allocated = alloc_size; |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 331 | *usable_size = alloc_size; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 332 | break; |
| 333 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 334 | case kAllocatorTypeRegion: { |
| 335 | DCHECK(region_space_ != nullptr); |
| 336 | alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 337 | ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size, |
| 338 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | case kAllocatorTypeRegionTLAB: { |
| 342 | DCHECK(region_space_ != nullptr); |
| 343 | DCHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment); |
| 344 | if (UNLIKELY(self->TlabSize() < alloc_size)) { |
| 345 | if (space::RegionSpace::kRegionSize >= alloc_size) { |
| 346 | // Non-large. Check OOME for a tlab. |
| 347 | if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, space::RegionSpace::kRegionSize))) { |
| 348 | // Try to allocate a tlab. |
| 349 | if (!region_space_->AllocNewTlab(self)) { |
| 350 | // Failed to allocate a tlab. Try non-tlab. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 351 | ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size, |
| 352 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 353 | return ret; |
| 354 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 355 | *bytes_tl_bulk_allocated = space::RegionSpace::kRegionSize; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 356 | // Fall-through. |
| 357 | } else { |
| 358 | // Check OOME for a non-tlab allocation. |
| 359 | if (!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size)) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 360 | ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size, |
| 361 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 362 | return ret; |
| 363 | } else { |
| 364 | // Neither tlab or non-tlab works. Give up. |
| 365 | return nullptr; |
| 366 | } |
| 367 | } |
| 368 | } else { |
| 369 | // Large. Check OOME. |
| 370 | if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 371 | ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size, |
| 372 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 373 | return ret; |
| 374 | } else { |
| 375 | return nullptr; |
| 376 | } |
| 377 | } |
| 378 | } else { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 379 | *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 380 | } |
| 381 | // The allocation can't fail. |
| 382 | ret = self->AllocTlab(alloc_size); |
| 383 | DCHECK(ret != nullptr); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 384 | *bytes_allocated = alloc_size; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 385 | *usable_size = alloc_size; |
| 386 | break; |
| 387 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 388 | default: { |
| 389 | LOG(FATAL) << "Invalid allocator type"; |
| 390 | ret = nullptr; |
| 391 | } |
| 392 | } |
| 393 | return ret; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 396 | inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 397 | // We need to have a zygote space or else our newly allocated large object can end up in the |
| 398 | // Zygote resulting in it being prematurely freed. |
| 399 | // We can only do this for primitive objects since large objects will not be within the card table |
| 400 | // range. This also means that we rely on SetClass not dirtying the object's card. |
Jeff Hao | 13e0091 | 2015-06-22 15:14:49 -0700 | [diff] [blame] | 401 | return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass()); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 404 | template <bool kGrow> |
| 405 | inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 406 | size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 407 | if (UNLIKELY(new_footprint > max_allowed_footprint_)) { |
| 408 | if (UNLIKELY(new_footprint > growth_limit_)) { |
| 409 | return true; |
| 410 | } |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 411 | if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 412 | if (!kGrow) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 413 | return true; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 414 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 415 | // TODO: Grow for allocation is racy, fix it. |
| 416 | VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to " |
| 417 | << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation"; |
| 418 | max_allowed_footprint_ = new_footprint; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 424 | inline void Heap::CheckConcurrentGC(Thread* self, |
| 425 | size_t new_num_bytes_allocated, |
Mathieu Chartier | f517f1a | 2014-03-06 15:52:27 -0800 | [diff] [blame] | 426 | mirror::Object** obj) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 427 | if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) { |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 428 | RequestConcurrentGCAndSaveObject(self, false, obj); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | } // namespace gc |
| 433 | } // namespace art |
| 434 | |
| 435 | #endif // ART_RUNTIME_GC_HEAP_INL_H_ |