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