342N/A/*
3067N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
342N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
342N/A *
342N/A * This code is free software; you can redistribute it and/or modify it
342N/A * under the terms of the GNU General Public License version 2 only, as
342N/A * published by the Free Software Foundation.
342N/A *
342N/A * This code is distributed in the hope that it will be useful, but WITHOUT
342N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
342N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
342N/A * version 2 for more details (a copy is included in the LICENSE file that
342N/A * accompanied this code).
342N/A *
342N/A * You should have received a copy of the GNU General Public License version
342N/A * 2 along with this work; if not, write to the Free Software Foundation,
342N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
342N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
342N/A *
342N/A */
342N/A
1879N/A#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
1879N/A#define SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A#include "utilities/sizes.hpp"
1879N/A
342N/A// There are various techniques that require threads to be able to log
342N/A// addresses. For example, a generational write barrier might log
342N/A// the addresses of modified old-generation objects. This type supports
342N/A// this operation.
342N/A
1111N/A// The definition of placement operator new(size_t, void*) in the <new>.
1111N/A#include <new>
1111N/A
342N/Aclass PtrQueueSet;
549N/Aclass PtrQueue VALUE_OBJ_CLASS_SPEC {
342N/A
342N/Aprotected:
342N/A // The ptr queue set to which this queue belongs.
342N/A PtrQueueSet* _qset;
342N/A
342N/A // Whether updates should be logged.
342N/A bool _active;
342N/A
342N/A // The buffer.
342N/A void** _buf;
342N/A // The index at which an object was last enqueued. Starts at "_sz"
342N/A // (indicating an empty buffer) and goes towards zero.
342N/A size_t _index;
342N/A
342N/A // The size of the buffer.
342N/A size_t _sz;
342N/A
342N/A // If true, the queue is permanent, and doesn't need to deallocate
342N/A // its buffer in the destructor (since that obtains a lock which may not
342N/A // be legally locked by then.
342N/A bool _perm;
342N/A
342N/A // If there is a lock associated with this buffer, this is that lock.
342N/A Mutex* _lock;
342N/A
342N/A PtrQueueSet* qset() { return _qset; }
342N/A
342N/Apublic:
342N/A // Initialize this queue to contain a null buffer, and be part of the
342N/A // given PtrQueueSet.
2034N/A PtrQueue(PtrQueueSet* qset, bool perm = false, bool active = false);
342N/A // Release any contained resources.
3067N/A virtual void flush();
441N/A // Calls flush() when destroyed.
441N/A ~PtrQueue() { flush(); }
342N/A
342N/A // Associate a lock with a ptr queue.
342N/A void set_lock(Mutex* lock) { _lock = lock; }
342N/A
342N/A void reset() { if (_buf != NULL) _index = _sz; }
342N/A
342N/A // Enqueues the given "obj".
342N/A void enqueue(void* ptr) {
342N/A if (!_active) return;
342N/A else enqueue_known_active(ptr);
342N/A }
342N/A
2034N/A // This method is called when we're doing the zero index handling
2034N/A // and gives a chance to the queues to do any pre-enqueueing
2034N/A // processing they might want to do on the buffer. It should return
2034N/A // true if the buffer should be enqueued, or false if enough
2034N/A // entries were cleared from it so that it can be re-used. It should
2034N/A // not return false if the buffer is still full (otherwise we can
2034N/A // get into an infinite loop).
2034N/A virtual bool should_enqueue_buffer() { return true; }
1111N/A void handle_zero_index();
342N/A void locking_enqueue_completed_buffer(void** buf);
342N/A
342N/A void enqueue_known_active(void* ptr);
342N/A
342N/A size_t size() {
342N/A assert(_sz >= _index, "Invariant.");
342N/A return _buf == NULL ? 0 : _sz - _index;
342N/A }
342N/A
1762N/A bool is_empty() {
1762N/A return _buf == NULL || _sz == _index;
1762N/A }
1762N/A
342N/A // Set the "active" property of the queue to "b". An enqueue to an
342N/A // inactive thread is a no-op. Setting a queue to inactive resets its
342N/A // log to the empty state.
342N/A void set_active(bool b) {
342N/A _active = b;
342N/A if (!b && _buf != NULL) {
342N/A _index = _sz;
342N/A } else if (b && _buf != NULL) {
342N/A assert(_index == _sz, "invariant: queues are empty when activated.");
342N/A }
342N/A }
342N/A
1317N/A bool is_active() { return _active; }
1317N/A
342N/A static int byte_index_to_index(int ind) {
342N/A assert((ind % oopSize) == 0, "Invariant.");
342N/A return ind / oopSize;
342N/A }
342N/A
342N/A static int index_to_byte_index(int byte_ind) {
342N/A return byte_ind * oopSize;
342N/A }
342N/A
342N/A // To support compiler.
342N/A static ByteSize byte_offset_of_index() {
342N/A return byte_offset_of(PtrQueue, _index);
342N/A }
342N/A static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
342N/A
342N/A static ByteSize byte_offset_of_buf() {
342N/A return byte_offset_of(PtrQueue, _buf);
342N/A }
342N/A static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }
342N/A
342N/A static ByteSize byte_offset_of_active() {
342N/A return byte_offset_of(PtrQueue, _active);
342N/A }
342N/A static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
342N/A
342N/A};
342N/A
1111N/Aclass BufferNode {
1111N/A size_t _index;
1111N/A BufferNode* _next;
1111N/Apublic:
1111N/A BufferNode() : _index(0), _next(NULL) { }
1111N/A BufferNode* next() const { return _next; }
1111N/A void set_next(BufferNode* n) { _next = n; }
1111N/A size_t index() const { return _index; }
1111N/A void set_index(size_t i) { _index = i; }
1111N/A
1111N/A // Align the size of the structure to the size of the pointer
1111N/A static size_t aligned_size() {
1111N/A static const size_t alignment = round_to(sizeof(BufferNode), sizeof(void*));
1111N/A return alignment;
1111N/A }
1111N/A
1111N/A // BufferNode is allocated before the buffer.
1111N/A // The chunk of memory that holds both of them is a block.
1111N/A
1111N/A // Produce a new BufferNode given a buffer.
1111N/A static BufferNode* new_from_buffer(void** buf) {
1111N/A return new (make_block_from_buffer(buf)) BufferNode;
1111N/A }
1111N/A
1111N/A // The following are the required conversion routines:
1111N/A static BufferNode* make_node_from_buffer(void** buf) {
1111N/A return (BufferNode*)make_block_from_buffer(buf);
1111N/A }
1111N/A static void** make_buffer_from_node(BufferNode *node) {
1111N/A return make_buffer_from_block(node);
1111N/A }
1111N/A static void* make_block_from_node(BufferNode *node) {
1111N/A return (void*)node;
1111N/A }
1111N/A static void** make_buffer_from_block(void* p) {
1111N/A return (void**)((char*)p + aligned_size());
1111N/A }
1111N/A static void* make_block_from_buffer(void** p) {
1111N/A return (void*)((char*)p - aligned_size());
1111N/A }
1111N/A};
1111N/A
342N/A// A PtrQueueSet represents resources common to a set of pointer queues.
342N/A// In particular, the individual queues allocate buffers from this shared
342N/A// set, and return completed buffers to the set.
342N/A// All these variables are are protected by the TLOQ_CBL_mon. XXX ???
549N/Aclass PtrQueueSet VALUE_OBJ_CLASS_SPEC {
342N/Aprotected:
342N/A Monitor* _cbl_mon; // Protects the fields below.
1111N/A BufferNode* _completed_buffers_head;
1111N/A BufferNode* _completed_buffers_tail;
1111N/A int _n_completed_buffers;
1111N/A int _process_completed_threshold;
342N/A volatile bool _process_completed;
342N/A
342N/A // This (and the interpretation of the first element as a "next"
342N/A // pointer) are protected by the TLOQ_FL_lock.
342N/A Mutex* _fl_lock;
1111N/A BufferNode* _buf_free_list;
342N/A size_t _buf_free_list_sz;
616N/A // Queue set can share a freelist. The _fl_owner variable
616N/A // specifies the owner. It is set to "this" by default.
616N/A PtrQueueSet* _fl_owner;
342N/A
342N/A // The size of all buffers in the set.
342N/A size_t _sz;
342N/A
342N/A bool _all_active;
342N/A
342N/A // If true, notify_all on _cbl_mon when the threshold is reached.
342N/A bool _notify_when_complete;
342N/A
342N/A // Maximum number of elements allowed on completed queue: after that,
342N/A // enqueuer does the work itself. Zero indicates no maximum.
342N/A int _max_completed_queue;
1111N/A int _completed_queue_padding;
342N/A
342N/A int completed_buffers_list_length();
342N/A void assert_completed_buffer_list_len_correct_locked();
342N/A void assert_completed_buffer_list_len_correct();
342N/A
342N/Aprotected:
342N/A // A mutator thread does the the work of processing a buffer.
342N/A // Returns "true" iff the work is complete (and the buffer may be
342N/A // deallocated).
342N/A virtual bool mut_process_buffer(void** buf) {
342N/A ShouldNotReachHere();
342N/A return false;
342N/A }
342N/A
342N/Apublic:
342N/A // Create an empty ptr queue set.
342N/A PtrQueueSet(bool notify_when_complete = false);
342N/A
342N/A // Because of init-order concerns, we can't pass these as constructor
342N/A // arguments.
342N/A void initialize(Monitor* cbl_mon, Mutex* fl_lock,
1111N/A int process_completed_threshold,
1111N/A int max_completed_queue,
616N/A PtrQueueSet *fl_owner = NULL) {
342N/A _max_completed_queue = max_completed_queue;
1111N/A _process_completed_threshold = process_completed_threshold;
1111N/A _completed_queue_padding = 0;
342N/A assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
616N/A _cbl_mon = cbl_mon;
616N/A _fl_lock = fl_lock;
616N/A _fl_owner = (fl_owner != NULL) ? fl_owner : this;
342N/A }
342N/A
342N/A // Return an empty oop array of size _sz (required to be non-zero).
342N/A void** allocate_buffer();
342N/A
342N/A // Return an empty buffer to the free list. The "buf" argument is
342N/A // required to be a pointer to the head of an array of length "_sz".
342N/A void deallocate_buffer(void** buf);
342N/A
342N/A // Declares that "buf" is a complete buffer.
1111N/A void enqueue_complete_buffer(void** buf, size_t index = 0);
1111N/A
1111N/A // To be invoked by the mutator.
1111N/A bool process_or_enqueue_complete_buffer(void** buf);
342N/A
342N/A bool completed_buffers_exist_dirty() {
342N/A return _n_completed_buffers > 0;
342N/A }
342N/A
342N/A bool process_completed_buffers() { return _process_completed; }
1111N/A void set_process_completed(bool x) { _process_completed = x; }
342N/A
1317N/A bool is_active() { return _all_active; }
342N/A
342N/A // Set the buffer size. Should be called before any "enqueue" operation
342N/A // can be called. And should only be called once.
342N/A void set_buffer_size(size_t sz);
342N/A
342N/A // Get the buffer size.
342N/A size_t buffer_size() { return _sz; }
342N/A
1111N/A // Get/Set the number of completed buffers that triggers log processing.
1111N/A void set_process_completed_threshold(int sz) { _process_completed_threshold = sz; }
1111N/A int process_completed_threshold() const { return _process_completed_threshold; }
342N/A
342N/A // Must only be called at a safe point. Indicates that the buffer free
342N/A // list size may be reduced, if that is deemed desirable.
342N/A void reduce_free_list();
342N/A
1111N/A int completed_buffers_num() { return _n_completed_buffers; }
616N/A
616N/A void merge_bufferlists(PtrQueueSet* src);
1111N/A
1111N/A void set_max_completed_queue(int m) { _max_completed_queue = m; }
1111N/A int max_completed_queue() { return _max_completed_queue; }
1111N/A
1111N/A void set_completed_queue_padding(int padding) { _completed_queue_padding = padding; }
1111N/A int completed_queue_padding() { return _completed_queue_padding; }
1111N/A
1111N/A // Notify the consumer if the number of buffers crossed the threshold
1111N/A void notify_if_necessary();
342N/A};
1879N/A
1879N/A#endif // SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP