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