blob: 274f5ade5edbc2e59eacb15a304f62510e9b2aea [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 Chartiered8990a2015-07-23 14:11:16 -070061 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070062
Mathieu Chartiered8990a2015-07-23 14:11:16 -070063 // Only used by image writer. Special version that may not cause thread suspension since the GC
Roland Levillain91d65e02016-01-19 15:59:16 +000064 // cannot be running while we are doing image writing. Maybe be called while while holding a
Mathieu Chartier90ef3db2015-08-04 15:19:41 -070065 // lock since there will not be thread suspension.
66 mirror::String* InternStrongImageString(mirror::String* s)
67 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070068
69 // Interns a potentially new string in the 'strong' table. May cause thread suspension.
Mathieu Chartiered8990a2015-07-23 14:11:16 -070070 mirror::String* InternStrong(const char* utf8_data) SHARED_REQUIRES(Locks::mutator_lock_)
71 REQUIRES(!Roles::uninterruptible_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070072
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070073 // Interns a potentially new string in the 'strong' table. May cause thread suspension.
Mathieu Chartiered8990a2015-07-23 14:11:16 -070074 mirror::String* InternStrong(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_)
75 REQUIRES(!Roles::uninterruptible_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070076
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070077 // Interns a potentially new string in the 'weak' table. May cause thread suspension.
Mathieu Chartiered8990a2015-07-23 14:11:16 -070078 mirror::String* InternWeak(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_)
79 REQUIRES(!Roles::uninterruptible_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070080
Mathieu Chartier90443472015-07-16 20:32:27 -070081 void SweepInternTableWeaks(IsMarkedVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_)
82 REQUIRES(!Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070083
Mathieu Chartiered8990a2015-07-23 14:11:16 -070084 bool ContainsWeak(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_)
85 REQUIRES(!Locks::intern_table_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070086
Mathieu Chartierfbc31082016-01-24 11:59:56 -080087 // Lookup a strong intern, returns null if not found.
88 mirror::String* LookupStrong(Thread* self, mirror::String* s)
89 REQUIRES(!Locks::intern_table_lock_)
90 SHARED_REQUIRES(Locks::mutator_lock_);
91
92 // Lookup a weak intern, returns null if not found.
93 mirror::String* LookupWeak(Thread* self, mirror::String* s)
94 REQUIRES(!Locks::intern_table_lock_)
95 SHARED_REQUIRES(Locks::mutator_lock_);
96
Mathieu Chartiereb175f72014-10-31 11:49:27 -070097 // Total number of interned strings.
Mathieu Chartier90443472015-07-16 20:32:27 -070098 size_t Size() const REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080099
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700100 // Total number of weakly live interned strings.
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 size_t StrongSize() const REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800102
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700103 // Total number of strongly live interned strings.
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 size_t WeakSize() const REQUIRES(!Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700105
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700106 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700107 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700108
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700109 void DumpForSigQuit(std::ostream& os) const REQUIRES(!Locks::intern_table_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -0700110
Mathieu Chartier90443472015-07-16 20:32:27 -0700111 void BroadcastForNewInterns() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700112
Mathieu Chartier205b7622016-01-06 15:47:09 -0800113 // Adds all of the resolved image strings from the image spaces into the intern table. The
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800114 // advantage of doing this is preventing expensive DexFile::FindStringId calls. Sets
Mathieu Chartier205b7622016-01-06 15:47:09 -0800115 // images_added_to_intern_table_ to true.
116 void AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces)
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700118
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800119 // Add a new intern table for inserting to, previous intern tables are still there but no
120 // longer inserted into and ideally unmodified. This is done to prevent dirty pages.
121 void AddNewTable()
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700123
124 // Read the intern table from memory. The elements aren't copied, the intern hash set data will
125 // point to somewhere within ptr. Only reads the strong interns.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800126 size_t AddTableFromMemory(const uint8_t* ptr) REQUIRES(!Locks::intern_table_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700127 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700128
129 // Write the post zygote intern table to a pointer. Only writes the strong interns since it is
130 // expected that there is no weak interns since this is called from the image writer.
Mathieu Chartier90443472015-07-16 20:32:27 -0700131 size_t WriteToMemory(uint8_t* ptr) SHARED_REQUIRES(Locks::mutator_lock_)
132 REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700133
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700134 // Change the weak root state. May broadcast to waiters.
135 void ChangeWeakRootState(gc::WeakRootState new_state)
Mathieu Chartier90443472015-07-16 20:32:27 -0700136 REQUIRES(!Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700137
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700138 private:
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700139 class StringHashEquals {
140 public:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800141 std::size_t operator()(const GcRoot<mirror::String>& root) const NO_THREAD_SAFETY_ANALYSIS;
142 bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b) const
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700143 NO_THREAD_SAFETY_ANALYSIS;
144 };
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800145 class GcRootEmptyFn {
146 public:
147 void MakeEmpty(GcRoot<mirror::String>& item) const {
148 item = GcRoot<mirror::String>();
149 }
150 bool IsEmpty(const GcRoot<mirror::String>& item) const {
151 return item.IsNull();
152 }
153 };
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700154
155 // Table which holds pre zygote and post zygote interned strings. There is one instance for
156 // weak interns and strong interns.
157 class Table {
158 public:
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700159 Table();
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 mirror::String* Find(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_)
161 REQUIRES(Locks::intern_table_lock_);
162 void Insert(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_)
163 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700164 void Remove(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700165 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700166 void VisitRoots(RootVisitor* visitor)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700167 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700168 void SweepWeaks(IsMarkedVisitor* visitor)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700169 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800170 // Add a new intern table that will only be inserted into from now on.
171 void AddNewTable() REQUIRES(Locks::intern_table_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700172 size_t Size() const REQUIRES(Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800173 // Read and add an intern table from ptr.
174 // Tables read are inserted at the front of the table array. Only checks for conflicts in
175 // debug builds. Returns how many bytes were read.
176 size_t AddTableFromMemory(const uint8_t* ptr)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700177 REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800178 // Write the intern tables to ptr, if there are multiple tables they are combined into a single
179 // one. Returns how many bytes were written.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800180 size_t WriteToMemory(uint8_t* ptr)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700181 REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700182
183 private:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800184 typedef HashSet<GcRoot<mirror::String>, GcRootEmptyFn, StringHashEquals, StringHashEquals,
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700185 TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>> UnorderedSet;
186
Mathieu Chartier97509952015-07-13 14:35:43 -0700187 void SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700188 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700189
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800190 // We call AddNewTable when we create the zygote to reduce private dirty pages caused by
191 // modifying the zygote intern table. The back of table is modified when strings are interned.
192 std::vector<UnorderedSet> tables_;
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700193 };
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700194
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700195 // Insert if non null, otherwise return null. Must be called holding the mutator lock.
196 // If holding_locks is true, then we may also hold other locks. If holding_locks is true, then we
197 // require GC is not running since it is not safe to wait while holding locks.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700198 mirror::String* Insert(mirror::String* s, bool is_strong, bool holding_locks)
Mathieu Chartier90443472015-07-16 20:32:27 -0700199 REQUIRES(!Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700200
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800201 mirror::String* LookupStrongLocked(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700202 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800203 mirror::String* LookupWeakLocked(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700204 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700205 mirror::String* InsertStrong(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700206 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700207 mirror::String* InsertWeak(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700208 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700209 void RemoveStrong(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700210 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700211 void RemoveWeak(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700212 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700213
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100214 // Transaction rollback access.
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700215 mirror::String* LookupStringFromImage(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700216 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700217 mirror::String* InsertStrongFromTransaction(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700218 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700219 mirror::String* InsertWeakFromTransaction(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700220 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700221 void RemoveStrongFromTransaction(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700222 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700223 void RemoveWeakFromTransaction(mirror::String* s)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700224 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100225
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800226 size_t AddTableFromMemoryLocked(const uint8_t* ptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700227 REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700228
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700229 // Change the weak root state. May broadcast to waiters.
230 void ChangeWeakRootStateLocked(gc::WeakRootState new_state)
Mathieu Chartier90443472015-07-16 20:32:27 -0700231 REQUIRES(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700232
233 // Wait until we can read weak roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700234 void WaitUntilAccessible(Thread* self)
235 REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700236
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800237 bool images_added_to_intern_table_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800238 bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700239 ConditionVariable weak_intern_condition_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700240 // Since this contains (strong) roots, they need a read barrier to
241 // enable concurrent intern table (strong) root scan. Do not
242 // directly access the strings in it. Use functions that contain
243 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100244 Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700245 std::vector<GcRoot<mirror::String>> new_strong_intern_roots_
Mathieu Chartier893263b2014-03-04 11:07:42 -0800246 GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700247 // Since this contains (weak) roots, they need a read barrier. Do
248 // not directly access the strings in it. Use functions that contain
249 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250 Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700251 // Weak root state, used for concurrent system weak processing and more.
252 gc::WeakRootState weak_root_state_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700253
254 friend class Transaction;
255 DISALLOW_COPY_AND_ASSIGN(InternTable);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700256};
257
258} // namespace art
259
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700260#endif // ART_RUNTIME_INTERN_TABLE_H_