1 /* 2 * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_GC_SHARED_WORKGROUP_HPP 26 #define SHARE_VM_GC_SHARED_WORKGROUP_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/globals.hpp" 30 #include "runtime/thread.hpp" 31 #include "gc/shared/gcId.hpp" 32 #include "utilities/debug.hpp" 33 #include "utilities/globalDefinitions.hpp" 34 35 // Task class hierarchy: 36 // AbstractGangTask 37 // 38 // Gang/Group class hierarchy: 39 // AbstractWorkGang 40 // WorkGang 41 // YieldingFlexibleWorkGang (defined in another file) 42 // 43 // Worker class hierarchy: 44 // AbstractGangWorker (subclass of WorkerThread) 45 // GangWorker 46 // YieldingFlexibleGangWorker (defined in another file) 47 48 // Forward declarations of classes defined here 49 50 class AbstractGangWorker; 51 class Semaphore; 52 class WorkGang; 53 54 // An abstract task to be worked on by a gang. 55 // You subclass this to supply your own work() method 56 class AbstractGangTask VALUE_OBJ_CLASS_SPEC { 57 const char* _name; 58 const uint _gc_id; 59 60 public: 61 AbstractGangTask(const char* name) : 62 _name(name), 63 _gc_id(GCId::current_raw()) 64 {} 65 66 // The abstract work method. 67 // The argument tells you which member of the gang you are. 68 virtual void work(uint worker_id) = 0; 69 70 // Debugging accessor for the name. 71 const char* name() const { return _name; } 72 const uint gc_id() const { return _gc_id; } 73 }; 74 75 struct WorkData { 76 AbstractGangTask* _task; 77 uint _worker_id; 78 WorkData(AbstractGangTask* task, uint worker_id) : _task(task), _worker_id(worker_id) {} 79 }; 80 81 // Interface to handle the synchronization between the coordinator thread and the worker threads, 82 // when a task is dispatched out to the worker threads. 83 class GangTaskDispatcher : public CHeapObj<mtGC> { 84 public: 85 virtual ~GangTaskDispatcher() {} 86 87 // Coordinator API. 88 89 // Distributes the task out to num_workers workers. 90 // Returns when the task has been completed by all workers. 91 virtual void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) = 0; 92 93 // Worker API. 94 95 // Waits for a task to become available to the worker. 96 // Returns when the worker has been assigned a task. 97 virtual WorkData worker_wait_for_task() = 0; 98 99 // Signal to the coordinator that the worker is done with the assigned task. 100 virtual void worker_done_with_task() = 0; 101 }; 102 103 // The work gang is the collection of workers to execute tasks. 104 // The number of workers run for a task is "_active_workers" 105 // while "_total_workers" is the number of available of workers. 106 class AbstractWorkGang : public CHeapObj<mtInternal> { 107 protected: 108 // The array of worker threads for this gang. 109 AbstractGangWorker** _workers; 110 // The count of the number of workers in the gang. 111 uint _total_workers; 112 // The currently active workers in this gang. 113 uint _active_workers; 114 // Printing support. 115 const char* _name; 116 117 private: 118 // Initialize only instance data. 119 const bool _are_GC_task_threads; 120 const bool _are_ConcurrentGC_threads; 121 122 public: 123 AbstractWorkGang(const char* name, uint workers, bool are_GC_task_threads, bool are_ConcurrentGC_threads) : 124 _name(name), 125 _total_workers(workers), 126 _active_workers(UseDynamicNumberOfGCThreads ? 1U : workers), 127 _are_GC_task_threads(are_GC_task_threads), 128 _are_ConcurrentGC_threads(are_ConcurrentGC_threads) 129 { } 130 131 // Initialize workers in the gang. Return true if initialization succeeded. 132 bool initialize_workers(); 133 134 bool are_GC_task_threads() const { return _are_GC_task_threads; } 135 bool are_ConcurrentGC_threads() const { return _are_ConcurrentGC_threads; } 136 137 uint total_workers() const { return _total_workers; } 138 139 virtual uint active_workers() const { 140 assert(_active_workers <= _total_workers, 141 "_active_workers: %u > _total_workers: %u", _active_workers, _total_workers); 142 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 143 "Unless dynamic should use total workers"); 144 return _active_workers; 145 } 146 void set_active_workers(uint v) { 147 assert(v <= _total_workers, 148 "Trying to set more workers active than there are"); 149 _active_workers = MIN2(v, _total_workers); 150 assert(v != 0, "Trying to set active workers to 0"); 151 _active_workers = MAX2(1U, _active_workers); 152 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 153 "Unless dynamic should use total workers"); 154 } 155 156 // Return the Ith worker. 157 AbstractGangWorker* worker(uint i) const; 158 159 void threads_do(ThreadClosure* tc) const; 160 161 // Debugging. 162 const char* name() const { return _name; } 163 164 // Printing 165 void print_worker_threads_on(outputStream *st) const; 166 void print_worker_threads() const { 167 print_worker_threads_on(tty); 168 } 169 170 protected: 171 virtual AbstractGangWorker* allocate_worker(uint which) = 0; 172 }; 173 174 // An class representing a gang of workers. 175 class WorkGang: public AbstractWorkGang { 176 // To get access to the GangTaskDispatcher instance. 177 friend class GangWorker; 178 179 // Never deleted. 180 ~WorkGang(); 181 182 GangTaskDispatcher* const _dispatcher; 183 GangTaskDispatcher* dispatcher() const { 184 return _dispatcher; 185 } 186 187 public: 188 WorkGang(const char* name, 189 uint workers, 190 bool are_GC_task_threads, 191 bool are_ConcurrentGC_threads); 192 193 // Run a task, returns when the task is done. 194 virtual void run_task(AbstractGangTask* task); 195 196 protected: 197 virtual AbstractGangWorker* allocate_worker(uint which); 198 }; 199 200 // Several instances of this class run in parallel as workers for a gang. 201 class AbstractGangWorker: public WorkerThread { 202 public: 203 AbstractGangWorker(AbstractWorkGang* gang, uint id); 204 205 // The only real method: run a task for the gang. 206 virtual void run(); 207 // Predicate for Thread 208 virtual bool is_GC_task_thread() const; 209 virtual bool is_ConcurrentGC_thread() const; 210 // Printing 211 void print_on(outputStream* st) const; 212 virtual void print() const { print_on(tty); } 213 214 protected: 215 AbstractWorkGang* _gang; 216 217 virtual void initialize(); 218 virtual void loop() = 0; 219 220 AbstractWorkGang* gang() const { return _gang; } 221 }; 222 223 class GangWorker: public AbstractGangWorker { 224 public: 225 GangWorker(WorkGang* gang, uint id) : AbstractGangWorker(gang, id) {} 226 227 protected: 228 virtual void loop(); 229 230 private: 231 WorkData wait_for_task(); 232 void run_task(WorkData work); 233 void signal_task_done(); 234 235 void print_task_started(WorkData data); 236 void print_task_done(WorkData data); 237 238 WorkGang* gang() const { return (WorkGang*)_gang; } 239 }; 240 241 // A class that acts as a synchronisation barrier. Workers enter 242 // the barrier and must wait until all other workers have entered 243 // before any of them may leave. 244 245 class WorkGangBarrierSync : public StackObj { 246 protected: 247 Monitor _monitor; 248 uint _n_workers; 249 uint _n_completed; 250 bool _should_reset; 251 bool _aborted; 252 253 Monitor* monitor() { return &_monitor; } 254 uint n_workers() { return _n_workers; } 255 uint n_completed() { return _n_completed; } 256 bool should_reset() { return _should_reset; } 257 bool aborted() { return _aborted; } 258 259 void zero_completed() { _n_completed = 0; } 260 void inc_completed() { _n_completed++; } 261 void set_aborted() { _aborted = true; } 262 void set_should_reset(bool v) { _should_reset = v; } 263 264 public: 265 WorkGangBarrierSync(); 266 WorkGangBarrierSync(uint n_workers, const char* name); 267 268 // Set the number of workers that will use the barrier. 269 // Must be called before any of the workers start running. 270 void set_n_workers(uint n_workers); 271 272 // Enter the barrier. A worker that enters the barrier will 273 // not be allowed to leave until all other threads have 274 // also entered the barrier or the barrier is aborted. 275 // Returns false if the barrier was aborted. 276 bool enter(); 277 278 // Aborts the barrier and wakes up any threads waiting for 279 // the barrier to complete. The barrier will remain in the 280 // aborted state until the next call to set_n_workers(). 281 void abort(); 282 }; 283 284 // A class to manage claiming of subtasks within a group of tasks. The 285 // subtasks will be identified by integer indices, usually elements of an 286 // enumeration type. 287 288 class SubTasksDone: public CHeapObj<mtInternal> { 289 uint* _tasks; 290 uint _n_tasks; 291 uint _threads_completed; 292 #ifdef ASSERT 293 volatile uint _claimed; 294 #endif 295 296 // Set all tasks to unclaimed. 297 void clear(); 298 299 public: 300 // Initializes "this" to a state in which there are "n" tasks to be 301 // processed, none of the which are originally claimed. The number of 302 // threads doing the tasks is initialized 1. 303 SubTasksDone(uint n); 304 305 // True iff the object is in a valid state. 306 bool valid(); 307 308 // Returns "false" if the task "t" is unclaimed, and ensures that task is 309 // claimed. The task "t" is required to be within the range of "this". 310 bool is_task_claimed(uint t); 311 312 // The calling thread asserts that it has attempted to claim all the 313 // tasks that it will try to claim. Every thread in the parallel task 314 // must execute this. (When the last thread does so, the task array is 315 // cleared.) 316 // 317 // n_threads - Number of threads executing the sub-tasks. 318 void all_tasks_completed(uint n_threads); 319 320 // Destructor. 321 ~SubTasksDone(); 322 }; 323 324 // As above, but for sequential tasks, i.e. instead of claiming 325 // sub-tasks from a set (possibly an enumeration), claim sub-tasks 326 // in sequential order. This is ideal for claiming dynamically 327 // partitioned tasks (like striding in the parallel remembered 328 // set scanning). Note that unlike the above class this is 329 // a stack object - is there any reason for it not to be? 330 331 class SequentialSubTasksDone : public StackObj { 332 protected: 333 uint _n_tasks; // Total number of tasks available. 334 uint _n_claimed; // Number of tasks claimed. 335 // _n_threads is used to determine when a sub task is done. 336 // See comments on SubTasksDone::_n_threads 337 uint _n_threads; // Total number of parallel threads. 338 uint _n_completed; // Number of completed threads. 339 340 void clear(); 341 342 public: 343 SequentialSubTasksDone() { 344 clear(); 345 } 346 ~SequentialSubTasksDone() {} 347 348 // True iff the object is in a valid state. 349 bool valid(); 350 351 // number of tasks 352 uint n_tasks() const { return _n_tasks; } 353 354 // Get/set the number of parallel threads doing the tasks to t. 355 // Should be called before the task starts but it is safe 356 // to call this once a task is running provided that all 357 // threads agree on the number of threads. 358 uint n_threads() { return _n_threads; } 359 void set_n_threads(uint t) { _n_threads = t; } 360 361 // Set the number of tasks to be claimed to t. As above, 362 // should be called before the tasks start but it is safe 363 // to call this once a task is running provided all threads 364 // agree on the number of tasks. 365 void set_n_tasks(uint t) { _n_tasks = t; } 366 367 // Returns false if the next task in the sequence is unclaimed, 368 // and ensures that it is claimed. Will set t to be the index 369 // of the claimed task in the sequence. Will return true if 370 // the task cannot be claimed and there are none left to claim. 371 bool is_task_claimed(uint& t); 372 373 // The calling thread asserts that it has attempted to claim 374 // all the tasks it possibly can in the sequence. Every thread 375 // claiming tasks must promise call this. Returns true if this 376 // is the last thread to complete so that the thread can perform 377 // cleanup if necessary. 378 bool all_tasks_completed(); 379 }; 380 381 // Represents a set of free small integer ids. 382 class FreeIdSet : public CHeapObj<mtInternal> { 383 enum { 384 end_of_list = UINT_MAX, 385 claimed = UINT_MAX - 1 386 }; 387 388 int _sz; 389 Monitor* _mon; 390 391 uint* _ids; 392 uint _hd; 393 uint _waiters; 394 uint _claimed; 395 396 static bool _safepoint; 397 typedef FreeIdSet* FreeIdSetPtr; 398 static const int NSets = 10; 399 static FreeIdSetPtr _sets[NSets]; 400 static bool _stat_init; 401 int _index; 402 403 public: 404 FreeIdSet(uint sz, Monitor* mon); 405 ~FreeIdSet(); 406 407 static void set_safepoint(bool b); 408 409 // Returns an unclaimed parallel id (waiting for one to be released if 410 // necessary). Returns "UINT_MAX" if a GC wakes up a wait for an id. 411 int claim_par_id(); 412 413 void release_par_id(uint id); 414 }; 415 416 #endif // SHARE_VM_GC_SHARED_WORKGROUP_HPP