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