Mercurial > hg > graal-compiler
annotate src/share/vm/utilities/taskqueue.cpp @ 596:83ef1482304c
6806226: Signed integer overflow in growable array code causes JVM crash
Summary: Workaround the overflow by doing the intermediate calculations in an unsigned variable.
Reviewed-by: ysr, jcoomes
author | jmasa |
---|---|
date | Tue, 24 Feb 2009 22:12:24 -0800 |
parents | 05c6d52fa7a9 |
children | 0fbdb4381b99 |
rev | line source |
---|---|
0 | 1 /* |
470 | 2 * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
20 * CA 95054 USA or visit www.sun.com if you need additional information or | |
21 * have any questions. | |
22 * | |
23 */ | |
24 | |
25 # include "incls/_precompiled.incl" | |
26 # include "incls/_taskqueue.cpp.incl" | |
27 | |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
28 #ifdef TRACESPINNING |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
29 uint ParallelTaskTerminator::_total_yields = 0; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
30 uint ParallelTaskTerminator::_total_spins = 0; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
31 uint ParallelTaskTerminator::_total_peeks = 0; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
32 #endif |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
33 |
0 | 34 bool TaskQueueSuper::peek() { |
35 return _bottom != _age.top(); | |
36 } | |
37 | |
38 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) { | |
39 const int a = 16807; | |
40 const int m = 2147483647; | |
41 const int q = 127773; /* m div a */ | |
42 const int r = 2836; /* m mod a */ | |
43 assert(sizeof(int) == 4, "I think this relies on that"); | |
44 int seed = *seed0; | |
45 int hi = seed / q; | |
46 int lo = seed % q; | |
47 int test = a * lo - r * hi; | |
48 if (test > 0) | |
49 seed = test; | |
50 else | |
51 seed = test + m; | |
52 *seed0 = seed; | |
53 return seed; | |
54 } | |
55 | |
56 ParallelTaskTerminator:: | |
57 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) : | |
58 _n_threads(n_threads), | |
59 _queue_set(queue_set), | |
60 _offered_termination(0) {} | |
61 | |
62 bool ParallelTaskTerminator::peek_in_queue_set() { | |
63 return _queue_set->peek(); | |
64 } | |
65 | |
66 void ParallelTaskTerminator::yield() { | |
67 os::yield(); | |
68 } | |
69 | |
70 void ParallelTaskTerminator::sleep(uint millis) { | |
71 os::sleep(Thread::current(), millis, false); | |
72 } | |
73 | |
342 | 74 bool |
75 ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) { | |
0 | 76 Atomic::inc(&_offered_termination); |
77 | |
541
23673011938d
6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents:
470
diff
changeset
|
78 uint yield_count = 0; |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
79 // Number of hard spin loops done since last yield |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
80 uint hard_spin_count = 0; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
81 // Number of iterations in the hard spin loop. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
82 uint hard_spin_limit = WorkStealingHardSpins; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
83 |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
84 // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
85 // If it is greater than 0, then start with a small number |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
86 // of spins and increase number with each turn at spinning until |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
87 // the count of hard spins exceeds WorkStealingSpinToYieldRatio. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
88 // Then do a yield() call and start spinning afresh. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
89 if (WorkStealingSpinToYieldRatio > 0) { |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
90 hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
91 hard_spin_limit = MAX2(hard_spin_limit, 1U); |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
92 } |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
93 // Remember the initial spin limit. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
94 uint hard_spin_start = hard_spin_limit; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
95 |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
96 // Loop waiting for all threads to offer termination or |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
97 // more work. |
0 | 98 while (true) { |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
99 // Are all threads offering termination? |
0 | 100 if (_offered_termination == _n_threads) { |
101 return true; | |
102 } else { | |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
103 // Look for more work. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
104 // Periodically sleep() instead of yield() to give threads |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
105 // waiting on the cores the chance to grab this code |
0 | 106 if (yield_count <= WorkStealingYieldsBeforeSleep) { |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
107 // Do a yield or hardspin. For purposes of deciding whether |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
108 // to sleep, count this as a yield. |
0 | 109 yield_count++; |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
110 |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
111 // Periodically call yield() instead spinning |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
112 // After WorkStealingSpinToYieldRatio spins, do a yield() call |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
113 // and reset the counts and starting limit. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
114 if (hard_spin_count > WorkStealingSpinToYieldRatio) { |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
115 yield(); |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
116 hard_spin_count = 0; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
117 hard_spin_limit = hard_spin_start; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
118 #ifdef TRACESPINNING |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
119 _total_yields++; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
120 #endif |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
121 } else { |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
122 // Hard spin this time |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
123 // Increase the hard spinning period but only up to a limit. |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
124 hard_spin_limit = MIN2(2*hard_spin_limit, |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
125 (uint) WorkStealingHardSpins); |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
126 for (uint j = 0; j < hard_spin_limit; j++) { |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
127 SpinPause(); |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
128 } |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
129 hard_spin_count++; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
130 #ifdef TRACESPINNING |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
131 _total_spins++; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
132 #endif |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
133 } |
0 | 134 } else { |
135 if (PrintGCDetails && Verbose) { | |
136 gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() " | |
137 "thread %d sleeps after %d yields", | |
138 Thread::current(), yield_count); | |
139 } | |
140 yield_count = 0; | |
141 // A sleep will cause this processor to seek work on another processor's | |
142 // runqueue, if it has nothing else to run (as opposed to the yield | |
143 // which may only move the thread to the end of the this processor's | |
144 // runqueue). | |
145 sleep(WorkStealingSleepMillis); | |
146 } | |
147 | |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
148 #ifdef TRACESPINNING |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
149 _total_peeks++; |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
150 #endif |
342 | 151 if (peek_in_queue_set() || |
152 (terminator != NULL && terminator->should_exit_termination())) { | |
0 | 153 Atomic::dec(&_offered_termination); |
154 return false; | |
155 } | |
156 } | |
157 } | |
158 } | |
159 | |
546
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
160 #ifdef TRACESPINNING |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
161 void ParallelTaskTerminator::print_termination_counts() { |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
162 gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %lld " |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
163 "Total spins: %lld Total peeks: %lld", |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
164 total_yields(), |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
165 total_spins(), |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
166 total_peeks()); |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
167 } |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
168 #endif |
05c6d52fa7a9
6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents:
541
diff
changeset
|
169 |
0 | 170 void ParallelTaskTerminator::reset_for_reuse() { |
171 if (_offered_termination != 0) { | |
172 assert(_offered_termination == _n_threads, | |
173 "Terminator may still be in use"); | |
174 _offered_termination = 0; | |
175 } | |
176 } | |
177 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
178 bool RegionTaskQueueWithOverflow::is_empty() { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
179 return (_region_queue.size() == 0) && |
0 | 180 (_overflow_stack->length() == 0); |
181 } | |
182 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
183 bool RegionTaskQueueWithOverflow::stealable_is_empty() { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
184 return _region_queue.size() == 0; |
0 | 185 } |
186 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
187 bool RegionTaskQueueWithOverflow::overflow_is_empty() { |
0 | 188 return _overflow_stack->length() == 0; |
189 } | |
190 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
191 void RegionTaskQueueWithOverflow::initialize() { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
192 _region_queue.initialize(); |
0 | 193 assert(_overflow_stack == 0, "Creating memory leak"); |
194 _overflow_stack = | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
195 new (ResourceObj::C_HEAP) GrowableArray<RegionTask>(10, true); |
0 | 196 } |
197 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
198 void RegionTaskQueueWithOverflow::save(RegionTask t) { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
199 if (TraceRegionTasksQueuing && Verbose) { |
0 | 200 gclog_or_tty->print_cr("CTQ: save " PTR_FORMAT, t); |
201 } | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
202 if(!_region_queue.push(t)) { |
0 | 203 _overflow_stack->push(t); |
204 } | |
205 } | |
206 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
207 // Note that using this method will retrieve all regions |
0 | 208 // that have been saved but that it will always check |
209 // the overflow stack. It may be more efficient to | |
210 // check the stealable queue and the overflow stack | |
211 // separately. | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
212 bool RegionTaskQueueWithOverflow::retrieve(RegionTask& region_task) { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
213 bool result = retrieve_from_overflow(region_task); |
0 | 214 if (!result) { |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
215 result = retrieve_from_stealable_queue(region_task); |
0 | 216 } |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
217 if (TraceRegionTasksQueuing && Verbose && result) { |
0 | 218 gclog_or_tty->print_cr(" CTQ: retrieve " PTR_FORMAT, result); |
219 } | |
220 return result; | |
221 } | |
222 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
223 bool RegionTaskQueueWithOverflow::retrieve_from_stealable_queue( |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
224 RegionTask& region_task) { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
225 bool result = _region_queue.pop_local(region_task); |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
226 if (TraceRegionTasksQueuing && Verbose) { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
227 gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task); |
0 | 228 } |
229 return result; | |
230 } | |
231 | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
232 bool |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
233 RegionTaskQueueWithOverflow::retrieve_from_overflow(RegionTask& region_task) { |
0 | 234 bool result; |
235 if (!_overflow_stack->is_empty()) { | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
236 region_task = _overflow_stack->pop(); |
0 | 237 result = true; |
238 } else { | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
239 region_task = (RegionTask) NULL; |
0 | 240 result = false; |
241 } | |
375
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
242 if (TraceRegionTasksQueuing && Verbose) { |
81cd571500b0
6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents:
342
diff
changeset
|
243 gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task); |
0 | 244 } |
245 return result; | |
246 } |