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