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