src/share/vm/classfile/symbolTable.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7207
152cf4afc11f
parent 6876
710a3c8b516e
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

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

mercurial