1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "class_table-inl.h"
#include "base/stl_util.h"
#include "mirror/class-inl.h"
#include "oat_file.h"
namespace art {
ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
Runtime* const runtime = Runtime::Current();
classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
runtime->GetHashTableMaxLoadFactor()));
}
void ClassTable::FreezeSnapshot() {
WriterMutexLock mu(Thread::Current(), lock_);
classes_.push_back(ClassSet());
}
bool ClassTable::Contains(ObjPtr<mirror::Class> klass) {
return LookupByDescriptor(klass) == klass;
}
ObjPtr<mirror::Class> ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) {
ReaderMutexLock mu(Thread::Current(), lock_);
TableSlot slot(klass);
for (ClassSet& class_set : classes_) {
auto it = class_set.find(slot);
if (it != class_set.end()) {
return it->Read();
}
}
return nullptr;
}
ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor,
ObjPtr<mirror::Class> klass,
size_t hash) {
WriterMutexLock mu(Thread::Current(), lock_);
// Should only be updating latest table.
DescriptorHashPair pair(descriptor, hash);
auto existing_it = classes_.back().FindWithHash(pair, hash);
if (kIsDebugBuild && existing_it == classes_.back().end()) {
for (const ClassSet& class_set : classes_) {
if (class_set.FindWithHash(pair, hash) != class_set.end()) {
LOG(FATAL) << "Updating class found in frozen table " << descriptor;
}
}
LOG(FATAL) << "Updating class not found " << descriptor;
}
const ObjPtr<mirror::Class> existing = existing_it->Read();
CHECK_NE(existing, klass) << descriptor;
CHECK(!existing->IsResolved()) << descriptor;
CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
CHECK(!klass->IsTemp()) << descriptor;
VerifyObject(klass);
// Update the element in the hash set with the new class. This is safe to do since the descriptor
// doesn't change.
*existing_it = TableSlot(klass, hash);
return existing;
}
size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
const ClassSet& set) const {
size_t count = 0;
for (const TableSlot& root : set) {
if (root.Read()->GetClassLoader() == defining_loader) {
++count;
}
}
return count;
}
size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
ReaderMutexLock mu(Thread::Current(), lock_);
size_t sum = 0;
for (size_t i = 0; i < classes_.size() - 1; ++i) {
sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
}
return sum;
}
size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
ReaderMutexLock mu(Thread::Current(), lock_);
return CountDefiningLoaderClasses(defining_loader, classes_.back());
}
size_t ClassTable::NumReferencedZygoteClasses() const {
ReaderMutexLock mu(Thread::Current(), lock_);
size_t sum = 0;
for (size_t i = 0; i < classes_.size() - 1; ++i) {
sum += classes_[i].size();
}
return sum;
}
size_t ClassTable::NumReferencedNonZygoteClasses() const {
ReaderMutexLock mu(Thread::Current(), lock_);
return classes_.back().size();
}
ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) {
DescriptorHashPair pair(descriptor, hash);
ReaderMutexLock mu(Thread::Current(), lock_);
for (ClassSet& class_set : classes_) {
auto it = class_set.FindWithHash(pair, hash);
if (it != class_set.end()) {
return it->Read();
}
}
return nullptr;
}
ObjPtr<mirror::Class> ClassTable::TryInsert(ObjPtr<mirror::Class> klass) {
TableSlot slot(klass);
WriterMutexLock mu(Thread::Current(), lock_);
for (ClassSet& class_set : classes_) {
auto it = class_set.find(slot);
if (it != class_set.end()) {
return it->Read();
}
}
classes_.back().insert(slot);
return klass;
}
void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
const uint32_t hash = TableSlot::HashDescriptor(klass);
WriterMutexLock mu(Thread::Current(), lock_);
classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
}
void ClassTable::CopyWithoutLocks(const ClassTable& source_table) {
if (kIsDebugBuild) {
for (ClassSet& class_set : classes_) {
CHECK(class_set.empty());
}
}
for (const ClassSet& class_set : source_table.classes_) {
for (const TableSlot& slot : class_set) {
classes_.back().insert(slot);
}
}
}
void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
const uint32_t hash = TableSlot::HashDescriptor(klass);
classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
}
void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
WriterMutexLock mu(Thread::Current(), lock_);
classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
}
bool ClassTable::Remove(const char* descriptor) {
DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
WriterMutexLock mu(Thread::Current(), lock_);
for (ClassSet& class_set : classes_) {
auto it = class_set.find(pair);
if (it != class_set.end()) {
class_set.erase(it);
return true;
}
}
return false;
}
uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
const {
std::string temp;
// No read barrier needed, we're reading a chain of constant references for comparison
// with null and retrieval of constant primitive data. See ReadBarrierOption.
return ComputeModifiedUtf8Hash(slot.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
}
bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
const TableSlot& b) const {
// No read barrier needed, we're reading a chain of constant references for comparison
// with null and retrieval of constant primitive data. See ReadBarrierOption.
if (a.Hash() != b.Hash()) {
std::string temp;
DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(
b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)));
return false;
}
std::string temp;
return a.Read<kWithoutReadBarrier>()->DescriptorEquals(
b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
}
bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
const DescriptorHashPair& b) const {
// No read barrier needed, we're reading a chain of constant references for comparison
// with null and retrieval of constant primitive data. See ReadBarrierOption.
if (!a.MaskedHashEquals(b.second)) {
DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first));
return false;
}
return a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first);
}
uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
return ComputeModifiedUtf8Hash(pair.first);
}
bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
WriterMutexLock mu(Thread::Current(), lock_);
DCHECK(obj != nullptr);
for (GcRoot<mirror::Object>& root : strong_roots_) {
if (root.Read() == obj) {
return false;
}
}
strong_roots_.push_back(GcRoot<mirror::Object>(obj));
// If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
if (obj->IsDexCache()) {
const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
InsertOatFileLocked(oat_file); // Ignore return value.
}
}
}
return true;
}
bool ClassTable::InsertOatFile(const OatFile* oat_file) {
WriterMutexLock mu(Thread::Current(), lock_);
return InsertOatFileLocked(oat_file);
}
bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
if (ContainsElement(oat_files_, oat_file)) {
return false;
}
oat_files_.push_back(oat_file);
return true;
}
size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
ReaderMutexLock mu(Thread::Current(), lock_);
ClassSet combined;
// Combine all the class sets in case there are multiple, also adjusts load factor back to
// default in case classes were pruned.
for (const ClassSet& class_set : classes_) {
for (const TableSlot& root : class_set) {
combined.insert(root);
}
}
const size_t ret = combined.WriteToMemory(ptr);
// Sanity check.
if (kIsDebugBuild && ptr != nullptr) {
size_t read_count;
ClassSet class_set(ptr, /*make copy*/false, &read_count);
class_set.Verify();
}
return ret;
}
size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
size_t read_count = 0;
AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
return read_count;
}
void ClassTable::AddClassSet(ClassSet&& set) {
WriterMutexLock mu(Thread::Current(), lock_);
classes_.insert(classes_.begin(), std::move(set));
}
void ClassTable::ClearStrongRoots() {
WriterMutexLock mu(Thread::Current(), lock_);
oat_files_.clear();
strong_roots_.clear();
}
ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
: TableSlot(klass, HashDescriptor(klass)) {}
uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
std::string temp;
return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
}
} // namespace art
|