src/share/vm/classfile/symbolTable.cpp

Sat, 24 Oct 2020 16:43:47 +0800

author
aoqi
date
Sat, 24 Oct 2020 16:43:47 +0800
changeset 10015
eb7ce841ccec
parent 8856
ac27a9c85bea
parent 10009
8adf45218add
permissions
-rw-r--r--

Merge

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

mercurial