| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 20 | #include "base/enums.h" |
| Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 21 | #include "base/logging.h" // For VLOG |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 23 | #include "obj_ptr-inl.h" |
| Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 24 | #include "object_callbacks.h" |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 25 | #include "stack.h" |
| 26 | |
| Elliott Hughes | f049a0b | 2018-10-25 14:28:12 -0700 | [diff] [blame] | 27 | #include <android-base/properties.h> |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace gc { |
| 31 | |
| 32 | int32_t AllocRecordStackTraceElement::ComputeLineNumber() const { |
| 33 | DCHECK(method_ != nullptr); |
| 34 | return method_->GetLineNumFromDexPC(dex_pc_); |
| 35 | } |
| 36 | |
| Man Cao | 41656de | 2015-07-06 18:53:15 -0700 | [diff] [blame] | 37 | const char* AllocRecord::GetClassDescriptor(std::string* storage) const { |
| 38 | // klass_ could contain null only if we implement class unloading. |
| Mathieu Chartier | a7deef9 | 2016-02-22 14:49:04 -0800 | [diff] [blame] | 39 | return klass_.IsNull() ? "null" : klass_.Read()->GetDescriptor(storage); |
| Man Cao | 41656de | 2015-07-06 18:53:15 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| Mathieu Chartier | 0a20607 | 2019-03-28 12:29:22 -0700 | [diff] [blame] | 42 | void 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 Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | AllocRecordObjectMap::~AllocRecordObjectMap() { |
| Mathieu Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 50 | Clear(); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 53 | void 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 Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 57 | // Only visit the last recent_record_max_ number of allocation records in entries_ and mark the |
| 58 | // klass_ fields as strong roots. |
| Mathieu Chartier | a7deef9 | 2016-02-22 14:49:04 -0800 | [diff] [blame] | 59 | for (auto it = entries_.rbegin(), end = entries_.rend(); it != end; ++it) { |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 60 | AllocRecord& record = it->second; |
| Mathieu Chartier | a7deef9 | 2016-02-22 14:49:04 -0800 | [diff] [blame] | 61 | if (count > 0) { |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 62 | buffered_visitor.VisitRootIfNonNull(record.GetClassGcRoot()); |
| Mathieu Chartier | a7deef9 | 2016-02-22 14:49:04 -0800 | [diff] [blame] | 63 | --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 Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 67 | for (size_t i = 0, depth = record.GetDepth(); i < depth; ++i) { |
| 68 | const AllocRecordStackTraceElement& element = record.StackElement(i); |
| Mathieu Chartier | a7deef9 | 2016-02-22 14:49:04 -0800 | [diff] [blame] | 69 | DCHECK(element.GetMethod() != nullptr); |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 70 | element.GetMethod()->VisitRoots(buffered_visitor, kRuntimePointerSize); |
| Mathieu Chartier | a7deef9 | 2016-02-22 14:49:04 -0800 | [diff] [blame] | 71 | } |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 75 | static inline void SweepClassObject(AllocRecord* record, IsMarkedVisitor* visitor) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 76 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 77 | REQUIRES(Locks::alloc_tracker_lock_) { |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 78 | 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 Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 81 | 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 Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 92 | void AllocRecordObjectMap::SweepAllocationRecords(IsMarkedVisitor* visitor) { |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 93 | VLOG(heap) << "Start SweepAllocationRecords()"; |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 94 | 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 Chartier | a7deef9 | 2016-02-22 14:49:04 -0800 | [diff] [blame] | 96 | const size_t delete_bound = std::max(entries_.size(), recent_record_max_) - recent_record_max_; |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 97 | for (auto it = entries_.begin(), end = entries_.end(); it != end;) { |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 98 | ++count; |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 99 | // This does not need a read barrier because this is called by GC. |
| 100 | mirror::Object* old_object = it->first.Read<kWithoutReadBarrier>(); |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 101 | AllocRecord& record = it->second; |
| Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 102 | mirror::Object* new_object = old_object == nullptr ? nullptr : visitor->IsMarked(old_object); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 103 | if (new_object == nullptr) { |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 104 | if (count > delete_bound) { |
| 105 | it->first = GcRoot<mirror::Object>(nullptr); |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 106 | SweepClassObject(&record, visitor); |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 107 | ++it; |
| 108 | } else { |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 109 | it = entries_.erase(it); |
| 110 | ++count_deleted; |
| 111 | } |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 112 | } else { |
| 113 | if (old_object != new_object) { |
| 114 | it->first = GcRoot<mirror::Object>(new_object); |
| 115 | ++count_moved; |
| 116 | } |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 117 | SweepClassObject(&record, visitor); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 118 | ++it; |
| 119 | } |
| 120 | } |
| 121 | VLOG(heap) << "Deleted " << count_deleted << " allocation records"; |
| 122 | VLOG(heap) << "Updated " << count_moved << " allocation records"; |
| 123 | } |
| 124 | |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 125 | void AllocRecordObjectMap::AllowNewAllocationRecords() { |
| Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 126 | CHECK(!kUseReadBarrier); |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 127 | allow_new_record_ = true; |
| 128 | new_record_condition_.Broadcast(Thread::Current()); |
| 129 | } |
| 130 | |
| 131 | void AllocRecordObjectMap::DisallowNewAllocationRecords() { |
| Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 132 | CHECK(!kUseReadBarrier); |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 133 | allow_new_record_ = false; |
| 134 | } |
| 135 | |
| Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 136 | void AllocRecordObjectMap::BroadcastForNewAllocationRecords() { |
| Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 137 | new_record_condition_.Broadcast(Thread::Current()); |
| 138 | } |
| 139 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 140 | void 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 Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 149 | AllocRecordObjectMap* records = heap->GetAllocationRecords(); |
| 150 | if (records == nullptr) { |
| 151 | records = new AllocRecordObjectMap; |
| 152 | heap->SetAllocationRecords(records); |
| 153 | } |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 154 | CHECK(records != nullptr); |
| Mathieu Chartier | 0a20607 | 2019-03-28 12:29:22 -0700 | [diff] [blame] | 155 | records->SetMaxStackDepth(heap->GetAllocTrackerStackDepth()); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 156 | 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 Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 161 | } |
| 162 | Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints(); |
| Mathieu Chartier | 16e51be | 2016-02-23 10:37:32 -0800 | [diff] [blame] | 163 | { |
| 164 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
| 165 | heap->SetAllocTrackingEnabled(true); |
| 166 | } |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 167 | } else { |
| Mathieu Chartier | 0b8b4a6 | 2016-03-02 12:52:37 -0800 | [diff] [blame] | 168 | // Delete outside of the critical section to avoid possible lock violations like the runtime |
| 169 | // shutdown lock. |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 170 | { |
| 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 Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 177 | AllocRecordObjectMap* records = heap->GetAllocationRecords(); |
| 178 | records->Clear(); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 179 | } |
| 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 Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 185 | void AllocRecordObjectMap::RecordAllocation(Thread* self, |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 186 | ObjPtr<mirror::Object>* obj, |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 187 | size_t byte_count) { |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 188 | // Get stack trace outside of lock in case there are allocations during the stack walk. |
| 189 | // b/27858645. |
| 190 | AllocRecordStackTrace trace; |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 191 | { |
| 192 | StackHandleScope<1> hs(self); |
| 193 | auto obj_wrapper = hs.NewHandleWrapper(obj); |
| Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 194 | |
| 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 Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 213 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 214 | Heap* const heap = Runtime::Current()->GetHeap(); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 215 | if (!heap->IsAllocTrackingEnabled()) { |
| 216 | // In the process of shutting down recording, bail. |
| 217 | return; |
| 218 | } |
| 219 | |
| Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 220 | // 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 Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 223 | |
| Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 224 | // Wait for GC's sweeping to complete and allow new records. |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 225 | while (UNLIKELY((!kUseReadBarrier && !allow_new_record_) || |
| Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 226 | (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) { |
| Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 227 | // 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 Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 229 | self->CheckEmptyCheckpointFromWeakRefAccess(Locks::alloc_tracker_lock_); |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 230 | new_record_condition_.WaitHoldingLocks(self); |
| Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| Hiroshi Yamauchi | 6f0c6cd | 2016-03-18 17:17:52 -0700 | [diff] [blame] | 233 | 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 Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 239 | DCHECK_LE(Size(), alloc_record_max_); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 240 | |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 241 | // Erase extra unfilled elements. |
| 242 | trace.SetTid(self->GetTid()); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 243 | |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 244 | // Add the record. |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 245 | Put(obj->Ptr(), AllocRecord(byte_count, (*obj)->GetClass(), std::move(trace))); |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 246 | DCHECK_LE(Size(), alloc_record_max_); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| Mathieu Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 249 | void AllocRecordObjectMap::Clear() { |
| Mathieu Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 250 | entries_.clear(); |
| 251 | } |
| 252 | |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 253 | AllocRecordObjectMap::AllocRecordObjectMap() |
| 254 | : new_record_condition_("New allocation record condition", *Locks::alloc_tracker_lock_) {} |
| 255 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 256 | } // namespace gc |
| 257 | } // namespace art |