Thu, 31 Mar 2016 14:23:12 -0700
Merge
coleenp@4037 | 1 | /* |
coleenp@6316 | 2 | * Copyright (c) 2012, 2014, 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" |
mgronlun@6131 | 65 | #include "utilities/macros.hpp" |
coleenp@4037 | 66 | #include "utilities/ostream.hpp" |
sla@5237 | 67 | #if INCLUDE_TRACE |
mgronlun@7367 | 68 | #include "trace/tracing.hpp" |
sla@5237 | 69 | #endif |
sla@5237 | 70 | |
coleenp@4037 | 71 | ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; |
coleenp@4037 | 72 | |
mgerdin@5016 | 73 | ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) : |
coleenp@4345 | 74 | _class_loader(h_class_loader()), |
stefank@6974 | 75 | _is_anonymous(is_anonymous), |
stefank@6974 | 76 | // An anonymous class loader data doesn't have anything to keep |
stefank@6974 | 77 | // it from being unloaded during parsing of the anonymous class. |
stefank@6974 | 78 | // The null-class-loader should always be kept alive. |
stefank@6974 | 79 | _keep_alive(is_anonymous || h_class_loader.is_null()), |
coleenp@4345 | 80 | _metaspace(NULL), _unloading(false), _klasses(NULL), |
coleenp@4304 | 81 | _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL), |
mgerdin@5016 | 82 | _next(NULL), _dependencies(dependencies), |
coleenp@4037 | 83 | _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) { |
coleenp@4037 | 84 | // empty |
coleenp@4037 | 85 | } |
coleenp@4037 | 86 | |
coleenp@4304 | 87 | void ClassLoaderData::init_dependencies(TRAPS) { |
mgerdin@5016 | 88 | assert(!Universe::is_fully_initialized(), "should only be called when initializing"); |
mgerdin@5016 | 89 | assert(is_the_null_class_loader_data(), "should only call this for the null class loader"); |
mgerdin@4903 | 90 | _dependencies.init(CHECK); |
mgerdin@4903 | 91 | } |
mgerdin@4903 | 92 | |
mgerdin@4903 | 93 | void ClassLoaderData::Dependencies::init(TRAPS) { |
coleenp@4304 | 94 | // Create empty dependencies array to add to. CMS requires this to be |
coleenp@4304 | 95 | // an oop so that it can track additions via card marks. We think. |
mgerdin@4903 | 96 | _list_head = oopFactory::new_objectArray(2, CHECK); |
coleenp@4304 | 97 | } |
coleenp@4304 | 98 | |
coleenp@4037 | 99 | bool ClassLoaderData::claim() { |
coleenp@4037 | 100 | if (_claimed == 1) { |
coleenp@4037 | 101 | return false; |
coleenp@4037 | 102 | } |
coleenp@4037 | 103 | |
coleenp@4037 | 104 | return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0; |
coleenp@4037 | 105 | } |
coleenp@4037 | 106 | |
coleenp@4037 | 107 | void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { |
coleenp@4037 | 108 | if (must_claim && !claim()) { |
coleenp@4037 | 109 | return; |
coleenp@4037 | 110 | } |
coleenp@4037 | 111 | |
coleenp@4037 | 112 | f->do_oop(&_class_loader); |
mgerdin@4903 | 113 | _dependencies.oops_do(f); |
coleenp@4037 | 114 | _handles->oops_do(f); |
coleenp@4037 | 115 | if (klass_closure != NULL) { |
coleenp@4037 | 116 | classes_do(klass_closure); |
coleenp@4037 | 117 | } |
coleenp@4037 | 118 | } |
coleenp@4037 | 119 | |
mgerdin@4903 | 120 | void ClassLoaderData::Dependencies::oops_do(OopClosure* f) { |
mgerdin@4903 | 121 | f->do_oop((oop*)&_list_head); |
mgerdin@4903 | 122 | } |
mgerdin@4903 | 123 | |
coleenp@4037 | 124 | void ClassLoaderData::classes_do(KlassClosure* klass_closure) { |
coleenp@4037 | 125 | for (Klass* k = _klasses; k != NULL; k = k->next_link()) { |
coleenp@4037 | 126 | klass_closure->do_klass(k); |
coleenp@4751 | 127 | assert(k != k->next_link(), "no loops!"); |
coleenp@4037 | 128 | } |
coleenp@4037 | 129 | } |
coleenp@4037 | 130 | |
sla@5237 | 131 | void ClassLoaderData::classes_do(void f(Klass * const)) { |
sla@5237 | 132 | for (Klass* k = _klasses; k != NULL; k = k->next_link()) { |
sla@5237 | 133 | f(k); |
sla@5237 | 134 | } |
sla@5237 | 135 | } |
sla@5237 | 136 | |
farvidsson@6024 | 137 | void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) { |
farvidsson@6024 | 138 | // Lock to avoid classes being modified/added/removed during iteration |
farvidsson@6024 | 139 | MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); |
farvidsson@6024 | 140 | for (Klass* k = _klasses; k != NULL; k = k->next_link()) { |
farvidsson@6024 | 141 | // Do not filter ArrayKlass oops here... |
farvidsson@6024 | 142 | if (k->oop_is_array() || (k->oop_is_instance() && InstanceKlass::cast(k)->is_loaded())) { |
farvidsson@6024 | 143 | klass_closure->do_klass(k); |
farvidsson@6024 | 144 | } |
farvidsson@6024 | 145 | } |
farvidsson@6024 | 146 | } |
farvidsson@6024 | 147 | |
coleenp@4037 | 148 | void ClassLoaderData::classes_do(void f(InstanceKlass*)) { |
coleenp@4037 | 149 | for (Klass* k = _klasses; k != NULL; k = k->next_link()) { |
coleenp@4037 | 150 | if (k->oop_is_instance()) { |
coleenp@4037 | 151 | f(InstanceKlass::cast(k)); |
coleenp@4037 | 152 | } |
coleenp@4751 | 153 | assert(k != k->next_link(), "no loops!"); |
coleenp@4037 | 154 | } |
coleenp@4037 | 155 | } |
coleenp@4037 | 156 | |
coleenp@4037 | 157 | void ClassLoaderData::record_dependency(Klass* k, TRAPS) { |
coleenp@4037 | 158 | ClassLoaderData * const from_cld = this; |
coleenp@4037 | 159 | ClassLoaderData * const to_cld = k->class_loader_data(); |
coleenp@4037 | 160 | |
coleenp@4304 | 161 | // Dependency to the null class loader data doesn't need to be recorded |
coleenp@4304 | 162 | // because the null class loader data never goes away. |
coleenp@4304 | 163 | if (to_cld->is_the_null_class_loader_data()) { |
coleenp@4037 | 164 | return; |
coleenp@4037 | 165 | } |
coleenp@4037 | 166 | |
coleenp@4304 | 167 | oop to; |
coleenp@4304 | 168 | if (to_cld->is_anonymous()) { |
coleenp@4304 | 169 | // Anonymous class dependencies are through the mirror. |
coleenp@4304 | 170 | to = k->java_mirror(); |
coleenp@4304 | 171 | } else { |
coleenp@4304 | 172 | to = to_cld->class_loader(); |
coleenp@4037 | 173 | |
coleenp@4304 | 174 | // If from_cld is anonymous, even if it's class_loader is a parent of 'to' |
coleenp@4304 | 175 | // we still have to add it. The class_loader won't keep from_cld alive. |
coleenp@4304 | 176 | if (!from_cld->is_anonymous()) { |
coleenp@4304 | 177 | // Check that this dependency isn't from the same or parent class_loader |
coleenp@4304 | 178 | oop from = from_cld->class_loader(); |
coleenp@4304 | 179 | |
coleenp@4304 | 180 | oop curr = from; |
coleenp@4304 | 181 | while (curr != NULL) { |
coleenp@4304 | 182 | if (curr == to) { |
coleenp@4304 | 183 | return; // this class loader is in the parent list, no need to add it. |
coleenp@4304 | 184 | } |
coleenp@4304 | 185 | curr = java_lang_ClassLoader::parent(curr); |
coleenp@4304 | 186 | } |
coleenp@4037 | 187 | } |
coleenp@4037 | 188 | } |
coleenp@4037 | 189 | |
coleenp@4037 | 190 | // It's a dependency we won't find through GC, add it. This is relatively rare |
coleenp@4304 | 191 | // Must handle over GC point. |
coleenp@4304 | 192 | Handle dependency(THREAD, to); |
mgerdin@4903 | 193 | from_cld->_dependencies.add(dependency, CHECK); |
coleenp@4037 | 194 | } |
coleenp@4037 | 195 | |
coleenp@4037 | 196 | |
mgerdin@4903 | 197 | void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) { |
coleenp@4304 | 198 | // Check first if this dependency is already in the list. |
coleenp@4304 | 199 | // Save a pointer to the last to add to under the lock. |
mgerdin@4903 | 200 | objArrayOop ok = _list_head; |
coleenp@4304 | 201 | objArrayOop last = NULL; |
coleenp@4037 | 202 | while (ok != NULL) { |
coleenp@4304 | 203 | last = ok; |
coleenp@4304 | 204 | if (ok->obj_at(0) == dependency()) { |
coleenp@4304 | 205 | // Don't need to add it |
coleenp@4304 | 206 | return; |
coleenp@4037 | 207 | } |
coleenp@4037 | 208 | ok = (objArrayOop)ok->obj_at(1); |
coleenp@4037 | 209 | } |
coleenp@4304 | 210 | |
stefank@4353 | 211 | // Must handle over GC points |
stefank@4353 | 212 | assert (last != NULL, "dependencies should be initialized"); |
stefank@4353 | 213 | objArrayHandle last_handle(THREAD, last); |
stefank@4353 | 214 | |
coleenp@4304 | 215 | // Create a new dependency node with fields for (class_loader or mirror, next) |
coleenp@4304 | 216 | objArrayOop deps = oopFactory::new_objectArray(2, CHECK); |
coleenp@4304 | 217 | deps->obj_at_put(0, dependency()); |
coleenp@4304 | 218 | |
stefank@4353 | 219 | // Must handle over GC points |
coleenp@4304 | 220 | objArrayHandle new_dependency(THREAD, deps); |
coleenp@4304 | 221 | |
coleenp@4304 | 222 | // Add the dependency under lock |
mgerdin@4903 | 223 | locked_add(last_handle, new_dependency, THREAD); |
coleenp@4037 | 224 | } |
coleenp@4037 | 225 | |
mgerdin@4903 | 226 | void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle, |
mgerdin@4903 | 227 | objArrayHandle new_dependency, |
mgerdin@4903 | 228 | Thread* THREAD) { |
coleenp@4304 | 229 | |
coleenp@4304 | 230 | // Have to lock and put the new dependency on the end of the dependency |
coleenp@4304 | 231 | // array so the card mark for CMS sees that this dependency is new. |
coleenp@4304 | 232 | // Can probably do this lock free with some effort. |
mgerdin@4903 | 233 | ObjectLocker ol(Handle(THREAD, _list_head), THREAD); |
coleenp@4304 | 234 | |
coleenp@4304 | 235 | oop loader_or_mirror = new_dependency->obj_at(0); |
coleenp@4304 | 236 | |
coleenp@4304 | 237 | // Since the dependencies are only added, add to the end. |
coleenp@4304 | 238 | objArrayOop end = last_handle(); |
coleenp@4304 | 239 | objArrayOop last = NULL; |
coleenp@4304 | 240 | while (end != NULL) { |
coleenp@4304 | 241 | last = end; |
coleenp@4304 | 242 | // check again if another thread added it to the end. |
coleenp@4304 | 243 | if (end->obj_at(0) == loader_or_mirror) { |
coleenp@4304 | 244 | // Don't need to add it |
coleenp@4304 | 245 | return; |
coleenp@4304 | 246 | } |
coleenp@4304 | 247 | end = (objArrayOop)end->obj_at(1); |
coleenp@4037 | 248 | } |
coleenp@4304 | 249 | assert (last != NULL, "dependencies should be initialized"); |
coleenp@4304 | 250 | // fill in the first element with the oop in new_dependency. |
coleenp@4304 | 251 | if (last->obj_at(0) == NULL) { |
coleenp@4304 | 252 | last->obj_at_put(0, new_dependency->obj_at(0)); |
coleenp@4304 | 253 | } else { |
coleenp@4304 | 254 | last->obj_at_put(1, new_dependency()); |
coleenp@4037 | 255 | } |
coleenp@4037 | 256 | } |
coleenp@4037 | 257 | |
coleenp@4037 | 258 | void ClassLoaderDataGraph::clear_claimed_marks() { |
coleenp@4037 | 259 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
coleenp@4037 | 260 | cld->clear_claimed(); |
coleenp@4037 | 261 | } |
coleenp@4037 | 262 | } |
coleenp@4037 | 263 | |
coleenp@4037 | 264 | void ClassLoaderData::add_class(Klass* k) { |
coleenp@4037 | 265 | MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); |
coleenp@4037 | 266 | Klass* old_value = _klasses; |
coleenp@4037 | 267 | k->set_next_link(old_value); |
coleenp@4037 | 268 | // link the new item into the list |
coleenp@4037 | 269 | _klasses = k; |
coleenp@4037 | 270 | |
coleenp@4304 | 271 | if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) { |
coleenp@4037 | 272 | ResourceMark rm; |
coleenp@4037 | 273 | tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: " |
coleenp@4037 | 274 | PTR_FORMAT " loader: " PTR_FORMAT " %s", |
drchase@6680 | 275 | p2i(k), |
coleenp@4037 | 276 | k->external_name(), |
drchase@6680 | 277 | p2i(k->class_loader_data()), |
drchase@6680 | 278 | p2i((void *)k->class_loader()), |
coleenp@4304 | 279 | loader_name()); |
coleenp@4037 | 280 | } |
coleenp@4037 | 281 | } |
coleenp@4037 | 282 | |
coleenp@4037 | 283 | // This is called by InstanceKlass::deallocate_contents() to remove the |
coleenp@4037 | 284 | // scratch_class for redefine classes. We need a lock because there it may not |
coleenp@4037 | 285 | // be called at a safepoint if there's an error. |
coleenp@4037 | 286 | void ClassLoaderData::remove_class(Klass* scratch_class) { |
coleenp@4037 | 287 | MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); |
coleenp@4037 | 288 | Klass* prev = NULL; |
coleenp@4037 | 289 | for (Klass* k = _klasses; k != NULL; k = k->next_link()) { |
coleenp@4037 | 290 | if (k == scratch_class) { |
coleenp@4037 | 291 | if (prev == NULL) { |
coleenp@4037 | 292 | _klasses = k->next_link(); |
coleenp@4037 | 293 | } else { |
coleenp@4037 | 294 | Klass* next = k->next_link(); |
coleenp@4037 | 295 | prev->set_next_link(next); |
coleenp@4037 | 296 | } |
coleenp@4037 | 297 | return; |
coleenp@4037 | 298 | } |
coleenp@4037 | 299 | prev = k; |
coleenp@4751 | 300 | assert(k != k->next_link(), "no loops!"); |
coleenp@4037 | 301 | } |
coleenp@4037 | 302 | ShouldNotReachHere(); // should have found this class!! |
coleenp@4037 | 303 | } |
coleenp@4037 | 304 | |
coleenp@4304 | 305 | void ClassLoaderData::unload() { |
coleenp@4304 | 306 | _unloading = true; |
coleenp@4304 | 307 | |
coleenp@4981 | 308 | // Tell serviceability tools these classes are unloading |
coleenp@4981 | 309 | classes_do(InstanceKlass::notify_unload_class); |
coleenp@4981 | 310 | |
coleenp@4304 | 311 | if (TraceClassLoaderData) { |
coleenp@4304 | 312 | ResourceMark rm; |
drchase@6680 | 313 | tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this)); |
drchase@6680 | 314 | tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()), |
coleenp@4304 | 315 | loader_name()); |
coleenp@4304 | 316 | if (is_anonymous()) { |
drchase@6680 | 317 | tty->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses)); |
coleenp@4304 | 318 | } |
coleenp@4304 | 319 | tty->print_cr("]"); |
coleenp@4304 | 320 | } |
coleenp@4304 | 321 | } |
coleenp@4304 | 322 | |
stefank@6974 | 323 | oop ClassLoaderData::keep_alive_object() const { |
stefank@6974 | 324 | assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive"); |
stefank@6974 | 325 | return is_anonymous() ? _klasses->java_mirror() : class_loader(); |
stefank@6974 | 326 | } |
stefank@6974 | 327 | |
coleenp@4304 | 328 | bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const { |
stefank@6974 | 329 | bool alive = keep_alive() // null class loader and incomplete anonymous klasses. |
stefank@6974 | 330 | || is_alive_closure->do_object_b(keep_alive_object()); |
stefank@6974 | 331 | |
coleenp@4304 | 332 | return alive; |
coleenp@4304 | 333 | } |
coleenp@4304 | 334 | |
coleenp@4304 | 335 | |
coleenp@4037 | 336 | ClassLoaderData::~ClassLoaderData() { |
coleenp@4981 | 337 | // Release C heap structures for all the classes. |
coleenp@4981 | 338 | classes_do(InstanceKlass::release_C_heap_structures); |
coleenp@4981 | 339 | |
coleenp@4037 | 340 | Metaspace *m = _metaspace; |
coleenp@4037 | 341 | if (m != NULL) { |
coleenp@4037 | 342 | _metaspace = NULL; |
coleenp@4037 | 343 | // release the metaspace |
coleenp@4037 | 344 | delete m; |
coleenp@4037 | 345 | // release the handles |
coleenp@4037 | 346 | if (_handles != NULL) { |
coleenp@4037 | 347 | JNIHandleBlock::release_block(_handles); |
coleenp@4037 | 348 | _handles = NULL; |
coleenp@4037 | 349 | } |
coleenp@4037 | 350 | } |
coleenp@4037 | 351 | |
coleenp@4037 | 352 | // Clear all the JNI handles for methods |
coleenp@4037 | 353 | // These aren't deallocated and are going to look like a leak, but that's |
coleenp@4037 | 354 | // needed because we can't really get rid of jmethodIDs because we don't |
coleenp@4037 | 355 | // know when native code is going to stop using them. The spec says that |
coleenp@4037 | 356 | // they're "invalid" but existing programs likely rely on their being |
coleenp@4037 | 357 | // NULL after class unloading. |
coleenp@4037 | 358 | if (_jmethod_ids != NULL) { |
coleenp@4037 | 359 | Method::clear_jmethod_ids(this); |
coleenp@4037 | 360 | } |
coleenp@4037 | 361 | // Delete lock |
coleenp@4037 | 362 | delete _metaspace_lock; |
coleenp@4037 | 363 | |
coleenp@4037 | 364 | // Delete free list |
coleenp@4037 | 365 | if (_deallocate_list != NULL) { |
coleenp@4037 | 366 | delete _deallocate_list; |
coleenp@4037 | 367 | } |
coleenp@4037 | 368 | } |
coleenp@4037 | 369 | |
twisti@4866 | 370 | /** |
twisti@4866 | 371 | * Returns true if this class loader data is for the extension class loader. |
twisti@4866 | 372 | */ |
twisti@4866 | 373 | bool ClassLoaderData::is_ext_class_loader_data() const { |
twisti@4866 | 374 | return SystemDictionary::is_ext_class_loader(class_loader()); |
twisti@4866 | 375 | } |
twisti@4866 | 376 | |
coleenp@4037 | 377 | Metaspace* ClassLoaderData::metaspace_non_null() { |
jmasa@4457 | 378 | assert(!DumpSharedSpaces, "wrong metaspace!"); |
coleenp@4037 | 379 | // If the metaspace has not been allocated, create a new one. Might want |
coleenp@4037 | 380 | // to create smaller arena for Reflection class loaders also. |
coleenp@4037 | 381 | // The reason for the delayed allocation is because some class loaders are |
coleenp@4037 | 382 | // simply for delegating with no metadata of their own. |
coleenp@4037 | 383 | if (_metaspace == NULL) { |
coleenp@4037 | 384 | MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); |
coleenp@4037 | 385 | // Check again if metaspace has been allocated while we were getting this lock. |
coleenp@4037 | 386 | if (_metaspace != NULL) { |
coleenp@4037 | 387 | return _metaspace; |
coleenp@4037 | 388 | } |
coleenp@4304 | 389 | if (this == the_null_class_loader_data()) { |
coleenp@4304 | 390 | assert (class_loader() == NULL, "Must be"); |
jmasa@4382 | 391 | set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType)); |
jmasa@4382 | 392 | } else if (is_anonymous()) { |
jmasa@4382 | 393 | if (TraceClassLoaderData && Verbose && class_loader() != NULL) { |
jmasa@4382 | 394 | tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name()); |
jmasa@4382 | 395 | } |
jmasa@4382 | 396 | set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType)); |
jmasa@4382 | 397 | } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) { |
jmasa@4382 | 398 | if (TraceClassLoaderData && Verbose && class_loader() != NULL) { |
jmasa@4382 | 399 | tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name()); |
jmasa@4382 | 400 | } |
jmasa@4382 | 401 | set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType)); |
coleenp@4037 | 402 | } else { |
jmasa@4382 | 403 | set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType)); |
coleenp@4037 | 404 | } |
coleenp@4037 | 405 | } |
coleenp@4037 | 406 | return _metaspace; |
coleenp@4037 | 407 | } |
coleenp@4037 | 408 | |
coleenp@4037 | 409 | JNIHandleBlock* ClassLoaderData::handles() const { return _handles; } |
coleenp@4037 | 410 | void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; } |
coleenp@4037 | 411 | |
coleenp@4037 | 412 | jobject ClassLoaderData::add_handle(Handle h) { |
coleenp@4037 | 413 | MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); |
coleenp@4037 | 414 | if (handles() == NULL) { |
coleenp@4037 | 415 | set_handles(JNIHandleBlock::allocate_block()); |
coleenp@4037 | 416 | } |
coleenp@4037 | 417 | return handles()->allocate_handle(h()); |
coleenp@4037 | 418 | } |
coleenp@4037 | 419 | |
coleenp@4037 | 420 | // Add this metadata pointer to be freed when it's safe. This is only during |
coleenp@4037 | 421 | // class unloading because Handles might point to this metadata field. |
coleenp@4037 | 422 | void ClassLoaderData::add_to_deallocate_list(Metadata* m) { |
coleenp@4037 | 423 | // Metadata in shared region isn't deleted. |
coleenp@4037 | 424 | if (!m->is_shared()) { |
coleenp@4037 | 425 | MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); |
coleenp@4037 | 426 | if (_deallocate_list == NULL) { |
coleenp@4037 | 427 | _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true); |
coleenp@4037 | 428 | } |
coleenp@4037 | 429 | _deallocate_list->append_if_missing(m); |
coleenp@4037 | 430 | } |
coleenp@4037 | 431 | } |
coleenp@4037 | 432 | |
coleenp@4037 | 433 | // Deallocate free metadata on the free list. How useful the PermGen was! |
coleenp@4037 | 434 | void ClassLoaderData::free_deallocate_list() { |
coleenp@4037 | 435 | // Don't need lock, at safepoint |
coleenp@4037 | 436 | assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); |
coleenp@4037 | 437 | if (_deallocate_list == NULL) { |
coleenp@4037 | 438 | return; |
coleenp@4037 | 439 | } |
coleenp@4037 | 440 | // Go backwards because this removes entries that are freed. |
coleenp@4037 | 441 | for (int i = _deallocate_list->length() - 1; i >= 0; i--) { |
coleenp@4037 | 442 | Metadata* m = _deallocate_list->at(i); |
coleenp@4037 | 443 | if (!m->on_stack()) { |
coleenp@4037 | 444 | _deallocate_list->remove_at(i); |
coleenp@4037 | 445 | // There are only three types of metadata that we deallocate directly. |
coleenp@4037 | 446 | // Cast them so they can be used by the template function. |
coleenp@4037 | 447 | if (m->is_method()) { |
coleenp@4037 | 448 | MetadataFactory::free_metadata(this, (Method*)m); |
coleenp@4037 | 449 | } else if (m->is_constantPool()) { |
coleenp@4037 | 450 | MetadataFactory::free_metadata(this, (ConstantPool*)m); |
coleenp@4037 | 451 | } else if (m->is_klass()) { |
coleenp@4037 | 452 | MetadataFactory::free_metadata(this, (InstanceKlass*)m); |
coleenp@4037 | 453 | } else { |
coleenp@4037 | 454 | ShouldNotReachHere(); |
coleenp@4037 | 455 | } |
coleenp@4037 | 456 | } |
coleenp@4037 | 457 | } |
coleenp@4037 | 458 | } |
coleenp@4037 | 459 | |
coleenp@4304 | 460 | // These anonymous class loaders are to contain classes used for JSR292 |
coleenp@4304 | 461 | ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) { |
coleenp@4304 | 462 | // Add a new class loader data to the graph. |
mgerdin@5013 | 463 | return ClassLoaderDataGraph::add(loader, true, CHECK_NULL); |
coleenp@4037 | 464 | } |
coleenp@4037 | 465 | |
coleenp@4304 | 466 | const char* ClassLoaderData::loader_name() { |
coleenp@4304 | 467 | // Handles null class loader |
coleenp@4304 | 468 | return SystemDictionary::loader_name(class_loader()); |
coleenp@4304 | 469 | } |
coleenp@4304 | 470 | |
coleenp@4304 | 471 | #ifndef PRODUCT |
coleenp@4037 | 472 | // Define to dump klasses |
coleenp@4037 | 473 | #undef CLD_DUMP_KLASSES |
coleenp@4037 | 474 | |
coleenp@4037 | 475 | void ClassLoaderData::dump(outputStream * const out) { |
coleenp@4037 | 476 | ResourceMark rm; |
coleenp@4037 | 477 | out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {", |
drchase@6680 | 478 | p2i(this), p2i((void *)class_loader()), |
drchase@6680 | 479 | p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name()); |
coleenp@4037 | 480 | if (claimed()) out->print(" claimed "); |
coleenp@4037 | 481 | if (is_unloading()) out->print(" unloading "); |
drchase@6680 | 482 | out->print(" handles " INTPTR_FORMAT, p2i(handles())); |
coleenp@4037 | 483 | out->cr(); |
coleenp@4037 | 484 | if (metaspace_or_null() != NULL) { |
drchase@6680 | 485 | out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null())); |
coleenp@4037 | 486 | metaspace_or_null()->dump(out); |
coleenp@4037 | 487 | } else { |
coleenp@4037 | 488 | out->print_cr("metaspace: NULL"); |
coleenp@4037 | 489 | } |
coleenp@4037 | 490 | |
coleenp@4037 | 491 | #ifdef CLD_DUMP_KLASSES |
coleenp@4037 | 492 | if (Verbose) { |
coleenp@4037 | 493 | ResourceMark rm; |
coleenp@4037 | 494 | Klass* k = _klasses; |
coleenp@4037 | 495 | while (k != NULL) { |
coleenp@4037 | 496 | out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(), |
coleenp@4037 | 497 | k->has_modified_oops(), k->has_accumulated_modified_oops()); |
coleenp@4751 | 498 | assert(k != k->next_link(), "no loops!"); |
coleenp@4037 | 499 | k = k->next_link(); |
coleenp@4037 | 500 | } |
coleenp@4037 | 501 | } |
coleenp@4037 | 502 | #endif // CLD_DUMP_KLASSES |
coleenp@4037 | 503 | #undef CLD_DUMP_KLASSES |
coleenp@4037 | 504 | if (_jmethod_ids != NULL) { |
coleenp@4037 | 505 | Method::print_jmethod_ids(this, out); |
coleenp@4037 | 506 | } |
coleenp@4037 | 507 | out->print_cr("}"); |
coleenp@4037 | 508 | } |
coleenp@4037 | 509 | #endif // PRODUCT |
coleenp@4037 | 510 | |
coleenp@4037 | 511 | void ClassLoaderData::verify() { |
coleenp@4037 | 512 | oop cl = class_loader(); |
coleenp@4037 | 513 | |
coleenp@4304 | 514 | guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same"); |
coleenp@4304 | 515 | guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be"); |
coleenp@4037 | 516 | |
coleenp@4037 | 517 | // Verify the integrity of the allocated space. |
coleenp@4037 | 518 | if (metaspace_or_null() != NULL) { |
coleenp@4037 | 519 | metaspace_or_null()->verify(); |
coleenp@4037 | 520 | } |
coleenp@4037 | 521 | |
coleenp@4037 | 522 | for (Klass* k = _klasses; k != NULL; k = k->next_link()) { |
coleenp@4037 | 523 | guarantee(k->class_loader_data() == this, "Must be the same"); |
coleenp@4037 | 524 | k->verify(); |
coleenp@4751 | 525 | assert(k != k->next_link(), "no loops!"); |
coleenp@4037 | 526 | } |
coleenp@4037 | 527 | } |
coleenp@4037 | 528 | |
coleenp@6316 | 529 | bool ClassLoaderData::contains_klass(Klass* klass) { |
coleenp@6316 | 530 | for (Klass* k = _klasses; k != NULL; k = k->next_link()) { |
coleenp@6316 | 531 | if (k == klass) return true; |
coleenp@6316 | 532 | } |
coleenp@6316 | 533 | return false; |
coleenp@6316 | 534 | } |
coleenp@6316 | 535 | |
coleenp@4304 | 536 | |
coleenp@4037 | 537 | // GC root of class loader data created. |
coleenp@4037 | 538 | ClassLoaderData* ClassLoaderDataGraph::_head = NULL; |
coleenp@4037 | 539 | ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL; |
mgronlun@6742 | 540 | ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL; |
coleenp@4037 | 541 | ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL; |
coleenp@4037 | 542 | |
coleenp@6678 | 543 | bool ClassLoaderDataGraph::_should_purge = false; |
coleenp@6678 | 544 | |
coleenp@4037 | 545 | // Add a new class loader data node to the list. Assign the newly created |
coleenp@4037 | 546 | // ClassLoaderData into the java/lang/ClassLoader object as a hidden field |
mgerdin@5013 | 547 | ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) { |
mgerdin@5016 | 548 | // We need to allocate all the oops for the ClassLoaderData before allocating the |
mgerdin@5016 | 549 | // actual ClassLoaderData object. |
mgerdin@5016 | 550 | ClassLoaderData::Dependencies dependencies(CHECK_NULL); |
coleenp@4345 | 551 | |
mgerdin@5016 | 552 | No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the |
mgerdin@5016 | 553 | // ClassLoaderData in the graph since the CLD |
mgerdin@5016 | 554 | // contains unhandled oops |
coleenp@4037 | 555 | |
mgerdin@5016 | 556 | ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies); |
mgerdin@5016 | 557 | |
mgerdin@4903 | 558 | |
mgerdin@5013 | 559 | if (!is_anonymous) { |
mgerdin@5013 | 560 | ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader()); |
coleenp@4304 | 561 | // First, Atomically set it |
coleenp@4304 | 562 | ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL); |
coleenp@4304 | 563 | if (old != NULL) { |
coleenp@4304 | 564 | delete cld; |
coleenp@4304 | 565 | // Returns the data. |
coleenp@4304 | 566 | return old; |
coleenp@4304 | 567 | } |
coleenp@4037 | 568 | } |
coleenp@4037 | 569 | |
coleenp@4037 | 570 | // We won the race, and therefore the task of adding the data to the list of |
coleenp@4037 | 571 | // class loader data |
mgerdin@5013 | 572 | ClassLoaderData** list_head = &_head; |
mgerdin@5013 | 573 | ClassLoaderData* next = _head; |
mgerdin@5013 | 574 | |
coleenp@4037 | 575 | do { |
coleenp@4037 | 576 | cld->set_next(next); |
coleenp@4037 | 577 | ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next); |
coleenp@4037 | 578 | if (exchanged == next) { |
coleenp@4037 | 579 | if (TraceClassLoaderData) { |
coleenp@4304 | 580 | ResourceMark rm; |
coleenp@4037 | 581 | tty->print("[ClassLoaderData: "); |
drchase@6680 | 582 | tty->print("create class loader data " INTPTR_FORMAT, p2i(cld)); |
drchase@6680 | 583 | tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()), |
coleenp@4304 | 584 | cld->loader_name()); |
coleenp@4037 | 585 | tty->print_cr("]"); |
coleenp@4037 | 586 | } |
coleenp@4037 | 587 | return cld; |
coleenp@4037 | 588 | } |
coleenp@4037 | 589 | next = exchanged; |
coleenp@4037 | 590 | } while (true); |
coleenp@4304 | 591 | |
coleenp@4037 | 592 | } |
coleenp@4037 | 593 | |
coleenp@4037 | 594 | void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { |
coleenp@4037 | 595 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
coleenp@4037 | 596 | cld->oops_do(f, klass_closure, must_claim); |
coleenp@4037 | 597 | } |
coleenp@4037 | 598 | } |
coleenp@4037 | 599 | |
coleenp@4304 | 600 | void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { |
coleenp@4304 | 601 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
coleenp@4304 | 602 | if (cld->keep_alive()) { |
coleenp@4304 | 603 | cld->oops_do(f, klass_closure, must_claim); |
coleenp@4304 | 604 | } |
coleenp@4304 | 605 | } |
coleenp@4304 | 606 | } |
coleenp@4304 | 607 | |
coleenp@4037 | 608 | void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { |
coleenp@4037 | 609 | if (ClassUnloading) { |
stefank@6992 | 610 | keep_alive_oops_do(f, klass_closure, must_claim); |
coleenp@4037 | 611 | } else { |
stefank@6992 | 612 | oops_do(f, klass_closure, must_claim); |
stefank@6992 | 613 | } |
stefank@6992 | 614 | } |
stefank@6992 | 615 | |
stefank@6992 | 616 | void ClassLoaderDataGraph::cld_do(CLDClosure* cl) { |
stefank@6992 | 617 | for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) { |
stefank@6992 | 618 | cl->do_cld(cld); |
stefank@6992 | 619 | } |
stefank@6992 | 620 | } |
stefank@6992 | 621 | |
stefank@6992 | 622 | void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) { |
stefank@6992 | 623 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) { |
stefank@6992 | 624 | CLDClosure* closure = cld->keep_alive() ? strong : weak; |
stefank@6992 | 625 | if (closure != NULL) { |
stefank@6992 | 626 | closure->do_cld(cld); |
stefank@6992 | 627 | } |
stefank@6992 | 628 | } |
stefank@6992 | 629 | } |
stefank@6992 | 630 | |
stefank@6992 | 631 | void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) { |
stefank@6992 | 632 | roots_cld_do(cl, NULL); |
stefank@6992 | 633 | } |
stefank@6992 | 634 | |
stefank@6992 | 635 | void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) { |
stefank@6992 | 636 | if (ClassUnloading) { |
stefank@6992 | 637 | keep_alive_cld_do(cl); |
stefank@6992 | 638 | } else { |
stefank@6992 | 639 | cld_do(cl); |
coleenp@4037 | 640 | } |
coleenp@4037 | 641 | } |
coleenp@4037 | 642 | |
coleenp@4037 | 643 | void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) { |
coleenp@4037 | 644 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
coleenp@4037 | 645 | cld->classes_do(klass_closure); |
coleenp@4037 | 646 | } |
coleenp@4037 | 647 | } |
coleenp@4037 | 648 | |
sla@5237 | 649 | void ClassLoaderDataGraph::classes_do(void f(Klass* const)) { |
sla@5237 | 650 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
sla@5237 | 651 | cld->classes_do(f); |
sla@5237 | 652 | } |
sla@5237 | 653 | } |
sla@5237 | 654 | |
farvidsson@6024 | 655 | void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) { |
farvidsson@6024 | 656 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
farvidsson@6024 | 657 | cld->loaded_classes_do(klass_closure); |
farvidsson@6024 | 658 | } |
farvidsson@6024 | 659 | } |
farvidsson@6024 | 660 | |
sla@5237 | 661 | void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) { |
sla@5237 | 662 | assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); |
mgronlun@6742 | 663 | // Only walk the head until any clds not purged from prior unloading |
mgronlun@6742 | 664 | // (CMS doesn't purge right away). |
mgronlun@6742 | 665 | for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { |
sla@5237 | 666 | cld->classes_do(f); |
sla@5237 | 667 | } |
sla@5237 | 668 | } |
sla@5237 | 669 | |
coleenp@4037 | 670 | GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() { |
coleenp@4037 | 671 | assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?"); |
coleenp@4037 | 672 | |
coleenp@4037 | 673 | GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>(); |
coleenp@4037 | 674 | |
coleenp@4037 | 675 | // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true); |
coleenp@4037 | 676 | ClassLoaderData* curr = _head; |
coleenp@4037 | 677 | while (curr != _saved_head) { |
coleenp@4037 | 678 | if (!curr->claimed()) { |
coleenp@4037 | 679 | array->push(curr); |
coleenp@4037 | 680 | |
coleenp@4037 | 681 | if (TraceClassLoaderData) { |
coleenp@4037 | 682 | tty->print("[ClassLoaderData] found new CLD: "); |
coleenp@4037 | 683 | curr->print_value_on(tty); |
coleenp@4037 | 684 | tty->cr(); |
coleenp@4037 | 685 | } |
coleenp@4037 | 686 | } |
coleenp@4037 | 687 | |
coleenp@4037 | 688 | curr = curr->_next; |
coleenp@4037 | 689 | } |
coleenp@4037 | 690 | |
coleenp@4037 | 691 | return array; |
coleenp@4037 | 692 | } |
coleenp@4037 | 693 | |
stefank@6992 | 694 | bool ClassLoaderDataGraph::unload_list_contains(const void* x) { |
stefank@6992 | 695 | assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint"); |
stefank@6992 | 696 | for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) { |
stefank@6992 | 697 | if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) { |
stefank@6992 | 698 | return true; |
stefank@6992 | 699 | } |
stefank@6992 | 700 | } |
stefank@6992 | 701 | return false; |
stefank@6992 | 702 | } |
stefank@6992 | 703 | |
coleenp@6305 | 704 | #ifndef PRODUCT |
coleenp@4037 | 705 | bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { |
coleenp@4037 | 706 | for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { |
coleenp@4037 | 707 | if (loader_data == data) { |
coleenp@4037 | 708 | return true; |
coleenp@4037 | 709 | } |
coleenp@4037 | 710 | } |
coleenp@4037 | 711 | |
coleenp@4037 | 712 | return false; |
coleenp@4037 | 713 | } |
coleenp@4037 | 714 | #endif // PRODUCT |
coleenp@4037 | 715 | |
coleenp@4304 | 716 | |
coleenp@4037 | 717 | // Move class loader data from main list to the unloaded list for unloading |
coleenp@4037 | 718 | // and deallocation later. |
stefank@7333 | 719 | bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, bool clean_alive) { |
coleenp@4037 | 720 | ClassLoaderData* data = _head; |
coleenp@4037 | 721 | ClassLoaderData* prev = NULL; |
coleenp@4037 | 722 | bool seen_dead_loader = false; |
mgronlun@6742 | 723 | |
mgronlun@6742 | 724 | // Save previous _unloading pointer for CMS which may add to unloading list before |
mgronlun@6742 | 725 | // purging and we don't want to rewalk the previously unloaded class loader data. |
mgronlun@6742 | 726 | _saved_unloading = _unloading; |
mgronlun@6742 | 727 | |
coleenp@4037 | 728 | while (data != NULL) { |
stefank@6974 | 729 | if (data->is_alive(is_alive_closure)) { |
coleenp@4037 | 730 | prev = data; |
coleenp@4037 | 731 | data = data->next(); |
coleenp@4037 | 732 | continue; |
coleenp@4037 | 733 | } |
coleenp@4037 | 734 | seen_dead_loader = true; |
coleenp@4037 | 735 | ClassLoaderData* dead = data; |
coleenp@4304 | 736 | dead->unload(); |
coleenp@4037 | 737 | data = data->next(); |
coleenp@4037 | 738 | // Remove from loader list. |
jmasa@5015 | 739 | // This class loader data will no longer be found |
jmasa@5015 | 740 | // in the ClassLoaderDataGraph. |
coleenp@4037 | 741 | if (prev != NULL) { |
coleenp@4037 | 742 | prev->set_next(data); |
coleenp@4037 | 743 | } else { |
coleenp@4037 | 744 | assert(dead == _head, "sanity check"); |
coleenp@4037 | 745 | _head = data; |
coleenp@4037 | 746 | } |
coleenp@4037 | 747 | dead->set_next(_unloading); |
coleenp@4037 | 748 | _unloading = dead; |
coleenp@4037 | 749 | } |
sla@5237 | 750 | |
stefank@7333 | 751 | if (clean_alive) { |
stefank@7333 | 752 | // Clean previous versions and the deallocate list. |
stefank@7333 | 753 | ClassLoaderDataGraph::clean_metaspaces(); |
stefank@7333 | 754 | } |
stefank@7333 | 755 | |
sla@5237 | 756 | if (seen_dead_loader) { |
sla@5237 | 757 | post_class_unload_events(); |
sla@5237 | 758 | } |
sla@5237 | 759 | |
coleenp@4037 | 760 | return seen_dead_loader; |
coleenp@4037 | 761 | } |
coleenp@4037 | 762 | |
stefank@7333 | 763 | void ClassLoaderDataGraph::clean_metaspaces() { |
stefank@7333 | 764 | // mark metadata seen on the stack and code cache so we can delete unneeded entries. |
stefank@7333 | 765 | bool has_redefined_a_class = JvmtiExport::has_redefined_a_class(); |
stefank@7333 | 766 | MetadataOnStackMark md_on_stack(has_redefined_a_class); |
stefank@7333 | 767 | |
stefank@7333 | 768 | if (has_redefined_a_class) { |
stefank@7333 | 769 | // purge_previous_versions also cleans weak method links. Because |
stefank@7333 | 770 | // one method's MDO can reference another method from another |
stefank@7333 | 771 | // class loader, we need to first clean weak method links for all |
stefank@7333 | 772 | // class loaders here. Below, we can then free redefined methods |
stefank@7333 | 773 | // for all class loaders. |
stefank@7333 | 774 | for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { |
stefank@7333 | 775 | data->classes_do(InstanceKlass::purge_previous_versions); |
stefank@7333 | 776 | } |
stefank@7333 | 777 | } |
stefank@7333 | 778 | |
stefank@7333 | 779 | // Need to purge the previous version before deallocating. |
stefank@7333 | 780 | free_deallocate_lists(); |
stefank@7333 | 781 | } |
stefank@7333 | 782 | |
coleenp@4037 | 783 | void ClassLoaderDataGraph::purge() { |
coleenp@6678 | 784 | assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); |
coleenp@4037 | 785 | ClassLoaderData* list = _unloading; |
coleenp@4037 | 786 | _unloading = NULL; |
coleenp@4037 | 787 | ClassLoaderData* next = list; |
coleenp@4037 | 788 | while (next != NULL) { |
coleenp@4037 | 789 | ClassLoaderData* purge_me = next; |
coleenp@4037 | 790 | next = purge_me->next(); |
coleenp@4037 | 791 | delete purge_me; |
coleenp@4037 | 792 | } |
jmasa@5007 | 793 | Metaspace::purge(); |
coleenp@4037 | 794 | } |
coleenp@4037 | 795 | |
sla@5237 | 796 | void ClassLoaderDataGraph::post_class_unload_events(void) { |
sla@5237 | 797 | #if INCLUDE_TRACE |
sla@5237 | 798 | assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); |
sla@5237 | 799 | if (Tracing::enabled()) { |
sla@5237 | 800 | if (Tracing::is_event_enabled(TraceClassUnloadEvent)) { |
sla@5237 | 801 | assert(_unloading != NULL, "need class loader data unload list!"); |
mgronlun@6131 | 802 | _class_unload_time = Ticks::now(); |
sla@5237 | 803 | classes_unloading_do(&class_unload_event); |
sla@5237 | 804 | } |
sla@5237 | 805 | Tracing::on_unloading_classes(); |
sla@5237 | 806 | } |
sla@5237 | 807 | #endif |
sla@5237 | 808 | } |
sla@5237 | 809 | |
stefank@7333 | 810 | void ClassLoaderDataGraph::free_deallocate_lists() { |
stefank@7333 | 811 | for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { |
stefank@7333 | 812 | // We need to keep this data until InstanceKlass::purge_previous_version has been |
stefank@7333 | 813 | // called on all alive classes. See the comment in ClassLoaderDataGraph::clean_metaspaces. |
stefank@7333 | 814 | cld->free_deallocate_list(); |
stefank@7333 | 815 | } |
stefank@7333 | 816 | } |
stefank@7333 | 817 | |
coleenp@4037 | 818 | // CDS support |
coleenp@4037 | 819 | |
coleenp@4037 | 820 | // Global metaspaces for writing information to the shared archive. When |
coleenp@4037 | 821 | // application CDS is supported, we may need one per metaspace, so this |
coleenp@4037 | 822 | // sort of looks like it. |
coleenp@4037 | 823 | Metaspace* ClassLoaderData::_ro_metaspace = NULL; |
coleenp@4037 | 824 | Metaspace* ClassLoaderData::_rw_metaspace = NULL; |
coleenp@4037 | 825 | static bool _shared_metaspaces_initialized = false; |
coleenp@4037 | 826 | |
coleenp@4037 | 827 | // Initialize shared metaspaces (change to call from somewhere not lazily) |
coleenp@4037 | 828 | void ClassLoaderData::initialize_shared_metaspaces() { |
coleenp@4037 | 829 | assert(DumpSharedSpaces, "only use this for dumping shared spaces"); |
coleenp@4037 | 830 | assert(this == ClassLoaderData::the_null_class_loader_data(), |
coleenp@4037 | 831 | "only supported for null loader data for now"); |
coleenp@4037 | 832 | assert (!_shared_metaspaces_initialized, "only initialize once"); |
coleenp@4037 | 833 | MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); |
jmasa@4382 | 834 | _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType); |
jmasa@4382 | 835 | _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType); |
coleenp@4037 | 836 | _shared_metaspaces_initialized = true; |
coleenp@4037 | 837 | } |
coleenp@4037 | 838 | |
coleenp@4037 | 839 | Metaspace* ClassLoaderData::ro_metaspace() { |
coleenp@4037 | 840 | assert(_ro_metaspace != NULL, "should already be initialized"); |
coleenp@4037 | 841 | return _ro_metaspace; |
coleenp@4037 | 842 | } |
coleenp@4037 | 843 | |
coleenp@4037 | 844 | Metaspace* ClassLoaderData::rw_metaspace() { |
coleenp@4037 | 845 | assert(_rw_metaspace != NULL, "should already be initialized"); |
coleenp@4037 | 846 | return _rw_metaspace; |
coleenp@4037 | 847 | } |
coleenp@4037 | 848 | |
stefank@6992 | 849 | ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic() |
stefank@6992 | 850 | : _next_klass(NULL) { |
stefank@6992 | 851 | ClassLoaderData* cld = ClassLoaderDataGraph::_head; |
stefank@6992 | 852 | Klass* klass = NULL; |
stefank@6992 | 853 | |
stefank@6992 | 854 | // Find the first klass in the CLDG. |
stefank@6992 | 855 | while (cld != NULL) { |
stefank@6992 | 856 | klass = cld->_klasses; |
stefank@6992 | 857 | if (klass != NULL) { |
stefank@6992 | 858 | _next_klass = klass; |
stefank@6992 | 859 | return; |
stefank@6992 | 860 | } |
stefank@6992 | 861 | cld = cld->next(); |
stefank@6992 | 862 | } |
stefank@6992 | 863 | } |
stefank@6992 | 864 | |
stefank@6992 | 865 | Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) { |
stefank@6992 | 866 | Klass* next = klass->next_link(); |
stefank@6992 | 867 | if (next != NULL) { |
stefank@6992 | 868 | return next; |
stefank@6992 | 869 | } |
stefank@6992 | 870 | |
stefank@6992 | 871 | // No more klasses in the current CLD. Time to find a new CLD. |
stefank@6992 | 872 | ClassLoaderData* cld = klass->class_loader_data(); |
stefank@6992 | 873 | while (next == NULL) { |
stefank@6992 | 874 | cld = cld->next(); |
stefank@6992 | 875 | if (cld == NULL) { |
stefank@6992 | 876 | break; |
stefank@6992 | 877 | } |
stefank@6992 | 878 | next = cld->_klasses; |
stefank@6992 | 879 | } |
stefank@6992 | 880 | |
stefank@6992 | 881 | return next; |
stefank@6992 | 882 | } |
stefank@6992 | 883 | |
stefank@6992 | 884 | Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() { |
asiebenborn@7765 | 885 | Klass* head = _next_klass; |
stefank@6992 | 886 | |
stefank@6992 | 887 | while (head != NULL) { |
stefank@6992 | 888 | Klass* next = next_klass_in_cldg(head); |
stefank@6992 | 889 | |
stefank@6992 | 890 | Klass* old_head = (Klass*)Atomic::cmpxchg_ptr(next, &_next_klass, head); |
stefank@6992 | 891 | |
stefank@6992 | 892 | if (old_head == head) { |
stefank@6992 | 893 | return head; // Won the CAS. |
stefank@6992 | 894 | } |
stefank@6992 | 895 | |
stefank@6992 | 896 | head = old_head; |
stefank@6992 | 897 | } |
stefank@6992 | 898 | |
stefank@6992 | 899 | // Nothing more for the iterator to hand out. |
stefank@6992 | 900 | assert(head == NULL, err_msg("head is " PTR_FORMAT ", expected not null:", p2i(head))); |
stefank@6992 | 901 | return NULL; |
stefank@6992 | 902 | } |
coleenp@4037 | 903 | |
coleenp@4037 | 904 | ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() { |
coleenp@4037 | 905 | _data = ClassLoaderDataGraph::_head; |
coleenp@4037 | 906 | } |
coleenp@4037 | 907 | |
coleenp@4037 | 908 | ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {} |
coleenp@4037 | 909 | |
coleenp@4037 | 910 | #ifndef PRODUCT |
coleenp@4037 | 911 | // callable from debugger |
coleenp@4037 | 912 | extern "C" int print_loader_data_graph() { |
coleenp@4037 | 913 | ClassLoaderDataGraph::dump_on(tty); |
coleenp@4037 | 914 | return 0; |
coleenp@4037 | 915 | } |
coleenp@4037 | 916 | |
coleenp@4037 | 917 | void ClassLoaderDataGraph::verify() { |
coleenp@4037 | 918 | for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { |
coleenp@4037 | 919 | data->verify(); |
coleenp@4037 | 920 | } |
coleenp@4037 | 921 | } |
coleenp@4037 | 922 | |
coleenp@4037 | 923 | void ClassLoaderDataGraph::dump_on(outputStream * const out) { |
coleenp@4037 | 924 | for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { |
coleenp@4037 | 925 | data->dump(out); |
coleenp@4037 | 926 | } |
coleenp@4037 | 927 | MetaspaceAux::dump(out); |
coleenp@4037 | 928 | } |
acorn@4497 | 929 | #endif // PRODUCT |
coleenp@4037 | 930 | |
coleenp@4037 | 931 | void ClassLoaderData::print_value_on(outputStream* out) const { |
coleenp@4037 | 932 | if (class_loader() == NULL) { |
acorn@4497 | 933 | out->print("NULL class_loader"); |
coleenp@4037 | 934 | } else { |
drchase@6680 | 935 | out->print("class loader " INTPTR_FORMAT, p2i(this)); |
coleenp@4037 | 936 | class_loader()->print_value_on(out); |
coleenp@4037 | 937 | } |
coleenp@4037 | 938 | } |
sla@5237 | 939 | |
sla@5237 | 940 | #if INCLUDE_TRACE |
sla@5237 | 941 | |
mgronlun@6131 | 942 | Ticks ClassLoaderDataGraph::_class_unload_time; |
sla@5237 | 943 | |
sla@5237 | 944 | void ClassLoaderDataGraph::class_unload_event(Klass* const k) { |
sla@5237 | 945 | |
sla@5237 | 946 | // post class unload event |
sla@5237 | 947 | EventClassUnload event(UNTIMED); |
sla@5237 | 948 | event.set_endtime(_class_unload_time); |
sla@5237 | 949 | event.set_unloadedClass(k); |
sla@5237 | 950 | oop defining_class_loader = k->class_loader(); |
sla@5237 | 951 | event.set_definingClassLoader(defining_class_loader != NULL ? |
sla@5237 | 952 | defining_class_loader->klass() : (Klass*)NULL); |
sla@5237 | 953 | event.commit(); |
sla@5237 | 954 | } |
sla@5237 | 955 | |
mgronlun@7367 | 956 | #endif // INCLUDE_TRACE |