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