satbQueue.cpp 14.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25
#include "precompiled.hpp"
26
#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
27 28 29
#include "gc_implementation/g1/satbQueue.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/sharedHeap.hpp"
30
#include "oops/oop.inline.hpp"
31 32
#include "runtime/mutexLocker.hpp"
#include "runtime/thread.hpp"
33
#include "runtime/vmThread.hpp"
34

35 36
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC

37 38 39 40 41 42 43 44
void ObjPtrQueue::flush() {
  // The buffer might contain refs into the CSet. We have to filter it
  // first before we flush it, otherwise we might end up with an
  // enqueued buffer with refs into the CSet which breaks our invariants.
  filter();
  PtrQueue::flush();
}

45 46 47 48 49 50 51 52 53 54 55 56 57
// This method removes entries from an SATB buffer that will not be
// useful to the concurrent marking threads. An entry is removed if it
// satisfies one of the following conditions:
//
// * it points to an object outside the G1 heap (G1's concurrent
//     marking only visits objects inside the G1 heap),
// * it points to an object that has been allocated since marking
//     started (according to SATB those objects do not need to be
//     visited during marking), or
// * it points to an object that has already been marked (no need to
//     process it again).
//
// The rest of the entries will be retained and are compacted towards
58 59 60 61
// the top of the buffer. Note that, because we do not allow old
// regions in the CSet during marking, all objects on the CSet regions
// are young (eden or survivors) and therefore implicitly live. So any
// references into the CSet will be removed during filtering.
62

63
void ObjPtrQueue::filter() {
64 65 66 67
  G1CollectedHeap* g1h = G1CollectedHeap::heap();
  void** buf = _buf;
  size_t sz = _sz;

68 69 70 71 72
  if (buf == NULL) {
    // nothing to do
    return;
  }

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  // Used for sanity checking at the end of the loop.
  debug_only(size_t entries = 0; size_t retained = 0;)

  size_t i = sz;
  size_t new_index = sz;

  while (i > _index) {
    assert(i > 0, "we should have at least one more entry to process");
    i -= oopSize;
    debug_only(entries += 1;)
    oop* p = (oop*) &buf[byte_index_to_index((int) i)];
    oop obj = *p;
    // NULL the entry so that unused parts of the buffer contain NULLs
    // at the end. If we are going to retain it we will copy it to its
    // final place. If we have retained all entries we have visited so
    // far, we'll just end up copying it to the same place.
    *p = NULL;

    bool retain = g1h->is_obj_ill(obj);
    if (retain) {
      assert(new_index > 0, "we should not have already filled up the buffer");
      new_index -= oopSize;
      assert(new_index >= i,
             "new_index should never be below i, as we alwaysr compact 'up'");
      oop* new_p = (oop*) &buf[byte_index_to_index((int) new_index)];
      assert(new_p >= p, "the destination location should never be below "
             "the source as we always compact 'up'");
      assert(*new_p == NULL,
             "we should have already cleared the destination location");
      *new_p = obj;
      debug_only(retained += 1;)
    }
  }
106 107

#ifdef ASSERT
108 109 110 111 112 113
  size_t entries_calc = (sz - _index) / oopSize;
  assert(entries == entries_calc, "the number of entries we counted "
         "should match the number of entries we calculated");
  size_t retained_calc = (sz - new_index) / oopSize;
  assert(retained == retained_calc, "the number of retained entries we counted "
         "should match the number of retained entries we calculated");
114 115
#endif // ASSERT

116
  _index = new_index;
117 118 119 120 121 122 123 124 125 126 127
}

// This method will first apply the above filtering to the buffer. If
// post-filtering a large enough chunk of the buffer has been cleared
// we can re-use the buffer (instead of enqueueing it) and we can just
// allow the mutator to carry on executing using the same buffer
// instead of replacing it.

bool ObjPtrQueue::should_enqueue_buffer() {
  assert(_lock == NULL || _lock->owned_by_self(),
         "we should have taken the lock before calling this");
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  // Even if G1SATBBufferEnqueueingThresholdPercent == 0 we have to
  // filter the buffer given that this will remove any references into
  // the CSet as we currently assume that no such refs will appear in
  // enqueued buffers.

  // This method should only be called if there is a non-NULL buffer
  // that is full.
  assert(_index == 0, "pre-condition");
  assert(_buf != NULL, "pre-condition");

  filter();

  size_t sz = _sz;
  size_t all_entries = sz / oopSize;
  size_t retained_entries = (sz - _index) / oopSize;
  size_t perc = retained_entries * 100 / all_entries;
  bool should_enqueue = perc > (size_t) G1SATBBufferEnqueueingThresholdPercent;
146 147 148
  return should_enqueue;
}

149
void ObjPtrQueue::apply_closure(ObjectClosure* cl) {
150 151 152 153 154 155
  if (_buf != NULL) {
    apply_closure_to_buffer(cl, _buf, _index, _sz);
  }
}

