1 /* 2 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc/shared/satbMarkQueue.hpp" 27 #include "gc/shared/collectedHeap.hpp" 28 #include "logging/log.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "oops/oop.inline.hpp" 31 #include "runtime/mutexLocker.hpp" 32 #include "runtime/os.hpp" 33 #include "runtime/safepoint.hpp" 34 #include "runtime/thread.hpp" 35 #include "runtime/threadSMR.hpp" 36 #include "runtime/vmThread.hpp" 37 38 SATBMarkQueue::SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent) : 39 // SATB queues are only active during marking cycles. We create 40 // them with their active field set to false. If a thread is 41 // created during a cycle and its SATB queue needs to be activated 42 // before the thread starts running, we'll need to set its active 43 // field to true. This must be done in the collector-specific 44 // BarrierSet::on_thread_attach() implementation. 45 PtrQueue(qset, permanent, false /* active */) 46 { } 47 48 void SATBMarkQueue::flush() { 49 // Filter now to possibly save work later. If filtering empties the 50 // buffer then flush_impl can deallocate the buffer. 51 filter(); 52 flush_impl(); 53 } 54 55 // This method will first apply filtering to the buffer. If filtering 56 // retains a small enough collection in the buffer, we can continue to 57 // use the buffer as-is, instead of enqueueing and replacing it. 58 59 bool SATBMarkQueue::should_enqueue_buffer() { 60 assert(_lock == NULL || _lock->owned_by_self(), 61 "we should have taken the lock before calling this"); 62 63 // This method should only be called if there is a non-NULL buffer 64 // that is full. 65 assert(index() == 0, "pre-condition"); 66 assert(_buf != NULL, "pre-condition"); 67 68 filter(); 69 70 SATBMarkQueueSet* satb_qset = static_cast<SATBMarkQueueSet*>(qset()); 71 size_t threshold = satb_qset->buffer_enqueue_threshold(); 72 // Ensure we'll enqueue completely full buffers. 73 assert(threshold > 0, "enqueue threshold = 0"); 74 // Ensure we won't enqueue empty buffers. 75 assert(threshold <= capacity(), 76 "enqueue threshold " SIZE_FORMAT " exceeds capacity " SIZE_FORMAT, 77 threshold, capacity()); 78 return index() < threshold; 79 } 80 81 void SATBMarkQueue::apply_closure_and_empty(SATBBufferClosure* cl) { 82 assert(SafepointSynchronize::is_at_safepoint(), 83 "SATB queues must only be processed at safepoints"); 84 if (_buf != NULL) { 85 cl->do_buffer(&_buf[index()], size()); 86 reset(); 87 } 88 } 89 90 #ifndef PRODUCT 91 // Helpful for debugging 92 93 static void print_satb_buffer(const char* name, 94 void** buf, 95 size_t index, 96 size_t capacity) { 97 tty->print_cr(" SATB BUFFER [%s] buf: " PTR_FORMAT " index: " SIZE_FORMAT 98 " capacity: " SIZE_FORMAT, 99 name, p2i(buf), index, capacity); 100 } 101 102 void SATBMarkQueue::print(const char* name) { 103 print_satb_buffer(name, _buf, index(), capacity()); 104 } 105 106 #endif // PRODUCT 107 108 SATBMarkQueueSet::SATBMarkQueueSet() : 109 PtrQueueSet(), 110 _shared_satb_queue(this, true /* permanent */), 111 _buffer_enqueue_threshold(0) 112 {} 113 114 void SATBMarkQueueSet::initialize(Monitor* cbl_mon, 115 BufferNode::Allocator* allocator, 116 size_t process_completed_buffers_threshold, 117 uint buffer_enqueue_threshold_percentage, 118 Mutex* lock) { 119 PtrQueueSet::initialize(cbl_mon, allocator); 120 set_process_completed_buffers_threshold(process_completed_buffers_threshold); 121 _shared_satb_queue.set_lock(lock); 122 assert(buffer_size() != 0, "buffer size not initialized"); 123 // Minimum threshold of 1 ensures enqueuing of completely full buffers. 124 size_t size = buffer_size(); 125 size_t enqueue_qty = (size * buffer_enqueue_threshold_percentage) / 100; 126 _buffer_enqueue_threshold = MAX2(size - enqueue_qty, (size_t)1); 127 } 128 129 #ifdef ASSERT 130 void SATBMarkQueueSet::dump_active_states(bool expected_active) { 131 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE"); 132 log_error(gc, verify)("Actual SATB active states:"); 133 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE"); 134 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 135 log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), satb_queue_for_thread(t).is_active() ? "ACTIVE" : "INACTIVE"); 136 } 137 log_error(gc, verify)(" Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE"); 138 } 139 140 void SATBMarkQueueSet::verify_active_states(bool expected_active) { 141 // Verify queue set state 142 if (is_active() != expected_active) { 143 dump_active_states(expected_active); 144 guarantee(false, "SATB queue set has an unexpected active state"); 145 } 146 147 // Verify thread queue states 148 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 149 if (satb_queue_for_thread(t).is_active() != expected_active) { 150 dump_active_states(expected_active); 151 guarantee(false, "Thread SATB queue has an unexpected active state"); 152 } 153 } 154 155 // Verify shared queue state 156 if (shared_satb_queue()->is_active() != expected_active) { 157 dump_active_states(expected_active); 158 guarantee(false, "Shared SATB queue has an unexpected active state"); 159 } 160 } 161 #endif // ASSERT 162 163 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) { 164 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 165 #ifdef ASSERT 166 verify_active_states(expected_active); 167 #endif // ASSERT 168 _all_active = active; 169 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 170 satb_queue_for_thread(t).set_active(active); 171 } 172 shared_satb_queue()->set_active(active); 173 } 174 175 void SATBMarkQueueSet::filter_thread_buffers() { 176 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 177 satb_queue_for_thread(t).filter(); 178 } 179 shared_satb_queue()->filter(); 180 } 181 182 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) { 183 BufferNode* nd = get_completed_buffer(); 184 if (nd != NULL) { 185 void **buf = BufferNode::make_buffer_from_node(nd); 186 size_t index = nd->index(); 187 size_t size = buffer_size(); 188 assert(index <= size, "invariant"); 189 cl->do_buffer(buf + index, size - index); 190 deallocate_buffer(nd); 191 return true; 192 } else { 193 return false; 194 } 195 } 196 197 #ifndef PRODUCT 198 // Helpful for debugging 199 200 #define SATB_PRINTER_BUFFER_SIZE 256 201 202 void SATBMarkQueueSet::print_all(const char* msg) { 203 char buffer[SATB_PRINTER_BUFFER_SIZE]; 204 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 205 206 tty->cr(); 207 tty->print_cr("SATB BUFFERS [%s]", msg); 208 209 BufferNode* nd = completed_buffers_head(); 210 int i = 0; 211 while (nd != NULL) { 212 void** buf = BufferNode::make_buffer_from_node(nd); 213 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i); 214 print_satb_buffer(buffer, buf, nd->index(), buffer_size()); 215 nd = nd->next(); 216 i += 1; 217 } 218 219 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 220 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name()); 221 satb_queue_for_thread(t).print(buffer); 222 } 223 224 shared_satb_queue()->print("Shared"); 225 226 tty->cr(); 227 } 228 #endif // PRODUCT 229 230 void SATBMarkQueueSet::abandon_partial_marking() { 231 abandon_completed_buffers(); 232 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 233 // So we can safely manipulate these queues. 234 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 235 satb_queue_for_thread(t).reset(); 236 } 237 shared_satb_queue()->reset(); 238 }