1 /*
   2  * Copyright (c) 2001, 2016, 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 #include "precompiled.hpp"
  26 #include "gc/shared/gcId.hpp"
  27 #include "gc/shared/workgroup.hpp"
  28 #include "gc/shared/adaptiveSizePolicy.inline.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "runtime/atomic.inline.hpp"
  32 #include "runtime/os.hpp"
  33 #include "runtime/semaphore.hpp"
  34 #include "runtime/thread.inline.hpp"
  35 
  36 // Definitions of WorkGang methods.
  37 
  38 // The current implementation will exit if the allocation
  39 // of any worker fails.  Still, return a boolean so that
  40 // a future implementation can possibly do a partial
  41 // initialization of the workers and report such to the
  42 // caller.
  43 bool AbstractWorkGang::initialize_workers() {
  44 
  45   if (TraceWorkGang) {
  46     tty->print_cr("Constructing work gang %s with %d threads",
  47                   name(),
  48                   total_workers());
  49   }
  50   _workers = NEW_C_HEAP_ARRAY(AbstractGangWorker*, total_workers(), mtInternal);
  51   if (_workers == NULL) {
  52     vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GangWorker array.");
  53     return false;
  54   }
  55 
  56   _active_workers = AdaptiveSizePolicy::initial_number_of_workers();
  57   return add_workers(true);
  58 }
  59 
  60 
  61 AbstractGangWorker* AbstractWorkGang::install_worker(uint worker_id) {
  62   AbstractGangWorker* new_worker = allocate_worker(worker_id);
  63   set_thread(worker_id, new_worker);
  64   return new_worker;
  65 }
  66 
  67 bool AbstractWorkGang::add_workers(bool initializing) {
  68 
  69   os::ThreadType worker_type;
  70   if (are_ConcurrentGC_threads()) {
  71     worker_type = os::cgc_thread;
  72   } else {
  73     worker_type = os::pgc_thread;
  74   }
  75 
  76   return AdaptiveSizePolicy::add_workers(this,
  77                                          _active_workers,
  78                                          _total_workers,
  79                                          _created_workers,
  80                                          worker_type,
  81                                          initializing);
  82 }
  83 
  84 AbstractGangWorker* AbstractWorkGang::worker(uint i) const {
  85   // Array index bounds checking.
  86   AbstractGangWorker* result = NULL;
  87   assert(_workers != NULL, "No workers for indexing");
  88   assert(i < total_workers(), "Worker index out of bounds");
  89   result = _workers[i];
  90   assert(result != NULL, "Indexing to null worker");
  91   return result;
  92 }
  93 
  94 void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
  95   uint workers = created_workers();
  96   for (uint i = 0; i < workers; i++) {
  97     worker(i)->print_on(st);
  98     st->cr();
  99   }
 100 }
 101 
 102 void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
 103   assert(tc != NULL, "Null ThreadClosure");
 104   uint workers = created_workers();
 105   for (uint i = 0; i < workers; i++) {
 106     tc->do_thread(worker(i));
 107   }
 108 }
 109 
 110 // WorkGang dispatcher implemented with semaphores.
 111 //
 112 // Semaphores don't require the worker threads to re-claim the lock when they wake up.
 113 // This helps lowering the latency when starting and stopping the worker threads.
 114 class SemaphoreGangTaskDispatcher : public GangTaskDispatcher {
 115   // The task currently being dispatched to the GangWorkers.
 116   AbstractGangTask* _task;
 117 
 118   volatile uint _started;
 119   volatile uint _not_finished;
 120 
 121   // Semaphore used to start the GangWorkers.
 122   Semaphore* _start_semaphore;
 123   // Semaphore used to notify the coordinator that all workers are done.
 124   Semaphore* _end_semaphore;
 125 
 126 public:
 127   SemaphoreGangTaskDispatcher() :
 128       _task(NULL),
 129       _started(0),
 130       _not_finished(0),
 131       _start_semaphore(new Semaphore()),
 132       _end_semaphore(new Semaphore())
 133 { }
 134 
 135   ~SemaphoreGangTaskDispatcher() {
 136     delete _start_semaphore;
 137     delete _end_semaphore;
 138   }
 139 
 140   void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
 141     // No workers are allowed to read the state variables until they have been signaled.
 142     _task         = task;
 143     _not_finished = num_workers;
 144 
 145     // Dispatch 'num_workers' number of tasks.
 146     _start_semaphore->signal(num_workers);
 147 
 148     // Wait for the last worker to signal the coordinator.
 149     _end_semaphore->wait();
 150 
 151     // No workers are allowed to read the state variables after the coordinator has been signaled.
 152     assert(_not_finished == 0, "%d not finished workers?", _not_finished);
 153     _task    = NULL;
 154     _started = 0;
 155 
 156   }
 157 
 158   WorkData worker_wait_for_task() {
 159     // Wait for the coordinator to dispatch a task.
 160     _start_semaphore->wait();
 161 
 162     uint num_started = (uint) Atomic::add(1, (volatile jint*)&_started);
 163 
 164     // Subtract one to get a zero-indexed worker id.
 165     uint worker_id = num_started - 1;
 166 
 167     return WorkData(_task, worker_id);
 168   }
 169 
 170   void worker_done_with_task() {
 171     // Mark that the worker is done with the task.
 172     // The worker is not allowed to read the state variables after this line.
 173     uint not_finished = (uint) Atomic::add(-1, (volatile jint*)&_not_finished);
 174 
 175     // The last worker signals to the coordinator that all work is completed.
 176     if (not_finished == 0) {
 177       _end_semaphore->signal();
 178     }
 179   }
 180 };
 181 
 182 class MutexGangTaskDispatcher : public GangTaskDispatcher {
 183   AbstractGangTask* _task;
 184 
 185   volatile uint _started;
 186   volatile uint _finished;
 187   volatile uint _num_workers;
 188 
 189   Monitor* _monitor;
 190 
 191  public:
 192   MutexGangTaskDispatcher()
 193       : _task(NULL),
 194         _monitor(new Monitor(Monitor::leaf, "WorkGang dispatcher lock", false, Monitor::_safepoint_check_never)),
 195         _started(0),
 196         _finished(0),
 197         _num_workers(0) {}
 198 
 199   ~MutexGangTaskDispatcher() {
 200     delete _monitor;
 201   }
 202 
 203   void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
 204     MutexLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
 205 
 206     _task        = task;
 207     _num_workers = num_workers;
 208 
 209     // Tell the workers to get to work.
 210     _monitor->notify_all();
 211 
 212     // Wait for them to finish.
 213     while (_finished < _num_workers) {
 214       _monitor->wait(/* no_safepoint_check */ true);
 215     }
 216 
 217     _task        = NULL;
 218     _num_workers = 0;
 219     _started     = 0;
 220     _finished    = 0;
 221   }
 222 
 223   WorkData worker_wait_for_task() {
 224     MonitorLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
 225 
 226     while (_num_workers == 0 || _started == _num_workers) {
 227       _monitor->wait(/* no_safepoint_check */ true);
 228     }
 229 
 230     _started++;
 231 
 232     // Subtract one to get a zero-indexed worker id.
 233     uint worker_id = _started - 1;
 234 
 235     return WorkData(_task, worker_id);
 236   }
 237 
 238   void worker_done_with_task() {
 239     MonitorLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
 240 
 241     _finished++;
 242 
 243     if (_finished == _num_workers) {
 244       // This will wake up all workers and not only the coordinator.
 245       _monitor->notify_all();
 246     }
 247   }
 248 };
 249 
 250 static GangTaskDispatcher* create_dispatcher() {
 251   if (UseSemaphoreGCThreadsSynchronization) {
 252     return new SemaphoreGangTaskDispatcher();
 253   }
 254 
 255   return new MutexGangTaskDispatcher();
 256 }
 257 
 258 WorkGang::WorkGang(const char* name,
 259                    uint  workers,
 260                    bool  are_GC_task_threads,
 261                    bool  are_ConcurrentGC_threads) :
 262     AbstractWorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads),
 263     _dispatcher(create_dispatcher())
 264 { }
 265 
 266 AbstractGangWorker* WorkGang::allocate_worker(uint worker_id) {
 267   return new GangWorker(this, worker_id);
 268 }
 269 
 270 void WorkGang::run_task(AbstractGangTask* task) {
 271   _dispatcher->coordinator_execute_on_workers(task, active_workers());
 272 }
 273 
 274 AbstractGangWorker::AbstractGangWorker(AbstractWorkGang* gang, uint id) {
 275   _gang = gang;
 276   set_id(id);
 277   set_name("%s#%d", gang->name(), id);
 278 }
 279 
 280 void AbstractGangWorker::run() {
 281   initialize();
 282   loop();
 283 }
 284 
 285 void AbstractGangWorker::initialize() {
 286   this->record_stack_base_and_size();
 287   this->initialize_named_thread();
 288   assert(_gang != NULL, "No gang to run in");
 289   os::set_priority(this, NearMaxPriority);
 290   if (TraceWorkGang) {
 291     tty->print_cr("Running gang worker for gang %s id %u",
 292                   gang()->name(), id());
 293   }
 294   // The VM thread should not execute here because MutexLocker's are used
 295   // as (opposed to MutexLockerEx's).
 296   assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
 297          " of a work gang");
 298 }
 299 
 300 bool AbstractGangWorker::is_GC_task_thread() const {
 301   return gang()->are_GC_task_threads();
 302 }
 303 
 304 bool AbstractGangWorker::is_ConcurrentGC_thread() const {
 305   return gang()->are_ConcurrentGC_threads();
 306 }
 307 
 308 void AbstractGangWorker::print_on(outputStream* st) const {
 309   st->print("\"%s\" ", name());
 310   Thread::print_on(st);
 311   st->cr();
 312 }
 313 
 314 WorkData GangWorker::wait_for_task() {
 315   return gang()->dispatcher()->worker_wait_for_task();
 316 }
 317 
 318 void GangWorker::signal_task_done() {
 319   gang()->dispatcher()->worker_done_with_task();
 320 }
 321 
 322 void GangWorker::print_task_started(WorkData data) {
 323   if (TraceWorkGang) {
 324     tty->print_cr("Running work gang %s task %s worker %u", name(), data._task->name(), data._worker_id);
 325   }
 326 }
 327 
 328 void GangWorker::print_task_done(WorkData data) {
 329   if (TraceWorkGang) {
 330     tty->print_cr("\nFinished work gang %s task %s worker %u", name(), data._task->name(), data._worker_id);
 331     Thread* me = Thread::current();
 332     tty->print_cr("  T: " PTR_FORMAT "  VM_thread: %d", p2i(me), me->is_VM_thread());
 333   }
 334 }
 335 
 336 void GangWorker::run_task(WorkData data) {
 337   print_task_started(data);
 338 
 339   GCIdMark gc_id_mark(data._task->gc_id());
 340   data._task->work(data._worker_id);
 341 
 342   print_task_done(data);
 343 }
 344 
 345 void GangWorker::loop() {
 346   while (true) {
 347     WorkData data = wait_for_task();
 348 
 349     run_task(data);
 350 
 351     signal_task_done();
 352   }
 353 }
 354 
 355 // *** WorkGangBarrierSync
 356 
 357 WorkGangBarrierSync::WorkGangBarrierSync()
 358   : _monitor(Mutex::safepoint, "work gang barrier sync", true,
 359              Monitor::_safepoint_check_never),
 360     _n_workers(0), _n_completed(0), _should_reset(false), _aborted(false) {
 361 }
 362 
 363 WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name)
 364   : _monitor(Mutex::safepoint, name, true, Monitor::_safepoint_check_never),
 365     _n_workers(n_workers), _n_completed(0), _should_reset(false), _aborted(false) {
 366 }
 367 
 368 void WorkGangBarrierSync::set_n_workers(uint n_workers) {
 369   _n_workers    = n_workers;
 370   _n_completed  = 0;
 371   _should_reset = false;
 372   _aborted      = false;
 373 }
 374 
 375 bool WorkGangBarrierSync::enter() {
 376   MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
 377   if (should_reset()) {
 378     // The should_reset() was set and we are the first worker to enter
 379     // the sync barrier. We will zero the n_completed() count which
 380     // effectively resets the barrier.
 381     zero_completed();
 382     set_should_reset(false);
 383   }
 384   inc_completed();
 385   if (n_completed() == n_workers()) {
 386     // At this point we would like to reset the barrier to be ready in
 387     // case it is used again. However, we cannot set n_completed() to
 388     // 0, even after the notify_all(), given that some other workers
 389     // might still be waiting for n_completed() to become ==
 390     // n_workers(). So, if we set n_completed() to 0, those workers
 391     // will get stuck (as they will wake up, see that n_completed() !=
 392     // n_workers() and go back to sleep). Instead, we raise the
 393     // should_reset() flag and the barrier will be reset the first
 394     // time a worker enters it again.
 395     set_should_reset(true);
 396     monitor()->notify_all();
 397   } else {
 398     while (n_completed() != n_workers() && !aborted()) {
 399       monitor()->wait(/* no_safepoint_check */ true);
 400     }
 401   }
 402   return !aborted();
 403 }
 404 
 405 void WorkGangBarrierSync::abort() {
 406   MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
 407   set_aborted();
 408   monitor()->notify_all();
 409 }
 410 
 411 // SubTasksDone functions.
 412 
 413 SubTasksDone::SubTasksDone(uint n) :
 414   _n_tasks(n), _tasks(NULL) {
 415   _tasks = NEW_C_HEAP_ARRAY(uint, n, mtInternal);
 416   guarantee(_tasks != NULL, "alloc failure");
 417   clear();
 418 }
 419 
 420 bool SubTasksDone::valid() {
 421   return _tasks != NULL;
 422 }
 423 
 424 void SubTasksDone::clear() {
 425   for (uint i = 0; i < _n_tasks; i++) {
 426     _tasks[i] = 0;
 427   }
 428   _threads_completed = 0;
 429 #ifdef ASSERT
 430   _claimed = 0;
 431 #endif
 432 }
 433 
 434 bool SubTasksDone::is_task_claimed(uint t) {
 435   assert(t < _n_tasks, "bad task id.");
 436   uint old = _tasks[t];
 437   if (old == 0) {
 438     old = Atomic::cmpxchg(1, &_tasks[t], 0);
 439   }
 440   assert(_tasks[t] == 1, "What else?");
 441   bool res = old != 0;
 442 #ifdef ASSERT
 443   if (!res) {
 444     assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
 445     Atomic::inc((volatile jint*) &_claimed);
 446   }
 447 #endif
 448   return res;
 449 }
 450 
 451 void SubTasksDone::all_tasks_completed(uint n_threads) {
 452   jint observed = _threads_completed;
 453   jint old;
 454   do {
 455     old = observed;
 456     observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
 457   } while (observed != old);
 458   // If this was the last thread checking in, clear the tasks.
 459   uint adjusted_thread_count = (n_threads == 0 ? 1 : n_threads);
 460   if (observed + 1 == (jint)adjusted_thread_count) {
 461     clear();
 462   }
 463 }
 464 
 465 
 466 SubTasksDone::~SubTasksDone() {
 467   if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
 468 }
 469 
 470 // *** SequentialSubTasksDone
 471 
 472 void SequentialSubTasksDone::clear() {
 473   _n_tasks   = _n_claimed   = 0;
 474   _n_threads = _n_completed = 0;
 475 }
 476 
 477 bool SequentialSubTasksDone::valid() {
 478   return _n_threads > 0;
 479 }
 480 
 481 bool SequentialSubTasksDone::is_task_claimed(uint& t) {
 482   uint* n_claimed_ptr = &_n_claimed;
 483   t = *n_claimed_ptr;
 484   while (t < _n_tasks) {
 485     jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t);
 486     if (res == (jint)t) {
 487       return false;
 488     }
 489     t = *n_claimed_ptr;
 490   }
 491   return true;
 492 }
 493 
 494 bool SequentialSubTasksDone::all_tasks_completed() {
 495   uint* n_completed_ptr = &_n_completed;
 496   uint  complete        = *n_completed_ptr;
 497   while (true) {
 498     uint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete);
 499     if (res == complete) {
 500       break;
 501     }
 502     complete = res;
 503   }
 504   if (complete+1 == _n_threads) {
 505     clear();
 506     return true;
 507   }
 508   return false;
 509 }