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