annotate src/share/vm/utilities/workgroup.hpp @ 2796:f08d439fab8c

7089790: integrate bsd-port changes Reviewed-by: kvn, twisti, jrose Contributed-by: Kurt Miller <kurt@intricatesoftware.com>, Greg Lewis <glewis@eyesbeyond.com>, Jung-uk Kim <jkim@freebsd.org>, Christos Zoulas <christos@zoulas.com>, Landon Fuller <landonf@plausible.coop>, The FreeBSD Foundation <board@freebsdfoundation.org>, Michael Franz <mvfranz@gmail.com>, Roger Hoover <rhoover@apple.com>, Alexander Strange <astrange@apple.com>
author never
date Sun, 25 Sep 2011 16:03:29 -0700
parents 92da084fefc9
children bca17e38de00
rev   line source
duke@0 1 /*
ysr@2216 2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #ifndef SHARE_VM_UTILITIES_WORKGROUP_HPP
stefank@1879 26 #define SHARE_VM_UTILITIES_WORKGROUP_HPP
stefank@1879 27
stefank@1879 28 #include "utilities/taskqueue.hpp"
stefank@1879 29 #ifdef TARGET_OS_FAMILY_linux
stefank@1879 30 # include "thread_linux.inline.hpp"
stefank@1879 31 #endif
stefank@1879 32 #ifdef TARGET_OS_FAMILY_solaris
stefank@1879 33 # include "thread_solaris.inline.hpp"
stefank@1879 34 #endif
stefank@1879 35 #ifdef TARGET_OS_FAMILY_windows
stefank@1879 36 # include "thread_windows.inline.hpp"
stefank@1879 37 #endif
never@2796 38 #ifdef TARGET_OS_FAMILY_bsd
never@2796 39 # include "thread_bsd.inline.hpp"
never@2796 40 #endif
stefank@1879 41
ysr@2216 42 // Task class hierarchy:
ysr@2216 43 // AbstractGangTask
ysr@2216 44 // AbstractGangTaskWOopQueues
ysr@2216 45 //
ysr@2216 46 // Gang/Group class hierarchy:
ysr@2216 47 // AbstractWorkGang
ysr@2216 48 // WorkGang
ysr@2216 49 // FlexibleWorkGang
ysr@2216 50 // YieldingFlexibleWorkGang (defined in another file)
ysr@2216 51 //
ysr@2216 52 // Worker class hierarchy:
ysr@2216 53 // GangWorker (subclass of WorkerThread)
ysr@2216 54 // YieldingFlexibleGangWorker (defined in another file)
ysr@2216 55
duke@0 56 // Forward declarations of classes defined here
duke@0 57
duke@0 58 class WorkGang;
duke@0 59 class GangWorker;
duke@0 60 class YieldingFlexibleGangWorker;
duke@0 61 class YieldingFlexibleGangTask;
duke@0 62 class WorkData;
jmasa@1753 63 class AbstractWorkGang;
duke@0 64
duke@0 65 // An abstract task to be worked on by a gang.
duke@0 66 // You subclass this to supply your own work() method
apetrusenko@549 67 class AbstractGangTask VALUE_OBJ_CLASS_SPEC {
duke@0 68 public:
duke@0 69 // The abstract work method.
duke@0 70 // The argument tells you which member of the gang you are.
duke@0 71 virtual void work(int i) = 0;
duke@0 72
jmasa@1753 73 // This method configures the task for proper termination.
jmasa@1753 74 // Some tasks do not have any requirements on termination
jmasa@1753 75 // and may inherit this method that does nothing. Some
jmasa@1753 76 // tasks do some coordination on termination and override
jmasa@1753 77 // this method to implement that coordination.
jmasa@1753 78 virtual void set_for_termination(int active_workers) {};
jmasa@1753 79
duke@0 80 // Debugging accessor for the name.
duke@0 81 const char* name() const PRODUCT_RETURN_(return NULL;);
duke@0 82 int counter() { return _counter; }
duke@0 83 void set_counter(int value) { _counter = value; }
duke@0 84 int *address_of_counter() { return &_counter; }
duke@0 85
duke@0 86 // RTTI
duke@0 87 NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const {
duke@0 88 return false;
duke@0 89 })
duke@0 90
duke@0 91 private:
duke@0 92 NOT_PRODUCT(const char* _name;)
duke@0 93 // ??? Should a task have a priority associated with it?
duke@0 94 // ??? Or can the run method adjust priority as needed?
duke@0 95 int _counter;
duke@0 96
duke@0 97 protected:
duke@0 98 // Constructor and desctructor: only construct subclasses.
duke@0 99 AbstractGangTask(const char* name) {
duke@0 100 NOT_PRODUCT(_name = name);
duke@0 101 _counter = 0;
duke@0 102 }
duke@0 103 virtual ~AbstractGangTask() { }
duke@0 104 };
duke@0 105
jmasa@1753 106 class AbstractGangTaskWOopQueues : public AbstractGangTask {
jmasa@1753 107 OopTaskQueueSet* _queues;
jmasa@1753 108 ParallelTaskTerminator _terminator;
jmasa@1753 109 public:
jmasa@1753 110 AbstractGangTaskWOopQueues(const char* name, OopTaskQueueSet* queues) :
jmasa@1753 111 AbstractGangTask(name), _queues(queues), _terminator(0, _queues) {}
jmasa@1753 112 ParallelTaskTerminator* terminator() { return &_terminator; }
jmasa@1753 113 virtual void set_for_termination(int active_workers) {
jmasa@1753 114 terminator()->reset_for_reuse(active_workers);
jmasa@1753 115 }
jmasa@1753 116 OopTaskQueueSet* queues() { return _queues; }
jmasa@1753 117 };
duke@0 118
duke@0 119 // Class AbstractWorkGang:
duke@0 120 // An abstract class representing a gang of workers.
duke@0 121 // You subclass this to supply an implementation of run_task().
duke@0 122 class AbstractWorkGang: public CHeapObj {
duke@0 123 // Here's the public interface to this class.
duke@0 124 public:
duke@0 125 // Constructor and destructor.
ysr@342 126 AbstractWorkGang(const char* name, bool are_GC_task_threads,
ysr@342 127 bool are_ConcurrentGC_threads);
duke@0 128 ~AbstractWorkGang();
duke@0 129 // Run a task, returns when the task is done (or terminated).
duke@0 130 virtual void run_task(AbstractGangTask* task) = 0;
duke@0 131 // Stop and terminate all workers.
duke@0 132 virtual void stop();
duke@0 133 public:
duke@0 134 // Debugging.
duke@0 135 const char* name() const;
duke@0 136 protected:
duke@0 137 // Initialize only instance data.
ysr@342 138 const bool _are_GC_task_threads;
ysr@342 139 const bool _are_ConcurrentGC_threads;
duke@0 140 // Printing support.
duke@0 141 const char* _name;
duke@0 142 // The monitor which protects these data,
duke@0 143 // and notifies of changes in it.
duke@0 144 Monitor* _monitor;
duke@0 145 // The count of the number of workers in the gang.
duke@0 146 int _total_workers;
duke@0 147 // Whether the workers should terminate.
duke@0 148 bool _terminate;
duke@0 149 // The array of worker threads for this gang.
duke@0 150 // This is only needed for cleaning up.
duke@0 151 GangWorker** _gang_workers;
duke@0 152 // The task for this gang.
duke@0 153 AbstractGangTask* _task;
duke@0 154 // A sequence number for the current task.
duke@0 155 int _sequence_number;
duke@0 156 // The number of started workers.
duke@0 157 int _started_workers;
duke@0 158 // The number of finished workers.
duke@0 159 int _finished_workers;
duke@0 160 public:
duke@0 161 // Accessors for fields
duke@0 162 Monitor* monitor() const {
duke@0 163 return _monitor;
duke@0 164 }
duke@0 165 int total_workers() const {
duke@0 166 return _total_workers;
duke@0 167 }
jmasa@1753 168 virtual int active_workers() const {
jmasa@1753 169 return _total_workers;
jmasa@1753 170 }
duke@0 171 bool terminate() const {
duke@0 172 return _terminate;
duke@0 173 }
duke@0 174 GangWorker** gang_workers() const {
duke@0 175 return _gang_workers;
duke@0 176 }
duke@0 177 AbstractGangTask* task() const {
duke@0 178 return _task;
duke@0 179 }
duke@0 180 int sequence_number() const {
duke@0 181 return _sequence_number;
duke@0 182 }
duke@0 183 int started_workers() const {
duke@0 184 return _started_workers;
duke@0 185 }
duke@0 186 int finished_workers() const {
duke@0 187 return _finished_workers;
duke@0 188 }
ysr@342 189 bool are_GC_task_threads() const {
ysr@342 190 return _are_GC_task_threads;
ysr@342 191 }
ysr@342 192 bool are_ConcurrentGC_threads() const {
ysr@342 193 return _are_ConcurrentGC_threads;
duke@0 194 }
duke@0 195 // Predicates.
duke@0 196 bool is_idle() const {
duke@0 197 return (task() == NULL);
duke@0 198 }
duke@0 199 // Return the Ith gang worker.
duke@0 200 GangWorker* gang_worker(int i) const;
duke@0 201
duke@0 202 void threads_do(ThreadClosure* tc) const;
duke@0 203
duke@0 204 // Printing
duke@0 205 void print_worker_threads_on(outputStream *st) const;
duke@0 206 void print_worker_threads() const {
duke@0 207 print_worker_threads_on(tty);
duke@0 208 }
duke@0 209
duke@0 210 protected:
duke@0 211 friend class GangWorker;
duke@0 212 friend class YieldingFlexibleGangWorker;
duke@0 213 // Note activation and deactivation of workers.
duke@0 214 // These methods should only be called with the mutex held.
duke@0 215 void internal_worker_poll(WorkData* data) const;
duke@0 216 void internal_note_start();
duke@0 217 void internal_note_finish();
duke@0 218 };
duke@0 219
duke@0 220 class WorkData: public StackObj {
duke@0 221 // This would be a struct, but I want accessor methods.
duke@0 222 private:
duke@0 223 bool _terminate;
duke@0 224 AbstractGangTask* _task;
duke@0 225 int _sequence_number;
duke@0 226 public:
duke@0 227 // Constructor and destructor
duke@0 228 WorkData() {
duke@0 229 _terminate = false;
duke@0 230 _task = NULL;
duke@0 231 _sequence_number = 0;
duke@0 232 }
duke@0 233 ~WorkData() {
duke@0 234 }
duke@0 235 // Accessors and modifiers
duke@0 236 bool terminate() const { return _terminate; }
duke@0 237 void set_terminate(bool value) { _terminate = value; }
duke@0 238 AbstractGangTask* task() const { return _task; }
duke@0 239 void set_task(AbstractGangTask* value) { _task = value; }
duke@0 240 int sequence_number() const { return _sequence_number; }
duke@0 241 void set_sequence_number(int value) { _sequence_number = value; }
duke@0 242
duke@0 243 YieldingFlexibleGangTask* yf_task() const {
duke@0 244 return (YieldingFlexibleGangTask*)_task;
duke@0 245 }
duke@0 246 };
duke@0 247
duke@0 248 // Class WorkGang:
duke@0 249 class WorkGang: public AbstractWorkGang {
duke@0 250 public:
duke@0 251 // Constructor
ysr@342 252 WorkGang(const char* name, int workers,
ysr@342 253 bool are_GC_task_threads, bool are_ConcurrentGC_threads);
duke@0 254 // Run a task, returns when the task is done (or terminated).
duke@0 255 virtual void run_task(AbstractGangTask* task);
jmasa@1753 256 void run_task(AbstractGangTask* task, uint no_of_parallel_workers);
jmasa@1753 257 // Allocate a worker and return a pointer to it.
jmasa@1753 258 virtual GangWorker* allocate_worker(int which);
jmasa@1753 259 // Initialize workers in the gang. Return true if initialization
jmasa@1753 260 // succeeded. The type of the worker can be overridden in a derived
jmasa@1753 261 // class with the appropriate implementation of allocate_worker().
jmasa@1753 262 bool initialize_workers();
duke@0 263 };
duke@0 264
duke@0 265 // Class GangWorker:
duke@0 266 // Several instances of this class run in parallel as workers for a gang.
duke@0 267 class GangWorker: public WorkerThread {
duke@0 268 public:
duke@0 269 // Constructors and destructor.
duke@0 270 GangWorker(AbstractWorkGang* gang, uint id);
duke@0 271
duke@0 272 // The only real method: run a task for the gang.
duke@0 273 virtual void run();
duke@0 274 // Predicate for Thread
duke@0 275 virtual bool is_GC_task_thread() const;
ysr@342 276 virtual bool is_ConcurrentGC_thread() const;
duke@0 277 // Printing
duke@0 278 void print_on(outputStream* st) const;
duke@0 279 virtual void print() const { print_on(tty); }
duke@0 280 protected:
duke@0 281 AbstractWorkGang* _gang;
duke@0 282
duke@0 283 virtual void initialize();
duke@0 284 virtual void loop();
duke@0 285
duke@0 286 public:
duke@0 287 AbstractWorkGang* gang() const { return _gang; }
duke@0 288 };
duke@0 289
jmasa@1753 290 class FlexibleWorkGang: public WorkGang {
jmasa@1753 291 protected:
jmasa@1753 292 int _active_workers;
jmasa@1753 293 public:
jmasa@1753 294 // Constructor and destructor.
jmasa@1753 295 FlexibleWorkGang(const char* name, int workers,
jmasa@1753 296 bool are_GC_task_threads,
jmasa@1753 297 bool are_ConcurrentGC_threads) :
jmasa@1753 298 WorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads) {
jmasa@1753 299 _active_workers = ParallelGCThreads;
jmasa@1753 300 };
jmasa@1753 301 // Accessors for fields
jmasa@1753 302 virtual int active_workers() const { return _active_workers; }
jmasa@1753 303 void set_active_workers(int v) { _active_workers = v; }
jmasa@1753 304 };
jmasa@1753 305
jmasa@1753 306 // Work gangs in garbage collectors: 2009-06-10
jmasa@1753 307 //
jmasa@1753 308 // SharedHeap - work gang for stop-the-world parallel collection.
jmasa@1753 309 // Used by
jmasa@1753 310 // ParNewGeneration
jmasa@1753 311 // CMSParRemarkTask
jmasa@1753 312 // CMSRefProcTaskExecutor
jmasa@1753 313 // G1CollectedHeap
jmasa@1753 314 // G1ParFinalCountTask
jmasa@1753 315 // ConcurrentMark
jmasa@1753 316 // CMSCollector
jmasa@1753 317
duke@0 318 // A class that acts as a synchronisation barrier. Workers enter
duke@0 319 // the barrier and must wait until all other workers have entered
duke@0 320 // before any of them may leave.
duke@0 321
duke@0 322 class WorkGangBarrierSync : public StackObj {
duke@0 323 protected:
duke@0 324 Monitor _monitor;
duke@0 325 int _n_workers;
duke@0 326 int _n_completed;
ysr@342 327 bool _should_reset;
duke@0 328
ysr@342 329 Monitor* monitor() { return &_monitor; }
ysr@342 330 int n_workers() { return _n_workers; }
ysr@342 331 int n_completed() { return _n_completed; }
ysr@342 332 bool should_reset() { return _should_reset; }
duke@0 333
ysr@342 334 void zero_completed() { _n_completed = 0; }
ysr@342 335 void inc_completed() { _n_completed++; }
ysr@342 336
ysr@342 337 void set_should_reset(bool v) { _should_reset = v; }
duke@0 338
duke@0 339 public:
duke@0 340 WorkGangBarrierSync();
duke@0 341 WorkGangBarrierSync(int n_workers, const char* name);
duke@0 342
duke@0 343 // Set the number of workers that will use the barrier.
duke@0 344 // Must be called before any of the workers start running.
duke@0 345 void set_n_workers(int n_workers);
duke@0 346
duke@0 347 // Enter the barrier. A worker that enters the barrier will
duke@0 348 // not be allowed to leave until all other threads have
duke@0 349 // also entered the barrier.
duke@0 350 void enter();
duke@0 351 };
duke@0 352
duke@0 353 // A class to manage claiming of subtasks within a group of tasks. The
duke@0 354 // subtasks will be identified by integer indices, usually elements of an
duke@0 355 // enumeration type.
duke@0 356
duke@0 357 class SubTasksDone: public CHeapObj {
duke@0 358 jint* _tasks;
duke@0 359 int _n_tasks;
duke@0 360 int _n_threads;
duke@0 361 jint _threads_completed;
duke@0 362 #ifdef ASSERT
jmasa@1753 363 volatile jint _claimed;
duke@0 364 #endif
duke@0 365
duke@0 366 // Set all tasks to unclaimed.
duke@0 367 void clear();
duke@0 368
duke@0 369 public:
duke@0 370 // Initializes "this" to a state in which there are "n" tasks to be
duke@0 371 // processed, none of the which are originally claimed. The number of
duke@0 372 // threads doing the tasks is initialized 1.
duke@0 373 SubTasksDone(int n);
duke@0 374
duke@0 375 // True iff the object is in a valid state.
duke@0 376 bool valid();
duke@0 377
jmasa@1753 378 // Get/set the number of parallel threads doing the tasks to "t". Can only
duke@0 379 // be called before tasks start or after they are complete.
jmasa@1753 380 int n_threads() { return _n_threads; }
jmasa@1753 381 void set_n_threads(int t);
duke@0 382
duke@0 383 // Returns "false" if the task "t" is unclaimed, and ensures that task is
duke@0 384 // claimed. The task "t" is required to be within the range of "this".
duke@0 385 bool is_task_claimed(int t);
duke@0 386
duke@0 387 // The calling thread asserts that it has attempted to claim all the
duke@0 388 // tasks that it will try to claim. Every thread in the parallel task
duke@0 389 // must execute this. (When the last thread does so, the task array is
duke@0 390 // cleared.)
duke@0 391 void all_tasks_completed();
duke@0 392
duke@0 393 // Destructor.
duke@0 394 ~SubTasksDone();
duke@0 395 };
duke@0 396
duke@0 397 // As above, but for sequential tasks, i.e. instead of claiming
duke@0 398 // sub-tasks from a set (possibly an enumeration), claim sub-tasks
duke@0 399 // in sequential order. This is ideal for claiming dynamically
duke@0 400 // partitioned tasks (like striding in the parallel remembered
duke@0 401 // set scanning). Note that unlike the above class this is
duke@0 402 // a stack object - is there any reason for it not to be?
duke@0 403
duke@0 404 class SequentialSubTasksDone : public StackObj {
duke@0 405 protected:
duke@0 406 jint _n_tasks; // Total number of tasks available.
duke@0 407 jint _n_claimed; // Number of tasks claimed.
jmasa@1753 408 // _n_threads is used to determine when a sub task is done.
jmasa@1753 409 // See comments on SubTasksDone::_n_threads
duke@0 410 jint _n_threads; // Total number of parallel threads.
duke@0 411 jint _n_completed; // Number of completed threads.
duke@0 412
duke@0 413 void clear();
duke@0 414
duke@0 415 public:
jmasa@1753 416 SequentialSubTasksDone() {
jmasa@1753 417 clear();
jmasa@1753 418 }
duke@0 419 ~SequentialSubTasksDone() {}
duke@0 420
duke@0 421 // True iff the object is in a valid state.
duke@0 422 bool valid();
duke@0 423
duke@0 424 // number of tasks
duke@0 425 jint n_tasks() const { return _n_tasks; }
duke@0 426
jmasa@1753 427 // Get/set the number of parallel threads doing the tasks to t.
duke@0 428 // Should be called before the task starts but it is safe
duke@0 429 // to call this once a task is running provided that all
duke@0 430 // threads agree on the number of threads.
jmasa@1753 431 int n_threads() { return _n_threads; }
jmasa@1753 432 void set_n_threads(int t) { _n_threads = t; }
duke@0 433
duke@0 434 // Set the number of tasks to be claimed to t. As above,
duke@0 435 // should be called before the tasks start but it is safe
duke@0 436 // to call this once a task is running provided all threads
duke@0 437 // agree on the number of tasks.
duke@0 438 void set_n_tasks(int t) { _n_tasks = t; }
duke@0 439
duke@0 440 // Returns false if the next task in the sequence is unclaimed,
duke@0 441 // and ensures that it is claimed. Will set t to be the index
duke@0 442 // of the claimed task in the sequence. Will return true if
duke@0 443 // the task cannot be claimed and there are none left to claim.
duke@0 444 bool is_task_claimed(int& t);
duke@0 445
duke@0 446 // The calling thread asserts that it has attempted to claim
duke@0 447 // all the tasks it possibly can in the sequence. Every thread
duke@0 448 // claiming tasks must promise call this. Returns true if this
duke@0 449 // is the last thread to complete so that the thread can perform
duke@0 450 // cleanup if necessary.
duke@0 451 bool all_tasks_completed();
duke@0 452 };
ysr@342 453
ysr@342 454 // Represents a set of free small integer ids.
ysr@342 455 class FreeIdSet {
ysr@342 456 enum {
ysr@342 457 end_of_list = -1,
ysr@342 458 claimed = -2
ysr@342 459 };
ysr@342 460
ysr@342 461 int _sz;
ysr@342 462 Monitor* _mon;
ysr@342 463
ysr@342 464 int* _ids;
ysr@342 465 int _hd;
ysr@342 466 int _waiters;
ysr@342 467 int _claimed;
ysr@342 468
ysr@342 469 static bool _safepoint;
ysr@342 470 typedef FreeIdSet* FreeIdSetPtr;
ysr@342 471 static const int NSets = 10;
ysr@342 472 static FreeIdSetPtr _sets[NSets];
ysr@342 473 static bool _stat_init;
ysr@342 474 int _index;
ysr@342 475
ysr@342 476 public:
ysr@342 477 FreeIdSet(int sz, Monitor* mon);
ysr@342 478 ~FreeIdSet();
ysr@342 479
ysr@342 480 static void set_safepoint(bool b);
ysr@342 481
ysr@342 482 // Attempt to claim the given id permanently. Returns "true" iff
ysr@342 483 // successful.
ysr@342 484 bool claim_perm_id(int i);
ysr@342 485
ysr@342 486 // Returns an unclaimed parallel id (waiting for one to be released if
ysr@342 487 // necessary). Returns "-1" if a GC wakes up a wait for an id.
ysr@342 488 int claim_par_id();
ysr@342 489
ysr@342 490 void release_par_id(int id);
ysr@342 491 };
stefank@1879 492
stefank@1879 493 #endif // SHARE_VM_UTILITIES_WORKGROUP_HPP