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