src/share/vm/classfile/classLoaderData.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6742
e4a6e7f1b90b
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 // A ClassLoaderData identifies the full set of class types that a class
aoqi@0 26 // loader's name resolution strategy produces for a given configuration of the
aoqi@0 27 // class loader.
aoqi@0 28 // Class types in the ClassLoaderData may be defined by from class file binaries
aoqi@0 29 // provided by the class loader, or from other class loader it interacts with
aoqi@0 30 // according to its name resolution strategy.
aoqi@0 31 //
aoqi@0 32 // Class loaders that implement a deterministic name resolution strategy
aoqi@0 33 // (including with respect to their delegation behavior), such as the boot, the
aoqi@0 34 // extension, and the system loaders of the JDK's built-in class loader
aoqi@0 35 // hierarchy, always produce the same linkset for a given configuration.
aoqi@0 36 //
aoqi@0 37 // ClassLoaderData carries information related to a linkset (e.g.,
aoqi@0 38 // metaspace holding its klass definitions).
aoqi@0 39 // The System Dictionary and related data structures (e.g., placeholder table,
aoqi@0 40 // loader constraints table) as well as the runtime representation of classes
aoqi@0 41 // only reference ClassLoaderData.
aoqi@0 42 //
aoqi@0 43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
aoqi@0 44 // that represent the loader's "linking domain" in the JVM.
aoqi@0 45 //
aoqi@0 46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
aoqi@0 47 // the singleton class the_null_class_loader_data().
aoqi@0 48
aoqi@0 49 #include "precompiled.hpp"
aoqi@0 50 #include "classfile/classLoaderData.hpp"
aoqi@0 51 #include "classfile/classLoaderData.inline.hpp"
aoqi@0 52 #include "classfile/javaClasses.hpp"
aoqi@0 53 #include "classfile/metadataOnStackMark.hpp"
aoqi@0 54 #include "classfile/systemDictionary.hpp"
aoqi@0 55 #include "code/codeCache.hpp"
aoqi@0 56 #include "memory/gcLocker.hpp"
aoqi@0 57 #include "memory/metadataFactory.hpp"
aoqi@0 58 #include "memory/metaspaceShared.hpp"
aoqi@0 59 #include "memory/oopFactory.hpp"
aoqi@0 60 #include "runtime/jniHandles.hpp"
aoqi@0 61 #include "runtime/mutex.hpp"
aoqi@0 62 #include "runtime/safepoint.hpp"
aoqi@0 63 #include "runtime/synchronizer.hpp"
aoqi@0 64 #include "utilities/growableArray.hpp"
aoqi@0 65 #include "utilities/macros.hpp"
aoqi@0 66 #include "utilities/ostream.hpp"
aoqi@0 67
aoqi@0 68 #if INCLUDE_TRACE
aoqi@0 69 #include "trace/tracing.hpp"
aoqi@0 70 #endif
aoqi@0 71
aoqi@0 72 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
aoqi@0 73
aoqi@0 74 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
aoqi@0 75 _class_loader(h_class_loader()),
aoqi@0 76 _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
aoqi@0 77 _metaspace(NULL), _unloading(false), _klasses(NULL),
aoqi@0 78 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
aoqi@0 79 _next(NULL), _dependencies(dependencies),
aoqi@0 80 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
aoqi@0 81 // empty
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 void ClassLoaderData::init_dependencies(TRAPS) {
aoqi@0 85 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
aoqi@0 86 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
aoqi@0 87 _dependencies.init(CHECK);
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 void ClassLoaderData::Dependencies::init(TRAPS) {
aoqi@0 91 // Create empty dependencies array to add to. CMS requires this to be
aoqi@0 92 // an oop so that it can track additions via card marks. We think.
aoqi@0 93 _list_head = oopFactory::new_objectArray(2, CHECK);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 bool ClassLoaderData::claim() {
aoqi@0 97 if (_claimed == 1) {
aoqi@0 98 return false;
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
aoqi@0 105 if (must_claim && !claim()) {
aoqi@0 106 return;
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 f->do_oop(&_class_loader);
aoqi@0 110 _dependencies.oops_do(f);
aoqi@0 111 _handles->oops_do(f);
aoqi@0 112 if (klass_closure != NULL) {
aoqi@0 113 classes_do(klass_closure);
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
aoqi@0 118 f->do_oop((oop*)&_list_head);
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
aoqi@0 122 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
aoqi@0 123 klass_closure->do_klass(k);
aoqi@0 124 assert(k != k->next_link(), "no loops!");
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 void ClassLoaderData::classes_do(void f(Klass * const)) {
aoqi@0 129 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
aoqi@0 130 f(k);
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
aoqi@0 135 // Lock to avoid classes being modified/added/removed during iteration
aoqi@0 136 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 137 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
aoqi@0 138 // Do not filter ArrayKlass oops here...
aoqi@0 139 if (k->oop_is_array() || (k->oop_is_instance() && InstanceKlass::cast(k)->is_loaded())) {
aoqi@0 140 klass_closure->do_klass(k);
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
aoqi@0 146 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
aoqi@0 147 if (k->oop_is_instance()) {
aoqi@0 148 f(InstanceKlass::cast(k));
aoqi@0 149 }
aoqi@0 150 assert(k != k->next_link(), "no loops!");
aoqi@0 151 }
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
aoqi@0 155 ClassLoaderData * const from_cld = this;
aoqi@0 156 ClassLoaderData * const to_cld = k->class_loader_data();
aoqi@0 157
aoqi@0 158 // Dependency to the null class loader data doesn't need to be recorded
aoqi@0 159 // because the null class loader data never goes away.
aoqi@0 160 if (to_cld->is_the_null_class_loader_data()) {
aoqi@0 161 return;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 oop to;
aoqi@0 165 if (to_cld->is_anonymous()) {
aoqi@0 166 // Anonymous class dependencies are through the mirror.
aoqi@0 167 to = k->java_mirror();
aoqi@0 168 } else {
aoqi@0 169 to = to_cld->class_loader();
aoqi@0 170
aoqi@0 171 // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
aoqi@0 172 // we still have to add it. The class_loader won't keep from_cld alive.
aoqi@0 173 if (!from_cld->is_anonymous()) {
aoqi@0 174 // Check that this dependency isn't from the same or parent class_loader
aoqi@0 175 oop from = from_cld->class_loader();
aoqi@0 176
aoqi@0 177 oop curr = from;
aoqi@0 178 while (curr != NULL) {
aoqi@0 179 if (curr == to) {
aoqi@0 180 return; // this class loader is in the parent list, no need to add it.
aoqi@0 181 }
aoqi@0 182 curr = java_lang_ClassLoader::parent(curr);
aoqi@0 183 }
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 // It's a dependency we won't find through GC, add it. This is relatively rare
aoqi@0 188 // Must handle over GC point.
aoqi@0 189 Handle dependency(THREAD, to);
aoqi@0 190 from_cld->_dependencies.add(dependency, CHECK);
aoqi@0 191 }
aoqi@0 192
aoqi@0 193
aoqi@0 194 void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {
aoqi@0 195 // Check first if this dependency is already in the list.
aoqi@0 196 // Save a pointer to the last to add to under the lock.
aoqi@0 197 objArrayOop ok = _list_head;
aoqi@0 198 objArrayOop last = NULL;
aoqi@0 199 while (ok != NULL) {
aoqi@0 200 last = ok;
aoqi@0 201 if (ok->obj_at(0) == dependency()) {
aoqi@0 202 // Don't need to add it
aoqi@0 203 return;
aoqi@0 204 }
aoqi@0 205 ok = (objArrayOop)ok->obj_at(1);
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 // Must handle over GC points
aoqi@0 209 assert (last != NULL, "dependencies should be initialized");
aoqi@0 210 objArrayHandle last_handle(THREAD, last);
aoqi@0 211
aoqi@0 212 // Create a new dependency node with fields for (class_loader or mirror, next)
aoqi@0 213 objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
aoqi@0 214 deps->obj_at_put(0, dependency());
aoqi@0 215
aoqi@0 216 // Must handle over GC points
aoqi@0 217 objArrayHandle new_dependency(THREAD, deps);
aoqi@0 218
aoqi@0 219 // Add the dependency under lock
aoqi@0 220 locked_add(last_handle, new_dependency, THREAD);
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,
aoqi@0 224 objArrayHandle new_dependency,
aoqi@0 225 Thread* THREAD) {
aoqi@0 226
aoqi@0 227 // Have to lock and put the new dependency on the end of the dependency
aoqi@0 228 // array so the card mark for CMS sees that this dependency is new.
aoqi@0 229 // Can probably do this lock free with some effort.
aoqi@0 230 ObjectLocker ol(Handle(THREAD, _list_head), THREAD);
aoqi@0 231
aoqi@0 232 oop loader_or_mirror = new_dependency->obj_at(0);
aoqi@0 233
aoqi@0 234 // Since the dependencies are only added, add to the end.
aoqi@0 235 objArrayOop end = last_handle();
aoqi@0 236 objArrayOop last = NULL;
aoqi@0 237 while (end != NULL) {
aoqi@0 238 last = end;
aoqi@0 239 // check again if another thread added it to the end.
aoqi@0 240 if (end->obj_at(0) == loader_or_mirror) {
aoqi@0 241 // Don't need to add it
aoqi@0 242 return;
aoqi@0 243 }
aoqi@0 244 end = (objArrayOop)end->obj_at(1);
aoqi@0 245 }
aoqi@0 246 assert (last != NULL, "dependencies should be initialized");
aoqi@0 247 // fill in the first element with the oop in new_dependency.
aoqi@0 248 if (last->obj_at(0) == NULL) {
aoqi@0 249 last->obj_at_put(0, new_dependency->obj_at(0));
aoqi@0 250 } else {
aoqi@0 251 last->obj_at_put(1, new_dependency());
aoqi@0 252 }
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 void ClassLoaderDataGraph::clear_claimed_marks() {
aoqi@0 256 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
aoqi@0 257 cld->clear_claimed();
aoqi@0 258 }
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 void ClassLoaderData::add_class(Klass* k) {
aoqi@0 262 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 263 Klass* old_value = _klasses;
aoqi@0 264 k->set_next_link(old_value);
aoqi@0 265 // link the new item into the list
aoqi@0 266 _klasses = k;
aoqi@0 267
aoqi@0 268 if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
aoqi@0 269 ResourceMark rm;
aoqi@0 270 tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
aoqi@0 271 PTR_FORMAT " loader: " PTR_FORMAT " %s",
aoqi@0 272 p2i(k),
aoqi@0 273 k->external_name(),
aoqi@0 274 p2i(k->class_loader_data()),
aoqi@0 275 p2i((void *)k->class_loader()),
aoqi@0 276 loader_name());
aoqi@0 277 }
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 // This is called by InstanceKlass::deallocate_contents() to remove the
aoqi@0 281 // scratch_class for redefine classes. We need a lock because there it may not
aoqi@0 282 // be called at a safepoint if there's an error.
aoqi@0 283 void ClassLoaderData::remove_class(Klass* scratch_class) {
aoqi@0 284 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 285 Klass* prev = NULL;
aoqi@0 286 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
aoqi@0 287 if (k == scratch_class) {
aoqi@0 288 if (prev == NULL) {
aoqi@0 289 _klasses = k->next_link();
aoqi@0 290 } else {
aoqi@0 291 Klass* next = k->next_link();
aoqi@0 292 prev->set_next_link(next);
aoqi@0 293 }
aoqi@0 294 return;
aoqi@0 295 }
aoqi@0 296 prev = k;
aoqi@0 297 assert(k != k->next_link(), "no loops!");
aoqi@0 298 }
aoqi@0 299 ShouldNotReachHere(); // should have found this class!!
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 void ClassLoaderData::unload() {
aoqi@0 303 _unloading = true;
aoqi@0 304
aoqi@0 305 // Tell serviceability tools these classes are unloading
aoqi@0 306 classes_do(InstanceKlass::notify_unload_class);
aoqi@0 307
aoqi@0 308 if (TraceClassLoaderData) {
aoqi@0 309 ResourceMark rm;
aoqi@0 310 tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this));
aoqi@0 311 tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
aoqi@0 312 loader_name());
aoqi@0 313 if (is_anonymous()) {
aoqi@0 314 tty->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
aoqi@0 315 }
aoqi@0 316 tty->print_cr("]");
aoqi@0 317 }
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
aoqi@0 321 bool alive =
aoqi@0 322 is_anonymous() ?
aoqi@0 323 is_alive_closure->do_object_b(_klasses->java_mirror()) :
aoqi@0 324 class_loader() == NULL || is_alive_closure->do_object_b(class_loader());
aoqi@0 325 assert(!alive || claimed(), "must be claimed");
aoqi@0 326 return alive;
aoqi@0 327 }
aoqi@0 328
aoqi@0 329
aoqi@0 330 ClassLoaderData::~ClassLoaderData() {
aoqi@0 331 // Release C heap structures for all the classes.
aoqi@0 332 classes_do(InstanceKlass::release_C_heap_structures);
aoqi@0 333
aoqi@0 334 Metaspace *m = _metaspace;
aoqi@0 335 if (m != NULL) {
aoqi@0 336 _metaspace = NULL;
aoqi@0 337 // release the metaspace
aoqi@0 338 delete m;
aoqi@0 339 // release the handles
aoqi@0 340 if (_handles != NULL) {
aoqi@0 341 JNIHandleBlock::release_block(_handles);
aoqi@0 342 _handles = NULL;
aoqi@0 343 }
aoqi@0 344 }
aoqi@0 345
aoqi@0 346 // Clear all the JNI handles for methods
aoqi@0 347 // These aren't deallocated and are going to look like a leak, but that's
aoqi@0 348 // needed because we can't really get rid of jmethodIDs because we don't
aoqi@0 349 // know when native code is going to stop using them. The spec says that
aoqi@0 350 // they're "invalid" but existing programs likely rely on their being
aoqi@0 351 // NULL after class unloading.
aoqi@0 352 if (_jmethod_ids != NULL) {
aoqi@0 353 Method::clear_jmethod_ids(this);
aoqi@0 354 }
aoqi@0 355 // Delete lock
aoqi@0 356 delete _metaspace_lock;
aoqi@0 357
aoqi@0 358 // Delete free list
aoqi@0 359 if (_deallocate_list != NULL) {
aoqi@0 360 delete _deallocate_list;
aoqi@0 361 }
aoqi@0 362 }
aoqi@0 363
aoqi@0 364 /**
aoqi@0 365 * Returns true if this class loader data is for the extension class loader.
aoqi@0 366 */
aoqi@0 367 bool ClassLoaderData::is_ext_class_loader_data() const {
aoqi@0 368 return SystemDictionary::is_ext_class_loader(class_loader());
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 Metaspace* ClassLoaderData::metaspace_non_null() {
aoqi@0 372 assert(!DumpSharedSpaces, "wrong metaspace!");
aoqi@0 373 // If the metaspace has not been allocated, create a new one. Might want
aoqi@0 374 // to create smaller arena for Reflection class loaders also.
aoqi@0 375 // The reason for the delayed allocation is because some class loaders are
aoqi@0 376 // simply for delegating with no metadata of their own.
aoqi@0 377 if (_metaspace == NULL) {
aoqi@0 378 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 379 // Check again if metaspace has been allocated while we were getting this lock.
aoqi@0 380 if (_metaspace != NULL) {
aoqi@0 381 return _metaspace;
aoqi@0 382 }
aoqi@0 383 if (this == the_null_class_loader_data()) {
aoqi@0 384 assert (class_loader() == NULL, "Must be");
aoqi@0 385 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
aoqi@0 386 } else if (is_anonymous()) {
aoqi@0 387 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
aoqi@0 388 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
aoqi@0 389 }
aoqi@0 390 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
aoqi@0 391 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
aoqi@0 392 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
aoqi@0 393 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
aoqi@0 394 }
aoqi@0 395 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
aoqi@0 396 } else {
aoqi@0 397 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
aoqi@0 398 }
aoqi@0 399 }
aoqi@0 400 return _metaspace;
aoqi@0 401 }
aoqi@0 402
aoqi@0 403 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
aoqi@0 404 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
aoqi@0 405
aoqi@0 406 jobject ClassLoaderData::add_handle(Handle h) {
aoqi@0 407 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 408 if (handles() == NULL) {
aoqi@0 409 set_handles(JNIHandleBlock::allocate_block());
aoqi@0 410 }
aoqi@0 411 return handles()->allocate_handle(h());
aoqi@0 412 }
aoqi@0 413
aoqi@0 414 // Add this metadata pointer to be freed when it's safe. This is only during
aoqi@0 415 // class unloading because Handles might point to this metadata field.
aoqi@0 416 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
aoqi@0 417 // Metadata in shared region isn't deleted.
aoqi@0 418 if (!m->is_shared()) {
aoqi@0 419 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 420 if (_deallocate_list == NULL) {
aoqi@0 421 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
aoqi@0 422 }
aoqi@0 423 _deallocate_list->append_if_missing(m);
aoqi@0 424 }
aoqi@0 425 }
aoqi@0 426
aoqi@0 427 // Deallocate free metadata on the free list. How useful the PermGen was!
aoqi@0 428 void ClassLoaderData::free_deallocate_list() {
aoqi@0 429 // Don't need lock, at safepoint
aoqi@0 430 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
aoqi@0 431 if (_deallocate_list == NULL) {
aoqi@0 432 return;
aoqi@0 433 }
aoqi@0 434 // Go backwards because this removes entries that are freed.
aoqi@0 435 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
aoqi@0 436 Metadata* m = _deallocate_list->at(i);
aoqi@0 437 if (!m->on_stack()) {
aoqi@0 438 _deallocate_list->remove_at(i);
aoqi@0 439 // There are only three types of metadata that we deallocate directly.
aoqi@0 440 // Cast them so they can be used by the template function.
aoqi@0 441 if (m->is_method()) {
aoqi@0 442 MetadataFactory::free_metadata(this, (Method*)m);
aoqi@0 443 } else if (m->is_constantPool()) {
aoqi@0 444 MetadataFactory::free_metadata(this, (ConstantPool*)m);
aoqi@0 445 } else if (m->is_klass()) {
aoqi@0 446 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
aoqi@0 447 } else {
aoqi@0 448 ShouldNotReachHere();
aoqi@0 449 }
aoqi@0 450 }
aoqi@0 451 }
aoqi@0 452 }
aoqi@0 453
aoqi@0 454 // These anonymous class loaders are to contain classes used for JSR292
aoqi@0 455 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
aoqi@0 456 // Add a new class loader data to the graph.
aoqi@0 457 return ClassLoaderDataGraph::add(loader, true, CHECK_NULL);
aoqi@0 458 }
aoqi@0 459
aoqi@0 460 const char* ClassLoaderData::loader_name() {
aoqi@0 461 // Handles null class loader
aoqi@0 462 return SystemDictionary::loader_name(class_loader());
aoqi@0 463 }
aoqi@0 464
aoqi@0 465 #ifndef PRODUCT
aoqi@0 466 // Define to dump klasses
aoqi@0 467 #undef CLD_DUMP_KLASSES
aoqi@0 468
aoqi@0 469 void ClassLoaderData::dump(outputStream * const out) {
aoqi@0 470 ResourceMark rm;
aoqi@0 471 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
aoqi@0 472 p2i(this), p2i((void *)class_loader()),
aoqi@0 473 p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
aoqi@0 474 if (claimed()) out->print(" claimed ");
aoqi@0 475 if (is_unloading()) out->print(" unloading ");
aoqi@0 476 out->print(" handles " INTPTR_FORMAT, p2i(handles()));
aoqi@0 477 out->cr();
aoqi@0 478 if (metaspace_or_null() != NULL) {
aoqi@0 479 out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
aoqi@0 480 metaspace_or_null()->dump(out);
aoqi@0 481 } else {
aoqi@0 482 out->print_cr("metaspace: NULL");
aoqi@0 483 }
aoqi@0 484
aoqi@0 485 #ifdef CLD_DUMP_KLASSES
aoqi@0 486 if (Verbose) {
aoqi@0 487 ResourceMark rm;
aoqi@0 488 Klass* k = _klasses;
aoqi@0 489 while (k != NULL) {
aoqi@0 490 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
aoqi@0 491 k->has_modified_oops(), k->has_accumulated_modified_oops());
aoqi@0 492 assert(k != k->next_link(), "no loops!");
aoqi@0 493 k = k->next_link();
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496 #endif // CLD_DUMP_KLASSES
aoqi@0 497 #undef CLD_DUMP_KLASSES
aoqi@0 498 if (_jmethod_ids != NULL) {
aoqi@0 499 Method::print_jmethod_ids(this, out);
aoqi@0 500 }
aoqi@0 501 out->print_cr("}");
aoqi@0 502 }
aoqi@0 503 #endif // PRODUCT
aoqi@0 504
aoqi@0 505 void ClassLoaderData::verify() {
aoqi@0 506 oop cl = class_loader();
aoqi@0 507
aoqi@0 508 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
aoqi@0 509 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
aoqi@0 510
aoqi@0 511 // Verify the integrity of the allocated space.
aoqi@0 512 if (metaspace_or_null() != NULL) {
aoqi@0 513 metaspace_or_null()->verify();
aoqi@0 514 }
aoqi@0 515
aoqi@0 516 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
aoqi@0 517 guarantee(k->class_loader_data() == this, "Must be the same");
aoqi@0 518 k->verify();
aoqi@0 519 assert(k != k->next_link(), "no loops!");
aoqi@0 520 }
aoqi@0 521 }
aoqi@0 522
aoqi@0 523 bool ClassLoaderData::contains_klass(Klass* klass) {
aoqi@0 524 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
aoqi@0 525 if (k == klass) return true;
aoqi@0 526 }
aoqi@0 527 return false;
aoqi@0 528 }
aoqi@0 529
aoqi@0 530
aoqi@0 531 // GC root of class loader data created.
aoqi@0 532 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
aoqi@0 533 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
aoqi@0 534 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
aoqi@0 535 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
aoqi@0 536
aoqi@0 537 bool ClassLoaderDataGraph::_should_purge = false;
aoqi@0 538
aoqi@0 539 // Add a new class loader data node to the list. Assign the newly created
aoqi@0 540 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
aoqi@0 541 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
aoqi@0 542 // We need to allocate all the oops for the ClassLoaderData before allocating the
aoqi@0 543 // actual ClassLoaderData object.
aoqi@0 544 ClassLoaderData::Dependencies dependencies(CHECK_NULL);
aoqi@0 545
aoqi@0 546 No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
aoqi@0 547 // ClassLoaderData in the graph since the CLD
aoqi@0 548 // contains unhandled oops
aoqi@0 549
aoqi@0 550 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
aoqi@0 551
aoqi@0 552
aoqi@0 553 if (!is_anonymous) {
aoqi@0 554 ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
aoqi@0 555 // First, Atomically set it
aoqi@0 556 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
aoqi@0 557 if (old != NULL) {
aoqi@0 558 delete cld;
aoqi@0 559 // Returns the data.
aoqi@0 560 return old;
aoqi@0 561 }
aoqi@0 562 }
aoqi@0 563
aoqi@0 564 // We won the race, and therefore the task of adding the data to the list of
aoqi@0 565 // class loader data
aoqi@0 566 ClassLoaderData** list_head = &_head;
aoqi@0 567 ClassLoaderData* next = _head;
aoqi@0 568
aoqi@0 569 do {
aoqi@0 570 cld->set_next(next);
aoqi@0 571 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
aoqi@0 572 if (exchanged == next) {
aoqi@0 573 if (TraceClassLoaderData) {
aoqi@0 574 ResourceMark rm;
aoqi@0 575 tty->print("[ClassLoaderData: ");
aoqi@0 576 tty->print("create class loader data " INTPTR_FORMAT, p2i(cld));
aoqi@0 577 tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
aoqi@0 578 cld->loader_name());
aoqi@0 579 tty->print_cr("]");
aoqi@0 580 }
aoqi@0 581 return cld;
aoqi@0 582 }
aoqi@0 583 next = exchanged;
aoqi@0 584 } while (true);
aoqi@0 585
aoqi@0 586 }
aoqi@0 587
aoqi@0 588 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
aoqi@0 589 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
aoqi@0 590 cld->oops_do(f, klass_closure, must_claim);
aoqi@0 591 }
aoqi@0 592 }
aoqi@0 593
aoqi@0 594 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
aoqi@0 595 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
aoqi@0 596 if (cld->keep_alive()) {
aoqi@0 597 cld->oops_do(f, klass_closure, must_claim);
aoqi@0 598 }
aoqi@0 599 }
aoqi@0 600 }
aoqi@0 601
aoqi@0 602 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
aoqi@0 603 if (ClassUnloading) {
aoqi@0 604 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
aoqi@0 605 // keep any special CLDs alive.
aoqi@0 606 ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
aoqi@0 607 } else {
aoqi@0 608 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
aoqi@0 609 }
aoqi@0 610 }
aoqi@0 611
aoqi@0 612 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
aoqi@0 613 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
aoqi@0 614 cld->classes_do(klass_closure);
aoqi@0 615 }
aoqi@0 616 }
aoqi@0 617
aoqi@0 618 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
aoqi@0 619 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
aoqi@0 620 cld->classes_do(f);
aoqi@0 621 }
aoqi@0 622 }
aoqi@0 623
aoqi@0 624 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
aoqi@0 625 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
aoqi@0 626 cld->loaded_classes_do(klass_closure);
aoqi@0 627 }
aoqi@0 628 }
aoqi@0 629
aoqi@0 630 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
aoqi@0 631 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
aoqi@0 632 // Only walk the head until any clds not purged from prior unloading
aoqi@0 633 // (CMS doesn't purge right away).
aoqi@0 634 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
aoqi@0 635 cld->classes_do(f);
aoqi@0 636 }
aoqi@0 637 }
aoqi@0 638
aoqi@0 639 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
aoqi@0 640 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
aoqi@0 641
aoqi@0 642 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
aoqi@0 643
aoqi@0 644 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
aoqi@0 645 ClassLoaderData* curr = _head;
aoqi@0 646 while (curr != _saved_head) {
aoqi@0 647 if (!curr->claimed()) {
aoqi@0 648 array->push(curr);
aoqi@0 649
aoqi@0 650 if (TraceClassLoaderData) {
aoqi@0 651 tty->print("[ClassLoaderData] found new CLD: ");
aoqi@0 652 curr->print_value_on(tty);
aoqi@0 653 tty->cr();
aoqi@0 654 }
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 curr = curr->_next;
aoqi@0 658 }
aoqi@0 659
aoqi@0 660 return array;
aoqi@0 661 }
aoqi@0 662
aoqi@0 663 #ifndef PRODUCT
aoqi@0 664 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
aoqi@0 665 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
aoqi@0 666 if (loader_data == data) {
aoqi@0 667 return true;
aoqi@0 668 }
aoqi@0 669 }
aoqi@0 670
aoqi@0 671 return false;
aoqi@0 672 }
aoqi@0 673 #endif // PRODUCT
aoqi@0 674
aoqi@0 675
aoqi@0 676 // Move class loader data from main list to the unloaded list for unloading
aoqi@0 677 // and deallocation later.
aoqi@0 678 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) {
aoqi@0 679 ClassLoaderData* data = _head;
aoqi@0 680 ClassLoaderData* prev = NULL;
aoqi@0 681 bool seen_dead_loader = false;
aoqi@0 682
aoqi@0 683 // Save previous _unloading pointer for CMS which may add to unloading list before
aoqi@0 684 // purging and we don't want to rewalk the previously unloaded class loader data.
aoqi@0 685 _saved_unloading = _unloading;
aoqi@0 686
aoqi@0 687 // mark metadata seen on the stack and code cache so we can delete
aoqi@0 688 // unneeded entries.
aoqi@0 689 bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
aoqi@0 690 MetadataOnStackMark md_on_stack;
aoqi@0 691 while (data != NULL) {
aoqi@0 692 if (data->keep_alive() || data->is_alive(is_alive_closure)) {
aoqi@0 693 if (has_redefined_a_class) {
aoqi@0 694 data->classes_do(InstanceKlass::purge_previous_versions);
aoqi@0 695 }
aoqi@0 696 data->free_deallocate_list();
aoqi@0 697 prev = data;
aoqi@0 698 data = data->next();
aoqi@0 699 continue;
aoqi@0 700 }
aoqi@0 701 seen_dead_loader = true;
aoqi@0 702 ClassLoaderData* dead = data;
aoqi@0 703 dead->unload();
aoqi@0 704 data = data->next();
aoqi@0 705 // Remove from loader list.
aoqi@0 706 // This class loader data will no longer be found
aoqi@0 707 // in the ClassLoaderDataGraph.
aoqi@0 708 if (prev != NULL) {
aoqi@0 709 prev->set_next(data);
aoqi@0 710 } else {
aoqi@0 711 assert(dead == _head, "sanity check");
aoqi@0 712 _head = data;
aoqi@0 713 }
aoqi@0 714 dead->set_next(_unloading);
aoqi@0 715 _unloading = dead;
aoqi@0 716 }
aoqi@0 717
aoqi@0 718 if (seen_dead_loader) {
aoqi@0 719 post_class_unload_events();
aoqi@0 720 }
aoqi@0 721
aoqi@0 722 return seen_dead_loader;
aoqi@0 723 }
aoqi@0 724
aoqi@0 725 void ClassLoaderDataGraph::purge() {
aoqi@0 726 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
aoqi@0 727 ClassLoaderData* list = _unloading;
aoqi@0 728 _unloading = NULL;
aoqi@0 729 ClassLoaderData* next = list;
aoqi@0 730 while (next != NULL) {
aoqi@0 731 ClassLoaderData* purge_me = next;
aoqi@0 732 next = purge_me->next();
aoqi@0 733 delete purge_me;
aoqi@0 734 }
aoqi@0 735 Metaspace::purge();
aoqi@0 736 }
aoqi@0 737
aoqi@0 738 void ClassLoaderDataGraph::post_class_unload_events(void) {
aoqi@0 739 #if INCLUDE_TRACE
aoqi@0 740 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
aoqi@0 741 if (Tracing::enabled()) {
aoqi@0 742 if (Tracing::is_event_enabled(TraceClassUnloadEvent)) {
aoqi@0 743 assert(_unloading != NULL, "need class loader data unload list!");
aoqi@0 744 _class_unload_time = Ticks::now();
aoqi@0 745 classes_unloading_do(&class_unload_event);
aoqi@0 746 }
aoqi@0 747 Tracing::on_unloading_classes();
aoqi@0 748 }
aoqi@0 749 #endif
aoqi@0 750 }
aoqi@0 751
aoqi@0 752 // CDS support
aoqi@0 753
aoqi@0 754 // Global metaspaces for writing information to the shared archive. When
aoqi@0 755 // application CDS is supported, we may need one per metaspace, so this
aoqi@0 756 // sort of looks like it.
aoqi@0 757 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
aoqi@0 758 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
aoqi@0 759 static bool _shared_metaspaces_initialized = false;
aoqi@0 760
aoqi@0 761 // Initialize shared metaspaces (change to call from somewhere not lazily)
aoqi@0 762 void ClassLoaderData::initialize_shared_metaspaces() {
aoqi@0 763 assert(DumpSharedSpaces, "only use this for dumping shared spaces");
aoqi@0 764 assert(this == ClassLoaderData::the_null_class_loader_data(),
aoqi@0 765 "only supported for null loader data for now");
aoqi@0 766 assert (!_shared_metaspaces_initialized, "only initialize once");
aoqi@0 767 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 768 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
aoqi@0 769 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
aoqi@0 770 _shared_metaspaces_initialized = true;
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 Metaspace* ClassLoaderData::ro_metaspace() {
aoqi@0 774 assert(_ro_metaspace != NULL, "should already be initialized");
aoqi@0 775 return _ro_metaspace;
aoqi@0 776 }
aoqi@0 777
aoqi@0 778 Metaspace* ClassLoaderData::rw_metaspace() {
aoqi@0 779 assert(_rw_metaspace != NULL, "should already be initialized");
aoqi@0 780 return _rw_metaspace;
aoqi@0 781 }
aoqi@0 782
aoqi@0 783
aoqi@0 784 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
aoqi@0 785 _data = ClassLoaderDataGraph::_head;
aoqi@0 786 }
aoqi@0 787
aoqi@0 788 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
aoqi@0 789
aoqi@0 790 #ifndef PRODUCT
aoqi@0 791 // callable from debugger
aoqi@0 792 extern "C" int print_loader_data_graph() {
aoqi@0 793 ClassLoaderDataGraph::dump_on(tty);
aoqi@0 794 return 0;
aoqi@0 795 }
aoqi@0 796
aoqi@0 797 void ClassLoaderDataGraph::verify() {
aoqi@0 798 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
aoqi@0 799 data->verify();
aoqi@0 800 }
aoqi@0 801 }
aoqi@0 802
aoqi@0 803 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
aoqi@0 804 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
aoqi@0 805 data->dump(out);
aoqi@0 806 }
aoqi@0 807 MetaspaceAux::dump(out);
aoqi@0 808 }
aoqi@0 809 #endif // PRODUCT
aoqi@0 810
aoqi@0 811 void ClassLoaderData::print_value_on(outputStream* out) const {
aoqi@0 812 if (class_loader() == NULL) {
aoqi@0 813 out->print("NULL class_loader");
aoqi@0 814 } else {
aoqi@0 815 out->print("class loader " INTPTR_FORMAT, p2i(this));
aoqi@0 816 class_loader()->print_value_on(out);
aoqi@0 817 }
aoqi@0 818 }
aoqi@0 819
aoqi@0 820 #if INCLUDE_TRACE
aoqi@0 821
aoqi@0 822 Ticks ClassLoaderDataGraph::_class_unload_time;
aoqi@0 823
aoqi@0 824 void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
aoqi@0 825
aoqi@0 826 // post class unload event
aoqi@0 827 EventClassUnload event(UNTIMED);
aoqi@0 828 event.set_endtime(_class_unload_time);
aoqi@0 829 event.set_unloadedClass(k);
aoqi@0 830 oop defining_class_loader = k->class_loader();
aoqi@0 831 event.set_definingClassLoader(defining_class_loader != NULL ?
aoqi@0 832 defining_class_loader->klass() : (Klass*)NULL);
aoqi@0 833 event.commit();
aoqi@0 834 }
aoqi@0 835
aoqi@0 836 #endif /* INCLUDE_TRACE */

mercurial