src/hotspot/share/gc/g1/g1StringDedupQueue.cpp
author stefank
Fri, 23 Mar 2018 18:54:12 +0100
changeset 49594 898ef81cbc0e
parent 47552 8a3599d60996
child 50574 fa727a4d7934
permissions -rw-r--r--
8200106: Move NoSafepointVerifier out from gcLocker.hpp Reviewed-by: coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     1
/*
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
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"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 25351
diff changeset
    26
#include "classfile/javaClasses.inline.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30173
diff changeset
    27
#include "gc/g1/g1CollectedHeap.hpp"
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30173
diff changeset
    28
#include "gc/g1/g1StringDedup.hpp"
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30173
diff changeset
    29
#include "gc/g1/g1StringDedupQueue.hpp"
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 31592
diff changeset
    30
#include "logging/log.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 25351
diff changeset
    31
#include "oops/oop.inline.hpp"
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39690
diff changeset
    32
#include "runtime/atomic.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    33
#include "runtime/mutexLocker.hpp"
49594
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47552
diff changeset
    34
#include "runtime/safepointVerifiers.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    35
#include "utilities/stack.inline.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    36
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    37
G1StringDedupQueue* G1StringDedupQueue::_queue = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    38
const size_t        G1StringDedupQueue::_max_size = 1000000; // Max number of elements per queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    39
const size_t        G1StringDedupQueue::_max_cache_size = 0; // Max cache size per queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    40
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    41
G1StringDedupQueue::G1StringDedupQueue() :
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    42
  _cursor(0),
24093
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    43
  _cancel(false),
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    44
  _empty(true),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    45
  _dropped(0) {
30876
44a71334fd94 8080876: Replace unnecessary MAX2(ParallelGCThreads, 1) calls with ParallelGCThreads
stefank
parents: 30764
diff changeset
    46
  _nqueues = ParallelGCThreads;
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    47
  _queues = NEW_C_HEAP_ARRAY(G1StringDedupWorkerQueue, _nqueues, mtGC);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    48
  for (size_t i = 0; i < _nqueues; i++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    49
    new (_queues + i) G1StringDedupWorkerQueue(G1StringDedupWorkerQueue::default_segment_size(), _max_cache_size, _max_size);
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
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    53
G1StringDedupQueue::~G1StringDedupQueue() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    54
  ShouldNotReachHere();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    55
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    56
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    57
void G1StringDedupQueue::create() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    58
  assert(_queue == NULL, "One string deduplication queue allowed");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    59
  _queue = new G1StringDedupQueue();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    60
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    61
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    62
void G1StringDedupQueue::wait() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    63
  MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag);
24093
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    64
  while (_queue->_empty && !_queue->_cancel) {
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    65
    ml.wait(Mutex::_no_safepoint_check_flag);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    66
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    67
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    68
24093
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    69
void G1StringDedupQueue::cancel_wait() {
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    70
  MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag);
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    71
  _queue->_cancel = true;
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    72
  ml.notify();
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    73
}
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    74
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    75
void G1StringDedupQueue::push(uint worker_id, oop java_string) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    76
  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    77
  assert(worker_id < _queue->_nqueues, "Invalid queue");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    78
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    79
  // Push and notify waiter
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    80
  G1StringDedupWorkerQueue& worker_queue = _queue->_queues[worker_id];
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    81
  if (!worker_queue.is_full()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    82
    worker_queue.push(java_string);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    83
    if (_queue->_empty) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    84
      MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    85
      if (_queue->_empty) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    86
        // Mark non-empty and notify waiter
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    87
        _queue->_empty = false;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    88
        ml.notify();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    89
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    90
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    91
  } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    92
    // Queue is full, drop the string and update the statistics
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
    93
    Atomic::inc(&_queue->_dropped);
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    94
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    95
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    96
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    97
oop G1StringDedupQueue::pop() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    98
  assert(!SafepointSynchronize::is_at_safepoint(), "Must not be at safepoint");
35492
c8c0273e6b91 8146690: Make all classes in GC follow the naming convention.
david
parents: 35061
diff changeset
    99
  NoSafepointVerifier nsv;
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   100
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   101
  // Try all queues before giving up
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   102
  for (size_t tries = 0; tries < _queue->_nqueues; tries++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   103
    // The cursor indicates where we left of last time
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   104
    G1StringDedupWorkerQueue* queue = &_queue->_queues[_queue->_cursor];
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   105
    while (!queue->is_empty()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   106
      oop obj = queue->pop();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   107
      // The oop we pop can be NULL if it was marked
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   108
      // dead. Just ignore those and pop the next oop.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   109
      if (obj != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   110
        return obj;
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
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   114
    // Try next queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   115
    _queue->_cursor = (_queue->_cursor + 1) % _queue->_nqueues;
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
  // Mark empty
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   119
  _queue->_empty = true;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   120
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   121
  return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   122
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   123
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   124
void G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   125
  // A worker thread first claims a queue, which ensures exclusive
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   126
  // access to that queue, then continues to process it.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   127
  for (;;) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   128
    // Grab next queue to scan
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   129
    size_t queue = cl->claim_queue();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   130
    if (queue >= _queue->_nqueues) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   131
      // End of queues
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   132
      break;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   133
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   134
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   135
    // Scan the queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   136
    unlink_or_oops_do(cl, queue);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   137
  }
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
void G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, size_t queue) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   141
  assert(queue < _queue->_nqueues, "Invalid queue");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   142
  StackIterator<oop, mtGC> iter(_queue->_queues[queue]);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   143
  while (!iter.is_empty()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   144
    oop* p = iter.next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   145
    if (*p != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   146
      if (cl->is_alive(*p)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   147
        cl->keep_alive(p);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   148
      } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   149
        // Clear dead reference
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   150
        *p = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   151
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   152
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   153
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   154
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   155
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 31592
diff changeset
   156
void G1StringDedupQueue::print_statistics() {
39690
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 35492
diff changeset
   157
  log_debug(gc, stringdedup)("  Queue");
09a3ee292336 8159974: G1 String deduplication logging not aligned with the rest of G1
pliden
parents: 35492
diff changeset
   158
  log_debug(gc, stringdedup)("    Dropped: " UINTX_FORMAT, _queue->_dropped);
23472
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
void G1StringDedupQueue::verify() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   162
  for (size_t i = 0; i < _queue->_nqueues; i++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   163
    StackIterator<oop, mtGC> iter(_queue->_queues[i]);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   164
    while (!iter.is_empty()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   165
      oop obj = iter.next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   166
      if (obj != NULL) {
30173
13cf7580b000 8077413: Avoid use of Universe::heap() inside collectors
pliden
parents: 29081
diff changeset
   167
        guarantee(G1CollectedHeap::heap()->is_in_reserved(obj), "Object must be on the heap");
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   168
        guarantee(!obj->is_forwarded(), "Object must not be forwarded");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   169
        guarantee(java_lang_String::is_instance(obj), "Object must be a String");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   170
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   171
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   172
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   173
}