hashtable.cpp 10.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 *
19 20 21
 * 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.
D
duke 已提交
22 23 24
 *
 */

25
#include "precompiled.hpp"
26 27
#include "classfile/altHashing.hpp"
#include "classfile/javaClasses.hpp"
28
#include "memory/allocation.inline.hpp"
29
#include "memory/filemap.hpp"
30 31 32 33 34 35
#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"
D
duke 已提交
36

37

D
duke 已提交
38 39 40 41 42 43 44 45
// This is a generic hashtable, designed to be used for the symbol
// and string tables.
//
// It is implemented as an open hash table with a fixed number of buckets.
//
// %note:
//  - HashtableEntrys are allocated in blocks to reduce the space overhead.

Z
zgu 已提交
46 47
template <MEMFLAGS F> BasicHashtableEntry<F>* BasicHashtable<F>::new_entry(unsigned int hashValue) {
  BasicHashtableEntry<F>* entry;
D
duke 已提交
48 49 50 51 52

  if (_free_list) {
    entry = _free_list;
    _free_list = _free_list->next();
  } else {
J
jrose 已提交
53 54
    if (_first_free_entry + _entry_size >= _end_block) {
      int block_size = MIN2(512, MAX2((int)_table_size / 2, (int)_number_of_entries));
D
duke 已提交
55
      int len = _entry_size * block_size;
J
jrose 已提交
56 57
      len = 1 << log2_intptr(len); // round down to power of 2
      assert(len >= _entry_size, "");
Z
zgu 已提交
58
      _first_free_entry = NEW_C_HEAP_ARRAY2(char, len, F, CURRENT_PC);
D
duke 已提交
59 60
      _end_block = _first_free_entry + len;
    }
Z
zgu 已提交
61
    entry = (BasicHashtableEntry<F>*)_first_free_entry;
D
duke 已提交
62 63 64
    _first_free_entry += _entry_size;
  }

J
jrose 已提交
65
  assert(_entry_size % HeapWordSize == 0, "");
D
duke 已提交
66 67 68 69 70
  entry->set_hash(hashValue);
  return entry;
}


Z
zgu 已提交
71 72
template <class T, MEMFLAGS F> HashtableEntry<T, F>* Hashtable<T, F>::new_entry(unsigned int hashValue, T obj) {
  HashtableEntry<T, F>* entry;
D
duke 已提交
73

Z
zgu 已提交
74
  entry = (HashtableEntry<T, F>*)BasicHashtable<F>::new_entry(hashValue);
75
  entry->set_literal(obj);
D
duke 已提交
76 77 78
  return entry;
}

79 80 81 82 83 84
// 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.

Z
zgu 已提交
85
template <MEMFLAGS F> bool BasicHashtable<F>::check_rehash_table(int count) {
86 87 88 89 90 91 92 93 94
  assert(table_size() != 0, "underflow");
  if (count > (((double)number_of_entries()/(double)table_size())*rehash_multiple)) {
    // Set a flag for the next safepoint, which should be at some guaranteed
    // safepoint interval.
    return true;
  }
  return false;
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
template <class T, MEMFLAGS F> jint Hashtable<T, F>::_seed = 0;

template <class T, MEMFLAGS F> unsigned int Hashtable<T, F>::new_hash(Symbol* sym) {
  ResourceMark rm;
  // Use alternate hashing algorithm on this symbol.
  return AltHashing::murmur3_32(seed(), (const jbyte*)sym->as_C_string(), sym->utf8_length());
}

template <class T, MEMFLAGS F> unsigned int Hashtable<T, F>::new_hash(oop string) {
  ResourceMark rm;
  int length;
  jchar* chars = java_lang_String::as_unicode_string(string, length);
  // Use alternate hashing algorithm on the string
  return AltHashing::murmur3_32(seed(), chars, length);
}

111 112 113 114
// 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.

Z
zgu 已提交
115
template <class T, MEMFLAGS F> void Hashtable<T, F>::move_to(Hashtable<T, F>* new_table) {
116 117 118 119 120 121

  // 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();
122 123 124

  // Iterate through the table and create a new entry for the new table
  for (int i = 0; i < new_table->table_size(); ++i) {
Z
zgu 已提交
125 126
    for (HashtableEntry<T, F>* p = bucket(i); p != NULL; ) {
      HashtableEntry<T, F>* next = p->next();
127 128 129 130 131 132
      T string = p->literal();
      // Use alternate hashing algorithm on the symbol in the first table
      unsigned int hashValue = new_hash(string);
      // 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);
133 134 135 136 137
      // 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();
138 139
      unlink_entry(p);
      new_table->add_entry(index, p);
140 141 142
      if (keep_shared) {
        p->set_shared();
      }
143 144 145 146 147 148 149 150 151 152 153
      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.
Z
zgu 已提交
154
  BasicHashtable<F>::free_buckets();
155 156
}

Z
zgu 已提交
157
template <MEMFLAGS F> void BasicHashtable<F>::free_buckets() {
158 159 160 161 162
  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)) {
Z
zgu 已提交
163
       FREE_C_HEAP_ARRAY(HashtableBucket, _buckets, F);
164 165 166 167 168 169
    }
    _buckets = NULL;
  }
}


