1 /* 2 * Copyright (c) 2001, 2018, 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 "gc/shared/taskqueue.hpp" 27 #include "gc/shared/owstTaskTerminator.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "logging/log.hpp" 30 #include "runtime/atomic.hpp" 31 #include "runtime/os.hpp" 32 #include "runtime/thread.inline.hpp" 33 #include "utilities/debug.hpp" 34 #include "utilities/stack.inline.hpp" 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 "push=" SIZE_FORMAT " pop=" SIZE_FORMAT " steal=" SIZE_FORMAT, 98 get(push), get(pop), get(steal)); 99 assert(get(pop_slow) <= get(pop), 100 "pop_slow=" SIZE_FORMAT " pop=" SIZE_FORMAT, 101 get(pop_slow), get(pop)); 102 assert(get(steal) <= get(steal_attempt), 103 "steal=" SIZE_FORMAT " steal_attempt=" SIZE_FORMAT, 104 get(steal), get(steal_attempt)); 105 assert(get(overflow) == 0 || get(push) != 0, 106 "overflow=" SIZE_FORMAT " push=" SIZE_FORMAT, 107 get(overflow), get(push)); 108 assert(get(overflow_max_len) == 0 || get(overflow) != 0, 109 "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 ParallelTaskTerminator:: 116 ParallelTaskTerminator(uint n_threads, TaskQueueSetSuper* queue_set) : 117 _n_threads(n_threads), 118 _queue_set(queue_set), 119 _offered_termination(0) {} 120 121 bool ParallelTaskTerminator::peek_in_queue_set() { 122 return _queue_set->peek(); 123 } 124 125 void ParallelTaskTerminator::yield() { 126 assert(_offered_termination <= _n_threads, "Invariant"); 127 os::naked_yield(); 128 } 129 130 void ParallelTaskTerminator::sleep(uint millis) { 131 assert(_offered_termination <= _n_threads, "Invariant"); 132 os::sleep(Thread::current(), millis, false); 133 } 134 135 bool 136 ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) { 137 assert(_n_threads > 0, "Initialization is incorrect"); 138 assert(_offered_termination < _n_threads, "Invariant"); 139 Atomic::inc(&_offered_termination); 140 141 uint yield_count = 0; 142 // Number of hard spin loops done since last yield 143 uint hard_spin_count = 0; 144 // Number of iterations in the hard spin loop. 145 uint hard_spin_limit = WorkStealingHardSpins; 146 147 // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done. 148 // If it is greater than 0, then start with a small number 149 // of spins and increase number with each turn at spinning until 150 // the count of hard spins exceeds WorkStealingSpinToYieldRatio. 151 // Then do a yield() call and start spinning afresh. 152 if (WorkStealingSpinToYieldRatio > 0) { 153 hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio; 154 hard_spin_limit = MAX2(hard_spin_limit, 1U); 155 } 156 // Remember the initial spin limit. 157 uint hard_spin_start = hard_spin_limit; 158 159 // Loop waiting for all threads to offer termination or 160 // more work. 161 while (true) { 162 assert(_offered_termination <= _n_threads, "Invariant"); 163 // Are all threads offering termination? 164 if (_offered_termination == _n_threads) { 165 return true; 166 } else { 167 // Look for more work. 168 // Periodically sleep() instead of yield() to give threads 169 // waiting on the cores the chance to grab this code 170 if (yield_count <= WorkStealingYieldsBeforeSleep) { 171 // Do a yield or hardspin. For purposes of deciding whether 172 // to sleep, count this as a yield. 173 yield_count++; 174 175 // Periodically call yield() instead spinning 176 // After WorkStealingSpinToYieldRatio spins, do a yield() call 177 // and reset the counts and starting limit. 178 if (hard_spin_count > WorkStealingSpinToYieldRatio) { 179 yield(); 180 hard_spin_count = 0; 181 hard_spin_limit = hard_spin_start; 182 #ifdef TRACESPINNING 183 _total_yields++; 184 #endif 185 } else { 186 // Hard spin this time 187 // Increase the hard spinning period but only up to a limit. 188 hard_spin_limit = MIN2(2*hard_spin_limit, 189 (uint) WorkStealingHardSpins); 190 for (uint j = 0; j < hard_spin_limit; j++) { 191 SpinPause(); 192 } 193 hard_spin_count++; 194 #ifdef TRACESPINNING 195 _total_spins++; 196 #endif 197 } 198 } else { 199 log_develop_trace(gc, task)("ParallelTaskTerminator::offer_termination() thread " PTR_FORMAT " sleeps after %u yields", 200 p2i(Thread::current()), yield_count); 201 yield_count = 0; 202 // A sleep will cause this processor to seek work on another processor's 203 // runqueue, if it has nothing else to run (as opposed to the yield 204 // which may only move the thread to the end of the this processor's 205 // runqueue). 206 sleep(WorkStealingSleepMillis); 207 } 208 209 #ifdef TRACESPINNING 210 _total_peeks++; 211 #endif 212 if (peek_in_queue_set() || 213 (terminator != NULL && terminator->should_exit_termination())) { 214 Atomic::dec(&_offered_termination); 215 assert(_offered_termination < _n_threads, "Invariant"); 216 return false; 217 } 218 } 219 } 220 } 221 222 #ifdef TRACESPINNING 223 void ParallelTaskTerminator::print_termination_counts() { 224 log_trace(gc, task)("ParallelTaskTerminator Total yields: %u" 225 " Total spins: %u Total peeks: %u", 226 total_yields(), 227 total_spins(), 228 total_peeks()); 229 } 230 #endif 231 232 void ParallelTaskTerminator::reset_for_reuse() { 233 if (_offered_termination != 0) { 234 assert(_offered_termination == _n_threads, 235 "Terminator may still be in use"); 236 _offered_termination = 0; 237 } 238 } 239 240 #ifdef ASSERT 241 bool ObjArrayTask::is_valid() const { 242 return _obj != NULL && _obj->is_objArray() && _index >= 0 && 243 _index < objArrayOop(_obj)->length(); 244 } 245 #endif // ASSERT 246 247 void ParallelTaskTerminator::reset_for_reuse(uint n_threads) { 248 reset_for_reuse(); 249 _n_threads = n_threads; 250 } 251 252 TaskTerminator::TaskTerminator(uint n_threads, TaskQueueSetSuper* queue_set) : 253 _terminator(UseOWSTTaskTerminator ? new OWSTTaskTerminator(n_threads, queue_set) 254 : new ParallelTaskTerminator(n_threads, queue_set)) { 255 } 256 257 TaskTerminator::~TaskTerminator() { 258 if (_terminator != NULL) { 259 delete _terminator; 260 } 261 } 262 263 // Move assignment 264 TaskTerminator& TaskTerminator::operator=(const TaskTerminator& o) { 265 if (_terminator != NULL) { 266 delete _terminator; 267 } 268 _terminator = o.terminator(); 269 const_cast<TaskTerminator&>(o)._terminator = NULL; 270 return *this; 271 } 272