1 /*
   2  * Copyright (c) 1997, 2018, 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.inline.hpp"
  29 #include "classfile/stringTable.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "gc/shared/collectedHeap.hpp"
  32 #include "gc/shared/oopStorage.inline.hpp"
  33 #include "gc/shared/oopStorageParState.inline.hpp"
  34 #include "logging/log.hpp"
  35 #include "logging/logStream.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/filemap.hpp"
  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 }
  80 
  81 class StringTableConfig : public StringTableHash::BaseConfig {
  82  private:
  83  public:
  84   static uintx get_hash(WeakHandle<vm_string_table_data> const& value,
  85                         bool* is_dead) {
  86     EXCEPTION_MARK;
  87     oop val_oop = value.peek();
  88     if (val_oop == NULL) {
  89       *is_dead = true;
  90       return 0;
  91     }
  92     *is_dead = false;
  93     ResourceMark rm(THREAD);
  94     // All String oops are hashed as unicode
  95     int length;
  96     jchar* chars = java_lang_String::as_unicode_string(val_oop, length, THREAD);
  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;
 138     }
 139     bool equals = java_lang_String::equals(val_oop, (jchar*)_str, _len);
 140     if (!equals) {
 141       return false;
 142     }
 143     // Need to resolve weak handle and Handleize through possible safepoint.
 144      _found = Handle(_thread, value->resolve());
 145     return true;
 146   }
 147 };
 148 
 149 class StringTableLookupOop : public StackObj {
 150  private:
 151   Thread* _thread;
 152   uintx _hash;
 153   Handle _find;
 154   Handle _found;  // Might be a different oop with the same value that's already
 155                   // in the table, which is the point.
 156  public:
 157   StringTableLookupOop(Thread* thread, uintx hash, Handle handle)
 158     : _thread(thread), _hash(hash), _find(handle) { }
 159 
 160   uintx get_hash() const {
 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) {
 409   assert(f != NULL, "No closure");
 410   StringTable::the_table()->_weak_handles->oops_do(f);
 411 }
 412 
 413 void StringTable::possibly_parallel_unlink(
 414    OopStorage::ParState<false, false>* _par_state_string, BoolObjectClosure* cl,
 415    int* processed, int* removed)
 416 {
 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;
 566 }
 567 
 568 void StringTable::try_rehash_table() {
 569   static bool rehashed = false;
 570   log_debug(stringtable)("Table imbalanced, rehashing called.");
 571 
 572   // Grow instead of rehash.
 573   if (get_load_factor() > PREF_AVG_LIST_LEN &&
 574       !_local_table->is_max_size_reached()) {
 575     log_debug(stringtable)("Choosing growing over rehashing.");
 576     trigger_concurrent_work();
 577     _needs_rehashing = false;
 578     return;
 579   }
 580   // Already rehashed.
 581   if (rehashed) {
 582     log_warning(stringtable)("Rehashing already done, still long lists.");
 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 
 661       if (!is_latin1) {
 662         jchar* chars = value->char_at_addr(0);
 663         utf8_string = UNICODE::as_utf8(chars, utf8_length);
 664       } else {
 665         jbyte* bytes = value->byte_at_addr(0);
 666         utf8_string = UNICODE::as_utf8(bytes, utf8_length);
 667       }
 668 
 669       _st->print("%d: ", utf8_length);
 670       HashtableTextDump::put_utf8(_st, utf8_string, utf8_length);
 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