blob: 60fb71d9524f5e2951b01dcc79446752e330ce20 [file] [log] [blame]
Man Cao8c2ff642015-05-27 17:25:30 -07001/*
2 * Copyright (C) 2015 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#include "allocation_record.h"
18
19#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080021#include "base/logging.h" // For VLOG
Man Cao8c2ff642015-05-27 17:25:30 -070022#include "base/stl_util.h"
Mathieu Chartier9d156d52016-10-06 17:44:26 -070023#include "obj_ptr-inl.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070024#include "object_callbacks.h"
Man Cao8c2ff642015-05-27 17:25:30 -070025#include "stack.h"
26
Elliott Hughesf049a0b2018-10-25 14:28:12 -070027#include <android-base/properties.h>
Man Cao8c2ff642015-05-27 17:25:30 -070028
29namespace art {
30namespace gc {
31
32int32_t AllocRecordStackTraceElement::ComputeLineNumber() const {
33 DCHECK(method_ != nullptr);
34 return method_->GetLineNumFromDexPC(dex_pc_);
35}
36
Man Cao41656de2015-07-06 18:53:15 -070037const char* AllocRecord::GetClassDescriptor(std::string* storage) const {
38 // klass_ could contain null only if we implement class unloading.
Mathieu Chartiera7deef92016-02-22 14:49:04 -080039 return klass_.IsNull() ? "null" : klass_.Read()->GetDescriptor(storage);
Man Cao41656de2015-07-06 18:53:15 -070040}
41
Mathieu Chartier0a206072019-03-28 12:29:22 -070042void AllocRecordObjectMap::SetMaxStackDepth(size_t max_stack_depth) {
43 // Log fatal since this should already be checked when calling VMDebug.setAllocTrackerStackDepth.
44 CHECK_LE(max_stack_depth, kMaxSupportedStackDepth)
45 << "Allocation record max stack depth is too large";
46 max_stack_depth_ = max_stack_depth;
Man Cao8c2ff642015-05-27 17:25:30 -070047}
48
49AllocRecordObjectMap::~AllocRecordObjectMap() {
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -080050 Clear();
Man Cao8c2ff642015-05-27 17:25:30 -070051}
52
Man Cao1ed11b92015-06-11 22:47:35 -070053void AllocRecordObjectMap::VisitRoots(RootVisitor* visitor) {
54 CHECK_LE(recent_record_max_, alloc_record_max_);
55 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(visitor, RootInfo(kRootDebugger));
56 size_t count = recent_record_max_;
Man Cao42c3c332015-06-23 16:38:25 -070057 // Only visit the last recent_record_max_ number of allocation records in entries_ and mark the
58 // klass_ fields as strong roots.
Mathieu Chartiera7deef92016-02-22 14:49:04 -080059 for (auto it = entries_.rbegin(), end = entries_.rend(); it != end; ++it) {
Mathieu Chartier458b1052016-03-29 14:02:55 -070060 AllocRecord& record = it->second;
Mathieu Chartiera7deef92016-02-22 14:49:04 -080061 if (count > 0) {
Mathieu Chartier458b1052016-03-29 14:02:55 -070062 buffered_visitor.VisitRootIfNonNull(record.GetClassGcRoot());
Mathieu Chartiera7deef92016-02-22 14:49:04 -080063 --count;
64 }
65 // Visit all of the stack frames to make sure no methods in the stack traces get unloaded by
66 // class unloading.
Mathieu Chartier458b1052016-03-29 14:02:55 -070067 for (size_t i = 0, depth = record.GetDepth(); i < depth; ++i) {
68 const AllocRecordStackTraceElement& element = record.StackElement(i);
Mathieu Chartiera7deef92016-02-22 14:49:04 -080069 DCHECK(element.GetMethod() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -070070 element.GetMethod()->VisitRoots(buffered_visitor, kRuntimePointerSize);
Mathieu Chartiera7deef92016-02-22 14:49:04 -080071 }
Man Cao42c3c332015-06-23 16:38:25 -070072 }
73}
74
Mathieu Chartier97509952015-07-13 14:35:43 -070075static inline void SweepClassObject(AllocRecord* record, IsMarkedVisitor* visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070076 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070077 REQUIRES(Locks::alloc_tracker_lock_) {
Man Cao42c3c332015-06-23 16:38:25 -070078 GcRoot<mirror::Class>& klass = record->GetClassGcRoot();
79 // This does not need a read barrier because this is called by GC.
80 mirror::Object* old_object = klass.Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -070081 if (old_object != nullptr) {
82 // The class object can become null if we implement class unloading.
83 // In that case we might still want to keep the class name string (not implemented).
84 mirror::Object* new_object = visitor->IsMarked(old_object);
85 DCHECK(new_object != nullptr);
86 if (UNLIKELY(old_object != new_object)) {
87 klass = GcRoot<mirror::Class>(new_object->AsClass());
88 }
Man Cao1ed11b92015-06-11 22:47:35 -070089 }
90}
91
Mathieu Chartier97509952015-07-13 14:35:43 -070092void AllocRecordObjectMap::SweepAllocationRecords(IsMarkedVisitor* visitor) {
Man Cao8c2ff642015-05-27 17:25:30 -070093 VLOG(heap) << "Start SweepAllocationRecords()";
Man Cao42c3c332015-06-23 16:38:25 -070094 size_t count_deleted = 0, count_moved = 0, count = 0;
95 // Only the first (size - recent_record_max_) number of records can be deleted.
Mathieu Chartiera7deef92016-02-22 14:49:04 -080096 const size_t delete_bound = std::max(entries_.size(), recent_record_max_) - recent_record_max_;
Man Cao8c2ff642015-05-27 17:25:30 -070097 for (auto it = entries_.begin(), end = entries_.end(); it != end;) {
Man Cao42c3c332015-06-23 16:38:25 -070098 ++count;
Man Cao8c2ff642015-05-27 17:25:30 -070099 // This does not need a read barrier because this is called by GC.
100 mirror::Object* old_object = it->first.Read<kWithoutReadBarrier>();
Mathieu Chartier458b1052016-03-29 14:02:55 -0700101 AllocRecord& record = it->second;
Mathieu Chartier97509952015-07-13 14:35:43 -0700102 mirror::Object* new_object = old_object == nullptr ? nullptr : visitor->IsMarked(old_object);
Man Cao8c2ff642015-05-27 17:25:30 -0700103 if (new_object == nullptr) {
Man Cao42c3c332015-06-23 16:38:25 -0700104 if (count > delete_bound) {
105 it->first = GcRoot<mirror::Object>(nullptr);
Mathieu Chartier458b1052016-03-29 14:02:55 -0700106 SweepClassObject(&record, visitor);
Man Cao42c3c332015-06-23 16:38:25 -0700107 ++it;
108 } else {
Man Cao42c3c332015-06-23 16:38:25 -0700109 it = entries_.erase(it);
110 ++count_deleted;
111 }
Man Cao8c2ff642015-05-27 17:25:30 -0700112 } else {
113 if (old_object != new_object) {
114 it->first = GcRoot<mirror::Object>(new_object);
115 ++count_moved;
116 }
Mathieu Chartier458b1052016-03-29 14:02:55 -0700117 SweepClassObject(&record, visitor);
Man Cao8c2ff642015-05-27 17:25:30 -0700118 ++it;
119 }
120 }
121 VLOG(heap) << "Deleted " << count_deleted << " allocation records";
122 VLOG(heap) << "Updated " << count_moved << " allocation records";
123}
124
Man Cao42c3c332015-06-23 16:38:25 -0700125void AllocRecordObjectMap::AllowNewAllocationRecords() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700126 CHECK(!kUseReadBarrier);
Man Cao42c3c332015-06-23 16:38:25 -0700127 allow_new_record_ = true;
128 new_record_condition_.Broadcast(Thread::Current());
129}
130
131void AllocRecordObjectMap::DisallowNewAllocationRecords() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700132 CHECK(!kUseReadBarrier);
Man Cao42c3c332015-06-23 16:38:25 -0700133 allow_new_record_ = false;
134}
135
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700136void AllocRecordObjectMap::BroadcastForNewAllocationRecords() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700137 new_record_condition_.Broadcast(Thread::Current());
138}
139
Man Cao8c2ff642015-05-27 17:25:30 -0700140void AllocRecordObjectMap::SetAllocTrackingEnabled(bool enable) {
141 Thread* self = Thread::Current();
142 Heap* heap = Runtime::Current()->GetHeap();
143 if (enable) {
144 {
145 MutexLock mu(self, *Locks::alloc_tracker_lock_);
146 if (heap->IsAllocTrackingEnabled()) {
147 return; // Already enabled, bail.
148 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800149 AllocRecordObjectMap* records = heap->GetAllocationRecords();
150 if (records == nullptr) {
151 records = new AllocRecordObjectMap;
152 heap->SetAllocationRecords(records);
153 }
Man Cao8c2ff642015-05-27 17:25:30 -0700154 CHECK(records != nullptr);
Mathieu Chartier0a206072019-03-28 12:29:22 -0700155 records->SetMaxStackDepth(heap->GetAllocTrackerStackDepth());
Man Cao8c2ff642015-05-27 17:25:30 -0700156 size_t sz = sizeof(AllocRecordStackTraceElement) * records->max_stack_depth_ +
157 sizeof(AllocRecord) + sizeof(AllocRecordStackTrace);
158 LOG(INFO) << "Enabling alloc tracker (" << records->alloc_record_max_ << " entries of "
159 << records->max_stack_depth_ << " frames, taking up to "
160 << PrettySize(sz * records->alloc_record_max_) << ")";
Man Cao8c2ff642015-05-27 17:25:30 -0700161 }
162 Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
Mathieu Chartier16e51be2016-02-23 10:37:32 -0800163 {
164 MutexLock mu(self, *Locks::alloc_tracker_lock_);
165 heap->SetAllocTrackingEnabled(true);
166 }
Man Cao8c2ff642015-05-27 17:25:30 -0700167 } else {
Mathieu Chartier0b8b4a62016-03-02 12:52:37 -0800168 // Delete outside of the critical section to avoid possible lock violations like the runtime
169 // shutdown lock.
Man Cao8c2ff642015-05-27 17:25:30 -0700170 {
171 MutexLock mu(self, *Locks::alloc_tracker_lock_);
172 if (!heap->IsAllocTrackingEnabled()) {
173 return; // Already disabled, bail.
174 }
175 heap->SetAllocTrackingEnabled(false);
176 LOG(INFO) << "Disabling alloc tracker";
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800177 AllocRecordObjectMap* records = heap->GetAllocationRecords();
178 records->Clear();
Man Cao8c2ff642015-05-27 17:25:30 -0700179 }
180 // If an allocation comes in before we uninstrument, we will safely drop it on the floor.
181 Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
182 }
183}
184
Mathieu Chartier458b1052016-03-29 14:02:55 -0700185void AllocRecordObjectMap::RecordAllocation(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700186 ObjPtr<mirror::Object>* obj,
Man Cao42c3c332015-06-23 16:38:25 -0700187 size_t byte_count) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700188 // Get stack trace outside of lock in case there are allocations during the stack walk.
189 // b/27858645.
190 AllocRecordStackTrace trace;
Mathieu Chartier458b1052016-03-29 14:02:55 -0700191 {
192 StackHandleScope<1> hs(self);
193 auto obj_wrapper = hs.NewHandleWrapper(obj);
Andreas Gampec7d878d2018-11-19 18:42:06 +0000194
195 StackVisitor::WalkStack(
196 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
197 if (trace.GetDepth() >= max_stack_depth_) {
198 return false;
199 }
200 ArtMethod* m = stack_visitor->GetMethod();
201 // m may be null if we have inlined methods of unresolved classes. b/27858645
202 if (m != nullptr && !m->IsRuntimeMethod()) {
203 m = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
204 trace.AddStackElement(AllocRecordStackTraceElement(m, stack_visitor->GetDexPc()));
205 }
206 return true;
207 },
208 self,
209 /* context= */ nullptr,
210 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
Mathieu Chartier458b1052016-03-29 14:02:55 -0700211 }
212
Man Cao8c2ff642015-05-27 17:25:30 -0700213 MutexLock mu(self, *Locks::alloc_tracker_lock_);
Mathieu Chartier458b1052016-03-29 14:02:55 -0700214 Heap* const heap = Runtime::Current()->GetHeap();
Man Cao8c2ff642015-05-27 17:25:30 -0700215 if (!heap->IsAllocTrackingEnabled()) {
216 // In the process of shutting down recording, bail.
217 return;
218 }
219
Alex Lightfc588092020-01-23 15:39:08 -0800220 // TODO Skip recording allocations associated with DDMS. This was a feature of the old debugger
221 // but when we switched to the JVMTI based debugger the feature was (unintentionally) broken.
222 // Since nobody seemed to really notice or care it might not be worth the trouble.
Man Cao8c2ff642015-05-27 17:25:30 -0700223
Roland Levillainaf290312018-02-27 20:02:17 +0000224 // Wait for GC's sweeping to complete and allow new records.
Mathieu Chartier458b1052016-03-29 14:02:55 -0700225 while (UNLIKELY((!kUseReadBarrier && !allow_new_record_) ||
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700226 (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700227 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
228 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -0800229 self->CheckEmptyCheckpointFromWeakRefAccess(Locks::alloc_tracker_lock_);
Mathieu Chartier458b1052016-03-29 14:02:55 -0700230 new_record_condition_.WaitHoldingLocks(self);
Man Cao42c3c332015-06-23 16:38:25 -0700231 }
232
Hiroshi Yamauchi6f0c6cd2016-03-18 17:17:52 -0700233 if (!heap->IsAllocTrackingEnabled()) {
234 // Return if the allocation tracking has been disabled while waiting for system weak access
235 // above.
236 return;
237 }
238
Mathieu Chartier458b1052016-03-29 14:02:55 -0700239 DCHECK_LE(Size(), alloc_record_max_);
Man Cao8c2ff642015-05-27 17:25:30 -0700240
Mathieu Chartier458b1052016-03-29 14:02:55 -0700241 // Erase extra unfilled elements.
242 trace.SetTid(self->GetTid());
Man Cao8c2ff642015-05-27 17:25:30 -0700243
Mathieu Chartier458b1052016-03-29 14:02:55 -0700244 // Add the record.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700245 Put(obj->Ptr(), AllocRecord(byte_count, (*obj)->GetClass(), std::move(trace)));
Mathieu Chartier458b1052016-03-29 14:02:55 -0700246 DCHECK_LE(Size(), alloc_record_max_);
Man Cao8c2ff642015-05-27 17:25:30 -0700247}
248
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800249void AllocRecordObjectMap::Clear() {
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800250 entries_.clear();
251}
252
Mathieu Chartier458b1052016-03-29 14:02:55 -0700253AllocRecordObjectMap::AllocRecordObjectMap()
254 : new_record_condition_("New allocation record condition", *Locks::alloc_tracker_lock_) {}
255
Man Cao8c2ff642015-05-27 17:25:30 -0700256} // namespace gc
257} // namespace art