1 /* 2 * Copyright (c) 2018, 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/collectedHeap.hpp" 27 #include "gc/shared/workerPolicy.hpp" 28 #include "logging/log.hpp" 29 #include "memory/universe.hpp" 30 #include "runtime/os.inline.hpp" 31 #include "runtime/vm_version.hpp" 32 33 bool WorkerPolicy::_debug_perturbation = false; 34 uint WorkerPolicy::_parallel_worker_threads = 0; 35 bool WorkerPolicy::_parallel_worker_threads_initialized = false; 36 37 uint WorkerPolicy::nof_parallel_worker_threads(uint num, 38 uint den, 39 uint switch_pt) { 40 if (FLAG_IS_DEFAULT(ParallelGCThreads)) { 41 assert(ParallelGCThreads == 0, "Default ParallelGCThreads is not 0"); 42 uint threads; 43 // For very large machines, there are diminishing returns 44 // for large numbers of worker threads. Instead of 45 // hogging the whole system, use a fraction of the workers for every 46 // processor after the first 8. For example, on a 72 cpu machine 47 // and a chosen fraction of 5/8 48 // use 8 + (72 - 8) * (5/8) == 48 worker threads. 49 uint ncpus = (uint) os::initial_active_processor_count(); 50 threads = (ncpus <= switch_pt) ? 51 ncpus : 52 (switch_pt + ((ncpus - switch_pt) * num) / den); 53 #ifndef _LP64 54 // On 32-bit binaries the virtual address space available to the JVM 55 // is usually limited to 2-3 GB (depends on the platform). 56 // Do not use up address space with too many threads (stacks and per-thread 57 // data). Note that x86 apps running on Win64 have 2 stacks per thread. 58 // GC may more generally scale down threads by max heap size (etc), but the 59 // consequences of over-provisioning threads are higher on 32-bit JVMS, 60 // so add hard limit here: 61 threads = MIN2(threads, (2 * switch_pt)); 62 #endif 63 return threads; 64 } else { 65 return ParallelGCThreads; 66 } 67 } 68 69 uint WorkerPolicy::calc_parallel_worker_threads() { 70 uint den = VM_Version::parallel_worker_threads_denominator(); 71 return nof_parallel_worker_threads(5, den, 8); 72 } 73 74 uint WorkerPolicy::parallel_worker_threads() { 75 if (!_parallel_worker_threads_initialized) { 76 if (FLAG_IS_DEFAULT(ParallelGCThreads)) { 77 _parallel_worker_threads = WorkerPolicy::calc_parallel_worker_threads(); 78 } else { 79 _parallel_worker_threads = ParallelGCThreads; 80 } 81 _parallel_worker_threads_initialized = true; 82 } 83 return _parallel_worker_threads; 84 } 85 86 // If the number of GC threads was set on the command line, use it. 87 // Else 88 // Calculate the number of GC threads based on the number of Java threads. 89 // Calculate the number of GC threads based on the size of the heap. 90 // Use the larger. 91 uint WorkerPolicy::calc_default_active_workers(uintx total_workers, 92 const uintx min_workers, 93 uintx active_workers, 94 uintx application_workers) { 95 // If the user has specifically set the number of GC threads, use them. 96 97 // If the user has turned off using a dynamic number of GC threads 98 // or the users has requested a specific number, set the active 99 // number of workers to all the workers. 100 101 uintx new_active_workers = total_workers; 102 uintx prev_active_workers = active_workers; 103 uintx active_workers_by_JT = 0; 104 uintx active_workers_by_heap_size = 0; 105 106 // Always use at least min_workers but use up to 107 // GCThreadsPerJavaThreads * application threads. 108 active_workers_by_JT = 109 MAX2((uintx) GCWorkersPerJavaThread * application_workers, 110 min_workers); 111 112 // Choose a number of GC threads based on the current size 113 // of the heap. This may be complicated because the size of 114 // the heap depends on factors such as the throughput goal. 115 // Still a large heap should be collected by more GC threads. 116 active_workers_by_heap_size = 117 MAX2((size_t) 2U, Universe::heap()->capacity() / HeapSizePerGCThread); 118 119 uintx max_active_workers = 120 MAX2(active_workers_by_JT, active_workers_by_heap_size); 121 122 new_active_workers = MIN2(max_active_workers, (uintx) total_workers); 123 124 // Increase GC workers instantly but decrease them more 125 // slowly. 126 if (new_active_workers < prev_active_workers) { 127 new_active_workers = 128 MAX2(min_workers, (prev_active_workers + new_active_workers) / 2); 129 } 130 131 // Check once more that the number of workers is within the limits. 132 assert(min_workers <= total_workers, "Minimum workers not consistent with total workers"); 133 assert(new_active_workers >= min_workers, "Minimum workers not observed"); 134 assert(new_active_workers <= total_workers, "Total workers not observed"); 135 136 if (ForceDynamicNumberOfGCThreads) { 137 // Assume this is debugging and jiggle the number of GC threads. 138 if (new_active_workers == prev_active_workers) { 139 if (new_active_workers < total_workers) { 140 new_active_workers++; 141 } else if (new_active_workers > min_workers) { 142 new_active_workers--; 143 } 144 } 145 if (new_active_workers == total_workers) { 146 if (_debug_perturbation) { 147 new_active_workers = min_workers; 148 } 149 _debug_perturbation = !_debug_perturbation; 150 } 151 assert((new_active_workers <= ParallelGCThreads) && 152 (new_active_workers >= min_workers), 153 "Jiggled active workers too much"); 154 } 155 156 log_trace(gc, task)("WorkerPolicy::calc_default_active_workers() : " 157 "active_workers(): " UINTX_FORMAT " new_active_workers: " UINTX_FORMAT " " 158 "prev_active_workers: " UINTX_FORMAT "\n" 159 " active_workers_by_JT: " UINTX_FORMAT " active_workers_by_heap_size: " UINTX_FORMAT, 160 active_workers, new_active_workers, prev_active_workers, 161 active_workers_by_JT, active_workers_by_heap_size); 162 assert(new_active_workers > 0, "Always need at least 1"); 163 return new_active_workers; 164 } 165 166 uint WorkerPolicy::calc_active_workers(uintx total_workers, 167 uintx active_workers, 168 uintx application_workers) { 169 // If the user has specifically set the number of GC threads, use them. 170 171 // If the user has turned off using a dynamic number of GC threads 172 // or the users has requested a specific number, set the active 173 // number of workers to all the workers. 174 175 uint new_active_workers; 176 if (!UseDynamicNumberOfGCThreads || 177 (!FLAG_IS_DEFAULT(ParallelGCThreads) && !ForceDynamicNumberOfGCThreads)) { 178 new_active_workers = total_workers; 179 } else { 180 uintx min_workers = (total_workers == 1) ? 1 : 2; 181 new_active_workers = calc_default_active_workers(total_workers, 182 min_workers, 183 active_workers, 184 application_workers); 185 } 186 assert(new_active_workers > 0, "Always need at least 1"); 187 return new_active_workers; 188 } 189 190 uint WorkerPolicy::calc_active_conc_workers(uintx total_workers, 191 uintx active_workers, 192 uintx application_workers) { 193 if (!UseDynamicNumberOfGCThreads || 194 (!FLAG_IS_DEFAULT(ConcGCThreads) && !ForceDynamicNumberOfGCThreads)) { 195 return ConcGCThreads; 196 } else { 197 uint no_of_gc_threads = calc_default_active_workers(total_workers, 198 1, /* Minimum number of workers */ 199 active_workers, 200 application_workers); 201 return no_of_gc_threads; 202 } 203 }