1/*
2 * Copyright (c) 2001, 2017, 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_G1_PTRQUEUE_HPP
26#define SHARE_VM_GC_G1_PTRQUEUE_HPP
27
28#include "memory/allocation.hpp"
29#include "utilities/align.hpp"
30#include "utilities/sizes.hpp"
31
32// There are various techniques that require threads to be able to log
33// addresses.  For example, a generational write barrier might log
34// the addresses of modified old-generation objects.  This type supports
35// this operation.
36
37class BufferNode;
38class PtrQueueSet;
39class PtrQueue VALUE_OBJ_CLASS_SPEC {
40  friend class VMStructs;
41
42  // Noncopyable - not defined.
43  PtrQueue(const PtrQueue&);
44  PtrQueue& operator=(const PtrQueue&);
45
46  // The ptr queue set to which this queue belongs.
47  PtrQueueSet* const _qset;
48
49  // Whether updates should be logged.
50  bool _active;
51
52  // If true, the queue is permanent, and doesn't need to deallocate
53  // its buffer in the destructor (since that obtains a lock which may not
54  // be legally locked by then.
55  const bool _permanent;
56
57  // The (byte) index at which an object was last enqueued.  Starts at
58  // capacity_in_bytes (indicating an empty buffer) and goes towards zero.
59  // Value is always pointer-size aligned.
60  size_t _index;
61
62  // Size of the current buffer, in bytes.
63  // Value is always pointer-size aligned.
64  size_t _capacity_in_bytes;
65
66  static const size_t _element_size = sizeof(void*);
67
68  // Get the capacity, in bytes.  The capacity must have been set.
69  size_t capacity_in_bytes() const {
70    assert(_capacity_in_bytes > 0, "capacity not set");
71    return _capacity_in_bytes;
72  }
73
74  void set_capacity(size_t entries) {
75    size_t byte_capacity = index_to_byte_index(entries);
76    assert(_capacity_in_bytes == 0 || _capacity_in_bytes == byte_capacity,
77           "changing capacity " SIZE_FORMAT " -> " SIZE_FORMAT,
78           _capacity_in_bytes, byte_capacity);
79    _capacity_in_bytes = byte_capacity;
80  }
81
82  static size_t byte_index_to_index(size_t ind) {
83    assert(is_aligned(ind, _element_size), "precondition");
84    return ind / _element_size;
85  }
86
87  static size_t index_to_byte_index(size_t ind) {
88    return ind * _element_size;
89  }
90
91protected:
92  // The buffer.
93  void** _buf;
94
95  size_t index() const {
96    return byte_index_to_index(_index);
97  }
98
99  void set_index(size_t new_index) {
100    size_t byte_index = index_to_byte_index(new_index);
101    assert(byte_index <= capacity_in_bytes(), "precondition");
102    _index = byte_index;
103  }
104
105  size_t capacity() const {
106    return byte_index_to_index(capacity_in_bytes());
107  }
108
109  // If there is a lock associated with this buffer, this is that lock.
110  Mutex* _lock;
111
112  PtrQueueSet* qset() { return _qset; }
113  bool is_permanent() const { return _permanent; }
114
115  // Process queue entries and release resources.
116  void flush_impl();
117
118  // Initialize this queue to contain a null buffer, and be part of the
119  // given PtrQueueSet.
120  PtrQueue(PtrQueueSet* qset, bool permanent = false, bool active = false);
121
122  // Requires queue flushed or permanent.
123  ~PtrQueue();
124
125public:
126
127  // Associate a lock with a ptr queue.
128  void set_lock(Mutex* lock) { _lock = lock; }
129
130  // Forcibly set empty.
131  void reset() {
132    if (_buf != NULL) {
133      _index = capacity_in_bytes();
134    }
135  }
136
137  void enqueue(volatile void* ptr) {
138    enqueue((void*)(ptr));
139  }
140
141  // Enqueues the given "obj".
142  void enqueue(void* ptr) {
143    if (!_active) return;
144    else enqueue_known_active(ptr);
145  }
146
147  // This method is called when we're doing the zero index handling
148  // and gives a chance to the queues to do any pre-enqueueing
149  // processing they might want to do on the buffer. It should return
150  // true if the buffer should be enqueued, or false if enough
151  // entries were cleared from it so that it can be re-used. It should
152  // not return false if the buffer is still full (otherwise we can
153  // get into an infinite loop).
154  virtual bool should_enqueue_buffer() { return true; }
155  void handle_zero_index();
156  void locking_enqueue_completed_buffer(BufferNode* node);
157
158  void enqueue_known_active(void* ptr);
159
160  // Return the size of the in-use region.
161  size_t size() const {
162    size_t result = 0;
163    if (_buf != NULL) {
164      assert(_index <= capacity_in_bytes(), "Invariant");
165      result = byte_index_to_index(capacity_in_bytes() - _index);
166    }
167    return result;
168  }
169
170  bool is_empty() const {
171    return _buf == NULL || capacity_in_bytes() == _index;
172  }
173
174  // Set the "active" property of the queue to "b".  An enqueue to an
175  // inactive thread is a no-op.  Setting a queue to inactive resets its
176  // log to the empty state.
177  void set_active(bool b) {
178    _active = b;
179    if (!b && _buf != NULL) {
180      reset();
181    } else if (b && _buf != NULL) {
182      assert(index() == capacity(),
183             "invariant: queues are empty when activated.");
184    }
185  }
186
187  bool is_active() const { return _active; }
188
189  // To support compiler.
190
191protected:
192  template<typename Derived>
193  static ByteSize byte_offset_of_index() {
194    return byte_offset_of(Derived, _index);
195  }
196
197  static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
198
199  template<typename Derived>
200  static ByteSize byte_offset_of_buf() {
201    return byte_offset_of(Derived, _buf);
202  }
203
204  static ByteSize byte_width_of_buf() { return in_ByteSize(_element_size); }
205
206  template<typename Derived>
207  static ByteSize byte_offset_of_active() {
208    return byte_offset_of(Derived, _active);
209  }
210
211  static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
212
213};
214
215class BufferNode {
216  size_t _index;
217  BufferNode* _next;
218  void* _buffer[1];             // Pseudo flexible array member.
219
220  BufferNode() : _index(0), _next(NULL) { }
221  ~BufferNode() { }
222
223  static size_t buffer_offset() {
224    return offset_of(BufferNode, _buffer);
225  }
226
227public:
228  BufferNode* next() const     { return _next;  }
229  void set_next(BufferNode* n) { _next = n;     }
230  size_t index() const         { return _index; }
231  void set_index(size_t i)     { _index = i; }
232
233  // Allocate a new BufferNode with the "buffer" having size elements.
234  static BufferNode* allocate(size_t size);
235
236  // Free a BufferNode.
237  static void deallocate(BufferNode* node);
238
239  // Return the BufferNode containing the buffer, after setting its index.
240  static BufferNode* make_node_from_buffer(void** buffer, size_t index) {
241    BufferNode* node =
242      reinterpret_cast<BufferNode*>(
243        reinterpret_cast<char*>(buffer) - buffer_offset());
244    node->set_index(index);
245    return node;
246  }
247
248  // Return the buffer for node.
249  static void** make_buffer_from_node(BufferNode *node) {
250    // &_buffer[0] might lead to index out of bounds warnings.
251    return reinterpret_cast<void**>(
252      reinterpret_cast<char*>(node) + buffer_offset());
253  }
254};
255
256// A PtrQueueSet represents resources common to a set of pointer queues.
257// In particular, the individual queues allocate buffers from this shared
258// set, and return completed buffers to the set.
259// All these variables are are protected by the TLOQ_CBL_mon. XXX ???
260class PtrQueueSet VALUE_OBJ_CLASS_SPEC {
261private:
262  // The size of all buffers in the set.
263  size_t _buffer_size;
264
265protected:
266  Monitor* _cbl_mon;  // Protects the fields below.
267  BufferNode* _completed_buffers_head;
268  BufferNode* _completed_buffers_tail;
269  size_t _n_completed_buffers;
270  int _process_completed_threshold;
271  volatile bool _process_completed;
272
273  // This (and the interpretation of the first element as a "next"
274  // pointer) are protected by the TLOQ_FL_lock.
275  Mutex* _fl_lock;
276  BufferNode* _buf_free_list;
277  size_t _buf_free_list_sz;
278  // Queue set can share a freelist. The _fl_owner variable
279  // specifies the owner. It is set to "this" by default.
280  PtrQueueSet* _fl_owner;
281
282  bool _all_active;
283
284  // If true, notify_all on _cbl_mon when the threshold is reached.
285  bool _notify_when_complete;
286
287  // Maximum number of elements allowed on completed queue: after that,
288  // enqueuer does the work itself.  Zero indicates no maximum.
289  int _max_completed_queue;
290  size_t _completed_queue_padding;
291
292  size_t completed_buffers_list_length();
293  void assert_completed_buffer_list_len_correct_locked();
294  void assert_completed_buffer_list_len_correct();
295
296protected:
297  // A mutator thread does the the work of processing a buffer.
298  // Returns "true" iff the work is complete (and the buffer may be
299  // deallocated).
300  virtual bool mut_process_buffer(BufferNode* node) {
301    ShouldNotReachHere();
302    return false;
303  }
304
305  // Create an empty ptr queue set.
306  PtrQueueSet(bool notify_when_complete = false);
307  ~PtrQueueSet();
308
309  // Because of init-order concerns, we can't pass these as constructor
310  // arguments.
311  void initialize(Monitor* cbl_mon,
312                  Mutex* fl_lock,
313                  int process_completed_threshold,
314                  int max_completed_queue,
315                  PtrQueueSet *fl_owner = NULL);
316
317public:
318
319  // Return the buffer for a BufferNode of size buffer_size().
320  void** allocate_buffer();
321
322  // Return an empty buffer to the free list.  The node is required
323  // to have been allocated with a size of buffer_size().
324  void deallocate_buffer(BufferNode* node);
325
326  // Declares that "buf" is a complete buffer.
327  void enqueue_complete_buffer(BufferNode* node);
328
329  // To be invoked by the mutator.
330  bool process_or_enqueue_complete_buffer(BufferNode* node);
331
332  bool completed_buffers_exist_dirty() {
333    return _n_completed_buffers > 0;
334  }
335
336  bool process_completed_buffers() { return _process_completed; }
337  void set_process_completed(bool x) { _process_completed = x; }
338
339  bool is_active() { return _all_active; }
340
341  // Set the buffer size.  Should be called before any "enqueue" operation
342  // can be called.  And should only be called once.
343  void set_buffer_size(size_t sz);
344
345  // Get the buffer size.  Must have been set.
346  size_t buffer_size() const {
347    assert(_buffer_size > 0, "buffer size not set");
348    return _buffer_size;
349  }
350
351  // Get/Set the number of completed buffers that triggers log processing.
352  void set_process_completed_threshold(int sz) { _process_completed_threshold = sz; }
353  int process_completed_threshold() const { return _process_completed_threshold; }
354
355  // Must only be called at a safe point.  Indicates that the buffer free
356  // list size may be reduced, if that is deemed desirable.
357  void reduce_free_list();
358
359  size_t completed_buffers_num() { return _n_completed_buffers; }
360
361  void merge_bufferlists(PtrQueueSet* src);
362
363  void set_max_completed_queue(int m) { _max_completed_queue = m; }
364  int max_completed_queue() { return _max_completed_queue; }
365
366  void set_completed_queue_padding(size_t padding) { _completed_queue_padding = padding; }
367  size_t completed_queue_padding() { return _completed_queue_padding; }
368
369  // Notify the consumer if the number of buffers crossed the threshold
370  void notify_if_necessary();
371};
372
373#endif // SHARE_VM_GC_G1_PTRQUEUE_HPP
374