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