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 "gc/shared/taskqueue.hpp" 29 #include "runtime/thread.inline.hpp" 30 31 // Task class hierarchy: 32 // AbstractGangTask 33 // AbstractGangTaskWOopQueues 34 // 35 // Gang/Group class hierarchy: 36 // AbstractWorkGang 37 // WorkGang 38 // FlexibleWorkGang 39 // YieldingFlexibleWorkGang (defined in another file) 40 // 41 // Worker class hierarchy: 42 // GangWorker (subclass of WorkerThread) 43 // YieldingFlexibleGangWorker (defined in another file) 44 45 // Forward declarations of classes defined here 46 47 class WorkGang; 48 class GangWorker; 49 class YieldingFlexibleGangWorker; 50 class YieldingFlexibleGangTask; 51 class WorkData; 52 class AbstractWorkGang; 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 public: 58 // The abstract work method. 59 // The argument tells you which member of the gang you are. 60 virtual void work(uint worker_id) = 0; 61 62 // Debugging accessor for the name. 63 const char* name() const PRODUCT_RETURN_(return NULL;); 64 int counter() { return _counter; } 65 void set_counter(int value) { _counter = value; } 66 int *address_of_counter() { return &_counter; } 67 68 // RTTI 69 NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const { 70 return false; 71 }) 72 73 private: 74 NOT_PRODUCT(const char* _name;) 75 // ??? Should a task have a priority associated with it? 76 // ??? Or can the run method adjust priority as needed? 77 int _counter; 78 79 protected: 80 // Constructor and desctructor: only construct subclasses. 81 AbstractGangTask(const char* name) 82 { 83 NOT_PRODUCT(_name = name); 84 _counter = 0; 85 } 86 ~AbstractGangTask() { } 87 88 public: 89 }; 90 91 class AbstractGangTaskWOopQueues : public AbstractGangTask { 92 OopTaskQueueSet* _queues; 93 ParallelTaskTerminator _terminator; 94 public: 95 AbstractGangTaskWOopQueues(const char* name, OopTaskQueueSet* queues, uint n_threads) : 96 AbstractGangTask(name), _queues(queues), _terminator(n_threads, _queues) {} 97 ParallelTaskTerminator* terminator() { return &_terminator; } 98 OopTaskQueueSet* queues() { return _queues; } 99 }; 100 101 102 // Class AbstractWorkGang: 103 // An abstract class representing a gang of workers. 104 // You subclass this to supply an implementation of run_task(). 105 class AbstractWorkGang: public CHeapObj<mtInternal> { 106 protected: 107 // Work gangs are never deleted, so no need to cleanup. 108 ~AbstractWorkGang() { ShouldNotReachHere(); } 109 public: 110 // Constructor. 111 AbstractWorkGang(const char* name, bool are_GC_task_threads, 112 bool are_ConcurrentGC_threads); 113 // Run a task, returns when the task is done (or terminated). 114 virtual void run_task(AbstractGangTask* task) = 0; 115 // Return true if more workers should be applied to the task. 116 virtual bool needs_more_workers() const { return true; } 117 public: 118 // Debugging. 119 const char* name() const; 120 protected: 121 // Initialize only instance data. 122 const bool _are_GC_task_threads; 123 const bool _are_ConcurrentGC_threads; 124 // Printing support. 125 const char* _name; 126 // The monitor which protects these data, 127 // and notifies of changes in it. 128 Monitor* _monitor; 129 // The count of the number of workers in the gang. 130 uint _total_workers; 131 // The array of worker threads for this gang. 132 // This is only needed for cleaning up. 133 GangWorker** _gang_workers; 134 // The task for this gang. 135 AbstractGangTask* _task; 136 // A sequence number for the current task. 137 int _sequence_number; 138 // The number of started workers. 139 uint _started_workers; 140 // The number of finished workers. 141 uint _finished_workers; 142 public: 143 // Accessors for fields 144 Monitor* monitor() const { 145 return _monitor; 146 } 147 uint total_workers() const { 148 return _total_workers; 149 } 150 virtual uint active_workers() const { 151 return _total_workers; 152 } 153 GangWorker** gang_workers() const { 154 return _gang_workers; 155 } 156 AbstractGangTask* task() const { 157 return _task; 158 } 159 int sequence_number() const { 160 return _sequence_number; 161 } 162 uint started_workers() const { 163 return _started_workers; 164 } 165 uint finished_workers() const { 166 return _finished_workers; 167 } 168 bool are_GC_task_threads() const { 169 return _are_GC_task_threads; 170 } 171 bool are_ConcurrentGC_threads() const { 172 return _are_ConcurrentGC_threads; 173 } 174 // Predicates. 175 bool is_idle() const { 176 return (task() == NULL); 177 } 178 // Return the Ith gang worker. 179 GangWorker* gang_worker(uint i) const; 180 181 void threads_do(ThreadClosure* tc) const; 182 183 // Printing 184 void print_worker_threads_on(outputStream *st) const; 185 void print_worker_threads() const { 186 print_worker_threads_on(tty); 187 } 188 189 protected: 190 friend class GangWorker; 191 friend class YieldingFlexibleGangWorker; 192 // Note activation and deactivation of workers. 193 // These methods should only be called with the mutex held. 194 void internal_worker_poll(WorkData* data) const; 195 void internal_note_start(); 196 void internal_note_finish(); 197 }; 198 199 class WorkData: public StackObj { 200 // This would be a struct, but I want accessor methods. 201 private: 202 AbstractGangTask* _task; 203 int _sequence_number; 204 public: 205 // Constructor and destructor 206 WorkData() { 207 _task = NULL; 208 _sequence_number = 0; 209 } 210 ~WorkData() { 211 } 212 AbstractGangTask* task() const { return _task; } 213 void set_task(AbstractGangTask* value) { _task = value; } 214 int sequence_number() const { return _sequence_number; } 215 void set_sequence_number(int value) { _sequence_number = value; } 216 217 YieldingFlexibleGangTask* yf_task() const { 218 return (YieldingFlexibleGangTask*)_task; 219 } 220 }; 221 222 // Class WorkGang: 223 class WorkGang: public AbstractWorkGang { 224 public: 225 // Constructor 226 WorkGang(const char* name, uint workers, 227 bool are_GC_task_threads, bool are_ConcurrentGC_threads); 228 // Run a task, returns when the task is done (or terminated). 229 virtual void run_task(AbstractGangTask* task); 230 void run_task(AbstractGangTask* task, uint no_of_parallel_workers); 231 // Allocate a worker and return a pointer to it. 232 virtual GangWorker* allocate_worker(uint which); 233 // Initialize workers in the gang. Return true if initialization 234 // succeeded. The type of the worker can be overridden in a derived 235 // class with the appropriate implementation of allocate_worker(). 236 bool initialize_workers(); 237 }; 238 239 // Class GangWorker: 240 // Several instances of this class run in parallel as workers for a gang. 241 class GangWorker: public WorkerThread { 242 public: 243 // Constructors and destructor. 244 GangWorker(AbstractWorkGang* gang, uint id); 245 246 // The only real method: run a task for the gang. 247 virtual void run(); 248 // Predicate for Thread 249 virtual bool is_GC_task_thread() const; 250 virtual bool is_ConcurrentGC_thread() const; 251 // Printing 252 void print_on(outputStream* st) const; 253 virtual void print() const { print_on(tty); } 254 protected: 255 AbstractWorkGang* _gang; 256 257 virtual void initialize(); 258 virtual void loop(); 259 260 public: 261 AbstractWorkGang* gang() const { return _gang; } 262 }; 263 264 // Dynamic number of worker threads 265 // 266 // This type of work gang is used to run different numbers of 267 // worker threads at different times. The 268 // number of workers run for a task is "_active_workers" 269 // instead of "_total_workers" in a WorkGang. The method 270 // "needs_more_workers()" returns true until "_active_workers" 271 // have been started and returns false afterwards. The 272 // implementation of "needs_more_workers()" in WorkGang always 273 // returns true so that all workers are started. The method 274 // "loop()" in GangWorker was modified to ask "needs_more_workers()" 275 // in its loop to decide if it should start working on a task. 276 // A worker in "loop()" waits for notification on the WorkGang 277 // monitor and execution of each worker as it checks for work 278 // is serialized via the same monitor. The "needs_more_workers()" 279 // call is serialized and additionally the calculation for the 280 // "part" (effectively the worker id for executing the task) is 281 // serialized to give each worker a unique "part". Workers that 282 // are not needed for this tasks (i.e., "_active_workers" have 283 // been started before it, continue to wait for work. 284 285 class FlexibleWorkGang: public WorkGang { 286 // The currently active workers in this gang. 287 // This is a number that is dynamically adjusted 288 // and checked in the run_task() method at each invocation. 289 // As described above _active_workers determines the number 290 // of threads started on a task. It must also be used to 291 // determine completion. 292 293 protected: 294 uint _active_workers; 295 public: 296 // Constructor and destructor. 297 FlexibleWorkGang(const char* name, uint workers, 298 bool are_GC_task_threads, 299 bool are_ConcurrentGC_threads) : 300 WorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads), 301 _active_workers(UseDynamicNumberOfGCThreads ? 1U : workers) {} 302 303 // Accessors for fields. 304 virtual uint active_workers() const { 305 assert(_active_workers <= _total_workers, 306 err_msg("_active_workers: %u > _total_workers: %u", _active_workers, _total_workers)); 307 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 308 "Unless dynamic should use total workers"); 309 return _active_workers; 310 } 311 void set_active_workers(uint v) { 312 assert(v <= _total_workers, 313 "Trying to set more workers active than there are"); 314 _active_workers = MIN2(v, _total_workers); 315 assert(v != 0, "Trying to set active workers to 0"); 316 _active_workers = MAX2(1U, _active_workers); 317 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 318 "Unless dynamic should use total workers"); 319 } 320 virtual void run_task(AbstractGangTask* task); 321 virtual bool needs_more_workers() const { 322 return _started_workers < _active_workers; 323 } 324 }; 325 326 // A class that acts as a synchronisation barrier. Workers enter 327 // the barrier and must wait until all other workers have entered 328 // before any of them may leave. 329 330 class WorkGangBarrierSync : public StackObj { 331 protected: 332 Monitor _monitor; 333 uint _n_workers; 334 uint _n_completed; 335 bool _should_reset; 336 bool _aborted; 337 338 Monitor* monitor() { return &_monitor; } 339 uint n_workers() { return _n_workers; } 340 uint n_completed() { return _n_completed; } 341 bool should_reset() { return _should_reset; } 342 bool aborted() { return _aborted; } 343 344 void zero_completed() { _n_completed = 0; } | 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 "runtime/thread.inline.hpp" 29 30 // Task class hierarchy: 31 // AbstractGangTask 32 // 33 // Gang/Group class hierarchy: 34 // AbstractWorkGang 35 // WorkGang 36 // YieldingFlexibleWorkGang (defined in another file) 37 // 38 // Worker class hierarchy: 39 // AbstractGangWorker (subclass of WorkerThread) 40 // GangWorker 41 // YieldingFlexibleGangWorker (defined in another file) 42 43 // Forward declarations of classes defined here 44 45 class AbstractGangWorker; 46 class GangWorker; 47 class WorkData; 48 49 // An abstract task to be worked on by a gang. 50 // You subclass this to supply your own work() method 51 class AbstractGangTask VALUE_OBJ_CLASS_SPEC { 52 const char* _name; 53 54 public: 55 AbstractGangTask(const char* name) : _name(name) {} 56 57 // The abstract work method. 58 // The argument tells you which member of the gang you are. 59 virtual void work(uint worker_id) = 0; 60 61 // Debugging accessor for the name. 62 const char* name() const { return _name; } 63 }; 64 65 66 // The work gang is the collection of workers to execute tasks. 67 // The number of workers run for a task is "_active_workers" 68 // while "_total_workers" is the number of available of workers. 69 class AbstractWorkGang : public CHeapObj<mtInternal> { 70 protected: 71 // The array of worker threads for this gang. 72 AbstractGangWorker** _workers; 73 // The count of the number of workers in the gang. 74 uint _total_workers; 75 // The currently active workers in this gang. 76 uint _active_workers; 77 // Printing support. 78 const char* _name; 79 80 private: 81 // Initialize only instance data. 82 const bool _are_GC_task_threads; 83 const bool _are_ConcurrentGC_threads; 84 85 public: 86 AbstractWorkGang(const char* name, uint workers, bool are_GC_task_threads, bool are_ConcurrentGC_threads) : 87 _name(name), 88 _total_workers(workers), 89 _active_workers(UseDynamicNumberOfGCThreads ? 1U : workers), 90 _are_GC_task_threads(are_GC_task_threads), 91 _are_ConcurrentGC_threads(are_ConcurrentGC_threads) 92 { } 93 94 virtual AbstractGangWorker* allocate_worker(uint which) = 0; 95 96 // Initialize workers in the gang. Return true if initialization succeeded. 97 bool initialize_workers(); 98 99 bool are_GC_task_threads() const { return _are_GC_task_threads; } 100 bool are_ConcurrentGC_threads() const { return _are_ConcurrentGC_threads; } 101 102 uint total_workers() const { return _total_workers; } 103 104 virtual uint active_workers() const { 105 assert(_active_workers <= _total_workers, 106 err_msg("_active_workers: %u > _total_workers: %u", _active_workers, _total_workers)); 107 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 108 "Unless dynamic should use total workers"); 109 return _active_workers; 110 } 111 void set_active_workers(uint v) { 112 assert(v <= _total_workers, 113 "Trying to set more workers active than there are"); 114 _active_workers = MIN2(v, _total_workers); 115 assert(v != 0, "Trying to set active workers to 0"); 116 _active_workers = MAX2(1U, _active_workers); 117 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 118 "Unless dynamic should use total workers"); 119 } 120 121 // Return the Ith worker. 122 AbstractGangWorker* worker(uint i) const; 123 124 void threads_do(ThreadClosure* tc) const; 125 126 // Debugging. 127 const char* name() const { return _name; } 128 129 // Printing 130 void print_worker_threads_on(outputStream *st) const; 131 void print_worker_threads() const { 132 print_worker_threads_on(tty); 133 } 134 }; 135 136 // An class representing a gang of workers. 137 class WorkGang: public AbstractWorkGang { 138 private: 139 // Never deleted. 140 ~WorkGang(); 141 public: 142 WorkGang(const char* name, 143 uint workers, 144 bool are_GC_task_threads, 145 bool are_ConcurrentGC_threads); 146 147 // Run a task, returns when the task is done. 148 virtual void run_task(AbstractGangTask* task); 149 void run_task(AbstractGangTask* task, uint no_of_parallel_workers); 150 151 // Return true if more workers should be applied to the task. 152 virtual bool needs_more_workers() const { 153 return _started_workers < _active_workers; 154 } 155 156 protected: 157 // The monitor which protects these data, 158 // and notifies of changes in it. 159 Monitor* _monitor; 160 // The task for this gang. 161 AbstractGangTask* _task; 162 // A sequence number for the current task. 163 int _sequence_number; 164 // The number of started workers. 165 uint _started_workers; 166 // The number of finished workers. 167 uint _finished_workers; 168 169 public: 170 virtual AbstractGangWorker* allocate_worker(uint which); 171 172 // Accessors for fields 173 Monitor* monitor() const { 174 return _monitor; 175 } 176 AbstractGangTask* task() const { 177 return _task; 178 } 179 int sequence_number() const { 180 return _sequence_number; 181 } 182 uint started_workers() const { 183 return _started_workers; 184 } 185 uint finished_workers() const { 186 return _finished_workers; 187 } 188 // Predicates. 189 bool is_idle() const { 190 return (task() == NULL); 191 } 192 // Return the Ith gang worker. 193 GangWorker* gang_worker(uint i) const; 194 195 protected: 196 friend class GangWorker; 197 // Note activation and deactivation of workers. 198 // These methods should only be called with the mutex held. 199 void internal_worker_poll(WorkData* data) const; 200 void internal_note_start(); 201 void internal_note_finish(); 202 }; 203 204 class WorkData: public StackObj { 205 // This would be a struct, but I want accessor methods. 206 private: 207 AbstractGangTask* _task; 208 int _sequence_number; 209 public: 210 // Constructor and destructor 211 WorkData() { 212 _task = NULL; 213 _sequence_number = 0; 214 } 215 ~WorkData() { 216 } 217 AbstractGangTask* task() const { return _task; } 218 void set_task(AbstractGangTask* value) { _task = value; } 219 int sequence_number() const { return _sequence_number; } 220 void set_sequence_number(int value) { _sequence_number = value; } 221 }; 222 223 // Several instances of this class run in parallel as workers for a gang. 224 class AbstractGangWorker: public WorkerThread { 225 public: 226 // Constructors and destructor. 227 AbstractGangWorker(AbstractWorkGang* gang, uint id); 228 229 // The only real method: run a task for the gang. 230 virtual void run(); 231 // Predicate for Thread 232 virtual bool is_GC_task_thread() const; 233 virtual bool is_ConcurrentGC_thread() const; 234 // Printing 235 void print_on(outputStream* st) const; 236 virtual void print() const { print_on(tty); } 237 238 protected: 239 AbstractWorkGang* _gang; 240 241 virtual void initialize(); 242 virtual void loop() = 0; 243 244 AbstractWorkGang* gang() const { return _gang; } 245 }; 246 247 class GangWorker: public AbstractGangWorker { 248 public: 249 GangWorker(WorkGang* gang, uint id) : AbstractGangWorker(gang, id) {} 250 251 protected: 252 virtual void loop(); 253 254 private: 255 WorkGang* gang() const { return (WorkGang*)_gang; } 256 }; 257 258 // Dynamic number of worker threads 259 // 260 // This type of work gang is used to run different numbers of 261 // worker threads at different times. The 262 // number of workers run for a task is "_active_workers" 263 // instead of "_total_workers" in a WorkGang. The method 264 // "needs_more_workers()" returns true until "_active_workers" 265 // have been started and returns false afterwards. The 266 // implementation of "needs_more_workers()" in WorkGang always 267 // returns true so that all workers are started. The method 268 // "loop()" in GangWorker was modified to ask "needs_more_workers()" 269 // in its loop to decide if it should start working on a task. 270 // A worker in "loop()" waits for notification on the WorkGang 271 // monitor and execution of each worker as it checks for work 272 // is serialized via the same monitor. The "needs_more_workers()" 273 // call is serialized and additionally the calculation for the 274 // "part" (effectively the worker id for executing the task) is 275 // serialized to give each worker a unique "part". Workers that 276 // are not needed for this tasks (i.e., "_active_workers" have 277 // been started before it, continue to wait for work. 278 279 // A class that acts as a synchronisation barrier. Workers enter 280 // the barrier and must wait until all other workers have entered 281 // before any of them may leave. 282 283 class WorkGangBarrierSync : public StackObj { 284 protected: 285 Monitor _monitor; 286 uint _n_workers; 287 uint _n_completed; 288 bool _should_reset; 289 bool _aborted; 290 291 Monitor* monitor() { return &_monitor; } 292 uint n_workers() { return _n_workers; } 293 uint n_completed() { return _n_completed; } 294 bool should_reset() { return _should_reset; } 295 bool aborted() { return _aborted; } 296 297 void zero_completed() { _n_completed = 0; } |