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