1 /*
2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
114
115 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
116 const int a = 16807;
117 const int m = 2147483647;
118 const int q = 127773; /* m div a */
119 const int r = 2836; /* m mod a */
120 assert(sizeof(int) == 4, "I think this relies on that");
121 int seed = *seed0;
122 int hi = seed / q;
123 int lo = seed % q;
124 int test = a * lo - r * hi;
125 if (test > 0)
126 seed = test;
127 else
128 seed = test + m;
129 *seed0 = seed;
130 return seed;
131 }
132
133 ParallelTaskTerminator::
134 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
135 _n_threads(n_threads),
136 _queue_set(queue_set),
137 _offered_termination(0) {}
138
139 bool ParallelTaskTerminator::peek_in_queue_set() {
140 return _queue_set->peek();
141 }
142
143 void ParallelTaskTerminator::yield() {
144 assert(_offered_termination <= _n_threads, "Invariant");
145 os::naked_yield();
146 }
147
148 void ParallelTaskTerminator::sleep(uint millis) {
149 assert(_offered_termination <= _n_threads, "Invariant");
150 os::sleep(Thread::current(), millis, false);
151 }
152
153 bool
154 ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
155 assert(_n_threads > 0, "Initialization is incorrect");
156 assert(_offered_termination < _n_threads, "Invariant");
157 Atomic::inc(&_offered_termination);
158
159 uint yield_count = 0;
160 // Number of hard spin loops done since last yield
161 uint hard_spin_count = 0;
162 // Number of iterations in the hard spin loop.
163 uint hard_spin_limit = WorkStealingHardSpins;
164
165 // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done.
166 // If it is greater than 0, then start with a small number
167 // of spins and increase number with each turn at spinning until
168 // the count of hard spins exceeds WorkStealingSpinToYieldRatio.
169 // Then do a yield() call and start spinning afresh.
170 if (WorkStealingSpinToYieldRatio > 0) {
171 hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio;
172 hard_spin_limit = MAX2(hard_spin_limit, 1U);
173 }
174 // Remember the initial spin limit.
175 uint hard_spin_start = hard_spin_limit;
176
177 // Loop waiting for all threads to offer termination or
215 }
216 } else {
217 if (PrintGCDetails && Verbose) {
218 gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
219 "thread %d sleeps after %d yields",
220 Thread::current(), yield_count);
221 }
222 yield_count = 0;
223 // A sleep will cause this processor to seek work on another processor's
224 // runqueue, if it has nothing else to run (as opposed to the yield
225 // which may only move the thread to the end of the this processor's
226 // runqueue).
227 sleep(WorkStealingSleepMillis);
228 }
229
230 #ifdef TRACESPINNING
231 _total_peeks++;
232 #endif
233 if (peek_in_queue_set() ||
234 (terminator != NULL && terminator->should_exit_termination())) {
235 Atomic::dec(&_offered_termination);
236 assert(_offered_termination < _n_threads, "Invariant");
237 return false;
238 }
239 }
240 }
241 }
242
243 #ifdef TRACESPINNING
244 void ParallelTaskTerminator::print_termination_counts() {
245 gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %u"
246 " Total spins: %u Total peeks: %u",
247 total_yields(),
248 total_spins(),
249 total_peeks());
250 }
251 #endif
252
253 void ParallelTaskTerminator::reset_for_reuse() {
254 if (_offered_termination != 0) {
255 assert(_offered_termination == _n_threads,
256 "Terminator may still be in use");
257 _offered_termination = 0;
258 }
259 }
260
261 #ifdef ASSERT
262 bool ObjArrayTask::is_valid() const {
263 return _obj != NULL && _obj->is_objArray() && _index > 0 &&
264 _index < objArrayOop(_obj)->length();
265 }
266 #endif // ASSERT
267
268 void ParallelTaskTerminator::reset_for_reuse(int n_threads) {
269 reset_for_reuse();
270 _n_threads = n_threads;
271 }
|
1 /*
2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
114
115 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
116 const int a = 16807;
117 const int m = 2147483647;
118 const int q = 127773; /* m div a */
119 const int r = 2836; /* m mod a */
120 assert(sizeof(int) == 4, "I think this relies on that");
121 int seed = *seed0;
122 int hi = seed / q;
123 int lo = seed % q;
124 int test = a * lo - r * hi;
125 if (test > 0)
126 seed = test;
127 else
128 seed = test + m;
129 *seed0 = seed;
130 return seed;
131 }
132
133 ParallelTaskTerminator::
134 ParallelTaskTerminator(uint n_threads, TaskQueueSetSuper* queue_set) :
135 _n_threads(n_threads),
136 _queue_set(queue_set),
137 _offered_termination(0) {}
138
139 bool ParallelTaskTerminator::peek_in_queue_set() {
140 return _queue_set->peek();
141 }
142
143 void ParallelTaskTerminator::yield() {
144 assert(_offered_termination <= _n_threads, "Invariant");
145 os::naked_yield();
146 }
147
148 void ParallelTaskTerminator::sleep(uint millis) {
149 assert(_offered_termination <= _n_threads, "Invariant");
150 os::sleep(Thread::current(), millis, false);
151 }
152
153 bool
154 ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
155 assert(_n_threads > 0, "Initialization is incorrect");
156 assert(_offered_termination < _n_threads, "Invariant");
157 Atomic::inc((int *)&_offered_termination);
158
159 uint yield_count = 0;
160 // Number of hard spin loops done since last yield
161 uint hard_spin_count = 0;
162 // Number of iterations in the hard spin loop.
163 uint hard_spin_limit = WorkStealingHardSpins;
164
165 // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done.
166 // If it is greater than 0, then start with a small number
167 // of spins and increase number with each turn at spinning until
168 // the count of hard spins exceeds WorkStealingSpinToYieldRatio.
169 // Then do a yield() call and start spinning afresh.
170 if (WorkStealingSpinToYieldRatio > 0) {
171 hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio;
172 hard_spin_limit = MAX2(hard_spin_limit, 1U);
173 }
174 // Remember the initial spin limit.
175 uint hard_spin_start = hard_spin_limit;
176
177 // Loop waiting for all threads to offer termination or
215 }
216 } else {
217 if (PrintGCDetails && Verbose) {
218 gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
219 "thread %d sleeps after %d yields",
220 Thread::current(), yield_count);
221 }
222 yield_count = 0;
223 // A sleep will cause this processor to seek work on another processor's
224 // runqueue, if it has nothing else to run (as opposed to the yield
225 // which may only move the thread to the end of the this processor's
226 // runqueue).
227 sleep(WorkStealingSleepMillis);
228 }
229
230 #ifdef TRACESPINNING
231 _total_peeks++;
232 #endif
233 if (peek_in_queue_set() ||
234 (terminator != NULL && terminator->should_exit_termination())) {
235 Atomic::dec((int *)&_offered_termination);
236 assert(_offered_termination < _n_threads, "Invariant");
237 return false;
238 }
239 }
240 }
241 }
242
243 #ifdef TRACESPINNING
244 void ParallelTaskTerminator::print_termination_counts() {
245 gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %u"
246 " Total spins: %u Total peeks: %u",
247 total_yields(),
248 total_spins(),
249 total_peeks());
250 }
251 #endif
252
253 void ParallelTaskTerminator::reset_for_reuse() {
254 if (_offered_termination != 0) {
255 assert(_offered_termination == _n_threads,
256 "Terminator may still be in use");
257 _offered_termination = 0;
258 }
259 }
260
261 #ifdef ASSERT
262 bool ObjArrayTask::is_valid() const {
263 return _obj != NULL && _obj->is_objArray() && _index > 0 &&
264 _index < objArrayOop(_obj)->length();
265 }
266 #endif // ASSERT
267
268 void ParallelTaskTerminator::reset_for_reuse(uint n_threads) {
269 reset_for_reuse();
270 _n_threads = n_threads;
271 }
|