blob: 4819b9ffa7595c9b2f6c279d697b7b84117f884e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom7e93b502011-08-04 14:16:22 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INTERN_TABLE_H_
18#define ART_RUNTIME_INTERN_TABLE_H_
Brian Carlstrom7e93b502011-08-04 14:16:22 -070019
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070020#include <unordered_set>
Brian Carlstrom7e93b502011-08-04 14:16:22 -070021
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070022#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070023#include "base/allocator.h"
Mathieu Chartierc2e20622014-11-03 11:41:47 -080024#include "base/hash_set.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080025#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070026#include "gc_root.h"
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070027#include "gc/weak_root_state.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080028#include "object_callbacks.h"
29
Brian Carlstrom7e93b502011-08-04 14:16:22 -070030namespace art {
Mathieu Chartier893263b2014-03-04 11:07:42 -080031
Mathieu Chartiereb175f72014-10-31 11:49:27 -070032namespace gc {
33namespace space {
34class ImageSpace;
35} // namespace space
36} // namespace gc
37
Mathieu Chartier893263b2014-03-04 11:07:42 -080038enum VisitRootFlags : uint8_t;
39
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040namespace mirror {
41class String;
42} // namespace mirror
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010043class Transaction;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070044
Elliott Hughescf4c6c42011-09-01 15:16:42 -070045/**
46 * Used to intern strings.
47 *
48 * There are actually two tables: one that holds strong references to its strings, and one that
49 * holds weak references. The former is used for string literals, for which there is an effective
50 * reference from the constant pool. The latter is used for strings interned at runtime via
51 * String.intern. Some code (XML parsers being a prime example) relies on being able to intern
52 * arbitrarily many strings for the duration of a parse without permanently increasing the memory
53 * footprint.
54 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070055class InternTable {
56 public:
57 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070058
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070059 // Interns a potentially new string in the 'strong' table. May cause thread suspension.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data)
Mathieu Chartier90443472015-07-16 20:32:27 -070061 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070062
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070063 // Only used by image writer.
Mathieu Chartier90443472015-07-16 20:32:27 -070064 mirror::String* InternImageString(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070065
66 // Interns a potentially new string in the 'strong' table. May cause thread suspension.
Mathieu Chartier90443472015-07-16 20:32:27 -070067 mirror::String* InternStrong(const char* utf8_data) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070068
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070069 // Interns a potentially new string in the 'strong' table. May cause thread suspension.
Mathieu Chartier90443472015-07-16 20:32:27 -070070 mirror::String* InternStrong(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070071
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070072 // Interns a potentially new string in the 'weak' table. May cause thread suspension.
Mathieu Chartier90443472015-07-16 20:32:27 -070073 mirror::String* InternWeak(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074
Mathieu Chartier90443472015-07-16 20:32:27 -070075 void SweepInternTableWeaks(IsMarkedVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_)
76 REQUIRES(!Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077
Mathieu Chartier90443472015-07-16 20:32:27 -070078 bool ContainsWeak(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070079
Mathieu Chartiereb175f72014-10-31 11:49:27 -070080 // Total number of interned strings.
Mathieu Chartier90443472015-07-16 20:32:27 -070081 size_t Size() const REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070082 // Total number of weakly live interned strings.
Mathieu Chartier90443472015-07-16 20:32:27 -070083 size_t StrongSize() const REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070084 // Total number of strongly live interned strings.
Mathieu Chartier90443472015-07-16 20:32:27 -070085 size_t WeakSize() const REQUIRES(!Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070086
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070087 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
Mathieu Chartier90443472015-07-16 20:32:27 -070088 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070089
Elliott Hughescac6cc72011-11-03 20:31:21 -070090 void DumpForSigQuit(std::ostream& os) const;
91
Mathieu Chartier90443472015-07-16 20:32:27 -070092 void DisallowNewInterns() SHARED_REQUIRES(Locks::mutator_lock_);
93 void AllowNewInterns() SHARED_REQUIRES(Locks::mutator_lock_);
94 void EnsureNewInternsDisallowed() SHARED_REQUIRES(Locks::mutator_lock_);
95 void BroadcastForNewInterns() SHARED_REQUIRES(Locks::mutator_lock_);
96 void EnsureNewWeakInternsDisallowed() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070097
Mathieu Chartiereb175f72014-10-31 11:49:27 -070098 // Adds all of the resolved image strings from the image space into the intern table. The
99 // advantage of doing this is preventing expensive DexFile::FindStringId calls.
100 void AddImageStringsToTable(gc::space::ImageSpace* image_space)
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700102 // Copy the post zygote tables to pre zygote to save memory by preventing dirty pages.
103 void SwapPostZygoteWithPreZygote()
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700105
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700106 // Add an intern table which was serialized to the image.
107 void AddImageInternTable(gc::space::ImageSpace* image_space)
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700109
110 // Read the intern table from memory. The elements aren't copied, the intern hash set data will
111 // point to somewhere within ptr. Only reads the strong interns.
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 size_t ReadFromMemory(const uint8_t* ptr) REQUIRES(!Locks::intern_table_lock_)
113 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700114
115 // Write the post zygote intern table to a pointer. Only writes the strong interns since it is
116 // expected that there is no weak interns since this is called from the image writer.
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 size_t WriteToMemory(uint8_t* ptr) SHARED_REQUIRES(Locks::mutator_lock_)
118 REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700119
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700120 // Change the weak root state. May broadcast to waiters.
121 void ChangeWeakRootState(gc::WeakRootState new_state)
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700123
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700124 private:
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700125 class StringHashEquals {
126 public:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800127 std::size_t operator()(const GcRoot<mirror::String>& root) const NO_THREAD_SAFETY_ANALYSIS;
128 bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b) const
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700129 NO_THREAD_SAFETY_ANALYSIS;
130 };
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800131 class GcRootEmptyFn {
132 public:
133 void MakeEmpty(GcRoot<mirror::String>& item) const {
134 item = GcRoot<mirror::String>();
135 }
136 bool IsEmpty(const GcRoot<mirror::String>& item) const {
137 return item.IsNull();
138 }
139 };
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700140
141 // Table which holds pre zygote and post zygote interned strings. There is one instance for
142 // weak interns and strong interns.
143 class Table {
144 public:
Mathieu Chartier90443472015-07-16 20:32:27 -0700145 mirror::String* Find(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_)
146 REQUIRES(Locks::intern_table_lock_);
147 void Insert(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_)
148 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700149 void Remove(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 SHARED_REQUIRES(Locks::mutator_lock_)
151 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700152 void VisitRoots(RootVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 SHARED_REQUIRES(Locks::mutator_lock_)
154 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700155 void SweepWeaks(IsMarkedVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700156 SHARED_REQUIRES(Locks::mutator_lock_)
157 REQUIRES(Locks::intern_table_lock_);
158 void SwapPostZygoteWithPreZygote() REQUIRES(Locks::intern_table_lock_);
159 size_t Size() const REQUIRES(Locks::intern_table_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700160 // Read pre zygote table is called from ReadFromMemory which happens during runtime creation
161 // when we load the image intern table. Returns how many bytes were read.
162 size_t ReadIntoPreZygoteTable(const uint8_t* ptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 REQUIRES(Locks::intern_table_lock_)
164 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700165 // The image writer calls WritePostZygoteTable through WriteToMemory, it writes the interns in
166 // the post zygote table. Returns how many bytes were written.
167 size_t WriteFromPostZygoteTable(uint8_t* ptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 REQUIRES(Locks::intern_table_lock_)
169 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700170
171 private:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800172 typedef HashSet<GcRoot<mirror::String>, GcRootEmptyFn, StringHashEquals, StringHashEquals,
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700173 TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>> UnorderedSet;
174
Mathieu Chartier97509952015-07-13 14:35:43 -0700175 void SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700176 SHARED_REQUIRES(Locks::mutator_lock_)
177 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700178
179 // We call SwapPostZygoteWithPreZygote when we create the zygote to reduce private dirty pages
180 // caused by modifying the zygote intern table hash table. The pre zygote table are the
181 // interned strings which were interned before we created the zygote space. Post zygote is self
182 // explanatory.
183 UnorderedSet pre_zygote_table_;
184 UnorderedSet post_zygote_table_;
185 };
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700186
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700187 // Insert if non null, otherwise return null.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700188 mirror::String* Insert(mirror::String* s, bool is_strong, bool holding_locks)
Mathieu Chartier90443472015-07-16 20:32:27 -0700189 REQUIRES(!Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700190
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700191 mirror::String* LookupStrong(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700192 SHARED_REQUIRES(Locks::mutator_lock_)
193 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700194 mirror::String* LookupWeak(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 SHARED_REQUIRES(Locks::mutator_lock_)
196 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700197 mirror::String* InsertStrong(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 SHARED_REQUIRES(Locks::mutator_lock_)
199 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700200 mirror::String* InsertWeak(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700201 SHARED_REQUIRES(Locks::mutator_lock_)
202 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700203 void RemoveStrong(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700204 SHARED_REQUIRES(Locks::mutator_lock_)
205 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700206 void RemoveWeak(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700207 SHARED_REQUIRES(Locks::mutator_lock_)
208 REQUIRES(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700209
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100210 // Transaction rollback access.
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700211 mirror::String* LookupStringFromImage(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700212 SHARED_REQUIRES(Locks::mutator_lock_)
213 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700214 mirror::String* InsertStrongFromTransaction(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700215 SHARED_REQUIRES(Locks::mutator_lock_)
216 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700217 mirror::String* InsertWeakFromTransaction(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700218 SHARED_REQUIRES(Locks::mutator_lock_)
219 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700220 void RemoveStrongFromTransaction(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700221 SHARED_REQUIRES(Locks::mutator_lock_)
222 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700223 void RemoveWeakFromTransaction(mirror::String* s)
Mathieu Chartier90443472015-07-16 20:32:27 -0700224 SHARED_REQUIRES(Locks::mutator_lock_)
225 REQUIRES(Locks::intern_table_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100226 friend class Transaction;
227
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700228 size_t ReadFromMemoryLocked(const uint8_t* ptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700229 REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700230
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700231 // Change the weak root state. May broadcast to waiters.
232 void ChangeWeakRootStateLocked(gc::WeakRootState new_state)
Mathieu Chartier90443472015-07-16 20:32:27 -0700233 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700234
235 // Wait until we can read weak roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700236 void WaitUntilAccessible(Thread* self)
237 REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700238
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700239 bool image_added_to_intern_table_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800240 bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700241 ConditionVariable weak_intern_condition_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700242 // Since this contains (strong) roots, they need a read barrier to
243 // enable concurrent intern table (strong) root scan. Do not
244 // directly access the strings in it. Use functions that contain
245 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100246 Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700247 std::vector<GcRoot<mirror::String>> new_strong_intern_roots_
Mathieu Chartier893263b2014-03-04 11:07:42 -0800248 GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700249 // Since this contains (weak) roots, they need a read barrier. Do
250 // not directly access the strings in it. Use functions that contain
251 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100252 Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700253 // Weak root state, used for concurrent system weak processing and more.
254 gc::WeakRootState weak_root_state_ GUARDED_BY(Locks::intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700255};
256
257} // namespace art
258
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700259#endif // ART_RUNTIME_INTERN_TABLE_H_