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