1 /* 2 * Copyright (c) 1997, 2014, 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 #include "precompiled.hpp" 26 #include "classfile/altHashing.hpp" 27 #include "classfile/javaClasses.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "gc_interface/collectedHeap.inline.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "memory/filemap.hpp" 33 #include "memory/gcLocker.inline.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "oops/oop.inline2.hpp" 36 #include "runtime/mutexLocker.hpp" 37 #include "utilities/hashtable.inline.hpp" 38 39 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 40 41 // -------------------------------------------------------------------------- 42 // the number of buckets a thread claims 43 const int ClaimChunkSize = 32; 44 45 SymbolTable* SymbolTable::_the_table = NULL; 46 // Static arena for symbols that are not deallocated 47 Arena* SymbolTable::_arena = NULL; 48 bool SymbolTable::_needs_rehashing = false; 49 50 Symbol* SymbolTable::allocate_symbol(const u1* name, int len, bool c_heap, TRAPS) { 51 assert (len <= Symbol::max_length(), "should be checked by caller"); 52 53 Symbol* sym; 54 55 if (DumpSharedSpaces) { 56 // Allocate all symbols to CLD shared metaspace 57 sym = new (len, ClassLoaderData::the_null_class_loader_data(), THREAD) Symbol(name, len, -1); 58 } else if (c_heap) { 59 // refcount starts as 1 60 sym = new (len, THREAD) Symbol(name, len, 1); 61 assert(sym != NULL, "new should call vm_exit_out_of_memory if C_HEAP is exhausted"); 62 } else { 63 // Allocate to global arena 64 sym = new (len, arena(), THREAD) Symbol(name, len, -1); 65 } 66 return sym; 67 } 68 69 void SymbolTable::initialize_symbols(int arena_alloc_size) { 70 // Initialize the arena for global symbols, size passed in depends on CDS. 71 if (arena_alloc_size == 0) { 72 _arena = new (mtSymbol) Arena(); 73 } else { 74 _arena = new (mtSymbol) Arena(arena_alloc_size); 75 } 76 } 77 78 // Call function for all symbols in the symbol table. 79 void SymbolTable::symbols_do(SymbolClosure *cl) { 80 const int n = the_table()->table_size(); 81 for (int i = 0; i < n; i++) { 82 for (HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 83 p != NULL; 84 p = p->next()) { 85 cl->do_symbol(p->literal_addr()); 86 } 87 } 88 } 89 90 int SymbolTable::_symbols_removed = 0; 91 int SymbolTable::_symbols_counted = 0; 92 volatile int SymbolTable::_parallel_claimed_idx = 0; 93 94 void SymbolTable::buckets_unlink(int start_idx, int end_idx, int* processed, int* removed, size_t* memory_total) { 95 for (int i = start_idx; i < end_idx; ++i) { 96 HashtableEntry<Symbol*, mtSymbol>** p = the_table()->bucket_addr(i); 97 HashtableEntry<Symbol*, mtSymbol>* entry = the_table()->bucket(i); 98 while (entry != NULL) { 99 // Shared entries are normally at the end of the bucket and if we run into 100 // a shared entry, then there is nothing more to remove. However, if we 101 // have rehashed the table, then the shared entries are no longer at the 102 // end of the bucket. 103 if (entry->is_shared() && !use_alternate_hashcode()) { 104 break; 105 } 106 Symbol* s = entry->literal(); 107 (*memory_total) += s->size(); 108 (*processed)++; 109 assert(s != NULL, "just checking"); 110 // If reference count is zero, remove. 111 if (s->refcount() == 0) { 112 assert(!entry->is_shared(), "shared entries should be kept live"); 113 delete s; 114 (*removed)++; 115 *p = entry->next(); 116 the_table()->free_entry(entry); 117 } else { 118 p = entry->next_addr(); 119 } 120 // get next entry 121 entry = (HashtableEntry<Symbol*, mtSymbol>*)HashtableEntry<Symbol*, mtSymbol>::make_ptr(*p); 122 } 123 } 124 } 125 126 // Remove unreferenced symbols from the symbol table 127 // This is done late during GC. 128 void SymbolTable::unlink(int* processed, int* removed) { 129 size_t memory_total = 0; 130 buckets_unlink(0, the_table()->table_size(), processed, removed, &memory_total); 131 _symbols_removed += *removed; 132 _symbols_counted += *processed; 133 // Exclude printing for normal PrintGCDetails because people parse 134 // this output. 135 if (PrintGCDetails && Verbose && WizardMode) { 136 gclog_or_tty->print(" [Symbols=%d size=" SIZE_FORMAT "K] ", *processed, 137 (memory_total*HeapWordSize)/1024); 138 } 139 } 140 141 void SymbolTable::possibly_parallel_unlink(int* processed, int* removed) { 142 const int limit = the_table()->table_size(); 143 144 size_t memory_total = 0; 145 146 for (;;) { 147 // Grab next set of buckets to scan 148 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; 149 if (start_idx >= limit) { 150 // End of table 151 break; 152 } 153 154 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); 155 buckets_unlink(start_idx, end_idx, processed, removed, &memory_total); 156 } 157 Atomic::add(*processed, &_symbols_counted); 158 Atomic::add(*removed, &_symbols_removed); 159 // Exclude printing for normal PrintGCDetails because people parse 160 // this output. 161 if (PrintGCDetails && Verbose && WizardMode) { 162 gclog_or_tty->print(" [Symbols: scanned=%d removed=%d size=" SIZE_FORMAT "K] ", *processed, *removed, 163 (memory_total*HeapWordSize)/1024); 164 } 165 } 166 167 // Create a new table and using alternate hash code, populate the new table 168 // with the existing strings. Set flag to use the alternate hash code afterwards. 169 void SymbolTable::rehash_table() { 170 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 171 // This should never happen with -Xshare:dump but it might in testing mode. 172 if (DumpSharedSpaces) return; 173 // Create a new symbol table 174 SymbolTable* new_table = new SymbolTable(); 175 176 the_table()->move_to(new_table); 177 178 // Delete the table and buckets (entries are reused in new table). 179 delete _the_table; 180 // Don't check if we need rehashing until the table gets unbalanced again. 181 // Then rehash with a new global seed. 182 _needs_rehashing = false; 183 _the_table = new_table; 184 } 185 186 // Lookup a symbol in a bucket. 187 188 Symbol* SymbolTable::lookup(int index, const char* name, 189 int len, unsigned int hash) { 190 int count = 0; 191 for (HashtableEntry<Symbol*, mtSymbol>* e = bucket(index); e != NULL; e = e->next()) { 192 count++; // count all entries in this bucket, not just ones with same hash 193 if (e->hash() == hash) { 194 Symbol* sym = e->literal(); 195 if (sym->equals(name, len)) { 196 // something is referencing this symbol now. 197 sym->increment_refcount(); 198 return sym; 199 } 200 } 201 } 202 // If the bucket size is too deep check if this hash code is insufficient. 203 if (count >= BasicHashtable<mtSymbol>::rehash_count && !needs_rehashing()) { 204 _needs_rehashing = check_rehash_table(count); 205 } 206 return NULL; 207 } 208 209 // Pick hashing algorithm. 210 unsigned int SymbolTable::hash_symbol(const char* s, int len) { 211 return use_alternate_hashcode() ? 212 AltHashing::murmur3_32(seed(), (const jbyte*)s, len) : 213 java_lang_String::hash_code(s, len); 214 } 215 216 217 // We take care not to be blocking while holding the 218 // SymbolTable_lock. Otherwise, the system might deadlock, since the 219 // symboltable is used during compilation (VM_thread) The lock free 220 // synchronization is simplified by the fact that we do not delete 221 // entries in the symbol table during normal execution (only during 222 // safepoints). 223 224 Symbol* SymbolTable::lookup(const char* name, int len, TRAPS) { 225 unsigned int hashValue = hash_symbol(name, len); 226 int index = the_table()->hash_to_index(hashValue); 227 228 Symbol* s = the_table()->lookup(index, name, len, hashValue); 229 230 // Found 231 if (s != NULL) return s; 232 233 // Grab SymbolTable_lock first. 234 MutexLocker ml(SymbolTable_lock, THREAD); 235 236 // Otherwise, add to symbol to table 237 return the_table()->basic_add(index, (u1*)name, len, hashValue, true, CHECK_NULL); 238 } 239 240 Symbol* SymbolTable::lookup(const Symbol* sym, int begin, int end, TRAPS) { 241 char* buffer; 242 int index, len; 243 unsigned int hashValue; 244 char* name; 245 { 246 debug_only(No_Safepoint_Verifier nsv;) 247 248 name = (char*)sym->base() + begin; 249 len = end - begin; 250 hashValue = hash_symbol(name, len); 251 index = the_table()->hash_to_index(hashValue); 252 Symbol* s = the_table()->lookup(index, name, len, hashValue); 253 254 // Found 255 if (s != NULL) return s; 256 } 257 258 // Otherwise, add to symbol to table. Copy to a C string first. 259 char stack_buf[128]; 260 ResourceMark rm(THREAD); 261 if (len <= 128) { 262 buffer = stack_buf; 263 } else { 264 buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); 265 } 266 for (int i=0; i<len; i++) { 267 buffer[i] = name[i]; 268 } 269 // Make sure there is no safepoint in the code above since name can't move. 270 // We can't include the code in No_Safepoint_Verifier because of the 271 // ResourceMark. 272 273 // Grab SymbolTable_lock first. 274 MutexLocker ml(SymbolTable_lock, THREAD); 275 276 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, true, CHECK_NULL); 277 } 278 279 Symbol* SymbolTable::lookup_only(const char* name, int len, 280 unsigned int& hash) { 281 hash = hash_symbol(name, len); 282 int index = the_table()->hash_to_index(hash); 283 284 Symbol* s = the_table()->lookup(index, name, len, hash); 285 return s; 286 } 287 288 // Look up the address of the literal in the SymbolTable for this Symbol* 289 // Do not create any new symbols 290 // Do not increment the reference count to keep this alive 291 Symbol** SymbolTable::lookup_symbol_addr(Symbol* sym){ 292 unsigned int hash = hash_symbol((char*)sym->bytes(), sym->utf8_length()); 293 int index = the_table()->hash_to_index(hash); 294 295 for (HashtableEntry<Symbol*, mtSymbol>* e = the_table()->bucket(index); e != NULL; e = e->next()) { 296 if (e->hash() == hash) { 297 Symbol* literal_sym = e->literal(); 298 if (sym == literal_sym) { 299 return e->literal_addr(); 300 } 301 } 302 } 303 return NULL; 304 } 305 306 // Suggestion: Push unicode-based lookup all the way into the hashing 307 // and probing logic, so there is no need for convert_to_utf8 until 308 // an actual new Symbol* is created. 309 Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) { 310 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); 311 char stack_buf[128]; 312 if (utf8_length < (int) sizeof(stack_buf)) { 313 char* chars = stack_buf; 314 UNICODE::convert_to_utf8(name, utf16_length, chars); 315 return lookup(chars, utf8_length, THREAD); 316 } else { 317 ResourceMark rm(THREAD); 318 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; 319 UNICODE::convert_to_utf8(name, utf16_length, chars); 320 return lookup(chars, utf8_length, THREAD); 321 } 322 } 323 324 Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length, 325 unsigned int& hash) { 326 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); 327 char stack_buf[128]; 328 if (utf8_length < (int) sizeof(stack_buf)) { 329 char* chars = stack_buf; 330 UNICODE::convert_to_utf8(name, utf16_length, chars); 331 return lookup_only(chars, utf8_length, hash); 332 } else { 333 ResourceMark rm; 334 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; 335 UNICODE::convert_to_utf8(name, utf16_length, chars); 336 return lookup_only(chars, utf8_length, hash); 337 } 338 } 339 340 void SymbolTable::add(ClassLoaderData* loader_data, constantPoolHandle cp, 341 int names_count, 342 const char** names, int* lengths, int* cp_indices, 343 unsigned int* hashValues, TRAPS) { 344 // Grab SymbolTable_lock first. 345 MutexLocker ml(SymbolTable_lock, THREAD); 346 347 SymbolTable* table = the_table(); 348 bool added = table->basic_add(loader_data, cp, names_count, names, lengths, 349 cp_indices, hashValues, CHECK); 350 if (!added) { 351 // do it the hard way 352 for (int i=0; i<names_count; i++) { 353 int index = table->hash_to_index(hashValues[i]); 354 bool c_heap = !loader_data->is_the_null_class_loader_data(); 355 Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i], hashValues[i], c_heap, CHECK); 356 cp->symbol_at_put(cp_indices[i], sym); 357 } 358 } 359 } 360 361 Symbol* SymbolTable::new_permanent_symbol(const char* name, TRAPS) { 362 unsigned int hash; 363 Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash); 364 if (result != NULL) { 365 return result; 366 } 367 // Grab SymbolTable_lock first. 368 MutexLocker ml(SymbolTable_lock, THREAD); 369 370 SymbolTable* table = the_table(); 371 int index = table->hash_to_index(hash); 372 return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD); 373 } 374 375 Symbol* SymbolTable::basic_add(int index_arg, u1 *name, int len, 376 unsigned int hashValue_arg, bool c_heap, TRAPS) { 377 assert(!Universe::heap()->is_in_reserved(name), 378 "proposed name of symbol must be stable"); 379 380 // Don't allow symbols to be created which cannot fit in a Symbol*. 381 if (len > Symbol::max_length()) { 382 THROW_MSG_0(vmSymbols::java_lang_InternalError(), 383 "name is too long to represent"); 384 } 385 386 // Cannot hit a safepoint in this function because the "this" pointer can move. 387 No_Safepoint_Verifier nsv; 388 389 // Check if the symbol table has been rehashed, if so, need to recalculate 390 // the hash value and index. 391 unsigned int hashValue; 392 int index; 393 if (use_alternate_hashcode()) { 394 hashValue = hash_symbol((const char*)name, len); 395 index = hash_to_index(hashValue); 396 } else { 397 hashValue = hashValue_arg; 398 index = index_arg; 399 } 400 401 // Since look-up was done lock-free, we need to check if another 402 // thread beat us in the race to insert the symbol. 403 Symbol* test = lookup(index, (char*)name, len, hashValue); 404 if (test != NULL) { 405 // A race occurred and another thread introduced the symbol. 406 assert(test->refcount() != 0, "lookup should have incremented the count"); 407 return test; 408 } 409 410 // Create a new symbol. 411 Symbol* sym = allocate_symbol(name, len, c_heap, CHECK_NULL); 412 assert(sym->equals((char*)name, len), "symbol must be properly initialized"); 413 414 HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym); 415 add_entry(index, entry); 416 return sym; 417 } 418 419 // This version of basic_add adds symbols in batch from the constant pool 420 // parsing. 421 bool SymbolTable::basic_add(ClassLoaderData* loader_data, constantPoolHandle cp, 422 int names_count, 423 const char** names, int* lengths, 424 int* cp_indices, unsigned int* hashValues, 425 TRAPS) { 426 427 // Check symbol names are not too long. If any are too long, don't add any. 428 for (int i = 0; i< names_count; i++) { 429 if (lengths[i] > Symbol::max_length()) { 430 THROW_MSG_0(vmSymbols::java_lang_InternalError(), 431 "name is too long to represent"); 432 } 433 } 434 435 // Cannot hit a safepoint in this function because the "this" pointer can move. 436 No_Safepoint_Verifier nsv; 437 438 for (int i=0; i<names_count; i++) { 439 // Check if the symbol table has been rehashed, if so, need to recalculate 440 // the hash value. 441 unsigned int hashValue; 442 if (use_alternate_hashcode()) { 443 hashValue = hash_symbol(names[i], lengths[i]); 444 } else { 445 hashValue = hashValues[i]; 446 } 447 // Since look-up was done lock-free, we need to check if another 448 // thread beat us in the race to insert the symbol. 449 int index = hash_to_index(hashValue); 450 Symbol* test = lookup(index, names[i], lengths[i], hashValue); 451 if (test != NULL) { 452 // A race occurred and another thread introduced the symbol, this one 453 // will be dropped and collected. Use test instead. 454 cp->symbol_at_put(cp_indices[i], test); 455 assert(test->refcount() != 0, "lookup should have incremented the count"); 456 } else { 457 // Create a new symbol. The null class loader is never unloaded so these 458 // are allocated specially in a permanent arena. 459 bool c_heap = !loader_data->is_the_null_class_loader_data(); 460 Symbol* sym = allocate_symbol((const u1*)names[i], lengths[i], c_heap, CHECK_(false)); 461 assert(sym->equals(names[i], lengths[i]), "symbol must be properly initialized"); // why wouldn't it be??? 462 HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym); 463 add_entry(index, entry); 464 cp->symbol_at_put(cp_indices[i], sym); 465 } 466 } 467 return true; 468 } 469 470 471 void SymbolTable::verify() { 472 for (int i = 0; i < the_table()->table_size(); ++i) { 473 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 474 for ( ; p != NULL; p = p->next()) { 475 Symbol* s = (Symbol*)(p->literal()); 476 guarantee(s != NULL, "symbol is NULL"); 477 unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length()); 478 guarantee(p->hash() == h, "broken hash in symbol table entry"); 479 guarantee(the_table()->hash_to_index(h) == i, 480 "wrong index in symbol table"); 481 } 482 } 483 } 484 485 void SymbolTable::dump(outputStream* st) { 486 the_table()->dump_table(st, "SymbolTable"); 487 } 488 489 490 //--------------------------------------------------------------------------- 491 // Non-product code 492 493 #ifndef PRODUCT 494 495 void SymbolTable::print_histogram() { 496 MutexLocker ml(SymbolTable_lock); 497 const int results_length = 100; 498 int results[results_length]; 499 int i,j; 500 501 // initialize results to zero 502 for (j = 0; j < results_length; j++) { 503 results[j] = 0; 504 } 505 506 int total = 0; 507 int max_symbols = 0; 508 int out_of_range = 0; 509 int memory_total = 0; 510 int count = 0; 511 for (i = 0; i < the_table()->table_size(); i++) { 512 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 513 for ( ; p != NULL; p = p->next()) { 514 memory_total += p->literal()->size(); 515 count++; 516 int counter = p->literal()->utf8_length(); 517 total += counter; 518 if (counter < results_length) { 519 results[counter]++; 520 } else { 521 out_of_range++; 522 } 523 max_symbols = MAX2(max_symbols, counter); 524 } 525 } 526 tty->print_cr("Symbol Table:"); 527 tty->print_cr("Total number of symbols %5d", count); 528 tty->print_cr("Total size in memory %5dK", 529 (memory_total*HeapWordSize)/1024); 530 tty->print_cr("Total counted %5d", _symbols_counted); 531 tty->print_cr("Total removed %5d", _symbols_removed); 532 if (_symbols_counted > 0) { 533 tty->print_cr("Percent removed %3.2f", 534 ((float)_symbols_removed/(float)_symbols_counted)* 100); 535 } 536 tty->print_cr("Reference counts %5d", Symbol::_total_count); 537 tty->print_cr("Symbol arena size %5d used %5d", 538 arena()->size_in_bytes(), arena()->used()); 539 tty->print_cr("Histogram of symbol length:"); 540 tty->print_cr("%8s %5d", "Total ", total); 541 tty->print_cr("%8s %5d", "Maximum", max_symbols); 542 tty->print_cr("%8s %3.2f", "Average", 543 ((float) total / (float) the_table()->table_size())); 544 tty->print_cr("%s", "Histogram:"); 545 tty->print_cr(" %s %29s", "Length", "Number chains that length"); 546 for (i = 0; i < results_length; i++) { 547 if (results[i] > 0) { 548 tty->print_cr("%6d %10d", i, results[i]); 549 } 550 } 551 if (Verbose) { 552 int line_length = 70; 553 tty->print_cr("%s %30s", " Length", "Number chains that length"); 554 for (i = 0; i < results_length; i++) { 555 if (results[i] > 0) { 556 tty->print("%4d", i); 557 for (j = 0; (j < results[i]) && (j < line_length); j++) { 558 tty->print("%1s", "*"); 559 } 560 if (j == line_length) { 561 tty->print("%1s", "+"); 562 } 563 tty->cr(); 564 } 565 } 566 } 567 tty->print_cr(" %s %d: %d\n", "Number chains longer than", 568 results_length, out_of_range); 569 } 570 571 void SymbolTable::print() { 572 for (int i = 0; i < the_table()->table_size(); ++i) { 573 HashtableEntry<Symbol*, mtSymbol>** p = the_table()->bucket_addr(i); 574 HashtableEntry<Symbol*, mtSymbol>* entry = the_table()->bucket(i); 575 if (entry != NULL) { 576 while (entry != NULL) { 577 tty->print(PTR_FORMAT " ", entry->literal()); 578 entry->literal()->print(); 579 tty->print(" %d", entry->literal()->refcount()); 580 p = entry->next_addr(); 581 entry = (HashtableEntry<Symbol*, mtSymbol>*)HashtableEntry<Symbol*, mtSymbol>::make_ptr(*p); 582 } 583 tty->cr(); 584 } 585 } 586 } 587 #endif // PRODUCT