author | ysr |
Fri, 30 Jan 2009 14:17:52 -0800 | |
changeset 2005 | 42075507972b |
parent 1407 | 9006b01ba3fd |
child 2010 | c13462bbad17 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
670 | 2 |
* Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved. |
1 | 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 |
||
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
25 |
#ifdef LP64 |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
26 |
typedef juint TAG_TYPE; |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
27 |
// for a taskqueue size of 4M |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
28 |
#define LOG_TASKQ_SIZE 22 |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
29 |
#else |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
30 |
typedef jushort TAG_TYPE; |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
31 |
// for a taskqueue size of 16K |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
32 |
#define LOG_TASKQ_SIZE 14 |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
33 |
#endif |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
34 |
|
1 | 35 |
class TaskQueueSuper: public CHeapObj { |
36 |
protected: |
|
37 |
// The first free element after the last one pushed (mod _n). |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
38 |
volatile uint _bottom; |
1 | 39 |
|
40 |
// log2 of the size of the queue. |
|
41 |
enum SomeProtectedConstants { |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
42 |
Log_n = LOG_TASKQ_SIZE |
1 | 43 |
}; |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
44 |
#undef LOG_TASKQ_SIZE |
1 | 45 |
|
46 |
// Size of the queue. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
47 |
uint n() { return (1 << Log_n); } |
1 | 48 |
// For computing "x mod n" efficiently. |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
49 |
uint n_mod_mask() { return n() - 1; } |
1 | 50 |
|
51 |
struct Age { |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
52 |
TAG_TYPE _top; |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
53 |
TAG_TYPE _tag; |
1 | 54 |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
55 |
TAG_TYPE tag() const { return _tag; } |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
56 |
TAG_TYPE top() const { return _top; } |
1 | 57 |
|
58 |
Age() { _tag = 0; _top = 0; } |
|
59 |
||
60 |
friend bool operator ==(const Age& a1, const Age& a2) { |
|
61 |
return a1.tag() == a2.tag() && a1.top() == a2.top(); |
|
62 |
} |
|
63 |
}; |
|
64 |
Age _age; |
|
65 |
// These make sure we do single atomic reads and writes. |
|
66 |
Age get_age() { |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
67 |
uint res = *(volatile uint*)(&_age); |
1 | 68 |
return *(Age*)(&res); |
69 |
} |
|
70 |
void set_age(Age a) { |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
71 |
*(volatile uint*)(&_age) = *(uint*)(&a); |
1 | 72 |
} |
73 |
||
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
74 |
TAG_TYPE get_top() { |
1 | 75 |
return get_age().top(); |
76 |
} |
|
77 |
||
78 |
// These both operate mod _n. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
79 |
uint increment_index(uint ind) { |
1 | 80 |
return (ind + 1) & n_mod_mask(); |
81 |
} |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
82 |
uint decrement_index(uint ind) { |
1 | 83 |
return (ind - 1) & n_mod_mask(); |
84 |
} |
|
85 |
||
86 |
// Returns a number in the range [0.._n). If the result is "n-1", it |
|
87 |
// should be interpreted as 0. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
88 |
uint dirty_size(uint bot, uint top) { |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
89 |
return ((int)bot - (int)top) & n_mod_mask(); |
1 | 90 |
} |
91 |
||
92 |
// Returns the size corresponding to the given "bot" and "top". |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
93 |
uint size(uint bot, uint top) { |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
94 |
uint sz = dirty_size(bot, top); |
1 | 95 |
// Has the queue "wrapped", so that bottom is less than top? |
96 |
// There's a complicated special case here. A pair of threads could |
|
97 |
// perform pop_local and pop_global operations concurrently, starting |
|
98 |
// from a state in which _bottom == _top+1. The pop_local could |
|
99 |
// succeed in decrementing _bottom, and the pop_global in incrementing |
|
100 |
// _top (in which case the pop_global will be awarded the contested |
|
101 |
// queue element.) The resulting state must be interpreted as an empty |
|
102 |
// queue. (We only need to worry about one such event: only the queue |
|
103 |
// owner performs pop_local's, and several concurrent threads |
|
104 |
// attempting to perform the pop_global will all perform the same CAS, |
|
105 |
// and only one can succeed. Any stealing thread that reads after |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
106 |
// either the increment or decrement will see an empty queue, and will |
1 | 107 |
// not join the competitors. The "sz == -1 || sz == _n-1" state will |
108 |
// not be modified by concurrent queues, so the owner thread can reset |
|
109 |
// the state to _bottom == top so subsequent pushes will be performed |
|
110 |
// normally. |
|
111 |
if (sz == (n()-1)) return 0; |
|
112 |
else return sz; |
|
113 |
} |
|
114 |
||
115 |
public: |
|
116 |
TaskQueueSuper() : _bottom(0), _age() {} |
|
117 |
||
118 |
// Return "true" if the TaskQueue contains any tasks. |
|
119 |
bool peek(); |
|
120 |
||
121 |
// Return an estimate of the number of elements in the queue. |
|
122 |
// The "careful" version admits the possibility of pop_local/pop_global |
|
123 |
// races. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
124 |
uint size() { |
1 | 125 |
return size(_bottom, get_top()); |
126 |
} |
|
127 |
||
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
128 |
uint dirty_size() { |
1 | 129 |
return dirty_size(_bottom, get_top()); |
130 |
} |
|
131 |
||
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
132 |
void set_empty() { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
133 |
_bottom = 0; |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
134 |
_age = Age(); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
135 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
136 |
|
1 | 137 |
// Maximum number of elements allowed in the queue. This is two less |
138 |
// than the actual queue size, for somewhat complicated reasons. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
139 |
uint max_elems() { return n() - 2; } |
1 | 140 |
|
141 |
}; |
|
142 |
||
143 |
template<class E> class GenericTaskQueue: public TaskQueueSuper { |
|
144 |
private: |
|
145 |
// Slow paths for push, pop_local. (pop_global has no fast path.) |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
146 |
bool push_slow(E t, uint dirty_n_elems); |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
147 |
bool pop_local_slow(uint localBot, Age oldAge); |
1 | 148 |
|
149 |
public: |
|
150 |
// Initializes the queue to empty. |
|
151 |
GenericTaskQueue(); |
|
152 |
||
153 |
void initialize(); |
|
154 |
||
155 |
// Push the task "t" on the queue. Returns "false" iff the queue is |
|
156 |
// full. |
|
157 |
inline bool push(E t); |
|
158 |
||
159 |
// If succeeds in claiming a task (from the 'local' end, that is, the |
|
160 |
// most recently pushed task), returns "true" and sets "t" to that task. |
|
161 |
// Otherwise, the queue is empty and returns false. |
|
162 |
inline bool pop_local(E& t); |
|
163 |
||
164 |
// If succeeds in claiming a task (from the 'global' end, that is, the |
|
165 |
// least recently pushed task), returns "true" and sets "t" to that task. |
|
166 |
// Otherwise, the queue is empty and returns false. |
|
167 |
bool pop_global(E& t); |
|
168 |
||
169 |
// Delete any resource associated with the queue. |
|
170 |
~GenericTaskQueue(); |
|
171 |
||
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
172 |
// apply the closure to all elements in the task queue |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
173 |
void oops_do(OopClosure* f); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
174 |
|
1 | 175 |
private: |
176 |
// Element array. |
|
177 |
volatile E* _elems; |
|
178 |
}; |
|
179 |
||
180 |
template<class E> |
|
181 |
GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() { |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
182 |
assert(sizeof(Age) == sizeof(int), "Depends on this."); |
1 | 183 |
} |
184 |
||
185 |
template<class E> |
|
186 |
void GenericTaskQueue<E>::initialize() { |
|
187 |
_elems = NEW_C_HEAP_ARRAY(E, n()); |
|
188 |
guarantee(_elems != NULL, "Allocation failed."); |
|
189 |
} |
|
190 |
||
191 |
template<class E> |
|
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
192 |
void GenericTaskQueue<E>::oops_do(OopClosure* f) { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
193 |
// tty->print_cr("START OopTaskQueue::oops_do"); |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
194 |
uint iters = size(); |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
195 |
uint index = _bottom; |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
196 |
for (uint i = 0; i < iters; ++i) { |
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
197 |
index = decrement_index(index); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
198 |
// tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T, |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
199 |
// index, &_elems[index], _elems[index]); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
200 |
E* t = (E*)&_elems[index]; // cast away volatility |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
201 |
oop* p = (oop*)t; |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
202 |
assert((*t)->is_oop_or_null(), "Not an oop or null"); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
203 |
f->do_oop(p); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
204 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
205 |
// tty->print_cr("END OopTaskQueue::oops_do"); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
206 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
207 |
|
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
208 |
|
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
209 |
template<class E> |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
210 |
bool GenericTaskQueue<E>::push_slow(E t, uint dirty_n_elems) { |
1 | 211 |
if (dirty_n_elems == n() - 1) { |
212 |
// Actually means 0, so do the push. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
213 |
uint localBot = _bottom; |
1 | 214 |
_elems[localBot] = t; |
215 |
_bottom = increment_index(localBot); |
|
216 |
return true; |
|
217 |
} else |
|
218 |
return false; |
|
219 |
} |
|
220 |
||
221 |
template<class E> |
|
222 |
bool GenericTaskQueue<E>:: |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
223 |
pop_local_slow(uint localBot, Age oldAge) { |
1 | 224 |
// This queue was observed to contain exactly one element; either this |
225 |
// thread will claim it, or a competing "pop_global". In either case, |
|
226 |
// the queue will be logically empty afterwards. Create a new Age value |
|
227 |
// that represents the empty queue for the given value of "_bottom". (We |
|
228 |
// must also increment "tag" because of the case where "bottom == 1", |
|
229 |
// "top == 0". A pop_global could read the queue element in that case, |
|
230 |
// then have the owner thread do a pop followed by another push. Without |
|
231 |
// the incrementing of "tag", the pop_global's CAS could succeed, |
|
232 |
// allowing it to believe it has claimed the stale element.) |
|
233 |
Age newAge; |
|
234 |
newAge._top = localBot; |
|
235 |
newAge._tag = oldAge.tag() + 1; |
|
236 |
// Perhaps a competing pop_global has already incremented "top", in which |
|
237 |
// case it wins the element. |
|
238 |
if (localBot == oldAge.top()) { |
|
239 |
Age tempAge; |
|
240 |
// No competing pop_global has yet incremented "top"; we'll try to |
|
241 |
// install new_age, thus claiming the element. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
242 |
assert(sizeof(Age) == sizeof(int), "Assumption about CAS unit."); |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
243 |
*(uint*)&tempAge = Atomic::cmpxchg(*(uint*)&newAge, (volatile uint*)&_age, *(uint*)&oldAge); |
1 | 244 |
if (tempAge == oldAge) { |
245 |
// We win. |
|
246 |
assert(dirty_size(localBot, get_top()) != n() - 1, |
|
247 |
"Shouldn't be possible..."); |
|
248 |
return true; |
|
249 |
} |
|
250 |
} |
|
251 |
// We fail; a completing pop_global gets the element. But the queue is |
|
252 |
// empty (and top is greater than bottom.) Fix this representation of |
|
253 |
// the empty queue to become the canonical one. |
|
254 |
set_age(newAge); |
|
255 |
assert(dirty_size(localBot, get_top()) != n() - 1, |
|
256 |
"Shouldn't be possible..."); |
|
257 |
return false; |
|
258 |
} |
|
259 |
||
260 |
template<class E> |
|
261 |
bool GenericTaskQueue<E>::pop_global(E& t) { |
|
262 |
Age newAge; |
|
263 |
Age oldAge = get_age(); |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
264 |
uint localBot = _bottom; |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
265 |
uint n_elems = size(localBot, oldAge.top()); |
1 | 266 |
if (n_elems == 0) { |
267 |
return false; |
|
268 |
} |
|
269 |
t = _elems[oldAge.top()]; |
|
270 |
newAge = oldAge; |
|
271 |
newAge._top = increment_index(newAge.top()); |
|
272 |
if ( newAge._top == 0 ) newAge._tag++; |
|
273 |
Age resAge; |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
274 |
*(uint*)&resAge = Atomic::cmpxchg(*(uint*)&newAge, (volatile uint*)&_age, *(uint*)&oldAge); |
1 | 275 |
// Note that using "_bottom" here might fail, since a pop_local might |
276 |
// have decremented it. |
|
277 |
assert(dirty_size(localBot, newAge._top) != n() - 1, |
|
278 |
"Shouldn't be possible..."); |
|
279 |
return (resAge == oldAge); |
|
280 |
} |
|
281 |
||
282 |
template<class E> |
|
283 |
GenericTaskQueue<E>::~GenericTaskQueue() { |
|
284 |
FREE_C_HEAP_ARRAY(E, _elems); |
|
285 |
} |
|
286 |
||
287 |
// Inherits the typedef of "Task" from above. |
|
288 |
class TaskQueueSetSuper: public CHeapObj { |
|
289 |
protected: |
|
290 |
static int randomParkAndMiller(int* seed0); |
|
291 |
public: |
|
292 |
// Returns "true" if some TaskQueue in the set contains a task. |
|
293 |
virtual bool peek() = 0; |
|
294 |
}; |
|
295 |
||
296 |
template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper { |
|
297 |
private: |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
298 |
uint _n; |
1 | 299 |
GenericTaskQueue<E>** _queues; |
300 |
||
301 |
public: |
|
302 |
GenericTaskQueueSet(int n) : _n(n) { |
|
303 |
typedef GenericTaskQueue<E>* GenericTaskQueuePtr; |
|
304 |
_queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n); |
|
305 |
guarantee(_queues != NULL, "Allocation failure."); |
|
306 |
for (int i = 0; i < n; i++) { |
|
307 |
_queues[i] = NULL; |
|
308 |
} |
|
309 |
} |
|
310 |
||
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
311 |
bool steal_1_random(uint queue_num, int* seed, E& t); |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
312 |
bool steal_best_of_2(uint queue_num, int* seed, E& t); |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
313 |
bool steal_best_of_all(uint queue_num, int* seed, E& t); |
1 | 314 |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
315 |
void register_queue(uint i, GenericTaskQueue<E>* q); |
1 | 316 |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
317 |
GenericTaskQueue<E>* queue(uint n); |
1 | 318 |
|
319 |
// The thread with queue number "queue_num" (and whose random number seed |
|
320 |
// is at "seed") is trying to steal a task from some other queue. (It |
|
321 |
// may try several queues, according to some configuration parameter.) |
|
322 |
// If some steal succeeds, returns "true" and sets "t" the stolen task, |
|
323 |
// otherwise returns false. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
324 |
bool steal(uint queue_num, int* seed, E& t); |
1 | 325 |
|
326 |
bool peek(); |
|
327 |
}; |
|
328 |
||
329 |
template<class E> |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
330 |
void GenericTaskQueueSet<E>::register_queue(uint i, GenericTaskQueue<E>* q) { |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
331 |
assert(i < _n, "index out of range."); |
1 | 332 |
_queues[i] = q; |
333 |
} |
|
334 |
||
335 |
template<class E> |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
336 |
GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(uint i) { |
1 | 337 |
return _queues[i]; |
338 |
} |
|
339 |
||
340 |
template<class E> |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
341 |
bool GenericTaskQueueSet<E>::steal(uint queue_num, int* seed, E& t) { |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
342 |
for (uint i = 0; i < 2 * _n; i++) |
1 | 343 |
if (steal_best_of_2(queue_num, seed, t)) |
344 |
return true; |
|
345 |
return false; |
|
346 |
} |
|
347 |
||
348 |
template<class E> |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
349 |
bool GenericTaskQueueSet<E>::steal_best_of_all(uint queue_num, int* seed, E& t) { |
1 | 350 |
if (_n > 2) { |
351 |
int best_k; |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
352 |
uint best_sz = 0; |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
353 |
for (uint k = 0; k < _n; k++) { |
1 | 354 |
if (k == queue_num) continue; |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
355 |
uint sz = _queues[k]->size(); |
1 | 356 |
if (sz > best_sz) { |
357 |
best_sz = sz; |
|
358 |
best_k = k; |
|
359 |
} |
|
360 |
} |
|
361 |
return best_sz > 0 && _queues[best_k]->pop_global(t); |
|
362 |
} else if (_n == 2) { |
|
363 |
// Just try the other one. |
|
364 |
int k = (queue_num + 1) % 2; |
|
365 |
return _queues[k]->pop_global(t); |
|
366 |
} else { |
|
367 |
assert(_n == 1, "can't be zero."); |
|
368 |
return false; |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
template<class E> |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
373 |
bool GenericTaskQueueSet<E>::steal_1_random(uint queue_num, int* seed, E& t) { |
1 | 374 |
if (_n > 2) { |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
375 |
uint k = queue_num; |
1 | 376 |
while (k == queue_num) k = randomParkAndMiller(seed) % _n; |
377 |
return _queues[2]->pop_global(t); |
|
378 |
} else if (_n == 2) { |
|
379 |
// Just try the other one. |
|
380 |
int k = (queue_num + 1) % 2; |
|
381 |
return _queues[k]->pop_global(t); |
|
382 |
} else { |
|
383 |
assert(_n == 1, "can't be zero."); |
|
384 |
return false; |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
template<class E> |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
389 |
bool GenericTaskQueueSet<E>::steal_best_of_2(uint queue_num, int* seed, E& t) { |
1 | 390 |
if (_n > 2) { |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
391 |
uint k1 = queue_num; |
1 | 392 |
while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n; |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
393 |
uint k2 = queue_num; |
1 | 394 |
while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n; |
395 |
// Sample both and try the larger. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
396 |
uint sz1 = _queues[k1]->size(); |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
397 |
uint sz2 = _queues[k2]->size(); |
1 | 398 |
if (sz2 > sz1) return _queues[k2]->pop_global(t); |
399 |
else return _queues[k1]->pop_global(t); |
|
400 |
} else if (_n == 2) { |
|
401 |
// Just try the other one. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
402 |
uint k = (queue_num + 1) % 2; |
1 | 403 |
return _queues[k]->pop_global(t); |
404 |
} else { |
|
405 |
assert(_n == 1, "can't be zero."); |
|
406 |
return false; |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
template<class E> |
|
411 |
bool GenericTaskQueueSet<E>::peek() { |
|
412 |
// Try all the queues. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
413 |
for (uint j = 0; j < _n; j++) { |
1 | 414 |
if (_queues[j]->peek()) |
415 |
return true; |
|
416 |
} |
|
417 |
return false; |
|
418 |
} |
|
419 |
||
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
420 |
// When to terminate from the termination protocol. |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
421 |
class TerminatorTerminator: public CHeapObj { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
422 |
public: |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
423 |
virtual bool should_exit_termination() = 0; |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
424 |
}; |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
425 |
|
1 | 426 |
// A class to aid in the termination of a set of parallel tasks using |
427 |
// TaskQueueSet's for work stealing. |
|
428 |
||
429 |
class ParallelTaskTerminator: public StackObj { |
|
430 |
private: |
|
431 |
int _n_threads; |
|
432 |
TaskQueueSetSuper* _queue_set; |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
433 |
int _offered_termination; |
1 | 434 |
|
435 |
bool peek_in_queue_set(); |
|
436 |
protected: |
|
437 |
virtual void yield(); |
|
438 |
void sleep(uint millis); |
|
439 |
||
440 |
public: |
|
441 |
||
442 |
// "n_threads" is the number of threads to be terminated. "queue_set" is a |
|
443 |
// queue sets of work queues of other threads. |
|
444 |
ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set); |
|
445 |
||
446 |
// The current thread has no work, and is ready to terminate if everyone |
|
447 |
// else is. If returns "true", all threads are terminated. If returns |
|
448 |
// "false", available work has been observed in one of the task queues, |
|
449 |
// so the global task is not complete. |
|
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
450 |
bool offer_termination() { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
451 |
return offer_termination(NULL); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
452 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
453 |
|
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
454 |
// As above, but it also terminates of the should_exit_termination() |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
455 |
// method of the terminator parameter returns true. If terminator is |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
456 |
// NULL, then it is ignored. |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
360
diff
changeset
|
457 |
bool offer_termination(TerminatorTerminator* terminator); |
1 | 458 |
|
459 |
// Reset the terminator, so that it may be reused again. |
|
460 |
// The caller is responsible for ensuring that this is done |
|
461 |
// in an MT-safe manner, once the previous round of use of |
|
462 |
// the terminator is finished. |
|
463 |
void reset_for_reuse(); |
|
464 |
||
465 |
}; |
|
466 |
||
467 |
#define SIMPLE_STACK 0 |
|
468 |
||
469 |
template<class E> inline bool GenericTaskQueue<E>::push(E t) { |
|
470 |
#if SIMPLE_STACK |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
471 |
uint localBot = _bottom; |
1 | 472 |
if (_bottom < max_elems()) { |
473 |
_elems[localBot] = t; |
|
474 |
_bottom = localBot + 1; |
|
475 |
return true; |
|
476 |
} else { |
|
477 |
return false; |
|
478 |
} |
|
479 |
#else |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
480 |
uint localBot = _bottom; |
1 | 481 |
assert((localBot >= 0) && (localBot < n()), "_bottom out of range."); |
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
482 |
TAG_TYPE top = get_top(); |
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
483 |
uint dirty_n_elems = dirty_size(localBot, top); |
1 | 484 |
assert((dirty_n_elems >= 0) && (dirty_n_elems < n()), |
485 |
"n_elems out of range."); |
|
486 |
if (dirty_n_elems < max_elems()) { |
|
487 |
_elems[localBot] = t; |
|
488 |
_bottom = increment_index(localBot); |
|
489 |
return true; |
|
490 |
} else { |
|
491 |
return push_slow(t, dirty_n_elems); |
|
492 |
} |
|
493 |
#endif |
|
494 |
} |
|
495 |
||
496 |
template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) { |
|
497 |
#if SIMPLE_STACK |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
498 |
uint localBot = _bottom; |
1 | 499 |
assert(localBot > 0, "precondition."); |
500 |
localBot--; |
|
501 |
t = _elems[localBot]; |
|
502 |
_bottom = localBot; |
|
503 |
return true; |
|
504 |
#else |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
505 |
uint localBot = _bottom; |
1 | 506 |
// This value cannot be n-1. That can only occur as a result of |
507 |
// the assignment to bottom in this method. If it does, this method |
|
508 |
// resets the size( to 0 before the next call (which is sequential, |
|
509 |
// since this is pop_local.) |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
510 |
uint dirty_n_elems = dirty_size(localBot, get_top()); |
1 | 511 |
assert(dirty_n_elems != n() - 1, "Shouldn't be possible..."); |
512 |
if (dirty_n_elems == 0) return false; |
|
513 |
localBot = decrement_index(localBot); |
|
514 |
_bottom = localBot; |
|
515 |
// This is necessary to prevent any read below from being reordered |
|
516 |
// before the store just above. |
|
517 |
OrderAccess::fence(); |
|
518 |
t = _elems[localBot]; |
|
519 |
// This is a second read of "age"; the "size()" above is the first. |
|
520 |
// If there's still at least one element in the queue, based on the |
|
521 |
// "_bottom" and "age" we've read, then there can be no interference with |
|
522 |
// a "pop_global" operation, and we're done. |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
523 |
TAG_TYPE tp = get_top(); // XXX |
1 | 524 |
if (size(localBot, tp) > 0) { |
525 |
assert(dirty_size(localBot, tp) != n() - 1, |
|
526 |
"Shouldn't be possible..."); |
|
527 |
return true; |
|
528 |
} else { |
|
529 |
// Otherwise, the queue contained exactly one element; we take the slow |
|
530 |
// path. |
|
531 |
return pop_local_slow(localBot, get_age()); |
|
532 |
} |
|
533 |
#endif |
|
534 |
} |
|
535 |
||
536 |
typedef oop Task; |
|
537 |
typedef GenericTaskQueue<Task> OopTaskQueue; |
|
538 |
typedef GenericTaskQueueSet<Task> OopTaskQueueSet; |
|
539 |
||
360
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
540 |
|
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
541 |
#define COMPRESSED_OOP_MASK 1 |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
542 |
|
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
543 |
// This is a container class for either an oop* or a narrowOop*. |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
544 |
// Both are pushed onto a task queue and the consumer will test is_narrow() |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
545 |
// to determine which should be processed. |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
546 |
class StarTask { |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
547 |
void* _holder; // either union oop* or narrowOop* |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
548 |
public: |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
549 |
StarTask(narrowOop *p) { _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); } |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
550 |
StarTask(oop *p) { _holder = (void*)p; } |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
551 |
StarTask() { _holder = NULL; } |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
552 |
operator oop*() { return (oop*)_holder; } |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
553 |
operator narrowOop*() { |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
554 |
return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK); |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
555 |
} |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
556 |
|
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
557 |
// Operators to preserve const/volatile in assignments required by gcc |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
558 |
void operator=(const volatile StarTask& t) volatile { _holder = t._holder; } |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
559 |
|
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
560 |
bool is_narrow() const { |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
561 |
return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0); |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
562 |
} |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
563 |
}; |
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
564 |
|
1 | 565 |
typedef GenericTaskQueue<StarTask> OopStarTaskQueue; |
566 |
typedef GenericTaskQueueSet<StarTask> OopStarTaskQueueSet; |
|
567 |
||
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
568 |
typedef size_t RegionTask; // index for region |
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
569 |
typedef GenericTaskQueue<RegionTask> RegionTaskQueue; |
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
570 |
typedef GenericTaskQueueSet<RegionTask> RegionTaskQueueSet; |
1 | 571 |
|
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
572 |
class RegionTaskQueueWithOverflow: public CHeapObj { |
1 | 573 |
protected: |
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
574 |
RegionTaskQueue _region_queue; |
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
575 |
GrowableArray<RegionTask>* _overflow_stack; |
1 | 576 |
|
577 |
public: |
|
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
578 |
RegionTaskQueueWithOverflow() : _overflow_stack(NULL) {} |
1 | 579 |
// Initialize both stealable queue and overflow |
580 |
void initialize(); |
|
581 |
// Save first to stealable queue and then to overflow |
|
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
582 |
void save(RegionTask t); |
1 | 583 |
// Retrieve first from overflow and then from stealable queue |
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
584 |
bool retrieve(RegionTask& region_index); |
1 | 585 |
// Retrieve from stealable queue |
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
586 |
bool retrieve_from_stealable_queue(RegionTask& region_index); |
1 | 587 |
// Retrieve from overflow |
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
588 |
bool retrieve_from_overflow(RegionTask& region_index); |
1 | 589 |
bool is_empty(); |
590 |
bool stealable_is_empty(); |
|
591 |
bool overflow_is_empty(); |
|
2005
42075507972b
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
1407
diff
changeset
|
592 |
uint stealable_size() { return _region_queue.size(); } |
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
593 |
RegionTaskQueue* task_queue() { return &_region_queue; } |
1 | 594 |
}; |
595 |
||
1407
9006b01ba3fd
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
1388
diff
changeset
|
596 |
#define USE_RegionTaskQueueWithOverflow |