src/share/vm/utilities/taskqueue.cpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 2188
8b10f48633dc
child 3156
f08d439fab8c
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

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
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "oops/oop.inline.hpp"
stefank@2314 27 #include "runtime/os.hpp"
stefank@2314 28 #include "utilities/debug.hpp"
stefank@2314 29 #include "utilities/stack.inline.hpp"
stefank@2314 30 #include "utilities/taskqueue.hpp"
stefank@2314 31 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 32 # include "thread_linux.inline.hpp"
stefank@2314 33 #endif
stefank@2314 34 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 35 # include "thread_solaris.inline.hpp"
stefank@2314 36 #endif
stefank@2314 37 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 38 # include "thread_windows.inline.hpp"
stefank@2314 39 #endif
duke@435 40
jmasa@981 41 #ifdef TRACESPINNING
jmasa@981 42 uint ParallelTaskTerminator::_total_yields = 0;
jmasa@981 43 uint ParallelTaskTerminator::_total_spins = 0;
jmasa@981 44 uint ParallelTaskTerminator::_total_peeks = 0;
jmasa@981 45 #endif
jmasa@981 46
jcoomes@2020 47 #if TASKQUEUE_STATS
jcoomes@2020 48 const char * const TaskQueueStats::_names[last_stat_id] = {
jcoomes@2020 49 "qpush", "qpop", "qpop-s", "qattempt", "qsteal", "opush", "omax"
jcoomes@2020 50 };
jcoomes@2020 51
jcoomes@2064 52 TaskQueueStats & TaskQueueStats::operator +=(const TaskQueueStats & addend)
jcoomes@2064 53 {
jcoomes@2064 54 for (unsigned int i = 0; i < last_stat_id; ++i) {
jcoomes@2064 55 _stats[i] += addend._stats[i];
jcoomes@2064 56 }
jcoomes@2064 57 return *this;
jcoomes@2064 58 }
jcoomes@2064 59
jcoomes@2020 60 void TaskQueueStats::print_header(unsigned int line, outputStream* const stream,
jcoomes@2020 61 unsigned int width)
jcoomes@2020 62 {
jcoomes@2020 63 // Use a width w: 1 <= w <= max_width
jcoomes@2020 64 const unsigned int max_width = 40;
jcoomes@2020 65 const unsigned int w = MAX2(MIN2(width, max_width), 1U);
jcoomes@2020 66
jcoomes@2020 67 if (line == 0) { // spaces equal in width to the header
jcoomes@2020 68 const unsigned int hdr_width = w * last_stat_id + last_stat_id - 1;
jcoomes@2020 69 stream->print("%*s", hdr_width, " ");
jcoomes@2020 70 } else if (line == 1) { // labels
jcoomes@2020 71 stream->print("%*s", w, _names[0]);
jcoomes@2020 72 for (unsigned int i = 1; i < last_stat_id; ++i) {
jcoomes@2020 73 stream->print(" %*s", w, _names[i]);
jcoomes@2020 74 }
jcoomes@2020 75 } else if (line == 2) { // dashed lines
jcoomes@2020 76 char dashes[max_width + 1];
jcoomes@2020 77 memset(dashes, '-', w);
jcoomes@2020 78 dashes[w] = '\0';
jcoomes@2020 79 stream->print("%s", dashes);
jcoomes@2020 80 for (unsigned int i = 1; i < last_stat_id; ++i) {
jcoomes@2020 81 stream->print(" %s", dashes);
jcoomes@2020 82 }
jcoomes@2020 83 }
jcoomes@2020 84 }
jcoomes@2020 85
jcoomes@2020 86 void TaskQueueStats::print(outputStream* stream, unsigned int width) const
jcoomes@2020 87 {
jcoomes@2020 88 #define FMT SIZE_FORMAT_W(*)
jcoomes@2020 89 stream->print(FMT, width, _stats[0]);
jcoomes@2020 90 for (unsigned int i = 1; i < last_stat_id; ++i) {
jcoomes@2020 91 stream->print(" " FMT, width, _stats[i]);
jcoomes@2020 92 }
jcoomes@2020 93 #undef FMT
jcoomes@2020 94 }
jcoomes@2064 95
jcoomes@2064 96 #ifdef ASSERT
jcoomes@2064 97 // Invariants which should hold after a TaskQueue has been emptied and is
jcoomes@2064 98 // quiescent; they do not hold at arbitrary times.
jcoomes@2064 99 void TaskQueueStats::verify() const
jcoomes@2064 100 {
jcoomes@2064 101 assert(get(push) == get(pop) + get(steal),
jcoomes@2064 102 err_msg("push=" SIZE_FORMAT " pop=" SIZE_FORMAT " steal=" SIZE_FORMAT,
jcoomes@2064 103 get(push), get(pop), get(steal)));
jcoomes@2064 104 assert(get(pop_slow) <= get(pop),
jcoomes@2064 105 err_msg("pop_slow=" SIZE_FORMAT " pop=" SIZE_FORMAT,
jcoomes@2064 106 get(pop_slow), get(pop)));
jcoomes@2064 107 assert(get(steal) <= get(steal_attempt),
jcoomes@2064 108 err_msg("steal=" SIZE_FORMAT " steal_attempt=" SIZE_FORMAT,
jcoomes@2064 109 get(steal), get(steal_attempt)));
jcoomes@2064 110 assert(get(overflow) == 0 || get(push) != 0,
jcoomes@2064 111 err_msg("overflow=" SIZE_FORMAT " push=" SIZE_FORMAT,
jcoomes@2064 112 get(overflow), get(push)));
jcoomes@2064 113 assert(get(overflow_max_len) == 0 || get(overflow) != 0,
jcoomes@2064 114 err_msg("overflow_max_len=" SIZE_FORMAT " overflow=" SIZE_FORMAT,
jcoomes@2064 115 get(overflow_max_len), get(overflow)));
jcoomes@2064 116 }
jcoomes@2064 117 #endif // ASSERT
jcoomes@2020 118 #endif // TASKQUEUE_STATS
jcoomes@2020 119
duke@435 120 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
duke@435 121 const int a = 16807;
duke@435 122 const int m = 2147483647;
duke@435 123 const int q = 127773; /* m div a */
duke@435 124 const int r = 2836; /* m mod a */
duke@435 125 assert(sizeof(int) == 4, "I think this relies on that");
duke@435 126 int seed = *seed0;
duke@435 127 int hi = seed / q;
duke@435 128 int lo = seed % q;
duke@435 129 int test = a * lo - r * hi;
duke@435 130 if (test > 0)
duke@435 131 seed = test;
duke@435 132 else
duke@435 133 seed = test + m;
duke@435 134 *seed0 = seed;
duke@435 135 return seed;
duke@435 136 }
duke@435 137
duke@435 138 ParallelTaskTerminator::
duke@435 139 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
duke@435 140 _n_threads(n_threads),
duke@435 141 _queue_set(queue_set),
duke@435 142 _offered_termination(0) {}
duke@435 143
duke@435 144 bool ParallelTaskTerminator::peek_in_queue_set() {
duke@435 145 return _queue_set->peek();
duke@435 146 }
duke@435 147
duke@435 148 void ParallelTaskTerminator::yield() {
ysr@1280 149 assert(_offered_termination <= _n_threads, "Invariant");
duke@435 150 os::yield();
duke@435 151 }
duke@435 152
duke@435 153 void ParallelTaskTerminator::sleep(uint millis) {
ysr@1280 154 assert(_offered_termination <= _n_threads, "Invariant");
duke@435 155 os::sleep(Thread::current(), millis, false);
duke@435 156 }
duke@435 157
ysr@777 158 bool
ysr@777 159 ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
jmasa@2188 160 assert(_n_threads > 0, "Initialization is incorrect");
ysr@1280 161 assert(_offered_termination < _n_threads, "Invariant");
duke@435 162 Atomic::inc(&_offered_termination);
duke@435 163
ysr@976 164 uint yield_count = 0;
jmasa@981 165 // Number of hard spin loops done since last yield
jmasa@981 166 uint hard_spin_count = 0;
jmasa@981 167 // Number of iterations in the hard spin loop.
jmasa@981 168 uint hard_spin_limit = WorkStealingHardSpins;
jmasa@981 169
jmasa@981 170 // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done.
jmasa@981 171 // If it is greater than 0, then start with a small number
jmasa@981 172 // of spins and increase number with each turn at spinning until
jmasa@981 173 // the count of hard spins exceeds WorkStealingSpinToYieldRatio.
jmasa@981 174 // Then do a yield() call and start spinning afresh.
jmasa@981 175 if (WorkStealingSpinToYieldRatio > 0) {
jmasa@981 176 hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio;
jmasa@981 177 hard_spin_limit = MAX2(hard_spin_limit, 1U);
jmasa@981 178 }
jmasa@981 179 // Remember the initial spin limit.
jmasa@981 180 uint hard_spin_start = hard_spin_limit;
jmasa@981 181
jmasa@981 182 // Loop waiting for all threads to offer termination or
jmasa@981 183 // more work.
duke@435 184 while (true) {
ysr@1280 185 assert(_offered_termination <= _n_threads, "Invariant");
jmasa@981 186 // Are all threads offering termination?
duke@435 187 if (_offered_termination == _n_threads) {
duke@435 188 return true;
duke@435 189 } else {
jmasa@981 190 // Look for more work.
jmasa@981 191 // Periodically sleep() instead of yield() to give threads
jmasa@981 192 // waiting on the cores the chance to grab this code
duke@435 193 if (yield_count <= WorkStealingYieldsBeforeSleep) {
jmasa@981 194 // Do a yield or hardspin. For purposes of deciding whether
jmasa@981 195 // to sleep, count this as a yield.
duke@435 196 yield_count++;
jmasa@981 197
jmasa@981 198 // Periodically call yield() instead spinning
jmasa@981 199 // After WorkStealingSpinToYieldRatio spins, do a yield() call
jmasa@981 200 // and reset the counts and starting limit.
jmasa@981 201 if (hard_spin_count > WorkStealingSpinToYieldRatio) {
jmasa@981 202 yield();
jmasa@981 203 hard_spin_count = 0;
jmasa@981 204 hard_spin_limit = hard_spin_start;
jmasa@981 205 #ifdef TRACESPINNING
jmasa@981 206 _total_yields++;
jmasa@981 207 #endif
jmasa@981 208 } else {
jmasa@981 209 // Hard spin this time
jmasa@981 210 // Increase the hard spinning period but only up to a limit.
jmasa@981 211 hard_spin_limit = MIN2(2*hard_spin_limit,
jmasa@981 212 (uint) WorkStealingHardSpins);
jmasa@981 213 for (uint j = 0; j < hard_spin_limit; j++) {
jmasa@981 214 SpinPause();
jmasa@981 215 }
jmasa@981 216 hard_spin_count++;
jmasa@981 217 #ifdef TRACESPINNING
jmasa@981 218 _total_spins++;
jmasa@981 219 #endif
jmasa@981 220 }
duke@435 221 } else {
duke@435 222 if (PrintGCDetails && Verbose) {
duke@435 223 gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
duke@435 224 "thread %d sleeps after %d yields",
duke@435 225 Thread::current(), yield_count);
duke@435 226 }
duke@435 227 yield_count = 0;
duke@435 228 // A sleep will cause this processor to seek work on another processor's
duke@435 229 // runqueue, if it has nothing else to run (as opposed to the yield
duke@435 230 // which may only move the thread to the end of the this processor's
duke@435 231 // runqueue).
duke@435 232 sleep(WorkStealingSleepMillis);
duke@435 233 }
duke@435 234
jmasa@981 235 #ifdef TRACESPINNING
jmasa@981 236 _total_peeks++;
jmasa@981 237 #endif
ysr@777 238 if (peek_in_queue_set() ||
ysr@777 239 (terminator != NULL && terminator->should_exit_termination())) {
duke@435 240 Atomic::dec(&_offered_termination);
ysr@1280 241 assert(_offered_termination < _n_threads, "Invariant");
duke@435 242 return false;
duke@435 243 }
duke@435 244 }
duke@435 245 }
duke@435 246 }
duke@435 247
jmasa@981 248 #ifdef TRACESPINNING
jmasa@981 249 void ParallelTaskTerminator::print_termination_counts() {
jmasa@981 250 gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %lld "
jmasa@981 251 "Total spins: %lld Total peeks: %lld",
jmasa@981 252 total_yields(),
jmasa@981 253 total_spins(),
jmasa@981 254 total_peeks());
jmasa@981 255 }
jmasa@981 256 #endif
jmasa@981 257
duke@435 258 void ParallelTaskTerminator::reset_for_reuse() {
duke@435 259 if (_offered_termination != 0) {
duke@435 260 assert(_offered_termination == _n_threads,
duke@435 261 "Terminator may still be in use");
duke@435 262 _offered_termination = 0;
duke@435 263 }
duke@435 264 }
duke@435 265
jcoomes@1746 266 #ifdef ASSERT
jcoomes@1746 267 bool ObjArrayTask::is_valid() const {
jcoomes@1746 268 return _obj != NULL && _obj->is_objArray() && _index > 0 &&
jcoomes@1746 269 _index < objArrayOop(_obj)->length();
jcoomes@1746 270 }
jcoomes@1746 271 #endif // ASSERT
jmasa@2188 272
jmasa@2188 273 void ParallelTaskTerminator::reset_for_reuse(int n_threads) {
jmasa@2188 274 reset_for_reuse();
jmasa@2188 275 _n_threads = n_threads;
jmasa@2188 276 }
jmasa@2188 277

mercurial