src/share/vm/utilities/hashtable.cpp

Tue, 08 Aug 2017 15:57:29 +0800

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

mercurial