< prev index next >

src/hotspot/share/classfile/symbolTable.cpp

Print this page




  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 
 194   // Create a new symbol table
 195   SymbolTable* new_table = new SymbolTable();
 196 
 197   the_table()->move_to(new_table);
 198 
 199   // Delete the table and buckets (entries are reused in new table).
 200   delete _the_table;
 201   // Don't check if we need rehashing until the table gets unbalanced again.
 202   // Then rehash with a new global seed.
 203   _needs_rehashing = false;
 204   _the_table = new_table;
 205 }
 206 
 207 // Lookup a symbol in a bucket.
 208 
 209 Symbol* SymbolTable::lookup_dynamic(int index, const char* name,
 210                                     int len, unsigned int hash) {
 211   int count = 0;
 212   for (HashtableEntry<Symbol*, mtSymbol>* e = bucket(index); e != NULL; e = e->next()) {
 213     count++;  // count all entries in this bucket, not just ones with same hash
 214     if (e->hash() == hash) {
 215       Symbol* sym = e->literal();
 216       // Skip checking already dead symbols in the bucket.
 217       if (sym->refcount() == 0) {
 218         count--;   // Don't count this symbol towards rehashing.
 219       } else if (sym->equals(name, len)) {
 220         if (sym->try_increment_refcount()) {
 221           // something is referencing this symbol now.
 222           return sym;
 223         } else {
 224           count--;   // don't count this symbol.
 225         }
 226       }
 227     }
 228   }
 229   // If the bucket size is too deep check if this hash code is insufficient.
 230   if (count >= rehash_count && !needs_rehashing()) {
 231     _needs_rehashing = check_rehash_table(count);
 232   }
 233   return NULL;
 234 }
 235 
 236 Symbol* SymbolTable::lookup_shared(const char* name,
 237                                    int len, unsigned int hash) {
 238   if (use_alternate_hashcode()) {
 239     // hash_code parameter may use alternate hashing algorithm but the shared table
 240     // always uses the same original hash code.
 241     hash = hash_shared_symbol(name, len);





 242   }
 243   return _shared_table.lookup(name, hash, len);
 244 }
 245 
 246 Symbol* SymbolTable::lookup(int index, const char* name,
 247                             int len, unsigned int hash) {
 248   Symbol* sym;
 249   if (_lookup_shared_first) {
 250     sym = lookup_shared(name, len, hash);
 251     if (sym != NULL) {
 252       return sym;

 253     }
 254     _lookup_shared_first = false;
 255     return lookup_dynamic(index, name, len, hash);
 256   } else {
 257     sym = lookup_dynamic(index, name, len, hash);
 258     if (sym != NULL) {
 259       return sym;
 260     }
 261     sym = lookup_shared(name, len, hash);
 262     if (sym != NULL) {
 263       _lookup_shared_first = true;
 264     }
 265     return sym;
 266   }
 267 }
 268 
 269 u4 SymbolTable::encode_shared(Symbol* sym) {
 270   assert(DumpSharedSpaces, "called only during dump time");
 271   uintx base_address = uintx(MetaspaceShared::shared_rs()->base());
 272   uintx offset = uintx(sym) - base_address;
 273   assert(offset < 0x7fffffff, "sanity");
 274   return u4(offset);
 275 }
 276 
 277 Symbol* SymbolTable::decode_shared(u4 offset) {
 278   assert(!DumpSharedSpaces, "called only during runtime");
 279   uintx base_address = _shared_table.base_address();
 280   Symbol* sym = (Symbol*)(base_address + offset);
 281 
 282 #ifndef PRODUCT
 283   const char* s = (const char*)sym->bytes();
 284   int len = sym->utf8_length();
 285   unsigned int hash = hash_symbol(s, len);
 286   assert(sym == lookup_shared(s, len, hash), "must be shared symbol");
 287 #endif
 288 
 289   return sym;
 290 }
 291 
 292 // Pick hashing algorithm.
 293 unsigned int SymbolTable::hash_symbol(const char* s, int len) {
 294   return use_alternate_hashcode() ?
 295            AltHashing::murmur3_32(seed(), (const jbyte*)s, len) :
 296            java_lang_String::hash_code((const jbyte*)s, len);
 297 }
 298 
 299 unsigned int SymbolTable::hash_shared_symbol(const char* s, int len) {
 300   return java_lang_String::hash_code((const jbyte*)s, len);
 301 }
 302 
 303 
 304 // We take care not to be blocking while holding the
 305 // SymbolTable_lock. Otherwise, the system might deadlock, since the
 306 // symboltable is used during compilation (VM_thread) The lock free
 307 // synchronization is simplified by the fact that we do not delete
 308 // entries in the symbol table during normal execution (only during
 309 // safepoints).
 310 
 311 Symbol* SymbolTable::lookup(const char* name, int len, TRAPS) {
 312   unsigned int hashValue = hash_symbol(name, len);
 313   int index = the_table()->hash_to_index(hashValue);
 314 
 315   Symbol* s = the_table()->lookup(index, name, len, hashValue);
 316 
 317   // Found
 318   if (s != NULL) return s;
 319 
 320   // Grab SymbolTable_lock first.
 321   MutexLocker ml(SymbolTable_lock, THREAD);
 322 
 323   // Otherwise, add to symbol to table
 324   return the_table()->basic_add(index, (u1*)name, len, hashValue, true, THREAD);
 325 }
 326 
 327 Symbol* SymbolTable::lookup(const Symbol* sym, int begin, int end, TRAPS) {
 328   char* buffer;
 329   int index, len;
 330   unsigned int hashValue;
 331   char* name;
 332   {
 333     debug_only(NoSafepointVerifier nsv;)
 334 
 335     name = (char*)sym->base() + begin;
 336     len = end - begin;
 337     hashValue = hash_symbol(name, len);
 338     index = the_table()->hash_to_index(hashValue);
 339     Symbol* s = the_table()->lookup(index, name, len, hashValue);
 340 
 341     // Found
 342     if (s != NULL) return s;



 343   }
 344 
 345   // Otherwise, add to symbol to table. Copy to a C string first.
 346   char stack_buf[128];
 347   ResourceMark rm(THREAD);
 348   if (len <= 128) {
 349     buffer = stack_buf;
 350   } else {
 351     buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
 352   }
 353   for (int i=0; i<len; i++) {
 354     buffer[i] = name[i];
 355   }
 356   // Make sure there is no safepoint in the code above since name can't move.
 357   // We can't include the code in NoSafepointVerifier because of the
 358   // ResourceMark.
 359 
 360   // Grab SymbolTable_lock first.
 361   MutexLocker ml(SymbolTable_lock, THREAD);
 362 
 363   return the_table()->basic_add(index, (u1*)buffer, len, hashValue, true, THREAD);
 364 }
 365 
 366 Symbol* SymbolTable::lookup_only(const char* name, int len,
 367                                    unsigned int& hash) {
 368   hash = hash_symbol(name, len);
 369   int index = the_table()->hash_to_index(hash);
 370 
 371   Symbol* s = the_table()->lookup(index, name, len, hash);
 372   return s;
 373 }
 374 
 375 // Look up the address of the literal in the SymbolTable for this Symbol*
 376 // Do not create any new symbols
 377 // Do not increment the reference count to keep this alive
 378 Symbol** SymbolTable::lookup_symbol_addr(Symbol* sym){
 379   unsigned int hash = hash_symbol((char*)sym->bytes(), sym->utf8_length());
 380   int index = the_table()->hash_to_index(hash);
 381 
 382   for (HashtableEntry<Symbol*, mtSymbol>* e = the_table()->bucket(index); e != NULL; e = e->next()) {
 383     if (e->hash() == hash) {
 384       Symbol* literal_sym = e->literal();
 385       if (sym == literal_sym) {
 386         return e->literal_addr();














 387       }


 388     }
 389   }
 390   return NULL;
































 391 }
 392 
 393 // Suggestion: Push unicode-based lookup all the way into the hashing
 394 // and probing logic, so there is no need for convert_to_utf8 until
 395 // an actual new Symbol* is created.
 396 Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) {
 397   int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
 398   char stack_buf[128];
 399   if (utf8_length < (int) sizeof(stack_buf)) {
 400     char* chars = stack_buf;
 401     UNICODE::convert_to_utf8(name, utf16_length, chars);
 402     return lookup(chars, utf8_length, THREAD);
 403   } else {
 404     ResourceMark rm(THREAD);
 405     char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
 406     UNICODE::convert_to_utf8(name, utf16_length, chars);
 407     return lookup(chars, utf8_length, THREAD);
 408   }
 409 }
 410 
 411 Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length,
 412                                            unsigned int& hash) {
 413   int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
 414   char stack_buf[128];
 415   if (utf8_length < (int) sizeof(stack_buf)) {
 416     char* chars = stack_buf;
 417     UNICODE::convert_to_utf8(name, utf16_length, chars);
 418     return lookup_only(chars, utf8_length, hash);
 419   } else {
 420     ResourceMark rm;
 421     char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
 422     UNICODE::convert_to_utf8(name, utf16_length, chars);
 423     return lookup_only(chars, utf8_length, hash);
 424   }
 425 }
 426 
 427 void SymbolTable::add(ClassLoaderData* loader_data, const constantPoolHandle& cp,
 428                       int names_count,
 429                       const char** names, int* lengths, int* cp_indices,
 430                       unsigned int* hashValues, TRAPS) {
 431   // Grab SymbolTable_lock first.
 432   MutexLocker ml(SymbolTable_lock, THREAD);
 433 
 434   SymbolTable* table = the_table();
 435   bool added = table->basic_add(loader_data, cp, names_count, names, lengths,
 436                                 cp_indices, hashValues, CHECK);
 437   if (!added) {
 438     // do it the hard way
 439     for (int i=0; i<names_count; i++) {
 440       int index = table->hash_to_index(hashValues[i]);
 441       bool c_heap = !loader_data->is_the_null_class_loader_data();
 442       Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i], hashValues[i], c_heap, CHECK);
 443       cp->symbol_at_put(cp_indices[i], sym);















 444     }
 445   }


































































 446 }
 447 
 448 Symbol* SymbolTable::new_permanent_symbol(const char* name, TRAPS) {
 449   unsigned int hash;
 450   Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash);
 451   if (result != NULL) {
 452     return result;





 453   }
 454   // Grab SymbolTable_lock first.
 455   MutexLocker ml(SymbolTable_lock, THREAD);
 456 
 457   SymbolTable* table = the_table();
 458   int index = table->hash_to_index(hash);
 459   return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD);
 460 }
 461 
 462 Symbol* SymbolTable::basic_add(int index_arg, u1 *name, int len,
 463                                unsigned int hashValue_arg, bool c_heap, TRAPS) {
 464   assert(!Universe::heap()->is_in_reserved(name),
 465          "proposed name of symbol must be stable");






















 466 
 467   // Don't allow symbols to be created which cannot fit in a Symbol*.
 468   if (len > Symbol::max_length()) {
 469     THROW_MSG_0(vmSymbols::java_lang_InternalError(),
 470                 "name is too long to represent");

 471   }

 472 
 473   // Cannot hit a safepoint in this function because the "this" pointer can move.
 474   NoSafepointVerifier nsv;
















 475 
 476   // Check if the symbol table has been rehashed, if so, need to recalculate
 477   // the hash value and index.
 478   unsigned int hashValue;
 479   int index;
 480   if (use_alternate_hashcode()) {
 481     hashValue = hash_symbol((const char*)name, len);
 482     index = hash_to_index(hashValue);
 483   } else {
 484     hashValue = hashValue_arg;
 485     index = index_arg;





 486   }

 487 
 488   // Since look-up was done lock-free, we need to check if another
 489   // thread beat us in the race to insert the symbol.
 490   Symbol* test = lookup(index, (char*)name, len, hashValue);
 491   if (test != NULL) {
 492     // A race occurred and another thread introduced the symbol.
 493     assert(test->refcount() != 0, "lookup should have incremented the count");
 494     return test;
 495   }



















 496 
 497   // Create a new symbol.
 498   Symbol* sym = allocate_symbol(name, len, c_heap, CHECK_NULL);
 499   assert(sym->equals((char*)name, len), "symbol must be properly initialized");
 500 
 501   HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym);
 502   add_entry(index, entry);
 503   return sym;










 504 }
 505 
 506 // This version of basic_add adds symbols in batch from the constant pool
 507 // parsing.
 508 bool SymbolTable::basic_add(ClassLoaderData* loader_data, const constantPoolHandle& cp,
 509                             int names_count,
 510                             const char** names, int* lengths,
 511                             int* cp_indices, unsigned int* hashValues,
 512                             TRAPS) {
 513 
 514   // Check symbol names are not too long.  If any are too long, don't add any.
 515   for (int i = 0; i< names_count; i++) {
 516     if (lengths[i] > Symbol::max_length()) {
 517       THROW_MSG_0(vmSymbols::java_lang_InternalError(),
 518                   "name is too long to represent");
 519     }
 520   }
 521 
 522   // Cannot hit a safepoint in this function because the "this" pointer can move.
 523   NoSafepointVerifier nsv;




 524 
 525   for (int i=0; i<names_count; i++) {
 526     // Check if the symbol table has been rehashed, if so, need to recalculate
 527     // the hash value.
 528     unsigned int hashValue;
 529     if (use_alternate_hashcode()) {
 530       hashValue = hash_symbol(names[i], lengths[i]);
 531     } else {
 532       hashValue = hashValues[i];







 533     }
 534     // Since look-up was done lock-free, we need to check if another
 535     // thread beat us in the race to insert the symbol.
 536     int index = hash_to_index(hashValue);
 537     Symbol* test = lookup(index, names[i], lengths[i], hashValue);
 538     if (test != NULL) {
 539       // A race occurred and another thread introduced the symbol, this one
 540       // will be dropped and collected. Use test instead.
 541       cp->symbol_at_put(cp_indices[i], test);
 542       assert(test->refcount() != 0, "lookup should have incremented the count");



















 543     } else {
 544       // Create a new symbol.  The null class loader is never unloaded so these
 545       // are allocated specially in a permanent arena.
 546       bool c_heap = !loader_data->is_the_null_class_loader_data();
 547       Symbol* sym = allocate_symbol((const u1*)names[i], lengths[i], c_heap, CHECK_(false));
 548       assert(sym->equals(names[i], lengths[i]), "symbol must be properly initialized");  // why wouldn't it be???
 549       HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym);
 550       add_entry(index, entry);
 551       cp->symbol_at_put(cp_indices[i], sym);
 552     }
 553   }
 554   return true;
 555 }
 556 





 557 
 558 void SymbolTable::verify() {
 559   for (int i = 0; i < the_table()->table_size(); ++i) {
 560     HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i);
 561     for ( ; p != NULL; p = p->next()) {
 562       Symbol* s = (Symbol*)(p->literal());
 563       guarantee(s != NULL, "symbol is NULL");
 564       unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length());
 565       guarantee(p->hash() == h, "broken hash in symbol table entry");
 566       guarantee(the_table()->hash_to_index(h) == i,
 567                 "wrong index in symbol table");
 568     }


 569   }





 570 }
 571 
 572 void SymbolTable::dump(outputStream* st, bool verbose) {
 573   if (!verbose) {
 574     the_table()->print_table_statistics(st, "SymbolTable");





















 575   } else {
 576     st->print_cr("VERSION: 1.0");
 577     for (int i = 0; i < the_table()->table_size(); ++i) {
 578       HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i);
 579       for ( ; p != NULL; p = p->next()) {
 580         Symbol* s = (Symbol*)(p->literal());
 581         const char* utf8_string = (const char*)s->bytes();
 582         int utf8_length = s->utf8_length();
 583         st->print("%d %d: ", utf8_length, s->refcount());
 584         HashtableTextDump::put_utf8(st, utf8_string, utf8_length);
 585         st->cr();
 586       }
 587     }
 588   }

 589 }
 590 
 591 void SymbolTable::write_to_archive() {
 592 #if INCLUDE_CDS
 593     _shared_table.reset();
























 594 
 595     int num_buckets = the_table()->number_of_entries() /
 596                             SharedSymbolTableBucketSize;
 597     CompactSymbolTableWriter writer(num_buckets,
 598                                     &MetaspaceShared::stats()->symbol);
 599     for (int i = 0; i < the_table()->table_size(); ++i) {
 600       HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i);
 601       for ( ; p != NULL; p = p->next()) {
 602         Symbol* s = (Symbol*)(p->literal());
 603       unsigned int fixed_hash =  hash_shared_symbol((char*)s->bytes(), s->utf8_length());
 604         assert(fixed_hash == p->hash(), "must not rehash during dumping");
 605         writer.add(fixed_hash, s);
 606       }
 607     }
 608 
 609     writer.dump(&_shared_table);




 610 
 611     // Verify table is correct
 612     Symbol* sym = vmSymbols::java_lang_Object();
 613     const char* name = (const char*)sym->bytes();
 614     int len = sym->utf8_length();
 615     unsigned int hash = hash_symbol(name, len);
 616     assert(sym == _shared_table.lookup(name, hash, len), "sanity");
 617 #endif








 618 }
 619 
 620 void SymbolTable::serialize(SerializeClosure* soc) {
 621 #if INCLUDE_CDS
 622   _shared_table.set_type(CompactHashtable<Symbol*, char>::_symbol_table);
 623   _shared_table.serialize(soc);








 624 
 625   if (soc->writing()) {
 626     // Sanity. Make sure we don't use the shared table at dump time
 627     _shared_table.reset();



 628   }
 629 #endif













 630 }
 631 
 632 //---------------------------------------------------------------------------
 633 // Non-product code
 634 
 635 #ifndef PRODUCT
 636 
 637 void SymbolTable::print_histogram() {
 638   MutexLocker ml(SymbolTable_lock);
 639   const int results_length = 100;
 640   int counts[results_length];
 641   int sizes[results_length];
 642   int i,j;
 643 
 644   // initialize results to zero
 645   for (j = 0; j < results_length; j++) {
 646     counts[j] = 0;
 647     sizes[j] = 0;
 648   }
 649 
 650   int total_size = 0;
 651   int total_count = 0;
 652   int total_length = 0;
 653   int max_length = 0;
 654   int out_of_range_count = 0;
 655   int out_of_range_size = 0;
 656   for (i = 0; i < the_table()->table_size(); i++) {
 657     HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i);
 658     for ( ; p != NULL; p = p->next()) {
 659       int size = p->literal()->size();
 660       int len = p->literal()->utf8_length();
 661       if (len < results_length) {
 662         counts[len]++;
 663         sizes[len] += size;
 664       } else {
 665         out_of_range_count++;
 666         out_of_range_size += size;
 667       }
 668       total_count++;
 669       total_size += size;
 670       total_length += len;
 671       max_length = MAX2(max_length, len);
 672     }
 673   }











 674   tty->print_cr("Symbol Table Histogram:");
 675   tty->print_cr("  Total number of symbols  %7d", total_count);
 676   tty->print_cr("  Total size in memory     %7dK",
 677           (total_size*wordSize)/1024);
 678   tty->print_cr("  Total counted            %7d", _symbols_counted);
 679   tty->print_cr("  Total removed            %7d", _symbols_removed);
 680   if (_symbols_counted > 0) {
 681     tty->print_cr("  Percent removed          %3.2f",
 682           ((float)_symbols_removed/(float)_symbols_counted)* 100);
 683   }
 684   tty->print_cr("  Reference counts         %7d", Symbol::_total_count);
 685   tty->print_cr("  Symbol arena used        " SIZE_FORMAT_W(7) "K", arena()->used()/1024);
 686   tty->print_cr("  Symbol arena size        " SIZE_FORMAT_W(7) "K", arena()->size_in_bytes()/1024);
 687   tty->print_cr("  Total symbol length      %7d", total_length);
 688   tty->print_cr("  Maximum symbol length    %7d", max_length);
 689   tty->print_cr("  Average symbol length    %7.2f", ((float) total_length / (float) total_count));
 690   tty->print_cr("  Symbol length histogram:");
 691   tty->print_cr("    %6s %10s %10s", "Length", "#Symbols", "Size");
 692   for (i = 0; i < results_length; i++) {
 693     if (counts[i] > 0) {
 694       tty->print_cr("    %6d %10d %10dK", i, counts[i], (sizes[i]*wordSize)/1024);
 695     }
 696   }
 697   tty->print_cr("  >=%6d %10d %10dK\n", results_length,
 698           out_of_range_count, (out_of_range_size*wordSize)/1024);
 699 }
 700 
 701 void SymbolTable::print() {
 702   for (int i = 0; i < the_table()->table_size(); ++i) {
 703     HashtableEntry<Symbol*, mtSymbol>** p = the_table()->bucket_addr(i);
 704     HashtableEntry<Symbol*, mtSymbol>* entry = the_table()->bucket(i);
 705     if (entry != NULL) {
 706       while (entry != NULL) {
 707         tty->print(PTR_FORMAT " ", p2i(entry->literal()));
 708         entry->literal()->print();
 709         tty->print(" %d", entry->literal()->refcount());
 710         p = entry->next_addr();
 711         entry = (HashtableEntry<Symbol*, mtSymbol>*)HashtableEntry<Symbol*, mtSymbol>::make_ptr(*p);
 712       }
 713       tty->cr();
 714     }
 715   }


 716 }
 717 #endif // PRODUCT
 718 
 719 
 720 // Utility for dumping symbols
 721 SymboltableDCmd::SymboltableDCmd(outputStream* output, bool heap) :
 722                                  DCmdWithParser(output, heap),
 723   _verbose("-verbose", "Dump the content of each symbol in the table",
 724            "BOOLEAN", false, "false") {
 725   _dcmdparser.add_dcmd_option(&_verbose);
 726 }
 727 
 728 void SymboltableDCmd::execute(DCmdSource source, TRAPS) {
 729   VM_DumpHashtable dumper(output(), VM_DumpHashtable::DumpSymbols,
 730                          _verbose.value());
 731   VMThread::execute(&dumper);
 732 }
 733 
 734 int SymboltableDCmd::num_arguments() {
 735   ResourceMark rm;
 736   SymboltableDCmd* dcmd = new SymboltableDCmd(NULL, false);
 737   if (dcmd != NULL) {
 738     DCmdMark mark(dcmd);
 739     return dcmd->_dcmdparser.num_arguments();
< prev index next >