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; } | 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 "utilities/debug.hpp" 32 #include "utilities/globalDefinitions.hpp" 33 34 // Task class hierarchy: 35 // AbstractGangTask 36 // 37 // Gang/Group class hierarchy: 38 // AbstractWorkGang 39 // WorkGang 40 // YieldingFlexibleWorkGang (defined in another file) 41 // 42 // Worker class hierarchy: 43 // AbstractGangWorker (subclass of WorkerThread) 44 // GangWorker 45 // YieldingFlexibleGangWorker (defined in another file) 46 47 // Forward declarations of classes defined here 48 49 class AbstractGangWorker; 50 class Semaphore; 51 class WorkGang; 52 53 // An abstract task to be worked on by a gang. 54 // You subclass this to supply your own work() method 55 class AbstractGangTask VALUE_OBJ_CLASS_SPEC { 56 const char* _name; 57 58 public: 59 AbstractGangTask(const char* name) : _name(name) {} 60 61 // The abstract work method. 62 // The argument tells you which member of the gang you are. 63 virtual void work(uint worker_id) = 0; 64 65 // Debugging accessor for the name. 66 const char* name() const { return _name; } 67 }; 68 69 struct WorkData { 70 AbstractGangTask* _task; 71 uint _worker_id; 72 WorkData(AbstractGangTask* task, uint worker_id) : _task(task), _worker_id(worker_id) {} 73 }; 74 75 // Interface to handle the synchronization between the coordinator thread and the worker threads, 76 // when a task is dispatched out to the worker threads. 77 class GangTaskDispatcher : public CHeapObj<mtGC> { 78 public: 79 virtual ~GangTaskDispatcher() {} 80 81 // Coordinator API. 82 83 // Distributes the task out to num_workers workers. 84 // Returns when the task has been completed by all workers. 85 virtual void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) = 0; 86 87 // Worker API. 88 89 // Waits for a task to become available to the worker. 90 // Returns when the worker has been assigned a task. 91 virtual WorkData worker_wait_for_task() = 0; 92 93 // Signal to the coordinator that the worker is done with the assigned task. 94 virtual void worker_done_with_task() = 0; 95 }; 96 97 // The work gang is the collection of workers to execute tasks. 98 // The number of workers run for a task is "_active_workers" 99 // while "_total_workers" is the number of available of workers. 100 class AbstractWorkGang : public CHeapObj<mtInternal> { 101 protected: 102 // The array of worker threads for this gang. 103 AbstractGangWorker** _workers; 104 // The count of the number of workers in the gang. 105 uint _total_workers; 106 // The currently active workers in this gang. 107 uint _active_workers; 108 // Printing support. 109 const char* _name; 110 111 private: 112 // Initialize only instance data. 113 const bool _are_GC_task_threads; 114 const bool _are_ConcurrentGC_threads; 115 116 public: 117 AbstractWorkGang(const char* name, uint workers, bool are_GC_task_threads, bool are_ConcurrentGC_threads) : 118 _name(name), 119 _total_workers(workers), 120 _active_workers(UseDynamicNumberOfGCThreads ? 1U : workers), 121 _are_GC_task_threads(are_GC_task_threads), 122 _are_ConcurrentGC_threads(are_ConcurrentGC_threads) 123 { } 124 125 // Initialize workers in the gang. Return true if initialization succeeded. 126 bool initialize_workers(); 127 128 bool are_GC_task_threads() const { return _are_GC_task_threads; } 129 bool are_ConcurrentGC_threads() const { return _are_ConcurrentGC_threads; } 130 131 uint total_workers() const { return _total_workers; } 132 133 virtual uint active_workers() const { 134 assert(_active_workers <= _total_workers, 135 err_msg("_active_workers: %u > _total_workers: %u", _active_workers, _total_workers)); 136 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 137 "Unless dynamic should use total workers"); 138 return _active_workers; 139 } 140 void set_active_workers(uint v) { 141 assert(v <= _total_workers, 142 "Trying to set more workers active than there are"); 143 _active_workers = MIN2(v, _total_workers); 144 assert(v != 0, "Trying to set active workers to 0"); 145 _active_workers = MAX2(1U, _active_workers); 146 assert(UseDynamicNumberOfGCThreads || _active_workers == _total_workers, 147 "Unless dynamic should use total workers"); 148 } 149 150 // Return the Ith worker. 151 AbstractGangWorker* worker(uint i) const; 152 153 void threads_do(ThreadClosure* tc) const; 154 155 // Debugging. 156 const char* name() const { return _name; } 157 158 // Printing 159 void print_worker_threads_on(outputStream *st) const; 160 void print_worker_threads() const { 161 print_worker_threads_on(tty); 162 } 163 164 protected: 165 virtual AbstractGangWorker* allocate_worker(uint which) = 0; 166 }; 167 168 // An class representing a gang of workers. 169 class WorkGang: public AbstractWorkGang { 170 // To get access to the GangTaskDispatcher instance. 171 friend class GangWorker; 172 173 // Never deleted. 174 ~WorkGang(); 175 176 GangTaskDispatcher* const _dispatcher; 177 GangTaskDispatcher* dispatcher()const { 178 return _dispatcher; 179 } 180 181 public: 182 WorkGang(const char* name, 183 uint workers, 184 bool are_GC_task_threads, 185 bool are_ConcurrentGC_threads); 186 187 // Run a task, returns when the task is done. 188 virtual void run_task(AbstractGangTask* task); 189 190 protected: 191 virtual AbstractGangWorker* allocate_worker(uint which); 192 193 private: 194 void print_worker_started_task(AbstractGangTask* task, uint worker_id); 195 void print_worker_finished_task(AbstractGangTask* task, uint worker_id); 196 }; 197 198 // Several instances of this class run in parallel as workers for a gang. 199 class AbstractGangWorker: public WorkerThread { 200 public: 201 AbstractGangWorker(AbstractWorkGang* gang, uint id); 202 203 // The only real method: run a task for the gang. 204 virtual void run(); 205 // Predicate for Thread 206 virtual bool is_GC_task_thread() const; 207 virtual bool is_ConcurrentGC_thread() const; 208 // Printing 209 void print_on(outputStream* st) const; 210 virtual void print() const { print_on(tty); } 211 212 protected: 213 AbstractWorkGang* _gang; 214 215 virtual void initialize(); 216 virtual void loop() = 0; 217 218 AbstractWorkGang* gang() const { return _gang; } 219 }; 220 221 class GangWorker: public AbstractGangWorker { 222 public: 223 GangWorker(WorkGang* gang, uint id) : AbstractGangWorker(gang, id) {} 224 225 protected: 226 virtual void loop(); 227 228 private: 229 WorkData wait_for_task(); 230 void run_task(WorkData work); 231 void signal_task_done(); 232 233 void print_task_started(WorkData data); 234 void print_task_done(WorkData data); 235 236 WorkGang* gang() const { return (WorkGang*)_gang; } 237 }; 238 239 // A class that acts as a synchronisation barrier. Workers enter 240 // the barrier and must wait until all other workers have entered 241 // before any of them may leave. 242 243 class WorkGangBarrierSync : public StackObj { 244 protected: 245 Monitor _monitor; 246 uint _n_workers; 247 uint _n_completed; 248 bool _should_reset; 249 bool _aborted; 250 251 Monitor* monitor() { return &_monitor; } 252 uint n_workers() { return _n_workers; } 253 uint n_completed() { return _n_completed; } 254 bool should_reset() { return _should_reset; } 255 bool aborted() { return _aborted; } 256 257 void zero_completed() { _n_completed = 0; } |