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