Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 "reference_table.h" |
| 18 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 19 | #include "base/mutex.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 20 | #include "indirect_reference_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/array.h" |
| 22 | #include "mirror/array-inl.h" |
| 23 | #include "mirror/class.h" |
| 24 | #include "mirror/class-inl.h" |
| 25 | #include "mirror/object-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 26 | #include "mirror/string-inl.h" |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 27 | #include "runtime-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "thread.h" |
| 29 | #include "utils.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 33 | ReferenceTable::ReferenceTable(const char* name, size_t initial_size, size_t max_size) |
| 34 | : name_(name), max_size_(max_size) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 35 | CHECK_LE(initial_size, max_size); |
| 36 | entries_.reserve(initial_size); |
| 37 | } |
| 38 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 39 | ReferenceTable::~ReferenceTable() { |
| 40 | } |
| 41 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 42 | void ReferenceTable::Add(mirror::Object* obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 43 | DCHECK(obj != NULL); |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 44 | VerifyObject(obj); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 45 | if (entries_.size() >= max_size_) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 46 | LOG(FATAL) << "ReferenceTable '" << name_ << "' " |
| 47 | << "overflowed (" << max_size_ << " entries)"; |
| 48 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 49 | entries_.push_back(GcRoot<mirror::Object>(obj)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 52 | void ReferenceTable::Remove(mirror::Object* obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 53 | // We iterate backwards on the assumption that references are LIFO. |
| 54 | for (int i = entries_.size() - 1; i >= 0; --i) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 55 | mirror::Object* entry = entries_[i].Read(); |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 56 | if (entry == obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 57 | entries_.erase(entries_.begin() + i); |
| 58 | return; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Carl Shapiro | 5b1982d | 2011-08-16 18:35:19 -0700 | [diff] [blame] | 63 | // If "obj" is an array, return the number of elements in the array. |
| 64 | // Otherwise, return zero. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 65 | static size_t GetElementCount(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 66 | // We assume the special cleared value isn't an array in the if statement below. |
| 67 | DCHECK(!Runtime::Current()->GetClearedJniWeakGlobal()->IsArrayInstance()); |
| 68 | if (obj == nullptr || !obj->IsArrayInstance()) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | return obj->AsArray()->GetLength(); |
| 72 | } |
| 73 | |
| 74 | struct ObjectComparator { |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 75 | bool operator()(GcRoot<mirror::Object> root1, GcRoot<mirror::Object> root2) const |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 76 | // TODO: enable analysis when analysis can work with the STL. |
| 77 | NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 78 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 79 | mirror::Object* obj1 = root1.Read<kWithoutReadBarrier>(); |
| 80 | mirror::Object* obj2 = root2.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | 4099b78 | 2014-12-08 12:59:27 -0800 | [diff] [blame] | 81 | DCHECK(obj1 != nullptr); |
| 82 | DCHECK(obj2 != nullptr); |
| 83 | Runtime* runtime = Runtime::Current(); |
| 84 | DCHECK(!runtime->IsClearedJniWeakGlobal(obj1)); |
| 85 | DCHECK(!runtime->IsClearedJniWeakGlobal(obj2)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 86 | // Sort by class... |
| 87 | if (obj1->GetClass() != obj2->GetClass()) { |
Brian Carlstrom | 838debd | 2014-12-08 16:22:06 -0800 | [diff] [blame] | 88 | return obj1->GetClass()->IdentityHashCode() < obj2->GetClass()->IdentityHashCode(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 89 | } |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 90 | // ...then by size... |
| 91 | const size_t size1 = obj1->SizeOf(); |
| 92 | const size_t size2 = obj2->SizeOf(); |
| 93 | if (size1 != size2) { |
| 94 | return size1 < size2; |
| 95 | } |
| 96 | // ...and finally by identity hash code. |
| 97 | return obj1->IdentityHashCode() < obj2->IdentityHashCode(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 98 | } |
| 99 | }; |
| 100 | |
Carl Shapiro | 5b1982d | 2011-08-16 18:35:19 -0700 | [diff] [blame] | 101 | // Log an object with some additional info. |
| 102 | // |
| 103 | // Pass in the number of elements in the array (or 0 if this is not an |
| 104 | // array object), and the number of additional objects that are identical |
| 105 | // or equivalent to the original. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 106 | static void DumpSummaryLine(std::ostream& os, mirror::Object* obj, size_t element_count, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 107 | int identical, int equiv) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 108 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 109 | if (obj == NULL) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 110 | os << " NULL reference (count=" << equiv << ")\n"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 113 | if (Runtime::Current()->IsClearedJniWeakGlobal(obj)) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 114 | os << " cleared jweak (count=" << equiv << ")\n"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 118 | std::string className(PrettyTypeOf(obj)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 119 | if (obj->IsClass()) { |
| 120 | // We're summarizing multiple instances, so using the exemplar |
| 121 | // Class' type parameter here would be misleading. |
| 122 | className = "java.lang.Class"; |
| 123 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 124 | if (element_count != 0) { |
| 125 | StringAppendF(&className, " (%zd elements)", element_count); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | size_t total = identical + equiv + 1; |
Elliott Hughes | 215f314 | 2012-01-19 00:22:47 -0800 | [diff] [blame] | 129 | std::string msg(StringPrintf("%5zd of %s", total, className.c_str())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 130 | if (identical + equiv != 0) { |
| 131 | StringAppendF(&msg, " (%d unique instances)", equiv + 1); |
| 132 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 133 | os << " " << msg << "\n"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | size_t ReferenceTable::Size() const { |
| 137 | return entries_.size(); |
| 138 | } |
| 139 | |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 140 | void ReferenceTable::Dump(std::ostream& os) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 141 | os << name_ << " reference table dump:\n"; |
| 142 | Dump(os, entries_); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 143 | } |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 144 | |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 145 | void ReferenceTable::Dump(std::ostream& os, Table& entries) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 146 | if (entries.empty()) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 147 | os << " (empty)\n"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Dump the most recent N entries. |
| 152 | const size_t kLast = 10; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 153 | size_t count = entries.size(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 154 | int first = count - kLast; |
| 155 | if (first < 0) { |
| 156 | first = 0; |
| 157 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 158 | os << " Last " << (count - first) << " entries (of " << count << "):\n"; |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 159 | Runtime* runtime = Runtime::Current(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 160 | for (int idx = count - 1; idx >= first; --idx) { |
Mathieu Chartier | 4099b78 | 2014-12-08 12:59:27 -0800 | [diff] [blame] | 161 | mirror::Object* ref = entries[idx].Read(); |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 162 | if (ref == nullptr) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 163 | continue; |
| 164 | } |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 165 | if (runtime->IsClearedJniWeakGlobal(ref)) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 166 | os << StringPrintf(" %5d: cleared jweak\n", idx); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 167 | continue; |
| 168 | } |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 169 | if (ref->GetClass() == nullptr) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 170 | // should only be possible right after a plain dvmMalloc(). |
| 171 | size_t size = ref->SizeOf(); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 172 | os << StringPrintf(" %5d: %p (raw) (%zd bytes)\n", idx, ref, size); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 173 | continue; |
| 174 | } |
| 175 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 176 | std::string className(PrettyTypeOf(ref)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 177 | |
| 178 | std::string extras; |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 179 | size_t element_count = GetElementCount(ref); |
| 180 | if (element_count != 0) { |
| 181 | StringAppendF(&extras, " (%zd elements)", element_count); |
| 182 | } else if (ref->GetClass()->IsStringClass()) { |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 183 | mirror::String* s = ref->AsString(); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 184 | std::string utf8(s->ToModifiedUtf8()); |
| 185 | if (s->GetLength() <= 16) { |
| 186 | StringAppendF(&extras, " \"%s\"", utf8.c_str()); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 187 | } else { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 188 | StringAppendF(&extras, " \"%.16s... (%d chars)", utf8.c_str(), s->GetLength()); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 189 | } |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 190 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 191 | os << StringPrintf(" %5d: ", idx) << ref << " " << className << extras << "\n"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 194 | // Make a copy of the table and sort it, only adding non null and not cleared elements. |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 195 | Table sorted_entries; |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 196 | for (GcRoot<mirror::Object>& root : entries) { |
Mathieu Chartier | 4099b78 | 2014-12-08 12:59:27 -0800 | [diff] [blame] | 197 | if (!root.IsNull() && !runtime->IsClearedJniWeakGlobal(root.Read())) { |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 198 | sorted_entries.push_back(root); |
| 199 | } |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 200 | } |
| 201 | if (sorted_entries.empty()) { |
| 202 | return; |
| 203 | } |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 204 | std::sort(sorted_entries.begin(), sorted_entries.end(), ObjectComparator()); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 205 | |
| 206 | // Dump a summary of the whole table. |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 207 | os << " Summary:\n"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 208 | size_t equiv = 0; |
| 209 | size_t identical = 0; |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 210 | mirror::Object* prev = nullptr; |
| 211 | for (GcRoot<mirror::Object>& root : sorted_entries) { |
| 212 | mirror::Object* current = root.Read<kWithoutReadBarrier>(); |
| 213 | if (prev != nullptr) { |
| 214 | const size_t element_count = GetElementCount(prev); |
| 215 | if (current == prev) { |
| 216 | // Same reference, added more than once. |
| 217 | ++identical; |
| 218 | } else if (current->GetClass() == prev->GetClass() && |
| 219 | GetElementCount(current) == element_count) { |
| 220 | // Same class / element count, different object. |
| 221 | ++equiv; |
| 222 | } else { |
| 223 | // Different class. |
| 224 | DumpSummaryLine(os, prev, element_count, identical, equiv); |
| 225 | equiv = 0; |
| 226 | identical = 0; |
| 227 | } |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 228 | } |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 229 | prev = current; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 230 | } |
| 231 | // Handle the last entry. |
Mathieu Chartier | 38ebea4 | 2014-12-08 11:50:36 -0800 | [diff] [blame] | 232 | DumpSummaryLine(os, prev, GetElementCount(prev), identical, equiv); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 235 | void ReferenceTable::VisitRoots(RootCallback* visitor, void* arg, uint32_t tid, |
| 236 | RootType root_type) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 237 | for (GcRoot<mirror::Object>& root : entries_) { |
| 238 | root.VisitRoot(visitor, arg, tid, root_type); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 242 | } // namespace art |