1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2013 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_GC_HEAP_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_HEAP_INL_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include "heap.h"
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include "allocation_listener.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/quasi_atomic.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
25*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/atomic_stack.h"
26*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/card_table-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "gc/allocation_record.h"
28*795d594fSAndroid Build Coastguard Worker #include "gc/collector/semi_space.h"
29*795d594fSAndroid Build Coastguard Worker #include "gc/space/bump_pointer_space-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "gc/space/dlmalloc_space-inl.h"
31*795d594fSAndroid Build Coastguard Worker #include "gc/space/large_object_space.h"
32*795d594fSAndroid Build Coastguard Worker #include "gc/space/region_space-inl.h"
33*795d594fSAndroid Build Coastguard Worker #include "gc/space/rosalloc_space-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
35*795d594fSAndroid Build Coastguard Worker #include "obj_ptr-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
37*795d594fSAndroid Build Coastguard Worker #include "thread-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "verify_object.h"
39*795d594fSAndroid Build Coastguard Worker #include "write_barrier-inl.h"
40*795d594fSAndroid Build Coastguard Worker
41*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
42*795d594fSAndroid Build Coastguard Worker namespace gc {
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
AllocObjectWithAllocator(Thread * self,ObjPtr<mirror::Class> klass,size_t byte_count,AllocatorType allocator,const PreFenceVisitor & pre_fence_visitor)45*795d594fSAndroid Build Coastguard Worker inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self,
46*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> klass,
47*795d594fSAndroid Build Coastguard Worker size_t byte_count,
48*795d594fSAndroid Build Coastguard Worker AllocatorType allocator,
49*795d594fSAndroid Build Coastguard Worker const PreFenceVisitor& pre_fence_visitor) {
50*795d594fSAndroid Build Coastguard Worker auto no_suspend_pre_fence_visitor =
51*795d594fSAndroid Build Coastguard Worker [&pre_fence_visitor](auto... x) REQUIRES_SHARED(Locks::mutator_lock_) {
52*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension sants("No thread suspension during pre-fence visitor");
53*795d594fSAndroid Build Coastguard Worker pre_fence_visitor(x...);
54*795d594fSAndroid Build Coastguard Worker };
55*795d594fSAndroid Build Coastguard Worker
56*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
57*795d594fSAndroid Build Coastguard Worker CheckPreconditionsForAllocObject(klass, byte_count);
58*795d594fSAndroid Build Coastguard Worker // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
59*795d594fSAndroid Build Coastguard Worker // done in the runnable state where suspension is expected.
60*795d594fSAndroid Build Coastguard Worker CHECK_EQ(self->GetState(), ThreadState::kRunnable);
61*795d594fSAndroid Build Coastguard Worker self->AssertThreadSuspensionIsAllowable();
62*795d594fSAndroid Build Coastguard Worker self->AssertNoPendingException();
63*795d594fSAndroid Build Coastguard Worker // Make sure to preserve klass.
64*795d594fSAndroid Build Coastguard Worker StackHandleScope<1> hs(self);
65*795d594fSAndroid Build Coastguard Worker HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass);
66*795d594fSAndroid Build Coastguard Worker self->PoisonObjectPointers();
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker auto pre_object_allocated = [&]() REQUIRES_SHARED(Locks::mutator_lock_)
69*795d594fSAndroid Build Coastguard Worker REQUIRES(!Roles::uninterruptible_ /* only suspends if kInstrumented */) {
70*795d594fSAndroid Build Coastguard Worker if constexpr (kInstrumented) {
71*795d594fSAndroid Build Coastguard Worker AllocationListener* l = alloc_listener_.load(std::memory_order_seq_cst);
72*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(l != nullptr) && UNLIKELY(l->HasPreAlloc())) {
73*795d594fSAndroid Build Coastguard Worker StackHandleScope<1> hs(self);
74*795d594fSAndroid Build Coastguard Worker HandleWrapperObjPtr<mirror::Class> h_klass(hs.NewHandleWrapper(&klass));
75*795d594fSAndroid Build Coastguard Worker l->PreObjectAllocated(self, h_klass, &byte_count);
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker };
79*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> obj;
80*795d594fSAndroid Build Coastguard Worker // bytes allocated for the (individual) object.
81*795d594fSAndroid Build Coastguard Worker size_t bytes_allocated;
82*795d594fSAndroid Build Coastguard Worker size_t usable_size;
83*795d594fSAndroid Build Coastguard Worker size_t new_num_bytes_allocated = 0;
84*795d594fSAndroid Build Coastguard Worker bool need_gc = false;
85*795d594fSAndroid Build Coastguard Worker uint32_t starting_gc_num; // o.w. GC number at which we observed need for GC.
86*795d594fSAndroid Build Coastguard Worker {
87*795d594fSAndroid Build Coastguard Worker // Bytes allocated that includes bulk thread-local buffer allocations in addition to direct
88*795d594fSAndroid Build Coastguard Worker // non-TLAB object allocations. Only set for non-thread-local allocation,
89*795d594fSAndroid Build Coastguard Worker size_t bytes_tl_bulk_allocated = 0u;
90*795d594fSAndroid Build Coastguard Worker // Do the initial pre-alloc
91*795d594fSAndroid Build Coastguard Worker // TODO: Consider what happens if the allocator is switched while suspended here.
92*795d594fSAndroid Build Coastguard Worker pre_object_allocated();
93*795d594fSAndroid Build Coastguard Worker
94*795d594fSAndroid Build Coastguard Worker // Need to check that we aren't the large object allocator since the large object allocation
95*795d594fSAndroid Build Coastguard Worker // code path includes this function. If we didn't check we would have an infinite loop.
96*795d594fSAndroid Build Coastguard Worker if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
97*795d594fSAndroid Build Coastguard Worker // AllocLargeObject can suspend and will recall PreObjectAllocated if needed.
98*795d594fSAndroid Build Coastguard Worker obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
99*795d594fSAndroid Build Coastguard Worker pre_fence_visitor);
100*795d594fSAndroid Build Coastguard Worker if (obj != nullptr) {
101*795d594fSAndroid Build Coastguard Worker return obj.Ptr();
102*795d594fSAndroid Build Coastguard Worker }
103*795d594fSAndroid Build Coastguard Worker // There should be an OOM exception, since we are retrying, clear it.
104*795d594fSAndroid Build Coastguard Worker self->ClearException();
105*795d594fSAndroid Build Coastguard Worker
106*795d594fSAndroid Build Coastguard Worker // If the large object allocation failed, try to use the normal spaces (main space,
107*795d594fSAndroid Build Coastguard Worker // non moving space). This can happen if there is significant virtual address space
108*795d594fSAndroid Build Coastguard Worker // fragmentation.
109*795d594fSAndroid Build Coastguard Worker // kInstrumented may be out of date, so recurse without large object checking, rather than
110*795d594fSAndroid Build Coastguard Worker // continue.
111*795d594fSAndroid Build Coastguard Worker return AllocObjectWithAllocator</*kInstrumented=*/ true, /*kCheckLargeObject=*/ false>
112*795d594fSAndroid Build Coastguard Worker (self, klass, byte_count, GetUpdatedAllocator(allocator), pre_fence_visitor);
113*795d594fSAndroid Build Coastguard Worker }
114*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension ants("Called PreObjectAllocated, no suspend until alloc");
115*795d594fSAndroid Build Coastguard Worker if (IsTLABAllocator(allocator)) {
116*795d594fSAndroid Build Coastguard Worker byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker // If we have a thread local allocation we don't need to update bytes allocated.
119*795d594fSAndroid Build Coastguard Worker if (IsTLABAllocator(allocator) && byte_count <= self->TlabSize()) {
120*795d594fSAndroid Build Coastguard Worker obj = self->AllocTlab(byte_count);
121*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr) << "AllocTlab can't fail";
122*795d594fSAndroid Build Coastguard Worker obj->SetClass(klass);
123*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
124*795d594fSAndroid Build Coastguard Worker obj->AssertReadBarrierState();
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker bytes_allocated = byte_count;
127*795d594fSAndroid Build Coastguard Worker usable_size = bytes_allocated;
128*795d594fSAndroid Build Coastguard Worker no_suspend_pre_fence_visitor(obj, usable_size);
129*795d594fSAndroid Build Coastguard Worker QuasiAtomic::ThreadFenceForConstructor();
130*795d594fSAndroid Build Coastguard Worker } else if (
131*795d594fSAndroid Build Coastguard Worker !kInstrumented && allocator == kAllocatorTypeRosAlloc &&
132*795d594fSAndroid Build Coastguard Worker (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) != nullptr &&
133*795d594fSAndroid Build Coastguard Worker LIKELY(obj != nullptr)) {
134*795d594fSAndroid Build Coastguard Worker DCHECK(!is_running_on_memory_tool_);
135*795d594fSAndroid Build Coastguard Worker obj->SetClass(klass);
136*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
137*795d594fSAndroid Build Coastguard Worker obj->AssertReadBarrierState();
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker usable_size = bytes_allocated;
140*795d594fSAndroid Build Coastguard Worker no_suspend_pre_fence_visitor(obj, usable_size);
141*795d594fSAndroid Build Coastguard Worker QuasiAtomic::ThreadFenceForConstructor();
142*795d594fSAndroid Build Coastguard Worker } else {
143*795d594fSAndroid Build Coastguard Worker obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
144*795d594fSAndroid Build Coastguard Worker &usable_size, &bytes_tl_bulk_allocated);
145*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(obj == nullptr)) {
146*795d594fSAndroid Build Coastguard Worker // AllocateInternalWithGc internally re-allows, and can cause, thread suspension, if
147*795d594fSAndroid Build Coastguard Worker // someone instruments the entrypoints or changes the allocator in a suspend point here,
148*795d594fSAndroid Build Coastguard Worker // we need to retry the allocation. It will send the pre-alloc event again.
149*795d594fSAndroid Build Coastguard Worker obj = AllocateInternalWithGc(self,
150*795d594fSAndroid Build Coastguard Worker allocator,
151*795d594fSAndroid Build Coastguard Worker kInstrumented,
152*795d594fSAndroid Build Coastguard Worker byte_count,
153*795d594fSAndroid Build Coastguard Worker &bytes_allocated,
154*795d594fSAndroid Build Coastguard Worker &usable_size,
155*795d594fSAndroid Build Coastguard Worker &bytes_tl_bulk_allocated,
156*795d594fSAndroid Build Coastguard Worker &klass);
157*795d594fSAndroid Build Coastguard Worker if (obj == nullptr) {
158*795d594fSAndroid Build Coastguard Worker // The only way that we can get a null return if there is no pending exception is if the
159*795d594fSAndroid Build Coastguard Worker // allocator or instrumentation changed.
160*795d594fSAndroid Build Coastguard Worker if (!self->IsExceptionPending()) {
161*795d594fSAndroid Build Coastguard Worker // Since we are restarting, allow thread suspension.
162*795d594fSAndroid Build Coastguard Worker ScopedAllowThreadSuspension ats;
163*795d594fSAndroid Build Coastguard Worker // Get the new class size in case class redefinition changed the class size since alloc
164*795d594fSAndroid Build Coastguard Worker // started.
165*795d594fSAndroid Build Coastguard Worker int new_byte_count = klass->IsVariableSize()? byte_count : klass->GetObjectSize();
166*795d594fSAndroid Build Coastguard Worker // AllocObject will pick up the new allocator type, and instrumented as true is the safe
167*795d594fSAndroid Build Coastguard Worker // default.
168*795d594fSAndroid Build Coastguard Worker return AllocObjectWithAllocator</*kInstrumented=*/true>(self,
169*795d594fSAndroid Build Coastguard Worker klass,
170*795d594fSAndroid Build Coastguard Worker new_byte_count,
171*795d594fSAndroid Build Coastguard Worker GetUpdatedAllocator(allocator),
172*795d594fSAndroid Build Coastguard Worker pre_fence_visitor);
173*795d594fSAndroid Build Coastguard Worker }
174*795d594fSAndroid Build Coastguard Worker return nullptr;
175*795d594fSAndroid Build Coastguard Worker }
176*795d594fSAndroid Build Coastguard Worker // Non-null result implies neither instrumentation nor allocator changed.
177*795d594fSAndroid Build Coastguard Worker }
178*795d594fSAndroid Build Coastguard Worker DCHECK_GT(bytes_allocated, 0u);
179*795d594fSAndroid Build Coastguard Worker DCHECK_GT(usable_size, 0u);
180*795d594fSAndroid Build Coastguard Worker obj->SetClass(klass);
181*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
182*795d594fSAndroid Build Coastguard Worker obj->AssertReadBarrierState();
183*795d594fSAndroid Build Coastguard Worker }
184*795d594fSAndroid Build Coastguard Worker if (collector::SemiSpace::kUseRememberedSet &&
185*795d594fSAndroid Build Coastguard Worker UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
186*795d594fSAndroid Build Coastguard Worker // (Note this if statement will be constant folded away for the fast-path quick entry
187*795d594fSAndroid Build Coastguard Worker // points.) Because SetClass() has no write barrier, the GC may need a write barrier in the
188*795d594fSAndroid Build Coastguard Worker // case the object is non movable and points to a recently allocated movable class.
189*795d594fSAndroid Build Coastguard Worker WriteBarrier::ForFieldWrite(obj, mirror::Object::ClassOffset(), klass);
190*795d594fSAndroid Build Coastguard Worker }
191*795d594fSAndroid Build Coastguard Worker no_suspend_pre_fence_visitor(obj, usable_size);
192*795d594fSAndroid Build Coastguard Worker QuasiAtomic::ThreadFenceForConstructor();
193*795d594fSAndroid Build Coastguard Worker }
194*795d594fSAndroid Build Coastguard Worker if (bytes_tl_bulk_allocated > 0) {
195*795d594fSAndroid Build Coastguard Worker starting_gc_num = GetCurrentGcNum();
196*795d594fSAndroid Build Coastguard Worker size_t num_bytes_allocated_before = AddBytesAllocated(bytes_tl_bulk_allocated);
197*795d594fSAndroid Build Coastguard Worker new_num_bytes_allocated = num_bytes_allocated_before + bytes_tl_bulk_allocated;
198*795d594fSAndroid Build Coastguard Worker // Only trace when we get an increase in the number of bytes allocated. This happens when
199*795d594fSAndroid Build Coastguard Worker // obtaining a new TLAB and isn't often enough to hurt performance according to golem.
200*795d594fSAndroid Build Coastguard Worker if (region_space_) {
201*795d594fSAndroid Build Coastguard Worker // With CC collector, during a GC cycle, the heap usage increases as
202*795d594fSAndroid Build Coastguard Worker // there are two copies of evacuated objects. Therefore, add evac-bytes
203*795d594fSAndroid Build Coastguard Worker // to the heap size. When the GC cycle is not running, evac-bytes
204*795d594fSAndroid Build Coastguard Worker // are 0, as required.
205*795d594fSAndroid Build Coastguard Worker TraceHeapSize(new_num_bytes_allocated + region_space_->EvacBytes());
206*795d594fSAndroid Build Coastguard Worker } else {
207*795d594fSAndroid Build Coastguard Worker TraceHeapSize(new_num_bytes_allocated);
208*795d594fSAndroid Build Coastguard Worker }
209*795d594fSAndroid Build Coastguard Worker // IsGcConcurrent() isn't known at compile time so we can optimize by not checking it for the
210*795d594fSAndroid Build Coastguard Worker // BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
211*795d594fSAndroid Build Coastguard Worker // optimized out.
212*795d594fSAndroid Build Coastguard Worker if (IsGcConcurrent() && UNLIKELY(ShouldConcurrentGCForJava(new_num_bytes_allocated))) {
213*795d594fSAndroid Build Coastguard Worker need_gc = true;
214*795d594fSAndroid Build Coastguard Worker }
215*795d594fSAndroid Build Coastguard Worker GetMetrics()->TotalBytesAllocated()->Add(bytes_tl_bulk_allocated);
216*795d594fSAndroid Build Coastguard Worker GetMetrics()->TotalBytesAllocatedDelta()->Add(bytes_tl_bulk_allocated);
217*795d594fSAndroid Build Coastguard Worker }
218*795d594fSAndroid Build Coastguard Worker }
219*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
220*795d594fSAndroid Build Coastguard Worker CHECK_LE(obj->SizeOf(), usable_size);
221*795d594fSAndroid Build Coastguard Worker }
222*795d594fSAndroid Build Coastguard Worker // TODO: Deprecate.
223*795d594fSAndroid Build Coastguard Worker if (kInstrumented) {
224*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->HasStatsEnabled()) {
225*795d594fSAndroid Build Coastguard Worker RuntimeStats* thread_stats = self->GetStats();
226*795d594fSAndroid Build Coastguard Worker ++thread_stats->allocated_objects;
227*795d594fSAndroid Build Coastguard Worker thread_stats->allocated_bytes += bytes_allocated;
228*795d594fSAndroid Build Coastguard Worker RuntimeStats* global_stats = Runtime::Current()->GetStats();
229*795d594fSAndroid Build Coastguard Worker ++global_stats->allocated_objects;
230*795d594fSAndroid Build Coastguard Worker global_stats->allocated_bytes += bytes_allocated;
231*795d594fSAndroid Build Coastguard Worker }
232*795d594fSAndroid Build Coastguard Worker } else {
233*795d594fSAndroid Build Coastguard Worker DCHECK(!Runtime::Current()->HasStatsEnabled());
234*795d594fSAndroid Build Coastguard Worker }
235*795d594fSAndroid Build Coastguard Worker if (kInstrumented) {
236*795d594fSAndroid Build Coastguard Worker if (IsAllocTrackingEnabled()) {
237*795d594fSAndroid Build Coastguard Worker // allocation_records_ is not null since it never becomes null after allocation tracking is
238*795d594fSAndroid Build Coastguard Worker // enabled.
239*795d594fSAndroid Build Coastguard Worker DCHECK(allocation_records_ != nullptr);
240*795d594fSAndroid Build Coastguard Worker allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
241*795d594fSAndroid Build Coastguard Worker }
242*795d594fSAndroid Build Coastguard Worker AllocationListener* l = alloc_listener_.load(std::memory_order_seq_cst);
243*795d594fSAndroid Build Coastguard Worker if (l != nullptr) {
244*795d594fSAndroid Build Coastguard Worker // Same as above. We assume that a listener that was once stored will never be deleted.
245*795d594fSAndroid Build Coastguard Worker // Otherwise we'd have to perform this under a lock.
246*795d594fSAndroid Build Coastguard Worker l->ObjectAllocated(self, &obj, bytes_allocated);
247*795d594fSAndroid Build Coastguard Worker }
248*795d594fSAndroid Build Coastguard Worker } else {
249*795d594fSAndroid Build Coastguard Worker DCHECK(!IsAllocTrackingEnabled());
250*795d594fSAndroid Build Coastguard Worker }
251*795d594fSAndroid Build Coastguard Worker if (AllocatorHasAllocationStack(allocator)) {
252*795d594fSAndroid Build Coastguard Worker PushOnAllocationStack(self, &obj);
253*795d594fSAndroid Build Coastguard Worker // Ensure that the push to allocation stack is done before the object is published.
254*795d594fSAndroid Build Coastguard Worker std::atomic_thread_fence(std::memory_order_release);
255*795d594fSAndroid Build Coastguard Worker }
256*795d594fSAndroid Build Coastguard Worker if (kInstrumented) {
257*795d594fSAndroid Build Coastguard Worker if (gc_stress_mode_) {
258*795d594fSAndroid Build Coastguard Worker CheckGcStressMode(self, &obj);
259*795d594fSAndroid Build Coastguard Worker }
260*795d594fSAndroid Build Coastguard Worker } else {
261*795d594fSAndroid Build Coastguard Worker DCHECK(!gc_stress_mode_);
262*795d594fSAndroid Build Coastguard Worker }
263*795d594fSAndroid Build Coastguard Worker if (need_gc) {
264*795d594fSAndroid Build Coastguard Worker // Do this only once thread suspension is allowed again, and we're done with kInstrumented.
265*795d594fSAndroid Build Coastguard Worker RequestConcurrentGCAndSaveObject(self, /*force_full=*/ false, starting_gc_num, &obj);
266*795d594fSAndroid Build Coastguard Worker }
267*795d594fSAndroid Build Coastguard Worker VerifyObject(obj);
268*795d594fSAndroid Build Coastguard Worker self->VerifyStack();
269*795d594fSAndroid Build Coastguard Worker return obj.Ptr();
270*795d594fSAndroid Build Coastguard Worker }
271*795d594fSAndroid Build Coastguard Worker
272*795d594fSAndroid Build Coastguard Worker // The size of a thread-local allocation stack in the number of references.
273*795d594fSAndroid Build Coastguard Worker static constexpr size_t kThreadLocalAllocationStackSize = 128;
274*795d594fSAndroid Build Coastguard Worker
PushOnAllocationStack(Thread * self,ObjPtr<mirror::Object> * obj)275*795d594fSAndroid Build Coastguard Worker inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) {
276*795d594fSAndroid Build Coastguard Worker if (kUseThreadLocalAllocationStack) {
277*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) {
278*795d594fSAndroid Build Coastguard Worker PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
279*795d594fSAndroid Build Coastguard Worker }
280*795d594fSAndroid Build Coastguard Worker } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) {
281*795d594fSAndroid Build Coastguard Worker PushOnAllocationStackWithInternalGC(self, obj);
282*795d594fSAndroid Build Coastguard Worker }
283*795d594fSAndroid Build Coastguard Worker }
284*795d594fSAndroid Build Coastguard Worker
285*795d594fSAndroid Build Coastguard Worker template <bool kInstrumented, typename PreFenceVisitor>
AllocLargeObject(Thread * self,ObjPtr<mirror::Class> * klass,size_t byte_count,const PreFenceVisitor & pre_fence_visitor)286*795d594fSAndroid Build Coastguard Worker inline mirror::Object* Heap::AllocLargeObject(Thread* self,
287*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class>* klass,
288*795d594fSAndroid Build Coastguard Worker size_t byte_count,
289*795d594fSAndroid Build Coastguard Worker const PreFenceVisitor& pre_fence_visitor) {
290*795d594fSAndroid Build Coastguard Worker // Save and restore the class in case it moves.
291*795d594fSAndroid Build Coastguard Worker StackHandleScope<1> hs(self);
292*795d594fSAndroid Build Coastguard Worker auto klass_wrapper = hs.NewHandleWrapper(klass);
293*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>
294*795d594fSAndroid Build Coastguard Worker (self, *klass, byte_count, kAllocatorTypeLOS, pre_fence_visitor);
295*795d594fSAndroid Build Coastguard Worker // Java Heap Profiler check and sample allocation.
296*795d594fSAndroid Build Coastguard Worker if (GetHeapSampler().IsEnabled()) {
297*795d594fSAndroid Build Coastguard Worker JHPCheckNonTlabSampleAllocation(self, obj, byte_count);
298*795d594fSAndroid Build Coastguard Worker }
299*795d594fSAndroid Build Coastguard Worker return obj;
300*795d594fSAndroid Build Coastguard Worker }
301*795d594fSAndroid Build Coastguard Worker
302*795d594fSAndroid Build Coastguard Worker template <const bool kInstrumented, const bool kGrow>
TryToAllocate(Thread * self,AllocatorType allocator_type,size_t alloc_size,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)303*795d594fSAndroid Build Coastguard Worker inline mirror::Object* Heap::TryToAllocate(Thread* self,
304*795d594fSAndroid Build Coastguard Worker AllocatorType allocator_type,
305*795d594fSAndroid Build Coastguard Worker size_t alloc_size,
306*795d594fSAndroid Build Coastguard Worker size_t* bytes_allocated,
307*795d594fSAndroid Build Coastguard Worker size_t* usable_size,
308*795d594fSAndroid Build Coastguard Worker size_t* bytes_tl_bulk_allocated) {
309*795d594fSAndroid Build Coastguard Worker if (allocator_type != kAllocatorTypeRegionTLAB &&
310*795d594fSAndroid Build Coastguard Worker allocator_type != kAllocatorTypeTLAB &&
311*795d594fSAndroid Build Coastguard Worker allocator_type != kAllocatorTypeRosAlloc &&
312*795d594fSAndroid Build Coastguard Worker UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) {
313*795d594fSAndroid Build Coastguard Worker return nullptr;
314*795d594fSAndroid Build Coastguard Worker }
315*795d594fSAndroid Build Coastguard Worker mirror::Object* ret;
316*795d594fSAndroid Build Coastguard Worker switch (allocator_type) {
317*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeBumpPointer: {
318*795d594fSAndroid Build Coastguard Worker DCHECK(bump_pointer_space_ != nullptr);
319*795d594fSAndroid Build Coastguard Worker alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
320*795d594fSAndroid Build Coastguard Worker ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
321*795d594fSAndroid Build Coastguard Worker if (LIKELY(ret != nullptr)) {
322*795d594fSAndroid Build Coastguard Worker *bytes_allocated = alloc_size;
323*795d594fSAndroid Build Coastguard Worker *usable_size = alloc_size;
324*795d594fSAndroid Build Coastguard Worker *bytes_tl_bulk_allocated = alloc_size;
325*795d594fSAndroid Build Coastguard Worker }
326*795d594fSAndroid Build Coastguard Worker break;
327*795d594fSAndroid Build Coastguard Worker }
328*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeRosAlloc: {
329*795d594fSAndroid Build Coastguard Worker if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
330*795d594fSAndroid Build Coastguard Worker // If running on ASan, we should be using the instrumented path.
331*795d594fSAndroid Build Coastguard Worker size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
332*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
333*795d594fSAndroid Build Coastguard Worker max_bytes_tl_bulk_allocated,
334*795d594fSAndroid Build Coastguard Worker kGrow))) {
335*795d594fSAndroid Build Coastguard Worker return nullptr;
336*795d594fSAndroid Build Coastguard Worker }
337*795d594fSAndroid Build Coastguard Worker ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
338*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
339*795d594fSAndroid Build Coastguard Worker } else {
340*795d594fSAndroid Build Coastguard Worker DCHECK(!is_running_on_memory_tool_);
341*795d594fSAndroid Build Coastguard Worker size_t max_bytes_tl_bulk_allocated =
342*795d594fSAndroid Build Coastguard Worker rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
343*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
344*795d594fSAndroid Build Coastguard Worker max_bytes_tl_bulk_allocated,
345*795d594fSAndroid Build Coastguard Worker kGrow))) {
346*795d594fSAndroid Build Coastguard Worker return nullptr;
347*795d594fSAndroid Build Coastguard Worker }
348*795d594fSAndroid Build Coastguard Worker if (!kInstrumented) {
349*795d594fSAndroid Build Coastguard Worker DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
350*795d594fSAndroid Build Coastguard Worker }
351*795d594fSAndroid Build Coastguard Worker ret = rosalloc_space_->AllocNonvirtual(self,
352*795d594fSAndroid Build Coastguard Worker alloc_size,
353*795d594fSAndroid Build Coastguard Worker bytes_allocated,
354*795d594fSAndroid Build Coastguard Worker usable_size,
355*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
356*795d594fSAndroid Build Coastguard Worker }
357*795d594fSAndroid Build Coastguard Worker break;
358*795d594fSAndroid Build Coastguard Worker }
359*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeDlMalloc: {
360*795d594fSAndroid Build Coastguard Worker if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
361*795d594fSAndroid Build Coastguard Worker // If running on ASan, we should be using the instrumented path.
362*795d594fSAndroid Build Coastguard Worker ret = dlmalloc_space_->Alloc(self,
363*795d594fSAndroid Build Coastguard Worker alloc_size,
364*795d594fSAndroid Build Coastguard Worker bytes_allocated,
365*795d594fSAndroid Build Coastguard Worker usable_size,
366*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
367*795d594fSAndroid Build Coastguard Worker } else {
368*795d594fSAndroid Build Coastguard Worker DCHECK(!is_running_on_memory_tool_);
369*795d594fSAndroid Build Coastguard Worker ret = dlmalloc_space_->AllocNonvirtual(self,
370*795d594fSAndroid Build Coastguard Worker alloc_size,
371*795d594fSAndroid Build Coastguard Worker bytes_allocated,
372*795d594fSAndroid Build Coastguard Worker usable_size,
373*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
374*795d594fSAndroid Build Coastguard Worker }
375*795d594fSAndroid Build Coastguard Worker break;
376*795d594fSAndroid Build Coastguard Worker }
377*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeNonMoving: {
378*795d594fSAndroid Build Coastguard Worker ret = non_moving_space_->Alloc(self,
379*795d594fSAndroid Build Coastguard Worker alloc_size,
380*795d594fSAndroid Build Coastguard Worker bytes_allocated,
381*795d594fSAndroid Build Coastguard Worker usable_size,
382*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
383*795d594fSAndroid Build Coastguard Worker break;
384*795d594fSAndroid Build Coastguard Worker }
385*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeLOS: {
386*795d594fSAndroid Build Coastguard Worker ret = large_object_space_->Alloc(self,
387*795d594fSAndroid Build Coastguard Worker alloc_size,
388*795d594fSAndroid Build Coastguard Worker bytes_allocated,
389*795d594fSAndroid Build Coastguard Worker usable_size,
390*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
391*795d594fSAndroid Build Coastguard Worker // Note that the bump pointer spaces aren't necessarily next to
392*795d594fSAndroid Build Coastguard Worker // the other continuous spaces like the non-moving alloc space or
393*795d594fSAndroid Build Coastguard Worker // the zygote space.
394*795d594fSAndroid Build Coastguard Worker DCHECK(ret == nullptr || large_object_space_->Contains(ret));
395*795d594fSAndroid Build Coastguard Worker break;
396*795d594fSAndroid Build Coastguard Worker }
397*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeRegion: {
398*795d594fSAndroid Build Coastguard Worker DCHECK(region_space_ != nullptr);
399*795d594fSAndroid Build Coastguard Worker alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
400*795d594fSAndroid Build Coastguard Worker ret = region_space_->AllocNonvirtual<false>(alloc_size,
401*795d594fSAndroid Build Coastguard Worker bytes_allocated,
402*795d594fSAndroid Build Coastguard Worker usable_size,
403*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
404*795d594fSAndroid Build Coastguard Worker break;
405*795d594fSAndroid Build Coastguard Worker }
406*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeTLAB:
407*795d594fSAndroid Build Coastguard Worker FALLTHROUGH_INTENDED;
408*795d594fSAndroid Build Coastguard Worker case kAllocatorTypeRegionTLAB: {
409*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(alloc_size, kObjectAlignment);
410*795d594fSAndroid Build Coastguard Worker static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment,
411*795d594fSAndroid Build Coastguard Worker "mismatched alignments");
412*795d594fSAndroid Build Coastguard Worker static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment,
413*795d594fSAndroid Build Coastguard Worker "mismatched alignments");
414*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(self->TlabSize() < alloc_size)) {
415*795d594fSAndroid Build Coastguard Worker return AllocWithNewTLAB(self,
416*795d594fSAndroid Build Coastguard Worker allocator_type,
417*795d594fSAndroid Build Coastguard Worker alloc_size,
418*795d594fSAndroid Build Coastguard Worker kGrow,
419*795d594fSAndroid Build Coastguard Worker bytes_allocated,
420*795d594fSAndroid Build Coastguard Worker usable_size,
421*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
422*795d594fSAndroid Build Coastguard Worker }
423*795d594fSAndroid Build Coastguard Worker // The allocation can't fail.
424*795d594fSAndroid Build Coastguard Worker ret = self->AllocTlab(alloc_size);
425*795d594fSAndroid Build Coastguard Worker DCHECK(ret != nullptr);
426*795d594fSAndroid Build Coastguard Worker *bytes_allocated = alloc_size;
427*795d594fSAndroid Build Coastguard Worker *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
428*795d594fSAndroid Build Coastguard Worker *usable_size = alloc_size;
429*795d594fSAndroid Build Coastguard Worker break;
430*795d594fSAndroid Build Coastguard Worker }
431*795d594fSAndroid Build Coastguard Worker default: {
432*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Invalid allocator type";
433*795d594fSAndroid Build Coastguard Worker ret = nullptr;
434*795d594fSAndroid Build Coastguard Worker }
435*795d594fSAndroid Build Coastguard Worker }
436*795d594fSAndroid Build Coastguard Worker return ret;
437*795d594fSAndroid Build Coastguard Worker }
438*795d594fSAndroid Build Coastguard Worker
ShouldAllocLargeObject(ObjPtr<mirror::Class> c,size_t byte_count)439*795d594fSAndroid Build Coastguard Worker inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const {
440*795d594fSAndroid Build Coastguard Worker // We need to have a zygote space or else our newly allocated large object can end up in the
441*795d594fSAndroid Build Coastguard Worker // Zygote resulting in it being prematurely freed.
442*795d594fSAndroid Build Coastguard Worker // We can only do this for primitive objects since large objects will not be within the card table
443*795d594fSAndroid Build Coastguard Worker // range. This also means that we rely on SetClass not dirtying the object's card.
444*795d594fSAndroid Build Coastguard Worker return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
445*795d594fSAndroid Build Coastguard Worker }
446*795d594fSAndroid Build Coastguard Worker
IsOutOfMemoryOnAllocation(AllocatorType allocator_type,size_t alloc_size,bool grow)447*795d594fSAndroid Build Coastguard Worker inline bool Heap::IsOutOfMemoryOnAllocation([[maybe_unused]] AllocatorType allocator_type,
448*795d594fSAndroid Build Coastguard Worker size_t alloc_size,
449*795d594fSAndroid Build Coastguard Worker bool grow) {
450*795d594fSAndroid Build Coastguard Worker size_t old_target = target_footprint_.load(std::memory_order_relaxed);
451*795d594fSAndroid Build Coastguard Worker while (true) {
452*795d594fSAndroid Build Coastguard Worker size_t old_allocated = num_bytes_allocated_.load(std::memory_order_relaxed);
453*795d594fSAndroid Build Coastguard Worker size_t new_footprint = old_allocated + alloc_size;
454*795d594fSAndroid Build Coastguard Worker // Tests against heap limits are inherently approximate, since multiple allocations may
455*795d594fSAndroid Build Coastguard Worker // race, and this is not atomic with the allocation.
456*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(new_footprint <= old_target)) {
457*795d594fSAndroid Build Coastguard Worker return false;
458*795d594fSAndroid Build Coastguard Worker } else if (UNLIKELY(new_footprint > growth_limit_)) {
459*795d594fSAndroid Build Coastguard Worker return true;
460*795d594fSAndroid Build Coastguard Worker }
461*795d594fSAndroid Build Coastguard Worker // We are between target_footprint_ and growth_limit_ .
462*795d594fSAndroid Build Coastguard Worker if (IsGcConcurrent()) {
463*795d594fSAndroid Build Coastguard Worker return false;
464*795d594fSAndroid Build Coastguard Worker } else {
465*795d594fSAndroid Build Coastguard Worker if (grow) {
466*795d594fSAndroid Build Coastguard Worker if (target_footprint_.compare_exchange_weak(/*inout ref*/old_target, new_footprint,
467*795d594fSAndroid Build Coastguard Worker std::memory_order_relaxed)) {
468*795d594fSAndroid Build Coastguard Worker VlogHeapGrowth(old_target, new_footprint, alloc_size);
469*795d594fSAndroid Build Coastguard Worker return false;
470*795d594fSAndroid Build Coastguard Worker } // else try again.
471*795d594fSAndroid Build Coastguard Worker } else {
472*795d594fSAndroid Build Coastguard Worker return true;
473*795d594fSAndroid Build Coastguard Worker }
474*795d594fSAndroid Build Coastguard Worker }
475*795d594fSAndroid Build Coastguard Worker }
476*795d594fSAndroid Build Coastguard Worker }
477*795d594fSAndroid Build Coastguard Worker
ShouldConcurrentGCForJava(size_t new_num_bytes_allocated)478*795d594fSAndroid Build Coastguard Worker inline bool Heap::ShouldConcurrentGCForJava(size_t new_num_bytes_allocated) {
479*795d594fSAndroid Build Coastguard Worker // For a Java allocation, we only check whether the number of Java allocated bytes excceeds a
480*795d594fSAndroid Build Coastguard Worker // threshold. By not considering native allocation here, we (a) ensure that Java heap bounds are
481*795d594fSAndroid Build Coastguard Worker // maintained, and (b) reduce the cost of the check here.
482*795d594fSAndroid Build Coastguard Worker return new_num_bytes_allocated >= concurrent_start_bytes_;
483*795d594fSAndroid Build Coastguard Worker }
484*795d594fSAndroid Build Coastguard Worker
485*795d594fSAndroid Build Coastguard Worker } // namespace gc
486*795d594fSAndroid Build Coastguard Worker } // namespace art
487*795d594fSAndroid Build Coastguard Worker
488*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_GC_HEAP_INL_H_
489