src/share/vm/gc_implementation/g1/satbQueue.cpp

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 9448
73d689add964
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

aoqi@0 1 /*
kbarrett@7831 2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 27 #include "gc_implementation/g1/satbQueue.hpp"
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "memory/sharedHeap.hpp"
aoqi@0 30 #include "oops/oop.inline.hpp"
aoqi@0 31 #include "runtime/mutexLocker.hpp"
kbarrett@7834 32 #include "runtime/safepoint.hpp"
aoqi@0 33 #include "runtime/thread.hpp"
aoqi@0 34 #include "runtime/vmThread.hpp"
aoqi@0 35
aoqi@0 36 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 37
aoqi@0 38 void ObjPtrQueue::flush() {
kbarrett@7831 39 // Filter now to possibly save work later. If filtering empties the
kbarrett@7831 40 // buffer then flush_impl can deallocate the buffer.
aoqi@0 41 filter();
tschatzl@7445 42 flush_impl();
aoqi@0 43 }
aoqi@0 44
kbarrett@7831 45 // Return true if a SATB buffer entry refers to an object that
kbarrett@7831 46 // requires marking.
aoqi@0 47 //
kbarrett@7831 48 // The entry must point into the G1 heap. In particular, it must not
kbarrett@7831 49 // be a NULL pointer. NULL pointers are pre-filtered and never
kbarrett@7831 50 // inserted into a SATB buffer.
aoqi@0 51 //
kbarrett@7831 52 // An entry that is below the NTAMS pointer for the containing heap
kbarrett@7831 53 // region requires marking. Such an entry must point to a valid object.
kbarrett@7831 54 //
kbarrett@7831 55 // An entry that is at least the NTAMS pointer for the containing heap
kbarrett@7831 56 // region might be any of the following, none of which should be marked.
kbarrett@7831 57 //
kbarrett@7831 58 // * A reference to an object allocated since marking started.
kbarrett@7831 59 // According to SATB, such objects are implicitly kept live and do
kbarrett@7831 60 // not need to be dealt with via SATB buffer processing.
kbarrett@7831 61 //
kbarrett@7831 62 // * A reference to a young generation object. Young objects are
kbarrett@7831 63 // handled separately and are not marked by concurrent marking.
kbarrett@7831 64 //
kbarrett@7831 65 // * A stale reference to a young generation object. If a young
kbarrett@7831 66 // generation object reference is recorded and not filtered out
kbarrett@7831 67 // before being moved by a young collection, the reference becomes
kbarrett@7831 68 // stale.
kbarrett@7831 69 //
kbarrett@7831 70 // * A stale reference to an eagerly reclaimed humongous object. If a
kbarrett@7831 71 // humongous object is recorded and then reclaimed, the reference
kbarrett@7831 72 // becomes stale.
kbarrett@7831 73 //
kbarrett@7831 74 // The stale reference cases are implicitly handled by the NTAMS
kbarrett@7831 75 // comparison. Because of the possibility of stale references, buffer
kbarrett@7831 76 // processing must be somewhat circumspect and not assume entries
kbarrett@7831 77 // in an unfiltered buffer refer to valid objects.
kbarrett@7831 78
kbarrett@7831 79 inline bool requires_marking(const void* entry, G1CollectedHeap* heap) {
kbarrett@7831 80 // Includes rejection of NULL pointers.
kbarrett@7831 81 assert(heap->is_in_reserved(entry),
kbarrett@7831 82 err_msg("Non-heap pointer in SATB buffer: " PTR_FORMAT, p2i(entry)));
kbarrett@7831 83
kbarrett@7831 84 HeapRegion* region = heap->heap_region_containing_raw(entry);
kbarrett@7831 85 assert(region != NULL, err_msg("No region for " PTR_FORMAT, p2i(entry)));
kbarrett@7831 86 if (entry >= region->next_top_at_mark_start()) {
kbarrett@7831 87 return false;
kbarrett@7831 88 }
kbarrett@7831 89
kbarrett@7831 90 assert(((oop)entry)->is_oop(true /* ignore mark word */),
kbarrett@7831 91 err_msg("Invalid oop in SATB buffer: " PTR_FORMAT, p2i(entry)));
kbarrett@7831 92
kbarrett@7831 93 return true;
kbarrett@7831 94 }
kbarrett@7831 95
kbarrett@7831 96 // This method removes entries from a SATB buffer that will not be
kbarrett@7831 97 // useful to the concurrent marking threads. Entries are retained if
kbarrett@7831 98 // they require marking and are not already marked. Retained entries
kbarrett@7831 99 // are compacted toward the top of the buffer.
aoqi@0 100
aoqi@0 101 void ObjPtrQueue::filter() {
aoqi@0 102 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 103 void** buf = _buf;
aoqi@0 104 size_t sz = _sz;
aoqi@0 105
aoqi@0 106 if (buf == NULL) {
aoqi@0 107 // nothing to do
aoqi@0 108 return;
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 // Used for sanity checking at the end of the loop.
aoqi@0 112 debug_only(size_t entries = 0; size_t retained = 0;)
aoqi@0 113
aoqi@0 114 size_t i = sz;
aoqi@0 115 size_t new_index = sz;
aoqi@0 116
aoqi@0 117 while (i > _index) {
aoqi@0 118 assert(i > 0, "we should have at least one more entry to process");
aoqi@0 119 i -= oopSize;
aoqi@0 120 debug_only(entries += 1;)
kbarrett@7831 121 void** p = &buf[byte_index_to_index((int) i)];
kbarrett@7831 122 void* entry = *p;
aoqi@0 123 // NULL the entry so that unused parts of the buffer contain NULLs
aoqi@0 124 // at the end. If we are going to retain it we will copy it to its
aoqi@0 125 // final place. If we have retained all entries we have visited so
aoqi@0 126 // far, we'll just end up copying it to the same place.
aoqi@0 127 *p = NULL;
aoqi@0 128
kbarrett@7831 129 if (requires_marking(entry, g1h) && !g1h->isMarkedNext((oop)entry)) {
aoqi@0 130 assert(new_index > 0, "we should not have already filled up the buffer");
aoqi@0 131 new_index -= oopSize;
aoqi@0 132 assert(new_index >= i,
aoqi@0 133 "new_index should never be below i, as we alwaysr compact 'up'");
kbarrett@7831 134 void** new_p = &buf[byte_index_to_index((int) new_index)];
aoqi@0 135 assert(new_p >= p, "the destination location should never be below "
aoqi@0 136 "the source as we always compact 'up'");
aoqi@0 137 assert(*new_p == NULL,
aoqi@0 138 "we should have already cleared the destination location");
kbarrett@7831 139 *new_p = entry;
aoqi@0 140 debug_only(retained += 1;)
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 #ifdef ASSERT
aoqi@0 145 size_t entries_calc = (sz - _index) / oopSize;
aoqi@0 146 assert(entries == entries_calc, "the number of entries we counted "
aoqi@0 147 "should match the number of entries we calculated");
aoqi@0 148 size_t retained_calc = (sz - new_index) / oopSize;
aoqi@0 149 assert(retained == retained_calc, "the number of retained entries we counted "
aoqi@0 150 "should match the number of retained entries we calculated");
aoqi@0 151 #endif // ASSERT
aoqi@0 152
aoqi@0 153 _index = new_index;
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 // This method will first apply the above filtering to the buffer. If
aoqi@0 157 // post-filtering a large enough chunk of the buffer has been cleared
aoqi@0 158 // we can re-use the buffer (instead of enqueueing it) and we can just
aoqi@0 159 // allow the mutator to carry on executing using the same buffer
aoqi@0 160 // instead of replacing it.
aoqi@0 161
aoqi@0 162 bool ObjPtrQueue::should_enqueue_buffer() {
aoqi@0 163 assert(_lock == NULL || _lock->owned_by_self(),
aoqi@0 164 "we should have taken the lock before calling this");
aoqi@0 165
kbarrett@7834 166 // If G1SATBBufferEnqueueingThresholdPercent == 0 we could skip filtering.
aoqi@0 167
aoqi@0 168 // This method should only be called if there is a non-NULL buffer
aoqi@0 169 // that is full.
aoqi@0 170 assert(_index == 0, "pre-condition");
aoqi@0 171 assert(_buf != NULL, "pre-condition");
aoqi@0 172
aoqi@0 173 filter();
aoqi@0 174
aoqi@0 175 size_t sz = _sz;
aoqi@0 176 size_t all_entries = sz / oopSize;
aoqi@0 177 size_t retained_entries = (sz - _index) / oopSize;
aoqi@0 178 size_t perc = retained_entries * 100 / all_entries;
aoqi@0 179 bool should_enqueue = perc > (size_t) G1SATBBufferEnqueueingThresholdPercent;
aoqi@0 180 return should_enqueue;
aoqi@0 181 }
aoqi@0 182
kbarrett@7834 183 void ObjPtrQueue::apply_closure_and_empty(SATBBufferClosure* cl) {
kbarrett@7834 184 assert(SafepointSynchronize::is_at_safepoint(),
kbarrett@7834 185 "SATB queues must only be processed at safepoints");
aoqi@0 186 if (_buf != NULL) {
kbarrett@7834 187 assert(_index % sizeof(void*) == 0, "invariant");
kbarrett@7834 188 assert(_sz % sizeof(void*) == 0, "invariant");
kbarrett@7834 189 assert(_index <= _sz, "invariant");
kbarrett@7834 190 cl->do_buffer(_buf + byte_index_to_index((int)_index),
kbarrett@7834 191 byte_index_to_index((int)(_sz - _index)));
aoqi@0 192 _index = _sz;
aoqi@0 193 }
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 #ifndef PRODUCT
aoqi@0 197 // Helpful for debugging
aoqi@0 198
aoqi@0 199 void ObjPtrQueue::print(const char* name) {
aoqi@0 200 print(name, _buf, _index, _sz);
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 void ObjPtrQueue::print(const char* name,
aoqi@0 204 void** buf, size_t index, size_t sz) {
kevinw@9327 205 gclog_or_tty->print_cr(" SATB BUFFER [%s] buf: " PTR_FORMAT " "
kevinw@9327 206 "index: " SIZE_FORMAT " sz: " SIZE_FORMAT,
aoqi@0 207 name, buf, index, sz);
aoqi@0 208 }
aoqi@0 209 #endif // PRODUCT
aoqi@0 210
aoqi@0 211 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
aoqi@0 212 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
aoqi@0 213 #endif // _MSC_VER
aoqi@0 214
aoqi@0 215 SATBMarkQueueSet::SATBMarkQueueSet() :
kbarrett@7832 216 PtrQueueSet(),
aoqi@0 217 _shared_satb_queue(this, true /*perm*/) { }
aoqi@0 218
aoqi@0 219 void SATBMarkQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock,
aoqi@0 220 int process_completed_threshold,
aoqi@0 221 Mutex* lock) {
aoqi@0 222 PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
aoqi@0 223 _shared_satb_queue.set_lock(lock);
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 void SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
aoqi@0 227 t->satb_mark_queue().handle_zero_index();
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 #ifdef ASSERT
aoqi@0 231 void SATBMarkQueueSet::dump_active_states(bool expected_active) {
aoqi@0 232 gclog_or_tty->print_cr("Expected SATB active state: %s",
aoqi@0 233 expected_active ? "ACTIVE" : "INACTIVE");
aoqi@0 234 gclog_or_tty->print_cr("Actual SATB active states:");
aoqi@0 235 gclog_or_tty->print_cr(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
aoqi@0 236 for (JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 237 gclog_or_tty->print_cr(" Thread \"%s\" queue: %s", t->name(),
aoqi@0 238 t->satb_mark_queue().is_active() ? "ACTIVE" : "INACTIVE");
aoqi@0 239 }
aoqi@0 240 gclog_or_tty->print_cr(" Shared queue: %s",
aoqi@0 241 shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE");
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 void SATBMarkQueueSet::verify_active_states(bool expected_active) {
aoqi@0 245 // Verify queue set state
aoqi@0 246 if (is_active() != expected_active) {
aoqi@0 247 dump_active_states(expected_active);
aoqi@0 248 guarantee(false, "SATB queue set has an unexpected active state");
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 // Verify thread queue states
aoqi@0 252 for (JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 253 if (t->satb_mark_queue().is_active() != expected_active) {
aoqi@0 254 dump_active_states(expected_active);
aoqi@0 255 guarantee(false, "Thread SATB queue has an unexpected active state");
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 // Verify shared queue state
aoqi@0 260 if (shared_satb_queue()->is_active() != expected_active) {
aoqi@0 261 dump_active_states(expected_active);
aoqi@0 262 guarantee(false, "Shared SATB queue has an unexpected active state");
aoqi@0 263 }
aoqi@0 264 }
aoqi@0 265 #endif // ASSERT
aoqi@0 266
aoqi@0 267 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
aoqi@0 268 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
aoqi@0 269 #ifdef ASSERT
aoqi@0 270 verify_active_states(expected_active);
aoqi@0 271 #endif // ASSERT
aoqi@0 272 _all_active = active;
aoqi@0 273 for (JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 274 t->satb_mark_queue().set_active(active);
aoqi@0 275 }
aoqi@0 276 shared_satb_queue()->set_active(active);
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 void SATBMarkQueueSet::filter_thread_buffers() {
aoqi@0 280 for(JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 281 t->satb_mark_queue().filter();
aoqi@0 282 }
aoqi@0 283 shared_satb_queue()->filter();
aoqi@0 284 }
aoqi@0 285
kbarrett@7834 286 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) {
aoqi@0 287 BufferNode* nd = NULL;
aoqi@0 288 {
aoqi@0 289 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 290 if (_completed_buffers_head != NULL) {
aoqi@0 291 nd = _completed_buffers_head;
aoqi@0 292 _completed_buffers_head = nd->next();
aoqi@0 293 if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL;
aoqi@0 294 _n_completed_buffers--;
aoqi@0 295 if (_n_completed_buffers == 0) _process_completed = false;
aoqi@0 296 }
aoqi@0 297 }
aoqi@0 298 if (nd != NULL) {
aoqi@0 299 void **buf = BufferNode::make_buffer_from_node(nd);
kbarrett@7834 300 // Skip over NULL entries at beginning (e.g. push end) of buffer.
kbarrett@7834 301 // Filtering can result in non-full completed buffers; see
kbarrett@7834 302 // should_enqueue_buffer.
kbarrett@7834 303 assert(_sz % sizeof(void*) == 0, "invariant");
kbarrett@7834 304 size_t limit = ObjPtrQueue::byte_index_to_index((int)_sz);
kbarrett@7834 305 for (size_t i = 0; i < limit; ++i) {
kbarrett@7834 306 if (buf[i] != NULL) {
kbarrett@7834 307 // Found the end of the block of NULLs; process the remainder.
kbarrett@7834 308 cl->do_buffer(buf + i, limit - i);
kbarrett@7834 309 break;
kbarrett@7834 310 }
kbarrett@7834 311 }
aoqi@0 312 deallocate_buffer(buf);
aoqi@0 313 return true;
aoqi@0 314 } else {
aoqi@0 315 return false;
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 #ifndef PRODUCT
aoqi@0 320 // Helpful for debugging
aoqi@0 321
aoqi@0 322 #define SATB_PRINTER_BUFFER_SIZE 256
aoqi@0 323
aoqi@0 324 void SATBMarkQueueSet::print_all(const char* msg) {
aoqi@0 325 char buffer[SATB_PRINTER_BUFFER_SIZE];
aoqi@0 326 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
aoqi@0 327
aoqi@0 328 gclog_or_tty->cr();
aoqi@0 329 gclog_or_tty->print_cr("SATB BUFFERS [%s]", msg);
aoqi@0 330
aoqi@0 331 BufferNode* nd = _completed_buffers_head;
aoqi@0 332 int i = 0;
aoqi@0 333 while (nd != NULL) {
aoqi@0 334 void** buf = BufferNode::make_buffer_from_node(nd);
aoqi@0 335 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
aoqi@0 336 ObjPtrQueue::print(buffer, buf, 0, _sz);
aoqi@0 337 nd = nd->next();
aoqi@0 338 i += 1;
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 for (JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 342 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
aoqi@0 343 t->satb_mark_queue().print(buffer);
aoqi@0 344 }
aoqi@0 345
aoqi@0 346 shared_satb_queue()->print("Shared");
aoqi@0 347
aoqi@0 348 gclog_or_tty->cr();
aoqi@0 349 }
aoqi@0 350 #endif // PRODUCT
aoqi@0 351
aoqi@0 352 void SATBMarkQueueSet::abandon_partial_marking() {
aoqi@0 353 BufferNode* buffers_to_delete = NULL;
aoqi@0 354 {
aoqi@0 355 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 356 while (_completed_buffers_head != NULL) {
aoqi@0 357 BufferNode* nd = _completed_buffers_head;
aoqi@0 358 _completed_buffers_head = nd->next();
aoqi@0 359 nd->set_next(buffers_to_delete);
aoqi@0 360 buffers_to_delete = nd;
aoqi@0 361 }
aoqi@0 362 _completed_buffers_tail = NULL;
aoqi@0 363 _n_completed_buffers = 0;
aoqi@0 364 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
aoqi@0 365 }
aoqi@0 366 while (buffers_to_delete != NULL) {
aoqi@0 367 BufferNode* nd = buffers_to_delete;
aoqi@0 368 buffers_to_delete = nd->next();
aoqi@0 369 deallocate_buffer(BufferNode::make_buffer_from_node(nd));
aoqi@0 370 }
aoqi@0 371 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
aoqi@0 372 // So we can safely manipulate these queues.
aoqi@0 373 for (JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 374 t->satb_mark_queue().reset();
aoqi@0 375 }
aoqi@0 376 shared_satb_queue()->reset();
aoqi@0 377 }

mercurial