src/share/vm/classfile/symbolTable.cpp

Tue, 18 Mar 2014 19:07:22 +0100

author
pliden
date
Tue, 18 Mar 2014 19:07:22 +0100
changeset 6413
595c0f60d50d
parent 6229
5a32d2a3cc1e
child 6680
78bbf4d43a14
permissions
-rw-r--r--

8029075: String deduplication in G1
Summary: Implementation of JEP 192, http://openjdk.java.net/jeps/192
Reviewed-by: brutisso, tschatzl, coleenp

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

mercurial