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

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7445
42c091d63c72
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
tschatzl@7445 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
aoqi@0 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "utilities/sizes.hpp"
aoqi@0 30
aoqi@0 31 // There are various techniques that require threads to be able to log
aoqi@0 32 // addresses. For example, a generational write barrier might log
aoqi@0 33 // the addresses of modified old-generation objects. This type supports
aoqi@0 34 // this operation.
aoqi@0 35
aoqi@0 36 // The definition of placement operator new(size_t, void*) in the <new>.
aoqi@0 37 #include <new>
aoqi@0 38
aoqi@0 39 class PtrQueueSet;
aoqi@0 40 class PtrQueue VALUE_OBJ_CLASS_SPEC {
aoqi@0 41 friend class VMStructs;
aoqi@0 42
aoqi@0 43 protected:
aoqi@0 44 // The ptr queue set to which this queue belongs.
aoqi@0 45 PtrQueueSet* _qset;
aoqi@0 46
aoqi@0 47 // Whether updates should be logged.
aoqi@0 48 bool _active;
aoqi@0 49
aoqi@0 50 // The buffer.
aoqi@0 51 void** _buf;
aoqi@0 52 // The index at which an object was last enqueued. Starts at "_sz"
aoqi@0 53 // (indicating an empty buffer) and goes towards zero.
aoqi@0 54 size_t _index;
aoqi@0 55
aoqi@0 56 // The size of the buffer.
aoqi@0 57 size_t _sz;
aoqi@0 58
aoqi@0 59 // If true, the queue is permanent, and doesn't need to deallocate
aoqi@0 60 // its buffer in the destructor (since that obtains a lock which may not
aoqi@0 61 // be legally locked by then.
aoqi@0 62 bool _perm;
aoqi@0 63
aoqi@0 64 // If there is a lock associated with this buffer, this is that lock.
aoqi@0 65 Mutex* _lock;
aoqi@0 66
aoqi@0 67 PtrQueueSet* qset() { return _qset; }
tschatzl@7445 68 bool is_permanent() const { return _perm; }
tschatzl@7445 69
tschatzl@7445 70 // Process queue entries and release resources, if not permanent.
tschatzl@7445 71 void flush_impl();
aoqi@0 72
aoqi@0 73 public:
aoqi@0 74 // Initialize this queue to contain a null buffer, and be part of the
aoqi@0 75 // given PtrQueueSet.
aoqi@0 76 PtrQueue(PtrQueueSet* qset, bool perm = false, bool active = false);
tschatzl@7445 77
tschatzl@7445 78 // Requires queue flushed or permanent.
tschatzl@7445 79 ~PtrQueue();
aoqi@0 80
aoqi@0 81 // Associate a lock with a ptr queue.
aoqi@0 82 void set_lock(Mutex* lock) { _lock = lock; }
aoqi@0 83
aoqi@0 84 void reset() { if (_buf != NULL) _index = _sz; }
aoqi@0 85
aoqi@0 86 void enqueue(volatile void* ptr) {
aoqi@0 87 enqueue((void*)(ptr));
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 // Enqueues the given "obj".
aoqi@0 91 void enqueue(void* ptr) {
aoqi@0 92 if (!_active) return;
aoqi@0 93 else enqueue_known_active(ptr);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 // This method is called when we're doing the zero index handling
aoqi@0 97 // and gives a chance to the queues to do any pre-enqueueing
aoqi@0 98 // processing they might want to do on the buffer. It should return
aoqi@0 99 // true if the buffer should be enqueued, or false if enough
aoqi@0 100 // entries were cleared from it so that it can be re-used. It should
aoqi@0 101 // not return false if the buffer is still full (otherwise we can
aoqi@0 102 // get into an infinite loop).
aoqi@0 103 virtual bool should_enqueue_buffer() { return true; }
aoqi@0 104 void handle_zero_index();
aoqi@0 105 void locking_enqueue_completed_buffer(void** buf);
aoqi@0 106
aoqi@0 107 void enqueue_known_active(void* ptr);
aoqi@0 108
aoqi@0 109 size_t size() {
aoqi@0 110 assert(_sz >= _index, "Invariant.");
aoqi@0 111 return _buf == NULL ? 0 : _sz - _index;
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 bool is_empty() {
aoqi@0 115 return _buf == NULL || _sz == _index;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 // Set the "active" property of the queue to "b". An enqueue to an
aoqi@0 119 // inactive thread is a no-op. Setting a queue to inactive resets its
aoqi@0 120 // log to the empty state.
aoqi@0 121 void set_active(bool b) {
aoqi@0 122 _active = b;
aoqi@0 123 if (!b && _buf != NULL) {
aoqi@0 124 _index = _sz;
aoqi@0 125 } else if (b && _buf != NULL) {
aoqi@0 126 assert(_index == _sz, "invariant: queues are empty when activated.");
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 bool is_active() { return _active; }
aoqi@0 131
aoqi@0 132 static int byte_index_to_index(int ind) {
aoqi@0 133 assert((ind % oopSize) == 0, "Invariant.");
aoqi@0 134 return ind / oopSize;
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 static int index_to_byte_index(int byte_ind) {
aoqi@0 138 return byte_ind * oopSize;
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 // To support compiler.
aoqi@0 142 static ByteSize byte_offset_of_index() {
aoqi@0 143 return byte_offset_of(PtrQueue, _index);
aoqi@0 144 }
aoqi@0 145 static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
aoqi@0 146
aoqi@0 147 static ByteSize byte_offset_of_buf() {
aoqi@0 148 return byte_offset_of(PtrQueue, _buf);
aoqi@0 149 }
aoqi@0 150 static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }
aoqi@0 151
aoqi@0 152 static ByteSize byte_offset_of_active() {
aoqi@0 153 return byte_offset_of(PtrQueue, _active);
aoqi@0 154 }
aoqi@0 155 static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
aoqi@0 156
aoqi@0 157 };
aoqi@0 158
aoqi@0 159 class BufferNode {
aoqi@0 160 size_t _index;
aoqi@0 161 BufferNode* _next;
aoqi@0 162 public:
aoqi@0 163 BufferNode() : _index(0), _next(NULL) { }
aoqi@0 164 BufferNode* next() const { return _next; }
aoqi@0 165 void set_next(BufferNode* n) { _next = n; }
aoqi@0 166 size_t index() const { return _index; }
aoqi@0 167 void set_index(size_t i) { _index = i; }
aoqi@0 168
aoqi@0 169 // Align the size of the structure to the size of the pointer
aoqi@0 170 static size_t aligned_size() {
aoqi@0 171 static const size_t alignment = round_to(sizeof(BufferNode), sizeof(void*));
aoqi@0 172 return alignment;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 // BufferNode is allocated before the buffer.
aoqi@0 176 // The chunk of memory that holds both of them is a block.
aoqi@0 177
aoqi@0 178 // Produce a new BufferNode given a buffer.
aoqi@0 179 static BufferNode* new_from_buffer(void** buf) {
aoqi@0 180 return new (make_block_from_buffer(buf)) BufferNode;
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 // The following are the required conversion routines:
aoqi@0 184 static BufferNode* make_node_from_buffer(void** buf) {
aoqi@0 185 return (BufferNode*)make_block_from_buffer(buf);
aoqi@0 186 }
aoqi@0 187 static void** make_buffer_from_node(BufferNode *node) {
aoqi@0 188 return make_buffer_from_block(node);
aoqi@0 189 }
aoqi@0 190 static void* make_block_from_node(BufferNode *node) {
aoqi@0 191 return (void*)node;
aoqi@0 192 }
aoqi@0 193 static void** make_buffer_from_block(void* p) {
aoqi@0 194 return (void**)((char*)p + aligned_size());
aoqi@0 195 }
aoqi@0 196 static void* make_block_from_buffer(void** p) {
aoqi@0 197 return (void*)((char*)p - aligned_size());
aoqi@0 198 }
aoqi@0 199 };
aoqi@0 200
aoqi@0 201 // A PtrQueueSet represents resources common to a set of pointer queues.
aoqi@0 202 // In particular, the individual queues allocate buffers from this shared
aoqi@0 203 // set, and return completed buffers to the set.
aoqi@0 204 // All these variables are are protected by the TLOQ_CBL_mon. XXX ???
aoqi@0 205 class PtrQueueSet VALUE_OBJ_CLASS_SPEC {
aoqi@0 206 protected:
aoqi@0 207 Monitor* _cbl_mon; // Protects the fields below.
aoqi@0 208 BufferNode* _completed_buffers_head;
aoqi@0 209 BufferNode* _completed_buffers_tail;
aoqi@0 210 int _n_completed_buffers;
aoqi@0 211 int _process_completed_threshold;
aoqi@0 212 volatile bool _process_completed;
aoqi@0 213
aoqi@0 214 // This (and the interpretation of the first element as a "next"
aoqi@0 215 // pointer) are protected by the TLOQ_FL_lock.
aoqi@0 216 Mutex* _fl_lock;
aoqi@0 217 BufferNode* _buf_free_list;
aoqi@0 218 size_t _buf_free_list_sz;
aoqi@0 219 // Queue set can share a freelist. The _fl_owner variable
aoqi@0 220 // specifies the owner. It is set to "this" by default.
aoqi@0 221 PtrQueueSet* _fl_owner;
aoqi@0 222
aoqi@0 223 // The size of all buffers in the set.
aoqi@0 224 size_t _sz;
aoqi@0 225
aoqi@0 226 bool _all_active;
aoqi@0 227
aoqi@0 228 // If true, notify_all on _cbl_mon when the threshold is reached.
aoqi@0 229 bool _notify_when_complete;
aoqi@0 230
aoqi@0 231 // Maximum number of elements allowed on completed queue: after that,
aoqi@0 232 // enqueuer does the work itself. Zero indicates no maximum.
aoqi@0 233 int _max_completed_queue;
aoqi@0 234 int _completed_queue_padding;
aoqi@0 235
aoqi@0 236 int completed_buffers_list_length();
aoqi@0 237 void assert_completed_buffer_list_len_correct_locked();
aoqi@0 238 void assert_completed_buffer_list_len_correct();
aoqi@0 239
aoqi@0 240 protected:
aoqi@0 241 // A mutator thread does the the work of processing a buffer.
aoqi@0 242 // Returns "true" iff the work is complete (and the buffer may be
aoqi@0 243 // deallocated).
aoqi@0 244 virtual bool mut_process_buffer(void** buf) {
aoqi@0 245 ShouldNotReachHere();
aoqi@0 246 return false;
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 public:
aoqi@0 250 // Create an empty ptr queue set.
aoqi@0 251 PtrQueueSet(bool notify_when_complete = false);
aoqi@0 252
aoqi@0 253 // Because of init-order concerns, we can't pass these as constructor
aoqi@0 254 // arguments.
aoqi@0 255 void initialize(Monitor* cbl_mon, Mutex* fl_lock,
aoqi@0 256 int process_completed_threshold,
aoqi@0 257 int max_completed_queue,
aoqi@0 258 PtrQueueSet *fl_owner = NULL) {
aoqi@0 259 _max_completed_queue = max_completed_queue;
aoqi@0 260 _process_completed_threshold = process_completed_threshold;
aoqi@0 261 _completed_queue_padding = 0;
aoqi@0 262 assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
aoqi@0 263 _cbl_mon = cbl_mon;
aoqi@0 264 _fl_lock = fl_lock;
aoqi@0 265 _fl_owner = (fl_owner != NULL) ? fl_owner : this;
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 // Return an empty oop array of size _sz (required to be non-zero).
aoqi@0 269 void** allocate_buffer();
aoqi@0 270
aoqi@0 271 // Return an empty buffer to the free list. The "buf" argument is
aoqi@0 272 // required to be a pointer to the head of an array of length "_sz".
aoqi@0 273 void deallocate_buffer(void** buf);
aoqi@0 274
aoqi@0 275 // Declares that "buf" is a complete buffer.
aoqi@0 276 void enqueue_complete_buffer(void** buf, size_t index = 0);
aoqi@0 277
aoqi@0 278 // To be invoked by the mutator.
aoqi@0 279 bool process_or_enqueue_complete_buffer(void** buf);
aoqi@0 280
aoqi@0 281 bool completed_buffers_exist_dirty() {
aoqi@0 282 return _n_completed_buffers > 0;
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 bool process_completed_buffers() { return _process_completed; }
aoqi@0 286 void set_process_completed(bool x) { _process_completed = x; }
aoqi@0 287
aoqi@0 288 bool is_active() { return _all_active; }
aoqi@0 289
aoqi@0 290 // Set the buffer size. Should be called before any "enqueue" operation
aoqi@0 291 // can be called. And should only be called once.
aoqi@0 292 void set_buffer_size(size_t sz);
aoqi@0 293
aoqi@0 294 // Get the buffer size.
aoqi@0 295 size_t buffer_size() { return _sz; }
aoqi@0 296
aoqi@0 297 // Get/Set the number of completed buffers that triggers log processing.
aoqi@0 298 void set_process_completed_threshold(int sz) { _process_completed_threshold = sz; }
aoqi@0 299 int process_completed_threshold() const { return _process_completed_threshold; }
aoqi@0 300
aoqi@0 301 // Must only be called at a safe point. Indicates that the buffer free
aoqi@0 302 // list size may be reduced, if that is deemed desirable.
aoqi@0 303 void reduce_free_list();
aoqi@0 304
aoqi@0 305 int completed_buffers_num() { return _n_completed_buffers; }
aoqi@0 306
aoqi@0 307 void merge_bufferlists(PtrQueueSet* src);
aoqi@0 308
aoqi@0 309 void set_max_completed_queue(int m) { _max_completed_queue = m; }
aoqi@0 310 int max_completed_queue() { return _max_completed_queue; }
aoqi@0 311
aoqi@0 312 void set_completed_queue_padding(int padding) { _completed_queue_padding = padding; }
aoqi@0 313 int completed_queue_padding() { return _completed_queue_padding; }
aoqi@0 314
aoqi@0 315 // Notify the consumer if the number of buffers crossed the threshold
aoqi@0 316 void notify_if_necessary();
aoqi@0 317 };
aoqi@0 318
aoqi@0 319 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_PTRQUEUE_HPP

mercurial