< prev index next >

src/hotspot/share/classfile/stringTable.cpp

Print this page
rev 50373 : 8195097: Make it possible to process StringTable outside safepoint
Reviewed-by:
rev 50374 : imported patch 8195097-stringtable-robbin
rev 50375 : [mq]: 8195097-stringtable-v2


  38 #include "memory/metaspaceShared.hpp"
  39 #include "memory/resourceArea.hpp"
  40 #include "memory/universe.hpp"
  41 #include "oops/access.inline.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "oops/typeArrayOop.inline.hpp"
  44 #include "oops/weakHandle.inline.hpp"
  45 #include "runtime/atomic.hpp"
  46 #include "runtime/handles.inline.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/safepointVerifiers.hpp"
  49 #include "runtime/timerTrace.hpp"
  50 #include "runtime/interfaceSupport.inline.hpp"
  51 #include "services/diagnosticCommand.hpp"
  52 #include "utilities/concurrentHashTable.inline.hpp"
  53 #include "utilities/concurrentHashTableTasks.inline.hpp"
  54 #include "utilities/macros.hpp"
  55 
  56 // We prefer short chains of avg 2
  57 #define PREF_AVG_LIST_LEN   2
  58 // We start with same old size, consider to reduce this
  59 #define START_SIZE         16
  60 // 2^24 is max size
  61 #define END_SIZE           24
  62 // If a chain gets to 32 something might be wrong
  63 #define REHASH_LEN         32
  64 // If we have as many dead items as 50% of the number of bucket
  65 #define CLEAN_DEAD_HIGH_WATER_MARK 0.5
  66 
  67 // --------------------------------------------------------------------------
  68 StringTable* StringTable::_the_table = NULL;
  69 bool StringTable::_shared_string_mapped = false;
  70 CompactHashtable<oop, char> StringTable::_shared_table;
  71 bool StringTable::_alt_hash = false;
  72 
  73 static juint murmur_seed = 0;
  74 
  75 uintx hash_string(const jchar* s, int len, bool useAlt) {
  76   return  useAlt ?
  77     AltHashing::murmur3_32(murmur_seed, s, len) :
  78     java_lang_String::hash_code(s, len);
  79 }


  97     if (chars != NULL) {
  98       return hash_string(chars, length, StringTable::_alt_hash);
  99     }
 100     vm_exit_out_of_memory(length, OOM_MALLOC_ERROR, "get hash from oop");
 101     return 0;
 102   }
 103   // We use default allocation/deallocation but counted
 104   static void* allocate_node(size_t size,
 105                              WeakHandle<vm_string_table_data> const& value) {
 106     StringTable::item_added();
 107     return StringTableHash::BaseConfig::allocate_node(size, value);
 108   }
 109   static void free_node(void* memory,
 110                         WeakHandle<vm_string_table_data> const& value) {
 111     value.release();
 112     StringTableHash::BaseConfig::free_node(memory, value);
 113     StringTable::item_removed();
 114   }
 115 };
 116 
 117 class StringTableLookupJchar {
 118  private:
 119   Thread* _thread;
 120   uintx _hash;
 121   int _len;
 122   const jchar* _str;
 123   Handle _found;
 124 
 125  public:
 126   StringTableLookupJchar(Thread* thread, uintx hash, const jchar* key, int len)
 127     : _thread(thread), _hash(hash), _str(key), _len(len) {
 128   }
 129   uintx get_hash() const {
 130     return _hash;
 131   }
 132   bool equals(WeakHandle<vm_string_table_data>* value, bool* is_dead) {
 133     oop val_oop = value->peek();
 134     if (val_oop == NULL) {
 135       // dead oop, mark this hash dead for cleaning
 136       *is_dead = true;
 137       return false;


 161     return _hash;
 162   }
 163 
 164   bool equals(WeakHandle<vm_string_table_data>* value, bool* is_dead) {
 165     oop val_oop = value->peek();
 166     if (val_oop == NULL) {
 167       // dead oop, mark this hash dead for cleaning
 168       *is_dead = true;
 169       return false;
 170     }
 171     bool equals = java_lang_String::equals(_find(), val_oop);
 172     if (!equals) {
 173       return false;
 174     }
 175     // Need to resolve weak handle and Handleize through possible safepoint.
 176     _found = Handle(_thread, value->resolve());
 177     return true;
 178   }
 179 };
 180 






 181 StringTable::StringTable() : _local_table(NULL), _current_size(0), _has_work(0),
 182   _needs_rehashing(false), _weak_handles(NULL), _items(0), _uncleaned_items(0) {
 183   _weak_handles = new OopStorage("StringTable weak",
 184                                  StringTableWeakAlloc_lock,
 185                                  StringTableWeakActive_lock);
 186   _local_table = new StringTableHash(START_SIZE, END_SIZE, REHASH_LEN);
 187   _current_size = ((size_t)1) << START_SIZE;



 188 }
 189 
 190 size_t StringTable::item_added() {
 191   return Atomic::add((size_t)1, &(the_table()->_items));
 192 }
 193 
 194 size_t StringTable::items_to_clean(size_t ncl) {
 195   size_t total = Atomic::add((size_t)ncl, &(the_table()->_uncleaned_items));
 196   log_trace(stringtable)(
 197      "Uncleaned items:" SIZE_FORMAT " added: " SIZE_FORMAT " total:" SIZE_FORMAT,
 198      the_table()->_uncleaned_items, ncl, total);
 199   return total;
 200 }
 201 
 202 void StringTable::item_removed() {
 203   Atomic::add((size_t)-1, &(the_table()->_items));
 204   Atomic::add((size_t)-1, &(the_table()->_uncleaned_items));
 205 }
 206 
 207 double StringTable::get_load_factor() {
 208   return (_items*1.0)/_current_size;
 209 }
 210 
 211 double StringTable::get_dead_factor() {
 212   return (_uncleaned_items*1.0)/_current_size;
 213 }
 214 












 215 oop StringTable::lookup(Symbol* symbol) {
 216   ResourceMark rm;
 217   int length;
 218   jchar* chars = symbol->as_unicode(length);
 219   return lookup(chars, length);
 220 }
 221 
 222 oop StringTable::lookup(jchar* name, int len) {
 223   unsigned int hash = java_lang_String::hash_code(name, len);
 224   oop string = StringTable::the_table()->lookup_shared(name, len, hash);
 225   if (string != NULL) {
 226     return string;
 227   }
 228   if (StringTable::_alt_hash) {
 229     hash = hash_string(name, len, true);
 230   }
 231   return StringTable::the_table()->do_lookup( name, len, hash);
 232 }
 233 
 234 class StringTableGet : public StackObj {
 235   Thread* _thread;
 236   Handle  _return;
 237  public:
 238   StringTableGet(Thread* thread) : _thread(thread) {}
 239   void operator()(WeakHandle<vm_string_table_data>* val) {
 240     oop result = val->resolve();
 241     assert(result != NULL, "Result should be reachable");
 242     _return = Handle(_thread, result);
 243   }
 244   oop get_res_oop() {
 245     return _return();
 246   }
 247 };
 248 
 249 oop StringTable::do_lookup(jchar* name, int len, uintx hash) {
 250   Thread* thread = Thread::current();
 251   StringTableLookupJchar lookup(thread, hash, name, len);
 252   StringTableGet stg(thread);
 253   bool rehash_warning;
 254   _local_table->get(thread, lookup, stg, &rehash_warning);
 255   if (rehash_warning) {
 256     _needs_rehashing = true;
 257   }
 258   return stg.get_res_oop();
 259 }
 260 















































 261 class StringTableCreateEntry : public StackObj {
 262  private:
 263    Thread* _thread;
 264    Handle  _return;
 265    Handle  _store;
 266  public:
 267   StringTableCreateEntry(Thread* thread, Handle store)
 268     : _thread(thread), _store(store) {}
 269 
 270   WeakHandle<vm_string_table_data> operator()() { // No dups found
 271     WeakHandle<vm_string_table_data> wh =
 272       WeakHandle<vm_string_table_data>::create(_store);
 273     return wh;
 274   }
 275   void operator()(bool inserted, WeakHandle<vm_string_table_data>* val) {
 276     oop result = val->resolve();
 277     assert(result != NULL, "Result should be reachable");
 278     _return = Handle(_thread, result);
 279   }
 280   oop get_return() const {
 281     return _return();
 282   }
 283 };
 284 
 285 oop StringTable::intern(Handle string_or_null_h, jchar* name, int len, TRAPS) {
 286   // shared table always uses java_lang_String::hash_code
 287   unsigned int hash = java_lang_String::hash_code(name, len);
 288   oop found_string = StringTable::the_table()->lookup_shared(name, len, hash);
 289   if (found_string != NULL) {
 290     return found_string;
 291   }
 292   if (StringTable::_alt_hash) {
 293     hash = hash_string(name, len, true);
 294   }
 295   return StringTable::the_table()->do_intern(string_or_null_h, name, len,
 296                                              hash, CHECK_NULL);
 297 }
 298 
 299 oop StringTable::do_intern(Handle string_or_null_h, jchar* name,
 300                            int len, uintx hash, TRAPS) {
 301   HandleMark hm(THREAD);  // cleanup strings created
 302   Handle string_h;
 303 
 304   if (!string_or_null_h.is_null()) {
 305     string_h = string_or_null_h;
 306   } else {
 307     string_h = java_lang_String::create_from_unicode(name, len, CHECK_NULL);
 308   }
 309 
 310   // Deduplicate the string before it is interned. Note that we should never
 311   // deduplicate a string after it has been interned. Doing so will counteract
 312   // compiler optimizations done on e.g. interned string literals.
 313   Universe::heap()->deduplicate_string(string_h());
 314 
 315   assert(java_lang_String::equals(string_h(), name, len),
 316          "string must be properly initialized");
 317   assert(len == java_lang_String::length(string_h()), "Must be same length");
 318   StringTableLookupOop lookup(THREAD, hash, string_h);
 319   StringTableCreateEntry stc(THREAD, string_h);
 320 
 321   bool rehash_warning;
 322   _local_table->get_insert_lazy(THREAD, lookup, stc, stc, &rehash_warning);
 323   if (rehash_warning) {
 324     _needs_rehashing = true;
 325   }
 326   return stc.get_return();
 327 }
 328 
 329 oop StringTable::intern(Symbol* symbol, TRAPS) {
 330   if (symbol == NULL) return NULL;
 331   ResourceMark rm(THREAD);
 332   int length;
 333   jchar* chars = symbol->as_unicode(length);
 334   Handle string;
 335   oop result = intern(string, chars, length, CHECK_NULL);
 336   return result;
 337 }
 338 
 339 oop StringTable::intern(oop string, TRAPS) {
 340   if (string == NULL) return NULL;
 341   ResourceMark rm(THREAD);
 342   int length;
 343   Handle h_string (THREAD, string);
 344   jchar* chars = java_lang_String::as_unicode_string(string, length,
 345                                                      CHECK_NULL);
 346   oop result = intern(h_string, chars, length, CHECK_NULL);
 347   return result;
 348 }
 349 
 350 oop StringTable::intern(const char* utf8_string, TRAPS) {
 351   if (utf8_string == NULL) return NULL;
 352   ResourceMark rm(THREAD);
 353   int length = UTF8::unicode_length(utf8_string);
 354   jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
 355   UTF8::convert_to_unicode(utf8_string, chars, length);
 356   Handle string;
 357   oop result = intern(string, chars, length, CHECK_NULL);
 358   return result;
 359 }
 360 
 361 size_t StringTable::table_size(Thread* thread) {
 362   return ((size_t)(1)) << _local_table->get_size_log2(thread != NULL ? thread
 363                                                       : Thread::current());
 364 }
 365 
 366 class StringTableIsAliveCounter : public BoolObjectClosure {
 367   BoolObjectClosure* _real_boc;
 368  public:
 369   size_t _count;
 370   size_t _count_total;
 371   StringTableIsAliveCounter(BoolObjectClosure* boc) : _real_boc(boc), _count(0),
 372                                                       _count_total(0) {}
 373   bool do_object_b(oop obj) {
 374     bool ret = _real_boc->do_object_b(obj);
 375     if (!ret) {
 376       ++_count;
 377     }
 378     ++_count_total;
 379     return ret;
 380   }
 381 };
 382 
 383 void StringTable::trigger_concurrent_work() {
 384   MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
 385   the_table()->_has_work = true;
 386   Service_lock->notify_all();
 387 }
 388 
 389 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f,
 390                                     int* processed, int* removed) {
 391   DoNothingClosure dnc;
 392   assert(is_alive != NULL, "No closure");
 393   StringTableIsAliveCounter stiac(is_alive);
 394   OopClosure* tmp = f != NULL ? f : &dnc;
 395 
 396   StringTable::the_table()->_weak_handles->weak_oops_do(&stiac, tmp);
 397 
 398   StringTable::the_table()->items_to_clean(stiac._count);
 399   StringTable::the_table()->check_concurrent_work();
 400   if (processed != NULL) {
 401     *processed = (int) stiac._count_total;
 402   }
 403   if (removed != NULL) {
 404     *removed = (int) stiac._count;
 405   }
 406 }
 407 
 408 void StringTable::oops_do(OopClosure* f) {


 417   DoNothingClosure dnc;
 418   assert(cl != NULL, "No closure");
 419   StringTableIsAliveCounter stiac(cl);
 420 
 421   _par_state_string->weak_oops_do(&stiac, &dnc);
 422 
 423   StringTable::the_table()->items_to_clean(stiac._count);
 424   StringTable::the_table()->check_concurrent_work();
 425   *processed = (int) stiac._count_total;
 426   *removed = (int) stiac._count;
 427 }
 428 
 429 void StringTable::possibly_parallel_oops_do(
 430    OopStorage::ParState<false /* concurrent */, false /* const */>*
 431    _par_state_string, OopClosure* f)
 432 {
 433   assert(f != NULL, "No closure");
 434   _par_state_string->oops_do(f);
 435 }
 436 
 437 struct StringTableDeleteCheck {
 438   long _count;
 439   long _item;
 440   StringTableDeleteCheck() : _count(0), _item(0) {}
 441   bool operator()(WeakHandle<vm_string_table_data>* val) {
 442     ++_item;
 443     oop tmp = val->peek();
 444     if (tmp == NULL) {
 445       ++_count;
 446       return true;
 447     } else {
 448       return false;
 449     }
 450   }
 451 };
 452 
 453 struct StringTableDoDelete {
 454   long _count;
 455   StringTableDoDelete() : _count(0) {}
 456   void operator()(WeakHandle<vm_string_table_data>* val) {
 457     ++_count;
 458   }
 459 };
 460 
 461 void StringTable::grow(JavaThread* jt) {
 462   StringTableHash::GrowTask gt(_local_table);
 463   if (!gt.prepare(jt)) {
 464     return;
 465   }
 466   log_trace(stringtable)("Started to growed");
 467   {
 468     TraceTime timer("Grow", TRACETIME_LOG(Debug, stringtable, perf));
 469     while(gt.doTask(jt)) {
 470       gt.pause(jt);
 471       {
 472         ThreadBlockInVM tbivm(jt);
 473       }
 474       gt.cont(jt);
 475     }
 476   }
 477   gt.done(jt);
 478   _current_size = table_size(jt);
 479   log_debug(stringtable)("Growed to size:" SIZE_FORMAT, _current_size);
 480 }
 481 
























 482 void StringTable::clean_dead_entries(JavaThread* jt) {
 483   StringTableHash::BulkDeleteTask bdt(_local_table);
 484   if (!bdt.prepare(jt)) {
 485     return;
 486   }
 487 
 488   StringTableDeleteCheck stdc;
 489   StringTableDoDelete stdd;
 490   bool interrupted = false;
 491   {
 492     TraceTime timer("Clean", TRACETIME_LOG(Debug, stringtable, perf));
 493     while(bdt.doTask(jt, stdc, stdd)) {
 494       bdt.pause(jt);
 495       {
 496         ThreadBlockInVM tbivm(jt);
 497       }
 498       if (!bdt.cont(jt)) {
 499         interrupted = true;
 500         break;
 501       }
 502     }
 503   }
 504   if (interrupted) {
 505     _has_work = true;
 506   } else {
 507     bdt.done(jt);
 508   }
 509   log_debug(stringtable)("Cleaned %ld of %ld", stdc._count, stdc._item);
 510 }
 511 
 512 void StringTable::check_concurrent_work() {
 513   if (_has_work) {
 514     return;
 515   }
 516   double fact = StringTable::get_load_factor();
 517   double dead_fact = StringTable::get_dead_factor();
 518   // We should clean/resize if we have more dead than alive,
 519   // more items than preferred load factor or
 520   // more dead items than water mark.
 521   if ((dead_fact > fact) ||
 522       (fact > PREF_AVG_LIST_LEN) ||
 523       (dead_fact > CLEAN_DEAD_HIGH_WATER_MARK)) {
 524     log_debug(stringtable)("Concurrent work triggered, live factor:%g dead factor:%g",
 525                            fact, dead_fact);
 526     trigger_concurrent_work();
 527   }
 528 }
 529 
 530 void StringTable::concurrent_work(JavaThread* jt) {
 531   _has_work = false;
 532   double fact = get_load_factor();
 533   log_debug(stringtable, perf)("Concurrent work, live factor: %g", fact);
 534   // We prefer growing, since that also removes dead items
 535   if (fact > PREF_AVG_LIST_LEN && !_local_table->is_max_size_reached()) {
 536     grow(jt);
 537   } else {
 538     clean_dead_entries(jt);
 539   }
 540 }
 541 
 542 void StringTable::do_concurrent_work(JavaThread* jt) {
 543   StringTable::the_table()->concurrent_work(jt);
 544 }
 545 

 546 bool StringTable::do_rehash() {
 547   if (!_local_table->is_safepoint_safe()) {
 548     return false;
 549   }
 550 
 551   // We use max size
 552   StringTableHash* new_table = new StringTableHash(END_SIZE, END_SIZE, REHASH_LEN);
 553   // Use alt hash from now on
 554   _alt_hash = true;
 555   if (!_local_table->try_move_nodes_to(Thread::current(), new_table)) {
 556     _alt_hash = false;
 557     delete new_table;
 558     return false;
 559   }
 560 
 561   // free old table
 562   delete _local_table;
 563   _local_table = new_table;
 564 
 565   return true;


 583     trigger_concurrent_work();
 584     _needs_rehashing = false;
 585     return;
 586   }
 587 
 588   murmur_seed = AltHashing::compute_seed();
 589   {
 590     if (do_rehash()) {
 591       rehashed = true;
 592     } else {
 593       log_info(stringtable)("Resizes in progress rehashing skipped.");
 594     }
 595   }
 596   _needs_rehashing = false;
 597 }
 598 
 599 void StringTable::rehash_table() {
 600   StringTable::the_table()->try_rehash_table();
 601 }
 602 
 603 oop StringTable::lookup_shared(jchar* name, int len, unsigned int hash) {
 604   assert(hash == java_lang_String::hash_code(name, len),
 605          "hash must be computed using java_lang_String::hash_code");
 606   return _shared_table.lookup((const char*)name, hash, len);
 607 }
 608 
 609 static int literal_size(oop obj) {
 610   // NOTE: this would over-count if (pre-JDK8)
 611   // java_lang_Class::has_offset_field() is true and the String.value array is
 612   // shared by several Strings. However, starting from JDK8, the String.value
 613   // array is not shared anymore.
 614   if (obj == NULL) {
 615     return 0;
 616   } else if (obj->klass() == SystemDictionary::String_klass()) {
 617     return (obj->size() + java_lang_String::value(obj)->size()) * HeapWordSize;
 618   } else {
 619     return obj->size();
 620   }
 621 }
 622 
 623 struct SizeFunc {
 624   size_t operator()(WeakHandle<vm_string_table_data>* val) {
 625     oop s = val->peek();
 626     if (s == NULL) {
 627       // Dead
 628       return 0;
 629     }
 630     return literal_size(s);
 631   };
 632 };
 633 
 634 void StringTable::print_table_statistics(outputStream* st,
 635                                          const char* table_name) {
 636   SizeFunc sz;
 637   _local_table->statistics_to(Thread::current(), sz, st, table_name);
 638 }
 639 
 640 class PrintString {




























































 641   Thread* _thr;
 642   outputStream* _st;
 643  public:
 644   PrintString(Thread* thr, outputStream* st) : _thr(thr), _st(st) {}
 645   bool operator()(WeakHandle<vm_string_table_data>* val) {
 646     oop s = val->peek();
 647     if (s == NULL) {
 648       return true;
 649     }
 650     typeArrayOop value     = java_lang_String::value_no_keepalive(s);
 651     int          length    = java_lang_String::length(s);
 652     bool         is_latin1 = java_lang_String::is_latin1(s);
 653 
 654     if (length <= 0) {
 655       _st->print("%d: ", length);
 656     } else {
 657       ResourceMark rm(_thr);
 658       int utf8_length = length;
 659       char* utf8_string;
 660 


 671     }
 672     _st->cr();
 673     return true;
 674   };
 675 };
 676 
 677 void StringTable::dump(outputStream* st, bool verbose) {
 678   if (!verbose) {
 679     the_table()->print_table_statistics(st, "StringTable");
 680   } else {
 681     Thread* thr = Thread::current();
 682     ResourceMark rm(thr);
 683     st->print_cr("VERSION: 1.1");
 684     PrintString ps(thr, st);
 685     if (!the_table()->_local_table->try_scan(thr, ps)) {
 686       st->print_cr("dump unavailable at this moment");
 687     }
 688   }
 689 }
 690 
 691 class VerifyStrings {
 692  public:
 693   bool operator()(WeakHandle<vm_string_table_data>* val) {
 694     oop s = val->peek();
 695     if (s != NULL) {
 696       assert(java_lang_String::length(s) >= 0, "Length on string must work.");
 697     }
 698     return true;
 699   };
 700 };
 701 
 702 // This verification is part of Universe::verify() and needs to be quick.
 703 void StringTable::verify() {
 704   Thread* thr = Thread::current();
 705   VerifyStrings vs;
 706   if (!the_table()->_local_table->try_scan(thr, vs)) {
 707     log_info(stringtable)("verify unavailable at this moment");
 708   }
 709 }
 710 
 711 // Utility for dumping strings
 712 StringtableDCmd::StringtableDCmd(outputStream* output, bool heap) :
 713                                  DCmdWithParser(output, heap),
 714   _verbose("-verbose", "Dump the content of each string in the table",
 715            "BOOLEAN", false, "false") {
 716   _dcmdparser.add_dcmd_option(&_verbose);
 717 }
 718 
 719 void StringtableDCmd::execute(DCmdSource source, TRAPS) {
 720   VM_DumpHashtable dumper(output(), VM_DumpHashtable::DumpStrings,
 721                          _verbose.value());
 722   VMThread::execute(&dumper);
 723 }
 724 
 725 int StringtableDCmd::num_arguments() {
 726   ResourceMark rm;
 727   StringtableDCmd* dcmd = new StringtableDCmd(NULL, false);
 728   if (dcmd != NULL) {
 729     DCmdMark mark(dcmd);
 730     return dcmd->_dcmdparser.num_arguments();
 731   } else {
 732     return 0;
 733   }
 734 }
 735 
 736 #if INCLUDE_CDS_JAVA_HEAP
 737 // Sharing







 738 oop StringTable::create_archived_string(oop s, Thread* THREAD) {
 739   assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
 740 
 741   oop new_s = NULL;
 742   typeArrayOop v = java_lang_String::value_no_keepalive(s);
 743   typeArrayOop new_v =
 744     (typeArrayOop)MetaspaceShared::archive_heap_object(v, THREAD);
 745   if (new_v == NULL) {
 746     return NULL;
 747   }
 748   new_s = MetaspaceShared::archive_heap_object(s, THREAD);
 749   if (new_s == NULL) {
 750     return NULL;
 751   }
 752 
 753   // adjust the pointer to the 'value' field in the new String oop
 754   java_lang_String::set_value_raw(new_s, new_v);
 755   return new_s;
 756 }
 757 
 758 struct CopyArchive {
 759   CompactStringTableWriter* _writer;
 760   CopyArchive(CompactStringTableWriter* writer) : _writer(writer) {}
 761   bool operator()(WeakHandle<vm_string_table_data>* val) {
 762     oop s = val->peek();
 763     if (s == NULL) {
 764       return true;
 765     }
 766     unsigned int hash = java_lang_String::hash_code(s);
 767     if (hash == 0) {
 768       return true;
 769     }
 770 
 771     java_lang_String::set_hash(s, hash);
 772     oop new_s = StringTable::create_archived_string(s, Thread::current());
 773     if (new_s == NULL) {
 774       return true;
 775     }
 776 
 777     val->replace(new_s);
 778     // add to the compact table
 779     _writer->add(hash, new_s);
 780     return true;
 781   }
 782 };
 783 
 784 void StringTable::copy_shared_string(CompactStringTableWriter* writer) {
 785   assert(MetaspaceShared::is_heap_object_archiving_allowed(), "must be");
 786 
 787   CopyArchive copy(writer);
 788   StringTable::the_table()->_local_table->do_scan(Thread::current(), copy);
 789 }
 790 
 791 void StringTable::write_to_archive() {
 792   assert(MetaspaceShared::is_heap_object_archiving_allowed(), "must be");
 793 
 794   _shared_table.reset();
 795   int num_buckets = the_table()->_items / SharedSymbolTableBucketSize;
 796   // calculation of num_buckets can result in zero buckets, we need at least one
 797   CompactStringTableWriter writer(num_buckets > 1 ? num_buckets : 1,
 798                                   &MetaspaceShared::stats()->string);
 799 
 800   // Copy the interned strings into the "string space" within the java heap
 801   copy_shared_string(&writer);
 802   writer.dump(&_shared_table);
 803 }
 804 
 805 void StringTable::serialize(SerializeClosure* soc) {
 806   _shared_table.set_type(CompactHashtable<oop, char>::_string_table);
 807   _shared_table.serialize(soc);
 808 
 809   if (soc->writing()) {
 810     // Sanity. Make sure we don't use the shared table at dump time
 811     _shared_table.reset();
 812   } else if (!_shared_string_mapped) {
 813     _shared_table.reset();
 814   }
 815 }
 816 
 817 void StringTable::shared_oops_do(OopClosure* f) {
 818   _shared_table.oops_do(f);
 819 }
 820 
 821 #endif //INCLUDE_CDS_JAVA_HEAP


  38 #include "memory/metaspaceShared.hpp"
  39 #include "memory/resourceArea.hpp"
  40 #include "memory/universe.hpp"
  41 #include "oops/access.inline.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "oops/typeArrayOop.inline.hpp"
  44 #include "oops/weakHandle.inline.hpp"
  45 #include "runtime/atomic.hpp"
  46 #include "runtime/handles.inline.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/safepointVerifiers.hpp"
  49 #include "runtime/timerTrace.hpp"
  50 #include "runtime/interfaceSupport.inline.hpp"
  51 #include "services/diagnosticCommand.hpp"
  52 #include "utilities/concurrentHashTable.inline.hpp"
  53 #include "utilities/concurrentHashTableTasks.inline.hpp"
  54 #include "utilities/macros.hpp"
  55 
  56 // We prefer short chains of avg 2
  57 #define PREF_AVG_LIST_LEN   2


  58 // 2^24 is max size
  59 #define END_SIZE           24
  60 // If a chain gets to 32 something might be wrong
  61 #define REHASH_LEN         32
  62 // If we have as many dead items as 50% of the number of bucket
  63 #define CLEAN_DEAD_HIGH_WATER_MARK 0.5
  64 
  65 // --------------------------------------------------------------------------
  66 StringTable* StringTable::_the_table = NULL;
  67 bool StringTable::_shared_string_mapped = false;
  68 CompactHashtable<oop, char> StringTable::_shared_table;
  69 bool StringTable::_alt_hash = false;
  70 
  71 static juint murmur_seed = 0;
  72 
  73 uintx hash_string(const jchar* s, int len, bool useAlt) {
  74   return  useAlt ?
  75     AltHashing::murmur3_32(murmur_seed, s, len) :
  76     java_lang_String::hash_code(s, len);
  77 }


  95     if (chars != NULL) {
  96       return hash_string(chars, length, StringTable::_alt_hash);
  97     }
  98     vm_exit_out_of_memory(length, OOM_MALLOC_ERROR, "get hash from oop");
  99     return 0;
 100   }
 101   // We use default allocation/deallocation but counted
 102   static void* allocate_node(size_t size,
 103                              WeakHandle<vm_string_table_data> const& value) {
 104     StringTable::item_added();
 105     return StringTableHash::BaseConfig::allocate_node(size, value);
 106   }
 107   static void free_node(void* memory,
 108                         WeakHandle<vm_string_table_data> const& value) {
 109     value.release();
 110     StringTableHash::BaseConfig::free_node(memory, value);
 111     StringTable::item_removed();
 112   }
 113 };
 114 
 115 class StringTableLookupJchar : StackObj {
 116  private:
 117   Thread* _thread;
 118   uintx _hash;
 119   int _len;
 120   const jchar* _str;
 121   Handle _found;
 122 
 123  public:
 124   StringTableLookupJchar(Thread* thread, uintx hash, const jchar* key, int len)
 125     : _thread(thread), _hash(hash), _str(key), _len(len) {
 126   }
 127   uintx get_hash() const {
 128     return _hash;
 129   }
 130   bool equals(WeakHandle<vm_string_table_data>* value, bool* is_dead) {
 131     oop val_oop = value->peek();
 132     if (val_oop == NULL) {
 133       // dead oop, mark this hash dead for cleaning
 134       *is_dead = true;
 135       return false;


 159     return _hash;
 160   }
 161 
 162   bool equals(WeakHandle<vm_string_table_data>* value, bool* is_dead) {
 163     oop val_oop = value->peek();
 164     if (val_oop == NULL) {
 165       // dead oop, mark this hash dead for cleaning
 166       *is_dead = true;
 167       return false;
 168     }
 169     bool equals = java_lang_String::equals(_find(), val_oop);
 170     if (!equals) {
 171       return false;
 172     }
 173     // Need to resolve weak handle and Handleize through possible safepoint.
 174     _found = Handle(_thread, value->resolve());
 175     return true;
 176   }
 177 };
 178 
 179 static size_t nearest_pow_2(uintx val) {
 180   size_t ret;
 181   for (ret = 1; ((size_t)1 << ret) < val; ++ret);
 182   return ret;
 183 }
 184 
 185 StringTable::StringTable() : _local_table(NULL), _current_size(0), _has_work(0),
 186   _needs_rehashing(false), _weak_handles(NULL), _items(0), _uncleaned_items(0) {
 187   _weak_handles = new OopStorage("StringTable weak",
 188                                  StringTableWeakAlloc_lock,
 189                                  StringTableWeakActive_lock);
 190   size_t start_size_log_2 = nearest_pow_2(StringTableSize);
 191   _current_size = ((size_t)1) << start_size_log_2;
 192   log_trace(stringtable)("Start size: " SIZE_FORMAT " (" SIZE_FORMAT ")",
 193                          _current_size, start_size_log_2);
 194   _local_table = new StringTableHash(start_size_log_2, END_SIZE, REHASH_LEN);
 195 }
 196 
 197 size_t StringTable::item_added() {
 198   return Atomic::add((size_t)1, &(the_table()->_items));
 199 }
 200 
 201 size_t StringTable::items_to_clean(size_t ncl) {
 202   size_t total = Atomic::add((size_t)ncl, &(the_table()->_uncleaned_items));
 203   log_trace(stringtable)(
 204      "Uncleaned items:" SIZE_FORMAT " added: " SIZE_FORMAT " total:" SIZE_FORMAT,
 205      the_table()->_uncleaned_items, ncl, total);
 206   return total;
 207 }
 208 
 209 void StringTable::item_removed() {
 210   Atomic::add((size_t)-1, &(the_table()->_items));
 211   Atomic::add((size_t)-1, &(the_table()->_uncleaned_items));
 212 }
 213 
 214 double StringTable::get_load_factor() {
 215   return (_items*1.0)/_current_size;
 216 }
 217 
 218 double StringTable::get_dead_factor() {
 219   return (_uncleaned_items*1.0)/_current_size;
 220 }
 221 
 222 size_t StringTable::table_size(Thread* thread) {
 223   return ((size_t)(1)) << _local_table->get_size_log2(thread != NULL ? thread
 224                                                       : Thread::current());
 225 }
 226 
 227 void StringTable::trigger_concurrent_work() {
 228   MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
 229   the_table()->_has_work = true;
 230   Service_lock->notify_all();
 231 }
 232 
 233 // Probing
 234 oop StringTable::lookup(Symbol* symbol) {
 235   ResourceMark rm;
 236   int length;
 237   jchar* chars = symbol->as_unicode(length);
 238   return lookup(chars, length);
 239 }
 240 
 241 oop StringTable::lookup(jchar* name, int len) {
 242   unsigned int hash = java_lang_String::hash_code(name, len);
 243   oop string = StringTable::the_table()->lookup_shared(name, len, hash);
 244   if (string != NULL) {
 245     return string;
 246   }
 247   if (StringTable::_alt_hash) {
 248     hash = hash_string(name, len, true);
 249   }
 250   return StringTable::the_table()->do_lookup(name, len, hash);
 251 }
 252 
 253 class StringTableGet : public StackObj {
 254   Thread* _thread;
 255   Handle  _return;
 256  public:
 257   StringTableGet(Thread* thread) : _thread(thread) {}
 258   void operator()(WeakHandle<vm_string_table_data>* val) {
 259     oop result = val->resolve();
 260     assert(result != NULL, "Result should be reachable");
 261     _return = Handle(_thread, result);
 262   }
 263   oop get_res_oop() {
 264     return _return();
 265   }
 266 };
 267 
 268 oop StringTable::do_lookup(jchar* name, int len, uintx hash) {
 269   Thread* thread = Thread::current();
 270   StringTableLookupJchar lookup(thread, hash, name, len);
 271   StringTableGet stg(thread);
 272   bool rehash_warning;
 273   _local_table->get(thread, lookup, stg, &rehash_warning);
 274   if (rehash_warning) {
 275     _needs_rehashing = true;
 276   }
 277   return stg.get_res_oop();
 278 }
 279 
 280 // Interning
 281 oop StringTable::intern(Symbol* symbol, TRAPS) {
 282   if (symbol == NULL) return NULL;
 283   ResourceMark rm(THREAD);
 284   int length;
 285   jchar* chars = symbol->as_unicode(length);
 286   Handle string;
 287   oop result = intern(string, chars, length, CHECK_NULL);
 288   return result;
 289 }
 290 
 291 oop StringTable::intern(oop string, TRAPS) {
 292   if (string == NULL) return NULL;
 293   ResourceMark rm(THREAD);
 294   int length;
 295   Handle h_string (THREAD, string);
 296   jchar* chars = java_lang_String::as_unicode_string(string, length,
 297                                                      CHECK_NULL);
 298   oop result = intern(h_string, chars, length, CHECK_NULL);
 299   return result;
 300 }
 301 
 302 oop StringTable::intern(const char* utf8_string, TRAPS) {
 303   if (utf8_string == NULL) return NULL;
 304   ResourceMark rm(THREAD);
 305   int length = UTF8::unicode_length(utf8_string);
 306   jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
 307   UTF8::convert_to_unicode(utf8_string, chars, length);
 308   Handle string;
 309   oop result = intern(string, chars, length, CHECK_NULL);
 310   return result;
 311 }
 312 
 313 oop StringTable::intern(Handle string_or_null_h, jchar* name, int len, TRAPS) {
 314   // shared table always uses java_lang_String::hash_code
 315   unsigned int hash = java_lang_String::hash_code(name, len);
 316   oop found_string = StringTable::the_table()->lookup_shared(name, len, hash);
 317   if (found_string != NULL) {
 318     return found_string;
 319   }
 320   if (StringTable::_alt_hash) {
 321     hash = hash_string(name, len, true);
 322   }
 323   return StringTable::the_table()->do_intern(string_or_null_h, name, len,
 324                                              hash, CHECK_NULL);
 325 }
 326 
 327 class StringTableCreateEntry : public StackObj {
 328  private:
 329    Thread* _thread;
 330    Handle  _return;
 331    Handle  _store;
 332  public:
 333   StringTableCreateEntry(Thread* thread, Handle store)
 334     : _thread(thread), _store(store) {}
 335 
 336   WeakHandle<vm_string_table_data> operator()() { // No dups found
 337     WeakHandle<vm_string_table_data> wh =
 338       WeakHandle<vm_string_table_data>::create(_store);
 339     return wh;
 340   }
 341   void operator()(bool inserted, WeakHandle<vm_string_table_data>* val) {
 342     oop result = val->resolve();
 343     assert(result != NULL, "Result should be reachable");
 344     _return = Handle(_thread, result);
 345   }
 346   oop get_return() const {
 347     return _return();
 348   }
 349 };
 350 














 351 oop StringTable::do_intern(Handle string_or_null_h, jchar* name,
 352                            int len, uintx hash, TRAPS) {
 353   HandleMark hm(THREAD);  // cleanup strings created
 354   Handle string_h;
 355 
 356   if (!string_or_null_h.is_null()) {
 357     string_h = string_or_null_h;
 358   } else {
 359     string_h = java_lang_String::create_from_unicode(name, len, CHECK_NULL);
 360   }
 361 
 362   // Deduplicate the string before it is interned. Note that we should never
 363   // deduplicate a string after it has been interned. Doing so will counteract
 364   // compiler optimizations done on e.g. interned string literals.
 365   Universe::heap()->deduplicate_string(string_h());
 366 
 367   assert(java_lang_String::equals(string_h(), name, len),
 368          "string must be properly initialized");
 369   assert(len == java_lang_String::length(string_h()), "Must be same length");
 370   StringTableLookupOop lookup(THREAD, hash, string_h);
 371   StringTableCreateEntry stc(THREAD, string_h);
 372 
 373   bool rehash_warning;
 374   _local_table->get_insert_lazy(THREAD, lookup, stc, stc, &rehash_warning);
 375   if (rehash_warning) {
 376     _needs_rehashing = true;
 377   }
 378   return stc.get_return();
 379 }
 380 
 381 // GC support




































 382 class StringTableIsAliveCounter : public BoolObjectClosure {
 383   BoolObjectClosure* _real_boc;
 384  public:
 385   size_t _count;
 386   size_t _count_total;
 387   StringTableIsAliveCounter(BoolObjectClosure* boc) : _real_boc(boc), _count(0),
 388                                                       _count_total(0) {}
 389   bool do_object_b(oop obj) {
 390     bool ret = _real_boc->do_object_b(obj);
 391     if (!ret) {
 392       ++_count;
 393     }
 394     ++_count_total;
 395     return ret;
 396   }
 397 };
 398 






 399 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f,
 400                                     int* processed, int* removed) {
 401   DoNothingClosure dnc;
 402   assert(is_alive != NULL, "No closure");
 403   StringTableIsAliveCounter stiac(is_alive);
 404   OopClosure* tmp = f != NULL ? f : &dnc;
 405 
 406   StringTable::the_table()->_weak_handles->weak_oops_do(&stiac, tmp);
 407 
 408   StringTable::the_table()->items_to_clean(stiac._count);
 409   StringTable::the_table()->check_concurrent_work();
 410   if (processed != NULL) {
 411     *processed = (int) stiac._count_total;
 412   }
 413   if (removed != NULL) {
 414     *removed = (int) stiac._count;
 415   }
 416 }
 417 
 418 void StringTable::oops_do(OopClosure* f) {


 427   DoNothingClosure dnc;
 428   assert(cl != NULL, "No closure");
 429   StringTableIsAliveCounter stiac(cl);
 430 
 431   _par_state_string->weak_oops_do(&stiac, &dnc);
 432 
 433   StringTable::the_table()->items_to_clean(stiac._count);
 434   StringTable::the_table()->check_concurrent_work();
 435   *processed = (int) stiac._count_total;
 436   *removed = (int) stiac._count;
 437 }
 438 
 439 void StringTable::possibly_parallel_oops_do(
 440    OopStorage::ParState<false /* concurrent */, false /* const */>*
 441    _par_state_string, OopClosure* f)
 442 {
 443   assert(f != NULL, "No closure");
 444   _par_state_string->oops_do(f);
 445 }
 446 
 447 // Concurrent work























 448 void StringTable::grow(JavaThread* jt) {
 449   StringTableHash::GrowTask gt(_local_table);
 450   if (!gt.prepare(jt)) {
 451     return;
 452   }
 453   log_trace(stringtable)("Started to growed");
 454   {
 455     TraceTime timer("Grow", TRACETIME_LOG(Debug, stringtable, perf));
 456     while(gt.doTask(jt)) {
 457       gt.pause(jt);
 458       {
 459         ThreadBlockInVM tbivm(jt);
 460       }
 461       gt.cont(jt);
 462     }
 463   }
 464   gt.done(jt);
 465   _current_size = table_size(jt);
 466   log_debug(stringtable)("Growed to size:" SIZE_FORMAT, _current_size);
 467 }
 468 
 469 struct StringTableDoDelete : StackObj {
 470   long _count;
 471   StringTableDoDelete() : _count(0) {}
 472   void operator()(WeakHandle<vm_string_table_data>* val) {
 473     ++_count;
 474   }
 475 };
 476 
 477 struct StringTableDeleteCheck : StackObj {
 478   long _count;
 479   long _item;
 480   StringTableDeleteCheck() : _count(0), _item(0) {}
 481   bool operator()(WeakHandle<vm_string_table_data>* val) {
 482     ++_item;
 483     oop tmp = val->peek();
 484     if (tmp == NULL) {
 485       ++_count;
 486       return true;
 487     } else {
 488       return false;
 489     }
 490   }
 491 };
 492 
 493 void StringTable::clean_dead_entries(JavaThread* jt) {
 494   StringTableHash::BulkDeleteTask bdt(_local_table);
 495   if (!bdt.prepare(jt)) {
 496     return;
 497   }
 498 
 499   StringTableDeleteCheck stdc;
 500   StringTableDoDelete stdd;
 501   bool interrupted = false;
 502   {
 503     TraceTime timer("Clean", TRACETIME_LOG(Debug, stringtable, perf));
 504     while(bdt.doTask(jt, stdc, stdd)) {
 505       bdt.pause(jt);
 506       {
 507         ThreadBlockInVM tbivm(jt);
 508       }
 509       if (!bdt.cont(jt)) {
 510         interrupted = true;
 511         break;
 512       }
 513     }
 514   }
 515   if (interrupted) {
 516     _has_work = true;
 517   } else {
 518     bdt.done(jt);
 519   }
 520   log_debug(stringtable)("Cleaned %ld of %ld", stdc._count, stdc._item);
 521 }
 522 
 523 void StringTable::check_concurrent_work() {
 524   if (_has_work) {
 525     return;
 526   }
 527   double load_factor = StringTable::get_load_factor();
 528   double dead_factor = StringTable::get_dead_factor();
 529   // We should clean/resize if we have more dead than alive,
 530   // more items than preferred load factor or
 531   // more dead items than water mark.
 532   if ((dead_factor > load_factor) ||
 533       (load_factor > PREF_AVG_LIST_LEN) ||
 534       (dead_factor > CLEAN_DEAD_HIGH_WATER_MARK)) {
 535     log_debug(stringtable)("Concurrent work triggered, live factor:%g dead factor:%g",
 536                            load_factor, dead_factor);
 537     trigger_concurrent_work();
 538   }
 539 }
 540 
 541 void StringTable::concurrent_work(JavaThread* jt) {
 542   _has_work = false;
 543   double load_factor = get_load_factor();
 544   log_debug(stringtable, perf)("Concurrent work, live factor: %g", load_factor);
 545   // We prefer growing, since that also removes dead items
 546   if (load_factor > PREF_AVG_LIST_LEN && !_local_table->is_max_size_reached()) {
 547     grow(jt);
 548   } else {
 549     clean_dead_entries(jt);
 550   }
 551 }
 552 
 553 void StringTable::do_concurrent_work(JavaThread* jt) {
 554   StringTable::the_table()->concurrent_work(jt);
 555 }
 556 
 557 // Rehash
 558 bool StringTable::do_rehash() {
 559   if (!_local_table->is_safepoint_safe()) {
 560     return false;
 561   }
 562 
 563   // We use max size
 564   StringTableHash* new_table = new StringTableHash(END_SIZE, END_SIZE, REHASH_LEN);
 565   // Use alt hash from now on
 566   _alt_hash = true;
 567   if (!_local_table->try_move_nodes_to(Thread::current(), new_table)) {
 568     _alt_hash = false;
 569     delete new_table;
 570     return false;
 571   }
 572 
 573   // free old table
 574   delete _local_table;
 575   _local_table = new_table;
 576 
 577   return true;


 595     trigger_concurrent_work();
 596     _needs_rehashing = false;
 597     return;
 598   }
 599 
 600   murmur_seed = AltHashing::compute_seed();
 601   {
 602     if (do_rehash()) {
 603       rehashed = true;
 604     } else {
 605       log_info(stringtable)("Resizes in progress rehashing skipped.");
 606     }
 607   }
 608   _needs_rehashing = false;
 609 }
 610 
 611 void StringTable::rehash_table() {
 612   StringTable::the_table()->try_rehash_table();
 613 }
 614 
 615 // Statistics





 616 static int literal_size(oop obj) {
 617   // NOTE: this would over-count if (pre-JDK8)
 618   // java_lang_Class::has_offset_field() is true and the String.value array is
 619   // shared by several Strings. However, starting from JDK8, the String.value
 620   // array is not shared anymore.
 621   if (obj == NULL) {
 622     return 0;
 623   } else if (obj->klass() == SystemDictionary::String_klass()) {
 624     return (obj->size() + java_lang_String::value(obj)->size()) * HeapWordSize;
 625   } else {
 626     return obj->size();
 627   }
 628 }
 629 
 630 struct SizeFunc : StackObj {
 631   size_t operator()(WeakHandle<vm_string_table_data>* val) {
 632     oop s = val->peek();
 633     if (s == NULL) {
 634       // Dead
 635       return 0;
 636     }
 637     return literal_size(s);
 638   };
 639 };
 640 
 641 void StringTable::print_table_statistics(outputStream* st,
 642                                          const char* table_name) {
 643   SizeFunc sz;
 644   _local_table->statistics_to(Thread::current(), sz, st, table_name);
 645 }
 646 
 647 // Verification
 648 class VerifyStrings : StackObj {
 649  public:
 650   bool operator()(WeakHandle<vm_string_table_data>* val) {
 651     oop s = val->peek();
 652     if (s != NULL) {
 653       assert(java_lang_String::length(s) >= 0, "Length on string must work.");
 654     }
 655     return true;
 656   };
 657 };
 658 
 659 // This verification is part of Universe::verify() and needs to be quick.
 660 void StringTable::verify() {
 661   Thread* thr = Thread::current();
 662   VerifyStrings vs;
 663   if (!the_table()->_local_table->try_scan(thr, vs)) {
 664     log_info(stringtable)("verify unavailable at this moment");
 665   }
 666 }
 667 
 668 // Verification and comp
 669 class VerifyCompStrings : StackObj {
 670   GrowableArray<oop>* _oops;
 671  public:
 672   size_t _errors;
 673   VerifyCompStrings(GrowableArray<oop>* oops) : _oops(oops), _errors(0) {}
 674   bool operator()(WeakHandle<vm_string_table_data>* val) {
 675     oop s = val->resolve();
 676     if (s == NULL) {
 677       return true;
 678     }
 679     int len = _oops->length();
 680     for (int i = 0; i < len; i++) {
 681       bool eq = java_lang_String::equals(s, _oops->at(i));
 682       assert(!eq, "Duplicate strings");
 683       if (eq) {
 684         _errors++;
 685       }
 686     }
 687     _oops->push(s);
 688     return true;
 689   };
 690 };
 691 
 692 size_t StringTable::verify_and_compare_entries() {
 693   Thread* thr = Thread::current();
 694   GrowableArray<oop>* oops =
 695     new (ResourceObj::C_HEAP, mtInternal)
 696       GrowableArray<oop>((int)the_table()->_current_size, true);
 697 
 698   VerifyCompStrings vcs(oops);
 699   if (!the_table()->_local_table->try_scan(thr, vcs)) {
 700     log_info(stringtable)("verify unavailable at this moment");
 701   }
 702   delete oops;
 703   return vcs._errors;
 704 }
 705 
 706 // Dumping
 707 class PrintString : StackObj {
 708   Thread* _thr;
 709   outputStream* _st;
 710  public:
 711   PrintString(Thread* thr, outputStream* st) : _thr(thr), _st(st) {}
 712   bool operator()(WeakHandle<vm_string_table_data>* val) {
 713     oop s = val->peek();
 714     if (s == NULL) {
 715       return true;
 716     }
 717     typeArrayOop value     = java_lang_String::value_no_keepalive(s);
 718     int          length    = java_lang_String::length(s);
 719     bool         is_latin1 = java_lang_String::is_latin1(s);
 720 
 721     if (length <= 0) {
 722       _st->print("%d: ", length);
 723     } else {
 724       ResourceMark rm(_thr);
 725       int utf8_length = length;
 726       char* utf8_string;
 727 


 738     }
 739     _st->cr();
 740     return true;
 741   };
 742 };
 743 
 744 void StringTable::dump(outputStream* st, bool verbose) {
 745   if (!verbose) {
 746     the_table()->print_table_statistics(st, "StringTable");
 747   } else {
 748     Thread* thr = Thread::current();
 749     ResourceMark rm(thr);
 750     st->print_cr("VERSION: 1.1");
 751     PrintString ps(thr, st);
 752     if (!the_table()->_local_table->try_scan(thr, ps)) {
 753       st->print_cr("dump unavailable at this moment");
 754     }
 755   }
 756 }
 757 




















 758 // Utility for dumping strings
 759 StringtableDCmd::StringtableDCmd(outputStream* output, bool heap) :
 760                                  DCmdWithParser(output, heap),
 761   _verbose("-verbose", "Dump the content of each string in the table",
 762            "BOOLEAN", false, "false") {
 763   _dcmdparser.add_dcmd_option(&_verbose);
 764 }
 765 
 766 void StringtableDCmd::execute(DCmdSource source, TRAPS) {
 767   VM_DumpHashtable dumper(output(), VM_DumpHashtable::DumpStrings,
 768                          _verbose.value());
 769   VMThread::execute(&dumper);
 770 }
 771 
 772 int StringtableDCmd::num_arguments() {
 773   ResourceMark rm;
 774   StringtableDCmd* dcmd = new StringtableDCmd(NULL, false);
 775   if (dcmd != NULL) {
 776     DCmdMark mark(dcmd);
 777     return dcmd->_dcmdparser.num_arguments();
 778   } else {
 779     return 0;
 780   }
 781 }
 782 

 783 // Sharing
 784 #if INCLUDE_CDS_JAVA_HEAP
 785 oop StringTable::lookup_shared(jchar* name, int len, unsigned int hash) {
 786   assert(hash == java_lang_String::hash_code(name, len),
 787          "hash must be computed using java_lang_String::hash_code");
 788   return _shared_table.lookup((const char*)name, hash, len);
 789 }
 790 
 791 oop StringTable::create_archived_string(oop s, Thread* THREAD) {
 792   assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
 793 
 794   oop new_s = NULL;
 795   typeArrayOop v = java_lang_String::value_no_keepalive(s);
 796   typeArrayOop new_v =
 797     (typeArrayOop)MetaspaceShared::archive_heap_object(v, THREAD);
 798   if (new_v == NULL) {
 799     return NULL;
 800   }
 801   new_s = MetaspaceShared::archive_heap_object(s, THREAD);
 802   if (new_s == NULL) {
 803     return NULL;
 804   }
 805 
 806   // adjust the pointer to the 'value' field in the new String oop
 807   java_lang_String::set_value_raw(new_s, new_v);
 808   return new_s;
 809 }
 810 
 811 struct CopyToArchive : StackObj {
 812   CompactStringTableWriter* _writer;
 813   CopyToArchive(CompactStringTableWriter* writer) : _writer(writer) {}
 814   bool operator()(WeakHandle<vm_string_table_data>* val) {
 815     oop s = val->peek();
 816     if (s == NULL) {
 817       return true;
 818     }
 819     unsigned int hash = java_lang_String::hash_code(s);
 820     if (hash == 0) {
 821       return true;
 822     }
 823 
 824     java_lang_String::set_hash(s, hash);
 825     oop new_s = StringTable::create_archived_string(s, Thread::current());
 826     if (new_s == NULL) {
 827       return true;
 828     }
 829 
 830     val->replace(new_s);
 831     // add to the compact table
 832     _writer->add(hash, new_s);
 833     return true;
 834   }
 835 };
 836 
 837 void StringTable::copy_shared_string_table(CompactStringTableWriter* writer) {
 838   assert(MetaspaceShared::is_heap_object_archiving_allowed(), "must be");
 839 
 840   CopyToArchive copy(writer);
 841   StringTable::the_table()->_local_table->do_scan(Thread::current(), copy);
 842 }
 843 
 844 void StringTable::write_to_archive() {
 845   assert(MetaspaceShared::is_heap_object_archiving_allowed(), "must be");
 846 
 847   _shared_table.reset();
 848   int num_buckets = the_table()->_items / SharedSymbolTableBucketSize;
 849   // calculation of num_buckets can result in zero buckets, we need at least one
 850   CompactStringTableWriter writer(num_buckets > 1 ? num_buckets : 1,
 851                                   &MetaspaceShared::stats()->string);
 852 
 853   // Copy the interned strings into the "string space" within the java heap
 854   copy_shared_string_table(&writer);
 855   writer.dump(&_shared_table);
 856 }
 857 
 858 void StringTable::serialize(SerializeClosure* soc) {
 859   _shared_table.set_type(CompactHashtable<oop, char>::_string_table);
 860   _shared_table.serialize(soc);
 861 
 862   if (soc->writing()) {
 863     // Sanity. Make sure we don't use the shared table at dump time
 864     _shared_table.reset();
 865   } else if (!_shared_string_mapped) {
 866     _shared_table.reset();
 867   }
 868 }
 869 
 870 void StringTable::shared_oops_do(OopClosure* f) {
 871   _shared_table.oops_do(f);
 872 }

 873 #endif //INCLUDE_CDS_JAVA_HEAP
< prev index next >