blob: ef08d74c7fced3fc4e147587e71c8994ed772862 [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)
Ian Rogersb726dcb2012-09-05 08:57:23 -070061 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070062
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070063 // Only used by image writer.
64 mirror::String* InternImageString(mirror::String* s)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
66
67 // Interns a potentially new string in the 'strong' table. May cause thread suspension.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 mirror::String* InternStrong(const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070070
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070071 // Interns a potentially new string in the 'strong' table. May cause thread suspension.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070073
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070074 // Interns a potentially new string in the 'weak' table. May cause thread suspension.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070076
Mathieu Chartier97509952015-07-13 14:35:43 -070077 void SweepInternTableWeaks(IsMarkedVisitor* visitor)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070081
Mathieu Chartiereb175f72014-10-31 11:49:27 -070082 // Total number of interned strings.
83 size_t Size() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
84 // Total number of weakly live interned strings.
85 size_t StrongSize() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
86 // Total number of strongly live interned strings.
87 size_t WeakSize() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070088
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070089 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070090 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070091
Elliott Hughescac6cc72011-11-03 20:31:21 -070092 void DumpForSigQuit(std::ostream& os) const;
93
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080094 void DisallowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070095 void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080096 void EnsureNewInternsDisallowed() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070097 void BroadcastForNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070098 void EnsureNewWeakInternsDisallowed() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070099
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700100 // Adds all of the resolved image strings from the image space into the intern table. The
101 // advantage of doing this is preventing expensive DexFile::FindStringId calls.
102 void AddImageStringsToTable(gc::space::ImageSpace* image_space)
103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::intern_table_lock_);
104 // Copy the post zygote tables to pre zygote to save memory by preventing dirty pages.
105 void SwapPostZygoteWithPreZygote()
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::intern_table_lock_);
107
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700108 // Add an intern table which was serialized to the image.
109 void AddImageInternTable(gc::space::ImageSpace* image_space)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::intern_table_lock_);
111
112 // Read the intern table from memory. The elements aren't copied, the intern hash set data will
113 // point to somewhere within ptr. Only reads the strong interns.
114 size_t ReadFromMemory(const uint8_t* ptr) LOCKS_EXCLUDED(Locks::intern_table_lock_)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117 // Write the post zygote intern table to a pointer. Only writes the strong interns since it is
118 // expected that there is no weak interns since this is called from the image writer.
119 size_t WriteToMemory(uint8_t* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
120 LOCKS_EXCLUDED(Locks::intern_table_lock_);
121
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700122 // Change the weak root state. May broadcast to waiters.
123 void ChangeWeakRootState(gc::WeakRootState new_state)
124 LOCKS_EXCLUDED(Locks::intern_table_lock_);
125
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700126 private:
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700127 class StringHashEquals {
128 public:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800129 std::size_t operator()(const GcRoot<mirror::String>& root) const NO_THREAD_SAFETY_ANALYSIS;
130 bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b) const
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700131 NO_THREAD_SAFETY_ANALYSIS;
132 };
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800133 class GcRootEmptyFn {
134 public:
135 void MakeEmpty(GcRoot<mirror::String>& item) const {
136 item = GcRoot<mirror::String>();
137 }
138 bool IsEmpty(const GcRoot<mirror::String>& item) const {
139 return item.IsNull();
140 }
141 };
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700142
143 // Table which holds pre zygote and post zygote interned strings. There is one instance for
144 // weak interns and strong interns.
145 class Table {
146 public:
147 mirror::String* Find(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
148 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
149 void Insert(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
150 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
151 void Remove(mirror::String* s)
152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
153 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700154 void VisitRoots(RootVisitor* visitor)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
156 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700157 void SweepWeaks(IsMarkedVisitor* visitor)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
159 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
160 void SwapPostZygoteWithPreZygote() EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800161 size_t Size() const EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700162 // Read pre zygote table is called from ReadFromMemory which happens during runtime creation
163 // when we load the image intern table. Returns how many bytes were read.
164 size_t ReadIntoPreZygoteTable(const uint8_t* ptr)
165 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_)
166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
167 // The image writer calls WritePostZygoteTable through WriteToMemory, it writes the interns in
168 // the post zygote table. Returns how many bytes were written.
169 size_t WriteFromPostZygoteTable(uint8_t* ptr)
170 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_)
171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700172
173 private:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800174 typedef HashSet<GcRoot<mirror::String>, GcRootEmptyFn, StringHashEquals, StringHashEquals,
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700175 TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>> UnorderedSet;
176
Mathieu Chartier97509952015-07-13 14:35:43 -0700177 void SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
179 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
180
181 // We call SwapPostZygoteWithPreZygote when we create the zygote to reduce private dirty pages
182 // caused by modifying the zygote intern table hash table. The pre zygote table are the
183 // interned strings which were interned before we created the zygote space. Post zygote is self
184 // explanatory.
185 UnorderedSet pre_zygote_table_;
186 UnorderedSet post_zygote_table_;
187 };
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700188
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700189 // Insert if non null, otherwise return null.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700190 mirror::String* Insert(mirror::String* s, bool is_strong, bool holding_locks)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100191 LOCKS_EXCLUDED(Locks::intern_table_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700193
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700194 mirror::String* LookupStrong(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
196 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700197 mirror::String* LookupWeak(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
199 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700200 mirror::String* InsertStrong(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100202 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700203 mirror::String* InsertWeak(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100205 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700206 void RemoveStrong(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100208 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700209 void RemoveWeak(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
211 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700212
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100213 // Transaction rollback access.
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700214 mirror::String* LookupStringFromImage(mirror::String* s)
215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
216 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700217 mirror::String* InsertStrongFromTransaction(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100219 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700220 mirror::String* InsertWeakFromTransaction(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700223 void RemoveStrongFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100225 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700226 void RemoveWeakFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100228 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
229 friend class Transaction;
230
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700231 size_t ReadFromMemoryLocked(const uint8_t* ptr)
232 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_)
233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
234
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700235 // Change the weak root state. May broadcast to waiters.
236 void ChangeWeakRootStateLocked(gc::WeakRootState new_state)
237 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
238
239 // Wait until we can read weak roots.
240 void WaitUntilAccessible(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_)
241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
242
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700243 bool image_added_to_intern_table_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800244 bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700245 ConditionVariable weak_intern_condition_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700246 // Since this contains (strong) roots, they need a read barrier to
247 // enable concurrent intern table (strong) root scan. Do not
248 // directly access the strings in it. Use functions that contain
249 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250 Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700251 std::vector<GcRoot<mirror::String>> new_strong_intern_roots_
Mathieu Chartier893263b2014-03-04 11:07:42 -0800252 GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700253 // Since this contains (weak) roots, they need a read barrier. Do
254 // not directly access the strings in it. Use functions that contain
255 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100256 Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700257 // Weak root state, used for concurrent system weak processing and more.
258 gc::WeakRootState weak_root_state_ GUARDED_BY(Locks::intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700259};
260
261} // namespace art
262
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700263#endif // ART_RUNTIME_INTERN_TABLE_H_