hotspot/src/share/vm/gc_implementation/g1/g1StringDedupQueue.cpp
author coleenp
Fri, 13 Mar 2015 18:59:41 +0000
changeset 29577 bb06d25e302d
parent 29081 c61eb4914428
child 30173 13cf7580b000
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     1
/*
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 25351
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     4
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     8
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    13
 * accompanied this code).
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    14
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    18
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    21
 * questions.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    22
 *
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    23
 */
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    24
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    25
#include "precompiled.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 25351
diff changeset
    26
#include "classfile/javaClasses.inline.hpp"
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 25351
diff changeset
    27
#include "gc_implementation/g1/g1StringDedup.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    28
#include "gc_implementation/g1/g1StringDedupQueue.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    29
#include "memory/gcLocker.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 25351
diff changeset
    30
#include "oops/oop.inline.hpp"
25351
7c198a690050 8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents: 24093
diff changeset
    31
#include "runtime/atomic.inline.hpp"
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    32
#include "runtime/mutexLocker.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    33
#include "utilities/stack.inline.hpp"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    34
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    35
G1StringDedupQueue* G1StringDedupQueue::_queue = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    36
const size_t        G1StringDedupQueue::_max_size = 1000000; // Max number of elements per queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    37
const size_t        G1StringDedupQueue::_max_cache_size = 0; // Max cache size per queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    38
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    39
G1StringDedupQueue::G1StringDedupQueue() :
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    40
  _cursor(0),
24093
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    41
  _cancel(false),
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    42
  _empty(true),
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    43
  _dropped(0) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    44
  _nqueues = MAX2(ParallelGCThreads, (size_t)1);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    45
  _queues = NEW_C_HEAP_ARRAY(G1StringDedupWorkerQueue, _nqueues, mtGC);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    46
  for (size_t i = 0; i < _nqueues; i++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    47
    new (_queues + i) G1StringDedupWorkerQueue(G1StringDedupWorkerQueue::default_segment_size(), _max_cache_size, _max_size);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    48
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    49
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    50
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    51
G1StringDedupQueue::~G1StringDedupQueue() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    52
  ShouldNotReachHere();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    53
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    54
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    55
void G1StringDedupQueue::create() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    56
  assert(_queue == NULL, "One string deduplication queue allowed");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    57
  _queue = new G1StringDedupQueue();
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
void G1StringDedupQueue::wait() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    61
  MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag);
24093
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    62
  while (_queue->_empty && !_queue->_cancel) {
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    63
    ml.wait(Mutex::_no_safepoint_check_flag);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    64
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    65
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    66
24093
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    67
void G1StringDedupQueue::cancel_wait() {
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    68
  MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag);
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    69
  _queue->_cancel = true;
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    70
  ml.notify();
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    71
}
095cc0a63ed9 8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
pliden
parents: 23472
diff changeset
    72
23472
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    73
void G1StringDedupQueue::push(uint worker_id, oop java_string) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    74
  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    75
  assert(worker_id < _queue->_nqueues, "Invalid queue");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    76
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    77
  // Push and notify waiter
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    78
  G1StringDedupWorkerQueue& worker_queue = _queue->_queues[worker_id];
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    79
  if (!worker_queue.is_full()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    80
    worker_queue.push(java_string);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    81
    if (_queue->_empty) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    82
      MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag);
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
        // Mark non-empty and notify waiter
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    85
        _queue->_empty = false;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    86
        ml.notify();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    87
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    88
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    89
  } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    90
    // Queue is full, drop the string and update the statistics
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    91
    Atomic::inc_ptr(&_queue->_dropped);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    92
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    93
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    94
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    95
oop G1StringDedupQueue::pop() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    96
  assert(!SafepointSynchronize::is_at_safepoint(), "Must not be at safepoint");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    97
  No_Safepoint_Verifier nsv;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    98
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
    99
  // Try all queues before giving up
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   100
  for (size_t tries = 0; tries < _queue->_nqueues; tries++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   101
    // The cursor indicates where we left of last time
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   102
    G1StringDedupWorkerQueue* queue = &_queue->_queues[_queue->_cursor];
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   103
    while (!queue->is_empty()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   104
      oop obj = queue->pop();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   105
      // The oop we pop can be NULL if it was marked
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   106
      // dead. Just ignore those and pop the next oop.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   107
      if (obj != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   108
        return obj;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   109
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   110
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   111
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   112
    // Try next queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   113
    _queue->_cursor = (_queue->_cursor + 1) % _queue->_nqueues;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   114
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   115
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   116
  // Mark empty
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   117
  _queue->_empty = true;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   118
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   119
  return NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   120
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   121
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   122
void G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   123
  // A worker thread first claims a queue, which ensures exclusive
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   124
  // access to that queue, then continues to process it.
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   125
  for (;;) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   126
    // Grab next queue to scan
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   127
    size_t queue = cl->claim_queue();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   128
    if (queue >= _queue->_nqueues) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   129
      // End of queues
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   130
      break;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   131
    }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   132
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   133
    // Scan the queue
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   134
    unlink_or_oops_do(cl, queue);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   135
  }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   136
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   137
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   138
void G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, size_t queue) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   139
  assert(queue < _queue->_nqueues, "Invalid queue");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   140
  StackIterator<oop, mtGC> iter(_queue->_queues[queue]);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   141
  while (!iter.is_empty()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   142
    oop* p = iter.next_addr();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   143
    if (*p != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   144
      if (cl->is_alive(*p)) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   145
        cl->keep_alive(p);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   146
      } else {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   147
        // Clear dead reference
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   148
        *p = NULL;
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   149
      }
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   150
    }
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
void G1StringDedupQueue::print_statistics(outputStream* st) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   155
  st->print_cr(
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   156
    "   [Queue]\n"
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   157
    "      [Dropped: "UINTX_FORMAT"]", _queue->_dropped);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   158
}
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   159
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   160
void G1StringDedupQueue::verify() {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   161
  for (size_t i = 0; i < _queue->_nqueues; i++) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   162
    StackIterator<oop, mtGC> iter(_queue->_queues[i]);
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   163
    while (!iter.is_empty()) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   164
      oop obj = iter.next();
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   165
      if (obj != NULL) {
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   166
        guarantee(Universe::heap()->is_in_reserved(obj), "Object must be on the heap");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   167
        guarantee(!obj->is_forwarded(), "Object must not be forwarded");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   168
        guarantee(java_lang_String::is_instance(obj), "Object must be a String");
35e93890ed88 8029075: String deduplication in G1
pliden
parents:
diff changeset
   169
      }
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
}