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

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial