g1StringDedupQueue.cpp revision 11857:d0fbf661cc16
1193323Sed/* 2193323Sed * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4193323Sed * 5193323Sed * This code is free software; you can redistribute it and/or modify it 6193323Sed * under the terms of the GNU General Public License version 2 only, as 7193323Sed * published by the Free Software Foundation. 8193323Sed * 9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT 10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11193323Sed * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12193323Sed * version 2 for more details (a copy is included in the LICENSE file that 13193323Sed * accompanied this code). 14193323Sed * 15193323Sed * You should have received a copy of the GNU General Public License version 16249423Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18234982Sdim * 19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20234353Sdim * or visit www.oracle.com if you need additional information or have any 21249423Sdim * questions. 22249423Sdim * 23234353Sdim */ 24234353Sdim 25193323Sed#include "precompiled.hpp" 26193323Sed#include "classfile/javaClasses.inline.hpp" 27193323Sed#include "gc/g1/g1CollectedHeap.hpp" 28193323Sed#include "gc/g1/g1StringDedup.hpp" 29205218Srdivacky#include "gc/g1/g1StringDedupQueue.hpp" 30205218Srdivacky#include "gc/shared/gcLocker.hpp" 31193323Sed#include "logging/log.hpp" 32193323Sed#include "oops/oop.inline.hpp" 33263508Sdim#include "runtime/atomic.hpp" 34193323Sed#include "runtime/mutexLocker.hpp" 35193323Sed#include "utilities/stack.inline.hpp" 36263508Sdim 37193323SedG1StringDedupQueue* G1StringDedupQueue::_queue = NULL; 38193323Sedconst size_t G1StringDedupQueue::_max_size = 1000000; // Max number of elements per queue 39263508Sdimconst size_t G1StringDedupQueue::_max_cache_size = 0; // Max cache size per queue 40193323Sed 41205407SrdivackyG1StringDedupQueue::G1StringDedupQueue() : 42263508Sdim _cursor(0), 43205407Srdivacky _cancel(false), 44193323Sed _empty(true), 45205218Srdivacky _dropped(0) { 46205218Srdivacky _nqueues = ParallelGCThreads; 47205218Srdivacky _queues = NEW_C_HEAP_ARRAY(G1StringDedupWorkerQueue, _nqueues, mtGC); 48205218Srdivacky for (size_t i = 0; i < _nqueues; i++) { 49205218Srdivacky new (_queues + i) G1StringDedupWorkerQueue(G1StringDedupWorkerQueue::default_segment_size(), _max_cache_size, _max_size); 50205218Srdivacky } 51205218Srdivacky} 52205218Srdivacky 53205218SrdivackyG1StringDedupQueue::~G1StringDedupQueue() { 54205218Srdivacky ShouldNotReachHere(); 55205218Srdivacky} 56205218Srdivacky 57193323Sedvoid G1StringDedupQueue::create() { 58193323Sed assert(_queue == NULL, "One string deduplication queue allowed"); 59205218Srdivacky _queue = new G1StringDedupQueue(); 60249423Sdim} 61205218Srdivacky 62205218Srdivackyvoid G1StringDedupQueue::wait() { 63218893Sdim MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag); 64205218Srdivacky while (_queue->_empty && !_queue->_cancel) { 65205218Srdivacky ml.wait(Mutex::_no_safepoint_check_flag); 66205218Srdivacky } 67218893Sdim} 68206083Srdivacky 69205218Srdivackyvoid G1StringDedupQueue::cancel_wait() { 70206083Srdivacky MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag); 71193323Sed _queue->_cancel = true; 72193323Sed ml.notify(); 73205407Srdivacky} 74205407Srdivacky 75205407Srdivackyvoid G1StringDedupQueue::push(uint worker_id, oop java_string) { 76205407Srdivacky assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint"); 77205407Srdivacky assert(worker_id < _queue->_nqueues, "Invalid queue"); 78205407Srdivacky 79249423Sdim // Push and notify waiter 80205407Srdivacky G1StringDedupWorkerQueue& worker_queue = _queue->_queues[worker_id]; 81218893Sdim if (!worker_queue.is_full()) { 82243830Sdim worker_queue.push(java_string); 83243830Sdim if (_queue->_empty) { 84243830Sdim MonitorLockerEx ml(StringDedupQueue_lock, Mutex::_no_safepoint_check_flag); 85205407Srdivacky if (_queue->_empty) { 86205407Srdivacky // Mark non-empty and notify waiter 87205407Srdivacky _queue->_empty = false; 88205218Srdivacky ml.notify(); 89205407Srdivacky } 90243830Sdim } 91205407Srdivacky } else { 92218893Sdim // Queue is full, drop the string and update the statistics 93243830Sdim Atomic::inc_ptr(&_queue->_dropped); 94243830Sdim } 95205407Srdivacky} 96205407Srdivacky 97205407Srdivackyoop G1StringDedupQueue::pop() { 98205407Srdivacky assert(!SafepointSynchronize::is_at_safepoint(), "Must not be at safepoint"); 99205407Srdivacky NoSafepointVerifier nsv; 100205407Srdivacky 101218893Sdim // Try all queues before giving up 102205407Srdivacky for (size_t tries = 0; tries < _queue->_nqueues; tries++) { 103205407Srdivacky // The cursor indicates where we left of last time 104205407Srdivacky G1StringDedupWorkerQueue* queue = &_queue->_queues[_queue->_cursor]; 105205218Srdivacky while (!queue->is_empty()) { 106205218Srdivacky oop obj = queue->pop(); 107205218Srdivacky // The oop we pop can be NULL if it was marked 108205218Srdivacky // dead. Just ignore those and pop the next oop. 109205218Srdivacky if (obj != NULL) { 110205218Srdivacky return obj; 111205218Srdivacky } 112218893Sdim } 113205218Srdivacky 114205218Srdivacky // Try next queue 115205218Srdivacky _queue->_cursor = (_queue->_cursor + 1) % _queue->_nqueues; 116205218Srdivacky } 117205218Srdivacky 118205218Srdivacky // Mark empty 119205218Srdivacky _queue->_empty = true; 120205218Srdivacky 121218893Sdim return NULL; 122205218Srdivacky} 123205218Srdivacky 124205218Srdivackyvoid G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl) { 125205218Srdivacky // A worker thread first claims a queue, which ensures exclusive 126205218Srdivacky // access to that queue, then continues to process it. 127205218Srdivacky for (;;) { 128205218Srdivacky // Grab next queue to scan 129205218Srdivacky size_t queue = cl->claim_queue(); 130193323Sed if (queue >= _queue->_nqueues) { 131198090Srdivacky // End of queues 132205218Srdivacky break; 133205218Srdivacky } 134205407Srdivacky 135218893Sdim // Scan the queue 136205218Srdivacky unlink_or_oops_do(cl, queue); 137218893Sdim } 138205218Srdivacky} 139205218Srdivacky 140205218Srdivackyvoid G1StringDedupQueue::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, size_t queue) { 141205218Srdivacky assert(queue < _queue->_nqueues, "Invalid queue"); 142205218Srdivacky StackIterator<oop, mtGC> iter(_queue->_queues[queue]); 143205218Srdivacky while (!iter.is_empty()) { 144205218Srdivacky oop* p = iter.next_addr(); 145205218Srdivacky if (*p != NULL) { 146218893Sdim if (cl->is_alive(*p)) { 147205218Srdivacky cl->keep_alive(p); 148205218Srdivacky } else { 149205218Srdivacky // Clear dead reference 150198090Srdivacky *p = NULL; 151193323Sed } 152205218Srdivacky } 153205218Srdivacky } 154243830Sdim} 155205218Srdivacky 156243830Sdimvoid G1StringDedupQueue::print_statistics() { 157205218Srdivacky log_debug(gc, stringdedup)(" Queue"); 158218893Sdim log_debug(gc, stringdedup)(" Dropped: " UINTX_FORMAT, _queue->_dropped); 159205218Srdivacky} 160205218Srdivacky 161205218Srdivackyvoid G1StringDedupQueue::verify() { 162205218Srdivacky for (size_t i = 0; i < _queue->_nqueues; i++) { 163218893Sdim StackIterator<oop, mtGC> iter(_queue->_queues[i]); 164205218Srdivacky while (!iter.is_empty()) { 165218893Sdim oop obj = iter.next(); 166205218Srdivacky if (obj != NULL) { 167205218Srdivacky guarantee(G1CollectedHeap::heap()->is_in_reserved(obj), "Object must be on the heap"); 168205218Srdivacky guarantee(!obj->is_forwarded(), "Object must not be forwarded"); 169205218Srdivacky guarantee(java_lang_String::is_instance(obj), "Object must be a String"); 170205218Srdivacky } 171205218Srdivacky } 172205218Srdivacky } 173205218Srdivacky} 174205218Srdivacky