src/share/vm/utilities/taskqueue.hpp

Tue, 05 Nov 2013 17:38:04 -0800

author
kvn
date
Tue, 05 Nov 2013 17:38:04 -0800
changeset 6472
2b8e28fdf503
parent 6462
e2722a66aba7
parent 5784
190899198332
child 6876
710a3c8b516e
child 6911
ce8f6bb717c9
permissions
-rw-r--r--

Merge

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

mercurial