D
duke 已提交
170 171
// Reverse the order of elements in the hash buckets.

Z
zgu 已提交
172
template <MEMFLAGS F> void BasicHashtable<F>::reverse() {
D
duke 已提交
173 174

  for (int i = 0; i < _table_size; ++i) {
Z
zgu 已提交
175 176
    BasicHashtableEntry<F>* new_list = NULL;
    BasicHashtableEntry<F>* p = bucket(i);
D
duke 已提交
177
    while (p != NULL) {
Z
zgu 已提交
178
      BasicHashtableEntry<F>* next = p->next();
D
duke 已提交
179 180 181 182 183 184 185 186 187 188 189
      p->set_next(new_list);
      new_list = p;
      p = next;
    }
    *bucket_addr(i) = new_list;
  }
}


// Copy the table to the shared space.

Z
zgu 已提交
190
template <MEMFLAGS F> void BasicHashtable<F>::copy_table(char** top, char* end) {
D
duke 已提交
191 192 193 194 195 196 197 198

  // Dump the hash table entries.

  intptr_t *plen = (intptr_t*)(*top);
  *top += sizeof(*plen);

  int i;
  for (i = 0; i < _table_size; ++i) {
Z
zgu 已提交
199
    for (BasicHashtableEntry<F>** p = _buckets[i].entry_addr();
D
duke 已提交
200 201 202
                              *p != NULL;
                               p = (*p)->next_addr()) {
      if (*top + entry_size() > end) {
203
        report_out_of_shared_space(SharedMiscData);
D
duke 已提交
204
      }
Z
zgu 已提交
205
      *p = (BasicHashtableEntry<F>*)memcpy(*top, *p, entry_size());
D
duke 已提交
206 207 208 209 210 211 212 213
      *top += entry_size();
    }
  }
  *plen = (char*)(*top) - (char*)plen - sizeof(*plen);

  // Set the shared bit.

  for (i = 0; i < _table_size; ++i) {
Z
zgu 已提交
214
    for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {
D
duke 已提交
215 216 217 218 219 220 221 222 223
      p->set_shared();
    }
  }
}



// Reverse the order of elements in the hash buckets.

