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/concurrentHashTable.hpp" 32 #include "utilities/hashtable.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 template <class T, class N> class CompactHashtable; 88 class CompactSymbolTableWriter; 89 class SerializeClosure; 90 91 class SymbolTableConfig; 92 typedef ConcurrentHashTable<Symbol*, 93 SymbolTableConfig, mtSymbol> SymbolTableHash; 94 95 class SymbolTableCreateEntry; 96 97 class SymbolTable : public CHeapObj<mtSymbol> { 98 friend class VMStructs; 99 friend class Symbol; 100 friend class ClassFileParser; 101 friend class SymbolTableConfig; 102 friend class SymbolTableCreateEntry; 103 104 private: 105 static void delete_symbol(Symbol* sym); 106 void grow(JavaThread* jt); 107 void clean_dead_entries(JavaThread* jt); 108 109 // The symbol table 110 static SymbolTable* _the_table; 111 // Shared symbol table. 112 static CompactHashtable<Symbol*, char> _shared_table; 113 static volatile bool _lookup_shared_first; 114 static volatile bool _alt_hash; 115 116 // For statistics 117 volatile size_t _symbols_removed; 118 volatile size_t _symbols_counted; 119 120 SymbolTableHash* _local_table; 121 size_t _current_size; 122 volatile bool _has_work; 123 // Set if one bucket is out of balance due to hash algorithm deficiency 124 volatile bool _needs_rehashing; 125 126 volatile size_t _items_count; 127 volatile size_t _uncleaned_items_count; 128 129 double get_load_factor(); 130 double get_dead_factor(); 131 132 void check_concurrent_work(); 133 void trigger_concurrent_work(); 134 135 static void item_added(); 136 static void item_removed(); 137 static void set_item_clean_count(size_t ncl); 138 static void mark_item_clean_count(); 139 140 SymbolTable(); 141 142 Symbol* allocate_symbol(const char* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F 143 Symbol* do_lookup(const char* name, int len, uintx hash); 144 Symbol* do_add_if_needed(const char* name, int len, uintx hash, bool heap, TRAPS); 145 146 // Adding elements 147 static void add(ClassLoaderData* loader_data, 148 const constantPoolHandle& cp, int names_count, 149 const char** names, int* lengths, int* cp_indices, 150 unsigned int* hashValues, TRAPS); 151 152 static void new_symbols(ClassLoaderData* loader_data, 153 const constantPoolHandle& cp, int names_count, 154 const char** name, int* lengths, 155 int* cp_indices, unsigned int* hashValues, 156 TRAPS) { 157 add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD); 158 } 159 160 static Symbol* lookup_shared(const char* name, int len, unsigned int hash); 161 Symbol* lookup_dynamic(const char* name, int len, unsigned int hash); 162 Symbol* lookup_common(const char* name, int len, unsigned int hash); 163 164 // Arena for permanent symbols (null class loader) that are never unloaded 165 static Arena* _arena; 166 static Arena* arena() { return _arena; } // called for statistics 167 168 static void initialize_symbols(int arena_alloc_size = 0); 169 170 void concurrent_work(JavaThread* jt); 171 void print_table_statistics(outputStream* st, const char* table_name); 172 173 void try_rehash_table(); 174 bool do_rehash(); 175 176 public: 177 // The symbol table 178 static SymbolTable* the_table() { return _the_table; } 179 size_t table_size(Thread* thread = NULL); 180 181 enum { 182 symbol_alloc_batch_size = 8, 183 // Pick initial size based on java -version size measurements 184 symbol_alloc_arena_size = 360*K // TODO (revisit) 185 }; 186 187 static void create_table() { 188 assert(_the_table == NULL, "One symbol table allowed."); 189 _the_table = new SymbolTable(); 190 initialize_symbols(symbol_alloc_arena_size); 191 } 192 193 static void unlink() { 194 do_check_concurrent_work(); 195 } 196 static void do_check_concurrent_work(); 197 static void do_concurrent_work(JavaThread* jt); 198 static bool has_work() { return the_table()->_has_work; } 199 200 // Probing 201 static Symbol* lookup(const char* name, int len, TRAPS); 202 // lookup only, won't add. Also calculate hash. 203 static Symbol* lookup_only(const char* name, int len, unsigned int& hash); 204 // Only copy to C string to be added if lookup failed. 205 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS); 206 // jchar (UTF16) version of lookups 207 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS); 208 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash); 209 // Needed for preloading classes in signatures when compiling. 210 // Returns the symbol is already present in symbol table, otherwise 211 // NULL. NO ALLOCATION IS GUARANTEED! 212 static Symbol* probe(const char* name, int len) { 213 unsigned int ignore_hash; 214 return lookup_only(name, len, ignore_hash); 215 } 216 static Symbol* probe_unicode(const jchar* name, int len) { 217 unsigned int ignore_hash; 218 return lookup_only_unicode(name, len, ignore_hash); 219 } 220 221 // Symbol creation 222 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { 223 assert(utf8_buffer != NULL, "just checking"); 224 return lookup(utf8_buffer, length, THREAD); 225 } 226 static Symbol* new_symbol(const char* name, TRAPS) { 227 return new_symbol(name, (int)strlen(name), THREAD); 228 } 229 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { 230 assert(begin <= end && end <= sym->utf8_length(), "just checking"); 231 return lookup(sym, begin, end, THREAD); 232 } 233 // Create a symbol in the arena for symbols that are not deleted 234 static Symbol* new_permanent_symbol(const char* name, TRAPS); 235 236 // Rehash the string table if it gets out of balance 237 static void rehash_table(); 238 static bool needs_rehashing() 239 { return SymbolTable::the_table()->_needs_rehashing; } 240 241 // Heap dumper and CDS 242 static void symbols_do(SymbolClosure *cl); 243 244 // Sharing 245 private: 246 static void copy_shared_symbol_table(CompactSymbolTableWriter* ch_table); 247 public: 248 static void write_to_archive() NOT_CDS_RETURN; 249 static void serialize(SerializeClosure* soc) NOT_CDS_RETURN; 250 static void metaspace_pointers_do(MetaspaceClosure* it); 251 252 // Jcmd 253 static void dump(outputStream* st, bool verbose=false); 254 // Debugging 255 static void verify(); 256 static void read(const char* filename, TRAPS); 257 258 // Histogram 259 static void print_histogram() PRODUCT_RETURN; 260 }; 261 262 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP