Mon, 09 Aug 2010 17:51:56 -0700
Merge
duke@435 | 1 | /* |
jcoomes@1993 | 2 | * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | # include "incls/_precompiled.incl" |
duke@435 | 26 | # include "incls/_taskqueue.cpp.incl" |
duke@435 | 27 | |
jmasa@981 | 28 | #ifdef TRACESPINNING |
jmasa@981 | 29 | uint ParallelTaskTerminator::_total_yields = 0; |
jmasa@981 | 30 | uint ParallelTaskTerminator::_total_spins = 0; |
jmasa@981 | 31 | uint ParallelTaskTerminator::_total_peeks = 0; |
jmasa@981 | 32 | #endif |
jmasa@981 | 33 | |
jcoomes@2020 | 34 | #if TASKQUEUE_STATS |
jcoomes@2020 | 35 | const char * const TaskQueueStats::_names[last_stat_id] = { |
jcoomes@2020 | 36 | "qpush", "qpop", "qpop-s", "qattempt", "qsteal", "opush", "omax" |
jcoomes@2020 | 37 | }; |
jcoomes@2020 | 38 | |
jcoomes@2020 | 39 | void TaskQueueStats::print_header(unsigned int line, outputStream* const stream, |
jcoomes@2020 | 40 | unsigned int width) |
jcoomes@2020 | 41 | { |
jcoomes@2020 | 42 | // Use a width w: 1 <= w <= max_width |
jcoomes@2020 | 43 | const unsigned int max_width = 40; |
jcoomes@2020 | 44 | const unsigned int w = MAX2(MIN2(width, max_width), 1U); |
jcoomes@2020 | 45 | |
jcoomes@2020 | 46 | if (line == 0) { // spaces equal in width to the header |
jcoomes@2020 | 47 | const unsigned int hdr_width = w * last_stat_id + last_stat_id - 1; |
jcoomes@2020 | 48 | stream->print("%*s", hdr_width, " "); |
jcoomes@2020 | 49 | } else if (line == 1) { // labels |
jcoomes@2020 | 50 | stream->print("%*s", w, _names[0]); |
jcoomes@2020 | 51 | for (unsigned int i = 1; i < last_stat_id; ++i) { |
jcoomes@2020 | 52 | stream->print(" %*s", w, _names[i]); |
jcoomes@2020 | 53 | } |
jcoomes@2020 | 54 | } else if (line == 2) { // dashed lines |
jcoomes@2020 | 55 | char dashes[max_width + 1]; |
jcoomes@2020 | 56 | memset(dashes, '-', w); |
jcoomes@2020 | 57 | dashes[w] = '\0'; |
jcoomes@2020 | 58 | stream->print("%s", dashes); |
jcoomes@2020 | 59 | for (unsigned int i = 1; i < last_stat_id; ++i) { |
jcoomes@2020 | 60 | stream->print(" %s", dashes); |
jcoomes@2020 | 61 | } |
jcoomes@2020 | 62 | } |
jcoomes@2020 | 63 | } |
jcoomes@2020 | 64 | |
jcoomes@2020 | 65 | void TaskQueueStats::print(outputStream* stream, unsigned int width) const |
jcoomes@2020 | 66 | { |
jcoomes@2020 | 67 | #define FMT SIZE_FORMAT_W(*) |
jcoomes@2020 | 68 | stream->print(FMT, width, _stats[0]); |
jcoomes@2020 | 69 | for (unsigned int i = 1; i < last_stat_id; ++i) { |
jcoomes@2020 | 70 | stream->print(" " FMT, width, _stats[i]); |
jcoomes@2020 | 71 | } |
jcoomes@2020 | 72 | #undef FMT |
jcoomes@2020 | 73 | } |
jcoomes@2020 | 74 | #endif // TASKQUEUE_STATS |
jcoomes@2020 | 75 | |
duke@435 | 76 | int TaskQueueSetSuper::randomParkAndMiller(int *seed0) { |
duke@435 | 77 | const int a = 16807; |
duke@435 | 78 | const int m = 2147483647; |
duke@435 | 79 | const int q = 127773; /* m div a */ |
duke@435 | 80 | const int r = 2836; /* m mod a */ |
duke@435 | 81 | assert(sizeof(int) == 4, "I think this relies on that"); |
duke@435 | 82 | int seed = *seed0; |
duke@435 | 83 | int hi = seed / q; |
duke@435 | 84 | int lo = seed % q; |
duke@435 | 85 | int test = a * lo - r * hi; |
duke@435 | 86 | if (test > 0) |
duke@435 | 87 | seed = test; |
duke@435 | 88 | else |
duke@435 | 89 | seed = test + m; |
duke@435 | 90 | *seed0 = seed; |
duke@435 | 91 | return seed; |
duke@435 | 92 | } |
duke@435 | 93 | |
duke@435 | 94 | ParallelTaskTerminator:: |
duke@435 | 95 | ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) : |
duke@435 | 96 | _n_threads(n_threads), |
duke@435 | 97 | _queue_set(queue_set), |
duke@435 | 98 | _offered_termination(0) {} |
duke@435 | 99 | |
duke@435 | 100 | bool ParallelTaskTerminator::peek_in_queue_set() { |
duke@435 | 101 | return _queue_set->peek(); |
duke@435 | 102 | } |
duke@435 | 103 | |
duke@435 | 104 | void ParallelTaskTerminator::yield() { |
ysr@1280 | 105 | assert(_offered_termination <= _n_threads, "Invariant"); |
duke@435 | 106 | os::yield(); |
duke@435 | 107 | } |
duke@435 | 108 | |
duke@435 | 109 | void ParallelTaskTerminator::sleep(uint millis) { |
ysr@1280 | 110 | assert(_offered_termination <= _n_threads, "Invariant"); |
duke@435 | 111 | os::sleep(Thread::current(), millis, false); |
duke@435 | 112 | } |
duke@435 | 113 | |
ysr@777 | 114 | bool |
ysr@777 | 115 | ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) { |
ysr@1280 | 116 | assert(_offered_termination < _n_threads, "Invariant"); |
duke@435 | 117 | Atomic::inc(&_offered_termination); |
duke@435 | 118 | |
ysr@976 | 119 | uint yield_count = 0; |
jmasa@981 | 120 | // Number of hard spin loops done since last yield |
jmasa@981 | 121 | uint hard_spin_count = 0; |
jmasa@981 | 122 | // Number of iterations in the hard spin loop. |
jmasa@981 | 123 | uint hard_spin_limit = WorkStealingHardSpins; |
jmasa@981 | 124 | |
jmasa@981 | 125 | // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done. |
jmasa@981 | 126 | // If it is greater than 0, then start with a small number |
jmasa@981 | 127 | // of spins and increase number with each turn at spinning until |
jmasa@981 | 128 | // the count of hard spins exceeds WorkStealingSpinToYieldRatio. |
jmasa@981 | 129 | // Then do a yield() call and start spinning afresh. |
jmasa@981 | 130 | if (WorkStealingSpinToYieldRatio > 0) { |
jmasa@981 | 131 | hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio; |
jmasa@981 | 132 | hard_spin_limit = MAX2(hard_spin_limit, 1U); |
jmasa@981 | 133 | } |
jmasa@981 | 134 | // Remember the initial spin limit. |
jmasa@981 | 135 | uint hard_spin_start = hard_spin_limit; |
jmasa@981 | 136 | |
jmasa@981 | 137 | // Loop waiting for all threads to offer termination or |
jmasa@981 | 138 | // more work. |
duke@435 | 139 | while (true) { |
ysr@1280 | 140 | assert(_offered_termination <= _n_threads, "Invariant"); |
jmasa@981 | 141 | // Are all threads offering termination? |
duke@435 | 142 | if (_offered_termination == _n_threads) { |
duke@435 | 143 | return true; |
duke@435 | 144 | } else { |
jmasa@981 | 145 | // Look for more work. |
jmasa@981 | 146 | // Periodically sleep() instead of yield() to give threads |
jmasa@981 | 147 | // waiting on the cores the chance to grab this code |
duke@435 | 148 | if (yield_count <= WorkStealingYieldsBeforeSleep) { |
jmasa@981 | 149 | // Do a yield or hardspin. For purposes of deciding whether |
jmasa@981 | 150 | // to sleep, count this as a yield. |
duke@435 | 151 | yield_count++; |
jmasa@981 | 152 | |
jmasa@981 | 153 | // Periodically call yield() instead spinning |
jmasa@981 | 154 | // After WorkStealingSpinToYieldRatio spins, do a yield() call |
jmasa@981 | 155 | // and reset the counts and starting limit. |
jmasa@981 | 156 | if (hard_spin_count > WorkStealingSpinToYieldRatio) { |
jmasa@981 | 157 | yield(); |
jmasa@981 | 158 | hard_spin_count = 0; |
jmasa@981 | 159 | hard_spin_limit = hard_spin_start; |
jmasa@981 | 160 | #ifdef TRACESPINNING |
jmasa@981 | 161 | _total_yields++; |
jmasa@981 | 162 | #endif |
jmasa@981 | 163 | } else { |
jmasa@981 | 164 | // Hard spin this time |
jmasa@981 | 165 | // Increase the hard spinning period but only up to a limit. |
jmasa@981 | 166 | hard_spin_limit = MIN2(2*hard_spin_limit, |
jmasa@981 | 167 | (uint) WorkStealingHardSpins); |
jmasa@981 | 168 | for (uint j = 0; j < hard_spin_limit; j++) { |
jmasa@981 | 169 | SpinPause(); |
jmasa@981 | 170 | } |
jmasa@981 | 171 | hard_spin_count++; |
jmasa@981 | 172 | #ifdef TRACESPINNING |
jmasa@981 | 173 | _total_spins++; |
jmasa@981 | 174 | #endif |
jmasa@981 | 175 | } |
duke@435 | 176 | } else { |
duke@435 | 177 | if (PrintGCDetails && Verbose) { |
duke@435 | 178 | gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() " |
duke@435 | 179 | "thread %d sleeps after %d yields", |
duke@435 | 180 | Thread::current(), yield_count); |
duke@435 | 181 | } |
duke@435 | 182 | yield_count = 0; |
duke@435 | 183 | // A sleep will cause this processor to seek work on another processor's |
duke@435 | 184 | // runqueue, if it has nothing else to run (as opposed to the yield |
duke@435 | 185 | // which may only move the thread to the end of the this processor's |
duke@435 | 186 | // runqueue). |
duke@435 | 187 | sleep(WorkStealingSleepMillis); |
duke@435 | 188 | } |
duke@435 | 189 | |
jmasa@981 | 190 | #ifdef TRACESPINNING |
jmasa@981 | 191 | _total_peeks++; |
jmasa@981 | 192 | #endif |
ysr@777 | 193 | if (peek_in_queue_set() || |
ysr@777 | 194 | (terminator != NULL && terminator->should_exit_termination())) { |
duke@435 | 195 | Atomic::dec(&_offered_termination); |
ysr@1280 | 196 | assert(_offered_termination < _n_threads, "Invariant"); |
duke@435 | 197 | return false; |
duke@435 | 198 | } |
duke@435 | 199 | } |
duke@435 | 200 | } |
duke@435 | 201 | } |
duke@435 | 202 | |
jmasa@981 | 203 | #ifdef TRACESPINNING |
jmasa@981 | 204 | void ParallelTaskTerminator::print_termination_counts() { |
jmasa@981 | 205 | gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %lld " |
jmasa@981 | 206 | "Total spins: %lld Total peeks: %lld", |
jmasa@981 | 207 | total_yields(), |
jmasa@981 | 208 | total_spins(), |
jmasa@981 | 209 | total_peeks()); |
jmasa@981 | 210 | } |
jmasa@981 | 211 | #endif |
jmasa@981 | 212 | |
duke@435 | 213 | void ParallelTaskTerminator::reset_for_reuse() { |
duke@435 | 214 | if (_offered_termination != 0) { |
duke@435 | 215 | assert(_offered_termination == _n_threads, |
duke@435 | 216 | "Terminator may still be in use"); |
duke@435 | 217 | _offered_termination = 0; |
duke@435 | 218 | } |
duke@435 | 219 | } |
duke@435 | 220 | |
jcoomes@1746 | 221 | #ifdef ASSERT |
jcoomes@1746 | 222 | bool ObjArrayTask::is_valid() const { |
jcoomes@1746 | 223 | return _obj != NULL && _obj->is_objArray() && _index > 0 && |
jcoomes@1746 | 224 | _index < objArrayOop(_obj)->length(); |
jcoomes@1746 | 225 | } |
jcoomes@1746 | 226 | #endif // ASSERT |