src/share/vm/classfile/classLoaderData.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/classfile/classLoaderData.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,836 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +// A ClassLoaderData identifies the full set of class types that a class
    1.29 +// loader's name resolution strategy produces for a given configuration of the
    1.30 +// class loader.
    1.31 +// Class types in the ClassLoaderData may be defined by from class file binaries
    1.32 +// provided by the class loader, or from other class loader it interacts with
    1.33 +// according to its name resolution strategy.
    1.34 +//
    1.35 +// Class loaders that implement a deterministic name resolution strategy
    1.36 +// (including with respect to their delegation behavior), such as the boot, the
    1.37 +// extension, and the system loaders of the JDK's built-in class loader
    1.38 +// hierarchy, always produce the same linkset for a given configuration.
    1.39 +//
    1.40 +// ClassLoaderData carries information related to a linkset (e.g.,
    1.41 +// metaspace holding its klass definitions).
    1.42 +// The System Dictionary and related data structures (e.g., placeholder table,
    1.43 +// loader constraints table) as well as the runtime representation of classes
    1.44 +// only reference ClassLoaderData.
    1.45 +//
    1.46 +// Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
    1.47 +// that represent the loader's "linking domain" in the JVM.
    1.48 +//
    1.49 +// The bootstrap loader (represented by NULL) also has a ClassLoaderData,
    1.50 +// the singleton class the_null_class_loader_data().
    1.51 +
    1.52 +#include "precompiled.hpp"
    1.53 +#include "classfile/classLoaderData.hpp"
    1.54 +#include "classfile/classLoaderData.inline.hpp"
    1.55 +#include "classfile/javaClasses.hpp"
    1.56 +#include "classfile/metadataOnStackMark.hpp"
    1.57 +#include "classfile/systemDictionary.hpp"
    1.58 +#include "code/codeCache.hpp"
    1.59 +#include "memory/gcLocker.hpp"
    1.60 +#include "memory/metadataFactory.hpp"
    1.61 +#include "memory/metaspaceShared.hpp"
    1.62 +#include "memory/oopFactory.hpp"
    1.63 +#include "runtime/jniHandles.hpp"
    1.64 +#include "runtime/mutex.hpp"
    1.65 +#include "runtime/safepoint.hpp"
    1.66 +#include "runtime/synchronizer.hpp"
    1.67 +#include "utilities/growableArray.hpp"
    1.68 +#include "utilities/macros.hpp"
    1.69 +#include "utilities/ostream.hpp"
    1.70 +
    1.71 +#if INCLUDE_TRACE
    1.72 + #include "trace/tracing.hpp"
    1.73 +#endif
    1.74 +
    1.75 +ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
    1.76 +
    1.77 +ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
    1.78 +  _class_loader(h_class_loader()),
    1.79 +  _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
    1.80 +  _metaspace(NULL), _unloading(false), _klasses(NULL),
    1.81 +  _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
    1.82 +  _next(NULL), _dependencies(dependencies),
    1.83 +  _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
    1.84 +    // empty
    1.85 +}
    1.86 +
    1.87 +void ClassLoaderData::init_dependencies(TRAPS) {
    1.88 +  assert(!Universe::is_fully_initialized(), "should only be called when initializing");
    1.89 +  assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
    1.90 +  _dependencies.init(CHECK);
    1.91 +}
    1.92 +
    1.93 +void ClassLoaderData::Dependencies::init(TRAPS) {
    1.94 +  // Create empty dependencies array to add to. CMS requires this to be
    1.95 +  // an oop so that it can track additions via card marks.  We think.
    1.96 +  _list_head = oopFactory::new_objectArray(2, CHECK);
    1.97 +}
    1.98 +
    1.99 +bool ClassLoaderData::claim() {
   1.100 +  if (_claimed == 1) {
   1.101 +    return false;
   1.102 +  }
   1.103 +
   1.104 +  return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
   1.105 +}
   1.106 +
   1.107 +void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   1.108 +  if (must_claim && !claim()) {
   1.109 +    return;
   1.110 +  }
   1.111 +
   1.112 +  f->do_oop(&_class_loader);
   1.113 +  _dependencies.oops_do(f);
   1.114 +  _handles->oops_do(f);
   1.115 +  if (klass_closure != NULL) {
   1.116 +    classes_do(klass_closure);
   1.117 +  }
   1.118 +}
   1.119 +
   1.120 +void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
   1.121 +  f->do_oop((oop*)&_list_head);
   1.122 +}
   1.123 +
   1.124 +void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
   1.125 +  for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   1.126 +    klass_closure->do_klass(k);
   1.127 +    assert(k != k->next_link(), "no loops!");
   1.128 +  }
   1.129 +}
   1.130 +
   1.131 +void ClassLoaderData::classes_do(void f(Klass * const)) {
   1.132 +  for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   1.133 +    f(k);
   1.134 +  }
   1.135 +}
   1.136 +
   1.137 +void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
   1.138 +  // Lock to avoid classes being modified/added/removed during iteration
   1.139 +  MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   1.140 +  for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   1.141 +    // Do not filter ArrayKlass oops here...
   1.142 +    if (k->oop_is_array() || (k->oop_is_instance() && InstanceKlass::cast(k)->is_loaded())) {
   1.143 +      klass_closure->do_klass(k);
   1.144 +    }
   1.145 +  }
   1.146 +}
   1.147 +
   1.148 +void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
   1.149 +  for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   1.150 +    if (k->oop_is_instance()) {
   1.151 +      f(InstanceKlass::cast(k));
   1.152 +    }
   1.153 +    assert(k != k->next_link(), "no loops!");
   1.154 +  }
   1.155 +}
   1.156 +
   1.157 +void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
   1.158 +  ClassLoaderData * const from_cld = this;
   1.159 +  ClassLoaderData * const to_cld = k->class_loader_data();
   1.160 +
   1.161 +  // Dependency to the null class loader data doesn't need to be recorded
   1.162 +  // because the null class loader data never goes away.
   1.163 +  if (to_cld->is_the_null_class_loader_data()) {
   1.164 +    return;
   1.165 +  }
   1.166 +
   1.167 +  oop to;
   1.168 +  if (to_cld->is_anonymous()) {
   1.169 +    // Anonymous class dependencies are through the mirror.
   1.170 +    to = k->java_mirror();
   1.171 +  } else {
   1.172 +    to = to_cld->class_loader();
   1.173 +
   1.174 +    // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
   1.175 +    // we still have to add it.  The class_loader won't keep from_cld alive.
   1.176 +    if (!from_cld->is_anonymous()) {
   1.177 +      // Check that this dependency isn't from the same or parent class_loader
   1.178 +      oop from = from_cld->class_loader();
   1.179 +
   1.180 +      oop curr = from;
   1.181 +      while (curr != NULL) {
   1.182 +        if (curr == to) {
   1.183 +          return; // this class loader is in the parent list, no need to add it.
   1.184 +        }
   1.185 +        curr = java_lang_ClassLoader::parent(curr);
   1.186 +      }
   1.187 +    }
   1.188 +  }
   1.189 +
   1.190 +  // It's a dependency we won't find through GC, add it. This is relatively rare
   1.191 +  // Must handle over GC point.
   1.192 +  Handle dependency(THREAD, to);
   1.193 +  from_cld->_dependencies.add(dependency, CHECK);
   1.194 +}
   1.195 +
   1.196 +
   1.197 +void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {
   1.198 +  // Check first if this dependency is already in the list.
   1.199 +  // Save a pointer to the last to add to under the lock.
   1.200 +  objArrayOop ok = _list_head;
   1.201 +  objArrayOop last = NULL;
   1.202 +  while (ok != NULL) {
   1.203 +    last = ok;
   1.204 +    if (ok->obj_at(0) == dependency()) {
   1.205 +      // Don't need to add it
   1.206 +      return;
   1.207 +    }
   1.208 +    ok = (objArrayOop)ok->obj_at(1);
   1.209 +  }
   1.210 +
   1.211 +  // Must handle over GC points
   1.212 +  assert (last != NULL, "dependencies should be initialized");
   1.213 +  objArrayHandle last_handle(THREAD, last);
   1.214 +
   1.215 +  // Create a new dependency node with fields for (class_loader or mirror, next)
   1.216 +  objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
   1.217 +  deps->obj_at_put(0, dependency());
   1.218 +
   1.219 +  // Must handle over GC points
   1.220 +  objArrayHandle new_dependency(THREAD, deps);
   1.221 +
   1.222 +  // Add the dependency under lock
   1.223 +  locked_add(last_handle, new_dependency, THREAD);
   1.224 +}
   1.225 +
   1.226 +void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,
   1.227 +                                               objArrayHandle new_dependency,
   1.228 +                                               Thread* THREAD) {
   1.229 +
   1.230 +  // Have to lock and put the new dependency on the end of the dependency
   1.231 +  // array so the card mark for CMS sees that this dependency is new.
   1.232 +  // Can probably do this lock free with some effort.
   1.233 +  ObjectLocker ol(Handle(THREAD, _list_head), THREAD);
   1.234 +
   1.235 +  oop loader_or_mirror = new_dependency->obj_at(0);
   1.236 +
   1.237 +  // Since the dependencies are only added, add to the end.
   1.238 +  objArrayOop end = last_handle();
   1.239 +  objArrayOop last = NULL;
   1.240 +  while (end != NULL) {
   1.241 +    last = end;
   1.242 +    // check again if another thread added it to the end.
   1.243 +    if (end->obj_at(0) == loader_or_mirror) {
   1.244 +      // Don't need to add it
   1.245 +      return;
   1.246 +    }
   1.247 +    end = (objArrayOop)end->obj_at(1);
   1.248 +  }
   1.249 +  assert (last != NULL, "dependencies should be initialized");
   1.250 +  // fill in the first element with the oop in new_dependency.
   1.251 +  if (last->obj_at(0) == NULL) {
   1.252 +    last->obj_at_put(0, new_dependency->obj_at(0));
   1.253 +  } else {
   1.254 +    last->obj_at_put(1, new_dependency());
   1.255 +  }
   1.256 +}
   1.257 +
   1.258 +void ClassLoaderDataGraph::clear_claimed_marks() {
   1.259 +  for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   1.260 +    cld->clear_claimed();
   1.261 +  }
   1.262 +}
   1.263 +
   1.264 +void ClassLoaderData::add_class(Klass* k) {
   1.265 +  MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   1.266 +  Klass* old_value = _klasses;
   1.267 +  k->set_next_link(old_value);
   1.268 +  // link the new item into the list
   1.269 +  _klasses = k;
   1.270 +
   1.271 +  if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
   1.272 +    ResourceMark rm;
   1.273 +    tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
   1.274 +                  PTR_FORMAT " loader: " PTR_FORMAT " %s",
   1.275 +                  p2i(k),
   1.276 +                  k->external_name(),
   1.277 +                  p2i(k->class_loader_data()),
   1.278 +                  p2i((void *)k->class_loader()),
   1.279 +                  loader_name());
   1.280 +  }
   1.281 +}
   1.282 +
   1.283 +// This is called by InstanceKlass::deallocate_contents() to remove the
   1.284 +// scratch_class for redefine classes.  We need a lock because there it may not
   1.285 +// be called at a safepoint if there's an error.
   1.286 +void ClassLoaderData::remove_class(Klass* scratch_class) {
   1.287 +  MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   1.288 +  Klass* prev = NULL;
   1.289 +  for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   1.290 +    if (k == scratch_class) {
   1.291 +      if (prev == NULL) {
   1.292 +        _klasses = k->next_link();
   1.293 +      } else {
   1.294 +        Klass* next = k->next_link();
   1.295 +        prev->set_next_link(next);
   1.296 +      }
   1.297 +      return;
   1.298 +    }
   1.299 +    prev = k;
   1.300 +    assert(k != k->next_link(), "no loops!");
   1.301 +  }
   1.302 +  ShouldNotReachHere();   // should have found this class!!
   1.303 +}
   1.304 +
   1.305 +void ClassLoaderData::unload() {
   1.306 +  _unloading = true;
   1.307 +
   1.308 +  // Tell serviceability tools these classes are unloading
   1.309 +  classes_do(InstanceKlass::notify_unload_class);
   1.310 +
   1.311 +  if (TraceClassLoaderData) {
   1.312 +    ResourceMark rm;
   1.313 +    tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this));
   1.314 +    tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
   1.315 +               loader_name());
   1.316 +    if (is_anonymous()) {
   1.317 +      tty->print(" for anonymous class  " INTPTR_FORMAT " ", p2i(_klasses));
   1.318 +    }
   1.319 +    tty->print_cr("]");
   1.320 +  }
   1.321 +}
   1.322 +
   1.323 +bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
   1.324 +  bool alive =
   1.325 +    is_anonymous() ?
   1.326 +       is_alive_closure->do_object_b(_klasses->java_mirror()) :
   1.327 +       class_loader() == NULL || is_alive_closure->do_object_b(class_loader());
   1.328 +  assert(!alive || claimed(), "must be claimed");
   1.329 +  return alive;
   1.330 +}
   1.331 +
   1.332 +
   1.333 +ClassLoaderData::~ClassLoaderData() {
   1.334 +  // Release C heap structures for all the classes.
   1.335 +  classes_do(InstanceKlass::release_C_heap_structures);
   1.336 +
   1.337 +  Metaspace *m = _metaspace;
   1.338 +  if (m != NULL) {
   1.339 +    _metaspace = NULL;
   1.340 +    // release the metaspace
   1.341 +    delete m;
   1.342 +    // release the handles
   1.343 +    if (_handles != NULL) {
   1.344 +      JNIHandleBlock::release_block(_handles);
   1.345 +      _handles = NULL;
   1.346 +    }
   1.347 +  }
   1.348 +
   1.349 +  // Clear all the JNI handles for methods
   1.350 +  // These aren't deallocated and are going to look like a leak, but that's
   1.351 +  // needed because we can't really get rid of jmethodIDs because we don't
   1.352 +  // know when native code is going to stop using them.  The spec says that
   1.353 +  // they're "invalid" but existing programs likely rely on their being
   1.354 +  // NULL after class unloading.
   1.355 +  if (_jmethod_ids != NULL) {
   1.356 +    Method::clear_jmethod_ids(this);
   1.357 +  }
   1.358 +  // Delete lock
   1.359 +  delete _metaspace_lock;
   1.360 +
   1.361 +  // Delete free list
   1.362 +  if (_deallocate_list != NULL) {
   1.363 +    delete _deallocate_list;
   1.364 +  }
   1.365 +}
   1.366 +
   1.367 +/**
   1.368 + * Returns true if this class loader data is for the extension class loader.
   1.369 + */
   1.370 +bool ClassLoaderData::is_ext_class_loader_data() const {
   1.371 +  return SystemDictionary::is_ext_class_loader(class_loader());
   1.372 +}
   1.373 +
   1.374 +Metaspace* ClassLoaderData::metaspace_non_null() {
   1.375 +  assert(!DumpSharedSpaces, "wrong metaspace!");
   1.376 +  // If the metaspace has not been allocated, create a new one.  Might want
   1.377 +  // to create smaller arena for Reflection class loaders also.
   1.378 +  // The reason for the delayed allocation is because some class loaders are
   1.379 +  // simply for delegating with no metadata of their own.
   1.380 +  if (_metaspace == NULL) {
   1.381 +    MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   1.382 +    // Check again if metaspace has been allocated while we were getting this lock.
   1.383 +    if (_metaspace != NULL) {
   1.384 +      return _metaspace;
   1.385 +    }
   1.386 +    if (this == the_null_class_loader_data()) {
   1.387 +      assert (class_loader() == NULL, "Must be");
   1.388 +      set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
   1.389 +    } else if (is_anonymous()) {
   1.390 +      if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   1.391 +        tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
   1.392 +      }
   1.393 +      set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
   1.394 +    } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
   1.395 +      if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   1.396 +        tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
   1.397 +      }
   1.398 +      set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
   1.399 +    } else {
   1.400 +      set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
   1.401 +    }
   1.402 +  }
   1.403 +  return _metaspace;
   1.404 +}
   1.405 +
   1.406 +JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
   1.407 +void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
   1.408 +
   1.409 +jobject ClassLoaderData::add_handle(Handle h) {
   1.410 +  MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   1.411 +  if (handles() == NULL) {
   1.412 +    set_handles(JNIHandleBlock::allocate_block());
   1.413 +  }
   1.414 +  return handles()->allocate_handle(h());
   1.415 +}
   1.416 +
   1.417 +// Add this metadata pointer to be freed when it's safe.  This is only during
   1.418 +// class unloading because Handles might point to this metadata field.
   1.419 +void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   1.420 +  // Metadata in shared region isn't deleted.
   1.421 +  if (!m->is_shared()) {
   1.422 +    MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   1.423 +    if (_deallocate_list == NULL) {
   1.424 +      _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
   1.425 +    }
   1.426 +    _deallocate_list->append_if_missing(m);
   1.427 +  }
   1.428 +}
   1.429 +
   1.430 +// Deallocate free metadata on the free list.  How useful the PermGen was!
   1.431 +void ClassLoaderData::free_deallocate_list() {
   1.432 +  // Don't need lock, at safepoint
   1.433 +  assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
   1.434 +  if (_deallocate_list == NULL) {
   1.435 +    return;
   1.436 +  }
   1.437 +  // Go backwards because this removes entries that are freed.
   1.438 +  for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
   1.439 +    Metadata* m = _deallocate_list->at(i);
   1.440 +    if (!m->on_stack()) {
   1.441 +      _deallocate_list->remove_at(i);
   1.442 +      // There are only three types of metadata that we deallocate directly.
   1.443 +      // Cast them so they can be used by the template function.
   1.444 +      if (m->is_method()) {
   1.445 +        MetadataFactory::free_metadata(this, (Method*)m);
   1.446 +      } else if (m->is_constantPool()) {
   1.447 +        MetadataFactory::free_metadata(this, (ConstantPool*)m);
   1.448 +      } else if (m->is_klass()) {
   1.449 +        MetadataFactory::free_metadata(this, (InstanceKlass*)m);
   1.450 +      } else {
   1.451 +        ShouldNotReachHere();
   1.452 +      }
   1.453 +    }
   1.454 +  }
   1.455 +}
   1.456 +
   1.457 +// These anonymous class loaders are to contain classes used for JSR292
   1.458 +ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
   1.459 +  // Add a new class loader data to the graph.
   1.460 +  return ClassLoaderDataGraph::add(loader, true, CHECK_NULL);
   1.461 +}
   1.462 +
   1.463 +const char* ClassLoaderData::loader_name() {
   1.464 +  // Handles null class loader
   1.465 +  return SystemDictionary::loader_name(class_loader());
   1.466 +}
   1.467 +
   1.468 +#ifndef PRODUCT
   1.469 +// Define to dump klasses
   1.470 +#undef CLD_DUMP_KLASSES
   1.471 +
   1.472 +void ClassLoaderData::dump(outputStream * const out) {
   1.473 +  ResourceMark rm;
   1.474 +  out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
   1.475 +      p2i(this), p2i((void *)class_loader()),
   1.476 +      p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
   1.477 +  if (claimed()) out->print(" claimed ");
   1.478 +  if (is_unloading()) out->print(" unloading ");
   1.479 +  out->print(" handles " INTPTR_FORMAT, p2i(handles()));
   1.480 +  out->cr();
   1.481 +  if (metaspace_or_null() != NULL) {
   1.482 +    out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
   1.483 +    metaspace_or_null()->dump(out);
   1.484 +  } else {
   1.485 +    out->print_cr("metaspace: NULL");
   1.486 +  }
   1.487 +
   1.488 +#ifdef CLD_DUMP_KLASSES
   1.489 +  if (Verbose) {
   1.490 +    ResourceMark rm;
   1.491 +    Klass* k = _klasses;
   1.492 +    while (k != NULL) {
   1.493 +      out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
   1.494 +          k->has_modified_oops(), k->has_accumulated_modified_oops());
   1.495 +      assert(k != k->next_link(), "no loops!");
   1.496 +      k = k->next_link();
   1.497 +    }
   1.498 +  }
   1.499 +#endif  // CLD_DUMP_KLASSES
   1.500 +#undef CLD_DUMP_KLASSES
   1.501 +  if (_jmethod_ids != NULL) {
   1.502 +    Method::print_jmethod_ids(this, out);
   1.503 +  }
   1.504 +  out->print_cr("}");
   1.505 +}
   1.506 +#endif // PRODUCT
   1.507 +
   1.508 +void ClassLoaderData::verify() {
   1.509 +  oop cl = class_loader();
   1.510 +
   1.511 +  guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
   1.512 +  guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
   1.513 +
   1.514 +  // Verify the integrity of the allocated space.
   1.515 +  if (metaspace_or_null() != NULL) {
   1.516 +    metaspace_or_null()->verify();
   1.517 +  }
   1.518 +
   1.519 +  for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   1.520 +    guarantee(k->class_loader_data() == this, "Must be the same");
   1.521 +    k->verify();
   1.522 +    assert(k != k->next_link(), "no loops!");
   1.523 +  }
   1.524 +}
   1.525 +
   1.526 +bool ClassLoaderData::contains_klass(Klass* klass) {
   1.527 +  for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   1.528 +    if (k == klass) return true;
   1.529 +  }
   1.530 +  return false;
   1.531 +}
   1.532 +
   1.533 +
   1.534 +// GC root of class loader data created.
   1.535 +ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
   1.536 +ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
   1.537 +ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
   1.538 +ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
   1.539 +
   1.540 +bool ClassLoaderDataGraph::_should_purge = false;
   1.541 +
   1.542 +// Add a new class loader data node to the list.  Assign the newly created
   1.543 +// ClassLoaderData into the java/lang/ClassLoader object as a hidden field
   1.544 +ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
   1.545 +  // We need to allocate all the oops for the ClassLoaderData before allocating the
   1.546 +  // actual ClassLoaderData object.
   1.547 +  ClassLoaderData::Dependencies dependencies(CHECK_NULL);
   1.548 +
   1.549 +  No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
   1.550 +                                       // ClassLoaderData in the graph since the CLD
   1.551 +                                       // contains unhandled oops
   1.552 +
   1.553 +  ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
   1.554 +
   1.555 +
   1.556 +  if (!is_anonymous) {
   1.557 +    ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
   1.558 +    // First, Atomically set it
   1.559 +    ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
   1.560 +    if (old != NULL) {
   1.561 +      delete cld;
   1.562 +      // Returns the data.
   1.563 +      return old;
   1.564 +    }
   1.565 +  }
   1.566 +
   1.567 +  // We won the race, and therefore the task of adding the data to the list of
   1.568 +  // class loader data
   1.569 +  ClassLoaderData** list_head = &_head;
   1.570 +  ClassLoaderData* next = _head;
   1.571 +
   1.572 +  do {
   1.573 +    cld->set_next(next);
   1.574 +    ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
   1.575 +    if (exchanged == next) {
   1.576 +      if (TraceClassLoaderData) {
   1.577 +        ResourceMark rm;
   1.578 +        tty->print("[ClassLoaderData: ");
   1.579 +        tty->print("create class loader data " INTPTR_FORMAT, p2i(cld));
   1.580 +        tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
   1.581 +                   cld->loader_name());
   1.582 +        tty->print_cr("]");
   1.583 +      }
   1.584 +      return cld;
   1.585 +    }
   1.586 +    next = exchanged;
   1.587 +  } while (true);
   1.588 +
   1.589 +}
   1.590 +
   1.591 +void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   1.592 +  for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   1.593 +    cld->oops_do(f, klass_closure, must_claim);
   1.594 +  }
   1.595 +}
   1.596 +
   1.597 +void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   1.598 +  for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   1.599 +    if (cld->keep_alive()) {
   1.600 +      cld->oops_do(f, klass_closure, must_claim);
   1.601 +    }
   1.602 +  }
   1.603 +}
   1.604 +
   1.605 +void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   1.606 +  if (ClassUnloading) {
   1.607 +    ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
   1.608 +    // keep any special CLDs alive.
   1.609 +    ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
   1.610 +  } else {
   1.611 +    ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
   1.612 +  }
   1.613 +}
   1.614 +
   1.615 +void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
   1.616 +  for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   1.617 +    cld->classes_do(klass_closure);
   1.618 +  }
   1.619 +}
   1.620 +
   1.621 +void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
   1.622 +  for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   1.623 +    cld->classes_do(f);
   1.624 +  }
   1.625 +}
   1.626 +
   1.627 +void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
   1.628 +  for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   1.629 +    cld->loaded_classes_do(klass_closure);
   1.630 +  }
   1.631 +}
   1.632 +
   1.633 +void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
   1.634 +  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   1.635 +  // Only walk the head until any clds not purged from prior unloading
   1.636 +  // (CMS doesn't purge right away).
   1.637 +  for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
   1.638 +    cld->classes_do(f);
   1.639 +  }
   1.640 +}
   1.641 +
   1.642 +GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
   1.643 +  assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
   1.644 +
   1.645 +  GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
   1.646 +
   1.647 +  // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
   1.648 +  ClassLoaderData* curr = _head;
   1.649 +  while (curr != _saved_head) {
   1.650 +    if (!curr->claimed()) {
   1.651 +      array->push(curr);
   1.652 +
   1.653 +      if (TraceClassLoaderData) {
   1.654 +        tty->print("[ClassLoaderData] found new CLD: ");
   1.655 +        curr->print_value_on(tty);
   1.656 +        tty->cr();
   1.657 +      }
   1.658 +    }
   1.659 +
   1.660 +    curr = curr->_next;
   1.661 +  }
   1.662 +
   1.663 +  return array;
   1.664 +}
   1.665 +
   1.666 +#ifndef PRODUCT
   1.667 +bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
   1.668 +  for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   1.669 +    if (loader_data == data) {
   1.670 +      return true;
   1.671 +    }
   1.672 +  }
   1.673 +
   1.674 +  return false;
   1.675 +}
   1.676 +#endif // PRODUCT
   1.677 +
   1.678 +
   1.679 +// Move class loader data from main list to the unloaded list for unloading
   1.680 +// and deallocation later.
   1.681 +bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) {
   1.682 +  ClassLoaderData* data = _head;
   1.683 +  ClassLoaderData* prev = NULL;
   1.684 +  bool seen_dead_loader = false;
   1.685 +
   1.686 +  // Save previous _unloading pointer for CMS which may add to unloading list before
   1.687 +  // purging and we don't want to rewalk the previously unloaded class loader data.
   1.688 +  _saved_unloading = _unloading;
   1.689 +
   1.690 +  // mark metadata seen on the stack and code cache so we can delete
   1.691 +  // unneeded entries.
   1.692 +  bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
   1.693 +  MetadataOnStackMark md_on_stack;
   1.694 +  while (data != NULL) {
   1.695 +    if (data->keep_alive() || data->is_alive(is_alive_closure)) {
   1.696 +      if (has_redefined_a_class) {
   1.697 +        data->classes_do(InstanceKlass::purge_previous_versions);
   1.698 +      }
   1.699 +      data->free_deallocate_list();
   1.700 +      prev = data;
   1.701 +      data = data->next();
   1.702 +      continue;
   1.703 +    }
   1.704 +    seen_dead_loader = true;
   1.705 +    ClassLoaderData* dead = data;
   1.706 +    dead->unload();
   1.707 +    data = data->next();
   1.708 +    // Remove from loader list.
   1.709 +    // This class loader data will no longer be found
   1.710 +    // in the ClassLoaderDataGraph.
   1.711 +    if (prev != NULL) {
   1.712 +      prev->set_next(data);
   1.713 +    } else {
   1.714 +      assert(dead == _head, "sanity check");
   1.715 +      _head = data;
   1.716 +    }
   1.717 +    dead->set_next(_unloading);
   1.718 +    _unloading = dead;
   1.719 +  }
   1.720 +
   1.721 +  if (seen_dead_loader) {
   1.722 +    post_class_unload_events();
   1.723 +  }
   1.724 +
   1.725 +  return seen_dead_loader;
   1.726 +}
   1.727 +
   1.728 +void ClassLoaderDataGraph::purge() {
   1.729 +  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   1.730 +  ClassLoaderData* list = _unloading;
   1.731 +  _unloading = NULL;
   1.732 +  ClassLoaderData* next = list;
   1.733 +  while (next != NULL) {
   1.734 +    ClassLoaderData* purge_me = next;
   1.735 +    next = purge_me->next();
   1.736 +    delete purge_me;
   1.737 +  }
   1.738 +  Metaspace::purge();
   1.739 +}
   1.740 +
   1.741 +void ClassLoaderDataGraph::post_class_unload_events(void) {
   1.742 +#if INCLUDE_TRACE
   1.743 +  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   1.744 +  if (Tracing::enabled()) {
   1.745 +    if (Tracing::is_event_enabled(TraceClassUnloadEvent)) {
   1.746 +      assert(_unloading != NULL, "need class loader data unload list!");
   1.747 +      _class_unload_time = Ticks::now();
   1.748 +      classes_unloading_do(&class_unload_event);
   1.749 +    }
   1.750 +    Tracing::on_unloading_classes();
   1.751 +  }
   1.752 +#endif
   1.753 +}
   1.754 +
   1.755 +// CDS support
   1.756 +
   1.757 +// Global metaspaces for writing information to the shared archive.  When
   1.758 +// application CDS is supported, we may need one per metaspace, so this
   1.759 +// sort of looks like it.
   1.760 +Metaspace* ClassLoaderData::_ro_metaspace = NULL;
   1.761 +Metaspace* ClassLoaderData::_rw_metaspace = NULL;
   1.762 +static bool _shared_metaspaces_initialized = false;
   1.763 +
   1.764 +// Initialize shared metaspaces (change to call from somewhere not lazily)
   1.765 +void ClassLoaderData::initialize_shared_metaspaces() {
   1.766 +  assert(DumpSharedSpaces, "only use this for dumping shared spaces");
   1.767 +  assert(this == ClassLoaderData::the_null_class_loader_data(),
   1.768 +         "only supported for null loader data for now");
   1.769 +  assert (!_shared_metaspaces_initialized, "only initialize once");
   1.770 +  MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   1.771 +  _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
   1.772 +  _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
   1.773 +  _shared_metaspaces_initialized = true;
   1.774 +}
   1.775 +
   1.776 +Metaspace* ClassLoaderData::ro_metaspace() {
   1.777 +  assert(_ro_metaspace != NULL, "should already be initialized");
   1.778 +  return _ro_metaspace;
   1.779 +}
   1.780 +
   1.781 +Metaspace* ClassLoaderData::rw_metaspace() {
   1.782 +  assert(_rw_metaspace != NULL, "should already be initialized");
   1.783 +  return _rw_metaspace;
   1.784 +}
   1.785 +
   1.786 +
   1.787 +ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
   1.788 +  _data = ClassLoaderDataGraph::_head;
   1.789 +}
   1.790 +
   1.791 +ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
   1.792 +
   1.793 +#ifndef PRODUCT
   1.794 +// callable from debugger
   1.795 +extern "C" int print_loader_data_graph() {
   1.796 +  ClassLoaderDataGraph::dump_on(tty);
   1.797 +  return 0;
   1.798 +}
   1.799 +
   1.800 +void ClassLoaderDataGraph::verify() {
   1.801 +  for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   1.802 +    data->verify();
   1.803 +  }
   1.804 +}
   1.805 +
   1.806 +void ClassLoaderDataGraph::dump_on(outputStream * const out) {
   1.807 +  for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   1.808 +    data->dump(out);
   1.809 +  }
   1.810 +  MetaspaceAux::dump(out);
   1.811 +}
   1.812 +#endif // PRODUCT
   1.813 +
   1.814 +void ClassLoaderData::print_value_on(outputStream* out) const {
   1.815 +  if (class_loader() == NULL) {
   1.816 +    out->print("NULL class_loader");
   1.817 +  } else {
   1.818 +    out->print("class loader " INTPTR_FORMAT, p2i(this));
   1.819 +    class_loader()->print_value_on(out);
   1.820 +  }
   1.821 +}
   1.822 +
   1.823 +#if INCLUDE_TRACE
   1.824 +
   1.825 +Ticks ClassLoaderDataGraph::_class_unload_time;
   1.826 +
   1.827 +void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
   1.828 +
   1.829 +  // post class unload event
   1.830 +  EventClassUnload event(UNTIMED);
   1.831 +  event.set_endtime(_class_unload_time);
   1.832 +  event.set_unloadedClass(k);
   1.833 +  oop defining_class_loader = k->class_loader();
   1.834 +  event.set_definingClassLoader(defining_class_loader != NULL ?
   1.835 +                                defining_class_loader->klass() : (Klass*)NULL);
   1.836 +  event.commit();
   1.837 +}
   1.838 +
   1.839 +#endif /* INCLUDE_TRACE */

mercurial