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 "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 // the number of buckets a thread claims 47 const int ClaimChunkSize = 32; 48 49 #ifdef ASSERT 50 class StableMemoryChecker : public StackObj { 51 enum { _bufsize = wordSize*4 }; 52 53 address _region; 54 jint _size; 55 u1 _save_buf[_bufsize]; 56 57 int sample(u1* save_buf) { 58 if (_size <= _bufsize) { 59 memcpy(save_buf, _region, _size); 60 return _size; 61 } else { 62 // copy head and tail 63 memcpy(&save_buf[0], _region, _bufsize/2); 64 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2); 65 return (_bufsize/2)*2; 66 } 67 } 68 69 public: 70 StableMemoryChecker(const void* region, jint size) { 71 _region = (address) region; 72 _size = size; 73 sample(_save_buf); 74 } 75 76 bool verify() { 77 u1 check_buf[sizeof(_save_buf)]; 78 int check_size = sample(check_buf); 79 return (0 == memcmp(_save_buf, check_buf, check_size)); 80 } 81 82 void set_region(const void* region) { _region = (address) region; } 83 }; 84 #endif 85 86 87 // -------------------------------------------------------------------------- 88 StringTable* StringTable::_the_table = NULL; 89 bool StringTable::_ignore_shared_strings = false; 90 bool StringTable::_needs_rehashing = false; 91 92 volatile int StringTable::_parallel_claimed_idx = 0; 93 94 CompactHashtable<oop, char> StringTable::_shared_table; 95 96 // Pick hashing algorithm 97 template<typename T> 98 unsigned int StringTable::hash_string(const T* 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 // Explicit instantiation for all supported types. 104 template unsigned int StringTable::hash_string<jchar>(const jchar* s, int len); 105 template unsigned int StringTable::hash_string<jbyte>(const jbyte* s, int len); 106 107 oop StringTable::lookup_shared(jchar* name, int len) { 108 // java_lang_String::hash_code() was used to compute hash values in the shared table. Don't 109 // use the hash value from StringTable::hash_string() as it might use alternate hashcode. 110 return _shared_table.lookup((const char*)name, 111 java_lang_String::hash_code(name, len), len); 112 } 113 114 // Tell the GC that this string was looked up in the StringTable. 115 static void ensure_string_alive(oop string) { 116 // A lookup in the StringTable could return an object that was previously 117 // considered dead. The SATB part of G1 needs to get notified about this 118 // potential resurrection, otherwise the marking might not find the object. 119 #if INCLUDE_ALL_GCS 120 assert(string != NULL, "should only be called when string found in table"); 121 if (UseG1GC) { 122 G1SATBCardTableModRefBS::enqueue(string); 123 } 124 #endif 125 } 126 127 oop StringTable::lookup_in_main_table(int index, jchar* name, 128 int len, unsigned int hash) { 129 int count = 0; 130 for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) { 131 count++; 132 if (l->hash() == hash) { 133 if (java_lang_String::equals(l->literal(), name, len)) { 134 oop result = l->literal(); 135 ensure_string_alive(result); 136 return result; 137 } 138 } 139 } 140 // If the bucket size is too deep check if this hash code is insufficient. 141 if (count >= rehash_count && !needs_rehashing()) { 142 _needs_rehashing = check_rehash_table(count); 143 } 144 return NULL; 145 } 146 147 148 oop StringTable::basic_add(int index_arg, Handle string, jchar* name, 149 int len, unsigned int hashValue_arg, TRAPS) { 150 151 assert(java_lang_String::equals(string(), name, len), 152 "string must be properly initialized"); 153 // Cannot hit a safepoint in this function because the "this" pointer can move. 154 NoSafepointVerifier nsv; 155 156 // Check if the symbol table has been rehashed, if so, need to recalculate 157 // the hash value and index before second lookup. 158 unsigned int hashValue; 159 int index; 160 if (use_alternate_hashcode()) { 161 hashValue = hash_string(name, len); 162 index = hash_to_index(hashValue); 163 } else { 164 hashValue = hashValue_arg; 165 index = index_arg; 166 } 167 168 // Since look-up was done lock-free, we need to check if another 169 // thread beat us in the race to insert the symbol. 170 171 // No need to lookup the shared table from here since the caller (intern()) already did 172 oop test = lookup_in_main_table(index, name, len, hashValue); // calls lookup(u1*, int) 173 if (test != NULL) { 174 // Entry already added 175 return test; 176 } 177 178 HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string()); 179 add_entry(index, entry); 180 return string(); 181 } 182 183 184 oop StringTable::lookup(Symbol* symbol) { 185 ResourceMark rm; 186 int length; 187 jchar* chars = symbol->as_unicode(length); 188 return lookup(chars, length); 189 } 190 191 oop StringTable::lookup(jchar* name, int len) { 192 oop string = lookup_shared(name, len); 193 if (string != NULL) { 194 return string; 195 } 196 197 unsigned int hash = hash_string(name, len); 198 int index = the_table()->hash_to_index(hash); 199 string = the_table()->lookup_in_main_table(index, name, len, hash); 200 return string; 201 } 202 203 204 oop StringTable::intern(Handle string_or_null, jchar* name, 205 int len, TRAPS) { 206 oop found_string = lookup_shared(name, len); 207 if (found_string != NULL) { 208 return found_string; 209 } 210 211 unsigned int hashValue = hash_string(name, len); 212 int index = the_table()->hash_to_index(hashValue); 213 found_string = the_table()->lookup_in_main_table(index, name, len, hashValue); 214 215 // Found 216 if (found_string != NULL) { 217 return found_string; 218 } 219 220 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); 221 assert(!Universe::heap()->is_in_reserved(name), 222 "proposed name of symbol must be stable"); 223 224 Handle string; 225 // try to reuse the string if possible 226 if (!string_or_null.is_null()) { 227 string = string_or_null; 228 } else { 229 string = java_lang_String::create_from_unicode(name, len, CHECK_NULL); 230 } 231 232 #if INCLUDE_ALL_GCS 233 if (G1StringDedup::is_enabled()) { 234 // Deduplicate the string before it is interned. Note that we should never 235 // deduplicate a string after it has been interned. Doing so will counteract 236 // compiler optimizations done on e.g. interned string literals. 237 G1StringDedup::deduplicate(string()); 238 } 239 #endif 240 241 // Grab the StringTable_lock before getting the_table() because it could 242 // change at safepoint. 243 oop added_or_found; 244 { 245 MutexLocker ml(StringTable_lock, THREAD); 246 // Otherwise, add to symbol to table 247 added_or_found = the_table()->basic_add(index, string, name, len, 248 hashValue, CHECK_NULL); 249 } 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 length = java_lang_String::length(s); 415 bool is_latin1 = java_lang_String::is_latin1(s); 416 417 if (length <= 0) { 418 st->print("%d: ", length); 419 } else { 420 ResourceMark rm(THREAD); 421 int utf8_length; 422 char* utf8_string; 423 424 if (!is_latin1) { 425 jchar* chars = value->char_at_addr(0); 426 utf8_length = UNICODE::utf8_length(chars, length); 427 utf8_string = UNICODE::as_utf8(chars, length); 428 } else { 429 jbyte* bytes = value->byte_at_addr(0); 430 utf8_length = UNICODE::utf8_length(bytes, length); 431 utf8_string = UNICODE::as_utf8(bytes, length); 432 } 433 434 st->print("%d: ", utf8_length); 435 HashtableTextDump::put_utf8(st, utf8_string, utf8_length); 436 } 437 st->cr(); 438 } 439 } 440 } 441 } 442 443 StringTable::VerifyRetTypes StringTable::compare_entries( 444 int bkt1, int e_cnt1, 445 HashtableEntry<oop, mtSymbol>* e_ptr1, 446 int bkt2, int e_cnt2, 447 HashtableEntry<oop, mtSymbol>* e_ptr2) { 448 // These entries are sanity checked by verify_and_compare_entries() 449 // before this function is called. 450 oop str1 = e_ptr1->literal(); 451 oop str2 = e_ptr2->literal(); 452 453 if (str1 == str2) { 454 tty->print_cr("ERROR: identical oop values (0x" PTR_FORMAT ") " 455 "in entry @ bucket[%d][%d] and entry @ bucket[%d][%d]", 456 p2i(str1), bkt1, e_cnt1, bkt2, e_cnt2); 457 return _verify_fail_continue; 458 } 459 460 if (java_lang_String::equals(str1, str2)) { 461 tty->print_cr("ERROR: identical String values in entry @ " 462 "bucket[%d][%d] and entry @ bucket[%d][%d]", 463 bkt1, e_cnt1, bkt2, e_cnt2); 464 return _verify_fail_continue; 465 } 466 467 return _verify_pass; 468 } 469 470 StringTable::VerifyRetTypes StringTable::verify_entry(int bkt, int e_cnt, 471 HashtableEntry<oop, mtSymbol>* e_ptr, 472 StringTable::VerifyMesgModes mesg_mode) { 473 474 VerifyRetTypes ret = _verify_pass; // be optimistic 475 476 oop str = e_ptr->literal(); 477 if (str == NULL) { 478 if (mesg_mode == _verify_with_mesgs) { 479 tty->print_cr("ERROR: NULL oop value in entry @ bucket[%d][%d]", bkt, 480 e_cnt); 481 } 482 // NULL oop means no more verifications are possible 483 return _verify_fail_done; 484 } 485 486 if (str->klass() != SystemDictionary::String_klass()) { 487 if (mesg_mode == _verify_with_mesgs) { 488 tty->print_cr("ERROR: oop is not a String in entry @ bucket[%d][%d]", 489 bkt, e_cnt); 490 } 491 // not a String means no more verifications are possible 492 return _verify_fail_done; 493 } 494 495 unsigned int h = java_lang_String::hash_string(str); 496 if (e_ptr->hash() != h) { 497 if (mesg_mode == _verify_with_mesgs) { 498 tty->print_cr("ERROR: broken hash value in entry @ bucket[%d][%d], " 499 "bkt_hash=%d, str_hash=%d", bkt, e_cnt, e_ptr->hash(), h); 500 } 501 ret = _verify_fail_continue; 502 } 503 504 if (the_table()->hash_to_index(h) != bkt) { 505 if (mesg_mode == _verify_with_mesgs) { 506 tty->print_cr("ERROR: wrong index value for entry @ bucket[%d][%d], " 507 "str_hash=%d, hash_to_index=%d", bkt, e_cnt, h, 508 the_table()->hash_to_index(h)); 509 } 510 ret = _verify_fail_continue; 511 } 512 513 return ret; 514 } 515 516 // See StringTable::verify() above for the quick verification that is 517 // part of Universe::verify(). This verification is exhaustive and 518 // reports on every issue that is found. StringTable::verify() only 519 // reports on the first issue that is found. 520 // 521 // StringTable::verify_entry() checks: 522 // - oop value != NULL (same as verify()) 523 // - oop value is a String 524 // - hash(String) == hash in entry (same as verify()) 525 // - index for hash == index of entry (same as verify()) 526 // 527 // StringTable::compare_entries() checks: 528 // - oops are unique across all entries 529 // - String values are unique across all entries 530 // 531 int StringTable::verify_and_compare_entries() { 532 assert(StringTable_lock->is_locked(), "sanity check"); 533 534 int fail_cnt = 0; 535 536 // first, verify all the entries individually: 537 for (int bkt = 0; bkt < the_table()->table_size(); bkt++) { 538 HashtableEntry<oop, mtSymbol>* e_ptr = the_table()->bucket(bkt); 539 for (int e_cnt = 0; e_ptr != NULL; e_ptr = e_ptr->next(), e_cnt++) { 540 VerifyRetTypes ret = verify_entry(bkt, e_cnt, e_ptr, _verify_with_mesgs); 541 if (ret != _verify_pass) { 542 fail_cnt++; 543 } 544 } 545 } 546 547 // Optimization: if the above check did not find any failures, then 548 // the comparison loop below does not need to call verify_entry() 549 // before calling compare_entries(). If there were failures, then we 550 // have to call verify_entry() to see if the entry can be passed to 551 // compare_entries() safely. When we call verify_entry() in the loop 552 // below, we do so quietly to void duplicate messages and we don't 553 // increment fail_cnt because the failures have already been counted. 554 bool need_entry_verify = (fail_cnt != 0); 555 556 // second, verify all entries relative to each other: 557 for (int bkt1 = 0; bkt1 < the_table()->table_size(); bkt1++) { 558 HashtableEntry<oop, mtSymbol>* e_ptr1 = the_table()->bucket(bkt1); 559 for (int e_cnt1 = 0; e_ptr1 != NULL; e_ptr1 = e_ptr1->next(), e_cnt1++) { 560 if (need_entry_verify) { 561 VerifyRetTypes ret = verify_entry(bkt1, e_cnt1, e_ptr1, 562 _verify_quietly); 563 if (ret == _verify_fail_done) { 564 // cannot use the current entry to compare against other entries 565 continue; 566 } 567 } 568 569 for (int bkt2 = bkt1; bkt2 < the_table()->table_size(); bkt2++) { 570 HashtableEntry<oop, mtSymbol>* e_ptr2 = the_table()->bucket(bkt2); 571 int e_cnt2; 572 for (e_cnt2 = 0; e_ptr2 != NULL; e_ptr2 = e_ptr2->next(), e_cnt2++) { 573 if (bkt1 == bkt2 && e_cnt2 <= e_cnt1) { 574 // skip the entries up to and including the one that 575 // we're comparing against 576 continue; 577 } 578 579 if (need_entry_verify) { 580 VerifyRetTypes ret = verify_entry(bkt2, e_cnt2, e_ptr2, 581 _verify_quietly); 582 if (ret == _verify_fail_done) { 583 // cannot compare against this entry 584 continue; 585 } 586 } 587 588 // compare two entries, report and count any failures: 589 if (compare_entries(bkt1, e_cnt1, e_ptr1, bkt2, e_cnt2, e_ptr2) 590 != _verify_pass) { 591 fail_cnt++; 592 } 593 } 594 } 595 } 596 } 597 return fail_cnt; 598 } 599 600 // Create a new table and using alternate hash code, populate the new table 601 // with the existing strings. Set flag to use the alternate hash code afterwards. 602 void StringTable::rehash_table() { 603 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 604 // This should never happen with -Xshare:dump but it might in testing mode. 605 if (DumpSharedSpaces) return; 606 StringTable* new_table = new StringTable(); 607 608 // Rehash the table 609 the_table()->move_to(new_table); 610 611 // Delete the table and buckets (entries are reused in new table). 612 delete _the_table; 613 // Don't check if we need rehashing until the table gets unbalanced again. 614 // Then rehash with a new global seed. 615 _needs_rehashing = false; 616 _the_table = new_table; 617 } 618 619 // Utility for dumping strings 620 StringtableDCmd::StringtableDCmd(outputStream* output, bool heap) : 621 DCmdWithParser(output, heap), 622 _verbose("-verbose", "Dump the content of each string in the table", 623 "BOOLEAN", false, "false") { 624 _dcmdparser.add_dcmd_option(&_verbose); 625 } 626 627 void StringtableDCmd::execute(DCmdSource source, TRAPS) { 628 VM_DumpHashtable dumper(output(), VM_DumpHashtable::DumpStrings, 629 _verbose.value()); 630 VMThread::execute(&dumper); 631 } 632 633 int StringtableDCmd::num_arguments() { 634 ResourceMark rm; 635 StringtableDCmd* dcmd = new StringtableDCmd(NULL, false); 636 if (dcmd != NULL) { 637 DCmdMark mark(dcmd); 638 return dcmd->_dcmdparser.num_arguments(); 639 } else { 640 return 0; 641 } 642 } 643 644 // Sharing 645 bool StringTable::copy_shared_string(GrowableArray<MemRegion> *string_space, 646 CompactHashtableWriter* ch_table) { 647 #if INCLUDE_CDS && INCLUDE_ALL_GCS && defined(_LP64) && !defined(_WINDOWS) 648 assert(UseG1GC, "Only support G1 GC"); 649 assert(UseCompressedOops && UseCompressedClassPointers, 650 "Only support UseCompressedOops and UseCompressedClassPointers enabled"); 651 652 Thread* THREAD = Thread::current(); 653 G1CollectedHeap::heap()->begin_archive_alloc_range(); 654 for (int i = 0; i < the_table()->table_size(); ++i) { 655 HashtableEntry<oop, mtSymbol>* bucket = the_table()->bucket(i); 656 for ( ; bucket != NULL; bucket = bucket->next()) { 657 oop s = bucket->literal(); 658 unsigned int hash = java_lang_String::hash_code(s); 659 if (hash == 0) { 660 continue; 661 } 662 663 // allocate the new 'value' array first 664 typeArrayOop v = java_lang_String::value(s); 665 int v_len = v->size(); 666 typeArrayOop new_v; 667 if (G1CollectedHeap::heap()->is_archive_alloc_too_large(v_len)) { 668 continue; // skip the current String. The 'value' array is too large to handle 669 } else { 670 new_v = (typeArrayOop)G1CollectedHeap::heap()->archive_mem_allocate(v_len); 671 if (new_v == NULL) { 672 return false; // allocation failed 673 } 674 } 675 // now allocate the new String object 676 int s_len = s->size(); 677 oop new_s = (oop)G1CollectedHeap::heap()->archive_mem_allocate(s_len); 678 if (new_s == NULL) { 679 return false; 680 } 681 682 s->identity_hash(); 683 v->identity_hash(); 684 685 // copy the objects' data 686 Copy::aligned_disjoint_words((HeapWord*)s, (HeapWord*)new_s, s_len); 687 Copy::aligned_disjoint_words((HeapWord*)v, (HeapWord*)new_v, v_len); 688 689 // adjust the pointer to the 'value' field in the new String oop. Also pre-compute and set the 690 // 'hash' field. That avoids "write" to the shared strings at runtime by the deduplication process. 691 java_lang_String::set_value_raw(new_s, new_v); 692 if (java_lang_String::hash(new_s) == 0) { 693 java_lang_String::set_hash(new_s, hash); 694 } 695 696 // add to the compact table 697 ch_table->add(hash, new_s); 698 } 699 } 700 701 G1CollectedHeap::heap()->end_archive_alloc_range(string_space, os::vm_allocation_granularity()); 702 assert(string_space->length() <= 2, "sanity"); 703 #endif 704 return true; 705 } 706 707 bool StringTable::copy_compact_table(char** top, char *end, GrowableArray<MemRegion> *string_space, 708 size_t* space_size) { 709 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS) 710 if (!(UseG1GC && UseCompressedOops && UseCompressedClassPointers)) { 711 if (PrintSharedSpaces) { 712 tty->print_cr("Shared strings are excluded from the archive as UseG1GC, " 713 "UseCompressedOops and UseCompressedClassPointers are required."); 714 } 715 return true; 716 } 717 718 CompactHashtableWriter ch_table(CompactHashtable<oop, char>::_string_table, 719 the_table()->number_of_entries(), 720 &MetaspaceShared::stats()->string); 721 722 // Copy the interned strings into the "string space" within the java heap 723 if (!copy_shared_string(string_space, &ch_table)) { 724 return false; 725 } 726 727 for (int i = 0; i < string_space->length(); i++) { 728 *space_size += string_space->at(i).byte_size(); 729 } 730 731 // Now dump the compact table 732 if (*top + ch_table.get_required_bytes() > end) { 733 // not enough space left 734 return false; 735 } 736 ch_table.dump(top, end); 737 *top = (char*)align_ptr_up(*top, sizeof(void*)); 738 739 #endif 740 return true; 741 } 742 743 void StringTable::shared_oops_do(OopClosure* f) { 744 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS) 745 _shared_table.oops_do(f); 746 #endif 747 } 748 749 const char* StringTable::init_shared_table(FileMapInfo *mapinfo, char *buffer) { 750 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS) 751 if (mapinfo->space_capacity(MetaspaceShared::first_string) == 0) { 752 // no shared string data 753 return buffer; 754 } 755 756 // initialize the shared table 757 juint *p = (juint*)buffer; 758 const char* end = _shared_table.init( 759 CompactHashtable<oop, char>::_string_table, (char*)p); 760 const char* aligned_end = (const char*)align_ptr_up(end, sizeof(void*)); 761 762 if (_ignore_shared_strings) { 763 _shared_table.reset(); 764 } 765 766 return aligned_end; 767 #endif 768 769 return buffer; 770 }