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