diff --git a/src/hotspot/share/prims/jvmtiTagMapTable.hpp b/src/hotspot/share/prims/jvmtiTagMapTable.hpp index 6714cbabb5d..7d52cf4fa73 100644 --- a/src/hotspot/share/prims/jvmtiTagMapTable.hpp +++ b/src/hotspot/share/prims/jvmtiTagMapTable.hpp @@ -28,7 +28,6 @@ #include "gc/shared/collectedHeap.hpp" #include "memory/allocation.hpp" #include "oops/weakHandle.hpp" -#include "utilities/hashtable.hpp" #include "utilities/resizeableResourceHash.hpp" class JvmtiEnv; diff --git a/src/hotspot/share/utilities/hashtable.cpp b/src/hotspot/share/utilities/hashtable.cpp deleted file mode 100644 index 6b0a081c9e7..00000000000 --- a/src/hotspot/share/utilities/hashtable.cpp +++ /dev/null @@ -1,257 +0,0 @@ -/* - * Copyright (c) 2003, 2023, 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/dictionary.hpp" -#include "classfile/javaClasses.inline.hpp" -#include "classfile/vmClasses.hpp" -#include "code/nmethod.hpp" -#include "logging/log.hpp" -#include "memory/allocation.inline.hpp" -#include "memory/resourceArea.hpp" -#include "oops/oop.inline.hpp" -#include "oops/symbol.hpp" -#include "oops/weakHandle.inline.hpp" -#include "prims/jvmtiTagMapTable.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. - -// Hashtable entry allocates in the C heap directly. - -template BasicHashtableEntry* BasicHashtable::new_entry(unsigned int hashValue) { - BasicHashtableEntry* entry = ::new (NEW_C_HEAP_ARRAY(char, this->entry_size(), F)) - BasicHashtableEntry(hashValue); - return entry; -} - - -template HashtableEntry* Hashtable::new_entry(unsigned int hashValue, T obj) { - HashtableEntry* entry = ::new (NEW_C_HEAP_ARRAY(char, this->entry_size(), F)) - HashtableEntry(hashValue, obj); - return entry; -} - -template inline void BasicHashtable::free_entry(BasicHashtableEntry* entry) { - // Unlink from the Hashtable prior to freeing - unlink_entry(entry); - FREE_C_HEAP_ARRAY(char, entry); - JFR_ONLY(_stats_rate.remove();) -} - - -template void BasicHashtable::free_buckets() { - FREE_C_HEAP_ARRAY(HashtableBucket, _buckets); - _buckets = nullptr; -} - -// Default overload, for types that are uninteresting. -template static size_t literal_size(T) { return 0; } - -static size_t literal_size(oop obj) { - if (obj == nullptr) { - return 0; - } - - size_t word_size = obj->size(); - - if (obj->klass() == vmClasses::String_klass()) { - // This may overcount if String.value arrays are shared. - word_size += java_lang_String::value(obj)->size(); - } - - return word_size * HeapWordSize; -} - -static size_t literal_size(WeakHandle v) { - return literal_size(v.peek()); -} - -const double _resize_factor = 2.0; // by how much we will resize using current number of entries -const int _small_table_sizes[] = { 107, 1009, 2017, 4049, 5051, 10103, 20201, 40423 } ; -const int _small_array_size = sizeof(_small_table_sizes)/sizeof(int); - -// possible hashmap sizes - odd primes that roughly double in size. -// To avoid excessive resizing the odd primes from 4801-76831 and -// 76831-307261 have been removed. -const int _large_table_sizes[] = { 4801, 76831, 307261, 614563, 1228891, - 2457733, 4915219, 9830479, 19660831, 39321619, 78643219 }; -const int _large_array_size = sizeof(_large_table_sizes)/sizeof(int); - -// Calculate next "good" hashtable size based on requested count -template int BasicHashtable::calculate_resize(bool use_large_table_sizes) const { - int requested = (int)(_resize_factor*number_of_entries()); - const int* primelist = use_large_table_sizes ? _large_table_sizes : _small_table_sizes; - int arraysize = use_large_table_sizes ? _large_array_size : _small_array_size; - int newsize; - for (int i = 0; i < arraysize; i++) { - newsize = primelist[i]; - if (newsize >= requested) - break; - } - return newsize; -} - -template bool BasicHashtable::resize(int new_size) { - - // Allocate new buckets - HashtableBucket* buckets_new = NEW_C_HEAP_ARRAY2_RETURN_NULL(HashtableBucket, new_size, F, CURRENT_PC); - if (buckets_new == nullptr) { - return false; - } - - // Clear the new buckets - for (int i = 0; i < new_size; i++) { - buckets_new[i].clear(); - } - - int table_size_old = _table_size; - // hash_to_index() uses _table_size, so switch the sizes now - _table_size = new_size; - - // Move entries from the old table to a new table - for (int index_old = 0; index_old < table_size_old; index_old++) { - for (BasicHashtableEntry* p = _buckets[index_old].get_entry(); p != nullptr; ) { - BasicHashtableEntry* next = p->next(); - int index_new = hash_to_index(p->hash()); - - p->set_next(buckets_new[index_new].get_entry()); - buckets_new[index_new].set_entry(p); - p = next; - } - } - - // The old backets now can be released - BasicHashtable::free_buckets(); - - // Switch to the new storage - _buckets = buckets_new; - - return true; -} - -template bool BasicHashtable::maybe_grow(int max_size, int load_factor) { - assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); - - if (table_size() >= max_size) { - return false; - } - if (number_of_entries() / table_size() > load_factor) { - resize(MIN2(table_size() * 2, max_size)); - return true; - } else { - return false; - } -} - -template TableStatistics Hashtable::statistics_calculate(T (*literal_load_barrier)(HashtableEntry*)) { - NumberSeq summary; - size_t literal_bytes = 0; - for (int i = 0; i < this->table_size(); ++i) { - int count = 0; - for (HashtableEntry* e = this->bucket(i); - e != nullptr; e = e->next()) { - count++; - T l = (literal_load_barrier != nullptr) ? literal_load_barrier(e) : e->literal(); - literal_bytes += literal_size(l); - } - summary.add((double)count); - } - return TableStatistics(this->_stats_rate, summary, literal_bytes, sizeof(HashtableBucket), sizeof(HashtableEntry)); -} - -// Dump footprint and bucket length statistics -template void Hashtable::print_table_statistics(outputStream* st, - const char *table_name, - T (*literal_load_barrier)(HashtableEntry*)) { - TableStatistics ts = statistics_calculate(literal_load_barrier); - ts.print(st, table_name); -} - -#ifndef PRODUCT -template static void print_literal(T const& l) { l.print(); } -template static void print_literal(T* l) { print_literal(*l); } - -template void Hashtable::print() { - ResourceMark rm; - - for (int i = 0; i < BasicHashtable::table_size(); i++) { - HashtableEntry* entry = bucket(i); - while(entry != nullptr) { - tty->print("%d : ", i); - print_literal(entry->literal()); - 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 != nullptr; 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 != nullptr; 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 -template class BasicHashtable; -template class BasicHashtable; - -template class Hashtable; -template class Hashtable; diff --git a/src/hotspot/share/utilities/hashtable.hpp b/src/hotspot/share/utilities/hashtable.hpp deleted file mode 100644 index 54d4162cd13..00000000000 --- a/src/hotspot/share/utilities/hashtable.hpp +++ /dev/null @@ -1,223 +0,0 @@ -/* - * Copyright (c) 2003, 2023, 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. - * - */ - -#ifndef SHARE_UTILITIES_HASHTABLE_HPP -#define SHARE_UTILITIES_HASHTABLE_HPP - -#include "memory/allocation.hpp" -#include "oops/symbol.hpp" -#include "runtime/handles.hpp" -#include "utilities/tableStatistics.hpp" - -// This is a generic hashtable which is implemented as an open hash table with -// a fixed number of buckets. - -template class BasicHashtableEntry { - friend class VMStructs; -private: - unsigned int _hash; // 32-bit hash for item - - // Link to next element in the linked list for this bucket. - BasicHashtableEntry* _next; - -public: - BasicHashtableEntry(unsigned int hashValue) : _hash(hashValue), _next(nullptr) {} - // Still should not call this. Entries are placement new allocated, so are - // deleted with free_entry. - ~BasicHashtableEntry() { ShouldNotReachHere(); } - - unsigned int hash() const { return _hash; } - void set_hash(unsigned int hash) { _hash = hash; } - unsigned int* hash_addr() { return &_hash; } - - BasicHashtableEntry* next() const { - return _next; - } - - void set_next(BasicHashtableEntry* next) { - _next = next; - } - - BasicHashtableEntry** next_addr() { - return &_next; - } -}; - - - -template class HashtableEntry : public BasicHashtableEntry { - friend class VMStructs; -private: - T _literal; // ref to item in table. - -public: - HashtableEntry(unsigned int hashValue, T value) : BasicHashtableEntry(hashValue), _literal(value) {} - - // Literal - T literal() const { return _literal; } - T* literal_addr() { return &_literal; } - void set_literal(T s) { _literal = s; } - - HashtableEntry* next() const { - return (HashtableEntry*)BasicHashtableEntry::next(); - } - HashtableEntry** next_addr() { - return (HashtableEntry**)BasicHashtableEntry::next_addr(); - } -}; - - - -template class HashtableBucket : public CHeapObj { - friend class VMStructs; -private: - // Instance variable - BasicHashtableEntry* _entry; - -public: - // Accessing - void clear() { _entry = nullptr; } - - // The following methods use order access methods to avoid race - // conditions in multiprocessor systems. - BasicHashtableEntry* get_entry() const; - void set_entry(BasicHashtableEntry* l); - - // The following method is not MT-safe and must be done under lock. - BasicHashtableEntry** entry_addr() { return &_entry; } - -}; - - -template class BasicHashtable : public CHeapObj { - friend class VMStructs; - -public: - BasicHashtable(int table_size, int entry_size); - BasicHashtable(int table_size, int entry_size, - HashtableBucket* buckets, int number_of_entries); - ~BasicHashtable(); - - // Bucket handling - int hash_to_index(unsigned int full_hash) const { - int h = full_hash % _table_size; - assert(h >= 0 && h < _table_size, "Illegal hash value"); - return h; - } - -private: - // Instance variables - int _table_size; - HashtableBucket* _buckets; - int _entry_size; - volatile int _number_of_entries; - -protected: - - TableRateStatistics _stats_rate; - - void initialize(int table_size, int entry_size, int number_of_entries); - - // Accessor - int entry_size() const { return _entry_size; } - - // The following method is MT-safe and may be used with caution. - BasicHashtableEntry* bucket(int i) const; - - // The following method is not MT-safe and must be done under lock. - BasicHashtableEntry** bucket_addr(int i) { return _buckets[i].entry_addr(); } - - // Table entry management - BasicHashtableEntry* new_entry(unsigned int hashValue); - - // Used when moving the entry to another table or deleting entry. - // Clean up links. - void unlink_entry(BasicHashtableEntry* entry) { - entry->set_next(nullptr); - --_number_of_entries; - } - - // Free the buckets in this hashtable - void free_buckets(); -public: - int table_size() const { return _table_size; } - void set_entry(int index, BasicHashtableEntry* entry); - - void add_entry(int index, BasicHashtableEntry* entry); - - void free_entry(BasicHashtableEntry* entry); - - int number_of_entries() const { return _number_of_entries; } - - int calculate_resize(bool use_large_table_sizes) const; - bool resize(int new_size); - - // Grow the number of buckets if the average entries per bucket is over the load_factor - bool maybe_grow(int max_size, int load_factor = 8); - - template void verify_table(const char* table_name) PRODUCT_RETURN; -}; - - -template class Hashtable : public BasicHashtable { - friend class VMStructs; - -public: - Hashtable(int table_size, int entry_size) - : BasicHashtable(table_size, entry_size) { } - - Hashtable(int table_size, int entry_size, - HashtableBucket* buckets, int number_of_entries) - : BasicHashtable(table_size, entry_size, buckets, number_of_entries) { } - - // Debugging - void print() PRODUCT_RETURN; - - unsigned int compute_hash(const Symbol* name) const { - return (unsigned int) name->identity_hash(); - } - - int index_for(const Symbol* name) const { - return this->hash_to_index(compute_hash(name)); - } - - TableStatistics statistics_calculate(T (*literal_load_barrier)(HashtableEntry*) = nullptr); - void print_table_statistics(outputStream* st, const char *table_name, T (*literal_load_barrier)(HashtableEntry*) = nullptr); - - protected: - - HashtableEntry* new_entry(unsigned int hashValue, T obj); - - // The following method is MT-safe and may be used with caution. - HashtableEntry* bucket(int i) const { - return (HashtableEntry*)BasicHashtable::bucket(i); - } - - // The following method is not MT-safe and must be done under lock. - HashtableEntry** bucket_addr(int i) { - return (HashtableEntry**)BasicHashtable::bucket_addr(i); - } -}; - -#endif // SHARE_UTILITIES_HASHTABLE_HPP diff --git a/src/hotspot/share/utilities/hashtable.inline.hpp b/src/hotspot/share/utilities/hashtable.inline.hpp deleted file mode 100644 index a4ae469f7fd..00000000000 --- a/src/hotspot/share/utilities/hashtable.inline.hpp +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2003, 2023, 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. - * - */ - -#ifndef SHARE_UTILITIES_HASHTABLE_INLINE_HPP -#define SHARE_UTILITIES_HASHTABLE_INLINE_HPP - -#include "utilities/hashtable.hpp" - -#include "memory/allocation.inline.hpp" -#include "runtime/atomic.hpp" -#include "services/memTracker.hpp" - -// Inline function definitions for hashtable.hpp. - -// -------------------------------------------------------------------------- - -// Initialize a table. - -template inline BasicHashtable::BasicHashtable(int table_size, int entry_size) { - // Called on startup, no locking needed - initialize(table_size, entry_size, 0); - _buckets = NEW_C_HEAP_ARRAY2(HashtableBucket, table_size, F, CURRENT_PC); - for (int index = 0; index < _table_size; index++) { - _buckets[index].clear(); - } - _stats_rate = TableRateStatistics(); -} - - -template inline BasicHashtable::BasicHashtable(int table_size, int entry_size, - HashtableBucket* buckets, - int number_of_entries) { - - // Called on startup, no locking needed - initialize(table_size, entry_size, number_of_entries); - _buckets = buckets; - _stats_rate = TableRateStatistics(); -} - -template inline BasicHashtable::~BasicHashtable() { - free_buckets(); -} - -template inline void BasicHashtable::initialize(int table_size, int entry_size, - int number_of_entries) { - // Called on startup, no locking needed - _table_size = table_size; - _entry_size = entry_size; - _number_of_entries = number_of_entries; -} - - -// The following method is MT-safe and may be used with caution. -template inline BasicHashtableEntry* BasicHashtable::bucket(int i) const { - return _buckets[i].get_entry(); -} - - -template inline void HashtableBucket::set_entry(BasicHashtableEntry* l) { - // Warning: Preserve store ordering. The PackageEntryTable, ModuleEntryTable and - // SystemDictionary are read without locks. The new entry must be - // complete before other threads can be allowed to see it - // via a store to _buckets[index]. - Atomic::release_store(&_entry, l); -} - - -template inline BasicHashtableEntry* HashtableBucket::get_entry() const { - // Warning: Preserve load ordering. The PackageEntryTable, ModuleEntryTable and - // SystemDictionary are read without locks. The new entry must be - // complete before other threads can be allowed to see it - // via a store to _buckets[index]. - return Atomic::load_acquire(&_entry); -} - - -template inline void BasicHashtable::set_entry(int index, BasicHashtableEntry* entry) { - _buckets[index].set_entry(entry); - if (entry != nullptr) { - JFR_ONLY(_stats_rate.add();) - } else { - JFR_ONLY(_stats_rate.remove();) - } -} - - -template inline void BasicHashtable::add_entry(int index, BasicHashtableEntry* entry) { - entry->set_next(bucket(index)); - _buckets[index].set_entry(entry); - ++_number_of_entries; - JFR_ONLY(_stats_rate.add();) -} - -#endif // SHARE_UTILITIES_HASHTABLE_INLINE_HPP