blob: dd3276550eeb51108b38f8b3cb0a04b53d4f2ec9 [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 Hughes6c1a3942011-08-17 15:00:06 -070019#include "indirect_reference_table.h"
20
Elliott Hughes11e45072011-08-16 17:40:46 -070021#include "object.h"
22
23namespace art {
24
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070025ReferenceTable::ReferenceTable(const char* name, size_t initial_size, size_t max_size)
26 : name_(name), max_size_(max_size) {
Elliott Hughes11e45072011-08-16 17:40:46 -070027 CHECK_LE(initial_size, max_size);
28 entries_.reserve(initial_size);
29}
30
Elliott Hughesc1674ed2011-08-25 18:09:09 -070031ReferenceTable::~ReferenceTable() {
32}
33
Elliott Hughes75770752011-08-24 17:52:38 -070034void ReferenceTable::Add(const Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -070035 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 Hughes75770752011-08-24 17:52:38 -070043void ReferenceTable::Remove(const Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -070044 // 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 Shapiro5b1982d2011-08-16 18:35:19 -070053// If "obj" is an array, return the number of elements in the array.
54// Otherwise, return zero.
Elliott Hughes11e45072011-08-16 17:40:46 -070055size_t GetElementCount(const Object* obj) {
Brian Carlstromb63ec392011-08-27 17:38:27 -070056 if (obj == NULL || obj == kClearedJniWeakGlobal || !obj->IsArrayInstance()) {
Elliott Hughes11e45072011-08-16 17:40:46 -070057 return 0;
58 }
59 return obj->AsArray()->GetLength();
60}
61
62struct ObjectComparator {
Elliott Hughesba8eee12012-01-24 20:25:24 -080063 bool operator()(const Object* obj1, const Object* obj2) {
Elliott Hughes11e45072011-08-16 17:40:46 -070064 // Ensure null references and cleared jweaks appear at the end.
65 if (obj1 == NULL) {
66 return true;
67 } else if (obj2 == NULL) {
68 return false;
69 }
70 if (obj1 == kClearedJniWeakGlobal) {
71 return true;
72 } else if (obj2 == kClearedJniWeakGlobal) {
73 return false;
74 }
75
76 // Sort by class...
77 if (obj1->GetClass() != obj2->GetClass()) {
78 return reinterpret_cast<uintptr_t>(obj1->GetClass()) <
79 reinterpret_cast<uintptr_t>(obj2->GetClass());
80 } else {
81 // ...then by size...
82 size_t count1 = obj1->SizeOf();
83 size_t count2 = obj2->SizeOf();
84 if (count1 != count2) {
85 return count1 < count2;
86 } else {
87 // ...and finally by address.
88 return reinterpret_cast<uintptr_t>(obj1) <
89 reinterpret_cast<uintptr_t>(obj2);
90 }
91 }
92 }
93};
94
Carl Shapiro5b1982d2011-08-16 18:35:19 -070095// Log an object with some additional info.
96//
97// Pass in the number of elements in the array (or 0 if this is not an
98// array object), and the number of additional objects that are identical
99// or equivalent to the original.
Elliott Hughes11e45072011-08-16 17:40:46 -0700100void LogSummaryLine(const Object* obj, size_t elems, int identical, int equiv) {
101 if (obj == NULL) {
102 LOG(WARNING) << " NULL reference (count=" << equiv << ")";
103 return;
104 }
105 if (obj == kClearedJniWeakGlobal) {
106 LOG(WARNING) << " cleared jweak (count=" << equiv << ")";
107 return;
108 }
109
Elliott Hughes54e7df12011-09-16 11:47:04 -0700110 std::string className(PrettyTypeOf(obj));
Elliott Hughes11e45072011-08-16 17:40:46 -0700111 if (obj->IsClass()) {
112 // We're summarizing multiple instances, so using the exemplar
113 // Class' type parameter here would be misleading.
114 className = "java.lang.Class";
115 }
116 if (elems != 0) {
117 StringAppendF(&className, " (%zd elements)", elems);
118 }
119
120 size_t total = identical + equiv + 1;
Elliott Hughes215f3142012-01-19 00:22:47 -0800121 std::string msg(StringPrintf("%5zd of %s", total, className.c_str()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700122 if (identical + equiv != 0) {
123 StringAppendF(&msg, " (%d unique instances)", equiv + 1);
124 }
125 LOG(WARNING) << " " << msg;
126}
127
128size_t ReferenceTable::Size() const {
129 return entries_.size();
130}
131
Elliott Hughes11e45072011-08-16 17:40:46 -0700132void ReferenceTable::Dump() const {
133 LOG(WARNING) << name_ << " reference table dump:";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700134 Dump(entries_);
135}
Elliott Hughes11e45072011-08-16 17:40:46 -0700136
Elliott Hughes410c0c82011-09-01 17:58:25 -0700137void ReferenceTable::Dump(const Table& entries) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138 if (entries.empty()) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700139 LOG(WARNING) << " (empty)";
140 return;
141 }
142
143 // Dump the most recent N entries.
144 const size_t kLast = 10;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700145 size_t count = entries.size();
Elliott Hughes11e45072011-08-16 17:40:46 -0700146 int first = count - kLast;
147 if (first < 0) {
148 first = 0;
149 }
150 LOG(WARNING) << " Last " << (count - first) << " entries (of " << count << "):";
151 for (int idx = count - 1; idx >= first; --idx) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700152 const Object* ref = entries[idx];
Elliott Hughes11e45072011-08-16 17:40:46 -0700153 if (ref == NULL) {
154 continue;
155 }
156 if (ref == kClearedJniWeakGlobal) {
157 LOG(WARNING) << StringPrintf(" %5d: cleared jweak", idx);
158 continue;
159 }
160 if (ref->GetClass() == NULL) {
161 // should only be possible right after a plain dvmMalloc().
162 size_t size = ref->SizeOf();
163 LOG(WARNING) << StringPrintf(" %5d: %p (raw) (%zd bytes)", idx, ref, size);
164 continue;
165 }
166
Elliott Hughes54e7df12011-09-16 11:47:04 -0700167 std::string className(PrettyTypeOf(ref));
Elliott Hughes11e45072011-08-16 17:40:46 -0700168
169 std::string extras;
170 size_t elems = GetElementCount(ref);
171 if (elems != 0) {
172 StringAppendF(&extras, " (%zd elements)", elems);
173 }
174#if 0
175 // TODO: support dumping string data.
176 else if (ref->GetClass() == gDvm.classJavaLangString) {
177 const StringObject* str = reinterpret_cast<const StringObject*>(ref);
178 extras += " \"";
179 size_t count = 0;
180 char* s = dvmCreateCstrFromString(str);
181 char* p = s;
182 for (; *p && count < 16; ++p, ++count) {
183 extras += *p;
184 }
185 if (*p == 0) {
186 extras += "\"";
187 } else {
188 StringAppendF(&extras, "... (%d chars)", str->length());
189 }
190 free(s);
191 }
192#endif
193 LOG(WARNING) << StringPrintf(" %5d: ", idx) << ref << " " << className << extras;
194 }
195
196 // Make a copy of the table and sort it.
Elliott Hughes410c0c82011-09-01 17:58:25 -0700197 Table sorted_entries(entries.begin(), entries.end());
Elliott Hughes11e45072011-08-16 17:40:46 -0700198 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 LOG(WARNING) << " Summary:";
213 size_t equiv = 0;
214 size_t identical = 0;
215 for (size_t idx = 1; idx < count; idx++) {
Elliott Hughes75770752011-08-24 17:52:38 -0700216 const Object* prev = sorted_entries[idx-1];
217 const Object* current = sorted_entries[idx];
Elliott Hughes11e45072011-08-16 17:40:46 -0700218 size_t elems = GetElementCount(prev);
219 if (current == prev) {
220 // Same reference, added more than once.
221 identical++;
222 } else if (current->GetClass() == prev->GetClass() && GetElementCount(current) == elems) {
223 // Same class / element count, different object.
224 equiv++;
225 } else {
226 // Different class.
227 LogSummaryLine(prev, elems, identical, equiv);
228 equiv = identical = 0;
229 }
230 }
231 // Handle the last entry.
232 LogSummaryLine(sorted_entries.back(), GetElementCount(sorted_entries.back()), identical, equiv);
233}
234
Elliott Hughes410c0c82011-09-01 17:58:25 -0700235void ReferenceTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
236 typedef Table::const_iterator It; // TODO: C++0x auto
237 for (It it = entries_.begin(), end = entries_.end(); it != end; ++it) {
238 visitor(*it, arg);
239 }
240}
241
Elliott Hughes11e45072011-08-16 17:40:46 -0700242} // namespace art