src/share/vm/gc_implementation/parallelScavenge/gcTaskManager.hpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 25
873fd82b133d
permissions
-rw-r--r--

Added MIPS 64-bit port.

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@1 25 /*
aoqi@1 26 * This file has been modified by Loongson Technology in 2015. These
aoqi@1 27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
aoqi@1 28 * available on the same license terms set forth above.
aoqi@1 29 */
aoqi@1 30
aoqi@0 31 #ifndef SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP
aoqi@0 32 #define SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP
aoqi@0 33
aoqi@0 34 #include "runtime/mutex.hpp"
aoqi@0 35 #include "utilities/growableArray.hpp"
aoqi@0 36
aoqi@0 37 //
aoqi@0 38 // The GCTaskManager is a queue of GCTasks, and accessors
aoqi@0 39 // to allow the queue to be accessed from many threads.
aoqi@0 40 //
aoqi@0 41
aoqi@0 42 // Forward declarations of types defined in this file.
aoqi@0 43 class GCTask;
aoqi@0 44 class GCTaskQueue;
aoqi@0 45 class SynchronizedGCTaskQueue;
aoqi@0 46 class GCTaskManager;
aoqi@0 47 class NotifyDoneClosure;
aoqi@0 48 // Some useful subclasses of GCTask. You can also make up your own.
aoqi@0 49 class NoopGCTask;
aoqi@0 50 class BarrierGCTask;
aoqi@0 51 class ReleasingBarrierGCTask;
aoqi@0 52 class NotifyingBarrierGCTask;
aoqi@0 53 class WaitForBarrierGCTask;
aoqi@0 54 class IdleGCTask;
aoqi@0 55 // A free list of Monitor*'s.
aoqi@0 56 class MonitorSupply;
aoqi@0 57
aoqi@0 58 // Forward declarations of classes referenced in this file via pointer.
aoqi@0 59 class GCTaskThread;
aoqi@0 60 class Mutex;
aoqi@0 61 class Monitor;
aoqi@0 62 class ThreadClosure;
aoqi@0 63
aoqi@0 64 // The abstract base GCTask.
aoqi@0 65 class GCTask : public ResourceObj {
aoqi@0 66 public:
aoqi@0 67 // Known kinds of GCTasks, for predicates.
aoqi@0 68 class Kind : AllStatic {
aoqi@0 69 public:
aoqi@0 70 enum kind {
aoqi@0 71 unknown_task,
aoqi@0 72 ordinary_task,
aoqi@0 73 barrier_task,
aoqi@0 74 noop_task,
aoqi@0 75 idle_task
aoqi@0 76 };
aoqi@0 77 static const char* to_string(kind value);
aoqi@0 78 };
aoqi@0 79 private:
aoqi@0 80 // Instance state.
aoqi@0 81 const Kind::kind _kind; // For runtime type checking.
aoqi@0 82 const uint _affinity; // Which worker should run task.
aoqi@1 83 int _numa_id; //Which numa node should run task.
aoqi@0 84 GCTask* _newer; // Tasks are on doubly-linked ...
aoqi@0 85 GCTask* _older; // ... lists.
aoqi@0 86 public:
aoqi@0 87 virtual char* name() { return (char *)"task"; }
aoqi@0 88
aoqi@0 89 // Abstract do_it method
aoqi@0 90 virtual void do_it(GCTaskManager* manager, uint which) = 0;
aoqi@0 91 // Accessors
aoqi@0 92 Kind::kind kind() const {
aoqi@0 93 return _kind;
aoqi@0 94 }
aoqi@0 95 uint affinity() const {
aoqi@0 96 return _affinity;
aoqi@0 97 }
aoqi@1 98 uint set_task_numa_id(int id) {
aoqi@1 99 _numa_id = id;
aoqi@1 100 }
aoqi@1 101 int task_numa_id() {
aoqi@1 102 return _numa_id;
aoqi@1 103 }
aoqi@0 104 GCTask* newer() const {
aoqi@0 105 return _newer;
aoqi@0 106 }
aoqi@0 107 void set_newer(GCTask* n) {
aoqi@0 108 _newer = n;
aoqi@0 109 }
aoqi@0 110 GCTask* older() const {
aoqi@0 111 return _older;
aoqi@0 112 }
aoqi@0 113 void set_older(GCTask* p) {
aoqi@0 114 _older = p;
aoqi@0 115 }
aoqi@0 116 // Predicates.
aoqi@0 117 bool is_ordinary_task() const {
aoqi@0 118 return kind()==Kind::ordinary_task;
aoqi@0 119 }
aoqi@0 120 bool is_barrier_task() const {
aoqi@0 121 return kind()==Kind::barrier_task;
aoqi@0 122 }
aoqi@0 123 bool is_noop_task() const {
aoqi@0 124 return kind()==Kind::noop_task;
aoqi@0 125 }
aoqi@0 126 bool is_idle_task() const {
aoqi@0 127 return kind()==Kind::idle_task;
aoqi@0 128 }
aoqi@0 129 void print(const char* message) const PRODUCT_RETURN;
aoqi@0 130 protected:
aoqi@0 131 // Constructors: Only create subclasses.
aoqi@0 132 // An ordinary GCTask.
aoqi@0 133 GCTask();
aoqi@0 134 // A GCTask of a particular kind, usually barrier or noop.
aoqi@0 135 GCTask(Kind::kind kind);
aoqi@0 136 // An ordinary GCTask with an affinity.
aoqi@0 137 GCTask(uint affinity);
aoqi@0 138 // A GCTask of a particular kind, with and affinity.
aoqi@0 139 GCTask(Kind::kind kind, uint affinity);
aoqi@0 140 // We want a virtual destructor because virtual methods,
aoqi@0 141 // but since ResourceObj's don't have their destructors
aoqi@0 142 // called, we don't have one at all. Instead we have
aoqi@0 143 // this method, which gets called by subclasses to clean up.
aoqi@0 144 virtual void destruct();
aoqi@0 145 // Methods.
aoqi@0 146 void initialize();
aoqi@0 147 };
aoqi@0 148
aoqi@0 149 // A doubly-linked list of GCTasks.
aoqi@0 150 // The list is not synchronized, because sometimes we want to
aoqi@0 151 // build up a list and then make it available to other threads.
aoqi@0 152 // See also: SynchronizedGCTaskQueue.
aoqi@0 153 class GCTaskQueue : public ResourceObj {
aoqi@0 154 private:
aoqi@0 155 // Instance state.
aoqi@0 156 GCTask* _insert_end; // Tasks are enqueued at this end.
aoqi@0 157 GCTask* _remove_end; // Tasks are dequeued from this end.
aoqi@0 158 uint _length; // The current length of the queue.
aoqi@0 159 const bool _is_c_heap_obj; // Is this a CHeapObj?
aoqi@0 160 public:
aoqi@0 161 // Factory create and destroy methods.
aoqi@0 162 // Create as ResourceObj.
aoqi@0 163 static GCTaskQueue* create();
aoqi@0 164 // Create as CHeapObj.
aoqi@0 165 static GCTaskQueue* create_on_c_heap();
aoqi@0 166 // Destroyer.
aoqi@0 167 static void destroy(GCTaskQueue* that);
aoqi@0 168 // Accessors.
aoqi@0 169 // These just examine the state of the queue.
aoqi@0 170 bool is_empty() const {
aoqi@0 171 assert(((insert_end() == NULL && remove_end() == NULL) ||
aoqi@0 172 (insert_end() != NULL && remove_end() != NULL)),
aoqi@0 173 "insert_end and remove_end don't match");
aoqi@0 174 assert((insert_end() != NULL) || (_length == 0), "Not empty");
aoqi@0 175 return insert_end() == NULL;
aoqi@0 176 }
aoqi@0 177 uint length() const {
aoqi@0 178 return _length;
aoqi@0 179 }
aoqi@0 180 // Methods.
aoqi@0 181 // Enqueue one task.
aoqi@0 182 void enqueue(GCTask* task);
aoqi@0 183 // Enqueue a list of tasks. Empties the argument list.
aoqi@0 184 void enqueue(GCTaskQueue* list);
aoqi@0 185 // Dequeue one task.
aoqi@0 186 GCTask* dequeue();
aoqi@0 187 // Dequeue one task, preferring one with affinity.
aoqi@0 188 GCTask* dequeue(uint affinity);
aoqi@1 189 // Dequeue one task, preferring on with numa_aware.
aoqi@1 190 GCTask* numa_dequeue(int numa_id);
aoqi@0 191 protected:
aoqi@0 192 // Constructor. Clients use factory, but there might be subclasses.
aoqi@0 193 GCTaskQueue(bool on_c_heap);
aoqi@0 194 // Destructor-like method.
aoqi@0 195 // Because ResourceMark doesn't call destructors.
aoqi@0 196 // This method cleans up like one.
aoqi@0 197 virtual void destruct();
aoqi@0 198 // Accessors.
aoqi@0 199 GCTask* insert_end() const {
aoqi@0 200 return _insert_end;
aoqi@0 201 }
aoqi@0 202 void set_insert_end(GCTask* value) {
aoqi@0 203 _insert_end = value;
aoqi@0 204 }
aoqi@0 205 GCTask* remove_end() const {
aoqi@0 206 return _remove_end;
aoqi@0 207 }
aoqi@0 208 void set_remove_end(GCTask* value) {
aoqi@0 209 _remove_end = value;
aoqi@0 210 }
aoqi@0 211 void increment_length() {
aoqi@0 212 _length += 1;
aoqi@0 213 }
aoqi@0 214 void decrement_length() {
aoqi@0 215 _length -= 1;
aoqi@0 216 }
aoqi@0 217 void set_length(uint value) {
aoqi@0 218 _length = value;
aoqi@0 219 }
aoqi@0 220 bool is_c_heap_obj() const {
aoqi@0 221 return _is_c_heap_obj;
aoqi@0 222 }
aoqi@0 223 // Methods.
aoqi@0 224 void initialize();
aoqi@0 225 GCTask* remove(); // Remove from remove end.
aoqi@0 226 GCTask* remove(GCTask* task); // Remove from the middle.
aoqi@0 227 void print(const char* message) const PRODUCT_RETURN;
aoqi@0 228 // Debug support
aoqi@0 229 void verify_length() const PRODUCT_RETURN;
aoqi@0 230 };
aoqi@0 231
aoqi@0 232 // A GCTaskQueue that can be synchronized.
aoqi@0 233 // This "has-a" GCTaskQueue and a mutex to do the exclusion.
aoqi@0 234 class SynchronizedGCTaskQueue : public CHeapObj<mtGC> {
aoqi@0 235 private:
aoqi@0 236 // Instance state.
aoqi@0 237 GCTaskQueue* _unsynchronized_queue; // Has-a unsynchronized queue.
aoqi@0 238 Monitor * _lock; // Lock to control access.
aoqi@0 239 public:
aoqi@0 240 // Factory create and destroy methods.
aoqi@0 241 static SynchronizedGCTaskQueue* create(GCTaskQueue* queue, Monitor * lock) {
aoqi@0 242 return new SynchronizedGCTaskQueue(queue, lock);
aoqi@0 243 }
aoqi@0 244 static void destroy(SynchronizedGCTaskQueue* that) {
aoqi@0 245 if (that != NULL) {
aoqi@0 246 delete that;
aoqi@0 247 }
aoqi@0 248 }
aoqi@0 249 // Accessors
aoqi@0 250 GCTaskQueue* unsynchronized_queue() const {
aoqi@0 251 return _unsynchronized_queue;
aoqi@0 252 }
aoqi@0 253 Monitor * lock() const {
aoqi@0 254 return _lock;
aoqi@0 255 }
aoqi@0 256 // GCTaskQueue wrapper methods.
aoqi@0 257 // These check that you hold the lock
aoqi@0 258 // and then call the method on the queue.
aoqi@0 259 bool is_empty() const {
aoqi@0 260 guarantee(own_lock(), "don't own the lock");
aoqi@0 261 return unsynchronized_queue()->is_empty();
aoqi@0 262 }
aoqi@0 263 void enqueue(GCTask* task) {
aoqi@0 264 guarantee(own_lock(), "don't own the lock");
aoqi@0 265 unsynchronized_queue()->enqueue(task);
aoqi@0 266 }
aoqi@0 267 void enqueue(GCTaskQueue* list) {
aoqi@0 268 guarantee(own_lock(), "don't own the lock");
aoqi@0 269 unsynchronized_queue()->enqueue(list);
aoqi@0 270 }
aoqi@0 271 GCTask* dequeue() {
aoqi@0 272 guarantee(own_lock(), "don't own the lock");
aoqi@0 273 return unsynchronized_queue()->dequeue();
aoqi@0 274 }
aoqi@0 275 GCTask* dequeue(uint affinity) {
aoqi@0 276 guarantee(own_lock(), "don't own the lock");
aoqi@0 277 return unsynchronized_queue()->dequeue(affinity);
aoqi@0 278 }
aoqi@1 279 GCTask* numa_dequeue(int numa_id) {
aoqi@1 280 guarantee(own_lock(), "don't own the lock");
aoqi@1 281 return unsynchronized_queue()->numa_dequeue(numa_id);
aoqi@1 282 }
aoqi@0 283 uint length() const {
aoqi@0 284 guarantee(own_lock(), "don't own the lock");
aoqi@0 285 return unsynchronized_queue()->length();
aoqi@0 286 }
aoqi@0 287 // For guarantees.
aoqi@0 288 bool own_lock() const {
aoqi@0 289 return lock()->owned_by_self();
aoqi@0 290 }
aoqi@0 291 protected:
aoqi@0 292 // Constructor. Clients use factory, but there might be subclasses.
aoqi@0 293 SynchronizedGCTaskQueue(GCTaskQueue* queue, Monitor * lock);
aoqi@0 294 // Destructor. Not virtual because no virtuals.
aoqi@0 295 ~SynchronizedGCTaskQueue();
aoqi@0 296 };
aoqi@0 297
aoqi@0 298 // This is an abstract base class for getting notifications
aoqi@0 299 // when a GCTaskManager is done.
aoqi@0 300 class NotifyDoneClosure : public CHeapObj<mtGC> {
aoqi@0 301 public:
aoqi@0 302 // The notification callback method.
aoqi@0 303 virtual void notify(GCTaskManager* manager) = 0;
aoqi@0 304 protected:
aoqi@0 305 // Constructor.
aoqi@0 306 NotifyDoneClosure() {
aoqi@0 307 // Nothing to do.
aoqi@0 308 }
aoqi@0 309 // Virtual destructor because virtual methods.
aoqi@0 310 virtual ~NotifyDoneClosure() {
aoqi@0 311 // Nothing to do.
aoqi@0 312 }
aoqi@0 313 };
aoqi@0 314
aoqi@0 315 // Dynamic number of GC threads
aoqi@0 316 //
aoqi@0 317 // GC threads wait in get_task() for work (i.e., a task) to perform.
aoqi@0 318 // When the number of GC threads was static, the number of tasks
aoqi@0 319 // created to do a job was equal to or greater than the maximum
aoqi@0 320 // number of GC threads (ParallelGCThreads). The job might be divided
aoqi@0 321 // into a number of tasks greater than the number of GC threads for
aoqi@0 322 // load balancing (i.e., over partitioning). The last task to be
aoqi@0 323 // executed by a GC thread in a job is a work stealing task. A
aoqi@0 324 // GC thread that gets a work stealing task continues to execute
aoqi@0 325 // that task until the job is done. In the static number of GC theads
aoqi@0 326 // case, tasks are added to a queue (FIFO). The work stealing tasks are
aoqi@0 327 // the last to be added. Once the tasks are added, the GC threads grab
aoqi@0 328 // a task and go. A single thread can do all the non-work stealing tasks
aoqi@0 329 // and then execute a work stealing and wait for all the other GC threads
aoqi@0 330 // to execute their work stealing task.
aoqi@0 331 // In the dynamic number of GC threads implementation, idle-tasks are
aoqi@0 332 // created to occupy the non-participating or "inactive" threads. An
aoqi@0 333 // idle-task makes the GC thread wait on a barrier that is part of the
aoqi@0 334 // GCTaskManager. The GC threads that have been "idled" in a IdleGCTask
aoqi@0 335 // are released once all the active GC threads have finished their work
aoqi@0 336 // stealing tasks. The GCTaskManager does not wait for all the "idled"
aoqi@0 337 // GC threads to resume execution. When those GC threads do resume
aoqi@0 338 // execution in the course of the thread scheduling, they call get_tasks()
aoqi@0 339 // as all the other GC threads do. Because all the "idled" threads are
aoqi@0 340 // not required to execute in order to finish a job, it is possible for
aoqi@0 341 // a GC thread to still be "idled" when the next job is started. Such
aoqi@0 342 // a thread stays "idled" for the next job. This can result in a new
aoqi@0 343 // job not having all the expected active workers. For example if on
aoqi@0 344 // job requests 4 active workers out of a total of 10 workers so the
aoqi@0 345 // remaining 6 are "idled", if the next job requests 6 active workers
aoqi@0 346 // but all 6 of the "idled" workers are still idle, then the next job
aoqi@0 347 // will only get 4 active workers.
aoqi@0 348 // The implementation for the parallel old compaction phase has an
aoqi@0 349 // added complication. In the static case parold partitions the chunks
aoqi@0 350 // ready to be filled into stacks, one for each GC thread. A GC thread
aoqi@0 351 // executing a draining task (drains the stack of ready chunks)
aoqi@0 352 // claims a stack according to it's id (the unique ordinal value assigned
aoqi@0 353 // to each GC thread). In the dynamic case not all GC threads will
aoqi@0 354 // actively participate so stacks with ready to fill chunks can only be
aoqi@0 355 // given to the active threads. An initial implementation chose stacks
aoqi@0 356 // number 1-n to get the ready chunks and required that GC threads
aoqi@0 357 // 1-n be the active workers. This was undesirable because it required
aoqi@0 358 // certain threads to participate. In the final implementation a
aoqi@0 359 // list of stacks equal in number to the active workers are filled
aoqi@0 360 // with ready chunks. GC threads that participate get a stack from
aoqi@0 361 // the task (DrainStacksCompactionTask), empty the stack, and then add it to a
aoqi@0 362 // recycling list at the end of the task. If the same GC thread gets
aoqi@0 363 // a second task, it gets a second stack to drain and returns it. The
aoqi@0 364 // stacks are added to a recycling list so that later stealing tasks
aoqi@0 365 // for this tasks can get a stack from the recycling list. Stealing tasks
aoqi@0 366 // use the stacks in its work in a way similar to the draining tasks.
aoqi@0 367 // A thread is not guaranteed to get anything but a stealing task and
aoqi@0 368 // a thread that only gets a stealing task has to get a stack. A failed
aoqi@0 369 // implementation tried to have the GC threads keep the stack they used
aoqi@0 370 // during a draining task for later use in the stealing task but that didn't
aoqi@0 371 // work because as noted a thread is not guaranteed to get a draining task.
aoqi@0 372 //
aoqi@0 373 // For PSScavenge and ParCompactionManager the GC threads are
aoqi@0 374 // held in the GCTaskThread** _thread array in GCTaskManager.
aoqi@0 375
aoqi@0 376
aoqi@0 377 class GCTaskManager : public CHeapObj<mtGC> {
aoqi@0 378 friend class ParCompactionManager;
aoqi@0 379 friend class PSParallelCompact;
aoqi@0 380 friend class PSScavenge;
aoqi@0 381 friend class PSRefProcTaskExecutor;
aoqi@0 382 friend class RefProcTaskExecutor;
aoqi@0 383 friend class GCTaskThread;
aoqi@0 384 friend class IdleGCTask;
aoqi@0 385 private:
aoqi@0 386 // Instance state.
aoqi@0 387 NotifyDoneClosure* _ndc; // Notify on completion.
aoqi@0 388 const uint _workers; // Number of workers.
aoqi@0 389 Monitor* _monitor; // Notification of changes.
aoqi@0 390 SynchronizedGCTaskQueue* _queue; // Queue of tasks.
aoqi@0 391 GCTaskThread** _thread; // Array of worker threads.
aoqi@0 392 uint _active_workers; // Number of active workers.
aoqi@0 393 uint _busy_workers; // Number of busy workers.
aoqi@0 394 uint _blocking_worker; // The worker that's blocking.
aoqi@0 395 bool* _resource_flag; // Array of flag per threads.
aoqi@0 396 uint _delivered_tasks; // Count of delivered tasks.
aoqi@0 397 uint _completed_tasks; // Count of completed tasks.
aoqi@0 398 uint _barriers; // Count of barrier tasks.
aoqi@0 399 uint _emptied_queue; // Times we emptied the queue.
aoqi@0 400 NoopGCTask* _noop_task; // The NoopGCTask instance.
aoqi@0 401 uint _noop_tasks; // Count of noop tasks.
aoqi@0 402 WaitForBarrierGCTask* _idle_inactive_task;// Task for inactive workers
aoqi@0 403 volatile uint _idle_workers; // Number of idled workers
aoqi@0 404 public:
aoqi@0 405 // Factory create and destroy methods.
aoqi@0 406 static GCTaskManager* create(uint workers) {
aoqi@0 407 return new GCTaskManager(workers);
aoqi@0 408 }
aoqi@0 409 static GCTaskManager* create(uint workers, NotifyDoneClosure* ndc) {
aoqi@0 410 return new GCTaskManager(workers, ndc);
aoqi@0 411 }
aoqi@0 412 static void destroy(GCTaskManager* that) {
aoqi@0 413 if (that != NULL) {
aoqi@0 414 delete that;
aoqi@0 415 }
aoqi@0 416 }
aoqi@0 417 // Accessors.
aoqi@0 418 uint busy_workers() const {
aoqi@0 419 return _busy_workers;
aoqi@0 420 }
aoqi@0 421 volatile uint idle_workers() const {
aoqi@0 422 return _idle_workers;
aoqi@0 423 }
aoqi@0 424 // Pun between Monitor* and Mutex*
aoqi@0 425 Monitor* monitor() const {
aoqi@0 426 return _monitor;
aoqi@0 427 }
aoqi@0 428 Monitor * lock() const {
aoqi@0 429 return _monitor;
aoqi@0 430 }
aoqi@0 431 WaitForBarrierGCTask* idle_inactive_task() {
aoqi@0 432 return _idle_inactive_task;
aoqi@0 433 }
aoqi@0 434 // Methods.
aoqi@0 435 // Add the argument task to be run.
aoqi@0 436 void add_task(GCTask* task);
aoqi@0 437 // Add a list of tasks. Removes task from the argument list.
aoqi@0 438 void add_list(GCTaskQueue* list);
aoqi@0 439 // Claim a task for argument worker.
aoqi@0 440 GCTask* get_task(uint which);
aoqi@0 441 // Note the completion of a task by the argument worker.
aoqi@0 442 void note_completion(uint which);
aoqi@0 443 // Is the queue blocked from handing out new tasks?
aoqi@0 444 bool is_blocked() const {
aoqi@0 445 return (blocking_worker() != sentinel_worker());
aoqi@0 446 }
aoqi@0 447 // Request that all workers release their resources.
aoqi@0 448 void release_all_resources();
aoqi@0 449 // Ask if a particular worker should release its resources.
aoqi@0 450 bool should_release_resources(uint which); // Predicate.
aoqi@0 451 // Note the release of resources by the argument worker.
aoqi@0 452 void note_release(uint which);
aoqi@0 453 // Create IdleGCTasks for inactive workers and start workers
aoqi@0 454 void task_idle_workers();
aoqi@0 455 // Release the workers in IdleGCTasks
aoqi@0 456 void release_idle_workers();
aoqi@0 457 // Constants.
aoqi@0 458 // A sentinel worker identifier.
aoqi@0 459 static uint sentinel_worker() {
aoqi@0 460 return (uint) -1; // Why isn't there a max_uint?
aoqi@0 461 }
aoqi@0 462
aoqi@0 463 // Execute the task queue and wait for the completion.
aoqi@0 464 void execute_and_wait(GCTaskQueue* list);
aoqi@0 465
aoqi@0 466 void print_task_time_stamps();
aoqi@0 467 void print_threads_on(outputStream* st);
aoqi@0 468 void threads_do(ThreadClosure* tc);
aoqi@0 469
aoqi@0 470 protected:
aoqi@0 471 // Constructors. Clients use factory, but there might be subclasses.
aoqi@0 472 // Create a GCTaskManager with the appropriate number of workers.
aoqi@0 473 GCTaskManager(uint workers);
aoqi@0 474 // Create a GCTaskManager that calls back when there's no more work.
aoqi@0 475 GCTaskManager(uint workers, NotifyDoneClosure* ndc);
aoqi@0 476 // Make virtual if necessary.
aoqi@0 477 ~GCTaskManager();
aoqi@0 478 // Accessors.
aoqi@0 479 uint workers() const {
aoqi@0 480 return _workers;
aoqi@0 481 }
aoqi@0 482 void set_active_workers(uint v) {
aoqi@0 483 assert(v <= _workers, "Trying to set more workers active than there are");
aoqi@0 484 _active_workers = MIN2(v, _workers);
aoqi@0 485 assert(v != 0, "Trying to set active workers to 0");
aoqi@0 486 _active_workers = MAX2(1U, _active_workers);
aoqi@0 487 }
aoqi@0 488 // Sets the number of threads that will be used in a collection
aoqi@0 489 void set_active_gang();
aoqi@0 490
aoqi@0 491 NotifyDoneClosure* notify_done_closure() const {
aoqi@0 492 return _ndc;
aoqi@0 493 }
aoqi@0 494 SynchronizedGCTaskQueue* queue() const {
aoqi@0 495 return _queue;
aoqi@0 496 }
aoqi@0 497 NoopGCTask* noop_task() const {
aoqi@0 498 return _noop_task;
aoqi@0 499 }
aoqi@0 500 // Bounds-checking per-thread data accessors.
aoqi@0 501 GCTaskThread* thread(uint which);
aoqi@0 502 void set_thread(uint which, GCTaskThread* value);
aoqi@0 503 bool resource_flag(uint which);
aoqi@0 504 void set_resource_flag(uint which, bool value);
aoqi@0 505 // Modifier methods with some semantics.
aoqi@0 506 // Is any worker blocking handing out new tasks?
aoqi@0 507 uint blocking_worker() const {
aoqi@0 508 return _blocking_worker;
aoqi@0 509 }
aoqi@0 510 void set_blocking_worker(uint value) {
aoqi@0 511 _blocking_worker = value;
aoqi@0 512 }
aoqi@0 513 void set_unblocked() {
aoqi@0 514 set_blocking_worker(sentinel_worker());
aoqi@0 515 }
aoqi@0 516 // Count of busy workers.
aoqi@0 517 void reset_busy_workers() {
aoqi@0 518 _busy_workers = 0;
aoqi@0 519 }
aoqi@0 520 uint increment_busy_workers();
aoqi@0 521 uint decrement_busy_workers();
aoqi@0 522 // Count of tasks delivered to workers.
aoqi@0 523 uint delivered_tasks() const {
aoqi@0 524 return _delivered_tasks;
aoqi@0 525 }
aoqi@0 526 void increment_delivered_tasks() {
aoqi@0 527 _delivered_tasks += 1;
aoqi@0 528 }
aoqi@0 529 void reset_delivered_tasks() {
aoqi@0 530 _delivered_tasks = 0;
aoqi@0 531 }
aoqi@0 532 // Count of tasks completed by workers.
aoqi@0 533 uint completed_tasks() const {
aoqi@0 534 return _completed_tasks;
aoqi@0 535 }
aoqi@0 536 void increment_completed_tasks() {
aoqi@0 537 _completed_tasks += 1;
aoqi@0 538 }
aoqi@0 539 void reset_completed_tasks() {
aoqi@0 540 _completed_tasks = 0;
aoqi@0 541 }
aoqi@0 542 // Count of barrier tasks completed.
aoqi@0 543 uint barriers() const {
aoqi@0 544 return _barriers;
aoqi@0 545 }
aoqi@0 546 void increment_barriers() {
aoqi@0 547 _barriers += 1;
aoqi@0 548 }
aoqi@0 549 void reset_barriers() {
aoqi@0 550 _barriers = 0;
aoqi@0 551 }
aoqi@0 552 // Count of how many times the queue has emptied.
aoqi@0 553 uint emptied_queue() const {
aoqi@0 554 return _emptied_queue;
aoqi@0 555 }
aoqi@0 556 void increment_emptied_queue() {
aoqi@0 557 _emptied_queue += 1;
aoqi@0 558 }
aoqi@0 559 void reset_emptied_queue() {
aoqi@0 560 _emptied_queue = 0;
aoqi@0 561 }
aoqi@0 562 // Count of the number of noop tasks we've handed out,
aoqi@0 563 // e.g., to handle resource release requests.
aoqi@0 564 uint noop_tasks() const {
aoqi@0 565 return _noop_tasks;
aoqi@0 566 }
aoqi@0 567 void increment_noop_tasks() {
aoqi@0 568 _noop_tasks += 1;
aoqi@0 569 }
aoqi@0 570 void reset_noop_tasks() {
aoqi@0 571 _noop_tasks = 0;
aoqi@0 572 }
aoqi@0 573 void increment_idle_workers() {
aoqi@0 574 _idle_workers++;
aoqi@0 575 }
aoqi@0 576 void decrement_idle_workers() {
aoqi@0 577 _idle_workers--;
aoqi@0 578 }
aoqi@0 579 // Other methods.
aoqi@0 580 void initialize();
aoqi@0 581
aoqi@0 582 public:
aoqi@0 583 // Return true if all workers are currently active.
aoqi@0 584 bool all_workers_active() { return workers() == active_workers(); }
aoqi@0 585 uint active_workers() const {
aoqi@0 586 return _active_workers;
aoqi@0 587 }
aoqi@0 588 };
aoqi@0 589
aoqi@0 590 //
aoqi@0 591 // Some exemplary GCTasks.
aoqi@0 592 //
aoqi@0 593
aoqi@0 594 // A noop task that does nothing,
aoqi@0 595 // except take us around the GCTaskThread loop.
aoqi@0 596 class NoopGCTask : public GCTask {
aoqi@0 597 private:
aoqi@0 598 const bool _is_c_heap_obj; // Is this a CHeapObj?
aoqi@0 599 public:
aoqi@0 600 // Factory create and destroy methods.
aoqi@0 601 static NoopGCTask* create();
aoqi@0 602 static NoopGCTask* create_on_c_heap();
aoqi@0 603 static void destroy(NoopGCTask* that);
aoqi@0 604
aoqi@0 605 virtual char* name() { return (char *)"noop task"; }
aoqi@0 606 // Methods from GCTask.
aoqi@0 607 void do_it(GCTaskManager* manager, uint which) {
aoqi@0 608 // Nothing to do.
aoqi@0 609 }
aoqi@0 610 protected:
aoqi@0 611 // Constructor.
aoqi@0 612 NoopGCTask(bool on_c_heap) :
aoqi@0 613 GCTask(GCTask::Kind::noop_task),
aoqi@0 614 _is_c_heap_obj(on_c_heap) {
aoqi@0 615 // Nothing to do.
aoqi@0 616 }
aoqi@0 617 // Destructor-like method.
aoqi@0 618 void destruct();
aoqi@0 619 // Accessors.
aoqi@0 620 bool is_c_heap_obj() const {
aoqi@0 621 return _is_c_heap_obj;
aoqi@0 622 }
aoqi@0 623 };
aoqi@0 624
aoqi@0 625 // A BarrierGCTask blocks other tasks from starting,
aoqi@0 626 // and waits until it is the only task running.
aoqi@0 627 class BarrierGCTask : public GCTask {
aoqi@0 628 public:
aoqi@0 629 // Factory create and destroy methods.
aoqi@0 630 static BarrierGCTask* create() {
aoqi@0 631 return new BarrierGCTask();
aoqi@0 632 }
aoqi@0 633 static void destroy(BarrierGCTask* that) {
aoqi@0 634 if (that != NULL) {
aoqi@0 635 that->destruct();
aoqi@0 636 delete that;
aoqi@0 637 }
aoqi@0 638 }
aoqi@0 639 // Methods from GCTask.
aoqi@0 640 void do_it(GCTaskManager* manager, uint which);
aoqi@0 641 protected:
aoqi@0 642 // Constructor. Clients use factory, but there might be subclasses.
aoqi@0 643 BarrierGCTask() :
aoqi@0 644 GCTask(GCTask::Kind::barrier_task) {
aoqi@0 645 // Nothing to do.
aoqi@0 646 }
aoqi@0 647 // Destructor-like method.
aoqi@0 648 void destruct();
aoqi@0 649
aoqi@0 650 virtual char* name() { return (char *)"barrier task"; }
aoqi@0 651 // Methods.
aoqi@0 652 // Wait for this to be the only task running.
aoqi@0 653 void do_it_internal(GCTaskManager* manager, uint which);
aoqi@0 654 };
aoqi@0 655
aoqi@0 656 // A ReleasingBarrierGCTask is a BarrierGCTask
aoqi@0 657 // that tells all the tasks to release their resource areas.
aoqi@0 658 class ReleasingBarrierGCTask : public BarrierGCTask {
aoqi@0 659 public:
aoqi@0 660 // Factory create and destroy methods.
aoqi@0 661 static ReleasingBarrierGCTask* create() {
aoqi@0 662 return new ReleasingBarrierGCTask();
aoqi@0 663 }
aoqi@0 664 static void destroy(ReleasingBarrierGCTask* that) {
aoqi@0 665 if (that != NULL) {
aoqi@0 666 that->destruct();
aoqi@0 667 delete that;
aoqi@0 668 }
aoqi@0 669 }
aoqi@0 670 // Methods from GCTask.
aoqi@0 671 void do_it(GCTaskManager* manager, uint which);
aoqi@0 672 protected:
aoqi@0 673 // Constructor. Clients use factory, but there might be subclasses.
aoqi@0 674 ReleasingBarrierGCTask() :
aoqi@0 675 BarrierGCTask() {
aoqi@0 676 // Nothing to do.
aoqi@0 677 }
aoqi@0 678 // Destructor-like method.
aoqi@0 679 void destruct();
aoqi@0 680 };
aoqi@0 681
aoqi@0 682 // A NotifyingBarrierGCTask is a BarrierGCTask
aoqi@0 683 // that calls a notification method when it is the only task running.
aoqi@0 684 class NotifyingBarrierGCTask : public BarrierGCTask {
aoqi@0 685 private:
aoqi@0 686 // Instance state.
aoqi@0 687 NotifyDoneClosure* _ndc; // The callback object.
aoqi@0 688 public:
aoqi@0 689 // Factory create and destroy methods.
aoqi@0 690 static NotifyingBarrierGCTask* create(NotifyDoneClosure* ndc) {
aoqi@0 691 return new NotifyingBarrierGCTask(ndc);
aoqi@0 692 }
aoqi@0 693 static void destroy(NotifyingBarrierGCTask* that) {
aoqi@0 694 if (that != NULL) {
aoqi@0 695 that->destruct();
aoqi@0 696 delete that;
aoqi@0 697 }
aoqi@0 698 }
aoqi@0 699 // Methods from GCTask.
aoqi@0 700 void do_it(GCTaskManager* manager, uint which);
aoqi@0 701 protected:
aoqi@0 702 // Constructor. Clients use factory, but there might be subclasses.
aoqi@0 703 NotifyingBarrierGCTask(NotifyDoneClosure* ndc) :
aoqi@0 704 BarrierGCTask(),
aoqi@0 705 _ndc(ndc) {
aoqi@0 706 assert(notify_done_closure() != NULL, "can't notify on NULL");
aoqi@0 707 }
aoqi@0 708 // Destructor-like method.
aoqi@0 709 void destruct();
aoqi@0 710 // Accessor.
aoqi@0 711 NotifyDoneClosure* notify_done_closure() const { return _ndc; }
aoqi@0 712 };
aoqi@0 713
aoqi@0 714 // A WaitForBarrierGCTask is a BarrierGCTask
aoqi@0 715 // with a method you can call to wait until
aoqi@0 716 // the BarrierGCTask is done.
aoqi@0 717 // This may cover many of the uses of NotifyingBarrierGCTasks.
aoqi@0 718 class WaitForBarrierGCTask : public BarrierGCTask {
aoqi@0 719 friend class GCTaskManager;
aoqi@0 720 friend class IdleGCTask;
aoqi@0 721 private:
aoqi@0 722 // Instance state.
aoqi@0 723 Monitor* _monitor; // Guard and notify changes.
aoqi@0 724 volatile bool _should_wait; // true=>wait, false=>proceed.
aoqi@0 725 const bool _is_c_heap_obj; // Was allocated on the heap.
aoqi@0 726 public:
aoqi@0 727 virtual char* name() { return (char *) "waitfor-barrier-task"; }
aoqi@0 728
aoqi@0 729 // Factory create and destroy methods.
aoqi@0 730 static WaitForBarrierGCTask* create();
aoqi@0 731 static WaitForBarrierGCTask* create_on_c_heap();
aoqi@0 732 static void destroy(WaitForBarrierGCTask* that);
aoqi@0 733 // Methods.
aoqi@0 734 void do_it(GCTaskManager* manager, uint which);
aoqi@0 735 void wait_for(bool reset);
aoqi@0 736 void set_should_wait(bool value) {
aoqi@0 737 _should_wait = value;
aoqi@0 738 }
aoqi@0 739 protected:
aoqi@0 740 // Constructor. Clients use factory, but there might be subclasses.
aoqi@0 741 WaitForBarrierGCTask(bool on_c_heap);
aoqi@0 742 // Destructor-like method.
aoqi@0 743 void destruct();
aoqi@0 744 // Accessors.
aoqi@0 745 Monitor* monitor() const {
aoqi@0 746 return _monitor;
aoqi@0 747 }
aoqi@0 748 bool should_wait() const {
aoqi@0 749 return _should_wait;
aoqi@0 750 }
aoqi@0 751 bool is_c_heap_obj() {
aoqi@0 752 return _is_c_heap_obj;
aoqi@0 753 }
aoqi@0 754 };
aoqi@0 755
aoqi@0 756 // Task that is used to idle a GC task when fewer than
aoqi@0 757 // the maximum workers are wanted.
aoqi@0 758 class IdleGCTask : public GCTask {
aoqi@0 759 const bool _is_c_heap_obj; // Was allocated on the heap.
aoqi@0 760 public:
aoqi@0 761 bool is_c_heap_obj() {
aoqi@0 762 return _is_c_heap_obj;
aoqi@0 763 }
aoqi@0 764 // Factory create and destroy methods.
aoqi@0 765 static IdleGCTask* create();
aoqi@0 766 static IdleGCTask* create_on_c_heap();
aoqi@0 767 static void destroy(IdleGCTask* that);
aoqi@0 768
aoqi@0 769 virtual char* name() { return (char *)"idle task"; }
aoqi@0 770 // Methods from GCTask.
aoqi@0 771 virtual void do_it(GCTaskManager* manager, uint which);
aoqi@0 772 protected:
aoqi@0 773 // Constructor.
aoqi@0 774 IdleGCTask(bool on_c_heap) :
aoqi@0 775 GCTask(GCTask::Kind::idle_task),
aoqi@0 776 _is_c_heap_obj(on_c_heap) {
aoqi@0 777 // Nothing to do.
aoqi@0 778 }
aoqi@0 779 // Destructor-like method.
aoqi@0 780 void destruct();
aoqi@0 781 };
aoqi@0 782
aoqi@0 783 class MonitorSupply : public AllStatic {
aoqi@0 784 private:
aoqi@0 785 // State.
aoqi@0 786 // Control multi-threaded access.
aoqi@0 787 static Mutex* _lock;
aoqi@0 788 // The list of available Monitor*'s.
aoqi@0 789 static GrowableArray<Monitor*>* _freelist;
aoqi@0 790 public:
aoqi@0 791 // Reserve a Monitor*.
aoqi@0 792 static Monitor* reserve();
aoqi@0 793 // Release a Monitor*.
aoqi@0 794 static void release(Monitor* instance);
aoqi@0 795 private:
aoqi@0 796 // Accessors.
aoqi@0 797 static Mutex* lock() {
aoqi@0 798 return _lock;
aoqi@0 799 }
aoqi@0 800 static GrowableArray<Monitor*>* freelist() {
aoqi@0 801 return _freelist;
aoqi@0 802 }
aoqi@0 803 };
aoqi@0 804
aoqi@0 805 #endif // SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP

mercurial