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