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