annotate src/share/vm/utilities/taskqueue.hpp @ 546:05c6d52fa7a9

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