Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 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.h" |
| 18 | |
| 19 | #include "mirror/class-inl.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 23 | ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) { |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 24 | Runtime* const runtime = Runtime::Current(); |
| 25 | classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(), |
| 26 | runtime->GetHashTableMaxLoadFactor())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void ClassTable::FreezeSnapshot() { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 30 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 31 | classes_.push_back(ClassSet()); |
| 32 | } |
| 33 | |
| 34 | bool ClassTable::Contains(mirror::Class* klass) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 35 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 36 | for (ClassSet& class_set : classes_) { |
| 37 | auto it = class_set.Find(GcRoot<mirror::Class>(klass)); |
| 38 | if (it != class_set.end()) { |
| 39 | return it->Read() == klass; |
| 40 | } |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 45 | mirror::Class* ClassTable::LookupByDescriptor(mirror::Class* klass) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 46 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 47 | for (ClassSet& class_set : classes_) { |
| 48 | auto it = class_set.Find(GcRoot<mirror::Class>(klass)); |
| 49 | if (it != class_set.end()) { |
| 50 | return it->Read(); |
| 51 | } |
| 52 | } |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
Pirama Arumuga Nainar | 813b9c4 | 2016-08-25 23:42:50 -0700 | [diff] [blame] | 56 | // 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 Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 60 | mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 61 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 62 | // Should only be updating latest table. |
| 63 | auto existing_it = classes_.back().FindWithHash(descriptor, hash); |
| 64 | if (kIsDebugBuild && existing_it == classes_.back().end()) { |
| 65 | for (const ClassSet& class_set : classes_) { |
| 66 | if (class_set.FindWithHash(descriptor, hash) != class_set.end()) { |
| 67 | LOG(FATAL) << "Updating class found in frozen table " << descriptor; |
| 68 | } |
| 69 | } |
| 70 | LOG(FATAL) << "Updating class not found " << descriptor; |
| 71 | } |
| 72 | mirror::Class* const existing = existing_it->Read(); |
| 73 | CHECK_NE(existing, klass) << descriptor; |
| 74 | CHECK(!existing->IsResolved()) << descriptor; |
| 75 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor; |
| 76 | CHECK(!klass->IsTemp()) << descriptor; |
| 77 | VerifyObject(klass); |
| 78 | // Update the element in the hash set with the new class. This is safe to do since the descriptor |
| 79 | // doesn't change. |
| 80 | *existing_it = GcRoot<mirror::Class>(klass); |
| 81 | return existing; |
| 82 | } |
| 83 | |
Pirama Arumuga Nainar | 813b9c4 | 2016-08-25 23:42:50 -0700 | [diff] [blame] | 84 | #pragma clang diagnostic pop // http://b/31104323 |
| 85 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 86 | size_t ClassTable::NumZygoteClasses() const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 87 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 88 | size_t sum = 0; |
| 89 | for (size_t i = 0; i < classes_.size() - 1; ++i) { |
| 90 | sum += classes_[i].Size(); |
| 91 | } |
| 92 | return sum; |
| 93 | } |
| 94 | |
| 95 | size_t ClassTable::NumNonZygoteClasses() const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 96 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 97 | return classes_.back().Size(); |
| 98 | } |
| 99 | |
| 100 | mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 101 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 102 | for (ClassSet& class_set : classes_) { |
| 103 | auto it = class_set.FindWithHash(descriptor, hash); |
| 104 | if (it != class_set.end()) { |
| 105 | return it->Read(); |
| 106 | } |
| 107 | } |
| 108 | return nullptr; |
| 109 | } |
| 110 | |
| 111 | void ClassTable::Insert(mirror::Class* klass) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 112 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 113 | classes_.back().Insert(GcRoot<mirror::Class>(klass)); |
| 114 | } |
| 115 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 116 | void ClassTable::InsertWithoutLocks(mirror::Class* klass) { |
| 117 | classes_.back().Insert(GcRoot<mirror::Class>(klass)); |
| 118 | } |
| 119 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 120 | void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 121 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 122 | classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash); |
| 123 | } |
| 124 | |
| 125 | bool ClassTable::Remove(const char* descriptor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 126 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 127 | for (ClassSet& class_set : classes_) { |
| 128 | auto it = class_set.Find(descriptor); |
| 129 | if (it != class_set.end()) { |
| 130 | class_set.Erase(it); |
| 131 | return true; |
| 132 | } |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 137 | uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root) |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 138 | const { |
| 139 | std::string temp; |
| 140 | return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp)); |
| 141 | } |
| 142 | |
| 143 | bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 144 | const GcRoot<mirror::Class>& b) const { |
| 145 | DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader()); |
| 146 | std::string temp; |
| 147 | return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)); |
| 148 | } |
| 149 | |
| 150 | bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 151 | const char* descriptor) const { |
| 152 | return a.Read()->DescriptorEquals(descriptor); |
| 153 | } |
| 154 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 155 | uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 156 | return ComputeModifiedUtf8Hash(descriptor); |
| 157 | } |
| 158 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 159 | bool ClassTable::InsertStrongRoot(mirror::Object* obj) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 160 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 161 | DCHECK(obj != nullptr); |
| 162 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
| 163 | if (root.Read() == obj) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 167 | strong_roots_.push_back(GcRoot<mirror::Object>(obj)); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame^] | 168 | // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_. |
| 169 | if (obj->IsDexCache()) { |
| 170 | const DexFile* dex_file = down_cast<mirror::DexCache*>(obj)->GetDexFile(); |
| 171 | if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) { |
| 172 | const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile(); |
| 173 | if (!oat_file->GetBssGcRoots().empty() && !ContainsElement(oat_files_, oat_file)) { |
| 174 | oat_files_.push_back(oat_file); |
| 175 | } |
| 176 | } |
| 177 | } |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 181 | size_t ClassTable::WriteToMemory(uint8_t* ptr) const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 182 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 183 | ClassSet combined; |
| 184 | // Combine all the class sets in case there are multiple, also adjusts load factor back to |
| 185 | // default in case classes were pruned. |
| 186 | for (const ClassSet& class_set : classes_) { |
| 187 | for (const GcRoot<mirror::Class>& root : class_set) { |
| 188 | combined.Insert(root); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 191 | const size_t ret = combined.WriteToMemory(ptr); |
| 192 | // Sanity check. |
| 193 | if (kIsDebugBuild && ptr != nullptr) { |
| 194 | size_t read_count; |
| 195 | ClassSet class_set(ptr, /*make copy*/false, &read_count); |
| 196 | class_set.Verify(); |
| 197 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | size_t ClassTable::ReadFromMemory(uint8_t* ptr) { |
| 202 | size_t read_count = 0; |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 203 | AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count)); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 204 | return read_count; |
| 205 | } |
| 206 | |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 207 | void ClassTable::AddClassSet(ClassSet&& set) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 208 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 209 | classes_.insert(classes_.begin(), std::move(set)); |
| 210 | } |
| 211 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 212 | void ClassTable::ClearStrongRoots() { |
| 213 | WriterMutexLock mu(Thread::Current(), lock_); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame^] | 214 | oat_files_.clear(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 215 | strong_roots_.clear(); |
| 216 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 217 | } // namespace art |