1 /* 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-inl.h" 18 19 #include "base/stl_util.h" 20 #include "mirror/class-inl.h" 21 #include "mirror/string-inl.h" 22 #include "oat/oat_file.h" 23 24 namespace art HIDDEN { 25 ClassTable()26 ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) { 27 Runtime* const runtime = Runtime::Current(); 28 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(), 29 runtime->GetHashTableMaxLoadFactor())); 30 } 31 FreezeSnapshot()32 void ClassTable::FreezeSnapshot() { 33 WriterMutexLock mu(Thread::Current(), lock_); 34 // 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)); 39 } 40 UpdateClass(ObjPtr<mirror::Class> klass,size_t hash)41 ObjPtr<mirror::Class> ClassTable::UpdateClass(ObjPtr<mirror::Class> klass, size_t hash) { 42 WriterMutexLock mu(Thread::Current(), lock_); 43 // Should only be updating latest table. 44 TableSlot slot(klass, hash); 45 auto existing_it = classes_.back().FindWithHash(slot, hash); 46 if (UNLIKELY(existing_it == classes_.back().end())) { 47 for (const ClassSet& class_set : classes_) { 48 if (class_set.FindWithHash(slot, hash) != class_set.end()) { 49 LOG(FATAL) << "Updating class found in frozen table " << klass->PrettyDescriptor(); 50 UNREACHABLE(); 51 } 52 } 53 LOG(FATAL) << "Updating class not found " << klass->PrettyDescriptor(); 54 UNREACHABLE(); 55 } 56 const ObjPtr<mirror::Class> existing = existing_it->Read(); 57 CHECK_NE(existing, klass) << klass->PrettyDescriptor(); 58 CHECK(!existing->IsResolved()) << klass->PrettyDescriptor(); 59 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << klass->PrettyDescriptor(); 60 CHECK(!klass->IsTemp()) << klass->PrettyDescriptor(); 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. 64 *existing_it = slot; 65 return existing; 66 } 67 CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,const ClassSet & set) const68 size_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 NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const79 size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const { 80 ReaderMutexLock mu(Thread::Current(), lock_); 81 size_t sum = 0; 82 for (size_t i = 0; i < classes_.size() - 1; ++i) { 83 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]); 84 } 85 return sum; 86 } 87 NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const88 size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const { 89 ReaderMutexLock mu(Thread::Current(), lock_); 90 return CountDefiningLoaderClasses(defining_loader, classes_.back()); 91 } 92 NumReferencedZygoteClasses() const93 size_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) { 97 sum += classes_[i].size(); 98 } 99 return sum; 100 } 101 NumReferencedNonZygoteClasses() const102 size_t ClassTable::NumReferencedNonZygoteClasses() const { 103 ReaderMutexLock mu(Thread::Current(), lock_); 104 return classes_.back().size(); 105 } 106 Lookup(const char * descriptor,size_t hash)107 ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) { 108 DescriptorHashPair pair(descriptor, hash); 109 ReaderMutexLock mu(Thread::Current(), lock_); 110 // 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. 115 for (ClassSet& class_set : ReverseRange(classes_)) { 116 auto it = class_set.FindWithHash(pair, hash); 117 if (it != class_set.end()) { 118 return it->Read(); 119 } 120 } 121 return nullptr; 122 } 123 Insert(ObjPtr<mirror::Class> klass)124 void ClassTable::Insert(ObjPtr<mirror::Class> klass) { 125 InsertWithHash(klass, klass->DescriptorHash()); 126 } 127 InsertWithHash(ObjPtr<mirror::Class> klass,size_t hash)128 void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) { 129 WriterMutexLock mu(Thread::Current(), lock_); 130 classes_.back().InsertWithHash(TableSlot(klass, hash), hash); 131 } 132 InsertStrongRoot(ObjPtr<mirror::Object> obj)133 bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) { 134 WriterMutexLock mu(Thread::Current(), lock_); 135 DCHECK(obj != nullptr); 136 for (GcRoot<mirror::Object>& root : strong_roots_) { 137 if (root.Read() == obj) { 138 return false; 139 } 140 } 141 strong_roots_.push_back(GcRoot<mirror::Object>(obj)); 142 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_. 143 if (obj->IsDexCache()) { 144 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile(); 145 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) { 146 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile(); 147 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) { 148 InsertOatFileLocked(oat_file); // Ignore return value. 149 } 150 } 151 } 152 return true; 153 } 154 InsertOatFile(const OatFile * oat_file)155 bool ClassTable::InsertOatFile(const OatFile* oat_file) { 156 WriterMutexLock mu(Thread::Current(), lock_); 157 return InsertOatFileLocked(oat_file); 158 } 159 InsertOatFileLocked(const OatFile * oat_file)160 bool 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 ReadFromMemory(uint8_t * ptr)168 size_t ClassTable::ReadFromMemory(uint8_t* ptr) { 169 size_t read_count = 0; 170 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count)); 171 return read_count; 172 } 173 AddClassSet(ClassSet && set)174 void ClassTable::AddClassSet(ClassSet&& set) { 175 WriterMutexLock mu(Thread::Current(), lock_); 176 // 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)); 182 } 183 ClearStrongRoots()184 void ClassTable::ClearStrongRoots() { 185 WriterMutexLock mu(Thread::Current(), lock_); 186 oat_files_.clear(); 187 strong_roots_.clear(); 188 } 189 190 } // namespace art 191