1 /*
2 * Copyright (c) 2018, 2021, 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/globals_extension.hpp"
31 #include "runtime/os.hpp"
32 #include "runtime/vm_version.hpp"
33
34 uint WorkerPolicy::_parallel_worker_threads = 0;
35 bool WorkerPolicy::_parallel_worker_threads_initialized = false;
36
nof_parallel_worker_threads(uint num,uint den,uint switch_pt)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
calc_parallel_worker_threads()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
parallel_worker_threads()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.
calc_default_active_workers(uintx total_workers,const uintx min_workers,uintx active_workers,uintx application_workers)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 log_trace(gc, task)("WorkerPolicy::calc_default_active_workers() : "
137 "active_workers(): " UINTX_FORMAT " new_active_workers: " UINTX_FORMAT " "
138 "prev_active_workers: " UINTX_FORMAT "\n"
139 " active_workers_by_JT: " UINTX_FORMAT " active_workers_by_heap_size: " UINTX_FORMAT,
140 active_workers, new_active_workers, prev_active_workers,
141 active_workers_by_JT, active_workers_by_heap_size);
142 assert(new_active_workers > 0, "Always need at least 1");
143 return new_active_workers;
144 }
145
calc_active_workers(uintx total_workers,uintx active_workers,uintx application_workers)146 uint WorkerPolicy::calc_active_workers(uintx total_workers,
147 uintx active_workers,
148 uintx application_workers) {
149 // If the user has specifically set the number of GC threads, use them.
150
151 // If the user has turned off using a dynamic number of GC threads
152 // or the users has requested a specific number, set the active
153 // number of workers to all the workers.
154
155 uint new_active_workers;
156 if (!UseDynamicNumberOfGCThreads || !FLAG_IS_DEFAULT(ParallelGCThreads)) {
157 new_active_workers = total_workers;
158 } else {
159 uintx min_workers = (total_workers == 1) ? 1 : 2;
160 new_active_workers = calc_default_active_workers(total_workers,
161 min_workers,
162 active_workers,
163 application_workers);
164 }
165 assert(new_active_workers > 0, "Always need at least 1");
166 return new_active_workers;
167 }
168
calc_active_conc_workers(uintx total_workers,uintx active_workers,uintx application_workers)169 uint WorkerPolicy::calc_active_conc_workers(uintx total_workers,
170 uintx active_workers,
171 uintx application_workers) {
172 if (!UseDynamicNumberOfGCThreads || !FLAG_IS_DEFAULT(ConcGCThreads)) {
173 return ConcGCThreads;
174 } else {
175 uint no_of_gc_threads = calc_default_active_workers(total_workers,
176 1, /* Minimum number of workers */
177 active_workers,
178 application_workers);
179 return no_of_gc_threads;
180 }
181 }
182