src/share/vm/classfile/symbolTable.cpp

Tue, 08 Aug 2017 15:57:29 +0800

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

mercurial