blob: 17eb7edac626f80202adc2a6e9517a0377c30c2c [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#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"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026#include "mirror/string-inl.h"
Ian Rogersc0542af2014-09-03 16:16:56 -070027#include "runtime-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "thread.h"
29#include "utils.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070030
31namespace art {
32
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070033ReferenceTable::ReferenceTable(const char* name, size_t initial_size, size_t max_size)
34 : name_(name), max_size_(max_size) {
Elliott Hughes11e45072011-08-16 17:40:46 -070035 CHECK_LE(initial_size, max_size);
36 entries_.reserve(initial_size);
37}
38
Elliott Hughesc1674ed2011-08-25 18:09:09 -070039ReferenceTable::~ReferenceTable() {
40}
41
Mathieu Chartier423d2a32013-09-12 17:33:56 -070042void ReferenceTable::Add(mirror::Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -070043 DCHECK(obj != NULL);
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080044 VerifyObject(obj);
Mathieu Chartier423d2a32013-09-12 17:33:56 -070045 if (entries_.size() >= max_size_) {
Elliott Hughes11e45072011-08-16 17:40:46 -070046 LOG(FATAL) << "ReferenceTable '" << name_ << "' "
47 << "overflowed (" << max_size_ << " entries)";
48 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070049 entries_.push_back(GcRoot<mirror::Object>(obj));
Elliott Hughes11e45072011-08-16 17:40:46 -070050}
51
Mathieu Chartier423d2a32013-09-12 17:33:56 -070052void ReferenceTable::Remove(mirror::Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -070053 // We iterate backwards on the assumption that references are LIFO.
54 for (int i = entries_.size() - 1; i >= 0; --i) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 mirror::Object* entry = entries_[i].Read();
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070056 if (entry == obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -070057 entries_.erase(entries_.begin() + i);
58 return;
59 }
60 }
61}
62
Carl Shapiro5b1982d2011-08-16 18:35:19 -070063// If "obj" is an array, return the number of elements in the array.
64// Otherwise, return zero.
Ian Rogersef7d42f2014-01-06 12:55:46 -080065static size_t GetElementCount(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -070066 // We assume the special cleared value isn't an array in the if statement below.
67 DCHECK(!Runtime::Current()->GetClearedJniWeakGlobal()->IsArrayInstance());
68 if (obj == nullptr || !obj->IsArrayInstance()) {
Elliott Hughes11e45072011-08-16 17:40:46 -070069 return 0;
70 }
71 return obj->AsArray()->GetLength();
72}
73
74struct ObjectComparator {
Mathieu Chartier38ebea42014-12-08 11:50:36 -080075 bool operator()(GcRoot<mirror::Object> root1, GcRoot<mirror::Object> root2) const
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 // TODO: enable analysis when analysis can work with the STL.
77 NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers81d425b2012-09-27 16:03:43 -070078 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070079 mirror::Object* obj1 = root1.Read<kWithoutReadBarrier>();
80 mirror::Object* obj2 = root2.Read<kWithoutReadBarrier>();
Elliott Hughes11e45072011-08-16 17:40:46 -070081 // Sort by class...
82 if (obj1->GetClass() != obj2->GetClass()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 return obj1->GetClass()->IdentityHashCode() < obj2->IdentityHashCode();
Elliott Hughes11e45072011-08-16 17:40:46 -070084 }
Mathieu Chartier38ebea42014-12-08 11:50:36 -080085 // ...then by size...
86 const size_t size1 = obj1->SizeOf();
87 const size_t size2 = obj2->SizeOf();
88 if (size1 != size2) {
89 return size1 < size2;
90 }
91 // ...and finally by identity hash code.
92 return obj1->IdentityHashCode() < obj2->IdentityHashCode();
Elliott Hughes11e45072011-08-16 17:40:46 -070093 }
94};
95
Carl Shapiro5b1982d2011-08-16 18:35:19 -070096// 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.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800101static void DumpSummaryLine(std::ostream& os, mirror::Object* obj, size_t element_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 int identical, int equiv)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700104 if (obj == NULL) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700105 os << " NULL reference (count=" << equiv << ")\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700106 return;
107 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700108 if (Runtime::Current()->IsClearedJniWeakGlobal(obj)) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700109 os << " cleared jweak (count=" << equiv << ")\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700110 return;
111 }
112
Elliott Hughes54e7df12011-09-16 11:47:04 -0700113 std::string className(PrettyTypeOf(obj));
Elliott Hughes11e45072011-08-16 17:40:46 -0700114 if (obj->IsClass()) {
115 // We're summarizing multiple instances, so using the exemplar
116 // Class' type parameter here would be misleading.
117 className = "java.lang.Class";
118 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700119 if (element_count != 0) {
120 StringAppendF(&className, " (%zd elements)", element_count);
Elliott Hughes11e45072011-08-16 17:40:46 -0700121 }
122
123 size_t total = identical + equiv + 1;
Elliott Hughes215f3142012-01-19 00:22:47 -0800124 std::string msg(StringPrintf("%5zd of %s", total, className.c_str()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700125 if (identical + equiv != 0) {
126 StringAppendF(&msg, " (%d unique instances)", equiv + 1);
127 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700128 os << " " << msg << "\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700129}
130
131size_t ReferenceTable::Size() const {
132 return entries_.size();
133}
134
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700135void ReferenceTable::Dump(std::ostream& os) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700136 os << name_ << " reference table dump:\n";
137 Dump(os, entries_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138}
Elliott Hughes11e45072011-08-16 17:40:46 -0700139
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700140void ReferenceTable::Dump(std::ostream& os, Table& entries) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141 if (entries.empty()) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700142 os << " (empty)\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700143 return;
144 }
145
146 // Dump the most recent N entries.
147 const size_t kLast = 10;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700148 size_t count = entries.size();
Elliott Hughes11e45072011-08-16 17:40:46 -0700149 int first = count - kLast;
150 if (first < 0) {
151 first = 0;
152 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700153 os << " Last " << (count - first) << " entries (of " << count << "):\n";
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800154 Runtime* runtime = Runtime::Current();
Elliott Hughes11e45072011-08-16 17:40:46 -0700155 for (int idx = count - 1; idx >= first; --idx) {
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800156 mirror::Object* ref = entries[idx].Read<kWithoutReadBarrier>();
157 if (ref == nullptr) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700158 continue;
159 }
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800160 if (runtime->IsClearedJniWeakGlobal(ref)) {
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 }
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800164 if (ref->GetClass() == nullptr) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700165 // 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()) {
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800178 mirror::String* s = ref->AsString();
Elliott Hughes73e66f72012-05-09 09:34:45 -0700179 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
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800189 // Make a copy of the table and sort it, only adding non null and not cleared elements.
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700190 Table sorted_entries;
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800191 for (GcRoot<mirror::Object>& root : entries) {
192 if (!root.IsNull() && !runtime->IsClearedJniWeakGlobal(root.Read<kWithoutReadBarrier>())) {
193 sorted_entries.push_back(root);
194 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700195 }
196 if (sorted_entries.empty()) {
197 return;
198 }
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800199 std::sort(sorted_entries.begin(), sorted_entries.end(), ObjectComparator());
Elliott Hughes11e45072011-08-16 17:40:46 -0700200
201 // Dump a summary of the whole table.
Elliott Hughes73e66f72012-05-09 09:34:45 -0700202 os << " Summary:\n";
Elliott Hughes11e45072011-08-16 17:40:46 -0700203 size_t equiv = 0;
204 size_t identical = 0;
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800205 mirror::Object* prev = nullptr;
206 for (GcRoot<mirror::Object>& root : sorted_entries) {
207 mirror::Object* current = root.Read<kWithoutReadBarrier>();
208 if (prev != nullptr) {
209 const size_t element_count = GetElementCount(prev);
210 if (current == prev) {
211 // Same reference, added more than once.
212 ++identical;
213 } else if (current->GetClass() == prev->GetClass() &&
214 GetElementCount(current) == element_count) {
215 // Same class / element count, different object.
216 ++equiv;
217 } else {
218 // Different class.
219 DumpSummaryLine(os, prev, element_count, identical, equiv);
220 equiv = 0;
221 identical = 0;
222 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700223 }
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800224 prev = current;
Elliott Hughes11e45072011-08-16 17:40:46 -0700225 }
226 // Handle the last entry.
Mathieu Chartier38ebea42014-12-08 11:50:36 -0800227 DumpSummaryLine(os, prev, GetElementCount(prev), identical, equiv);
Elliott Hughes11e45072011-08-16 17:40:46 -0700228}
229
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800230void ReferenceTable::VisitRoots(RootCallback* visitor, void* arg, uint32_t tid,
231 RootType root_type) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700232 for (GcRoot<mirror::Object>& root : entries_) {
233 root.VisitRoot(visitor, arg, tid, root_type);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700234 }
235}
236
Elliott Hughes11e45072011-08-16 17:40:46 -0700237} // namespace art