1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1StringDedup.cpp Tue Mar 18 19:07:22 2014 +0100 1.3 @@ -0,0 +1,208 @@ 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/g1CollectedHeap.inline.hpp" 1.31 +#include "gc_implementation/g1/g1GCPhaseTimes.hpp" 1.32 +#include "gc_implementation/g1/g1StringDedup.hpp" 1.33 +#include "gc_implementation/g1/g1StringDedupQueue.hpp" 1.34 +#include "gc_implementation/g1/g1StringDedupStat.hpp" 1.35 +#include "gc_implementation/g1/g1StringDedupTable.hpp" 1.36 +#include "gc_implementation/g1/g1StringDedupThread.hpp" 1.37 + 1.38 +bool G1StringDedup::_enabled = false; 1.39 + 1.40 +void G1StringDedup::initialize() { 1.41 + assert(UseG1GC, "String deduplication only available with G1"); 1.42 + if (UseStringDeduplication) { 1.43 + _enabled = true; 1.44 + G1StringDedupQueue::create(); 1.45 + G1StringDedupTable::create(); 1.46 + G1StringDedupThread::create(); 1.47 + } 1.48 +} 1.49 + 1.50 +bool G1StringDedup::is_candidate_from_mark(oop obj) { 1.51 + if (java_lang_String::is_instance(obj)) { 1.52 + bool from_young = G1CollectedHeap::heap()->heap_region_containing_raw(obj)->is_young(); 1.53 + if (from_young && obj->age() < StringDeduplicationAgeThreshold) { 1.54 + // Candidate found. String is being evacuated from young to old but has not 1.55 + // reached the deduplication age threshold, i.e. has not previously been a 1.56 + // candidate during its life in the young generation. 1.57 + return true; 1.58 + } 1.59 + } 1.60 + 1.61 + // Not a candidate 1.62 + return false; 1.63 +} 1.64 + 1.65 +void G1StringDedup::enqueue_from_mark(oop java_string) { 1.66 + assert(is_enabled(), "String deduplication not enabled"); 1.67 + if (is_candidate_from_mark(java_string)) { 1.68 + G1StringDedupQueue::push(0 /* worker_id */, java_string); 1.69 + } 1.70 +} 1.71 + 1.72 +bool G1StringDedup::is_candidate_from_evacuation(bool from_young, bool to_young, oop obj) { 1.73 + if (from_young && java_lang_String::is_instance(obj)) { 1.74 + if (to_young && obj->age() == StringDeduplicationAgeThreshold) { 1.75 + // Candidate found. String is being evacuated from young to young and just 1.76 + // reached the deduplication age threshold. 1.77 + return true; 1.78 + } 1.79 + if (!to_young && obj->age() < StringDeduplicationAgeThreshold) { 1.80 + // Candidate found. String is being evacuated from young to old but has not 1.81 + // reached the deduplication age threshold, i.e. has not previously been a 1.82 + // candidate during its life in the young generation. 1.83 + return true; 1.84 + } 1.85 + } 1.86 + 1.87 + // Not a candidate 1.88 + return false; 1.89 +} 1.90 + 1.91 +void G1StringDedup::enqueue_from_evacuation(bool from_young, bool to_young, uint worker_id, oop java_string) { 1.92 + assert(is_enabled(), "String deduplication not enabled"); 1.93 + if (is_candidate_from_evacuation(from_young, to_young, java_string)) { 1.94 + G1StringDedupQueue::push(worker_id, java_string); 1.95 + } 1.96 +} 1.97 + 1.98 +void G1StringDedup::deduplicate(oop java_string) { 1.99 + assert(is_enabled(), "String deduplication not enabled"); 1.100 + G1StringDedupStat dummy; // Statistics from this path is never used 1.101 + G1StringDedupTable::deduplicate(java_string, dummy); 1.102 +} 1.103 + 1.104 +void G1StringDedup::oops_do(OopClosure* keep_alive) { 1.105 + assert(is_enabled(), "String deduplication not enabled"); 1.106 + unlink_or_oops_do(NULL, keep_alive); 1.107 +} 1.108 + 1.109 +void G1StringDedup::unlink(BoolObjectClosure* is_alive) { 1.110 + assert(is_enabled(), "String deduplication not enabled"); 1.111 + // Don't allow a potential resize or rehash during unlink, as the unlink 1.112 + // operation itself might remove enough entries to invalidate such a decision. 1.113 + unlink_or_oops_do(is_alive, NULL, false /* allow_resize_and_rehash */); 1.114 +} 1.115 + 1.116 +// 1.117 +// Task for parallel unlink_or_oops_do() operation on the deduplication queue 1.118 +// and table. 1.119 +// 1.120 +class G1StringDedupUnlinkOrOopsDoTask : public AbstractGangTask { 1.121 +private: 1.122 + G1StringDedupUnlinkOrOopsDoClosure _cl; 1.123 + 1.124 +public: 1.125 + G1StringDedupUnlinkOrOopsDoTask(BoolObjectClosure* is_alive, 1.126 + OopClosure* keep_alive, 1.127 + bool allow_resize_and_rehash) : 1.128 + AbstractGangTask("G1StringDedupUnlinkOrOopsDoTask"), 1.129 + _cl(is_alive, keep_alive, allow_resize_and_rehash) { 1.130 + } 1.131 + 1.132 + virtual void work(uint worker_id) { 1.133 + double queue_fixup_start = os::elapsedTime(); 1.134 + G1StringDedupQueue::unlink_or_oops_do(&_cl); 1.135 + 1.136 + double table_fixup_start = os::elapsedTime(); 1.137 + G1StringDedupTable::unlink_or_oops_do(&_cl, worker_id); 1.138 + 1.139 + double queue_fixup_time_ms = (table_fixup_start - queue_fixup_start) * 1000.0; 1.140 + double table_fixup_time_ms = (os::elapsedTime() - table_fixup_start) * 1000.0; 1.141 + G1CollectorPolicy* g1p = G1CollectedHeap::heap()->g1_policy(); 1.142 + g1p->phase_times()->record_string_dedup_queue_fixup_worker_time(worker_id, queue_fixup_time_ms); 1.143 + g1p->phase_times()->record_string_dedup_table_fixup_worker_time(worker_id, table_fixup_time_ms); 1.144 + } 1.145 +}; 1.146 + 1.147 +void G1StringDedup::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, bool allow_resize_and_rehash) { 1.148 + assert(is_enabled(), "String deduplication not enabled"); 1.149 + G1CollectorPolicy* g1p = G1CollectedHeap::heap()->g1_policy(); 1.150 + g1p->phase_times()->note_string_dedup_fixup_start(); 1.151 + double fixup_start = os::elapsedTime(); 1.152 + 1.153 + G1StringDedupUnlinkOrOopsDoTask task(is_alive, keep_alive, allow_resize_and_rehash); 1.154 + if (G1CollectedHeap::use_parallel_gc_threads()) { 1.155 + G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1.156 + g1h->set_par_threads(); 1.157 + g1h->workers()->run_task(&task); 1.158 + g1h->set_par_threads(0); 1.159 + } else { 1.160 + task.work(0); 1.161 + } 1.162 + 1.163 + double fixup_time_ms = (os::elapsedTime() - fixup_start) * 1000.0; 1.164 + g1p->phase_times()->record_string_dedup_fixup_time(fixup_time_ms); 1.165 + g1p->phase_times()->note_string_dedup_fixup_end(); 1.166 +} 1.167 + 1.168 +void G1StringDedup::threads_do(ThreadClosure* tc) { 1.169 + assert(is_enabled(), "String deduplication not enabled"); 1.170 + tc->do_thread(G1StringDedupThread::thread()); 1.171 +} 1.172 + 1.173 +void G1StringDedup::print_worker_threads_on(outputStream* st) { 1.174 + assert(is_enabled(), "String deduplication not enabled"); 1.175 + G1StringDedupThread::thread()->print_on(st); 1.176 + st->cr(); 1.177 +} 1.178 + 1.179 +void G1StringDedup::verify() { 1.180 + assert(is_enabled(), "String deduplication not enabled"); 1.181 + G1StringDedupQueue::verify(); 1.182 + G1StringDedupTable::verify(); 1.183 +} 1.184 + 1.185 +G1StringDedupUnlinkOrOopsDoClosure::G1StringDedupUnlinkOrOopsDoClosure(BoolObjectClosure* is_alive, 1.186 + OopClosure* keep_alive, 1.187 + bool allow_resize_and_rehash) : 1.188 + _is_alive(is_alive), 1.189 + _keep_alive(keep_alive), 1.190 + _resized_table(NULL), 1.191 + _rehashed_table(NULL), 1.192 + _next_queue(0), 1.193 + _next_bucket(0) { 1.194 + if (allow_resize_and_rehash) { 1.195 + // If both resize and rehash is needed, only do resize. Rehash of 1.196 + // the table will eventually happen if the situation persists. 1.197 + _resized_table = G1StringDedupTable::prepare_resize(); 1.198 + if (!is_resizing()) { 1.199 + _rehashed_table = G1StringDedupTable::prepare_rehash(); 1.200 + } 1.201 + } 1.202 +} 1.203 + 1.204 +G1StringDedupUnlinkOrOopsDoClosure::~G1StringDedupUnlinkOrOopsDoClosure() { 1.205 + assert(!is_resizing() || !is_rehashing(), "Can not both resize and rehash"); 1.206 + if (is_resizing()) { 1.207 + G1StringDedupTable::finish_resize(_resized_table); 1.208 + } else if (is_rehashing()) { 1.209 + G1StringDedupTable::finish_rehash(_rehashed_table); 1.210 + } 1.211 +}