src/share/vm/classfile/symbolTable.cpp

Fri, 29 Aug 2014 13:08:01 +0200

author
mgerdin
date
Fri, 29 Aug 2014 13:08:01 +0200
changeset 7207
152cf4afc11f
parent 7074
833b0f92429a
child 7535
7ae4e26cb1e0
child 8766
ce9a710b0f63
permissions
-rw-r--r--

8056084: Refactor Hashtable to allow implementations without rehashing support
Reviewed-by: gziemski, jmasa, brutisso, coleenp, tschatzl

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

mercurial