hotspot/src/share/vm/gc_implementation/g1/g1StringDedupTable.cpp
author stefank
Fri, 13 Feb 2015 14:37:35 +0100
changeset 29081 c61eb4914428
parent 27880 afb974a04396
child 30173 13cf7580b000
permissions -rw-r--r--
8072911: Remove includes of oop.inline.hpp from .hpp files Reviewed-by: brutisso, coleenp, jwilhelm, simonis, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     1
/*
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 27880
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     4
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     8
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    13
 * accompanied this code).
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    14
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    18
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    21
 * questions.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    22
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    23
 */
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    24
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    25
#include "precompiled.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    26
#include "classfile/altHashing.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 27880
diff changeset
    27
#include "classfile/javaClasses.inline.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    28
#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    29
#include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    30
#include "gc_implementation/g1/g1StringDedupTable.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    31
#include "memory/gcLocker.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    32
#include "memory/padded.inline.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    33
#include "oops/typeArrayOop.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 27880
diff changeset
    34
#include "oops/oop.inline.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    35
#include "runtime/mutexLocker.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    36
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    37
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    38
// Freelist in the deduplication table entry cache. Links table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    39
// entries together using their _next fields.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    40
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    41
class G1StringDedupEntryFreeList : public CHeapObj<mtGC> {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    42
private:
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    43
  G1StringDedupEntry* _list;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    44
  size_t              _length;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    45
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    46
public:
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    47
  G1StringDedupEntryFreeList() :
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    48
    _list(NULL),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    49
    _length(0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    50
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    51
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    52
  void add(G1StringDedupEntry* entry) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    53
    entry->set_next(_list);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    54
    _list = entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    55
    _length++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    56
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    57
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    58
  G1StringDedupEntry* remove() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    59
    G1StringDedupEntry* entry = _list;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    60
    if (entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    61
      _list = entry->next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    62
      _length--;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    63
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    64
    return entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    65
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    66
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    67
  size_t length() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    68
    return _length;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    69
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    70
};
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    71
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    72
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    73
// Cache of deduplication table entries. This cache provides fast allocation and
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    74
// reuse of table entries to lower the pressure on the underlying allocator.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    75
// But more importantly, it provides fast/deferred freeing of table entries. This
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    76
// is important because freeing of table entries is done during stop-the-world
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    77
// phases and it is not uncommon for large number of entries to be freed at once.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    78
// Tables entries that are freed during these phases are placed onto a freelist in
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    79
// the cache. The deduplication thread, which executes in a concurrent phase, will
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    80
// later reuse or free the underlying memory for these entries.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    81
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    82
// The cache allows for single-threaded allocations and multi-threaded frees.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    83
// Allocations are synchronized by StringDedupTable_lock as part of a table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    84
// modification.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    85
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    86
class G1StringDedupEntryCache : public CHeapObj<mtGC> {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    87
private:
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    88
  // One freelist per GC worker to allow lock less freeing of
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    89
  // entries while doing a parallel scan of the table. Using
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    90
  // PaddedEnd to avoid false sharing.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    91
  PaddedEnd<G1StringDedupEntryFreeList>* _lists;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    92
  size_t                                 _nlists;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    93
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    94
public:
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    95
  G1StringDedupEntryCache();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    96
  ~G1StringDedupEntryCache();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    97
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    98
  // Get a table entry from the cache freelist, or allocate a new
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    99
  // entry if the cache is empty.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   100
  G1StringDedupEntry* alloc();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   101
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   102
  // Insert a table entry into the cache freelist.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   103
  void free(G1StringDedupEntry* entry, uint worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   104
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   105
  // Returns current number of entries in the cache.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   106
  size_t size();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   107
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   108
  // If the cache has grown above the given max size, trim it down
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   109
  // and deallocate the memory occupied by trimmed of entries.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   110
  void trim(size_t max_size);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   111
};
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   112
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   113
G1StringDedupEntryCache::G1StringDedupEntryCache() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   114
  _nlists = MAX2(ParallelGCThreads, (size_t)1);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   115
  _lists = PaddedArray<G1StringDedupEntryFreeList, mtGC>::create_unfreeable((uint)_nlists);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   116
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   117
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   118
G1StringDedupEntryCache::~G1StringDedupEntryCache() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   119
  ShouldNotReachHere();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   120
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   121
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   122
G1StringDedupEntry* G1StringDedupEntryCache::alloc() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   123
  for (size_t i = 0; i < _nlists; i++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   124
    G1StringDedupEntry* entry = _lists[i].remove();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   125
    if (entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   126
      return entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   127
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   128
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   129
  return new G1StringDedupEntry();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   130
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   131
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   132
void G1StringDedupEntryCache::free(G1StringDedupEntry* entry, uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   133
  assert(entry->obj() != NULL, "Double free");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   134
  assert(worker_id < _nlists, "Invalid worker id");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   135
  entry->set_obj(NULL);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   136
  entry->set_hash(0);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   137
  _lists[worker_id].add(entry);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   138
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   139
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   140
size_t G1StringDedupEntryCache::size() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   141
  size_t size = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   142
  for (size_t i = 0; i < _nlists; i++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   143
    size += _lists[i].length();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   144
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   145
  return size;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   146
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   147
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   148
void G1StringDedupEntryCache::trim(size_t max_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   149
  size_t cache_size = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   150
  for (size_t i = 0; i < _nlists; i++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   151
    G1StringDedupEntryFreeList* list = &_lists[i];
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   152
    cache_size += list->length();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   153
    while (cache_size > max_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   154
      G1StringDedupEntry* entry = list->remove();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   155
      assert(entry != NULL, "Should not be null");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   156
      cache_size--;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   157
      delete entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   158
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   159
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   160
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   161
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   162
G1StringDedupTable*      G1StringDedupTable::_table = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   163
G1StringDedupEntryCache* G1StringDedupTable::_entry_cache = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   164
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   165
const size_t             G1StringDedupTable::_min_size = (1 << 10);   // 1024
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   166
const size_t             G1StringDedupTable::_max_size = (1 << 24);   // 16777216
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   167
const double             G1StringDedupTable::_grow_load_factor = 2.0; // Grow table at 200% load
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   168
const double             G1StringDedupTable::_shrink_load_factor = _grow_load_factor / 3.0; // Shrink table at 67% load
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   169
const double             G1StringDedupTable::_max_cache_factor = 0.1; // Cache a maximum of 10% of the table size
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   170
const uintx              G1StringDedupTable::_rehash_multiple = 60;   // Hash bucket has 60 times more collisions than expected
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   171
const uintx              G1StringDedupTable::_rehash_threshold = (uintx)(_rehash_multiple * _grow_load_factor);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   172
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   173
uintx                    G1StringDedupTable::_entries_added = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   174
uintx                    G1StringDedupTable::_entries_removed = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   175
uintx                    G1StringDedupTable::_resize_count = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   176
uintx                    G1StringDedupTable::_rehash_count = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   177
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   178
G1StringDedupTable::G1StringDedupTable(size_t size, jint hash_seed) :
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   179
  _size(size),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   180
  _entries(0),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   181
  _grow_threshold((uintx)(size * _grow_load_factor)),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   182
  _shrink_threshold((uintx)(size * _shrink_load_factor)),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   183
  _rehash_needed(false),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   184
  _hash_seed(hash_seed) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   185
  assert(is_power_of_2(size), "Table size must be a power of 2");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   186
  _buckets = NEW_C_HEAP_ARRAY(G1StringDedupEntry*, _size, mtGC);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   187
  memset(_buckets, 0, _size * sizeof(G1StringDedupEntry*));
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   188
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   189
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   190
G1StringDedupTable::~G1StringDedupTable() {
27880
afb974a04396 8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents: 23472
diff changeset
   191
  FREE_C_HEAP_ARRAY(G1StringDedupEntry*, _buckets);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   192
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   193
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   194
void G1StringDedupTable::create() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   195
  assert(_table == NULL, "One string deduplication table allowed");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   196
  _entry_cache = new G1StringDedupEntryCache();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   197
  _table = new G1StringDedupTable(_min_size);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   198
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   199
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   200
void G1StringDedupTable::add(typeArrayOop value, unsigned int hash, G1StringDedupEntry** list) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   201
  G1StringDedupEntry* entry = _entry_cache->alloc();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   202
  entry->set_obj(value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   203
  entry->set_hash(hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   204
  entry->set_next(*list);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   205
  *list = entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   206
  _entries++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   207
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   208
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   209
void G1StringDedupTable::remove(G1StringDedupEntry** pentry, uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   210
  G1StringDedupEntry* entry = *pentry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   211
  *pentry = entry->next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   212
  _entry_cache->free(entry, worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   213
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   214
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   215
void G1StringDedupTable::transfer(G1StringDedupEntry** pentry, G1StringDedupTable* dest) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   216
  G1StringDedupEntry* entry = *pentry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   217
  *pentry = entry->next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   218
  unsigned int hash = entry->hash();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   219
  size_t index = dest->hash_to_index(hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   220
  G1StringDedupEntry** list = dest->bucket(index);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   221
  entry->set_next(*list);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   222
  *list = entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   223
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   224
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   225
bool G1StringDedupTable::equals(typeArrayOop value1, typeArrayOop value2) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   226
  return (value1 == value2 ||
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   227
          (value1->length() == value2->length() &&
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   228
           (!memcmp(value1->base(T_CHAR),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   229
                    value2->base(T_CHAR),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   230
                    value1->length() * sizeof(jchar)))));
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   231
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   232
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   233
typeArrayOop G1StringDedupTable::lookup(typeArrayOop value, unsigned int hash,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   234
                                        G1StringDedupEntry** list, uintx &count) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   235
  for (G1StringDedupEntry* entry = *list; entry != NULL; entry = entry->next()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   236
    if (entry->hash() == hash) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   237
      typeArrayOop existing_value = entry->obj();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   238
      if (equals(value, existing_value)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   239
        // Match found
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   240
        return existing_value;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   241
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   242
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   243
    count++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   244
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   245
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   246
  // Not found
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   247
  return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   248
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   249
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   250
typeArrayOop G1StringDedupTable::lookup_or_add_inner(typeArrayOop value, unsigned int hash) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   251
  size_t index = hash_to_index(hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   252
  G1StringDedupEntry** list = bucket(index);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   253
  uintx count = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   254
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   255
  // Lookup in list
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   256
  typeArrayOop existing_value = lookup(value, hash, list, count);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   257
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   258
  // Check if rehash is needed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   259
  if (count > _rehash_threshold) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   260
    _rehash_needed = true;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   261
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   262
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   263
  if (existing_value == NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   264
    // Not found, add new entry
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   265
    add(value, hash, list);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   266
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   267
    // Update statistics
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   268
    _entries_added++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   269
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   270
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   271
  return existing_value;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   272
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   273
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   274
unsigned int G1StringDedupTable::hash_code(typeArrayOop value) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   275
  unsigned int hash;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   276
  int length = value->length();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   277
  const jchar* data = (jchar*)value->base(T_CHAR);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   278
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   279
  if (use_java_hash()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   280
    hash = java_lang_String::hash_code(data, length);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   281
  } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   282
    hash = AltHashing::murmur3_32(_table->_hash_seed, data, length);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   283
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   284
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   285
  return hash;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   286
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   287
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   288
void G1StringDedupTable::deduplicate(oop java_string, G1StringDedupStat& stat) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   289
  assert(java_lang_String::is_instance(java_string), "Must be a string");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   290
  No_Safepoint_Verifier nsv;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   291
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   292
  stat.inc_inspected();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   293
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   294
  typeArrayOop value = java_lang_String::value(java_string);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   295
  if (value == NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   296
    // String has no value
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   297
    stat.inc_skipped();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   298
    return;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   299
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   300
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   301
  unsigned int hash = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   302
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   303
  if (use_java_hash()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   304
    // Get hash code from cache
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   305
    hash = java_lang_String::hash(java_string);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   306
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   307
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   308
  if (hash == 0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   309
    // Compute hash
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   310
    hash = hash_code(value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   311
    stat.inc_hashed();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   312
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   313
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   314
  if (use_java_hash() && hash != 0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   315
    // Store hash code in cache
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   316
    java_lang_String::set_hash(java_string, hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   317
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   318
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   319
  typeArrayOop existing_value = lookup_or_add(value, hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   320
  if (existing_value == value) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   321
    // Same value, already known
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   322
    stat.inc_known();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   323
    return;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   324
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   325
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   326
  // Get size of value array
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   327
  uintx size_in_bytes = value->size() * HeapWordSize;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   328
  stat.inc_new(size_in_bytes);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   329
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   330
  if (existing_value != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   331
    // Enqueue the reference to make sure it is kept alive. Concurrent mark might
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   332
    // otherwise declare it dead if there are no other strong references to this object.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   333
    G1SATBCardTableModRefBS::enqueue(existing_value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   334
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   335
    // Existing value found, deduplicate string
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   336
    java_lang_String::set_value(java_string, existing_value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   337
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   338
    if (G1CollectedHeap::heap()->is_in_young(value)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   339
      stat.inc_deduped_young(size_in_bytes);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   340
    } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   341
      stat.inc_deduped_old(size_in_bytes);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   342
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   343
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   344
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   345
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   346
G1StringDedupTable* G1StringDedupTable::prepare_resize() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   347
  size_t size = _table->_size;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   348
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   349
  // Check if the hashtable needs to be resized
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   350
  if (_table->_entries > _table->_grow_threshold) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   351
    // Grow table, double the size
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   352
    size *= 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   353
    if (size > _max_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   354
      // Too big, don't resize
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   355
      return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   356
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   357
  } else if (_table->_entries < _table->_shrink_threshold) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   358
    // Shrink table, half the size
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   359
    size /= 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   360
    if (size < _min_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   361
      // Too small, don't resize
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   362
      return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   363
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   364
  } else if (StringDeduplicationResizeALot) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   365
    // Force grow
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   366
    size *= 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   367
    if (size > _max_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   368
      // Too big, force shrink instead
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   369
      size /= 4;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   370
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   371
  } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   372
    // Resize not needed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   373
    return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   374
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   375
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   376
  // Update statistics
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   377
  _resize_count++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   378
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   379
  // Allocate the new table. The new table will be populated by workers
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   380
  // calling unlink_or_oops_do() and finally installed by finish_resize().
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   381
  return new G1StringDedupTable(size, _table->_hash_seed);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   382
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   383
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   384
void G1StringDedupTable::finish_resize(G1StringDedupTable* resized_table) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   385
  assert(resized_table != NULL, "Invalid table");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   386
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   387
  resized_table->_entries = _table->_entries;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   388
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   389
  // Free old table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   390
  delete _table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   391
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   392
  // Install new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   393
  _table = resized_table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   394
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   395
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   396
void G1StringDedupTable::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   397
  // The table is divided into partitions to allow lock-less parallel processing by
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   398
  // multiple worker threads. A worker thread first claims a partition, which ensures
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   399
  // exclusive access to that part of the table, then continues to process it. To allow
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   400
  // shrinking of the table in parallel we also need to make sure that the same worker
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   401
  // thread processes all partitions where entries will hash to the same destination
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   402
  // partition. Since the table size is always a power of two and we always shrink by
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   403
  // dividing the table in half, we know that for a given partition there is only one
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   404
  // other partition whoes entries will hash to the same destination partition. That
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   405
  // other partition is always the sibling partition in the second half of the table.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   406
  // For example, if the table is divided into 8 partitions, the sibling of partition 0
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   407
  // is partition 4, the sibling of partition 1 is partition 5, etc.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   408
  size_t table_half = _table->_size / 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   409
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   410
  // Let each partition be one page worth of buckets
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   411
  size_t partition_size = MIN2(table_half, os::vm_page_size() / sizeof(G1StringDedupEntry*));
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   412
  assert(table_half % partition_size == 0, "Invalid partition size");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   413
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   414
  // Number of entries removed during the scan
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   415
  uintx removed = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   416
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   417
  for (;;) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   418
    // Grab next partition to scan
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   419
    size_t partition_begin = cl->claim_table_partition(partition_size);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   420
    size_t partition_end = partition_begin + partition_size;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   421
    if (partition_begin >= table_half) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   422
      // End of table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   423
      break;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   424
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   425
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   426
    // Scan the partition followed by the sibling partition in the second half of the table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   427
    removed += unlink_or_oops_do(cl, partition_begin, partition_end, worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   428
    removed += unlink_or_oops_do(cl, table_half + partition_begin, table_half + partition_end, worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   429
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   430
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   431
  // Delayed update avoid contention on the table lock
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   432
  if (removed > 0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   433
    MutexLockerEx ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   434
    _table->_entries -= removed;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   435
    _entries_removed += removed;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   436
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   437
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   438
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   439
uintx G1StringDedupTable::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   440
                                            size_t partition_begin,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   441
                                            size_t partition_end,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   442
                                            uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   443
  uintx removed = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   444
  for (size_t bucket = partition_begin; bucket < partition_end; bucket++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   445
    G1StringDedupEntry** entry = _table->bucket(bucket);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   446
    while (*entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   447
      oop* p = (oop*)(*entry)->obj_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   448
      if (cl->is_alive(*p)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   449
        cl->keep_alive(p);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   450
        if (cl->is_resizing()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   451
          // We are resizing the table, transfer entry to the new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   452
          _table->transfer(entry, cl->resized_table());
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   453
        } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   454
          if (cl->is_rehashing()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   455
            // We are rehashing the table, rehash the entry but keep it
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   456
            // in the table. We can't transfer entries into the new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   457
            // at this point since we don't have exclusive access to all
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   458
            // destination partitions. finish_rehash() will do a single
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   459
            // threaded transfer of all entries.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   460
            typeArrayOop value = (typeArrayOop)*p;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   461
            unsigned int hash = hash_code(value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   462
            (*entry)->set_hash(hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   463
          }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   464
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   465
          // Move to next entry
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   466
          entry = (*entry)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   467
        }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   468
      } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   469
        // Not alive, remove entry from table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   470
        _table->remove(entry, worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   471
        removed++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   472
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   473
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   474
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   475
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   476
  return removed;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   477
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   478
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   479
G1StringDedupTable* G1StringDedupTable::prepare_rehash() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   480
  if (!_table->_rehash_needed && !StringDeduplicationRehashALot) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   481
    // Rehash not needed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   482
    return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   483
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   484
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   485
  // Update statistics
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   486
  _rehash_count++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   487
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   488
  // Compute new hash seed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   489
  _table->_hash_seed = AltHashing::compute_seed();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   490
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   491
  // Allocate the new table, same size and hash seed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   492
  return new G1StringDedupTable(_table->_size, _table->_hash_seed);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   493
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   494
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   495
void G1StringDedupTable::finish_rehash(G1StringDedupTable* rehashed_table) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   496
  assert(rehashed_table != NULL, "Invalid table");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   497
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   498
  // Move all newly rehashed entries into the correct buckets in the new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   499
  for (size_t bucket = 0; bucket < _table->_size; bucket++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   500
    G1StringDedupEntry** entry = _table->bucket(bucket);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   501
    while (*entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   502
      _table->transfer(entry, rehashed_table);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   503
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   504
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   505
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   506
  rehashed_table->_entries = _table->_entries;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   507
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   508
  // Free old table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   509
  delete _table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   510
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   511
  // Install new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   512
  _table = rehashed_table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   513
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   514
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   515
void G1StringDedupTable::verify() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   516
  for (size_t bucket = 0; bucket < _table->_size; bucket++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   517
    // Verify entries
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   518
    G1StringDedupEntry** entry = _table->bucket(bucket);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   519
    while (*entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   520
      typeArrayOop value = (*entry)->obj();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   521
      guarantee(value != NULL, "Object must not be NULL");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   522
      guarantee(Universe::heap()->is_in_reserved(value), "Object must be on the heap");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   523
      guarantee(!value->is_forwarded(), "Object must not be forwarded");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   524
      guarantee(value->is_typeArray(), "Object must be a typeArrayOop");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   525
      unsigned int hash = hash_code(value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   526
      guarantee((*entry)->hash() == hash, "Table entry has inorrect hash");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   527
      guarantee(_table->hash_to_index(hash) == bucket, "Table entry has incorrect index");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   528
      entry = (*entry)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   529
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   530
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   531
    // Verify that we do not have entries with identical oops or identical arrays.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   532
    // We only need to compare entries in the same bucket. If the same oop or an
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   533
    // identical array has been inserted more than once into different/incorrect
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   534
    // buckets the verification step above will catch that.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   535
    G1StringDedupEntry** entry1 = _table->bucket(bucket);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   536
    while (*entry1 != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   537
      typeArrayOop value1 = (*entry1)->obj();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   538
      G1StringDedupEntry** entry2 = (*entry1)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   539
      while (*entry2 != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   540
        typeArrayOop value2 = (*entry2)->obj();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   541
        guarantee(!equals(value1, value2), "Table entries must not have identical arrays");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   542
        entry2 = (*entry2)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   543
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   544
      entry1 = (*entry1)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   545
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   546
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   547
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   548
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   549
void G1StringDedupTable::trim_entry_cache() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   550
  MutexLockerEx ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   551
  size_t max_cache_size = (size_t)(_table->_size * _max_cache_factor);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   552
  _entry_cache->trim(max_cache_size);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   553
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   554
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   555
void G1StringDedupTable::print_statistics(outputStream* st) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   556
  st->print_cr(
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   557
    "   [Table]\n"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   558
    "      [Memory Usage: "G1_STRDEDUP_BYTES_FORMAT_NS"]\n"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   559
    "      [Size: "SIZE_FORMAT", Min: "SIZE_FORMAT", Max: "SIZE_FORMAT"]\n"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   560
    "      [Entries: "UINTX_FORMAT", Load: "G1_STRDEDUP_PERCENT_FORMAT_NS", Cached: " UINTX_FORMAT ", Added: "UINTX_FORMAT", Removed: "UINTX_FORMAT"]\n"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   561
    "      [Resize Count: "UINTX_FORMAT", Shrink Threshold: "UINTX_FORMAT"("G1_STRDEDUP_PERCENT_FORMAT_NS"), Grow Threshold: "UINTX_FORMAT"("G1_STRDEDUP_PERCENT_FORMAT_NS")]\n"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   562
    "      [Rehash Count: "UINTX_FORMAT", Rehash Threshold: "UINTX_FORMAT", Hash Seed: 0x%x]\n"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   563
    "      [Age Threshold: "UINTX_FORMAT"]",
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   564
    G1_STRDEDUP_BYTES_PARAM(_table->_size * sizeof(G1StringDedupEntry*) + (_table->_entries + _entry_cache->size()) * sizeof(G1StringDedupEntry)),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   565
    _table->_size, _min_size, _max_size,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   566
    _table->_entries, (double)_table->_entries / (double)_table->_size * 100.0, _entry_cache->size(), _entries_added, _entries_removed,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   567
    _resize_count, _table->_shrink_threshold, _shrink_load_factor * 100.0, _table->_grow_threshold, _grow_load_factor * 100.0,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   568
    _rehash_count, _rehash_threshold, _table->_hash_seed,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   569
    StringDeduplicationAgeThreshold);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   570
}