< prev index next >

src/share/vm/gc/shared/workgroup.cpp

Print this page




  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 #include "precompiled.hpp"
  26 #include "gc/shared/workgroup.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/atomic.inline.hpp"
  30 #include "runtime/os.hpp"
  31 
  32 // Definitions of WorkGang methods.
  33 
  34 AbstractWorkGang::AbstractWorkGang(const char* name,
  35                                    bool  are_GC_task_threads,
  36                                    bool  are_ConcurrentGC_threads) :
  37   _name(name),
  38   _are_GC_task_threads(are_GC_task_threads),
  39   _are_ConcurrentGC_threads(are_ConcurrentGC_threads) {
  40 
  41   assert(!(are_GC_task_threads && are_ConcurrentGC_threads),
  42          "They cannot both be STW GC and Concurrent threads" );
  43 
  44   // Other initialization.
  45   _monitor = new Monitor(/* priority */       Mutex::leaf,
  46                          /* name */           "WorkGroup monitor",
  47                          /* allow_vm_block */ are_GC_task_threads,
  48                                               Monitor::_safepoint_check_sometimes);
  49   assert(monitor() != NULL, "Failed to allocate monitor");
  50   _task = NULL;
  51   _sequence_number = 0;
  52   _started_workers = 0;
  53   _finished_workers = 0;
  54 }
  55 
  56 WorkGang::WorkGang(const char* name,
  57                    uint        workers,
  58                    bool        are_GC_task_threads,
  59                    bool        are_ConcurrentGC_threads) :
  60   AbstractWorkGang(name, are_GC_task_threads, are_ConcurrentGC_threads) {
  61   _total_workers = workers;
  62 }
  63 
  64 GangWorker* WorkGang::allocate_worker(uint which) {
  65   GangWorker* new_worker = new GangWorker(this, which);
  66   return new_worker;
  67 }
  68 
  69 // The current implementation will exit if the allocation
  70 // of any worker fails.  Still, return a boolean so that
  71 // a future implementation can possibly do a partial
  72 // initialization of the workers and report such to the
  73 // caller.
  74 bool WorkGang::initialize_workers() {
  75 
  76   if (TraceWorkGang) {
  77     tty->print_cr("Constructing work gang %s with %d threads",
  78                   name(),
  79                   total_workers());
  80   }
  81   _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, total_workers(), mtInternal);
  82   if (gang_workers() == NULL) {
  83     vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GangWorker array.");
  84     return false;
  85   }
  86   os::ThreadType worker_type;
  87   if (are_ConcurrentGC_threads()) {
  88     worker_type = os::cgc_thread;
  89   } else {
  90     worker_type = os::pgc_thread;
  91   }
  92   for (uint worker = 0; worker < total_workers(); worker += 1) {
  93     GangWorker* new_worker = allocate_worker(worker);
  94     assert(new_worker != NULL, "Failed to allocate GangWorker");
  95     _gang_workers[worker] = new_worker;
  96     if (new_worker == NULL || !os::create_thread(new_worker, worker_type)) {
  97       vm_exit_out_of_memory(0, OOM_MALLOC_ERROR,
  98               "Cannot create worker GC thread. Out of system resources.");
  99       return false;
 100     }
 101     if (!DisableStartThread) {
 102       os::start_thread(new_worker);
 103     }
 104   }
 105   return true;
 106 }
 107 
 108 GangWorker* AbstractWorkGang::gang_worker(uint i) const {
 109   // Array index bounds checking.
 110   GangWorker* result = NULL;
 111   assert(gang_workers() != NULL, "No workers for indexing");
 112   assert(i < total_workers(), "Worker index out of bounds");
 113   result = _gang_workers[i];
 114   assert(result != NULL, "Indexing to null worker");
 115   return result;
 116 }
 117 







































 118 void WorkGang::run_task(AbstractGangTask* task) {
 119   run_task(task, total_workers());
 120 }
 121 
 122 void WorkGang::run_task(AbstractGangTask* task, uint no_of_parallel_workers) {
 123   // This thread is executed by the VM thread which does not block
 124   // on ordinary MutexLocker's.
 125   MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
 126   if (TraceWorkGang) {
 127     tty->print_cr("Running work gang %s task %s", name(), task->name());
 128   }
 129   // Tell all the workers to run a task.
 130   assert(task != NULL, "Running a null task");
 131   // Initialize.
 132   _task = task;
 133   _sequence_number += 1;
 134   _started_workers = 0;
 135   _finished_workers = 0;
 136   // Tell the workers to get to work.
 137   monitor()->notify_all();
 138   // Wait for them to be finished
 139   while (finished_workers() < no_of_parallel_workers) {
 140     if (TraceWorkGang) {
 141       tty->print_cr("Waiting in work gang %s: %u/%u finished sequence %d",
 142                     name(), finished_workers(), no_of_parallel_workers,
 143                     _sequence_number);
 144     }
 145     monitor()->wait(/* no_safepoint_check */ true);
 146   }
 147   _task = NULL;
 148   if (TraceWorkGang) {
 149     tty->print_cr("\nFinished work gang %s: %u/%u sequence %d",
 150                   name(), finished_workers(), no_of_parallel_workers,
 151                   _sequence_number);
 152     Thread* me = Thread::current();
 153     tty->print_cr("  T: " PTR_FORMAT "  VM_thread: %d", p2i(me), me->is_VM_thread());
 154   }
 155 }
 156 
 157 void FlexibleWorkGang::run_task(AbstractGangTask* task) {
 158   // If active_workers() is passed, _finished_workers
 159   // must only be incremented for workers that find non_null
 160   // work (as opposed to all those that just check that the
 161   // task is not null).
 162   WorkGang::run_task(task, (uint) active_workers());
 163 }
 164 
 165 void AbstractWorkGang::internal_worker_poll(WorkData* data) const {
 166   assert(monitor()->owned_by_self(), "worker_poll is an internal method");
 167   assert(data != NULL, "worker data is null");
 168   data->set_task(task());
 169   data->set_sequence_number(sequence_number());
 170 }
 171 
 172 void AbstractWorkGang::internal_note_start() {
 173   assert(monitor()->owned_by_self(), "note_finish is an internal method");
 174   _started_workers += 1;
 175 }
 176 
 177 void AbstractWorkGang::internal_note_finish() {
 178   assert(monitor()->owned_by_self(), "note_finish is an internal method");
 179   _finished_workers += 1;
 180 }
 181 
 182 void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
 183   uint    num_thr = total_workers();
 184   for (uint i = 0; i < num_thr; i++) {
 185     gang_worker(i)->print_on(st);
 186     st->cr();
 187   }
 188 }
 189 
 190 void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
 191   assert(tc != NULL, "Null ThreadClosure");
 192   uint num_thr = total_workers();
 193   for (uint i = 0; i < num_thr; i++) {
 194     tc->do_thread(gang_worker(i));
 195   }
 196 }
 197 
 198 // GangWorker methods.
 199 
 200 GangWorker::GangWorker(AbstractWorkGang* gang, uint id) {
 201   _gang = gang;
 202   set_id(id);
 203   set_name("%s#%d", gang->name(), id);
 204 }
 205 
 206 void GangWorker::run() {
 207   initialize();
 208   loop();
 209 }
 210 
 211 void GangWorker::initialize() {
 212   this->initialize_thread_local_storage();
 213   this->record_stack_base_and_size();
 214   this->initialize_named_thread();
 215   assert(_gang != NULL, "No gang to run in");
 216   os::set_priority(this, NearMaxPriority);
 217   if (TraceWorkGang) {
 218     tty->print_cr("Running gang worker for gang %s id %u",
 219                   gang()->name(), id());
 220   }
 221   // The VM thread should not execute here because MutexLocker's are used
 222   // as (opposed to MutexLockerEx's).
 223   assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
 224          " of a work gang");
 225 }
 226 














 227 void GangWorker::loop() {
 228   int previous_sequence_number = 0;
 229   Monitor* gang_monitor = gang()->monitor();
 230   for ( ; ; ) {
 231     WorkData data;
 232     int part;  // Initialized below.
 233     {
 234       // Grab the gang mutex.
 235       MutexLocker ml(gang_monitor);
 236       // Wait for something to do.
 237       // Polling outside the while { wait } avoids missed notifies
 238       // in the outer loop.
 239       gang()->internal_worker_poll(&data);
 240       if (TraceWorkGang) {
 241         tty->print("Polled outside for work in gang %s worker %u",
 242                    gang()->name(), id());
 243         tty->print("  sequence: %d (prev: %d)",
 244                    data.sequence_number(), previous_sequence_number);
 245         if (data.task() != NULL) {
 246           tty->print("  task: %s", data.task()->name());


 282       tty->print("Work for work gang %s id %u task %s part %d",
 283                  gang()->name(), id(), data.task()->name(), part);
 284     }
 285     assert(data.task() != NULL, "Got null task");
 286     data.task()->work(part);
 287     {
 288       if (TraceWorkGang) {
 289         tty->print("Finish for work gang %s id %u task %s part %d",
 290                    gang()->name(), id(), data.task()->name(), part);
 291       }
 292       // Grab the gang mutex.
 293       MutexLocker ml(gang_monitor);
 294       gang()->internal_note_finish();
 295       // Tell the gang you are done.
 296       gang_monitor->notify_all();
 297       // Drop the gang mutex.
 298     }
 299     previous_sequence_number = data.sequence_number();
 300   }
 301 }
 302 
 303 bool GangWorker::is_GC_task_thread() const {
 304   return gang()->are_GC_task_threads();
 305 }
 306 
 307 bool GangWorker::is_ConcurrentGC_thread() const {
 308   return gang()->are_ConcurrentGC_threads();
 309 }
 310 
 311 void GangWorker::print_on(outputStream* st) const {
 312   st->print("\"%s\" ", name());
 313   Thread::print_on(st);
 314   st->cr();
 315 }
 316 
 317 // Printing methods
 318 
 319 const char* AbstractWorkGang::name() const {
 320   return _name;
 321 }
 322 
 323 #ifndef PRODUCT
 324 
 325 const char* AbstractGangTask::name() const {
 326   return _name;
 327 }
 328 
 329 #endif /* PRODUCT */
 330 
 331 // FlexibleWorkGang
 332 
 333 
 334 // *** WorkGangBarrierSync
 335 
 336 WorkGangBarrierSync::WorkGangBarrierSync()
 337   : _monitor(Mutex::safepoint, "work gang barrier sync", true,
 338              Monitor::_safepoint_check_never),
 339     _n_workers(0), _n_completed(0), _should_reset(false), _aborted(false) {
 340 }
 341 
 342 WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name)
 343   : _monitor(Mutex::safepoint, name, true, Monitor::_safepoint_check_never),
 344     _n_workers(n_workers), _n_completed(0), _should_reset(false), _aborted(false) {
 345 }
 346 
 347 void WorkGangBarrierSync::set_n_workers(uint n_workers) {
 348   _n_workers    = n_workers;
 349   _n_completed  = 0;
 350   _should_reset = false;
 351   _aborted      = false;
 352 }




  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 #include "precompiled.hpp"
  26 #include "gc/shared/workgroup.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/atomic.inline.hpp"
  30 #include "runtime/os.hpp"
  31 
  32 // Definitions of WorkGang methods.
  33 



































  34 // The current implementation will exit if the allocation
  35 // of any worker fails.  Still, return a boolean so that
  36 // a future implementation can possibly do a partial
  37 // initialization of the workers and report such to the
  38 // caller.
  39 bool AbstractWorkGang::initialize_workers() {
  40 
  41   if (TraceWorkGang) {
  42     tty->print_cr("Constructing work gang %s with %d threads",
  43                   name(),
  44                   total_workers());
  45   }
  46   _workers = NEW_C_HEAP_ARRAY(AbstractGangWorker*, total_workers(), mtInternal);
  47   if (_workers == NULL) {
  48     vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GangWorker array.");
  49     return false;
  50   }
  51   os::ThreadType worker_type;
  52   if (are_ConcurrentGC_threads()) {
  53     worker_type = os::cgc_thread;
  54   } else {
  55     worker_type = os::pgc_thread;
  56   }
  57   for (uint worker = 0; worker < total_workers(); worker += 1) {
  58     AbstractGangWorker* new_worker = allocate_worker(worker);
  59     assert(new_worker != NULL, "Failed to allocate GangWorker");
  60     _workers[worker] = new_worker;
  61     if (new_worker == NULL || !os::create_thread(new_worker, worker_type)) {
  62       vm_exit_out_of_memory(0, OOM_MALLOC_ERROR,
  63               "Cannot create worker GC thread. Out of system resources.");
  64       return false;
  65     }
  66     if (!DisableStartThread) {
  67       os::start_thread(new_worker);
  68     }
  69   }
  70   return true;
  71 }
  72 
  73 AbstractGangWorker* AbstractWorkGang::worker(uint i) const {
  74   // Array index bounds checking.
  75   AbstractGangWorker* result = NULL;
  76   assert(_workers != NULL, "No workers for indexing");
  77   assert(i < total_workers(), "Worker index out of bounds");
  78   result = _workers[i];
  79   assert(result != NULL, "Indexing to null worker");
  80   return result;
  81 }
  82 
  83 void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
  84   uint workers = total_workers();
  85   for (uint i = 0; i < workers; i++) {
  86     worker(i)->print_on(st);
  87     st->cr();
  88   }
  89 }
  90 
  91 void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
  92   assert(tc != NULL, "Null ThreadClosure");
  93   uint workers = total_workers();
  94   for (uint i = 0; i < workers; i++) {
  95     tc->do_thread(worker(i));
  96   }
  97 }
  98 
  99 WorkGang::WorkGang(const char* name,
 100                    uint        workers,
 101                    bool        are_GC_task_threads,
 102                    bool        are_ConcurrentGC_threads) :
 103     AbstractWorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads),
 104     _started_workers(0),
 105     _finished_workers(0),
 106     _sequence_number(0),
 107     _task(NULL) {
 108 
 109   // Other initialization.
 110   _monitor = new Monitor(/* priority */       Mutex::leaf,
 111                          /* name */           "WorkGroup monitor",
 112                          /* allow_vm_block */ are_GC_task_threads,
 113                                               Monitor::_safepoint_check_sometimes);
 114 
 115   assert(monitor() != NULL, "Failed to allocate monitor");
 116 }
 117 
 118 AbstractGangWorker* WorkGang::allocate_worker(uint worker_id) {
 119   return new GangWorker(this, worker_id);
 120 }
 121 
 122 void WorkGang::run_task(AbstractGangTask* task) {
 123   run_task(task, (uint)active_workers());
 124 }
 125 
 126 void WorkGang::run_task(AbstractGangTask* task, uint no_of_parallel_workers) {
 127   // This thread is executed by the VM thread which does not block
 128   // on ordinary MutexLocker's.
 129   MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
 130   if (TraceWorkGang) {
 131     tty->print_cr("Running work gang %s task %s", name(), task->name());
 132   }
 133   // Tell all the workers to run a task.
 134   assert(task != NULL, "Running a null task");
 135   // Initialize.
 136   _task = task;
 137   _sequence_number += 1;
 138   _started_workers = 0;
 139   _finished_workers = 0;
 140   // Tell the workers to get to work.
 141   monitor()->notify_all();
 142   // Wait for them to be finished
 143   while (finished_workers() < no_of_parallel_workers) {
 144     if (TraceWorkGang) {
 145       tty->print_cr("Waiting in work gang %s: %u/%u finished sequence %d",
 146                     name(), finished_workers(), no_of_parallel_workers,
 147                     _sequence_number);
 148     }
 149     monitor()->wait(/* no_safepoint_check */ true);
 150   }
 151   _task = NULL;
 152   if (TraceWorkGang) {
 153     tty->print_cr("\nFinished work gang %s: %u/%u sequence %d",
 154                   name(), finished_workers(), no_of_parallel_workers,
 155                   _sequence_number);
 156     Thread* me = Thread::current();
 157     tty->print_cr("  T: " PTR_FORMAT "  VM_thread: %d", p2i(me), me->is_VM_thread());
 158   }
 159 }
 160 
 161 void WorkGang::internal_worker_poll(WorkData* data) const {








 162   assert(monitor()->owned_by_self(), "worker_poll is an internal method");
 163   assert(data != NULL, "worker data is null");
 164   data->set_task(task());
 165   data->set_sequence_number(sequence_number());
 166 }
 167 
 168 void WorkGang::internal_note_start() {
 169   assert(monitor()->owned_by_self(), "note_finish is an internal method");
 170   _started_workers += 1;
 171 }
 172 
 173 void WorkGang::internal_note_finish() {
 174   assert(monitor()->owned_by_self(), "note_finish is an internal method");
 175   _finished_workers += 1;
 176 }
 177 
















 178 // GangWorker methods.
 179 
 180 AbstractGangWorker::AbstractGangWorker(AbstractWorkGang* gang, uint id) {
 181   _gang = gang;
 182   set_id(id);
 183   set_name("%s#%d", gang->name(), id);
 184 }
 185 
 186 void AbstractGangWorker::run() {
 187   initialize();
 188   loop();
 189 }
 190 
 191 void AbstractGangWorker::initialize() {
 192   this->initialize_thread_local_storage();
 193   this->record_stack_base_and_size();
 194   this->initialize_named_thread();
 195   assert(_gang != NULL, "No gang to run in");
 196   os::set_priority(this, NearMaxPriority);
 197   if (TraceWorkGang) {
 198     tty->print_cr("Running gang worker for gang %s id %u",
 199                   gang()->name(), id());
 200   }
 201   // The VM thread should not execute here because MutexLocker's are used
 202   // as (opposed to MutexLockerEx's).
 203   assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
 204          " of a work gang");
 205 }
 206 
 207 bool AbstractGangWorker::is_GC_task_thread() const {
 208   return gang()->are_GC_task_threads();
 209 }
 210 
 211 bool AbstractGangWorker::is_ConcurrentGC_thread() const {
 212   return gang()->are_ConcurrentGC_threads();
 213 }
 214 
 215 void AbstractGangWorker::print_on(outputStream* st) const {
 216   st->print("\"%s\" ", name());
 217   Thread::print_on(st);
 218   st->cr();
 219 }
 220 
 221 void GangWorker::loop() {
 222   int previous_sequence_number = 0;
 223   Monitor* gang_monitor = gang()->monitor();
 224   for ( ; ; ) {
 225     WorkData data;
 226     int part;  // Initialized below.
 227     {
 228       // Grab the gang mutex.
 229       MutexLocker ml(gang_monitor);
 230       // Wait for something to do.
 231       // Polling outside the while { wait } avoids missed notifies
 232       // in the outer loop.
 233       gang()->internal_worker_poll(&data);
 234       if (TraceWorkGang) {
 235         tty->print("Polled outside for work in gang %s worker %u",
 236                    gang()->name(), id());
 237         tty->print("  sequence: %d (prev: %d)",
 238                    data.sequence_number(), previous_sequence_number);
 239         if (data.task() != NULL) {
 240           tty->print("  task: %s", data.task()->name());


 276       tty->print("Work for work gang %s id %u task %s part %d",
 277                  gang()->name(), id(), data.task()->name(), part);
 278     }
 279     assert(data.task() != NULL, "Got null task");
 280     data.task()->work(part);
 281     {
 282       if (TraceWorkGang) {
 283         tty->print("Finish for work gang %s id %u task %s part %d",
 284                    gang()->name(), id(), data.task()->name(), part);
 285       }
 286       // Grab the gang mutex.
 287       MutexLocker ml(gang_monitor);
 288       gang()->internal_note_finish();
 289       // Tell the gang you are done.
 290       gang_monitor->notify_all();
 291       // Drop the gang mutex.
 292     }
 293     previous_sequence_number = data.sequence_number();
 294   }
 295 }































 296 
 297 // *** WorkGangBarrierSync
 298 
 299 WorkGangBarrierSync::WorkGangBarrierSync()
 300   : _monitor(Mutex::safepoint, "work gang barrier sync", true,
 301              Monitor::_safepoint_check_never),
 302     _n_workers(0), _n_completed(0), _should_reset(false), _aborted(false) {
 303 }
 304 
 305 WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name)
 306   : _monitor(Mutex::safepoint, name, true, Monitor::_safepoint_check_never),
 307     _n_workers(n_workers), _n_completed(0), _should_reset(false), _aborted(false) {
 308 }
 309 
 310 void WorkGangBarrierSync::set_n_workers(uint n_workers) {
 311   _n_workers    = n_workers;
 312   _n_completed  = 0;
 313   _should_reset = false;
 314   _aborted      = false;
 315 }


< prev index next >