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/g1/dirtyCardQueue.hpp" 27 #include "gc/g1/g1CollectedHeap.inline.hpp" 28 #include "gc/g1/g1RemSet.hpp" 29 #include "gc/g1/g1ThreadLocalData.hpp" 30 #include "gc/g1/heapRegionRemSet.hpp" 31 #include "gc/shared/suspendibleThreadSet.hpp" 32 #include "gc/shared/workgroup.hpp" 33 #include "runtime/atomic.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "runtime/safepoint.hpp" 36 #include "runtime/thread.inline.hpp" 37 #include "runtime/threadSMR.hpp" 38 39 // Closure used for updating remembered sets and recording references that 40 // point into the collection set while the mutator is running. 41 // Assumed to be only executed concurrently with the mutator. Yields via 42 // SuspendibleThreadSet after every card. 43 class G1RefineCardConcurrentlyClosure: public CardTableEntryClosure { 44 public: 45 bool do_card_ptr(jbyte* card_ptr, uint worker_i) { 46 G1CollectedHeap::heap()->g1_rem_set()->refine_card_concurrently(card_ptr, worker_i); 47 48 if (SuspendibleThreadSet::should_yield()) { 49 // Caller will actually yield. 50 return false; 51 } 52 // Otherwise, we finished successfully; return true. 53 return true; 54 } 55 }; 56 57 // Represents a set of free small integer ids. 58 class FreeIdSet : public CHeapObj<mtGC> { 59 enum { 60 end_of_list = UINT_MAX, 61 claimed = UINT_MAX - 1 62 }; 63 64 uint _size; 65 Monitor* _mon; 66 67 uint* _ids; 68 uint _hd; 69 uint _waiters; 70 uint _claimed; 71 72 public: 73 FreeIdSet(uint size, Monitor* mon); 74 ~FreeIdSet(); 75 76 // Returns an unclaimed parallel id (waiting for one to be released if 77 // necessary). 78 uint claim_par_id(); 79 80 void release_par_id(uint id); 81 }; 82 83 FreeIdSet::FreeIdSet(uint size, Monitor* mon) : 84 _size(size), _mon(mon), _hd(0), _waiters(0), _claimed(0) 85 { 86 guarantee(size != 0, "must be"); 87 _ids = NEW_C_HEAP_ARRAY(uint, size, mtGC); 88 for (uint i = 0; i < size - 1; i++) { 89 _ids[i] = i+1; 90 } 91 _ids[size-1] = end_of_list; // end of list. 92 } 93 94 FreeIdSet::~FreeIdSet() { 95 FREE_C_HEAP_ARRAY(uint, _ids); 96 } 97 98 uint FreeIdSet::claim_par_id() { 99 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); 100 while (_hd == end_of_list) { 101 _waiters++; 102 _mon->wait(Mutex::_no_safepoint_check_flag); 103 _waiters--; 104 } 105 uint res = _hd; 106 _hd = _ids[res]; 107 _ids[res] = claimed; // For debugging. 108 _claimed++; 109 return res; 110 } 111 112 void FreeIdSet::release_par_id(uint id) { 113 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); 114 assert(_ids[id] == claimed, "Precondition."); 115 _ids[id] = _hd; 116 _hd = id; 117 _claimed--; 118 if (_waiters > 0) { 119 _mon->notify_all(); 120 } 121 } 122 123 DirtyCardQueue::DirtyCardQueue(DirtyCardQueueSet* qset, bool permanent) : 124 // Dirty card queues are always active, so we create them with their 125 // active field set to true. 126 PtrQueue(qset, permanent, true /* active */) 127 { } 128 129 DirtyCardQueue::~DirtyCardQueue() { 130 if (!is_permanent()) { 131 flush(); 132 } 133 } 134 135 DirtyCardQueueSet::DirtyCardQueueSet(bool notify_when_complete) : 136 PtrQueueSet(notify_when_complete), 137 _shared_dirty_card_queue(this, true /* permanent */), 138 _free_ids(NULL), 139 _processed_buffers_mut(0), _processed_buffers_rs_thread(0) 140 { 141 _all_active = true; 142 } 143 144 // Determines how many mutator threads can process the buffers in parallel. 145 uint DirtyCardQueueSet::num_par_ids() { 146 return (uint)os::initial_active_processor_count(); 147 } 148 149 void DirtyCardQueueSet::initialize(Monitor* cbl_mon, 150 BufferNode::Allocator* allocator, 151 Mutex* lock, 152 bool init_free_ids) { 153 PtrQueueSet::initialize(cbl_mon, allocator); 154 _shared_dirty_card_queue.set_lock(lock); 155 if (init_free_ids) { 156 _free_ids = new FreeIdSet(num_par_ids(), _cbl_mon); 157 } 158 } 159 160 void DirtyCardQueueSet::handle_zero_index_for_thread(JavaThread* t) { 161 G1ThreadLocalData::dirty_card_queue(t).handle_zero_index(); 162 } 163 164 bool DirtyCardQueueSet::apply_closure_to_buffer(CardTableEntryClosure* cl, 165 BufferNode* node, 166 bool consume, 167 uint worker_i) { 168 if (cl == NULL) return true; 169 bool result = true; 170 void** buf = BufferNode::make_buffer_from_node(node); 171 size_t i = node->index(); 172 size_t limit = buffer_size(); 173 for ( ; i < limit; ++i) { 174 jbyte* card_ptr = static_cast<jbyte*>(buf[i]); 175 assert(card_ptr != NULL, "invariant"); 176 if (!cl->do_card_ptr(card_ptr, worker_i)) { 177 result = false; // Incomplete processing. 178 break; 179 } 180 } 181 if (consume) { 182 assert(i <= buffer_size(), "invariant"); 183 node->set_index(i); 184 } 185 return result; 186 } 187 188 #ifndef ASSERT 189 #define assert_fully_consumed(node, buffer_size) 190 #else 191 #define assert_fully_consumed(node, buffer_size) \ 192 do { \ 193 size_t _afc_index = (node)->index(); \ 194 size_t _afc_size = (buffer_size); \ 195 assert(_afc_index == _afc_size, \ 196 "Buffer was not fully consumed as claimed: index: " \ 197 SIZE_FORMAT ", size: " SIZE_FORMAT, \ 198 _afc_index, _afc_size); \ 199 } while (0) 200 #endif // ASSERT 201 202 bool DirtyCardQueueSet::mut_process_buffer(BufferNode* node) { 203 guarantee(_free_ids != NULL, "must be"); 204 205 uint worker_i = _free_ids->claim_par_id(); // temporarily claim an id 206 G1RefineCardConcurrentlyClosure cl; 207 bool result = apply_closure_to_buffer(&cl, node, true, worker_i); 208 _free_ids->release_par_id(worker_i); // release the id 209 210 if (result) { 211 assert_fully_consumed(node, buffer_size()); 212 Atomic::inc(&_processed_buffers_mut); 213 } 214 return result; 215 } 216 217 218 BufferNode* DirtyCardQueueSet::get_completed_buffer(size_t stop_at) { 219 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); 220 221 if (_n_completed_buffers <= stop_at) { 222 return NULL; 223 } 224 225 assert(_n_completed_buffers > 0, "invariant"); 226 assert(_completed_buffers_head != NULL, "invariant"); 227 assert(_completed_buffers_tail != NULL, "invariant"); 228 229 BufferNode* nd = _completed_buffers_head; 230 _completed_buffers_head = nd->next(); 231 _n_completed_buffers--; 232 if (_completed_buffers_head == NULL) { 233 assert(_n_completed_buffers == 0, "Invariant"); 234 _completed_buffers_tail = NULL; 235 } 236 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked()); 237 return nd; 238 } 239 240 bool DirtyCardQueueSet::refine_completed_buffer_concurrently(uint worker_i, size_t stop_at) { 241 G1RefineCardConcurrentlyClosure cl; 242 return apply_closure_to_completed_buffer(&cl, worker_i, stop_at, false); 243 } 244 245 bool DirtyCardQueueSet::apply_closure_during_gc(CardTableEntryClosure* cl, uint worker_i) { 246 assert_at_safepoint(); 247 return apply_closure_to_completed_buffer(cl, worker_i, 0, true); 248 } 249 250 bool DirtyCardQueueSet::apply_closure_to_completed_buffer(CardTableEntryClosure* cl, 251 uint worker_i, 252 size_t stop_at, 253 bool during_pause) { 254 assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause"); 255 BufferNode* nd = get_completed_buffer(stop_at); 256 if (nd == NULL) { 257 return false; 258 } else { 259 if (apply_closure_to_buffer(cl, nd, true, worker_i)) { 260 assert_fully_consumed(nd, buffer_size()); 261 // Done with fully processed buffer. 262 deallocate_buffer(nd); 263 Atomic::inc(&_processed_buffers_rs_thread); 264 } else { 265 // Return partially processed buffer to the queue. 266 guarantee(!during_pause, "Should never stop early"); 267 enqueue_complete_buffer(nd); 268 } 269 return true; 270 } 271 } 272 273 void DirtyCardQueueSet::par_apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) { 274 BufferNode* nd = _cur_par_buffer_node; 275 while (nd != NULL) { 276 BufferNode* next = nd->next(); 277 BufferNode* actual = Atomic::cmpxchg(next, &_cur_par_buffer_node, nd); 278 if (actual == nd) { 279 bool b = apply_closure_to_buffer(cl, nd, false); 280 guarantee(b, "Should not stop early."); 281 nd = next; 282 } else { 283 nd = actual; 284 } 285 } 286 } 287 288 // Deallocates any completed log buffers 289 void DirtyCardQueueSet::clear() { 290 BufferNode* buffers_to_delete = NULL; 291 { 292 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); 293 while (_completed_buffers_head != NULL) { 294 BufferNode* nd = _completed_buffers_head; 295 _completed_buffers_head = nd->next(); 296 nd->set_next(buffers_to_delete); 297 buffers_to_delete = nd; 298 } 299 _n_completed_buffers = 0; 300 _completed_buffers_tail = NULL; 301 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked()); 302 } 303 while (buffers_to_delete != NULL) { 304 BufferNode* nd = buffers_to_delete; 305 buffers_to_delete = nd->next(); 306 deallocate_buffer(nd); 307 } 308 309 } 310 311 void DirtyCardQueueSet::abandon_logs() { 312 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 313 clear(); 314 // Since abandon is done only at safepoints, we can safely manipulate 315 // these queues. 316 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 317 G1ThreadLocalData::dirty_card_queue(t).reset(); 318 } 319 shared_dirty_card_queue()->reset(); 320 } 321 322 void DirtyCardQueueSet::concatenate_log(DirtyCardQueue& dcq) { 323 if (!dcq.is_empty()) { 324 dcq.flush(); 325 } 326 } 327 328 void DirtyCardQueueSet::concatenate_logs() { 329 // Iterate over all the threads, if we find a partial log add it to 330 // the global list of logs. Temporarily turn off the limit on the number 331 // of outstanding buffers. 332 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); 333 size_t save_max_completed_buffers = _max_completed_buffers; 334 _max_completed_buffers = _max_completed_buffers_unlimited; 335 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 336 concatenate_log(G1ThreadLocalData::dirty_card_queue(t)); 337 } 338 concatenate_log(_shared_dirty_card_queue); 339 // Restore the completed buffer limit. 340 _max_completed_buffers = save_max_completed_buffers; 341 }