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