Mercurial > hg > truffle
annotate src/share/vm/utilities/workgroup.cpp @ 4530:6c6cb7be1324
bugfix
author | Christian Haeubl <christian.haeubl@oracle.com> |
---|---|
date | Wed, 08 Feb 2012 21:13:35 -0800 |
parents | bca17e38de00 |
children | 441e946dc1af |
rev | line source |
---|---|
0 | 1 /* |
2369
92da084fefc9
6668573: CMS: reference processing crash if ParallelCMSThreads > ParallelGCThreads
ysr
parents:
1972
diff
changeset
|
2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
342
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
342
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
342
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "memory/allocation.hpp" | |
27 #include "memory/allocation.inline.hpp" | |
28 #include "runtime/os.hpp" | |
29 #include "utilities/workgroup.hpp" | |
0 | 30 |
31 // Definitions of WorkGang methods. | |
32 | |
33 AbstractWorkGang::AbstractWorkGang(const char* name, | |
342 | 34 bool are_GC_task_threads, |
35 bool are_ConcurrentGC_threads) : | |
0 | 36 _name(name), |
342 | 37 _are_GC_task_threads(are_GC_task_threads), |
38 _are_ConcurrentGC_threads(are_ConcurrentGC_threads) { | |
39 | |
40 assert(!(are_GC_task_threads && are_ConcurrentGC_threads), | |
41 "They cannot both be STW GC and Concurrent threads" ); | |
42 | |
0 | 43 // Other initialization. |
44 _monitor = new Monitor(/* priority */ Mutex::leaf, | |
45 /* name */ "WorkGroup monitor", | |
342 | 46 /* allow_vm_block */ are_GC_task_threads); |
0 | 47 assert(monitor() != NULL, "Failed to allocate monitor"); |
48 _terminate = false; | |
49 _task = NULL; | |
50 _sequence_number = 0; | |
51 _started_workers = 0; | |
52 _finished_workers = 0; | |
53 } | |
54 | |
55 WorkGang::WorkGang(const char* name, | |
342 | 56 int workers, |
57 bool are_GC_task_threads, | |
58 bool are_ConcurrentGC_threads) : | |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
59 AbstractWorkGang(name, are_GC_task_threads, are_ConcurrentGC_threads) { |
0 | 60 _total_workers = workers; |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
61 } |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
62 |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
63 GangWorker* WorkGang::allocate_worker(int which) { |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
64 GangWorker* new_worker = new GangWorker(this, which); |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
65 return new_worker; |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
66 } |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
67 |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
68 // The current implementation will exit if the allocation |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
69 // of any worker fails. Still, return a boolean so that |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
70 // a future implementation can possibly do a partial |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
71 // initialization of the workers and report such to the |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
72 // caller. |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
73 bool WorkGang::initialize_workers() { |
342 | 74 |
0 | 75 if (TraceWorkGang) { |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
76 tty->print_cr("Constructing work gang %s with %d threads", |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
77 name(), |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
78 total_workers()); |
0 | 79 } |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
80 _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, total_workers()); |
342 | 81 if (gang_workers() == NULL) { |
82 vm_exit_out_of_memory(0, "Cannot create GangWorker array."); | |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
83 return false; |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
84 } |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
85 os::ThreadType worker_type; |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
86 if (are_ConcurrentGC_threads()) { |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
87 worker_type = os::cgc_thread; |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
88 } else { |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
89 worker_type = os::pgc_thread; |
342 | 90 } |
0 | 91 for (int worker = 0; worker < total_workers(); worker += 1) { |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
92 GangWorker* new_worker = allocate_worker(worker); |
0 | 93 assert(new_worker != NULL, "Failed to allocate GangWorker"); |
94 _gang_workers[worker] = new_worker; | |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
95 if (new_worker == NULL || !os::create_thread(new_worker, worker_type)) { |
0 | 96 vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources."); |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
97 return false; |
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
98 } |
0 | 99 if (!DisableStartThread) { |
100 os::start_thread(new_worker); | |
101 } | |
102 } | |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
103 return true; |
0 | 104 } |
105 | |
106 AbstractWorkGang::~AbstractWorkGang() { | |
107 if (TraceWorkGang) { | |
108 tty->print_cr("Destructing work gang %s", name()); | |
109 } | |
110 stop(); // stop all the workers | |
111 for (int worker = 0; worker < total_workers(); worker += 1) { | |
112 delete gang_worker(worker); | |
113 } | |
114 delete gang_workers(); | |
115 delete monitor(); | |
116 } | |
117 | |
118 GangWorker* AbstractWorkGang::gang_worker(int i) const { | |
119 // Array index bounds checking. | |
120 GangWorker* result = NULL; | |
121 assert(gang_workers() != NULL, "No workers for indexing"); | |
122 assert(((i >= 0) && (i < total_workers())), "Worker index out of bounds"); | |
123 result = _gang_workers[i]; | |
124 assert(result != NULL, "Indexing to null worker"); | |
125 return result; | |
126 } | |
127 | |
128 void WorkGang::run_task(AbstractGangTask* task) { | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
129 run_task(task, total_workers()); |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
130 } |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
131 |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
132 void WorkGang::run_task(AbstractGangTask* task, uint no_of_parallel_workers) { |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
133 task->set_for_termination(no_of_parallel_workers); |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
134 |
0 | 135 // This thread is executed by the VM thread which does not block |
136 // on ordinary MutexLocker's. | |
137 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag); | |
138 if (TraceWorkGang) { | |
139 tty->print_cr("Running work gang %s task %s", name(), task->name()); | |
140 } | |
141 // Tell all the workers to run a task. | |
142 assert(task != NULL, "Running a null task"); | |
143 // Initialize. | |
144 _task = task; | |
145 _sequence_number += 1; | |
146 _started_workers = 0; | |
147 _finished_workers = 0; | |
148 // Tell the workers to get to work. | |
149 monitor()->notify_all(); | |
150 // Wait for them to be finished | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
151 while (finished_workers() < (int) no_of_parallel_workers) { |
0 | 152 if (TraceWorkGang) { |
153 tty->print_cr("Waiting in work gang %s: %d/%d finished sequence %d", | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
154 name(), finished_workers(), no_of_parallel_workers, |
0 | 155 _sequence_number); |
156 } | |
157 monitor()->wait(/* no_safepoint_check */ true); | |
158 } | |
159 _task = NULL; | |
160 if (TraceWorkGang) { | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
161 tty->print_cr("\nFinished work gang %s: %d/%d sequence %d", |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
162 name(), finished_workers(), no_of_parallel_workers, |
0 | 163 _sequence_number); |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
164 Thread* me = Thread::current(); |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
165 tty->print_cr(" T: 0x%x VM_thread: %d", me, me->is_VM_thread()); |
2369
92da084fefc9
6668573: CMS: reference processing crash if ParallelCMSThreads > ParallelGCThreads
ysr
parents:
1972
diff
changeset
|
166 } |
0 | 167 } |
168 | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
169 void FlexibleWorkGang::run_task(AbstractGangTask* task) { |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
170 // If active_workers() is passed, _finished_workers |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
171 // must only be incremented for workers that find non_null |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
172 // work (as opposed to all those that just check that the |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
173 // task is not null). |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
174 WorkGang::run_task(task, (uint) active_workers()); |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
175 } |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
176 |
0 | 177 void AbstractWorkGang::stop() { |
178 // Tell all workers to terminate, then wait for them to become inactive. | |
179 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag); | |
180 if (TraceWorkGang) { | |
181 tty->print_cr("Stopping work gang %s task %s", name(), task()->name()); | |
182 } | |
183 _task = NULL; | |
184 _terminate = true; | |
185 monitor()->notify_all(); | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
186 while (finished_workers() < active_workers()) { |
0 | 187 if (TraceWorkGang) { |
188 tty->print_cr("Waiting in work gang %s: %d/%d finished", | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
189 name(), finished_workers(), active_workers()); |
0 | 190 } |
191 monitor()->wait(/* no_safepoint_check */ true); | |
192 } | |
193 } | |
194 | |
195 void AbstractWorkGang::internal_worker_poll(WorkData* data) const { | |
196 assert(monitor()->owned_by_self(), "worker_poll is an internal method"); | |
197 assert(data != NULL, "worker data is null"); | |
198 data->set_terminate(terminate()); | |
199 data->set_task(task()); | |
200 data->set_sequence_number(sequence_number()); | |
201 } | |
202 | |
203 void AbstractWorkGang::internal_note_start() { | |
204 assert(monitor()->owned_by_self(), "note_finish is an internal method"); | |
205 _started_workers += 1; | |
206 } | |
207 | |
208 void AbstractWorkGang::internal_note_finish() { | |
209 assert(monitor()->owned_by_self(), "note_finish is an internal method"); | |
210 _finished_workers += 1; | |
211 } | |
212 | |
213 void AbstractWorkGang::print_worker_threads_on(outputStream* st) const { | |
214 uint num_thr = total_workers(); | |
215 for (uint i = 0; i < num_thr; i++) { | |
216 gang_worker(i)->print_on(st); | |
217 st->cr(); | |
218 } | |
219 } | |
220 | |
221 void AbstractWorkGang::threads_do(ThreadClosure* tc) const { | |
222 assert(tc != NULL, "Null ThreadClosure"); | |
223 uint num_thr = total_workers(); | |
224 for (uint i = 0; i < num_thr; i++) { | |
225 tc->do_thread(gang_worker(i)); | |
226 } | |
227 } | |
228 | |
229 // GangWorker methods. | |
230 | |
231 GangWorker::GangWorker(AbstractWorkGang* gang, uint id) { | |
232 _gang = gang; | |
233 set_id(id); | |
234 set_name("Gang worker#%d (%s)", id, gang->name()); | |
235 } | |
236 | |
237 void GangWorker::run() { | |
238 initialize(); | |
239 loop(); | |
240 } | |
241 | |
242 void GangWorker::initialize() { | |
243 this->initialize_thread_local_storage(); | |
244 assert(_gang != NULL, "No gang to run in"); | |
245 os::set_priority(this, NearMaxPriority); | |
246 if (TraceWorkGang) { | |
247 tty->print_cr("Running gang worker for gang %s id %d", | |
248 gang()->name(), id()); | |
249 } | |
250 // The VM thread should not execute here because MutexLocker's are used | |
251 // as (opposed to MutexLockerEx's). | |
252 assert(!Thread::current()->is_VM_thread(), "VM thread should not be part" | |
253 " of a work gang"); | |
254 } | |
255 | |
256 void GangWorker::loop() { | |
257 int previous_sequence_number = 0; | |
258 Monitor* gang_monitor = gang()->monitor(); | |
259 for ( ; /* !terminate() */; ) { | |
260 WorkData data; | |
261 int part; // Initialized below. | |
262 { | |
263 // Grab the gang mutex. | |
264 MutexLocker ml(gang_monitor); | |
265 // Wait for something to do. | |
266 // Polling outside the while { wait } avoids missed notifies | |
267 // in the outer loop. | |
268 gang()->internal_worker_poll(&data); | |
269 if (TraceWorkGang) { | |
270 tty->print("Polled outside for work in gang %s worker %d", | |
271 gang()->name(), id()); | |
272 tty->print(" terminate: %s", | |
273 data.terminate() ? "true" : "false"); | |
274 tty->print(" sequence: %d (prev: %d)", | |
275 data.sequence_number(), previous_sequence_number); | |
276 if (data.task() != NULL) { | |
277 tty->print(" task: %s", data.task()->name()); | |
278 } else { | |
279 tty->print(" task: NULL"); | |
280 } | |
281 tty->cr(); | |
282 } | |
283 for ( ; /* break or return */; ) { | |
284 // Terminate if requested. | |
285 if (data.terminate()) { | |
286 gang()->internal_note_finish(); | |
287 gang_monitor->notify_all(); | |
288 return; | |
289 } | |
290 // Check for new work. | |
291 if ((data.task() != NULL) && | |
292 (data.sequence_number() != previous_sequence_number)) { | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
293 if (gang()->needs_more_workers()) { |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
294 gang()->internal_note_start(); |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
295 gang_monitor->notify_all(); |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
296 part = gang()->started_workers() - 1; |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
297 break; |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
298 } |
0 | 299 } |
300 // Nothing to do. | |
301 gang_monitor->wait(/* no_safepoint_check */ true); | |
302 gang()->internal_worker_poll(&data); | |
303 if (TraceWorkGang) { | |
304 tty->print("Polled inside for work in gang %s worker %d", | |
305 gang()->name(), id()); | |
306 tty->print(" terminate: %s", | |
307 data.terminate() ? "true" : "false"); | |
308 tty->print(" sequence: %d (prev: %d)", | |
309 data.sequence_number(), previous_sequence_number); | |
310 if (data.task() != NULL) { | |
311 tty->print(" task: %s", data.task()->name()); | |
312 } else { | |
313 tty->print(" task: NULL"); | |
314 } | |
315 tty->cr(); | |
316 } | |
317 } | |
318 // Drop gang mutex. | |
319 } | |
320 if (TraceWorkGang) { | |
321 tty->print("Work for work gang %s id %d task %s part %d", | |
322 gang()->name(), id(), data.task()->name(), part); | |
323 } | |
324 assert(data.task() != NULL, "Got null task"); | |
325 data.task()->work(part); | |
326 { | |
327 if (TraceWorkGang) { | |
328 tty->print("Finish for work gang %s id %d task %s part %d", | |
329 gang()->name(), id(), data.task()->name(), part); | |
330 } | |
331 // Grab the gang mutex. | |
332 MutexLocker ml(gang_monitor); | |
333 gang()->internal_note_finish(); | |
334 // Tell the gang you are done. | |
335 gang_monitor->notify_all(); | |
336 // Drop the gang mutex. | |
337 } | |
338 previous_sequence_number = data.sequence_number(); | |
339 } | |
340 } | |
341 | |
342 bool GangWorker::is_GC_task_thread() const { | |
342 | 343 return gang()->are_GC_task_threads(); |
344 } | |
345 | |
346 bool GangWorker::is_ConcurrentGC_thread() const { | |
347 return gang()->are_ConcurrentGC_threads(); | |
0 | 348 } |
349 | |
350 void GangWorker::print_on(outputStream* st) const { | |
351 st->print("\"%s\" ", name()); | |
352 Thread::print_on(st); | |
353 st->cr(); | |
354 } | |
355 | |
356 // Printing methods | |
357 | |
358 const char* AbstractWorkGang::name() const { | |
359 return _name; | |
360 } | |
361 | |
362 #ifndef PRODUCT | |
363 | |
364 const char* AbstractGangTask::name() const { | |
365 return _name; | |
366 } | |
367 | |
368 #endif /* PRODUCT */ | |
369 | |
4095
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
370 // FlexibleWorkGang |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
371 |
bca17e38de00
6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents:
2369
diff
changeset
|
372 |
0 | 373 // *** WorkGangBarrierSync |
374 | |
375 WorkGangBarrierSync::WorkGangBarrierSync() | |
376 : _monitor(Mutex::safepoint, "work gang barrier sync", true), | |
342 | 377 _n_workers(0), _n_completed(0), _should_reset(false) { |
0 | 378 } |
379 | |
380 WorkGangBarrierSync::WorkGangBarrierSync(int n_workers, const char* name) | |
381 : _monitor(Mutex::safepoint, name, true), | |
342 | 382 _n_workers(n_workers), _n_completed(0), _should_reset(false) { |
0 | 383 } |
384 | |
385 void WorkGangBarrierSync::set_n_workers(int n_workers) { | |
386 _n_workers = n_workers; | |
387 _n_completed = 0; | |
342 | 388 _should_reset = false; |
0 | 389 } |
390 | |
391 void WorkGangBarrierSync::enter() { | |
392 MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag); | |
342 | 393 if (should_reset()) { |
394 // The should_reset() was set and we are the first worker to enter | |
395 // the sync barrier. We will zero the n_completed() count which | |
396 // effectively resets the barrier. | |
397 zero_completed(); | |
398 set_should_reset(false); | |
399 } | |
0 | 400 inc_completed(); |
401 if (n_completed() == n_workers()) { | |
342 | 402 // At this point we would like to reset the barrier to be ready in |
403 // case it is used again. However, we cannot set n_completed() to | |
404 // 0, even after the notify_all(), given that some other workers | |
405 // might still be waiting for n_completed() to become == | |
406 // n_workers(). So, if we set n_completed() to 0, those workers | |
407 // will get stuck (as they will wake up, see that n_completed() != | |
408 // n_workers() and go back to sleep). Instead, we raise the | |
409 // should_reset() flag and the barrier will be reset the first | |
410 // time a worker enters it again. | |
411 set_should_reset(true); | |
0 | 412 monitor()->notify_all(); |
342 | 413 } else { |
0 | 414 while (n_completed() != n_workers()) { |
415 monitor()->wait(/* no_safepoint_check */ true); | |
416 } | |
417 } | |
418 } | |
419 | |
420 // SubTasksDone functions. | |
421 | |
422 SubTasksDone::SubTasksDone(int n) : | |
423 _n_tasks(n), _n_threads(1), _tasks(NULL) { | |
424 _tasks = NEW_C_HEAP_ARRAY(jint, n); | |
425 guarantee(_tasks != NULL, "alloc failure"); | |
426 clear(); | |
427 } | |
428 | |
429 bool SubTasksDone::valid() { | |
430 return _tasks != NULL; | |
431 } | |
432 | |
1833
8b10f48633dc
6984287: Regularize how GC parallel workers are specified.
jmasa
parents:
1552
diff
changeset
|
433 void SubTasksDone::set_n_threads(int t) { |
0 | 434 assert(_claimed == 0 || _threads_completed == _n_threads, |
435 "should not be called while tasks are being processed!"); | |
436 _n_threads = (t == 0 ? 1 : t); | |
437 } | |
438 | |
439 void SubTasksDone::clear() { | |
440 for (int i = 0; i < _n_tasks; i++) { | |
441 _tasks[i] = 0; | |
442 } | |
443 _threads_completed = 0; | |
444 #ifdef ASSERT | |
445 _claimed = 0; | |
446 #endif | |
447 } | |
448 | |
449 bool SubTasksDone::is_task_claimed(int t) { | |
450 assert(0 <= t && t < _n_tasks, "bad task id."); | |
451 jint old = _tasks[t]; | |
452 if (old == 0) { | |
453 old = Atomic::cmpxchg(1, &_tasks[t], 0); | |
454 } | |
455 assert(_tasks[t] == 1, "What else?"); | |
456 bool res = old != 0; | |
457 #ifdef ASSERT | |
458 if (!res) { | |
459 assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?"); | |
460 Atomic::inc(&_claimed); | |
461 } | |
462 #endif | |
463 return res; | |
464 } | |
465 | |
466 void SubTasksDone::all_tasks_completed() { | |
467 jint observed = _threads_completed; | |
468 jint old; | |
469 do { | |
470 old = observed; | |
471 observed = Atomic::cmpxchg(old+1, &_threads_completed, old); | |
472 } while (observed != old); | |
473 // If this was the last thread checking in, clear the tasks. | |
474 if (observed+1 == _n_threads) clear(); | |
475 } | |
476 | |
477 | |
478 SubTasksDone::~SubTasksDone() { | |
479 if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks); | |
480 } | |
481 | |
482 // *** SequentialSubTasksDone | |
483 | |
484 void SequentialSubTasksDone::clear() { | |
485 _n_tasks = _n_claimed = 0; | |
486 _n_threads = _n_completed = 0; | |
487 } | |
488 | |
489 bool SequentialSubTasksDone::valid() { | |
490 return _n_threads > 0; | |
491 } | |
492 | |
493 bool SequentialSubTasksDone::is_task_claimed(int& t) { | |
494 jint* n_claimed_ptr = &_n_claimed; | |
495 t = *n_claimed_ptr; | |
496 while (t < _n_tasks) { | |
497 jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t); | |
498 if (res == t) { | |
499 return false; | |
500 } | |
501 t = *n_claimed_ptr; | |
502 } | |
503 return true; | |
504 } | |
505 | |
506 bool SequentialSubTasksDone::all_tasks_completed() { | |
507 jint* n_completed_ptr = &_n_completed; | |
508 jint complete = *n_completed_ptr; | |
509 while (true) { | |
510 jint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete); | |
511 if (res == complete) { | |
512 break; | |
513 } | |
514 complete = res; | |
515 } | |
516 if (complete+1 == _n_threads) { | |
517 clear(); | |
518 return true; | |
519 } | |
520 return false; | |
521 } | |
342 | 522 |
523 bool FreeIdSet::_stat_init = false; | |
524 FreeIdSet* FreeIdSet::_sets[NSets]; | |
525 bool FreeIdSet::_safepoint; | |
526 | |
527 FreeIdSet::FreeIdSet(int sz, Monitor* mon) : | |
528 _sz(sz), _mon(mon), _hd(0), _waiters(0), _index(-1), _claimed(0) | |
529 { | |
530 _ids = new int[sz]; | |
531 for (int i = 0; i < sz; i++) _ids[i] = i+1; | |
532 _ids[sz-1] = end_of_list; // end of list. | |
533 if (_stat_init) { | |
534 for (int j = 0; j < NSets; j++) _sets[j] = NULL; | |
535 _stat_init = true; | |
536 } | |
537 // Add to sets. (This should happen while the system is still single-threaded.) | |
538 for (int j = 0; j < NSets; j++) { | |
539 if (_sets[j] == NULL) { | |
540 _sets[j] = this; | |
541 _index = j; | |
542 break; | |
543 } | |
544 } | |
545 guarantee(_index != -1, "Too many FreeIdSets in use!"); | |
546 } | |
547 | |
548 FreeIdSet::~FreeIdSet() { | |
549 _sets[_index] = NULL; | |
550 } | |
551 | |
552 void FreeIdSet::set_safepoint(bool b) { | |
553 _safepoint = b; | |
554 if (b) { | |
555 for (int j = 0; j < NSets; j++) { | |
556 if (_sets[j] != NULL && _sets[j]->_waiters > 0) { | |
557 Monitor* mon = _sets[j]->_mon; | |
558 mon->lock_without_safepoint_check(); | |
559 mon->notify_all(); | |
560 mon->unlock(); | |
561 } | |
562 } | |
563 } | |
564 } | |
565 | |
566 #define FID_STATS 0 | |
567 | |
568 int FreeIdSet::claim_par_id() { | |
569 #if FID_STATS | |
570 thread_t tslf = thr_self(); | |
571 tty->print("claim_par_id[%d]: sz = %d, claimed = %d\n", tslf, _sz, _claimed); | |
572 #endif | |
573 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); | |
574 while (!_safepoint && _hd == end_of_list) { | |
575 _waiters++; | |
576 #if FID_STATS | |
577 if (_waiters > 5) { | |
578 tty->print("claim_par_id waiting[%d]: %d waiters, %d claimed.\n", | |
579 tslf, _waiters, _claimed); | |
580 } | |
581 #endif | |
582 _mon->wait(Mutex::_no_safepoint_check_flag); | |
583 _waiters--; | |
584 } | |
585 if (_hd == end_of_list) { | |
586 #if FID_STATS | |
587 tty->print("claim_par_id[%d]: returning EOL.\n", tslf); | |
588 #endif | |
589 return -1; | |
590 } else { | |
591 int res = _hd; | |
592 _hd = _ids[res]; | |
593 _ids[res] = claimed; // For debugging. | |
594 _claimed++; | |
595 #if FID_STATS | |
596 tty->print("claim_par_id[%d]: returning %d, claimed = %d.\n", | |
597 tslf, res, _claimed); | |
598 #endif | |
599 return res; | |
600 } | |
601 } | |
602 | |
603 bool FreeIdSet::claim_perm_id(int i) { | |
604 assert(0 <= i && i < _sz, "Out of range."); | |
605 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); | |
606 int prev = end_of_list; | |
607 int cur = _hd; | |
608 while (cur != end_of_list) { | |
609 if (cur == i) { | |
610 if (prev == end_of_list) { | |
611 _hd = _ids[cur]; | |
612 } else { | |
613 _ids[prev] = _ids[cur]; | |
614 } | |
615 _ids[cur] = claimed; | |
616 _claimed++; | |
617 return true; | |
618 } else { | |
619 prev = cur; | |
620 cur = _ids[cur]; | |
621 } | |
622 } | |
623 return false; | |
624 | |
625 } | |
626 | |
627 void FreeIdSet::release_par_id(int id) { | |
628 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); | |
629 assert(_ids[id] == claimed, "Precondition."); | |
630 _ids[id] = _hd; | |
631 _hd = id; | |
632 _claimed--; | |
633 #if FID_STATS | |
634 tty->print("[%d] release_par_id(%d), waiters =%d, claimed = %d.\n", | |
635 thr_self(), id, _waiters, _claimed); | |
636 #endif | |
637 if (_waiters > 0) | |
638 // Notify all would be safer, but this is OK, right? | |
639 _mon->notify_all(); | |
640 } |