src/share/vm/classfile/symbolTable.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8766
ce9a710b0f63
parent 7535
7ae4e26cb1e0
child 10015
eb7ce841ccec
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
tschatzl@8766 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/altHashing.hpp"
aoqi@0 27 #include "classfile/javaClasses.hpp"
aoqi@0 28 #include "classfile/symbolTable.hpp"
aoqi@0 29 #include "classfile/systemDictionary.hpp"
aoqi@0 30 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 31 #include "memory/allocation.inline.hpp"
aoqi@0 32 #include "memory/filemap.hpp"
aoqi@0 33 #include "memory/gcLocker.inline.hpp"
aoqi@0 34 #include "oops/oop.inline.hpp"
aoqi@0 35 #include "oops/oop.inline2.hpp"
aoqi@0 36 #include "runtime/mutexLocker.hpp"
aoqi@0 37 #include "utilities/hashtable.inline.hpp"
aoqi@0 38 #if INCLUDE_ALL_GCS
stefank@6992 39 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
aoqi@0 40 #include "gc_implementation/g1/g1StringDedup.hpp"
aoqi@0 41 #endif
aoqi@0 42
aoqi@0 43 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 44
aoqi@0 45 // --------------------------------------------------------------------------
aoqi@0 46
aoqi@0 47 // the number of buckets a thread claims
aoqi@0 48 const int ClaimChunkSize = 32;
aoqi@0 49
aoqi@0 50 SymbolTable* SymbolTable::_the_table = NULL;
aoqi@0 51 // Static arena for symbols that are not deallocated
aoqi@0 52 Arena* SymbolTable::_arena = NULL;
aoqi@0 53 bool SymbolTable::_needs_rehashing = false;
aoqi@0 54
aoqi@0 55 Symbol* SymbolTable::allocate_symbol(const u1* name, int len, bool c_heap, TRAPS) {
aoqi@0 56 assert (len <= Symbol::max_length(), "should be checked by caller");
aoqi@0 57
aoqi@0 58 Symbol* sym;
aoqi@0 59
aoqi@0 60 if (DumpSharedSpaces) {
aoqi@0 61 // Allocate all symbols to CLD shared metaspace
aoqi@0 62 sym = new (len, ClassLoaderData::the_null_class_loader_data(), THREAD) Symbol(name, len, -1);
aoqi@0 63 } else if (c_heap) {
aoqi@0 64 // refcount starts as 1
aoqi@0 65 sym = new (len, THREAD) Symbol(name, len, 1);
aoqi@0 66 assert(sym != NULL, "new should call vm_exit_out_of_memory if C_HEAP is exhausted");
aoqi@0 67 } else {
aoqi@0 68 // Allocate to global arena
aoqi@0 69 sym = new (len, arena(), THREAD) Symbol(name, len, -1);
aoqi@0 70 }
aoqi@0 71 return sym;
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 void SymbolTable::initialize_symbols(int arena_alloc_size) {
aoqi@0 75 // Initialize the arena for global symbols, size passed in depends on CDS.
aoqi@0 76 if (arena_alloc_size == 0) {
zgu@7074 77 _arena = new (mtSymbol) Arena(mtSymbol);
aoqi@0 78 } else {
zgu@7074 79 _arena = new (mtSymbol) Arena(mtSymbol, arena_alloc_size);
aoqi@0 80 }
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 // Call function for all symbols in the symbol table.
aoqi@0 84 void SymbolTable::symbols_do(SymbolClosure *cl) {
aoqi@0 85 const int n = the_table()->table_size();
aoqi@0 86 for (int i = 0; i < n; i++) {
aoqi@0 87 for (HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i);
aoqi@0 88 p != NULL;
aoqi@0 89 p = p->next()) {
aoqi@0 90 cl->do_symbol(p->literal_addr());
aoqi@0 91 }
aoqi@0 92 }
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 int SymbolTable::_symbols_removed = 0;
aoqi@0 96 int SymbolTable::_symbols_counted = 0;
aoqi@0 97 volatile int SymbolTable::_parallel_claimed_idx = 0;
aoqi@0 98
tschatzl@8766 99 void SymbolTable::buckets_unlink(int start_idx, int end_idx, BucketUnlinkContext* context, size_t* memory_total) {
aoqi@0 100 for (int i = start_idx; i < end_idx; ++i) {
aoqi@0 101 HashtableEntry<Symbol*, mtSymbol>** p = the_table()->bucket_addr(i);
aoqi@0 102 HashtableEntry<Symbol*, mtSymbol>* entry = the_table()->bucket(i);
aoqi@0 103 while (entry != NULL) {
aoqi@0 104 // Shared entries are normally at the end of the bucket and if we run into
aoqi@0 105 // a shared entry, then there is nothing more to remove. However, if we
aoqi@0 106 // have rehashed the table, then the shared entries are no longer at the
aoqi@0 107 // end of the bucket.
aoqi@0 108 if (entry->is_shared() && !use_alternate_hashcode()) {
aoqi@0 109 break;
aoqi@0 110 }
aoqi@0 111 Symbol* s = entry->literal();
aoqi@0 112 (*memory_total) += s->size();
tschatzl@8766 113 context->_num_processed++;
aoqi@0 114 assert(s != NULL, "just checking");
aoqi@0 115 // If reference count is zero, remove.
aoqi@0 116 if (s->refcount() == 0) {
aoqi@0 117 assert(!entry->is_shared(), "shared entries should be kept live");
aoqi@0 118 delete s;
aoqi@0 119 *p = entry->next();
tschatzl@8766 120 context->free_entry(entry);
aoqi@0 121 } else {
aoqi@0 122 p = entry->next_addr();
aoqi@0 123 }
aoqi@0 124 // get next entry
aoqi@0 125 entry = (HashtableEntry<Symbol*, mtSymbol>*)HashtableEntry<Symbol*, mtSymbol>::make_ptr(*p);
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 // Remove unreferenced symbols from the symbol table
aoqi@0 131 // This is done late during GC.
aoqi@0 132 void SymbolTable::unlink(int* processed, int* removed) {
aoqi@0 133 size_t memory_total = 0;
tschatzl@8766 134 BucketUnlinkContext context;
tschatzl@8766 135 buckets_unlink(0, the_table()->table_size(), &context, &memory_total);
tschatzl@8766 136 _the_table->bulk_free_entries(&context);
tschatzl@8766 137 *processed = context._num_processed;
tschatzl@8766 138 *removed = context._num_removed;
tschatzl@8766 139
tschatzl@8766 140 _symbols_removed = context._num_removed;
tschatzl@8766 141 _symbols_counted = context._num_processed;
aoqi@0 142 // Exclude printing for normal PrintGCDetails because people parse
aoqi@0 143 // this output.
aoqi@0 144 if (PrintGCDetails && Verbose && WizardMode) {
aoqi@0 145 gclog_or_tty->print(" [Symbols=%d size=" SIZE_FORMAT "K] ", *processed,
aoqi@0 146 (memory_total*HeapWordSize)/1024);
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 void SymbolTable::possibly_parallel_unlink(int* processed, int* removed) {
aoqi@0 151 const int limit = the_table()->table_size();
aoqi@0 152
aoqi@0 153 size_t memory_total = 0;
aoqi@0 154
tschatzl@8766 155 BucketUnlinkContext context;
aoqi@0 156 for (;;) {
aoqi@0 157 // Grab next set of buckets to scan
aoqi@0 158 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize;
aoqi@0 159 if (start_idx >= limit) {
aoqi@0 160 // End of table
aoqi@0 161 break;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 int end_idx = MIN2(limit, start_idx + ClaimChunkSize);
tschatzl@8766 165 buckets_unlink(start_idx, end_idx, &context, &memory_total);
aoqi@0 166 }
tschatzl@8766 167
tschatzl@8766 168 _the_table->bulk_free_entries(&context);
tschatzl@8766 169 *processed = context._num_processed;
tschatzl@8766 170 *removed = context._num_removed;
tschatzl@8766 171
tschatzl@8766 172 Atomic::add(context._num_processed, &_symbols_counted);
tschatzl@8766 173 Atomic::add(context._num_removed, &_symbols_removed);
aoqi@0 174 // Exclude printing for normal PrintGCDetails because people parse
aoqi@0 175 // this output.
aoqi@0 176 if (PrintGCDetails && Verbose && WizardMode) {
aoqi@0 177 gclog_or_tty->print(" [Symbols: scanned=%d removed=%d size=" SIZE_FORMAT "K] ", *processed, *removed,
aoqi@0 178 (memory_total*HeapWordSize)/1024);
aoqi@0 179 }
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 // Create a new table and using alternate hash code, populate the new table
aoqi@0 183 // with the existing strings. Set flag to use the alternate hash code afterwards.
aoqi@0 184 void SymbolTable::rehash_table() {
aoqi@0 185 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
aoqi@0 186 // This should never happen with -Xshare:dump but it might in testing mode.
aoqi@0 187 if (DumpSharedSpaces) return;
aoqi@0 188 // Create a new symbol table
aoqi@0 189 SymbolTable* new_table = new SymbolTable();
aoqi@0 190
aoqi@0 191 the_table()->move_to(new_table);
aoqi@0 192
aoqi@0 193 // Delete the table and buckets (entries are reused in new table).
aoqi@0 194 delete _the_table;
aoqi@0 195 // Don't check if we need rehashing until the table gets unbalanced again.
aoqi@0 196 // Then rehash with a new global seed.
aoqi@0 197 _needs_rehashing = false;
aoqi@0 198 _the_table = new_table;
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 // Lookup a symbol in a bucket.
aoqi@0 202
aoqi@0 203 Symbol* SymbolTable::lookup(int index, const char* name,
aoqi@0 204 int len, unsigned int hash) {
aoqi@0 205 int count = 0;
aoqi@0 206 for (HashtableEntry<Symbol*, mtSymbol>* e = bucket(index); e != NULL; e = e->next()) {
aoqi@0 207 count++; // count all entries in this bucket, not just ones with same hash
aoqi@0 208 if (e->hash() == hash) {
aoqi@0 209 Symbol* sym = e->literal();
aoqi@0 210 if (sym->equals(name, len)) {
aoqi@0 211 // something is referencing this symbol now.
aoqi@0 212 sym->increment_refcount();
aoqi@0 213 return sym;
aoqi@0 214 }
aoqi@0 215 }
aoqi@0 216 }
aoqi@0 217 // If the bucket size is too deep check if this hash code is insufficient.
mgerdin@7207 218 if (count >= rehash_count && !needs_rehashing()) {
aoqi@0 219 _needs_rehashing = check_rehash_table(count);
aoqi@0 220 }
aoqi@0 221 return NULL;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 // Pick hashing algorithm.
aoqi@0 225 unsigned int SymbolTable::hash_symbol(const char* s, int len) {
aoqi@0 226 return use_alternate_hashcode() ?
aoqi@0 227 AltHashing::murmur3_32(seed(), (const jbyte*)s, len) :
aoqi@0 228 java_lang_String::hash_code(s, len);
aoqi@0 229 }
aoqi@0 230
aoqi@0 231
aoqi@0 232 // We take care not to be blocking while holding the
aoqi@0 233 // SymbolTable_lock. Otherwise, the system might deadlock, since the
aoqi@0 234 // symboltable is used during compilation (VM_thread) The lock free
aoqi@0 235 // synchronization is simplified by the fact that we do not delete
aoqi@0 236 // entries in the symbol table during normal execution (only during
aoqi@0 237 // safepoints).
aoqi@0 238
aoqi@0 239 Symbol* SymbolTable::lookup(const char* name, int len, TRAPS) {
aoqi@0 240 unsigned int hashValue = hash_symbol(name, len);
aoqi@0 241 int index = the_table()->hash_to_index(hashValue);
aoqi@0 242
aoqi@0 243 Symbol* s = the_table()->lookup(index, name, len, hashValue);
aoqi@0 244
aoqi@0 245 // Found
aoqi@0 246 if (s != NULL) return s;
aoqi@0 247
aoqi@0 248 // Grab SymbolTable_lock first.
aoqi@0 249 MutexLocker ml(SymbolTable_lock, THREAD);
aoqi@0 250
aoqi@0 251 // Otherwise, add to symbol to table
aoqi@0 252 return the_table()->basic_add(index, (u1*)name, len, hashValue, true, CHECK_NULL);
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 Symbol* SymbolTable::lookup(const Symbol* sym, int begin, int end, TRAPS) {
aoqi@0 256 char* buffer;
aoqi@0 257 int index, len;
aoqi@0 258 unsigned int hashValue;
aoqi@0 259 char* name;
aoqi@0 260 {
aoqi@0 261 debug_only(No_Safepoint_Verifier nsv;)
aoqi@0 262
aoqi@0 263 name = (char*)sym->base() + begin;
aoqi@0 264 len = end - begin;
aoqi@0 265 hashValue = hash_symbol(name, len);
aoqi@0 266 index = the_table()->hash_to_index(hashValue);
aoqi@0 267 Symbol* s = the_table()->lookup(index, name, len, hashValue);
aoqi@0 268
aoqi@0 269 // Found
aoqi@0 270 if (s != NULL) return s;
aoqi@0 271 }
aoqi@0 272
aoqi@0 273 // Otherwise, add to symbol to table. Copy to a C string first.
aoqi@0 274 char stack_buf[128];
aoqi@0 275 ResourceMark rm(THREAD);
aoqi@0 276 if (len <= 128) {
aoqi@0 277 buffer = stack_buf;
aoqi@0 278 } else {
aoqi@0 279 buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
aoqi@0 280 }
aoqi@0 281 for (int i=0; i<len; i++) {
aoqi@0 282 buffer[i] = name[i];
aoqi@0 283 }
aoqi@0 284 // Make sure there is no safepoint in the code above since name can't move.
aoqi@0 285 // We can't include the code in No_Safepoint_Verifier because of the
aoqi@0 286 // ResourceMark.
aoqi@0 287
aoqi@0 288 // Grab SymbolTable_lock first.
aoqi@0 289 MutexLocker ml(SymbolTable_lock, THREAD);
aoqi@0 290
aoqi@0 291 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, true, CHECK_NULL);
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 Symbol* SymbolTable::lookup_only(const char* name, int len,
aoqi@0 295 unsigned int& hash) {
aoqi@0 296 hash = hash_symbol(name, len);
aoqi@0 297 int index = the_table()->hash_to_index(hash);
aoqi@0 298
aoqi@0 299 Symbol* s = the_table()->lookup(index, name, len, hash);
aoqi@0 300 return s;
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 // Look up the address of the literal in the SymbolTable for this Symbol*
aoqi@0 304 // Do not create any new symbols
aoqi@0 305 // Do not increment the reference count to keep this alive
aoqi@0 306 Symbol** SymbolTable::lookup_symbol_addr(Symbol* sym){
aoqi@0 307 unsigned int hash = hash_symbol((char*)sym->bytes(), sym->utf8_length());
aoqi@0 308 int index = the_table()->hash_to_index(hash);
aoqi@0 309
aoqi@0 310 for (HashtableEntry<Symbol*, mtSymbol>* e = the_table()->bucket(index); e != NULL; e = e->next()) {
aoqi@0 311 if (e->hash() == hash) {
aoqi@0 312 Symbol* literal_sym = e->literal();
aoqi@0 313 if (sym == literal_sym) {
aoqi@0 314 return e->literal_addr();
aoqi@0 315 }
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318 return NULL;
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 // Suggestion: Push unicode-based lookup all the way into the hashing
aoqi@0 322 // and probing logic, so there is no need for convert_to_utf8 until
aoqi@0 323 // an actual new Symbol* is created.
aoqi@0 324 Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) {
aoqi@0 325 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
aoqi@0 326 char stack_buf[128];
aoqi@0 327 if (utf8_length < (int) sizeof(stack_buf)) {
aoqi@0 328 char* chars = stack_buf;
aoqi@0 329 UNICODE::convert_to_utf8(name, utf16_length, chars);
aoqi@0 330 return lookup(chars, utf8_length, THREAD);
aoqi@0 331 } else {
aoqi@0 332 ResourceMark rm(THREAD);
aoqi@0 333 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
aoqi@0 334 UNICODE::convert_to_utf8(name, utf16_length, chars);
aoqi@0 335 return lookup(chars, utf8_length, THREAD);
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length,
aoqi@0 340 unsigned int& hash) {
aoqi@0 341 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
aoqi@0 342 char stack_buf[128];
aoqi@0 343 if (utf8_length < (int) sizeof(stack_buf)) {
aoqi@0 344 char* chars = stack_buf;
aoqi@0 345 UNICODE::convert_to_utf8(name, utf16_length, chars);
aoqi@0 346 return lookup_only(chars, utf8_length, hash);
aoqi@0 347 } else {
aoqi@0 348 ResourceMark rm;
aoqi@0 349 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
aoqi@0 350 UNICODE::convert_to_utf8(name, utf16_length, chars);
aoqi@0 351 return lookup_only(chars, utf8_length, hash);
aoqi@0 352 }
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 void SymbolTable::add(ClassLoaderData* loader_data, constantPoolHandle cp,
aoqi@0 356 int names_count,
aoqi@0 357 const char** names, int* lengths, int* cp_indices,
aoqi@0 358 unsigned int* hashValues, TRAPS) {
aoqi@0 359 // Grab SymbolTable_lock first.
aoqi@0 360 MutexLocker ml(SymbolTable_lock, THREAD);
aoqi@0 361
aoqi@0 362 SymbolTable* table = the_table();
aoqi@0 363 bool added = table->basic_add(loader_data, cp, names_count, names, lengths,
aoqi@0 364 cp_indices, hashValues, CHECK);
aoqi@0 365 if (!added) {
aoqi@0 366 // do it the hard way
aoqi@0 367 for (int i=0; i<names_count; i++) {
aoqi@0 368 int index = table->hash_to_index(hashValues[i]);
aoqi@0 369 bool c_heap = !loader_data->is_the_null_class_loader_data();
aoqi@0 370 Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i], hashValues[i], c_heap, CHECK);
aoqi@0 371 cp->symbol_at_put(cp_indices[i], sym);
aoqi@0 372 }
aoqi@0 373 }
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 Symbol* SymbolTable::new_permanent_symbol(const char* name, TRAPS) {
aoqi@0 377 unsigned int hash;
aoqi@0 378 Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash);
aoqi@0 379 if (result != NULL) {
aoqi@0 380 return result;
aoqi@0 381 }
aoqi@0 382 // Grab SymbolTable_lock first.
aoqi@0 383 MutexLocker ml(SymbolTable_lock, THREAD);
aoqi@0 384
aoqi@0 385 SymbolTable* table = the_table();
aoqi@0 386 int index = table->hash_to_index(hash);
aoqi@0 387 return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD);
aoqi@0 388 }
aoqi@0 389
aoqi@0 390 Symbol* SymbolTable::basic_add(int index_arg, u1 *name, int len,
aoqi@0 391 unsigned int hashValue_arg, bool c_heap, TRAPS) {
aoqi@0 392 assert(!Universe::heap()->is_in_reserved(name),
aoqi@0 393 "proposed name of symbol must be stable");
aoqi@0 394
aoqi@0 395 // Don't allow symbols to be created which cannot fit in a Symbol*.
aoqi@0 396 if (len > Symbol::max_length()) {
aoqi@0 397 THROW_MSG_0(vmSymbols::java_lang_InternalError(),
aoqi@0 398 "name is too long to represent");
aoqi@0 399 }
aoqi@0 400
aoqi@0 401 // Cannot hit a safepoint in this function because the "this" pointer can move.
aoqi@0 402 No_Safepoint_Verifier nsv;
aoqi@0 403
aoqi@0 404 // Check if the symbol table has been rehashed, if so, need to recalculate
aoqi@0 405 // the hash value and index.
aoqi@0 406 unsigned int hashValue;
aoqi@0 407 int index;
aoqi@0 408 if (use_alternate_hashcode()) {
aoqi@0 409 hashValue = hash_symbol((const char*)name, len);
aoqi@0 410 index = hash_to_index(hashValue);
aoqi@0 411 } else {
aoqi@0 412 hashValue = hashValue_arg;
aoqi@0 413 index = index_arg;
aoqi@0 414 }
aoqi@0 415
aoqi@0 416 // Since look-up was done lock-free, we need to check if another
aoqi@0 417 // thread beat us in the race to insert the symbol.
aoqi@0 418 Symbol* test = lookup(index, (char*)name, len, hashValue);
aoqi@0 419 if (test != NULL) {
aoqi@0 420 // A race occurred and another thread introduced the symbol.
aoqi@0 421 assert(test->refcount() != 0, "lookup should have incremented the count");
aoqi@0 422 return test;
aoqi@0 423 }
aoqi@0 424
aoqi@0 425 // Create a new symbol.
aoqi@0 426 Symbol* sym = allocate_symbol(name, len, c_heap, CHECK_NULL);
aoqi@0 427 assert(sym->equals((char*)name, len), "symbol must be properly initialized");
aoqi@0 428
aoqi@0 429 HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym);
aoqi@0 430 add_entry(index, entry);
aoqi@0 431 return sym;
aoqi@0 432 }
aoqi@0 433
aoqi@0 434 // This version of basic_add adds symbols in batch from the constant pool
aoqi@0 435 // parsing.
aoqi@0 436 bool SymbolTable::basic_add(ClassLoaderData* loader_data, constantPoolHandle cp,
aoqi@0 437 int names_count,
aoqi@0 438 const char** names, int* lengths,
aoqi@0 439 int* cp_indices, unsigned int* hashValues,
aoqi@0 440 TRAPS) {
aoqi@0 441
aoqi@0 442 // Check symbol names are not too long. If any are too long, don't add any.
aoqi@0 443 for (int i = 0; i< names_count; i++) {
aoqi@0 444 if (lengths[i] > Symbol::max_length()) {
aoqi@0 445 THROW_MSG_0(vmSymbols::java_lang_InternalError(),
aoqi@0 446 "name is too long to represent");
aoqi@0 447 }
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 // Cannot hit a safepoint in this function because the "this" pointer can move.
aoqi@0 451 No_Safepoint_Verifier nsv;
aoqi@0 452
aoqi@0 453 for (int i=0; i<names_count; i++) {
aoqi@0 454 // Check if the symbol table has been rehashed, if so, need to recalculate
aoqi@0 455 // the hash value.
aoqi@0 456 unsigned int hashValue;
aoqi@0 457 if (use_alternate_hashcode()) {
aoqi@0 458 hashValue = hash_symbol(names[i], lengths[i]);
aoqi@0 459 } else {
aoqi@0 460 hashValue = hashValues[i];
aoqi@0 461 }
aoqi@0 462 // Since look-up was done lock-free, we need to check if another
aoqi@0 463 // thread beat us in the race to insert the symbol.
aoqi@0 464 int index = hash_to_index(hashValue);
aoqi@0 465 Symbol* test = lookup(index, names[i], lengths[i], hashValue);
aoqi@0 466 if (test != NULL) {
aoqi@0 467 // A race occurred and another thread introduced the symbol, this one
aoqi@0 468 // will be dropped and collected. Use test instead.
aoqi@0 469 cp->symbol_at_put(cp_indices[i], test);
aoqi@0 470 assert(test->refcount() != 0, "lookup should have incremented the count");
aoqi@0 471 } else {
aoqi@0 472 // Create a new symbol. The null class loader is never unloaded so these
aoqi@0 473 // are allocated specially in a permanent arena.
aoqi@0 474 bool c_heap = !loader_data->is_the_null_class_loader_data();
aoqi@0 475 Symbol* sym = allocate_symbol((const u1*)names[i], lengths[i], c_heap, CHECK_(false));
aoqi@0 476 assert(sym->equals(names[i], lengths[i]), "symbol must be properly initialized"); // why wouldn't it be???
aoqi@0 477 HashtableEntry<Symbol*, mtSymbol>* entry = new_entry(hashValue, sym);
aoqi@0 478 add_entry(index, entry);
aoqi@0 479 cp->symbol_at_put(cp_indices[i], sym);
aoqi@0 480 }
aoqi@0 481 }
aoqi@0 482 return true;
aoqi@0 483 }
aoqi@0 484
aoqi@0 485
aoqi@0 486 void SymbolTable::verify() {
aoqi@0 487 for (int i = 0; i < the_table()->table_size(); ++i) {
aoqi@0 488 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i);
aoqi@0 489 for ( ; p != NULL; p = p->next()) {
aoqi@0 490 Symbol* s = (Symbol*)(p->literal());
aoqi@0 491 guarantee(s != NULL, "symbol is NULL");
aoqi@0 492 unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length());
aoqi@0 493 guarantee(p->hash() == h, "broken hash in symbol table entry");
aoqi@0 494 guarantee(the_table()->hash_to_index(h) == i,
aoqi@0 495 "wrong index in symbol table");
aoqi@0 496 }
aoqi@0 497 }
aoqi@0 498 }
aoqi@0 499
aoqi@0 500 void SymbolTable::dump(outputStream* st) {
aoqi@0 501 the_table()->dump_table(st, "SymbolTable");
aoqi@0 502 }
aoqi@0 503
aoqi@0 504
aoqi@0 505 //---------------------------------------------------------------------------
aoqi@0 506 // Non-product code
aoqi@0 507
aoqi@0 508 #ifndef PRODUCT
aoqi@0 509
aoqi@0 510 void SymbolTable::print_histogram() {
aoqi@0 511 MutexLocker ml(SymbolTable_lock);
aoqi@0 512 const int results_length = 100;
aoqi@0 513 int results[results_length];
aoqi@0 514 int i,j;
aoqi@0 515
aoqi@0 516 // initialize results to zero
aoqi@0 517 for (j = 0; j < results_length; j++) {
aoqi@0 518 results[j] = 0;
aoqi@0 519 }
aoqi@0 520
aoqi@0 521 int total = 0;
aoqi@0 522 int max_symbols = 0;
aoqi@0 523 int out_of_range = 0;
aoqi@0 524 int memory_total = 0;
aoqi@0 525 int count = 0;
aoqi@0 526 for (i = 0; i < the_table()->table_size(); i++) {
aoqi@0 527 HashtableEntry<Symbol*, mtSymbol>* p = the_table()->bucket(i);
aoqi@0 528 for ( ; p != NULL; p = p->next()) {
aoqi@0 529 memory_total += p->literal()->size();
aoqi@0 530 count++;
aoqi@0 531 int counter = p->literal()->utf8_length();
aoqi@0 532 total += counter;
aoqi@0 533 if (counter < results_length) {
aoqi@0 534 results[counter]++;
aoqi@0 535 } else {
aoqi@0 536 out_of_range++;
aoqi@0 537 }
aoqi@0 538 max_symbols = MAX2(max_symbols, counter);
aoqi@0 539 }
aoqi@0 540 }
aoqi@0 541 tty->print_cr("Symbol Table:");
aoqi@0 542 tty->print_cr("Total number of symbols %5d", count);
aoqi@0 543 tty->print_cr("Total size in memory %5dK",
aoqi@0 544 (memory_total*HeapWordSize)/1024);
aoqi@0 545 tty->print_cr("Total counted %5d", _symbols_counted);
aoqi@0 546 tty->print_cr("Total removed %5d", _symbols_removed);
aoqi@0 547 if (_symbols_counted > 0) {
aoqi@0 548 tty->print_cr("Percent removed %3.2f",
aoqi@0 549 ((float)_symbols_removed/(float)_symbols_counted)* 100);
aoqi@0 550 }
aoqi@0 551 tty->print_cr("Reference counts %5d", Symbol::_total_count);
aoqi@0 552 tty->print_cr("Symbol arena size %5d used %5d",
aoqi@0 553 arena()->size_in_bytes(), arena()->used());
aoqi@0 554 tty->print_cr("Histogram of symbol length:");
aoqi@0 555 tty->print_cr("%8s %5d", "Total ", total);
aoqi@0 556 tty->print_cr("%8s %5d", "Maximum", max_symbols);
aoqi@0 557 tty->print_cr("%8s %3.2f", "Average",
aoqi@0 558 ((float) total / (float) the_table()->table_size()));
aoqi@0 559 tty->print_cr("%s", "Histogram:");
aoqi@0 560 tty->print_cr(" %s %29s", "Length", "Number chains that length");
aoqi@0 561 for (i = 0; i < results_length; i++) {
aoqi@0 562 if (results[i] > 0) {
aoqi@0 563 tty->print_cr("%6d %10d", i, results[i]);
aoqi@0 564 }
aoqi@0 565 }
aoqi@0 566 if (Verbose) {
aoqi@0 567 int line_length = 70;
aoqi@0 568 tty->print_cr("%s %30s", " Length", "Number chains that length");
aoqi@0 569 for (i = 0; i < results_length; i++) {
aoqi@0 570 if (results[i] > 0) {
aoqi@0 571 tty->print("%4d", i);
aoqi@0 572 for (j = 0; (j < results[i]) && (j < line_length); j++) {
aoqi@0 573 tty->print("%1s", "*");
aoqi@0 574 }
aoqi@0 575 if (j == line_length) {
aoqi@0 576 tty->print("%1s", "+");
aoqi@0 577 }
aoqi@0 578 tty->cr();
aoqi@0 579 }
aoqi@0 580 }
aoqi@0 581 }
aoqi@0 582 tty->print_cr(" %s %d: %d\n", "Number chains longer than",
aoqi@0 583 results_length, out_of_range);
aoqi@0 584 }
aoqi@0 585
aoqi@0 586 void SymbolTable::print() {
aoqi@0 587 for (int i = 0; i < the_table()->table_size(); ++i) {
aoqi@0 588 HashtableEntry<Symbol*, mtSymbol>** p = the_table()->bucket_addr(i);
aoqi@0 589 HashtableEntry<Symbol*, mtSymbol>* entry = the_table()->bucket(i);
aoqi@0 590 if (entry != NULL) {
aoqi@0 591 while (entry != NULL) {
aoqi@0 592 tty->print(PTR_FORMAT " ", entry->literal());
aoqi@0 593 entry->literal()->print();
aoqi@0 594 tty->print(" %d", entry->literal()->refcount());
aoqi@0 595 p = entry->next_addr();
aoqi@0 596 entry = (HashtableEntry<Symbol*, mtSymbol>*)HashtableEntry<Symbol*, mtSymbol>::make_ptr(*p);
aoqi@0 597 }
aoqi@0 598 tty->cr();
aoqi@0 599 }
aoqi@0 600 }
aoqi@0 601 }
aoqi@0 602 #endif // PRODUCT
aoqi@0 603
aoqi@0 604 // --------------------------------------------------------------------------
aoqi@0 605
aoqi@0 606 #ifdef ASSERT
aoqi@0 607 class StableMemoryChecker : public StackObj {
aoqi@0 608 enum { _bufsize = wordSize*4 };
aoqi@0 609
aoqi@0 610 address _region;
aoqi@0 611 jint _size;
aoqi@0 612 u1 _save_buf[_bufsize];
aoqi@0 613
aoqi@0 614 int sample(u1* save_buf) {
aoqi@0 615 if (_size <= _bufsize) {
aoqi@0 616 memcpy(save_buf, _region, _size);
aoqi@0 617 return _size;
aoqi@0 618 } else {
aoqi@0 619 // copy head and tail
aoqi@0 620 memcpy(&save_buf[0], _region, _bufsize/2);
aoqi@0 621 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
aoqi@0 622 return (_bufsize/2)*2;
aoqi@0 623 }
aoqi@0 624 }
aoqi@0 625
aoqi@0 626 public:
aoqi@0 627 StableMemoryChecker(const void* region, jint size) {
aoqi@0 628 _region = (address) region;
aoqi@0 629 _size = size;
aoqi@0 630 sample(_save_buf);
aoqi@0 631 }
aoqi@0 632
aoqi@0 633 bool verify() {
aoqi@0 634 u1 check_buf[sizeof(_save_buf)];
aoqi@0 635 int check_size = sample(check_buf);
aoqi@0 636 return (0 == memcmp(_save_buf, check_buf, check_size));
aoqi@0 637 }
aoqi@0 638
aoqi@0 639 void set_region(const void* region) { _region = (address) region; }
aoqi@0 640 };
aoqi@0 641 #endif
aoqi@0 642
aoqi@0 643
aoqi@0 644 // --------------------------------------------------------------------------
aoqi@0 645 StringTable* StringTable::_the_table = NULL;
aoqi@0 646
aoqi@0 647 bool StringTable::_needs_rehashing = false;
aoqi@0 648
aoqi@0 649 volatile int StringTable::_parallel_claimed_idx = 0;
aoqi@0 650
aoqi@0 651 // Pick hashing algorithm
aoqi@0 652 unsigned int StringTable::hash_string(const jchar* s, int len) {
aoqi@0 653 return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) :
aoqi@0 654 java_lang_String::hash_code(s, len);
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 oop StringTable::lookup(int index, jchar* name,
aoqi@0 658 int len, unsigned int hash) {
aoqi@0 659 int count = 0;
aoqi@0 660 for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) {
aoqi@0 661 count++;
aoqi@0 662 if (l->hash() == hash) {
aoqi@0 663 if (java_lang_String::equals(l->literal(), name, len)) {
aoqi@0 664 return l->literal();
aoqi@0 665 }
aoqi@0 666 }
aoqi@0 667 }
aoqi@0 668 // If the bucket size is too deep check if this hash code is insufficient.
mgerdin@7207 669 if (count >= rehash_count && !needs_rehashing()) {
aoqi@0 670 _needs_rehashing = check_rehash_table(count);
aoqi@0 671 }
aoqi@0 672 return NULL;
aoqi@0 673 }
aoqi@0 674
aoqi@0 675
aoqi@0 676 oop StringTable::basic_add(int index_arg, Handle string, jchar* name,
aoqi@0 677 int len, unsigned int hashValue_arg, TRAPS) {
aoqi@0 678
aoqi@0 679 assert(java_lang_String::equals(string(), name, len),
aoqi@0 680 "string must be properly initialized");
aoqi@0 681 // Cannot hit a safepoint in this function because the "this" pointer can move.
aoqi@0 682 No_Safepoint_Verifier nsv;
aoqi@0 683
aoqi@0 684 // Check if the symbol table has been rehashed, if so, need to recalculate
aoqi@0 685 // the hash value and index before second lookup.
aoqi@0 686 unsigned int hashValue;
aoqi@0 687 int index;
aoqi@0 688 if (use_alternate_hashcode()) {
aoqi@0 689 hashValue = hash_string(name, len);
aoqi@0 690 index = hash_to_index(hashValue);
aoqi@0 691 } else {
aoqi@0 692 hashValue = hashValue_arg;
aoqi@0 693 index = index_arg;
aoqi@0 694 }
aoqi@0 695
aoqi@0 696 // Since look-up was done lock-free, we need to check if another
aoqi@0 697 // thread beat us in the race to insert the symbol.
aoqi@0 698
aoqi@0 699 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int)
aoqi@0 700 if (test != NULL) {
aoqi@0 701 // Entry already added
aoqi@0 702 return test;
aoqi@0 703 }
aoqi@0 704
aoqi@0 705 HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string());
aoqi@0 706 add_entry(index, entry);
aoqi@0 707 return string();
aoqi@0 708 }
aoqi@0 709
aoqi@0 710
aoqi@0 711 oop StringTable::lookup(Symbol* symbol) {
aoqi@0 712 ResourceMark rm;
aoqi@0 713 int length;
aoqi@0 714 jchar* chars = symbol->as_unicode(length);
aoqi@0 715 return lookup(chars, length);
aoqi@0 716 }
aoqi@0 717
stefank@6992 718 // Tell the GC that this string was looked up in the StringTable.
stefank@6992 719 static void ensure_string_alive(oop string) {
stefank@6992 720 // A lookup in the StringTable could return an object that was previously
stefank@6992 721 // considered dead. The SATB part of G1 needs to get notified about this
stefank@6992 722 // potential resurrection, otherwise the marking might not find the object.
stefank@6992 723 #if INCLUDE_ALL_GCS
stefank@6992 724 if (UseG1GC && string != NULL) {
stefank@6992 725 G1SATBCardTableModRefBS::enqueue(string);
stefank@6992 726 }
stefank@6992 727 #endif
stefank@6992 728 }
aoqi@0 729
aoqi@0 730 oop StringTable::lookup(jchar* name, int len) {
aoqi@0 731 unsigned int hash = hash_string(name, len);
aoqi@0 732 int index = the_table()->hash_to_index(hash);
stefank@6992 733 oop string = the_table()->lookup(index, name, len, hash);
stefank@6992 734
stefank@6992 735 ensure_string_alive(string);
stefank@6992 736
stefank@6992 737 return string;
aoqi@0 738 }
aoqi@0 739
aoqi@0 740
aoqi@0 741 oop StringTable::intern(Handle string_or_null, jchar* name,
aoqi@0 742 int len, TRAPS) {
aoqi@0 743 unsigned int hashValue = hash_string(name, len);
aoqi@0 744 int index = the_table()->hash_to_index(hashValue);
aoqi@0 745 oop found_string = the_table()->lookup(index, name, len, hashValue);
aoqi@0 746
aoqi@0 747 // Found
stefank@6992 748 if (found_string != NULL) {
stefank@6992 749 ensure_string_alive(found_string);
stefank@6992 750 return found_string;
stefank@6992 751 }
aoqi@0 752
aoqi@0 753 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
aoqi@0 754 assert(!Universe::heap()->is_in_reserved(name),
aoqi@0 755 "proposed name of symbol must be stable");
aoqi@0 756
aoqi@0 757 Handle string;
aoqi@0 758 // try to reuse the string if possible
aoqi@0 759 if (!string_or_null.is_null()) {
aoqi@0 760 string = string_or_null;
aoqi@0 761 } else {
aoqi@0 762 string = java_lang_String::create_from_unicode(name, len, CHECK_NULL);
aoqi@0 763 }
aoqi@0 764
aoqi@0 765 #if INCLUDE_ALL_GCS
aoqi@0 766 if (G1StringDedup::is_enabled()) {
aoqi@0 767 // Deduplicate the string before it is interned. Note that we should never
aoqi@0 768 // deduplicate a string after it has been interned. Doing so will counteract
aoqi@0 769 // compiler optimizations done on e.g. interned string literals.
aoqi@0 770 G1StringDedup::deduplicate(string());
aoqi@0 771 }
aoqi@0 772 #endif
aoqi@0 773
aoqi@0 774 // Grab the StringTable_lock before getting the_table() because it could
aoqi@0 775 // change at safepoint.
stefank@6992 776 oop added_or_found;
stefank@6992 777 {
stefank@6992 778 MutexLocker ml(StringTable_lock, THREAD);
stefank@6992 779 // Otherwise, add to symbol to table
stefank@6992 780 added_or_found = the_table()->basic_add(index, string, name, len,
stefank@6992 781 hashValue, CHECK_NULL);
stefank@6992 782 }
aoqi@0 783
stefank@6992 784 ensure_string_alive(added_or_found);
stefank@6992 785
stefank@6992 786 return added_or_found;
aoqi@0 787 }
aoqi@0 788
aoqi@0 789 oop StringTable::intern(Symbol* symbol, TRAPS) {
aoqi@0 790 if (symbol == NULL) return NULL;
aoqi@0 791 ResourceMark rm(THREAD);
aoqi@0 792 int length;
aoqi@0 793 jchar* chars = symbol->as_unicode(length);
aoqi@0 794 Handle string;
aoqi@0 795 oop result = intern(string, chars, length, CHECK_NULL);
aoqi@0 796 return result;
aoqi@0 797 }
aoqi@0 798
aoqi@0 799
aoqi@0 800 oop StringTable::intern(oop string, TRAPS)
aoqi@0 801 {
aoqi@0 802 if (string == NULL) return NULL;
aoqi@0 803 ResourceMark rm(THREAD);
aoqi@0 804 int length;
aoqi@0 805 Handle h_string (THREAD, string);
aoqi@0 806 jchar* chars = java_lang_String::as_unicode_string(string, length, CHECK_NULL);
aoqi@0 807 oop result = intern(h_string, chars, length, CHECK_NULL);
aoqi@0 808 return result;
aoqi@0 809 }
aoqi@0 810
aoqi@0 811
aoqi@0 812 oop StringTable::intern(const char* utf8_string, TRAPS) {
aoqi@0 813 if (utf8_string == NULL) return NULL;
aoqi@0 814 ResourceMark rm(THREAD);
aoqi@0 815 int length = UTF8::unicode_length(utf8_string);
aoqi@0 816 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
aoqi@0 817 UTF8::convert_to_unicode(utf8_string, chars, length);
aoqi@0 818 Handle string;
aoqi@0 819 oop result = intern(string, chars, length, CHECK_NULL);
aoqi@0 820 return result;
aoqi@0 821 }
aoqi@0 822
aoqi@0 823 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) {
tschatzl@8766 824 BucketUnlinkContext context;
tschatzl@8766 825 buckets_unlink_or_oops_do(is_alive, f, 0, the_table()->table_size(), &context);
tschatzl@8766 826 _the_table->bulk_free_entries(&context);
tschatzl@8766 827 *processed = context._num_processed;
tschatzl@8766 828 *removed = context._num_removed;
aoqi@0 829 }
aoqi@0 830
aoqi@0 831 void StringTable::possibly_parallel_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) {
aoqi@0 832 // Readers of the table are unlocked, so we should only be removing
aoqi@0 833 // entries at a safepoint.
aoqi@0 834 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
aoqi@0 835 const int limit = the_table()->table_size();
aoqi@0 836
tschatzl@8766 837 BucketUnlinkContext context;
aoqi@0 838 for (;;) {
aoqi@0 839 // Grab next set of buckets to scan
aoqi@0 840 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize;
aoqi@0 841 if (start_idx >= limit) {
aoqi@0 842 // End of table
aoqi@0 843 break;
aoqi@0 844 }
aoqi@0 845
aoqi@0 846 int end_idx = MIN2(limit, start_idx + ClaimChunkSize);
tschatzl@8766 847 buckets_unlink_or_oops_do(is_alive, f, start_idx, end_idx, &context);
aoqi@0 848 }
tschatzl@8766 849 _the_table->bulk_free_entries(&context);
tschatzl@8766 850 *processed = context._num_processed;
tschatzl@8766 851 *removed = context._num_removed;
aoqi@0 852 }
aoqi@0 853
aoqi@0 854 void StringTable::buckets_oops_do(OopClosure* f, int start_idx, int end_idx) {
aoqi@0 855 const int limit = the_table()->table_size();
aoqi@0 856
aoqi@0 857 assert(0 <= start_idx && start_idx <= limit,
aoqi@0 858 err_msg("start_idx (" INT32_FORMAT ") is out of bounds", start_idx));
aoqi@0 859 assert(0 <= end_idx && end_idx <= limit,
aoqi@0 860 err_msg("end_idx (" INT32_FORMAT ") is out of bounds", end_idx));
aoqi@0 861 assert(start_idx <= end_idx,
aoqi@0 862 err_msg("Index ordering: start_idx=" INT32_FORMAT", end_idx=" INT32_FORMAT,
aoqi@0 863 start_idx, end_idx));
aoqi@0 864
aoqi@0 865 for (int i = start_idx; i < end_idx; i += 1) {
aoqi@0 866 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i);
aoqi@0 867 while (entry != NULL) {
aoqi@0 868 assert(!entry->is_shared(), "CDS not used for the StringTable");
aoqi@0 869
aoqi@0 870 f->do_oop((oop*)entry->literal_addr());
aoqi@0 871
aoqi@0 872 entry = entry->next();
aoqi@0 873 }
aoqi@0 874 }
aoqi@0 875 }
aoqi@0 876
tschatzl@8766 877 void StringTable::buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, BucketUnlinkContext* context) {
aoqi@0 878 const int limit = the_table()->table_size();
aoqi@0 879
aoqi@0 880 assert(0 <= start_idx && start_idx <= limit,
aoqi@0 881 err_msg("start_idx (" INT32_FORMAT ") is out of bounds", start_idx));
aoqi@0 882 assert(0 <= end_idx && end_idx <= limit,
aoqi@0 883 err_msg("end_idx (" INT32_FORMAT ") is out of bounds", end_idx));
aoqi@0 884 assert(start_idx <= end_idx,
aoqi@0 885 err_msg("Index ordering: start_idx=" INT32_FORMAT", end_idx=" INT32_FORMAT,
aoqi@0 886 start_idx, end_idx));
aoqi@0 887
aoqi@0 888 for (int i = start_idx; i < end_idx; ++i) {
aoqi@0 889 HashtableEntry<oop, mtSymbol>** p = the_table()->bucket_addr(i);
aoqi@0 890 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i);
aoqi@0 891 while (entry != NULL) {
aoqi@0 892 assert(!entry->is_shared(), "CDS not used for the StringTable");
aoqi@0 893
aoqi@0 894 if (is_alive->do_object_b(entry->literal())) {
aoqi@0 895 if (f != NULL) {
aoqi@0 896 f->do_oop((oop*)entry->literal_addr());
aoqi@0 897 }
aoqi@0 898 p = entry->next_addr();
aoqi@0 899 } else {
aoqi@0 900 *p = entry->next();
tschatzl@8766 901 context->free_entry(entry);
aoqi@0 902 }
tschatzl@8766 903 context->_num_processed++;
aoqi@0 904 entry = *p;
aoqi@0 905 }
aoqi@0 906 }
aoqi@0 907 }
aoqi@0 908
aoqi@0 909 void StringTable::oops_do(OopClosure* f) {
aoqi@0 910 buckets_oops_do(f, 0, the_table()->table_size());
aoqi@0 911 }
aoqi@0 912
aoqi@0 913 void StringTable::possibly_parallel_oops_do(OopClosure* f) {
aoqi@0 914 const int limit = the_table()->table_size();
aoqi@0 915
aoqi@0 916 for (;;) {
aoqi@0 917 // Grab next set of buckets to scan
aoqi@0 918 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize;
aoqi@0 919 if (start_idx >= limit) {
aoqi@0 920 // End of table
aoqi@0 921 break;
aoqi@0 922 }
aoqi@0 923
aoqi@0 924 int end_idx = MIN2(limit, start_idx + ClaimChunkSize);
aoqi@0 925 buckets_oops_do(f, start_idx, end_idx);
aoqi@0 926 }
aoqi@0 927 }
aoqi@0 928
aoqi@0 929 // This verification is part of Universe::verify() and needs to be quick.
aoqi@0 930 // See StringTable::verify_and_compare() below for exhaustive verification.
aoqi@0 931 void StringTable::verify() {
aoqi@0 932 for (int i = 0; i < the_table()->table_size(); ++i) {
aoqi@0 933 HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i);
aoqi@0 934 for ( ; p != NULL; p = p->next()) {
aoqi@0 935 oop s = p->literal();
aoqi@0 936 guarantee(s != NULL, "interned string is NULL");
aoqi@0 937 unsigned int h = java_lang_String::hash_string(s);
aoqi@0 938 guarantee(p->hash() == h, "broken hash in string table entry");
aoqi@0 939 guarantee(the_table()->hash_to_index(h) == i,
aoqi@0 940 "wrong index in string table");
aoqi@0 941 }
aoqi@0 942 }
aoqi@0 943 }
aoqi@0 944
aoqi@0 945 void StringTable::dump(outputStream* st) {
aoqi@0 946 the_table()->dump_table(st, "StringTable");
aoqi@0 947 }
aoqi@0 948
aoqi@0 949 StringTable::VerifyRetTypes StringTable::compare_entries(
aoqi@0 950 int bkt1, int e_cnt1,
aoqi@0 951 HashtableEntry<oop, mtSymbol>* e_ptr1,
aoqi@0 952 int bkt2, int e_cnt2,
aoqi@0 953 HashtableEntry<oop, mtSymbol>* e_ptr2) {
aoqi@0 954 // These entries are sanity checked by verify_and_compare_entries()
aoqi@0 955 // before this function is called.
aoqi@0 956 oop str1 = e_ptr1->literal();
aoqi@0 957 oop str2 = e_ptr2->literal();
aoqi@0 958
aoqi@0 959 if (str1 == str2) {
aoqi@0 960 tty->print_cr("ERROR: identical oop values (0x" PTR_FORMAT ") "
aoqi@0 961 "in entry @ bucket[%d][%d] and entry @ bucket[%d][%d]",
aoqi@0 962 (void *)str1, bkt1, e_cnt1, bkt2, e_cnt2);
aoqi@0 963 return _verify_fail_continue;
aoqi@0 964 }
aoqi@0 965
aoqi@0 966 if (java_lang_String::equals(str1, str2)) {
aoqi@0 967 tty->print_cr("ERROR: identical String values in entry @ "
aoqi@0 968 "bucket[%d][%d] and entry @ bucket[%d][%d]",
aoqi@0 969 bkt1, e_cnt1, bkt2, e_cnt2);
aoqi@0 970 return _verify_fail_continue;
aoqi@0 971 }
aoqi@0 972
aoqi@0 973 return _verify_pass;
aoqi@0 974 }
aoqi@0 975
aoqi@0 976 StringTable::VerifyRetTypes StringTable::verify_entry(int bkt, int e_cnt,
aoqi@0 977 HashtableEntry<oop, mtSymbol>* e_ptr,
aoqi@0 978 StringTable::VerifyMesgModes mesg_mode) {
aoqi@0 979
aoqi@0 980 VerifyRetTypes ret = _verify_pass; // be optimistic
aoqi@0 981
aoqi@0 982 oop str = e_ptr->literal();
aoqi@0 983 if (str == NULL) {
aoqi@0 984 if (mesg_mode == _verify_with_mesgs) {
aoqi@0 985 tty->print_cr("ERROR: NULL oop value in entry @ bucket[%d][%d]", bkt,
aoqi@0 986 e_cnt);
aoqi@0 987 }
aoqi@0 988 // NULL oop means no more verifications are possible
aoqi@0 989 return _verify_fail_done;
aoqi@0 990 }
aoqi@0 991
aoqi@0 992 if (str->klass() != SystemDictionary::String_klass()) {
aoqi@0 993 if (mesg_mode == _verify_with_mesgs) {
aoqi@0 994 tty->print_cr("ERROR: oop is not a String in entry @ bucket[%d][%d]",
aoqi@0 995 bkt, e_cnt);
aoqi@0 996 }
aoqi@0 997 // not a String means no more verifications are possible
aoqi@0 998 return _verify_fail_done;
aoqi@0 999 }
aoqi@0 1000
aoqi@0 1001 unsigned int h = java_lang_String::hash_string(str);
aoqi@0 1002 if (e_ptr->hash() != h) {
aoqi@0 1003 if (mesg_mode == _verify_with_mesgs) {
aoqi@0 1004 tty->print_cr("ERROR: broken hash value in entry @ bucket[%d][%d], "
aoqi@0 1005 "bkt_hash=%d, str_hash=%d", bkt, e_cnt, e_ptr->hash(), h);
aoqi@0 1006 }
aoqi@0 1007 ret = _verify_fail_continue;
aoqi@0 1008 }
aoqi@0 1009
aoqi@0 1010 if (the_table()->hash_to_index(h) != bkt) {
aoqi@0 1011 if (mesg_mode == _verify_with_mesgs) {
aoqi@0 1012 tty->print_cr("ERROR: wrong index value for entry @ bucket[%d][%d], "
aoqi@0 1013 "str_hash=%d, hash_to_index=%d", bkt, e_cnt, h,
aoqi@0 1014 the_table()->hash_to_index(h));
aoqi@0 1015 }
aoqi@0 1016 ret = _verify_fail_continue;
aoqi@0 1017 }
aoqi@0 1018
aoqi@0 1019 return ret;
aoqi@0 1020 }
aoqi@0 1021
aoqi@0 1022 // See StringTable::verify() above for the quick verification that is
aoqi@0 1023 // part of Universe::verify(). This verification is exhaustive and
aoqi@0 1024 // reports on every issue that is found. StringTable::verify() only
aoqi@0 1025 // reports on the first issue that is found.
aoqi@0 1026 //
aoqi@0 1027 // StringTable::verify_entry() checks:
aoqi@0 1028 // - oop value != NULL (same as verify())
aoqi@0 1029 // - oop value is a String
aoqi@0 1030 // - hash(String) == hash in entry (same as verify())
aoqi@0 1031 // - index for hash == index of entry (same as verify())
aoqi@0 1032 //
aoqi@0 1033 // StringTable::compare_entries() checks:
aoqi@0 1034 // - oops are unique across all entries
aoqi@0 1035 // - String values are unique across all entries
aoqi@0 1036 //
aoqi@0 1037 int StringTable::verify_and_compare_entries() {
aoqi@0 1038 assert(StringTable_lock->is_locked(), "sanity check");
aoqi@0 1039
aoqi@0 1040 int fail_cnt = 0;
aoqi@0 1041
aoqi@0 1042 // first, verify all the entries individually:
aoqi@0 1043 for (int bkt = 0; bkt < the_table()->table_size(); bkt++) {
aoqi@0 1044 HashtableEntry<oop, mtSymbol>* e_ptr = the_table()->bucket(bkt);
aoqi@0 1045 for (int e_cnt = 0; e_ptr != NULL; e_ptr = e_ptr->next(), e_cnt++) {
aoqi@0 1046 VerifyRetTypes ret = verify_entry(bkt, e_cnt, e_ptr, _verify_with_mesgs);
aoqi@0 1047 if (ret != _verify_pass) {
aoqi@0 1048 fail_cnt++;
aoqi@0 1049 }
aoqi@0 1050 }
aoqi@0 1051 }
aoqi@0 1052
aoqi@0 1053 // Optimization: if the above check did not find any failures, then
aoqi@0 1054 // the comparison loop below does not need to call verify_entry()
aoqi@0 1055 // before calling compare_entries(). If there were failures, then we
aoqi@0 1056 // have to call verify_entry() to see if the entry can be passed to
aoqi@0 1057 // compare_entries() safely. When we call verify_entry() in the loop
aoqi@0 1058 // below, we do so quietly to void duplicate messages and we don't
aoqi@0 1059 // increment fail_cnt because the failures have already been counted.
aoqi@0 1060 bool need_entry_verify = (fail_cnt != 0);
aoqi@0 1061
aoqi@0 1062 // second, verify all entries relative to each other:
aoqi@0 1063 for (int bkt1 = 0; bkt1 < the_table()->table_size(); bkt1++) {
aoqi@0 1064 HashtableEntry<oop, mtSymbol>* e_ptr1 = the_table()->bucket(bkt1);
aoqi@0 1065 for (int e_cnt1 = 0; e_ptr1 != NULL; e_ptr1 = e_ptr1->next(), e_cnt1++) {
aoqi@0 1066 if (need_entry_verify) {
aoqi@0 1067 VerifyRetTypes ret = verify_entry(bkt1, e_cnt1, e_ptr1,
aoqi@0 1068 _verify_quietly);
aoqi@0 1069 if (ret == _verify_fail_done) {
aoqi@0 1070 // cannot use the current entry to compare against other entries
aoqi@0 1071 continue;
aoqi@0 1072 }
aoqi@0 1073 }
aoqi@0 1074
aoqi@0 1075 for (int bkt2 = bkt1; bkt2 < the_table()->table_size(); bkt2++) {
aoqi@0 1076 HashtableEntry<oop, mtSymbol>* e_ptr2 = the_table()->bucket(bkt2);
aoqi@0 1077 int e_cnt2;
aoqi@0 1078 for (e_cnt2 = 0; e_ptr2 != NULL; e_ptr2 = e_ptr2->next(), e_cnt2++) {
aoqi@0 1079 if (bkt1 == bkt2 && e_cnt2 <= e_cnt1) {
aoqi@0 1080 // skip the entries up to and including the one that
aoqi@0 1081 // we're comparing against
aoqi@0 1082 continue;
aoqi@0 1083 }
aoqi@0 1084
aoqi@0 1085 if (need_entry_verify) {
aoqi@0 1086 VerifyRetTypes ret = verify_entry(bkt2, e_cnt2, e_ptr2,
aoqi@0 1087 _verify_quietly);
aoqi@0 1088 if (ret == _verify_fail_done) {
aoqi@0 1089 // cannot compare against this entry
aoqi@0 1090 continue;
aoqi@0 1091 }
aoqi@0 1092 }
aoqi@0 1093
aoqi@0 1094 // compare two entries, report and count any failures:
aoqi@0 1095 if (compare_entries(bkt1, e_cnt1, e_ptr1, bkt2, e_cnt2, e_ptr2)
aoqi@0 1096 != _verify_pass) {
aoqi@0 1097 fail_cnt++;
aoqi@0 1098 }
aoqi@0 1099 }
aoqi@0 1100 }
aoqi@0 1101 }
aoqi@0 1102 }
aoqi@0 1103 return fail_cnt;
aoqi@0 1104 }
aoqi@0 1105
aoqi@0 1106 // Create a new table and using alternate hash code, populate the new table
aoqi@0 1107 // with the existing strings. Set flag to use the alternate hash code afterwards.
aoqi@0 1108 void StringTable::rehash_table() {
aoqi@0 1109 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
aoqi@0 1110 // This should never happen with -Xshare:dump but it might in testing mode.
aoqi@0 1111 if (DumpSharedSpaces) return;
aoqi@0 1112 StringTable* new_table = new StringTable();
aoqi@0 1113
aoqi@0 1114 // Rehash the table
aoqi@0 1115 the_table()->move_to(new_table);
aoqi@0 1116
aoqi@0 1117 // Delete the table and buckets (entries are reused in new table).
aoqi@0 1118 delete _the_table;
aoqi@0 1119 // Don't check if we need rehashing until the table gets unbalanced again.
aoqi@0 1120 // Then rehash with a new global seed.
aoqi@0 1121 _needs_rehashing = false;
aoqi@0 1122 _the_table = new_table;
aoqi@0 1123 }

mercurial