Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 16 | |
| 17 | #include "intern_table.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "gc_root-inl.h" |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 22 | #include "gc/collector/garbage_collector.h" |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 23 | #include "gc/space/image_space.h" |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 24 | #include "gc/weak_root_state.h" |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 25 | #include "mirror/dex_cache.h" |
| 26 | #include "mirror/object_array-inl.h" |
| 27 | #include "mirror/object-inl.h" |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 28 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "thread.h" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 30 | #include "utf.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 34 | InternTable::InternTable() |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 35 | : image_added_to_intern_table_(false), log_new_roots_(false), |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 36 | weak_intern_condition_("New intern condition", *Locks::intern_table_lock_), |
| 37 | weak_root_state_(gc::kWeakRootStateNormal) { |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 38 | } |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 39 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 40 | size_t InternTable::Size() const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 41 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 42 | return strong_interns_.Size() + weak_interns_.Size(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 45 | size_t InternTable::StrongSize() const { |
| 46 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 47 | return strong_interns_.Size(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | size_t InternTable::WeakSize() const { |
| 51 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 52 | return weak_interns_.Size(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 55 | void InternTable::DumpForSigQuit(std::ostream& os) const { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 56 | os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 59 | void InternTable::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 60 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 61 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 62 | strong_interns_.VisitRoots(visitor); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 63 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 64 | for (auto& root : new_strong_intern_roots_) { |
| 65 | mirror::String* old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 66 | root.VisitRoot(visitor, RootInfo(kRootInternedString)); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 67 | mirror::String* new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 68 | if (new_ref != old_ref) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 69 | // The GC moved a root in the log. Need to search the strong interns and update the |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 70 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 71 | // concurrent moving GC. |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 72 | strong_interns_.Remove(old_ref); |
| 73 | strong_interns_.Insert(new_ref); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 76 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 77 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 78 | new_strong_intern_roots_.clear(); |
| 79 | } |
| 80 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 81 | log_new_roots_ = true; |
| 82 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 83 | log_new_roots_ = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 84 | } |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 85 | // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots. |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 88 | mirror::String* InternTable::LookupStrong(mirror::String* s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 89 | return strong_interns_.Find(s); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 92 | mirror::String* InternTable::LookupWeak(mirror::String* s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 93 | return weak_interns_.Find(s); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 96 | void InternTable::SwapPostZygoteWithPreZygote() { |
| 97 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 98 | weak_interns_.SwapPostZygoteWithPreZygote(); |
| 99 | strong_interns_.SwapPostZygoteWithPreZygote(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 102 | mirror::String* InternTable::InsertStrong(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 103 | Runtime* runtime = Runtime::Current(); |
| 104 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 105 | runtime->RecordStrongStringInsertion(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 106 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 107 | if (log_new_roots_) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 108 | new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 109 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 110 | strong_interns_.Insert(s); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 111 | return s; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 114 | mirror::String* InternTable::InsertWeak(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 115 | Runtime* runtime = Runtime::Current(); |
| 116 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 117 | runtime->RecordWeakStringInsertion(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 118 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 119 | weak_interns_.Insert(s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 120 | return s; |
| 121 | } |
| 122 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 123 | void InternTable::RemoveStrong(mirror::String* s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 124 | strong_interns_.Remove(s); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 127 | void InternTable::RemoveWeak(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 128 | Runtime* runtime = Runtime::Current(); |
| 129 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 130 | runtime->RecordWeakStringRemoval(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 131 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 132 | weak_interns_.Remove(s); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 135 | // Insert/remove methods used to undo changes made during an aborted transaction. |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 136 | mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 137 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 138 | return InsertStrong(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 139 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 140 | mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 141 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 142 | return InsertWeak(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 143 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 144 | void InternTable::RemoveStrongFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 145 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 146 | RemoveStrong(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 147 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 148 | void InternTable::RemoveWeakFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 149 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 150 | RemoveWeak(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 153 | void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space) { |
Mathieu Chartier | bc58ede | 2014-11-17 12:36:24 -0800 | [diff] [blame] | 154 | CHECK(image_space != nullptr); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 155 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 156 | if (!image_added_to_intern_table_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 157 | const ImageHeader* const header = &image_space->GetImageHeader(); |
| 158 | // Check if we have the interned strings section. |
| 159 | const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings); |
| 160 | if (section.Size() > 0) { |
| 161 | ReadFromMemoryLocked(image_space->Begin() + section.Offset()); |
| 162 | } else { |
| 163 | // TODO: Delete this logic? |
| 164 | mirror::Object* root = header->GetImageRoot(ImageHeader::kDexCaches); |
| 165 | mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>(); |
| 166 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 167 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 168 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 169 | const size_t num_strings = dex_file->NumStringIds(); |
| 170 | for (size_t j = 0; j < num_strings; ++j) { |
| 171 | mirror::String* image_string = dex_cache->GetResolvedString(j); |
| 172 | if (image_string != nullptr) { |
| 173 | mirror::String* found = LookupStrong(image_string); |
| 174 | if (found == nullptr) { |
| 175 | InsertStrong(image_string); |
| 176 | } else { |
| 177 | DCHECK_EQ(found, image_string); |
| 178 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | image_added_to_intern_table_ = true; |
| 184 | } |
| 185 | } |
| 186 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 187 | mirror::String* InternTable::LookupStringFromImage(mirror::String* s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 188 | if (image_added_to_intern_table_) { |
| 189 | return nullptr; |
| 190 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 191 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 192 | if (image == nullptr) { |
| 193 | return nullptr; // No image present. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 194 | } |
| 195 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 196 | mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>(); |
| 197 | const std::string utf8 = s->ToModifiedUtf8(); |
| 198 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 199 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 200 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 201 | // Binary search the dex file for the string index. |
| 202 | const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 203 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 204 | uint32_t string_idx = dex_file->GetIndexForStringId(*string_id); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 205 | // GetResolvedString() contains a RB. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 206 | mirror::String* image_string = dex_cache->GetResolvedString(string_idx); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 207 | if (image_string != nullptr) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 208 | return image_string; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 212 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 215 | void InternTable::EnsureNewWeakInternsDisallowed() { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 216 | // Lock and unlock once to ensure that no threads are still in the |
| 217 | // middle of adding new interns. |
| 218 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 219 | CHECK_EQ(weak_root_state_, gc::kWeakRootStateNoReadsOrWrites); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 222 | void InternTable::BroadcastForNewInterns() { |
| 223 | CHECK(kUseReadBarrier); |
| 224 | Thread* self = Thread::Current(); |
| 225 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 226 | weak_intern_condition_.Broadcast(self); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 229 | void InternTable::WaitUntilAccessible(Thread* self) { |
| 230 | Locks::intern_table_lock_->ExclusiveUnlock(self); |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 231 | self->TransitionFromRunnableToSuspended(kWaitingWeakGcRootRead); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 232 | Locks::intern_table_lock_->ExclusiveLock(self); |
| 233 | while (weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) { |
| 234 | weak_intern_condition_.Wait(self); |
| 235 | } |
| 236 | Locks::intern_table_lock_->ExclusiveUnlock(self); |
| 237 | self->TransitionFromSuspendedToRunnable(); |
| 238 | Locks::intern_table_lock_->ExclusiveLock(self); |
| 239 | } |
| 240 | |
| 241 | mirror::String* InternTable::Insert(mirror::String* s, bool is_strong, bool holding_locks) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 242 | if (s == nullptr) { |
| 243 | return nullptr; |
| 244 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 245 | Thread* const self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 246 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 247 | if (kDebugLocking && !holding_locks) { |
| 248 | Locks::mutator_lock_->AssertSharedHeld(self); |
| 249 | CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock"; |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 250 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 251 | while (true) { |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 252 | if (holding_locks) { |
| 253 | if (!kUseReadBarrier) { |
| 254 | CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal); |
| 255 | } else { |
| 256 | CHECK(self->GetWeakRefAccessEnabled()); |
| 257 | } |
| 258 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 259 | // Check the strong table for a match. |
| 260 | mirror::String* strong = LookupStrong(s); |
| 261 | if (strong != nullptr) { |
| 262 | return strong; |
| 263 | } |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 264 | if ((!kUseReadBarrier && weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) || |
| 265 | (kUseReadBarrier && self->GetWeakRefAccessEnabled())) { |
| 266 | break; |
| 267 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 268 | // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only |
| 269 | // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is |
| 270 | // cleared. |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 271 | CHECK(!holding_locks); |
| 272 | StackHandleScope<1> hs(self); |
| 273 | auto h = hs.NewHandleWrapper(&s); |
| 274 | WaitUntilAccessible(self); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 275 | } |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 276 | if (!kUseReadBarrier) { |
| 277 | CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal); |
| 278 | } else { |
| 279 | CHECK(self->GetWeakRefAccessEnabled()); |
| 280 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 281 | // There is no match in the strong table, check the weak table. |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 282 | mirror::String* weak = LookupWeak(s); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 283 | if (weak != nullptr) { |
| 284 | if (is_strong) { |
| 285 | // A match was found in the weak table. Promote to the strong table. |
| 286 | RemoveWeak(weak); |
| 287 | return InsertStrong(weak); |
| 288 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 289 | return weak; |
| 290 | } |
nikolay serdjuk | a446d86 | 2015-04-17 19:27:56 +0600 | [diff] [blame] | 291 | // Check the image for a match. |
| 292 | mirror::String* image = LookupStringFromImage(s); |
| 293 | if (image != nullptr) { |
| 294 | return is_strong ? InsertStrong(image) : InsertWeak(image); |
| 295 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 296 | // No match in the strong table or the weak table. Insert into the strong / weak table. |
| 297 | return is_strong ? InsertStrong(s) : InsertWeak(s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 300 | mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) { |
| 301 | DCHECK(utf8_data != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 302 | return InternStrong(mirror::String::AllocFromModifiedUtf8( |
| 303 | Thread::Current(), utf16_length, utf8_data)); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 306 | mirror::String* InternTable::InternStrong(const char* utf8_data) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 307 | DCHECK(utf8_data != nullptr); |
| 308 | return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 311 | mirror::String* InternTable::InternStrongImageString(mirror::String* s) { |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 312 | // May be holding the heap bitmap lock. |
| 313 | return Insert(s, true, true); |
| 314 | } |
| 315 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 316 | mirror::String* InternTable::InternStrong(mirror::String* s) { |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 317 | return Insert(s, true, false); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 320 | mirror::String* InternTable::InternWeak(mirror::String* s) { |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 321 | return Insert(s, false, false); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 324 | bool InternTable::ContainsWeak(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 325 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 326 | return LookupWeak(s) == s; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 329 | void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 330 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 331 | weak_interns_.SweepWeaks(visitor); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 334 | void InternTable::AddImageInternTable(gc::space::ImageSpace* image_space) { |
| 335 | const ImageSection& intern_section = image_space->GetImageHeader().GetImageSection( |
| 336 | ImageHeader::kSectionInternedStrings); |
| 337 | // Read the string tables from the image. |
| 338 | const uint8_t* ptr = image_space->Begin() + intern_section.Offset(); |
| 339 | const size_t offset = ReadFromMemory(ptr); |
| 340 | CHECK_LE(offset, intern_section.Size()); |
| 341 | } |
| 342 | |
| 343 | size_t InternTable::ReadFromMemory(const uint8_t* ptr) { |
| 344 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 345 | return ReadFromMemoryLocked(ptr); |
| 346 | } |
| 347 | |
| 348 | size_t InternTable::ReadFromMemoryLocked(const uint8_t* ptr) { |
| 349 | return strong_interns_.ReadIntoPreZygoteTable(ptr); |
| 350 | } |
| 351 | |
| 352 | size_t InternTable::WriteToMemory(uint8_t* ptr) { |
| 353 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 354 | return strong_interns_.WriteFromPostZygoteTable(ptr); |
| 355 | } |
| 356 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 357 | std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 358 | if (kIsDebugBuild) { |
| 359 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 360 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 361 | return static_cast<size_t>(root.Read()->GetHashCode()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 365 | const GcRoot<mirror::String>& b) const { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 366 | if (kIsDebugBuild) { |
| 367 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 368 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 369 | return a.Read()->Equals(b.Read()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 372 | size_t InternTable::Table::ReadIntoPreZygoteTable(const uint8_t* ptr) { |
| 373 | CHECK_EQ(pre_zygote_table_.Size(), 0u); |
| 374 | size_t read_count = 0; |
| 375 | pre_zygote_table_ = UnorderedSet(ptr, false /* make copy */, &read_count); |
| 376 | return read_count; |
| 377 | } |
| 378 | |
| 379 | size_t InternTable::Table::WriteFromPostZygoteTable(uint8_t* ptr) { |
| 380 | return post_zygote_table_.WriteToMemory(ptr); |
| 381 | } |
| 382 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 383 | void InternTable::Table::Remove(mirror::String* s) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 384 | auto it = post_zygote_table_.Find(GcRoot<mirror::String>(s)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 385 | if (it != post_zygote_table_.end()) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 386 | post_zygote_table_.Erase(it); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 387 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 388 | it = pre_zygote_table_.Find(GcRoot<mirror::String>(s)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 389 | DCHECK(it != pre_zygote_table_.end()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 390 | pre_zygote_table_.Erase(it); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | mirror::String* InternTable::Table::Find(mirror::String* s) { |
| 395 | Locks::intern_table_lock_->AssertHeld(Thread::Current()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 396 | auto it = pre_zygote_table_.Find(GcRoot<mirror::String>(s)); |
| 397 | if (it != pre_zygote_table_.end()) { |
| 398 | return it->Read(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 399 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 400 | it = post_zygote_table_.Find(GcRoot<mirror::String>(s)); |
| 401 | if (it != post_zygote_table_.end()) { |
| 402 | return it->Read(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 403 | } |
| 404 | return nullptr; |
| 405 | } |
| 406 | |
| 407 | void InternTable::Table::SwapPostZygoteWithPreZygote() { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 408 | if (pre_zygote_table_.Empty()) { |
| 409 | std::swap(pre_zygote_table_, post_zygote_table_); |
| 410 | VLOG(heap) << "Swapping " << pre_zygote_table_.Size() << " interns to the pre zygote table"; |
| 411 | } else { |
| 412 | // This case happens if read the intern table from the image. |
| 413 | VLOG(heap) << "Not swapping due to non-empty pre_zygote_table_"; |
| 414 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void InternTable::Table::Insert(mirror::String* s) { |
| 418 | // Always insert the post zygote table, this gets swapped when we create the zygote to be the |
| 419 | // pre zygote table. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 420 | post_zygote_table_.Insert(GcRoot<mirror::String>(s)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 423 | void InternTable::Table::VisitRoots(RootVisitor* visitor) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 424 | BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor( |
| 425 | visitor, RootInfo(kRootInternedString)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 426 | for (auto& intern : pre_zygote_table_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 427 | buffered_visitor.VisitRoot(intern); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 428 | } |
| 429 | for (auto& intern : post_zygote_table_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 430 | buffered_visitor.VisitRoot(intern); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 434 | void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) { |
| 435 | SweepWeaks(&pre_zygote_table_, visitor); |
| 436 | SweepWeaks(&post_zygote_table_, visitor); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 439 | void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 440 | for (auto it = set->begin(), end = set->end(); it != end;) { |
| 441 | // This does not need a read barrier because this is called by GC. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 442 | mirror::Object* object = it->Read<kWithoutReadBarrier>(); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 443 | mirror::Object* new_object = visitor->IsMarked(object); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 444 | if (new_object == nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 445 | it = set->Erase(it); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 446 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 447 | *it = GcRoot<mirror::String>(new_object->AsString()); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 448 | ++it; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 453 | size_t InternTable::Table::Size() const { |
| 454 | return pre_zygote_table_.Size() + post_zygote_table_.Size(); |
| 455 | } |
| 456 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 457 | void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) { |
| 458 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 459 | ChangeWeakRootStateLocked(new_state); |
| 460 | } |
| 461 | |
| 462 | void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) { |
| 463 | weak_root_state_ = new_state; |
| 464 | if (new_state != gc::kWeakRootStateNoReadsOrWrites) { |
| 465 | weak_intern_condition_.Broadcast(Thread::Current()); |
| 466 | } |
| 467 | } |
| 468 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 469 | } // namespace art |