annotate src/share/vm/gc_implementation/g1/ptrQueue.hpp @ 1111:44f61c24ddab

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