blob: 7d3fd2d23a32398c3461fb18fdc32b446f49910c [file] [log] [blame]
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -07001/*
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 Yamauchi38e68e92014-03-07 13:59:08 -080023#include "gc/accounting/card_table-inl.h"
24#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070025#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070026#include "gc/space/dlmalloc_space-inl.h"
27#include "gc/space/large_object_space.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070028#include "gc/space/rosalloc_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070029#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030#include "handle_scope-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070031#include "thread.h"
32#include "thread-inl.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080033#include "verify_object-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070034
35namespace art {
36namespace gc {
37
Mathieu Chartier692fafd2013-11-29 17:24:40 -080038template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartier1febddf2013-11-20 12:33:14 -080039inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, mirror::Class* klass,
40 size_t byte_count, AllocatorType allocator,
41 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080042 if (kIsDebugBuild) {
43 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070044 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
45 // done in the runnable state where suspension is expected.
46 CHECK_EQ(self->GetState(), kRunnable);
47 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080048 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080049 // Need to check that we arent the large object allocator since the large object allocation code
50 // path this function. If we didn't check we would have an infinite loop.
Mathieu Chartier692fafd2013-11-29 17:24:40 -080051 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080052 return AllocLargeObject<kInstrumented, PreFenceVisitor>(self, klass, byte_count,
53 pre_fence_visitor);
54 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080055 mirror::Object* obj;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080056 AllocationTimer alloc_timer(this, &obj);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070057 size_t bytes_allocated;
58 size_t usable_size;
59 size_t new_num_bytes_allocated = 0;
60 if (allocator == kAllocatorTypeTLAB) {
61 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
62 }
63 // If we have a thread local allocation we don't need to update bytes allocated.
64 if (allocator == kAllocatorTypeTLAB && byte_count <= self->TlabSize()) {
65 obj = self->AllocTlab(byte_count);
Mathieu Chartier59fe7112014-07-14 10:16:05 -070066 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070067 obj->SetClass(klass);
68 if (kUseBakerOrBrooksReadBarrier) {
69 if (kUseBrooksReadBarrier) {
70 obj->SetReadBarrierPointer(obj);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080071 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070072 obj->AssertReadBarrierPointer();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080073 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070074 bytes_allocated = byte_count;
Mathieu Chartier59fe7112014-07-14 10:16:05 -070075 usable_size = bytes_allocated;
76 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070077 QuasiAtomic::ThreadFenceForConstructor();
78 } else {
79 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
80 &usable_size);
81 if (UNLIKELY(obj == nullptr)) {
82 bool is_current_allocator = allocator == GetCurrentAllocator();
83 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &usable_size,
84 &klass);
85 if (obj == nullptr) {
86 bool after_is_current_allocator = allocator == GetCurrentAllocator();
87 if (is_current_allocator && !after_is_current_allocator) {
88 // If the allocator changed, we need to restart the allocation.
89 return AllocObject<kInstrumented>(self, klass, byte_count, pre_fence_visitor);
90 }
91 return nullptr;
92 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070093 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070094 DCHECK_GT(bytes_allocated, 0u);
95 DCHECK_GT(usable_size, 0u);
96 obj->SetClass(klass);
97 if (kUseBakerOrBrooksReadBarrier) {
98 if (kUseBrooksReadBarrier) {
99 obj->SetReadBarrierPointer(obj);
100 }
101 obj->AssertReadBarrierPointer();
102 }
103 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
104 // (Note this if statement will be constant folded away for the
105 // fast-path quick entry points.) Because SetClass() has no write
106 // barrier, if a non-moving space allocation, we need a write
107 // barrier as the class pointer may point to the bump pointer
108 // space (where the class pointer is an "old-to-young" reference,
109 // though rare) under the GSS collector with the remembered set
110 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
111 // cases because we don't directly allocate into the main alloc
112 // space (besides promotions) under the SS/GSS collector.
113 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
114 }
115 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700116 new_num_bytes_allocated =
117 static_cast<size_t>(num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_allocated))
118 + bytes_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800119 }
Mathieu Chartier59fe7112014-07-14 10:16:05 -0700120 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
121 CHECK_LE(obj->SizeOf(), usable_size);
122 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800123 // TODO: Deprecate.
124 if (kInstrumented) {
125 if (Runtime::Current()->HasStatsEnabled()) {
126 RuntimeStats* thread_stats = self->GetStats();
127 ++thread_stats->allocated_objects;
128 thread_stats->allocated_bytes += bytes_allocated;
129 RuntimeStats* global_stats = Runtime::Current()->GetStats();
130 ++global_stats->allocated_objects;
131 global_stats->allocated_bytes += bytes_allocated;
132 }
133 } else {
134 DCHECK(!Runtime::Current()->HasStatsEnabled());
135 }
136 if (AllocatorHasAllocationStack(allocator)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700137 PushOnAllocationStack(self, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800138 }
139 if (kInstrumented) {
140 if (Dbg::IsAllocTrackingEnabled()) {
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800141 Dbg::RecordAllocation(klass, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800142 }
143 } else {
144 DCHECK(!Dbg::IsAllocTrackingEnabled());
145 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700146 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800147 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
148 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
149 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700150 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800151 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800152 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800153 VerifyObject(obj);
154 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800155 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700156}
157
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800158// The size of a thread-local allocation stack in the number of references.
159static constexpr size_t kThreadLocalAllocationStackSize = 128;
160
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700161inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800162 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700163 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) {
164 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800165 }
Mathieu Chartierc1790162014-05-23 10:54:50 -0700166 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) {
167 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800168 }
169}
170
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800171template <bool kInstrumented, typename PreFenceVisitor>
172inline mirror::Object* Heap::AllocLargeObject(Thread* self, mirror::Class* klass,
173 size_t byte_count,
174 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800175 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, klass, byte_count,
176 kAllocatorTypeLOS,
177 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800178}
179
180template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800181inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type,
Ian Rogers6fac4472014-02-25 17:01:10 -0800182 size_t alloc_size, size_t* bytes_allocated,
183 size_t* usable_size) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700184 if (allocator_type != kAllocatorTypeTLAB &&
185 UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800186 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700187 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800188 mirror::Object* ret;
189 switch (allocator_type) {
190 case kAllocatorTypeBumpPointer: {
191 DCHECK(bump_pointer_space_ != nullptr);
192 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
193 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
194 if (LIKELY(ret != nullptr)) {
195 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800196 *usable_size = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800197 }
198 break;
199 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800200 case kAllocatorTypeRosAlloc: {
201 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
202 // If running on valgrind, we should be using the instrumented path.
Ian Rogers6fac4472014-02-25 17:01:10 -0800203 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800204 } else {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800205 DCHECK(!running_on_valgrind_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800206 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800207 }
208 break;
209 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800210 case kAllocatorTypeDlMalloc: {
211 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
212 // If running on valgrind, we should be using the instrumented path.
Ian Rogers6fac4472014-02-25 17:01:10 -0800213 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800214 } else {
215 DCHECK(!running_on_valgrind_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800216 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800217 }
218 break;
219 }
220 case kAllocatorTypeNonMoving: {
Ian Rogers6fac4472014-02-25 17:01:10 -0800221 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800222 break;
223 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800224 case kAllocatorTypeLOS: {
Ian Rogers6fac4472014-02-25 17:01:10 -0800225 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800226 // Note that the bump pointer spaces aren't necessarily next to
227 // the other continuous spaces like the non-moving alloc space or
228 // the zygote space.
229 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800230 break;
231 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800232 case kAllocatorTypeTLAB: {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700233 DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800234 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700235 const size_t new_tlab_size = alloc_size + kDefaultTLABSize;
236 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800237 return nullptr;
238 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700239 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
240 // full so return nullptr.
241 if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) {
242 return nullptr;
243 }
244 *bytes_allocated = new_tlab_size;
245 } else {
246 *bytes_allocated = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800247 }
248 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800249 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800250 DCHECK(ret != nullptr);
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700251 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800252 break;
253 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800254 default: {
255 LOG(FATAL) << "Invalid allocator type";
256 ret = nullptr;
257 }
258 }
259 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700260}
261
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700262inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
263 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) {
264 if (kMeasureAllocationTime) {
265 allocation_start_time_ = NanoTime() / kTimeAdjust;
266 }
267}
268
269inline Heap::AllocationTimer::~AllocationTimer() {
270 if (kMeasureAllocationTime) {
271 mirror::Object* allocated_obj = *allocated_obj_ptr_;
272 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800273 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700274 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700275 heap_->total_allocation_time_.FetchAndAddSequentiallyConsistent(allocation_end_time - allocation_start_time_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700276 }
277 }
278};
279
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800280inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700281 // We need to have a zygote space or else our newly allocated large object can end up in the
282 // Zygote resulting in it being prematurely freed.
283 // We can only do this for primitive objects since large objects will not be within the card table
284 // range. This also means that we rely on SetClass not dirtying the object's card.
Mathieu Chartierbd0a6532014-02-27 11:14:21 -0800285 return byte_count >= large_object_threshold_ && c->IsPrimitiveArray();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700286}
287
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800288template <bool kGrow>
289inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700290 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700291 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
292 if (UNLIKELY(new_footprint > growth_limit_)) {
293 return true;
294 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700295 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800296 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700297 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700298 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800299 // TODO: Grow for allocation is racy, fix it.
300 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
301 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
302 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700303 }
304 }
305 return false;
306}
307
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800308inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800309 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700310 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700311 RequestConcurrentGCAndSaveObject(self, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700312 }
313}
314
315} // namespace gc
316} // namespace art
317
318#endif // ART_RUNTIME_GC_HEAP_INL_H_