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