/* * Copyright (c) 2003, 2017, 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. * */ #include "precompiled.hpp" #include "classfile/altHashing.hpp" #include "classfile/dictionary.hpp" #include "classfile/javaClasses.inline.hpp" #include "classfile/moduleEntry.hpp" #include "classfile/packageEntry.hpp" #include "classfile/placeholders.hpp" #include "classfile/protectionDomainCache.hpp" #include "classfile/stringTable.hpp" #include "memory/allocation.inline.hpp" #include "memory/filemap.hpp" #include "memory/resourceArea.hpp" #include "oops/oop.inline.hpp" #include "runtime/safepoint.hpp" #include "utilities/dtrace.hpp" #include "utilities/hashtable.hpp" #include "utilities/hashtable.inline.hpp" #include "utilities/numberSeq.hpp" // This hashtable is implemented as an open hash table with a fixed number of buckets. template BasicHashtableEntry* BasicHashtable::new_entry_free_list() { BasicHashtableEntry* entry = NULL; if (_free_list != NULL) { entry = _free_list; _free_list = _free_list->next(); } return entry; } // HashtableEntrys are allocated in blocks to reduce the space overhead. template BasicHashtableEntry* BasicHashtable::new_entry(unsigned int hashValue) { BasicHashtableEntry* entry = new_entry_free_list(); if (entry == NULL) { if (_first_free_entry + _entry_size >= _end_block) { int block_size = MIN2(512, MAX2((int)_table_size / 2, (int)_number_of_entries)); int len = _entry_size * block_size; len = 1 << log2_intptr(len); // round down to power of 2 assert(len >= _entry_size, ""); _first_free_entry = NEW_C_HEAP_ARRAY2(char, len, F, CURRENT_PC); _end_block = _first_free_entry + len; } entry = (BasicHashtableEntry*)_first_free_entry; _first_free_entry += _entry_size; } assert(_entry_size % HeapWordSize == 0, ""); entry->set_hash(hashValue); return entry; } template HashtableEntry* Hashtable::new_entry(unsigned int hashValue, T obj) { HashtableEntry* entry; entry = (HashtableEntry*)BasicHashtable::new_entry(hashValue); entry->set_literal(obj); return entry; } // Check to see if the hashtable is unbalanced. The caller set a flag to // rehash at the next safepoint. If this bucket is 60 times greater than the // expected average bucket length, it's an unbalanced hashtable. // This is somewhat an arbitrary heuristic but if one bucket gets to // rehash_count which is currently 100, there's probably something wrong. template bool RehashableHashtable::check_rehash_table(int count) { assert(this->table_size() != 0, "underflow"); if (count > (((double)this->number_of_entries()/(double)this->table_size())*rehash_multiple)) { // Set a flag for the next safepoint, which should be at some guaranteed // safepoint interval. return true; } return false; } // Create a new table and using alternate hash code, populate the new table // with the existing elements. This can be used to change the hash code // and could in the future change the size of the table. template void RehashableHashtable::move_to(RehashableHashtable* new_table) { // Initialize the global seed for hashing. _seed = AltHashing::compute_seed(); assert(seed() != 0, "shouldn't be zero"); int saved_entry_count = this->number_of_entries(); // Iterate through the table and create a new entry for the new table for (int i = 0; i < new_table->table_size(); ++i) { for (HashtableEntry* p = this->bucket(i); p != NULL; ) { HashtableEntry* next = p->next(); T string = p->literal(); // Use alternate hashing algorithm on the symbol in the first table unsigned int hashValue = string->new_hash(seed()); // Get a new index relative to the new table (can also change size) int index = new_table->hash_to_index(hashValue); p->set_hash(hashValue); // Keep the shared bit in the Hashtable entry to indicate that this entry // can't be deleted. The shared bit is the LSB in the _next field so // walking the hashtable past these entries requires // BasicHashtableEntry::make_ptr() call. bool keep_shared = p->is_shared(); this->unlink_entry(p); new_table->add_entry(index, p); if (keep_shared) { p->set_shared(); } p = next; } } // give the new table the free list as well new_table->copy_freelist(this); assert(new_table->number_of_entries() == saved_entry_count, "lost entry on dictionary copy?"); // Destroy memory used by the buckets in the hashtable. The memory // for the elements has been used in a new table and is not // destroyed. The memory reuse will benefit resizing the SystemDictionary // to avoid a memory allocation spike at safepoint. BasicHashtable::free_buckets(); } template void BasicHashtable::free_buckets() { if (NULL != _buckets) { // Don't delete the buckets in the shared space. They aren't // allocated by os::malloc if (!UseSharedSpaces || !FileMapInfo::current_info()->is_in_shared_space(_buckets)) { FREE_C_HEAP_ARRAY(HashtableBucket, _buckets); } _buckets = NULL; } } template void BasicHashtable::BucketUnlinkContext::free_entry(BasicHashtableEntry* entry) { entry->set_next(_removed_head); _removed_head = entry; if (_removed_tail == NULL) { _removed_tail = entry; } _num_removed++; } template void BasicHashtable::bulk_free_entries(BucketUnlinkContext* context) { if (context->_num_removed == 0) { assert(context->_removed_head == NULL && context->_removed_tail == NULL, "Zero entries in the unlink context, but elements linked from " PTR_FORMAT " to " PTR_FORMAT, p2i(context->_removed_head), p2i(context->_removed_tail)); return; } // MT-safe add of the list of BasicHashTableEntrys from the context to the free list. BasicHashtableEntry* current = _free_list; while (true) { context->_removed_tail->set_next(current); BasicHashtableEntry* old = (BasicHashtableEntry*)Atomic::cmpxchg_ptr(context->_removed_head, &_free_list, current); if (old == current) { break; } current = old; } Atomic::add(-context->_num_removed, &_number_of_entries); } // Copy the table to the shared space. template void BasicHashtable::copy_table(char** top, char* end) { // Dump the hash table entries. intptr_t *plen = (intptr_t*)(*top); *top += sizeof(*plen); int i; for (i = 0; i < _table_size; ++i) { for (BasicHashtableEntry** p = _buckets[i].entry_addr(); *p != NULL; p = (*p)->next_addr()) { if (*top + entry_size() > end) { report_out_of_shared_space(SharedMiscData); } *p = (BasicHashtableEntry*)memcpy(*top, *p, entry_size()); *top += entry_size(); } } *plen = (char*)(*top) - (char*)plen - sizeof(*plen); // Set the shared bit. for (i = 0; i < _table_size; ++i) { for (BasicHashtableEntry* p = bucket(i); p != NULL; p = p->next()) { p->set_shared(); } } } template int RehashableHashtable::literal_size(Symbol *symbol) { return symbol->size() * HeapWordSize; } template int RehashableHashtable::literal_size(oop oop) { // NOTE: this would over-count if (pre-JDK8) java_lang_Class::has_offset_field() is true, // and the String.value array is shared by several Strings. However, starting from JDK8, // the String.value array is not shared anymore. assert(oop != NULL && oop->klass() == SystemDictionary::String_klass(), "only strings are supported"); return (oop->size() + java_lang_String::value(oop)->size()) * HeapWordSize; } // Dump footprint and bucket length statistics // // Note: if you create a new subclass of Hashtable, you will need to // add a new function Hashtable::literal_size(MyNewType lit) template void RehashableHashtable::dump_table(outputStream* st, const char *table_name) { NumberSeq summary; int literal_bytes = 0; for (int i = 0; i < this->table_size(); ++i) { int count = 0; for (HashtableEntry* e = this->bucket(i); e != NULL; e = e->next()) { count++; literal_bytes += literal_size(e->literal()); } summary.add((double)count); } double num_buckets = summary.num(); double num_entries = summary.sum(); int bucket_bytes = (int)num_buckets * sizeof(HashtableBucket); int entry_bytes = (int)num_entries * sizeof(HashtableEntry); int total_bytes = literal_bytes + bucket_bytes + entry_bytes; double bucket_avg = (num_buckets <= 0) ? 0 : (bucket_bytes / num_buckets); double entry_avg = (num_entries <= 0) ? 0 : (entry_bytes / num_entries); double literal_avg = (num_entries <= 0) ? 0 : (literal_bytes / num_entries); st->print_cr("%s statistics:", table_name); st->print_cr("Number of buckets : %9d = %9d bytes, avg %7.3f", (int)num_buckets, bucket_bytes, bucket_avg); st->print_cr("Number of entries : %9d = %9d bytes, avg %7.3f", (int)num_entries, entry_bytes, entry_avg); st->print_cr("Number of literals : %9d = %9d bytes, avg %7.3f", (int)num_entries, literal_bytes, literal_avg); st->print_cr("Total footprint : %9s = %9d bytes", "", total_bytes); st->print_cr("Average bucket size : %9.3f", summary.avg()); st->print_cr("Variance of bucket size : %9.3f", summary.variance()); st->print_cr("Std. dev. of bucket size: %9.3f", summary.sd()); st->print_cr("Maximum bucket size : %9d", (int)summary.maximum()); } // Dump the hash table buckets. template void BasicHashtable::copy_buckets(char** top, char* end) { intptr_t len = _table_size * sizeof(HashtableBucket); *(intptr_t*)(*top) = len; *top += sizeof(intptr_t); *(intptr_t*)(*top) = _number_of_entries; *top += sizeof(intptr_t); if (*top + len > end) { report_out_of_shared_space(SharedMiscData); } _buckets = (HashtableBucket*)memcpy(*top, _buckets, len); *top += len; } #ifndef PRODUCT template void Hashtable::print() { ResourceMark rm; for (int i = 0; i < BasicHashtable::table_size(); i++) { HashtableEntry* entry = bucket(i); while(entry != NULL) { tty->print("%d : ", i); entry->literal()->print(); tty->cr(); entry = entry->next(); } } } template template void BasicHashtable::verify_table(const char* table_name) { int element_count = 0; int max_bucket_count = 0; int max_bucket_number = 0; for (int index = 0; index < table_size(); index++) { int bucket_count = 0; for (T* probe = (T*)bucket(index); probe != NULL; probe = probe->next()) { probe->verify(); bucket_count++; } element_count += bucket_count; if (bucket_count > max_bucket_count) { max_bucket_count = bucket_count; max_bucket_number = index; } } guarantee(number_of_entries() == element_count, "Verify of %s failed", table_name); // Log some statistics about the hashtable log_info(hashtables)("%s max bucket size %d bucket %d element count %d table size %d", table_name, max_bucket_count, max_bucket_number, _number_of_entries, _table_size); if (_number_of_entries > 0 && log_is_enabled(Debug, hashtables)) { for (int index = 0; index < table_size(); index++) { int bucket_count = 0; for (T* probe = (T*)bucket(index); probe != NULL; probe = probe->next()) { log_debug(hashtables)("bucket %d hash " INTPTR_FORMAT, index, (intptr_t)probe->hash()); bucket_count++; } if (bucket_count > 0) { log_debug(hashtables)("bucket %d count %d", index, bucket_count); } } } } #endif // PRODUCT // Explicitly instantiate these types #if INCLUDE_ALL_GCS template class Hashtable; template class HashtableEntry; template class BasicHashtable; #endif template class Hashtable; template class RehashableHashtable; template class RehashableHashtable; template class Hashtable; template class Hashtable; template class Hashtable; template class Hashtable; #if defined(SOLARIS) || defined(CHECK_UNHANDLED_OOPS) template class Hashtable; template class RehashableHashtable; #endif // SOLARIS || CHECK_UNHANDLED_OOPS template class Hashtable; template class Hashtable; template class HashtableEntry; template class HashtableEntry; template class HashtableEntry; template class BasicHashtableEntry; template class BasicHashtableEntry; template class BasicHashtable; template class BasicHashtable; template class BasicHashtable; template class BasicHashtable; template class BasicHashtable; template class BasicHashtable; #if INCLUDE_TRACE template class Hashtable; template class HashtableEntry; template class BasicHashtable; #endif template class BasicHashtable; template void BasicHashtable::verify_table(char const*); template void BasicHashtable::verify_table(char const*); template void BasicHashtable::verify_table(char const*); template void BasicHashtable::verify_table(char const*); template void BasicHashtable::verify_table(char const*);