blob: cdb3004440be4dae794e1d94967c20930d6b1f47 [file] [log] [blame]
Elliott Hughes11e45072011-08-16 17:40:46 -07001/*
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 Hughes76b61672012-12-12 17:47:30 -080019#include "base/mutex.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070020#include "indirect_reference_table.h"
21
Elliott Hughes11e45072011-08-16 17:40:46 -070022#include "object.h"
23
24namespace art {
25
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070026ReferenceTable::ReferenceTable(const char* name, size_t initial_size, size_t max_size)
27 : name_(name), max_size_(max_size) {
Elliott Hughes11e45072011-08-16 17:40:46 -070028 CHECK_LE(initial_size, max_size);
29 entries_.reserve(initial_size);
30}
31
Elliott Hughesc1674ed2011-08-25 18:09:09 -070032ReferenceTable::~ReferenceTable() {
33}
34
Elliott Hughes75770752011-08-24 17:52:38 -070035void ReferenceTable::Add(const Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -070036 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 Hughes75770752011-08-24 17:52:38 -070044void ReferenceTable::Remove(const Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -070045 // 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 Shapiro5b1982d2011-08-16 18:35:19 -070054// If "obj" is an array, return the number of elements in the array.
55// Otherwise, return zero.
Elliott Hughes73e66f72012-05-09 09:34:45 -070056static size_t GetElementCount(const Object* obj) {
Brian Carlstromb63ec392011-08-27 17:38:27 -070057 if (obj == NULL || obj == kClearedJniWeakGlobal || !obj->IsArrayInstance()) {
Elliott Hughes11e45072011-08-16 17:40:46 -070058 return 0;
59 }
60 return obj->AsArray()->GetLength();
61}
62
63struct ObjectComparator {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 bool operator()(const Object* obj1, const Object* obj2)
65 // TODO: enable analysis when analysis can work with the STL.
66 NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers81d425b2012-09-27 16:03:43 -070067 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
Elliott Hughes11e45072011-08-16 17:40:46 -070068 // Ensure null references and cleared jweaks appear at the end.
69 if (obj1 == NULL) {
70 return true;
71 } else if (obj2 == NULL) {
72 return false;
73 }
74 if (obj1 == kClearedJniWeakGlobal) {
75 return true;
76 } else if (obj2 == kClearedJniWeakGlobal) {
77 return false;
78 }
79
80 // Sort by class...
81 if (obj1->GetClass() != obj2->GetClass()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 return obj1->GetClass()->IdentityHashCode() < obj2->IdentityHashCode();
Elliott Hughes11e45072011-08-16 17:40:46 -070083 } else {
84 // ...then by size...
85 size_t count1 = obj1->SizeOf();
86 size_t count2 = obj2->SizeOf();
87 if (count1 != count2) {
88 return count1 < count2;
89 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 // ...and finally by identity hash code.
91 return obj1->IdentityHashCode() < obj2->IdentityHashCode();
Elliott Hughes11e45072011-08-16 17:40:46 -070092 }
93 }
94 }
95};
96
Carl Shapiro5b1982d2011-08-16 18:35:19 -070097// Log an object with some additional info.
98//
99// Pass in the number of elements in the array (or 0 if this is not an
100// array object), and the number of additional objects that are identical
101// or equivalent to the original.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102static void DumpSummaryLine(std::ostream& os, const Object* obj, size_t element_count,
103 int identical, int equiv)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700105 if (obj == NULL) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700106 os << " NULL reference (count=" << equiv << ")\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700107 return;
108 }
109 if (obj == kClearedJniWeakGlobal) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700110 os << " cleared jweak (count=" << equiv << ")\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700111 return;
112 }
113
Elliott Hughes54e7df12011-09-16 11:47:04 -0700114 std::string className(PrettyTypeOf(obj));
Elliott Hughes11e45072011-08-16 17:40:46 -0700115 if (obj->IsClass()) {
116 // We're summarizing multiple instances, so using the exemplar
117 // Class' type parameter here would be misleading.
118 className = "java.lang.Class";
119 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700120 if (element_count != 0) {
121 StringAppendF(&className, " (%zd elements)", element_count);
Elliott Hughes11e45072011-08-16 17:40:46 -0700122 }
123
124 size_t total = identical + equiv + 1;
Elliott Hughes215f3142012-01-19 00:22:47 -0800125 std::string msg(StringPrintf("%5zd of %s", total, className.c_str()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700126 if (identical + equiv != 0) {
127 StringAppendF(&msg, " (%d unique instances)", equiv + 1);
128 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700129 os << " " << msg << "\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700130}
131
132size_t ReferenceTable::Size() const {
133 return entries_.size();
134}
135
Elliott Hughes73e66f72012-05-09 09:34:45 -0700136void ReferenceTable::Dump(std::ostream& os) const {
137 os << name_ << " reference table dump:\n";
138 Dump(os, entries_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700139}
Elliott Hughes11e45072011-08-16 17:40:46 -0700140
Elliott Hughes73e66f72012-05-09 09:34:45 -0700141void ReferenceTable::Dump(std::ostream& os, const Table& entries) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700142 if (entries.empty()) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700143 os << " (empty)\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700144 return;
145 }
146
147 // Dump the most recent N entries.
148 const size_t kLast = 10;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149 size_t count = entries.size();
Elliott Hughes11e45072011-08-16 17:40:46 -0700150 int first = count - kLast;
151 if (first < 0) {
152 first = 0;
153 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700154 os << " Last " << (count - first) << " entries (of " << count << "):\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700155 for (int idx = count - 1; idx >= first; --idx) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156 const Object* ref = entries[idx];
Elliott Hughes11e45072011-08-16 17:40:46 -0700157 if (ref == NULL) {
158 continue;
159 }
160 if (ref == kClearedJniWeakGlobal) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700161 os << StringPrintf(" %5d: cleared jweak\n", idx);
Elliott Hughes11e45072011-08-16 17:40:46 -0700162 continue;
163 }
164 if (ref->GetClass() == NULL) {
165 // should only be possible right after a plain dvmMalloc().
166 size_t size = ref->SizeOf();
Elliott Hughes73e66f72012-05-09 09:34:45 -0700167 os << StringPrintf(" %5d: %p (raw) (%zd bytes)\n", idx, ref, size);
Elliott Hughes11e45072011-08-16 17:40:46 -0700168 continue;
169 }
170
Elliott Hughes54e7df12011-09-16 11:47:04 -0700171 std::string className(PrettyTypeOf(ref));
Elliott Hughes11e45072011-08-16 17:40:46 -0700172
173 std::string extras;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700174 size_t element_count = GetElementCount(ref);
175 if (element_count != 0) {
176 StringAppendF(&extras, " (%zd elements)", element_count);
177 } else if (ref->GetClass()->IsStringClass()) {
178 String* s = const_cast<Object*>(ref)->AsString();
179 std::string utf8(s->ToModifiedUtf8());
180 if (s->GetLength() <= 16) {
181 StringAppendF(&extras, " \"%s\"", utf8.c_str());
Elliott Hughes11e45072011-08-16 17:40:46 -0700182 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700183 StringAppendF(&extras, " \"%.16s... (%d chars)", utf8.c_str(), s->GetLength());
Elliott Hughes11e45072011-08-16 17:40:46 -0700184 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700185 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700186 os << StringPrintf(" %5d: ", idx) << ref << " " << className << extras << "\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700187 }
188
189 // Make a copy of the table and sort it.
Elliott Hughes410c0c82011-09-01 17:58:25 -0700190 Table sorted_entries(entries.begin(), entries.end());
Elliott Hughes11e45072011-08-16 17:40:46 -0700191 std::sort(sorted_entries.begin(), sorted_entries.end(), ObjectComparator());
192
193 // Remove any uninteresting stuff from the list. The sort moved them all to the end.
194 while (!sorted_entries.empty() && sorted_entries.back() == NULL) {
195 sorted_entries.pop_back();
196 }
197 while (!sorted_entries.empty() && sorted_entries.back() == kClearedJniWeakGlobal) {
198 sorted_entries.pop_back();
199 }
200 if (sorted_entries.empty()) {
201 return;
202 }
203
204 // Dump a summary of the whole table.
Elliott Hughes73e66f72012-05-09 09:34:45 -0700205 os << " Summary:\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700206 size_t equiv = 0;
207 size_t identical = 0;
208 for (size_t idx = 1; idx < count; idx++) {
Elliott Hughes75770752011-08-24 17:52:38 -0700209 const Object* prev = sorted_entries[idx-1];
210 const Object* current = sorted_entries[idx];
Elliott Hughes73e66f72012-05-09 09:34:45 -0700211 size_t element_count = GetElementCount(prev);
Elliott Hughes11e45072011-08-16 17:40:46 -0700212 if (current == prev) {
213 // Same reference, added more than once.
214 identical++;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700215 } else if (current->GetClass() == prev->GetClass() && GetElementCount(current) == element_count) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700216 // Same class / element count, different object.
217 equiv++;
218 } else {
219 // Different class.
Elliott Hughes73e66f72012-05-09 09:34:45 -0700220 DumpSummaryLine(os, prev, element_count, identical, equiv);
Elliott Hughes11e45072011-08-16 17:40:46 -0700221 equiv = identical = 0;
222 }
223 }
224 // Handle the last entry.
Elliott Hughes73e66f72012-05-09 09:34:45 -0700225 DumpSummaryLine(os, sorted_entries.back(), GetElementCount(sorted_entries.back()), identical, equiv);
Elliott Hughes11e45072011-08-16 17:40:46 -0700226}
227
Elliott Hughes410c0c82011-09-01 17:58:25 -0700228void ReferenceTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
229 typedef Table::const_iterator It; // TODO: C++0x auto
230 for (It it = entries_.begin(), end = entries_.end(); it != end; ++it) {
231 visitor(*it, arg);
232 }
233}
234
Elliott Hughes11e45072011-08-16 17:40:46 -0700235} // namespace art