1 /* 2 * Copyright (c) 2001, 2019, 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) : 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 thread attachment protocol. 45 PtrQueue(qset, 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 void SATBMarkQueue::handle_completed_buffer() { 60 // This method should only be called if there is a non-NULL buffer 61 // that is full. 62 assert(index() == 0, "pre-condition"); 63 assert(_buf != NULL, "pre-condition"); 64 65 filter(); 66 67 size_t threshold = satb_qset()->buffer_enqueue_threshold(); 68 // Ensure we'll enqueue completely full buffers. 69 assert(threshold > 0, "enqueue threshold = 0"); 70 // Ensure we won't enqueue empty buffers. 71 assert(threshold <= capacity(), 72 "enqueue threshold " SIZE_FORMAT " exceeds capacity " SIZE_FORMAT, 73 threshold, capacity()); 74 75 if (index() < threshold) { 76 // Buffer is sufficiently full; enqueue and allocate a new one. 77 enqueue_completed_buffer(); 78 } // Else continue to accumulate in buffer. 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 _buffer_enqueue_threshold(0) 111 {} 112 113 void SATBMarkQueueSet::initialize(Monitor* cbl_mon, 114 BufferNode::Allocator* allocator, 115 size_t process_completed_buffers_threshold, 116 uint buffer_enqueue_threshold_percentage) { 117 PtrQueueSet::initialize(cbl_mon, allocator); 118 set_process_completed_buffers_threshold(process_completed_buffers_threshold); 119 assert(buffer_size() != 0, "buffer size not initialized"); 120 // Minimum threshold of 1 ensures enqueuing of completely full buffers. 121 size_t size = buffer_size(); 122 size_t enqueue_qty = (size * buffer_enqueue_threshold_percentage) / 100; 123 _buffer_enqueue_threshold = MAX2(size - enqueue_qty, (size_t)1); 124 } 125 126 #ifdef ASSERT 127 void SATBMarkQueueSet::dump_active_states(bool expected_active) { 128 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE"); 129 log_error(gc, verify)("Actual SATB active states:"); 130 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE"); 131 132 class DumpThreadStateClosure : public ThreadClosure { 133 SATBMarkQueueSet* _qset; 134 public: 135 DumpThreadStateClosure(SATBMarkQueueSet* qset) : _qset(qset) {} 136 virtual void do_thread(Thread* t) { 137 SATBMarkQueue& queue = _qset->satb_queue_for_thread(t); 138 log_error(gc, verify)(" Thread \"%s\" queue: %s", 139 t->name(), 140 queue.is_active() ? "ACTIVE" : "INACTIVE"); 141 } 142 } closure(this); 143 Threads::threads_do(&closure); 144 } 145 146 void SATBMarkQueueSet::verify_active_states(bool expected_active) { 147 // Verify queue set state 148 if (is_active() != expected_active) { 149 dump_active_states(expected_active); 150 fatal("SATB queue set has an unexpected active state"); 151 } 152 153 // Verify thread queue states 154 class VerifyThreadStatesClosure : public ThreadClosure { 155 SATBMarkQueueSet* _qset; 156 bool _expected_active; 157 public: 158 VerifyThreadStatesClosure(SATBMarkQueueSet* qset, bool expected_active) : 159 _qset(qset), _expected_active(expected_active) {} 160 virtual void do_thread(Thread* t) { 161 if (_qset->satb_queue_for_thread(t).is_active() != _expected_active) { 162 _qset->dump_active_states(_expected_active); 163 fatal("Thread SATB queue has an unexpected active state"); 164 } 165 } 166 } closure(this, expected_active); 167 Threads::threads_do(&closure); 168 } 169 #endif // ASSERT 170 171 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) { 172 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 173 #ifdef ASSERT 174 verify_active_states(expected_active); 175 #endif // ASSERT 176 // Update the global state, synchronized with threads list management. 177 { 178 MutexLocker ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); 179 _all_active = active; 180 } 181 182 class SetThreadActiveClosure : public ThreadClosure { 183 SATBMarkQueueSet* _qset; 184 bool _active; 185 public: 186 SetThreadActiveClosure(SATBMarkQueueSet* qset, bool active) : 187 _qset(qset), _active(active) {} 188 virtual void do_thread(Thread* t) { 189 _qset->satb_queue_for_thread(t).set_active(_active); 190 } 191 } closure(this, active); 192 Threads::threads_do(&closure); 193 } 194 195 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) { 196 BufferNode* nd = get_completed_buffer(); 197 if (nd != NULL) { 198 void **buf = BufferNode::make_buffer_from_node(nd); 199 size_t index = nd->index(); 200 size_t size = buffer_size(); 201 assert(index <= size, "invariant"); 202 cl->do_buffer(buf + index, size - index); 203 deallocate_buffer(nd); 204 return true; 205 } else { 206 return false; 207 } 208 } 209 210 #ifndef PRODUCT 211 // Helpful for debugging 212 213 #define SATB_PRINTER_BUFFER_SIZE 256 214 215 void SATBMarkQueueSet::print_all(const char* msg) { 216 char buffer[SATB_PRINTER_BUFFER_SIZE]; 217 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 218 219 tty->cr(); 220 tty->print_cr("SATB BUFFERS [%s]", msg); 221 222 BufferNode* nd = completed_buffers_head(); 223 int i = 0; 224 while (nd != NULL) { 225 void** buf = BufferNode::make_buffer_from_node(nd); 226 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i); 227 print_satb_buffer(buffer, buf, nd->index(), buffer_size()); 228 nd = nd->next(); 229 i += 1; 230 } 231 232 class PrintThreadClosure : public ThreadClosure { 233 SATBMarkQueueSet* _qset; 234 char* _buffer; 235 236 public: 237 PrintThreadClosure(SATBMarkQueueSet* qset, char* buffer) : 238 _qset(qset), _buffer(buffer) {} 239 240 virtual void do_thread(Thread* t) { 241 os::snprintf(_buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name()); 242 _qset->satb_queue_for_thread(t).print(_buffer); 243 } 244 } closure(this, buffer); 245 Threads::threads_do(&closure); 246 247 tty->cr(); 248 } 249 #endif // PRODUCT 250 251 void SATBMarkQueueSet::abandon_partial_marking() { 252 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 253 abandon_completed_buffers(); 254 255 class AbandonThreadQueueClosure : public ThreadClosure { 256 SATBMarkQueueSet* _qset; 257 public: 258 AbandonThreadQueueClosure(SATBMarkQueueSet* qset) : _qset(qset) {} 259 virtual void do_thread(Thread* t) { 260 _qset->satb_queue_for_thread(t).reset(); 261 } 262 } closure(this); 263 Threads::threads_do(&closure); 264 }