src/share/vm/classfile/symbolTable.cpp

Mon, 27 May 2013 12:56:34 +0200

author
stefank
date
Mon, 27 May 2013 12:56:34 +0200
changeset 5195
95c00927be11
parent 5144
a5d6f0c3585f
child 5196
8dbc025ff709
permissions
-rw-r--r--

8015428: Remove unused CDS support from StringTable
Summary: The string in StringTable is not used by CDS anymore. Remove the unnecessary code in preparation for 8015422: Large performance hit when the StringTable is walked twice in Parallel Scavenge
Reviewed-by: pliden, tschatzl, coleenp

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

mercurial