2008-06-05 15:57:56 -07:00
|
|
|
/*
|
2016-02-24 13:18:54 -05:00
|
|
|
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
|
2008-06-05 15:57:56 -07:00
|
|
|
* 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.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* 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.
|
2008-06-05 15:57:56 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-05-13 15:16:06 +02:00
|
|
|
#ifndef SHARE_VM_GC_G1_PTRQUEUE_HPP
|
|
|
|
#define SHARE_VM_GC_G1_PTRQUEUE_HPP
|
2010-11-23 13:22:55 -08:00
|
|
|
|
|
|
|
#include "memory/allocation.hpp"
|
|
|
|
#include "utilities/sizes.hpp"
|
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
// There are various techniques that require threads to be able to log
|
|
|
|
// addresses. For example, a generational write barrier might log
|
|
|
|
// the addresses of modified old-generation objects. This type supports
|
|
|
|
// this operation.
|
|
|
|
|
2016-03-10 16:21:46 -05:00
|
|
|
class BufferNode;
|
2009-12-16 15:12:51 -08:00
|
|
|
class PtrQueueSet;
|
2009-02-10 18:39:09 +03:00
|
|
|
class PtrQueue VALUE_OBJ_CLASS_SPEC {
|
2013-09-13 16:55:44 -07:00
|
|
|
friend class VMStructs;
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
// Noncopyable - not defined.
|
|
|
|
PtrQueue(const PtrQueue&);
|
|
|
|
PtrQueue& operator=(const PtrQueue&);
|
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
// The ptr queue set to which this queue belongs.
|
2015-11-04 13:09:57 -05:00
|
|
|
PtrQueueSet* const _qset;
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
// Whether updates should be logged.
|
|
|
|
bool _active;
|
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
// If true, the queue is permanent, and doesn't need to deallocate
|
|
|
|
// its buffer in the destructor (since that obtains a lock which may not
|
|
|
|
// be legally locked by then.
|
|
|
|
const bool _permanent;
|
|
|
|
|
|
|
|
protected:
|
2008-06-05 15:57:56 -07:00
|
|
|
// The buffer.
|
|
|
|
void** _buf;
|
2015-11-04 13:09:57 -05:00
|
|
|
// The (byte) index at which an object was last enqueued. Starts at "_sz"
|
2008-06-05 15:57:56 -07:00
|
|
|
// (indicating an empty buffer) and goes towards zero.
|
|
|
|
size_t _index;
|
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
// The (byte) size of the buffer.
|
2008-06-05 15:57:56 -07:00
|
|
|
size_t _sz;
|
|
|
|
|
|
|
|
// If there is a lock associated with this buffer, this is that lock.
|
|
|
|
Mutex* _lock;
|
|
|
|
|
|
|
|
PtrQueueSet* qset() { return _qset; }
|
2015-11-04 13:09:57 -05:00
|
|
|
bool is_permanent() const { return _permanent; }
|
2014-07-31 11:10:02 +02:00
|
|
|
|
|
|
|
// Process queue entries and release resources, if not permanent.
|
|
|
|
void flush_impl();
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
// Initialize this queue to contain a null buffer, and be part of the
|
|
|
|
// given PtrQueueSet.
|
2015-11-04 13:09:57 -05:00
|
|
|
PtrQueue(PtrQueueSet* qset, bool permanent = false, bool active = false);
|
2014-07-31 11:10:02 +02:00
|
|
|
|
|
|
|
// Requires queue flushed or permanent.
|
|
|
|
~PtrQueue();
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
public:
|
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
// Associate a lock with a ptr queue.
|
|
|
|
void set_lock(Mutex* lock) { _lock = lock; }
|
|
|
|
|
|
|
|
void reset() { if (_buf != NULL) _index = _sz; }
|
|
|
|
|
2013-10-08 17:35:51 +02:00
|
|
|
void enqueue(volatile void* ptr) {
|
|
|
|
enqueue((void*)(ptr));
|
|
|
|
}
|
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
// Enqueues the given "obj".
|
|
|
|
void enqueue(void* ptr) {
|
|
|
|
if (!_active) return;
|
|
|
|
else enqueue_known_active(ptr);
|
|
|
|
}
|
|
|
|
|
2011-01-19 09:35:17 -05:00
|
|
|
// This method is called when we're doing the zero index handling
|
|
|
|
// and gives a chance to the queues to do any pre-enqueueing
|
|
|
|
// processing they might want to do on the buffer. It should return
|
|
|
|
// true if the buffer should be enqueued, or false if enough
|
|
|
|
// entries were cleared from it so that it can be re-used. It should
|
|
|
|
// not return false if the buffer is still full (otherwise we can
|
|
|
|
// get into an infinite loop).
|
|
|
|
virtual bool should_enqueue_buffer() { return true; }
|
2009-12-16 15:12:51 -08:00
|
|
|
void handle_zero_index();
|
2016-03-10 16:21:46 -05:00
|
|
|
void locking_enqueue_completed_buffer(BufferNode* node);
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
void enqueue_known_active(void* ptr);
|
|
|
|
|
|
|
|
size_t size() {
|
|
|
|
assert(_sz >= _index, "Invariant.");
|
|
|
|
return _buf == NULL ? 0 : _sz - _index;
|
|
|
|
}
|
|
|
|
|
2010-10-01 16:43:05 -04:00
|
|
|
bool is_empty() {
|
|
|
|
return _buf == NULL || _sz == _index;
|
|
|
|
}
|
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
// Set the "active" property of the queue to "b". An enqueue to an
|
|
|
|
// inactive thread is a no-op. Setting a queue to inactive resets its
|
|
|
|
// log to the empty state.
|
|
|
|
void set_active(bool b) {
|
|
|
|
_active = b;
|
|
|
|
if (!b && _buf != NULL) {
|
|
|
|
_index = _sz;
|
|
|
|
} else if (b && _buf != NULL) {
|
|
|
|
assert(_index == _sz, "invariant: queues are empty when activated.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 12:14:59 -04:00
|
|
|
bool is_active() { return _active; }
|
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
static size_t byte_index_to_index(size_t ind) {
|
|
|
|
assert((ind % sizeof(void*)) == 0, "Invariant.");
|
|
|
|
return ind / sizeof(void*);
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
|
|
|
|
2016-03-10 16:21:46 -05:00
|
|
|
static size_t index_to_byte_index(size_t ind) {
|
|
|
|
return ind * sizeof(void*);
|
|
|
|
}
|
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
// To support compiler.
|
2015-11-17 16:40:52 -05:00
|
|
|
|
|
|
|
protected:
|
|
|
|
template<typename Derived>
|
2008-06-05 15:57:56 -07:00
|
|
|
static ByteSize byte_offset_of_index() {
|
2015-11-17 16:40:52 -05:00
|
|
|
return byte_offset_of(Derived, _index);
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2015-11-17 16:40:52 -05:00
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
|
|
|
|
|
2015-11-17 16:40:52 -05:00
|
|
|
template<typename Derived>
|
2008-06-05 15:57:56 -07:00
|
|
|
static ByteSize byte_offset_of_buf() {
|
2015-11-17 16:40:52 -05:00
|
|
|
return byte_offset_of(Derived, _buf);
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2015-11-17 16:40:52 -05:00
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }
|
|
|
|
|
2015-11-17 16:40:52 -05:00
|
|
|
template<typename Derived>
|
2008-06-05 15:57:56 -07:00
|
|
|
static ByteSize byte_offset_of_active() {
|
2015-11-17 16:40:52 -05:00
|
|
|
return byte_offset_of(Derived, _active);
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2015-11-17 16:40:52 -05:00
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-12-16 15:12:51 -08:00
|
|
|
class BufferNode {
|
|
|
|
size_t _index;
|
|
|
|
BufferNode* _next;
|
2016-02-24 13:18:54 -05:00
|
|
|
void* _buffer[1]; // Pseudo flexible array member.
|
|
|
|
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode() : _index(0), _next(NULL) { }
|
2016-02-24 13:18:54 -05:00
|
|
|
~BufferNode() { }
|
|
|
|
|
|
|
|
static size_t buffer_offset() {
|
|
|
|
return offset_of(BufferNode, _buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode* next() const { return _next; }
|
|
|
|
void set_next(BufferNode* n) { _next = n; }
|
|
|
|
size_t index() const { return _index; }
|
|
|
|
void set_index(size_t i) { _index = i; }
|
|
|
|
|
2016-02-24 13:18:54 -05:00
|
|
|
// Allocate a new BufferNode with the "buffer" having size bytes.
|
|
|
|
static BufferNode* allocate(size_t byte_size);
|
2009-12-16 15:12:51 -08:00
|
|
|
|
2016-02-24 13:18:54 -05:00
|
|
|
// Free a BufferNode.
|
|
|
|
static void deallocate(BufferNode* node);
|
2009-12-16 15:12:51 -08:00
|
|
|
|
2016-03-10 16:21:46 -05:00
|
|
|
// Return the BufferNode containing the buffer, after setting its index.
|
|
|
|
static BufferNode* make_node_from_buffer(void** buffer, size_t index) {
|
|
|
|
BufferNode* node =
|
|
|
|
reinterpret_cast<BufferNode*>(
|
|
|
|
reinterpret_cast<char*>(buffer) - buffer_offset());
|
|
|
|
node->set_index(index);
|
|
|
|
return node;
|
2009-12-16 15:12:51 -08:00
|
|
|
}
|
|
|
|
|
2016-02-24 13:18:54 -05:00
|
|
|
// Return the buffer for node.
|
2009-12-16 15:12:51 -08:00
|
|
|
static void** make_buffer_from_node(BufferNode *node) {
|
2016-02-24 13:18:54 -05:00
|
|
|
// &_buffer[0] might lead to index out of bounds warnings.
|
|
|
|
return reinterpret_cast<void**>(
|
|
|
|
reinterpret_cast<char*>(node) + buffer_offset());
|
2009-12-16 15:12:51 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-06-05 15:57:56 -07:00
|
|
|
// A PtrQueueSet represents resources common to a set of pointer queues.
|
|
|
|
// In particular, the individual queues allocate buffers from this shared
|
|
|
|
// set, and return completed buffers to the set.
|
|
|
|
// All these variables are are protected by the TLOQ_CBL_mon. XXX ???
|
2009-02-10 18:39:09 +03:00
|
|
|
class PtrQueueSet VALUE_OBJ_CLASS_SPEC {
|
2008-06-05 15:57:56 -07:00
|
|
|
protected:
|
|
|
|
Monitor* _cbl_mon; // Protects the fields below.
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode* _completed_buffers_head;
|
|
|
|
BufferNode* _completed_buffers_tail;
|
2016-02-26 14:02:39 -05:00
|
|
|
size_t _n_completed_buffers;
|
2009-12-16 15:12:51 -08:00
|
|
|
int _process_completed_threshold;
|
2008-06-05 15:57:56 -07:00
|
|
|
volatile bool _process_completed;
|
|
|
|
|
|
|
|
// This (and the interpretation of the first element as a "next"
|
|
|
|
// pointer) are protected by the TLOQ_FL_lock.
|
|
|
|
Mutex* _fl_lock;
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode* _buf_free_list;
|
2008-06-05 15:57:56 -07:00
|
|
|
size_t _buf_free_list_sz;
|
2009-03-06 13:50:14 -08:00
|
|
|
// Queue set can share a freelist. The _fl_owner variable
|
|
|
|
// specifies the owner. It is set to "this" by default.
|
|
|
|
PtrQueueSet* _fl_owner;
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
// The size of all buffers in the set.
|
|
|
|
size_t _sz;
|
|
|
|
|
|
|
|
bool _all_active;
|
|
|
|
|
|
|
|
// If true, notify_all on _cbl_mon when the threshold is reached.
|
|
|
|
bool _notify_when_complete;
|
|
|
|
|
|
|
|
// Maximum number of elements allowed on completed queue: after that,
|
|
|
|
// enqueuer does the work itself. Zero indicates no maximum.
|
|
|
|
int _max_completed_queue;
|
2016-02-26 14:02:39 -05:00
|
|
|
size_t _completed_queue_padding;
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2016-02-26 14:02:39 -05:00
|
|
|
size_t completed_buffers_list_length();
|
2008-06-05 15:57:56 -07:00
|
|
|
void assert_completed_buffer_list_len_correct_locked();
|
|
|
|
void assert_completed_buffer_list_len_correct();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// A mutator thread does the the work of processing a buffer.
|
|
|
|
// Returns "true" iff the work is complete (and the buffer may be
|
|
|
|
// deallocated).
|
2016-03-10 16:21:46 -05:00
|
|
|
virtual bool mut_process_buffer(BufferNode* node) {
|
2008-06-05 15:57:56 -07:00
|
|
|
ShouldNotReachHere();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an empty ptr queue set.
|
|
|
|
PtrQueueSet(bool notify_when_complete = false);
|
2015-11-04 13:09:57 -05:00
|
|
|
~PtrQueueSet();
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
// Because of init-order concerns, we can't pass these as constructor
|
|
|
|
// arguments.
|
2015-11-04 13:09:57 -05:00
|
|
|
void initialize(Monitor* cbl_mon,
|
|
|
|
Mutex* fl_lock,
|
2009-12-16 15:12:51 -08:00
|
|
|
int process_completed_threshold,
|
|
|
|
int max_completed_queue,
|
2015-11-04 13:09:57 -05:00
|
|
|
PtrQueueSet *fl_owner = NULL);
|
|
|
|
|
|
|
|
public:
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
// Return an empty array of size _sz (required to be non-zero).
|
2008-06-05 15:57:56 -07:00
|
|
|
void** allocate_buffer();
|
|
|
|
|
|
|
|
// Return an empty buffer to the free list. The "buf" argument is
|
|
|
|
// required to be a pointer to the head of an array of length "_sz".
|
2016-03-10 16:21:46 -05:00
|
|
|
void deallocate_buffer(BufferNode* node);
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
// Declares that "buf" is a complete buffer.
|
2016-03-10 16:21:46 -05:00
|
|
|
void enqueue_complete_buffer(BufferNode* node);
|
2009-12-16 15:12:51 -08:00
|
|
|
|
|
|
|
// To be invoked by the mutator.
|
2016-03-10 16:21:46 -05:00
|
|
|
bool process_or_enqueue_complete_buffer(BufferNode* node);
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
bool completed_buffers_exist_dirty() {
|
|
|
|
return _n_completed_buffers > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool process_completed_buffers() { return _process_completed; }
|
2009-12-16 15:12:51 -08:00
|
|
|
void set_process_completed(bool x) { _process_completed = x; }
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2010-03-18 12:14:59 -04:00
|
|
|
bool is_active() { return _all_active; }
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
// Set the buffer size. Should be called before any "enqueue" operation
|
|
|
|
// can be called. And should only be called once.
|
|
|
|
void set_buffer_size(size_t sz);
|
|
|
|
|
|
|
|
// Get the buffer size.
|
|
|
|
size_t buffer_size() { return _sz; }
|
|
|
|
|
2009-12-16 15:12:51 -08:00
|
|
|
// Get/Set the number of completed buffers that triggers log processing.
|
|
|
|
void set_process_completed_threshold(int sz) { _process_completed_threshold = sz; }
|
|
|
|
int process_completed_threshold() const { return _process_completed_threshold; }
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
// Must only be called at a safe point. Indicates that the buffer free
|
|
|
|
// list size may be reduced, if that is deemed desirable.
|
|
|
|
void reduce_free_list();
|
|
|
|
|
2016-02-26 14:02:39 -05:00
|
|
|
size_t completed_buffers_num() { return _n_completed_buffers; }
|
2009-03-06 13:50:14 -08:00
|
|
|
|
|
|
|
void merge_bufferlists(PtrQueueSet* src);
|
2009-12-16 15:12:51 -08:00
|
|
|
|
|
|
|
void set_max_completed_queue(int m) { _max_completed_queue = m; }
|
|
|
|
int max_completed_queue() { return _max_completed_queue; }
|
|
|
|
|
2016-02-26 14:02:39 -05:00
|
|
|
void set_completed_queue_padding(size_t padding) { _completed_queue_padding = padding; }
|
|
|
|
size_t completed_queue_padding() { return _completed_queue_padding; }
|
2009-12-16 15:12:51 -08:00
|
|
|
|
|
|
|
// Notify the consumer if the number of buffers crossed the threshold
|
|
|
|
void notify_if_necessary();
|
2008-06-05 15:57:56 -07:00
|
|
|
};
|
2010-11-23 13:22:55 -08:00
|
|
|
|
2015-05-13 15:16:06 +02:00
|
|
|
#endif // SHARE_VM_GC_G1_PTRQUEUE_HPP
|