src/share/vm/classfile/classLoaderData.cpp

Sat, 01 Sep 2012 13:25:18 -0400

author
coleenp
date
Sat, 01 Sep 2012 13:25:18 -0400
changeset 4037
da91efe96a93
child 4304
90273fc0a981
permissions
-rw-r--r--

6964458: Reimplement class meta-data storage to use native memory
Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>

coleenp@4037 1 /*
coleenp@4037 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
coleenp@4037 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@4037 4 *
coleenp@4037 5 * This code is free software; you can redistribute it and/or modify it
coleenp@4037 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@4037 7 * published by the Free Software Foundation.
coleenp@4037 8 *
coleenp@4037 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@4037 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@4037 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@4037 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@4037 13 * accompanied this code).
coleenp@4037 14 *
coleenp@4037 15 * You should have received a copy of the GNU General Public License version
coleenp@4037 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@4037 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@4037 18 *
coleenp@4037 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@4037 20 * or visit www.oracle.com if you need additional information or have any
coleenp@4037 21 * questions.
coleenp@4037 22 *
coleenp@4037 23 */
coleenp@4037 24
coleenp@4037 25 // A ClassLoaderData identifies the full set of class types that a class
coleenp@4037 26 // loader's name resolution strategy produces for a given configuration of the
coleenp@4037 27 // class loader.
coleenp@4037 28 // Class types in the ClassLoaderData may be defined by from class file binaries
coleenp@4037 29 // provided by the class loader, or from other class loader it interacts with
coleenp@4037 30 // according to its name resolution strategy.
coleenp@4037 31 //
coleenp@4037 32 // Class loaders that implement a deterministic name resolution strategy
coleenp@4037 33 // (including with respect to their delegation behavior), such as the boot, the
coleenp@4037 34 // extension, and the system loaders of the JDK's built-in class loader
coleenp@4037 35 // hierarchy, always produce the same linkset for a given configuration.
coleenp@4037 36 //
coleenp@4037 37 // ClassLoaderData carries information related to a linkset (e.g.,
coleenp@4037 38 // metaspace holding its klass definitions).
coleenp@4037 39 // The System Dictionary and related data structures (e.g., placeholder table,
coleenp@4037 40 // loader constraints table) as well as the runtime representation of classes
coleenp@4037 41 // only reference ClassLoaderData.
coleenp@4037 42 //
coleenp@4037 43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
coleenp@4037 44 // that represent the loader's "linking domain" in the JVM.
coleenp@4037 45 //
coleenp@4037 46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
coleenp@4037 47 // the singleton class the_null_class_loader_data().
coleenp@4037 48
coleenp@4037 49 #include "precompiled.hpp"
coleenp@4037 50 #include "classfile/classLoaderData.hpp"
coleenp@4037 51 #include "classfile/classLoaderData.inline.hpp"
coleenp@4037 52 #include "classfile/javaClasses.hpp"
coleenp@4037 53 #include "classfile/systemDictionary.hpp"
coleenp@4037 54 #include "code/codeCache.hpp"
coleenp@4037 55 #include "memory/metadataFactory.hpp"
coleenp@4037 56 #include "memory/metaspaceShared.hpp"
coleenp@4037 57 #include "prims/jvmtiRedefineClasses.hpp"
coleenp@4037 58 #include "runtime/jniHandles.hpp"
coleenp@4037 59 #include "runtime/mutex.hpp"
coleenp@4037 60 #include "runtime/safepoint.hpp"
coleenp@4037 61 #include "runtime/synchronizer.hpp"
coleenp@4037 62 #include "utilities/growableArray.hpp"
coleenp@4037 63 #include "utilities/ostream.hpp"
coleenp@4037 64
coleenp@4037 65 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
coleenp@4037 66
coleenp@4037 67 ClassLoaderData::ClassLoaderData(Handle h_class_loader) : _class_loader(h_class_loader()),
coleenp@4037 68 _metaspace(NULL), _unloading(false), _klasses(NULL),
coleenp@4037 69 _claimed(0), _jmethod_ids(NULL), _handles(NULL),
coleenp@4037 70 _deallocate_list(NULL), _next(NULL),
coleenp@4037 71 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
coleenp@4037 72 // empty
coleenp@4037 73 }
coleenp@4037 74
coleenp@4037 75 bool ClassLoaderData::claim() {
coleenp@4037 76 if (_claimed == 1) {
coleenp@4037 77 return false;
coleenp@4037 78 }
coleenp@4037 79
coleenp@4037 80 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
coleenp@4037 81 }
coleenp@4037 82
coleenp@4037 83 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 84 if (must_claim && !claim()) {
coleenp@4037 85 return;
coleenp@4037 86 }
coleenp@4037 87
coleenp@4037 88 f->do_oop(&_class_loader);
coleenp@4037 89 _handles->oops_do(f);
coleenp@4037 90 if (klass_closure != NULL) {
coleenp@4037 91 classes_do(klass_closure);
coleenp@4037 92 }
coleenp@4037 93 }
coleenp@4037 94
coleenp@4037 95 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
coleenp@4037 96 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 97 klass_closure->do_klass(k);
coleenp@4037 98 }
coleenp@4037 99 }
coleenp@4037 100
coleenp@4037 101 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
coleenp@4037 102 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 103 if (k->oop_is_instance()) {
coleenp@4037 104 f(InstanceKlass::cast(k));
coleenp@4037 105 }
coleenp@4037 106 }
coleenp@4037 107 }
coleenp@4037 108
coleenp@4037 109 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
coleenp@4037 110 ClassLoaderData * const from_cld = this;
coleenp@4037 111 ClassLoaderData * const to_cld = k->class_loader_data();
coleenp@4037 112
coleenp@4037 113 // Records dependency between non-null class loaders only.
coleenp@4037 114 if (to_cld->is_the_null_class_loader_data() || from_cld->is_the_null_class_loader_data()) {
coleenp@4037 115 return;
coleenp@4037 116 }
coleenp@4037 117
coleenp@4037 118 // Check that this dependency isn't from the same or parent class_loader
coleenp@4037 119 oop to = to_cld->class_loader();
coleenp@4037 120 oop from = from_cld->class_loader();
coleenp@4037 121
coleenp@4037 122 oop curr = from;
coleenp@4037 123 while (curr != NULL) {
coleenp@4037 124 if (curr == to) {
coleenp@4037 125 return; // this class loader is in the parent list, no need to add it.
coleenp@4037 126 }
coleenp@4037 127 curr = java_lang_ClassLoader::parent(curr);
coleenp@4037 128 }
coleenp@4037 129
coleenp@4037 130 // It's a dependency we won't find through GC, add it. This is relatively rare
coleenp@4037 131 from_cld->add_dependency(to_cld, CHECK);
coleenp@4037 132 }
coleenp@4037 133
coleenp@4037 134 bool ClassLoaderData::has_dependency(ClassLoaderData* dependency) {
coleenp@4037 135 oop loader = dependency->class_loader();
coleenp@4037 136
coleenp@4037 137 // Get objArrayOop out of the class_loader oop and see if this dependency
coleenp@4037 138 // is there. Don't safepoint! These are all oops.
coleenp@4037 139 // Dependency list is (oop class_loader, objArrayOop next)
coleenp@4037 140 objArrayOop ok = (objArrayOop)java_lang_ClassLoader::dependencies(class_loader());
coleenp@4037 141 while (ok != NULL) {
coleenp@4037 142 if (ok->obj_at(0) == loader) {
coleenp@4037 143 return true;
coleenp@4037 144 }
coleenp@4037 145 ok = (objArrayOop)ok->obj_at(1);
coleenp@4037 146 }
coleenp@4037 147 return false;
coleenp@4037 148 }
coleenp@4037 149
coleenp@4037 150 void ClassLoaderData::add_dependency(ClassLoaderData* dependency, TRAPS) {
coleenp@4037 151 // Minimize the number of duplicates in the list.
coleenp@4037 152 if (has_dependency(dependency)) {
coleenp@4037 153 return;
coleenp@4037 154 }
coleenp@4037 155
coleenp@4037 156 // Create a new dependency node with fields for (class_loader, next)
coleenp@4037 157 objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
coleenp@4037 158 deps->obj_at_put(0, dependency->class_loader());
coleenp@4037 159
coleenp@4037 160 // Add this lock free, using compare and exchange, need barriers for GC
coleenp@4037 161 // Do the barrier first.
coleenp@4037 162 HeapWord* addr = java_lang_ClassLoader::dependencies_addr(class_loader());
coleenp@4037 163 while (true) {
coleenp@4037 164 oop old_dependency = java_lang_ClassLoader::dependencies(class_loader());
coleenp@4037 165 deps->obj_at_put(1, old_dependency);
coleenp@4037 166
coleenp@4037 167 oop newold = oopDesc::atomic_compare_exchange_oop((oop)deps, addr, old_dependency, true);
coleenp@4037 168 if (newold == old_dependency) {
coleenp@4037 169 update_barrier_set((void*)addr, (oop)deps);
coleenp@4037 170 // we won the race to add this dependency
coleenp@4037 171 break;
coleenp@4037 172 }
coleenp@4037 173 }
coleenp@4037 174 }
coleenp@4037 175
coleenp@4037 176
coleenp@4037 177 void ClassLoaderDataGraph::clear_claimed_marks() {
coleenp@4037 178 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 179 cld->clear_claimed();
coleenp@4037 180 }
coleenp@4037 181 }
coleenp@4037 182
coleenp@4037 183 void ClassLoaderData::add_class(Klass* k) {
coleenp@4037 184 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 185 Klass* old_value = _klasses;
coleenp@4037 186 k->set_next_link(old_value);
coleenp@4037 187 // link the new item into the list
coleenp@4037 188 _klasses = k;
coleenp@4037 189
coleenp@4037 190 if (TraceClassLoaderData && k->class_loader_data() != NULL) {
coleenp@4037 191 ResourceMark rm;
coleenp@4037 192 tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
coleenp@4037 193 PTR_FORMAT " loader: " PTR_FORMAT " %s",
coleenp@4037 194 k,
coleenp@4037 195 k->external_name(),
coleenp@4037 196 k->class_loader_data(),
coleenp@4037 197 k->class_loader(),
coleenp@4037 198 k->class_loader() != NULL ? k->class_loader()->klass()->external_name() : "NULL"
coleenp@4037 199 );
coleenp@4037 200 }
coleenp@4037 201 }
coleenp@4037 202
coleenp@4037 203 // This is called by InstanceKlass::deallocate_contents() to remove the
coleenp@4037 204 // scratch_class for redefine classes. We need a lock because there it may not
coleenp@4037 205 // be called at a safepoint if there's an error.
coleenp@4037 206 void ClassLoaderData::remove_class(Klass* scratch_class) {
coleenp@4037 207 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 208 Klass* prev = NULL;
coleenp@4037 209 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 210 if (k == scratch_class) {
coleenp@4037 211 if (prev == NULL) {
coleenp@4037 212 _klasses = k->next_link();
coleenp@4037 213 } else {
coleenp@4037 214 Klass* next = k->next_link();
coleenp@4037 215 prev->set_next_link(next);
coleenp@4037 216 }
coleenp@4037 217 return;
coleenp@4037 218 }
coleenp@4037 219 prev = k;
coleenp@4037 220 }
coleenp@4037 221 ShouldNotReachHere(); // should have found this class!!
coleenp@4037 222 }
coleenp@4037 223
coleenp@4037 224 ClassLoaderData::~ClassLoaderData() {
coleenp@4037 225 Metaspace *m = _metaspace;
coleenp@4037 226 if (m != NULL) {
coleenp@4037 227 _metaspace = NULL;
coleenp@4037 228 // release the metaspace
coleenp@4037 229 delete m;
coleenp@4037 230 // release the handles
coleenp@4037 231 if (_handles != NULL) {
coleenp@4037 232 JNIHandleBlock::release_block(_handles);
coleenp@4037 233 _handles = NULL;
coleenp@4037 234 }
coleenp@4037 235 }
coleenp@4037 236
coleenp@4037 237 // Clear all the JNI handles for methods
coleenp@4037 238 // These aren't deallocated and are going to look like a leak, but that's
coleenp@4037 239 // needed because we can't really get rid of jmethodIDs because we don't
coleenp@4037 240 // know when native code is going to stop using them. The spec says that
coleenp@4037 241 // they're "invalid" but existing programs likely rely on their being
coleenp@4037 242 // NULL after class unloading.
coleenp@4037 243 if (_jmethod_ids != NULL) {
coleenp@4037 244 Method::clear_jmethod_ids(this);
coleenp@4037 245 }
coleenp@4037 246 // Delete lock
coleenp@4037 247 delete _metaspace_lock;
coleenp@4037 248
coleenp@4037 249 // Delete free list
coleenp@4037 250 if (_deallocate_list != NULL) {
coleenp@4037 251 delete _deallocate_list;
coleenp@4037 252 }
coleenp@4037 253 }
coleenp@4037 254
coleenp@4037 255 Metaspace* ClassLoaderData::metaspace_non_null() {
coleenp@4037 256 // If the metaspace has not been allocated, create a new one. Might want
coleenp@4037 257 // to create smaller arena for Reflection class loaders also.
coleenp@4037 258 // The reason for the delayed allocation is because some class loaders are
coleenp@4037 259 // simply for delegating with no metadata of their own.
coleenp@4037 260 if (_metaspace == NULL) {
coleenp@4037 261 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 262 // Check again if metaspace has been allocated while we were getting this lock.
coleenp@4037 263 if (_metaspace != NULL) {
coleenp@4037 264 return _metaspace;
coleenp@4037 265 }
coleenp@4037 266 if (class_loader() == NULL) {
coleenp@4037 267 assert(this == the_null_class_loader_data(), "Must be");
coleenp@4037 268 size_t word_size = Metaspace::first_chunk_word_size();
coleenp@4037 269 set_metaspace(new Metaspace(_metaspace_lock, word_size));
coleenp@4037 270 } else {
coleenp@4037 271 set_metaspace(new Metaspace(_metaspace_lock)); // default size for now.
coleenp@4037 272 }
coleenp@4037 273 }
coleenp@4037 274 return _metaspace;
coleenp@4037 275 }
coleenp@4037 276
coleenp@4037 277 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
coleenp@4037 278 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
coleenp@4037 279
coleenp@4037 280 jobject ClassLoaderData::add_handle(Handle h) {
coleenp@4037 281 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 282 if (handles() == NULL) {
coleenp@4037 283 set_handles(JNIHandleBlock::allocate_block());
coleenp@4037 284 }
coleenp@4037 285 return handles()->allocate_handle(h());
coleenp@4037 286 }
coleenp@4037 287
coleenp@4037 288 // Add this metadata pointer to be freed when it's safe. This is only during
coleenp@4037 289 // class unloading because Handles might point to this metadata field.
coleenp@4037 290 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
coleenp@4037 291 // Metadata in shared region isn't deleted.
coleenp@4037 292 if (!m->is_shared()) {
coleenp@4037 293 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 294 if (_deallocate_list == NULL) {
coleenp@4037 295 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
coleenp@4037 296 }
coleenp@4037 297 _deallocate_list->append_if_missing(m);
coleenp@4037 298 }
coleenp@4037 299 }
coleenp@4037 300
coleenp@4037 301 // Deallocate free metadata on the free list. How useful the PermGen was!
coleenp@4037 302 void ClassLoaderData::free_deallocate_list() {
coleenp@4037 303 // Don't need lock, at safepoint
coleenp@4037 304 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
coleenp@4037 305 if (_deallocate_list == NULL) {
coleenp@4037 306 return;
coleenp@4037 307 }
coleenp@4037 308 // Go backwards because this removes entries that are freed.
coleenp@4037 309 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
coleenp@4037 310 Metadata* m = _deallocate_list->at(i);
coleenp@4037 311 if (!m->on_stack()) {
coleenp@4037 312 _deallocate_list->remove_at(i);
coleenp@4037 313 // There are only three types of metadata that we deallocate directly.
coleenp@4037 314 // Cast them so they can be used by the template function.
coleenp@4037 315 if (m->is_method()) {
coleenp@4037 316 MetadataFactory::free_metadata(this, (Method*)m);
coleenp@4037 317 } else if (m->is_constantPool()) {
coleenp@4037 318 MetadataFactory::free_metadata(this, (ConstantPool*)m);
coleenp@4037 319 } else if (m->is_klass()) {
coleenp@4037 320 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
coleenp@4037 321 } else {
coleenp@4037 322 ShouldNotReachHere();
coleenp@4037 323 }
coleenp@4037 324 }
coleenp@4037 325 }
coleenp@4037 326 }
coleenp@4037 327
coleenp@4037 328 #ifndef PRODUCT
coleenp@4037 329 void ClassLoaderData::print_loader(ClassLoaderData *loader_data, outputStream* out) {
coleenp@4037 330 oop class_loader = loader_data->class_loader();
coleenp@4037 331 out->print("%s", SystemDictionary::loader_name(class_loader));
coleenp@4037 332 }
coleenp@4037 333
coleenp@4037 334 // Define to dump klasses
coleenp@4037 335 #undef CLD_DUMP_KLASSES
coleenp@4037 336
coleenp@4037 337 void ClassLoaderData::dump(outputStream * const out) {
coleenp@4037 338 ResourceMark rm;
coleenp@4037 339 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
coleenp@4037 340 this, class_loader(),
coleenp@4037 341 class_loader() != NULL ? class_loader()->klass() : NULL,
coleenp@4037 342 class_loader() != NULL ? class_loader()->klass()->external_name() : "NULL");
coleenp@4037 343 if (claimed()) out->print(" claimed ");
coleenp@4037 344 if (is_unloading()) out->print(" unloading ");
coleenp@4037 345 out->print(" handles " INTPTR_FORMAT, handles());
coleenp@4037 346 out->cr();
coleenp@4037 347 if (metaspace_or_null() != NULL) {
coleenp@4037 348 out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null());
coleenp@4037 349 metaspace_or_null()->dump(out);
coleenp@4037 350 } else {
coleenp@4037 351 out->print_cr("metaspace: NULL");
coleenp@4037 352 }
coleenp@4037 353
coleenp@4037 354 #ifdef CLD_DUMP_KLASSES
coleenp@4037 355 if (Verbose) {
coleenp@4037 356 ResourceMark rm;
coleenp@4037 357 Klass* k = _klasses;
coleenp@4037 358 while (k != NULL) {
coleenp@4037 359 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
coleenp@4037 360 k->has_modified_oops(), k->has_accumulated_modified_oops());
coleenp@4037 361 k = k->next_link();
coleenp@4037 362 }
coleenp@4037 363 }
coleenp@4037 364 #endif // CLD_DUMP_KLASSES
coleenp@4037 365 #undef CLD_DUMP_KLASSES
coleenp@4037 366 if (_jmethod_ids != NULL) {
coleenp@4037 367 Method::print_jmethod_ids(this, out);
coleenp@4037 368 }
coleenp@4037 369 out->print_cr("}");
coleenp@4037 370 }
coleenp@4037 371 #endif // PRODUCT
coleenp@4037 372
coleenp@4037 373 void ClassLoaderData::verify() {
coleenp@4037 374 oop cl = class_loader();
coleenp@4037 375
coleenp@4037 376 guarantee(this == class_loader_data(cl), "Must be the same");
coleenp@4037 377 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data(), "must be");
coleenp@4037 378
coleenp@4037 379 // Verify the integrity of the allocated space.
coleenp@4037 380 if (metaspace_or_null() != NULL) {
coleenp@4037 381 metaspace_or_null()->verify();
coleenp@4037 382 }
coleenp@4037 383
coleenp@4037 384 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 385 guarantee(k->class_loader_data() == this, "Must be the same");
coleenp@4037 386 k->verify();
coleenp@4037 387 }
coleenp@4037 388 }
coleenp@4037 389
coleenp@4037 390 // GC root of class loader data created.
coleenp@4037 391 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
coleenp@4037 392 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
coleenp@4037 393 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
coleenp@4037 394
coleenp@4037 395
coleenp@4037 396 // Add a new class loader data node to the list. Assign the newly created
coleenp@4037 397 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
coleenp@4037 398 ClassLoaderData* ClassLoaderDataGraph::add(ClassLoaderData** cld_addr, Handle loader_data) {
coleenp@4037 399 // Not assigned a class loader data yet.
coleenp@4037 400 // Create one.
coleenp@4037 401 ClassLoaderData* *list_head = &_head;
coleenp@4037 402 ClassLoaderData* next = _head;
coleenp@4037 403 ClassLoaderData* cld = new ClassLoaderData(loader_data);
coleenp@4037 404
coleenp@4037 405 // First, Atomically set it.
coleenp@4037 406 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
coleenp@4037 407 if (old != NULL) {
coleenp@4037 408 delete cld;
coleenp@4037 409 // Returns the data.
coleenp@4037 410 return old;
coleenp@4037 411 }
coleenp@4037 412
coleenp@4037 413 // We won the race, and therefore the task of adding the data to the list of
coleenp@4037 414 // class loader data
coleenp@4037 415 do {
coleenp@4037 416 cld->set_next(next);
coleenp@4037 417 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
coleenp@4037 418 if (exchanged == next) {
coleenp@4037 419 if (TraceClassLoaderData) {
coleenp@4037 420 tty->print("[ClassLoaderData: ");
coleenp@4037 421 tty->print("create class loader data "PTR_FORMAT, cld);
coleenp@4037 422 tty->print(" for instance "PTR_FORMAT" of ", cld->class_loader());
coleenp@4037 423 loader_data->klass()->name()->print_symbol_on(tty);
coleenp@4037 424 tty->print_cr("]");
coleenp@4037 425 }
coleenp@4037 426 return cld;
coleenp@4037 427 }
coleenp@4037 428 next = exchanged;
coleenp@4037 429 } while (true);
coleenp@4037 430 }
coleenp@4037 431
coleenp@4037 432 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 433 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 434 cld->oops_do(f, klass_closure, must_claim);
coleenp@4037 435 }
coleenp@4037 436 }
coleenp@4037 437
coleenp@4037 438 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 439 if (ClassUnloading) {
coleenp@4037 440 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
coleenp@4037 441 } else {
coleenp@4037 442 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
coleenp@4037 443 }
coleenp@4037 444 }
coleenp@4037 445
coleenp@4037 446 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
coleenp@4037 447 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 448 cld->classes_do(klass_closure);
coleenp@4037 449 }
coleenp@4037 450 }
coleenp@4037 451
coleenp@4037 452 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
coleenp@4037 453 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
coleenp@4037 454
coleenp@4037 455 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
coleenp@4037 456
coleenp@4037 457 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
coleenp@4037 458 ClassLoaderData* curr = _head;
coleenp@4037 459 while (curr != _saved_head) {
coleenp@4037 460 if (!curr->claimed()) {
coleenp@4037 461 array->push(curr);
coleenp@4037 462
coleenp@4037 463 if (TraceClassLoaderData) {
coleenp@4037 464 tty->print("[ClassLoaderData] found new CLD: ");
coleenp@4037 465 curr->print_value_on(tty);
coleenp@4037 466 tty->cr();
coleenp@4037 467 }
coleenp@4037 468 }
coleenp@4037 469
coleenp@4037 470 curr = curr->_next;
coleenp@4037 471 }
coleenp@4037 472
coleenp@4037 473 return array;
coleenp@4037 474 }
coleenp@4037 475
coleenp@4037 476 #ifndef PRODUCT
coleenp@4037 477 // for debugging and hsfind(x)
coleenp@4037 478 bool ClassLoaderDataGraph::contains(address x) {
coleenp@4037 479 // I think we need the _metaspace_lock taken here because the class loader
coleenp@4037 480 // data graph could be changing while we are walking it (new entries added,
coleenp@4037 481 // new entries being unloaded, etc).
coleenp@4037 482 if (DumpSharedSpaces) {
coleenp@4037 483 // There are only two metaspaces to worry about.
coleenp@4037 484 ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data();
coleenp@4037 485 return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x));
coleenp@4037 486 }
coleenp@4037 487
coleenp@4037 488 if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) {
coleenp@4037 489 return true;
coleenp@4037 490 }
coleenp@4037 491
coleenp@4037 492 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 493 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
coleenp@4037 494 return true;
coleenp@4037 495 }
coleenp@4037 496 }
coleenp@4037 497
coleenp@4037 498 // Could also be on an unloading list which is okay, ie. still allocated
coleenp@4037 499 // for a little while.
coleenp@4037 500 for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) {
coleenp@4037 501 if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) {
coleenp@4037 502 return true;
coleenp@4037 503 }
coleenp@4037 504 }
coleenp@4037 505 return false;
coleenp@4037 506 }
coleenp@4037 507
coleenp@4037 508 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
coleenp@4037 509 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 510 if (loader_data == data) {
coleenp@4037 511 return true;
coleenp@4037 512 }
coleenp@4037 513 }
coleenp@4037 514
coleenp@4037 515 return false;
coleenp@4037 516 }
coleenp@4037 517 #endif // PRODUCT
coleenp@4037 518
coleenp@4037 519 // Move class loader data from main list to the unloaded list for unloading
coleenp@4037 520 // and deallocation later.
coleenp@4037 521 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive) {
coleenp@4037 522 ClassLoaderData* data = _head;
coleenp@4037 523 ClassLoaderData* prev = NULL;
coleenp@4037 524 bool seen_dead_loader = false;
coleenp@4037 525 // mark metadata seen on the stack and code cache so we can delete
coleenp@4037 526 // unneeded entries.
coleenp@4037 527 bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
coleenp@4037 528 MetadataOnStackMark md_on_stack;
coleenp@4037 529 while (data != NULL) {
coleenp@4037 530 if (data->class_loader() == NULL || is_alive->do_object_b(data->class_loader())) {
coleenp@4037 531 assert(data->claimed(), "class loader data must have been claimed");
coleenp@4037 532 if (has_redefined_a_class) {
coleenp@4037 533 data->classes_do(InstanceKlass::purge_previous_versions);
coleenp@4037 534 }
coleenp@4037 535 data->free_deallocate_list();
coleenp@4037 536 prev = data;
coleenp@4037 537 data = data->next();
coleenp@4037 538 continue;
coleenp@4037 539 }
coleenp@4037 540 seen_dead_loader = true;
coleenp@4037 541 ClassLoaderData* dead = data;
coleenp@4037 542 dead->mark_for_unload();
coleenp@4037 543 if (TraceClassLoaderData) {
coleenp@4037 544 tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, dead);
coleenp@4037 545 tty->print(" for instance "PTR_FORMAT" of ", dead->class_loader());
coleenp@4037 546 dead->class_loader()->klass()->name()->print_symbol_on(tty);
coleenp@4037 547 tty->print_cr("]");
coleenp@4037 548 }
coleenp@4037 549 data = data->next();
coleenp@4037 550 // Remove from loader list.
coleenp@4037 551 if (prev != NULL) {
coleenp@4037 552 prev->set_next(data);
coleenp@4037 553 } else {
coleenp@4037 554 assert(dead == _head, "sanity check");
coleenp@4037 555 _head = data;
coleenp@4037 556 }
coleenp@4037 557 dead->set_next(_unloading);
coleenp@4037 558 _unloading = dead;
coleenp@4037 559 }
coleenp@4037 560 return seen_dead_loader;
coleenp@4037 561 }
coleenp@4037 562
coleenp@4037 563 void ClassLoaderDataGraph::purge() {
coleenp@4037 564 ClassLoaderData* list = _unloading;
coleenp@4037 565 _unloading = NULL;
coleenp@4037 566 ClassLoaderData* next = list;
coleenp@4037 567 while (next != NULL) {
coleenp@4037 568 ClassLoaderData* purge_me = next;
coleenp@4037 569 next = purge_me->next();
coleenp@4037 570 delete purge_me;
coleenp@4037 571 }
coleenp@4037 572 }
coleenp@4037 573
coleenp@4037 574 // CDS support
coleenp@4037 575
coleenp@4037 576 // Global metaspaces for writing information to the shared archive. When
coleenp@4037 577 // application CDS is supported, we may need one per metaspace, so this
coleenp@4037 578 // sort of looks like it.
coleenp@4037 579 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
coleenp@4037 580 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
coleenp@4037 581 static bool _shared_metaspaces_initialized = false;
coleenp@4037 582
coleenp@4037 583 // Initialize shared metaspaces (change to call from somewhere not lazily)
coleenp@4037 584 void ClassLoaderData::initialize_shared_metaspaces() {
coleenp@4037 585 assert(DumpSharedSpaces, "only use this for dumping shared spaces");
coleenp@4037 586 assert(this == ClassLoaderData::the_null_class_loader_data(),
coleenp@4037 587 "only supported for null loader data for now");
coleenp@4037 588 assert (!_shared_metaspaces_initialized, "only initialize once");
coleenp@4037 589 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 590 _ro_metaspace = new Metaspace(_metaspace_lock, SharedReadOnlySize/wordSize);
coleenp@4037 591 _rw_metaspace = new Metaspace(_metaspace_lock, SharedReadWriteSize/wordSize);
coleenp@4037 592 _shared_metaspaces_initialized = true;
coleenp@4037 593 }
coleenp@4037 594
coleenp@4037 595 Metaspace* ClassLoaderData::ro_metaspace() {
coleenp@4037 596 assert(_ro_metaspace != NULL, "should already be initialized");
coleenp@4037 597 return _ro_metaspace;
coleenp@4037 598 }
coleenp@4037 599
coleenp@4037 600 Metaspace* ClassLoaderData::rw_metaspace() {
coleenp@4037 601 assert(_rw_metaspace != NULL, "should already be initialized");
coleenp@4037 602 return _rw_metaspace;
coleenp@4037 603 }
coleenp@4037 604
coleenp@4037 605
coleenp@4037 606 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
coleenp@4037 607 _data = ClassLoaderDataGraph::_head;
coleenp@4037 608 }
coleenp@4037 609
coleenp@4037 610 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
coleenp@4037 611
coleenp@4037 612 #ifndef PRODUCT
coleenp@4037 613 // callable from debugger
coleenp@4037 614 extern "C" int print_loader_data_graph() {
coleenp@4037 615 ClassLoaderDataGraph::dump_on(tty);
coleenp@4037 616 return 0;
coleenp@4037 617 }
coleenp@4037 618
coleenp@4037 619 void ClassLoaderDataGraph::verify() {
coleenp@4037 620 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 621 data->verify();
coleenp@4037 622 }
coleenp@4037 623 }
coleenp@4037 624
coleenp@4037 625 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
coleenp@4037 626 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 627 data->dump(out);
coleenp@4037 628 }
coleenp@4037 629 MetaspaceAux::dump(out);
coleenp@4037 630 }
coleenp@4037 631
coleenp@4037 632 void ClassLoaderData::print_value_on(outputStream* out) const {
coleenp@4037 633 if (class_loader() == NULL) {
coleenp@4037 634 out->print_cr("NULL class_loader");
coleenp@4037 635 } else {
coleenp@4037 636 out->print("class loader "PTR_FORMAT, this);
coleenp@4037 637 class_loader()->print_value_on(out);
coleenp@4037 638 }
coleenp@4037 639 }
coleenp@4037 640 #endif // PRODUCT

mercurial