blob: 03921a132abca21e4aa27751739fd748c738d1f2 [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
Andreas Gampe2ff3b972017-06-05 18:14:53 -070017#include "class_table-inl.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070018
Andreas Gampe5678db52017-06-08 14:11:18 -070019#include "base/stl_util.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070020#include "mirror/class-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070021#include "oat_file.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070022
23namespace art {
24
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070025ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -070026 Runtime* const runtime = Runtime::Current();
27 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
28 runtime->GetHashTableMaxLoadFactor()));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070029}
30
31void ClassTable::FreezeSnapshot() {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070032 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070033 classes_.push_back(ClassSet());
34}
35
Vladimir Marko1fe58392019-04-10 16:14:56 +010036ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor,
37 ObjPtr<mirror::Class> klass,
38 size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070039 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070040 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080041 DescriptorHashPair pair(descriptor, hash);
42 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartier1a8d83b2020-10-12 15:43:23 -070043 if (existing_it == classes_.back().end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070044 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080045 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070046 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
47 }
48 }
49 LOG(FATAL) << "Updating class not found " << descriptor;
50 }
Vladimir Marko1fe58392019-04-10 16:14:56 +010051 const ObjPtr<mirror::Class> existing = existing_it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070052 CHECK_NE(existing, klass) << descriptor;
53 CHECK(!existing->IsResolved()) << descriptor;
Vladimir Marko2c64a832018-01-04 11:31:56 +000054 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070055 CHECK(!klass->IsTemp()) << descriptor;
56 VerifyObject(klass);
57 // Update the element in the hash set with the new class. This is safe to do since the descriptor
58 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080059 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070060 return existing;
61}
62
Vladimir Markoc5798bf2016-12-09 10:20:54 +000063size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
64 const ClassSet& set) const {
65 size_t count = 0;
66 for (const TableSlot& root : set) {
67 if (root.Read()->GetClassLoader() == defining_loader) {
68 ++count;
69 }
70 }
71 return count;
72}
73
74size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070075 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070076 size_t sum = 0;
77 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +000078 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070079 }
80 return sum;
81}
82
Vladimir Markoc5798bf2016-12-09 10:20:54 +000083size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070084 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +000085 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070086}
87
Vladimir Marko8d6768d2017-03-14 10:13:21 +000088size_t ClassTable::NumReferencedZygoteClasses() const {
89 ReaderMutexLock mu(Thread::Current(), lock_);
90 size_t sum = 0;
91 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Marko54159c62018-06-20 14:30:08 +010092 sum += classes_[i].size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +000093 }
94 return sum;
95}
96
97size_t ClassTable::NumReferencedNonZygoteClasses() const {
98 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Marko54159c62018-06-20 14:30:08 +010099 return classes_.back().size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000100}
101
Vladimir Marko1fe58392019-04-10 16:14:56 +0100102ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800103 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800104 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700105 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800106 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700107 if (it != class_set.end()) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000108 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700109 }
110 }
111 return nullptr;
112}
113
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700114void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Vladimir Marko1dab5752021-02-25 15:41:33 +0000115 InsertWithHash(klass, TableSlot::HashDescriptor(klass));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700116}
117
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700118void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700119 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800120 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700121}
122
123bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800124 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800125 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700126 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100127 auto it = class_set.find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700128 if (it != class_set.end()) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100129 class_set.erase(it);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700130 return true;
131 }
132 }
133 return false;
134}
135
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700136bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700137 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700138 DCHECK(obj != nullptr);
139 for (GcRoot<mirror::Object>& root : strong_roots_) {
140 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700141 return false;
142 }
143 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700144 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000145 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
146 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700147 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000148 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
149 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800150 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000151 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000152 }
153 }
154 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700155 return true;
156}
157
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000158bool ClassTable::InsertOatFile(const OatFile* oat_file) {
159 WriterMutexLock mu(Thread::Current(), lock_);
160 return InsertOatFileLocked(oat_file);
161}
162
163bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
164 if (ContainsElement(oat_files_, oat_file)) {
165 return false;
166 }
167 oat_files_.push_back(oat_file);
168 return true;
169}
170
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800171size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
172 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800173 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800174 return read_count;
175}
176
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800177void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700178 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800179 classes_.insert(classes_.begin(), std::move(set));
180}
181
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700182void ClassTable::ClearStrongRoots() {
183 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000184 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700185 strong_roots_.clear();
186}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800187
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800188ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
189 : TableSlot(klass, HashDescriptor(klass)) {}
190
191uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800192 std::string temp;
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800193 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800194}
195
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700196} // namespace art