1 /*
   2  * Copyright (c) 2001, 2010, 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/sizes.hpp"
  30 
  31 // There are various techniques that require threads to be able to log
  32 // addresses.  For example, a generational write barrier might log
  33 // the addresses of modified old-generation objects.  This type supports
  34 // this operation.
  35 
  36 // The definition of placement operator new(size_t, void*) in the <new>.
  37 #include <new>
  38 
  39 class PtrQueueSet;
  40 class PtrQueue VALUE_OBJ_CLASS_SPEC {
  41 
  42 protected:
  43   // The ptr queue set to which this queue belongs.
  44   PtrQueueSet* _qset;
  45 
  46   // Whether updates should be logged.
  47   bool _active;
  48 
  49   // The buffer.
  50   void** _buf;
  51   // The index at which an object was last enqueued.  Starts at "_sz"
  52   // (indicating an empty buffer) and goes towards zero.
  53   size_t _index;
  54 
  55   // The size of the buffer.
  56   size_t _sz;
  57 
  58   // If true, the queue is permanent, and doesn't need to deallocate
  59   // its buffer in the destructor (since that obtains a lock which may not
  60   // be legally locked by then.
  61   bool _perm;
  62 
  63   // If there is a lock associated with this buffer, this is that lock.
  64   Mutex* _lock;
  65 
  66   PtrQueueSet* qset() { return _qset; }
  67 
  68 public:
  69   // Initialize this queue to contain a null buffer, and be part of the
  70   // given PtrQueueSet.
  71   PtrQueue(PtrQueueSet*, bool perm = false, bool active = false);
  72   // Release any contained resources.
  73   void flush();
  74   // Calls flush() when destroyed.
  75   ~PtrQueue() { flush(); }
  76 
  77   // Associate a lock with a ptr queue.
  78   void set_lock(Mutex* lock) { _lock = lock; }
  79 
  80   void reset() { if (_buf != NULL) _index = _sz; }
  81 
  82   // Enqueues the given "obj".
  83   void enqueue(void* ptr) {
  84     if (!_active) return;
  85     else enqueue_known_active(ptr);
  86   }
  87 
  88   void handle_zero_index();
  89   void locking_enqueue_completed_buffer(void** buf);
  90 
  91   void enqueue_known_active(void* ptr);
  92 
  93   size_t size() {
  94     assert(_sz >= _index, "Invariant.");
  95     return _buf == NULL ? 0 : _sz - _index;
  96   }
  97 
  98   bool is_empty() {
  99     return _buf == NULL || _sz == _index;
 100   }
 101 
 102   // Set the "active" property of the queue to "b".  An enqueue to an
 103   // inactive thread is a no-op.  Setting a queue to inactive resets its
 104   // log to the empty state.
 105   void set_active(bool b) {
 106     _active = b;
 107     if (!b && _buf != NULL) {
 108       _index = _sz;
 109     } else if (b && _buf != NULL) {
 110       assert(_index == _sz, "invariant: queues are empty when activated.");
 111     }
 112   }
 113 
 114   bool is_active() { return _active; }
 115 
 116   static int byte_index_to_index(int ind) {
 117     assert((ind % oopSize) == 0, "Invariant.");
 118     return ind / oopSize;
 119   }
 120 
 121   static int index_to_byte_index(int byte_ind) {
 122     return byte_ind * oopSize;
 123   }
 124 
 125   // To support compiler.
 126   static ByteSize byte_offset_of_index() {
 127     return byte_offset_of(PtrQueue, _index);
 128   }
 129   static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
 130 
 131   static ByteSize byte_offset_of_buf() {
 132     return byte_offset_of(PtrQueue, _buf);
 133   }
 134   static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }
 135 
 136   static ByteSize byte_offset_of_active() {
 137     return byte_offset_of(PtrQueue, _active);
 138   }
 139   static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
 140 
 141 };
 142 
 143 class BufferNode {
 144   size_t _index;
 145   BufferNode* _next;
 146 public:
 147   BufferNode() : _index(0), _next(NULL) { }
 148   BufferNode* next() const     { return _next;  }
 149   void set_next(BufferNode* n) { _next = n;     }
 150   size_t index() const         { return _index; }
 151   void set_index(size_t i)     { _index = i;    }
 152 
 153   // Align the size of the structure to the size of the pointer
 154   static size_t aligned_size() {
 155     static const size_t alignment = round_to(sizeof(BufferNode), sizeof(void*));
 156     return alignment;
 157   }
 158 
 159   // BufferNode is allocated before the buffer.
 160   // The chunk of memory that holds both of them is a block.
 161 
 162   // Produce a new BufferNode given a buffer.
 163   static BufferNode* new_from_buffer(void** buf) {
 164     return new (make_block_from_buffer(buf)) BufferNode;
 165   }
 166 
 167   // The following are the required conversion routines:
 168   static BufferNode* make_node_from_buffer(void** buf) {
 169     return (BufferNode*)make_block_from_buffer(buf);
 170   }
 171   static void** make_buffer_from_node(BufferNode *node) {
 172     return make_buffer_from_block(node);
 173   }
 174   static void* make_block_from_node(BufferNode *node) {
 175     return (void*)node;
 176   }
 177   static void** make_buffer_from_block(void* p) {
 178     return (void**)((char*)p + aligned_size());
 179   }
 180   static void* make_block_from_buffer(void** p) {
 181     return (void*)((char*)p - aligned_size());
 182   }
 183 };
 184 
 185 // A PtrQueueSet represents resources common to a set of pointer queues.
 186 // In particular, the individual queues allocate buffers from this shared
 187 // set, and return completed buffers to the set.
 188 // All these variables are are protected by the TLOQ_CBL_mon. XXX ???
 189 class PtrQueueSet VALUE_OBJ_CLASS_SPEC {
 190 protected:
 191   Monitor* _cbl_mon;  // Protects the fields below.
 192   BufferNode* _completed_buffers_head;
 193   BufferNode* _completed_buffers_tail;
 194   int _n_completed_buffers;
 195   int _process_completed_threshold;
 196   volatile bool _process_completed;
 197 
 198   // This (and the interpretation of the first element as a "next"
 199   // pointer) are protected by the TLOQ_FL_lock.
 200   Mutex* _fl_lock;
 201   BufferNode* _buf_free_list;
 202   size_t _buf_free_list_sz;
 203   // Queue set can share a freelist. The _fl_owner variable
 204   // specifies the owner. It is set to "this" by default.
 205   PtrQueueSet* _fl_owner;
 206 
 207   // The size of all buffers in the set.
 208   size_t _sz;
 209 
 210   bool _all_active;
 211 
 212   // If true, notify_all on _cbl_mon when the threshold is reached.
 213   bool _notify_when_complete;
 214 
 215   // Maximum number of elements allowed on completed queue: after that,
 216   // enqueuer does the work itself.  Zero indicates no maximum.
 217   int _max_completed_queue;
 218   int _completed_queue_padding;
 219 
 220   int completed_buffers_list_length();
 221   void assert_completed_buffer_list_len_correct_locked();
 222   void assert_completed_buffer_list_len_correct();
 223 
 224 protected:
 225   // A mutator thread does the the work of processing a buffer.
 226   // Returns "true" iff the work is complete (and the buffer may be
 227   // deallocated).
 228   virtual bool mut_process_buffer(void** buf) {
 229     ShouldNotReachHere();
 230     return false;
 231   }
 232 
 233 public:
 234   // Create an empty ptr queue set.
 235   PtrQueueSet(bool notify_when_complete = false);
 236 
 237   // Because of init-order concerns, we can't pass these as constructor
 238   // arguments.
 239   void initialize(Monitor* cbl_mon, Mutex* fl_lock,
 240                   int process_completed_threshold,
 241                   int max_completed_queue,
 242                   PtrQueueSet *fl_owner = NULL) {
 243     _max_completed_queue = max_completed_queue;
 244     _process_completed_threshold = process_completed_threshold;
 245     _completed_queue_padding = 0;
 246     assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
 247     _cbl_mon = cbl_mon;
 248     _fl_lock = fl_lock;
 249     _fl_owner = (fl_owner != NULL) ? fl_owner : this;
 250   }
 251 
 252   // Return an empty oop array of size _sz (required to be non-zero).
 253   void** allocate_buffer();
 254 
 255   // Return an empty buffer to the free list.  The "buf" argument is
 256   // required to be a pointer to the head of an array of length "_sz".
 257   void deallocate_buffer(void** buf);
 258 
 259   // Declares that "buf" is a complete buffer.
 260   void enqueue_complete_buffer(void** buf, size_t index = 0);
 261 
 262   // To be invoked by the mutator.
 263   bool process_or_enqueue_complete_buffer(void** buf);
 264 
 265   bool completed_buffers_exist_dirty() {
 266     return _n_completed_buffers > 0;
 267   }
 268 
 269   bool process_completed_buffers() { return _process_completed; }
 270   void set_process_completed(bool x) { _process_completed = x; }
 271 
 272   bool is_active() { return _all_active; }
 273 
 274   // Set the buffer size.  Should be called before any "enqueue" operation
 275   // can be called.  And should only be called once.
 276   void set_buffer_size(size_t sz);
 277 
 278   // Get the buffer size.
 279   size_t buffer_size() { return _sz; }
 280 
 281   // Get/Set the number of completed buffers that triggers log processing.
 282   void set_process_completed_threshold(int sz) { _process_completed_threshold = sz; }
 283   int process_completed_threshold() const { return _process_completed_threshold; }
 284 
 285   // Must only be called at a safe point.  Indicates that the buffer free
 286   // list size may be reduced, if that is deemed desirable.
 287   void reduce_free_list();
 288 
 289   int completed_buffers_num() { return _n_completed_buffers; }
 290 
 291   void merge_bufferlists(PtrQueueSet* src);
 292 
 293   void set_max_completed_queue(int m) { _max_completed_queue = m; }
 294   int max_completed_queue() { return _max_completed_queue; }
 295 
 296   void set_completed_queue_padding(int padding) { _completed_queue_padding = padding; }
 297   int completed_queue_padding() { return _completed_queue_padding; }
 298 
 299   // Notify the consumer if the number of buffers crossed the threshold
 300   void notify_if_necessary();
 301 };
 302 
 303 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP