|
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/classLoaderDataGraph.inline.hpp" |
|
27 #include "classfile/dictionary.hpp" |
|
28 #include "classfile/javaClasses.hpp" |
|
29 #include "classfile/metadataOnStackMark.hpp" |
|
30 #include "classfile/moduleEntry.hpp" |
|
31 #include "classfile/packageEntry.hpp" |
|
32 #include "logging/log.hpp" |
|
33 #include "logging/logStream.hpp" |
|
34 #include "memory/allocation.inline.hpp" |
|
35 #include "memory/metaspace.hpp" |
|
36 #include "memory/resourceArea.hpp" |
|
37 #include "runtime/atomic.hpp" |
|
38 #include "runtime/handles.inline.hpp" |
|
39 #include "runtime/mutex.hpp" |
|
40 #include "runtime/safepoint.hpp" |
|
41 #include "runtime/safepointVerifiers.hpp" |
|
42 #include "utilities/growableArray.hpp" |
|
43 #include "utilities/macros.hpp" |
|
44 #include "utilities/ostream.hpp" |
|
45 |
|
46 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0; |
|
47 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0; |
|
48 |
|
49 void ClassLoaderDataGraph::clear_claimed_marks() { |
|
50 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
|
51 cld->clear_claimed(); |
|
52 } |
|
53 } |
|
54 |
|
55 // Class iterator used by the compiler. It gets some number of classes at |
|
56 // a safepoint to decay invocation counters on the methods. |
|
57 class ClassLoaderDataGraphKlassIteratorStatic { |
|
58 ClassLoaderData* _current_loader_data; |
|
59 Klass* _current_class_entry; |
|
60 public: |
|
61 |
|
62 ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {} |
|
63 |
|
64 InstanceKlass* try_get_next_class() { |
|
65 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); |
|
66 size_t max_classes = ClassLoaderDataGraph::num_instance_classes(); |
|
67 assert(max_classes > 0, "should not be called with no instance classes"); |
|
68 for (size_t i = 0; i < max_classes; ) { |
|
69 |
|
70 if (_current_class_entry != NULL) { |
|
71 Klass* k = _current_class_entry; |
|
72 _current_class_entry = _current_class_entry->next_link(); |
|
73 |
|
74 if (k->is_instance_klass()) { |
|
75 InstanceKlass* ik = InstanceKlass::cast(k); |
|
76 i++; // count all instance classes found |
|
77 // Not yet loaded classes are counted in max_classes |
|
78 // but only return loaded classes. |
|
79 if (ik->is_loaded()) { |
|
80 return ik; |
|
81 } |
|
82 } |
|
83 } else { |
|
84 // Go to next CLD |
|
85 if (_current_loader_data != NULL) { |
|
86 _current_loader_data = _current_loader_data->next(); |
|
87 } |
|
88 // Start at the beginning |
|
89 if (_current_loader_data == NULL) { |
|
90 _current_loader_data = ClassLoaderDataGraph::_head; |
|
91 } |
|
92 |
|
93 _current_class_entry = _current_loader_data->klasses(); |
|
94 } |
|
95 } |
|
96 // Should never be reached unless all instance classes have failed or are not fully loaded. |
|
97 // Caller handles NULL. |
|
98 return NULL; |
|
99 } |
|
100 |
|
101 // If the current class for the static iterator is a class being unloaded or |
|
102 // deallocated, adjust the current class. |
|
103 void adjust_saved_class(ClassLoaderData* cld) { |
|
104 if (_current_loader_data == cld) { |
|
105 _current_loader_data = cld->next(); |
|
106 if (_current_loader_data != NULL) { |
|
107 _current_class_entry = _current_loader_data->klasses(); |
|
108 } // else try_get_next_class will start at the head |
|
109 } |
|
110 } |
|
111 |
|
112 void adjust_saved_class(Klass* klass) { |
|
113 if (_current_class_entry == klass) { |
|
114 _current_class_entry = klass->next_link(); |
|
115 } |
|
116 } |
|
117 }; |
|
118 |
|
119 static ClassLoaderDataGraphKlassIteratorStatic static_klass_iterator; |
|
120 |
|
121 InstanceKlass* ClassLoaderDataGraph::try_get_next_class() { |
|
122 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); |
|
123 return static_klass_iterator.try_get_next_class(); |
|
124 } |
|
125 |
|
126 void ClassLoaderDataGraph::adjust_saved_class(ClassLoaderData* cld) { |
|
127 return static_klass_iterator.adjust_saved_class(cld); |
|
128 } |
|
129 |
|
130 void ClassLoaderDataGraph::adjust_saved_class(Klass* klass) { |
|
131 return static_klass_iterator.adjust_saved_class(klass); |
|
132 } |
|
133 |
|
134 void ClassLoaderDataGraph::clean_deallocate_lists(bool walk_previous_versions) { |
|
135 assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint"); |
|
136 uint loaders_processed = 0; |
|
137 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
|
138 // is_alive check will be necessary for concurrent class unloading. |
|
139 if (cld->is_alive()) { |
|
140 // clean metaspace |
|
141 if (walk_previous_versions) { |
|
142 cld->classes_do(InstanceKlass::purge_previous_versions); |
|
143 } |
|
144 cld->free_deallocate_list(); |
|
145 loaders_processed++; |
|
146 } |
|
147 } |
|
148 log_debug(class, loader, data)("clean_deallocate_lists: loaders processed %u %s", |
|
149 loaders_processed, walk_previous_versions ? "walk_previous_versions" : ""); |
|
150 } |
|
151 |
|
152 void ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces() { |
|
153 assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint"); |
|
154 |
|
155 _should_clean_deallocate_lists = false; // assume everything gets cleaned |
|
156 |
|
157 // Mark metadata seen on the stack so we can delete unreferenced entries. |
|
158 // Walk all metadata, including the expensive code cache walk, only for class redefinition. |
|
159 // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods |
|
160 // on the stack or in the code cache, so we only have to repeat the full walk if |
|
161 // they were found at that time. |
|
162 // TODO: have redefinition clean old methods out of the code cache. They still exist in some places. |
|
163 bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset(); |
|
164 |
|
165 MetadataOnStackMark md_on_stack(walk_all_metadata); |
|
166 clean_deallocate_lists(walk_all_metadata); |
|
167 } |
|
168 |
|
169 // GC root of class loader data created. |
|
170 ClassLoaderData* ClassLoaderDataGraph::_head = NULL; |
|
171 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL; |
|
172 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL; |
|
173 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL; |
|
174 |
|
175 bool ClassLoaderDataGraph::_should_purge = false; |
|
176 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false; |
|
177 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false; |
|
178 bool ClassLoaderDataGraph::_metaspace_oom = false; |
|
179 |
|
180 // Add a new class loader data node to the list. Assign the newly created |
|
181 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field |
|
182 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) { |
|
183 |
|
184 assert_lock_strong(ClassLoaderDataGraph_lock); |
|
185 |
|
186 ClassLoaderData* cld; |
|
187 |
|
188 // First check if another thread beat us to creating the CLD and installing |
|
189 // it into the loader while we were waiting for the lock. |
|
190 if (!is_unsafe_anonymous && loader.not_null()) { |
|
191 cld = java_lang_ClassLoader::loader_data_acquire(loader()); |
|
192 if (cld != NULL) { |
|
193 return cld; |
|
194 } |
|
195 } |
|
196 |
|
197 // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD |
|
198 // contains oops in _handles that must be walked. GC doesn't walk CLD from the |
|
199 // loader oop in all collections, particularly young collections. |
|
200 NoSafepointVerifier no_safepoints; |
|
201 |
|
202 cld = new ClassLoaderData(loader, is_unsafe_anonymous); |
|
203 |
|
204 // First install the new CLD to the Graph. |
|
205 cld->set_next(_head); |
|
206 _head = cld; |
|
207 |
|
208 // Next associate with the class_loader. |
|
209 if (!is_unsafe_anonymous) { |
|
210 // Use OrderAccess, since readers need to get the loader_data only after |
|
211 // it's added to the Graph |
|
212 java_lang_ClassLoader::release_set_loader_data(loader(), cld); |
|
213 } |
|
214 |
|
215 // Lastly log, if requested |
|
216 LogTarget(Trace, class, loader, data) lt; |
|
217 if (lt.is_enabled()) { |
|
218 ResourceMark rm; |
|
219 LogStream ls(lt); |
|
220 ls.print("create "); |
|
221 cld->print_value_on(&ls); |
|
222 ls.cr(); |
|
223 } |
|
224 return cld; |
|
225 } |
|
226 |
|
227 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) { |
|
228 MutexLocker ml(ClassLoaderDataGraph_lock); |
|
229 ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous); |
|
230 return loader_data; |
|
231 } |
|
232 |
|
233 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) { |
|
234 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
235 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) { |
|
236 cl->do_cld(cld); |
|
237 } |
|
238 } |
|
239 |
|
240 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) { |
|
241 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
242 // Only walk the head until any clds not purged from prior unloading |
|
243 // (CMS doesn't purge right away). |
|
244 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { |
|
245 assert(cld->is_unloading(), "invariant"); |
|
246 cl->do_cld(cld); |
|
247 } |
|
248 } |
|
249 |
|
250 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) { |
|
251 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
252 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) { |
|
253 CLDClosure* closure = cld->keep_alive() ? strong : weak; |
|
254 if (closure != NULL) { |
|
255 closure->do_cld(cld); |
|
256 } |
|
257 } |
|
258 } |
|
259 |
|
260 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) { |
|
261 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
262 if (ClassUnloading) { |
|
263 roots_cld_do(cl, NULL); |
|
264 } else { |
|
265 cld_do(cl); |
|
266 } |
|
267 } |
|
268 |
|
269 // Closure for locking and iterating through classes. |
|
270 LockedClassesDo::LockedClassesDo(classes_do_func_t f) : _function(f) { |
|
271 ClassLoaderDataGraph_lock->lock(); |
|
272 } |
|
273 |
|
274 LockedClassesDo::LockedClassesDo() : _function(NULL) { |
|
275 // callers provide their own do_klass |
|
276 ClassLoaderDataGraph_lock->lock(); |
|
277 } |
|
278 |
|
279 LockedClassesDo::~LockedClassesDo() { ClassLoaderDataGraph_lock->unlock(); } |
|
280 |
|
281 |
|
282 // Iterating over the CLDG needs to be locked because |
|
283 // unloading can remove entries concurrently soon. |
|
284 class ClassLoaderDataGraphIterator : public StackObj { |
|
285 ClassLoaderData* _next; |
|
286 HandleMark _hm; // clean up handles when this is done. |
|
287 Handle _holder; |
|
288 Thread* _thread; |
|
289 |
|
290 void hold_next() { |
|
291 if (_next != NULL) { |
|
292 _holder = Handle(_thread, _next->holder_phantom()); |
|
293 } |
|
294 } |
|
295 public: |
|
296 ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head) { |
|
297 _thread = Thread::current(); |
|
298 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
299 hold_next(); |
|
300 } |
|
301 |
|
302 bool repeat() const { |
|
303 return _next != NULL; |
|
304 } |
|
305 |
|
306 ClassLoaderData* get_next() { |
|
307 ClassLoaderData* next = _next; |
|
308 if (_next != NULL) { |
|
309 _next = _next->next(); |
|
310 hold_next(); |
|
311 } |
|
312 return next; |
|
313 } |
|
314 }; |
|
315 |
|
316 // These functions assume that the caller has locked the ClassLoaderDataGraph_lock |
|
317 // if they are not calling the function from a safepoint. |
|
318 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) { |
|
319 ClassLoaderDataGraphIterator iter; |
|
320 while (iter.repeat()) { |
|
321 ClassLoaderData* cld = iter.get_next(); |
|
322 cld->classes_do(klass_closure); |
|
323 } |
|
324 } |
|
325 |
|
326 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) { |
|
327 ClassLoaderDataGraphIterator iter; |
|
328 while (iter.repeat()) { |
|
329 ClassLoaderData* cld = iter.get_next(); |
|
330 cld->classes_do(f); |
|
331 } |
|
332 } |
|
333 |
|
334 void ClassLoaderDataGraph::methods_do(void f(Method*)) { |
|
335 ClassLoaderDataGraphIterator iter; |
|
336 while (iter.repeat()) { |
|
337 ClassLoaderData* cld = iter.get_next(); |
|
338 cld->methods_do(f); |
|
339 } |
|
340 } |
|
341 |
|
342 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) { |
|
343 assert_locked_or_safepoint(Module_lock); |
|
344 ClassLoaderDataGraphIterator iter; |
|
345 while (iter.repeat()) { |
|
346 ClassLoaderData* cld = iter.get_next(); |
|
347 cld->modules_do(f); |
|
348 } |
|
349 } |
|
350 |
|
351 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) { |
|
352 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
353 // Only walk the head until any clds not purged from prior unloading |
|
354 // (CMS doesn't purge right away). |
|
355 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { |
|
356 assert(cld->is_unloading(), "invariant"); |
|
357 cld->modules_do(f); |
|
358 } |
|
359 } |
|
360 |
|
361 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) { |
|
362 assert_locked_or_safepoint(Module_lock); |
|
363 ClassLoaderDataGraphIterator iter; |
|
364 while (iter.repeat()) { |
|
365 ClassLoaderData* cld = iter.get_next(); |
|
366 cld->packages_do(f); |
|
367 } |
|
368 } |
|
369 |
|
370 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) { |
|
371 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
372 // Only walk the head until any clds not purged from prior unloading |
|
373 // (CMS doesn't purge right away). |
|
374 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { |
|
375 assert(cld->is_unloading(), "invariant"); |
|
376 cld->packages_do(f); |
|
377 } |
|
378 } |
|
379 |
|
380 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) { |
|
381 ClassLoaderDataGraphIterator iter; |
|
382 while (iter.repeat()) { |
|
383 ClassLoaderData* cld = iter.get_next(); |
|
384 cld->loaded_classes_do(klass_closure); |
|
385 } |
|
386 } |
|
387 |
|
388 // This case can block but cannot do unloading (called from CDS) |
|
389 void ClassLoaderDataGraph::unlocked_loaded_classes_do(KlassClosure* klass_closure) { |
|
390 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
|
391 cld->loaded_classes_do(klass_closure); |
|
392 } |
|
393 } |
|
394 |
|
395 |
|
396 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) { |
|
397 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
398 // Only walk the head until any clds not purged from prior unloading |
|
399 // (CMS doesn't purge right away). |
|
400 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { |
|
401 assert(cld->is_unloading(), "invariant"); |
|
402 cld->classes_do(f); |
|
403 } |
|
404 } |
|
405 |
|
406 #define FOR_ALL_DICTIONARY(X) ClassLoaderDataGraphIterator iter; \ |
|
407 ClassLoaderData* X; \ |
|
408 while ((X = iter.get_next()) != NULL) \ |
|
409 if (X->dictionary() != NULL) |
|
410 |
|
411 // Walk classes in the loaded class dictionaries in various forms. |
|
412 // Only walks the classes defined in this class loader. |
|
413 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) { |
|
414 FOR_ALL_DICTIONARY(cld) { |
|
415 cld->dictionary()->classes_do(f); |
|
416 } |
|
417 } |
|
418 |
|
419 // Only walks the classes defined in this class loader. |
|
420 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) { |
|
421 FOR_ALL_DICTIONARY(cld) { |
|
422 cld->dictionary()->classes_do(f, CHECK); |
|
423 } |
|
424 } |
|
425 |
|
426 void ClassLoaderDataGraph::verify_dictionary() { |
|
427 FOR_ALL_DICTIONARY(cld) { |
|
428 cld->dictionary()->verify(); |
|
429 } |
|
430 } |
|
431 |
|
432 void ClassLoaderDataGraph::print_dictionary(outputStream* st) { |
|
433 FOR_ALL_DICTIONARY(cld) { |
|
434 st->print("Dictionary for "); |
|
435 cld->print_value_on(st); |
|
436 st->cr(); |
|
437 cld->dictionary()->print_on(st); |
|
438 st->cr(); |
|
439 } |
|
440 } |
|
441 |
|
442 void ClassLoaderDataGraph::print_dictionary_statistics(outputStream* st) { |
|
443 FOR_ALL_DICTIONARY(cld) { |
|
444 ResourceMark rm; |
|
445 stringStream tempst; |
|
446 tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id()); |
|
447 cld->dictionary()->print_table_statistics(st, tempst.as_string()); |
|
448 } |
|
449 } |
|
450 |
|
451 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() { |
|
452 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
453 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?"); |
|
454 |
|
455 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>(); |
|
456 |
|
457 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true); |
|
458 ClassLoaderData* curr = _head; |
|
459 while (curr != _saved_head) { |
|
460 if (!curr->claimed()) { |
|
461 array->push(curr); |
|
462 LogTarget(Debug, class, loader, data) lt; |
|
463 if (lt.is_enabled()) { |
|
464 LogStream ls(lt); |
|
465 ls.print("found new CLD: "); |
|
466 curr->print_value_on(&ls); |
|
467 ls.cr(); |
|
468 } |
|
469 } |
|
470 |
|
471 curr = curr->_next; |
|
472 } |
|
473 |
|
474 return array; |
|
475 } |
|
476 |
|
477 #ifndef PRODUCT |
|
478 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { |
|
479 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
480 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { |
|
481 if (loader_data == data) { |
|
482 return true; |
|
483 } |
|
484 } |
|
485 |
|
486 return false; |
|
487 } |
|
488 #endif // PRODUCT |
|
489 |
|
490 // Move class loader data from main list to the unloaded list for unloading |
|
491 // and deallocation later. |
|
492 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) { |
|
493 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
494 |
|
495 // Indicate whether safepoint cleanup is needed. |
|
496 _safepoint_cleanup_needed |= do_cleaning; |
|
497 |
|
498 ClassLoaderData* data = _head; |
|
499 ClassLoaderData* prev = NULL; |
|
500 bool seen_dead_loader = false; |
|
501 uint loaders_processed = 0; |
|
502 uint loaders_removed = 0; |
|
503 |
|
504 // Save previous _unloading pointer for CMS which may add to unloading list before |
|
505 // purging and we don't want to rewalk the previously unloaded class loader data. |
|
506 _saved_unloading = _unloading; |
|
507 |
|
508 data = _head; |
|
509 while (data != NULL) { |
|
510 if (data->is_alive()) { |
|
511 prev = data; |
|
512 data = data->next(); |
|
513 loaders_processed++; |
|
514 continue; |
|
515 } |
|
516 seen_dead_loader = true; |
|
517 loaders_removed++; |
|
518 ClassLoaderData* dead = data; |
|
519 dead->unload(); |
|
520 data = data->next(); |
|
521 // Remove from loader list. |
|
522 // This class loader data will no longer be found |
|
523 // in the ClassLoaderDataGraph. |
|
524 if (prev != NULL) { |
|
525 prev->set_next(data); |
|
526 } else { |
|
527 assert(dead == _head, "sanity check"); |
|
528 _head = data; |
|
529 } |
|
530 dead->set_next(_unloading); |
|
531 _unloading = dead; |
|
532 } |
|
533 |
|
534 log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed); |
|
535 |
|
536 return seen_dead_loader; |
|
537 } |
|
538 |
|
539 // There's at least one dead class loader. Purge refererences of healthy module |
|
540 // reads lists and package export lists to modules belonging to dead loaders. |
|
541 void ClassLoaderDataGraph::clean_module_and_package_info() { |
|
542 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); |
|
543 |
|
544 ClassLoaderData* data = _head; |
|
545 while (data != NULL) { |
|
546 // Remove entries in the dictionary of live class loader that have |
|
547 // initiated loading classes in a dead class loader. |
|
548 if (data->dictionary() != NULL) { |
|
549 data->dictionary()->do_unloading(); |
|
550 } |
|
551 // Walk a ModuleEntry's reads, and a PackageEntry's exports |
|
552 // lists to determine if there are modules on those lists that are now |
|
553 // dead and should be removed. A module's life cycle is equivalent |
|
554 // to its defining class loader's life cycle. Since a module is |
|
555 // considered dead if its class loader is dead, these walks must |
|
556 // occur after each class loader's aliveness is determined. |
|
557 if (data->packages() != NULL) { |
|
558 data->packages()->purge_all_package_exports(); |
|
559 } |
|
560 if (data->modules_defined()) { |
|
561 data->modules()->purge_all_module_reads(); |
|
562 } |
|
563 data = data->next(); |
|
564 } |
|
565 } |
|
566 |
|
567 void ClassLoaderDataGraph::purge() { |
|
568 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); |
|
569 ClassLoaderData* list = _unloading; |
|
570 _unloading = NULL; |
|
571 ClassLoaderData* next = list; |
|
572 bool classes_unloaded = false; |
|
573 while (next != NULL) { |
|
574 ClassLoaderData* purge_me = next; |
|
575 next = purge_me->next(); |
|
576 delete purge_me; |
|
577 classes_unloaded = true; |
|
578 } |
|
579 if (classes_unloaded) { |
|
580 Metaspace::purge(); |
|
581 set_metaspace_oom(false); |
|
582 } |
|
583 } |
|
584 |
|
585 int ClassLoaderDataGraph::resize_if_needed() { |
|
586 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); |
|
587 int resized = 0; |
|
588 if (Dictionary::does_any_dictionary_needs_resizing()) { |
|
589 FOR_ALL_DICTIONARY(cld) { |
|
590 if (cld->dictionary()->resize_if_needed()) { |
|
591 resized++; |
|
592 } |
|
593 } |
|
594 } |
|
595 return resized; |
|
596 } |
|
597 |
|
598 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic() |
|
599 : _next_klass(NULL) { |
|
600 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); |
|
601 ClassLoaderData* cld = ClassLoaderDataGraph::_head; |
|
602 Klass* klass = NULL; |
|
603 |
|
604 // Find the first klass in the CLDG. |
|
605 while (cld != NULL) { |
|
606 assert_locked_or_safepoint(cld->metaspace_lock()); |
|
607 klass = cld->_klasses; |
|
608 if (klass != NULL) { |
|
609 _next_klass = klass; |
|
610 return; |
|
611 } |
|
612 cld = cld->next(); |
|
613 } |
|
614 } |
|
615 |
|
616 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) { |
|
617 Klass* next = klass->next_link(); |
|
618 if (next != NULL) { |
|
619 return next; |
|
620 } |
|
621 |
|
622 // No more klasses in the current CLD. Time to find a new CLD. |
|
623 ClassLoaderData* cld = klass->class_loader_data(); |
|
624 assert_locked_or_safepoint(cld->metaspace_lock()); |
|
625 while (next == NULL) { |
|
626 cld = cld->next(); |
|
627 if (cld == NULL) { |
|
628 break; |
|
629 } |
|
630 next = cld->_klasses; |
|
631 } |
|
632 |
|
633 return next; |
|
634 } |
|
635 |
|
636 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() { |
|
637 Klass* head = _next_klass; |
|
638 |
|
639 while (head != NULL) { |
|
640 Klass* next = next_klass_in_cldg(head); |
|
641 |
|
642 Klass* old_head = Atomic::cmpxchg(next, &_next_klass, head); |
|
643 |
|
644 if (old_head == head) { |
|
645 return head; // Won the CAS. |
|
646 } |
|
647 |
|
648 head = old_head; |
|
649 } |
|
650 |
|
651 // Nothing more for the iterator to hand out. |
|
652 assert(head == NULL, "head is " PTR_FORMAT ", expected not null:", p2i(head)); |
|
653 return NULL; |
|
654 } |
|
655 |
|
656 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() { |
|
657 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); |
|
658 _data = ClassLoaderDataGraph::_head; |
|
659 } |
|
660 |
|
661 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {} |
|
662 |
|
663 ClassLoaderMetaspace* ClassLoaderDataGraphMetaspaceIterator::get_next() { |
|
664 assert(_data != NULL, "Should not be NULL in call to the iterator"); |
|
665 ClassLoaderMetaspace* result = _data->metaspace_or_null(); |
|
666 _data = _data->next(); |
|
667 // This result might be NULL for class loaders without metaspace |
|
668 // yet. It would be nice to return only non-null results but |
|
669 // there is no guarantee that there will be a non-null result |
|
670 // down the list so the caller is going to have to check. |
|
671 return result; |
|
672 } |
|
673 |
|
674 #ifndef PRODUCT |
|
675 // callable from debugger |
|
676 extern "C" int print_loader_data_graph() { |
|
677 ResourceMark rm; |
|
678 ClassLoaderDataGraph::print_on(tty); |
|
679 return 0; |
|
680 } |
|
681 |
|
682 void ClassLoaderDataGraph::verify() { |
|
683 ClassLoaderDataGraphIterator iter; |
|
684 while (iter.repeat()) { |
|
685 ClassLoaderData* cld = iter.get_next(); |
|
686 cld->verify(); |
|
687 } |
|
688 } |
|
689 |
|
690 void ClassLoaderDataGraph::print_on(outputStream * const out) { |
|
691 ClassLoaderDataGraphIterator iter; |
|
692 while (iter.repeat()) { |
|
693 ClassLoaderData* cld = iter.get_next(); |
|
694 cld->print_on(out); |
|
695 } |
|
696 } |
|
697 #endif // PRODUCT |