blob: ec33e5ef808a69145e3d9dd0eac67d141e418ed1 [file] [log] [blame]
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -07001/*
2 * Copyright (C) 2015 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 "class_table.h"
18
19#include "mirror/class-inl.h"
20
21namespace art {
22
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070023ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -070024 Runtime* const runtime = Runtime::Current();
25 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
26 runtime->GetHashTableMaxLoadFactor()));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070027}
28
29void ClassTable::FreezeSnapshot() {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070030 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070031 classes_.push_back(ClassSet());
32}
33
Mathieu Chartier28357fa2016-10-18 16:27:40 -070034bool ClassTable::Contains(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070035 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070036 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080037 auto it = class_set.Find(TableSlot(klass));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070038 if (it != class_set.end()) {
39 return it->Read() == klass;
40 }
41 }
42 return false;
43}
44
Mathieu Chartier28357fa2016-10-18 16:27:40 -070045mirror::Class* ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070046 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080047 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080048 auto it = class_set.Find(TableSlot(klass));
Mathieu Chartierfbc31082016-01-24 11:59:56 -080049 if (it != class_set.end()) {
50 return it->Read();
51 }
52 }
53 return nullptr;
54}
55
Pirama Arumuga Nainar813b9c42016-08-25 23:42:50 -070056// Bug: http://b/31104323 Ignore -Wunreachable-code from the for loop below
57#pragma clang diagnostic push
58#pragma clang diagnostic ignored "-Wunreachable-code"
59
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070060mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070061 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070062 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080063 DescriptorHashPair pair(descriptor, hash);
64 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070065 if (kIsDebugBuild && existing_it == classes_.back().end()) {
66 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080067 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070068 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
69 }
70 }
71 LOG(FATAL) << "Updating class not found " << descriptor;
72 }
73 mirror::Class* const existing = existing_it->Read();
74 CHECK_NE(existing, klass) << descriptor;
75 CHECK(!existing->IsResolved()) << descriptor;
76 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
77 CHECK(!klass->IsTemp()) << descriptor;
78 VerifyObject(klass);
79 // Update the element in the hash set with the new class. This is safe to do since the descriptor
80 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080081 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070082 return existing;
83}
84
Pirama Arumuga Nainar813b9c42016-08-25 23:42:50 -070085#pragma clang diagnostic pop // http://b/31104323
86
Vladimir Markoc5798bf2016-12-09 10:20:54 +000087size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
88 const ClassSet& set) const {
89 size_t count = 0;
90 for (const TableSlot& root : set) {
91 if (root.Read()->GetClassLoader() == defining_loader) {
92 ++count;
93 }
94 }
95 return count;
96}
97
98size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070099 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700100 size_t sum = 0;
101 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000102 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700103 }
104 return sum;
105}
106
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000107size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700108 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000109 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700110}
111
112mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700113 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800114 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700115 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800116 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700117 if (it != class_set.end()) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000118 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700119 }
120 }
121 return nullptr;
122}
123
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700124void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700125 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800126 classes_.back().Insert(TableSlot(klass));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700127}
128
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700129void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800130 classes_.back().Insert(TableSlot(klass));
Mathieu Chartier496577f2016-09-20 15:33:31 -0700131}
132
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700133void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700134 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800135 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700136}
137
138bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700139 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800140 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700141 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800142 auto it = class_set.Find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700143 if (it != class_set.end()) {
144 class_set.Erase(it);
145 return true;
146 }
147 }
148 return false;
149}
150
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800151uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700152 const {
153 std::string temp;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800154 return ComputeModifiedUtf8Hash(slot.Read()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700155}
156
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800157bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
158 const TableSlot& b) const {
159 if (a.Hash() != b.Hash()) {
160 std::string temp;
161 DCHECK(!a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)));
162 return false;
163 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700164 std::string temp;
165 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
166}
167
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800168bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
169 const DescriptorHashPair& b) const {
170 if (!a.MaskedHashEquals(b.second)) {
171 DCHECK(!a.Read()->DescriptorEquals(b.first));
172 return false;
173 }
174 return a.Read()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700175}
176
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800177uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
178 return ComputeModifiedUtf8Hash(pair.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700179}
180
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700181bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700182 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700183 DCHECK(obj != nullptr);
184 for (GcRoot<mirror::Object>& root : strong_roots_) {
185 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700186 return false;
187 }
188 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700189 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000190 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
191 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700192 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000193 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
194 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800195 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000196 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000197 }
198 }
199 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700200 return true;
201}
202
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000203bool ClassTable::InsertOatFile(const OatFile* oat_file) {
204 WriterMutexLock mu(Thread::Current(), lock_);
205 return InsertOatFileLocked(oat_file);
206}
207
208bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
209 if (ContainsElement(oat_files_, oat_file)) {
210 return false;
211 }
212 oat_files_.push_back(oat_file);
213 return true;
214}
215
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800216size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700217 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800218 ClassSet combined;
219 // Combine all the class sets in case there are multiple, also adjusts load factor back to
220 // default in case classes were pruned.
221 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800222 for (const TableSlot& root : class_set) {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800223 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800224 }
225 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800226 const size_t ret = combined.WriteToMemory(ptr);
227 // Sanity check.
228 if (kIsDebugBuild && ptr != nullptr) {
229 size_t read_count;
230 ClassSet class_set(ptr, /*make copy*/false, &read_count);
231 class_set.Verify();
232 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800233 return ret;
234}
235
236size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
237 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800238 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800239 return read_count;
240}
241
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800242void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700243 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800244 classes_.insert(classes_.begin(), std::move(set));
245}
246
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700247void ClassTable::ClearStrongRoots() {
248 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000249 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700250 strong_roots_.clear();
251}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800252
253ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass) {
254 std::string temp;
255 data_.StoreRelaxed(Encode(klass.Ptr(),
256 MaskHash(ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp)))));
257}
258
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700259} // namespace art