src/share/vm/gc_implementation/g1/ptrQueue.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/gc_implementation/g1/ptrQueue.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,316 @@
     1.4 +/*
     1.5 + * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
    1.29 +#define SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
    1.30 +
    1.31 +#include "memory/allocation.hpp"
    1.32 +#include "utilities/sizes.hpp"
    1.33 +
    1.34 +// There are various techniques that require threads to be able to log
    1.35 +// addresses.  For example, a generational write barrier might log
    1.36 +// the addresses of modified old-generation objects.  This type supports
    1.37 +// this operation.
    1.38 +
    1.39 +// The definition of placement operator new(size_t, void*) in the <new>.
    1.40 +#include <new>
    1.41 +
    1.42 +class PtrQueueSet;
    1.43 +class PtrQueue VALUE_OBJ_CLASS_SPEC {
    1.44 +  friend class VMStructs;
    1.45 +
    1.46 +protected:
    1.47 +  // The ptr queue set to which this queue belongs.
    1.48 +  PtrQueueSet* _qset;
    1.49 +
    1.50 +  // Whether updates should be logged.
    1.51 +  bool _active;
    1.52 +
    1.53 +  // The buffer.
    1.54 +  void** _buf;
    1.55 +  // The index at which an object was last enqueued.  Starts at "_sz"
    1.56 +  // (indicating an empty buffer) and goes towards zero.
    1.57 +  size_t _index;
    1.58 +
    1.59 +  // The size of the buffer.
    1.60 +  size_t _sz;
    1.61 +
    1.62 +  // If true, the queue is permanent, and doesn't need to deallocate
    1.63 +  // its buffer in the destructor (since that obtains a lock which may not
    1.64 +  // be legally locked by then.
    1.65 +  bool _perm;
    1.66 +
    1.67 +  // If there is a lock associated with this buffer, this is that lock.
    1.68 +  Mutex* _lock;
    1.69 +
    1.70 +  PtrQueueSet* qset() { return _qset; }
    1.71 +
    1.72 +public:
    1.73 +  // Initialize this queue to contain a null buffer, and be part of the
    1.74 +  // given PtrQueueSet.
    1.75 +  PtrQueue(PtrQueueSet* qset, bool perm = false, bool active = false);
    1.76 +  // Release any contained resources.
    1.77 +  virtual void flush();
    1.78 +  // Calls flush() when destroyed.
    1.79 +  ~PtrQueue() { flush(); }
    1.80 +
    1.81 +  // Associate a lock with a ptr queue.
    1.82 +  void set_lock(Mutex* lock) { _lock = lock; }
    1.83 +
    1.84 +  void reset() { if (_buf != NULL) _index = _sz; }
    1.85 +
    1.86 +  void enqueue(volatile void* ptr) {
    1.87 +    enqueue((void*)(ptr));
    1.88 +  }
    1.89 +
    1.90 +  // Enqueues the given "obj".
    1.91 +  void enqueue(void* ptr) {
    1.92 +    if (!_active) return;
    1.93 +    else enqueue_known_active(ptr);
    1.94 +  }
    1.95 +
    1.96 +  // This method is called when we're doing the zero index handling
    1.97 +  // and gives a chance to the queues to do any pre-enqueueing
    1.98 +  // processing they might want to do on the buffer. It should return
    1.99 +  // true if the buffer should be enqueued, or false if enough
   1.100 +  // entries were cleared from it so that it can be re-used. It should
   1.101 +  // not return false if the buffer is still full (otherwise we can
   1.102 +  // get into an infinite loop).
   1.103 +  virtual bool should_enqueue_buffer() { return true; }
   1.104 +  void handle_zero_index();
   1.105 +  void locking_enqueue_completed_buffer(void** buf);
   1.106 +
   1.107 +  void enqueue_known_active(void* ptr);
   1.108 +
   1.109 +  size_t size() {
   1.110 +    assert(_sz >= _index, "Invariant.");
   1.111 +    return _buf == NULL ? 0 : _sz - _index;
   1.112 +  }
   1.113 +
   1.114 +  bool is_empty() {
   1.115 +    return _buf == NULL || _sz == _index;
   1.116 +  }
   1.117 +
   1.118 +  // Set the "active" property of the queue to "b".  An enqueue to an
   1.119 +  // inactive thread is a no-op.  Setting a queue to inactive resets its
   1.120 +  // log to the empty state.
   1.121 +  void set_active(bool b) {
   1.122 +    _active = b;
   1.123 +    if (!b && _buf != NULL) {
   1.124 +      _index = _sz;
   1.125 +    } else if (b && _buf != NULL) {
   1.126 +      assert(_index == _sz, "invariant: queues are empty when activated.");
   1.127 +    }
   1.128 +  }
   1.129 +
   1.130 +  bool is_active() { return _active; }
   1.131 +
   1.132 +  static int byte_index_to_index(int ind) {
   1.133 +    assert((ind % oopSize) == 0, "Invariant.");
   1.134 +    return ind / oopSize;
   1.135 +  }
   1.136 +
   1.137 +  static int index_to_byte_index(int byte_ind) {
   1.138 +    return byte_ind * oopSize;
   1.139 +  }
   1.140 +
   1.141 +  // To support compiler.
   1.142 +  static ByteSize byte_offset_of_index() {
   1.143 +    return byte_offset_of(PtrQueue, _index);
   1.144 +  }
   1.145 +  static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
   1.146 +
   1.147 +  static ByteSize byte_offset_of_buf() {
   1.148 +    return byte_offset_of(PtrQueue, _buf);
   1.149 +  }
   1.150 +  static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }
   1.151 +
   1.152 +  static ByteSize byte_offset_of_active() {
   1.153 +    return byte_offset_of(PtrQueue, _active);
   1.154 +  }
   1.155 +  static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
   1.156 +
   1.157 +};
   1.158 +
   1.159 +class BufferNode {
   1.160 +  size_t _index;
   1.161 +  BufferNode* _next;
   1.162 +public:
   1.163 +  BufferNode() : _index(0), _next(NULL) { }
   1.164 +  BufferNode* next() const     { return _next;  }
   1.165 +  void set_next(BufferNode* n) { _next = n;     }
   1.166 +  size_t index() const         { return _index; }
   1.167 +  void set_index(size_t i)     { _index = i;    }
   1.168 +
   1.169 +  // Align the size of the structure to the size of the pointer
   1.170 +  static size_t aligned_size() {
   1.171 +    static const size_t alignment = round_to(sizeof(BufferNode), sizeof(void*));
   1.172 +    return alignment;
   1.173 +  }
   1.174 +
   1.175 +  // BufferNode is allocated before the buffer.
   1.176 +  // The chunk of memory that holds both of them is a block.
   1.177 +
   1.178 +  // Produce a new BufferNode given a buffer.
   1.179 +  static BufferNode* new_from_buffer(void** buf) {
   1.180 +    return new (make_block_from_buffer(buf)) BufferNode;
   1.181 +  }
   1.182 +
   1.183 +  // The following are the required conversion routines:
   1.184 +  static BufferNode* make_node_from_buffer(void** buf) {
   1.185 +    return (BufferNode*)make_block_from_buffer(buf);
   1.186 +  }
   1.187 +  static void** make_buffer_from_node(BufferNode *node) {
   1.188 +    return make_buffer_from_block(node);
   1.189 +  }
   1.190 +  static void* make_block_from_node(BufferNode *node) {
   1.191 +    return (void*)node;
   1.192 +  }
   1.193 +  static void** make_buffer_from_block(void* p) {
   1.194 +    return (void**)((char*)p + aligned_size());
   1.195 +  }
   1.196 +  static void* make_block_from_buffer(void** p) {
   1.197 +    return (void*)((char*)p - aligned_size());
   1.198 +  }
   1.199 +};
   1.200 +
   1.201 +// A PtrQueueSet represents resources common to a set of pointer queues.
   1.202 +// In particular, the individual queues allocate buffers from this shared
   1.203 +// set, and return completed buffers to the set.
   1.204 +// All these variables are are protected by the TLOQ_CBL_mon. XXX ???
   1.205 +class PtrQueueSet VALUE_OBJ_CLASS_SPEC {
   1.206 +protected:
   1.207 +  Monitor* _cbl_mon;  // Protects the fields below.
   1.208 +  BufferNode* _completed_buffers_head;
   1.209 +  BufferNode* _completed_buffers_tail;
   1.210 +  int _n_completed_buffers;
   1.211 +  int _process_completed_threshold;
   1.212 +  volatile bool _process_completed;
   1.213 +
   1.214 +  // This (and the interpretation of the first element as a "next"
   1.215 +  // pointer) are protected by the TLOQ_FL_lock.
   1.216 +  Mutex* _fl_lock;
   1.217 +  BufferNode* _buf_free_list;
   1.218 +  size_t _buf_free_list_sz;
   1.219 +  // Queue set can share a freelist. The _fl_owner variable
   1.220 +  // specifies the owner. It is set to "this" by default.
   1.221 +  PtrQueueSet* _fl_owner;
   1.222 +
   1.223 +  // The size of all buffers in the set.
   1.224 +  size_t _sz;
   1.225 +
   1.226 +  bool _all_active;
   1.227 +
   1.228 +  // If true, notify_all on _cbl_mon when the threshold is reached.
   1.229 +  bool _notify_when_complete;
   1.230 +
   1.231 +  // Maximum number of elements allowed on completed queue: after that,
   1.232 +  // enqueuer does the work itself.  Zero indicates no maximum.
   1.233 +  int _max_completed_queue;
   1.234 +  int _completed_queue_padding;
   1.235 +
   1.236 +  int completed_buffers_list_length();
   1.237 +  void assert_completed_buffer_list_len_correct_locked();
   1.238 +  void assert_completed_buffer_list_len_correct();
   1.239 +
   1.240 +protected:
   1.241 +  // A mutator thread does the the work of processing a buffer.
   1.242 +  // Returns "true" iff the work is complete (and the buffer may be
   1.243 +  // deallocated).
   1.244 +  virtual bool mut_process_buffer(void** buf) {
   1.245 +    ShouldNotReachHere();
   1.246 +    return false;
   1.247 +  }
   1.248 +
   1.249 +public:
   1.250 +  // Create an empty ptr queue set.
   1.251 +  PtrQueueSet(bool notify_when_complete = false);
   1.252 +
   1.253 +  // Because of init-order concerns, we can't pass these as constructor
   1.254 +  // arguments.
   1.255 +  void initialize(Monitor* cbl_mon, Mutex* fl_lock,
   1.256 +                  int process_completed_threshold,
   1.257 +                  int max_completed_queue,
   1.258 +                  PtrQueueSet *fl_owner = NULL) {
   1.259 +    _max_completed_queue = max_completed_queue;
   1.260 +    _process_completed_threshold = process_completed_threshold;
   1.261 +    _completed_queue_padding = 0;
   1.262 +    assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
   1.263 +    _cbl_mon = cbl_mon;
   1.264 +    _fl_lock = fl_lock;
   1.265 +    _fl_owner = (fl_owner != NULL) ? fl_owner : this;
   1.266 +  }
   1.267 +
   1.268 +  // Return an empty oop array of size _sz (required to be non-zero).
   1.269 +  void** allocate_buffer();
   1.270 +
   1.271 +  // Return an empty buffer to the free list.  The "buf" argument is
   1.272 +  // required to be a pointer to the head of an array of length "_sz".
   1.273 +  void deallocate_buffer(void** buf);
   1.274 +
   1.275 +  // Declares that "buf" is a complete buffer.
   1.276 +  void enqueue_complete_buffer(void** buf, size_t index = 0);
   1.277 +
   1.278 +  // To be invoked by the mutator.
   1.279 +  bool process_or_enqueue_complete_buffer(void** buf);
   1.280 +
   1.281 +  bool completed_buffers_exist_dirty() {
   1.282 +    return _n_completed_buffers > 0;
   1.283 +  }
   1.284 +
   1.285 +  bool process_completed_buffers() { return _process_completed; }
   1.286 +  void set_process_completed(bool x) { _process_completed = x; }
   1.287 +
   1.288 +  bool is_active() { return _all_active; }
   1.289 +
   1.290 +  // Set the buffer size.  Should be called before any "enqueue" operation
   1.291 +  // can be called.  And should only be called once.
   1.292 +  void set_buffer_size(size_t sz);
   1.293 +
   1.294 +  // Get the buffer size.
   1.295 +  size_t buffer_size() { return _sz; }
   1.296 +
   1.297 +  // Get/Set the number of completed buffers that triggers log processing.
   1.298 +  void set_process_completed_threshold(int sz) { _process_completed_threshold = sz; }
   1.299 +  int process_completed_threshold() const { return _process_completed_threshold; }
   1.300 +
   1.301 +  // Must only be called at a safe point.  Indicates that the buffer free
   1.302 +  // list size may be reduced, if that is deemed desirable.
   1.303 +  void reduce_free_list();
   1.304 +
   1.305 +  int completed_buffers_num() { return _n_completed_buffers; }
   1.306 +
   1.307 +  void merge_bufferlists(PtrQueueSet* src);
   1.308 +
   1.309 +  void set_max_completed_queue(int m) { _max_completed_queue = m; }
   1.310 +  int max_completed_queue() { return _max_completed_queue; }
   1.311 +
   1.312 +  void set_completed_queue_padding(int padding) { _completed_queue_padding = padding; }
   1.313 +  int completed_queue_padding() { return _completed_queue_padding; }
   1.314 +
   1.315 +  // Notify the consumer if the number of buffers crossed the threshold
   1.316 +  void notify_if_necessary();
   1.317 +};
   1.318 +
   1.319 +#endif // SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP

mercurial