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 "jvm.h" 27 #include "gc/g1/g1CollectedHeap.inline.hpp" 28 #include "gc/g1/g1ThreadLocalData.hpp" 29 #include "gc/g1/satbMarkQueue.hpp" 30 #include "gc/shared/collectedHeap.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "runtime/mutexLocker.hpp" 34 #include "runtime/safepoint.hpp" 35 #include "runtime/thread.hpp" 36 #include "runtime/threadSMR.hpp" 37 #include "runtime/vmThread.hpp" 38 39 SATBMarkQueue::SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent) : 40 // SATB queues are only active during marking cycles. We create 41 // them with their active field set to false. If a thread is 42 // created during a cycle and its SATB queue needs to be activated 43 // before the thread starts running, we'll need to set its active 44 // field to true. This is done in G1SBarrierSet::on_thread_attach(). 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 // If G1SATBBufferEnqueueingThresholdPercent == 0 we could skip filtering. 64 65 // This method should only be called if there is a non-NULL buffer 66 // that is full. 67 assert(index() == 0, "pre-condition"); 68 assert(_buf != NULL, "pre-condition"); 69 70 filter(); 71 72 size_t cap = capacity(); 73 size_t percent_used = ((cap - index()) * 100) / cap; 74 bool should_enqueue = percent_used > G1SATBBufferEnqueueingThresholdPercent; 75 return should_enqueue; 76 } 77 78 void SATBMarkQueue::apply_closure_and_empty(SATBBufferClosure* cl) { 79 assert(SafepointSynchronize::is_at_safepoint(), 80 "SATB queues must only be processed at safepoints"); 81 if (_buf != NULL) { 82 cl->do_buffer(&_buf[index()], size()); 83 reset(); 84 } 85 } 86 87 #ifndef PRODUCT 88 // Helpful for debugging 89 90 static void print_satb_buffer(const char* name, 91 void** buf, 92 size_t index, 93 size_t capacity) { 94 tty->print_cr(" SATB BUFFER [%s] buf: " PTR_FORMAT " index: " SIZE_FORMAT 95 " capacity: " SIZE_FORMAT, 96 name, p2i(buf), index, capacity); 97 } 98 99 void SATBMarkQueue::print(const char* name) { 100 print_satb_buffer(name, _buf, index(), capacity()); 101 } 102 103 #endif // PRODUCT 104 105 SATBMarkQueueSet::SATBMarkQueueSet() : 106 PtrQueueSet(), 107 _shared_satb_queue(this, true /* permanent */), 108 _filter(NULL) 109 {} 110 111 void SATBMarkQueueSet::initialize(SATBMarkQueueFilter* filter, 112 Monitor* cbl_mon, Mutex* fl_lock, 113 int process_completed_threshold, 114 Mutex* lock) { 115 PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1); 116 _shared_satb_queue.set_lock(lock); 117 _filter = filter; 118 } 119 120 void SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) { 121 G1ThreadLocalData::satb_mark_queue(t).handle_zero_index(); 122 } 123 124 #ifdef ASSERT 125 void SATBMarkQueueSet::dump_active_states(bool expected_active) { 126 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE"); 127 log_error(gc, verify)("Actual SATB active states:"); 128 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE"); 129 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 130 log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), G1ThreadLocalData::satb_mark_queue(t).is_active() ? "ACTIVE" : "INACTIVE"); 131 } 132 log_error(gc, verify)(" Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE"); 133 } 134 135 void SATBMarkQueueSet::verify_active_states(bool expected_active) { 136 // Verify queue set state 137 if (is_active() != expected_active) { 138 dump_active_states(expected_active); 139 guarantee(false, "SATB queue set has an unexpected active state"); 140 } 141 142 // Verify thread queue states 143 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 144 if (G1ThreadLocalData::satb_mark_queue(t).is_active() != expected_active) { 145 dump_active_states(expected_active); 146 guarantee(false, "Thread SATB queue has an unexpected active state"); 147 } 148 } 149 150 // Verify shared queue state 151 if (shared_satb_queue()->is_active() != expected_active) { 152 dump_active_states(expected_active); 153 guarantee(false, "Shared SATB queue has an unexpected active state"); 154 } 155 } 156 #endif // ASSERT 157 158 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) { 159 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 160 #ifdef ASSERT 161 verify_active_states(expected_active); 162 #endif // ASSERT 163 _all_active = active; 164 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 165 G1ThreadLocalData::satb_mark_queue(t).set_active(active); 166 } 167 shared_satb_queue()->set_active(active); 168 } 169 170 void SATBMarkQueueSet::filter_thread_buffers() { 171 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 172 G1ThreadLocalData::satb_mark_queue(t).filter(); 173 } 174 shared_satb_queue()->filter(); 175 } 176 177 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) { 178 BufferNode* nd = NULL; 179 { 180 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); 181 if (_completed_buffers_head != NULL) { 182 nd = _completed_buffers_head; 183 _completed_buffers_head = nd->next(); 184 if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL; 185 _n_completed_buffers--; 186 if (_n_completed_buffers == 0) _process_completed = false; 187 } 188 } 189 if (nd != NULL) { 190 void **buf = BufferNode::make_buffer_from_node(nd); 191 size_t index = nd->index(); 192 size_t size = buffer_size(); 193 assert(index <= size, "invariant"); 194 cl->do_buffer(buf + index, size - index); 195 deallocate_buffer(nd); 196 return true; 197 } else { 198 return false; 199 } 200 } 201 202 #ifndef PRODUCT 203 // Helpful for debugging 204 205 #define SATB_PRINTER_BUFFER_SIZE 256 206 207 void SATBMarkQueueSet::print_all(const char* msg) { 208 char buffer[SATB_PRINTER_BUFFER_SIZE]; 209 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 210 211 tty->cr(); 212 tty->print_cr("SATB BUFFERS [%s]", msg); 213 214 BufferNode* nd = _completed_buffers_head; 215 int i = 0; 216 while (nd != NULL) { 217 void** buf = BufferNode::make_buffer_from_node(nd); 218 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i); 219 print_satb_buffer(buffer, buf, nd->index(), buffer_size()); 220 nd = nd->next(); 221 i += 1; 222 } 223 224 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 225 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name()); 226 G1ThreadLocalData::satb_mark_queue(t).print(buffer); 227 } 228 229 shared_satb_queue()->print("Shared"); 230 231 tty->cr(); 232 } 233 #endif // PRODUCT 234 235 void SATBMarkQueueSet::abandon_partial_marking() { 236 BufferNode* buffers_to_delete = NULL; 237 { 238 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); 239 while (_completed_buffers_head != NULL) { 240 BufferNode* nd = _completed_buffers_head; 241 _completed_buffers_head = nd->next(); 242 nd->set_next(buffers_to_delete); 243 buffers_to_delete = nd; 244 } 245 _completed_buffers_tail = NULL; 246 _n_completed_buffers = 0; 247 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked()); 248 } 249 while (buffers_to_delete != NULL) { 250 BufferNode* nd = buffers_to_delete; 251 buffers_to_delete = nd->next(); 252 deallocate_buffer(nd); 253 } 254 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 255 // So we can safely manipulate these queues. 256 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 257 G1ThreadLocalData::satb_mark_queue(t).reset(); 258 } 259 shared_satb_queue()->reset(); 260 }