1 /*
   2  * Copyright (c) 2018, 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/shared/gcLogPrecious.hpp"
  26 #include "gc/shared/workgroup.hpp"
  27 #include "gc/z/zRuntimeWorkers.hpp"
  28 #include "gc/z/zThread.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 
  31 class ZRuntimeWorkersInitializeTask : public AbstractGangTask {
  32 private:
  33   const uint _nworkers;
  34   uint       _started;
  35   Monitor    _monitor;
  36 
  37 public:
  38   ZRuntimeWorkersInitializeTask(uint nworkers) :
  39       AbstractGangTask("ZRuntimeWorkersInitializeTask"),
  40       _nworkers(nworkers),
  41       _started(0),
  42       _monitor(Monitor::leaf,
  43                "ZRuntimeWorkersInitialize",
  44                false /* allow_vm_block */,
  45                Monitor::_safepoint_check_never) {}
  46 
  47   virtual void work(uint worker_id) {
  48     // Register as runtime worker
  49     ZThread::set_runtime_worker();
  50 
  51     // Wait for all threads to start
  52     MonitorLocker ml(&_monitor, Monitor::_no_safepoint_check_flag);
  53     if (++_started == _nworkers) {
  54       // All threads started
  55       ml.notify_all();
  56     } else {
  57       while (_started != _nworkers) {
  58         ml.wait();
  59       }
  60     }
  61   }
  62 };
  63 
  64 ZRuntimeWorkers::ZRuntimeWorkers() :
  65     _workers("RuntimeWorker",
  66              nworkers(),
  67              false /* are_GC_task_threads */,
  68              false /* are_ConcurrentGC_threads */) {
  69 
  70   log_info_p(gc, init)("Runtime Workers: %u parallel", nworkers());
  71 
  72   // Initialize worker threads
  73   _workers.initialize_workers();
  74   _workers.update_active_workers(nworkers());
  75   if (_workers.active_workers() != nworkers()) {
  76     vm_exit_during_initialization("Failed to create ZRuntimeWorkers");
  77   }
  78 
  79   // Execute task to register threads as runtime workers. This also
  80   // helps reduce latency in early safepoints, which otherwise would
  81   // have to take on any warmup costs.
  82   ZRuntimeWorkersInitializeTask task(nworkers());
  83   _workers.run_task(&task);
  84 }
  85 
  86 uint ZRuntimeWorkers::nworkers() const {
  87   return ParallelGCThreads;
  88 }
  89 
  90 WorkGang* ZRuntimeWorkers::workers() {
  91   return &_workers;
  92 }
  93 
  94 void ZRuntimeWorkers::threads_do(ThreadClosure* tc) const {
  95   _workers.threads_do(tc);
  96 }
  97 
  98 void ZRuntimeWorkers::print_threads_on(outputStream* st) const {
  99   _workers.print_worker_threads_on(st);
 100 }