src/share/vm/utilities/workgroup.hpp

Sun, 25 Sep 2011 16:03:29 -0700

author
never
date
Sun, 25 Sep 2011 16:03:29 -0700
changeset 3156
f08d439fab8c
parent 2651
92da084fefc9
child 3294
bca17e38de00
permissions
-rw-r--r--

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>

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

mercurial