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