taskqueue.cpp revision 1753
0N/A/*
1558N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. 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 *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A# include "incls/_precompiled.incl"
0N/A# include "incls/_taskqueue.cpp.incl"
0N/A
546N/A#ifdef TRACESPINNING
546N/Auint ParallelTaskTerminator::_total_yields = 0;
546N/Auint ParallelTaskTerminator::_total_spins = 0;
546N/Auint ParallelTaskTerminator::_total_peeks = 0;
546N/A#endif
546N/A
1585N/A#if TASKQUEUE_STATS
1585N/Aconst char * const TaskQueueStats::_names[last_stat_id] = {
1585N/A "qpush", "qpop", "qpop-s", "qattempt", "qsteal", "opush", "omax"
1585N/A};
1585N/A
1629N/ATaskQueueStats & TaskQueueStats::operator +=(const TaskQueueStats & addend)
1629N/A{
1629N/A for (unsigned int i = 0; i < last_stat_id; ++i) {
1629N/A _stats[i] += addend._stats[i];
1629N/A }
1629N/A return *this;
1629N/A}
1629N/A
1585N/Avoid TaskQueueStats::print_header(unsigned int line, outputStream* const stream,
1585N/A unsigned int width)
1585N/A{
1585N/A // Use a width w: 1 <= w <= max_width
1585N/A const unsigned int max_width = 40;
1585N/A const unsigned int w = MAX2(MIN2(width, max_width), 1U);
1585N/A
1585N/A if (line == 0) { // spaces equal in width to the header
1585N/A const unsigned int hdr_width = w * last_stat_id + last_stat_id - 1;
1585N/A stream->print("%*s", hdr_width, " ");
1585N/A } else if (line == 1) { // labels
1585N/A stream->print("%*s", w, _names[0]);
1585N/A for (unsigned int i = 1; i < last_stat_id; ++i) {
1585N/A stream->print(" %*s", w, _names[i]);
1585N/A }
1585N/A } else if (line == 2) { // dashed lines
1585N/A char dashes[max_width + 1];
1585N/A memset(dashes, '-', w);
1585N/A dashes[w] = '\0';
1585N/A stream->print("%s", dashes);
1585N/A for (unsigned int i = 1; i < last_stat_id; ++i) {
1585N/A stream->print(" %s", dashes);
1585N/A }
1585N/A }
1585N/A}
1585N/A
1585N/Avoid TaskQueueStats::print(outputStream* stream, unsigned int width) const
1585N/A{
1585N/A #define FMT SIZE_FORMAT_W(*)
1585N/A stream->print(FMT, width, _stats[0]);
1585N/A for (unsigned int i = 1; i < last_stat_id; ++i) {
1585N/A stream->print(" " FMT, width, _stats[i]);
1585N/A }
1585N/A #undef FMT
1585N/A}
1629N/A
1629N/A#ifdef ASSERT
1629N/A// Invariants which should hold after a TaskQueue has been emptied and is
1629N/A// quiescent; they do not hold at arbitrary times.
1629N/Avoid TaskQueueStats::verify() const
1629N/A{
1629N/A assert(get(push) == get(pop) + get(steal),
1629N/A err_msg("push=" SIZE_FORMAT " pop=" SIZE_FORMAT " steal=" SIZE_FORMAT,
1629N/A get(push), get(pop), get(steal)));
1629N/A assert(get(pop_slow) <= get(pop),
1629N/A err_msg("pop_slow=" SIZE_FORMAT " pop=" SIZE_FORMAT,
1629N/A get(pop_slow), get(pop)));
1629N/A assert(get(steal) <= get(steal_attempt),
1629N/A err_msg("steal=" SIZE_FORMAT " steal_attempt=" SIZE_FORMAT,
1629N/A get(steal), get(steal_attempt)));
1629N/A assert(get(overflow) == 0 || get(push) != 0,
1629N/A err_msg("overflow=" SIZE_FORMAT " push=" SIZE_FORMAT,
1629N/A get(overflow), get(push)));
1629N/A assert(get(overflow_max_len) == 0 || get(overflow) != 0,
1629N/A err_msg("overflow_max_len=" SIZE_FORMAT " overflow=" SIZE_FORMAT,
1629N/A get(overflow_max_len), get(overflow)));
1629N/A}
1629N/A#endif // ASSERT
1585N/A#endif // TASKQUEUE_STATS
1585N/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
0N/Abool ParallelTaskTerminator::peek_in_queue_set() {
0N/A return _queue_set->peek();
0N/A}
0N/A
0N/Avoid ParallelTaskTerminator::yield() {
845N/A assert(_offered_termination <= _n_threads, "Invariant");
0N/A os::yield();
0N/A}
0N/A
0N/Avoid ParallelTaskTerminator::sleep(uint millis) {
845N/A assert(_offered_termination <= _n_threads, "Invariant");
0N/A os::sleep(Thread::current(), millis, false);
0N/A}
0N/A
342N/Abool
342N/AParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
1753N/A assert(_n_threads > 0, "Initialization is incorrect");
845N/A assert(_offered_termination < _n_threads, "Invariant");
0N/A Atomic::inc(&_offered_termination);
0N/A
541N/A uint yield_count = 0;
546N/A // Number of hard spin loops done since last yield
546N/A uint hard_spin_count = 0;
546N/A // Number of iterations in the hard spin loop.
546N/A uint hard_spin_limit = WorkStealingHardSpins;
546N/A
546N/A // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done.
546N/A // If it is greater than 0, then start with a small number
546N/A // of spins and increase number with each turn at spinning until
546N/A // the count of hard spins exceeds WorkStealingSpinToYieldRatio.
546N/A // Then do a yield() call and start spinning afresh.
546N/A if (WorkStealingSpinToYieldRatio > 0) {
546N/A hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio;
546N/A hard_spin_limit = MAX2(hard_spin_limit, 1U);
546N/A }
546N/A // Remember the initial spin limit.
546N/A uint hard_spin_start = hard_spin_limit;
546N/A
546N/A // Loop waiting for all threads to offer termination or
546N/A // more work.
0N/A while (true) {
845N/A assert(_offered_termination <= _n_threads, "Invariant");
546N/A // Are all threads offering termination?
0N/A if (_offered_termination == _n_threads) {
0N/A return true;
0N/A } else {
546N/A // Look for more work.
546N/A // Periodically sleep() instead of yield() to give threads
546N/A // waiting on the cores the chance to grab this code
0N/A if (yield_count <= WorkStealingYieldsBeforeSleep) {
546N/A // Do a yield or hardspin. For purposes of deciding whether
546N/A // to sleep, count this as a yield.
0N/A yield_count++;
546N/A
546N/A // Periodically call yield() instead spinning
546N/A // After WorkStealingSpinToYieldRatio spins, do a yield() call
546N/A // and reset the counts and starting limit.
546N/A if (hard_spin_count > WorkStealingSpinToYieldRatio) {
546N/A yield();
546N/A hard_spin_count = 0;
546N/A hard_spin_limit = hard_spin_start;
546N/A#ifdef TRACESPINNING
546N/A _total_yields++;
546N/A#endif
546N/A } else {
546N/A // Hard spin this time
546N/A // Increase the hard spinning period but only up to a limit.
546N/A hard_spin_limit = MIN2(2*hard_spin_limit,
546N/A (uint) WorkStealingHardSpins);
546N/A for (uint j = 0; j < hard_spin_limit; j++) {
546N/A SpinPause();
546N/A }
546N/A hard_spin_count++;
546N/A#ifdef TRACESPINNING
546N/A _total_spins++;
546N/A#endif
546N/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
546N/A#ifdef TRACESPINNING
546N/A _total_peeks++;
546N/A#endif
342N/A if (peek_in_queue_set() ||
342N/A (terminator != NULL && terminator->should_exit_termination())) {
0N/A Atomic::dec(&_offered_termination);
845N/A assert(_offered_termination < _n_threads, "Invariant");
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
546N/A#ifdef TRACESPINNING
546N/Avoid ParallelTaskTerminator::print_termination_counts() {
546N/A gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %lld "
546N/A "Total spins: %lld Total peeks: %lld",
546N/A total_yields(),
546N/A total_spins(),
546N/A total_peeks());
546N/A}
546N/A#endif
546N/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
1311N/A#ifdef ASSERT
1311N/Abool ObjArrayTask::is_valid() const {
1311N/A return _obj != NULL && _obj->is_objArray() && _index > 0 &&
1311N/A _index < objArrayOop(_obj)->length();
1311N/A}
1311N/A#endif // ASSERT
1753N/A
1753N/Avoid ParallelTaskTerminator::reset_for_reuse(int n_threads) {
1753N/A reset_for_reuse();
1753N/A _n_threads = n_threads;
1753N/A}
1753N/A