Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/hashtable.cpp
32285 views
/*1* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/altHashing.hpp"26#include "classfile/javaClasses.hpp"27#include "memory/allocation.inline.hpp"28#include "memory/filemap.hpp"29#include "memory/resourceArea.hpp"30#include "oops/oop.inline.hpp"31#include "runtime/safepoint.hpp"32#include "utilities/dtrace.hpp"33#include "utilities/hashtable.hpp"34#include "utilities/hashtable.inline.hpp"35#include "utilities/numberSeq.hpp"363738// This hashtable is implemented as an open hash table with a fixed number of buckets.3940template <MEMFLAGS F> BasicHashtableEntry<F>* BasicHashtable<F>::new_entry_free_list() {41BasicHashtableEntry<F>* entry = NULL;42if (_free_list != NULL) {43entry = _free_list;44_free_list = _free_list->next();45}46return entry;47}4849// HashtableEntrys are allocated in blocks to reduce the space overhead.50template <MEMFLAGS F> BasicHashtableEntry<F>* BasicHashtable<F>::new_entry(unsigned int hashValue) {51BasicHashtableEntry<F>* entry = new_entry_free_list();5253if (entry == NULL) {54if (_first_free_entry + _entry_size >= _end_block) {55int block_size = MIN2(512, MAX2((int)_table_size / 2, (int)_number_of_entries));56int len = _entry_size * block_size;57len = 1 << log2_int(len); // round down to power of 258assert(len >= _entry_size, "");59_first_free_entry = NEW_C_HEAP_ARRAY2(char, len, F, CURRENT_PC);60_end_block = _first_free_entry + len;61}62entry = (BasicHashtableEntry<F>*)_first_free_entry;63_first_free_entry += _entry_size;64}6566assert(_entry_size % HeapWordSize == 0, "");67entry->set_hash(hashValue);68return entry;69}707172template <class T, MEMFLAGS F> HashtableEntry<T, F>* Hashtable<T, F>::new_entry(unsigned int hashValue, T obj) {73HashtableEntry<T, F>* entry;7475entry = (HashtableEntry<T, F>*)BasicHashtable<F>::new_entry(hashValue);76entry->set_literal(obj);77return entry;78}7980// Check to see if the hashtable is unbalanced. The caller set a flag to81// rehash at the next safepoint. If this bucket is 60 times greater than the82// expected average bucket length, it's an unbalanced hashtable.83// This is somewhat an arbitrary heuristic but if one bucket gets to84// rehash_count which is currently 100, there's probably something wrong.8586template <class T, MEMFLAGS F> bool RehashableHashtable<T, F>::check_rehash_table(int count) {87assert(this->table_size() != 0, "underflow");88if (count > (((double)this->number_of_entries()/(double)this->table_size())*rehash_multiple)) {89// Set a flag for the next safepoint, which should be at some guaranteed90// safepoint interval.91return true;92}93return false;94}9596template <class T, MEMFLAGS F> juint RehashableHashtable<T, F>::_seed = 0;9798// Create a new table and using alternate hash code, populate the new table99// with the existing elements. This can be used to change the hash code100// and could in the future change the size of the table.101102template <class T, MEMFLAGS F> void RehashableHashtable<T, F>::move_to(RehashableHashtable<T, F>* new_table) {103104// Initialize the global seed for hashing.105_seed = AltHashing::compute_seed();106assert(seed() != 0, "shouldn't be zero");107108int saved_entry_count = this->number_of_entries();109110// Iterate through the table and create a new entry for the new table111for (int i = 0; i < new_table->table_size(); ++i) {112for (HashtableEntry<T, F>* p = this->bucket(i); p != NULL; ) {113HashtableEntry<T, F>* next = p->next();114T string = p->literal();115// Use alternate hashing algorithm on the symbol in the first table116unsigned int hashValue = string->new_hash(seed());117// Get a new index relative to the new table (can also change size)118int index = new_table->hash_to_index(hashValue);119p->set_hash(hashValue);120// Keep the shared bit in the Hashtable entry to indicate that this entry121// can't be deleted. The shared bit is the LSB in the _next field so122// walking the hashtable past these entries requires123// BasicHashtableEntry::make_ptr() call.124bool keep_shared = p->is_shared();125this->unlink_entry(p);126new_table->add_entry(index, p);127if (keep_shared) {128p->set_shared();129}130p = next;131}132}133// give the new table the free list as well134new_table->copy_freelist(this);135assert(new_table->number_of_entries() == saved_entry_count, "lost entry on dictionary copy?");136137// Destroy memory used by the buckets in the hashtable. The memory138// for the elements has been used in a new table and is not139// destroyed. The memory reuse will benefit resizing the SystemDictionary140// to avoid a memory allocation spike at safepoint.141BasicHashtable<F>::free_buckets();142}143144template <MEMFLAGS F> void BasicHashtable<F>::free_buckets() {145if (NULL != _buckets) {146// Don't delete the buckets in the shared space. They aren't147// allocated by os::malloc148if (!UseSharedSpaces ||149!FileMapInfo::current_info()->is_in_shared_space(_buckets)) {150FREE_C_HEAP_ARRAY(HashtableBucket, _buckets, F);151}152_buckets = NULL;153}154}155156157// Reverse the order of elements in the hash buckets.158159template <MEMFLAGS F> void BasicHashtable<F>::reverse() {160161for (int i = 0; i < _table_size; ++i) {162BasicHashtableEntry<F>* new_list = NULL;163BasicHashtableEntry<F>* p = bucket(i);164while (p != NULL) {165BasicHashtableEntry<F>* next = p->next();166p->set_next(new_list);167new_list = p;168p = next;169}170*bucket_addr(i) = new_list;171}172}173174template <MEMFLAGS F> void BasicHashtable<F>::BucketUnlinkContext::free_entry(BasicHashtableEntry<F>* entry) {175entry->set_next(_removed_head);176_removed_head = entry;177if (_removed_tail == NULL) {178_removed_tail = entry;179}180_num_removed++;181}182183template <MEMFLAGS F> void BasicHashtable<F>::bulk_free_entries(BucketUnlinkContext* context) {184if (context->_num_removed == 0) {185assert(context->_removed_head == NULL && context->_removed_tail == NULL,186err_msg("Zero entries in the unlink context, but elements linked from " PTR_FORMAT " to " PTR_FORMAT,187p2i(context->_removed_head), p2i(context->_removed_tail)));188return;189}190191// MT-safe add of the list of BasicHashTableEntrys from the context to the free list.192BasicHashtableEntry<F>* current = _free_list;193while (true) {194context->_removed_tail->set_next(current);195BasicHashtableEntry<F>* old = (BasicHashtableEntry<F>*)Atomic::cmpxchg_ptr(context->_removed_head, &_free_list, current);196if (old == current) {197break;198}199current = old;200}201Atomic::add(-context->_num_removed, &_number_of_entries);202}203204// Copy the table to the shared space.205206template <MEMFLAGS F> void BasicHashtable<F>::copy_table(char** top, char* end) {207208// Dump the hash table entries.209210intptr_t *plen = (intptr_t*)(*top);211*top += sizeof(*plen);212213int i;214for (i = 0; i < _table_size; ++i) {215for (BasicHashtableEntry<F>** p = _buckets[i].entry_addr();216*p != NULL;217p = (*p)->next_addr()) {218if (*top + entry_size() > end) {219report_out_of_shared_space(SharedMiscData);220}221*p = (BasicHashtableEntry<F>*)memcpy(*top, *p, entry_size());222*top += entry_size();223}224}225*plen = (char*)(*top) - (char*)plen - sizeof(*plen);226227// Set the shared bit.228229for (i = 0; i < _table_size; ++i) {230for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {231p->set_shared();232}233}234}235236237238// Reverse the order of elements in the hash buckets.239240template <class T, MEMFLAGS F> void Hashtable<T, F>::reverse(void* boundary) {241242for (int i = 0; i < this->table_size(); ++i) {243HashtableEntry<T, F>* high_list = NULL;244HashtableEntry<T, F>* low_list = NULL;245HashtableEntry<T, F>* last_low_entry = NULL;246HashtableEntry<T, F>* p = bucket(i);247while (p != NULL) {248HashtableEntry<T, F>* next = p->next();249if ((void*)p->literal() >= boundary) {250p->set_next(high_list);251high_list = p;252} else {253p->set_next(low_list);254low_list = p;255if (last_low_entry == NULL) {256last_low_entry = p;257}258}259p = next;260}261if (low_list != NULL) {262*bucket_addr(i) = low_list;263last_low_entry->set_next(high_list);264} else {265*bucket_addr(i) = high_list;266}267}268}269270template <class T, MEMFLAGS F> int RehashableHashtable<T, F>::literal_size(Symbol *symbol) {271return symbol->size() * HeapWordSize;272}273274template <class T, MEMFLAGS F> int RehashableHashtable<T, F>::literal_size(oop oop) {275// NOTE: this would over-count if (pre-JDK8) java_lang_Class::has_offset_field() is true,276// and the String.value array is shared by several Strings. However, starting from JDK8,277// the String.value array is not shared anymore.278assert(oop != NULL && oop->klass() == SystemDictionary::String_klass(), "only strings are supported");279return (oop->size() + java_lang_String::value(oop)->size()) * HeapWordSize;280}281282// Dump footprint and bucket length statistics283//284// Note: if you create a new subclass of Hashtable<MyNewType, F>, you will need to285// add a new function Hashtable<T, F>::literal_size(MyNewType lit)286287template <class T, MEMFLAGS F> void RehashableHashtable<T, F>::dump_table(outputStream* st, const char *table_name) {288NumberSeq summary;289int literal_bytes = 0;290for (int i = 0; i < this->table_size(); ++i) {291int count = 0;292for (HashtableEntry<T, F>* e = this->bucket(i);293e != NULL; e = e->next()) {294count++;295literal_bytes += literal_size(e->literal());296}297summary.add((double)count);298}299double num_buckets = summary.num();300double num_entries = summary.sum();301302int bucket_bytes = (int)num_buckets * sizeof(HashtableBucket<F>);303int entry_bytes = (int)num_entries * sizeof(HashtableEntry<T, F>);304int total_bytes = literal_bytes + bucket_bytes + entry_bytes;305306double bucket_avg = (num_buckets <= 0) ? 0 : (bucket_bytes / num_buckets);307double entry_avg = (num_entries <= 0) ? 0 : (entry_bytes / num_entries);308double literal_avg = (num_entries <= 0) ? 0 : (literal_bytes / num_entries);309310st->print_cr("%s statistics:", table_name);311st->print_cr("Number of buckets : %9d = %9d bytes, avg %7.3f", (int)num_buckets, bucket_bytes, bucket_avg);312st->print_cr("Number of entries : %9d = %9d bytes, avg %7.3f", (int)num_entries, entry_bytes, entry_avg);313st->print_cr("Number of literals : %9d = %9d bytes, avg %7.3f", (int)num_entries, literal_bytes, literal_avg);314st->print_cr("Total footprint : %9s = %9d bytes", "", total_bytes);315st->print_cr("Average bucket size : %9.3f", summary.avg());316st->print_cr("Variance of bucket size : %9.3f", summary.variance());317st->print_cr("Std. dev. of bucket size: %9.3f", summary.sd());318st->print_cr("Maximum bucket size : %9d", (int)summary.maximum());319}320321322// Dump the hash table buckets.323324template <MEMFLAGS F> void BasicHashtable<F>::copy_buckets(char** top, char* end) {325intptr_t len = _table_size * sizeof(HashtableBucket<F>);326*(intptr_t*)(*top) = len;327*top += sizeof(intptr_t);328329*(intptr_t*)(*top) = _number_of_entries;330*top += sizeof(intptr_t);331332if (*top + len > end) {333report_out_of_shared_space(SharedMiscData);334}335_buckets = (HashtableBucket<F>*)memcpy(*top, _buckets, len);336*top += len;337}338339340#ifndef PRODUCT341342template <class T, MEMFLAGS F> void Hashtable<T, F>::print() {343ResourceMark rm;344345for (int i = 0; i < BasicHashtable<F>::table_size(); i++) {346HashtableEntry<T, F>* entry = bucket(i);347while(entry != NULL) {348tty->print("%d : ", i);349entry->literal()->print();350tty->cr();351entry = entry->next();352}353}354}355356357template <MEMFLAGS F> void BasicHashtable<F>::verify() {358int count = 0;359for (int i = 0; i < table_size(); i++) {360for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {361++count;362}363}364assert(count == number_of_entries(), "number of hashtable entries incorrect");365}366367368#endif // PRODUCT369370371#ifdef ASSERT372373template <MEMFLAGS F> void BasicHashtable<F>::verify_lookup_length(double load) {374if ((double)_lookup_length / (double)_lookup_count > load * 2.0) {375warning("Performance bug: SystemDictionary lookup_count=%d "376"lookup_length=%d average=%lf load=%f",377_lookup_count, _lookup_length,378(double) _lookup_length / _lookup_count, load);379}380}381382#endif383// Explicitly instantiate these types384#if INCLUDE_ALL_GCS385template class Hashtable<nmethod*, mtGC>;386template class HashtableEntry<nmethod*, mtGC>;387template class BasicHashtable<mtGC>;388#endif389template class Hashtable<ConstantPool*, mtClass>;390template class RehashableHashtable<Symbol*, mtSymbol>;391template class RehashableHashtable<oopDesc*, mtSymbol>;392template class Hashtable<Symbol*, mtSymbol>;393template class Hashtable<Klass*, mtClass>;394template class Hashtable<oop, mtClass>;395#if defined(SOLARIS) || defined(CHECK_UNHANDLED_OOPS)396template class Hashtable<oop, mtSymbol>;397template class RehashableHashtable<oop, mtSymbol>;398#endif // SOLARIS || CHECK_UNHANDLED_OOPS399template class Hashtable<oopDesc*, mtSymbol>;400template class Hashtable<Symbol*, mtClass>;401template class HashtableEntry<Symbol*, mtSymbol>;402template class HashtableEntry<Symbol*, mtClass>;403template class HashtableEntry<oop, mtSymbol>;404template class BasicHashtableEntry<mtSymbol>;405template class BasicHashtableEntry<mtCode>;406template class BasicHashtable<mtClass>;407template class BasicHashtable<mtSymbol>;408template class BasicHashtable<mtCode>;409template class BasicHashtable<mtInternal>;410411412