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