void ObjPtrQueue::apply_closure_and_empty(ObjectClosure* cl) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  if (_buf != NULL) {
    apply_closure_to_buffer(cl, _buf, _index, _sz);
    _index = _sz;
  }
}

void ObjPtrQueue::apply_closure_to_buffer(ObjectClosure* cl,
                                          void** buf, size_t index, size_t sz) {
  if (cl == NULL) return;
  for (size_t i = index; i < sz; i += oopSize) {
    oop obj = (oop)buf[byte_index_to_index((int)i)];
    // There can be NULL entries because of destructors.
    if (obj != NULL) {
      cl->do_object(obj);
    }
  }
}
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#ifndef PRODUCT
// Helpful for debugging

void ObjPtrQueue::print(const char* name) {
  print(name, _buf, _index, _sz);
}

void ObjPtrQueue::print(const char* name,
                        void** buf, size_t index, size_t sz) {
  gclog_or_tty->print_cr("  SATB BUFFER [%s] buf: "PTR_FORMAT" "
                         "index: "SIZE_FORMAT" sz: "SIZE_FORMAT,
                         name, buf, index, sz);
}
#endif // PRODUCT

189 190 191 192 193 194 195 196 197 198 199
#ifdef ASSERT
void ObjPtrQueue::verify_oops_in_buffer() {
  if (_buf == NULL) return;
  for (size_t i = _index; i < _sz; i += oopSize) {
    oop obj = (oop)_buf[byte_index_to_index((int)i)];
    assert(obj != NULL && obj->is_oop(true /* ignore mark word */),
           "Not an oop");
  }
}
#endif

200 201 202 203 204
#ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
#pragma warning( disable:4355 ) // 'this' : used in base member initializer list
#endif // _MSC_VER

SATBMarkQueueSet::SATBMarkQueueSet() :
205 206
  PtrQueueSet(), _closure(NULL), _par_closures(NULL),
  _shared_satb_queue(this, true /*perm*/) { }
207 208

void SATBMarkQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock,
209
                                  int process_completed_threshold,
210
                                  Mutex* lock) {
211
  PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
212 213
  _shared_satb_queue.set_lock(lock);
  if (ParallelGCThreads > 0) {
Z
zgu 已提交
214
    _par_closures = NEW_C_HEAP_ARRAY(ObjectClosure*, ParallelGCThreads, mtGC);
215 216 217 218
  }
}

void SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
219
  DEBUG_ONLY(t->satb_mark_queue().verify_oops_in_buffer();)
220 221 222
  t->satb_mark_queue().handle_zero_index();
}

