src/hotspot/share/gc/g1/g1StringDedupTable.cpp
author tschatzl
Fri, 09 Feb 2018 12:53:08 +0100
changeset 48890 f9884e190f2b
parent 47679 4cfcb7be4984
child 49041 44122f767467
permissions -rw-r--r--
8197493: [Backout] JDK-8196602 Change HeapRegionClosure to comply to naming conventions Reviewed-by: shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     1
/*
47679
4cfcb7be4984 8189666: Replace various inlined percentage calculations with global percent_of()
tschatzl
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2017, 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"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30175
diff changeset
    28
#include "gc/g1/g1CollectedHeap.inline.hpp"
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30175
diff changeset
    29
#include "gc/g1/g1SATBCardTableModRefBS.hpp"
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30175
diff changeset
    30
#include "gc/g1/g1StringDedup.hpp"
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30175
diff changeset
    31
#include "gc/g1/g1StringDedupTable.hpp"
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30175
diff changeset
    32
#include "gc/shared/gcLocker.hpp"
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 33628
diff changeset
    33
#include "logging/log.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    34
#include "memory/padded.inline.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30175
diff changeset
    35
#include "oops/oop.inline.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    36
#include "oops/typeArrayOop.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    37
#include "runtime/mutexLocker.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    38
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    39
//
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    40
// List of deduplication table entries. Links table
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    41
// entries together using their _next fields.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    42
//
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    43
class G1StringDedupEntryList : public CHeapObj<mtGC> {
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    44
private:
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    45
  G1StringDedupEntry* _list;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    46
  size_t              _length;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    47
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    48
public:
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    49
  G1StringDedupEntryList() :
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    50
    _list(NULL),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    51
    _length(0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    52
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    53
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    54
  void add(G1StringDedupEntry* entry) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    55
    entry->set_next(_list);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    56
    _list = entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    57
    _length++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    58
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    59
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    60
  G1StringDedupEntry* remove() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    61
    G1StringDedupEntry* entry = _list;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    62
    if (entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    63
      _list = entry->next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    64
      _length--;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    65
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    66
    return entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    67
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    68
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    69
  G1StringDedupEntry* remove_all() {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    70
    G1StringDedupEntry* list = _list;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    71
    _list = NULL;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    72
    return list;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    73
  }
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    74
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    75
  size_t length() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    76
    return _length;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    77
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    78
};
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    79
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    80
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    81
// Cache of deduplication table entries. This cache provides fast allocation and
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    82
// reuse of table entries to lower the pressure on the underlying allocator.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    83
// But more importantly, it provides fast/deferred freeing of table entries. This
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    84
// is important because freeing of table entries is done during stop-the-world
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    85
// 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
    86
// Tables entries that are freed during these phases are placed onto a freelist in
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    87
// the cache. The deduplication thread, which executes in a concurrent phase, will
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    88
// later reuse or free the underlying memory for these entries.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    89
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    90
// The cache allows for single-threaded allocations and multi-threaded frees.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    91
// Allocations are synchronized by StringDedupTable_lock as part of a table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    92
// modification.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    93
//
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    94
class G1StringDedupEntryCache : public CHeapObj<mtGC> {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    95
private:
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    96
  // One cache/overflow list per GC worker to allow lock less freeing of
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    97
  // entries while doing a parallel scan of the table. Using PaddedEnd to
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    98
  // avoid false sharing.
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
    99
  size_t                             _nlists;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   100
  size_t                             _max_list_length;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   101
  PaddedEnd<G1StringDedupEntryList>* _cached;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   102
  PaddedEnd<G1StringDedupEntryList>* _overflowed;
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   103
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   104
public:
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   105
  G1StringDedupEntryCache(size_t max_size);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   106
  ~G1StringDedupEntryCache();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   107
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   108
  // Set max number of table entries to cache.
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   109
  void set_max_size(size_t max_size);
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   110
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   111
  // Get a table entry from the cache, or allocate a new entry if the cache is empty.
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   112
  G1StringDedupEntry* alloc();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   113
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   114
  // Insert a table entry into the cache.
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   115
  void free(G1StringDedupEntry* entry, uint worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   116
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   117
  // Returns current number of entries in the cache.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   118
  size_t size();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   119
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   120
  // Deletes overflowed entries.
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   121
  void delete_overflowed();
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   122
};
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   123
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   124
G1StringDedupEntryCache::G1StringDedupEntryCache(size_t max_size) :
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   125
  _nlists(ParallelGCThreads),
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   126
  _max_list_length(0),
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   127
  _cached(PaddedArray<G1StringDedupEntryList, mtGC>::create_unfreeable((uint)_nlists)),
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   128
  _overflowed(PaddedArray<G1StringDedupEntryList, mtGC>::create_unfreeable((uint)_nlists)) {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   129
  set_max_size(max_size);
23472
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
G1StringDedupEntryCache::~G1StringDedupEntryCache() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   133
  ShouldNotReachHere();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   134
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   135
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   136
void G1StringDedupEntryCache::set_max_size(size_t size) {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   137
  _max_list_length = size / _nlists;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   138
}
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   139
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   140
G1StringDedupEntry* G1StringDedupEntryCache::alloc() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   141
  for (size_t i = 0; i < _nlists; i++) {
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   142
    G1StringDedupEntry* entry = _cached[i].remove();
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   143
    if (entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   144
      return entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   145
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   146
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   147
  return new G1StringDedupEntry();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   148
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   149
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   150
void G1StringDedupEntryCache::free(G1StringDedupEntry* entry, uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   151
  assert(entry->obj() != NULL, "Double free");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   152
  assert(worker_id < _nlists, "Invalid worker id");
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   153
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   154
  entry->set_obj(NULL);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   155
  entry->set_hash(0);
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   156
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   157
  if (_cached[worker_id].length() < _max_list_length) {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   158
    // Cache is not full
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   159
    _cached[worker_id].add(entry);
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   160
  } else {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   161
    // Cache is full, add to overflow list for later deletion
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   162
    _overflowed[worker_id].add(entry);
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   163
  }
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   164
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   165
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   166
size_t G1StringDedupEntryCache::size() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   167
  size_t size = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   168
  for (size_t i = 0; i < _nlists; i++) {
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   169
    size += _cached[i].length();
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   170
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   171
  return size;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   172
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   173
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   174
void G1StringDedupEntryCache::delete_overflowed() {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   175
  double start = os::elapsedTime();
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   176
  uintx count = 0;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   177
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   178
  for (size_t i = 0; i < _nlists; i++) {
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   179
    G1StringDedupEntry* entry;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   180
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   181
    {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   182
      // The overflow list can be modified during safepoints, therefore
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   183
      // we temporarily join the suspendible thread set while removing
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   184
      // all entries from the list.
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   185
      SuspendibleThreadSetJoiner sts_join;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   186
      entry = _overflowed[i].remove_all();
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   187
    }
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   188
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   189
    // Delete all entries
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   190
    while (entry != NULL) {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   191
      G1StringDedupEntry* next = entry->next();
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   192
      delete entry;
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   193
      entry = next;
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   194
      count++;
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   195
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   196
  }
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   197
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   198
  double end = os::elapsedTime();
39690
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   199
  log_trace(gc, stringdedup)("Deleted " UINTX_FORMAT " entries, " G1_STRDEDUP_TIME_FORMAT_MS,
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   200
                             count, G1_STRDEDUP_TIME_PARAM_MS(end - start));
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   201
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   202
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   203
G1StringDedupTable*      G1StringDedupTable::_table = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   204
G1StringDedupEntryCache* G1StringDedupTable::_entry_cache = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   205
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   206
const size_t             G1StringDedupTable::_min_size = (1 << 10);   // 1024
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   207
const size_t             G1StringDedupTable::_max_size = (1 << 24);   // 16777216
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   208
const double             G1StringDedupTable::_grow_load_factor = 2.0; // Grow table at 200% load
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   209
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
   210
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
   211
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
   212
const uintx              G1StringDedupTable::_rehash_threshold = (uintx)(_rehash_multiple * _grow_load_factor);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   213
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   214
uintx                    G1StringDedupTable::_entries_added = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   215
uintx                    G1StringDedupTable::_entries_removed = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   216
uintx                    G1StringDedupTable::_resize_count = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   217
uintx                    G1StringDedupTable::_rehash_count = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   218
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   219
G1StringDedupTable::G1StringDedupTable(size_t size, jint hash_seed) :
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   220
  _size(size),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   221
  _entries(0),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   222
  _grow_threshold((uintx)(size * _grow_load_factor)),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   223
  _shrink_threshold((uintx)(size * _shrink_load_factor)),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   224
  _rehash_needed(false),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   225
  _hash_seed(hash_seed) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   226
  assert(is_power_of_2(size), "Table size must be a power of 2");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   227
  _buckets = NEW_C_HEAP_ARRAY(G1StringDedupEntry*, _size, mtGC);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   228
  memset(_buckets, 0, _size * sizeof(G1StringDedupEntry*));
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   229
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   230
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   231
G1StringDedupTable::~G1StringDedupTable() {
27880
afb974a04396 8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents: 23472
diff changeset
   232
  FREE_C_HEAP_ARRAY(G1StringDedupEntry*, _buckets);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   233
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   234
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   235
void G1StringDedupTable::create() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   236
  assert(_table == NULL, "One string deduplication table allowed");
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   237
  _entry_cache = new G1StringDedupEntryCache(_min_size * _max_cache_factor);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   238
  _table = new G1StringDedupTable(_min_size);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   239
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   240
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   241
void G1StringDedupTable::add(typeArrayOop value, bool latin1, unsigned int hash, G1StringDedupEntry** list) {
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   242
  G1StringDedupEntry* entry = _entry_cache->alloc();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   243
  entry->set_obj(value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   244
  entry->set_hash(hash);
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   245
  entry->set_latin1(latin1);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   246
  entry->set_next(*list);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   247
  *list = entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   248
  _entries++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   249
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   250
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   251
void G1StringDedupTable::remove(G1StringDedupEntry** pentry, uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   252
  G1StringDedupEntry* entry = *pentry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   253
  *pentry = entry->next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   254
  _entry_cache->free(entry, worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   255
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   256
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   257
void G1StringDedupTable::transfer(G1StringDedupEntry** pentry, G1StringDedupTable* dest) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   258
  G1StringDedupEntry* entry = *pentry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   259
  *pentry = entry->next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   260
  unsigned int hash = entry->hash();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   261
  size_t index = dest->hash_to_index(hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   262
  G1StringDedupEntry** list = dest->bucket(index);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   263
  entry->set_next(*list);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   264
  *list = entry;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   265
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   266
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   267
bool G1StringDedupTable::equals(typeArrayOop value1, typeArrayOop value2) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   268
  return (value1 == value2 ||
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   269
          (value1->length() == value2->length() &&
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   270
           (!memcmp(value1->base(T_BYTE),
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   271
                    value2->base(T_BYTE),
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   272
                    value1->length() * sizeof(jbyte)))));
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   273
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   274
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   275
typeArrayOop G1StringDedupTable::lookup(typeArrayOop value, bool latin1, unsigned int hash,
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   276
                                        G1StringDedupEntry** list, uintx &count) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   277
  for (G1StringDedupEntry* entry = *list; entry != NULL; entry = entry->next()) {
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   278
    if (entry->hash() == hash && entry->latin1() == latin1) {
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   279
      typeArrayOop existing_value = entry->obj();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   280
      if (equals(value, existing_value)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   281
        // Match found
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   282
        return existing_value;
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
    count++;
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
  // Not found
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   289
  return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   290
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   291
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   292
typeArrayOop G1StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool latin1, unsigned int hash) {
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   293
  size_t index = hash_to_index(hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   294
  G1StringDedupEntry** list = bucket(index);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   295
  uintx count = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   296
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   297
  // Lookup in list
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   298
  typeArrayOop existing_value = lookup(value, latin1, hash, list, count);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   299
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   300
  // Check if rehash is needed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   301
  if (count > _rehash_threshold) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   302
    _rehash_needed = true;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   303
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   304
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   305
  if (existing_value == NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   306
    // Not found, add new entry
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   307
    add(value, latin1, hash, list);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   308
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   309
    // Update statistics
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   310
    _entries_added++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   311
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   312
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   313
  return existing_value;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   314
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   315
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   316
unsigned int G1StringDedupTable::hash_code(typeArrayOop value, bool latin1) {
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   317
  unsigned int hash;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   318
  int length = value->length();
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   319
  if (latin1) {
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   320
    const jbyte* data = (jbyte*)value->base(T_BYTE);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   321
    if (use_java_hash()) {
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   322
      hash = java_lang_String::hash_code(data, length);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   323
    } else {
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   324
      hash = AltHashing::murmur3_32(_table->_hash_seed, data, length);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   325
    }
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   326
  } else {
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   327
    length /= sizeof(jchar) / sizeof(jbyte); // Convert number of bytes to number of chars
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   328
    const jchar* data = (jchar*)value->base(T_CHAR);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   329
    if (use_java_hash()) {
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   330
      hash = java_lang_String::hash_code(data, length);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   331
    } else {
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   332
      hash = AltHashing::murmur3_32(_table->_hash_seed, data, length);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   333
    }
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   334
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   335
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   336
  return hash;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   337
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   338
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   339
void G1StringDedupTable::deduplicate(oop java_string, G1StringDedupStat& stat) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   340
  assert(java_lang_String::is_instance(java_string), "Must be a string");
35492
c8c0273e6b91 8146690: Make all classes in GC follow the naming convention.
david
parents: 35061
diff changeset
   341
  NoSafepointVerifier nsv;
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   342
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   343
  stat.inc_inspected();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   344
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   345
  typeArrayOop value = java_lang_String::value(java_string);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   346
  if (value == NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   347
    // String has no value
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   348
    stat.inc_skipped();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   349
    return;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   350
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   351
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   352
  bool latin1 = java_lang_String::is_latin1(java_string);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   353
  unsigned int hash = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   354
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   355
  if (use_java_hash()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   356
    // Get hash code from cache
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   357
    hash = java_lang_String::hash(java_string);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   358
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   359
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   360
  if (hash == 0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   361
    // Compute hash
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   362
    hash = hash_code(value, latin1);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   363
    stat.inc_hashed();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   364
30619
f02421441c2e 8079840: G1StringDedupTable::deduplicate() reset String hash value unnecessarily.
jiangli
parents: 30175
diff changeset
   365
    if (use_java_hash() && hash != 0) {
f02421441c2e 8079840: G1StringDedupTable::deduplicate() reset String hash value unnecessarily.
jiangli
parents: 30175
diff changeset
   366
      // Store hash code in cache
f02421441c2e 8079840: G1StringDedupTable::deduplicate() reset String hash value unnecessarily.
jiangli
parents: 30175
diff changeset
   367
      java_lang_String::set_hash(java_string, hash);
f02421441c2e 8079840: G1StringDedupTable::deduplicate() reset String hash value unnecessarily.
jiangli
parents: 30175
diff changeset
   368
    }
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   369
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   370
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   371
  typeArrayOop existing_value = lookup_or_add(value, latin1, hash);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   372
  if (existing_value == value) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   373
    // Same value, already known
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   374
    stat.inc_known();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   375
    return;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   376
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   377
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   378
  // Get size of value array
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   379
  uintx size_in_bytes = value->size() * HeapWordSize;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   380
  stat.inc_new(size_in_bytes);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   381
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   382
  if (existing_value != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   383
    // Enqueue the reference to make sure it is kept alive. Concurrent mark might
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   384
    // otherwise declare it dead if there are no other strong references to this object.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   385
    G1SATBCardTableModRefBS::enqueue(existing_value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   386
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   387
    // Existing value found, deduplicate string
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   388
    java_lang_String::set_value(java_string, existing_value);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   389
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   390
    if (G1CollectedHeap::heap()->is_in_young(value)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   391
      stat.inc_deduped_young(size_in_bytes);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   392
    } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   393
      stat.inc_deduped_old(size_in_bytes);
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
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   397
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   398
G1StringDedupTable* G1StringDedupTable::prepare_resize() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   399
  size_t size = _table->_size;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   400
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   401
  // Check if the hashtable needs to be resized
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   402
  if (_table->_entries > _table->_grow_threshold) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   403
    // Grow table, double the size
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   404
    size *= 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   405
    if (size > _max_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   406
      // Too big, don't resize
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   407
      return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   408
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   409
  } else if (_table->_entries < _table->_shrink_threshold) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   410
    // Shrink table, half the size
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   411
    size /= 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   412
    if (size < _min_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   413
      // Too small, don't resize
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   414
      return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   415
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   416
  } else if (StringDeduplicationResizeALot) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   417
    // Force grow
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   418
    size *= 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   419
    if (size > _max_size) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   420
      // Too big, force shrink instead
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   421
      size /= 4;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   422
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   423
  } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   424
    // Resize not needed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   425
    return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   426
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   427
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   428
  // Update statistics
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   429
  _resize_count++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   430
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   431
  // Update max cache size
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   432
  _entry_cache->set_max_size(size * _max_cache_factor);
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   433
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   434
  // Allocate the new table. The new table will be populated by workers
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   435
  // calling unlink_or_oops_do() and finally installed by finish_resize().
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   436
  return new G1StringDedupTable(size, _table->_hash_seed);
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
void G1StringDedupTable::finish_resize(G1StringDedupTable* resized_table) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   440
  assert(resized_table != NULL, "Invalid table");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   441
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   442
  resized_table->_entries = _table->_entries;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   443
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   444
  // Free old table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   445
  delete _table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   446
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   447
  // Install new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   448
  _table = resized_table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   449
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   450
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   451
void G1StringDedupTable::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   452
  // The table is divided into partitions to allow lock-less parallel processing by
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   453
  // multiple worker threads. A worker thread first claims a partition, which ensures
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   454
  // 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
   455
  // 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
   456
  // thread processes all partitions where entries will hash to the same destination
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   457
  // 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
   458
  // 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
   459
  // other partition whoes entries will hash to the same destination partition. That
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   460
  // other partition is always the sibling partition in the second half of the table.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   461
  // 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
   462
  // is partition 4, the sibling of partition 1 is partition 5, etc.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   463
  size_t table_half = _table->_size / 2;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   464
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   465
  // Let each partition be one page worth of buckets
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   466
  size_t partition_size = MIN2(table_half, os::vm_page_size() / sizeof(G1StringDedupEntry*));
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   467
  assert(table_half % partition_size == 0, "Invalid partition size");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   468
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   469
  // Number of entries removed during the scan
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   470
  uintx removed = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   471
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   472
  for (;;) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   473
    // Grab next partition to scan
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   474
    size_t partition_begin = cl->claim_table_partition(partition_size);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   475
    size_t partition_end = partition_begin + partition_size;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   476
    if (partition_begin >= table_half) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   477
      // End of table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   478
      break;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   479
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   480
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   481
    // 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
   482
    removed += unlink_or_oops_do(cl, partition_begin, partition_end, worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   483
    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
   484
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   485
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   486
  // Delayed update to avoid contention on the table lock
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   487
  if (removed > 0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   488
    MutexLockerEx ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   489
    _table->_entries -= removed;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   490
    _entries_removed += removed;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   491
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   492
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   493
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   494
uintx G1StringDedupTable::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   495
                                            size_t partition_begin,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   496
                                            size_t partition_end,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   497
                                            uint worker_id) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   498
  uintx removed = 0;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   499
  for (size_t bucket = partition_begin; bucket < partition_end; 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
      oop* p = (oop*)(*entry)->obj_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   503
      if (cl->is_alive(*p)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   504
        cl->keep_alive(p);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   505
        if (cl->is_resizing()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   506
          // We are resizing the table, transfer entry to the new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   507
          _table->transfer(entry, cl->resized_table());
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   508
        } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   509
          if (cl->is_rehashing()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   510
            // We are rehashing the table, rehash the entry but keep it
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   511
            // in the table. We can't transfer entries into the new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   512
            // at this point since we don't have exclusive access to all
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   513
            // destination partitions. finish_rehash() will do a single
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   514
            // threaded transfer of all entries.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   515
            typeArrayOop value = (typeArrayOop)*p;
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   516
            bool latin1 = (*entry)->latin1();
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   517
            unsigned int hash = hash_code(value, latin1);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   518
            (*entry)->set_hash(hash);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   519
          }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   520
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   521
          // Move to next entry
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   522
          entry = (*entry)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   523
        }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   524
      } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   525
        // Not alive, remove entry from table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   526
        _table->remove(entry, worker_id);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   527
        removed++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   528
      }
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
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   532
  return removed;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   533
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   534
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   535
G1StringDedupTable* G1StringDedupTable::prepare_rehash() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   536
  if (!_table->_rehash_needed && !StringDeduplicationRehashALot) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   537
    // Rehash not needed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   538
    return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   539
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   540
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   541
  // Update statistics
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   542
  _rehash_count++;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   543
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   544
  // Compute new hash seed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   545
  _table->_hash_seed = AltHashing::compute_seed();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   546
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   547
  // Allocate the new table, same size and hash seed
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   548
  return new G1StringDedupTable(_table->_size, _table->_hash_seed);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   549
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   550
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   551
void G1StringDedupTable::finish_rehash(G1StringDedupTable* rehashed_table) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   552
  assert(rehashed_table != NULL, "Invalid table");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   553
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   554
  // Move all newly rehashed entries into the correct buckets in the new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   555
  for (size_t bucket = 0; bucket < _table->_size; bucket++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   556
    G1StringDedupEntry** entry = _table->bucket(bucket);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   557
    while (*entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   558
      _table->transfer(entry, rehashed_table);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   559
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   560
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   561
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   562
  rehashed_table->_entries = _table->_entries;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   563
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   564
  // Free old table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   565
  delete _table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   566
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   567
  // Install new table
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   568
  _table = rehashed_table;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   569
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   570
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   571
void G1StringDedupTable::verify() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   572
  for (size_t bucket = 0; bucket < _table->_size; bucket++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   573
    // Verify entries
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   574
    G1StringDedupEntry** entry = _table->bucket(bucket);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   575
    while (*entry != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   576
      typeArrayOop value = (*entry)->obj();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   577
      guarantee(value != NULL, "Object must not be NULL");
30173
13cf7580b000 8077413: Avoid use of Universe::heap() inside collectors
pliden
parents: 29081
diff changeset
   578
      guarantee(G1CollectedHeap::heap()->is_in_reserved(value), "Object must be on the heap");
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   579
      guarantee(!value->is_forwarded(), "Object must not be forwarded");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   580
      guarantee(value->is_typeArray(), "Object must be a typeArrayOop");
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   581
      bool latin1 = (*entry)->latin1();
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   582
      unsigned int hash = hash_code(value, latin1);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   583
      guarantee((*entry)->hash() == hash, "Table entry has inorrect hash");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   584
      guarantee(_table->hash_to_index(hash) == bucket, "Table entry has incorrect index");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   585
      entry = (*entry)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   586
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   587
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   588
    // Verify that we do not have entries with identical oops or identical arrays.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   589
    // 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
   590
    // identical array has been inserted more than once into different/incorrect
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   591
    // buckets the verification step above will catch that.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   592
    G1StringDedupEntry** entry1 = _table->bucket(bucket);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   593
    while (*entry1 != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   594
      typeArrayOop value1 = (*entry1)->obj();
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   595
      bool latin1_1 = (*entry1)->latin1();
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   596
      G1StringDedupEntry** entry2 = (*entry1)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   597
      while (*entry2 != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   598
        typeArrayOop value2 = (*entry2)->obj();
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   599
        bool latin1_2 = (*entry2)->latin1();
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 31592
diff changeset
   600
        guarantee(latin1_1 != latin1_2 || !equals(value1, value2), "Table entries must not have identical arrays");
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   601
        entry2 = (*entry2)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   602
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   603
      entry1 = (*entry1)->next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   604
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   605
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   606
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   607
39452
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   608
void G1StringDedupTable::clean_entry_cache() {
eebe070746ad 8158871: Long response times with G1 and StringDeduplication
pliden
parents: 37242
diff changeset
   609
  _entry_cache->delete_overflowed();
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   610
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   611
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 33628
diff changeset
   612
void G1StringDedupTable::print_statistics() {
37242
91e5f98fff6f 8152632: Rename LogHandle(...) to Log(...)
stefank
parents: 35492
diff changeset
   613
  Log(gc, stringdedup) log;
39690
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   614
  log.debug("  Table");
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   615
  log.debug("    Memory Usage: " G1_STRDEDUP_BYTES_FORMAT_NS,
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 33628
diff changeset
   616
            G1_STRDEDUP_BYTES_PARAM(_table->_size * sizeof(G1StringDedupEntry*) + (_table->_entries + _entry_cache->size()) * sizeof(G1StringDedupEntry)));
39690
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   617
  log.debug("    Size: " SIZE_FORMAT ", Min: " SIZE_FORMAT ", Max: " SIZE_FORMAT, _table->_size, _min_size, _max_size);
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   618
  log.debug("    Entries: " UINTX_FORMAT ", Load: " G1_STRDEDUP_PERCENT_FORMAT_NS ", Cached: " UINTX_FORMAT ", Added: " UINTX_FORMAT ", Removed: " UINTX_FORMAT,
47679
4cfcb7be4984 8189666: Replace various inlined percentage calculations with global percent_of()
tschatzl
parents: 47216
diff changeset
   619
            _table->_entries, percent_of(_table->_entries, _table->_size), _entry_cache->size(), _entries_added, _entries_removed);
39690
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   620
  log.debug("    Resize Count: " UINTX_FORMAT ", Shrink Threshold: " UINTX_FORMAT "(" G1_STRDEDUP_PERCENT_FORMAT_NS "), Grow Threshold: " UINTX_FORMAT "(" G1_STRDEDUP_PERCENT_FORMAT_NS ")",
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 33628
diff changeset
   621
            _resize_count, _table->_shrink_threshold, _shrink_load_factor * 100.0, _table->_grow_threshold, _grow_load_factor * 100.0);
39690
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   622
  log.debug("    Rehash Count: " UINTX_FORMAT ", Rehash Threshold: " UINTX_FORMAT ", Hash Seed: 0x%x", _rehash_count, _rehash_threshold, _table->_hash_seed);
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 39452
diff changeset
   623
  log.debug("    Age Threshold: " UINTX_FORMAT, StringDeduplicationAgeThreshold);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   624
}