1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1StringDedupQueue.cpp Tue Mar 18 19:07:22 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "classfile/javaClasses.hpp" 1.30 +#include "gc_implementation/g1/g1StringDedupQueue.hpp" 1.31 +#include "memory/gcLocker.hpp" 1.32 +#include "runtime/mutexLocker.hpp" 1.33 +#include "utilities/stack.inline.hpp" 1.34 + 1.35 +G1StringDedupQueue* G1StringDedupQueue::_queue = NULL; 1.36 +const size_t G1StringDedupQueue::_max_size = 1000000; // Max number of elements per queue 1.37 +const size_t G1StringDedupQueue::_max_cache_size = 0; // Max cache size per queue 1.38 + 1.39 +G1StringDedupQueue::G1StringDedupQueue() : 1.40 + _cursor(0), 1.41 + _empty(true), 1.42 + _dropped(0) { 1.43 + _nqueues = MAX2(ParallelGCThreads, (size_t)1); 1.44 + _queues = NEW_C_HEAP_ARRAY(G1StringDedupWorkerQueue, _nqueues, mtGC); 1.45 + for (size_t i = 0; i < _nqueues; i++) { 1.46 + new (_queues + i) G1StringDedupWorkerQueue(G1StringDedupWorkerQueue::default_segment_size(), _max_cache_size, _max_size); 1.47 + } 1.48 +} 1.49 + 1.50 +G1StringDedupQueue::~G1StringDedupQueue() { 1.51 + ShouldNotReachHere(); 1.52 +} 1.53 + 1.54 +void G1StringDedupQueue::create() { 1.55 + assert(_queue == NULL, "One string deduplication queue allowed"); 1.56 + _queue = new G1StringDedupQueue(); 1.57 +} 1.58 + 1.59 +void G1StringDedupQueue::wait() { 1.60 + MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag); 1.61 + while (_queue->_empty) { 1.62 + ml.wait(Mutex::_no_safepoint_check_flag); 1.63 + } 1.64 +} 1.65 + 1.66 +void G1StringDedupQueue::push(uint worker_id, oop java_string) { 1.67 + assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint"); 1.68 + assert(worker_id < _queue->_nqueues, "Invalid queue"); 1.69 + 1.70 + // Push and notify waiter 1.71 + G1StringDedupWorkerQueue& worker_queue = _queue->_queues[worker_id]; 1.72 + if (!worker_queue.is_full()) { 1.73 + worker_queue.push(java_string); 1.74 + if (_queue->_empty) { 1.75 + MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag); 1.76 + if (_queue->_empty) { 1.77 + // Mark non-empty and notify waiter 1.78 + _queue->_empty = false; 1.79 + ml.notify(); 1.80 + } 1.81 + } 1.82 + } else { 1.83 + // Queue is full, drop the string and update the statistics 1.84 + Atomic::inc_ptr(&_queue->_dropped); 1.85 + } 1.86 +} 1.87 + 1.88 +oop G1StringDedupQueue::pop() { 1.89 + assert(!SafepointSynchronize::is_at_safepoint(), "Must not be at safepoint"); 1.90 + No_Safepoint_Verifier nsv; 1.91 + 1.92 + // Try all queues before giving up 1.93 + for (size_t tries = 0; tries < _queue->_nqueues; tries++) { 1.94 + // The cursor indicates where we left of last time 1.95 + G1StringDedupWorkerQueue* queue = &_queue->_queues[_queue->_cursor]; 1.96 + while (!queue->is_empty()) { 1.97 + oop obj = queue->pop(); 1.98 + // The oop we pop can be NULL if it was marked 1.99 + // dead. Just ignore those and pop the next oop. 1.100 + if (obj != NULL) { 1.101 + return obj; 1.102 + } 1.103 + } 1.104 + 1.105 + // Try next queue 1.106 + _queue->_cursor = (_queue->_cursor + 1) % _queue->_nqueues; 1.107 + } 1.108 + 1.109 + // Mark empty 1.110 + _queue->_empty = true; 1.111 + 1.112 + return NULL; 1.113 +} 1.114 + 1.115 +void G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl) { 1.116 + // A worker thread first claims a queue, which ensures exclusive 1.117 + // access to that queue, then continues to process it. 1.118 + for (;;) { 1.119 + // Grab next queue to scan 1.120 + size_t queue = cl->claim_queue(); 1.121 + if (queue >= _queue->_nqueues) { 1.122 + // End of queues 1.123 + break; 1.124 + } 1.125 + 1.126 + // Scan the queue 1.127 + unlink_or_oops_do(cl, queue); 1.128 + } 1.129 +} 1.130 + 1.131 +void G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, size_t queue) { 1.132 + assert(queue < _queue->_nqueues, "Invalid queue"); 1.133 + StackIterator<oop, mtGC> iter(_queue->_queues[queue]); 1.134 + while (!iter.is_empty()) { 1.135 + oop* p = iter.next_addr(); 1.136 + if (*p != NULL) { 1.137 + if (cl->is_alive(*p)) { 1.138 + cl->keep_alive(p); 1.139 + } else { 1.140 + // Clear dead reference 1.141 + *p = NULL; 1.142 + } 1.143 + } 1.144 + } 1.145 +} 1.146 + 1.147 +void G1StringDedupQueue::print_statistics(outputStream* st) { 1.148 + st->print_cr( 1.149 + " [Queue]\n" 1.150 + " [Dropped: "UINTX_FORMAT"]", _queue->_dropped); 1.151 +} 1.152 + 1.153 +void G1StringDedupQueue::verify() { 1.154 + for (size_t i = 0; i < _queue->_nqueues; i++) { 1.155 + StackIterator<oop, mtGC> iter(_queue->_queues[i]); 1.156 + while (!iter.is_empty()) { 1.157 + oop obj = iter.next(); 1.158 + if (obj != NULL) { 1.159 + guarantee(Universe::heap()->is_in_reserved(obj), "Object must be on the heap"); 1.160 + guarantee(!obj->is_forwarded(), "Object must not be forwarded"); 1.161 + guarantee(java_lang_String::is_instance(obj), "Object must be a String"); 1.162 + } 1.163 + } 1.164 + } 1.165 +}