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(); 679 assert(_data != NULL, "Should not be NULL in call to the iterator"); 680 ClassLoaderMetaspace* result = _data->metaspace_or_null(); 681 _data = _data->next(); 682 // This result might be NULL for class loaders without metaspace 683 // yet. It would be nice to return only non-null results but 684 // there is no guarantee that there will be a non-null result 685 // down the list so the caller is going to have to check. 686 return result; 687 } 688 689 #ifndef PRODUCT 690 // callable from debugger 691 extern "C" int print_loader_data_graph() { 692 ResourceMark rm; 693 ClassLoaderDataGraph::print_on(tty); 694 return 0; 695 } 696 697 void ClassLoaderDataGraph::verify() { 698 ClassLoaderDataGraphIterator iter; 699 while (iter.repeat()) { 700 ClassLoaderData* cld = iter.get_next(); 701 cld->verify(); 702 } 703 } 704 705 void ClassLoaderDataGraph::print_on(outputStream * const out) { 706 ClassLoaderDataGraphIterator iter; 707 while (iter.repeat()) { 708 ClassLoaderData* cld = iter.get_next(); 709 cld->print_on(out); 710 } 711 } 712 #endif // PRODUCT | 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 public: 291 ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head) { 292 _thread = Thread::current(); 293 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 294 } 295 296 ClassLoaderData* get_next() { 297 ClassLoaderData* cld = _next; 298 // Skip already unloaded CLD for concurrent unloading. 299 while (cld != NULL && !cld->is_alive()) { 300 cld = cld->next(); 301 } 302 if (cld != NULL) { 303 // Keep cld that is being returned alive. 304 _holder = Handle(_thread, cld->holder_phantom()); 305 _next = cld->next(); 306 } else { 307 _next = NULL; 308 } 309 return cld; 310 } 311 312 313 }; 314 315 // These functions assume that the caller has locked the ClassLoaderDataGraph_lock 316 // if they are not calling the function from a safepoint. 317 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) { 318 ClassLoaderDataGraphIterator iter; 319 while (ClassLoaderData* cld = iter.get_next()) { 320 cld->classes_do(klass_closure); 321 } 322 } 323 324 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) { 325 ClassLoaderDataGraphIterator iter; 326 while (ClassLoaderData* cld = iter.get_next()) { 327 cld->classes_do(f); 328 } 329 } 330 331 void ClassLoaderDataGraph::methods_do(void f(Method*)) { 332 ClassLoaderDataGraphIterator iter; 333 while (ClassLoaderData* cld = iter.get_next()) { 334 cld->methods_do(f); 335 } 336 } 337 338 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) { 339 assert_locked_or_safepoint(Module_lock); 340 ClassLoaderDataGraphIterator iter; 341 while (ClassLoaderData* cld = iter.get_next()) { 342 cld->modules_do(f); 343 } 344 } 345 346 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) { 347 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 348 // Only walk the head until any clds not purged from prior unloading 349 // (CMS doesn't purge right away). 350 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 351 assert(cld->is_unloading(), "invariant"); 352 cld->modules_do(f); 353 } 354 } 355 356 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) { 357 assert_locked_or_safepoint(Module_lock); 358 ClassLoaderDataGraphIterator iter; 359 while (ClassLoaderData* cld = iter.get_next()) { 360 cld->packages_do(f); 361 } 362 } 363 364 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) { 365 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 366 // Only walk the head until any clds not purged from prior unloading 367 // (CMS doesn't purge right away). 368 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 369 assert(cld->is_unloading(), "invariant"); 370 cld->packages_do(f); 371 } 372 } 373 374 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) { 375 ClassLoaderDataGraphIterator iter; 376 while (ClassLoaderData* cld = iter.get_next()) { 377 cld->loaded_classes_do(klass_closure); 378 } 379 } 380 381 // This case can block but cannot do unloading (called from CDS) 382 void ClassLoaderDataGraph::unlocked_loaded_classes_do(KlassClosure* klass_closure) { 383 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 384 cld->loaded_classes_do(klass_closure); 385 } 386 } 387 388 389 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) { 390 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 391 // Only walk the head until any clds not purged from prior unloading 392 // (CMS doesn't purge right away). 393 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 394 assert(cld->is_unloading(), "invariant"); 395 cld->classes_do(f); 396 } 397 } 398 399 #define FOR_ALL_DICTIONARY(X) ClassLoaderDataGraphIterator iter; \ 400 while (ClassLoaderData* X = iter.get_next()) \ 401 if (X->dictionary() != NULL) 402 403 // Walk classes in the loaded class dictionaries in various forms. 404 // Only walks the classes defined in this class loader. 405 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) { 406 FOR_ALL_DICTIONARY(cld) { 407 cld->dictionary()->classes_do(f); 408 } 409 } 410 411 // Only walks the classes defined in this class loader. 412 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) { 413 FOR_ALL_DICTIONARY(cld) { 414 cld->dictionary()->classes_do(f, CHECK); 415 } 416 } 417 418 void ClassLoaderDataGraph::verify_dictionary() { 419 FOR_ALL_DICTIONARY(cld) { 420 cld->dictionary()->verify(); 671 assert(_data != NULL, "Should not be NULL in call to the iterator"); 672 ClassLoaderMetaspace* result = _data->metaspace_or_null(); 673 _data = _data->next(); 674 // This result might be NULL for class loaders without metaspace 675 // yet. It would be nice to return only non-null results but 676 // there is no guarantee that there will be a non-null result 677 // down the list so the caller is going to have to check. 678 return result; 679 } 680 681 #ifndef PRODUCT 682 // callable from debugger 683 extern "C" int print_loader_data_graph() { 684 ResourceMark rm; 685 ClassLoaderDataGraph::print_on(tty); 686 return 0; 687 } 688 689 void ClassLoaderDataGraph::verify() { 690 ClassLoaderDataGraphIterator iter; 691 while (ClassLoaderData* cld = iter.get_next()) { 692 cld->verify(); 693 } 694 } 695 696 void ClassLoaderDataGraph::print_on(outputStream * const out) { 697 ClassLoaderDataGraphIterator iter; 698 while (ClassLoaderData* cld = iter.get_next()) { 699 cld->print_on(out); 700 } 701 } 702 #endif // PRODUCT |