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 #include "precompiled.hpp" 26 #include "classfile/altHashing.hpp" 27 #include "classfile/compactHashtable.inline.hpp" 28 #include "classfile/javaClasses.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "gc/shared/collectedHeap.inline.hpp" 32 #include "memory/allocation.inline.hpp" 33 #include "memory/filemap.hpp" 34 #include "memory/metaspaceClosure.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "oops/oop.inline.hpp" 37 #include "runtime/atomic.hpp" 38 #include "runtime/mutexLocker.hpp" 39 #include "runtime/safepointVerifiers.hpp" 40 #include "services/diagnosticCommand.hpp" 41 #include "utilities/hashtable.inline.hpp" 42 43 // -------------------------------------------------------------------------- 44 // the number of buckets a thread claims 45 const int ClaimChunkSize = 32; 46 47 SymbolTable* SymbolTable::_the_table = NULL; 48 // Static arena for symbols that are not deallocated 49 Arena* SymbolTable::_arena = NULL; 50 bool SymbolTable::_needs_rehashing = false; 51 bool SymbolTable::_lookup_shared_first = false; 52 53 CompactHashtable<Symbol*, char> SymbolTable::_shared_table; 54 55 Symbol* SymbolTable::allocate_symbol(const u1* name, int len, bool c_heap, TRAPS) { 56 assert (len <= Symbol::max_length(), "should be checked by caller"); 57 58 Symbol* sym; 59 60 if (DumpSharedSpaces) { 61 c_heap = false; 62 } 63 if (c_heap) { 64 // refcount starts as 1 65 sym = new (len, THREAD) Symbol(name, len, 1); 66 assert(sym != NULL, "new should call vm_exit_out_of_memory if C_HEAP is exhausted"); 67 } else { 68 // Allocate to global arena 69 sym = new (len, arena(), THREAD) Symbol(name, len, PERM_REFCOUNT); 70 } 71 return sym; 72 } 73 74 void SymbolTable::initialize_symbols(int arena_alloc_size) { 75 // Initialize the arena for global symbols, size passed in depends on CDS. 76 if (arena_alloc_size == 0) { 77 _arena = new (mtSymbol) Arena(mtSymbol); 78 } else { 79 _arena = new (mtSymbol) Arena(mtSymbol, arena_alloc_size); 80 } 81 } 82 83 // Call function for all symbols in the symbol table. 84 void SymbolTable::symbols_do(SymbolClosure *cl) { 85 // all symbols from shared table 86 _shared_table.symbols_do(cl); 87 88 // all symbols from the dynamic table 89 const int n = the_table()->table_size(); 90 for (int i = 0; i < n; i++) { 91 for (HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 92 p != NULL; 93 p = p->next()) { 94 cl->do_symbol(p->literal_addr()); 95 } 96 } 97 } 98 99 void SymbolTable::metaspace_pointers_do(MetaspaceClosure* it) { 100 assert(DumpSharedSpaces, "called only during dump time"); 101 const int n = the_table()->table_size(); 102 for (int i = 0; i < n; i++) { 103 for (HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 104 p != NULL; 105 p = p->next()) { 106 it->push(p->literal_addr()); 107 } 108 } 109 } 110 111 int SymbolTable::_symbols_removed = 0; 112 int SymbolTable::_symbols_counted = 0; 113 volatile int SymbolTable::_parallel_claimed_idx = 0; 114 115 void SymbolTable::buckets_unlink(int start_idx, int end_idx, BucketUnlinkContext* context) { 116 for (int i = start_idx; i < end_idx; ++i) { 117 HashtableEntry<Symbol*, mtSymbol>** p = the_table()->bucket_addr(i); 118 HashtableEntry<Symbol*, mtSymbol>* entry = the_table()->bucket(i); 119 while (entry != NULL) { 120 // Shared entries are normally at the end of the bucket and if we run into 121 // a shared entry, then there is nothing more to remove. However, if we 122 // have rehashed the table, then the shared entries are no longer at the 123 // end of the bucket. 124 if (entry->is_shared() && !use_alternate_hashcode()) { 125 break; 126 } 127 Symbol* s = entry->literal(); 128 context->_num_processed++; 129 assert(s != NULL, "just checking"); 130 // If reference count is zero, remove. 131 if (s->refcount() == 0) { 132 assert(!entry->is_shared(), "shared entries should be kept live"); 133 delete s; 134 *p = entry->next(); 135 context->free_entry(entry); 136 } else { 137 p = entry->next_addr(); 138 } 139 // get next entry 140 entry = (HashtableEntry<Symbol*, mtSymbol>*)HashtableEntry<Symbol*, mtSymbol>::make_ptr(*p); 141 } 142 } 143 } 144 145 // Remove unreferenced symbols from the symbol table 146 // This is done late during GC. 147 void SymbolTable::unlink(int* processed, int* removed) { 148 BucketUnlinkContext context; 149 buckets_unlink(0, the_table()->table_size(), &context); 150 _the_table->bulk_free_entries(&context); 151 *processed = context._num_processed; 152 *removed = context._num_removed; 153 154 _symbols_removed = context._num_removed; 155 _symbols_counted = context._num_processed; 156 } 157 158 void SymbolTable::possibly_parallel_unlink(int* processed, int* removed) { 159 const int limit = the_table()->table_size(); 160 161 BucketUnlinkContext context; 162 for (;;) { 163 // Grab next set of buckets to scan 164 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; 165 if (start_idx >= limit) { 166 // End of table 167 break; 168 } 169 170 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); 171 buckets_unlink(start_idx, end_idx, &context); 172 } 173 174 _the_table->bulk_free_entries(&context); 175 *processed = context._num_processed; 176 *removed = context._num_removed; 177 178 Atomic::add(context._num_processed, &_symbols_counted); 179 Atomic::add(context._num_removed, &_symbols_removed); 180 } 181 182 // Create a new table and using alternate hash code, populate the new table 183 // with the existing strings. Set flag to use the alternate hash code afterwards. 184 void SymbolTable::rehash_table() { 185 if (DumpSharedSpaces) { 186 tty->print_cr("Warning: rehash_table should not be called while dumping archive"); 187 return; 188 } 189 190 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 191 // This should never happen with -Xshare:dump but it might in testing mode. 192 if (DumpSharedSpaces) return; 193 // Create a new symbol table 194 SymbolTable* new_table = new SymbolTable(); 195 196 the_table()->move_to(new_table); 197 198 // Delete the table and buckets (entries are reused in new table). 199 delete _the_table; 200 // Don't check if we need rehashing until the table gets unbalanced again. 201 // Then rehash with a new global seed. 202 _needs_rehashing = false; 203 _the_table = new_table; 204 } 205 206 // Lookup a symbol in a bucket. 207 208 Symbol* SymbolTable::lookup_dynamic(int index, const char* name, 209 int len, unsigned int hash) { 210 int count = 0; 211 for (HashtableEntry<Symbol*, mtSymbol>* e = bucket(index); e != NULL; e = e->next()) { 212 count++; // count all entries in this bucket, not just ones with same hash 213 if (e->hash() == hash) { 214 Symbol* sym = e->literal(); 215 if (sym->equals(name, len)) { 216 // something is referencing this symbol now. 217 sym->increment_refcount(); 218 return sym; 219 } 220 } 221 } 222 // If the bucket size is too deep check if this hash code is insufficient. 223 if (count >= rehash_count && !needs_rehashing()) { 224 _needs_rehashing = check_rehash_table(count); 225 } 226 return NULL; 227 } 228 229 Symbol* SymbolTable::lookup_shared(const char* name, 230 int len, unsigned int hash) { 231 if (use_alternate_hashcode()) { 232 // hash_code parameter may use alternate hashing algorithm but the shared table 233 // always uses the same original hash code. 234 hash = hash_shared_symbol(name, len); 235 } 236 return _shared_table.lookup(name, hash, len); 237 } 238 239 Symbol* SymbolTable::lookup(int index, const char* name, 240 int len, unsigned int hash) { 241 Symbol* sym; 242 if (_lookup_shared_first) { 243 sym = lookup_shared(name, len, hash); 244 if (sym != NULL) { 245 return sym; 246 } 247 _lookup_shared_first = false; 248 return lookup_dynamic(index, name, len, hash); 249 } else { 250 sym = lookup_dynamic(index, name, len, hash); 251 if (sym != NULL) { 252 return sym; 253 } 254 sym = lookup_shared(name, len, hash); 255 if (sym != NULL) { 256 _lookup_shared_first = true; 257 } 258 return sym; 259 } 260 } 261 262 u4 SymbolTable::encode_shared(Symbol* sym) { 263 assert(DumpSharedSpaces, "called only during dump time"); 264 uintx base_address = uintx(MetaspaceShared::shared_rs()->base()); 265 uintx offset = uintx(sym) - base_address; 266 assert(offset < 0x7fffffff, "sanity"); 267 return u4(offset); 268 } 269 270 Symbol* SymbolTable::decode_shared(u4 offset) { 271 assert(!DumpSharedSpaces, "called only during runtime"); 272 uintx base_address = _shared_table.base_address(); 273 Symbol* sym = (Symbol*)(base_address + offset); 274 275 #ifndef PRODUCT 276 const char* s = (const char*)sym->bytes(); 277 int len = sym->utf8_length(); 278 unsigned int hash = hash_symbol(s, len); 279 assert(sym == lookup_shared(s, len, hash), "must be shared symbol"); 280 #endif 281 282 return sym; 283 } 284 285 // Pick hashing algorithm. 286 unsigned int SymbolTable::hash_symbol(const char* s, int len) { 287 return use_alternate_hashcode() ? 288 AltHashing::murmur3_32(seed(), (const jbyte*)s, len) : 289 java_lang_String::hash_code((const jbyte*)s, len); 290 } 291 292 unsigned int SymbolTable::hash_shared_symbol(const char* s, int len) { 293 return java_lang_String::hash_code((const jbyte*)s, len); 294 } 295 296 297 // We take care not to be blocking while holding the 298 // SymbolTable_lock. Otherwise, the system might deadlock, since the 299 // symboltable is used during compilation (VM_thread) The lock free 300 // synchronization is simplified by the fact that we do not delete 301 // entries in the symbol table during normal execution (only during 302 // safepoints). 303 304 Symbol* SymbolTable::lookup(const char* name, int len, TRAPS) { 305 unsigned int hashValue = hash_symbol(name, len); 306 int index = the_table()->hash_to_index(hashValue); 307 308 Symbol* s = the_table()->lookup(index, name, len, hashValue); 309 310 // Found 311 if (s != NULL) return s; 312 313 // Grab SymbolTable_lock first. 314 MutexLocker ml(SymbolTable_lock, THREAD); 315 316 // Otherwise, add to symbol to table 317 return the_table()->basic_add(index, (u1*)name, len, hashValue, true, THREAD); 318 } 319 320 Symbol* SymbolTable::lookup(const Symbol* sym, int begin, int end, TRAPS) { 321 char* buffer; 322 int index, len; 323 unsigned int hashValue; 324 char* name; 325 { 326 debug_only(NoSafepointVerifier nsv;) 327 328 name = (char*)sym->base() + begin; 329 len = end - begin; 330 hashValue = hash_symbol(name, len); 331 index = the_table()->hash_to_index(hashValue); 332 Symbol* s = the_table()->lookup(index, name, len, hashValue); 333 334 // Found 335 if (s != NULL) return s; 336 } 337 338 // Otherwise, add to symbol to table. Copy to a C string first. 339 char stack_buf[128]; 340 ResourceMark rm(THREAD); 341 if (len <= 128) { 342 buffer = stack_buf; 343 } else { 344 buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); 345 } 346 for (int i=0; i<len; i++) { 347 buffer[i] = name[i]; 348 } 349 // Make sure there is no safepoint in the code above since name can't move. 350 // We can't include the code in NoSafepointVerifier because of the 351 // ResourceMark. 352 353 // Grab SymbolTable_lock first. 354 MutexLocker ml(SymbolTable_lock, THREAD); 355 356 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, true, THREAD); 357 } 358 359 Symbol* SymbolTable::lookup_only(const char* name, int len, 360 unsigned int& hash) { 361 hash = hash_symbol(name, len); 362 int index = the_table()->hash_to_index(hash); 363 364 Symbol* s = the_table()->lookup(index, name, len, hash); 365 return s; 366 } 367 368 // Look up the address of the literal in the SymbolTable for this Symbol* 369 // Do not create any new symbols 370 // Do not increment the reference count to keep this alive 371 Symbol** SymbolTable::lookup_symbol_addr(Symbol* sym){ 372 unsigned int hash = hash_symbol((char*)sym->bytes(), sym->utf8_length()); 373 int index = the_table()->hash_to_index(hash); 374 375 for (HashtableEntry<Symbol*, mtSymbol>* e = the_table()->bucket(index); e != NULL; e = e->next()) { 376 if (e->hash() == hash) { 377 Symbol* literal_sym = e->literal(); 378 if (sym == literal_sym) { 379 return e->literal_addr(); 380 } 381 } 382 } 383 return NULL; 384 } 385 386 // Suggestion: Push unicode-based lookup all the way into the hashing 387 // and probing logic, so there is no need for convert_to_utf8 until 388 // an actual new Symbol* is created. 389 Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) { 390 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); 391 char stack_buf[128]; 392 if (utf8_length < (int) sizeof(stack_buf)) { 393 char* chars = stack_buf; 394 UNICODE::convert_to_utf8(name, utf16_length, chars); 395 return lookup(chars, utf8_length, THREAD); 396 } else { 397 ResourceMark rm(THREAD); 398 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; 399 UNICODE::convert_to_utf8(name, utf16_length, chars); 400 return lookup(chars, utf8_length, THREAD); 401 } 402 } 403 404 Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length, 405 unsigned int& hash) { 406 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); 407 char stack_buf[128]; 408 if (utf8_length < (int) sizeof(stack_buf)) { 409 char* chars = stack_buf; 410 UNICODE::convert_to_utf8(name, utf16_length, chars); 411 return lookup_only(chars, utf8_length, hash); 412 } else { 413 ResourceMark rm; 414 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; 415 UNICODE::convert_to_utf8(name, utf16_length, chars); 416 return lookup_only(chars, utf8_length, hash); 417 } 418 } 419 420 void SymbolTable::add(ClassLoaderData* loader_data, const constantPoolHandle& cp, 421 int names_count, 422 const char** names, int* lengths, int* cp_indices, 423 unsigned int* hashValues, TRAPS) { 424 // Grab SymbolTable_lock first. 425 MutexLocker ml(SymbolTable_lock, THREAD); 426 427 SymbolTable* table = the_table(); 428 bool added = table->basic_add(loader_data, cp, names_count, names, lengths, 429 cp_indices, hashValues, CHECK); 430 if (!added) { 431 // do it the hard way 432 for (int i=0; i<names_count; i++) { 433 int index = table->hash_to_index(hashValues[i]); 434 bool c_heap = !loader_data->is_the_null_class_loader_data(); 435 Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i], hashValues[i], c_heap, CHECK); 436 cp->symbol_at_put(cp_indices[i], sym); 437 } 438 } 439 } 440 441 Symbol* SymbolTable::new_permanent_symbol(const char* name, TRAPS) { 442 unsigned int hash; 443 Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash); 444 if (result != NULL) { 445 return result; 446 } 447 // Grab SymbolTable_lock first. 448 MutexLocker ml(SymbolTable_lock, THREAD); 449 450 SymbolTable* table = the_table(); 451 int index = table->hash_to_index(hash); 452 return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD); 453 } 454 455 TableStatistics SymbolTable::get_table_statistics() { 456 return the_table()->statistics_calculate(); 457 } 458 459 Symbol* SymbolTable::basic_add(int index_arg, u1 *name, int len, 460 unsigned int hashValue_arg, bool c_heap, TRAPS) { 461 assert(!Universe::heap()->is_in_reserved(name), 462 "proposed name of symbol must be stable"); 463 464 // Don't allow symbols to be created which cannot fit in a Symbol*. 465 if (len > Symbol::max_length()) { 466 THROW_MSG_0(vmSymbols::java_lang_InternalError(), 467 "name is too long to represent"); 468 } 469 470 // Cannot hit a safepoint in this function because the "this" pointer can move. 471 NoSafepointVerifier nsv; 472 473 // Check if the symbol table has been rehashed, if so, need to recalculate 474 // the hash value and index. 475 unsigned int hashValue; 476 int index; 477 if (use_alternate_hashcode()) { 478 hashValue = hash_symbol((const char*)name, len); 479 index = hash_to_index(hashValue); 480 } else { 481 hashValue = hashValue_arg; 482 index = index_arg; 483 } 484 485 // Since look-up was done lock-free, we need to check if another 486 // thread beat us in the race to insert the symbol. 487 Symbol* test = lookup(index, (char*)name, len, hashValue); 488 if (test != NULL) { 489 // A race occurred and another thread introduced the symbol. 490 assert(test->refcount() != 0, "lookup should have incremented the count"); 491 return test; 492 } 493 494 // Create a new symbol. 495 Symbol* sym = allocate_symbol(name, len, c_heap, CHECK_NULL); 496 assert(sym->equals((char*)name, len), "symbol must be properly initialized"); 497 498 HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym); 499 add_entry(index, entry); 500 return sym; 501 } 502 503 // This version of basic_add adds symbols in batch from the constant pool 504 // parsing. 505 bool SymbolTable::basic_add(ClassLoaderData* loader_data, const constantPoolHandle& cp, 506 int names_count, 507 const char** names, int* lengths, 508 int* cp_indices, unsigned int* hashValues, 509 TRAPS) { 510 511 // Check symbol names are not too long. If any are too long, don't add any. 512 for (int i = 0; i< names_count; i++) { 513 if (lengths[i] > Symbol::max_length()) { 514 THROW_MSG_0(vmSymbols::java_lang_InternalError(), 515 "name is too long to represent"); 516 } 517 } 518 519 // Cannot hit a safepoint in this function because the "this" pointer can move. 520 NoSafepointVerifier nsv; 521 522 for (int i=0; i<names_count; i++) { 523 // Check if the symbol table has been rehashed, if so, need to recalculate 524 // the hash value. 525 unsigned int hashValue; 526 if (use_alternate_hashcode()) { 527 hashValue = hash_symbol(names[i], lengths[i]); 528 } else { 529 hashValue = hashValues[i]; 530 } 531 // Since look-up was done lock-free, we need to check if another 532 // thread beat us in the race to insert the symbol. 533 int index = hash_to_index(hashValue); 534 Symbol* test = lookup(index, names[i], lengths[i], hashValue); 535 if (test != NULL) { 536 // A race occurred and another thread introduced the symbol, this one 537 // will be dropped and collected. Use test instead. 538 cp->symbol_at_put(cp_indices[i], test); 539 assert(test->refcount() != 0, "lookup should have incremented the count"); 540 } else { 541 // Create a new symbol. The null class loader is never unloaded so these 542 // are allocated specially in a permanent arena. 543 bool c_heap = !loader_data->is_the_null_class_loader_data(); 544 Symbol* sym = allocate_symbol((const u1*)names[i], lengths[i], c_heap, CHECK_(false)); 545 assert(sym->equals(names[i], lengths[i]), "symbol must be properly initialized"); // why wouldn't it be??? 546 HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym); 547 add_entry(index, entry); 548 cp->symbol_at_put(cp_indices[i], sym); 549 } 550 } 551 return true; 552 } 553 554 555 void SymbolTable::verify() { 556 for (int i = 0; i < the_table()->table_size(); ++i) { 557 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 558 for ( ; p != NULL; p = p->next()) { 559 Symbol* s = (Symbol*)(p->literal()); 560 guarantee(s != NULL, "symbol is NULL"); 561 unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length()); 562 guarantee(p->hash() == h, "broken hash in symbol table entry"); 563 guarantee(the_table()->hash_to_index(h) == i, 564 "wrong index in symbol table"); 565 } 566 } 567 } 568 569 void SymbolTable::dump(outputStream* st, bool verbose) { 570 if (!verbose) { 571 the_table()->print_table_statistics(st, "SymbolTable"); 572 } else { 573 st->print_cr("VERSION: 1.0"); 574 for (int i = 0; i < the_table()->table_size(); ++i) { 575 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 576 for ( ; p != NULL; p = p->next()) { 577 Symbol* s = (Symbol*)(p->literal()); 578 const char* utf8_string = (const char*)s->bytes(); 579 int utf8_length = s->utf8_length(); 580 st->print("%d %d: ", utf8_length, s->refcount()); 581 HashtableTextDump::put_utf8(st, utf8_string, utf8_length); 582 st->cr(); 583 } 584 } 585 } 586 } 587 588 void SymbolTable::write_to_archive() { 589 #if INCLUDE_CDS 590 _shared_table.reset(); 591 592 int num_buckets = the_table()->number_of_entries() / 593 SharedSymbolTableBucketSize; 594 CompactSymbolTableWriter writer(num_buckets, 595 &MetaspaceShared::stats()->symbol); 596 for (int i = 0; i < the_table()->table_size(); ++i) { 597 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 598 for ( ; p != NULL; p = p->next()) { 599 Symbol* s = (Symbol*)(p->literal()); 600 unsigned int fixed_hash = hash_shared_symbol((char*)s->bytes(), s->utf8_length()); 601 assert(fixed_hash == p->hash(), "must not rehash during dumping"); 602 writer.add(fixed_hash, s); 603 } 604 } 605 606 writer.dump(&_shared_table); 607 608 // Verify table is correct 609 Symbol* sym = vmSymbols::java_lang_Object(); 610 const char* name = (const char*)sym->bytes(); 611 int len = sym->utf8_length(); 612 unsigned int hash = hash_symbol(name, len); 613 assert(sym == _shared_table.lookup(name, hash, len), "sanity"); 614 #endif 615 } 616 617 void SymbolTable::serialize(SerializeClosure* soc) { 618 #if INCLUDE_CDS 619 _shared_table.set_type(CompactHashtable<Symbol*, char>::_symbol_table); 620 _shared_table.serialize(soc); 621 622 if (soc->writing()) { 623 // Sanity. Make sure we don't use the shared table at dump time 624 _shared_table.reset(); 625 } 626 #endif 627 } 628 629 //--------------------------------------------------------------------------- 630 // Non-product code 631 632 #ifndef PRODUCT 633 634 void SymbolTable::print_histogram() { 635 MutexLocker ml(SymbolTable_lock); 636 const int results_length = 100; 637 int counts[results_length]; 638 int sizes[results_length]; 639 int i,j; 640 641 // initialize results to zero 642 for (j = 0; j < results_length; j++) { 643 counts[j] = 0; 644 sizes[j] = 0; 645 } 646 647 int total_size = 0; 648 int total_count = 0; 649 int total_length = 0; 650 int max_length = 0; 651 int out_of_range_count = 0; 652 int out_of_range_size = 0; 653 for (i = 0; i < the_table()->table_size(); i++) { 654 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i); 655 for ( ; p != NULL; p = p->next()) { 656 int size = p->literal()->size(); 657 int len = p->literal()->utf8_length(); 658 if (len < results_length) { 659 counts[len]++; 660 sizes[len] += size; 661 } else { 662 out_of_range_count++; 663 out_of_range_size += size; 664 } 665 total_count++; 666 total_size += size; 667 total_length += len; 668 max_length = MAX2(max_length, len); 669 } 670 } 671 tty->print_cr("Symbol Table Histogram:"); 672 tty->print_cr(" Total number of symbols %7d", total_count); 673 tty->print_cr(" Total size in memory %7dK", 674 (total_size*wordSize)/1024); 675 tty->print_cr(" Total counted %7d", _symbols_counted); 676 tty->print_cr(" Total removed %7d", _symbols_removed); 677 if (_symbols_counted > 0) { 678 tty->print_cr(" Percent removed %3.2f", 679 ((float)_symbols_removed/(float)_symbols_counted)* 100); 680 } 681 tty->print_cr(" Reference counts %7d", Symbol::_total_count); 682 tty->print_cr(" Symbol arena used " SIZE_FORMAT_W(7) "K", arena()->used()/1024); 683 tty->print_cr(" Symbol arena size " SIZE_FORMAT_W(7) "K", arena()->size_in_bytes()/1024); 684 tty->print_cr(" Total symbol length %7d", total_length); 685 tty->print_cr(" Maximum symbol length %7d", max_length); 686 tty->print_cr(" Average symbol length %7.2f", ((float) total_length / (float) total_count)); 687 tty->print_cr(" Symbol length histogram:"); 688 tty->print_cr(" %6s %10s %10s", "Length", "#Symbols", "Size"); 689 for (i = 0; i < results_length; i++) { 690 if (counts[i] > 0) { 691 tty->print_cr(" %6d %10d %10dK", i, counts[i], (sizes[i]*wordSize)/1024); 692 } 693 } 694 tty->print_cr(" >=%6d %10d %10dK\n", results_length, 695 out_of_range_count, (out_of_range_size*wordSize)/1024); 696 } 697 698 void SymbolTable::print() { 699 for (int i = 0; i < the_table()->table_size(); ++i) { 700 HashtableEntry<Symbol*, mtSymbol>** p = the_table()->bucket_addr(i); 701 HashtableEntry<Symbol*, mtSymbol>* entry = the_table()->bucket(i); 702 if (entry != NULL) { 703 while (entry != NULL) { 704 tty->print(PTR_FORMAT " ", p2i(entry->literal())); 705 entry->literal()->print(); 706 tty->print(" %d", entry->literal()->refcount()); 707 p = entry->next_addr(); 708 entry = (HashtableEntry<Symbol*, mtSymbol>*)HashtableEntry<Symbol*, mtSymbol>::make_ptr(*p); 709 } 710 tty->cr(); 711 } 712 } 713 } 714 #endif // PRODUCT 715 716 717 // Utility for dumping symbols 718 SymboltableDCmd::SymboltableDCmd(outputStream* output, bool heap) : 719 DCmdWithParser(output, heap), 720 _verbose("-verbose", "Dump the content of each symbol in the table", 721 "BOOLEAN", false, "false") { 722 _dcmdparser.add_dcmd_option(&_verbose); 723 } 724 725 void SymboltableDCmd::execute(DCmdSource source, TRAPS) { 726 VM_DumpHashtable dumper(output(), VM_DumpHashtable::DumpSymbols, 727 _verbose.value()); 728 VMThread::execute(&dumper); 729 } 730 731 int SymboltableDCmd::num_arguments() { 732 ResourceMark rm; 733 SymboltableDCmd* dcmd = new SymboltableDCmd(NULL, false); 734 if (dcmd != NULL) { 735 DCmdMark mark(dcmd); 736 return dcmd->_dcmdparser.num_arguments(); 737 } else { 738 return 0; 739 } 740 }