src/share/vm/classfile/classLoaderData.cpp

Mon, 27 May 2013 12:56:34 +0200

author
stefank
date
Mon, 27 May 2013 12:56:34 +0200
changeset 5195
95c00927be11
parent 5025
d58c62b7447d
child 5237
f2110083203d
permissions
-rw-r--r--

8015428: Remove unused CDS support from StringTable
Summary: The string in StringTable is not used by CDS anymore. Remove the unnecessary code in preparation for 8015422: Large performance hit when the StringTable is walked twice in Parallel Scavenge
Reviewed-by: pliden, tschatzl, coleenp

coleenp@4037 1 /*
coleenp@4490 2 * Copyright (c) 2012, 2013, 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@4490 53 #include "classfile/metadataOnStackMark.hpp"
coleenp@4037 54 #include "classfile/systemDictionary.hpp"
coleenp@4037 55 #include "code/codeCache.hpp"
mgerdin@5013 56 #include "memory/gcLocker.hpp"
coleenp@4037 57 #include "memory/metadataFactory.hpp"
coleenp@4037 58 #include "memory/metaspaceShared.hpp"
coleenp@4490 59 #include "memory/oopFactory.hpp"
coleenp@4037 60 #include "runtime/jniHandles.hpp"
coleenp@4037 61 #include "runtime/mutex.hpp"
coleenp@4037 62 #include "runtime/safepoint.hpp"
coleenp@4037 63 #include "runtime/synchronizer.hpp"
coleenp@4037 64 #include "utilities/growableArray.hpp"
coleenp@4037 65 #include "utilities/ostream.hpp"
coleenp@4037 66
coleenp@4037 67 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
coleenp@4037 68
mgerdin@5016 69 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
coleenp@4345 70 _class_loader(h_class_loader()),
coleenp@4345 71 _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
coleenp@4345 72 _metaspace(NULL), _unloading(false), _klasses(NULL),
coleenp@4304 73 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
mgerdin@5016 74 _next(NULL), _dependencies(dependencies),
coleenp@4037 75 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
coleenp@4037 76 // empty
coleenp@4037 77 }
coleenp@4037 78
coleenp@4304 79 void ClassLoaderData::init_dependencies(TRAPS) {
mgerdin@5016 80 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
mgerdin@5016 81 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
mgerdin@4903 82 _dependencies.init(CHECK);
mgerdin@4903 83 }
mgerdin@4903 84
mgerdin@4903 85 void ClassLoaderData::Dependencies::init(TRAPS) {
coleenp@4304 86 // Create empty dependencies array to add to. CMS requires this to be
coleenp@4304 87 // an oop so that it can track additions via card marks. We think.
mgerdin@4903 88 _list_head = oopFactory::new_objectArray(2, CHECK);
coleenp@4304 89 }
coleenp@4304 90
coleenp@4037 91 bool ClassLoaderData::claim() {
coleenp@4037 92 if (_claimed == 1) {
coleenp@4037 93 return false;
coleenp@4037 94 }
coleenp@4037 95
coleenp@4037 96 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
coleenp@4037 97 }
coleenp@4037 98
coleenp@4037 99 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 100 if (must_claim && !claim()) {
coleenp@4037 101 return;
coleenp@4037 102 }
coleenp@4037 103
coleenp@4037 104 f->do_oop(&_class_loader);
mgerdin@4903 105 _dependencies.oops_do(f);
coleenp@4037 106 _handles->oops_do(f);
coleenp@4037 107 if (klass_closure != NULL) {
coleenp@4037 108 classes_do(klass_closure);
coleenp@4037 109 }
coleenp@4037 110 }
coleenp@4037 111
mgerdin@4903 112 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
mgerdin@4903 113 f->do_oop((oop*)&_list_head);
mgerdin@4903 114 }
mgerdin@4903 115
coleenp@4037 116 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
coleenp@4037 117 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 118 klass_closure->do_klass(k);
coleenp@4751 119 assert(k != k->next_link(), "no loops!");
coleenp@4037 120 }
coleenp@4037 121 }
coleenp@4037 122
coleenp@4037 123 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
coleenp@4037 124 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 125 if (k->oop_is_instance()) {
coleenp@4037 126 f(InstanceKlass::cast(k));
coleenp@4037 127 }
coleenp@4751 128 assert(k != k->next_link(), "no loops!");
coleenp@4037 129 }
coleenp@4037 130 }
coleenp@4037 131
coleenp@4037 132 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
coleenp@4037 133 ClassLoaderData * const from_cld = this;
coleenp@4037 134 ClassLoaderData * const to_cld = k->class_loader_data();
coleenp@4037 135
coleenp@4304 136 // Dependency to the null class loader data doesn't need to be recorded
coleenp@4304 137 // because the null class loader data never goes away.
coleenp@4304 138 if (to_cld->is_the_null_class_loader_data()) {
coleenp@4037 139 return;
coleenp@4037 140 }
coleenp@4037 141
coleenp@4304 142 oop to;
coleenp@4304 143 if (to_cld->is_anonymous()) {
coleenp@4304 144 // Anonymous class dependencies are through the mirror.
coleenp@4304 145 to = k->java_mirror();
coleenp@4304 146 } else {
coleenp@4304 147 to = to_cld->class_loader();
coleenp@4037 148
coleenp@4304 149 // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
coleenp@4304 150 // we still have to add it. The class_loader won't keep from_cld alive.
coleenp@4304 151 if (!from_cld->is_anonymous()) {
coleenp@4304 152 // Check that this dependency isn't from the same or parent class_loader
coleenp@4304 153 oop from = from_cld->class_loader();
coleenp@4304 154
coleenp@4304 155 oop curr = from;
coleenp@4304 156 while (curr != NULL) {
coleenp@4304 157 if (curr == to) {
coleenp@4304 158 return; // this class loader is in the parent list, no need to add it.
coleenp@4304 159 }
coleenp@4304 160 curr = java_lang_ClassLoader::parent(curr);
coleenp@4304 161 }
coleenp@4037 162 }
coleenp@4037 163 }
coleenp@4037 164
coleenp@4037 165 // It's a dependency we won't find through GC, add it. This is relatively rare
coleenp@4304 166 // Must handle over GC point.
coleenp@4304 167 Handle dependency(THREAD, to);
mgerdin@4903 168 from_cld->_dependencies.add(dependency, CHECK);
coleenp@4037 169 }
coleenp@4037 170
coleenp@4037 171
mgerdin@4903 172 void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {
coleenp@4304 173 // Check first if this dependency is already in the list.
coleenp@4304 174 // Save a pointer to the last to add to under the lock.
mgerdin@4903 175 objArrayOop ok = _list_head;
coleenp@4304 176 objArrayOop last = NULL;
coleenp@4037 177 while (ok != NULL) {
coleenp@4304 178 last = ok;
coleenp@4304 179 if (ok->obj_at(0) == dependency()) {
coleenp@4304 180 // Don't need to add it
coleenp@4304 181 return;
coleenp@4037 182 }
coleenp@4037 183 ok = (objArrayOop)ok->obj_at(1);
coleenp@4037 184 }
coleenp@4304 185
stefank@4353 186 // Must handle over GC points
stefank@4353 187 assert (last != NULL, "dependencies should be initialized");
stefank@4353 188 objArrayHandle last_handle(THREAD, last);
stefank@4353 189
coleenp@4304 190 // Create a new dependency node with fields for (class_loader or mirror, next)
coleenp@4304 191 objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
coleenp@4304 192 deps->obj_at_put(0, dependency());
coleenp@4304 193
stefank@4353 194 // Must handle over GC points
coleenp@4304 195 objArrayHandle new_dependency(THREAD, deps);
coleenp@4304 196
coleenp@4304 197 // Add the dependency under lock
mgerdin@4903 198 locked_add(last_handle, new_dependency, THREAD);
coleenp@4037 199 }
coleenp@4037 200
mgerdin@4903 201 void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,
mgerdin@4903 202 objArrayHandle new_dependency,
mgerdin@4903 203 Thread* THREAD) {
coleenp@4304 204
coleenp@4304 205 // Have to lock and put the new dependency on the end of the dependency
coleenp@4304 206 // array so the card mark for CMS sees that this dependency is new.
coleenp@4304 207 // Can probably do this lock free with some effort.
mgerdin@4903 208 ObjectLocker ol(Handle(THREAD, _list_head), THREAD);
coleenp@4304 209
coleenp@4304 210 oop loader_or_mirror = new_dependency->obj_at(0);
coleenp@4304 211
coleenp@4304 212 // Since the dependencies are only added, add to the end.
coleenp@4304 213 objArrayOop end = last_handle();
coleenp@4304 214 objArrayOop last = NULL;
coleenp@4304 215 while (end != NULL) {
coleenp@4304 216 last = end;
coleenp@4304 217 // check again if another thread added it to the end.
coleenp@4304 218 if (end->obj_at(0) == loader_or_mirror) {
coleenp@4304 219 // Don't need to add it
coleenp@4304 220 return;
coleenp@4304 221 }
coleenp@4304 222 end = (objArrayOop)end->obj_at(1);
coleenp@4037 223 }
coleenp@4304 224 assert (last != NULL, "dependencies should be initialized");
coleenp@4304 225 // fill in the first element with the oop in new_dependency.
coleenp@4304 226 if (last->obj_at(0) == NULL) {
coleenp@4304 227 last->obj_at_put(0, new_dependency->obj_at(0));
coleenp@4304 228 } else {
coleenp@4304 229 last->obj_at_put(1, new_dependency());
coleenp@4037 230 }
coleenp@4037 231 }
coleenp@4037 232
coleenp@4037 233 void ClassLoaderDataGraph::clear_claimed_marks() {
coleenp@4037 234 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 235 cld->clear_claimed();
coleenp@4037 236 }
coleenp@4037 237 }
coleenp@4037 238
coleenp@4037 239 void ClassLoaderData::add_class(Klass* k) {
coleenp@4037 240 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 241 Klass* old_value = _klasses;
coleenp@4037 242 k->set_next_link(old_value);
coleenp@4037 243 // link the new item into the list
coleenp@4037 244 _klasses = k;
coleenp@4037 245
coleenp@4304 246 if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
coleenp@4037 247 ResourceMark rm;
coleenp@4037 248 tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
coleenp@4037 249 PTR_FORMAT " loader: " PTR_FORMAT " %s",
coleenp@4037 250 k,
coleenp@4037 251 k->external_name(),
coleenp@4037 252 k->class_loader_data(),
coleenp@4037 253 k->class_loader(),
coleenp@4304 254 loader_name());
coleenp@4037 255 }
coleenp@4037 256 }
coleenp@4037 257
coleenp@4037 258 // This is called by InstanceKlass::deallocate_contents() to remove the
coleenp@4037 259 // scratch_class for redefine classes. We need a lock because there it may not
coleenp@4037 260 // be called at a safepoint if there's an error.
coleenp@4037 261 void ClassLoaderData::remove_class(Klass* scratch_class) {
coleenp@4037 262 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 263 Klass* prev = NULL;
coleenp@4037 264 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 265 if (k == scratch_class) {
coleenp@4037 266 if (prev == NULL) {
coleenp@4037 267 _klasses = k->next_link();
coleenp@4037 268 } else {
coleenp@4037 269 Klass* next = k->next_link();
coleenp@4037 270 prev->set_next_link(next);
coleenp@4037 271 }
coleenp@4037 272 return;
coleenp@4037 273 }
coleenp@4037 274 prev = k;
coleenp@4751 275 assert(k != k->next_link(), "no loops!");
coleenp@4037 276 }
coleenp@4037 277 ShouldNotReachHere(); // should have found this class!!
coleenp@4037 278 }
coleenp@4037 279
coleenp@4304 280 void ClassLoaderData::unload() {
coleenp@4304 281 _unloading = true;
coleenp@4304 282
coleenp@4981 283 // Tell serviceability tools these classes are unloading
coleenp@4981 284 classes_do(InstanceKlass::notify_unload_class);
coleenp@4981 285
coleenp@4304 286 if (TraceClassLoaderData) {
coleenp@4304 287 ResourceMark rm;
coleenp@4304 288 tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, this);
coleenp@4304 289 tty->print(" for instance "PTR_FORMAT" of %s", class_loader(),
coleenp@4304 290 loader_name());
coleenp@4304 291 if (is_anonymous()) {
coleenp@4304 292 tty->print(" for anonymous class "PTR_FORMAT " ", _klasses);
coleenp@4304 293 }
coleenp@4304 294 tty->print_cr("]");
coleenp@4304 295 }
coleenp@4304 296 }
coleenp@4304 297
coleenp@4304 298 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
coleenp@4304 299 bool alive =
coleenp@4304 300 is_anonymous() ?
coleenp@4304 301 is_alive_closure->do_object_b(_klasses->java_mirror()) :
coleenp@4304 302 class_loader() == NULL || is_alive_closure->do_object_b(class_loader());
coleenp@4304 303 assert(!alive || claimed(), "must be claimed");
coleenp@4304 304 return alive;
coleenp@4304 305 }
coleenp@4304 306
coleenp@4304 307
coleenp@4037 308 ClassLoaderData::~ClassLoaderData() {
coleenp@4981 309 // Release C heap structures for all the classes.
coleenp@4981 310 classes_do(InstanceKlass::release_C_heap_structures);
coleenp@4981 311
coleenp@4037 312 Metaspace *m = _metaspace;
coleenp@4037 313 if (m != NULL) {
coleenp@4037 314 _metaspace = NULL;
coleenp@4037 315 // release the metaspace
coleenp@4037 316 delete m;
coleenp@4037 317 // release the handles
coleenp@4037 318 if (_handles != NULL) {
coleenp@4037 319 JNIHandleBlock::release_block(_handles);
coleenp@4037 320 _handles = NULL;
coleenp@4037 321 }
coleenp@4037 322 }
coleenp@4037 323
coleenp@4037 324 // Clear all the JNI handles for methods
coleenp@4037 325 // These aren't deallocated and are going to look like a leak, but that's
coleenp@4037 326 // needed because we can't really get rid of jmethodIDs because we don't
coleenp@4037 327 // know when native code is going to stop using them. The spec says that
coleenp@4037 328 // they're "invalid" but existing programs likely rely on their being
coleenp@4037 329 // NULL after class unloading.
coleenp@4037 330 if (_jmethod_ids != NULL) {
coleenp@4037 331 Method::clear_jmethod_ids(this);
coleenp@4037 332 }
coleenp@4037 333 // Delete lock
coleenp@4037 334 delete _metaspace_lock;
coleenp@4037 335
coleenp@4037 336 // Delete free list
coleenp@4037 337 if (_deallocate_list != NULL) {
coleenp@4037 338 delete _deallocate_list;
coleenp@4037 339 }
coleenp@4037 340 }
coleenp@4037 341
twisti@4866 342 /**
twisti@4866 343 * Returns true if this class loader data is for the extension class loader.
twisti@4866 344 */
twisti@4866 345 bool ClassLoaderData::is_ext_class_loader_data() const {
twisti@4866 346 return SystemDictionary::is_ext_class_loader(class_loader());
twisti@4866 347 }
twisti@4866 348
coleenp@4037 349 Metaspace* ClassLoaderData::metaspace_non_null() {
jmasa@4457 350 assert(!DumpSharedSpaces, "wrong metaspace!");
coleenp@4037 351 // If the metaspace has not been allocated, create a new one. Might want
coleenp@4037 352 // to create smaller arena for Reflection class loaders also.
coleenp@4037 353 // The reason for the delayed allocation is because some class loaders are
coleenp@4037 354 // simply for delegating with no metadata of their own.
coleenp@4037 355 if (_metaspace == NULL) {
coleenp@4037 356 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 357 // Check again if metaspace has been allocated while we were getting this lock.
coleenp@4037 358 if (_metaspace != NULL) {
coleenp@4037 359 return _metaspace;
coleenp@4037 360 }
coleenp@4304 361 if (this == the_null_class_loader_data()) {
coleenp@4304 362 assert (class_loader() == NULL, "Must be");
jmasa@4382 363 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
jmasa@4382 364 } else if (is_anonymous()) {
jmasa@4382 365 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
jmasa@4382 366 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
jmasa@4382 367 }
jmasa@4382 368 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
jmasa@4382 369 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
jmasa@4382 370 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
jmasa@4382 371 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
jmasa@4382 372 }
jmasa@4382 373 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
coleenp@4037 374 } else {
jmasa@4382 375 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
coleenp@4037 376 }
coleenp@4037 377 }
coleenp@4037 378 return _metaspace;
coleenp@4037 379 }
coleenp@4037 380
coleenp@4037 381 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
coleenp@4037 382 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
coleenp@4037 383
coleenp@4037 384 jobject ClassLoaderData::add_handle(Handle h) {
coleenp@4037 385 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 386 if (handles() == NULL) {
coleenp@4037 387 set_handles(JNIHandleBlock::allocate_block());
coleenp@4037 388 }
coleenp@4037 389 return handles()->allocate_handle(h());
coleenp@4037 390 }
coleenp@4037 391
coleenp@4037 392 // Add this metadata pointer to be freed when it's safe. This is only during
coleenp@4037 393 // class unloading because Handles might point to this metadata field.
coleenp@4037 394 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
coleenp@4037 395 // Metadata in shared region isn't deleted.
coleenp@4037 396 if (!m->is_shared()) {
coleenp@4037 397 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 398 if (_deallocate_list == NULL) {
coleenp@4037 399 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
coleenp@4037 400 }
coleenp@4037 401 _deallocate_list->append_if_missing(m);
coleenp@4037 402 }
coleenp@4037 403 }
coleenp@4037 404
coleenp@4037 405 // Deallocate free metadata on the free list. How useful the PermGen was!
coleenp@4037 406 void ClassLoaderData::free_deallocate_list() {
coleenp@4037 407 // Don't need lock, at safepoint
coleenp@4037 408 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
coleenp@4037 409 if (_deallocate_list == NULL) {
coleenp@4037 410 return;
coleenp@4037 411 }
coleenp@4037 412 // Go backwards because this removes entries that are freed.
coleenp@4037 413 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
coleenp@4037 414 Metadata* m = _deallocate_list->at(i);
coleenp@4037 415 if (!m->on_stack()) {
coleenp@4037 416 _deallocate_list->remove_at(i);
coleenp@4037 417 // There are only three types of metadata that we deallocate directly.
coleenp@4037 418 // Cast them so they can be used by the template function.
coleenp@4037 419 if (m->is_method()) {
coleenp@4037 420 MetadataFactory::free_metadata(this, (Method*)m);
coleenp@4037 421 } else if (m->is_constantPool()) {
coleenp@4037 422 MetadataFactory::free_metadata(this, (ConstantPool*)m);
coleenp@4037 423 } else if (m->is_klass()) {
coleenp@4037 424 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
coleenp@4037 425 } else {
coleenp@4037 426 ShouldNotReachHere();
coleenp@4037 427 }
coleenp@4037 428 }
coleenp@4037 429 }
coleenp@4037 430 }
coleenp@4037 431
coleenp@4304 432 // These anonymous class loaders are to contain classes used for JSR292
coleenp@4304 433 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
coleenp@4304 434 // Add a new class loader data to the graph.
mgerdin@5013 435 return ClassLoaderDataGraph::add(loader, true, CHECK_NULL);
coleenp@4037 436 }
coleenp@4037 437
coleenp@4304 438 const char* ClassLoaderData::loader_name() {
coleenp@4304 439 // Handles null class loader
coleenp@4304 440 return SystemDictionary::loader_name(class_loader());
coleenp@4304 441 }
coleenp@4304 442
coleenp@4304 443 #ifndef PRODUCT
coleenp@4037 444 // Define to dump klasses
coleenp@4037 445 #undef CLD_DUMP_KLASSES
coleenp@4037 446
coleenp@4037 447 void ClassLoaderData::dump(outputStream * const out) {
coleenp@4037 448 ResourceMark rm;
coleenp@4037 449 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
coleenp@4037 450 this, class_loader(),
coleenp@4304 451 class_loader() != NULL ? class_loader()->klass() : NULL, loader_name());
coleenp@4037 452 if (claimed()) out->print(" claimed ");
coleenp@4037 453 if (is_unloading()) out->print(" unloading ");
coleenp@4037 454 out->print(" handles " INTPTR_FORMAT, handles());
coleenp@4037 455 out->cr();
coleenp@4037 456 if (metaspace_or_null() != NULL) {
coleenp@4037 457 out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null());
coleenp@4037 458 metaspace_or_null()->dump(out);
coleenp@4037 459 } else {
coleenp@4037 460 out->print_cr("metaspace: NULL");
coleenp@4037 461 }
coleenp@4037 462
coleenp@4037 463 #ifdef CLD_DUMP_KLASSES
coleenp@4037 464 if (Verbose) {
coleenp@4037 465 ResourceMark rm;
coleenp@4037 466 Klass* k = _klasses;
coleenp@4037 467 while (k != NULL) {
coleenp@4037 468 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
coleenp@4037 469 k->has_modified_oops(), k->has_accumulated_modified_oops());
coleenp@4751 470 assert(k != k->next_link(), "no loops!");
coleenp@4037 471 k = k->next_link();
coleenp@4037 472 }
coleenp@4037 473 }
coleenp@4037 474 #endif // CLD_DUMP_KLASSES
coleenp@4037 475 #undef CLD_DUMP_KLASSES
coleenp@4037 476 if (_jmethod_ids != NULL) {
coleenp@4037 477 Method::print_jmethod_ids(this, out);
coleenp@4037 478 }
coleenp@4037 479 out->print_cr("}");
coleenp@4037 480 }
coleenp@4037 481 #endif // PRODUCT
coleenp@4037 482
coleenp@4037 483 void ClassLoaderData::verify() {
coleenp@4037 484 oop cl = class_loader();
coleenp@4037 485
coleenp@4304 486 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
coleenp@4304 487 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
coleenp@4037 488
coleenp@4037 489 // Verify the integrity of the allocated space.
coleenp@4037 490 if (metaspace_or_null() != NULL) {
coleenp@4037 491 metaspace_or_null()->verify();
coleenp@4037 492 }
coleenp@4037 493
coleenp@4037 494 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 495 guarantee(k->class_loader_data() == this, "Must be the same");
coleenp@4037 496 k->verify();
coleenp@4751 497 assert(k != k->next_link(), "no loops!");
coleenp@4037 498 }
coleenp@4037 499 }
coleenp@4037 500
coleenp@4304 501
coleenp@4037 502 // GC root of class loader data created.
coleenp@4037 503 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
coleenp@4037 504 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
coleenp@4037 505 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
coleenp@4037 506
coleenp@4037 507 // Add a new class loader data node to the list. Assign the newly created
coleenp@4037 508 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
mgerdin@5013 509 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
mgerdin@5016 510 // We need to allocate all the oops for the ClassLoaderData before allocating the
mgerdin@5016 511 // actual ClassLoaderData object.
mgerdin@5016 512 ClassLoaderData::Dependencies dependencies(CHECK_NULL);
coleenp@4345 513
mgerdin@5016 514 No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
mgerdin@5016 515 // ClassLoaderData in the graph since the CLD
mgerdin@5016 516 // contains unhandled oops
coleenp@4037 517
mgerdin@5016 518 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
mgerdin@5016 519
mgerdin@4903 520
mgerdin@5013 521 if (!is_anonymous) {
mgerdin@5013 522 ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
coleenp@4304 523 // First, Atomically set it
coleenp@4304 524 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
coleenp@4304 525 if (old != NULL) {
coleenp@4304 526 delete cld;
coleenp@4304 527 // Returns the data.
coleenp@4304 528 return old;
coleenp@4304 529 }
coleenp@4037 530 }
coleenp@4037 531
coleenp@4037 532 // We won the race, and therefore the task of adding the data to the list of
coleenp@4037 533 // class loader data
mgerdin@5013 534 ClassLoaderData** list_head = &_head;
mgerdin@5013 535 ClassLoaderData* next = _head;
mgerdin@5013 536
coleenp@4037 537 do {
coleenp@4037 538 cld->set_next(next);
coleenp@4037 539 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
coleenp@4037 540 if (exchanged == next) {
coleenp@4037 541 if (TraceClassLoaderData) {
coleenp@4304 542 ResourceMark rm;
coleenp@4037 543 tty->print("[ClassLoaderData: ");
coleenp@4037 544 tty->print("create class loader data "PTR_FORMAT, cld);
coleenp@4304 545 tty->print(" for instance "PTR_FORMAT" of %s", cld->class_loader(),
coleenp@4304 546 cld->loader_name());
coleenp@4037 547 tty->print_cr("]");
coleenp@4037 548 }
coleenp@4037 549 return cld;
coleenp@4037 550 }
coleenp@4037 551 next = exchanged;
coleenp@4037 552 } while (true);
coleenp@4304 553
coleenp@4037 554 }
coleenp@4037 555
coleenp@4037 556 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 557 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 558 cld->oops_do(f, klass_closure, must_claim);
coleenp@4037 559 }
coleenp@4037 560 }
coleenp@4037 561
coleenp@4304 562 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4304 563 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4304 564 if (cld->keep_alive()) {
coleenp@4304 565 cld->oops_do(f, klass_closure, must_claim);
coleenp@4304 566 }
coleenp@4304 567 }
coleenp@4304 568 }
coleenp@4304 569
coleenp@4037 570 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 571 if (ClassUnloading) {
coleenp@4037 572 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
coleenp@4304 573 // keep any special CLDs alive.
coleenp@4304 574 ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
coleenp@4037 575 } else {
coleenp@4037 576 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
coleenp@4037 577 }
coleenp@4037 578 }
coleenp@4037 579
coleenp@4037 580 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
coleenp@4037 581 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 582 cld->classes_do(klass_closure);
coleenp@4037 583 }
coleenp@4037 584 }
coleenp@4037 585
coleenp@4037 586 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
coleenp@4037 587 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
coleenp@4037 588
coleenp@4037 589 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
coleenp@4037 590
coleenp@4037 591 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
coleenp@4037 592 ClassLoaderData* curr = _head;
coleenp@4037 593 while (curr != _saved_head) {
coleenp@4037 594 if (!curr->claimed()) {
coleenp@4037 595 array->push(curr);
coleenp@4037 596
coleenp@4037 597 if (TraceClassLoaderData) {
coleenp@4037 598 tty->print("[ClassLoaderData] found new CLD: ");
coleenp@4037 599 curr->print_value_on(tty);
coleenp@4037 600 tty->cr();
coleenp@4037 601 }
coleenp@4037 602 }
coleenp@4037 603
coleenp@4037 604 curr = curr->_next;
coleenp@4037 605 }
coleenp@4037 606
coleenp@4037 607 return array;
coleenp@4037 608 }
coleenp@4037 609
coleenp@4037 610 #ifndef PRODUCT
coleenp@4037 611 // for debugging and hsfind(x)
coleenp@4037 612 bool ClassLoaderDataGraph::contains(address x) {
coleenp@4037 613 // I think we need the _metaspace_lock taken here because the class loader
coleenp@4037 614 // data graph could be changing while we are walking it (new entries added,
coleenp@4037 615 // new entries being unloaded, etc).
coleenp@4037 616 if (DumpSharedSpaces) {
coleenp@4037 617 // There are only two metaspaces to worry about.
coleenp@4037 618 ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data();
coleenp@4037 619 return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x));
coleenp@4037 620 }
coleenp@4037 621
coleenp@4037 622 if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) {
coleenp@4037 623 return true;
coleenp@4037 624 }
coleenp@4037 625
coleenp@4037 626 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 627 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
coleenp@4037 628 return true;
coleenp@4037 629 }
coleenp@4037 630 }
coleenp@4037 631
coleenp@4037 632 // Could also be on an unloading list which is okay, ie. still allocated
coleenp@4037 633 // for a little while.
coleenp@4037 634 for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) {
coleenp@4037 635 if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) {
coleenp@4037 636 return true;
coleenp@4037 637 }
coleenp@4037 638 }
coleenp@4037 639 return false;
coleenp@4037 640 }
coleenp@4037 641
coleenp@4037 642 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
coleenp@4037 643 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 644 if (loader_data == data) {
coleenp@4037 645 return true;
coleenp@4037 646 }
coleenp@4037 647 }
coleenp@4037 648
coleenp@4037 649 return false;
coleenp@4037 650 }
coleenp@4037 651 #endif // PRODUCT
coleenp@4037 652
coleenp@4304 653
coleenp@4037 654 // Move class loader data from main list to the unloaded list for unloading
coleenp@4037 655 // and deallocation later.
coleenp@4304 656 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) {
coleenp@4037 657 ClassLoaderData* data = _head;
coleenp@4037 658 ClassLoaderData* prev = NULL;
coleenp@4037 659 bool seen_dead_loader = false;
coleenp@4037 660 // mark metadata seen on the stack and code cache so we can delete
coleenp@4037 661 // unneeded entries.
coleenp@4037 662 bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
coleenp@4037 663 MetadataOnStackMark md_on_stack;
coleenp@4037 664 while (data != NULL) {
coleenp@4304 665 if (data->keep_alive() || data->is_alive(is_alive_closure)) {
coleenp@4037 666 if (has_redefined_a_class) {
coleenp@4037 667 data->classes_do(InstanceKlass::purge_previous_versions);
coleenp@4037 668 }
coleenp@4037 669 data->free_deallocate_list();
coleenp@4037 670 prev = data;
coleenp@4037 671 data = data->next();
coleenp@4037 672 continue;
coleenp@4037 673 }
coleenp@4037 674 seen_dead_loader = true;
coleenp@4037 675 ClassLoaderData* dead = data;
coleenp@4304 676 dead->unload();
coleenp@4037 677 data = data->next();
coleenp@4037 678 // Remove from loader list.
jmasa@5015 679 // This class loader data will no longer be found
jmasa@5015 680 // in the ClassLoaderDataGraph.
coleenp@4037 681 if (prev != NULL) {
coleenp@4037 682 prev->set_next(data);
coleenp@4037 683 } else {
coleenp@4037 684 assert(dead == _head, "sanity check");
coleenp@4037 685 _head = data;
coleenp@4037 686 }
coleenp@4037 687 dead->set_next(_unloading);
coleenp@4037 688 _unloading = dead;
coleenp@4037 689 }
coleenp@4037 690 return seen_dead_loader;
coleenp@4037 691 }
coleenp@4037 692
coleenp@4037 693 void ClassLoaderDataGraph::purge() {
coleenp@4037 694 ClassLoaderData* list = _unloading;
coleenp@4037 695 _unloading = NULL;
coleenp@4037 696 ClassLoaderData* next = list;
coleenp@4037 697 while (next != NULL) {
coleenp@4037 698 ClassLoaderData* purge_me = next;
coleenp@4037 699 next = purge_me->next();
coleenp@4037 700 delete purge_me;
coleenp@4037 701 }
jmasa@5007 702 Metaspace::purge();
coleenp@4037 703 }
coleenp@4037 704
coleenp@4037 705 // CDS support
coleenp@4037 706
coleenp@4037 707 // Global metaspaces for writing information to the shared archive. When
coleenp@4037 708 // application CDS is supported, we may need one per metaspace, so this
coleenp@4037 709 // sort of looks like it.
coleenp@4037 710 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
coleenp@4037 711 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
coleenp@4037 712 static bool _shared_metaspaces_initialized = false;
coleenp@4037 713
coleenp@4037 714 // Initialize shared metaspaces (change to call from somewhere not lazily)
coleenp@4037 715 void ClassLoaderData::initialize_shared_metaspaces() {
coleenp@4037 716 assert(DumpSharedSpaces, "only use this for dumping shared spaces");
coleenp@4037 717 assert(this == ClassLoaderData::the_null_class_loader_data(),
coleenp@4037 718 "only supported for null loader data for now");
coleenp@4037 719 assert (!_shared_metaspaces_initialized, "only initialize once");
coleenp@4037 720 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
jmasa@4382 721 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
jmasa@4382 722 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
coleenp@4037 723 _shared_metaspaces_initialized = true;
coleenp@4037 724 }
coleenp@4037 725
coleenp@4037 726 Metaspace* ClassLoaderData::ro_metaspace() {
coleenp@4037 727 assert(_ro_metaspace != NULL, "should already be initialized");
coleenp@4037 728 return _ro_metaspace;
coleenp@4037 729 }
coleenp@4037 730
coleenp@4037 731 Metaspace* ClassLoaderData::rw_metaspace() {
coleenp@4037 732 assert(_rw_metaspace != NULL, "should already be initialized");
coleenp@4037 733 return _rw_metaspace;
coleenp@4037 734 }
coleenp@4037 735
coleenp@4037 736
coleenp@4037 737 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
coleenp@4037 738 _data = ClassLoaderDataGraph::_head;
coleenp@4037 739 }
coleenp@4037 740
coleenp@4037 741 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
coleenp@4037 742
coleenp@4037 743 #ifndef PRODUCT
coleenp@4037 744 // callable from debugger
coleenp@4037 745 extern "C" int print_loader_data_graph() {
coleenp@4037 746 ClassLoaderDataGraph::dump_on(tty);
coleenp@4037 747 return 0;
coleenp@4037 748 }
coleenp@4037 749
coleenp@4037 750 void ClassLoaderDataGraph::verify() {
coleenp@4037 751 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 752 data->verify();
coleenp@4037 753 }
coleenp@4037 754 }
coleenp@4037 755
coleenp@4037 756 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
coleenp@4037 757 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 758 data->dump(out);
coleenp@4037 759 }
coleenp@4037 760 MetaspaceAux::dump(out);
coleenp@4037 761 }
acorn@4497 762 #endif // PRODUCT
coleenp@4037 763
coleenp@4037 764 void ClassLoaderData::print_value_on(outputStream* out) const {
coleenp@4037 765 if (class_loader() == NULL) {
acorn@4497 766 out->print("NULL class_loader");
coleenp@4037 767 } else {
coleenp@4037 768 out->print("class loader "PTR_FORMAT, this);
coleenp@4037 769 class_loader()->print_value_on(out);
coleenp@4037 770 }
coleenp@4037 771 }

mercurial