blob: 429f9268ec081c86f97115192f62010b9eb42f6b [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"
Vladimir Marko65258db2022-03-31 12:39:21 +000021#include "mirror/string-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070022#include "oat_file.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070023
24namespace art {
25
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070026ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -070027 Runtime* const runtime = Runtime::Current();
28 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
29 runtime->GetHashTableMaxLoadFactor()));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070030}
31
32void ClassTable::FreezeSnapshot() {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070033 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Marko3ab24742022-03-17 07:11:11 +000034 // Propagate the min/max load factor from the old active set.
35 DCHECK(!classes_.empty());
36 const ClassSet& last_set = classes_.back();
37 ClassSet new_set(last_set.GetMinLoadFactor(), last_set.GetMaxLoadFactor());
38 classes_.push_back(std::move(new_set));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070039}
40
Vladimir Marko1fe58392019-04-10 16:14:56 +010041ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor,
42 ObjPtr<mirror::Class> klass,
43 size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070044 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070045 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080046 DescriptorHashPair pair(descriptor, hash);
47 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartier1a8d83b2020-10-12 15:43:23 -070048 if (existing_it == classes_.back().end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070049 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080050 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070051 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
52 }
53 }
54 LOG(FATAL) << "Updating class not found " << descriptor;
55 }
Vladimir Marko1fe58392019-04-10 16:14:56 +010056 const ObjPtr<mirror::Class> existing = existing_it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070057 CHECK_NE(existing, klass) << descriptor;
58 CHECK(!existing->IsResolved()) << descriptor;
Vladimir Marko2c64a832018-01-04 11:31:56 +000059 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070060 CHECK(!klass->IsTemp()) << descriptor;
61 VerifyObject(klass);
62 // Update the element in the hash set with the new class. This is safe to do since the descriptor
63 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080064 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070065 return existing;
66}
67
Vladimir Markoc5798bf2016-12-09 10:20:54 +000068size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
69 const ClassSet& set) const {
70 size_t count = 0;
71 for (const TableSlot& root : set) {
72 if (root.Read()->GetClassLoader() == defining_loader) {
73 ++count;
74 }
75 }
76 return count;
77}
78
79size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070080 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070081 size_t sum = 0;
82 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +000083 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070084 }
85 return sum;
86}
87
Vladimir Markoc5798bf2016-12-09 10:20:54 +000088size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070089 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +000090 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070091}
92
Vladimir Marko8d6768d2017-03-14 10:13:21 +000093size_t ClassTable::NumReferencedZygoteClasses() const {
94 ReaderMutexLock mu(Thread::Current(), lock_);
95 size_t sum = 0;
96 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Marko54159c62018-06-20 14:30:08 +010097 sum += classes_[i].size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +000098 }
99 return sum;
100}
101
102size_t ClassTable::NumReferencedNonZygoteClasses() const {
103 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Marko54159c62018-06-20 14:30:08 +0100104 return classes_.back().size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000105}
106
Vladimir Marko1fe58392019-04-10 16:14:56 +0100107ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800108 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800109 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoa23cf272022-04-20 17:21:21 +0100110 // Search from the last table, assuming that apps shall search for their own classes
111 // more often than for boot image classes. For prebuilt boot images, this also helps
112 // by searching the large table from the framework boot image extension compiled as
113 // single-image before the individual small tables from the primary boot image
114 // compiled as multi-image.
Vladimir Markod0600622022-04-12 11:55:36 +0100115 for (ClassSet& class_set : ReverseRange(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) {
Vladimir Marko10137ab2022-04-14 10:59:53 +0100125 InsertWithHash(klass, klass->DescriptorHash());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700126}
127
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700128void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700129 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800130 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700131}
132
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700133bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700134 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700135 DCHECK(obj != nullptr);
136 for (GcRoot<mirror::Object>& root : strong_roots_) {
137 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700138 return false;
139 }
140 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700141 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000142 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
143 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700144 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000145 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
146 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800147 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000148 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000149 }
150 }
151 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700152 return true;
153}
154
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000155bool ClassTable::InsertOatFile(const OatFile* oat_file) {
156 WriterMutexLock mu(Thread::Current(), lock_);
157 return InsertOatFileLocked(oat_file);
158}
159
160bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
161 if (ContainsElement(oat_files_, oat_file)) {
162 return false;
163 }
164 oat_files_.push_back(oat_file);
165 return true;
166}
167
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800168size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
169 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800170 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800171 return read_count;
172}
173
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800174void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700175 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markod0600622022-04-12 11:55:36 +0100176 // Insert before the last (unfrozen) table since we add new classes into the back.
177 // Keep the order of previous frozen tables unchanged, so that we can can remember
178 // the number of searched frozen tables and not search them again.
179 // TODO: Make use of this in `ClassLinker::FindClass()`.
180 DCHECK(!classes_.empty());
181 classes_.insert(classes_.end() - 1, std::move(set));
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800182}
183
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700184void ClassTable::ClearStrongRoots() {
185 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000186 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700187 strong_roots_.clear();
188}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800189
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700190} // namespace art