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