1 /*
2 * Copyright (c) 2012, 2014, 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 *
61 #include "runtime/mutex.hpp"
62 #include "runtime/safepoint.hpp"
63 #include "runtime/synchronizer.hpp"
64 #include "utilities/growableArray.hpp"
65 #include "utilities/macros.hpp"
66 #include "utilities/ostream.hpp"
67 #if INCLUDE_TRACE
68 #include "trace/tracing.hpp"
69 #endif
70
71 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
72
73 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
74 _class_loader(h_class_loader()),
75 _is_anonymous(is_anonymous),
76 // An anonymous class loader data doesn't have anything to keep
77 // it from being unloaded during parsing of the anonymous class.
78 // The null-class-loader should always be kept alive.
79 _keep_alive(is_anonymous || h_class_loader.is_null()),
80 _metaspace(NULL), _unloading(false), _klasses(NULL),
81 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
82 _next(NULL), _dependencies(dependencies),
83 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
84 // empty
85 }
86
87 void ClassLoaderData::init_dependencies(TRAPS) {
88 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
89 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
90 _dependencies.init(CHECK);
91 }
92
93 void ClassLoaderData::Dependencies::init(TRAPS) {
94 // Create empty dependencies array to add to. CMS requires this to be
95 // an oop so that it can track additions via card marks. We think.
96 _list_head = oopFactory::new_objectArray(2, CHECK);
97 }
98
99 bool ClassLoaderData::claim() {
100 if (_claimed == 1) {
101 return false;
102 }
103
104 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
105 }
106
107 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
108 if (must_claim && !claim()) {
109 return;
110 }
111
112 f->do_oop(&_class_loader);
113 _dependencies.oops_do(f);
114 _handles->oops_do(f);
115 if (klass_closure != NULL) {
116 classes_do(klass_closure);
117 }
118 }
119
120 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
121 f->do_oop((oop*)&_list_head);
122 }
123
124 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
125 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
126 klass_closure->do_klass(k);
127 assert(k != k->next_link(), "no loops!");
128 }
129 }
130
131 void ClassLoaderData::classes_do(void f(Klass * const)) {
132 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
133 f(k);
134 }
325 return is_anonymous() ? _klasses->java_mirror() : class_loader();
326 }
327
328 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
329 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
330 || is_alive_closure->do_object_b(keep_alive_object());
331
332 return alive;
333 }
334
335
336 ClassLoaderData::~ClassLoaderData() {
337 // Release C heap structures for all the classes.
338 classes_do(InstanceKlass::release_C_heap_structures);
339
340 Metaspace *m = _metaspace;
341 if (m != NULL) {
342 _metaspace = NULL;
343 // release the metaspace
344 delete m;
345 // release the handles
346 if (_handles != NULL) {
347 JNIHandleBlock::release_block(_handles);
348 _handles = NULL;
349 }
350 }
351
352 // Clear all the JNI handles for methods
353 // These aren't deallocated and are going to look like a leak, but that's
354 // needed because we can't really get rid of jmethodIDs because we don't
355 // know when native code is going to stop using them. The spec says that
356 // they're "invalid" but existing programs likely rely on their being
357 // NULL after class unloading.
358 if (_jmethod_ids != NULL) {
359 Method::clear_jmethod_ids(this);
360 }
361 // Delete lock
362 delete _metaspace_lock;
363
364 // Delete free list
365 if (_deallocate_list != NULL) {
366 delete _deallocate_list;
367 }
368 }
369
389 if (this == the_null_class_loader_data()) {
390 assert (class_loader() == NULL, "Must be");
391 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
392 } else if (is_anonymous()) {
393 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
394 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
395 }
396 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
397 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
398 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
399 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
400 }
401 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
402 } else {
403 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
404 }
405 }
406 return _metaspace;
407 }
408
409 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
410 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
411
412 jobject ClassLoaderData::add_handle(Handle h) {
413 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
414 if (handles() == NULL) {
415 set_handles(JNIHandleBlock::allocate_block());
416 }
417 return handles()->allocate_handle(h());
418 }
419
420 // Add this metadata pointer to be freed when it's safe. This is only during
421 // class unloading because Handles might point to this metadata field.
422 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
423 // Metadata in shared region isn't deleted.
424 if (!m->is_shared()) {
425 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
426 if (_deallocate_list == NULL) {
427 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
428 }
429 _deallocate_list->append_if_missing(m);
430 }
431 }
432
433 // Deallocate free metadata on the free list. How useful the PermGen was!
434 void ClassLoaderData::free_deallocate_list() {
435 // Don't need lock, at safepoint
436 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
437 if (_deallocate_list == NULL) {
462 // Add a new class loader data to the graph.
463 return ClassLoaderDataGraph::add(loader, true, CHECK_NULL);
464 }
465
466 const char* ClassLoaderData::loader_name() {
467 // Handles null class loader
468 return SystemDictionary::loader_name(class_loader());
469 }
470
471 #ifndef PRODUCT
472 // Define to dump klasses
473 #undef CLD_DUMP_KLASSES
474
475 void ClassLoaderData::dump(outputStream * const out) {
476 ResourceMark rm;
477 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
478 p2i(this), p2i((void *)class_loader()),
479 p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
480 if (claimed()) out->print(" claimed ");
481 if (is_unloading()) out->print(" unloading ");
482 out->print(" handles " INTPTR_FORMAT, p2i(handles()));
483 out->cr();
484 if (metaspace_or_null() != NULL) {
485 out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
486 metaspace_or_null()->dump(out);
487 } else {
488 out->print_cr("metaspace: NULL");
489 }
490
491 #ifdef CLD_DUMP_KLASSES
492 if (Verbose) {
493 ResourceMark rm;
494 Klass* k = _klasses;
495 while (k != NULL) {
496 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
497 k->has_modified_oops(), k->has_accumulated_modified_oops());
498 assert(k != k->next_link(), "no loops!");
499 k = k->next_link();
500 }
501 }
502 #endif // CLD_DUMP_KLASSES
|
1 /*
2 * Copyright (c) 2012, 2017, 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 *
61 #include "runtime/mutex.hpp"
62 #include "runtime/safepoint.hpp"
63 #include "runtime/synchronizer.hpp"
64 #include "utilities/growableArray.hpp"
65 #include "utilities/macros.hpp"
66 #include "utilities/ostream.hpp"
67 #if INCLUDE_TRACE
68 #include "trace/tracing.hpp"
69 #endif
70
71 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
72
73 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
74 _class_loader(h_class_loader()),
75 _is_anonymous(is_anonymous),
76 // An anonymous class loader data doesn't have anything to keep
77 // it from being unloaded during parsing of the anonymous class.
78 // The null-class-loader should always be kept alive.
79 _keep_alive(is_anonymous || h_class_loader.is_null()),
80 _metaspace(NULL), _unloading(false), _klasses(NULL),
81 _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
82 _next(NULL), _dependencies(dependencies),
83 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
84 // empty
85 }
86
87 void ClassLoaderData::init_dependencies(TRAPS) {
88 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
89 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
90 _dependencies.init(CHECK);
91 }
92
93 void ClassLoaderData::Dependencies::init(TRAPS) {
94 // Create empty dependencies array to add to. CMS requires this to be
95 // an oop so that it can track additions via card marks. We think.
96 _list_head = oopFactory::new_objectArray(2, CHECK);
97 }
98
99 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
100 Chunk* c = _head;
101 while (c != NULL) {
102 Chunk* next = c->_next;
103 delete c;
104 c = next;
105 }
106 }
107
108 oop* ClassLoaderData::ChunkedHandleList::add(oop o) {
109 if (_head == NULL || _head->_size == Chunk::CAPACITY) {
110 Chunk* next = new Chunk(_head);
111 OrderAccess::release_store_ptr(&_head, next);
112 }
113 oop* handle = &_head->_data[_head->_size];
114 *handle = o;
115 OrderAccess::release_store(&_head->_size, _head->_size + 1);
116 return handle;
117 }
118
119 inline void ClassLoaderData::ChunkedHandleList::oops_do_chunk(OopClosure* f, Chunk* c, const juint size) {
120 for (juint i = 0; i < size; i++) {
121 if (c->_data[i] != NULL) {
122 f->do_oop(&c->_data[i]);
123 }
124 }
125 }
126
127 void ClassLoaderData::ChunkedHandleList::oops_do(OopClosure* f) {
128 Chunk* head = (Chunk*) OrderAccess::load_ptr_acquire(&_head);
129 if (head != NULL) {
130 // Must be careful when reading size of head
131 oops_do_chunk(f, head, OrderAccess::load_acquire(&head->_size));
132 for (Chunk* c = head->_next; c != NULL; c = c->_next) {
133 oops_do_chunk(f, c, c->_size);
134 }
135 }
136 }
137
138 #ifdef ASSERT
139 class VerifyContainsOopClosure : public OopClosure {
140 oop* _target;
141 bool _found;
142
143 public:
144 VerifyContainsOopClosure(oop* target) : _target(target), _found(false) {}
145
146 void do_oop(oop* p) {
147 if (p == _target) {
148 _found = true;
149 }
150 }
151
152 void do_oop(narrowOop* p) {
153 // The ChunkedHandleList should not contain any narrowOop
154 ShouldNotReachHere();
155 }
156
157 bool found() const {
158 return _found;
159 }
160 };
161
162 bool ClassLoaderData::ChunkedHandleList::contains(oop* p) {
163 VerifyContainsOopClosure cl(p);
164 oops_do(&cl);
165 return cl.found();
166 }
167 #endif
168
169 bool ClassLoaderData::claim() {
170 if (_claimed == 1) {
171 return false;
172 }
173
174 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
175 }
176
177 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
178 if (must_claim && !claim()) {
179 return;
180 }
181
182 f->do_oop(&_class_loader);
183 _dependencies.oops_do(f);
184 _handles.oops_do(f);
185 if (klass_closure != NULL) {
186 classes_do(klass_closure);
187 }
188 }
189
190 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
191 f->do_oop((oop*)&_list_head);
192 }
193
194 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
195 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
196 klass_closure->do_klass(k);
197 assert(k != k->next_link(), "no loops!");
198 }
199 }
200
201 void ClassLoaderData::classes_do(void f(Klass * const)) {
202 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
203 f(k);
204 }
395 return is_anonymous() ? _klasses->java_mirror() : class_loader();
396 }
397
398 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
399 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
400 || is_alive_closure->do_object_b(keep_alive_object());
401
402 return alive;
403 }
404
405
406 ClassLoaderData::~ClassLoaderData() {
407 // Release C heap structures for all the classes.
408 classes_do(InstanceKlass::release_C_heap_structures);
409
410 Metaspace *m = _metaspace;
411 if (m != NULL) {
412 _metaspace = NULL;
413 // release the metaspace
414 delete m;
415 }
416
417 // Clear all the JNI handles for methods
418 // These aren't deallocated and are going to look like a leak, but that's
419 // needed because we can't really get rid of jmethodIDs because we don't
420 // know when native code is going to stop using them. The spec says that
421 // they're "invalid" but existing programs likely rely on their being
422 // NULL after class unloading.
423 if (_jmethod_ids != NULL) {
424 Method::clear_jmethod_ids(this);
425 }
426 // Delete lock
427 delete _metaspace_lock;
428
429 // Delete free list
430 if (_deallocate_list != NULL) {
431 delete _deallocate_list;
432 }
433 }
434
454 if (this == the_null_class_loader_data()) {
455 assert (class_loader() == NULL, "Must be");
456 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
457 } else if (is_anonymous()) {
458 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
459 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
460 }
461 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
462 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
463 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
464 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
465 }
466 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
467 } else {
468 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
469 }
470 }
471 return _metaspace;
472 }
473
474 jobject ClassLoaderData::add_handle(Handle h) {
475 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
476 return (jobject) _handles.add(h());
477 }
478
479 // Add this metadata pointer to be freed when it's safe. This is only during
480 // class unloading because Handles might point to this metadata field.
481 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
482 // Metadata in shared region isn't deleted.
483 if (!m->is_shared()) {
484 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
485 if (_deallocate_list == NULL) {
486 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
487 }
488 _deallocate_list->append_if_missing(m);
489 }
490 }
491
492 // Deallocate free metadata on the free list. How useful the PermGen was!
493 void ClassLoaderData::free_deallocate_list() {
494 // Don't need lock, at safepoint
495 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
496 if (_deallocate_list == NULL) {
521 // Add a new class loader data to the graph.
522 return ClassLoaderDataGraph::add(loader, true, CHECK_NULL);
523 }
524
525 const char* ClassLoaderData::loader_name() {
526 // Handles null class loader
527 return SystemDictionary::loader_name(class_loader());
528 }
529
530 #ifndef PRODUCT
531 // Define to dump klasses
532 #undef CLD_DUMP_KLASSES
533
534 void ClassLoaderData::dump(outputStream * const out) {
535 ResourceMark rm;
536 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
537 p2i(this), p2i((void *)class_loader()),
538 p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
539 if (claimed()) out->print(" claimed ");
540 if (is_unloading()) out->print(" unloading ");
541 out->cr();
542 if (metaspace_or_null() != NULL) {
543 out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
544 metaspace_or_null()->dump(out);
545 } else {
546 out->print_cr("metaspace: NULL");
547 }
548
549 #ifdef CLD_DUMP_KLASSES
550 if (Verbose) {
551 ResourceMark rm;
552 Klass* k = _klasses;
553 while (k != NULL) {
554 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
555 k->has_modified_oops(), k->has_accumulated_modified_oops());
556 assert(k != k->next_link(), "no loops!");
557 k = k->next_link();
558 }
559 }
560 #endif // CLD_DUMP_KLASSES
|