taskqueue.cpp revision 546
0N/A/*
0N/A * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A# include "incls/_precompiled.incl"
0N/A# include "incls/_taskqueue.cpp.incl"
0N/A
0N/A#ifdef TRACESPINNING
0N/Auint ParallelTaskTerminator::_total_yields = 0;
0N/Auint ParallelTaskTerminator::_total_spins = 0;
0N/Auint ParallelTaskTerminator::_total_peeks = 0;
0N/A#endif
0N/A
0N/Abool TaskQueueSuper::peek() {
0N/A return _bottom != _age.top();
0N/A}
0N/A
0N/Aint TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
0N/A const int a = 16807;
0N/A const int m = 2147483647;
0N/A const int q = 127773; /* m div a */
0N/A const int r = 2836; /* m mod a */
0N/A assert(sizeof(int) == 4, "I think this relies on that");
0N/A int seed = *seed0;
0N/A int hi = seed / q;
0N/A int lo = seed % q;
0N/A int test = a * lo - r * hi;
0N/A if (test > 0)
0N/A seed = test;
0N/A else
0N/A seed = test + m;
0N/A *seed0 = seed;
0N/A return seed;
0N/A}
0N/A
0N/AParallelTaskTerminator::
0N/AParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
0N/A _n_threads(n_threads),
0N/A _queue_set(queue_set),
0N/A _offered_termination(0) {}
0N/A
100N/Abool ParallelTaskTerminator::peek_in_queue_set() {
0N/A return _queue_set->peek();
0N/A}
0N/A
0N/Avoid ParallelTaskTerminator::yield() {
0N/A os::yield();
0N/A}
0N/A
0N/Avoid ParallelTaskTerminator::sleep(uint millis) {
0N/A os::sleep(Thread::current(), millis, false);
0N/A}
0N/A
0N/Abool
0N/AParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
0N/A Atomic::inc(&_offered_termination);
0N/A
0N/A uint yield_count = 0;
0N/A // Number of hard spin loops done since last yield
0N/A uint hard_spin_count = 0;
0N/A // Number of iterations in the hard spin loop.
100N/A uint hard_spin_limit = WorkStealingHardSpins;
0N/A
0N/A // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done.
0N/A // If it is greater than 0, then start with a small number
0N/A // of spins and increase number with each turn at spinning until
0N/A // the count of hard spins exceeds WorkStealingSpinToYieldRatio.
0N/A // Then do a yield() call and start spinning afresh.
0N/A if (WorkStealingSpinToYieldRatio > 0) {
0N/A hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio;
0N/A hard_spin_limit = MAX2(hard_spin_limit, 1U);
0N/A }
0N/A // Remember the initial spin limit.
0N/A uint hard_spin_start = hard_spin_limit;
0N/A
0N/A // Loop waiting for all threads to offer termination or
0N/A // more work.
0N/A while (true) {
0N/A // Are all threads offering termination?
0N/A if (_offered_termination == _n_threads) {
0N/A return true;
0N/A } else {
0N/A // Look for more work.
0N/A // Periodically sleep() instead of yield() to give threads
0N/A // waiting on the cores the chance to grab this code
0N/A if (yield_count <= WorkStealingYieldsBeforeSleep) {
0N/A // Do a yield or hardspin. For purposes of deciding whether
0N/A // to sleep, count this as a yield.
0N/A yield_count++;
0N/A
0N/A // Periodically call yield() instead spinning
0N/A // After WorkStealingSpinToYieldRatio spins, do a yield() call
0N/A // and reset the counts and starting limit.
0N/A if (hard_spin_count > WorkStealingSpinToYieldRatio) {
0N/A yield();
0N/A hard_spin_count = 0;
0N/A hard_spin_limit = hard_spin_start;
0N/A#ifdef TRACESPINNING
0N/A _total_yields++;
0N/A#endif
0N/A } else {
0N/A // Hard spin this time
0N/A // Increase the hard spinning period but only up to a limit.
0N/A hard_spin_limit = MIN2(2*hard_spin_limit,
0N/A (uint) WorkStealingHardSpins);
0N/A for (uint j = 0; j < hard_spin_limit; j++) {
0N/A SpinPause();
0N/A }
0N/A hard_spin_count++;
0N/A#ifdef TRACESPINNING
0N/A _total_spins++;
0N/A#endif
0N/A }
0N/A } else {
0N/A if (PrintGCDetails && Verbose) {
0N/A gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
0N/A "thread %d sleeps after %d yields",
0N/A Thread::current(), yield_count);
0N/A }
0N/A yield_count = 0;
0N/A // A sleep will cause this processor to seek work on another processor's
0N/A // runqueue, if it has nothing else to run (as opposed to the yield
0N/A // which may only move the thread to the end of the this processor's
0N/A // runqueue).
0N/A sleep(WorkStealingSleepMillis);
0N/A }
0N/A
0N/A#ifdef TRACESPINNING
0N/A _total_peeks++;
0N/A#endif
0N/A if (peek_in_queue_set() ||
0N/A (terminator != NULL && terminator->should_exit_termination())) {
0N/A Atomic::dec(&_offered_termination);
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A#ifdef TRACESPINNING
0N/Avoid ParallelTaskTerminator::print_termination_counts() {
0N/A gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %lld "
0N/A "Total spins: %lld Total peeks: %lld",
0N/A total_yields(),
0N/A total_spins(),
0N/A total_peeks());
0N/A}
0N/A#endif
0N/A
0N/Avoid ParallelTaskTerminator::reset_for_reuse() {
0N/A if (_offered_termination != 0) {
0N/A assert(_offered_termination == _n_threads,
0N/A "Terminator may still be in use");
0N/A _offered_termination = 0;
0N/A }
0N/A}
0N/A
0N/Abool RegionTaskQueueWithOverflow::is_empty() {
0N/A return (_region_queue.size() == 0) &&
0N/A (_overflow_stack->length() == 0);
0N/A}
0N/A
0N/Abool RegionTaskQueueWithOverflow::stealable_is_empty() {
0N/A return _region_queue.size() == 0;
0N/A}
0N/A
0N/Abool RegionTaskQueueWithOverflow::overflow_is_empty() {
0N/A return _overflow_stack->length() == 0;
0N/A}
0N/A
0N/Avoid RegionTaskQueueWithOverflow::initialize() {
0N/A _region_queue.initialize();
0N/A assert(_overflow_stack == 0, "Creating memory leak");
0N/A _overflow_stack =
0N/A new (ResourceObj::C_HEAP) GrowableArray<RegionTask>(10, true);
0N/A}
0N/A
0N/Avoid RegionTaskQueueWithOverflow::save(RegionTask t) {
0N/A if (TraceRegionTasksQueuing && Verbose) {
0N/A gclog_or_tty->print_cr("CTQ: save " PTR_FORMAT, t);
0N/A }
0N/A if(!_region_queue.push(t)) {
0N/A _overflow_stack->push(t);
0N/A }
0N/A}
0N/A
0N/A// Note that using this method will retrieve all regions
0N/A// that have been saved but that it will always check
222N/A// the overflow stack. It may be more efficient to
0N/A// check the stealable queue and the overflow stack
0N/A// separately.
0N/Abool RegionTaskQueueWithOverflow::retrieve(RegionTask& region_task) {
222N/A bool result = retrieve_from_overflow(region_task);
222N/A if (!result) {
0N/A result = retrieve_from_stealable_queue(region_task);
0N/A }
0N/A if (TraceRegionTasksQueuing && Verbose && result) {
0N/A gclog_or_tty->print_cr(" CTQ: retrieve " PTR_FORMAT, result);
0N/A }
0N/A return result;
0N/A}
0N/A
0N/Abool RegionTaskQueueWithOverflow::retrieve_from_stealable_queue(
0N/A RegionTask& region_task) {
0N/A bool result = _region_queue.pop_local(region_task);
0N/A if (TraceRegionTasksQueuing && Verbose) {
0N/A gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task);
0N/A }
0N/A return result;
0N/A}
0N/A
0N/Abool
0N/ARegionTaskQueueWithOverflow::retrieve_from_overflow(RegionTask& region_task) {
0N/A bool result;
0N/A if (!_overflow_stack->is_empty()) {
0N/A region_task = _overflow_stack->pop();
0N/A result = true;
0N/A } else {
0N/A region_task = (RegionTask) NULL;
0N/A result = false;
0N/A }
0N/A if (TraceRegionTasksQueuing && Verbose) {
0N/A gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task);
}
return result;
}