|
1 /* |
|
2 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 // There are various techniques that require threads to be able to log |
|
26 // addresses. For example, a generational write barrier might log |
|
27 // the addresses of modified old-generation objects. This type supports |
|
28 // this operation. |
|
29 |
|
30 class PtrQueueSet; |
|
31 |
|
32 class PtrQueue: public CHeapObj { |
|
33 |
|
34 protected: |
|
35 // The ptr queue set to which this queue belongs. |
|
36 PtrQueueSet* _qset; |
|
37 |
|
38 // Whether updates should be logged. |
|
39 bool _active; |
|
40 |
|
41 // The buffer. |
|
42 void** _buf; |
|
43 // The index at which an object was last enqueued. Starts at "_sz" |
|
44 // (indicating an empty buffer) and goes towards zero. |
|
45 size_t _index; |
|
46 |
|
47 // The size of the buffer. |
|
48 size_t _sz; |
|
49 |
|
50 // If true, the queue is permanent, and doesn't need to deallocate |
|
51 // its buffer in the destructor (since that obtains a lock which may not |
|
52 // be legally locked by then. |
|
53 bool _perm; |
|
54 |
|
55 // If there is a lock associated with this buffer, this is that lock. |
|
56 Mutex* _lock; |
|
57 |
|
58 PtrQueueSet* qset() { return _qset; } |
|
59 |
|
60 public: |
|
61 // Initialize this queue to contain a null buffer, and be part of the |
|
62 // given PtrQueueSet. |
|
63 PtrQueue(PtrQueueSet*, bool perm = false); |
|
64 // Release any contained resources. |
|
65 ~PtrQueue(); |
|
66 |
|
67 // Associate a lock with a ptr queue. |
|
68 void set_lock(Mutex* lock) { _lock = lock; } |
|
69 |
|
70 void reset() { if (_buf != NULL) _index = _sz; } |
|
71 |
|
72 // Enqueues the given "obj". |
|
73 void enqueue(void* ptr) { |
|
74 if (!_active) return; |
|
75 else enqueue_known_active(ptr); |
|
76 } |
|
77 |
|
78 inline void handle_zero_index(); |
|
79 void locking_enqueue_completed_buffer(void** buf); |
|
80 |
|
81 void enqueue_known_active(void* ptr); |
|
82 |
|
83 size_t size() { |
|
84 assert(_sz >= _index, "Invariant."); |
|
85 return _buf == NULL ? 0 : _sz - _index; |
|
86 } |
|
87 |
|
88 // Set the "active" property of the queue to "b". An enqueue to an |
|
89 // inactive thread is a no-op. Setting a queue to inactive resets its |
|
90 // log to the empty state. |
|
91 void set_active(bool b) { |
|
92 _active = b; |
|
93 if (!b && _buf != NULL) { |
|
94 _index = _sz; |
|
95 } else if (b && _buf != NULL) { |
|
96 assert(_index == _sz, "invariant: queues are empty when activated."); |
|
97 } |
|
98 } |
|
99 |
|
100 static int byte_index_to_index(int ind) { |
|
101 assert((ind % oopSize) == 0, "Invariant."); |
|
102 return ind / oopSize; |
|
103 } |
|
104 |
|
105 static int index_to_byte_index(int byte_ind) { |
|
106 return byte_ind * oopSize; |
|
107 } |
|
108 |
|
109 // To support compiler. |
|
110 static ByteSize byte_offset_of_index() { |
|
111 return byte_offset_of(PtrQueue, _index); |
|
112 } |
|
113 static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); } |
|
114 |
|
115 static ByteSize byte_offset_of_buf() { |
|
116 return byte_offset_of(PtrQueue, _buf); |
|
117 } |
|
118 static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); } |
|
119 |
|
120 static ByteSize byte_offset_of_active() { |
|
121 return byte_offset_of(PtrQueue, _active); |
|
122 } |
|
123 static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); } |
|
124 |
|
125 }; |
|
126 |
|
127 // A PtrQueueSet represents resources common to a set of pointer queues. |
|
128 // In particular, the individual queues allocate buffers from this shared |
|
129 // set, and return completed buffers to the set. |
|
130 // All these variables are are protected by the TLOQ_CBL_mon. XXX ??? |
|
131 class PtrQueueSet: public CHeapObj { |
|
132 |
|
133 protected: |
|
134 |
|
135 class CompletedBufferNode: public CHeapObj { |
|
136 public: |
|
137 void** buf; |
|
138 size_t index; |
|
139 CompletedBufferNode* next; |
|
140 CompletedBufferNode() : buf(NULL), |
|
141 index(0), next(NULL){ } |
|
142 }; |
|
143 |
|
144 Monitor* _cbl_mon; // Protects the fields below. |
|
145 CompletedBufferNode* _completed_buffers_head; |
|
146 CompletedBufferNode* _completed_buffers_tail; |
|
147 size_t _n_completed_buffers; |
|
148 size_t _process_completed_threshold; |
|
149 volatile bool _process_completed; |
|
150 |
|
151 // This (and the interpretation of the first element as a "next" |
|
152 // pointer) are protected by the TLOQ_FL_lock. |
|
153 Mutex* _fl_lock; |
|
154 void** _buf_free_list; |
|
155 size_t _buf_free_list_sz; |
|
156 |
|
157 // The size of all buffers in the set. |
|
158 size_t _sz; |
|
159 |
|
160 bool _all_active; |
|
161 |
|
162 // If true, notify_all on _cbl_mon when the threshold is reached. |
|
163 bool _notify_when_complete; |
|
164 |
|
165 // Maximum number of elements allowed on completed queue: after that, |
|
166 // enqueuer does the work itself. Zero indicates no maximum. |
|
167 int _max_completed_queue; |
|
168 |
|
169 int completed_buffers_list_length(); |
|
170 void assert_completed_buffer_list_len_correct_locked(); |
|
171 void assert_completed_buffer_list_len_correct(); |
|
172 |
|
173 protected: |
|
174 // A mutator thread does the the work of processing a buffer. |
|
175 // Returns "true" iff the work is complete (and the buffer may be |
|
176 // deallocated). |
|
177 virtual bool mut_process_buffer(void** buf) { |
|
178 ShouldNotReachHere(); |
|
179 return false; |
|
180 } |
|
181 |
|
182 public: |
|
183 // Create an empty ptr queue set. |
|
184 PtrQueueSet(bool notify_when_complete = false); |
|
185 |
|
186 // Because of init-order concerns, we can't pass these as constructor |
|
187 // arguments. |
|
188 void initialize(Monitor* cbl_mon, Mutex* fl_lock, |
|
189 int max_completed_queue = 0) { |
|
190 _max_completed_queue = max_completed_queue; |
|
191 assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?"); |
|
192 _cbl_mon = cbl_mon; _fl_lock = fl_lock; |
|
193 } |
|
194 |
|
195 // Return an empty oop array of size _sz (required to be non-zero). |
|
196 void** allocate_buffer(); |
|
197 |
|
198 // Return an empty buffer to the free list. The "buf" argument is |
|
199 // required to be a pointer to the head of an array of length "_sz". |
|
200 void deallocate_buffer(void** buf); |
|
201 |
|
202 // Declares that "buf" is a complete buffer. |
|
203 void enqueue_complete_buffer(void** buf, size_t index = 0, |
|
204 bool ignore_max_completed = false); |
|
205 |
|
206 bool completed_buffers_exist_dirty() { |
|
207 return _n_completed_buffers > 0; |
|
208 } |
|
209 |
|
210 bool process_completed_buffers() { return _process_completed; } |
|
211 |
|
212 bool active() { return _all_active; } |
|
213 |
|
214 // Set the buffer size. Should be called before any "enqueue" operation |
|
215 // can be called. And should only be called once. |
|
216 void set_buffer_size(size_t sz); |
|
217 |
|
218 // Get the buffer size. |
|
219 size_t buffer_size() { return _sz; } |
|
220 |
|
221 // Set the number of completed buffers that triggers log processing. |
|
222 void set_process_completed_threshold(size_t sz); |
|
223 |
|
224 // Must only be called at a safe point. Indicates that the buffer free |
|
225 // list size may be reduced, if that is deemed desirable. |
|
226 void reduce_free_list(); |
|
227 |
|
228 size_t completed_buffers_num() { return _n_completed_buffers; } |
|
229 }; |