/* * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
// We used to not resize at all, so let's be conservative // and not set it too short before we decide to resize, // to match previous startup behavior constdouble PREF_AVG_LIST_LEN = 8.0; // 2^24 is max size, like StringTable. const size_t END_SIZE = 24; // If a chain gets to 100 something might be wrong const size_t REHASH_LEN = 100;
#ifdef USE_LIBRARY_BASED_TLS_ONLY staticvolatilebool _lookup_shared_first = false; #else // "_lookup_shared_first" can get highly contended with many cores if multiple threads // are updating "lookup success history" in a global shared variable. If built-in TLS is available, use it. static THREAD_LOCAL bool _lookup_shared_first = false; #endif
// Static arena for symbols that are not deallocated
Arena* SymbolTable::_arena = NULL;
class SymbolTableConfig : public AllStatic { private: public: typedef Symbol* Value; // value of the Node in the hashtable
static uintx get_hash(Value const& value, bool* is_dead) {
*is_dead = (value->refcount() == 0); if (*is_dead) { return 0;
} else { return hash_symbol((constchar*)value->bytes(), value->utf8_length(), _alt_hash);
}
} // We use default allocation/deallocation but counted staticvoid* allocate_node(void* context, size_t size, Value const& value) {
SymbolTable::item_added(); return AllocateHeap(size, mtSymbol);
} staticvoid free_node(void* context, void* memory, Value const& value) { // We get here because #1 some threads lost a race to insert a newly created Symbol // or #2 we're cleaning up unused symbol. // If #1, then the symbol can be either permanent, // or regular newly created one (refcount==1) // If #2, then the symbol is dead (refcount==0)
assert(value->is_permanent() || (value->refcount() == 1) || (value->refcount() == 0), "refcount %d", value->refcount()); if (value->refcount() == 1) {
value->decrement_refcount();
assert(value->refcount() == 0, "expected dead symbol");
}
SymbolTable::delete_symbol(value);
FreeHeap(memory);
SymbolTable::item_removed();
}
};
// Initialize the arena for global symbols, size passed in depends on CDS. if (symbol_alloc_arena_size == 0) {
_arena = new (mtSymbol) Arena(mtSymbol);
} else {
_arena = new (mtSymbol) Arena(mtSymbol, symbol_alloc_arena_size);
}
}
void SymbolTable::delete_symbol(Symbol* sym) { if (sym->is_permanent()) {
MutexLocker ml(SymbolArena_lock, Mutex::_no_safepoint_check_flag); // Protect arena // Deleting permanent symbol should not occur very often (insert race condition), // so log it.
log_trace_symboltable_helper(sym, "Freeing permanent symbol"); if (!arena()->Afree(sym, sym->size())) {
log_trace_symboltable_helper(sym, "Leaked permanent symbol");
}
} else { delete sym;
}
}
Symbol* SymbolTable::allocate_symbol(constchar* name, int len, bool c_heap) {
assert (len <= Symbol::max_length(), "should be checked by caller");
Symbol* sym; if (DumpSharedSpaces) { // TODO: Special handling of Symbol allocation for DumpSharedSpaces will be removed // in JDK-8250989
c_heap = false;
} if (c_heap) { // refcount starts as 1
sym = new (len) Symbol((const u1*)name, len, 1);
assert(sym != NULL, "new should call vm_exit_out_of_memory if C_HEAP is exhausted");
} elseif (DumpSharedSpaces) { // See comments inside Symbol::operator new(size_t, int)
sym = new (len) Symbol((const u1*)name, len, PERM_REFCOUNT);
assert(sym != NULL, "new should call vm_exit_out_of_memory if failed to allocate symbol during DumpSharedSpaces");
} else { // Allocate to global arena
MutexLocker ml(SymbolArena_lock, Mutex::_no_safepoint_check_flag); // Protect arena
sym = new (len, arena()) Symbol((const u1*)name, len, PERM_REFCOUNT);
} return sym;
}
class SymbolsDo : StackObj {
SymbolClosure *_cl; public:
SymbolsDo(SymbolClosure *cl) : _cl(cl) {} booloperator()(Symbol** value) {
assert(value != NULL, "expected valid value");
assert(*value != NULL, "value should point to a symbol");
_cl->do_symbol(value); returntrue;
};
};
// Call function for all symbols in the symbol table. void SymbolTable::symbols_do(SymbolClosure *cl) {
assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint"); // all symbols from shared table
SharedSymbolIterator iter(cl);
_shared_table.iterate(&iter);
_dynamic_shared_table.iterate(&iter);
// all symbols from the dynamic table
SymbolsDo sd(cl);
_local_table->do_safepoint_scan(sd);
}
// Call function for all symbols in shared table. Used by -XX:+PrintSharedArchiveAndExit void SymbolTable::shared_symbols_do(SymbolClosure *cl) {
SharedSymbolIterator iter(cl);
_shared_table.iterate(&iter);
_dynamic_shared_table.iterate(&iter);
}
Symbol* SymbolTable::lookup_dynamic(constchar* name, int len, unsignedint hash) {
Symbol* sym = do_lookup(name, len, hash);
assert((sym == NULL) || sym->refcount() != 0, "refcount must not be zero"); return sym;
}
#if INCLUDE_CDS
Symbol* SymbolTable::lookup_shared(constchar* name, int len, unsignedint hash) {
Symbol* sym = NULL; if (!_shared_table.empty()) { if (_alt_hash) { // hash_code parameter may use alternate hashing algorithm but the shared table // always uses the same original hash code.
hash = hash_shared_symbol(name, len);
}
sym = _shared_table.lookup(name, hash, len); if (sym == NULL && DynamicArchive::is_mapped()) {
sym = _dynamic_shared_table.lookup(name, hash, len);
}
} return sym;
} #endif
// Suggestion: Push unicode-based lookup all the way into the hashing // and probing logic, so there is no need for convert_to_utf8 until // an actual new Symbol* is created.
Symbol* SymbolTable::new_symbol(const jchar* name, int utf16_length) { int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); char stack_buf[ON_STACK_BUFFER_LENGTH]; if (utf8_length < (int) sizeof(stack_buf)) { char* chars = stack_buf;
UNICODE::convert_to_utf8(name, utf16_length, chars); return new_symbol(chars, utf8_length);
} else {
ResourceMark rm; char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);
UNICODE::convert_to_utf8(name, utf16_length, chars); return new_symbol(chars, utf8_length);
}
}
void SymbolTable::new_symbols(ClassLoaderData* loader_data, const constantPoolHandle& cp, int names_count, constchar** names, int* lengths, int* cp_indices, unsignedint* hashValues) { // Note that c_heap will be true for non-strong hidden classes. // even if their loader is the boot loader because they will have a different cld. bool c_heap = !loader_data->is_the_null_class_loader_data(); for (int i = 0; i < names_count; i++) { constchar *name = names[i]; int len = lengths[i]; unsignedint hash = hashValues[i];
assert(lookup_shared(name, len, hash) == NULL, "must have checked already");
Symbol* sym = do_add_if_needed(name, len, hash, c_heap);
assert(sym->refcount() != 0, "lookup should have incremented the count");
cp->symbol_at_put(cp_indices[i], sym);
}
}
do { // Callers have looked up the symbol once, insert the symbol.
sym = allocate_symbol(name, len, heap); if (_local_table->insert(current, lookup, sym, &rehash_warning, &clean_hint)) { break;
} // In case another thread did a concurrent add, return value already in the table. // This could fail if the symbol got deleted concurrently, so loop back until success. if (_local_table->get(current, lookup, stg, &rehash_warning)) {
sym = stg.get_res_sym(); break;
}
} while(true);
update_needs_rehash(rehash_warning);
if (clean_hint) {
mark_has_items_to_clean();
check_concurrent_work();
}
log_debug(symboltable)("Cleaned " SIZE_FORMAT " of " SIZE_FORMAT,
stdd._deleted, stdc._processed);
}
void SymbolTable::check_concurrent_work() { if (_has_work) { return;
} // We should clean/resize if we have // more items than preferred load factor or // more dead items than water mark. if (has_items_to_clean() || (get_load_factor() > PREF_AVG_LIST_LEN)) {
log_debug(symboltable)("Concurrent work triggered, load factor: %f, items to clean: %s",
get_load_factor(), has_items_to_clean() ? "true" : "false");
trigger_cleanup();
}
}
void SymbolTable::do_concurrent_work(JavaThread* jt) { double load_factor = get_load_factor();
log_debug(symboltable, perf)("Concurrent work, live factor: %g", load_factor); // We prefer growing, since that also removes dead items if (load_factor > PREF_AVG_LIST_LEN && !_local_table->is_max_size_reached()) {
grow(jt);
} else {
clean_dead_entries(jt);
}
_has_work = false;
}
// Rehash bool SymbolTable::do_rehash() { if (!_local_table->is_safepoint_safe()) { returnfalse;
}
// We use current size
size_t new_size = _local_table->get_size_log2(Thread::current());
SymbolTableHash* new_table = new SymbolTableHash(new_size, END_SIZE, REHASH_LEN, true); // Use alt hash from now on
_alt_hash = true; if (!_local_table->try_move_nodes_to(Thread::current(), new_table)) {
_alt_hash = false; delete new_table; returnfalse;
}
// free old table delete _local_table;
_local_table = new_table;
class HistogramIterator : StackObj { public: staticconst size_t results_length = 100;
size_t counts[results_length];
size_t sizes[results_length];
size_t total_size;
size_t total_count;
size_t total_length;
size_t max_length;
size_t out_of_range_count;
size_t out_of_range_size;
HistogramIterator() : total_size(0), total_count(0), total_length(0),
max_length(0), out_of_range_count(0), out_of_range_size(0) { // initialize results to zero for (size_t i = 0; i < results_length; i++) {
counts[i] = 0;
sizes[i] = 0;
}
} booloperator()(Symbol** value) {
assert(value != NULL, "expected valid value");
assert(*value != NULL, "value should point to a symbol");
Symbol* sym = *value;
size_t size = sym->size();
size_t len = sym->utf8_length(); if (len < results_length) {
counts[len]++;
sizes[len] += size;
} else {
out_of_range_count++;
out_of_range_size += size;
}
total_count++;
total_size += size;
total_length += len;
max_length = MAX2(max_length, len);
returntrue;
};
};
void SymbolTable::print_histogram() {
HistogramIterator hi;
_local_table->do_scan(Thread::current(), hi);
tty->print_cr("Symbol Table Histogram:");
tty->print_cr(" Total number of symbols " SIZE_FORMAT_W(7), hi.total_count);
tty->print_cr(" Total size in memory " SIZE_FORMAT_W(7) "K", (hi.total_size * wordSize) / K);
tty->print_cr(" Total counted " SIZE_FORMAT_W(7), _symbols_counted);
tty->print_cr(" Total removed " SIZE_FORMAT_W(7), _symbols_removed); if (_symbols_counted > 0) {
tty->print_cr(" Percent removed %3.2f",
((float)_symbols_removed / _symbols_counted) * 100);
}
tty->print_cr(" Reference counts " SIZE_FORMAT_W(7), Symbol::_total_count);
tty->print_cr(" Symbol arena used " SIZE_FORMAT_W(7) "K", arena()->used() / K);
tty->print_cr(" Symbol arena size " SIZE_FORMAT_W(7) "K", arena()->size_in_bytes() / K);
tty->print_cr(" Total symbol length " SIZE_FORMAT_W(7), hi.total_length);
tty->print_cr(" Maximum symbol length " SIZE_FORMAT_W(7), hi.max_length);
tty->print_cr(" Average symbol length %7.2f", ((float)hi.total_length / hi.total_count));
tty->print_cr(" Symbol length histogram:");
tty->print_cr(" %6s %10s %10s", "Length", "#Symbols", "Size"); for (size_t i = 0; i < hi.results_length; i++) { if (hi.counts[i] > 0) {
tty->print_cr(" " SIZE_FORMAT_W(6) " " SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10) "K",
i, hi.counts[i], (hi.sizes[i] * wordSize) / K);
}
}
tty->print_cr(" >=" SIZE_FORMAT_W(6) " " SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10) "K\n",
hi.results_length, hi.out_of_range_count, (hi.out_of_range_size*wordSize) / K);
} #endif// PRODUCT
// Utility for dumping symbols
SymboltableDCmd::SymboltableDCmd(outputStream* output, bool heap) :
DCmdWithParser(output, heap),
_verbose("-verbose", "Dump the content of each symbol in the table", "BOOLEAN", false, "false") {
_dcmdparser.add_dcmd_option(&_verbose);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.