1 /*
   2  * Copyright (c) 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUPTABLE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUPTABLE_HPP
  27 
  28 #include "gc_implementation/g1/g1StringDedupStat.hpp"
  29 
  30 class G1StringDedupEntryCache;
  31 
  32 //
  33 // Table entry in the deduplication hashtable. Points weakly to the
  34 // character array. Can be chained in a linked list in case of hash
  35 // collisions or when placed in a freelist in the entry cache.
  36 //
  37 class G1StringDedupEntry : public CHeapObj<mtGC> {
  38 private:
  39   G1StringDedupEntry* _next;
  40   unsigned int      _hash;
  41   typeArrayOop      _obj;
  42 
  43 public:
  44   G1StringDedupEntry() :
  45     _next(NULL),
  46     _hash(0),
  47     _obj(NULL) {
  48   }
  49 
  50   G1StringDedupEntry* next() {
  51     return _next;
  52   }
  53 
  54   G1StringDedupEntry** next_addr() {
  55     return &_next;
  56   }
  57 
  58   void set_next(G1StringDedupEntry* next) {
  59     _next = next;
  60   }
  61 
  62   unsigned int hash() {
  63     return _hash;
  64   }
  65 
  66   void set_hash(unsigned int hash) {
  67     _hash = hash;
  68   }
  69 
  70   typeArrayOop obj() {
  71     return _obj;
  72   }
  73 
  74   typeArrayOop* obj_addr() {
  75     return &_obj;
  76   }
  77 
  78   void set_obj(typeArrayOop obj) {
  79     _obj = obj;
  80   }
  81 };
  82 
  83 //
  84 // The deduplication hashtable keeps track of all unique character arrays used
  85 // by String objects. Each table entry weakly points to an character array, allowing
  86 // otherwise unreachable character arrays to be declared dead and pruned from the
  87 // table.
  88 //
  89 // The table is dynamically resized to accommodate the current number of table entries.
  90 // The table has hash buckets with chains for hash collision. If the average chain
  91 // length goes above or below given thresholds the table grows or shrinks accordingly.
  92 //
  93 // The table is also dynamically rehashed (using a new hash seed) if it becomes severely
  94 // unbalanced, i.e., a hash chain is significantly longer than average.
  95 //
  96 // All access to the table is protected by the StringDedupTable_lock, except under
  97 // safepoints in which case GC workers are allowed to access a table partitions they
  98 // have claimed without first acquiring the lock. Note however, that this applies only
  99 // the table partition (i.e. a range of elements in _buckets), not other parts of the
 100 // table such as the _entries field, statistics counters, etc.
 101 //
 102 class G1StringDedupTable : public CHeapObj<mtGC> {
 103 private:
 104   // The currently active hashtable instance. Only modified when
 105   // the table is resizes or rehashed.
 106   static G1StringDedupTable*      _table;
 107 
 108   // Cache for reuse and fast alloc/free of table entries.
 109   static G1StringDedupEntryCache* _entry_cache;
 110 
 111   G1StringDedupEntry**            _buckets;
 112   size_t                          _size;
 113   uintx                           _entries;
 114   uintx                           _shrink_threshold;
 115   uintx                           _grow_threshold;
 116   bool                            _rehash_needed;
 117 
 118   // The hash seed also dictates which hash function to use. A
 119   // zero hash seed means we will use the Java compatible hash
 120   // function (which doesn't use a seed), and a non-zero hash
 121   // seed means we use the murmur3 hash function.
 122   jint                            _hash_seed;
 123 
 124   // Constants governing table resize/rehash/cache.
 125   static const size_t             _min_size;
 126   static const size_t             _max_size;
 127   static const double             _grow_load_factor;
 128   static const double             _shrink_load_factor;
 129   static const uintx              _rehash_multiple;
 130   static const uintx              _rehash_threshold;
 131   static const double             _max_cache_factor;
 132 
 133   // Table statistics, only used for logging.
 134   static uintx                    _entries_added;
 135   static uintx                    _entries_removed;
 136   static uintx                    _resize_count;
 137   static uintx                    _rehash_count;
 138 
 139   G1StringDedupTable(size_t size, jint hash_seed = 0);
 140   ~G1StringDedupTable();
 141 
 142   // Returns the hash bucket at the given index.
 143   G1StringDedupEntry** bucket(size_t index) {
 144     return _buckets + index;
 145   }
 146 
 147   // Returns the hash bucket index for the given hash code.
 148   size_t hash_to_index(unsigned int hash) {
 149     return (size_t)hash & (_size - 1);
 150   }
 151 
 152   // Adds a new table entry to the given hash bucket.
 153   void add(typeArrayOop value, unsigned int hash, G1StringDedupEntry** list);
 154 
 155   // Removes the given table entry from the table.
 156   void remove(G1StringDedupEntry** pentry, uint worker_id);
 157 
 158   // Transfers a table entry from the current table to the destination table.
 159   void transfer(G1StringDedupEntry** pentry, G1StringDedupTable* dest);
 160 
 161   // Returns an existing character array in the given hash bucket, or NULL
 162   // if no matching character array exists.
 163   typeArrayOop lookup(typeArrayOop value, unsigned int hash,
 164                       G1StringDedupEntry** list, uintx &count);
 165 
 166   // Returns an existing character array in the table, or inserts a new
 167   // table entry if no matching character array exists.
 168   typeArrayOop lookup_or_add_inner(typeArrayOop value, unsigned int hash);
 169 
 170   // Thread safe lookup or add of table entry
 171   static typeArrayOop lookup_or_add(typeArrayOop value, unsigned int hash) {
 172     // Protect the table from concurrent access. Also note that this lock
 173     // acts as a fence for _table, which could have been replaced by a new
 174     // instance if the table was resized or rehashed.
 175     MutexLockerEx ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag);
 176     return _table->lookup_or_add_inner(value, hash);
 177   }
 178 
 179   // Returns true if the hashtable is currently using a Java compatible
 180   // hash function.
 181   static bool use_java_hash() {
 182     return _table->_hash_seed == 0;
 183   }
 184 
 185   static bool equals(typeArrayOop value1, typeArrayOop value2);
 186 
 187   // Computes the hash code for the given character array, using the
 188   // currently active hash function and hash seed.
 189   static unsigned int hash_code(typeArrayOop value);
 190 
 191   static uintx unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl,
 192                                  size_t partition_begin,
 193                                  size_t partition_end,
 194                                  uint worker_id);
 195 
 196 public:
 197   static void create();
 198 
 199   // Deduplicates the given String object, or adds its backing
 200   // character array to the deduplication hashtable.
 201   static void deduplicate(oop java_string, G1StringDedupStat& stat);
 202 
 203   // If a table resize is needed, returns a newly allocated empty
 204   // hashtable of the proper size.
 205   static G1StringDedupTable* prepare_resize();
 206 
 207   // Installs a newly resized table as the currently active table
 208   // and deletes the previously active table.
 209   static void finish_resize(G1StringDedupTable* resized_table);
 210 
 211   // If a table rehash is needed, returns a newly allocated empty
 212   // hashtable and updates the hash seed.
 213   static G1StringDedupTable* prepare_rehash();
 214 
 215   // Transfers rehashed entries from the currently active table into
 216   // the new table. Installs the new table as the currently active table
 217   // and deletes the previously active table.
 218   static void finish_rehash(G1StringDedupTable* rehashed_table);
 219 
 220   // If the table entry cache has grown too large, trim it down according to policy
 221   static void trim_entry_cache();
 222 
 223   static void unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, uint worker_id);
 224 
 225   static void print_statistics(outputStream* st);
 226   static void verify();
 227 };
 228 
 229 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUPTABLE_HPP