src/share/vm/utilities/taskqueue.hpp

Tue, 30 Oct 2012 20:26:44 +0100

author
brutisso
date
Tue, 30 Oct 2012 20:26:44 +0100
changeset 4235
3dfffc8b9722
parent 4153
b9a9ed0f8eeb
child 4901
83f27710f5f7
permissions
-rw-r--r--

8001564: The load balancing function steal_1_random in taskqueue is not random
Summary: Removes the two unused functions GenericTaskQueueSet::steal_1_random and GenericTaskQueueSet::steal_best_of_all
Reviewed-by: brutisso, stefank
Contributed-by: erik.x.helin@oracle.com

duke@435 1 /*
mikael@4153 2 * Copyright (c) 2001, 2012, 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 #ifndef SHARE_VM_UTILITIES_TASKQUEUE_HPP
stefank@2314 26 #define SHARE_VM_UTILITIES_TASKQUEUE_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.hpp"
stefank@2314 29 #include "memory/allocation.inline.hpp"
stefank@2314 30 #include "runtime/mutex.hpp"
stefank@2314 31 #include "utilities/stack.hpp"
stefank@2314 32 #ifdef TARGET_OS_ARCH_linux_x86
stefank@2314 33 # include "orderAccess_linux_x86.inline.hpp"
stefank@2314 34 #endif
stefank@2314 35 #ifdef TARGET_OS_ARCH_linux_sparc
stefank@2314 36 # include "orderAccess_linux_sparc.inline.hpp"
stefank@2314 37 #endif
stefank@2314 38 #ifdef TARGET_OS_ARCH_linux_zero
stefank@2314 39 # include "orderAccess_linux_zero.inline.hpp"
stefank@2314 40 #endif
stefank@2314 41 #ifdef TARGET_OS_ARCH_solaris_x86
stefank@2314 42 # include "orderAccess_solaris_x86.inline.hpp"
stefank@2314 43 #endif
stefank@2314 44 #ifdef TARGET_OS_ARCH_solaris_sparc
stefank@2314 45 # include "orderAccess_solaris_sparc.inline.hpp"
stefank@2314 46 #endif
stefank@2314 47 #ifdef TARGET_OS_ARCH_windows_x86
stefank@2314 48 # include "orderAccess_windows_x86.inline.hpp"
stefank@2314 49 #endif
bobv@2508 50 #ifdef TARGET_OS_ARCH_linux_arm
bobv@2508 51 # include "orderAccess_linux_arm.inline.hpp"
bobv@2508 52 #endif
bobv@2508 53 #ifdef TARGET_OS_ARCH_linux_ppc
bobv@2508 54 # include "orderAccess_linux_ppc.inline.hpp"
bobv@2508 55 #endif
never@3156 56 #ifdef TARGET_OS_ARCH_bsd_x86
never@3156 57 # include "orderAccess_bsd_x86.inline.hpp"
never@3156 58 #endif
never@3156 59 #ifdef TARGET_OS_ARCH_bsd_zero
never@3156 60 # include "orderAccess_bsd_zero.inline.hpp"
never@3156 61 #endif
stefank@2314 62
jcoomes@2020 63 // Simple TaskQueue stats that are collected by default in debug builds.
jcoomes@2020 64
jcoomes@2020 65 #if !defined(TASKQUEUE_STATS) && defined(ASSERT)
jcoomes@2020 66 #define TASKQUEUE_STATS 1
jcoomes@2020 67 #elif !defined(TASKQUEUE_STATS)
jcoomes@2020 68 #define TASKQUEUE_STATS 0
jcoomes@2020 69 #endif
jcoomes@2020 70
jcoomes@2020 71 #if TASKQUEUE_STATS
jcoomes@2020 72 #define TASKQUEUE_STATS_ONLY(code) code
jcoomes@2020 73 #else
jcoomes@2020 74 #define TASKQUEUE_STATS_ONLY(code)
jcoomes@2020 75 #endif // TASKQUEUE_STATS
jcoomes@2020 76
jcoomes@2020 77 #if TASKQUEUE_STATS
jcoomes@2020 78 class TaskQueueStats {
jcoomes@2020 79 public:
jcoomes@2020 80 enum StatId {
jcoomes@2020 81 push, // number of taskqueue pushes
jcoomes@2020 82 pop, // number of taskqueue pops
jcoomes@2020 83 pop_slow, // subset of taskqueue pops that were done slow-path
jcoomes@2020 84 steal_attempt, // number of taskqueue steal attempts
jcoomes@2020 85 steal, // number of taskqueue steals
jcoomes@2020 86 overflow, // number of overflow pushes
jcoomes@2020 87 overflow_max_len, // max length of overflow stack
jcoomes@2020 88 last_stat_id
jcoomes@2020 89 };
jcoomes@2020 90
jcoomes@2020 91 public:
jcoomes@2020 92 inline TaskQueueStats() { reset(); }
jcoomes@2020 93
jcoomes@2020 94 inline void record_push() { ++_stats[push]; }
jcoomes@2020 95 inline void record_pop() { ++_stats[pop]; }
jcoomes@2020 96 inline void record_pop_slow() { record_pop(); ++_stats[pop_slow]; }
jcoomes@2020 97 inline void record_steal(bool success);
jcoomes@2020 98 inline void record_overflow(size_t new_length);
jcoomes@2020 99
jcoomes@2064 100 TaskQueueStats & operator +=(const TaskQueueStats & addend);
jcoomes@2064 101
jcoomes@2020 102 inline size_t get(StatId id) const { return _stats[id]; }
jcoomes@2020 103 inline const size_t* get() const { return _stats; }
jcoomes@2020 104
jcoomes@2020 105 inline void reset();
jcoomes@2020 106
jcoomes@2064 107 // Print the specified line of the header (does not include a line separator).
jcoomes@2020 108 static void print_header(unsigned int line, outputStream* const stream = tty,
jcoomes@2020 109 unsigned int width = 10);
jcoomes@2064 110 // Print the statistics (does not include a line separator).
jcoomes@2020 111 void print(outputStream* const stream = tty, unsigned int width = 10) const;
jcoomes@2020 112
jcoomes@2064 113 DEBUG_ONLY(void verify() const;)
jcoomes@2064 114
jcoomes@2020 115 private:
jcoomes@2020 116 size_t _stats[last_stat_id];
jcoomes@2020 117 static const char * const _names[last_stat_id];
jcoomes@2020 118 };
jcoomes@2020 119
jcoomes@2020 120 void TaskQueueStats::record_steal(bool success) {
jcoomes@2020 121 ++_stats[steal_attempt];
jcoomes@2020 122 if (success) ++_stats[steal];
jcoomes@2020 123 }
jcoomes@2020 124
jcoomes@2020 125 void TaskQueueStats::record_overflow(size_t new_len) {
jcoomes@2020 126 ++_stats[overflow];
jcoomes@2020 127 if (new_len > _stats[overflow_max_len]) _stats[overflow_max_len] = new_len;
jcoomes@2020 128 }
jcoomes@2020 129
jcoomes@2020 130 void TaskQueueStats::reset() {
jcoomes@2020 131 memset(_stats, 0, sizeof(_stats));
jcoomes@2020 132 }
jcoomes@2020 133 #endif // TASKQUEUE_STATS
jcoomes@2020 134
zgu@3900 135 template <unsigned int N, MEMFLAGS F>
zgu@3900 136 class TaskQueueSuper: public CHeapObj<F> {
duke@435 137 protected:
jcoomes@1342 138 // Internal type for indexing the queue; also used for the tag.
jcoomes@1342 139 typedef NOT_LP64(uint16_t) LP64_ONLY(uint32_t) idx_t;
jcoomes@1342 140
jcoomes@1342 141 // The first free element after the last one pushed (mod N).
ysr@976 142 volatile uint _bottom;
duke@435 143
jcoomes@1746 144 enum { MOD_N_MASK = N - 1 };
duke@435 145
jcoomes@1342 146 class Age {
jcoomes@1342 147 public:
jcoomes@1342 148 Age(size_t data = 0) { _data = data; }
jcoomes@1342 149 Age(const Age& age) { _data = age._data; }
jcoomes@1342 150 Age(idx_t top, idx_t tag) { _fields._top = top; _fields._tag = tag; }
duke@435 151
jcoomes@1342 152 Age get() const volatile { return _data; }
jcoomes@1342 153 void set(Age age) volatile { _data = age._data; }
duke@435 154
jcoomes@1342 155 idx_t top() const volatile { return _fields._top; }
jcoomes@1342 156 idx_t tag() const volatile { return _fields._tag; }
duke@435 157
jcoomes@1342 158 // Increment top; if it wraps, increment tag also.
jcoomes@1342 159 void increment() {
jcoomes@1342 160 _fields._top = increment_index(_fields._top);
jcoomes@1342 161 if (_fields._top == 0) ++_fields._tag;
jcoomes@1342 162 }
duke@435 163
jcoomes@1342 164 Age cmpxchg(const Age new_age, const Age old_age) volatile {
jcoomes@1342 165 return (size_t) Atomic::cmpxchg_ptr((intptr_t)new_age._data,
jcoomes@1342 166 (volatile intptr_t *)&_data,
jcoomes@1342 167 (intptr_t)old_age._data);
duke@435 168 }
jcoomes@1342 169
jcoomes@1342 170 bool operator ==(const Age& other) const { return _data == other._data; }
jcoomes@1342 171
jcoomes@1342 172 private:
jcoomes@1342 173 struct fields {
jcoomes@1342 174 idx_t _top;
jcoomes@1342 175 idx_t _tag;
jcoomes@1342 176 };
jcoomes@1342 177 union {
jcoomes@1342 178 size_t _data;
jcoomes@1342 179 fields _fields;
jcoomes@1342 180 };
duke@435 181 };
jcoomes@1342 182
jcoomes@1342 183 volatile Age _age;
jcoomes@1342 184
jcoomes@1342 185 // These both operate mod N.
jcoomes@1342 186 static uint increment_index(uint ind) {
jcoomes@1342 187 return (ind + 1) & MOD_N_MASK;
duke@435 188 }
jcoomes@1342 189 static uint decrement_index(uint ind) {
jcoomes@1342 190 return (ind - 1) & MOD_N_MASK;
duke@435 191 }
duke@435 192
jcoomes@1342 193 // Returns a number in the range [0..N). If the result is "N-1", it should be
jcoomes@1342 194 // interpreted as 0.
jcoomes@1746 195 uint dirty_size(uint bot, uint top) const {
jcoomes@1342 196 return (bot - top) & MOD_N_MASK;
duke@435 197 }
duke@435 198
duke@435 199 // Returns the size corresponding to the given "bot" and "top".
jcoomes@1746 200 uint size(uint bot, uint top) const {
ysr@976 201 uint sz = dirty_size(bot, top);
jcoomes@1342 202 // Has the queue "wrapped", so that bottom is less than top? There's a
jcoomes@1342 203 // complicated special case here. A pair of threads could perform pop_local
jcoomes@1342 204 // and pop_global operations concurrently, starting from a state in which
jcoomes@1342 205 // _bottom == _top+1. The pop_local could succeed in decrementing _bottom,
jcoomes@1342 206 // and the pop_global in incrementing _top (in which case the pop_global
jcoomes@1342 207 // will be awarded the contested queue element.) The resulting state must
jcoomes@1342 208 // be interpreted as an empty queue. (We only need to worry about one such
jcoomes@1342 209 // event: only the queue owner performs pop_local's, and several concurrent
jcoomes@1342 210 // threads attempting to perform the pop_global will all perform the same
jcoomes@1342 211 // CAS, and only one can succeed.) Any stealing thread that reads after
jcoomes@1342 212 // either the increment or decrement will see an empty queue, and will not
jcoomes@1342 213 // join the competitors. The "sz == -1 || sz == N-1" state will not be
jcoomes@1342 214 // modified by concurrent queues, so the owner thread can reset the state to
jcoomes@1342 215 // _bottom == top so subsequent pushes will be performed normally.
jcoomes@1342 216 return (sz == N - 1) ? 0 : sz;
duke@435 217 }
duke@435 218
duke@435 219 public:
duke@435 220 TaskQueueSuper() : _bottom(0), _age() {}
duke@435 221
jcoomes@1993 222 // Return true if the TaskQueue contains/does not contain any tasks.
jcoomes@1993 223 bool peek() const { return _bottom != _age.top(); }
jcoomes@1993 224 bool is_empty() const { return size() == 0; }
duke@435 225
duke@435 226 // Return an estimate of the number of elements in the queue.
duke@435 227 // The "careful" version admits the possibility of pop_local/pop_global
duke@435 228 // races.
jcoomes@1746 229 uint size() const {
jcoomes@1342 230 return size(_bottom, _age.top());
duke@435 231 }
duke@435 232
jcoomes@1746 233 uint dirty_size() const {
jcoomes@1342 234 return dirty_size(_bottom, _age.top());
duke@435 235 }
duke@435 236
ysr@777 237 void set_empty() {
ysr@777 238 _bottom = 0;
jcoomes@1342 239 _age.set(0);
ysr@777 240 }
ysr@777 241
duke@435 242 // Maximum number of elements allowed in the queue. This is two less
duke@435 243 // than the actual queue size, for somewhat complicated reasons.
jcoomes@1746 244 uint max_elems() const { return N - 2; }
jmasa@1719 245
jmasa@1719 246 // Total size of queue.
jmasa@1719 247 static const uint total_size() { return N; }
jcoomes@2020 248
jcoomes@2020 249 TASKQUEUE_STATS_ONLY(TaskQueueStats stats;)
duke@435 250 };
duke@435 251
zgu@3900 252
zgu@3900 253
zgu@3900 254 template <class E, MEMFLAGS F, unsigned int N = TASKQUEUE_SIZE>
zgu@3900 255 class GenericTaskQueue: public TaskQueueSuper<N, F> {
jcoomes@1746 256 protected:
zgu@3900 257 typedef typename TaskQueueSuper<N, F>::Age Age;
zgu@3900 258 typedef typename TaskQueueSuper<N, F>::idx_t idx_t;
jcoomes@1746 259
zgu@3900 260 using TaskQueueSuper<N, F>::_bottom;
zgu@3900 261 using TaskQueueSuper<N, F>::_age;
zgu@3900 262 using TaskQueueSuper<N, F>::increment_index;
zgu@3900 263 using TaskQueueSuper<N, F>::decrement_index;
zgu@3900 264 using TaskQueueSuper<N, F>::dirty_size;
jcoomes@1746 265
jcoomes@1746 266 public:
zgu@3900 267 using TaskQueueSuper<N, F>::max_elems;
zgu@3900 268 using TaskQueueSuper<N, F>::size;
zgu@3900 269
zgu@3900 270 #if TASKQUEUE_STATS
zgu@3900 271 using TaskQueueSuper<N, F>::stats;
zgu@3900 272 #endif
jcoomes@1746 273
duke@435 274 private:
duke@435 275 // Slow paths for push, pop_local. (pop_global has no fast path.)
ysr@976 276 bool push_slow(E t, uint dirty_n_elems);
ysr@976 277 bool pop_local_slow(uint localBot, Age oldAge);
duke@435 278
duke@435 279 public:
jcoomes@1746 280 typedef E element_type;
jcoomes@1746 281
duke@435 282 // Initializes the queue to empty.
duke@435 283 GenericTaskQueue();
duke@435 284
duke@435 285 void initialize();
duke@435 286
jcoomes@1993 287 // Push the task "t" on the queue. Returns "false" iff the queue is full.
duke@435 288 inline bool push(E t);
duke@435 289
jcoomes@1993 290 // Attempts to claim a task from the "local" end of the queue (the most
jcoomes@1993 291 // recently pushed). If successful, returns true and sets t to the task;
jcoomes@1993 292 // otherwise, returns false (the queue is empty).
duke@435 293 inline bool pop_local(E& t);
duke@435 294
jcoomes@1993 295 // Like pop_local(), but uses the "global" end of the queue (the least
jcoomes@1993 296 // recently pushed).
duke@435 297 bool pop_global(E& t);
duke@435 298
duke@435 299 // Delete any resource associated with the queue.
duke@435 300 ~GenericTaskQueue();
duke@435 301
ysr@777 302 // apply the closure to all elements in the task queue
ysr@777 303 void oops_do(OopClosure* f);
ysr@777 304
duke@435 305 private:
duke@435 306 // Element array.
duke@435 307 volatile E* _elems;
duke@435 308 };
duke@435 309
zgu@3900 310 template<class E, MEMFLAGS F, unsigned int N>
zgu@3900 311 GenericTaskQueue<E, F, N>::GenericTaskQueue() {
jcoomes@1342 312 assert(sizeof(Age) == sizeof(size_t), "Depends on this.");
duke@435 313 }
duke@435 314
zgu@3900 315 template<class E, MEMFLAGS F, unsigned int N>
zgu@3900 316 void GenericTaskQueue<E, F, N>::initialize() {
zgu@3900 317 _elems = NEW_C_HEAP_ARRAY(E, N, F);
duke@435 318 }
duke@435 319
zgu@3900 320 template<class E, MEMFLAGS F, unsigned int N>
zgu@3900 321 void GenericTaskQueue<E, F, N>::oops_do(OopClosure* f) {
ysr@777 322 // tty->print_cr("START OopTaskQueue::oops_do");
ysr@976 323 uint iters = size();
ysr@976 324 uint index = _bottom;
ysr@976 325 for (uint i = 0; i < iters; ++i) {
ysr@777 326 index = decrement_index(index);
ysr@777 327 // tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T,
ysr@777 328 // index, &_elems[index], _elems[index]);
ysr@777 329 E* t = (E*)&_elems[index]; // cast away volatility
ysr@777 330 oop* p = (oop*)t;
ysr@777 331 assert((*t)->is_oop_or_null(), "Not an oop or null");
ysr@777 332 f->do_oop(p);
ysr@777 333 }
ysr@777 334 // tty->print_cr("END OopTaskQueue::oops_do");
ysr@777 335 }
ysr@777 336
zgu@3900 337 template<class E, MEMFLAGS F, unsigned int N>
zgu@3900 338 bool GenericTaskQueue<E, F, N>::push_slow(E t, uint dirty_n_elems) {
jcoomes@1342 339 if (dirty_n_elems == N - 1) {
duke@435 340 // Actually means 0, so do the push.
ysr@976 341 uint localBot = _bottom;
jcoomes@1746 342 // g++ complains if the volatile result of the assignment is unused.
jcoomes@1746 343 const_cast<E&>(_elems[localBot] = t);
bobv@1459 344 OrderAccess::release_store(&_bottom, increment_index(localBot));
jcoomes@2020 345 TASKQUEUE_STATS_ONLY(stats.record_push());
duke@435 346 return true;
jcoomes@1342 347 }
jcoomes@1342 348 return false;
duke@435 349 }
duke@435 350
jmasa@2188 351 // pop_local_slow() is done by the owning thread and is trying to
jmasa@2188 352 // get the last task in the queue. It will compete with pop_global()
jmasa@2188 353 // that will be used by other threads. The tag age is incremented
jmasa@2188 354 // whenever the queue goes empty which it will do here if this thread
jmasa@2188 355 // gets the last task or in pop_global() if the queue wraps (top == 0
jmasa@2188 356 // and pop_global() succeeds, see pop_global()).
zgu@3900 357 template<class E, MEMFLAGS F, unsigned int N>
zgu@3900 358 bool GenericTaskQueue<E, F, N>::pop_local_slow(uint localBot, Age oldAge) {
duke@435 359 // This queue was observed to contain exactly one element; either this
duke@435 360 // thread will claim it, or a competing "pop_global". In either case,
duke@435 361 // the queue will be logically empty afterwards. Create a new Age value
duke@435 362 // that represents the empty queue for the given value of "_bottom". (We
duke@435 363 // must also increment "tag" because of the case where "bottom == 1",
duke@435 364 // "top == 0". A pop_global could read the queue element in that case,
duke@435 365 // then have the owner thread do a pop followed by another push. Without
duke@435 366 // the incrementing of "tag", the pop_global's CAS could succeed,
duke@435 367 // allowing it to believe it has claimed the stale element.)
jcoomes@1342 368 Age newAge((idx_t)localBot, oldAge.tag() + 1);
duke@435 369 // Perhaps a competing pop_global has already incremented "top", in which
duke@435 370 // case it wins the element.
duke@435 371 if (localBot == oldAge.top()) {
duke@435 372 // No competing pop_global has yet incremented "top"; we'll try to
duke@435 373 // install new_age, thus claiming the element.
jcoomes@1342 374 Age tempAge = _age.cmpxchg(newAge, oldAge);
duke@435 375 if (tempAge == oldAge) {
duke@435 376 // We win.
jcoomes@1342 377 assert(dirty_size(localBot, _age.top()) != N - 1, "sanity");
jcoomes@2020 378 TASKQUEUE_STATS_ONLY(stats.record_pop_slow());
duke@435 379 return true;
duke@435 380 }
duke@435 381 }
jcoomes@1342 382 // We lose; a completing pop_global gets the element. But the queue is empty
jcoomes@1342 383 // and top is greater than bottom. Fix this representation of the empty queue
jcoomes@1342 384 // to become the canonical one.
jcoomes@1342 385 _age.set(newAge);
jcoomes@1342 386 assert(dirty_size(localBot, _age.top()) != N - 1, "sanity");
duke@435 387 return false;
duke@435 388 }
duke@435 389
zgu@3900 390 template<class E, MEMFLAGS F, unsigned int N>
zgu@3900 391 bool GenericTaskQueue<E, F, N>::pop_global(E& t) {
jcoomes@1342 392 Age oldAge = _age.get();
ysr@976 393 uint localBot = _bottom;
ysr@976 394 uint n_elems = size(localBot, oldAge.top());
duke@435 395 if (n_elems == 0) {
duke@435 396 return false;
duke@435 397 }
jcoomes@1342 398
jcoomes@1746 399 const_cast<E&>(t = _elems[oldAge.top()]);
jcoomes@1342 400 Age newAge(oldAge);
jcoomes@1342 401 newAge.increment();
jcoomes@1342 402 Age resAge = _age.cmpxchg(newAge, oldAge);
jcoomes@1342 403
duke@435 404 // Note that using "_bottom" here might fail, since a pop_local might
duke@435 405 // have decremented it.
jcoomes@1342 406 assert(dirty_size(localBot, newAge.top()) != N - 1, "sanity");
jcoomes@1342 407 return resAge == oldAge;
duke@435 408 }
duke@435 409
zgu@3900 410 template<class E, MEMFLAGS F, unsigned int N>
zgu@3900 411 GenericTaskQueue<E, F, N>::~GenericTaskQueue() {
zgu@3900 412 FREE_C_HEAP_ARRAY(E, _elems, F);
duke@435 413 }
duke@435 414
jcoomes@1993 415 // OverflowTaskQueue is a TaskQueue that also includes an overflow stack for
jcoomes@1993 416 // elements that do not fit in the TaskQueue.
jcoomes@1993 417 //
jcoomes@2191 418 // This class hides two methods from super classes:
jcoomes@1993 419 //
jcoomes@1993 420 // push() - push onto the task queue or, if that fails, onto the overflow stack
jcoomes@1993 421 // is_empty() - return true if both the TaskQueue and overflow stack are empty
jcoomes@1993 422 //
jcoomes@2191 423 // Note that size() is not hidden--it returns the number of elements in the
jcoomes@1993 424 // TaskQueue, and does not include the size of the overflow stack. This
jcoomes@1993 425 // simplifies replacement of GenericTaskQueues with OverflowTaskQueues.
zgu@3900 426 template<class E, MEMFLAGS F, unsigned int N = TASKQUEUE_SIZE>
zgu@3900 427 class OverflowTaskQueue: public GenericTaskQueue<E, F, N>
jcoomes@1993 428 {
jcoomes@1993 429 public:
zgu@3900 430 typedef Stack<E, F> overflow_t;
zgu@3900 431 typedef GenericTaskQueue<E, F, N> taskqueue_t;
jcoomes@1993 432
jcoomes@2020 433 TASKQUEUE_STATS_ONLY(using taskqueue_t::stats;)
jcoomes@2020 434
jcoomes@1993 435 // Push task t onto the queue or onto the overflow stack. Return true.
jcoomes@1993 436 inline bool push(E t);
jcoomes@1993 437
jcoomes@1993 438 // Attempt to pop from the overflow stack; return true if anything was popped.
jcoomes@1993 439 inline bool pop_overflow(E& t);
jcoomes@1993 440
jcoomes@2191 441 inline overflow_t* overflow_stack() { return &_overflow_stack; }
jcoomes@2191 442
jcoomes@1993 443 inline bool taskqueue_empty() const { return taskqueue_t::is_empty(); }
jcoomes@2191 444 inline bool overflow_empty() const { return _overflow_stack.is_empty(); }
jcoomes@1993 445 inline bool is_empty() const {
jcoomes@1993 446 return taskqueue_empty() && overflow_empty();
jcoomes@1993 447 }
jcoomes@1993 448
jcoomes@1993 449 private:
jcoomes@2191 450 overflow_t _overflow_stack;
jcoomes@1993 451 };
jcoomes@1993 452
zgu@3900 453 template <class E, MEMFLAGS F, unsigned int N>
zgu@3900 454 bool OverflowTaskQueue<E, F, N>::push(E t)
jcoomes@1993 455 {
jcoomes@1993 456 if (!taskqueue_t::push(t)) {
jcoomes@1993 457 overflow_stack()->push(t);
jcoomes@2191 458 TASKQUEUE_STATS_ONLY(stats.record_overflow(overflow_stack()->size()));
jcoomes@1993 459 }
jcoomes@1993 460 return true;
jcoomes@1993 461 }
jcoomes@1993 462
zgu@3900 463 template <class E, MEMFLAGS F, unsigned int N>
zgu@3900 464 bool OverflowTaskQueue<E, F, N>::pop_overflow(E& t)
jcoomes@1993 465 {
jcoomes@1993 466 if (overflow_empty()) return false;
jcoomes@1993 467 t = overflow_stack()->pop();
jcoomes@1993 468 return true;
jcoomes@1993 469 }
jcoomes@1993 470
zgu@3900 471 class TaskQueueSetSuper {
duke@435 472 protected:
duke@435 473 static int randomParkAndMiller(int* seed0);
duke@435 474 public:
duke@435 475 // Returns "true" if some TaskQueue in the set contains a task.
duke@435 476 virtual bool peek() = 0;
duke@435 477 };
duke@435 478
zgu@3900 479 template <MEMFLAGS F> class TaskQueueSetSuperImpl: public CHeapObj<F>, public TaskQueueSetSuper {
zgu@3900 480 };
zgu@3900 481
zgu@3900 482 template<class T, MEMFLAGS F>
zgu@3900 483 class GenericTaskQueueSet: public TaskQueueSetSuperImpl<F> {
duke@435 484 private:
ysr@976 485 uint _n;
jcoomes@1746 486 T** _queues;
duke@435 487
duke@435 488 public:
jcoomes@1746 489 typedef typename T::element_type E;
jcoomes@1746 490
duke@435 491 GenericTaskQueueSet(int n) : _n(n) {
jcoomes@1746 492 typedef T* GenericTaskQueuePtr;
zgu@3900 493 _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n, F);
duke@435 494 for (int i = 0; i < n; i++) {
duke@435 495 _queues[i] = NULL;
duke@435 496 }
duke@435 497 }
duke@435 498
ysr@976 499 bool steal_best_of_2(uint queue_num, int* seed, E& t);
duke@435 500
jcoomes@1746 501 void register_queue(uint i, T* q);
duke@435 502
jcoomes@1746 503 T* queue(uint n);
duke@435 504
jcoomes@1993 505 // The thread with queue number "queue_num" (and whose random number seed is
jcoomes@1993 506 // at "seed") is trying to steal a task from some other queue. (It may try
jcoomes@1993 507 // several queues, according to some configuration parameter.) If some steal
jcoomes@1993 508 // succeeds, returns "true" and sets "t" to the stolen task, otherwise returns
jcoomes@1993 509 // false.
ysr@976 510 bool steal(uint queue_num, int* seed, E& t);
duke@435 511
duke@435 512 bool peek();
duke@435 513 };
duke@435 514
zgu@3900 515 template<class T, MEMFLAGS F> void
zgu@3900 516 GenericTaskQueueSet<T, F>::register_queue(uint i, T* q) {
ysr@976 517 assert(i < _n, "index out of range.");
duke@435 518 _queues[i] = q;
duke@435 519 }
duke@435 520
zgu@3900 521 template<class T, MEMFLAGS F> T*
zgu@3900 522 GenericTaskQueueSet<T, F>::queue(uint i) {
duke@435 523 return _queues[i];
duke@435 524 }
duke@435 525
zgu@3900 526 template<class T, MEMFLAGS F> bool
zgu@3900 527 GenericTaskQueueSet<T, F>::steal(uint queue_num, int* seed, E& t) {
jcoomes@2020 528 for (uint i = 0; i < 2 * _n; i++) {
jcoomes@2020 529 if (steal_best_of_2(queue_num, seed, t)) {
jcoomes@2020 530 TASKQUEUE_STATS_ONLY(queue(queue_num)->stats.record_steal(true));
duke@435 531 return true;
jcoomes@2020 532 }
jcoomes@2020 533 }
jcoomes@2020 534 TASKQUEUE_STATS_ONLY(queue(queue_num)->stats.record_steal(false));
duke@435 535 return false;
duke@435 536 }
duke@435 537
zgu@3900 538 template<class T, MEMFLAGS F> bool
zgu@3900 539 GenericTaskQueueSet<T, F>::steal_best_of_2(uint queue_num, int* seed, E& t) {
duke@435 540 if (_n > 2) {
ysr@976 541 uint k1 = queue_num;
zgu@3900 542 while (k1 == queue_num) k1 = TaskQueueSetSuper::randomParkAndMiller(seed) % _n;
ysr@976 543 uint k2 = queue_num;
zgu@3900 544 while (k2 == queue_num || k2 == k1) k2 = TaskQueueSetSuper::randomParkAndMiller(seed) % _n;
duke@435 545 // Sample both and try the larger.
ysr@976 546 uint sz1 = _queues[k1]->size();
ysr@976 547 uint sz2 = _queues[k2]->size();
duke@435 548 if (sz2 > sz1) return _queues[k2]->pop_global(t);
duke@435 549 else return _queues[k1]->pop_global(t);
duke@435 550 } else if (_n == 2) {
duke@435 551 // Just try the other one.
ysr@976 552 uint k = (queue_num + 1) % 2;
duke@435 553 return _queues[k]->pop_global(t);
duke@435 554 } else {
duke@435 555 assert(_n == 1, "can't be zero.");
duke@435 556 return false;
duke@435 557 }
duke@435 558 }
duke@435 559
zgu@3900 560 template<class T, MEMFLAGS F>
zgu@3900 561 bool GenericTaskQueueSet<T, F>::peek() {
duke@435 562 // Try all the queues.
ysr@976 563 for (uint j = 0; j < _n; j++) {
duke@435 564 if (_queues[j]->peek())
duke@435 565 return true;
duke@435 566 }
duke@435 567 return false;
duke@435 568 }
duke@435 569
ysr@777 570 // When to terminate from the termination protocol.
zgu@3900 571 class TerminatorTerminator: public CHeapObj<mtInternal> {
ysr@777 572 public:
ysr@777 573 virtual bool should_exit_termination() = 0;
ysr@777 574 };
ysr@777 575
duke@435 576 // A class to aid in the termination of a set of parallel tasks using
duke@435 577 // TaskQueueSet's for work stealing.
duke@435 578
jmasa@981 579 #undef TRACESPINNING
jmasa@981 580
duke@435 581 class ParallelTaskTerminator: public StackObj {
duke@435 582 private:
duke@435 583 int _n_threads;
duke@435 584 TaskQueueSetSuper* _queue_set;
ysr@976 585 int _offered_termination;
duke@435 586
jmasa@981 587 #ifdef TRACESPINNING
jmasa@981 588 static uint _total_yields;
jmasa@981 589 static uint _total_spins;
jmasa@981 590 static uint _total_peeks;
jmasa@981 591 #endif
jmasa@981 592
duke@435 593 bool peek_in_queue_set();
duke@435 594 protected:
duke@435 595 virtual void yield();
duke@435 596 void sleep(uint millis);
duke@435 597
duke@435 598 public:
duke@435 599
duke@435 600 // "n_threads" is the number of threads to be terminated. "queue_set" is a
duke@435 601 // queue sets of work queues of other threads.
duke@435 602 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set);
duke@435 603
duke@435 604 // The current thread has no work, and is ready to terminate if everyone
duke@435 605 // else is. If returns "true", all threads are terminated. If returns
duke@435 606 // "false", available work has been observed in one of the task queues,
duke@435 607 // so the global task is not complete.
ysr@777 608 bool offer_termination() {
ysr@777 609 return offer_termination(NULL);
ysr@777 610 }
ysr@777 611
jcoomes@1342 612 // As above, but it also terminates if the should_exit_termination()
ysr@777 613 // method of the terminator parameter returns true. If terminator is
ysr@777 614 // NULL, then it is ignored.
ysr@777 615 bool offer_termination(TerminatorTerminator* terminator);
duke@435 616
duke@435 617 // Reset the terminator, so that it may be reused again.
duke@435 618 // The caller is responsible for ensuring that this is done
duke@435 619 // in an MT-safe manner, once the previous round of use of
duke@435 620 // the terminator is finished.
duke@435 621 void reset_for_reuse();
jmasa@2188 622 // Same as above but the number of parallel threads is set to the
jmasa@2188 623 // given number.
jmasa@2188 624 void reset_for_reuse(int n_threads);
duke@435 625
jmasa@981 626 #ifdef TRACESPINNING
jmasa@981 627 static uint total_yields() { return _total_yields; }
jmasa@981 628 static uint total_spins() { return _total_spins; }
jmasa@981 629 static uint total_peeks() { return _total_peeks; }
jmasa@981 630 static void print_termination_counts();
jmasa@981 631 #endif
duke@435 632 };
duke@435 633
zgu@3900 634 template<class E, MEMFLAGS F, unsigned int N> inline bool
zgu@3900 635 GenericTaskQueue<E, F, N>::push(E t) {
ysr@976 636 uint localBot = _bottom;
jcoomes@1342 637 assert((localBot >= 0) && (localBot < N), "_bottom out of range.");
jcoomes@1342 638 idx_t top = _age.top();
ysr@976 639 uint dirty_n_elems = dirty_size(localBot, top);
jcoomes@1746 640 assert(dirty_n_elems < N, "n_elems out of range.");
duke@435 641 if (dirty_n_elems < max_elems()) {
jcoomes@1746 642 // g++ complains if the volatile result of the assignment is unused.
jcoomes@1746 643 const_cast<E&>(_elems[localBot] = t);
bobv@1459 644 OrderAccess::release_store(&_bottom, increment_index(localBot));
jcoomes@2020 645 TASKQUEUE_STATS_ONLY(stats.record_push());
duke@435 646 return true;
duke@435 647 } else {
duke@435 648 return push_slow(t, dirty_n_elems);
duke@435 649 }
duke@435 650 }
duke@435 651
zgu@3900 652 template<class E, MEMFLAGS F, unsigned int N> inline bool
zgu@3900 653 GenericTaskQueue<E, F, N>::pop_local(E& t) {
ysr@976 654 uint localBot = _bottom;
jcoomes@1342 655 // This value cannot be N-1. That can only occur as a result of
duke@435 656 // the assignment to bottom in this method. If it does, this method
jcoomes@1993 657 // resets the size to 0 before the next call (which is sequential,
duke@435 658 // since this is pop_local.)
jcoomes@1342 659 uint dirty_n_elems = dirty_size(localBot, _age.top());
jcoomes@1342 660 assert(dirty_n_elems != N - 1, "Shouldn't be possible...");
duke@435 661 if (dirty_n_elems == 0) return false;
duke@435 662 localBot = decrement_index(localBot);
duke@435 663 _bottom = localBot;
duke@435 664 // This is necessary to prevent any read below from being reordered
duke@435 665 // before the store just above.
duke@435 666 OrderAccess::fence();
jcoomes@1746 667 const_cast<E&>(t = _elems[localBot]);
duke@435 668 // This is a second read of "age"; the "size()" above is the first.
duke@435 669 // If there's still at least one element in the queue, based on the
duke@435 670 // "_bottom" and "age" we've read, then there can be no interference with
duke@435 671 // a "pop_global" operation, and we're done.
jcoomes@1342 672 idx_t tp = _age.top(); // XXX
duke@435 673 if (size(localBot, tp) > 0) {
jcoomes@1342 674 assert(dirty_size(localBot, tp) != N - 1, "sanity");
jcoomes@2020 675 TASKQUEUE_STATS_ONLY(stats.record_pop());
duke@435 676 return true;
duke@435 677 } else {
duke@435 678 // Otherwise, the queue contained exactly one element; we take the slow
duke@435 679 // path.
jcoomes@1342 680 return pop_local_slow(localBot, _age.get());
duke@435 681 }
duke@435 682 }
duke@435 683
zgu@3900 684 typedef GenericTaskQueue<oop, mtGC> OopTaskQueue;
zgu@3900 685 typedef GenericTaskQueueSet<OopTaskQueue, mtGC> OopTaskQueueSet;
duke@435 686
jcoomes@1746 687 #ifdef _MSC_VER
jcoomes@1746 688 #pragma warning(push)
jcoomes@1746 689 // warning C4522: multiple assignment operators specified
jcoomes@1746 690 #pragma warning(disable:4522)
jcoomes@1746 691 #endif
coleenp@548 692
coleenp@548 693 // This is a container class for either an oop* or a narrowOop*.
coleenp@548 694 // Both are pushed onto a task queue and the consumer will test is_narrow()
coleenp@548 695 // to determine which should be processed.
coleenp@548 696 class StarTask {
coleenp@548 697 void* _holder; // either union oop* or narrowOop*
jcoomes@1746 698
jcoomes@1746 699 enum { COMPRESSED_OOP_MASK = 1 };
jcoomes@1746 700
coleenp@548 701 public:
ysr@1280 702 StarTask(narrowOop* p) {
ysr@1280 703 assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!");
ysr@1280 704 _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK);
ysr@1280 705 }
ysr@1280 706 StarTask(oop* p) {
ysr@1280 707 assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!");
ysr@1280 708 _holder = (void*)p;
ysr@1280 709 }
coleenp@548 710 StarTask() { _holder = NULL; }
coleenp@548 711 operator oop*() { return (oop*)_holder; }
coleenp@548 712 operator narrowOop*() {
coleenp@548 713 return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK);
coleenp@548 714 }
coleenp@548 715
jcoomes@1746 716 StarTask& operator=(const StarTask& t) {
jcoomes@1746 717 _holder = t._holder;
jcoomes@1746 718 return *this;
jcoomes@1746 719 }
jcoomes@1746 720 volatile StarTask& operator=(const volatile StarTask& t) volatile {
jcoomes@1746 721 _holder = t._holder;
jcoomes@1746 722 return *this;
jcoomes@1746 723 }
coleenp@548 724
coleenp@548 725 bool is_narrow() const {
coleenp@548 726 return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0);
coleenp@548 727 }
coleenp@548 728 };
coleenp@548 729
jcoomes@1746 730 class ObjArrayTask
jcoomes@1746 731 {
jcoomes@1746 732 public:
jcoomes@1746 733 ObjArrayTask(oop o = NULL, int idx = 0): _obj(o), _index(idx) { }
jcoomes@1746 734 ObjArrayTask(oop o, size_t idx): _obj(o), _index(int(idx)) {
jcoomes@1746 735 assert(idx <= size_t(max_jint), "too big");
jcoomes@1746 736 }
jcoomes@1746 737 ObjArrayTask(const ObjArrayTask& t): _obj(t._obj), _index(t._index) { }
jcoomes@1746 738
jcoomes@1746 739 ObjArrayTask& operator =(const ObjArrayTask& t) {
jcoomes@1746 740 _obj = t._obj;
jcoomes@1746 741 _index = t._index;
jcoomes@1746 742 return *this;
jcoomes@1746 743 }
jcoomes@1746 744 volatile ObjArrayTask&
jcoomes@1746 745 operator =(const volatile ObjArrayTask& t) volatile {
jcoomes@1746 746 _obj = t._obj;
jcoomes@1746 747 _index = t._index;
jcoomes@1746 748 return *this;
jcoomes@1746 749 }
jcoomes@1746 750
jcoomes@1746 751 inline oop obj() const { return _obj; }
jcoomes@1746 752 inline int index() const { return _index; }
jcoomes@1746 753
jcoomes@1746 754 DEBUG_ONLY(bool is_valid() const); // Tasks to be pushed/popped must be valid.
jcoomes@1746 755
jcoomes@1746 756 private:
jcoomes@1746 757 oop _obj;
jcoomes@1746 758 int _index;
jcoomes@1746 759 };
jcoomes@1746 760
jcoomes@1746 761 #ifdef _MSC_VER
jcoomes@1746 762 #pragma warning(pop)
jcoomes@1746 763 #endif
jcoomes@1746 764
zgu@3900 765 typedef OverflowTaskQueue<StarTask, mtClass> OopStarTaskQueue;
zgu@3900 766 typedef GenericTaskQueueSet<OopStarTaskQueue, mtClass> OopStarTaskQueueSet;
duke@435 767
zgu@3900 768 typedef OverflowTaskQueue<size_t, mtInternal> RegionTaskQueue;
zgu@3900 769 typedef GenericTaskQueueSet<RegionTaskQueue, mtClass> RegionTaskQueueSet;
jmasa@2188 770
stefank@2314 771
stefank@2314 772 #endif // SHARE_VM_UTILITIES_TASKQUEUE_HPP

mercurial