1 /* 2 * Copyright (c) 1997, 2014, 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/javaClasses.hpp" 28 #include "classfile/stringTable.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "gc_interface/collectedHeap.inline.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "memory/filemap.hpp" 33 #include "memory/gcLocker.inline.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "oops/oop.inline2.hpp" 36 #include "runtime/atomic.inline.hpp" 37 #include "runtime/mutexLocker.hpp" 38 #include "utilities/hashtable.inline.hpp" 39 #if INCLUDE_ALL_GCS 40 #include "gc_implementation/g1/g1StringDedup.hpp" 41 #endif 42 43 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 44 45 // the number of buckets a thread claims 46 const int ClaimChunkSize = 32; 47 48 #ifdef ASSERT 49 class StableMemoryChecker : public StackObj { 50 enum { _bufsize = wordSize*4 }; 51 52 address _region; 53 jint _size; 54 u1 _save_buf[_bufsize]; 55 56 int sample(u1* save_buf) { 57 if (_size <= _bufsize) { 58 memcpy(save_buf, _region, _size); 59 return _size; 60 } else { 61 // copy head and tail 62 memcpy(&save_buf[0], _region, _bufsize/2); 63 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2); 64 return (_bufsize/2)*2; 65 } 66 } 67 68 public: 69 StableMemoryChecker(const void* region, jint size) { 70 _region = (address) region; 71 _size = size; 72 sample(_save_buf); 73 } 74 75 bool verify() { 76 u1 check_buf[sizeof(_save_buf)]; 77 int check_size = sample(check_buf); 78 return (0 == memcmp(_save_buf, check_buf, check_size)); 79 } 80 81 void set_region(const void* region) { _region = (address) region; } 82 }; 83 #endif 84 85 86 // -------------------------------------------------------------------------- 87 StringTable* StringTable::_the_table = NULL; 88 89 bool StringTable::_needs_rehashing = false; 90 91 volatile int StringTable::_parallel_claimed_idx = 0; 92 93 // Pick hashing algorithm 94 unsigned int StringTable::hash_string(const jchar* s, int len) { 95 return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) : 96 java_lang_String::hash_code(s, len); 97 } 98 99 oop StringTable::lookup(int index, jchar* name, 100 int len, unsigned int hash) { 101 int count = 0; 102 for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) { 103 count++; 104 if (l->hash() == hash) { 105 if (java_lang_String::equals(l->literal(), name, len)) { 106 return l->literal(); 107 } 108 } 109 } 110 // If the bucket size is too deep check if this hash code is insufficient. 111 if (count >= BasicHashtable<mtSymbol>::rehash_count && !needs_rehashing()) { 112 _needs_rehashing = check_rehash_table(count); 113 } 114 return NULL; 115 } 116 117 118 oop StringTable::basic_add(int index_arg, Handle string, jchar* name, 119 int len, unsigned int hashValue_arg, TRAPS) { 120 121 assert(java_lang_String::equals(string(), name, len), 122 "string must be properly initialized"); 123 // Cannot hit a safepoint in this function because the "this" pointer can move. 124 No_Safepoint_Verifier nsv; 125 126 // Check if the symbol table has been rehashed, if so, need to recalculate 127 // the hash value and index before second lookup. 128 unsigned int hashValue; 129 int index; 130 if (use_alternate_hashcode()) { 131 hashValue = hash_string(name, len); 132 index = hash_to_index(hashValue); 133 } else { 134 hashValue = hashValue_arg; 135 index = index_arg; 136 } 137 138 // Since look-up was done lock-free, we need to check if another 139 // thread beat us in the race to insert the symbol. 140 141 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int) 142 if (test != NULL) { 143 // Entry already added 144 return test; 145 } 146 147 HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string()); 148 add_entry(index, entry); 149 return string(); 150 } 151 152 153 oop StringTable::lookup(Symbol* symbol) { 154 ResourceMark rm; 155 int length; 156 jchar* chars = symbol->as_unicode(length); 157 return lookup(chars, length); 158 } 159 160 161 oop StringTable::lookup(jchar* name, int len) { 162 unsigned int hash = hash_string(name, len); 163 int index = the_table()->hash_to_index(hash); 164 return the_table()->lookup(index, name, len, hash); 165 } 166 167 168 oop StringTable::intern(Handle string_or_null, jchar* name, 169 int len, TRAPS) { 170 unsigned int hashValue = hash_string(name, len); 171 int index = the_table()->hash_to_index(hashValue); 172 oop found_string = the_table()->lookup(index, name, len, hashValue); 173 174 // Found 175 if (found_string != NULL) return found_string; 176 177 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); 178 assert(!Universe::heap()->is_in_reserved(name), 179 "proposed name of symbol must be stable"); 180 181 Handle string; 182 // try to reuse the string if possible 183 if (!string_or_null.is_null()) { 184 string = string_or_null; 185 } else { 186 string = java_lang_String::create_from_unicode(name, len, CHECK_NULL); 187 } 188 189 #if INCLUDE_ALL_GCS 190 if (G1StringDedup::is_enabled()) { 191 // Deduplicate the string before it is interned. Note that we should never 192 // deduplicate a string after it has been interned. Doing so will counteract 193 // compiler optimizations done on e.g. interned string literals. 194 G1StringDedup::deduplicate(string()); 195 } 196 #endif 197 198 // Grab the StringTable_lock before getting the_table() because it could 199 // change at safepoint. 200 MutexLocker ml(StringTable_lock, THREAD); 201 202 // Otherwise, add to symbol to table 203 return the_table()->basic_add(index, string, name, len, 204 hashValue, CHECK_NULL); 205 } 206 207 oop StringTable::intern(Symbol* symbol, TRAPS) { 208 if (symbol == NULL) return NULL; 209 ResourceMark rm(THREAD); 210 int length; 211 jchar* chars = symbol->as_unicode(length); 212 Handle string; 213 oop result = intern(string, chars, length, CHECK_NULL); 214 return result; 215 } 216 217 218 oop StringTable::intern(oop string, TRAPS) 219 { 220 if (string == NULL) return NULL; 221 ResourceMark rm(THREAD); 222 int length; 223 Handle h_string (THREAD, string); 224 jchar* chars = java_lang_String::as_unicode_string(string, length, CHECK_NULL); 225 oop result = intern(h_string, chars, length, CHECK_NULL); 226 return result; 227 } 228 229 230 oop StringTable::intern(const char* utf8_string, TRAPS) { 231 if (utf8_string == NULL) return NULL; 232 ResourceMark rm(THREAD); 233 int length = UTF8::unicode_length(utf8_string); 234 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length); 235 UTF8::convert_to_unicode(utf8_string, chars, length); 236 Handle string; 237 oop result = intern(string, chars, length, CHECK_NULL); 238 return result; 239 } 240 241 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) { 242 buckets_unlink_or_oops_do(is_alive, f, 0, the_table()->table_size(), processed, removed); 243 } 244 245 void StringTable::possibly_parallel_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) { 246 // Readers of the table are unlocked, so we should only be removing 247 // entries at a safepoint. 248 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 249 const int limit = the_table()->table_size(); 250 251 for (;;) { 252 // Grab next set of buckets to scan 253 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; 254 if (start_idx >= limit) { 255 // End of table 256 break; 257 } 258 259 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); 260 buckets_unlink_or_oops_do(is_alive, f, start_idx, end_idx, processed, removed); 261 } 262 } 263 264 void StringTable::buckets_oops_do(OopClosure* f, int start_idx, int end_idx) { 265 const int limit = the_table()->table_size(); 266 267 assert(0 <= start_idx && start_idx <= limit, 268 err_msg("start_idx (%d) is out of bounds", start_idx)); 269 assert(0 <= end_idx && end_idx <= limit, 270 err_msg("end_idx (%d) is out of bounds", end_idx)); 271 assert(start_idx <= end_idx, 272 err_msg("Index ordering: start_idx=%d, end_idx=%d", 273 start_idx, end_idx)); 274 275 for (int i = start_idx; i < end_idx; i += 1) { 276 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i); 277 while (entry != NULL) { 278 assert(!entry->is_shared(), "CDS not used for the StringTable"); 279 280 f->do_oop((oop*)entry->literal_addr()); 281 282 entry = entry->next(); 283 } 284 } 285 } 286 287 void StringTable::buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, int* processed, int* removed) { 288 const int limit = the_table()->table_size(); 289 290 assert(0 <= start_idx && start_idx <= limit, 291 err_msg("start_idx (%d) is out of bounds", start_idx)); 292 assert(0 <= end_idx && end_idx <= limit, 293 err_msg("end_idx (%d) is out of bounds", end_idx)); 294 assert(start_idx <= end_idx, 295 err_msg("Index ordering: start_idx=%d, end_idx=%d", 296 start_idx, end_idx)); 297 298 for (int i = start_idx; i < end_idx; ++i) { 299 HashtableEntry<oop, mtSymbol>** p = the_table()->bucket_addr(i); 300 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i); 301 while (entry != NULL) { 302 assert(!entry->is_shared(), "CDS not used for the StringTable"); 303 304 if (is_alive->do_object_b(entry->literal())) { 305 if (f != NULL) { 306 f->do_oop((oop*)entry->literal_addr()); 307 } 308 p = entry->next_addr(); 309 } else { 310 *p = entry->next(); 311 the_table()->free_entry(entry); 312 (*removed)++; 313 } 314 (*processed)++; 315 entry = *p; 316 } 317 } 318 } 319 320 void StringTable::oops_do(OopClosure* f) { 321 buckets_oops_do(f, 0, the_table()->table_size()); 322 } 323 324 void StringTable::possibly_parallel_oops_do(OopClosure* f) { 325 const int limit = the_table()->table_size(); 326 327 for (;;) { 328 // Grab next set of buckets to scan 329 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; 330 if (start_idx >= limit) { 331 // End of table 332 break; 333 } 334 335 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); 336 buckets_oops_do(f, start_idx, end_idx); 337 } 338 } 339 340 // This verification is part of Universe::verify() and needs to be quick. 341 // See StringTable::verify_and_compare() below for exhaustive verification. 342 void StringTable::verify() { 343 for (int i = 0; i < the_table()->table_size(); ++i) { 344 HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i); 345 for ( ; p != NULL; p = p->next()) { 346 oop s = p->literal(); 347 guarantee(s != NULL, "interned string is NULL"); 348 unsigned int h = java_lang_String::hash_string(s); 349 guarantee(p->hash() == h, "broken hash in string table entry"); 350 guarantee(the_table()->hash_to_index(h) == i, 351 "wrong index in string table"); 352 } 353 } 354 } 355 356 void StringTable::dump(outputStream* st) { 357 the_table()->dump_table(st, "StringTable"); 358 } 359 360 StringTable::VerifyRetTypes StringTable::compare_entries( 361 int bkt1, int e_cnt1, 362 HashtableEntry<oop, mtSymbol>* e_ptr1, 363 int bkt2, int e_cnt2, 364 HashtableEntry<oop, mtSymbol>* e_ptr2) { 365 // These entries are sanity checked by verify_and_compare_entries() 366 // before this function is called. 367 oop str1 = e_ptr1->literal(); 368 oop str2 = e_ptr2->literal(); 369 370 if (str1 == str2) { 371 tty->print_cr("ERROR: identical oop values (0x" PTR_FORMAT ") " 372 "in entry @ bucket[%d][%d] and entry @ bucket[%d][%d]", 373 (void *)str1, bkt1, e_cnt1, bkt2, e_cnt2); 374 return _verify_fail_continue; 375 } 376 377 if (java_lang_String::equals(str1, str2)) { 378 tty->print_cr("ERROR: identical String values in entry @ " 379 "bucket[%d][%d] and entry @ bucket[%d][%d]", 380 bkt1, e_cnt1, bkt2, e_cnt2); 381 return _verify_fail_continue; 382 } 383 384 return _verify_pass; 385 } 386 387 StringTable::VerifyRetTypes StringTable::verify_entry(int bkt, int e_cnt, 388 HashtableEntry<oop, mtSymbol>* e_ptr, 389 StringTable::VerifyMesgModes mesg_mode) { 390 391 VerifyRetTypes ret = _verify_pass; // be optimistic 392 393 oop str = e_ptr->literal(); 394 if (str == NULL) { 395 if (mesg_mode == _verify_with_mesgs) { 396 tty->print_cr("ERROR: NULL oop value in entry @ bucket[%d][%d]", bkt, 397 e_cnt); 398 } 399 // NULL oop means no more verifications are possible 400 return _verify_fail_done; 401 } 402 403 if (str->klass() != SystemDictionary::String_klass()) { 404 if (mesg_mode == _verify_with_mesgs) { 405 tty->print_cr("ERROR: oop is not a String in entry @ bucket[%d][%d]", 406 bkt, e_cnt); 407 } 408 // not a String means no more verifications are possible 409 return _verify_fail_done; 410 } 411 412 unsigned int h = java_lang_String::hash_string(str); 413 if (e_ptr->hash() != h) { 414 if (mesg_mode == _verify_with_mesgs) { 415 tty->print_cr("ERROR: broken hash value in entry @ bucket[%d][%d], " 416 "bkt_hash=%d, str_hash=%d", bkt, e_cnt, e_ptr->hash(), h); 417 } 418 ret = _verify_fail_continue; 419 } 420 421 if (the_table()->hash_to_index(h) != bkt) { 422 if (mesg_mode == _verify_with_mesgs) { 423 tty->print_cr("ERROR: wrong index value for entry @ bucket[%d][%d], " 424 "str_hash=%d, hash_to_index=%d", bkt, e_cnt, h, 425 the_table()->hash_to_index(h)); 426 } 427 ret = _verify_fail_continue; 428 } 429 430 return ret; 431 } 432 433 // See StringTable::verify() above for the quick verification that is 434 // part of Universe::verify(). This verification is exhaustive and 435 // reports on every issue that is found. StringTable::verify() only 436 // reports on the first issue that is found. 437 // 438 // StringTable::verify_entry() checks: 439 // - oop value != NULL (same as verify()) 440 // - oop value is a String 441 // - hash(String) == hash in entry (same as verify()) 442 // - index for hash == index of entry (same as verify()) 443 // 444 // StringTable::compare_entries() checks: 445 // - oops are unique across all entries 446 // - String values are unique across all entries 447 // 448 int StringTable::verify_and_compare_entries() { 449 assert(StringTable_lock->is_locked(), "sanity check"); 450 451 int fail_cnt = 0; 452 453 // first, verify all the entries individually: 454 for (int bkt = 0; bkt < the_table()->table_size(); bkt++) { 455 HashtableEntry<oop, mtSymbol>* e_ptr = the_table()->bucket(bkt); 456 for (int e_cnt = 0; e_ptr != NULL; e_ptr = e_ptr->next(), e_cnt++) { 457 VerifyRetTypes ret = verify_entry(bkt, e_cnt, e_ptr, _verify_with_mesgs); 458 if (ret != _verify_pass) { 459 fail_cnt++; 460 } 461 } 462 } 463 464 // Optimization: if the above check did not find any failures, then 465 // the comparison loop below does not need to call verify_entry() 466 // before calling compare_entries(). If there were failures, then we 467 // have to call verify_entry() to see if the entry can be passed to 468 // compare_entries() safely. When we call verify_entry() in the loop 469 // below, we do so quietly to void duplicate messages and we don't 470 // increment fail_cnt because the failures have already been counted. 471 bool need_entry_verify = (fail_cnt != 0); 472 473 // second, verify all entries relative to each other: 474 for (int bkt1 = 0; bkt1 < the_table()->table_size(); bkt1++) { 475 HashtableEntry<oop, mtSymbol>* e_ptr1 = the_table()->bucket(bkt1); 476 for (int e_cnt1 = 0; e_ptr1 != NULL; e_ptr1 = e_ptr1->next(), e_cnt1++) { 477 if (need_entry_verify) { 478 VerifyRetTypes ret = verify_entry(bkt1, e_cnt1, e_ptr1, 479 _verify_quietly); 480 if (ret == _verify_fail_done) { 481 // cannot use the current entry to compare against other entries 482 continue; 483 } 484 } 485 486 for (int bkt2 = bkt1; bkt2 < the_table()->table_size(); bkt2++) { 487 HashtableEntry<oop, mtSymbol>* e_ptr2 = the_table()->bucket(bkt2); 488 int e_cnt2; 489 for (e_cnt2 = 0; e_ptr2 != NULL; e_ptr2 = e_ptr2->next(), e_cnt2++) { 490 if (bkt1 == bkt2 && e_cnt2 <= e_cnt1) { 491 // skip the entries up to and including the one that 492 // we're comparing against 493 continue; 494 } 495 496 if (need_entry_verify) { 497 VerifyRetTypes ret = verify_entry(bkt2, e_cnt2, e_ptr2, 498 _verify_quietly); 499 if (ret == _verify_fail_done) { 500 // cannot compare against this entry 501 continue; 502 } 503 } 504 505 // compare two entries, report and count any failures: 506 if (compare_entries(bkt1, e_cnt1, e_ptr1, bkt2, e_cnt2, e_ptr2) 507 != _verify_pass) { 508 fail_cnt++; 509 } 510 } 511 } 512 } 513 } 514 return fail_cnt; 515 } 516 517 // Create a new table and using alternate hash code, populate the new table 518 // with the existing strings. Set flag to use the alternate hash code afterwards. 519 void StringTable::rehash_table() { 520 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 521 // This should never happen with -Xshare:dump but it might in testing mode. 522 if (DumpSharedSpaces) return; 523 StringTable* new_table = new StringTable(); 524 525 // Rehash the table 526 the_table()->move_to(new_table); 527 528 // Delete the table and buckets (entries are reused in new table). 529 delete _the_table; 530 // Don't check if we need rehashing until the table gets unbalanced again. 531 // Then rehash with a new global seed. 532 _needs_rehashing = false; 533 _the_table = new_table; 534 }