51476
|
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 "classfile/symbolTable.hpp"
|
|
27 |
#include "classfile/stringTable.hpp"
|
|
28 |
#include "code/codeCache.hpp"
|
|
29 |
#include "gc/shared/parallelCleaning.hpp"
|
|
30 |
#include "memory/resourceArea.hpp"
|
|
31 |
#include "prims/resolvedMethodTable.hpp"
|
|
32 |
#include "logging/log.hpp"
|
|
33 |
|
|
34 |
StringCleaningTask::StringCleaningTask(BoolObjectClosure* is_alive, StringDedupUnlinkOrOopsDoClosure* dedup_closure, bool process_strings) :
|
|
35 |
AbstractGangTask("String Unlinking"),
|
|
36 |
_is_alive(is_alive),
|
|
37 |
_dedup_closure(dedup_closure),
|
|
38 |
_par_state_string(StringTable::weak_storage()),
|
|
39 |
_initial_string_table_size((int) StringTable::the_table()->table_size()),
|
|
40 |
_process_strings(process_strings), _strings_processed(0), _strings_removed(0) {
|
|
41 |
|
|
42 |
if (process_strings) {
|
|
43 |
StringTable::reset_dead_counter();
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
|
47 |
StringCleaningTask::~StringCleaningTask() {
|
|
48 |
log_info(gc, stringtable)(
|
|
49 |
"Cleaned string table, "
|
|
50 |
"strings: " SIZE_FORMAT " processed, " SIZE_FORMAT " removed",
|
|
51 |
strings_processed(), strings_removed());
|
|
52 |
if (_process_strings) {
|
|
53 |
StringTable::finish_dead_counter();
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
|
57 |
void StringCleaningTask::work(uint worker_id) {
|
|
58 |
int strings_processed = 0;
|
|
59 |
int strings_removed = 0;
|
|
60 |
if (_process_strings) {
|
|
61 |
StringTable::possibly_parallel_unlink(&_par_state_string, _is_alive, &strings_processed, &strings_removed);
|
|
62 |
Atomic::add(strings_processed, &_strings_processed);
|
|
63 |
Atomic::add(strings_removed, &_strings_removed);
|
|
64 |
}
|
|
65 |
if (_dedup_closure != NULL) {
|
|
66 |
StringDedup::parallel_unlink(_dedup_closure, worker_id);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
CodeCacheUnloadingTask::CodeCacheUnloadingTask(uint num_workers, BoolObjectClosure* is_alive, bool unloading_occurred) :
|
|
71 |
_is_alive(is_alive),
|
|
72 |
_unloading_occurred(unloading_occurred),
|
|
73 |
_num_workers(num_workers),
|
|
74 |
_first_nmethod(NULL),
|
|
75 |
_claimed_nmethod(NULL),
|
|
76 |
_postponed_list(NULL),
|
|
77 |
_num_entered_barrier(0) {
|
|
78 |
CompiledMethod::increase_unloading_clock();
|
|
79 |
// Get first alive nmethod
|
|
80 |
CompiledMethodIterator iter = CompiledMethodIterator();
|
|
81 |
if(iter.next_alive()) {
|
|
82 |
_first_nmethod = iter.method();
|
|
83 |
}
|
|
84 |
_claimed_nmethod = _first_nmethod;
|
|
85 |
}
|
|
86 |
|
|
87 |
CodeCacheUnloadingTask::~CodeCacheUnloadingTask() {
|
|
88 |
CodeCache::verify_clean_inline_caches();
|
|
89 |
|
|
90 |
CodeCache::set_needs_cache_clean(false);
|
|
91 |
guarantee(CodeCache::scavenge_root_nmethods() == NULL, "Must be");
|
|
92 |
|
|
93 |
CodeCache::verify_icholder_relocations();
|
|
94 |
}
|
|
95 |
|
|
96 |
Monitor* CodeCacheUnloadingTask::_lock = new Monitor(Mutex::leaf, "Code Cache Unload lock", false, Monitor::_safepoint_check_never);
|
|
97 |
|
|
98 |
void CodeCacheUnloadingTask::add_to_postponed_list(CompiledMethod* nm) {
|
|
99 |
CompiledMethod* old;
|
|
100 |
do {
|
|
101 |
old = _postponed_list;
|
|
102 |
nm->set_unloading_next(old);
|
|
103 |
} while (Atomic::cmpxchg(nm, &_postponed_list, old) != old);
|
|
104 |
}
|
|
105 |
|
|
106 |
void CodeCacheUnloadingTask::clean_nmethod(CompiledMethod* nm) {
|
|
107 |
bool postponed = nm->do_unloading_parallel(_is_alive, _unloading_occurred);
|
|
108 |
|
|
109 |
if (postponed) {
|
|
110 |
// This nmethod referred to an nmethod that has not been cleaned/unloaded yet.
|
|
111 |
add_to_postponed_list(nm);
|
|
112 |
}
|
|
113 |
|
|
114 |
// Mark that this nmethod has been cleaned/unloaded.
|
|
115 |
// After this call, it will be safe to ask if this nmethod was unloaded or not.
|
|
116 |
nm->set_unloading_clock(CompiledMethod::global_unloading_clock());
|
|
117 |
}
|
|
118 |
|
|
119 |
void CodeCacheUnloadingTask::clean_nmethod_postponed(CompiledMethod* nm) {
|
|
120 |
nm->do_unloading_parallel_postponed();
|
|
121 |
}
|
|
122 |
|
|
123 |
void CodeCacheUnloadingTask::claim_nmethods(CompiledMethod** claimed_nmethods, int *num_claimed_nmethods) {
|
|
124 |
CompiledMethod* first;
|
|
125 |
CompiledMethodIterator last;
|
|
126 |
|
|
127 |
do {
|
|
128 |
*num_claimed_nmethods = 0;
|
|
129 |
|
|
130 |
first = _claimed_nmethod;
|
|
131 |
last = CompiledMethodIterator(first);
|
|
132 |
|
|
133 |
if (first != NULL) {
|
|
134 |
|
|
135 |
for (int i = 0; i < MaxClaimNmethods; i++) {
|
|
136 |
if (!last.next_alive()) {
|
|
137 |
break;
|
|
138 |
}
|
|
139 |
claimed_nmethods[i] = last.method();
|
|
140 |
(*num_claimed_nmethods)++;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
} while (Atomic::cmpxchg(last.method(), &_claimed_nmethod, first) != first);
|
|
145 |
}
|
|
146 |
|
|
147 |
CompiledMethod* CodeCacheUnloadingTask::claim_postponed_nmethod() {
|
|
148 |
CompiledMethod* claim;
|
|
149 |
CompiledMethod* next;
|
|
150 |
|
|
151 |
do {
|
|
152 |
claim = _postponed_list;
|
|
153 |
if (claim == NULL) {
|
|
154 |
return NULL;
|
|
155 |
}
|
|
156 |
|
|
157 |
next = claim->unloading_next();
|
|
158 |
|
|
159 |
} while (Atomic::cmpxchg(next, &_postponed_list, claim) != claim);
|
|
160 |
|
|
161 |
return claim;
|
|
162 |
}
|
|
163 |
|
|
164 |
void CodeCacheUnloadingTask::barrier_mark(uint worker_id) {
|
|
165 |
MonitorLockerEx ml(_lock, Mutex::_no_safepoint_check_flag);
|
|
166 |
_num_entered_barrier++;
|
|
167 |
if (_num_entered_barrier == _num_workers) {
|
|
168 |
ml.notify_all();
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
void CodeCacheUnloadingTask::barrier_wait(uint worker_id) {
|
|
173 |
if (_num_entered_barrier < _num_workers) {
|
|
174 |
MonitorLockerEx ml(_lock, Mutex::_no_safepoint_check_flag);
|
|
175 |
while (_num_entered_barrier < _num_workers) {
|
|
176 |
ml.wait(Mutex::_no_safepoint_check_flag, 0, false);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|
|
180 |
|
|
181 |
void CodeCacheUnloadingTask::work_first_pass(uint worker_id) {
|
|
182 |
// The first nmethods is claimed by the first worker.
|
|
183 |
if (worker_id == 0 && _first_nmethod != NULL) {
|
|
184 |
clean_nmethod(_first_nmethod);
|
|
185 |
_first_nmethod = NULL;
|
|
186 |
}
|
|
187 |
|
|
188 |
int num_claimed_nmethods;
|
|
189 |
CompiledMethod* claimed_nmethods[MaxClaimNmethods];
|
|
190 |
|
|
191 |
while (true) {
|
|
192 |
claim_nmethods(claimed_nmethods, &num_claimed_nmethods);
|
|
193 |
|
|
194 |
if (num_claimed_nmethods == 0) {
|
|
195 |
break;
|
|
196 |
}
|
|
197 |
|
|
198 |
for (int i = 0; i < num_claimed_nmethods; i++) {
|
|
199 |
clean_nmethod(claimed_nmethods[i]);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
void CodeCacheUnloadingTask::work_second_pass(uint worker_id) {
|
|
205 |
CompiledMethod* nm;
|
|
206 |
// Take care of postponed nmethods.
|
|
207 |
while ((nm = claim_postponed_nmethod()) != NULL) {
|
|
208 |
clean_nmethod_postponed(nm);
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
|
|
213 |
KlassCleaningTask::KlassCleaningTask() :
|
|
214 |
_clean_klass_tree_claimed(0),
|
|
215 |
_klass_iterator() {
|
|
216 |
}
|
|
217 |
|
|
218 |
bool KlassCleaningTask::claim_clean_klass_tree_task() {
|
|
219 |
if (_clean_klass_tree_claimed) {
|
|
220 |
return false;
|
|
221 |
}
|
|
222 |
|
|
223 |
return Atomic::cmpxchg(1, &_clean_klass_tree_claimed, 0) == 0;
|
|
224 |
}
|
|
225 |
|
|
226 |
InstanceKlass* KlassCleaningTask::claim_next_klass() {
|
|
227 |
Klass* klass;
|
|
228 |
do {
|
|
229 |
klass =_klass_iterator.next_klass();
|
|
230 |
} while (klass != NULL && !klass->is_instance_klass());
|
|
231 |
|
|
232 |
// this can be null so don't call InstanceKlass::cast
|
|
233 |
return static_cast<InstanceKlass*>(klass);
|
|
234 |
}
|
|
235 |
|
|
236 |
void KlassCleaningTask::work() {
|
|
237 |
ResourceMark rm;
|
|
238 |
|
|
239 |
// One worker will clean the subklass/sibling klass tree.
|
|
240 |
if (claim_clean_klass_tree_task()) {
|
|
241 |
Klass::clean_subklass_tree();
|
|
242 |
}
|
|
243 |
|
|
244 |
// All workers will help cleaning the classes,
|
|
245 |
InstanceKlass* klass;
|
|
246 |
while ((klass = claim_next_klass()) != NULL) {
|
|
247 |
clean_klass(klass);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
bool ResolvedMethodCleaningTask::claim_resolved_method_task() {
|
|
252 |
if (_resolved_method_task_claimed) {
|
|
253 |
return false;
|
|
254 |
}
|
|
255 |
return Atomic::cmpxchg(1, &_resolved_method_task_claimed, 0) == 0;
|
|
256 |
}
|
|
257 |
|
|
258 |
// These aren't big, one thread can do it all.
|
|
259 |
void ResolvedMethodCleaningTask::work() {
|
|
260 |
if (claim_resolved_method_task()) {
|
|
261 |
ResolvedMethodTable::unlink();
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
class ParallelCleaningTaskTimer {
|
|
266 |
volatile jint* _timer_us;
|
|
267 |
jlong start;
|
|
268 |
public:
|
|
269 |
ParallelCleaningTaskTimer(jint* timer_us) : _timer_us(timer_us) {
|
|
270 |
start = os::javaTimeNanos();
|
|
271 |
}
|
|
272 |
~ParallelCleaningTaskTimer() {
|
|
273 |
jlong ns = os::javaTimeNanos() - start;
|
|
274 |
jlong us = ns / 1000;
|
|
275 |
assert (us < max_jint, "overflow");
|
|
276 |
Atomic::add((jint) us, _timer_us);
|
|
277 |
}
|
|
278 |
};
|
|
279 |
|
|
280 |
|
|
281 |
ParallelCleaningTask::ParallelCleaningTask(BoolObjectClosure* is_alive,
|
|
282 |
StringDedupUnlinkOrOopsDoClosure* dedup_closure, uint num_workers, bool unloading_occurred) :
|
|
283 |
AbstractGangTask("Parallel Cleaning"),
|
|
284 |
_unloading_occurred(unloading_occurred),
|
|
285 |
_string_task(is_alive, StringDedup::is_enabled() ? dedup_closure : NULL, true),
|
|
286 |
_code_cache_task(num_workers, is_alive, unloading_occurred),
|
|
287 |
_klass_cleaning_task(),
|
|
288 |
_resolved_method_cleaning_task() {
|
|
289 |
}
|
|
290 |
|
|
291 |
// The parallel work done by all worker threads.
|
|
292 |
void ParallelCleaningTask::work(uint worker_id) {
|
|
293 |
{
|
|
294 |
ParallelCleaningTaskTimer timer(&_times._codecache_work);
|
|
295 |
// Do first pass of code cache cleaning.
|
|
296 |
_code_cache_task.work_first_pass(worker_id);
|
|
297 |
}
|
|
298 |
|
|
299 |
{
|
|
300 |
ParallelCleaningTaskTimer timer(&_times._sync);
|
|
301 |
// Let the threads mark that the first pass is done.
|
|
302 |
_code_cache_task.barrier_mark(worker_id);
|
|
303 |
}
|
|
304 |
|
|
305 |
{
|
|
306 |
ParallelCleaningTaskTimer timer(&_times._tables_work);
|
|
307 |
// Clean the Strings and Symbols.
|
|
308 |
_string_task.work(worker_id);
|
|
309 |
}
|
|
310 |
|
|
311 |
{
|
|
312 |
ParallelCleaningTaskTimer timer(&_times._rmt_work);
|
|
313 |
// Clean unreferenced things in the ResolvedMethodTable
|
|
314 |
_resolved_method_cleaning_task.work();
|
|
315 |
}
|
|
316 |
|
|
317 |
{
|
|
318 |
ParallelCleaningTaskTimer timer(&_times._sync);
|
|
319 |
// Wait for all workers to finish the first code cache cleaning pass.
|
|
320 |
_code_cache_task.barrier_wait(worker_id);
|
|
321 |
}
|
|
322 |
|
|
323 |
{
|
|
324 |
ParallelCleaningTaskTimer timer(&_times._codecache_work);
|
|
325 |
// Do the second code cache cleaning work, which realize on
|
|
326 |
// the liveness information gathered during the first pass.
|
|
327 |
_code_cache_task.work_second_pass(worker_id);
|
|
328 |
}
|
|
329 |
|
|
330 |
// Clean all klasses that were not unloaded.
|
|
331 |
// The weak metadata in klass doesn't need to be
|
|
332 |
// processed if there was no unloading.
|
|
333 |
if (_unloading_occurred) {
|
|
334 |
ParallelCleaningTaskTimer timer(&_times._klass_work);
|
|
335 |
_klass_cleaning_task.work();
|
|
336 |
}
|
|
337 |
}
|