src/share/vm/classfile/symbolTable.cpp

Mon, 25 Jun 2012 21:33:35 -0400

author
coleenp
date
Mon, 25 Jun 2012 21:33:35 -0400
changeset 3875
246d977b51f2
parent 3865
e9140bf80b4a
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
Summary: Cannot delete _buckets and HashtableEntries in shared space (CDS)
Reviewed-by: acorn, kvn, dlong, dcubed, kamg

duke@435 1 /*
coleenp@3682 2 * Copyright (c) 1997, 2012, 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"
coleenp@3865 38 #include "utilities/numberSeq.hpp"
duke@435 39
duke@435 40 // --------------------------------------------------------------------------
duke@435 41
duke@435 42 SymbolTable* SymbolTable::_the_table = NULL;
coleenp@3682 43 // Static arena for symbols that are not deallocated
coleenp@3682 44 Arena* SymbolTable::_arena = NULL;
coleenp@3865 45 bool SymbolTable::_needs_rehashing = false;
coleenp@3865 46 jint SymbolTable::_seed = 0;
duke@435 47
coleenp@3682 48 Symbol* SymbolTable::allocate_symbol(const u1* name, int len, bool c_heap, TRAPS) {
coleenp@3875 49 assert (len <= Symbol::max_length(), "should be checked by caller");
coleenp@3875 50
coleenp@3682 51 Symbol* sym;
coleenp@3682 52 // Allocate symbols in the C heap when dumping shared spaces in case there
coleenp@3682 53 // are temporary symbols we can remove.
coleenp@3682 54 if (c_heap || DumpSharedSpaces) {
coleenp@3682 55 // refcount starts as 1
coleenp@3682 56 sym = new (len, THREAD) Symbol(name, len, 1);
coleenp@3682 57 } else {
coleenp@3682 58 sym = new (len, arena(), THREAD) Symbol(name, len, -1);
coleenp@3682 59 }
coleenp@2497 60 assert(sym != NULL, "new should call vm_exit_out_of_memory if C_HEAP is exhausted");
coleenp@2497 61 return sym;
coleenp@2497 62 }
coleenp@2497 63
coleenp@3682 64 void SymbolTable::initialize_symbols(int arena_alloc_size) {
coleenp@3682 65 // Initialize the arena for global symbols, size passed in depends on CDS.
coleenp@3682 66 if (arena_alloc_size == 0) {
coleenp@3682 67 _arena = new Arena();
coleenp@3682 68 } else {
coleenp@3682 69 _arena = new Arena(arena_alloc_size);
coleenp@2497 70 }
coleenp@2497 71 }
coleenp@2497 72
coleenp@2497 73 // Call function for all symbols in the symbol table.
coleenp@2497 74 void SymbolTable::symbols_do(SymbolClosure *cl) {
coleenp@2497 75 const int n = the_table()->table_size();
coleenp@2497 76 for (int i = 0; i < n; i++) {
coleenp@2497 77 for (HashtableEntry<Symbol*>* p = the_table()->bucket(i);
coleenp@2497 78 p != NULL;
coleenp@2497 79 p = p->next()) {
coleenp@2497 80 cl->do_symbol(p->literal_addr());
coleenp@2497 81 }
coleenp@2497 82 }
coleenp@2497 83 }
coleenp@2497 84
coleenp@2497 85 int SymbolTable::symbols_removed = 0;
coleenp@2497 86 int SymbolTable::symbols_counted = 0;
coleenp@2497 87
coleenp@2497 88 // Remove unreferenced symbols from the symbol table
coleenp@3682 89 // This is done late during GC.
coleenp@2497 90 void SymbolTable::unlink() {
coleenp@2497 91 int removed = 0;
coleenp@2497 92 int total = 0;
coleenp@2618 93 size_t memory_total = 0;
coleenp@2497 94 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@3875 95 HashtableEntry<Symbol*>** p = the_table()->bucket_addr(i);
coleenp@3875 96 HashtableEntry<Symbol*>* entry = the_table()->bucket(i);
coleenp@3875 97 while (entry != NULL) {
coleenp@3875 98 // Shared entries are normally at the end of the bucket and if we run into
coleenp@3875 99 // a shared entry, then there is nothing more to remove. However, if we
coleenp@3875 100 // have rehashed the table, then the shared entries are no longer at the
coleenp@3875 101 // end of the bucket.
coleenp@3875 102 if (entry->is_shared() && !use_alternate_hashcode()) {
coleenp@2497 103 break;
coleenp@2497 104 }
coleenp@2497 105 Symbol* s = entry->literal();
coleenp@2497 106 memory_total += s->object_size();
coleenp@2497 107 total++;
coleenp@2497 108 assert(s != NULL, "just checking");
coleenp@2497 109 // If reference count is zero, remove.
coleenp@2497 110 if (s->refcount() == 0) {
coleenp@3875 111 assert(!entry->is_shared(), "shared entries should be kept live");
coleenp@2497 112 delete s;
coleenp@2497 113 removed++;
coleenp@2497 114 *p = entry->next();
coleenp@2497 115 the_table()->free_entry(entry);
coleenp@2497 116 } else {
coleenp@2497 117 p = entry->next_addr();
coleenp@2497 118 }
coleenp@3875 119 // get next entry
coleenp@3875 120 entry = (HashtableEntry<Symbol*>*)HashtableEntry<Symbol*>::make_ptr(*p);
coleenp@2497 121 }
coleenp@2497 122 }
coleenp@2497 123 symbols_removed += removed;
coleenp@2497 124 symbols_counted += total;
coleenp@2618 125 // Exclude printing for normal PrintGCDetails because people parse
coleenp@2618 126 // this output.
coleenp@2618 127 if (PrintGCDetails && Verbose && WizardMode) {
coleenp@2618 128 gclog_or_tty->print(" [Symbols=%d size=" SIZE_FORMAT "K] ", total,
coleenp@2497 129 (memory_total*HeapWordSize)/1024);
coleenp@2497 130 }
coleenp@2497 131 }
coleenp@2497 132
coleenp@3865 133 unsigned int SymbolTable::new_hash(Symbol* sym) {
coleenp@3865 134 ResourceMark rm;
coleenp@3865 135 // Use alternate hashing algorithm on this symbol.
coleenp@3865 136 return AltHashing::murmur3_32(seed(), (const jbyte*)sym->as_C_string(), sym->utf8_length());
coleenp@3865 137 }
coleenp@3865 138
coleenp@3865 139 // Create a new table and using alternate hash code, populate the new table
coleenp@3865 140 // with the existing strings. Set flag to use the alternate hash code afterwards.
coleenp@3865 141 void SymbolTable::rehash_table() {
coleenp@3865 142 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
coleenp@3875 143 // This should never happen with -Xshare:dump but it might in testing mode.
coleenp@3875 144 if (DumpSharedSpaces) return;
coleenp@3865 145 // Create a new symbol table
coleenp@3865 146 SymbolTable* new_table = new SymbolTable();
coleenp@3865 147
coleenp@3865 148 // Initialize the global seed for hashing.
coleenp@3865 149 _seed = AltHashing::compute_seed();
coleenp@3865 150 assert(seed() != 0, "shouldn't be zero");
coleenp@3865 151
coleenp@3865 152 the_table()->move_to(new_table);
coleenp@3865 153
coleenp@3865 154 // Delete the table and buckets (entries are reused in new table).
coleenp@3865 155 delete _the_table;
coleenp@3865 156 // Don't check if we need rehashing until the table gets unbalanced again.
coleenp@3865 157 // Then rehash with a new global seed.
coleenp@3865 158 _needs_rehashing = false;
coleenp@3865 159 _the_table = new_table;
coleenp@3865 160 }
coleenp@2497 161
duke@435 162 // Lookup a symbol in a bucket.
duke@435 163
coleenp@2497 164 Symbol* SymbolTable::lookup(int index, const char* name,
duke@435 165 int len, unsigned int hash) {
coleenp@3865 166 int count = 0;
coleenp@2497 167 for (HashtableEntry<Symbol*>* e = bucket(index); e != NULL; e = e->next()) {
coleenp@3865 168 count++; // count all entries in this bucket, not just ones with same hash
duke@435 169 if (e->hash() == hash) {
coleenp@2497 170 Symbol* sym = e->literal();
duke@435 171 if (sym->equals(name, len)) {
coleenp@2497 172 // something is referencing this symbol now.
coleenp@2497 173 sym->increment_refcount();
duke@435 174 return sym;
duke@435 175 }
duke@435 176 }
duke@435 177 }
coleenp@3865 178 // If the bucket size is too deep check if this hash code is insufficient.
coleenp@3865 179 if (count >= BasicHashtable::rehash_count && !needs_rehashing()) {
coleenp@3865 180 _needs_rehashing = check_rehash_table(count);
coleenp@3865 181 }
duke@435 182 return NULL;
duke@435 183 }
duke@435 184
coleenp@3875 185 // Pick hashing algorithm.
coleenp@3875 186 unsigned int SymbolTable::hash_symbol(const char* s, int len) {
coleenp@3865 187 return use_alternate_hashcode() ?
coleenp@3865 188 AltHashing::murmur3_32(seed(), (const jbyte*)s, len) :
coleenp@3875 189 java_lang_String::to_hash(s, len);
coleenp@3865 190 }
coleenp@3865 191
duke@435 192
duke@435 193 // We take care not to be blocking while holding the
duke@435 194 // SymbolTable_lock. Otherwise, the system might deadlock, since the
duke@435 195 // symboltable is used during compilation (VM_thread) The lock free
duke@435 196 // synchronization is simplified by the fact that we do not delete
duke@435 197 // entries in the symbol table during normal execution (only during
duke@435 198 // safepoints).
duke@435 199
coleenp@2497 200 Symbol* SymbolTable::lookup(const char* name, int len, TRAPS) {
duke@435 201 unsigned int hashValue = hash_symbol(name, len);
duke@435 202 int index = the_table()->hash_to_index(hashValue);
duke@435 203
coleenp@2497 204 Symbol* s = the_table()->lookup(index, name, len, hashValue);
duke@435 205
duke@435 206 // Found
duke@435 207 if (s != NULL) return s;
duke@435 208
coleenp@3875 209 // Grab SymbolTable_lock first.
coleenp@3875 210 MutexLocker ml(SymbolTable_lock, THREAD);
coleenp@3875 211
duke@435 212 // Otherwise, add to symbol to table
coleenp@3682 213 return the_table()->basic_add(index, (u1*)name, len, hashValue, true, CHECK_NULL);
duke@435 214 }
duke@435 215
coleenp@2497 216 Symbol* SymbolTable::lookup(const Symbol* sym, int begin, int end, TRAPS) {
duke@435 217 char* buffer;
duke@435 218 int index, len;
duke@435 219 unsigned int hashValue;
duke@435 220 char* name;
duke@435 221 {
duke@435 222 debug_only(No_Safepoint_Verifier nsv;)
duke@435 223
duke@435 224 name = (char*)sym->base() + begin;
duke@435 225 len = end - begin;
duke@435 226 hashValue = hash_symbol(name, len);
duke@435 227 index = the_table()->hash_to_index(hashValue);
coleenp@2497 228 Symbol* s = the_table()->lookup(index, name, len, hashValue);
duke@435 229
duke@435 230 // Found
duke@435 231 if (s != NULL) return s;
duke@435 232 }
duke@435 233
duke@435 234 // Otherwise, add to symbol to table. Copy to a C string first.
duke@435 235 char stack_buf[128];
duke@435 236 ResourceMark rm(THREAD);
duke@435 237 if (len <= 128) {
duke@435 238 buffer = stack_buf;
duke@435 239 } else {
duke@435 240 buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
duke@435 241 }
duke@435 242 for (int i=0; i<len; i++) {
duke@435 243 buffer[i] = name[i];
duke@435 244 }
duke@435 245 // Make sure there is no safepoint in the code above since name can't move.
duke@435 246 // We can't include the code in No_Safepoint_Verifier because of the
duke@435 247 // ResourceMark.
duke@435 248
coleenp@3875 249 // Grab SymbolTable_lock first.
coleenp@3875 250 MutexLocker ml(SymbolTable_lock, THREAD);
coleenp@3875 251
coleenp@3682 252 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, true, CHECK_NULL);
duke@435 253 }
duke@435 254
coleenp@2497 255 Symbol* SymbolTable::lookup_only(const char* name, int len,
duke@435 256 unsigned int& hash) {
duke@435 257 hash = hash_symbol(name, len);
duke@435 258 int index = the_table()->hash_to_index(hash);
duke@435 259
coleenp@2497 260 Symbol* s = the_table()->lookup(index, name, len, hash);
coleenp@2497 261 return s;
duke@435 262 }
duke@435 263
phh@3427 264 // Look up the address of the literal in the SymbolTable for this Symbol*
phh@3427 265 // Do not create any new symbols
phh@3427 266 // Do not increment the reference count to keep this alive
phh@3427 267 Symbol** SymbolTable::lookup_symbol_addr(Symbol* sym){
phh@3427 268 unsigned int hash = hash_symbol((char*)sym->bytes(), sym->utf8_length());
phh@3427 269 int index = the_table()->hash_to_index(hash);
phh@3427 270
phh@3427 271 for (HashtableEntry<Symbol*>* e = the_table()->bucket(index); e != NULL; e = e->next()) {
phh@3427 272 if (e->hash() == hash) {
phh@3427 273 Symbol* literal_sym = e->literal();
phh@3427 274 if (sym == literal_sym) {
phh@3427 275 return e->literal_addr();
phh@3427 276 }
phh@3427 277 }
phh@3427 278 }
phh@3427 279 return NULL;
phh@3427 280 }
phh@3427 281
jrose@1100 282 // Suggestion: Push unicode-based lookup all the way into the hashing
jrose@1100 283 // and probing logic, so there is no need for convert_to_utf8 until
coleenp@2497 284 // an actual new Symbol* is created.
coleenp@2497 285 Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) {
jrose@1100 286 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
jrose@1100 287 char stack_buf[128];
jrose@1100 288 if (utf8_length < (int) sizeof(stack_buf)) {
jrose@1100 289 char* chars = stack_buf;
jrose@1100 290 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 291 return lookup(chars, utf8_length, THREAD);
jrose@1100 292 } else {
jrose@1100 293 ResourceMark rm(THREAD);
jrose@1100 294 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
jrose@1100 295 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 296 return lookup(chars, utf8_length, THREAD);
jrose@1100 297 }
jrose@1100 298 }
jrose@1100 299
coleenp@2497 300 Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length,
jrose@1100 301 unsigned int& hash) {
jrose@1100 302 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
jrose@1100 303 char stack_buf[128];
jrose@1100 304 if (utf8_length < (int) sizeof(stack_buf)) {
jrose@1100 305 char* chars = stack_buf;
jrose@1100 306 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 307 return lookup_only(chars, utf8_length, hash);
jrose@1100 308 } else {
jrose@1100 309 ResourceMark rm;
jrose@1100 310 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
jrose@1100 311 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 312 return lookup_only(chars, utf8_length, hash);
jrose@1100 313 }
jrose@1100 314 }
jrose@1100 315
coleenp@3682 316 void SymbolTable::add(Handle class_loader, constantPoolHandle cp,
coleenp@3682 317 int names_count,
duke@435 318 const char** names, int* lengths, int* cp_indices,
duke@435 319 unsigned int* hashValues, TRAPS) {
coleenp@3875 320 // Grab SymbolTable_lock first.
coleenp@3875 321 MutexLocker ml(SymbolTable_lock, THREAD);
coleenp@3875 322
duke@435 323 SymbolTable* table = the_table();
coleenp@3682 324 bool added = table->basic_add(class_loader, cp, names_count, names, lengths,
duke@435 325 cp_indices, hashValues, CHECK);
duke@435 326 if (!added) {
duke@435 327 // do it the hard way
duke@435 328 for (int i=0; i<names_count; i++) {
duke@435 329 int index = table->hash_to_index(hashValues[i]);
coleenp@3682 330 bool c_heap = class_loader() != NULL;
coleenp@3682 331 Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i], hashValues[i], c_heap, CHECK);
duke@435 332 cp->symbol_at_put(cp_indices[i], sym);
duke@435 333 }
duke@435 334 }
duke@435 335 }
duke@435 336
coleenp@3682 337 Symbol* SymbolTable::new_permanent_symbol(const char* name, TRAPS) {
coleenp@3682 338 unsigned int hash;
coleenp@3682 339 Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash);
coleenp@3682 340 if (result != NULL) {
coleenp@3682 341 return result;
coleenp@3682 342 }
coleenp@3875 343 // Grab SymbolTable_lock first.
coleenp@3875 344 MutexLocker ml(SymbolTable_lock, THREAD);
coleenp@3875 345
coleenp@3682 346 SymbolTable* table = the_table();
coleenp@3682 347 int index = table->hash_to_index(hash);
coleenp@3682 348 return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD);
coleenp@3682 349 }
coleenp@3682 350
coleenp@3875 351 Symbol* SymbolTable::basic_add(int index_arg, u1 *name, int len,
coleenp@3865 352 unsigned int hashValue_arg, bool c_heap, TRAPS) {
duke@435 353 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
duke@435 354 "proposed name of symbol must be stable");
duke@435 355
coleenp@3875 356 // Don't allow symbols to be created which cannot fit in a Symbol*.
coleenp@3875 357 if (len > Symbol::max_length()) {
coleenp@3875 358 THROW_MSG_0(vmSymbols::java_lang_InternalError(),
coleenp@3875 359 "name is too long to represent");
coleenp@3875 360 }
coleenp@3875 361
coleenp@3875 362 // Cannot hit a safepoint in this function because the "this" pointer can move.
coleenp@3875 363 No_Safepoint_Verifier nsv;
duke@435 364
coleenp@3865 365 // Check if the symbol table has been rehashed, if so, need to recalculate
coleenp@3875 366 // the hash value and index.
coleenp@3875 367 unsigned int hashValue;
coleenp@3875 368 int index;
coleenp@3875 369 if (use_alternate_hashcode()) {
coleenp@3875 370 hashValue = hash_symbol((const char*)name, len);
coleenp@3875 371 index = hash_to_index(hashValue);
coleenp@3875 372 } else {
coleenp@3875 373 hashValue = hashValue_arg;
coleenp@3875 374 index = index_arg;
coleenp@3875 375 }
coleenp@3865 376
duke@435 377 // Since look-up was done lock-free, we need to check if another
duke@435 378 // thread beat us in the race to insert the symbol.
coleenp@2497 379 Symbol* test = lookup(index, (char*)name, len, hashValue);
duke@435 380 if (test != NULL) {
coleenp@3682 381 // A race occurred and another thread introduced the symbol.
coleenp@2497 382 assert(test->refcount() != 0, "lookup should have incremented the count");
duke@435 383 return test;
duke@435 384 }
duke@435 385
coleenp@3682 386 // Create a new symbol.
coleenp@3682 387 Symbol* sym = allocate_symbol(name, len, c_heap, CHECK_NULL);
coleenp@3682 388 assert(sym->equals((char*)name, len), "symbol must be properly initialized");
coleenp@3682 389
coleenp@2497 390 HashtableEntry<Symbol*>* entry = new_entry(hashValue, sym);
duke@435 391 add_entry(index, entry);
coleenp@2497 392 return sym;
duke@435 393 }
duke@435 394
coleenp@3682 395 // This version of basic_add adds symbols in batch from the constant pool
coleenp@3682 396 // parsing.
coleenp@3682 397 bool SymbolTable::basic_add(Handle class_loader, constantPoolHandle cp,
coleenp@3682 398 int names_count,
duke@435 399 const char** names, int* lengths,
duke@435 400 int* cp_indices, unsigned int* hashValues,
duke@435 401 TRAPS) {
coleenp@3682 402
coleenp@3682 403 // Check symbol names are not too long. If any are too long, don't add any.
coleenp@3682 404 for (int i = 0; i< names_count; i++) {
coleenp@3682 405 if (lengths[i] > Symbol::max_length()) {
coleenp@3682 406 THROW_MSG_0(vmSymbols::java_lang_InternalError(),
coleenp@3682 407 "name is too long to represent");
coleenp@3682 408 }
duke@435 409 }
duke@435 410
coleenp@3875 411 // Cannot hit a safepoint in this function because the "this" pointer can move.
coleenp@3875 412 No_Safepoint_Verifier nsv;
duke@435 413
coleenp@2497 414 for (int i=0; i<names_count; i++) {
coleenp@3865 415 // Check if the symbol table has been rehashed, if so, need to recalculate
coleenp@3865 416 // the hash value.
coleenp@3875 417 unsigned int hashValue;
coleenp@3875 418 if (use_alternate_hashcode()) {
coleenp@3875 419 hashValue = hash_symbol(names[i], lengths[i]);
coleenp@3875 420 } else {
coleenp@3875 421 hashValue = hashValues[i];
coleenp@3875 422 }
duke@435 423 // Since look-up was done lock-free, we need to check if another
duke@435 424 // thread beat us in the race to insert the symbol.
coleenp@3865 425 int index = hash_to_index(hashValue);
coleenp@3865 426 Symbol* test = lookup(index, names[i], lengths[i], hashValue);
duke@435 427 if (test != NULL) {
twisti@1040 428 // A race occurred and another thread introduced the symbol, this one
duke@435 429 // will be dropped and collected. Use test instead.
duke@435 430 cp->symbol_at_put(cp_indices[i], test);
coleenp@2497 431 assert(test->refcount() != 0, "lookup should have incremented the count");
duke@435 432 } else {
coleenp@3682 433 // Create a new symbol. The null class loader is never unloaded so these
coleenp@3682 434 // are allocated specially in a permanent arena.
coleenp@3682 435 bool c_heap = class_loader() != NULL;
coleenp@3682 436 Symbol* sym = allocate_symbol((const u1*)names[i], lengths[i], c_heap, CHECK_(false));
coleenp@3682 437 assert(sym->equals(names[i], lengths[i]), "symbol must be properly initialized"); // why wouldn't it be???
coleenp@3865 438 HashtableEntry<Symbol*>* entry = new_entry(hashValue, sym);
duke@435 439 add_entry(index, entry);
duke@435 440 cp->symbol_at_put(cp_indices[i], sym);
duke@435 441 }
duke@435 442 }
duke@435 443 return true;
duke@435 444 }
duke@435 445
duke@435 446
duke@435 447 void SymbolTable::verify() {
duke@435 448 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 449 HashtableEntry<Symbol*>* p = the_table()->bucket(i);
duke@435 450 for ( ; p != NULL; p = p->next()) {
coleenp@2497 451 Symbol* s = (Symbol*)(p->literal());
duke@435 452 guarantee(s != NULL, "symbol is NULL");
duke@435 453 unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length());
duke@435 454 guarantee(p->hash() == h, "broken hash in symbol table entry");
duke@435 455 guarantee(the_table()->hash_to_index(h) == i,
duke@435 456 "wrong index in symbol table");
duke@435 457 }
duke@435 458 }
duke@435 459 }
duke@435 460
coleenp@3865 461 void SymbolTable::dump(outputStream* st) {
coleenp@3865 462 NumberSeq summary;
coleenp@3865 463 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@3865 464 int count = 0;
coleenp@3865 465 for (HashtableEntry<Symbol*>* e = the_table()->bucket(i);
coleenp@3865 466 e != NULL; e = e->next()) {
coleenp@3865 467 count++;
coleenp@3865 468 }
coleenp@3865 469 summary.add((double)count);
coleenp@3865 470 }
coleenp@3865 471 st->print_cr("SymbolTable statistics:");
coleenp@3865 472 st->print_cr("Number of buckets : %7d", summary.num());
coleenp@3865 473 st->print_cr("Average bucket size : %7.0f", summary.avg());
coleenp@3865 474 st->print_cr("Variance of bucket size : %7.0f", summary.variance());
coleenp@3865 475 st->print_cr("Std. dev. of bucket size: %7.0f", summary.sd());
coleenp@3865 476 st->print_cr("Maximum bucket size : %7.0f", summary.maximum());
coleenp@3865 477 }
coleenp@3865 478
duke@435 479
duke@435 480 //---------------------------------------------------------------------------
duke@435 481 // Non-product code
duke@435 482
duke@435 483 #ifndef PRODUCT
duke@435 484
duke@435 485 void SymbolTable::print_histogram() {
duke@435 486 MutexLocker ml(SymbolTable_lock);
duke@435 487 const int results_length = 100;
duke@435 488 int results[results_length];
duke@435 489 int i,j;
duke@435 490
duke@435 491 // initialize results to zero
duke@435 492 for (j = 0; j < results_length; j++) {
duke@435 493 results[j] = 0;
duke@435 494 }
duke@435 495
duke@435 496 int total = 0;
duke@435 497 int max_symbols = 0;
duke@435 498 int out_of_range = 0;
coleenp@2497 499 int memory_total = 0;
coleenp@2497 500 int count = 0;
duke@435 501 for (i = 0; i < the_table()->table_size(); i++) {
coleenp@2497 502 HashtableEntry<Symbol*>* p = the_table()->bucket(i);
duke@435 503 for ( ; p != NULL; p = p->next()) {
coleenp@2497 504 memory_total += p->literal()->object_size();
coleenp@2497 505 count++;
coleenp@2497 506 int counter = p->literal()->utf8_length();
duke@435 507 total += counter;
duke@435 508 if (counter < results_length) {
duke@435 509 results[counter]++;
duke@435 510 } else {
duke@435 511 out_of_range++;
duke@435 512 }
duke@435 513 max_symbols = MAX2(max_symbols, counter);
duke@435 514 }
duke@435 515 }
duke@435 516 tty->print_cr("Symbol Table:");
coleenp@2497 517 tty->print_cr("Total number of symbols %5d", count);
coleenp@2497 518 tty->print_cr("Total size in memory %5dK",
coleenp@2497 519 (memory_total*HeapWordSize)/1024);
coleenp@2497 520 tty->print_cr("Total counted %5d", symbols_counted);
coleenp@2497 521 tty->print_cr("Total removed %5d", symbols_removed);
coleenp@2497 522 if (symbols_counted > 0) {
coleenp@2497 523 tty->print_cr("Percent removed %3.2f",
coleenp@2497 524 ((float)symbols_removed/(float)symbols_counted)* 100);
coleenp@2497 525 }
coleenp@2497 526 tty->print_cr("Reference counts %5d", Symbol::_total_count);
coleenp@3682 527 tty->print_cr("Symbol arena size %5d used %5d",
coleenp@3682 528 arena()->size_in_bytes(), arena()->used());
coleenp@2497 529 tty->print_cr("Histogram of symbol length:");
duke@435 530 tty->print_cr("%8s %5d", "Total ", total);
duke@435 531 tty->print_cr("%8s %5d", "Maximum", max_symbols);
duke@435 532 tty->print_cr("%8s %3.2f", "Average",
duke@435 533 ((float) total / (float) the_table()->table_size()));
duke@435 534 tty->print_cr("%s", "Histogram:");
duke@435 535 tty->print_cr(" %s %29s", "Length", "Number chains that length");
duke@435 536 for (i = 0; i < results_length; i++) {
duke@435 537 if (results[i] > 0) {
duke@435 538 tty->print_cr("%6d %10d", i, results[i]);
duke@435 539 }
duke@435 540 }
coleenp@2497 541 if (Verbose) {
coleenp@2497 542 int line_length = 70;
coleenp@2497 543 tty->print_cr("%s %30s", " Length", "Number chains that length");
coleenp@2497 544 for (i = 0; i < results_length; i++) {
coleenp@2497 545 if (results[i] > 0) {
coleenp@2497 546 tty->print("%4d", i);
coleenp@2497 547 for (j = 0; (j < results[i]) && (j < line_length); j++) {
coleenp@2497 548 tty->print("%1s", "*");
coleenp@2497 549 }
coleenp@2497 550 if (j == line_length) {
coleenp@2497 551 tty->print("%1s", "+");
coleenp@2497 552 }
coleenp@2497 553 tty->cr();
duke@435 554 }
coleenp@2497 555 }
coleenp@2497 556 }
coleenp@2497 557 tty->print_cr(" %s %d: %d\n", "Number chains longer than",
coleenp@2497 558 results_length, out_of_range);
coleenp@2497 559 }
coleenp@2497 560
coleenp@2497 561 void SymbolTable::print() {
coleenp@2497 562 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 563 HashtableEntry<Symbol*>** p = the_table()->bucket_addr(i);
coleenp@2497 564 HashtableEntry<Symbol*>* entry = the_table()->bucket(i);
coleenp@2497 565 if (entry != NULL) {
coleenp@2497 566 while (entry != NULL) {
coleenp@2497 567 tty->print(PTR_FORMAT " ", entry->literal());
coleenp@2497 568 entry->literal()->print();
coleenp@2497 569 tty->print(" %d", entry->literal()->refcount());
coleenp@2497 570 p = entry->next_addr();
coleenp@2497 571 entry = (HashtableEntry<Symbol*>*)HashtableEntry<Symbol*>::make_ptr(*p);
duke@435 572 }
duke@435 573 tty->cr();
duke@435 574 }
duke@435 575 }
duke@435 576 }
duke@435 577 #endif // PRODUCT
duke@435 578
duke@435 579 // --------------------------------------------------------------------------
duke@435 580
duke@435 581 #ifdef ASSERT
duke@435 582 class StableMemoryChecker : public StackObj {
duke@435 583 enum { _bufsize = wordSize*4 };
duke@435 584
duke@435 585 address _region;
duke@435 586 jint _size;
duke@435 587 u1 _save_buf[_bufsize];
duke@435 588
duke@435 589 int sample(u1* save_buf) {
duke@435 590 if (_size <= _bufsize) {
duke@435 591 memcpy(save_buf, _region, _size);
duke@435 592 return _size;
duke@435 593 } else {
duke@435 594 // copy head and tail
duke@435 595 memcpy(&save_buf[0], _region, _bufsize/2);
duke@435 596 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
duke@435 597 return (_bufsize/2)*2;
duke@435 598 }
duke@435 599 }
duke@435 600
duke@435 601 public:
duke@435 602 StableMemoryChecker(const void* region, jint size) {
duke@435 603 _region = (address) region;
duke@435 604 _size = size;
duke@435 605 sample(_save_buf);
duke@435 606 }
duke@435 607
duke@435 608 bool verify() {
duke@435 609 u1 check_buf[sizeof(_save_buf)];
duke@435 610 int check_size = sample(check_buf);
duke@435 611 return (0 == memcmp(_save_buf, check_buf, check_size));
duke@435 612 }
duke@435 613
duke@435 614 void set_region(const void* region) { _region = (address) region; }
duke@435 615 };
duke@435 616 #endif
duke@435 617
duke@435 618
duke@435 619 // --------------------------------------------------------------------------
duke@435 620 StringTable* StringTable::_the_table = NULL;
duke@435 621
coleenp@3865 622 bool StringTable::_needs_rehashing = false;
coleenp@3865 623 jint StringTable::_seed = 0;
coleenp@3865 624
coleenp@3865 625 // Pick hashing algorithm
coleenp@3875 626 unsigned int StringTable::hash_string(const jchar* s, int len) {
coleenp@3865 627 return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) :
coleenp@3875 628 java_lang_String::to_hash(s, len);
coleenp@3865 629 }
coleenp@3865 630
duke@435 631 oop StringTable::lookup(int index, jchar* name,
duke@435 632 int len, unsigned int hash) {
coleenp@3865 633 int count = 0;
coleenp@2497 634 for (HashtableEntry<oop>* l = bucket(index); l != NULL; l = l->next()) {
coleenp@3865 635 count++;
duke@435 636 if (l->hash() == hash) {
duke@435 637 if (java_lang_String::equals(l->literal(), name, len)) {
duke@435 638 return l->literal();
duke@435 639 }
duke@435 640 }
duke@435 641 }
coleenp@3865 642 // If the bucket size is too deep check if this hash code is insufficient.
coleenp@3865 643 if (count >= BasicHashtable::rehash_count && !needs_rehashing()) {
coleenp@3865 644 _needs_rehashing = check_rehash_table(count);
coleenp@3865 645 }
duke@435 646 return NULL;
duke@435 647 }
duke@435 648
duke@435 649
coleenp@3875 650 oop StringTable::basic_add(int index_arg, Handle string, jchar* name,
coleenp@3865 651 int len, unsigned int hashValue_arg, TRAPS) {
duke@435 652
duke@435 653 assert(java_lang_String::equals(string(), name, len),
duke@435 654 "string must be properly initialized");
coleenp@3875 655 // Cannot hit a safepoint in this function because the "this" pointer can move.
coleenp@3875 656 No_Safepoint_Verifier nsv;
duke@435 657
coleenp@3865 658 // Check if the symbol table has been rehashed, if so, need to recalculate
coleenp@3875 659 // the hash value and index before second lookup.
coleenp@3875 660 unsigned int hashValue;
coleenp@3875 661 int index;
coleenp@3875 662 if (use_alternate_hashcode()) {
coleenp@3875 663 hashValue = hash_string(name, len);
coleenp@3875 664 index = hash_to_index(hashValue);
coleenp@3875 665 } else {
coleenp@3875 666 hashValue = hashValue_arg;
coleenp@3875 667 index = index_arg;
coleenp@3875 668 }
coleenp@3865 669
duke@435 670 // Since look-up was done lock-free, we need to check if another
duke@435 671 // thread beat us in the race to insert the symbol.
duke@435 672
duke@435 673 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int)
duke@435 674 if (test != NULL) {
duke@435 675 // Entry already added
duke@435 676 return test;
duke@435 677 }
duke@435 678
coleenp@2497 679 HashtableEntry<oop>* entry = new_entry(hashValue, string());
duke@435 680 add_entry(index, entry);
duke@435 681 return string();
duke@435 682 }
duke@435 683
duke@435 684
coleenp@2497 685 oop StringTable::lookup(Symbol* symbol) {
duke@435 686 ResourceMark rm;
duke@435 687 int length;
duke@435 688 jchar* chars = symbol->as_unicode(length);
coleenp@3865 689 unsigned int hashValue = hash_string(chars, length);
duke@435 690 int index = the_table()->hash_to_index(hashValue);
duke@435 691 return the_table()->lookup(index, chars, length, hashValue);
duke@435 692 }
duke@435 693
duke@435 694
duke@435 695 oop StringTable::intern(Handle string_or_null, jchar* name,
duke@435 696 int len, TRAPS) {
coleenp@3865 697 unsigned int hashValue = hash_string(name, len);
duke@435 698 int index = the_table()->hash_to_index(hashValue);
coleenp@3875 699 oop found_string = the_table()->lookup(index, name, len, hashValue);
duke@435 700
duke@435 701 // Found
coleenp@3875 702 if (found_string != NULL) return found_string;
coleenp@3875 703
coleenp@3875 704 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
coleenp@3875 705 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
coleenp@3875 706 "proposed name of symbol must be stable");
coleenp@3875 707
coleenp@3875 708 Handle string;
coleenp@3875 709 // try to reuse the string if possible
coleenp@3875 710 if (!string_or_null.is_null() && (!JavaObjectsInPerm || string_or_null()->is_perm())) {
coleenp@3875 711 string = string_or_null;
coleenp@3875 712 } else {
coleenp@3875 713 string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL);
coleenp@3875 714 }
coleenp@3875 715
coleenp@3875 716 // Grab the StringTable_lock before getting the_table() because it could
coleenp@3875 717 // change at safepoint.
coleenp@3875 718 MutexLocker ml(StringTable_lock, THREAD);
duke@435 719
duke@435 720 // Otherwise, add to symbol to table
coleenp@3875 721 return the_table()->basic_add(index, string, name, len,
duke@435 722 hashValue, CHECK_NULL);
duke@435 723 }
duke@435 724
coleenp@2497 725 oop StringTable::intern(Symbol* symbol, TRAPS) {
duke@435 726 if (symbol == NULL) return NULL;
duke@435 727 ResourceMark rm(THREAD);
duke@435 728 int length;
duke@435 729 jchar* chars = symbol->as_unicode(length);
duke@435 730 Handle string;
duke@435 731 oop result = intern(string, chars, length, CHECK_NULL);
duke@435 732 return result;
duke@435 733 }
duke@435 734
duke@435 735
duke@435 736 oop StringTable::intern(oop string, TRAPS)
duke@435 737 {
duke@435 738 if (string == NULL) return NULL;
duke@435 739 ResourceMark rm(THREAD);
duke@435 740 int length;
duke@435 741 Handle h_string (THREAD, string);
duke@435 742 jchar* chars = java_lang_String::as_unicode_string(string, length);
duke@435 743 oop result = intern(h_string, chars, length, CHECK_NULL);
duke@435 744 return result;
duke@435 745 }
duke@435 746
duke@435 747
duke@435 748 oop StringTable::intern(const char* utf8_string, TRAPS) {
duke@435 749 if (utf8_string == NULL) return NULL;
duke@435 750 ResourceMark rm(THREAD);
duke@435 751 int length = UTF8::unicode_length(utf8_string);
duke@435 752 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
duke@435 753 UTF8::convert_to_unicode(utf8_string, chars, length);
duke@435 754 Handle string;
duke@435 755 oop result = intern(string, chars, length, CHECK_NULL);
duke@435 756 return result;
duke@435 757 }
duke@435 758
coleenp@2497 759 void StringTable::unlink(BoolObjectClosure* is_alive) {
coleenp@2497 760 // Readers of the table are unlocked, so we should only be removing
coleenp@2497 761 // entries at a safepoint.
coleenp@2497 762 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
coleenp@2497 763 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@3875 764 HashtableEntry<oop>** p = the_table()->bucket_addr(i);
coleenp@3875 765 HashtableEntry<oop>* entry = the_table()->bucket(i);
coleenp@3875 766 while (entry != NULL) {
coleenp@3875 767 // Shared entries are normally at the end of the bucket and if we run into
coleenp@3875 768 // a shared entry, then there is nothing more to remove. However, if we
coleenp@3875 769 // have rehashed the table, then the shared entries are no longer at the
coleenp@3875 770 // end of the bucket.
coleenp@3875 771 if (entry->is_shared() && !use_alternate_hashcode()) {
coleenp@2497 772 break;
coleenp@2497 773 }
coleenp@2497 774 assert(entry->literal() != NULL, "just checking");
coleenp@3875 775 if (entry->is_shared() || is_alive->do_object_b(entry->literal())) {
coleenp@2497 776 p = entry->next_addr();
coleenp@2497 777 } else {
coleenp@2497 778 *p = entry->next();
coleenp@2497 779 the_table()->free_entry(entry);
coleenp@2497 780 }
coleenp@3875 781 entry = (HashtableEntry<oop>*)HashtableEntry<oop>::make_ptr(*p);
coleenp@2497 782 }
coleenp@2497 783 }
coleenp@2497 784 }
coleenp@2497 785
coleenp@2497 786 void StringTable::oops_do(OopClosure* f) {
coleenp@2497 787 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 788 HashtableEntry<oop>** p = the_table()->bucket_addr(i);
coleenp@2497 789 HashtableEntry<oop>* entry = the_table()->bucket(i);
coleenp@2497 790 while (entry != NULL) {
coleenp@2497 791 f->do_oop((oop*)entry->literal_addr());
coleenp@2497 792
coleenp@2497 793 // Did the closure remove the literal from the table?
coleenp@2497 794 if (entry->literal() == NULL) {
coleenp@2497 795 assert(!entry->is_shared(), "immutable hashtable entry?");
coleenp@2497 796 *p = entry->next();
coleenp@2497 797 the_table()->free_entry(entry);
coleenp@2497 798 } else {
coleenp@2497 799 p = entry->next_addr();
coleenp@2497 800 }
coleenp@2497 801 entry = (HashtableEntry<oop>*)HashtableEntry<oop>::make_ptr(*p);
coleenp@2497 802 }
coleenp@2497 803 }
coleenp@2497 804 }
coleenp@2497 805
duke@435 806 void StringTable::verify() {
duke@435 807 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 808 HashtableEntry<oop>* p = the_table()->bucket(i);
duke@435 809 for ( ; p != NULL; p = p->next()) {
duke@435 810 oop s = p->literal();
duke@435 811 guarantee(s != NULL, "interned string is NULL");
jcoomes@2661 812 guarantee(s->is_perm() || !JavaObjectsInPerm, "interned string not in permspace");
never@2700 813 unsigned int h = java_lang_String::hash_string(s);
duke@435 814 guarantee(p->hash() == h, "broken hash in string table entry");
duke@435 815 guarantee(the_table()->hash_to_index(h) == i,
duke@435 816 "wrong index in string table");
duke@435 817 }
duke@435 818 }
duke@435 819 }
coleenp@3865 820
coleenp@3865 821 void StringTable::dump(outputStream* st) {
coleenp@3865 822 NumberSeq summary;
coleenp@3865 823 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@3865 824 HashtableEntry<oop>* p = the_table()->bucket(i);
coleenp@3865 825 int count = 0;
coleenp@3865 826 for ( ; p != NULL; p = p->next()) {
coleenp@3865 827 count++;
coleenp@3865 828 }
coleenp@3865 829 summary.add((double)count);
coleenp@3865 830 }
coleenp@3865 831 st->print_cr("StringTable statistics:");
coleenp@3865 832 st->print_cr("Number of buckets : %7d", summary.num());
coleenp@3865 833 st->print_cr("Average bucket size : %7.0f", summary.avg());
coleenp@3865 834 st->print_cr("Variance of bucket size : %7.0f", summary.variance());
coleenp@3865 835 st->print_cr("Std. dev. of bucket size: %7.0f", summary.sd());
coleenp@3865 836 st->print_cr("Maximum bucket size : %7.0f", summary.maximum());
coleenp@3865 837 }
coleenp@3865 838
coleenp@3865 839
coleenp@3865 840 unsigned int StringTable::new_hash(oop string) {
coleenp@3865 841 ResourceMark rm;
coleenp@3865 842 int length;
coleenp@3865 843 jchar* chars = java_lang_String::as_unicode_string(string, length);
coleenp@3865 844 // Use alternate hashing algorithm on the string
coleenp@3865 845 return AltHashing::murmur3_32(seed(), chars, length);
coleenp@3865 846 }
coleenp@3865 847
coleenp@3865 848 // Create a new table and using alternate hash code, populate the new table
coleenp@3865 849 // with the existing strings. Set flag to use the alternate hash code afterwards.
coleenp@3865 850 void StringTable::rehash_table() {
coleenp@3865 851 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
coleenp@3875 852 // This should never happen with -Xshare:dump but it might in testing mode.
coleenp@3875 853 if (DumpSharedSpaces) return;
coleenp@3865 854 StringTable* new_table = new StringTable();
coleenp@3865 855
coleenp@3865 856 // Initialize new global seed for hashing.
coleenp@3865 857 _seed = AltHashing::compute_seed();
coleenp@3865 858 assert(seed() != 0, "shouldn't be zero");
coleenp@3865 859
coleenp@3865 860 // Rehash the table
coleenp@3865 861 the_table()->move_to(new_table);
coleenp@3865 862
coleenp@3865 863 // Delete the table and buckets (entries are reused in new table).
coleenp@3865 864 delete _the_table;
coleenp@3865 865 // Don't check if we need rehashing until the table gets unbalanced again.
coleenp@3865 866 // Then rehash with a new global seed.
coleenp@3865 867 _needs_rehashing = false;
coleenp@3865 868 _the_table = new_table;
coleenp@3865 869 }

mercurial