1 /* 2 * Copyright (c) 2017, 2018, Red Hat, Inc. and/or its affiliates. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "gc_implementation/shared/suspendibleThreadSet.hpp" 27 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 28 #include "gc_implementation/shenandoah/shenandoahLogging.hpp" 29 #include "gc_implementation/shenandoah/shenandoahStrDedupQueue.inline.hpp" 30 #include "gc_implementation/shenandoah/shenandoahStrDedupThread.hpp" 31 #include "gc_implementation/shenandoah/shenandoahStringDedup.hpp" 32 33 ShenandoahStrDedupThread::ShenandoahStrDedupThread(ShenandoahStrDedupQueueSet* queues) : 34 ConcurrentGCThread(), _queues(queues), _claimed(0) { 35 size_t num_queues = queues->num_queues(); 36 _work_list = NEW_C_HEAP_ARRAY(QueueChunkedList*, num_queues, mtGC); 37 for (size_t index = 0; index < num_queues; index ++) { 38 _work_list[index] = NULL; 39 } 40 41 set_name("%s", "ShenandoahStringDedupTherad"); 42 create_and_start(); 43 } 44 45 ShenandoahStrDedupThread::~ShenandoahStrDedupThread() { 46 ShouldNotReachHere(); 47 } 48 49 void ShenandoahStrDedupThread::run() { 50 for (;;) { 51 ShenandoahStrDedupStats stats; 52 53 assert(is_work_list_empty(), "Work list must be empty"); 54 // Queue has been shutdown 55 if (!poll(&stats)) { 56 assert(queues()->has_terminated(), "Must be terminated"); 57 break; 58 } 59 60 // Include thread in safepoints 61 SuspendibleThreadSetJoiner sts_join; 62 // Process the queue 63 for (uint queue_index = 0; queue_index < queues()->num_queues(); queue_index ++) { 64 QueueChunkedList* cur_list = _work_list[queue_index]; 65 66 while (cur_list != NULL) { 67 stats.mark_exec(); 68 69 while (!cur_list->is_empty()) { 70 oop java_string = cur_list->pop(); 71 stats.inc_inspected(); 72 assert(!ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must not at Shenandoah safepoint"); 73 74 if (oopDesc::is_null(java_string) || 75 !ShenandoahStringDedup::is_candidate(java_string)) { 76 stats.inc_skipped(); 77 } else { 78 if (ShenandoahStringDedup::deduplicate(java_string, false /* update counter */)) { 79 stats.inc_deduped(); 80 } else { 81 stats.inc_known(); 82 } 83 } 84 85 // Safepoint this thread if needed 86 if (sts_join.should_yield()) { 87 stats.mark_block(); 88 sts_join.yield(); 89 stats.mark_unblock(); 90 } 91 } 92 93 // Advance list only after processed. Otherwise, we may miss scanning 94 // during safepoints 95 _work_list[queue_index] = cur_list->next(); 96 queues()->release_chunked_list(cur_list); 97 cur_list = _work_list[queue_index]; 98 } 99 } 100 101 stats.mark_done(); 102 103 ShenandoahStringDedup::dedup_stats().update(stats); 104 105 if (ShenandoahLogDebug) { 106 stats.print_statistics(tty); 107 } 108 } 109 110 if (ShenandoahLogDebug) { 111 ShenandoahStringDedup::print_statistics(tty); 112 } 113 } 114 115 void ShenandoahStrDedupThread::stop() { 116 queues()->terminate(); 117 } 118 119 void ShenandoahStrDedupThread::parallel_oops_do(OopClosure* cl) { 120 assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint"); 121 size_t claimed_index; 122 while ((claimed_index = claim()) < queues()->num_queues()) { 123 QueueChunkedList* q = _work_list[claimed_index]; 124 while (q != NULL) { 125 q->oops_do(cl); 126 q = q->next(); 127 } 128 } 129 } 130 131 void ShenandoahStrDedupThread::oops_do_slow(OopClosure* cl) { 132 assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint"); 133 for (size_t index = 0; index < queues()->num_queues(); index ++) { 134 QueueChunkedList* q = _work_list[index]; 135 while (q != NULL) { 136 q->oops_do(cl); 137 q = q->next(); 138 } 139 } 140 } 141 142 bool ShenandoahStrDedupThread::is_work_list_empty() const { 143 assert(Thread::current() == this, "Only from dedup thread"); 144 for (uint index = 0; index < queues()->num_queues(); index ++) { 145 if (_work_list[index] != NULL) return false; 146 } 147 return true; 148 } 149 150 void ShenandoahStrDedupThread::parallel_cleanup() { 151 ShenandoahStrDedupQueueCleanupClosure cl; 152 parallel_oops_do(&cl); 153 } 154 155 bool ShenandoahStrDedupThread::poll(ShenandoahStrDedupStats* stats) { 156 assert(is_work_list_empty(), "Only poll when work list is empty"); 157 158 while (!_queues->has_terminated()) { 159 { 160 bool has_work = false; 161 stats->mark_exec(); 162 // Include thread in safepoints 163 SuspendibleThreadSetJoiner sts_join; 164 165 for (uint index = 0; index < queues()->num_queues(); index ++) { 166 assert(!ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Not at Shenandoah Safepoint"); 167 _work_list[index] = queues()->remove_work_list_atomic(index); 168 if (_work_list[index] != NULL) { 169 has_work = true; 170 } 171 172 // Safepoint this thread if needed 173 if (sts_join.should_yield()) { 174 stats->mark_block(); 175 sts_join.yield(); 176 stats->mark_unblock(); 177 } 178 } 179 180 if (has_work) return true; 181 } 182 183 { 184 stats->mark_idle(); 185 MonitorLockerEx locker(queues()->lock(), Monitor::_no_safepoint_check_flag); 186 locker.wait(Mutex::_no_safepoint_check_flag); 187 } 188 } 189 return false; 190 } 191 192 size_t ShenandoahStrDedupThread::claim() { 193 size_t index = (size_t)Atomic::add(1, (volatile jint*)&_claimed) - 1; 194 return index; 195 }