blob: 6eb74967e528474217b528a8a59a9755c3273830 [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 Marko2c8c6b62016-12-01 17:42:00 +000087size_t ClassTable::NumZygoteClasses() const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070088 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070089 size_t sum = 0;
90 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Marko2c8c6b62016-12-01 17:42:00 +000091 sum += classes_[i].Size();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070092 }
93 return sum;
94}
95
Vladimir Marko2c8c6b62016-12-01 17:42:00 +000096size_t ClassTable::NumNonZygoteClasses() const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070097 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Marko2c8c6b62016-12-01 17:42:00 +000098 return classes_.back().Size();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070099}
100
101mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700102 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800103 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700104 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800105 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700106 if (it != class_set.end()) {
Vladimir Marko2c8c6b62016-12-01 17:42:00 +0000107 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700108 }
109 }
110 return nullptr;
111}
112
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700113void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700114 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800115 classes_.back().Insert(TableSlot(klass));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700116}
117
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700118void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800119 classes_.back().Insert(TableSlot(klass));
Mathieu Chartier496577f2016-09-20 15:33:31 -0700120}
121
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700122void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700123 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800124 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700125}
126
127bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700128 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800129 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700130 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800131 auto it = class_set.Find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700132 if (it != class_set.end()) {
133 class_set.Erase(it);
134 return true;
135 }
136 }
137 return false;
138}
139
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800140uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700141 const {
142 std::string temp;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800143 return ComputeModifiedUtf8Hash(slot.Read()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700144}
145
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800146bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
147 const TableSlot& b) const {
148 if (a.Hash() != b.Hash()) {
149 std::string temp;
150 DCHECK(!a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)));
151 return false;
152 }
Vladimir Marko2c8c6b62016-12-01 17:42:00 +0000153 DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700154 std::string temp;
155 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
156}
157
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800158bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
159 const DescriptorHashPair& b) const {
160 if (!a.MaskedHashEquals(b.second)) {
161 DCHECK(!a.Read()->DescriptorEquals(b.first));
162 return false;
163 }
164 return a.Read()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700165}
166
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800167uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
168 return ComputeModifiedUtf8Hash(pair.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700169}
170
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700171bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700172 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700173 DCHECK(obj != nullptr);
174 for (GcRoot<mirror::Object>& root : strong_roots_) {
175 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700176 return false;
177 }
178 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700179 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000180 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
181 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700182 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000183 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
184 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800185 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000186 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000187 }
188 }
189 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700190 return true;
191}
192
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000193bool ClassTable::InsertOatFile(const OatFile* oat_file) {
194 WriterMutexLock mu(Thread::Current(), lock_);
195 return InsertOatFileLocked(oat_file);
196}
197
198bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
199 if (ContainsElement(oat_files_, oat_file)) {
200 return false;
201 }
202 oat_files_.push_back(oat_file);
203 return true;
204}
205
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800206size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700207 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800208 ClassSet combined;
209 // Combine all the class sets in case there are multiple, also adjusts load factor back to
210 // default in case classes were pruned.
211 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800212 for (const TableSlot& root : class_set) {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800213 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800214 }
215 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800216 const size_t ret = combined.WriteToMemory(ptr);
217 // Sanity check.
218 if (kIsDebugBuild && ptr != nullptr) {
219 size_t read_count;
220 ClassSet class_set(ptr, /*make copy*/false, &read_count);
221 class_set.Verify();
222 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800223 return ret;
224}
225
226size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
227 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800228 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800229 return read_count;
230}
231
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800232void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700233 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800234 classes_.insert(classes_.begin(), std::move(set));
235}
236
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700237void ClassTable::ClearStrongRoots() {
238 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000239 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700240 strong_roots_.clear();
241}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800242
243ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass) {
244 std::string temp;
245 data_.StoreRelaxed(Encode(klass.Ptr(),
246 MaskHash(ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp)))));
247}
248
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700249} // namespace art