annotate src/share/vm/gc_implementation/g1/ptrQueue.hpp @ 342:37f87013dfd8

6711316: Open source the Garbage-First garbage collector Summary: First mercurial integration of the code for the Garbage-First garbage collector. Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr
author ysr
date Thu, 05 Jun 2008 15:57:56 -0700
parents
children da9cb4e97a5f
rev   line source
ysr@342 1 /*
ysr@342 2 * Copyright 2001-2007 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
ysr@342 30 class PtrQueueSet;
ysr@342 31
ysr@342 32 class PtrQueue: public CHeapObj {
ysr@342 33
ysr@342 34 protected:
ysr@342 35 // The ptr queue set to which this queue belongs.
ysr@342 36 PtrQueueSet* _qset;
ysr@342 37
ysr@342 38 // Whether updates should be logged.
ysr@342 39 bool _active;
ysr@342 40
ysr@342 41 // The buffer.
ysr@342 42 void** _buf;
ysr@342 43 // The index at which an object was last enqueued. Starts at "_sz"
ysr@342 44 // (indicating an empty buffer) and goes towards zero.
ysr@342 45 size_t _index;
ysr@342 46
ysr@342 47 // The size of the buffer.
ysr@342 48 size_t _sz;
ysr@342 49
ysr@342 50 // If true, the queue is permanent, and doesn't need to deallocate
ysr@342 51 // its buffer in the destructor (since that obtains a lock which may not
ysr@342 52 // be legally locked by then.
ysr@342 53 bool _perm;
ysr@342 54
ysr@342 55 // If there is a lock associated with this buffer, this is that lock.
ysr@342 56 Mutex* _lock;
ysr@342 57
ysr@342 58 PtrQueueSet* qset() { return _qset; }
ysr@342 59
ysr@342 60 public:
ysr@342 61 // Initialize this queue to contain a null buffer, and be part of the
ysr@342 62 // given PtrQueueSet.
ysr@342 63 PtrQueue(PtrQueueSet*, bool perm = false);
ysr@342 64 // Release any contained resources.
ysr@342 65 ~PtrQueue();
ysr@342 66
ysr@342 67 // Associate a lock with a ptr queue.
ysr@342 68 void set_lock(Mutex* lock) { _lock = lock; }
ysr@342 69
ysr@342 70 void reset() { if (_buf != NULL) _index = _sz; }
ysr@342 71
ysr@342 72 // Enqueues the given "obj".
ysr@342 73 void enqueue(void* ptr) {
ysr@342 74 if (!_active) return;
ysr@342 75 else enqueue_known_active(ptr);
ysr@342 76 }
ysr@342 77
ysr@342 78 inline void handle_zero_index();
ysr@342 79 void locking_enqueue_completed_buffer(void** buf);
ysr@342 80
ysr@342 81 void enqueue_known_active(void* ptr);
ysr@342 82
ysr@342 83 size_t size() {
ysr@342 84 assert(_sz >= _index, "Invariant.");
ysr@342 85 return _buf == NULL ? 0 : _sz - _index;
ysr@342 86 }
ysr@342 87
ysr@342 88 // Set the "active" property of the queue to "b". An enqueue to an
ysr@342 89 // inactive thread is a no-op. Setting a queue to inactive resets its
ysr@342 90 // log to the empty state.
ysr@342 91 void set_active(bool b) {
ysr@342 92 _active = b;
ysr@342 93 if (!b && _buf != NULL) {
ysr@342 94 _index = _sz;
ysr@342 95 } else if (b && _buf != NULL) {
ysr@342 96 assert(_index == _sz, "invariant: queues are empty when activated.");
ysr@342 97 }
ysr@342 98 }
ysr@342 99
ysr@342 100 static int byte_index_to_index(int ind) {
ysr@342 101 assert((ind % oopSize) == 0, "Invariant.");
ysr@342 102 return ind / oopSize;
ysr@342 103 }
ysr@342 104
ysr@342 105 static int index_to_byte_index(int byte_ind) {
ysr@342 106 return byte_ind * oopSize;
ysr@342 107 }
ysr@342 108
ysr@342 109 // To support compiler.
ysr@342 110 static ByteSize byte_offset_of_index() {
ysr@342 111 return byte_offset_of(PtrQueue, _index);
ysr@342 112 }
ysr@342 113 static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
ysr@342 114
ysr@342 115 static ByteSize byte_offset_of_buf() {
ysr@342 116 return byte_offset_of(PtrQueue, _buf);
ysr@342 117 }
ysr@342 118 static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }
ysr@342 119
ysr@342 120 static ByteSize byte_offset_of_active() {
ysr@342 121 return byte_offset_of(PtrQueue, _active);
ysr@342 122 }
ysr@342 123 static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
ysr@342 124
ysr@342 125 };
ysr@342 126
ysr@342 127 // A PtrQueueSet represents resources common to a set of pointer queues.
ysr@342 128 // In particular, the individual queues allocate buffers from this shared
ysr@342 129 // set, and return completed buffers to the set.
ysr@342 130 // All these variables are are protected by the TLOQ_CBL_mon. XXX ???
ysr@342 131 class PtrQueueSet: public CHeapObj {
ysr@342 132
ysr@342 133 protected:
ysr@342 134
ysr@342 135 class CompletedBufferNode: public CHeapObj {
ysr@342 136 public:
ysr@342 137 void** buf;
ysr@342 138 size_t index;
ysr@342 139 CompletedBufferNode* next;
ysr@342 140 CompletedBufferNode() : buf(NULL),
ysr@342 141 index(0), next(NULL){ }
ysr@342 142 };
ysr@342 143
ysr@342 144 Monitor* _cbl_mon; // Protects the fields below.
ysr@342 145 CompletedBufferNode* _completed_buffers_head;
ysr@342 146 CompletedBufferNode* _completed_buffers_tail;
ysr@342 147 size_t _n_completed_buffers;
ysr@342 148 size_t _process_completed_threshold;
ysr@342 149 volatile bool _process_completed;
ysr@342 150
ysr@342 151 // This (and the interpretation of the first element as a "next"
ysr@342 152 // pointer) are protected by the TLOQ_FL_lock.
ysr@342 153 Mutex* _fl_lock;
ysr@342 154 void** _buf_free_list;
ysr@342 155 size_t _buf_free_list_sz;
ysr@342 156
ysr@342 157 // The size of all buffers in the set.
ysr@342 158 size_t _sz;
ysr@342 159
ysr@342 160 bool _all_active;
ysr@342 161
ysr@342 162 // If true, notify_all on _cbl_mon when the threshold is reached.
ysr@342 163 bool _notify_when_complete;
ysr@342 164
ysr@342 165 // Maximum number of elements allowed on completed queue: after that,
ysr@342 166 // enqueuer does the work itself. Zero indicates no maximum.
ysr@342 167 int _max_completed_queue;
ysr@342 168
ysr@342 169 int completed_buffers_list_length();
ysr@342 170 void assert_completed_buffer_list_len_correct_locked();
ysr@342 171 void assert_completed_buffer_list_len_correct();
ysr@342 172
ysr@342 173 protected:
ysr@342 174 // A mutator thread does the the work of processing a buffer.
ysr@342 175 // Returns "true" iff the work is complete (and the buffer may be
ysr@342 176 // deallocated).
ysr@342 177 virtual bool mut_process_buffer(void** buf) {
ysr@342 178 ShouldNotReachHere();
ysr@342 179 return false;
ysr@342 180 }
ysr@342 181
ysr@342 182 public:
ysr@342 183 // Create an empty ptr queue set.
ysr@342 184 PtrQueueSet(bool notify_when_complete = false);
ysr@342 185
ysr@342 186 // Because of init-order concerns, we can't pass these as constructor
ysr@342 187 // arguments.
ysr@342 188 void initialize(Monitor* cbl_mon, Mutex* fl_lock,
ysr@342 189 int max_completed_queue = 0) {
ysr@342 190 _max_completed_queue = max_completed_queue;
ysr@342 191 assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
ysr@342 192 _cbl_mon = cbl_mon; _fl_lock = fl_lock;
ysr@342 193 }
ysr@342 194
ysr@342 195 // Return an empty oop array of size _sz (required to be non-zero).
ysr@342 196 void** allocate_buffer();
ysr@342 197
ysr@342 198 // Return an empty buffer to the free list. The "buf" argument is
ysr@342 199 // required to be a pointer to the head of an array of length "_sz".
ysr@342 200 void deallocate_buffer(void** buf);
ysr@342 201
ysr@342 202 // Declares that "buf" is a complete buffer.
ysr@342 203 void enqueue_complete_buffer(void** buf, size_t index = 0,
ysr@342 204 bool ignore_max_completed = false);
ysr@342 205
ysr@342 206 bool completed_buffers_exist_dirty() {
ysr@342 207 return _n_completed_buffers > 0;
ysr@342 208 }
ysr@342 209
ysr@342 210 bool process_completed_buffers() { return _process_completed; }
ysr@342 211
ysr@342 212 bool active() { return _all_active; }
ysr@342 213
ysr@342 214 // Set the buffer size. Should be called before any "enqueue" operation
ysr@342 215 // can be called. And should only be called once.
ysr@342 216 void set_buffer_size(size_t sz);
ysr@342 217
ysr@342 218 // Get the buffer size.
ysr@342 219 size_t buffer_size() { return _sz; }
ysr@342 220
ysr@342 221 // Set the number of completed buffers that triggers log processing.
ysr@342 222 void set_process_completed_threshold(size_t sz);
ysr@342 223
ysr@342 224 // Must only be called at a safe point. Indicates that the buffer free
ysr@342 225 // list size may be reduced, if that is deemed desirable.
ysr@342 226 void reduce_free_list();
ysr@342 227
ysr@342 228 size_t completed_buffers_num() { return _n_completed_buffers; }
ysr@342 229 };