1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/altHashing.hpp" 27 #include "classfile/compactHashtable.inline.hpp" 28 #include "classfile/javaClasses.inline.hpp" 29 #include "classfile/stringTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "gc/shared/collectedHeap.inline.hpp" 32 #include "gc/shared/gcLocker.inline.hpp" 33 #include "logging/log.hpp" 34 #include "memory/allocation.inline.hpp" 35 #include "memory/filemap.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "oops/oop.inline.hpp" 38 #include "runtime/atomic.hpp" 39 #include "runtime/mutexLocker.hpp" 40 #include "utilities/hashtable.inline.hpp" 41 #include "utilities/macros.hpp" 42 #if INCLUDE_ALL_GCS 43 #include "gc/g1/g1CollectedHeap.hpp" 44 #include "gc/g1/g1SATBCardTableModRefBS.hpp" 45 #include "gc/g1/g1StringDedup.hpp" 46 #endif 47 48 // the number of buckets a thread claims 49 const int ClaimChunkSize = 32; 50 51 #ifdef ASSERT 52 class StableMemoryChecker : public StackObj { 53 enum { _bufsize = wordSize*4 }; 54 55 address _region; 56 jint _size; 57 u1 _save_buf[_bufsize]; 58 59 int sample(u1* save_buf) { 60 if (_size <= _bufsize) { 61 memcpy(save_buf, _region, _size); 62 return _size; 63 } else { 64 // copy head and tail 65 memcpy(&save_buf[0], _region, _bufsize/2); 66 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2); 67 return (_bufsize/2)*2; 68 } 69 } 70 71 public: 72 StableMemoryChecker(const void* region, jint size) { 73 _region = (address) region; 74 _size = size; 75 sample(_save_buf); 76 } 77 78 bool verify() { 79 u1 check_buf[sizeof(_save_buf)]; 80 int check_size = sample(check_buf); 81 return (0 == memcmp(_save_buf, check_buf, check_size)); 82 } 83 84 void set_region(const void* region) { _region = (address) region; } 85 }; 86 #endif 87 88 89 // -------------------------------------------------------------------------- 90 StringTable* StringTable::_the_table = NULL; 91 bool StringTable::_ignore_shared_strings = false; 92 bool StringTable::_needs_rehashing = false; 93 94 volatile int StringTable::_parallel_claimed_idx = 0; 95 96 CompactHashtable<oop, char> StringTable::_shared_table; 97 98 // Pick hashing algorithm 99 unsigned int StringTable::hash_string(const jchar* s, int len) { 100 return use_alternate_hashcode() ? alt_hash_string(s, len) : 101 java_lang_String::hash_code(s, len); 102 } 103 104 unsigned int StringTable::alt_hash_string(const jchar* s, int len) { 105 return AltHashing::murmur3_32(seed(), s, len); 106 } 107 108 unsigned int StringTable::hash_string(oop string) { 109 EXCEPTION_MARK; 110 if (string == NULL) { 111 return hash_string((jchar*)NULL, 0); 112 } 113 ResourceMark rm(THREAD); 114 // All String oops are hashed as unicode 115 int length; 116 jchar* chars = java_lang_String::as_unicode_string(string, length, THREAD); 117 if (chars != NULL) { 118 return hash_string(chars, length); 119 } else { 120 vm_exit_out_of_memory(length, OOM_MALLOC_ERROR, "unable to create Unicode string for verification"); 121 return 0; 122 } 123 } 124 125 oop StringTable::lookup_shared(jchar* name, int len, unsigned int hash) { 126 assert(hash == java_lang_String::hash_code(name, len), 127 "hash must be computed using java_lang_String::hash_code"); 128 return _shared_table.lookup((const char*)name, hash, len); 129 } 130 131 oop StringTable::lookup_in_main_table(int index, jchar* name, 132 int len, unsigned int hash) { 133 int count = 0; 134 for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) { 135 count++; 136 if (l->hash() == hash) { 137 if (java_lang_String::equals(l->literal(), name, len)) { 138 return l->literal(); 139 } 140 } 141 } 142 // If the bucket size is too deep check if this hash code is insufficient. 143 if (count >= rehash_count && !needs_rehashing()) { 144 _needs_rehashing = check_rehash_table(count); 145 } 146 return NULL; 147 } 148 149 150 oop StringTable::basic_add(int index_arg, Handle string, jchar* name, 151 int len, unsigned int hashValue_arg, TRAPS) { 152 153 assert(java_lang_String::equals(string(), name, len), 154 "string must be properly initialized"); 155 // Cannot hit a safepoint in this function because the "this" pointer can move. 156 NoSafepointVerifier nsv; 157 158 // Check if the symbol table has been rehashed, if so, need to recalculate 159 // the hash value and index before second lookup. 160 unsigned int hashValue; 161 int index; 162 if (use_alternate_hashcode()) { 163 hashValue = alt_hash_string(name, len); 164 index = hash_to_index(hashValue); 165 } else { 166 hashValue = hashValue_arg; 167 index = index_arg; 168 } 169 170 // Since look-up was done lock-free, we need to check if another 171 // thread beat us in the race to insert the symbol. 172 173 // No need to lookup the shared table from here since the caller (intern()) already did 174 oop test = lookup_in_main_table(index, name, len, hashValue); // calls lookup(u1*, int) 175 if (test != NULL) { 176 // Entry already added 177 return test; 178 } 179 180 HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string()); 181 add_entry(index, entry); 182 return string(); 183 } 184 185 186 oop StringTable::lookup(Symbol* symbol) { 187 ResourceMark rm; 188 int length; 189 jchar* chars = symbol->as_unicode(length); 190 return lookup(chars, length); 191 } 192 193 // Tell the GC that this string was looked up in the StringTable. 194 static void ensure_string_alive(oop string) { 195 // A lookup in the StringTable could return an object that was previously 196 // considered dead. The SATB part of G1 needs to get notified about this 197 // potential resurrection, otherwise the marking might not find the object. 198 #if INCLUDE_ALL_GCS 199 if (UseG1GC && string != NULL) { 200 G1SATBCardTableModRefBS::enqueue(string); 201 } 202 #endif 203 } 204 205 oop StringTable::lookup(jchar* name, int len) { 206 // shared table always uses java_lang_String::hash_code 207 unsigned int hash = java_lang_String::hash_code(name, len); 208 oop string = lookup_shared(name, len, hash); 209 if (string != NULL) { 210 return string; 211 } 212 if (use_alternate_hashcode()) { 213 hash = alt_hash_string(name, len); 214 } 215 int index = the_table()->hash_to_index(hash); 216 string = the_table()->lookup_in_main_table(index, name, len, hash); 217 218 ensure_string_alive(string); 219 220 return string; 221 } 222 223 oop StringTable::intern(Handle string_or_null, jchar* name, 224 int len, TRAPS) { 225 // shared table always uses java_lang_String::hash_code 226 unsigned int hashValue = java_lang_String::hash_code(name, len); 227 oop found_string = lookup_shared(name, len, hashValue); 228 if (found_string != NULL) { 229 return found_string; 230 } 231 if (use_alternate_hashcode()) { 232 hashValue = alt_hash_string(name, len); 233 } 234 int index = the_table()->hash_to_index(hashValue); 235 found_string = the_table()->lookup_in_main_table(index, name, len, hashValue); 236 237 // Found 238 if (found_string != NULL) { 239 if (found_string != string_or_null()) { 240 ensure_string_alive(found_string); 241 } 242 return found_string; 243 } 244 245 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); 246 assert(!Universe::heap()->is_in_reserved(name), 247 "proposed name of symbol must be stable"); 248 249 HandleMark hm(THREAD); // cleanup strings created 250 Handle string; 251 // try to reuse the string if possible 252 if (!string_or_null.is_null()) { 253 string = string_or_null; 254 } else { 255 string = java_lang_String::create_from_unicode(name, len, CHECK_NULL); 256 } 257 258 #if INCLUDE_ALL_GCS 259 if (G1StringDedup::is_enabled()) { 260 // Deduplicate the string before it is interned. Note that we should never 261 // deduplicate a string after it has been interned. Doing so will counteract 262 // compiler optimizations done on e.g. interned string literals. 263 G1StringDedup::deduplicate(string()); 264 } 265 #endif 266 267 // Grab the StringTable_lock before getting the_table() because it could 268 // change at safepoint. 269 oop added_or_found; 270 { 271 MutexLocker ml(StringTable_lock, THREAD); 272 // Otherwise, add to symbol to table 273 added_or_found = the_table()->basic_add(index, string, name, len, 274 hashValue, CHECK_NULL); 275 } 276 277 if (added_or_found != string()) { 278 ensure_string_alive(added_or_found); 279 } 280 281 return added_or_found; 282 } 283 284 oop StringTable::intern(Symbol* symbol, TRAPS) { 285 if (symbol == NULL) return NULL; 286 ResourceMark rm(THREAD); 287 int length; 288 jchar* chars = symbol->as_unicode(length); 289 Handle string; 290 oop result = intern(string, chars, length, CHECK_NULL); 291 return result; 292 } 293 294 295 oop StringTable::intern(oop string, TRAPS) 296 { 297 if (string == NULL) return NULL; 298 ResourceMark rm(THREAD); 299 int length; 300 Handle h_string (THREAD, string); 301 jchar* chars = java_lang_String::as_unicode_string(string, length, CHECK_NULL); 302 oop result = intern(h_string, chars, length, CHECK_NULL); 303 return result; 304 } 305 306 307 oop StringTable::intern(const char* utf8_string, TRAPS) { 308 if (utf8_string == NULL) return NULL; 309 ResourceMark rm(THREAD); 310 int length = UTF8::unicode_length(utf8_string); 311 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length); 312 UTF8::convert_to_unicode(utf8_string, chars, length); 313 Handle string; 314 oop result = intern(string, chars, length, CHECK_NULL); 315 return result; 316 } 317 318 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) { 319 BucketUnlinkContext context; 320 buckets_unlink_or_oops_do(is_alive, f, 0, the_table()->table_size(), &context); 321 _the_table->bulk_free_entries(&context); 322 *processed = context._num_processed; 323 *removed = context._num_removed; 324 } 325 326 void StringTable::possibly_parallel_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) { 327 // Readers of the table are unlocked, so we should only be removing 328 // entries at a safepoint. 329 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 330 const int limit = the_table()->table_size(); 331 332 BucketUnlinkContext context; 333 for (;;) { 334 // Grab next set of buckets to scan 335 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; 336 if (start_idx >= limit) { 337 // End of table 338 break; 339 } 340 341 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); 342 buckets_unlink_or_oops_do(is_alive, f, start_idx, end_idx, &context); 343 } 344 _the_table->bulk_free_entries(&context); 345 *processed = context._num_processed; 346 *removed = context._num_removed; 347 } 348 349 void StringTable::buckets_oops_do(OopClosure* f, int start_idx, int end_idx) { 350 const int limit = the_table()->table_size(); 351 352 assert(0 <= start_idx && start_idx <= limit, 353 "start_idx (%d) is out of bounds", start_idx); 354 assert(0 <= end_idx && end_idx <= limit, 355 "end_idx (%d) is out of bounds", end_idx); 356 assert(start_idx <= end_idx, 357 "Index ordering: start_idx=%d, end_idx=%d", 358 start_idx, end_idx); 359 360 for (int i = start_idx; i < end_idx; i += 1) { 361 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i); 362 while (entry != NULL) { 363 assert(!entry->is_shared(), "CDS not used for the StringTable"); 364 365 f->do_oop((oop*)entry->literal_addr()); 366 367 entry = entry->next(); 368 } 369 } 370 } 371 372 void StringTable::buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, BucketUnlinkContext* context) { 373 const int limit = the_table()->table_size(); 374 375 assert(0 <= start_idx && start_idx <= limit, 376 "start_idx (%d) is out of bounds", start_idx); 377 assert(0 <= end_idx && end_idx <= limit, 378 "end_idx (%d) is out of bounds", end_idx); 379 assert(start_idx <= end_idx, 380 "Index ordering: start_idx=%d, end_idx=%d", 381 start_idx, end_idx); 382 383 for (int i = start_idx; i < end_idx; ++i) { 384 HashtableEntry<oop, mtSymbol>** p = the_table()->bucket_addr(i); 385 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i); 386 while (entry != NULL) { 387 assert(!entry->is_shared(), "CDS not used for the StringTable"); 388 389 if (is_alive->do_object_b(entry->literal())) { 390 if (f != NULL) { 391 f->do_oop((oop*)entry->literal_addr()); 392 } 393 p = entry->next_addr(); 394 } else { 395 *p = entry->next(); 396 context->free_entry(entry); 397 } 398 context->_num_processed++; 399 entry = *p; 400 } 401 } 402 } 403 404 void StringTable::oops_do(OopClosure* f) { 405 buckets_oops_do(f, 0, the_table()->table_size()); 406 } 407 408 void StringTable::possibly_parallel_oops_do(OopClosure* f) { 409 const int limit = the_table()->table_size(); 410 411 for (;;) { 412 // Grab next set of buckets to scan 413 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; 414 if (start_idx >= limit) { 415 // End of table 416 break; 417 } 418 419 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); 420 buckets_oops_do(f, start_idx, end_idx); 421 } 422 } 423 424 // This verification is part of Universe::verify() and needs to be quick. 425 // See StringTable::verify_and_compare() below for exhaustive verification. 426 void StringTable::verify() { 427 for (int i = 0; i < the_table()->table_size(); ++i) { 428 HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i); 429 for ( ; p != NULL; p = p->next()) { 430 oop s = p->literal(); 431 guarantee(s != NULL, "interned string is NULL"); 432 unsigned int h = hash_string(s); 433 guarantee(p->hash() == h, "broken hash in string table entry"); 434 guarantee(the_table()->hash_to_index(h) == i, 435 "wrong index in string table"); 436 } 437 } 438 } 439 440 void StringTable::dump(outputStream* st, bool verbose) { 441 if (!verbose) { 442 the_table()->dump_table(st, "StringTable"); 443 } else { 444 Thread* THREAD = Thread::current(); 445 st->print_cr("VERSION: 1.1"); 446 for (int i = 0; i < the_table()->table_size(); ++i) { 447 HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i); 448 for ( ; p != NULL; p = p->next()) { 449 oop s = p->literal(); 450 typeArrayOop value = java_lang_String::value(s); 451 int length = java_lang_String::length(s); 452 bool is_latin1 = java_lang_String::is_latin1(s); 453 454 if (length <= 0) { 455 st->print("%d: ", length); 456 } else { 457 ResourceMark rm(THREAD); 458 int utf8_length = length; 459 char* utf8_string; 460 461 if (!is_latin1) { 462 jchar* chars = value->char_at_addr(0); 463 utf8_string = UNICODE::as_utf8(chars, utf8_length); 464 } else { 465 jbyte* bytes = value->byte_at_addr(0); 466 utf8_string = UNICODE::as_utf8(bytes, utf8_length); 467 } 468 469 st->print("%d: ", utf8_length); 470 HashtableTextDump::put_utf8(st, utf8_string, utf8_length); 471 } 472 st->cr(); 473 } 474 } 475 } 476 } 477 478 StringTable::VerifyRetTypes StringTable::compare_entries( 479 int bkt1, int e_cnt1, 480 HashtableEntry<oop, mtSymbol>* e_ptr1, 481 int bkt2, int e_cnt2, 482 HashtableEntry<oop, mtSymbol>* e_ptr2) { 483 // These entries are sanity checked by verify_and_compare_entries() 484 // before this function is called. 485 oop str1 = e_ptr1->literal(); 486 oop str2 = e_ptr2->literal(); 487 488 if (str1 == str2) { 489 tty->print_cr("ERROR: identical oop values (0x" PTR_FORMAT ") " 490 "in entry @ bucket[%d][%d] and entry @ bucket[%d][%d]", 491 p2i(str1), bkt1, e_cnt1, bkt2, e_cnt2); 492 return _verify_fail_continue; 493 } 494 495 if (java_lang_String::equals(str1, str2)) { 496 tty->print_cr("ERROR: identical String values in entry @ " 497 "bucket[%d][%d] and entry @ bucket[%d][%d]", 498 bkt1, e_cnt1, bkt2, e_cnt2); 499 return _verify_fail_continue; 500 } 501 502 return _verify_pass; 503 } 504 505 StringTable::VerifyRetTypes StringTable::verify_entry(int bkt, int e_cnt, 506 HashtableEntry<oop, mtSymbol>* e_ptr, 507 StringTable::VerifyMesgModes mesg_mode) { 508 509 VerifyRetTypes ret = _verify_pass; // be optimistic 510 511 oop str = e_ptr->literal(); 512 if (str == NULL) { 513 if (mesg_mode == _verify_with_mesgs) { 514 tty->print_cr("ERROR: NULL oop value in entry @ bucket[%d][%d]", bkt, 515 e_cnt); 516 } 517 // NULL oop means no more verifications are possible 518 return _verify_fail_done; 519 } 520 521 if (str->klass() != SystemDictionary::String_klass()) { 522 if (mesg_mode == _verify_with_mesgs) { 523 tty->print_cr("ERROR: oop is not a String in entry @ bucket[%d][%d]", 524 bkt, e_cnt); 525 } 526 // not a String means no more verifications are possible 527 return _verify_fail_done; 528 } 529 530 unsigned int h = hash_string(str); 531 if (e_ptr->hash() != h) { 532 if (mesg_mode == _verify_with_mesgs) { 533 tty->print_cr("ERROR: broken hash value in entry @ bucket[%d][%d], " 534 "bkt_hash=%d, str_hash=%d", bkt, e_cnt, e_ptr->hash(), h); 535 } 536 ret = _verify_fail_continue; 537 } 538 539 if (the_table()->hash_to_index(h) != bkt) { 540 if (mesg_mode == _verify_with_mesgs) { 541 tty->print_cr("ERROR: wrong index value for entry @ bucket[%d][%d], " 542 "str_hash=%d, hash_to_index=%d", bkt, e_cnt, h, 543 the_table()->hash_to_index(h)); 544 } 545 ret = _verify_fail_continue; 546 } 547 548 return ret; 549 } 550 551 // See StringTable::verify() above for the quick verification that is 552 // part of Universe::verify(). This verification is exhaustive and 553 // reports on every issue that is found. StringTable::verify() only 554 // reports on the first issue that is found. 555 // 556 // StringTable::verify_entry() checks: 557 // - oop value != NULL (same as verify()) 558 // - oop value is a String 559 // - hash(String) == hash in entry (same as verify()) 560 // - index for hash == index of entry (same as verify()) 561 // 562 // StringTable::compare_entries() checks: 563 // - oops are unique across all entries 564 // - String values are unique across all entries 565 // 566 int StringTable::verify_and_compare_entries() { 567 assert(StringTable_lock->is_locked(), "sanity check"); 568 569 int fail_cnt = 0; 570 571 // first, verify all the entries individually: 572 for (int bkt = 0; bkt < the_table()->table_size(); bkt++) { 573 HashtableEntry<oop, mtSymbol>* e_ptr = the_table()->bucket(bkt); 574 for (int e_cnt = 0; e_ptr != NULL; e_ptr = e_ptr->next(), e_cnt++) { 575 VerifyRetTypes ret = verify_entry(bkt, e_cnt, e_ptr, _verify_with_mesgs); 576 if (ret != _verify_pass) { 577 fail_cnt++; 578 } 579 } 580 } 581 582 // Optimization: if the above check did not find any failures, then 583 // the comparison loop below does not need to call verify_entry() 584 // before calling compare_entries(). If there were failures, then we 585 // have to call verify_entry() to see if the entry can be passed to 586 // compare_entries() safely. When we call verify_entry() in the loop 587 // below, we do so quietly to void duplicate messages and we don't 588 // increment fail_cnt because the failures have already been counted. 589 bool need_entry_verify = (fail_cnt != 0); 590 591 // second, verify all entries relative to each other: 592 for (int bkt1 = 0; bkt1 < the_table()->table_size(); bkt1++) { 593 HashtableEntry<oop, mtSymbol>* e_ptr1 = the_table()->bucket(bkt1); 594 for (int e_cnt1 = 0; e_ptr1 != NULL; e_ptr1 = e_ptr1->next(), e_cnt1++) { 595 if (need_entry_verify) { 596 VerifyRetTypes ret = verify_entry(bkt1, e_cnt1, e_ptr1, 597 _verify_quietly); 598 if (ret == _verify_fail_done) { 599 // cannot use the current entry to compare against other entries 600 continue; 601 } 602 } 603 604 for (int bkt2 = bkt1; bkt2 < the_table()->table_size(); bkt2++) { 605 HashtableEntry<oop, mtSymbol>* e_ptr2 = the_table()->bucket(bkt2); 606 int e_cnt2; 607 for (e_cnt2 = 0; e_ptr2 != NULL; e_ptr2 = e_ptr2->next(), e_cnt2++) { 608 if (bkt1 == bkt2 && e_cnt2 <= e_cnt1) { 609 // skip the entries up to and including the one that 610 // we're comparing against 611 continue; 612 } 613 614 if (need_entry_verify) { 615 VerifyRetTypes ret = verify_entry(bkt2, e_cnt2, e_ptr2, 616 _verify_quietly); 617 if (ret == _verify_fail_done) { 618 // cannot compare against this entry 619 continue; 620 } 621 } 622 623 // compare two entries, report and count any failures: 624 if (compare_entries(bkt1, e_cnt1, e_ptr1, bkt2, e_cnt2, e_ptr2) 625 != _verify_pass) { 626 fail_cnt++; 627 } 628 } 629 } 630 } 631 } 632 return fail_cnt; 633 } 634 635 // Create a new table and using alternate hash code, populate the new table 636 // with the existing strings. Set flag to use the alternate hash code afterwards. 637 void StringTable::rehash_table() { 638 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 639 // This should never happen with -Xshare:dump but it might in testing mode. 640 if (DumpSharedSpaces) return; 641 StringTable* new_table = new StringTable(); 642 643 // Rehash the table 644 the_table()->move_to(new_table); 645 646 // Delete the table and buckets (entries are reused in new table). 647 delete _the_table; 648 // Don't check if we need rehashing until the table gets unbalanced again. 649 // Then rehash with a new global seed. 650 _needs_rehashing = false; 651 _the_table = new_table; 652 } 653 654 // Utility for dumping strings 655 StringtableDCmd::StringtableDCmd(outputStream* output, bool heap) : 656 DCmdWithParser(output, heap), 657 _verbose("-verbose", "Dump the content of each string in the table", 658 "BOOLEAN", false, "false") { 659 _dcmdparser.add_dcmd_option(&_verbose); 660 } 661 662 void StringtableDCmd::execute(DCmdSource source, TRAPS) { 663 VM_DumpHashtable dumper(output(), VM_DumpHashtable::DumpStrings, 664 _verbose.value()); 665 VMThread::execute(&dumper); 666 } 667 668 int StringtableDCmd::num_arguments() { 669 ResourceMark rm; 670 StringtableDCmd* dcmd = new StringtableDCmd(NULL, false); 671 if (dcmd != NULL) { 672 DCmdMark mark(dcmd); 673 return dcmd->_dcmdparser.num_arguments(); 674 } else { 675 return 0; 676 } 677 } 678 679 // Sharing 680 bool StringTable::copy_shared_string(GrowableArray<MemRegion> *string_space, 681 CompactStringTableWriter* writer) { 682 #if INCLUDE_CDS && INCLUDE_ALL_GCS && defined(_LP64) && !defined(_WINDOWS) 683 assert(UseG1GC, "Only support G1 GC"); 684 assert(UseCompressedOops && UseCompressedClassPointers, 685 "Only support UseCompressedOops and UseCompressedClassPointers enabled"); 686 687 Thread* THREAD = Thread::current(); 688 G1CollectedHeap::heap()->begin_archive_alloc_range(); 689 for (int i = 0; i < the_table()->table_size(); ++i) { 690 HashtableEntry<oop, mtSymbol>* bucket = the_table()->bucket(i); 691 for ( ; bucket != NULL; bucket = bucket->next()) { 692 oop s = bucket->literal(); 693 unsigned int hash = java_lang_String::hash_code(s); 694 if (hash == 0) { 695 continue; 696 } 697 698 // allocate the new 'value' array first 699 typeArrayOop v = java_lang_String::value(s); 700 int v_len = v->size(); 701 typeArrayOop new_v; 702 if (G1CollectedHeap::heap()->is_archive_alloc_too_large(v_len)) { 703 continue; // skip the current String. The 'value' array is too large to handle 704 } else { 705 new_v = (typeArrayOop)G1CollectedHeap::heap()->archive_mem_allocate(v_len); 706 if (new_v == NULL) { 707 return false; // allocation failed 708 } 709 } 710 // now allocate the new String object 711 int s_len = s->size(); 712 oop new_s = (oop)G1CollectedHeap::heap()->archive_mem_allocate(s_len); 713 if (new_s == NULL) { 714 return false; 715 } 716 717 s->identity_hash(); 718 v->identity_hash(); 719 720 // copy the objects' data 721 Copy::aligned_disjoint_words((HeapWord*)s, (HeapWord*)new_s, s_len); 722 Copy::aligned_disjoint_words((HeapWord*)v, (HeapWord*)new_v, v_len); 723 724 // adjust the pointer to the 'value' field in the new String oop. Also pre-compute and set the 725 // 'hash' field. That avoids "write" to the shared strings at runtime by the deduplication process. 726 java_lang_String::set_value_raw(new_s, new_v); 727 if (java_lang_String::hash(new_s) == 0) { 728 java_lang_String::set_hash(new_s, hash); 729 } 730 731 // add to the compact table 732 writer->add(hash, new_s); 733 } 734 } 735 736 G1CollectedHeap::heap()->end_archive_alloc_range(string_space, os::vm_allocation_granularity()); 737 assert(string_space->length() <= 2, "sanity"); 738 #endif 739 return true; 740 } 741 742 void StringTable::serialize(SerializeClosure* soc, GrowableArray<MemRegion> *string_space, 743 size_t* space_size) { 744 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS) 745 _shared_table.reset(); 746 if (soc->writing()) { 747 if (!(UseG1GC && UseCompressedOops && UseCompressedClassPointers)) { 748 log_info(cds)( 749 "Shared strings are excluded from the archive as UseG1GC, " 750 "UseCompressedOops and UseCompressedClassPointers are required." 751 "Current settings: UseG1GC=%s, UseCompressedOops=%s, UseCompressedClassPointers=%s.", 752 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedOops), 753 BOOL_TO_STR(UseCompressedClassPointers)); 754 } else { 755 int num_buckets = the_table()->number_of_entries() / 756 SharedSymbolTableBucketSize; 757 // calculation of num_buckets can result in zero buckets, we need at least one 758 CompactStringTableWriter writer(num_buckets > 1 ? num_buckets : 1, 759 &MetaspaceShared::stats()->string); 760 761 // Copy the interned strings into the "string space" within the java heap 762 if (copy_shared_string(string_space, &writer)) { 763 for (int i = 0; i < string_space->length(); i++) { 764 *space_size += string_space->at(i).byte_size(); 765 } 766 writer.dump(&_shared_table); 767 } 768 } 769 } 770 771 _shared_table.set_type(CompactHashtable<oop, char>::_string_table); 772 _shared_table.serialize(soc); 773 774 if (soc->writing()) { 775 _shared_table.reset(); // Sanity. Make sure we don't use the shared table at dump time 776 } else if (_ignore_shared_strings) { 777 _shared_table.reset(); 778 } 779 #endif 780 } 781 782 void StringTable::shared_oops_do(OopClosure* f) { 783 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS) 784 _shared_table.oops_do(f); 785 #endif 786 } 787