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 175 enum { 176 symbol_alloc_batch_size = 8, 177 // Pick initial size based on java -version size measurements 178 symbol_alloc_arena_size = 360*K // TODO (revisit) 179 }; 180 181 static void create_table() { 182 assert(_the_table == NULL, "One symbol table allowed."); 183 _the_table = new SymbolTable(); 184 initialize_symbols(symbol_alloc_arena_size); 185 } 186 187 static void do_concurrent_work(JavaThread* jt); 188 static bool has_work() { return the_table()->_has_work; } 189 static void trigger_cleanup(); 190 191 // Probing 192 static Symbol* lookup(const char* name, int len, TRAPS); 193 // lookup only, won't add. Also calculate hash. 194 static Symbol* lookup_only(const char* name, int len, unsigned int& hash); 195 // adds new symbol if not found 196 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS); 197 // jchar (UTF16) version of lookups 198 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS); 199 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash); 200 // Needed for preloading classes in signatures when compiling. 201 // Returns the symbol is already present in symbol table, otherwise 202 // NULL. NO ALLOCATION IS GUARANTEED! 203 static Symbol* probe(const char* name, int len) { 204 unsigned int ignore_hash; 205 return lookup_only(name, len, ignore_hash); 206 } 207 static Symbol* probe_unicode(const jchar* name, int len) { 208 unsigned int ignore_hash; 209 return lookup_only_unicode(name, len, ignore_hash); 210 } 211 212 // Symbol creation 213 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { 214 assert(utf8_buffer != NULL, "just checking"); 215 return lookup(utf8_buffer, length, THREAD); 216 } 217 static Symbol* new_symbol(const char* name, TRAPS) { 218 return new_symbol(name, (int)strlen(name), THREAD); 219 } 220 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { 221 assert(begin <= end && end <= sym->utf8_length(), "just checking"); 222 return lookup(sym, begin, end, THREAD); 223 } 224 // Create a symbol in the arena for symbols that are not deleted 225 static Symbol* new_permanent_symbol(const char* name, TRAPS); 226 227 // Rehash the string table if it gets out of balance 228 static void rehash_table(); 229 static bool needs_rehashing() 230 { return SymbolTable::the_table()->_needs_rehashing; } 231 232 // Heap dumper and CDS 233 static void symbols_do(SymbolClosure *cl); 234 235 // Sharing 236 private: 237 static void copy_shared_symbol_table(CompactHashtableWriter* ch_table); 238 public: 239 static void write_to_archive() NOT_CDS_RETURN; 240 static void serialize_shared_table_header(SerializeClosure* soc) NOT_CDS_RETURN; 241 static void metaspace_pointers_do(MetaspaceClosure* it); 242 243 // Jcmd 244 static void dump(outputStream* st, bool verbose=false); 245 // Debugging 246 static void verify(); 247 static void read(const char* filename, TRAPS); 248 249 // Histogram 250 static void print_histogram() PRODUCT_RETURN; 251 }; 252 253 #endif // SHARE_CLASSFILE_SYMBOLTABLE_HPP