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