annotate src/share/vm/utilities/yieldingWorkgroup.cpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 37f87013dfd8
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 # include "incls/_precompiled.incl"
duke@0 26 # include "incls/_yieldingWorkgroup.cpp.incl"
duke@0 27
duke@0 28 // Forward declaration of classes declared here.
duke@0 29
duke@0 30 class GangWorker;
duke@0 31 class WorkData;
duke@0 32
duke@0 33 YieldingFlexibleWorkGang::YieldingFlexibleWorkGang(
duke@0 34 const char* name, int workers, bool are_GC_threads) :
duke@0 35 AbstractWorkGang(name, are_GC_threads) {
duke@0 36 // Save arguments.
duke@0 37 _total_workers = workers;
duke@0 38 assert(_total_workers > 0, "Must have more than 1 worker");
duke@0 39
duke@0 40 _yielded_workers = 0;
duke@0 41
duke@0 42 if (TraceWorkGang) {
duke@0 43 tty->print_cr("Constructing work gang %s with %d threads", name, workers);
duke@0 44 }
duke@0 45 _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, workers);
duke@0 46 assert(gang_workers() != NULL, "Failed to allocate gang workers");
duke@0 47 for (int worker = 0; worker < total_workers(); worker += 1) {
duke@0 48 YieldingFlexibleGangWorker* new_worker =
duke@0 49 new YieldingFlexibleGangWorker(this, worker);
duke@0 50 assert(new_worker != NULL, "Failed to allocate YieldingFlexibleGangWorker");
duke@0 51 _gang_workers[worker] = new_worker;
duke@0 52 if (new_worker == NULL || !os::create_thread(new_worker, os::pgc_thread))
duke@0 53 vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources.");
duke@0 54 if (!DisableStartThread) {
duke@0 55 os::start_thread(new_worker);
duke@0 56 }
duke@0 57 }
duke@0 58 }
duke@0 59
duke@0 60 // Run a task; returns when the task is done, or the workers yield,
duke@0 61 // or the task is aborted, or the work gang is terminated via stop().
duke@0 62 // A task that has been yielded can be continued via this interface
duke@0 63 // by using the same task repeatedly as the argument to the call.
duke@0 64 // It is expected that the YieldingFlexibleGangTask carries the appropriate
duke@0 65 // continuation information used by workers to continue the task
duke@0 66 // from its last yield point. Thus, a completed task will return
duke@0 67 // immediately with no actual work having been done by the workers.
duke@0 68 /////////////////////
duke@0 69 // Implementatiuon notes: remove before checking XXX
duke@0 70 /*
duke@0 71 Each gang is working on a task at a certain time.
duke@0 72 Some subset of workers may have yielded and some may
duke@0 73 have finished their quota of work. Until this task has
duke@0 74 been completed, the workers are bound to that task.
duke@0 75 Once the task has been completed, the gang unbounds
duke@0 76 itself from the task.
duke@0 77
duke@0 78 The yielding work gang thus exports two invokation
duke@0 79 interfaces: run_task() and continue_task(). The
duke@0 80 first is used to initiate a new task and bind it
duke@0 81 to the workers; the second is used to continue an
duke@0 82 already bound task that has yielded. Upon completion
duke@0 83 the binding is released and a new binding may be
duke@0 84 created.
duke@0 85
duke@0 86 The shape of a yielding work gang is as follows:
duke@0 87
duke@0 88 Overseer invokes run_task(*task).
duke@0 89 Lock gang monitor
duke@0 90 Check that there is no existing binding for the gang
duke@0 91 If so, abort with an error
duke@0 92 Else, create a new binding of this gang to the given task
duke@0 93 Set number of active workers (as asked)
duke@0 94 Notify workers that work is ready to be done
duke@0 95 [the requisite # workers would then start up
duke@0 96 and do the task]
duke@0 97 Wait on the monitor until either
duke@0 98 all work is completed or the task has yielded
duke@0 99 -- this is normally done through
duke@0 100 yielded + completed == active
duke@0 101 [completed workers are rest to idle state by overseer?]
duke@0 102 return appropriate status to caller
duke@0 103
duke@0 104 Overseer invokes continue_task(*task),
duke@0 105 Lock gang monitor
duke@0 106 Check that task is the same as current binding
duke@0 107 If not, abort with an error
duke@0 108 Else, set the number of active workers as requested?
duke@0 109 Notify workers that they can continue from yield points
duke@0 110 New workers can also start up as required
duke@0 111 while satisfying the constraint that
duke@0 112 active + yielded does not exceed required number
duke@0 113 Wait (as above).
duke@0 114
duke@0 115 NOTE: In the above, for simplicity in a first iteration
duke@0 116 our gangs will be of fixed population and will not
duke@0 117 therefore be flexible work gangs, just yielding work
duke@0 118 gangs. Once this works well, we will in a second
duke@0 119 iteration.refinement introduce flexibility into
duke@0 120 the work gang.
duke@0 121
duke@0 122 NOTE: we can always create a new gang per each iteration
duke@0 123 in order to get the flexibility, but we will for now
duke@0 124 desist that simplified route.
duke@0 125
duke@0 126 */
duke@0 127 /////////////////////
duke@0 128 void YieldingFlexibleWorkGang::start_task(YieldingFlexibleGangTask* new_task) {
duke@0 129 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
duke@0 130 assert(task() == NULL, "Gang currently tied to a task");
duke@0 131 assert(new_task != NULL, "Null task");
duke@0 132 // Bind task to gang
duke@0 133 _task = new_task;
duke@0 134 new_task->set_gang(this); // Establish 2-way binding to support yielding
duke@0 135 _sequence_number++;
duke@0 136
duke@0 137 int requested_size = new_task->requested_size();
duke@0 138 assert(requested_size >= 0, "Should be non-negative");
duke@0 139 if (requested_size != 0) {
duke@0 140 _active_workers = MIN2(requested_size, total_workers());
duke@0 141 } else {
duke@0 142 _active_workers = total_workers();
duke@0 143 }
duke@0 144 new_task->set_actual_size(_active_workers);
duke@0 145
duke@0 146 assert(_started_workers == 0, "Tabula rasa non");
duke@0 147 assert(_finished_workers == 0, "Tabula rasa non");
duke@0 148 assert(_yielded_workers == 0, "Tabula rasa non");
duke@0 149 yielding_task()->set_status(ACTIVE);
duke@0 150
duke@0 151 // Wake up all the workers, the first few will get to work,
duke@0 152 // and the rest will go back to sleep
duke@0 153 monitor()->notify_all();
duke@0 154 wait_for_gang();
duke@0 155 }
duke@0 156
duke@0 157 void YieldingFlexibleWorkGang::wait_for_gang() {
duke@0 158
duke@0 159 assert(monitor()->owned_by_self(), "Data race");
duke@0 160 // Wait for task to complete or yield
duke@0 161 for (Status status = yielding_task()->status();
duke@0 162 status != COMPLETED && status != YIELDED && status != ABORTED;
duke@0 163 status = yielding_task()->status()) {
duke@0 164 assert(started_workers() <= active_workers(), "invariant");
duke@0 165 assert(finished_workers() <= active_workers(), "invariant");
duke@0 166 assert(yielded_workers() <= active_workers(), "invariant");
duke@0 167 monitor()->wait(Mutex::_no_safepoint_check_flag);
duke@0 168 }
duke@0 169 switch (yielding_task()->status()) {
duke@0 170 case COMPLETED:
duke@0 171 case ABORTED: {
duke@0 172 assert(finished_workers() == active_workers(), "Inconsistent status");
duke@0 173 assert(yielded_workers() == 0, "Invariant");
duke@0 174 reset(); // for next task; gang<->task binding released
duke@0 175 break;
duke@0 176 }
duke@0 177 case YIELDED: {
duke@0 178 assert(yielded_workers() > 0, "Invariant");
duke@0 179 assert(yielded_workers() + finished_workers() == active_workers(),
duke@0 180 "Inconsistent counts");
duke@0 181 break;
duke@0 182 }
duke@0 183 case ACTIVE:
duke@0 184 case INACTIVE:
duke@0 185 case COMPLETING:
duke@0 186 case YIELDING:
duke@0 187 case ABORTING:
duke@0 188 default:
duke@0 189 ShouldNotReachHere();
duke@0 190 }
duke@0 191 }
duke@0 192
duke@0 193 void YieldingFlexibleWorkGang::continue_task(
duke@0 194 YieldingFlexibleGangTask* gang_task) {
duke@0 195
duke@0 196 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
duke@0 197 assert(task() != NULL && task() == gang_task, "Incorrect usage");
duke@0 198 // assert(_active_workers == total_workers(), "For now");
duke@0 199 assert(_started_workers == _active_workers, "Precondition");
duke@0 200 assert(_yielded_workers > 0 && yielding_task()->status() == YIELDED,
duke@0 201 "Else why are we calling continue_task()");
duke@0 202 // Restart the yielded gang workers
duke@0 203 yielding_task()->set_status(ACTIVE);
duke@0 204 monitor()->notify_all();
duke@0 205 wait_for_gang();
duke@0 206 }
duke@0 207
duke@0 208 void YieldingFlexibleWorkGang::reset() {
duke@0 209 _started_workers = 0;
duke@0 210 _finished_workers = 0;
duke@0 211 _active_workers = 0;
duke@0 212 yielding_task()->set_gang(NULL);
duke@0 213 _task = NULL; // unbind gang from task
duke@0 214 }
duke@0 215
duke@0 216 void YieldingFlexibleWorkGang::yield() {
duke@0 217 assert(task() != NULL, "Inconsistency; should have task binding");
duke@0 218 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
duke@0 219 assert(yielded_workers() < active_workers(), "Consistency check");
duke@0 220 if (yielding_task()->status() == ABORTING) {
duke@0 221 // Do not yield; we need to abort as soon as possible
duke@0 222 // XXX NOTE: This can cause a performance pathology in the
duke@0 223 // current implementation in Mustang, as of today, and
duke@0 224 // pre-Mustang in that as soon as an overflow occurs,
duke@0 225 // yields will not be honoured. The right way to proceed
duke@0 226 // of course is to fix bug # TBF, so that abort's cause
duke@0 227 // us to return at each potential yield point.
duke@0 228 return;
duke@0 229 }
duke@0 230 if (++_yielded_workers + finished_workers() == active_workers()) {
duke@0 231 yielding_task()->set_status(YIELDED);
duke@0 232 monitor()->notify_all();
duke@0 233 } else {
duke@0 234 yielding_task()->set_status(YIELDING);
duke@0 235 }
duke@0 236
duke@0 237 while (true) {
duke@0 238 switch (yielding_task()->status()) {
duke@0 239 case YIELDING:
duke@0 240 case YIELDED: {
duke@0 241 monitor()->wait(Mutex::_no_safepoint_check_flag);
duke@0 242 break; // from switch
duke@0 243 }
duke@0 244 case ACTIVE:
duke@0 245 case ABORTING:
duke@0 246 case COMPLETING: {
duke@0 247 assert(_yielded_workers > 0, "Else why am i here?");
duke@0 248 _yielded_workers--;
duke@0 249 return;
duke@0 250 }
duke@0 251 case INACTIVE:
duke@0 252 case ABORTED:
duke@0 253 case COMPLETED:
duke@0 254 default: {
duke@0 255 ShouldNotReachHere();
duke@0 256 }
duke@0 257 }
duke@0 258 }
duke@0 259 // Only return is from inside switch statement above
duke@0 260 ShouldNotReachHere();
duke@0 261 }
duke@0 262
duke@0 263 void YieldingFlexibleWorkGang::abort() {
duke@0 264 assert(task() != NULL, "Inconsistency; should have task binding");
duke@0 265 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
duke@0 266 assert(yielded_workers() < active_workers(), "Consistency check");
duke@0 267 #ifndef PRODUCT
duke@0 268 switch (yielding_task()->status()) {
duke@0 269 // allowed states
duke@0 270 case ACTIVE:
duke@0 271 case ABORTING:
duke@0 272 case COMPLETING:
duke@0 273 case YIELDING:
duke@0 274 break;
duke@0 275 // not allowed states
duke@0 276 case INACTIVE:
duke@0 277 case ABORTED:
duke@0 278 case COMPLETED:
duke@0 279 case YIELDED:
duke@0 280 default:
duke@0 281 ShouldNotReachHere();
duke@0 282 }
duke@0 283 #endif // !PRODUCT
duke@0 284 Status prev_status = yielding_task()->status();
duke@0 285 yielding_task()->set_status(ABORTING);
duke@0 286 if (prev_status == YIELDING) {
duke@0 287 assert(yielded_workers() > 0, "Inconsistency");
duke@0 288 // At least one thread has yielded, wake it up
duke@0 289 // so it can go back to waiting stations ASAP.
duke@0 290 monitor()->notify_all();
duke@0 291 }
duke@0 292 }
duke@0 293
duke@0 294 ///////////////////////////////
duke@0 295 // YieldingFlexibleGangTask
duke@0 296 ///////////////////////////////
duke@0 297 void YieldingFlexibleGangTask::yield() {
duke@0 298 assert(gang() != NULL, "No gang to signal");
duke@0 299 gang()->yield();
duke@0 300 }
duke@0 301
duke@0 302 void YieldingFlexibleGangTask::abort() {
duke@0 303 assert(gang() != NULL, "No gang to signal");
duke@0 304 gang()->abort();
duke@0 305 }
duke@0 306
duke@0 307 ///////////////////////////////
duke@0 308 // YieldingFlexibleGangWorker
duke@0 309 ///////////////////////////////
duke@0 310 void YieldingFlexibleGangWorker::loop() {
duke@0 311 int previous_sequence_number = 0;
duke@0 312 Monitor* gang_monitor = gang()->monitor();
duke@0 313 MutexLockerEx ml(gang_monitor, Mutex::_no_safepoint_check_flag);
duke@0 314 WorkData data;
duke@0 315 int id;
duke@0 316 while (true) {
duke@0 317 // Check if there is work to do or if we have been asked
duke@0 318 // to terminate
duke@0 319 gang()->internal_worker_poll(&data);
duke@0 320 if (data.terminate()) {
duke@0 321 // We have been asked to terminate.
duke@0 322 assert(gang()->task() == NULL, "No task binding");
duke@0 323 // set_status(TERMINATED);
duke@0 324 return;
duke@0 325 } else if (data.task() != NULL &&
duke@0 326 data.sequence_number() != previous_sequence_number) {
duke@0 327 // There is work to be done.
duke@0 328 // First check if we need to become active or if there
duke@0 329 // are already the requisite number of workers
duke@0 330 if (gang()->started_workers() == yf_gang()->active_workers()) {
duke@0 331 // There are already enough workers, we do not need to
duke@0 332 // to run; fall through and wait on monitor.
duke@0 333 } else {
duke@0 334 // We need to pitch in and do the work.
duke@0 335 assert(gang()->started_workers() < yf_gang()->active_workers(),
duke@0 336 "Unexpected state");
duke@0 337 id = gang()->started_workers();
duke@0 338 gang()->internal_note_start();
duke@0 339 // Now, release the gang mutex and do the work.
duke@0 340 {
duke@0 341 MutexUnlockerEx mul(gang_monitor, Mutex::_no_safepoint_check_flag);
duke@0 342 data.task()->work(id); // This might include yielding
duke@0 343 }
duke@0 344 // Reacquire monitor and note completion of this worker
duke@0 345 gang()->internal_note_finish();
duke@0 346 // Update status of task based on whether all workers have
duke@0 347 // finished or some have yielded
duke@0 348 assert(data.task() == gang()->task(), "Confused task binding");
duke@0 349 if (gang()->finished_workers() == yf_gang()->active_workers()) {
duke@0 350 switch (data.yf_task()->status()) {
duke@0 351 case ABORTING: {
duke@0 352 data.yf_task()->set_status(ABORTED);
duke@0 353 break;
duke@0 354 }
duke@0 355 case ACTIVE:
duke@0 356 case COMPLETING: {
duke@0 357 data.yf_task()->set_status(COMPLETED);
duke@0 358 break;
duke@0 359 }
duke@0 360 default:
duke@0 361 ShouldNotReachHere();
duke@0 362 }
duke@0 363 gang_monitor->notify_all(); // Notify overseer
duke@0 364 } else { // at least one worker is still working or yielded
duke@0 365 assert(gang()->finished_workers() < yf_gang()->active_workers(),
duke@0 366 "Counts inconsistent");
duke@0 367 switch (data.yf_task()->status()) {
duke@0 368 case ACTIVE: {
duke@0 369 // first, but not only thread to complete
duke@0 370 data.yf_task()->set_status(COMPLETING);
duke@0 371 break;
duke@0 372 }
duke@0 373 case YIELDING: {
duke@0 374 if (gang()->finished_workers() + yf_gang()->yielded_workers()
duke@0 375 == yf_gang()->active_workers()) {
duke@0 376 data.yf_task()->set_status(YIELDED);
duke@0 377 gang_monitor->notify_all(); // notify overseer
duke@0 378 }
duke@0 379 break;
duke@0 380 }
duke@0 381 case ABORTING:
duke@0 382 case COMPLETING: {
duke@0 383 break; // nothing to do
duke@0 384 }
duke@0 385 default: // everything else: INACTIVE, YIELDED, ABORTED, COMPLETED
duke@0 386 ShouldNotReachHere();
duke@0 387 }
duke@0 388 }
duke@0 389 }
duke@0 390 }
duke@0 391 // Remember the sequence number
duke@0 392 previous_sequence_number = data.sequence_number();
duke@0 393 // Wait for more work
duke@0 394 gang_monitor->wait(Mutex::_no_safepoint_check_flag);
duke@0 395 }
duke@0 396 }