1 /* 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP 26 #define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/padded.hpp" 30 #include "oops/symbol.hpp" 31 #include "utilities/hashtable.hpp" 32 #include "utilities/concurrentHashTable.hpp" 33 34 // TempNewSymbol acts as a handle class in a handle/body idiom and is 35 // responsible for proper resource management of the body (which is a Symbol*). 36 // The body is resource managed by a reference counting scheme. 37 // TempNewSymbol can therefore be used to properly hold a newly created or referenced 38 // Symbol* temporarily in scope. 39 // 40 // Routines in SymbolTable will initialize the reference count of a Symbol* before 41 // it becomes "managed" by TempNewSymbol instances. As a handle class, TempNewSymbol 42 // needs to maintain proper reference counting in context of copy semantics. 43 // 44 // In SymbolTable, new_symbol() and lookup() will create a Symbol* if not already in the 45 // symbol table and add to the symbol's reference count. 46 // probe() and lookup_only() will increment the refcount if symbol is found. 47 class TempNewSymbol : public StackObj { 48 Symbol* _temp; 49 50 public: 51 TempNewSymbol() : _temp(NULL) {} 52 53 // Conversion from a Symbol* to a TempNewSymbol. 54 // Does not increment the current reference count. 55 TempNewSymbol(Symbol *s) : _temp(s) {} 56 57 // Copy constructor increments reference count. 58 TempNewSymbol(const TempNewSymbol& rhs) : _temp(rhs._temp) { 59 if (_temp != NULL) { 60 _temp->increment_refcount(); 61 } 62 } 63 64 // Assignment operator uses a c++ trick called copy and swap idiom. 65 // rhs is passed by value so within the scope of this method it is a copy. 66 // At method exit it contains the former value of _temp, triggering the correct refcount 67 // decrement upon destruction. 68 void operator=(TempNewSymbol rhs) { 69 Symbol* tmp = rhs._temp; 70 rhs._temp = _temp; 71 _temp = tmp; 72 } 73 74 // Decrement reference counter so it can go away if it's unused 75 ~TempNewSymbol() { 76 if (_temp != NULL) { 77 _temp->decrement_refcount(); 78 } 79 } 80 81 // Symbol* conversion operators 82 Symbol* operator -> () const { return _temp; } 83 bool operator == (Symbol* o) const { return _temp == o; } 84 operator Symbol*() { return _temp; } 85 }; 86 87 // The symbol table holds all Symbol*s and corresponding interned strings. 88 // Symbol*s and literal strings should be canonicalized. 89 // 90 // The interned strings are created lazily. 91 // 92 // %note: 93 // - symbolTableEntrys are allocated in blocks to reduce the space overhead. 94 template <class T, class N> class CompactHashtable; 95 class CompactSymbolTableWriter; 96 class SerializeClosure; 97 98 class SymbolTable; 99 class SymbolTableConfig; 100 typedef ConcurrentHashTable<Symbol*, 101 SymbolTableConfig, mtSymbol> SymbolTableHash; 102 103 class SymbolTableCreateEntry; 104 105 class SymbolTable : public CHeapObj<mtSymbol> { 106 friend class VMStructs; 107 friend class Symbol; 108 friend class ClassFileParser; 109 friend class SymbolTableConfig; 110 friend class SymbolTableCreateEntry; 111 112 private: 113 static void delete_symbol(Symbol* sym); 114 void grow(JavaThread* jt); 115 void clean_dead_entries(JavaThread* jt); 116 117 // The symbol table 118 static SymbolTable* _the_table; 119 // Shared symbol table. 120 static CompactHashtable<Symbol*, char> _shared_table; 121 static bool _lookup_shared_first; 122 static bool _alt_hash; 123 124 // For statistics 125 static int _symbols_removed; 126 static int _symbols_counted; 127 128 // Set if one bucket is out of balance due to hash algorithm deficiency 129 SymbolTableHash* _local_table; 130 size_t _current_size; 131 volatile bool _has_work; 132 volatile bool _needs_rehashing; 133 134 volatile int _items; 135 DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int)); 136 volatile int _uncleaned_items; 137 DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int)); 138 139 double get_load_factor(); 140 double get_dead_factor(); 141 142 void check_concurrent_work(); 143 void trigger_concurrent_work(); 144 145 static uintx item_added(); 146 static void item_removed(); 147 static void set_item_clean_count(size_t ncl); 148 static void mark_item_clean_count(); 149 150 SymbolTable(); 151 152 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F 153 Symbol* do_lookup(char* name, int len, uintx hash); 154 Symbol* do_add_if_needed(char* name, int len, uintx hash, bool heap, TRAPS); 155 156 // Adding elements 157 static void add(ClassLoaderData* loader_data, 158 const constantPoolHandle& cp, int names_count, 159 const char** names, int* lengths, int* cp_indices, 160 unsigned int* hashValues, TRAPS); 161 162 static void new_symbols(ClassLoaderData* loader_data, 163 const constantPoolHandle& cp, int names_count, 164 const char** name, int* lengths, 165 int* cp_indices, unsigned int* hashValues, 166 TRAPS) { 167 add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD); 168 } 169 170 static Symbol* lookup_shared(const char* name, int len, unsigned int hash); 171 Symbol* lookup_dynamic(const char* name, int len, unsigned int hash); 172 Symbol* lookup_common(const char* name, int len, unsigned int hash); 173 174 // Arena for permanent symbols (null class loader) that are never unloaded 175 static Arena* _arena; 176 static Arena* arena() { return _arena; } // called for statistics 177 178 static void initialize_symbols(int arena_alloc_size = 0); 179 180 void concurrent_work(JavaThread* jt); 181 void print_table_statistics(outputStream* st, const char* table_name); 182 183 void try_rehash_table(); 184 bool do_rehash(); 185 186 public: 187 // The symbol table 188 static SymbolTable* the_table() { return _the_table; } 189 size_t table_size(Thread* thread = NULL); 190 191 enum { 192 symbol_alloc_batch_size = 8, 193 // Pick initial size based on java -version size measurements 194 symbol_alloc_arena_size = 360*K // TODO (revisit) 195 }; 196 197 static void create_table() { 198 assert(_the_table == NULL, "One symbol table allowed."); 199 _the_table = new SymbolTable(); 200 initialize_symbols(symbol_alloc_arena_size); 201 } 202 203 static void unlink() { 204 do_check_concurrent_work(); 205 } 206 static void do_check_concurrent_work(); 207 static void do_concurrent_work(JavaThread* jt); 208 static bool has_work() { return the_table()->_has_work; } 209 210 // Probing 211 static Symbol* lookup(const char* name, int len, TRAPS); 212 // lookup only, won't add. Also calculate hash. 213 static Symbol* lookup_only(const char* name, int len, unsigned int& hash); 214 // Only copy to C string to be added if lookup failed. 215 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS); 216 // jchar (UTF16) version of lookups 217 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS); 218 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash); 219 // Needed for preloading classes in signatures when compiling. 220 // Returns the symbol is already present in symbol table, otherwise 221 // NULL. NO ALLOCATION IS GUARANTEED! 222 static Symbol* probe(const char* name, int len) { 223 unsigned int ignore_hash; 224 return lookup_only(name, len, ignore_hash); 225 } 226 static Symbol* probe_unicode(const jchar* name, int len) { 227 unsigned int ignore_hash; 228 return lookup_only_unicode(name, len, ignore_hash); 229 } 230 231 // Symbol creation 232 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { 233 assert(utf8_buffer != NULL, "just checking"); 234 return lookup(utf8_buffer, length, THREAD); 235 } 236 static Symbol* new_symbol(const char* name, TRAPS) { 237 return new_symbol(name, (int)strlen(name), THREAD); 238 } 239 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { 240 assert(begin <= end && end <= sym->utf8_length(), "just checking"); 241 return lookup(sym, begin, end, THREAD); 242 } 243 // Create a symbol in the arena for symbols that are not deleted 244 static Symbol* new_permanent_symbol(const char* name, TRAPS); 245 246 // Rehash the string table if it gets out of balance 247 static void rehash_table(); 248 static bool needs_rehashing() 249 { return SymbolTable::the_table()->_needs_rehashing; } 250 251 // Heap dumper and CDS 252 static void symbols_do(SymbolClosure *cl); 253 254 // Sharing 255 private: 256 static void copy_shared_symbol_table(CompactSymbolTableWriter* ch_table); 257 public: 258 static void write_to_archive() NOT_CDS_RETURN; 259 static void serialize(SerializeClosure* soc) NOT_CDS_RETURN; 260 static void metaspace_pointers_do(MetaspaceClosure* it); 261 262 // Jcmd 263 static void dump(outputStream* st, bool verbose=false); 264 // Debugging 265 static void verify(); 266 static void read(const char* filename, TRAPS); 267 268 // Histogram 269 static void print_histogram() PRODUCT_RETURN; 270 }; 271 272 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP