src/share/vm/utilities/hashtable.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7208
7baf47cb97cb
parent 6876
710a3c8b516e
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/altHashing.hpp"
aoqi@0 27 #include "classfile/javaClasses.hpp"
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "memory/filemap.hpp"
aoqi@0 30 #include "memory/resourceArea.hpp"
aoqi@0 31 #include "oops/oop.inline.hpp"
aoqi@0 32 #include "runtime/safepoint.hpp"
aoqi@0 33 #include "utilities/dtrace.hpp"
aoqi@0 34 #include "utilities/hashtable.hpp"
aoqi@0 35 #include "utilities/hashtable.inline.hpp"
aoqi@0 36 #include "utilities/numberSeq.hpp"
aoqi@0 37
aoqi@0 38
mgerdin@7207 39 // This hashtable is implemented as an open hash table with a fixed number of buckets.
aoqi@0 40
mgerdin@7207 41 template <MEMFLAGS F> BasicHashtableEntry<F>* BasicHashtable<F>::new_entry_free_list() {
mgerdin@7207 42 BasicHashtableEntry<F>* entry = NULL;
mgerdin@7207 43 if (_free_list != NULL) {
aoqi@0 44 entry = _free_list;
aoqi@0 45 _free_list = _free_list->next();
mgerdin@7207 46 }
mgerdin@7207 47 return entry;
mgerdin@7207 48 }
mgerdin@7207 49
mgerdin@7207 50 // HashtableEntrys are allocated in blocks to reduce the space overhead.
mgerdin@7207 51 template <MEMFLAGS F> BasicHashtableEntry<F>* BasicHashtable<F>::new_entry(unsigned int hashValue) {
mgerdin@7207 52 BasicHashtableEntry<F>* entry = new_entry_free_list();
mgerdin@7207 53
mgerdin@7207 54 if (entry == NULL) {
aoqi@0 55 if (_first_free_entry + _entry_size >= _end_block) {
aoqi@0 56 int block_size = MIN2(512, MAX2((int)_table_size / 2, (int)_number_of_entries));
aoqi@0 57 int len = _entry_size * block_size;
aoqi@0 58 len = 1 << log2_intptr(len); // round down to power of 2
aoqi@0 59 assert(len >= _entry_size, "");
aoqi@0 60 _first_free_entry = NEW_C_HEAP_ARRAY2(char, len, F, CURRENT_PC);
aoqi@0 61 _end_block = _first_free_entry + len;
aoqi@0 62 }
aoqi@0 63 entry = (BasicHashtableEntry<F>*)_first_free_entry;
aoqi@0 64 _first_free_entry += _entry_size;
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 assert(_entry_size % HeapWordSize == 0, "");
aoqi@0 68 entry->set_hash(hashValue);
aoqi@0 69 return entry;
aoqi@0 70 }
aoqi@0 71
aoqi@0 72
aoqi@0 73 template <class T, MEMFLAGS F> HashtableEntry<T, F>* Hashtable<T, F>::new_entry(unsigned int hashValue, T obj) {
aoqi@0 74 HashtableEntry<T, F>* entry;
aoqi@0 75
aoqi@0 76 entry = (HashtableEntry<T, F>*)BasicHashtable<F>::new_entry(hashValue);
aoqi@0 77 entry->set_literal(obj);
aoqi@0 78 return entry;
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 // Check to see if the hashtable is unbalanced. The caller set a flag to
aoqi@0 82 // rehash at the next safepoint. If this bucket is 60 times greater than the
aoqi@0 83 // expected average bucket length, it's an unbalanced hashtable.
aoqi@0 84 // This is somewhat an arbitrary heuristic but if one bucket gets to
aoqi@0 85 // rehash_count which is currently 100, there's probably something wrong.
aoqi@0 86
mgerdin@7207 87 template <class T, MEMFLAGS F> bool RehashableHashtable<T, F>::check_rehash_table(int count) {
mgerdin@7207 88 assert(this->table_size() != 0, "underflow");
mgerdin@7207 89 if (count > (((double)this->number_of_entries()/(double)this->table_size())*rehash_multiple)) {
aoqi@0 90 // Set a flag for the next safepoint, which should be at some guaranteed
aoqi@0 91 // safepoint interval.
aoqi@0 92 return true;
aoqi@0 93 }
aoqi@0 94 return false;
aoqi@0 95 }
aoqi@0 96
mgerdin@7207 97 template <class T, MEMFLAGS F> juint RehashableHashtable<T, F>::_seed = 0;
aoqi@0 98
aoqi@0 99 // Create a new table and using alternate hash code, populate the new table
aoqi@0 100 // with the existing elements. This can be used to change the hash code
aoqi@0 101 // and could in the future change the size of the table.
aoqi@0 102
mgerdin@7207 103 template <class T, MEMFLAGS F> void RehashableHashtable<T, F>::move_to(RehashableHashtable<T, F>* new_table) {
aoqi@0 104
aoqi@0 105 // Initialize the global seed for hashing.
aoqi@0 106 _seed = AltHashing::compute_seed();
aoqi@0 107 assert(seed() != 0, "shouldn't be zero");
aoqi@0 108
aoqi@0 109 int saved_entry_count = this->number_of_entries();
aoqi@0 110
aoqi@0 111 // Iterate through the table and create a new entry for the new table
aoqi@0 112 for (int i = 0; i < new_table->table_size(); ++i) {
mgerdin@7207 113 for (HashtableEntry<T, F>* p = this->bucket(i); p != NULL; ) {
aoqi@0 114 HashtableEntry<T, F>* next = p->next();
aoqi@0 115 T string = p->literal();
aoqi@0 116 // Use alternate hashing algorithm on the symbol in the first table
aoqi@0 117 unsigned int hashValue = string->new_hash(seed());
aoqi@0 118 // Get a new index relative to the new table (can also change size)
aoqi@0 119 int index = new_table->hash_to_index(hashValue);
aoqi@0 120 p->set_hash(hashValue);
aoqi@0 121 // Keep the shared bit in the Hashtable entry to indicate that this entry
aoqi@0 122 // can't be deleted. The shared bit is the LSB in the _next field so
aoqi@0 123 // walking the hashtable past these entries requires
aoqi@0 124 // BasicHashtableEntry::make_ptr() call.
aoqi@0 125 bool keep_shared = p->is_shared();
aoqi@0 126 this->unlink_entry(p);
aoqi@0 127 new_table->add_entry(index, p);
aoqi@0 128 if (keep_shared) {
aoqi@0 129 p->set_shared();
aoqi@0 130 }
aoqi@0 131 p = next;
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134 // give the new table the free list as well
aoqi@0 135 new_table->copy_freelist(this);
aoqi@0 136 assert(new_table->number_of_entries() == saved_entry_count, "lost entry on dictionary copy?");
aoqi@0 137
aoqi@0 138 // Destroy memory used by the buckets in the hashtable. The memory
aoqi@0 139 // for the elements has been used in a new table and is not
aoqi@0 140 // destroyed. The memory reuse will benefit resizing the SystemDictionary
aoqi@0 141 // to avoid a memory allocation spike at safepoint.
aoqi@0 142 BasicHashtable<F>::free_buckets();
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 template <MEMFLAGS F> void BasicHashtable<F>::free_buckets() {
aoqi@0 146 if (NULL != _buckets) {
aoqi@0 147 // Don't delete the buckets in the shared space. They aren't
aoqi@0 148 // allocated by os::malloc
aoqi@0 149 if (!UseSharedSpaces ||
aoqi@0 150 !FileMapInfo::current_info()->is_in_shared_space(_buckets)) {
aoqi@0 151 FREE_C_HEAP_ARRAY(HashtableBucket, _buckets, F);
aoqi@0 152 }
aoqi@0 153 _buckets = NULL;
aoqi@0 154 }
aoqi@0 155 }
aoqi@0 156
aoqi@0 157
aoqi@0 158 // Reverse the order of elements in the hash buckets.
aoqi@0 159
aoqi@0 160 template <MEMFLAGS F> void BasicHashtable<F>::reverse() {
aoqi@0 161
aoqi@0 162 for (int i = 0; i < _table_size; ++i) {
aoqi@0 163 BasicHashtableEntry<F>* new_list = NULL;
aoqi@0 164 BasicHashtableEntry<F>* p = bucket(i);
aoqi@0 165 while (p != NULL) {
aoqi@0 166 BasicHashtableEntry<F>* next = p->next();
aoqi@0 167 p->set_next(new_list);
aoqi@0 168 new_list = p;
aoqi@0 169 p = next;
aoqi@0 170 }
aoqi@0 171 *bucket_addr(i) = new_list;
aoqi@0 172 }
aoqi@0 173 }
aoqi@0 174
aoqi@0 175
aoqi@0 176 // Copy the table to the shared space.
aoqi@0 177
aoqi@0 178 template <MEMFLAGS F> void BasicHashtable<F>::copy_table(char** top, char* end) {
aoqi@0 179
aoqi@0 180 // Dump the hash table entries.
aoqi@0 181
aoqi@0 182 intptr_t *plen = (intptr_t*)(*top);
aoqi@0 183 *top += sizeof(*plen);
aoqi@0 184
aoqi@0 185 int i;
aoqi@0 186 for (i = 0; i < _table_size; ++i) {
aoqi@0 187 for (BasicHashtableEntry<F>** p = _buckets[i].entry_addr();
aoqi@0 188 *p != NULL;
aoqi@0 189 p = (*p)->next_addr()) {
aoqi@0 190 if (*top + entry_size() > end) {
aoqi@0 191 report_out_of_shared_space(SharedMiscData);
aoqi@0 192 }
aoqi@0 193 *p = (BasicHashtableEntry<F>*)memcpy(*top, *p, entry_size());
aoqi@0 194 *top += entry_size();
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197 *plen = (char*)(*top) - (char*)plen - sizeof(*plen);
aoqi@0 198
aoqi@0 199 // Set the shared bit.
aoqi@0 200
aoqi@0 201 for (i = 0; i < _table_size; ++i) {
aoqi@0 202 for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {
aoqi@0 203 p->set_shared();
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206 }
aoqi@0 207
aoqi@0 208
aoqi@0 209
aoqi@0 210 // Reverse the order of elements in the hash buckets.
aoqi@0 211
aoqi@0 212 template <class T, MEMFLAGS F> void Hashtable<T, F>::reverse(void* boundary) {
aoqi@0 213
aoqi@0 214 for (int i = 0; i < this->table_size(); ++i) {
aoqi@0 215 HashtableEntry<T, F>* high_list = NULL;
aoqi@0 216 HashtableEntry<T, F>* low_list = NULL;
aoqi@0 217 HashtableEntry<T, F>* last_low_entry = NULL;
aoqi@0 218 HashtableEntry<T, F>* p = bucket(i);
aoqi@0 219 while (p != NULL) {
aoqi@0 220 HashtableEntry<T, F>* next = p->next();
aoqi@0 221 if ((void*)p->literal() >= boundary) {
aoqi@0 222 p->set_next(high_list);
aoqi@0 223 high_list = p;
aoqi@0 224 } else {
aoqi@0 225 p->set_next(low_list);
aoqi@0 226 low_list = p;
aoqi@0 227 if (last_low_entry == NULL) {
aoqi@0 228 last_low_entry = p;
aoqi@0 229 }
aoqi@0 230 }
aoqi@0 231 p = next;
aoqi@0 232 }
aoqi@0 233 if (low_list != NULL) {
aoqi@0 234 *bucket_addr(i) = low_list;
aoqi@0 235 last_low_entry->set_next(high_list);
aoqi@0 236 } else {
aoqi@0 237 *bucket_addr(i) = high_list;
aoqi@0 238 }
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241
mgerdin@7207 242 template <class T, MEMFLAGS F> int RehashableHashtable<T, F>::literal_size(Symbol *symbol) {
aoqi@0 243 return symbol->size() * HeapWordSize;
aoqi@0 244 }
aoqi@0 245
mgerdin@7207 246 template <class T, MEMFLAGS F> int RehashableHashtable<T, F>::literal_size(oop oop) {
aoqi@0 247 // NOTE: this would over-count if (pre-JDK8) java_lang_Class::has_offset_field() is true,
aoqi@0 248 // and the String.value array is shared by several Strings. However, starting from JDK8,
aoqi@0 249 // the String.value array is not shared anymore.
aoqi@0 250 assert(oop != NULL && oop->klass() == SystemDictionary::String_klass(), "only strings are supported");
aoqi@0 251 return (oop->size() + java_lang_String::value(oop)->size()) * HeapWordSize;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 // Dump footprint and bucket length statistics
aoqi@0 255 //
aoqi@0 256 // Note: if you create a new subclass of Hashtable<MyNewType, F>, you will need to
aoqi@0 257 // add a new function Hashtable<T, F>::literal_size(MyNewType lit)
aoqi@0 258
mgerdin@7207 259 template <class T, MEMFLAGS F> void RehashableHashtable<T, F>::dump_table(outputStream* st, const char *table_name) {
aoqi@0 260 NumberSeq summary;
aoqi@0 261 int literal_bytes = 0;
aoqi@0 262 for (int i = 0; i < this->table_size(); ++i) {
aoqi@0 263 int count = 0;
mgerdin@7207 264 for (HashtableEntry<T, F>* e = this->bucket(i);
aoqi@0 265 e != NULL; e = e->next()) {
aoqi@0 266 count++;
aoqi@0 267 literal_bytes += literal_size(e->literal());
aoqi@0 268 }
aoqi@0 269 summary.add((double)count);
aoqi@0 270 }
aoqi@0 271 double num_buckets = summary.num();
aoqi@0 272 double num_entries = summary.sum();
aoqi@0 273
mgerdin@7207 274 int bucket_bytes = (int)num_buckets * sizeof(HashtableBucket<F>);
aoqi@0 275 int entry_bytes = (int)num_entries * sizeof(HashtableEntry<T, F>);
aoqi@0 276 int total_bytes = literal_bytes + bucket_bytes + entry_bytes;
aoqi@0 277
aoqi@0 278 double bucket_avg = (num_buckets <= 0) ? 0 : (bucket_bytes / num_buckets);
aoqi@0 279 double entry_avg = (num_entries <= 0) ? 0 : (entry_bytes / num_entries);
aoqi@0 280 double literal_avg = (num_entries <= 0) ? 0 : (literal_bytes / num_entries);
aoqi@0 281
aoqi@0 282 st->print_cr("%s statistics:", table_name);
aoqi@0 283 st->print_cr("Number of buckets : %9d = %9d bytes, avg %7.3f", (int)num_buckets, bucket_bytes, bucket_avg);
aoqi@0 284 st->print_cr("Number of entries : %9d = %9d bytes, avg %7.3f", (int)num_entries, entry_bytes, entry_avg);
aoqi@0 285 st->print_cr("Number of literals : %9d = %9d bytes, avg %7.3f", (int)num_entries, literal_bytes, literal_avg);
aoqi@0 286 st->print_cr("Total footprint : %9s = %9d bytes", "", total_bytes);
aoqi@0 287 st->print_cr("Average bucket size : %9.3f", summary.avg());
aoqi@0 288 st->print_cr("Variance of bucket size : %9.3f", summary.variance());
aoqi@0 289 st->print_cr("Std. dev. of bucket size: %9.3f", summary.sd());
aoqi@0 290 st->print_cr("Maximum bucket size : %9d", (int)summary.maximum());
aoqi@0 291 }
aoqi@0 292
aoqi@0 293
aoqi@0 294 // Dump the hash table buckets.
aoqi@0 295
aoqi@0 296 template <MEMFLAGS F> void BasicHashtable<F>::copy_buckets(char** top, char* end) {
aoqi@0 297 intptr_t len = _table_size * sizeof(HashtableBucket<F>);
aoqi@0 298 *(intptr_t*)(*top) = len;
aoqi@0 299 *top += sizeof(intptr_t);
aoqi@0 300
aoqi@0 301 *(intptr_t*)(*top) = _number_of_entries;
aoqi@0 302 *top += sizeof(intptr_t);
aoqi@0 303
aoqi@0 304 if (*top + len > end) {
aoqi@0 305 report_out_of_shared_space(SharedMiscData);
aoqi@0 306 }
aoqi@0 307 _buckets = (HashtableBucket<F>*)memcpy(*top, _buckets, len);
aoqi@0 308 *top += len;
aoqi@0 309 }
aoqi@0 310
aoqi@0 311
aoqi@0 312 #ifndef PRODUCT
aoqi@0 313
aoqi@0 314 template <class T, MEMFLAGS F> void Hashtable<T, F>::print() {
aoqi@0 315 ResourceMark rm;
aoqi@0 316
aoqi@0 317 for (int i = 0; i < BasicHashtable<F>::table_size(); i++) {
aoqi@0 318 HashtableEntry<T, F>* entry = bucket(i);
aoqi@0 319 while(entry != NULL) {
aoqi@0 320 tty->print("%d : ", i);
aoqi@0 321 entry->literal()->print();
aoqi@0 322 tty->cr();
aoqi@0 323 entry = entry->next();
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326 }
aoqi@0 327
aoqi@0 328
aoqi@0 329 template <MEMFLAGS F> void BasicHashtable<F>::verify() {
aoqi@0 330 int count = 0;
aoqi@0 331 for (int i = 0; i < table_size(); i++) {
aoqi@0 332 for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {
aoqi@0 333 ++count;
aoqi@0 334 }
aoqi@0 335 }
aoqi@0 336 assert(count == number_of_entries(), "number of hashtable entries incorrect");
aoqi@0 337 }
aoqi@0 338
aoqi@0 339
aoqi@0 340 #endif // PRODUCT
aoqi@0 341
aoqi@0 342
aoqi@0 343 #ifdef ASSERT
aoqi@0 344
aoqi@0 345 template <MEMFLAGS F> void BasicHashtable<F>::verify_lookup_length(double load) {
aoqi@0 346 if ((double)_lookup_length / (double)_lookup_count > load * 2.0) {
aoqi@0 347 warning("Performance bug: SystemDictionary lookup_count=%d "
aoqi@0 348 "lookup_length=%d average=%lf load=%f",
aoqi@0 349 _lookup_count, _lookup_length,
aoqi@0 350 (double) _lookup_length / _lookup_count, load);
aoqi@0 351 }
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 #endif
aoqi@0 355 // Explicitly instantiate these types
mgerdin@7208 356 #if INCLUDE_ALL_GCS
mgerdin@7208 357 template class Hashtable<nmethod*, mtGC>;
mgerdin@7208 358 template class HashtableEntry<nmethod*, mtGC>;
mgerdin@7208 359 template class BasicHashtable<mtGC>;
mgerdin@7208 360 #endif
aoqi@0 361 template class Hashtable<ConstantPool*, mtClass>;
mgerdin@7207 362 template class RehashableHashtable<Symbol*, mtSymbol>;
mgerdin@7207 363 template class RehashableHashtable<oopDesc*, mtSymbol>;
aoqi@0 364 template class Hashtable<Symbol*, mtSymbol>;
aoqi@0 365 template class Hashtable<Klass*, mtClass>;
aoqi@0 366 template class Hashtable<oop, mtClass>;
aoqi@0 367 #if defined(SOLARIS) || defined(CHECK_UNHANDLED_OOPS)
aoqi@0 368 template class Hashtable<oop, mtSymbol>;
mgerdin@7207 369 template class RehashableHashtable<oop, mtSymbol>;
aoqi@0 370 #endif // SOLARIS || CHECK_UNHANDLED_OOPS
aoqi@0 371 template class Hashtable<oopDesc*, mtSymbol>;
aoqi@0 372 template class Hashtable<Symbol*, mtClass>;
aoqi@0 373 template class HashtableEntry<Symbol*, mtSymbol>;
aoqi@0 374 template class HashtableEntry<Symbol*, mtClass>;
aoqi@0 375 template class HashtableEntry<oop, mtSymbol>;
aoqi@0 376 template class BasicHashtableEntry<mtSymbol>;
aoqi@0 377 template class BasicHashtableEntry<mtCode>;
aoqi@0 378 template class BasicHashtable<mtClass>;
aoqi@0 379 template class BasicHashtable<mtSymbol>;
aoqi@0 380 template class BasicHashtable<mtCode>;
aoqi@0 381 template class BasicHashtable<mtInternal>;

mercurial