1 /*
   2  * Copyright (c) 2015, 2019, 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 #include "precompiled.hpp"
  25 #include "gc/z/zGlobals.hpp"
  26 #include "gc/z/zTask.hpp"
  27 #include "gc/z/zThread.hpp"
  28 #include "gc/z/zWorkers.inline.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 #include "runtime/safepoint.hpp"
  31 
  32 class ZWorkersInitializeTask : public ZTask {
  33 private:
  34   const uint _nworkers;
  35   uint       _started;
  36   Monitor    _monitor;
  37 
  38 public:
  39   ZWorkersInitializeTask(uint nworkers) :
  40       ZTask("ZWorkersInitializeTask"),
  41       _nworkers(nworkers),
  42       _started(0),
  43       _monitor(Monitor::leaf,
  44                "ZWorkersInitialize",
  45                false /* allow_vm_block */,
  46                Monitor::_safepoint_check_never) {}
  47 
  48   virtual void work() {
  49     // Register as worker
  50     ZThread::set_worker();
  51 
  52     // Wait for all threads to start
  53     MonitorLocker ml(&_monitor, Monitor::_no_safepoint_check_flag);
  54     if (++_started == _nworkers) {
  55       // All threads started
  56       ml.notify_all();
  57     } else {
  58       while (_started != _nworkers) {
  59         ml.wait();
  60       }
  61     }
  62   }
  63 };
  64 
  65 ZWorkers::ZWorkers() :
  66     _boost(false),
  67     _workers("ZWorker",
  68              nworkers(),
  69              true /* are_GC_task_threads */,
  70              true /* are_ConcurrentGC_threads */) {
  71 
  72   log_info(gc, init)("Workers: %u parallel, %u concurrent", nparallel(), nconcurrent());
  73 
  74   // Initialize worker threads
  75   _workers.initialize_workers();
  76   _workers.update_active_workers(nworkers());
  77   if (_workers.active_workers() != nworkers()) {
  78     vm_exit_during_initialization("Failed to create ZWorkers");
  79   }
  80 
  81   // Execute task to register threads as workers. This also helps
  82   // reduce latency in early GC pauses, which otherwise would have
  83   // to take on any warmup costs.
  84   ZWorkersInitializeTask task(nworkers());
  85   run(&task, nworkers());
  86 }
  87 
  88 void ZWorkers::set_boost(bool boost) {
  89   if (boost) {
  90     log_debug(gc)("Boosting workers");
  91   }
  92 
  93   _boost = boost;
  94 }
  95 
  96 void ZWorkers::run(ZTask* task, uint nworkers) {
  97   log_debug(gc, task)("Executing Task: %s, Active Workers: %u", task->name(), nworkers);
  98   _workers.update_active_workers(nworkers);
  99   _workers.run_task(task->gang_task());
 100 }
 101 
 102 void ZWorkers::run_parallel(ZTask* task) {
 103   run(task, nparallel());
 104 }
 105 
 106 void ZWorkers::run_concurrent(ZTask* task) {
 107   run(task, nconcurrent());
 108 }
 109 
 110 void ZWorkers::threads_do(ThreadClosure* tc) const {
 111   _workers.threads_do(tc);
 112 }
 113 
 114 void ZWorkers::print_threads_on(outputStream* st) const {
 115   _workers.print_worker_threads_on(st);
 116 }