blob: a06f272b55bf6dd5d023edcf705c71c164b8a7db [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 Chartierc645f1d2014-03-06 18:11:53 -080030#include "sirt_ref-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);
44 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070045 // 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 DCHECK_EQ(self->GetState(), kRunnable);
48 self->AssertThreadSuspensionIsAllowable();
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);
Ian Rogers6fac4472014-02-25 17:01:10 -080057 size_t bytes_allocated, usable_size;
58 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
59 &usable_size);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080060 if (UNLIKELY(obj == nullptr)) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080061 bool is_current_allocator = allocator == GetCurrentAllocator();
Ian Rogers6fac4472014-02-25 17:01:10 -080062 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &usable_size,
63 &klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080064 if (obj == nullptr) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080065 bool after_is_current_allocator = allocator == GetCurrentAllocator();
66 if (is_current_allocator && !after_is_current_allocator) {
67 // If the allocator changed, we need to restart the allocation.
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -070068 return AllocObject<kInstrumented>(self, klass, byte_count, pre_fence_visitor);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080069 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080070 return nullptr;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080071 }
72 }
Ian Rogers6fac4472014-02-25 17:01:10 -080073 DCHECK_GT(bytes_allocated, 0u);
74 DCHECK_GT(usable_size, 0u);
Mathieu Chartier1febddf2013-11-20 12:33:14 -080075 obj->SetClass(klass);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070076 if (kUseBakerOrBrooksReadBarrier) {
77 if (kUseBrooksReadBarrier) {
78 obj->SetReadBarrierPointer(obj);
79 }
80 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -080081 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080082 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
83 // (Note this if statement will be constant folded away for the
84 // fast-path quick entry points.) Because SetClass() has no write
85 // barrier, if a non-moving space allocation, we need a write
86 // barrier as the class pointer may point to the bump pointer
87 // space (where the class pointer is an "old-to-young" reference,
88 // though rare) under the GSS collector with the remembered set
89 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
90 // cases because we don't directly allocate into the main alloc
91 // space (besides promotions) under the SS/GSS collector.
92 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
93 }
Ian Rogers6fac4472014-02-25 17:01:10 -080094 pre_fence_visitor(obj, usable_size);
Ian Rogersa55cf412014-02-27 00:31:26 -080095 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
Ian Rogers6fac4472014-02-25 17:01:10 -080096 CHECK_LE(obj->SizeOf(), usable_size);
97 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080098 const size_t new_num_bytes_allocated =
Ian Rogersb122a4b2013-11-19 18:00:50 -080099 static_cast<size_t>(num_bytes_allocated_.FetchAndAdd(bytes_allocated)) + bytes_allocated;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800100 // TODO: Deprecate.
101 if (kInstrumented) {
102 if (Runtime::Current()->HasStatsEnabled()) {
103 RuntimeStats* thread_stats = self->GetStats();
104 ++thread_stats->allocated_objects;
105 thread_stats->allocated_bytes += bytes_allocated;
106 RuntimeStats* global_stats = Runtime::Current()->GetStats();
107 ++global_stats->allocated_objects;
108 global_stats->allocated_bytes += bytes_allocated;
109 }
110 } else {
111 DCHECK(!Runtime::Current()->HasStatsEnabled());
112 }
113 if (AllocatorHasAllocationStack(allocator)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700114 PushOnAllocationStack(self, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800115 }
116 if (kInstrumented) {
117 if (Dbg::IsAllocTrackingEnabled()) {
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800118 Dbg::RecordAllocation(klass, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800119 }
120 } else {
121 DCHECK(!Dbg::IsAllocTrackingEnabled());
122 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700123 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800124 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
125 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
126 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700127 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800128 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800129 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800130 VerifyObject(obj);
131 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800132 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700133}
134
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800135// The size of a thread-local allocation stack in the number of references.
136static constexpr size_t kThreadLocalAllocationStackSize = 128;
137
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700138inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800139 if (kUseThreadLocalAllocationStack) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700140 bool success = self->PushOnThreadLocalAllocationStack(*obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800141 if (UNLIKELY(!success)) {
142 // Slow path. Allocate a new thread-local allocation stack.
143 mirror::Object** start_address;
144 mirror::Object** end_address;
145 while (!allocation_stack_->AtomicBumpBack(kThreadLocalAllocationStackSize,
146 &start_address, &end_address)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700147 // Disable verify object in SirtRef as obj isn't on the alloc stack yet.
148 SirtRefNoVerify<mirror::Object> ref(self, *obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800149 CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false);
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700150 *obj = ref.get();
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800151 }
152 self->SetThreadLocalAllocationStack(start_address, end_address);
153 // Retry on the new thread-local allocation stack.
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700154 success = self->PushOnThreadLocalAllocationStack(*obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800155 // Must succeed.
156 CHECK(success);
157 }
158 } else {
159 // This is safe to do since the GC will never free objects which are neither in the allocation
160 // stack or the live bitmap.
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700161 while (!allocation_stack_->AtomicPushBack(*obj)) {
162 // Disable verify object in SirtRef as obj isn't on the alloc stack yet.
163 SirtRefNoVerify<mirror::Object> ref(self, *obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800164 CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false);
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700165 *obj = ref.get();
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800166 }
167 }
168}
169
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800170template <bool kInstrumented, typename PreFenceVisitor>
171inline mirror::Object* Heap::AllocLargeObject(Thread* self, mirror::Class* klass,
172 size_t byte_count,
173 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800174 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, klass, byte_count,
175 kAllocatorTypeLOS,
176 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800177}
178
179template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800180inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type,
Ian Rogers6fac4472014-02-25 17:01:10 -0800181 size_t alloc_size, size_t* bytes_allocated,
182 size_t* usable_size) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800183 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800184 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700185 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800186 mirror::Object* ret;
187 switch (allocator_type) {
188 case kAllocatorTypeBumpPointer: {
189 DCHECK(bump_pointer_space_ != nullptr);
190 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
191 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
192 if (LIKELY(ret != nullptr)) {
193 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800194 *usable_size = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800195 }
196 break;
197 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800198 case kAllocatorTypeRosAlloc: {
199 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
200 // If running on valgrind, we should be using the instrumented path.
Ian Rogers6fac4472014-02-25 17:01:10 -0800201 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800202 } else {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800203 DCHECK(!running_on_valgrind_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800204 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800205 }
206 break;
207 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800208 case kAllocatorTypeDlMalloc: {
209 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
210 // If running on valgrind, we should be using the instrumented path.
Ian Rogers6fac4472014-02-25 17:01:10 -0800211 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800212 } else {
213 DCHECK(!running_on_valgrind_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800214 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800215 }
216 break;
217 }
218 case kAllocatorTypeNonMoving: {
Ian Rogers6fac4472014-02-25 17:01:10 -0800219 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800220 break;
221 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800222 case kAllocatorTypeLOS: {
Ian Rogers6fac4472014-02-25 17:01:10 -0800223 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800224 // Note that the bump pointer spaces aren't necessarily next to
225 // the other continuous spaces like the non-moving alloc space or
226 // the zygote space.
227 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800228 break;
229 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800230 case kAllocatorTypeTLAB: {
231 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800232 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800233 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
234 // full so return nullptr.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800235 if (!bump_pointer_space_->AllocNewTlab(self, alloc_size + kDefaultTLABSize)) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800236 return nullptr;
237 }
238 }
239 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800240 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800241 DCHECK(ret != nullptr);
242 *bytes_allocated = alloc_size;
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700243 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800244 break;
245 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800246 default: {
247 LOG(FATAL) << "Invalid allocator type";
248 ret = nullptr;
249 }
250 }
251 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700252}
253
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700254inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
255 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) {
256 if (kMeasureAllocationTime) {
257 allocation_start_time_ = NanoTime() / kTimeAdjust;
258 }
259}
260
261inline Heap::AllocationTimer::~AllocationTimer() {
262 if (kMeasureAllocationTime) {
263 mirror::Object* allocated_obj = *allocated_obj_ptr_;
264 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800265 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700266 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800267 heap_->total_allocation_time_.FetchAndAdd(allocation_end_time - allocation_start_time_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700268 }
269 }
270};
271
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800272inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700273 // We need to have a zygote space or else our newly allocated large object can end up in the
274 // Zygote resulting in it being prematurely freed.
275 // We can only do this for primitive objects since large objects will not be within the card table
276 // range. This also means that we rely on SetClass not dirtying the object's card.
Mathieu Chartierbd0a6532014-02-27 11:14:21 -0800277 return byte_count >= large_object_threshold_ && c->IsPrimitiveArray();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700278}
279
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800280template <bool kGrow>
281inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700282 size_t new_footprint = num_bytes_allocated_ + alloc_size;
283 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
284 if (UNLIKELY(new_footprint > growth_limit_)) {
285 return true;
286 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700287 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800288 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700289 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700290 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800291 // TODO: Grow for allocation is racy, fix it.
292 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
293 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
294 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700295 }
296 }
297 return false;
298}
299
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800300inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800301 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700302 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
303 // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint.
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800304 SirtRef<mirror::Object> ref(self, *obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700305 RequestConcurrentGC(self);
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800306 // Restore obj in case it moved.
307 *obj = ref.get();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700308 }
309}
310
311} // namespace gc
312} // namespace art
313
314#endif // ART_RUNTIME_GC_HEAP_INL_H_