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