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 = NULL; 184 { 185 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); 186 if (_completed_buffers_head != NULL) { 187 nd = _completed_buffers_head; 188 _completed_buffers_head = nd->next(); 189 if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL; 190 _n_completed_buffers--; 191 if (_n_completed_buffers == 0) _process_completed = false; 192 } 193 } 194 if (nd != NULL) { 195 void **buf = BufferNode::make_buffer_from_node(nd); 196 size_t index = nd->index(); 197 size_t size = buffer_size(); 198 assert(index <= size, "invariant"); 199 cl->do_buffer(buf + index, size - index); 200 deallocate_buffer(nd); 201 return true; 202 } else { 203 return false; 204 } 205 } 206 207 #ifndef PRODUCT 208 // Helpful for debugging 209 210 #define SATB_PRINTER_BUFFER_SIZE 256 211 212 void SATBMarkQueueSet::print_all(const char* msg) { 213 char buffer[SATB_PRINTER_BUFFER_SIZE]; 214 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 215 216 tty->cr(); 217 tty->print_cr("SATB BUFFERS [%s]", msg); 218 219 BufferNode* nd = _completed_buffers_head; 220 int i = 0; 221 while (nd != NULL) { 222 void** buf = BufferNode::make_buffer_from_node(nd); 223 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i); 224 print_satb_buffer(buffer, buf, nd->index(), buffer_size()); 225 nd = nd->next(); 226 i += 1; 227 } 228 229 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 230 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name()); 231 satb_queue_for_thread(t).print(buffer); 232 } 233 234 shared_satb_queue()->print("Shared"); 235 236 tty->cr(); 237 } 238 #endif // PRODUCT 239 240 void SATBMarkQueueSet::abandon_partial_marking() { 241 BufferNode* buffers_to_delete = NULL; 242 { 243 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); 244 while (_completed_buffers_head != NULL) { 245 BufferNode* nd = _completed_buffers_head; 246 _completed_buffers_head = nd->next(); 247 nd->set_next(buffers_to_delete); 248 buffers_to_delete = nd; 249 } 250 _completed_buffers_tail = NULL; 251 _n_completed_buffers = 0; 252 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked()); 253 } 254 while (buffers_to_delete != NULL) { 255 BufferNode* nd = buffers_to_delete; 256 buffers_to_delete = nd->next(); 257 deallocate_buffer(nd); 258 } 259 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 260 // So we can safely manipulate these queues. 261 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 262 satb_queue_for_thread(t).reset(); 263 } 264 shared_satb_queue()->reset(); 265 }