src/share/vm/classfile/symbolTable.cpp

Wed, 13 Jun 2012 19:52:59 -0400

author
coleenp
date
Wed, 13 Jun 2012 19:52:59 -0400
changeset 3865
e9140bf80b4a
parent 3682
fc9d8850ab8b
child 3875
246d977b51f2
permissions
-rw-r--r--

7158800: Improve storage of symbol tables
Summary: Use an alternate version of hashing algorithm for symbol string tables and after a certain bucket size to improve performance
Reviewed-by: pbk, kamg, dlong, kvn, fparain

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

mercurial