       1 /*
       2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
       3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       4  *
       5  * This code is free software; you can redistribute it and/or modify it
       6  * under the terms of the GNU General Public License version 2 only, as
       7  * published by the Free Software Foundation.
       8  *
       9  * This code is distributed in the hope that it will be useful, but WITHOUT
      10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      12  * version 2 for more details (a copy is included in the LICENSE file that
      13  * accompanied this code).
      14  *
      15  * You should have received a copy of the GNU General Public License version
      16  * 2 along with this work; if not, write to the Free Software Foundation,
      17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      18  *
      19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      20  * or visit www.oracle.com if you need additional information or have any
      21  * questions.
      22  *
      23  */
      24
      25 #include "precompiled.hpp"
      26 #include "classfile/altHashing.hpp"
      27 #include "classfile/javaClasses.hpp"
      28 #include "memory/allocation.inline.hpp"
      29 #include "memory/filemap.hpp"
      30 #include "memory/resourceArea.hpp"
      31 #include "oops/oop.inline.hpp"
      32 #include "runtime/safepoint.hpp"
      33 #include "utilities/dtrace.hpp"
      34 #include "utilities/hashtable.hpp"
      35 #include "utilities/hashtable.inline.hpp"
      36 #include "utilities/numberSeq.hpp"
      37
      38 // ------------------------------------------------------------------------------------------
      39 //
      40 // start of next_prime
      41 //
      42 // http://stackoverflow.com/questions/4475996/given-prime-number-n-compute-the-next-prime
      43 //
      44 bool is_prime(int x)
      45 {
      46     int o = 4;
      47     for (int i = 5; true; i += o)
      48       {
      49           int q = x / i;
      50           if (q < i)
      51               return true;
      52           if (x == q * i)
      53               return false;
      54           o ^= 6;
      55         }
      56     return true;
      57   }
      58 int next_prime(int x)
      59 {
      60     switch (x)
      61     {
      62           case 0:
      63           case 1:
      64           case 2:
      65             return 2;
      66           case 3:
      67             return 3;
      68           case 4:
      69           case 5:
      70             return 5;
      71       }
      72     int k = x / 6;
      73     int i = x - 6 * k;
      74     int o = i < 2 ? 1 : 5;
      75     x = 6 * k + o;
      76     for (i = (3 + o) / 2; !is_prime(x); x += i)
      77         i ^= 6;
      78       return x;
      79   }
      80 // end of next_prime
      81 // ------------------------------------------------------------------------------------------
      82
      83 // This hashtable is implemented as an open hash table with a fixed number of buckets.
      84
      85 template <MEMFLAGS F> BasicHashtableEntry<F>* BasicHashtable<F>::new_entry_free_list() {
      86   BasicHashtableEntry<F>* entry = NULL;
      87   if (_free_list != NULL) {
      88     entry = _free_list;
      89     _free_list = _free_list->next();
      90   }
      91   return entry;
      92 }
      93
      94 // HashtableEntrys are allocated in blocks to reduce the space overhead.
      95 template <MEMFLAGS F> BasicHashtableEntry<F>* BasicHashtable<F>::new_entry(unsigned int hashValue) {
      96   BasicHashtableEntry<F>* entry = new_entry_free_list();
      97
      98   if (entry == NULL) {
      99     if (_first_free_entry + _entry_size >= _end_block) {
     100       int block_size = MIN2(512, MAX2((int)_table_size / 2, (int)_number_of_entries));
     101       int len = _entry_size * block_size;
     102       len = 1 << log2_intptr(len); // round down to power of 2
     103       assert(len >= _entry_size, "");
     104       _first_free_entry = NEW_C_HEAP_ARRAY2(char, len, F, CURRENT_PC);
     105       _end_block = _first_free_entry + len;
     106     }
     107     entry = (BasicHashtableEntry<F>*)_first_free_entry;
     108     _first_free_entry += _entry_size;
     109   }
     110
     111   assert(_entry_size % HeapWordSize == 0, "");
     112   entry->set_hash(hashValue);
     113   return entry;
     114 }
     115
     116
     117 template <class T, MEMFLAGS F> HashtableEntry<T, F>* Hashtable<T, F>::new_entry(unsigned int hashValue, T obj) {
     118   HashtableEntry<T, F>* entry;
     119
     120   entry = (HashtableEntry<T, F>*)BasicHashtable<F>::new_entry(hashValue);
     121   entry->set_literal(obj);
     122   entry->_debug_lookups = 0;
     123   return entry;
     124 }
     125
     126 // Check to see if the hashtable is unbalanced.  The caller set a flag to
     127 // rehash at the next safepoint.  If this bucket is 60 times greater than the
     128 // expected average bucket length, it's an unbalanced hashtable.
     129 // This is somewhat an arbitrary heuristic but if one bucket gets to
     130 // rehash_count which is currently 100, there's probably something wrong.
     131
     132 template <class T, MEMFLAGS F> bool RehashableHashtable<T, F>::check_rehash_table(int count) {
     133   assert(this->table_size() != 0, "underflow");
     134   if (count > (((double)this->number_of_entries()/(double)this->table_size())*rehash_multiple)) {
     135     // Set a flag for the next safepoint, which should be at some guaranteed
     136     // safepoint interval.
     137     return true;
     138   }
     139   return false;
     140 }
     141
     142 template <class T, MEMFLAGS F> juint RehashableHashtable<T, F>::_seed = 0;
     143
     144 // Create a new table and using alternate hash code, populate the new table
     145 // with the existing elements.   This can be used to change the hash code
     146 // and could in the future change the size of the table.
     147
     148 template <class T, MEMFLAGS F> void RehashableHashtable<T, F>::move_to(RehashableHashtable<T, F>* new_table) {
     149
     150   // Initialize the global seed for hashing.
     151   _seed = AltHashing::compute_seed();
     152   assert(seed() != 0, "shouldn't be zero");
     153
     154   int saved_entry_count = this->number_of_entries();
     155
     156   // Iterate through the table and create a new entry for the new table
     157   for (int i = 0; i < new_table->table_size(); ++i) {
     158     for (HashtableEntry<T, F>* p = this->bucket(i); p != NULL; ) {
     159       HashtableEntry<T, F>* next = p->next();
     160       T string = p->literal();
     161       // Use alternate hashing algorithm on the symbol in the first table
     162       unsigned int hashValue = string->new_hash(seed());
     163       // Get a new index relative to the new table (can also change size)
     164       int index = new_table->hash_to_index(hashValue);
     165       p->set_hash(hashValue);
     166       // Keep the shared bit in the Hashtable entry to indicate that this entry
     167       // can't be deleted.   The shared bit is the LSB in the _next field so
     168       // walking the hashtable past these entries requires
     169       // BasicHashtableEntry::make_ptr() call.
     170       bool keep_shared = p->is_shared();
     171       this->unlink_entry(p);
     172       new_table->add_entry(index, p);
     173       if (keep_shared) {
     174         p->set_shared();
     175       }
     176       p = next;
     177     }
     178   }
     179   // give the new table the free list as well
     180   new_table->copy_freelist(this);
     181   assert(new_table->number_of_entries() == saved_entry_count, "lost entry on dictionary copy?");
     182
     183   // Destroy memory used by the buckets in the hashtable.  The memory
     184   // for the elements has been used in a new table and is not
     185   // destroyed.  The memory reuse will benefit resizing the SystemDictionary
     186   // to avoid a memory allocation spike at safepoint.
     187   BasicHashtable<F>::free_buckets();
     188 }
     189
     190 template <MEMFLAGS F> void BasicHashtable<F>::free_buckets() {
     191   if (NULL != _buckets) {
     192     // Don't delete the buckets in the shared space.  They aren't
     193     // allocated by os::malloc
     194     if (!UseSharedSpaces ||
     195         !FileMapInfo::current_info()->is_in_shared_space(_buckets)) {
     196        FREE_C_HEAP_ARRAY(HashtableBucket, _buckets, F);
     197     }
     198     _buckets = NULL;
     199   }
     200 }
     201
     202 // Resize the internal bucket table if the current one reaches its maximum allowed chain length (LoadFactor)
     203 template <MEMFLAGS F> ResizeHashtableStats BasicHashtable<F>::resize_table(Monitor* lock, float trigger, float desired) {
     204   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
     205   assert(lock->is_locked()==false, "table lock must not be held");
     206
     207   //tty->print("  %9d, %9llu\n", _number_of_entries, _debug_all_lookup_count);
     208
     209   ResizeHashtableStats stats = {false, 0, 0};
     210
     211   static bool _resize_failed = false;
     212   if (_resize_failed == true) {
     213     return stats;
     214   }
     215
     216   //#define FORCE_RESIZE
     217 #ifdef FORCE_RESIZE
     218   int force_mod = 3;
     219   if (((_debug_resize_fire++) % force_mod) != 0) {
     220     return stats;
     221   }
     222 #else // FORCE_RESIZE
     223   // Check if there is anything to be done here
     224   float load_factor = (float)_number_of_entries/(float)_table_size;
     225   if (load_factor < trigger) {
     226     return stats;
     227   }
     228 #endif // FORCE_RESIZE
     229
     230 #ifndef PRODUCT
     231 //#ifdef PRODUCT_TEST
     232   julong time = (julong)os::javaTimeNanos();
     233 #endif // PRODUCT
     234
     235   MutexLocker mu(lock);
     236
     237   if (HashtablesVerifyBeforeResize == true) {
     238     verify();
     239   }
     240
     241   // Find the next best size for the hashtable.
     242   int table_size_old = _table_size;
     243 #ifdef FORCE_RESIZE
     244   int table_size_new = next_prime(table_size_old+1);
     245 #else
     246   int table_size_new = next_prime((int)(((float)_number_of_entries/(float)desired)+0.5f));
     247 #endif // FORCE_RESIZE
     248
     249   // Allocate new buckets
     250   HashtableBucket<F>* buckets_new = NEW_C_HEAP_ARRAY2(HashtableBucket<F>, table_size_new, F, CURRENT_PC);
     251   if (buckets_new == NULL) {
     252     _resize_failed = true;
     253     return stats;
     254   }
     255
     256   // Clear the new buckets
     257   for (int i = 0; i < table_size_new; i++) {
     258     buckets_new[i].clear();
     259   }
     260
     261   // hash_to_index() uses _table_size, so switch the sizes now
     262   _table_size = table_size_new;
     263
     264   // Do not bother sorting if new size is bigger than the number of entries
     265   bool sort = HashtablesResizeSort && (table_size_new > _number_of_entries);
     266
     267   // Move entries from the old table to a new table
     268   for (int index_old = 0; index_old < table_size_old; index_old++) {
     269     for (BasicHashtableEntry<F>* p = _buckets[index_old].get_entry(); p != NULL; ) {
     270       BasicHashtableEntry<F>* next = p->next();
     271       bool keep_shared = p->is_shared();
     272
     273       int index_new = hash_to_index(p->hash());
     274
     275       if (sort == true) {
     276         // Insert according to the lookup counter value, higher scored entries go to the front
     277         BasicHashtableEntry<F>* p_new_bigger = NULL;
     278         for (BasicHashtableEntry<F>* p_new = buckets_new[index_new].get_entry(); p_new != NULL; p_new = p_new->next()) {
     279           if (p_new != NULL) {
     280             if (p_new->_debug_lookups > p->_debug_lookups) {
     281               p_new_bigger = p_new;
     282             } else {
     283               break;
     284             }
     285           }
     286         }
     287         BasicHashtableEntry<F>* p_new_bigger_next = NULL;
     288         if (p_new_bigger == NULL) {
     289           p_new_bigger_next = buckets_new[index_new].get_entry();
     290           buckets_new[index_new].set_entry(p);
     291         } else {
     292           p_new_bigger_next = p_new_bigger->next();
     293           p_new_bigger->set_next(p);
     294         }
     295         p->set_next(p_new_bigger_next);
     296       } else {
     297 //#define ADD_TO_END
     298 #ifdef ADD_TO_END
     299         BasicHashtableEntry<F>* p_new_last = NULL;
     300         for (BasicHashtableEntry<F>* p_new = buckets_new[index_new].get_entry(); p_new != NULL; p_new = p_new->next()) {
     301           p_new_last = p_new;
     302         }
     303         if (p_new_last == NULL) {
     304           buckets_new[index_new].set_entry(p);
     305         } else {
     306           p_new_last->set_next(p);
     307         }
     308         p->set_next(NULL);
     309 #else // ADD_TO_END
     310         p->set_next(buckets_new[index_new].get_entry());
     311         buckets_new[index_new].set_entry(p);
     312 #endif // ADD_TO_END
     313       }
     314
     315       if (keep_shared) {
     316         p->set_shared();
     317       }
     318       p = next;
     319     }
     320   }
     321
     322   // The old backets now can be released
     323   BasicHashtable<F>::free_buckets();
     324
     325   // Switch to the new storage
     326   _buckets = buckets_new;
     327
     328   if (HashtablesVerifyAfterResize == true) {
     329     verify();
     330   }
     331
     332 #ifndef PRODUCT
     333 //#ifdef PRODUCT_TEST
     334   julong duration = (julong)os::javaTimeNanos() - time;
     335
     336   // Reset new table's statistics
     337   _debug_lookup_count = 0;
     338   _debug_insertion_count = 0;
     339   _debug_lookup_found_count = 0;
     340   _debug_lookup_found_max_duration = 0;
     341   _debug_lookup_found_total_duration = 0;
     342   _debug_chain_found_maximum_length = 0;
     343   _debug_chain_found_total_length = 0;
     344   _debug_lookup_failed_count = 0;
     345   _debug_lookup_failed_max_duration = 0;
     346   _debug_lookup_failed_total_duration = 0;
     347   _debug_chain_failed_maximum_length = 0;
     348   _debug_chain_failed_total_length = 0;
     349   _debug_resize_max_duration = (duration > _debug_resize_max_duration) ? duration : _debug_resize_max_duration;
     350   _debug_resize_total_duration += duration;
     351   _debug_resize_total_count++;
     352 #endif // PRODUCT
     353
     354   stats.resized = true;
     355   stats.old_size = table_size_old;
     356   stats.new_size = table_size_new;
     357   return stats;
     358 }
     359
     360 // Reverse the order of elements in the hash buckets.
     361
     362 template <MEMFLAGS F> void BasicHashtable<F>::reverse() {
     363
     364   for (int i = 0; i < _table_size; ++i) {
     365     BasicHashtableEntry<F>* new_list = NULL;
     366     BasicHashtableEntry<F>* p = bucket(i);
     367     while (p != NULL) {
     368       BasicHashtableEntry<F>* next = p->next();
     369       p->set_next(new_list);
     370       new_list = p;
     371       p = next;
     372     }
     373     *bucket_addr(i) = new_list;
     374   }
     375 }
     376
     377
     378 // Copy the table to the shared space.
     379
     380 template <MEMFLAGS F> void BasicHashtable<F>::copy_table(char** top, char* end) {
     381
     382   // Dump the hash table entries.
     383
     384   intptr_t *plen = (intptr_t*)(*top);
     385   *top += sizeof(*plen);
     386
     387   int i;
     388   for (i = 0; i < _table_size; ++i) {
     389     for (BasicHashtableEntry<F>** p = _buckets[i].entry_addr();
     390                               *p != NULL;
     391                                p = (*p)->next_addr()) {
     392       if (*top + entry_size() > end) {
     393         report_out_of_shared_space(SharedMiscData);
     394       }
     395       *p = (BasicHashtableEntry<F>*)memcpy(*top, *p, entry_size());
     396       *top += entry_size();
     397     }
     398   }
     399   *plen = (char*)(*top) - (char*)plen - sizeof(*plen);
     400
     401   // Set the shared bit.
     402
     403   for (i = 0; i < _table_size; ++i) {
     404     for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {
     405       p->set_shared();
     406     }
     407   }
     408 }
     409

     410 // Reverse the order of elements in the hash buckets.
     411
     412 template <class T, MEMFLAGS F> void Hashtable<T, F>::reverse(void* boundary) {
     413
     414   for (int i = 0; i < this->table_size(); ++i) {
     415     HashtableEntry<T, F>* high_list = NULL;
     416     HashtableEntry<T, F>* low_list = NULL;
     417     HashtableEntry<T, F>* last_low_entry = NULL;
     418     HashtableEntry<T, F>* p = bucket(i);
     419     while (p != NULL) {
     420       HashtableEntry<T, F>* next = p->next();
     421       if ((void*)p->literal() >= boundary) {
     422         p->set_next(high_list);
     423         high_list = p;
     424       } else {
     425         p->set_next(low_list);
     426         low_list = p;
     427         if (last_low_entry == NULL) {
     428           last_low_entry = p;
     429         }
     430       }
     431       p = next;
     432     }
     433     if (low_list != NULL) {
     434       *bucket_addr(i) = low_list;
     435       last_low_entry->set_next(high_list);
     436     } else {
     437       *bucket_addr(i) = high_list;
     438     }
     439   }
     440 }
     441
     442 // Dump the hash table buckets.
     443
     444 template <MEMFLAGS F> void BasicHashtable<F>::copy_buckets(char** top, char* end) {
     445   intptr_t len = _table_size * sizeof(HashtableBucket<F>);
     446   *(intptr_t*)(*top) = len;
     447   *top += sizeof(intptr_t);
     448
     449   *(intptr_t*)(*top) = _number_of_entries;
     450   *top += sizeof(intptr_t);
     451
     452   if (*top + len > end) {
     453     report_out_of_shared_space(SharedMiscData);
     454   }
     455   _buckets = (HashtableBucket<F>*)memcpy(*top, _buckets, len);
     456   *top += len;
     457 }
     458
     459 template <class T, MEMFLAGS F> int Hashtable<T, F>::literal_size(Symbol *symbol) {
     460   return symbol->size() * HeapWordSize;
     461 }
     462
     463 template <class T, MEMFLAGS F> int Hashtable<T, F>::literal_size(oop oop) {
     464   // NOTE: this would over-count if (pre-JDK8) java_lang_Class::has_offset_field() is true,
     465   // and the String.value array is shared by several Strings. However, starting from JDK8,
     466   // the String.value array is not shared anymore.
     467   assert(oop != NULL && oop->klass() == SystemDictionary::String_klass(), "only strings are supported");
     468   return (oop->size() + java_lang_String::value(oop)->size()) * HeapWordSize;
     469 }
     470
     471 template <class T, MEMFLAGS F> int Hashtable<T, F>::literal_size(Klass *klass) {
     472   return klass->size() * HeapWordSize;
     473 }
     474
     475 // Dump footprint and bucket length statistics
     476 //
     477 // Note: if you create a new subclass of Hashtable<MyNewType, F>, you will need to
     478 // add a new function Hashtable<T, F>::literal_size(MyNewType lit)
     479
     480 template <class T, MEMFLAGS F> void Hashtable<T, F>::dump_table(outputStream* st, const char *table_name) {
     481   NumberSeq summary;
     482   int literal_bytes = 0;
     483   for (int i = 0; i < this->table_size(); ++i) {
     484     int count = 0;
     485     for (HashtableEntry<T, F>* e = this->bucket(i);
     486        e != NULL; e = e->next()) {
     487       count++;
     488       literal_bytes += literal_size(e->literal());
     489     }
     490     summary.add((double)count);
     491   }
     492   double num_buckets = summary.num();
     493   double num_entries = summary.sum();
     494
     495   int bucket_bytes = (int)num_buckets * sizeof(HashtableBucket<F>);
     496   int entry_bytes  = (int)num_entries * sizeof(HashtableEntry<T, F>);
     497   int total_bytes = literal_bytes +  bucket_bytes + entry_bytes;
     498
     499   double bucket_avg  = (num_buckets <= 0) ? 0 : (bucket_bytes  / num_buckets);
     500   double entry_avg   = (num_entries <= 0) ? 0 : (entry_bytes   / num_entries);
     501   double literal_avg = (num_entries <= 0) ? 0 : (literal_bytes / num_entries);
     502
     503   st->print_cr("%s statistics:", table_name);
     504   st->print_cr("Number of buckets       : %9d = %9d bytes, avg %7.3f", (int)num_buckets, bucket_bytes,  bucket_avg);
     505   st->print_cr("Number of entries       : %9d = %9d bytes, avg %7.3f", (int)num_entries, entry_bytes,   entry_avg);
     506   st->print_cr("Number of literals      : %9d = %9d bytes, avg %7.3f", (int)num_entries, literal_bytes, literal_avg);
     507   st->print_cr("Total footprint         : %9s = %9d bytes", "", total_bytes);
     508   st->print_cr("Average bucket size     : %9.3f (load factor)", summary.avg());
     509   st->print_cr("Variance of bucket size : %9.3f", summary.variance());
     510   st->print_cr("Std. dev. of bucket size: %9.3f", summary.sd());
     511   st->print_cr("Maximum bucket size     : %9d", (int)summary.maximum());
     512 }
     513
     514 template <class T, MEMFLAGS F> void Hashtable<T, F>::dump(outputStream* st, const char* table_name) {
     515 #ifndef PRODUCT
     516 //#ifdef PRODUCT_TEST
     517   st->print("\n-------------------------------------------------------------------------------\n");
     518   dump_table(st, table_name);
     519   st->print("-------------------------\n");
     520   {
     521     int count = 0;
     522     for (int i=0; i<1024; i++) {
     523       if (this->_debug_table_threads[i] != 0) {
     524         count++;
     525       } else {
     526         break;
     527       }
     528     }
     529     st->print_cr("Number of threads       : %9d", count);
     530   }
     531   st->print("-------------------------\n");
     532   {
     533     st->print_cr("All Lookups             : %9llu", this->_debug_all_lookup_count);
     534     st->print_cr("All Insertions          : %9llu", this->_debug_all_insertion_count);
     535     double ratio = 0.0;
     536     if (this->_debug_all_lookup_count > 0.0) {
     537       ratio = (double)(this->_debug_all_lookup_count-this->_debug_all_insertion_count) / (double)this->_debug_all_lookup_count;
     538     }
     539     st->print_cr("All Lookups ratio       : %9.3f", 100.0*ratio);
     540   }
     541   st->print("-------------------------\n");
     542   {
     543     st->print_cr("Lookups                 : %9llu", this->_debug_lookup_count);
     544     st->print_cr("Insertions              : %9llu", this->_debug_insertion_count);
     545     double ratio = 0.0;
     546     if (this->_debug_lookup_count > 0.0) {
     547       ratio = (double)(this->_debug_lookup_count-this->_debug_insertion_count) / (double)this->_debug_lookup_count;
     548     }
     549     st->print_cr("Lookups ratio           : %9.3f", 100.0*ratio);
     550   }
     551   {
     552     st->print_cr("Found Lookups           : %9llu", this->_debug_lookup_found_count);
     553     st->print_cr("Failed Lookups          : %9llu", this->_debug_lookup_failed_count);
     554     double ratio = 0.0;
     555     if (this->_debug_lookup_found_count > 0) {
     556       ratio = (double)this->_debug_lookup_found_count / (double)(this->_debug_lookup_found_count+this->_debug_lookup_failed_count);
     557     }
     558     st->print_cr("Found ratio             : %9.3f", 100.0*ratio);
     559   }
     560   st->print("-------------------------\n");
     561   {
     562     double average = 0.0;
     563     if (this->_debug_lookup_found_count > 0) {
     564       average = (((double)this->_debug_lookup_found_total_duration / (double)this->_debug_lookup_found_count) + 0.5);
     565     }
     566     st->print_cr("Total found lookup time : %9llu ns", this->_debug_lookup_found_total_duration);
     567     st->print_cr("Max found lookup time   : %9llu ns", this->_debug_lookup_found_max_duration);
     568     st->print_cr("Avg found lookup time   : %9d ns", (int)average);
     569   }
     570   {
     571     double average = 0.0;
     572     if (this->_debug_lookup_failed_count > 0) {
     573       average = (((double)this->_debug_lookup_failed_total_duration / (double)this->_debug_lookup_failed_count) + 0.5);
     574     }
     575     st->print_cr("Total failed lookup time: %9llu ns", this->_debug_lookup_failed_total_duration);
     576     st->print_cr("Max failed lookup time  : %9llu ns", this->_debug_lookup_failed_max_duration);
     577     st->print_cr("Avg failed lookup time  : %9d ns", (int)average);
     578   }
     579   st->print("-------------------------\n");
     580   {
     581     double average = 0.0;
     582     if (this->_debug_lookup_found_count > 0) {
     583       average = (double)this->_debug_chain_found_total_length / (double)this->_debug_lookup_found_count;
     584     }
     585     st->print_cr("Max found chain length  : %9llu", this->_debug_chain_found_maximum_length);
     586     st->print_cr("Avg found chain length  : %9.3f", average);
     587   }
     588   {
     589     double average = 0.0;
     590     if (this->_debug_lookup_failed_count > 0) {
     591       average = (double)this->_debug_chain_failed_total_length / (double)this->_debug_lookup_failed_count;
     592     }
     593     st->print_cr("Max failed chain length : %9llu", this->_debug_chain_failed_maximum_length);
     594     st->print_cr("Avg failed chain length : %9.3f", average);
     595   }
     596   st->print("-------------------------\n");
     597   {
     598     st->print_cr("Times table was resized : %9llu", this->_debug_resize_total_count);
     599     double average = 0.0;
     600     if (this->_debug_resize_total_count > 0) {
     601       average = (((double)this->_debug_resize_total_duration / (double)this->_debug_resize_total_count) + 0.5);
     602     }
     603     st->print_cr("Maximum resize time     : %9d ns (%.5f s)", (int)this->_debug_resize_max_duration, (double)this->_debug_resize_max_duration/1000000000.0);
     604     st->print_cr("Average resize time     : %9d ns (%.5f s)", (int)average, (double)average/1000000000.0);
     605     st->print_cr("Total resize time       : %9.5f s", (double)this->_debug_resize_total_duration/1000000000.0);
     606   }
     607 #endif
     608 }
     609

     610 #ifndef PRODUCT
     611
     612 template <class T, MEMFLAGS F> void Hashtable<T, F>::print() {
     613   ResourceMark rm;
     614
     615   for (int i = 0; i < BasicHashtable<F>::table_size(); i++) {
     616     HashtableEntry<T, F>* entry = bucket(i);
     617     while(entry != NULL) {
     618       tty->print("%d : ", i);
     619       entry->literal()->print();
     620       tty->cr();
     621       entry = entry->next();
     622     }
     623   }
     624 }
     625

     626 template <MEMFLAGS F> void BasicHashtable<F>::verify() {
     627   int count = 0;
     628   for (int i = 0; i < table_size(); i++) {
     629     for (BasicHashtableEntry<F>* p = bucket(i); p != NULL; p = p->next()) {
     630       ++count;
     631     }
     632   }
     633   assert(count == number_of_entries(), "number of hashtable entries incorrect");
     634 }
     635

     636 #endif // PRODUCT
     637

     638 #ifdef ASSERT
     639
     640 template <MEMFLAGS F> void BasicHashtable<F>::verify_lookup_length(double load) {
     641   if ((double)_lookup_length / (double)_lookup_count > load * 2.0) {
     642     warning("Performance bug: SystemDictionary lookup_count=%d "
     643             "lookup_length=%d average=%lf load=%f",
     644             _lookup_count, _lookup_length,
     645             (double) _lookup_length / _lookup_count, load);
     646   }
     647 }
     648
     649 #endif // ASSERT
     650
     651 // Explicitly instantiate these types
     652 #if INCLUDE_ALL_GCS
     653 template class Hashtable<nmethod*, mtGC>;
     654 template class HashtableEntry<nmethod*, mtGC>;
     655 template class BasicHashtable<mtGC>;
     656 #endif
     657 template class Hashtable<ConstantPool*, mtClass>;
     658 template class RehashableHashtable<Symbol*, mtSymbol>;
     659 template class RehashableHashtable<oopDesc*, mtSymbol>;
     660 template class Hashtable<Symbol*, mtSymbol>;
     661 template class Hashtable<Klass*, mtClass>;
     662 template class Hashtable<oop, mtClass>;
     663 #if defined(SOLARIS) || defined(CHECK_UNHANDLED_OOPS)
     664 template class Hashtable<oop, mtSymbol>;
     665 template class RehashableHashtable<oop, mtSymbol>;
     666 #endif // SOLARIS || CHECK_UNHANDLED_OOPS
     667 template class Hashtable<oopDesc*, mtSymbol>;
     668 template class Hashtable<Symbol*, mtClass>;
     669 template class HashtableEntry<Symbol*, mtSymbol>;
     670 template class HashtableEntry<Symbol*, mtClass>;
     671 template class HashtableEntry<oop, mtSymbol>;
     672 template class BasicHashtableEntry<mtSymbol>;
     673 template class BasicHashtableEntry<mtCode>;
     674 template class BasicHashtable<mtClass>;
     675 template class BasicHashtable<mtSymbol>;
     676 template class BasicHashtable<mtCode>;
     677 template class BasicHashtable<mtInternal>;
    --- EOF ---