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