1 /* 2 * Copyright (c) 1997, 2017, 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.inline.hpp" 29 #include "oops/symbol.hpp" 30 #include "utilities/hashtable.hpp" 31 32 // The symbol table holds all Symbol*s and corresponding interned strings. 33 // Symbol*s and literal strings should be canonicalized. 34 // 35 // The interned strings are created lazily. 36 // 37 // It is implemented as an open hash table with a fixed number of buckets. 38 // 39 // %note: 40 // - symbolTableEntrys are allocated in blocks to reduce the space overhead. 41 42 class BoolObjectClosure; 43 class outputStream; 44 45 46 // Class to hold a newly created or referenced Symbol* temporarily in scope. 47 // new_symbol() and lookup() will create a Symbol* if not already in the 48 // symbol table and add to the symbol's reference count. 49 // probe() and lookup_only() will increment the refcount if symbol is found. 50 class TempNewSymbol : public StackObj { 51 Symbol* _temp; 52 53 public: 54 TempNewSymbol() : _temp(NULL) {} 55 // Creating or looking up a symbol increments the symbol's reference count 56 TempNewSymbol(Symbol *s) : _temp(s) {} 57 58 // Operator= increments reference count. 59 void operator=(const TempNewSymbol &s) { 60 //clear(); //FIXME 61 _temp = s._temp; 62 if (_temp !=NULL) _temp->increment_refcount(); 63 } 64 65 // Decrement reference counter so it can go away if it's unique 66 void clear() { if (_temp != NULL) _temp->decrement_refcount(); _temp = NULL; } 67 68 ~TempNewSymbol() { clear(); } 69 70 // Operators so they can be used like Symbols 71 Symbol* operator -> () const { return _temp; } 72 bool operator == (Symbol* o) const { return _temp == o; } 73 // Sneaky conversion function 74 operator Symbol*() { return _temp; } 75 }; 76 77 class SymbolTable : public RehashableHashtable<Symbol*, mtSymbol> { 78 friend class VMStructs; 79 friend class ClassFileParser; 80 81 private: 82 // The symbol table 83 static SymbolTable* _the_table; 84 85 // Set if one bucket is out of balance due to hash algorithm deficiency 86 static bool _needs_rehashing; 87 88 // For statistics 89 static int _symbols_removed; 90 static int _symbols_counted; 91 92 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F 93 94 // Adding elements 95 Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue, 96 bool c_heap, TRAPS); 97 bool basic_add(ClassLoaderData* loader_data, 98 constantPoolHandle cp, int names_count, 99 const char** names, int* lengths, int* cp_indices, 100 unsigned int* hashValues, TRAPS); 101 102 static void new_symbols(ClassLoaderData* loader_data, 103 constantPoolHandle cp, int names_count, 104 const char** name, int* lengths, 105 int* cp_indices, unsigned int* hashValues, 106 TRAPS) { 107 add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD); 108 } 109 110 Symbol* lookup(int index, const char* name, int len, unsigned int hash); 111 112 SymbolTable() 113 : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {} 114 115 SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries) 116 : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>), t, 117 number_of_entries) {} 118 119 // Arena for permanent symbols (null class loader) that are never unloaded 120 static Arena* _arena; 121 static Arena* arena() { return _arena; } // called for statistics 122 123 static void initialize_symbols(int arena_alloc_size = 0); 124 125 static volatile int _parallel_claimed_idx; 126 127 typedef SymbolTable::BucketUnlinkContext BucketUnlinkContext; 128 // Release any dead symbols. Unlinked bucket entries are collected in the given 129 // context to be freed later. 130 // This allows multiple threads to work on the table at once. 131 static void buckets_unlink(int start_idx, int end_idx, BucketUnlinkContext* context, size_t* memory_total); 132 public: 133 enum { 134 symbol_alloc_batch_size = 8, 135 // Pick initial size based on java -version size measurements 136 symbol_alloc_arena_size = 360*K 137 }; 138 139 // The symbol table 140 static SymbolTable* the_table() { return _the_table; } 141 142 TableStatistics get_table_statistics(); 143 144 // Size of one bucket in the string table. Used when checking for rollover. 145 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); } 146 147 static void create_table() { 148 assert(_the_table == NULL, "One symbol table allowed."); 149 _the_table = new SymbolTable(); 150 initialize_symbols(symbol_alloc_arena_size); 151 } 152 153 static void create_table(HashtableBucket<mtSymbol>* t, int length, 154 int number_of_entries) { 155 assert(_the_table == NULL, "One symbol table allowed."); 156 157 // If CDS archive used a different symbol table size, use that size instead 158 // which is better than giving an error. 159 SymbolTableSize = length/bucket_size(); 160 161 _the_table = new SymbolTable(t, number_of_entries); 162 // if CDS give symbol table a default arena size since most symbols 163 // are already allocated in the shared misc section. 164 initialize_symbols(); 165 } 166 167 static unsigned int hash_symbol(const char* s, int len); 168 169 static Symbol* lookup(const char* name, int len, TRAPS); 170 // lookup only, won't add. Also calculate hash. 171 static Symbol* lookup_only(const char* name, int len, unsigned int& hash); 172 // Only copy to C string to be added if lookup failed. 173 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS); 174 175 static void release(Symbol* sym); 176 177 // Look up the address of the literal in the SymbolTable for this Symbol* 178 static Symbol** lookup_symbol_addr(Symbol* sym); 179 180 // jchar (utf16) version of lookups 181 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS); 182 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash); 183 184 static void add(ClassLoaderData* loader_data, 185 constantPoolHandle cp, int names_count, 186 const char** names, int* lengths, int* cp_indices, 187 unsigned int* hashValues, TRAPS); 188 189 // Release any dead symbols 190 static void unlink() { 191 int processed = 0; 192 int removed = 0; 193 unlink(&processed, &removed); 194 } 195 static void unlink(int* processed, int* removed); 196 // Release any dead symbols, possibly parallel version 197 static void possibly_parallel_unlink(int* processed, int* removed); 198 199 // iterate over symbols 200 static void symbols_do(SymbolClosure *cl); 201 202 // Symbol creation 203 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { 204 assert(utf8_buffer != NULL, "just checking"); 205 return lookup(utf8_buffer, length, THREAD); 206 } 207 static Symbol* new_symbol(const char* name, TRAPS) { 208 return new_symbol(name, (int)strlen(name), THREAD); 209 } 210 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { 211 assert(begin <= end && end <= sym->utf8_length(), "just checking"); 212 return lookup(sym, begin, end, THREAD); 213 } 214 215 // Create a symbol in the arena for symbols that are not deleted 216 static Symbol* new_permanent_symbol(const char* name, TRAPS); 217 218 // Symbol lookup 219 static Symbol* lookup(int index, const char* name, int len, TRAPS); 220 221 // Needed for preloading classes in signatures when compiling. 222 // Returns the symbol is already present in symbol table, otherwise 223 // NULL. NO ALLOCATION IS GUARANTEED! 224 static Symbol* probe(const char* name, int len) { 225 unsigned int ignore_hash; 226 return lookup_only(name, len, ignore_hash); 227 } 228 static Symbol* probe_unicode(const jchar* name, int len) { 229 unsigned int ignore_hash; 230 return lookup_only_unicode(name, len, ignore_hash); 231 } 232 233 // Histogram 234 static void print_histogram() PRODUCT_RETURN; 235 static void print() PRODUCT_RETURN; 236 237 // Debugging 238 static void verify(); 239 static void dump(outputStream* st); 240 241 // Sharing 242 static void copy_buckets(char** top, char*end) { 243 the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end); 244 } 245 static void copy_table(char** top, char*end) { 246 the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end); 247 } 248 static void reverse(void* boundary = NULL) { 249 the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary); 250 } 251 252 // Rehash the symbol table if it gets out of balance 253 static void rehash_table(); 254 static bool needs_rehashing() { return _needs_rehashing; } 255 // Parallel chunked scanning 256 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; } 257 static int parallel_claimed_index() { return _parallel_claimed_idx; } 258 }; 259 260 class StringTable : public RehashableHashtable<oop, mtSymbol> { 261 friend class VMStructs; 262 263 private: 264 // The string table 265 static StringTable* _the_table; 266 267 // Set if one bucket is out of balance due to hash algorithm deficiency 268 static bool _needs_rehashing; 269 270 // Claimed high water mark for parallel chunked scanning 271 static volatile int _parallel_claimed_idx; 272 273 static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS); 274 oop basic_add(int index, Handle string_or_null, jchar* name, int len, 275 unsigned int hashValue, TRAPS); 276 277 oop lookup(int index, jchar* chars, int length, unsigned int hashValue); 278 279 // Apply the give oop closure to the entries to the buckets 280 // in the range [start_idx, end_idx). 281 static void buckets_oops_do(OopClosure* f, int start_idx, int end_idx); 282 283 typedef StringTable::BucketUnlinkContext BucketUnlinkContext; 284 // Unlink or apply the give oop closure to the entries to the buckets 285 // in the range [start_idx, end_idx). Unlinked bucket entries are collected in the given 286 // context to be freed later. 287 // This allows multiple threads to work on the table at once. 288 static void buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, BucketUnlinkContext* context); 289 290 StringTable() : RehashableHashtable<oop, mtSymbol>((int)StringTableSize, 291 sizeof (HashtableEntry<oop, mtSymbol>)) {} 292 293 StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries) 294 : RehashableHashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t, 295 number_of_entries) {} 296 public: 297 // The string table 298 static StringTable* the_table() { return _the_table; } 299 TableStatistics get_table_statistics(); 300 301 // Size of one bucket in the string table. Used when checking for rollover. 302 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); } 303 304 static void create_table() { 305 assert(_the_table == NULL, "One string table allowed."); 306 _the_table = new StringTable(); 307 } 308 309 // GC support 310 // Delete pointers to otherwise-unreachable objects. 311 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f) { 312 int processed = 0; 313 int removed = 0; 314 unlink_or_oops_do(cl, f, &processed, &removed); 315 } 316 static void unlink(BoolObjectClosure* cl) { 317 int processed = 0; 318 int removed = 0; 319 unlink_or_oops_do(cl, NULL, &processed, &removed); 320 } 321 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed); 322 static void unlink(BoolObjectClosure* cl, int* processed, int* removed) { 323 unlink_or_oops_do(cl, NULL, processed, removed); 324 } 325 // Serially invoke "f->do_oop" on the locations of all oops in the table. 326 static void oops_do(OopClosure* f); 327 328 // Possibly parallel versions of the above 329 static void possibly_parallel_unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed); 330 static void possibly_parallel_unlink(BoolObjectClosure* cl, int* processed, int* removed) { 331 possibly_parallel_unlink_or_oops_do(cl, NULL, processed, removed); 332 } 333 static void possibly_parallel_oops_do(OopClosure* f); 334 335 // Hashing algorithm, used as the hash value used by the 336 // StringTable for bucket selection and comparison (stored in the 337 // HashtableEntry structures). This is used in the String.intern() method. 338 static unsigned int hash_string(const jchar* s, int len); 339 340 // Internal test. 341 static void test_alt_hash() PRODUCT_RETURN; 342 343 // Probing 344 static oop lookup(Symbol* symbol); 345 static oop lookup(jchar* chars, int length); 346 347 // Interning 348 static oop intern(Symbol* symbol, TRAPS); 349 static oop intern(oop string, TRAPS); 350 static oop intern(const char *utf8_string, TRAPS); 351 352 // Debugging 353 static void verify(); 354 static void dump(outputStream* st); 355 356 enum VerifyMesgModes { 357 _verify_quietly = 0, 358 _verify_with_mesgs = 1 359 }; 360 361 enum VerifyRetTypes { 362 _verify_pass = 0, 363 _verify_fail_continue = 1, 364 _verify_fail_done = 2 365 }; 366 367 static VerifyRetTypes compare_entries(int bkt1, int e_cnt1, 368 HashtableEntry<oop, mtSymbol>* e_ptr1, 369 int bkt2, int e_cnt2, 370 HashtableEntry<oop, mtSymbol>* e_ptr2); 371 static VerifyRetTypes verify_entry(int bkt, int e_cnt, 372 HashtableEntry<oop, mtSymbol>* e_ptr, 373 VerifyMesgModes mesg_mode); 374 static int verify_and_compare_entries(); 375 376 // Sharing 377 static void copy_buckets(char** top, char*end) { 378 the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end); 379 } 380 static void copy_table(char** top, char*end) { 381 the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end); 382 } 383 static void reverse() { 384 the_table()->Hashtable<oop, mtSymbol>::reverse(); 385 } 386 387 // Rehash the symbol table if it gets out of balance 388 static void rehash_table(); 389 static bool needs_rehashing() { return _needs_rehashing; } 390 391 // Parallel chunked scanning 392 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; } 393 static int parallel_claimed_index() { return _parallel_claimed_idx; } 394 }; 395 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP