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