duke@435: /* coleenp@4718: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" coleenp@3865: #include "classfile/altHashing.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" coleenp@3682: #include "memory/allocation.inline.hpp" stefank@2314: #include "memory/filemap.hpp" stefank@2314: #include "memory/gcLocker.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "oops/oop.inline2.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" stefank@2314: #include "utilities/hashtable.inline.hpp" duke@435: duke@435: // -------------------------------------------------------------------------- duke@435: duke@435: SymbolTable* SymbolTable::_the_table = NULL; coleenp@3682: // Static arena for symbols that are not deallocated coleenp@3682: Arena* SymbolTable::_arena = NULL; coleenp@3865: bool SymbolTable::_needs_rehashing = false; duke@435: coleenp@3682: Symbol* SymbolTable::allocate_symbol(const u1* name, int len, bool c_heap, TRAPS) { coleenp@3875: assert (len <= Symbol::max_length(), "should be checked by caller"); coleenp@3875: coleenp@3682: Symbol* sym; coleenp@4037: coleenp@4718: if (DumpSharedSpaces) { coleenp@4718: // Allocate all symbols to CLD shared metaspace coleenp@4718: sym = new (len, ClassLoaderData::the_null_class_loader_data(), THREAD) Symbol(name, len, -1); coleenp@4718: } else if (c_heap) { coleenp@3682: // refcount starts as 1 coleenp@3682: sym = new (len, THREAD) Symbol(name, len, 1); coleenp@4037: assert(sym != NULL, "new should call vm_exit_out_of_memory if C_HEAP is exhausted"); coleenp@4037: } else { coleenp@4718: // Allocate to global arena coleenp@3682: sym = new (len, arena(), THREAD) Symbol(name, len, -1); coleenp@3682: } coleenp@2497: return sym; coleenp@2497: } coleenp@2497: coleenp@3682: void SymbolTable::initialize_symbols(int arena_alloc_size) { coleenp@3682: // Initialize the arena for global symbols, size passed in depends on CDS. coleenp@3682: if (arena_alloc_size == 0) { zgu@3900: _arena = new (mtSymbol) Arena(); coleenp@3682: } else { zgu@3900: _arena = new (mtSymbol) Arena(arena_alloc_size); coleenp@2497: } coleenp@2497: } coleenp@2497: coleenp@2497: // Call function for all symbols in the symbol table. coleenp@2497: void SymbolTable::symbols_do(SymbolClosure *cl) { coleenp@2497: const int n = the_table()->table_size(); coleenp@2497: for (int i = 0; i < n; i++) { zgu@3900: for (HashtableEntry* p = the_table()->bucket(i); coleenp@2497: p != NULL; coleenp@2497: p = p->next()) { coleenp@2497: cl->do_symbol(p->literal_addr()); coleenp@2497: } coleenp@2497: } coleenp@2497: } coleenp@2497: coleenp@2497: int SymbolTable::symbols_removed = 0; coleenp@2497: int SymbolTable::symbols_counted = 0; coleenp@2497: coleenp@2497: // Remove unreferenced symbols from the symbol table coleenp@3682: // This is done late during GC. coleenp@2497: void SymbolTable::unlink() { coleenp@2497: int removed = 0; coleenp@2497: int total = 0; coleenp@2618: size_t memory_total = 0; coleenp@2497: for (int i = 0; i < the_table()->table_size(); ++i) { zgu@3900: HashtableEntry** p = the_table()->bucket_addr(i); zgu@3900: HashtableEntry* entry = the_table()->bucket(i); coleenp@3875: while (entry != NULL) { coleenp@3875: // Shared entries are normally at the end of the bucket and if we run into coleenp@3875: // a shared entry, then there is nothing more to remove. However, if we coleenp@3875: // have rehashed the table, then the shared entries are no longer at the coleenp@3875: // end of the bucket. coleenp@3875: if (entry->is_shared() && !use_alternate_hashcode()) { coleenp@2497: break; coleenp@2497: } coleenp@2497: Symbol* s = entry->literal(); coleenp@4037: memory_total += s->size(); coleenp@2497: total++; coleenp@2497: assert(s != NULL, "just checking"); coleenp@2497: // If reference count is zero, remove. coleenp@2497: if (s->refcount() == 0) { coleenp@3875: assert(!entry->is_shared(), "shared entries should be kept live"); coleenp@2497: delete s; coleenp@2497: removed++; coleenp@2497: *p = entry->next(); coleenp@2497: the_table()->free_entry(entry); coleenp@2497: } else { coleenp@2497: p = entry->next_addr(); coleenp@2497: } coleenp@3875: // get next entry zgu@3900: entry = (HashtableEntry*)HashtableEntry::make_ptr(*p); coleenp@2497: } coleenp@2497: } coleenp@2497: symbols_removed += removed; coleenp@2497: symbols_counted += total; coleenp@2618: // Exclude printing for normal PrintGCDetails because people parse coleenp@2618: // this output. coleenp@2618: if (PrintGCDetails && Verbose && WizardMode) { coleenp@2618: gclog_or_tty->print(" [Symbols=%d size=" SIZE_FORMAT "K] ", total, coleenp@2497: (memory_total*HeapWordSize)/1024); coleenp@2497: } coleenp@2497: } coleenp@2497: coleenp@3865: // Create a new table and using alternate hash code, populate the new table coleenp@3865: // with the existing strings. Set flag to use the alternate hash code afterwards. coleenp@3865: void SymbolTable::rehash_table() { coleenp@3865: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); coleenp@3875: // This should never happen with -Xshare:dump but it might in testing mode. coleenp@3875: if (DumpSharedSpaces) return; coleenp@3865: // Create a new symbol table coleenp@3865: SymbolTable* new_table = new SymbolTable(); coleenp@3865: coleenp@3865: the_table()->move_to(new_table); coleenp@3865: coleenp@3865: // Delete the table and buckets (entries are reused in new table). coleenp@3865: delete _the_table; coleenp@3865: // Don't check if we need rehashing until the table gets unbalanced again. coleenp@3865: // Then rehash with a new global seed. coleenp@3865: _needs_rehashing = false; coleenp@3865: _the_table = new_table; coleenp@3865: } coleenp@2497: duke@435: // Lookup a symbol in a bucket. duke@435: coleenp@2497: Symbol* SymbolTable::lookup(int index, const char* name, duke@435: int len, unsigned int hash) { coleenp@3865: int count = 0; zgu@3900: for (HashtableEntry* e = bucket(index); e != NULL; e = e->next()) { coleenp@3865: count++; // count all entries in this bucket, not just ones with same hash duke@435: if (e->hash() == hash) { coleenp@2497: Symbol* sym = e->literal(); duke@435: if (sym->equals(name, len)) { coleenp@2497: // something is referencing this symbol now. coleenp@2497: sym->increment_refcount(); duke@435: return sym; duke@435: } duke@435: } duke@435: } coleenp@3865: // If the bucket size is too deep check if this hash code is insufficient. zgu@3900: if (count >= BasicHashtable::rehash_count && !needs_rehashing()) { coleenp@3865: _needs_rehashing = check_rehash_table(count); coleenp@3865: } duke@435: return NULL; duke@435: } duke@435: coleenp@3875: // Pick hashing algorithm. coleenp@3875: unsigned int SymbolTable::hash_symbol(const char* s, int len) { coleenp@3865: return use_alternate_hashcode() ? coleenp@3865: AltHashing::murmur3_32(seed(), (const jbyte*)s, len) : brutisso@4335: java_lang_String::hash_code(s, len); coleenp@3865: } coleenp@3865: duke@435: duke@435: // We take care not to be blocking while holding the duke@435: // SymbolTable_lock. Otherwise, the system might deadlock, since the duke@435: // symboltable is used during compilation (VM_thread) The lock free duke@435: // synchronization is simplified by the fact that we do not delete duke@435: // entries in the symbol table during normal execution (only during duke@435: // safepoints). duke@435: coleenp@2497: Symbol* SymbolTable::lookup(const char* name, int len, TRAPS) { duke@435: unsigned int hashValue = hash_symbol(name, len); duke@435: int index = the_table()->hash_to_index(hashValue); duke@435: coleenp@2497: Symbol* s = the_table()->lookup(index, name, len, hashValue); duke@435: duke@435: // Found duke@435: if (s != NULL) return s; duke@435: coleenp@3875: // Grab SymbolTable_lock first. coleenp@3875: MutexLocker ml(SymbolTable_lock, THREAD); coleenp@3875: duke@435: // Otherwise, add to symbol to table coleenp@3682: return the_table()->basic_add(index, (u1*)name, len, hashValue, true, CHECK_NULL); duke@435: } duke@435: coleenp@2497: Symbol* SymbolTable::lookup(const Symbol* sym, int begin, int end, TRAPS) { duke@435: char* buffer; duke@435: int index, len; duke@435: unsigned int hashValue; duke@435: char* name; duke@435: { duke@435: debug_only(No_Safepoint_Verifier nsv;) duke@435: duke@435: name = (char*)sym->base() + begin; duke@435: len = end - begin; duke@435: hashValue = hash_symbol(name, len); duke@435: index = the_table()->hash_to_index(hashValue); coleenp@2497: Symbol* s = the_table()->lookup(index, name, len, hashValue); duke@435: duke@435: // Found duke@435: if (s != NULL) return s; duke@435: } duke@435: duke@435: // Otherwise, add to symbol to table. Copy to a C string first. duke@435: char stack_buf[128]; duke@435: ResourceMark rm(THREAD); duke@435: if (len <= 128) { duke@435: buffer = stack_buf; duke@435: } else { duke@435: buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); duke@435: } duke@435: for (int i=0; ibasic_add(index, (u1*)buffer, len, hashValue, true, CHECK_NULL); duke@435: } duke@435: coleenp@2497: Symbol* SymbolTable::lookup_only(const char* name, int len, duke@435: unsigned int& hash) { duke@435: hash = hash_symbol(name, len); duke@435: int index = the_table()->hash_to_index(hash); duke@435: coleenp@2497: Symbol* s = the_table()->lookup(index, name, len, hash); coleenp@2497: return s; duke@435: } duke@435: phh@3427: // Look up the address of the literal in the SymbolTable for this Symbol* phh@3427: // Do not create any new symbols phh@3427: // Do not increment the reference count to keep this alive phh@3427: Symbol** SymbolTable::lookup_symbol_addr(Symbol* sym){ phh@3427: unsigned int hash = hash_symbol((char*)sym->bytes(), sym->utf8_length()); phh@3427: int index = the_table()->hash_to_index(hash); phh@3427: zgu@3900: for (HashtableEntry* e = the_table()->bucket(index); e != NULL; e = e->next()) { phh@3427: if (e->hash() == hash) { phh@3427: Symbol* literal_sym = e->literal(); phh@3427: if (sym == literal_sym) { phh@3427: return e->literal_addr(); phh@3427: } phh@3427: } phh@3427: } phh@3427: return NULL; phh@3427: } phh@3427: jrose@1100: // Suggestion: Push unicode-based lookup all the way into the hashing jrose@1100: // and probing logic, so there is no need for convert_to_utf8 until coleenp@2497: // an actual new Symbol* is created. coleenp@2497: Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) { jrose@1100: int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); jrose@1100: char stack_buf[128]; jrose@1100: if (utf8_length < (int) sizeof(stack_buf)) { jrose@1100: char* chars = stack_buf; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup(chars, utf8_length, THREAD); jrose@1100: } else { jrose@1100: ResourceMark rm(THREAD); jrose@1100: char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup(chars, utf8_length, THREAD); jrose@1100: } jrose@1100: } jrose@1100: coleenp@2497: Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length, jrose@1100: unsigned int& hash) { jrose@1100: int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); jrose@1100: char stack_buf[128]; jrose@1100: if (utf8_length < (int) sizeof(stack_buf)) { jrose@1100: char* chars = stack_buf; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup_only(chars, utf8_length, hash); jrose@1100: } else { jrose@1100: ResourceMark rm; jrose@1100: char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup_only(chars, utf8_length, hash); jrose@1100: } jrose@1100: } jrose@1100: coleenp@4037: void SymbolTable::add(ClassLoaderData* loader_data, constantPoolHandle cp, coleenp@3682: int names_count, duke@435: const char** names, int* lengths, int* cp_indices, duke@435: unsigned int* hashValues, TRAPS) { coleenp@3875: // Grab SymbolTable_lock first. coleenp@3875: MutexLocker ml(SymbolTable_lock, THREAD); coleenp@3875: duke@435: SymbolTable* table = the_table(); coleenp@4037: bool added = table->basic_add(loader_data, cp, names_count, names, lengths, duke@435: cp_indices, hashValues, CHECK); duke@435: if (!added) { duke@435: // do it the hard way duke@435: for (int i=0; ihash_to_index(hashValues[i]); coleenp@4037: bool c_heap = !loader_data->is_the_null_class_loader_data(); coleenp@3682: Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i], hashValues[i], c_heap, CHECK); duke@435: cp->symbol_at_put(cp_indices[i], sym); duke@435: } duke@435: } duke@435: } duke@435: coleenp@3682: Symbol* SymbolTable::new_permanent_symbol(const char* name, TRAPS) { coleenp@3682: unsigned int hash; coleenp@3682: Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash); coleenp@3682: if (result != NULL) { coleenp@3682: return result; coleenp@3682: } coleenp@3875: // Grab SymbolTable_lock first. coleenp@3875: MutexLocker ml(SymbolTable_lock, THREAD); coleenp@3875: coleenp@3682: SymbolTable* table = the_table(); coleenp@3682: int index = table->hash_to_index(hash); coleenp@3682: return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD); coleenp@3682: } coleenp@3682: coleenp@3875: Symbol* SymbolTable::basic_add(int index_arg, u1 *name, int len, coleenp@3865: unsigned int hashValue_arg, bool c_heap, TRAPS) { duke@435: assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), duke@435: "proposed name of symbol must be stable"); duke@435: coleenp@3875: // Don't allow symbols to be created which cannot fit in a Symbol*. coleenp@3875: if (len > Symbol::max_length()) { coleenp@3875: THROW_MSG_0(vmSymbols::java_lang_InternalError(), coleenp@3875: "name is too long to represent"); coleenp@3875: } coleenp@3875: coleenp@3875: // Cannot hit a safepoint in this function because the "this" pointer can move. coleenp@3875: No_Safepoint_Verifier nsv; duke@435: coleenp@3865: // Check if the symbol table has been rehashed, if so, need to recalculate coleenp@3875: // the hash value and index. coleenp@3875: unsigned int hashValue; coleenp@3875: int index; coleenp@3875: if (use_alternate_hashcode()) { coleenp@3875: hashValue = hash_symbol((const char*)name, len); coleenp@3875: index = hash_to_index(hashValue); coleenp@3875: } else { coleenp@3875: hashValue = hashValue_arg; coleenp@3875: index = index_arg; coleenp@3875: } coleenp@3865: duke@435: // Since look-up was done lock-free, we need to check if another duke@435: // thread beat us in the race to insert the symbol. coleenp@2497: Symbol* test = lookup(index, (char*)name, len, hashValue); duke@435: if (test != NULL) { coleenp@3682: // A race occurred and another thread introduced the symbol. coleenp@2497: assert(test->refcount() != 0, "lookup should have incremented the count"); duke@435: return test; duke@435: } duke@435: coleenp@3682: // Create a new symbol. coleenp@3682: Symbol* sym = allocate_symbol(name, len, c_heap, CHECK_NULL); coleenp@3682: assert(sym->equals((char*)name, len), "symbol must be properly initialized"); coleenp@3682: zgu@3900: HashtableEntry* entry = new_entry(hashValue, sym); duke@435: add_entry(index, entry); coleenp@2497: return sym; duke@435: } duke@435: coleenp@3682: // This version of basic_add adds symbols in batch from the constant pool coleenp@3682: // parsing. coleenp@4037: bool SymbolTable::basic_add(ClassLoaderData* loader_data, constantPoolHandle cp, coleenp@3682: int names_count, duke@435: const char** names, int* lengths, duke@435: int* cp_indices, unsigned int* hashValues, duke@435: TRAPS) { coleenp@3682: coleenp@3682: // Check symbol names are not too long. If any are too long, don't add any. coleenp@3682: for (int i = 0; i< names_count; i++) { coleenp@3682: if (lengths[i] > Symbol::max_length()) { coleenp@3682: THROW_MSG_0(vmSymbols::java_lang_InternalError(), coleenp@3682: "name is too long to represent"); coleenp@3682: } duke@435: } duke@435: coleenp@3875: // Cannot hit a safepoint in this function because the "this" pointer can move. coleenp@3875: No_Safepoint_Verifier nsv; duke@435: coleenp@2497: for (int i=0; isymbol_at_put(cp_indices[i], test); coleenp@2497: assert(test->refcount() != 0, "lookup should have incremented the count"); duke@435: } else { coleenp@3682: // Create a new symbol. The null class loader is never unloaded so these coleenp@3682: // are allocated specially in a permanent arena. coleenp@4037: bool c_heap = !loader_data->is_the_null_class_loader_data(); coleenp@3682: Symbol* sym = allocate_symbol((const u1*)names[i], lengths[i], c_heap, CHECK_(false)); coleenp@3682: assert(sym->equals(names[i], lengths[i]), "symbol must be properly initialized"); // why wouldn't it be??? zgu@3900: HashtableEntry* entry = new_entry(hashValue, sym); duke@435: add_entry(index, entry); duke@435: cp->symbol_at_put(cp_indices[i], sym); duke@435: } duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: duke@435: void SymbolTable::verify() { duke@435: for (int i = 0; i < the_table()->table_size(); ++i) { zgu@3900: HashtableEntry* p = the_table()->bucket(i); duke@435: for ( ; p != NULL; p = p->next()) { coleenp@2497: Symbol* s = (Symbol*)(p->literal()); duke@435: guarantee(s != NULL, "symbol is NULL"); duke@435: unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length()); duke@435: guarantee(p->hash() == h, "broken hash in symbol table entry"); duke@435: guarantee(the_table()->hash_to_index(h) == i, duke@435: "wrong index in symbol table"); duke@435: } duke@435: } duke@435: } duke@435: coleenp@3865: void SymbolTable::dump(outputStream* st) { iklam@5144: the_table()->dump_table(st, "SymbolTable"); coleenp@3865: } coleenp@3865: duke@435: duke@435: //--------------------------------------------------------------------------- duke@435: // Non-product code duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void SymbolTable::print_histogram() { duke@435: MutexLocker ml(SymbolTable_lock); duke@435: const int results_length = 100; duke@435: int results[results_length]; duke@435: int i,j; duke@435: duke@435: // initialize results to zero duke@435: for (j = 0; j < results_length; j++) { duke@435: results[j] = 0; duke@435: } duke@435: duke@435: int total = 0; duke@435: int max_symbols = 0; duke@435: int out_of_range = 0; coleenp@2497: int memory_total = 0; coleenp@2497: int count = 0; duke@435: for (i = 0; i < the_table()->table_size(); i++) { zgu@3900: HashtableEntry* p = the_table()->bucket(i); duke@435: for ( ; p != NULL; p = p->next()) { coleenp@4037: memory_total += p->literal()->size(); coleenp@2497: count++; coleenp@2497: int counter = p->literal()->utf8_length(); duke@435: total += counter; duke@435: if (counter < results_length) { duke@435: results[counter]++; duke@435: } else { duke@435: out_of_range++; duke@435: } duke@435: max_symbols = MAX2(max_symbols, counter); duke@435: } duke@435: } duke@435: tty->print_cr("Symbol Table:"); coleenp@2497: tty->print_cr("Total number of symbols %5d", count); coleenp@2497: tty->print_cr("Total size in memory %5dK", coleenp@2497: (memory_total*HeapWordSize)/1024); coleenp@2497: tty->print_cr("Total counted %5d", symbols_counted); coleenp@2497: tty->print_cr("Total removed %5d", symbols_removed); coleenp@2497: if (symbols_counted > 0) { coleenp@2497: tty->print_cr("Percent removed %3.2f", coleenp@2497: ((float)symbols_removed/(float)symbols_counted)* 100); coleenp@2497: } coleenp@2497: tty->print_cr("Reference counts %5d", Symbol::_total_count); coleenp@3682: tty->print_cr("Symbol arena size %5d used %5d", coleenp@3682: arena()->size_in_bytes(), arena()->used()); coleenp@2497: tty->print_cr("Histogram of symbol length:"); duke@435: tty->print_cr("%8s %5d", "Total ", total); duke@435: tty->print_cr("%8s %5d", "Maximum", max_symbols); duke@435: tty->print_cr("%8s %3.2f", "Average", duke@435: ((float) total / (float) the_table()->table_size())); duke@435: tty->print_cr("%s", "Histogram:"); duke@435: tty->print_cr(" %s %29s", "Length", "Number chains that length"); duke@435: for (i = 0; i < results_length; i++) { duke@435: if (results[i] > 0) { duke@435: tty->print_cr("%6d %10d", i, results[i]); duke@435: } duke@435: } coleenp@2497: if (Verbose) { coleenp@2497: int line_length = 70; coleenp@2497: tty->print_cr("%s %30s", " Length", "Number chains that length"); coleenp@2497: for (i = 0; i < results_length; i++) { coleenp@2497: if (results[i] > 0) { coleenp@2497: tty->print("%4d", i); coleenp@2497: for (j = 0; (j < results[i]) && (j < line_length); j++) { coleenp@2497: tty->print("%1s", "*"); coleenp@2497: } coleenp@2497: if (j == line_length) { coleenp@2497: tty->print("%1s", "+"); coleenp@2497: } coleenp@2497: tty->cr(); duke@435: } coleenp@2497: } coleenp@2497: } coleenp@2497: tty->print_cr(" %s %d: %d\n", "Number chains longer than", coleenp@2497: results_length, out_of_range); coleenp@2497: } coleenp@2497: coleenp@2497: void SymbolTable::print() { coleenp@2497: for (int i = 0; i < the_table()->table_size(); ++i) { zgu@3900: HashtableEntry** p = the_table()->bucket_addr(i); zgu@3900: HashtableEntry* entry = the_table()->bucket(i); coleenp@2497: if (entry != NULL) { coleenp@2497: while (entry != NULL) { coleenp@2497: tty->print(PTR_FORMAT " ", entry->literal()); coleenp@2497: entry->literal()->print(); coleenp@2497: tty->print(" %d", entry->literal()->refcount()); coleenp@2497: p = entry->next_addr(); zgu@3900: entry = (HashtableEntry*)HashtableEntry::make_ptr(*p); duke@435: } duke@435: tty->cr(); duke@435: } duke@435: } duke@435: } duke@435: #endif // PRODUCT duke@435: duke@435: // -------------------------------------------------------------------------- duke@435: duke@435: #ifdef ASSERT duke@435: class StableMemoryChecker : public StackObj { duke@435: enum { _bufsize = wordSize*4 }; duke@435: duke@435: address _region; duke@435: jint _size; duke@435: u1 _save_buf[_bufsize]; duke@435: duke@435: int sample(u1* save_buf) { duke@435: if (_size <= _bufsize) { duke@435: memcpy(save_buf, _region, _size); duke@435: return _size; duke@435: } else { duke@435: // copy head and tail duke@435: memcpy(&save_buf[0], _region, _bufsize/2); duke@435: memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2); duke@435: return (_bufsize/2)*2; duke@435: } duke@435: } duke@435: duke@435: public: duke@435: StableMemoryChecker(const void* region, jint size) { duke@435: _region = (address) region; duke@435: _size = size; duke@435: sample(_save_buf); duke@435: } duke@435: duke@435: bool verify() { duke@435: u1 check_buf[sizeof(_save_buf)]; duke@435: int check_size = sample(check_buf); duke@435: return (0 == memcmp(_save_buf, check_buf, check_size)); duke@435: } duke@435: duke@435: void set_region(const void* region) { _region = (address) region; } duke@435: }; duke@435: #endif duke@435: duke@435: duke@435: // -------------------------------------------------------------------------- duke@435: StringTable* StringTable::_the_table = NULL; duke@435: coleenp@3865: bool StringTable::_needs_rehashing = false; coleenp@3865: coleenp@3865: // Pick hashing algorithm coleenp@3875: unsigned int StringTable::hash_string(const jchar* s, int len) { coleenp@3865: return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) : brutisso@4335: java_lang_String::hash_code(s, len); coleenp@3865: } coleenp@3865: duke@435: oop StringTable::lookup(int index, jchar* name, duke@435: int len, unsigned int hash) { coleenp@3865: int count = 0; zgu@3900: for (HashtableEntry* l = bucket(index); l != NULL; l = l->next()) { coleenp@3865: count++; duke@435: if (l->hash() == hash) { duke@435: if (java_lang_String::equals(l->literal(), name, len)) { duke@435: return l->literal(); duke@435: } duke@435: } duke@435: } coleenp@3865: // If the bucket size is too deep check if this hash code is insufficient. zgu@3900: if (count >= BasicHashtable::rehash_count && !needs_rehashing()) { coleenp@3865: _needs_rehashing = check_rehash_table(count); coleenp@3865: } duke@435: return NULL; duke@435: } duke@435: duke@435: coleenp@3875: oop StringTable::basic_add(int index_arg, Handle string, jchar* name, coleenp@3865: int len, unsigned int hashValue_arg, TRAPS) { duke@435: duke@435: assert(java_lang_String::equals(string(), name, len), duke@435: "string must be properly initialized"); coleenp@3875: // Cannot hit a safepoint in this function because the "this" pointer can move. coleenp@3875: No_Safepoint_Verifier nsv; duke@435: coleenp@3865: // Check if the symbol table has been rehashed, if so, need to recalculate coleenp@3875: // the hash value and index before second lookup. coleenp@3875: unsigned int hashValue; coleenp@3875: int index; coleenp@3875: if (use_alternate_hashcode()) { coleenp@3875: hashValue = hash_string(name, len); coleenp@3875: index = hash_to_index(hashValue); coleenp@3875: } else { coleenp@3875: hashValue = hashValue_arg; coleenp@3875: index = index_arg; coleenp@3875: } coleenp@3865: duke@435: // Since look-up was done lock-free, we need to check if another duke@435: // thread beat us in the race to insert the symbol. duke@435: duke@435: oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int) duke@435: if (test != NULL) { duke@435: // Entry already added duke@435: return test; duke@435: } duke@435: zgu@3900: HashtableEntry* entry = new_entry(hashValue, string()); duke@435: add_entry(index, entry); duke@435: return string(); duke@435: } duke@435: duke@435: coleenp@2497: oop StringTable::lookup(Symbol* symbol) { duke@435: ResourceMark rm; duke@435: int length; duke@435: jchar* chars = symbol->as_unicode(length); mgerdin@4850: return lookup(chars, length); mgerdin@4850: } mgerdin@4850: mgerdin@4850: mgerdin@4850: oop StringTable::lookup(jchar* name, int len) { mgerdin@4850: unsigned int hash = hash_string(name, len); mgerdin@4850: int index = the_table()->hash_to_index(hash); mgerdin@4850: return the_table()->lookup(index, name, len, hash); duke@435: } duke@435: duke@435: duke@435: oop StringTable::intern(Handle string_or_null, jchar* name, duke@435: int len, TRAPS) { coleenp@3865: unsigned int hashValue = hash_string(name, len); duke@435: int index = the_table()->hash_to_index(hashValue); coleenp@3875: oop found_string = the_table()->lookup(index, name, len, hashValue); duke@435: duke@435: // Found coleenp@3875: if (found_string != NULL) return found_string; coleenp@3875: coleenp@3875: debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); coleenp@3875: assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), coleenp@3875: "proposed name of symbol must be stable"); coleenp@3875: coleenp@3875: Handle string; coleenp@3875: // try to reuse the string if possible coleenp@4037: if (!string_or_null.is_null()) { coleenp@3875: string = string_or_null; coleenp@3875: } else { coleenp@4037: string = java_lang_String::create_from_unicode(name, len, CHECK_NULL); coleenp@3875: } coleenp@3875: coleenp@3875: // Grab the StringTable_lock before getting the_table() because it could coleenp@3875: // change at safepoint. coleenp@3875: MutexLocker ml(StringTable_lock, THREAD); duke@435: duke@435: // Otherwise, add to symbol to table coleenp@3875: return the_table()->basic_add(index, string, name, len, duke@435: hashValue, CHECK_NULL); duke@435: } duke@435: coleenp@2497: oop StringTable::intern(Symbol* symbol, TRAPS) { duke@435: if (symbol == NULL) return NULL; duke@435: ResourceMark rm(THREAD); duke@435: int length; duke@435: jchar* chars = symbol->as_unicode(length); duke@435: Handle string; duke@435: oop result = intern(string, chars, length, CHECK_NULL); duke@435: return result; duke@435: } duke@435: duke@435: duke@435: oop StringTable::intern(oop string, TRAPS) duke@435: { duke@435: if (string == NULL) return NULL; duke@435: ResourceMark rm(THREAD); duke@435: int length; duke@435: Handle h_string (THREAD, string); hseigel@4987: jchar* chars = java_lang_String::as_unicode_string(string, length, CHECK_NULL); duke@435: oop result = intern(h_string, chars, length, CHECK_NULL); duke@435: return result; duke@435: } duke@435: duke@435: duke@435: oop StringTable::intern(const char* utf8_string, TRAPS) { duke@435: if (utf8_string == NULL) return NULL; duke@435: ResourceMark rm(THREAD); duke@435: int length = UTF8::unicode_length(utf8_string); duke@435: jchar* chars = NEW_RESOURCE_ARRAY(jchar, length); duke@435: UTF8::convert_to_unicode(utf8_string, chars, length); duke@435: Handle string; duke@435: oop result = intern(string, chars, length, CHECK_NULL); duke@435: return result; duke@435: } duke@435: stefank@5196: void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { coleenp@2497: // Readers of the table are unlocked, so we should only be removing coleenp@2497: // entries at a safepoint. coleenp@2497: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); coleenp@2497: for (int i = 0; i < the_table()->table_size(); ++i) { zgu@3900: HashtableEntry** p = the_table()->bucket_addr(i); zgu@3900: HashtableEntry* entry = the_table()->bucket(i); coleenp@3875: while (entry != NULL) { stefank@5195: assert(!entry->is_shared(), "CDS not used for the StringTable"); stefank@5195: stefank@5195: if (is_alive->do_object_b(entry->literal())) { stefank@5196: if (f != NULL) { stefank@5196: f->do_oop((oop*)entry->literal_addr()); stefank@5196: } coleenp@2497: p = entry->next_addr(); coleenp@2497: } else { coleenp@2497: *p = entry->next(); coleenp@2497: the_table()->free_entry(entry); coleenp@2497: } stefank@5195: entry = *p; coleenp@2497: } coleenp@2497: } coleenp@2497: } coleenp@2497: coleenp@2497: void StringTable::oops_do(OopClosure* f) { coleenp@2497: for (int i = 0; i < the_table()->table_size(); ++i) { zgu@3900: HashtableEntry* entry = the_table()->bucket(i); coleenp@2497: while (entry != NULL) { stefank@5195: assert(!entry->is_shared(), "CDS not used for the StringTable"); stefank@5195: coleenp@2497: f->do_oop((oop*)entry->literal_addr()); coleenp@2497: stefank@5195: entry = entry->next(); coleenp@2497: } coleenp@2497: } coleenp@2497: } coleenp@2497: duke@435: void StringTable::verify() { duke@435: for (int i = 0; i < the_table()->table_size(); ++i) { zgu@3900: HashtableEntry* p = the_table()->bucket(i); duke@435: for ( ; p != NULL; p = p->next()) { duke@435: oop s = p->literal(); duke@435: guarantee(s != NULL, "interned string is NULL"); never@2700: unsigned int h = java_lang_String::hash_string(s); duke@435: guarantee(p->hash() == h, "broken hash in string table entry"); duke@435: guarantee(the_table()->hash_to_index(h) == i, duke@435: "wrong index in string table"); duke@435: } duke@435: } duke@435: } coleenp@3865: coleenp@3865: void StringTable::dump(outputStream* st) { iklam@5144: the_table()->dump_table(st, "StringTable"); coleenp@3865: } coleenp@3865: coleenp@3865: coleenp@3865: // Create a new table and using alternate hash code, populate the new table coleenp@3865: // with the existing strings. Set flag to use the alternate hash code afterwards. coleenp@3865: void StringTable::rehash_table() { coleenp@3865: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); coleenp@3875: // This should never happen with -Xshare:dump but it might in testing mode. coleenp@3875: if (DumpSharedSpaces) return; coleenp@3865: StringTable* new_table = new StringTable(); coleenp@3865: coleenp@3865: // Rehash the table coleenp@3865: the_table()->move_to(new_table); coleenp@3865: coleenp@3865: // Delete the table and buckets (entries are reused in new table). coleenp@3865: delete _the_table; coleenp@3865: // Don't check if we need rehashing until the table gets unbalanced again. coleenp@3865: // Then rehash with a new global seed. coleenp@3865: _needs_rehashing = false; coleenp@3865: _the_table = new_table; coleenp@3865: }