Thu, 11 Feb 2010 15:52:19 -0800
6923991: G1: improve scalability of RSet scanning
Summary: Implemented block-based work stealing. Moved copying during the rset scanning phase to the main copying phase. Made the size of rset table depend on the region size.
Reviewed-by: apetrusenko, tonyp
duke@435 | 1 | /* |
xdono@1014 | 2 | * Copyright 2001-2009 Sun Microsystems, Inc. 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 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | class TaskQueueSuper: public CHeapObj { |
duke@435 | 26 | protected: |
jcoomes@1342 | 27 | // Internal type for indexing the queue; also used for the tag. |
jcoomes@1342 | 28 | typedef NOT_LP64(uint16_t) LP64_ONLY(uint32_t) idx_t; |
jcoomes@1342 | 29 | |
jcoomes@1342 | 30 | // The first free element after the last one pushed (mod N). |
ysr@976 | 31 | volatile uint _bottom; |
duke@435 | 32 | |
jcoomes@1342 | 33 | enum { |
jcoomes@1342 | 34 | N = 1 << NOT_LP64(14) LP64_ONLY(17), // Queue size: 16K or 128K |
jcoomes@1342 | 35 | MOD_N_MASK = N - 1 // To compute x mod N efficiently. |
duke@435 | 36 | }; |
duke@435 | 37 | |
jcoomes@1342 | 38 | class Age { |
jcoomes@1342 | 39 | public: |
jcoomes@1342 | 40 | Age(size_t data = 0) { _data = data; } |
jcoomes@1342 | 41 | Age(const Age& age) { _data = age._data; } |
jcoomes@1342 | 42 | Age(idx_t top, idx_t tag) { _fields._top = top; _fields._tag = tag; } |
duke@435 | 43 | |
jcoomes@1342 | 44 | Age get() const volatile { return _data; } |
jcoomes@1342 | 45 | void set(Age age) volatile { _data = age._data; } |
duke@435 | 46 | |
jcoomes@1342 | 47 | idx_t top() const volatile { return _fields._top; } |
jcoomes@1342 | 48 | idx_t tag() const volatile { return _fields._tag; } |
duke@435 | 49 | |
jcoomes@1342 | 50 | // Increment top; if it wraps, increment tag also. |
jcoomes@1342 | 51 | void increment() { |
jcoomes@1342 | 52 | _fields._top = increment_index(_fields._top); |
jcoomes@1342 | 53 | if (_fields._top == 0) ++_fields._tag; |
jcoomes@1342 | 54 | } |
duke@435 | 55 | |
jcoomes@1342 | 56 | Age cmpxchg(const Age new_age, const Age old_age) volatile { |
jcoomes@1342 | 57 | return (size_t) Atomic::cmpxchg_ptr((intptr_t)new_age._data, |
jcoomes@1342 | 58 | (volatile intptr_t *)&_data, |
jcoomes@1342 | 59 | (intptr_t)old_age._data); |
duke@435 | 60 | } |
jcoomes@1342 | 61 | |
jcoomes@1342 | 62 | bool operator ==(const Age& other) const { return _data == other._data; } |
jcoomes@1342 | 63 | |
jcoomes@1342 | 64 | private: |
jcoomes@1342 | 65 | struct fields { |
jcoomes@1342 | 66 | idx_t _top; |
jcoomes@1342 | 67 | idx_t _tag; |
jcoomes@1342 | 68 | }; |
jcoomes@1342 | 69 | union { |
jcoomes@1342 | 70 | size_t _data; |
jcoomes@1342 | 71 | fields _fields; |
jcoomes@1342 | 72 | }; |
duke@435 | 73 | }; |
jcoomes@1342 | 74 | |
jcoomes@1342 | 75 | volatile Age _age; |
jcoomes@1342 | 76 | |
jcoomes@1342 | 77 | // These both operate mod N. |
jcoomes@1342 | 78 | static uint increment_index(uint ind) { |
jcoomes@1342 | 79 | return (ind + 1) & MOD_N_MASK; |
duke@435 | 80 | } |
jcoomes@1342 | 81 | static uint decrement_index(uint ind) { |
jcoomes@1342 | 82 | return (ind - 1) & MOD_N_MASK; |
duke@435 | 83 | } |
duke@435 | 84 | |
jcoomes@1342 | 85 | // Returns a number in the range [0..N). If the result is "N-1", it should be |
jcoomes@1342 | 86 | // interpreted as 0. |
ysr@976 | 87 | uint dirty_size(uint bot, uint top) { |
jcoomes@1342 | 88 | return (bot - top) & MOD_N_MASK; |
duke@435 | 89 | } |
duke@435 | 90 | |
duke@435 | 91 | // Returns the size corresponding to the given "bot" and "top". |
ysr@976 | 92 | uint size(uint bot, uint top) { |
ysr@976 | 93 | uint sz = dirty_size(bot, top); |
jcoomes@1342 | 94 | // Has the queue "wrapped", so that bottom is less than top? There's a |
jcoomes@1342 | 95 | // complicated special case here. A pair of threads could perform pop_local |
jcoomes@1342 | 96 | // and pop_global operations concurrently, starting from a state in which |
jcoomes@1342 | 97 | // _bottom == _top+1. The pop_local could succeed in decrementing _bottom, |
jcoomes@1342 | 98 | // and the pop_global in incrementing _top (in which case the pop_global |
jcoomes@1342 | 99 | // will be awarded the contested queue element.) The resulting state must |
jcoomes@1342 | 100 | // be interpreted as an empty queue. (We only need to worry about one such |
jcoomes@1342 | 101 | // event: only the queue owner performs pop_local's, and several concurrent |
jcoomes@1342 | 102 | // threads attempting to perform the pop_global will all perform the same |
jcoomes@1342 | 103 | // CAS, and only one can succeed.) Any stealing thread that reads after |
jcoomes@1342 | 104 | // either the increment or decrement will see an empty queue, and will not |
jcoomes@1342 | 105 | // join the competitors. The "sz == -1 || sz == N-1" state will not be |
jcoomes@1342 | 106 | // modified by concurrent queues, so the owner thread can reset the state to |
jcoomes@1342 | 107 | // _bottom == top so subsequent pushes will be performed normally. |
jcoomes@1342 | 108 | return (sz == N - 1) ? 0 : sz; |
duke@435 | 109 | } |
duke@435 | 110 | |
duke@435 | 111 | public: |
duke@435 | 112 | TaskQueueSuper() : _bottom(0), _age() {} |
duke@435 | 113 | |
duke@435 | 114 | // Return "true" if the TaskQueue contains any tasks. |
duke@435 | 115 | bool peek(); |
duke@435 | 116 | |
duke@435 | 117 | // Return an estimate of the number of elements in the queue. |
duke@435 | 118 | // The "careful" version admits the possibility of pop_local/pop_global |
duke@435 | 119 | // races. |
ysr@976 | 120 | uint size() { |
jcoomes@1342 | 121 | return size(_bottom, _age.top()); |
duke@435 | 122 | } |
duke@435 | 123 | |
ysr@976 | 124 | uint dirty_size() { |
jcoomes@1342 | 125 | return dirty_size(_bottom, _age.top()); |
duke@435 | 126 | } |
duke@435 | 127 | |
ysr@777 | 128 | void set_empty() { |
ysr@777 | 129 | _bottom = 0; |
jcoomes@1342 | 130 | _age.set(0); |
ysr@777 | 131 | } |
ysr@777 | 132 | |
duke@435 | 133 | // Maximum number of elements allowed in the queue. This is two less |
duke@435 | 134 | // than the actual queue size, for somewhat complicated reasons. |
jcoomes@1342 | 135 | uint max_elems() { return N - 2; } |
duke@435 | 136 | }; |
duke@435 | 137 | |
duke@435 | 138 | template<class E> class GenericTaskQueue: public TaskQueueSuper { |
duke@435 | 139 | private: |
duke@435 | 140 | // Slow paths for push, pop_local. (pop_global has no fast path.) |
ysr@976 | 141 | bool push_slow(E t, uint dirty_n_elems); |
ysr@976 | 142 | bool pop_local_slow(uint localBot, Age oldAge); |
duke@435 | 143 | |
duke@435 | 144 | public: |
duke@435 | 145 | // Initializes the queue to empty. |
duke@435 | 146 | GenericTaskQueue(); |
duke@435 | 147 | |
duke@435 | 148 | void initialize(); |
duke@435 | 149 | |
duke@435 | 150 | // Push the task "t" on the queue. Returns "false" iff the queue is |
duke@435 | 151 | // full. |
duke@435 | 152 | inline bool push(E t); |
duke@435 | 153 | |
duke@435 | 154 | // If succeeds in claiming a task (from the 'local' end, that is, the |
duke@435 | 155 | // most recently pushed task), returns "true" and sets "t" to that task. |
duke@435 | 156 | // Otherwise, the queue is empty and returns false. |
duke@435 | 157 | inline bool pop_local(E& t); |
duke@435 | 158 | |
duke@435 | 159 | // If succeeds in claiming a task (from the 'global' end, that is, the |
duke@435 | 160 | // least recently pushed task), returns "true" and sets "t" to that task. |
duke@435 | 161 | // Otherwise, the queue is empty and returns false. |
duke@435 | 162 | bool pop_global(E& t); |
duke@435 | 163 | |
duke@435 | 164 | // Delete any resource associated with the queue. |
duke@435 | 165 | ~GenericTaskQueue(); |
duke@435 | 166 | |
ysr@777 | 167 | // apply the closure to all elements in the task queue |
ysr@777 | 168 | void oops_do(OopClosure* f); |
ysr@777 | 169 | |
duke@435 | 170 | private: |
duke@435 | 171 | // Element array. |
duke@435 | 172 | volatile E* _elems; |
duke@435 | 173 | }; |
duke@435 | 174 | |
duke@435 | 175 | template<class E> |
duke@435 | 176 | GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() { |
jcoomes@1342 | 177 | assert(sizeof(Age) == sizeof(size_t), "Depends on this."); |
duke@435 | 178 | } |
duke@435 | 179 | |
duke@435 | 180 | template<class E> |
duke@435 | 181 | void GenericTaskQueue<E>::initialize() { |
jcoomes@1342 | 182 | _elems = NEW_C_HEAP_ARRAY(E, N); |
duke@435 | 183 | guarantee(_elems != NULL, "Allocation failed."); |
duke@435 | 184 | } |
duke@435 | 185 | |
duke@435 | 186 | template<class E> |
ysr@777 | 187 | void GenericTaskQueue<E>::oops_do(OopClosure* f) { |
ysr@777 | 188 | // tty->print_cr("START OopTaskQueue::oops_do"); |
ysr@976 | 189 | uint iters = size(); |
ysr@976 | 190 | uint index = _bottom; |
ysr@976 | 191 | for (uint i = 0; i < iters; ++i) { |
ysr@777 | 192 | index = decrement_index(index); |
ysr@777 | 193 | // tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T, |
ysr@777 | 194 | // index, &_elems[index], _elems[index]); |
ysr@777 | 195 | E* t = (E*)&_elems[index]; // cast away volatility |
ysr@777 | 196 | oop* p = (oop*)t; |
ysr@777 | 197 | assert((*t)->is_oop_or_null(), "Not an oop or null"); |
ysr@777 | 198 | f->do_oop(p); |
ysr@777 | 199 | } |
ysr@777 | 200 | // tty->print_cr("END OopTaskQueue::oops_do"); |
ysr@777 | 201 | } |
ysr@777 | 202 | |
ysr@777 | 203 | |
ysr@777 | 204 | template<class E> |
ysr@976 | 205 | bool GenericTaskQueue<E>::push_slow(E t, uint dirty_n_elems) { |
jcoomes@1342 | 206 | if (dirty_n_elems == N - 1) { |
duke@435 | 207 | // Actually means 0, so do the push. |
ysr@976 | 208 | uint localBot = _bottom; |
duke@435 | 209 | _elems[localBot] = t; |
bobv@1459 | 210 | OrderAccess::release_store(&_bottom, increment_index(localBot)); |
duke@435 | 211 | return true; |
jcoomes@1342 | 212 | } |
jcoomes@1342 | 213 | return false; |
duke@435 | 214 | } |
duke@435 | 215 | |
duke@435 | 216 | template<class E> |
duke@435 | 217 | bool GenericTaskQueue<E>:: |
ysr@976 | 218 | pop_local_slow(uint localBot, Age oldAge) { |
duke@435 | 219 | // This queue was observed to contain exactly one element; either this |
duke@435 | 220 | // thread will claim it, or a competing "pop_global". In either case, |
duke@435 | 221 | // the queue will be logically empty afterwards. Create a new Age value |
duke@435 | 222 | // that represents the empty queue for the given value of "_bottom". (We |
duke@435 | 223 | // must also increment "tag" because of the case where "bottom == 1", |
duke@435 | 224 | // "top == 0". A pop_global could read the queue element in that case, |
duke@435 | 225 | // then have the owner thread do a pop followed by another push. Without |
duke@435 | 226 | // the incrementing of "tag", the pop_global's CAS could succeed, |
duke@435 | 227 | // allowing it to believe it has claimed the stale element.) |
jcoomes@1342 | 228 | Age newAge((idx_t)localBot, oldAge.tag() + 1); |
duke@435 | 229 | // Perhaps a competing pop_global has already incremented "top", in which |
duke@435 | 230 | // case it wins the element. |
duke@435 | 231 | if (localBot == oldAge.top()) { |
duke@435 | 232 | // No competing pop_global has yet incremented "top"; we'll try to |
duke@435 | 233 | // install new_age, thus claiming the element. |
jcoomes@1342 | 234 | Age tempAge = _age.cmpxchg(newAge, oldAge); |
duke@435 | 235 | if (tempAge == oldAge) { |
duke@435 | 236 | // We win. |
jcoomes@1342 | 237 | assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); |
duke@435 | 238 | return true; |
duke@435 | 239 | } |
duke@435 | 240 | } |
jcoomes@1342 | 241 | // We lose; a completing pop_global gets the element. But the queue is empty |
jcoomes@1342 | 242 | // and top is greater than bottom. Fix this representation of the empty queue |
jcoomes@1342 | 243 | // to become the canonical one. |
jcoomes@1342 | 244 | _age.set(newAge); |
jcoomes@1342 | 245 | assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); |
duke@435 | 246 | return false; |
duke@435 | 247 | } |
duke@435 | 248 | |
duke@435 | 249 | template<class E> |
duke@435 | 250 | bool GenericTaskQueue<E>::pop_global(E& t) { |
jcoomes@1342 | 251 | Age oldAge = _age.get(); |
ysr@976 | 252 | uint localBot = _bottom; |
ysr@976 | 253 | uint n_elems = size(localBot, oldAge.top()); |
duke@435 | 254 | if (n_elems == 0) { |
duke@435 | 255 | return false; |
duke@435 | 256 | } |
jcoomes@1342 | 257 | |
duke@435 | 258 | t = _elems[oldAge.top()]; |
jcoomes@1342 | 259 | Age newAge(oldAge); |
jcoomes@1342 | 260 | newAge.increment(); |
jcoomes@1342 | 261 | Age resAge = _age.cmpxchg(newAge, oldAge); |
jcoomes@1342 | 262 | |
duke@435 | 263 | // Note that using "_bottom" here might fail, since a pop_local might |
duke@435 | 264 | // have decremented it. |
jcoomes@1342 | 265 | assert(dirty_size(localBot, newAge.top()) != N - 1, "sanity"); |
jcoomes@1342 | 266 | return resAge == oldAge; |
duke@435 | 267 | } |
duke@435 | 268 | |
duke@435 | 269 | template<class E> |
duke@435 | 270 | GenericTaskQueue<E>::~GenericTaskQueue() { |
duke@435 | 271 | FREE_C_HEAP_ARRAY(E, _elems); |
duke@435 | 272 | } |
duke@435 | 273 | |
duke@435 | 274 | // Inherits the typedef of "Task" from above. |
duke@435 | 275 | class TaskQueueSetSuper: public CHeapObj { |
duke@435 | 276 | protected: |
duke@435 | 277 | static int randomParkAndMiller(int* seed0); |
duke@435 | 278 | public: |
duke@435 | 279 | // Returns "true" if some TaskQueue in the set contains a task. |
duke@435 | 280 | virtual bool peek() = 0; |
duke@435 | 281 | }; |
duke@435 | 282 | |
duke@435 | 283 | template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper { |
duke@435 | 284 | private: |
ysr@976 | 285 | uint _n; |
duke@435 | 286 | GenericTaskQueue<E>** _queues; |
duke@435 | 287 | |
duke@435 | 288 | public: |
duke@435 | 289 | GenericTaskQueueSet(int n) : _n(n) { |
duke@435 | 290 | typedef GenericTaskQueue<E>* GenericTaskQueuePtr; |
duke@435 | 291 | _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n); |
duke@435 | 292 | guarantee(_queues != NULL, "Allocation failure."); |
duke@435 | 293 | for (int i = 0; i < n; i++) { |
duke@435 | 294 | _queues[i] = NULL; |
duke@435 | 295 | } |
duke@435 | 296 | } |
duke@435 | 297 | |
ysr@976 | 298 | bool steal_1_random(uint queue_num, int* seed, E& t); |
ysr@976 | 299 | bool steal_best_of_2(uint queue_num, int* seed, E& t); |
ysr@976 | 300 | bool steal_best_of_all(uint queue_num, int* seed, E& t); |
duke@435 | 301 | |
ysr@976 | 302 | void register_queue(uint i, GenericTaskQueue<E>* q); |
duke@435 | 303 | |
ysr@976 | 304 | GenericTaskQueue<E>* queue(uint n); |
duke@435 | 305 | |
duke@435 | 306 | // The thread with queue number "queue_num" (and whose random number seed |
duke@435 | 307 | // is at "seed") is trying to steal a task from some other queue. (It |
duke@435 | 308 | // may try several queues, according to some configuration parameter.) |
duke@435 | 309 | // If some steal succeeds, returns "true" and sets "t" the stolen task, |
duke@435 | 310 | // otherwise returns false. |
ysr@976 | 311 | bool steal(uint queue_num, int* seed, E& t); |
duke@435 | 312 | |
duke@435 | 313 | bool peek(); |
duke@435 | 314 | }; |
duke@435 | 315 | |
duke@435 | 316 | template<class E> |
ysr@976 | 317 | void GenericTaskQueueSet<E>::register_queue(uint i, GenericTaskQueue<E>* q) { |
ysr@976 | 318 | assert(i < _n, "index out of range."); |
duke@435 | 319 | _queues[i] = q; |
duke@435 | 320 | } |
duke@435 | 321 | |
duke@435 | 322 | template<class E> |
ysr@976 | 323 | GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(uint i) { |
duke@435 | 324 | return _queues[i]; |
duke@435 | 325 | } |
duke@435 | 326 | |
duke@435 | 327 | template<class E> |
ysr@976 | 328 | bool GenericTaskQueueSet<E>::steal(uint queue_num, int* seed, E& t) { |
ysr@976 | 329 | for (uint i = 0; i < 2 * _n; i++) |
duke@435 | 330 | if (steal_best_of_2(queue_num, seed, t)) |
duke@435 | 331 | return true; |
duke@435 | 332 | return false; |
duke@435 | 333 | } |
duke@435 | 334 | |
duke@435 | 335 | template<class E> |
ysr@976 | 336 | bool GenericTaskQueueSet<E>::steal_best_of_all(uint queue_num, int* seed, E& t) { |
duke@435 | 337 | if (_n > 2) { |
duke@435 | 338 | int best_k; |
ysr@976 | 339 | uint best_sz = 0; |
ysr@976 | 340 | for (uint k = 0; k < _n; k++) { |
duke@435 | 341 | if (k == queue_num) continue; |
ysr@976 | 342 | uint sz = _queues[k]->size(); |
duke@435 | 343 | if (sz > best_sz) { |
duke@435 | 344 | best_sz = sz; |
duke@435 | 345 | best_k = k; |
duke@435 | 346 | } |
duke@435 | 347 | } |
duke@435 | 348 | return best_sz > 0 && _queues[best_k]->pop_global(t); |
duke@435 | 349 | } else if (_n == 2) { |
duke@435 | 350 | // Just try the other one. |
duke@435 | 351 | int k = (queue_num + 1) % 2; |
duke@435 | 352 | return _queues[k]->pop_global(t); |
duke@435 | 353 | } else { |
duke@435 | 354 | assert(_n == 1, "can't be zero."); |
duke@435 | 355 | return false; |
duke@435 | 356 | } |
duke@435 | 357 | } |
duke@435 | 358 | |
duke@435 | 359 | template<class E> |
ysr@976 | 360 | bool GenericTaskQueueSet<E>::steal_1_random(uint queue_num, int* seed, E& t) { |
duke@435 | 361 | if (_n > 2) { |
ysr@976 | 362 | uint k = queue_num; |
duke@435 | 363 | while (k == queue_num) k = randomParkAndMiller(seed) % _n; |
duke@435 | 364 | return _queues[2]->pop_global(t); |
duke@435 | 365 | } else if (_n == 2) { |
duke@435 | 366 | // Just try the other one. |
duke@435 | 367 | int k = (queue_num + 1) % 2; |
duke@435 | 368 | return _queues[k]->pop_global(t); |
duke@435 | 369 | } else { |
duke@435 | 370 | assert(_n == 1, "can't be zero."); |
duke@435 | 371 | return false; |
duke@435 | 372 | } |
duke@435 | 373 | } |
duke@435 | 374 | |
duke@435 | 375 | template<class E> |
ysr@976 | 376 | bool GenericTaskQueueSet<E>::steal_best_of_2(uint queue_num, int* seed, E& t) { |
duke@435 | 377 | if (_n > 2) { |
ysr@976 | 378 | uint k1 = queue_num; |
duke@435 | 379 | while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n; |
ysr@976 | 380 | uint k2 = queue_num; |
duke@435 | 381 | while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n; |
duke@435 | 382 | // Sample both and try the larger. |
ysr@976 | 383 | uint sz1 = _queues[k1]->size(); |
ysr@976 | 384 | uint sz2 = _queues[k2]->size(); |
duke@435 | 385 | if (sz2 > sz1) return _queues[k2]->pop_global(t); |
duke@435 | 386 | else return _queues[k1]->pop_global(t); |
duke@435 | 387 | } else if (_n == 2) { |
duke@435 | 388 | // Just try the other one. |
ysr@976 | 389 | uint k = (queue_num + 1) % 2; |
duke@435 | 390 | return _queues[k]->pop_global(t); |
duke@435 | 391 | } else { |
duke@435 | 392 | assert(_n == 1, "can't be zero."); |
duke@435 | 393 | return false; |
duke@435 | 394 | } |
duke@435 | 395 | } |
duke@435 | 396 | |
duke@435 | 397 | template<class E> |
duke@435 | 398 | bool GenericTaskQueueSet<E>::peek() { |
duke@435 | 399 | // Try all the queues. |
ysr@976 | 400 | for (uint j = 0; j < _n; j++) { |
duke@435 | 401 | if (_queues[j]->peek()) |
duke@435 | 402 | return true; |
duke@435 | 403 | } |
duke@435 | 404 | return false; |
duke@435 | 405 | } |
duke@435 | 406 | |
ysr@777 | 407 | // When to terminate from the termination protocol. |
ysr@777 | 408 | class TerminatorTerminator: public CHeapObj { |
ysr@777 | 409 | public: |
ysr@777 | 410 | virtual bool should_exit_termination() = 0; |
ysr@777 | 411 | }; |
ysr@777 | 412 | |
duke@435 | 413 | // A class to aid in the termination of a set of parallel tasks using |
duke@435 | 414 | // TaskQueueSet's for work stealing. |
duke@435 | 415 | |
jmasa@981 | 416 | #undef TRACESPINNING |
jmasa@981 | 417 | |
duke@435 | 418 | class ParallelTaskTerminator: public StackObj { |
duke@435 | 419 | private: |
duke@435 | 420 | int _n_threads; |
duke@435 | 421 | TaskQueueSetSuper* _queue_set; |
ysr@976 | 422 | int _offered_termination; |
duke@435 | 423 | |
jmasa@981 | 424 | #ifdef TRACESPINNING |
jmasa@981 | 425 | static uint _total_yields; |
jmasa@981 | 426 | static uint _total_spins; |
jmasa@981 | 427 | static uint _total_peeks; |
jmasa@981 | 428 | #endif |
jmasa@981 | 429 | |
duke@435 | 430 | bool peek_in_queue_set(); |
duke@435 | 431 | protected: |
duke@435 | 432 | virtual void yield(); |
duke@435 | 433 | void sleep(uint millis); |
duke@435 | 434 | |
duke@435 | 435 | public: |
duke@435 | 436 | |
duke@435 | 437 | // "n_threads" is the number of threads to be terminated. "queue_set" is a |
duke@435 | 438 | // queue sets of work queues of other threads. |
duke@435 | 439 | ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set); |
duke@435 | 440 | |
duke@435 | 441 | // The current thread has no work, and is ready to terminate if everyone |
duke@435 | 442 | // else is. If returns "true", all threads are terminated. If returns |
duke@435 | 443 | // "false", available work has been observed in one of the task queues, |
duke@435 | 444 | // so the global task is not complete. |
ysr@777 | 445 | bool offer_termination() { |
ysr@777 | 446 | return offer_termination(NULL); |
ysr@777 | 447 | } |
ysr@777 | 448 | |
jcoomes@1342 | 449 | // As above, but it also terminates if the should_exit_termination() |
ysr@777 | 450 | // method of the terminator parameter returns true. If terminator is |
ysr@777 | 451 | // NULL, then it is ignored. |
ysr@777 | 452 | bool offer_termination(TerminatorTerminator* terminator); |
duke@435 | 453 | |
duke@435 | 454 | // Reset the terminator, so that it may be reused again. |
duke@435 | 455 | // The caller is responsible for ensuring that this is done |
duke@435 | 456 | // in an MT-safe manner, once the previous round of use of |
duke@435 | 457 | // the terminator is finished. |
duke@435 | 458 | void reset_for_reuse(); |
duke@435 | 459 | |
jmasa@981 | 460 | #ifdef TRACESPINNING |
jmasa@981 | 461 | static uint total_yields() { return _total_yields; } |
jmasa@981 | 462 | static uint total_spins() { return _total_spins; } |
jmasa@981 | 463 | static uint total_peeks() { return _total_peeks; } |
jmasa@981 | 464 | static void print_termination_counts(); |
jmasa@981 | 465 | #endif |
duke@435 | 466 | }; |
duke@435 | 467 | |
duke@435 | 468 | template<class E> inline bool GenericTaskQueue<E>::push(E t) { |
ysr@976 | 469 | uint localBot = _bottom; |
jcoomes@1342 | 470 | assert((localBot >= 0) && (localBot < N), "_bottom out of range."); |
jcoomes@1342 | 471 | idx_t top = _age.top(); |
ysr@976 | 472 | uint dirty_n_elems = dirty_size(localBot, top); |
jcoomes@1342 | 473 | assert((dirty_n_elems >= 0) && (dirty_n_elems < N), "n_elems out of range."); |
duke@435 | 474 | if (dirty_n_elems < max_elems()) { |
duke@435 | 475 | _elems[localBot] = t; |
bobv@1459 | 476 | OrderAccess::release_store(&_bottom, increment_index(localBot)); |
duke@435 | 477 | return true; |
duke@435 | 478 | } else { |
duke@435 | 479 | return push_slow(t, dirty_n_elems); |
duke@435 | 480 | } |
duke@435 | 481 | } |
duke@435 | 482 | |
duke@435 | 483 | template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) { |
ysr@976 | 484 | uint localBot = _bottom; |
jcoomes@1342 | 485 | // This value cannot be N-1. That can only occur as a result of |
duke@435 | 486 | // the assignment to bottom in this method. If it does, this method |
duke@435 | 487 | // resets the size( to 0 before the next call (which is sequential, |
duke@435 | 488 | // since this is pop_local.) |
jcoomes@1342 | 489 | uint dirty_n_elems = dirty_size(localBot, _age.top()); |
jcoomes@1342 | 490 | assert(dirty_n_elems != N - 1, "Shouldn't be possible..."); |
duke@435 | 491 | if (dirty_n_elems == 0) return false; |
duke@435 | 492 | localBot = decrement_index(localBot); |
duke@435 | 493 | _bottom = localBot; |
duke@435 | 494 | // This is necessary to prevent any read below from being reordered |
duke@435 | 495 | // before the store just above. |
duke@435 | 496 | OrderAccess::fence(); |
duke@435 | 497 | t = _elems[localBot]; |
duke@435 | 498 | // This is a second read of "age"; the "size()" above is the first. |
duke@435 | 499 | // If there's still at least one element in the queue, based on the |
duke@435 | 500 | // "_bottom" and "age" we've read, then there can be no interference with |
duke@435 | 501 | // a "pop_global" operation, and we're done. |
jcoomes@1342 | 502 | idx_t tp = _age.top(); // XXX |
duke@435 | 503 | if (size(localBot, tp) > 0) { |
jcoomes@1342 | 504 | assert(dirty_size(localBot, tp) != N - 1, "sanity"); |
duke@435 | 505 | return true; |
duke@435 | 506 | } else { |
duke@435 | 507 | // Otherwise, the queue contained exactly one element; we take the slow |
duke@435 | 508 | // path. |
jcoomes@1342 | 509 | return pop_local_slow(localBot, _age.get()); |
duke@435 | 510 | } |
duke@435 | 511 | } |
duke@435 | 512 | |
duke@435 | 513 | typedef oop Task; |
duke@435 | 514 | typedef GenericTaskQueue<Task> OopTaskQueue; |
duke@435 | 515 | typedef GenericTaskQueueSet<Task> OopTaskQueueSet; |
duke@435 | 516 | |
coleenp@548 | 517 | |
coleenp@548 | 518 | #define COMPRESSED_OOP_MASK 1 |
coleenp@548 | 519 | |
coleenp@548 | 520 | // This is a container class for either an oop* or a narrowOop*. |
coleenp@548 | 521 | // Both are pushed onto a task queue and the consumer will test is_narrow() |
coleenp@548 | 522 | // to determine which should be processed. |
coleenp@548 | 523 | class StarTask { |
coleenp@548 | 524 | void* _holder; // either union oop* or narrowOop* |
coleenp@548 | 525 | public: |
ysr@1280 | 526 | StarTask(narrowOop* p) { |
ysr@1280 | 527 | assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); |
ysr@1280 | 528 | _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); |
ysr@1280 | 529 | } |
ysr@1280 | 530 | StarTask(oop* p) { |
ysr@1280 | 531 | assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); |
ysr@1280 | 532 | _holder = (void*)p; |
ysr@1280 | 533 | } |
coleenp@548 | 534 | StarTask() { _holder = NULL; } |
coleenp@548 | 535 | operator oop*() { return (oop*)_holder; } |
coleenp@548 | 536 | operator narrowOop*() { |
coleenp@548 | 537 | return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK); |
coleenp@548 | 538 | } |
coleenp@548 | 539 | |
coleenp@548 | 540 | // Operators to preserve const/volatile in assignments required by gcc |
coleenp@548 | 541 | void operator=(const volatile StarTask& t) volatile { _holder = t._holder; } |
coleenp@548 | 542 | |
coleenp@548 | 543 | bool is_narrow() const { |
coleenp@548 | 544 | return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0); |
coleenp@548 | 545 | } |
coleenp@548 | 546 | }; |
coleenp@548 | 547 | |
duke@435 | 548 | typedef GenericTaskQueue<StarTask> OopStarTaskQueue; |
duke@435 | 549 | typedef GenericTaskQueueSet<StarTask> OopStarTaskQueueSet; |
duke@435 | 550 | |
jcoomes@810 | 551 | typedef size_t RegionTask; // index for region |
jcoomes@810 | 552 | typedef GenericTaskQueue<RegionTask> RegionTaskQueue; |
jcoomes@810 | 553 | typedef GenericTaskQueueSet<RegionTask> RegionTaskQueueSet; |
duke@435 | 554 | |
jcoomes@810 | 555 | class RegionTaskQueueWithOverflow: public CHeapObj { |
duke@435 | 556 | protected: |
jcoomes@810 | 557 | RegionTaskQueue _region_queue; |
jcoomes@810 | 558 | GrowableArray<RegionTask>* _overflow_stack; |
duke@435 | 559 | |
duke@435 | 560 | public: |
jcoomes@810 | 561 | RegionTaskQueueWithOverflow() : _overflow_stack(NULL) {} |
duke@435 | 562 | // Initialize both stealable queue and overflow |
duke@435 | 563 | void initialize(); |
duke@435 | 564 | // Save first to stealable queue and then to overflow |
jcoomes@810 | 565 | void save(RegionTask t); |
duke@435 | 566 | // Retrieve first from overflow and then from stealable queue |
jcoomes@810 | 567 | bool retrieve(RegionTask& region_index); |
duke@435 | 568 | // Retrieve from stealable queue |
jcoomes@810 | 569 | bool retrieve_from_stealable_queue(RegionTask& region_index); |
duke@435 | 570 | // Retrieve from overflow |
jcoomes@810 | 571 | bool retrieve_from_overflow(RegionTask& region_index); |
duke@435 | 572 | bool is_empty(); |
duke@435 | 573 | bool stealable_is_empty(); |
duke@435 | 574 | bool overflow_is_empty(); |
ysr@976 | 575 | uint stealable_size() { return _region_queue.size(); } |
jcoomes@810 | 576 | RegionTaskQueue* task_queue() { return &_region_queue; } |
duke@435 | 577 | }; |
duke@435 | 578 | |
jcoomes@810 | 579 | #define USE_RegionTaskQueueWithOverflow |