1 /* 2 * Copyright (c) 1997, 2019, 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_CLASSFILE_SYMBOLTABLE_HPP 26 #define SHARE_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 class JavaThread; 35 36 // TempNewSymbol acts as a handle class in a handle/body idiom and is 37 // responsible for proper resource management of the body (which is a Symbol*). 38 // The body is resource managed by a reference counting scheme. 39 // TempNewSymbol can therefore be used to properly hold a newly created or referenced 40 // Symbol* temporarily in scope. 41 // 42 // Routines in SymbolTable will initialize the reference count of a Symbol* before 43 // it becomes "managed" by TempNewSymbol instances. As a handle class, TempNewSymbol 44 // needs to maintain proper reference counting in context of copy semantics. 45 // 46 // In SymbolTable, new_symbol() and lookup() will create a Symbol* if not already in the 47 // symbol table and add to the symbol's reference count. 48 // probe() and lookup_only() will increment the refcount if symbol is found. 49 class TempNewSymbol : public StackObj { 50 Symbol* _temp; 51 52 public: 53 TempNewSymbol() : _temp(NULL) {} 54 55 // Conversion from a Symbol* to a TempNewSymbol. 56 // Does not increment the current reference count. 57 TempNewSymbol(Symbol *s) : _temp(s) {} 58 59 // Copy constructor increments reference count. 60 TempNewSymbol(const TempNewSymbol& rhs) : _temp(rhs._temp) { 61 if (_temp != NULL) { 62 _temp->increment_refcount(); 63 } 64 } 65 66 // Assignment operator uses a c++ trick called copy and swap idiom. 67 // rhs is passed by value so within the scope of this method it is a copy. 68 // At method exit it contains the former value of _temp, triggering the correct refcount 69 // decrement upon destruction. 70 void operator=(TempNewSymbol rhs) { 71 Symbol* tmp = rhs._temp; 72 rhs._temp = _temp; 73 _temp = tmp; 74 } 75 76 // Decrement reference counter so it can go away if it's unused 77 ~TempNewSymbol() { 78 if (_temp != NULL) { 79 _temp->decrement_refcount(); 80 } 81 } 82 83 // Symbol* conversion operators 84 Symbol* operator -> () const { return _temp; } 85 bool operator == (Symbol* o) const { return _temp == o; } 86 operator Symbol*() { return _temp; } 87 }; 88 89 class CompactHashtableWriter; 90 class SerializeClosure; 91 92 class SymbolTableConfig; 93 typedef ConcurrentHashTable<Symbol*, 94 SymbolTableConfig, mtSymbol> SymbolTableHash; 95 96 class SymbolTableCreateEntry; 97 98 class SymbolTable : public CHeapObj<mtSymbol> { 99 friend class VMStructs; 100 friend class Symbol; 101 friend class ClassFileParser; 102 friend class SymbolTableConfig; 103 friend class SymbolTableCreateEntry; 104 105 private: 106 static void delete_symbol(Symbol* sym); 107 void grow(JavaThread* jt); 108 void clean_dead_entries(JavaThread* jt); 109 110 // The symbol table 111 static SymbolTable* _the_table; 112 static volatile bool _lookup_shared_first; 113 static volatile bool _alt_hash; 114 115 // For statistics 116 volatile size_t _symbols_removed; 117 volatile size_t _symbols_counted; 118 119 SymbolTableHash* _local_table; 120 size_t _current_size; 121 volatile bool _has_work; 122 // Set if one bucket is out of balance due to hash algorithm deficiency 123 volatile bool _needs_rehashing; 124 125 volatile size_t _items_count; 126 volatile bool _has_items_to_clean; 127 128 double get_load_factor() const; 129 130 void check_concurrent_work(); 131 132 static void item_added(); 133 static void item_removed(); 134 135 // For cleaning 136 void reset_has_items_to_clean(); 137 void mark_has_items_to_clean(); 138 bool has_items_to_clean() const; 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 new_symbols(ClassLoaderData* loader_data, 148 const constantPoolHandle& cp, int names_count, 149 const char** name, int* lengths, 150 int* cp_indices, unsigned int* hashValues, 151 TRAPS); 152 153 static Symbol* lookup_shared(const char* name, int len, unsigned int hash); 154 Symbol* lookup_dynamic(const char* name, int len, unsigned int hash); 155 Symbol* lookup_common(const char* name, int len, unsigned int hash); 156 157 // Arena for permanent symbols (null class loader) that are never unloaded 158 static Arena* _arena; 159 static Arena* arena() { return _arena; } // called for statistics 160 161 static void initialize_symbols(int arena_alloc_size = 0); 162 163 void concurrent_work(JavaThread* jt); 164 void print_table_statistics(outputStream* st, const char* table_name); 165 166 void try_rehash_table(); 167 bool do_rehash(); 168 inline void update_needs_rehash(bool rehash); 169 170 public: 171 // The symbol table 172 static SymbolTable* the_table() { return _the_table; } 173 size_t table_size(); 174 TableStatistics get_table_statistics(); 175 176 enum { 177 symbol_alloc_batch_size = 8, 178 // Pick initial size based on java -version size measurements 179 symbol_alloc_arena_size = 360*K // TODO (revisit) 180 }; 181 182 static void create_table() { 183 assert(_the_table == NULL, "One symbol table allowed."); 184 _the_table = new SymbolTable(); 185 initialize_symbols(symbol_alloc_arena_size); 186 } 187 188 static void do_concurrent_work(JavaThread* jt); 189 static bool has_work() { return the_table()->_has_work; } 190 static void trigger_cleanup(); 191 192 // Probing 193 static Symbol* lookup(const char* name, int len, TRAPS); 194 // lookup only, won't add. Also calculate hash. 195 static Symbol* lookup_only(const char* name, int len, unsigned int& hash); 196 // adds new symbol if not found 197 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS); 198 // jchar (UTF16) version of lookups 199 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS); 200 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash); 201 // Needed for preloading classes in signatures when compiling. 202 // Returns the symbol is already present in symbol table, otherwise 203 // NULL. NO ALLOCATION IS GUARANTEED! 204 static Symbol* probe(const char* name, int len) { 205 unsigned int ignore_hash; 206 return lookup_only(name, len, ignore_hash); 207 } 208 static Symbol* probe_unicode(const jchar* name, int len) { 209 unsigned int ignore_hash; 210 return lookup_only_unicode(name, len, ignore_hash); 211 } 212 213 // Symbol creation 214 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { 215 assert(utf8_buffer != NULL, "just checking"); 216 return lookup(utf8_buffer, length, THREAD); 217 } 218 static Symbol* new_symbol(const char* name, TRAPS) { 219 return new_symbol(name, (int)strlen(name), THREAD); 220 } 221 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { 222 assert(begin <= end && end <= sym->utf8_length(), "just checking"); 223 return lookup(sym, begin, end, THREAD); 224 } 225 // Create a symbol in the arena for symbols that are not deleted 226 static Symbol* new_permanent_symbol(const char* name, TRAPS); 227 228 // Rehash the string table if it gets out of balance 229 static void rehash_table(); 230 static bool needs_rehashing() 231 { return SymbolTable::the_table()->_needs_rehashing; } 232 233 // Heap dumper and CDS 234 static void symbols_do(SymbolClosure *cl); 235 236 // Sharing 237 private: 238 static void copy_shared_symbol_table(CompactHashtableWriter* ch_table); 239 public: 240 static void write_to_archive() NOT_CDS_RETURN; 241 static void serialize_shared_table_header(SerializeClosure* soc) NOT_CDS_RETURN; 242 static void metaspace_pointers_do(MetaspaceClosure* it); 243 244 // Jcmd 245 static void dump(outputStream* st, bool verbose=false); 246 // Debugging 247 static void verify(); 248 static void read(const char* filename, TRAPS); 249 250 // Histogram 251 static void print_histogram() PRODUCT_RETURN; 252 }; 253 254 #endif // SHARE_CLASSFILE_SYMBOLTABLE_HPP