Z
zgu 已提交
224
template <class T, MEMFLAGS F> void Hashtable<T, F>::reverse(void* boundary) {
D
duke 已提交
225

Z
zgu 已提交
226 227 228 229 230
  for (int i = 0; i < this->table_size(); ++i) {
    HashtableEntry<T, F>* high_list = NULL;
    HashtableEntry<T, F>* low_list = NULL;
    HashtableEntry<T, F>* last_low_entry = NULL;
    HashtableEntry<T, F>* p = bucket(i);
D
duke 已提交
231
    while (p != NULL) {
Z
zgu 已提交
232
      HashtableEntry<T, F>* next = p->next();
D
duke 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
      if ((void*)p->literal() >= boundary) {
        p->set_next(high_list);
        high_list = p;
      } else {
        p->set_next(low_list);
        low_list = p;
        if (last_low_entry == NULL) {
          last_low_entry = p;
        }
      }
      p = next;
    }
    if (low_list != NULL) {
      *bucket_addr(i) = low_list;
      last_low_entry->set_next(high_list);
    } else {
      *bucket_addr(i) = high_list;
    }
  }
}


// Dump the hash table buckets.

Z
zgu 已提交
257 258
template <MEMFLAGS F> void BasicHashtable<F>::copy_buckets(char** top, char* end) {
  intptr_t len = _table_size * sizeof(HashtableBucket<F>);
D
duke 已提交
259 260 261 262 263 264 265
  *(intptr_t*)(*top) = len;
  *top += sizeof(intptr_t);

  *(intptr_t*)(*top) = _number_of_entries;
  *top += sizeof(intptr_t);

  if (*top + len > end) {
266
    report_out_of_shared_space(SharedMiscData);
D
duke 已提交
267
  }
Z
zgu 已提交
268
  _buckets = (HashtableBucket<F>*)memcpy(*top, _buckets, len);
D
duke 已提交
269 270 271 272 273 274
  *top += len;
}


#ifndef PRODUCT

Z
zgu 已提交
275
template <class T, MEMFLAGS F> void Hashtable<T, F>::print() {
D
duke 已提交
276 277
  ResourceMark rm;

Z
zgu 已提交
278 279
  for (int i = 0; i < BasicHashtable<F>::table_size(); i++) {
    HashtableEntry<T, F>* entry = bucket(i);
D
duke 已提交
280 281 282 283 284 285 286 287 288 289
    while(entry != NULL) {
      tty->print("%d : ", i);
      entry->literal()->print();
      tty->cr();
      entry = entry->next();
    }
  }
}


Z
zgu 已提交
290
template <MEMFLAGS F> void BasicHashtable<F>::verify() {
D
duke 已提交
291 292
  int count = 0;
  for (int i = 0; i < table_size(); i++) {
Z
zgu 已提交
293
    for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {
D
duke 已提交
294 295 296 297 298 299 300 301 302 303 304 305
      ++count;
    }
  }
  assert(count == number_of_entries(), "number of hashtable entries incorrect");
}


#endif // PRODUCT


#ifdef ASSERT

Z
zgu 已提交
306
template <MEMFLAGS F> void BasicHashtable<F>::verify_lookup_length(double load) {
D
duke 已提交
307 308 309 310 311 312 313 314 315
  if ((double)_lookup_length / (double)_lookup_count > load * 2.0) {
    warning("Performance bug: SystemDictionary lookup_count=%d "
            "lookup_length=%d average=%lf load=%f",
            _lookup_count, _lookup_length,
            (double) _lookup_length / _lookup_count, load);
  }
}

#endif
316
// Explicitly instantiate these types
Z
zgu 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
template class Hashtable<constantPoolOop, mtClass>;
template class Hashtable<Symbol*, mtSymbol>;
template class Hashtable<klassOop, mtClass>;
template class Hashtable<oop, mtClass>;
#ifdef SOLARIS
template class Hashtable<oop, mtSymbol>;
#endif
template class Hashtable<oopDesc*, mtSymbol>;
template class Hashtable<Symbol*, mtClass>;
template class HashtableEntry<Symbol*, mtSymbol>;
template class HashtableEntry<Symbol*, mtClass>;
template class HashtableEntry<oop, mtSymbol>;
template class BasicHashtableEntry<mtSymbol>;
template class BasicHashtableEntry<mtCode>;
template class BasicHashtable<mtClass>;
template class BasicHashtable<mtSymbol>;
template class BasicHashtable<mtCode>;
template class BasicHashtable<mtInternal>;