223
#ifdef ASSERT
224 225 226 227 228 229 230 231
void SATBMarkQueueSet::dump_active_states(bool expected_active) {
  gclog_or_tty->print_cr("Expected SATB active state: %s",
                         expected_active ? "ACTIVE" : "INACTIVE");
  gclog_or_tty->print_cr("Actual SATB active states:");
  gclog_or_tty->print_cr("  Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
  for (JavaThread* t = Threads::first(); t; t = t->next()) {
    gclog_or_tty->print_cr("  Thread \"%s\" queue: %s", t->name(),
                           t->satb_mark_queue().is_active() ? "ACTIVE" : "INACTIVE");
232
  }
233 234
  gclog_or_tty->print_cr("  Shared queue: %s",
                         shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE");
235 236
}

237 238 239 240 241 242
void SATBMarkQueueSet::verify_active_states(bool expected_active) {
  // Verify queue set state
  if (is_active() != expected_active) {
    dump_active_states(expected_active);
    guarantee(false, "SATB queue set has an unexpected active state");
  }
243

244 245 246 247 248 249
  // Verify thread queue states
  for (JavaThread* t = Threads::first(); t; t = t->next()) {
    if (t->satb_mark_queue().is_active() != expected_active) {
      dump_active_states(expected_active);
      guarantee(false, "Thread SATB queue has an unexpected active state");
    }
250
  }
251 252 253 254 255 256 257

  // Verify shared queue state
  if (shared_satb_queue()->is_active() != expected_active) {
    dump_active_states(expected_active);
    guarantee(false, "Shared SATB queue has an unexpected active state");
  }
}
258 259
#endif // ASSERT

260 261
void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
262
#ifdef ASSERT
263
  verify_active_states(expected_active);
264
#endif // ASSERT
265 266 267
  _all_active = active;
  for (JavaThread* t = Threads::first(); t; t = t->next()) {
    t->satb_mark_queue().set_active(active);
268
  }
269
  shared_satb_queue()->set_active(active);
270 271
}

272 273 274 275 276 277 278
void SATBMarkQueueSet::filter_thread_buffers() {
  for(JavaThread* t = Threads::first(); t; t = t->next()) {
    t->satb_mark_queue().filter();
  }
  shared_satb_queue()->filter();
}

279 280 281 282 283 284 285 286 287 288 289
void SATBMarkQueueSet::set_closure(ObjectClosure* closure) {
  _closure = closure;
}

void SATBMarkQueueSet::set_par_closure(int i, ObjectClosure* par_closure) {
  assert(ParallelGCThreads > 0 && _par_closures != NULL, "Precondition");
  _par_closures[i] = par_closure;
}

void SATBMarkQueueSet::iterate_closure_all_threads() {
  for(JavaThread* t = Threads::first(); t; t = t->next()) {
290
    t->satb_mark_queue().apply_closure_and_empty(_closure);
291
  }
292
  shared_satb_queue()->apply_closure_and_empty(_closure);
293 294
}

295
void SATBMarkQueueSet::par_iterate_closure_all_threads(uint worker) {
296 297 298 299 300
  SharedHeap* sh = SharedHeap::heap();
  int parity = sh->strong_roots_parity();

  for(JavaThread* t = Threads::first(); t; t = t->next()) {
    if (t->claim_oops_do(true, parity)) {
301
      t->satb_mark_queue().apply_closure_and_empty(_par_closures[worker]);
302 303
    }
  }
304 305 306 307 308 309 310 311 312 313 314

  // We also need to claim the VMThread so that its parity is updated
  // otherwise the next call to Thread::possibly_parallel_oops_do inside
  // a StrongRootsScope might skip the VMThread because it has a stale
  // parity that matches the parity set by the StrongRootsScope
  //
  // Whichever worker succeeds in claiming the VMThread gets to do
  // the shared queue.

  VMThread* vmt = VMThread::vm_thread();
  if (vmt->claim_oops_do(true, parity)) {
315
    shared_satb_queue()->apply_closure_and_empty(_par_closures[worker]);
316 317 318 319
  }
}

bool SATBMarkQueueSet::apply_closure_to_completed_buffer_work(bool par,
320
                                                              uint worker) {
321
  BufferNode* nd = NULL;
322 323 324 325
  {
    MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
    if (_completed_buffers_head != NULL) {
      nd = _completed_buffers_head;
326
      _completed_buffers_head = nd->next();
327 328 329 330 331 332 333
      if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL;
      _n_completed_buffers--;
      if (_n_completed_buffers == 0) _process_completed = false;
    }
  }
  ObjectClosure* cl = (par ? _par_closures[worker] : _closure);
  if (nd != NULL) {
334 335 336
    void **buf = BufferNode::make_buffer_from_node(nd);
    ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
    deallocate_buffer(buf);
337 338 339 340 341 342
    return true;
  } else {
    return false;
  }
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
void SATBMarkQueueSet::iterate_completed_buffers_read_only(ObjectClosure* cl) {
  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
  assert(cl != NULL, "pre-condition");

  BufferNode* nd = _completed_buffers_head;
  while (nd != NULL) {
    void** buf = BufferNode::make_buffer_from_node(nd);
    ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
    nd = nd->next();
  }
}

void SATBMarkQueueSet::iterate_thread_buffers_read_only(ObjectClosure* cl) {
  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
  assert(cl != NULL, "pre-condition");

  for (JavaThread* t = Threads::first(); t; t = t->next()) {
    t->satb_mark_queue().apply_closure(cl);
  }
  shared_satb_queue()->apply_closure(cl);
}

#ifndef PRODUCT
// Helpful for debugging

#define SATB_PRINTER_BUFFER_SIZE 256

void SATBMarkQueueSet::print_all(const char* msg) {
  char buffer[SATB_PRINTER_BUFFER_SIZE];
  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");

  gclog_or_tty->cr();
  gclog_or_tty->print_cr("SATB BUFFERS [%s]", msg);

  BufferNode* nd = _completed_buffers_head;
  int i = 0;
  while (nd != NULL) {
    void** buf = BufferNode::make_buffer_from_node(nd);
    jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
    ObjPtrQueue::print(buffer, buf, 0, _sz);
    nd = nd->next();
    i += 1;
  }

  for (JavaThread* t = Threads::first(); t; t = t->next()) {
    jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
    t->satb_mark_queue().print(buffer);
  }

  shared_satb_queue()->print("Shared");

  gclog_or_tty->cr();
}
#endif // PRODUCT

398
void SATBMarkQueueSet::abandon_partial_marking() {
399
  BufferNode* buffers_to_delete = NULL;
400 401 402
  {
    MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
    while (_completed_buffers_head != NULL) {
403 404 405
      BufferNode* nd = _completed_buffers_head;
      _completed_buffers_head = nd->next();
      nd->set_next(buffers_to_delete);
406 407 408 409
      buffers_to_delete = nd;
    }
    _completed_buffers_tail = NULL;
    _n_completed_buffers = 0;
410
    DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
411 412
  }
  while (buffers_to_delete != NULL) {
413 414 415
    BufferNode* nd = buffers_to_delete;
    buffers_to_delete = nd->next();
    deallocate_buffer(BufferNode::make_buffer_from_node(nd));
416 417 418 419 420 421
  }
  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
  // So we can safely manipulate these queues.
  for (JavaThread* t = Threads::first(); t; t = t->next()) {
    t->satb_mark_queue().reset();
  }
422
 shared_satb_queue()->reset();
423
}