src/share/vm/utilities/taskqueue.hpp

Sat, 11 Dec 2010 13:20:56 -0500

author
zgu
date
Sat, 11 Dec 2010 13:20:56 -0500
changeset 2364
2d4762ec74af
parent 2314
f95d63e2154a
child 2508
b92c45f2bc75
permissions
-rw-r--r--

7003748: Decode C stack frames when symbols are presented (PhoneHome project)
Summary: Implemented in-process C native stack frame decoding when symbols are available.
Reviewed-by: coleenp, never

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

mercurial