src/share/vm/classfile/symbolTable.cpp

Thu, 22 May 2014 15:52:41 -0400

author
drchase
date
Thu, 22 May 2014 15:52:41 -0400
changeset 6680
78bbf4d43a14
parent 6413
595c0f60d50d
child 6876
710a3c8b516e
child 6992
2c6ef90f030a
permissions
-rw-r--r--

8037816: Fix for 8036122 breaks build with Xcode5/clang
8043029: Change 8037816 breaks HS build with older GCC versions which don't support diagnostic pragmas
8043164: Format warning in traceStream.hpp
Summary: Backport of main fix + two corrections, enables clang compilation, turns on format attributes, corrects/mutes warnings
Reviewed-by: kvn, coleenp, iveresov, twisti

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

mercurial