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

changeset 3416
2ace1c4ee8da
parent 3175
4dfb2df418f2
child 3900
d2a62e0f25eb
     1.1 --- a/src/share/vm/gc_implementation/g1/satbQueue.cpp	Tue Jan 10 20:02:41 2012 +0100
     1.2 +++ b/src/share/vm/gc_implementation/g1/satbQueue.cpp	Tue Jan 10 18:58:13 2012 -0500
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -31,6 +31,14 @@
    1.11  #include "runtime/thread.hpp"
    1.12  #include "runtime/vmThread.hpp"
    1.13  
    1.14 +void ObjPtrQueue::flush() {
    1.15 +  // The buffer might contain refs into the CSet. We have to filter it
    1.16 +  // first before we flush it, otherwise we might end up with an
    1.17 +  // enqueued buffer with refs into the CSet which breaks our invariants.
    1.18 +  filter();
    1.19 +  PtrQueue::flush();
    1.20 +}
    1.21 +
    1.22  // This method removes entries from an SATB buffer that will not be
    1.23  // useful to the concurrent marking threads. An entry is removed if it
    1.24  // satisfies one of the following conditions:
    1.25 @@ -44,38 +52,27 @@
    1.26  //     process it again).
    1.27  //
    1.28  // The rest of the entries will be retained and are compacted towards
    1.29 -// the top of the buffer. If with this filtering we clear a large
    1.30 -// enough chunk of the buffer we can re-use it (instead of enqueueing
    1.31 -// it) and we can just allow the mutator to carry on executing.
    1.32 +// the top of the buffer. Note that, because we do not allow old
    1.33 +// regions in the CSet during marking, all objects on the CSet regions
    1.34 +// are young (eden or survivors) and therefore implicitly live. So any
    1.35 +// references into the CSet will be removed during filtering.
    1.36  
    1.37 -bool ObjPtrQueue::should_enqueue_buffer() {
    1.38 -  assert(_lock == NULL || _lock->owned_by_self(),
    1.39 -         "we should have taken the lock before calling this");
    1.40 -
    1.41 -  // A value of 0 means "don't filter SATB buffers".
    1.42 -  if (G1SATBBufferEnqueueingThresholdPercent == 0) {
    1.43 -    return true;
    1.44 -  }
    1.45 -
    1.46 +void ObjPtrQueue::filter() {
    1.47    G1CollectedHeap* g1h = G1CollectedHeap::heap();
    1.48 -
    1.49 -  // This method should only be called if there is a non-NULL buffer
    1.50 -  // that is full.
    1.51 -  assert(_index == 0, "pre-condition");
    1.52 -  assert(_buf != NULL, "pre-condition");
    1.53 -
    1.54    void** buf = _buf;
    1.55    size_t sz = _sz;
    1.56  
    1.57 +  if (buf == NULL) {
    1.58 +    // nothing to do
    1.59 +    return;
    1.60 +  }
    1.61 +
    1.62    // Used for sanity checking at the end of the loop.
    1.63    debug_only(size_t entries = 0; size_t retained = 0;)
    1.64  
    1.65    size_t i = sz;
    1.66    size_t new_index = sz;
    1.67  
    1.68 -  // Given that we are expecting _index == 0, we could have changed
    1.69 -  // the loop condition to (i > 0). But we are using _index for
    1.70 -  // generality.
    1.71    while (i > _index) {
    1.72      assert(i > 0, "we should have at least one more entry to process");
    1.73      i -= oopSize;
    1.74 @@ -103,22 +100,58 @@
    1.75        debug_only(retained += 1;)
    1.76      }
    1.77    }
    1.78 +
    1.79 +#ifdef ASSERT
    1.80    size_t entries_calc = (sz - _index) / oopSize;
    1.81    assert(entries == entries_calc, "the number of entries we counted "
    1.82           "should match the number of entries we calculated");
    1.83    size_t retained_calc = (sz - new_index) / oopSize;
    1.84    assert(retained == retained_calc, "the number of retained entries we counted "
    1.85           "should match the number of retained entries we calculated");
    1.86 -  size_t perc = retained_calc * 100 / entries_calc;
    1.87 +#endif // ASSERT
    1.88 +
    1.89 +  _index = new_index;
    1.90 +}
    1.91 +
    1.92 +// This method will first apply the above filtering to the buffer. If
    1.93 +// post-filtering a large enough chunk of the buffer has been cleared
    1.94 +// we can re-use the buffer (instead of enqueueing it) and we can just
    1.95 +// allow the mutator to carry on executing using the same buffer
    1.96 +// instead of replacing it.
    1.97 +
    1.98 +bool ObjPtrQueue::should_enqueue_buffer() {
    1.99 +  assert(_lock == NULL || _lock->owned_by_self(),
   1.100 +         "we should have taken the lock before calling this");
   1.101 +
   1.102 +  // Even if G1SATBBufferEnqueueingThresholdPercent == 0 we have to
   1.103 +  // filter the buffer given that this will remove any references into
   1.104 +  // the CSet as we currently assume that no such refs will appear in
   1.105 +  // enqueued buffers.
   1.106 +
   1.107 +  // This method should only be called if there is a non-NULL buffer
   1.108 +  // that is full.
   1.109 +  assert(_index == 0, "pre-condition");
   1.110 +  assert(_buf != NULL, "pre-condition");
   1.111 +
   1.112 +  filter();
   1.113 +
   1.114 +  size_t sz = _sz;
   1.115 +  size_t all_entries = sz / oopSize;
   1.116 +  size_t retained_entries = (sz - _index) / oopSize;
   1.117 +  size_t perc = retained_entries * 100 / all_entries;
   1.118    bool should_enqueue = perc > (size_t) G1SATBBufferEnqueueingThresholdPercent;
   1.119 -  _index = new_index;
   1.120 -
   1.121    return should_enqueue;
   1.122  }
   1.123  
   1.124  void ObjPtrQueue::apply_closure(ObjectClosure* cl) {
   1.125    if (_buf != NULL) {
   1.126      apply_closure_to_buffer(cl, _buf, _index, _sz);
   1.127 +  }
   1.128 +}
   1.129 +
   1.130 +void ObjPtrQueue::apply_closure_and_empty(ObjectClosure* cl) {
   1.131 +  if (_buf != NULL) {
   1.132 +    apply_closure_to_buffer(cl, _buf, _index, _sz);
   1.133      _index = _sz;
   1.134    }
   1.135  }
   1.136 @@ -135,6 +168,21 @@
   1.137    }
   1.138  }
   1.139  
   1.140 +#ifndef PRODUCT
   1.141 +// Helpful for debugging
   1.142 +
   1.143 +void ObjPtrQueue::print(const char* name) {
   1.144 +  print(name, _buf, _index, _sz);
   1.145 +}
   1.146 +
   1.147 +void ObjPtrQueue::print(const char* name,
   1.148 +                        void** buf, size_t index, size_t sz) {
   1.149 +  gclog_or_tty->print_cr("  SATB BUFFER [%s] buf: "PTR_FORMAT" "
   1.150 +                         "index: "SIZE_FORMAT" sz: "SIZE_FORMAT,
   1.151 +                         name, buf, index, sz);
   1.152 +}
   1.153 +#endif // PRODUCT
   1.154 +
   1.155  #ifdef ASSERT
   1.156  void ObjPtrQueue::verify_oops_in_buffer() {
   1.157    if (_buf == NULL) return;
   1.158 @@ -150,12 +198,9 @@
   1.159  #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
   1.160  #endif // _MSC_VER
   1.161  
   1.162 -
   1.163  SATBMarkQueueSet::SATBMarkQueueSet() :
   1.164 -  PtrQueueSet(),
   1.165 -  _closure(NULL), _par_closures(NULL),
   1.166 -  _shared_satb_queue(this, true /*perm*/)
   1.167 -{}
   1.168 +  PtrQueueSet(), _closure(NULL), _par_closures(NULL),
   1.169 +  _shared_satb_queue(this, true /*perm*/) { }
   1.170  
   1.171  void SATBMarkQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock,
   1.172                                    int process_completed_threshold,
   1.173 @@ -167,7 +212,6 @@
   1.174    }
   1.175  }
   1.176  
   1.177 -
   1.178  void SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
   1.179    DEBUG_ONLY(t->satb_mark_queue().verify_oops_in_buffer();)
   1.180    t->satb_mark_queue().handle_zero_index();
   1.181 @@ -228,6 +272,13 @@
   1.182    }
   1.183  }
   1.184  
   1.185 +void SATBMarkQueueSet::filter_thread_buffers() {
   1.186 +  for(JavaThread* t = Threads::first(); t; t = t->next()) {
   1.187 +    t->satb_mark_queue().filter();
   1.188 +  }
   1.189 +  shared_satb_queue()->filter();
   1.190 +}
   1.191 +
   1.192  void SATBMarkQueueSet::set_closure(ObjectClosure* closure) {
   1.193    _closure = closure;
   1.194  }
   1.195 @@ -239,9 +290,9 @@
   1.196  
   1.197  void SATBMarkQueueSet::iterate_closure_all_threads() {
   1.198    for(JavaThread* t = Threads::first(); t; t = t->next()) {
   1.199 -    t->satb_mark_queue().apply_closure(_closure);
   1.200 +    t->satb_mark_queue().apply_closure_and_empty(_closure);
   1.201    }
   1.202 -  shared_satb_queue()->apply_closure(_closure);
   1.203 +  shared_satb_queue()->apply_closure_and_empty(_closure);
   1.204  }
   1.205  
   1.206  void SATBMarkQueueSet::par_iterate_closure_all_threads(int worker) {
   1.207 @@ -250,7 +301,7 @@
   1.208  
   1.209    for(JavaThread* t = Threads::first(); t; t = t->next()) {
   1.210      if (t->claim_oops_do(true, parity)) {
   1.211 -      t->satb_mark_queue().apply_closure(_par_closures[worker]);
   1.212 +      t->satb_mark_queue().apply_closure_and_empty(_par_closures[worker]);
   1.213      }
   1.214    }
   1.215  
   1.216 @@ -264,7 +315,7 @@
   1.217  
   1.218    VMThread* vmt = VMThread::vm_thread();
   1.219    if (vmt->claim_oops_do(true, parity)) {
   1.220 -    shared_satb_queue()->apply_closure(_par_closures[worker]);
   1.221 +    shared_satb_queue()->apply_closure_and_empty(_par_closures[worker]);
   1.222    }
   1.223  }
   1.224  
   1.225 @@ -292,6 +343,61 @@
   1.226    }
   1.227  }
   1.228  
   1.229 +void SATBMarkQueueSet::iterate_completed_buffers_read_only(ObjectClosure* cl) {
   1.230 +  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
   1.231 +  assert(cl != NULL, "pre-condition");
   1.232 +
   1.233 +  BufferNode* nd = _completed_buffers_head;
   1.234 +  while (nd != NULL) {
   1.235 +    void** buf = BufferNode::make_buffer_from_node(nd);
   1.236 +    ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
   1.237 +    nd = nd->next();
   1.238 +  }
   1.239 +}
   1.240 +
   1.241 +void SATBMarkQueueSet::iterate_thread_buffers_read_only(ObjectClosure* cl) {
   1.242 +  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
   1.243 +  assert(cl != NULL, "pre-condition");
   1.244 +
   1.245 +  for (JavaThread* t = Threads::first(); t; t = t->next()) {
   1.246 +    t->satb_mark_queue().apply_closure(cl);
   1.247 +  }
   1.248 +  shared_satb_queue()->apply_closure(cl);
   1.249 +}
   1.250 +
   1.251 +#ifndef PRODUCT
   1.252 +// Helpful for debugging
   1.253 +
   1.254 +#define SATB_PRINTER_BUFFER_SIZE 256
   1.255 +
   1.256 +void SATBMarkQueueSet::print_all(const char* msg) {
   1.257 +  char buffer[SATB_PRINTER_BUFFER_SIZE];
   1.258 +  assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
   1.259 +
   1.260 +  gclog_or_tty->cr();
   1.261 +  gclog_or_tty->print_cr("SATB BUFFERS [%s]", msg);
   1.262 +
   1.263 +  BufferNode* nd = _completed_buffers_head;
   1.264 +  int i = 0;
   1.265 +  while (nd != NULL) {
   1.266 +    void** buf = BufferNode::make_buffer_from_node(nd);
   1.267 +    jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
   1.268 +    ObjPtrQueue::print(buffer, buf, 0, _sz);
   1.269 +    nd = nd->next();
   1.270 +    i += 1;
   1.271 +  }
   1.272 +
   1.273 +  for (JavaThread* t = Threads::first(); t; t = t->next()) {
   1.274 +    jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
   1.275 +    t->satb_mark_queue().print(buffer);
   1.276 +  }
   1.277 +
   1.278 +  shared_satb_queue()->print("Shared");
   1.279 +
   1.280 +  gclog_or_tty->cr();
   1.281 +}
   1.282 +#endif // PRODUCT
   1.283 +
   1.284  void SATBMarkQueueSet::abandon_partial_marking() {
   1.285    BufferNode* buffers_to_delete = NULL;
   1.286    {
   1.287 @@ -316,5 +422,5 @@
   1.288    for (JavaThread* t = Threads::first(); t; t = t->next()) {
   1.289      t->satb_mark_queue().reset();
   1.290    }
   1.291 -  shared_satb_queue()->reset();
   1.292 + shared_satb_queue()->reset();
   1.293  }

mercurial