annotate src/hotspot/share/classfile/classLoaderData.cpp @ 51393:00c4edaf2017

8205509: assert(_name_and_id != 0LL) failed: encountered a class loader null name and id Summary: The method loader_name_and_id should return the external name of the class loader if _name_and_id is null. Reviewed-by: coleenp, stuefe
author lfoltan
date Mon, 25 Jun 2018 10:34:46 -0400
parents c349d409262a
children 45c1fde86050
rev   line source
coleenp@46729 1 /*
coleenp@49195 2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
coleenp@13728 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@13728 4 *
coleenp@13728 5 * This code is free software; you can redistribute it and/or modify it
coleenp@13728 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@13728 7 * published by the Free Software Foundation.
coleenp@13728 8 *
coleenp@13728 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@13728 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@13728 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@13728 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@13728 13 * accompanied this code).
coleenp@13728 14 *
coleenp@13728 15 * You should have received a copy of the GNU General Public License version
coleenp@13728 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@13728 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@13728 18 *
coleenp@13728 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@13728 20 * or visit www.oracle.com if you need additional information or have any
coleenp@13728 21 * questions.
coleenp@13728 22 *
coleenp@13728 23 */
coleenp@13728 24
coleenp@13728 25 // A ClassLoaderData identifies the full set of class types that a class
coleenp@13728 26 // loader's name resolution strategy produces for a given configuration of the
coleenp@13728 27 // class loader.
coleenp@13728 28 // Class types in the ClassLoaderData may be defined by from class file binaries
coleenp@13728 29 // provided by the class loader, or from other class loader it interacts with
coleenp@13728 30 // according to its name resolution strategy.
coleenp@13728 31 //
coleenp@13728 32 // Class loaders that implement a deterministic name resolution strategy
coleenp@13728 33 // (including with respect to their delegation behavior), such as the boot, the
alanb@36508 34 // platform, and the system loaders of the JDK's built-in class loader
coleenp@13728 35 // hierarchy, always produce the same linkset for a given configuration.
coleenp@13728 36 //
coleenp@13728 37 // ClassLoaderData carries information related to a linkset (e.g.,
coleenp@13728 38 // metaspace holding its klass definitions).
coleenp@13728 39 // The System Dictionary and related data structures (e.g., placeholder table,
coleenp@13728 40 // loader constraints table) as well as the runtime representation of classes
coleenp@13728 41 // only reference ClassLoaderData.
coleenp@13728 42 //
coleenp@13728 43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
coleenp@13728 44 // that represent the loader's "linking domain" in the JVM.
coleenp@13728 45 //
coleenp@13728 46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
coleenp@13728 47 // the singleton class the_null_class_loader_data().
coleenp@13728 48
coleenp@13728 49 #include "precompiled.hpp"
coleenp@13728 50 #include "classfile/classLoaderData.hpp"
coleenp@13728 51 #include "classfile/classLoaderData.inline.hpp"
coleenp@46729 52 #include "classfile/dictionary.hpp"
coleenp@13728 53 #include "classfile/javaClasses.hpp"
coleenp@15430 54 #include "classfile/metadataOnStackMark.hpp"
alanb@36508 55 #include "classfile/moduleEntry.hpp"
alanb@36508 56 #include "classfile/packageEntry.hpp"
coleenp@50319 57 #include "classfile/symbolTable.hpp"
coleenp@13728 58 #include "classfile/systemDictionary.hpp"
mockner@35917 59 #include "logging/log.hpp"
stuefe@46701 60 #include "logging/logStream.hpp"
stefank@49825 61 #include "memory/allocation.inline.hpp"
coleenp@13728 62 #include "memory/metadataFactory.hpp"
coleenp@13728 63 #include "memory/metaspaceShared.hpp"
jprovino@37248 64 #include "memory/resourceArea.hpp"
coleenp@50233 65 #include "memory/universe.hpp"
eosterlund@48933 66 #include "oops/access.inline.hpp"
stefank@29081 67 #include "oops/oop.inline.hpp"
coleenp@50311 68 #include "oops/oopHandle.inline.hpp"
coleenp@50233 69 #include "oops/weakHandle.inline.hpp"
dholmes@40655 70 #include "runtime/atomic.hpp"
simonis@49858 71 #include "runtime/handles.inline.hpp"
coleenp@13728 72 #include "runtime/mutex.hpp"
dholmes@38937 73 #include "runtime/orderAccess.hpp"
coleenp@13728 74 #include "runtime/safepoint.hpp"
stefank@50089 75 #include "runtime/safepointVerifiers.hpp"
coleenp@13728 76 #include "utilities/growableArray.hpp"
mgronlun@21767 77 #include "utilities/macros.hpp"
coleenp@13728 78 #include "utilities/ostream.hpp"
egahlin@50662 79 #include "utilities/ticks.hpp"
egahlin@50662 80 #if INCLUDE_JFR
egahlin@50662 81 #include "jfr/jfr.hpp"
egahlin@50662 82 #include "jfr/jfrEvents.hpp"
sla@18025 83 #endif
sla@18025 84
zgu@49268 85 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0;
zgu@49268 86 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0;
zgu@49268 87
coleenp@13728 88 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
coleenp@13728 89
coleenp@49813 90 void ClassLoaderData::init_null_class_loader_data() {
coleenp@49813 91 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
coleenp@49813 92 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
coleenp@49813 93
coleenp@49813 94 _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
coleenp@49813 95 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
coleenp@49813 96 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
coleenp@49813 97
coleenp@49813 98 LogTarget(Debug, class, loader, data) lt;
coleenp@49813 99 if (lt.is_enabled()) {
coleenp@49813 100 ResourceMark rm;
coleenp@49813 101 LogStream ls(lt);
coleenp@49813 102 ls.print("create ");
coleenp@49813 103 _the_null_class_loader_data->print_value_on(&ls);
coleenp@49813 104 ls.cr();
coleenp@49813 105 }
coleenp@49813 106 }
coleenp@49813 107
lfoltan@51268 108 // Obtain and set the class loader's name within the ClassLoaderData so
lfoltan@51268 109 // it will be available for error messages, logging, JFR, etc. The name
lfoltan@51268 110 // and klass are available after the class_loader oop is no longer alive,
lfoltan@51268 111 // during unloading.
lfoltan@51393 112 void ClassLoaderData::initialize_name(Handle class_loader) {
lfoltan@51268 113 Thread* THREAD = Thread::current();
lfoltan@51268 114 ResourceMark rm(THREAD);
coleenp@50319 115
lfoltan@51268 116 // Obtain the class loader's name. If the class loader's name was not
lfoltan@51268 117 // explicitly set during construction, the CLD's _name field will be null.
lfoltan@51268 118 oop cl_name = java_lang_ClassLoader::name(class_loader());
lfoltan@51268 119 if (cl_name != NULL) {
lfoltan@51268 120 const char* cl_instance_name = java_lang_String::as_utf8_string(cl_name);
lfoltan@51268 121
lfoltan@51268 122 if (cl_instance_name != NULL && cl_instance_name[0] != '\0') {
coleenp@50319 123 // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
lfoltan@51268 124 _name = SymbolTable::new_symbol(cl_instance_name, CATCH);
coleenp@50319 125 }
coleenp@50319 126 }
lfoltan@51268 127
lfoltan@51268 128 // Obtain the class loader's name and identity hash. If the class loader's
lfoltan@51268 129 // name was not explicitly set during construction, the class loader's name and id
lfoltan@51268 130 // will be set to the qualified class name of the class loader along with its
lfoltan@51268 131 // identity hash.
lfoltan@51268 132 // If for some reason the ClassLoader's constructor has not been run, instead of
lfoltan@51268 133 // leaving the _name_and_id field null, fall back to the external qualified class
lfoltan@51268 134 // name. Thus CLD's _name_and_id field should never have a null value.
lfoltan@51268 135 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader());
lfoltan@51268 136 const char* cl_instance_name_and_id =
lfoltan@51268 137 (cl_name_and_id == NULL) ? _class_loader_klass->external_name() :
lfoltan@51268 138 java_lang_String::as_utf8_string(cl_name_and_id);
lfoltan@51268 139 assert(cl_instance_name_and_id != NULL && cl_instance_name_and_id[0] != '\0', "class loader has no name and id");
lfoltan@51268 140 // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
lfoltan@51268 141 _name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id, CATCH);
coleenp@50319 142 }
coleenp@50319 143
coleenp@49813 144 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
stefank@22900 145 _is_anonymous(is_anonymous),
stefank@22900 146 // An anonymous class loader data doesn't have anything to keep
stefank@22900 147 // it from being unloaded during parsing of the anonymous class.
stefank@22900 148 // The null-class-loader should always be kept alive.
lfoltan@38014 149 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
coleenp@14816 150 _metaspace(NULL), _unloading(false), _klasses(NULL),
coleenp@50233 151 _modules(NULL), _packages(NULL), _unnamed_module(NULL), _dictionary(NULL),
coleenp@47735 152 _claimed(0), _modified_oops(true), _accumulated_modified_oops(false),
coleenp@47735 153 _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
coleenp@49813 154 _next(NULL),
lfoltan@51268 155 _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL),
coleenp@28163 156 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
coleenp@28163 157 Monitor::_safepoint_check_never)) {
rprotacio@46387 158
coleenp@50319 159 if (!h_class_loader.is_null()) {
coleenp@50319 160 _class_loader = _handles.add(h_class_loader());
lfoltan@51393 161 _class_loader_klass = h_class_loader->klass();
coleenp@50319 162 }
coleenp@50319 163
rprotacio@46387 164 if (!is_anonymous) {
coleenp@50233 165 // The holder is initialized later for anonymous classes, and before calling anything
coleenp@50233 166 // that call class_loader().
coleenp@50233 167 initialize_holder(h_class_loader);
coleenp@50233 168
coleenp@50233 169 // A ClassLoaderData created solely for an anonymous class should never have a
coleenp@50233 170 // ModuleEntryTable or PackageEntryTable created for it. The defining package
coleenp@50233 171 // and module for an anonymous class will be found in its host class.
hseigel@46420 172 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
rprotacio@46387 173 if (h_class_loader.is_null()) {
rprotacio@46387 174 // Create unnamed module for boot loader
rprotacio@46387 175 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
rprotacio@46387 176 } else {
rprotacio@46387 177 // Create unnamed module for all other loaders
rprotacio@46387 178 _unnamed_module = ModuleEntry::create_unnamed_module(this);
rprotacio@46387 179 }
coleenp@49813 180 _dictionary = create_dictionary();
rprotacio@46387 181 }
coleenp@46729 182
coleenp@49813 183 NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
coleenp@49813 184
egahlin@50662 185 JFR_ONLY(INIT_ID(this);)
coleenp@13728 186 }
coleenp@13728 187
ehelin@44084 188 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
ehelin@44084 189 Chunk* c = _head;
ehelin@44084 190 while (c != NULL) {
ehelin@44084 191 Chunk* next = c->_next;
ehelin@44084 192 delete c;
ehelin@44084 193 c = next;
ehelin@44084 194 }
ehelin@44084 195 }
ehelin@44084 196
ehelin@44084 197 oop* ClassLoaderData::ChunkedHandleList::add(oop o) {
ehelin@44084 198 if (_head == NULL || _head->_size == Chunk::CAPACITY) {
ehelin@44084 199 Chunk* next = new Chunk(_head);
coleenp@47789 200 OrderAccess::release_store(&_head, next);
ehelin@44084 201 }
ehelin@44084 202 oop* handle = &_head->_data[_head->_size];
ehelin@44084 203 *handle = o;
ehelin@44084 204 OrderAccess::release_store(&_head->_size, _head->_size + 1);
ehelin@44084 205 return handle;
ehelin@44084 206 }
ehelin@44084 207
coleenp@49813 208 int ClassLoaderData::ChunkedHandleList::count() const {
coleenp@49813 209 int count = 0;
coleenp@49813 210 Chunk* chunk = _head;
coleenp@49813 211 while (chunk != NULL) {
coleenp@49813 212 count += chunk->_size;
coleenp@49813 213 chunk = chunk->_next;
coleenp@49813 214 }
coleenp@49813 215 return count;
coleenp@49813 216 }
coleenp@49813 217
ehelin@44084 218 inline void ClassLoaderData::ChunkedHandleList::oops_do_chunk(OopClosure* f, Chunk* c, const juint size) {
ehelin@44084 219 for (juint i = 0; i < size; i++) {
ehelin@44084 220 if (c->_data[i] != NULL) {
ehelin@44084 221 f->do_oop(&c->_data[i]);
ehelin@44084 222 }
ehelin@44084 223 }
ehelin@44084 224 }
ehelin@44084 225
ehelin@44084 226 void ClassLoaderData::ChunkedHandleList::oops_do(OopClosure* f) {
coleenp@47789 227 Chunk* head = OrderAccess::load_acquire(&_head);
ehelin@44084 228 if (head != NULL) {
ehelin@44084 229 // Must be careful when reading size of head
ehelin@44084 230 oops_do_chunk(f, head, OrderAccess::load_acquire(&head->_size));
ehelin@44084 231 for (Chunk* c = head->_next; c != NULL; c = c->_next) {
ehelin@44084 232 oops_do_chunk(f, c, c->_size);
ehelin@44084 233 }
ehelin@44084 234 }
ehelin@44084 235 }
ehelin@44084 236
ehelin@44084 237 class VerifyContainsOopClosure : public OopClosure {
coleenp@49813 238 oop _target;
ehelin@44084 239 bool _found;
ehelin@44084 240
ehelin@44084 241 public:
coleenp@49813 242 VerifyContainsOopClosure(oop target) : _target(target), _found(false) {}
ehelin@44084 243
ehelin@44084 244 void do_oop(oop* p) {
rkennke@50153 245 if (p != NULL && oopDesc::equals(RawAccess<>::oop_load(p), _target)) {
ehelin@44084 246 _found = true;
ehelin@44084 247 }
ehelin@44084 248 }
ehelin@44084 249
ehelin@44084 250 void do_oop(narrowOop* p) {
ehelin@44084 251 // The ChunkedHandleList should not contain any narrowOop
ehelin@44084 252 ShouldNotReachHere();
ehelin@44084 253 }
ehelin@44084 254
ehelin@44084 255 bool found() const {
ehelin@44084 256 return _found;
ehelin@44084 257 }
ehelin@44084 258 };
ehelin@44084 259
coleenp@49813 260 bool ClassLoaderData::ChunkedHandleList::contains(oop p) {
ehelin@44084 261 VerifyContainsOopClosure cl(p);
ehelin@44084 262 oops_do(&cl);
ehelin@44084 263 return cl.found();
ehelin@44084 264 }
ehelin@44084 265
coleenp@49868 266 #ifndef PRODUCT
coleenp@49868 267 bool ClassLoaderData::ChunkedHandleList::owner_of(oop* oop_handle) {
coleenp@49868 268 Chunk* chunk = _head;
coleenp@49868 269 while (chunk != NULL) {
coleenp@49868 270 if (&(chunk->_data[0]) <= oop_handle && oop_handle < &(chunk->_data[chunk->_size])) {
coleenp@49868 271 return true;
coleenp@49868 272 }
coleenp@49868 273 chunk = chunk->_next;
coleenp@49868 274 }
coleenp@49868 275 return false;
coleenp@49868 276 }
coleenp@49868 277 #endif // PRODUCT
coleenp@49868 278
coleenp@13728 279 bool ClassLoaderData::claim() {
coleenp@13728 280 if (_claimed == 1) {
coleenp@13728 281 return false;
coleenp@13728 282 }
coleenp@13728 283
coleenp@13728 284 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
coleenp@13728 285 }
coleenp@13728 286
lfoltan@38014 287 // Anonymous classes have their own ClassLoaderData that is marked to keep alive
lfoltan@38014 288 // while the class is being parsed, and if the class appears on the module fixup list.
lfoltan@38014 289 // Due to the uniqueness that no other class shares the anonymous class' name or
lfoltan@38014 290 // ClassLoaderData, no other non-GC thread has knowledge of the anonymous class while
lfoltan@38014 291 // it is being defined, therefore _keep_alive is not volatile or atomic.
lfoltan@38014 292 void ClassLoaderData::inc_keep_alive() {
lfoltan@40358 293 if (is_anonymous()) {
lfoltan@40358 294 assert(_keep_alive >= 0, "Invalid keep alive increment count");
lfoltan@40358 295 _keep_alive++;
lfoltan@40358 296 }
lfoltan@38014 297 }
lfoltan@38014 298
lfoltan@38014 299 void ClassLoaderData::dec_keep_alive() {
lfoltan@40358 300 if (is_anonymous()) {
lfoltan@40358 301 assert(_keep_alive > 0, "Invalid keep alive decrement count");
lfoltan@40358 302 _keep_alive--;
lfoltan@40358 303 }
lfoltan@38014 304 }
lfoltan@38014 305
coleenp@47735 306 void ClassLoaderData::oops_do(OopClosure* f, bool must_claim, bool clear_mod_oops) {
coleenp@13728 307 if (must_claim && !claim()) {
coleenp@13728 308 return;
coleenp@13728 309 }
coleenp@13728 310
coleenp@47735 311 // Only clear modified_oops after the ClassLoaderData is claimed.
coleenp@47735 312 if (clear_mod_oops) {
coleenp@47735 313 clear_modified_oops();
coleenp@47735 314 }
coleenp@47735 315
ehelin@44084 316 _handles.oops_do(f);
coleenp@13728 317 }
coleenp@13728 318
coleenp@13728 319 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
coleenp@47789 320 // Lock-free access requires load_acquire
coleenp@47789 321 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
coleenp@13728 322 klass_closure->do_klass(k);
coleenp@16352 323 assert(k != k->next_link(), "no loops!");
coleenp@13728 324 }
coleenp@13728 325 }
coleenp@13728 326
sla@18025 327 void ClassLoaderData::classes_do(void f(Klass * const)) {
coleenp@47789 328 // Lock-free access requires load_acquire
coleenp@47789 329 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
sla@18025 330 f(k);
mgronlun@42639 331 assert(k != k->next_link(), "no loops!");
sla@18025 332 }
sla@18025 333 }
sla@18025 334
roland@23526 335 void ClassLoaderData::methods_do(void f(Method*)) {
coleenp@47789 336 // Lock-free access requires load_acquire
coleenp@47789 337 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
coleenp@46380 338 if (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded()) {
roland@23526 339 InstanceKlass::cast(k)->methods_do(f);
roland@23526 340 }
roland@23526 341 }
roland@23526 342 }
roland@23526 343
farvidsson@21183 344 void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
coleenp@47789 345 // Lock-free access requires load_acquire
coleenp@47789 346 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
farvidsson@21183 347 // Do not filter ArrayKlass oops here...
coleenp@33611 348 if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) {
farvidsson@21183 349 klass_closure->do_klass(k);
farvidsson@21183 350 }
farvidsson@21183 351 }
farvidsson@21183 352 }
farvidsson@21183 353
coleenp@13728 354 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
coleenp@47789 355 // Lock-free access requires load_acquire
coleenp@47789 356 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
coleenp@33611 357 if (k->is_instance_klass()) {
coleenp@13728 358 f(InstanceKlass::cast(k));
coleenp@13728 359 }
coleenp@16352 360 assert(k != k->next_link(), "no loops!");
coleenp@13728 361 }
coleenp@13728 362 }
coleenp@13728 363
alanb@36508 364 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
dholmes@38937 365 assert_locked_or_safepoint(Module_lock);
rprotacio@46387 366 if (_unnamed_module != NULL) {
rprotacio@46387 367 f(_unnamed_module);
rprotacio@46387 368 }
alanb@36508 369 if (_modules != NULL) {
alanb@36508 370 for (int i = 0; i < _modules->table_size(); i++) {
alanb@36508 371 for (ModuleEntry* entry = _modules->bucket(i);
hseigel@46420 372 entry != NULL;
hseigel@46420 373 entry = entry->next()) {
alanb@36508 374 f(entry);
alanb@36508 375 }
alanb@36508 376 }
alanb@36508 377 }
alanb@36508 378 }
alanb@36508 379
alanb@36508 380 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
hseigel@46420 381 assert_locked_or_safepoint(Module_lock);
hseigel@46420 382 if (_packages != NULL) {
hseigel@46420 383 for (int i = 0; i < _packages->table_size(); i++) {
hseigel@46420 384 for (PackageEntry* entry = _packages->bucket(i);
hseigel@46420 385 entry != NULL;
hseigel@46420 386 entry = entry->next()) {
alanb@36508 387 f(entry);
alanb@36508 388 }
alanb@36508 389 }
alanb@36508 390 }
alanb@36508 391 }
alanb@36508 392
coleenp@49813 393 void ClassLoaderData::record_dependency(const Klass* k) {
mgronlun@34666 394 assert(k != NULL, "invariant");
mgronlun@34666 395
coleenp@13728 396 ClassLoaderData * const from_cld = this;
coleenp@13728 397 ClassLoaderData * const to_cld = k->class_loader_data();
coleenp@13728 398
hseigel@49244 399 // Do not need to record dependency if the dependency is to a class whose
hseigel@49244 400 // class loader data is never freed. (i.e. the dependency's class loader
hseigel@49244 401 // is one of the three builtin class loaders and the dependency is not
hseigel@49244 402 // anonymous.)
hseigel@49244 403 if (to_cld->is_permanent_class_loader_data()) {
coleenp@13728 404 return;
coleenp@13728 405 }
coleenp@13728 406
coleenp@14588 407 oop to;
coleenp@14588 408 if (to_cld->is_anonymous()) {
hseigel@49244 409 // Just return if an anonymous class is attempting to record a dependency
hseigel@49244 410 // to itself. (Note that every anonymous class has its own unique class
hseigel@49244 411 // loader data.)
hseigel@49244 412 if (to_cld == from_cld) {
hseigel@49244 413 return;
hseigel@49244 414 }
coleenp@14588 415 // Anonymous class dependencies are through the mirror.
coleenp@14588 416 to = k->java_mirror();
coleenp@14588 417 } else {
coleenp@14588 418 to = to_cld->class_loader();
hseigel@49244 419 oop from = from_cld->class_loader();
coleenp@13728 420
hseigel@49244 421 // Just return if this dependency is to a class with the same or a parent
hseigel@49244 422 // class_loader.
rkennke@50153 423 if (oopDesc::equals(from, to) || java_lang_ClassLoader::isAncestor(from, to)) {
hseigel@49244 424 return; // this class loader is in the parent list, no need to add it.
coleenp@13728 425 }
coleenp@13728 426 }
coleenp@13728 427
coleenp@49813 428 // It's a dependency we won't find through GC, add it.
coleenp@49813 429 if (!_handles.contains(to)) {
coleenp@49813 430 NOT_PRODUCT(Atomic::inc(&_dependency_count));
coleenp@49813 431 LogTarget(Trace, class, loader, data) lt;
coleenp@49813 432 if (lt.is_enabled()) {
coleenp@49813 433 ResourceMark rm;
coleenp@49813 434 LogStream ls(lt);
coleenp@49813 435 ls.print("adding dependency from ");
coleenp@49813 436 print_value_on(&ls);
coleenp@49813 437 ls.print(" to ");
coleenp@49813 438 to_cld->print_value_on(&ls);
coleenp@49813 439 ls.cr();
coleenp@49813 440 }
coleenp@49813 441 Handle dependency(Thread::current(), to);
coleenp@49813 442 add_handle(dependency);
coleenp@49813 443 // Added a potentially young gen oop to the ClassLoaderData
coleenp@49813 444 record_modified_oops();
coleenp@49813 445 }
coleenp@13728 446 }
coleenp@13728 447
coleenp@13728 448
coleenp@13728 449 void ClassLoaderDataGraph::clear_claimed_marks() {
coleenp@13728 450 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@13728 451 cld->clear_claimed();
coleenp@13728 452 }
coleenp@13728 453 }
coleenp@13728 454
mgronlun@34666 455 void ClassLoaderData::add_class(Klass* k, bool publicize /* true */) {
mgronlun@34666 456 {
mgronlun@34666 457 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
mgronlun@34666 458 Klass* old_value = _klasses;
mgronlun@34666 459 k->set_next_link(old_value);
dholmes@38937 460 // Link the new item into the list, making sure the linked class is stable
dholmes@38937 461 // since the list can be walked without a lock
coleenp@47789 462 OrderAccess::release_store(&_klasses, k);
zgu@49268 463 if (k->is_array_klass()) {
zgu@49268 464 ClassLoaderDataGraph::inc_array_classes(1);
zgu@49268 465 } else {
zgu@49268 466 ClassLoaderDataGraph::inc_instance_classes(1);
zgu@49268 467 }
mgronlun@34666 468 }
coleenp@13728 469
coleenp@49813 470 if (publicize) {
coleenp@49813 471 LogTarget(Trace, class, loader, data) lt;
coleenp@49813 472 if (lt.is_enabled()) {
coleenp@49813 473 ResourceMark rm;
coleenp@49813 474 LogStream ls(lt);
coleenp@49813 475 ls.print("Adding k: " PTR_FORMAT " %s to ", p2i(k), k->external_name());
coleenp@49813 476 print_value_on(&ls);
coleenp@49813 477 ls.cr();
coleenp@49813 478 }
coleenp@13728 479 }
coleenp@13728 480 }
coleenp@13728 481
coleenp@46729 482 // Class iterator used by the compiler. It gets some number of classes at
coleenp@46729 483 // a safepoint to decay invocation counters on the methods.
coleenp@46729 484 class ClassLoaderDataGraphKlassIteratorStatic {
coleenp@46729 485 ClassLoaderData* _current_loader_data;
coleenp@46729 486 Klass* _current_class_entry;
coleenp@46729 487 public:
coleenp@46729 488
coleenp@46729 489 ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {}
coleenp@46729 490
coleenp@46729 491 InstanceKlass* try_get_next_class() {
coleenp@46729 492 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
zgu@49268 493 size_t max_classes = ClassLoaderDataGraph::num_instance_classes();
coleenp@46739 494 assert(max_classes > 0, "should not be called with no instance classes");
zgu@49268 495 for (size_t i = 0; i < max_classes; ) {
coleenp@46729 496
coleenp@46729 497 if (_current_class_entry != NULL) {
coleenp@46729 498 Klass* k = _current_class_entry;
coleenp@46729 499 _current_class_entry = _current_class_entry->next_link();
coleenp@46729 500
coleenp@46729 501 if (k->is_instance_klass()) {
coleenp@46729 502 InstanceKlass* ik = InstanceKlass::cast(k);
coleenp@46739 503 i++; // count all instance classes found
coleenp@46739 504 // Not yet loaded classes are counted in max_classes
coleenp@46739 505 // but only return loaded classes.
coleenp@46729 506 if (ik->is_loaded()) {
coleenp@46729 507 return ik;
coleenp@46729 508 }
coleenp@46729 509 }
coleenp@46729 510 } else {
coleenp@46729 511 // Go to next CLD
coleenp@46729 512 if (_current_loader_data != NULL) {
coleenp@46729 513 _current_loader_data = _current_loader_data->next();
coleenp@46729 514 }
coleenp@46729 515 // Start at the beginning
coleenp@46729 516 if (_current_loader_data == NULL) {
coleenp@46729 517 _current_loader_data = ClassLoaderDataGraph::_head;
coleenp@46729 518 }
coleenp@46729 519
coleenp@46729 520 _current_class_entry = _current_loader_data->klasses();
coleenp@46729 521 }
coleenp@46729 522 }
coleenp@46739 523 // Should never be reached unless all instance classes have failed or are not fully loaded.
coleenp@46739 524 // Caller handles NULL.
coleenp@46739 525 return NULL;
coleenp@46729 526 }
coleenp@46729 527
coleenp@46729 528 // If the current class for the static iterator is a class being unloaded or
coleenp@46729 529 // deallocated, adjust the current class.
coleenp@46729 530 void adjust_saved_class(ClassLoaderData* cld) {
coleenp@46729 531 if (_current_loader_data == cld) {
coleenp@46729 532 _current_loader_data = cld->next();
coleenp@46729 533 if (_current_loader_data != NULL) {
coleenp@46729 534 _current_class_entry = _current_loader_data->klasses();
coleenp@46729 535 } // else try_get_next_class will start at the head
coleenp@46729 536 }
coleenp@46729 537 }
coleenp@46729 538
coleenp@46729 539 void adjust_saved_class(Klass* klass) {
coleenp@46729 540 if (_current_class_entry == klass) {
coleenp@46729 541 _current_class_entry = klass->next_link();
coleenp@46729 542 }
coleenp@46729 543 }
coleenp@46729 544 };
coleenp@46729 545
coleenp@46729 546 static ClassLoaderDataGraphKlassIteratorStatic static_klass_iterator;
coleenp@46729 547
coleenp@46729 548 InstanceKlass* ClassLoaderDataGraph::try_get_next_class() {
coleenp@46729 549 return static_klass_iterator.try_get_next_class();
coleenp@46729 550 }
coleenp@46729 551
coleenp@46729 552
coleenp@50233 553 void ClassLoaderData::initialize_holder(Handle loader_or_mirror) {
coleenp@50233 554 if (loader_or_mirror() != NULL) {
coleenp@50233 555 assert(_holder.is_null(), "never replace holders");
coleenp@50233 556 _holder = WeakHandle<vm_class_loader_data>::create(loader_or_mirror);
coleenp@50233 557 }
coleenp@50233 558 }
coleenp@50233 559
dholmes@38937 560 // Remove a klass from the _klasses list for scratch_class during redefinition
dholmes@38937 561 // or parsed class in the case of an error.
coleenp@13728 562 void ClassLoaderData::remove_class(Klass* scratch_class) {
dholmes@38937 563 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
coleenp@46729 564
coleenp@46729 565 // Adjust global class iterator.
coleenp@46729 566 static_klass_iterator.adjust_saved_class(scratch_class);
coleenp@46729 567
coleenp@13728 568 Klass* prev = NULL;
coleenp@13728 569 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@13728 570 if (k == scratch_class) {
coleenp@13728 571 if (prev == NULL) {
coleenp@13728 572 _klasses = k->next_link();
coleenp@13728 573 } else {
coleenp@13728 574 Klass* next = k->next_link();
coleenp@13728 575 prev->set_next_link(next);
coleenp@13728 576 }
zgu@49268 577
zgu@49268 578 if (k->is_array_klass()) {
zgu@49268 579 ClassLoaderDataGraph::dec_array_classes(1);
zgu@49268 580 } else {
zgu@49268 581 ClassLoaderDataGraph::dec_instance_classes(1);
zgu@49268 582 }
zgu@49268 583
coleenp@13728 584 return;
coleenp@13728 585 }
coleenp@13728 586 prev = k;
coleenp@16352 587 assert(k != k->next_link(), "no loops!");
coleenp@13728 588 }
coleenp@13728 589 ShouldNotReachHere(); // should have found this class!!
coleenp@13728 590 }
coleenp@13728 591
coleenp@14588 592 void ClassLoaderData::unload() {
coleenp@14588 593 _unloading = true;
coleenp@14588 594
stuefe@46701 595 LogTarget(Debug, class, loader, data) lt;
stuefe@46701 596 if (lt.is_enabled()) {
coleenp@14588 597 ResourceMark rm;
stuefe@46701 598 LogStream ls(lt);
coleenp@50319 599 ls.print("unload");
coleenp@49813 600 print_value_on(&ls);
stuefe@46701 601 ls.cr();
coleenp@14588 602 }
aeriksso@38635 603
coleenp@48666 604 // Some items on the _deallocate_list need to free their C heap structures
coleenp@48666 605 // if they are not already on the _klasses list.
coleenp@48666 606 unload_deallocate_list();
coleenp@46729 607
coleenp@50803 608 // Tell serviceability tools these classes are unloading
coleenp@50803 609 // after erroneous classes are released.
coleenp@50803 610 classes_do(InstanceKlass::notify_unload_class);
coleenp@50803 611
coleenp@46729 612 // Clean up global class iterator for compiler
coleenp@46729 613 static_klass_iterator.adjust_saved_class(this);
coleenp@14588 614 }
coleenp@14588 615
alanb@36508 616 ModuleEntryTable* ClassLoaderData::modules() {
alanb@36508 617 // Lazily create the module entry table at first request.
coleenp@47789 618 // Lock-free access requires load_acquire.
coleenp@47789 619 ModuleEntryTable* modules = OrderAccess::load_acquire(&_modules);
dholmes@38937 620 if (modules == NULL) {
alanb@36508 621 MutexLocker m1(Module_lock);
dholmes@38937 622 // Check if _modules got allocated while we were waiting for this lock.
dholmes@38937 623 if ((modules = _modules) == NULL) {
dholmes@38937 624 modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
alanb@36508 625
dholmes@38937 626 {
dholmes@38937 627 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
dholmes@38937 628 // Ensure _modules is stable, since it is examined without a lock
coleenp@47789 629 OrderAccess::release_store(&_modules, modules);
dholmes@38937 630 }
alanb@36508 631 }
alanb@36508 632 }
dholmes@38937 633 return modules;
alanb@36508 634 }
alanb@36508 635
coleenp@46729 636 const int _boot_loader_dictionary_size = 1009;
coleenp@46729 637 const int _default_loader_dictionary_size = 107;
coleenp@46729 638
coleenp@46729 639 Dictionary* ClassLoaderData::create_dictionary() {
coleenp@46729 640 assert(!is_anonymous(), "anonymous class loader data do not have a dictionary");
coleenp@46729 641 int size;
gziemski@47955 642 bool resizable = false;
coleenp@46729 643 if (_the_null_class_loader_data == NULL) {
coleenp@46729 644 size = _boot_loader_dictionary_size;
gziemski@47955 645 resizable = true;
coleenp@46729 646 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
coleenp@46729 647 size = 1; // there's only one class in relection class loader and no initiated classes
coleenp@46729 648 } else if (is_system_class_loader_data()) {
gziemski@47955 649 size = _boot_loader_dictionary_size;
gziemski@47955 650 resizable = true;
coleenp@46729 651 } else {
coleenp@46729 652 size = _default_loader_dictionary_size;
gziemski@47955 653 resizable = true;
coleenp@46729 654 }
gziemski@47955 655 if (!DynamicallyResizeSystemDictionaries || DumpSharedSpaces || UseSharedSpaces) {
gziemski@47955 656 resizable = false;
gziemski@47955 657 }
gziemski@47955 658 return new Dictionary(this, size, resizable);
coleenp@46729 659 }
coleenp@46729 660
coleenp@49944 661 // Tell the GC to keep this klass alive while iterating ClassLoaderDataGraph
coleenp@50233 662 oop ClassLoaderData::holder_phantom() const {
coleenp@49944 663 // A klass that was previously considered dead can be looked up in the
coleenp@49944 664 // CLD/SD, and its _java_mirror or _class_loader can be stored in a root
coleenp@49944 665 // or a reachable object making it alive again. The SATB part of G1 needs
coleenp@49944 666 // to get notified about this potential resurrection, otherwise the marking
coleenp@49944 667 // might not find the object.
coleenp@50233 668 if (!_holder.is_null()) { // NULL class_loader
coleenp@50233 669 return _holder.resolve();
coleenp@49944 670 } else {
coleenp@49944 671 return NULL;
coleenp@49944 672 }
coleenp@49944 673 }
coleenp@49944 674
coleenp@46729 675 // Unloading support
coleenp@50233 676 bool ClassLoaderData::is_alive() const {
coleenp@50233 677 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
coleenp@50319 678 || (_holder.peek() != NULL); // and not cleaned by the GC weak handle processing.
stefank@22900 679
coleenp@14588 680 return alive;
coleenp@14588 681 }
coleenp@14588 682
zgu@49268 683 class ReleaseKlassClosure: public KlassClosure {
zgu@49268 684 private:
zgu@49268 685 size_t _instance_class_released;
zgu@49268 686 size_t _array_class_released;
zgu@49268 687 public:
zgu@49268 688 ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
zgu@49268 689
zgu@49268 690 size_t instance_class_released() const { return _instance_class_released; }
zgu@49268 691 size_t array_class_released() const { return _array_class_released; }
zgu@49268 692
zgu@49268 693 void do_klass(Klass* k) {
zgu@49268 694 if (k->is_array_klass()) {
zgu@49268 695 _array_class_released ++;
zgu@49268 696 } else {
zgu@49268 697 assert(k->is_instance_klass(), "Must be");
zgu@49268 698 _instance_class_released ++;
zgu@49268 699 InstanceKlass::release_C_heap_structures(InstanceKlass::cast(k));
zgu@49268 700 }
zgu@49268 701 }
zgu@49268 702 };
zgu@49268 703
coleenp@13728 704 ClassLoaderData::~ClassLoaderData() {
coleenp@17075 705 // Release C heap structures for all the classes.
zgu@49268 706 ReleaseKlassClosure cl;
zgu@49268 707 classes_do(&cl);
zgu@49268 708
zgu@49268 709 ClassLoaderDataGraph::dec_array_classes(cl.array_class_released());
zgu@49268 710 ClassLoaderDataGraph::dec_instance_classes(cl.instance_class_released());
coleenp@17075 711
coleenp@50233 712 // Release the WeakHandle
coleenp@50233 713 _holder.release();
coleenp@50233 714
alanb@36508 715 // Release C heap allocated hashtable for all the packages.
alanb@36508 716 if (_packages != NULL) {
alanb@36508 717 // Destroy the table itself
alanb@36508 718 delete _packages;
alanb@36508 719 _packages = NULL;
alanb@36508 720 }
alanb@36508 721
alanb@36508 722 // Release C heap allocated hashtable for all the modules.
alanb@36508 723 if (_modules != NULL) {
alanb@36508 724 // Destroy the table itself
alanb@36508 725 delete _modules;
alanb@36508 726 _modules = NULL;
alanb@36508 727 }
alanb@36508 728
coleenp@46729 729 // Release C heap allocated hashtable for the dictionary
coleenp@46729 730 if (_dictionary != NULL) {
coleenp@46729 731 // Destroy the table itself
coleenp@46729 732 delete _dictionary;
coleenp@46729 733 _dictionary = NULL;
coleenp@46729 734 }
coleenp@46729 735
rprotacio@46387 736 if (_unnamed_module != NULL) {
rprotacio@46387 737 _unnamed_module->delete_unnamed_module();
rprotacio@46387 738 _unnamed_module = NULL;
rprotacio@46387 739 }
rprotacio@46387 740
alanb@36508 741 // release the metaspace
stuefe@49866 742 ClassLoaderMetaspace *m = _metaspace;
coleenp@13728 743 if (m != NULL) {
coleenp@13728 744 _metaspace = NULL;
coleenp@13728 745 delete m;
alanb@36508 746 }
coleenp@13728 747 // Clear all the JNI handles for methods
coleenp@13728 748 // These aren't deallocated and are going to look like a leak, but that's
coleenp@13728 749 // needed because we can't really get rid of jmethodIDs because we don't
coleenp@13728 750 // know when native code is going to stop using them. The spec says that
coleenp@13728 751 // they're "invalid" but existing programs likely rely on their being
coleenp@13728 752 // NULL after class unloading.
coleenp@13728 753 if (_jmethod_ids != NULL) {
coleenp@13728 754 Method::clear_jmethod_ids(this);
coleenp@13728 755 }
coleenp@13728 756 // Delete lock
coleenp@13728 757 delete _metaspace_lock;
coleenp@13728 758
coleenp@13728 759 // Delete free list
coleenp@13728 760 if (_deallocate_list != NULL) {
coleenp@13728 761 delete _deallocate_list;
coleenp@13728 762 }
coleenp@13728 763 }
coleenp@13728 764
hseigel@49255 765 // Returns true if this class loader data is for the app class loader
hseigel@49255 766 // or a user defined system class loader. (Note that the class loader
hseigel@49255 767 // data may be anonymous.)
lfoltan@39616 768 bool ClassLoaderData::is_system_class_loader_data() const {
lfoltan@39616 769 return SystemDictionary::is_system_class_loader(class_loader());
lfoltan@39616 770 }
lfoltan@39616 771
lfoltan@39616 772 // Returns true if this class loader data is for the platform class loader.
hseigel@49255 773 // (Note that the class loader data may be anonymous.)
alanb@36508 774 bool ClassLoaderData::is_platform_class_loader_data() const {
alanb@36508 775 return SystemDictionary::is_platform_class_loader(class_loader());
twisti@16617 776 }
twisti@16617 777
hseigel@49255 778 // Returns true if the class loader for this class loader data is one of
hseigel@49255 779 // the 3 builtin (boot application/system or platform) class loaders,
hseigel@49255 780 // including a user-defined system class loader. Note that if the class
hseigel@49255 781 // loader data is for an anonymous class then it may get freed by a GC
hseigel@49255 782 // even if its class loader is one of these loaders.
lfoltan@39616 783 bool ClassLoaderData::is_builtin_class_loader_data() const {
hseigel@49255 784 return (is_boot_class_loader_data() ||
coleenp@46271 785 SystemDictionary::is_system_class_loader(class_loader()) ||
coleenp@46271 786 SystemDictionary::is_platform_class_loader(class_loader()));
lfoltan@39616 787 }
lfoltan@39616 788
hseigel@49244 789 // Returns true if this class loader data is a class loader data
hseigel@49244 790 // that is not ever freed by a GC. It must be one of the builtin
hseigel@49244 791 // class loaders and not anonymous.
hseigel@49244 792 bool ClassLoaderData::is_permanent_class_loader_data() const {
hseigel@49244 793 return is_builtin_class_loader_data() && !is_anonymous();
hseigel@49244 794 }
hseigel@49244 795
stuefe@49866 796 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
coleenp@13728 797 // If the metaspace has not been allocated, create a new one. Might want
coleenp@13728 798 // to create smaller arena for Reflection class loaders also.
coleenp@13728 799 // The reason for the delayed allocation is because some class loaders are
coleenp@13728 800 // simply for delegating with no metadata of their own.
coleenp@47789 801 // Lock-free access requires load_acquire.
stuefe@49866 802 ClassLoaderMetaspace* metaspace = OrderAccess::load_acquire(&_metaspace);
dholmes@38937 803 if (metaspace == NULL) {
dholmes@38937 804 MutexLockerEx ml(_metaspace_lock, Mutex::_no_safepoint_check_flag);
dholmes@38937 805 // Check if _metaspace got allocated while we were waiting for this lock.
dholmes@38937 806 if ((metaspace = _metaspace) == NULL) {
dholmes@38937 807 if (this == the_null_class_loader_data()) {
dholmes@38937 808 assert (class_loader() == NULL, "Must be");
stuefe@49866 809 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
dholmes@38937 810 } else if (is_anonymous()) {
stuefe@49866 811 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType);
dholmes@38937 812 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
stuefe@49866 813 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType);
dholmes@38937 814 } else {
stuefe@49866 815 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::StandardMetaspaceType);
jmasa@15086 816 }
dholmes@38937 817 // Ensure _metaspace is stable, since it is examined without a lock
coleenp@47789 818 OrderAccess::release_store(&_metaspace, metaspace);
coleenp@13728 819 }
coleenp@13728 820 }
dholmes@38937 821 return metaspace;
coleenp@13728 822 }
coleenp@13728 823
coleenp@46961 824 OopHandle ClassLoaderData::add_handle(Handle h) {
vlivanov@31046 825 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@47735 826 record_modified_oops();
coleenp@46961 827 return OopHandle(_handles.add(h()));
vlivanov@31046 828 }
vlivanov@31046 829
coleenp@47095 830 void ClassLoaderData::remove_handle(OopHandle h) {
coleenp@48666 831 assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");
coleenp@47095 832 oop* ptr = h.ptr_raw();
coleenp@47095 833 if (ptr != NULL) {
coleenp@49868 834 assert(_handles.owner_of(ptr), "Got unexpected handle " PTR_FORMAT, p2i(ptr));
eosterlund@48933 835 // This root is not walked in safepoints, and hence requires an appropriate
eosterlund@48933 836 // decorator that e.g. maintains the SATB invariant in SATB collectors.
kbarrett@51233 837 NativeAccess<IN_CONCURRENT_ROOT>::oop_store(ptr, oop(NULL));
coleenp@47095 838 }
coleenp@47095 839 }
coleenp@47095 840
coleenp@46961 841 void ClassLoaderData::init_handle_locked(OopHandle& dest, Handle h) {
coleenp@46961 842 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@46961 843 if (dest.resolve() != NULL) {
coleenp@46961 844 return;
coleenp@46961 845 } else {
coleenp@46961 846 dest = _handles.add(h());
coleenp@46961 847 }
alanb@36508 848 }
alanb@36508 849
coleenp@13728 850 // Add this metadata pointer to be freed when it's safe. This is only during
coleenp@13728 851 // class unloading because Handles might point to this metadata field.
coleenp@13728 852 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
coleenp@13728 853 // Metadata in shared region isn't deleted.
coleenp@13728 854 if (!m->is_shared()) {
coleenp@13728 855 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@13728 856 if (_deallocate_list == NULL) {
coleenp@13728 857 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
coleenp@13728 858 }
coleenp@13728 859 _deallocate_list->append_if_missing(m);
coleenp@13728 860 }
coleenp@13728 861 }
coleenp@13728 862
coleenp@13728 863 // Deallocate free metadata on the free list. How useful the PermGen was!
coleenp@13728 864 void ClassLoaderData::free_deallocate_list() {
coleenp@13728 865 // Don't need lock, at safepoint
coleenp@13728 866 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
coleenp@48666 867 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
coleenp@13728 868 if (_deallocate_list == NULL) {
coleenp@13728 869 return;
coleenp@13728 870 }
coleenp@13728 871 // Go backwards because this removes entries that are freed.
coleenp@13728 872 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
coleenp@13728 873 Metadata* m = _deallocate_list->at(i);
coleenp@13728 874 if (!m->on_stack()) {
coleenp@13728 875 _deallocate_list->remove_at(i);
coleenp@13728 876 // There are only three types of metadata that we deallocate directly.
coleenp@13728 877 // Cast them so they can be used by the template function.
coleenp@13728 878 if (m->is_method()) {
coleenp@13728 879 MetadataFactory::free_metadata(this, (Method*)m);
coleenp@13728 880 } else if (m->is_constantPool()) {
coleenp@13728 881 MetadataFactory::free_metadata(this, (ConstantPool*)m);
coleenp@13728 882 } else if (m->is_klass()) {
coleenp@13728 883 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
coleenp@13728 884 } else {
coleenp@13728 885 ShouldNotReachHere();
coleenp@13728 886 }
coleenp@29576 887 } else {
coleenp@29576 888 // Metadata is alive.
coleenp@29576 889 // If scratch_class is on stack then it shouldn't be on this list!
coleenp@29576 890 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
coleenp@29576 891 "scratch classes on this list should be dead");
coleenp@29576 892 // Also should assert that other metadata on the list was found in handles.
coleenp@13728 893 }
coleenp@13728 894 }
coleenp@13728 895 }
coleenp@13728 896
coleenp@48666 897 // This is distinct from free_deallocate_list. For class loader data that are
coleenp@48676 898 // unloading, this frees the C heap memory for items on the list, and unlinks
coleenp@48676 899 // scratch or error classes so that unloading events aren't triggered for these
coleenp@48676 900 // classes. The metadata is removed with the unloading metaspace.
coleenp@48676 901 // There isn't C heap memory allocated for methods, so nothing is done for them.
coleenp@48666 902 void ClassLoaderData::unload_deallocate_list() {
coleenp@48666 903 // Don't need lock, at safepoint
coleenp@48666 904 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
coleenp@48666 905 assert(is_unloading(), "only called for ClassLoaderData that are unloading");
coleenp@48666 906 if (_deallocate_list == NULL) {
coleenp@48666 907 return;
coleenp@48666 908 }
coleenp@48666 909 // Go backwards because this removes entries that are freed.
coleenp@48666 910 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
coleenp@48666 911 Metadata* m = _deallocate_list->at(i);
coleenp@48666 912 assert (!m->on_stack(), "wouldn't be unloading if this were so");
coleenp@48666 913 _deallocate_list->remove_at(i);
coleenp@48666 914 if (m->is_constantPool()) {
coleenp@48666 915 ((ConstantPool*)m)->release_C_heap_structures();
coleenp@48676 916 } else if (m->is_klass()) {
coleenp@48676 917 InstanceKlass* ik = (InstanceKlass*)m;
coleenp@48676 918 // also releases ik->constants() C heap memory
coleenp@48676 919 InstanceKlass::release_C_heap_structures(ik);
coleenp@48676 920 // Remove the class so unloading events aren't triggered for
coleenp@48676 921 // this class (scratch or error class) in do_unloading().
coleenp@48676 922 remove_class(ik);
coleenp@48666 923 }
coleenp@48666 924 }
coleenp@48666 925 }
coleenp@48666 926
coleenp@14588 927 // These anonymous class loaders are to contain classes used for JSR292
coleenp@49813 928 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
coleenp@14588 929 // Add a new class loader data to the graph.
coleenp@49813 930 return ClassLoaderDataGraph::add(loader, true);
coleenp@13728 931 }
coleenp@13728 932
lfoltan@51268 933 // Caller needs ResourceMark
lfoltan@51268 934 // If the class loader's _name has not been explicitly set, the class loader's
lfoltan@51268 935 // qualified class name is returned.
coleenp@49813 936 const char* ClassLoaderData::loader_name() const {
lfoltan@51268 937 if (_class_loader_klass == NULL) {
lfoltan@51268 938 return BOOTSTRAP_LOADER_NAME;
lfoltan@51268 939 } else if (_name != NULL) {
lfoltan@51268 940 return _name->as_C_string();
lfoltan@51268 941 } else {
lfoltan@51268 942 return _class_loader_klass->external_name();
lfoltan@51268 943 }
lfoltan@51268 944 }
lfoltan@51268 945
lfoltan@51268 946 // Caller needs ResourceMark
lfoltan@51268 947 // Format of the _name_and_id is as follows:
lfoltan@51268 948 // If the defining loader has a name explicitly set then '<loader-name>' @<id>
lfoltan@51268 949 // If the defining loader has no name then <qualified-class-name> @<id>
lfoltan@51268 950 // If built-in loader, then omit '@<id>' as there is only one instance.
lfoltan@51268 951 const char* ClassLoaderData::loader_name_and_id() const {
lfoltan@51268 952 if (_class_loader_klass == NULL) {
lfoltan@51268 953 return "'" BOOTSTRAP_LOADER_NAME "'";
lfoltan@51393 954 } else if (_name_and_id != NULL) {
lfoltan@51393 955 return _name_and_id->as_C_string();
coleenp@50319 956 } else {
lfoltan@51393 957 // May be called in a race before _name_and_id is initialized.
lfoltan@51393 958 return _class_loader_klass->external_name();
coleenp@50319 959 }
coleenp@14588 960 }
coleenp@14588 961
coleenp@49813 962 void ClassLoaderData::print_value_on(outputStream* out) const {
coleenp@50319 963 if (!is_unloading() && class_loader() != NULL) {
coleenp@49813 964 out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
lfoltan@51268 965 class_loader()->print_value_on(out); // includes loader_name_and_id() and address of class loader instance
coleenp@49813 966 } else {
lfoltan@51268 967 // loader data: 0xsomeaddr of 'bootstrap'
lfoltan@51268 968 out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
coleenp@49813 969 }
coleenp@49813 970 if (is_anonymous()) {
coleenp@49813 971 out->print(" anonymous");
coleenp@49813 972 }
coleenp@49813 973 }
coleenp@49813 974
coleenp@14588 975 #ifndef PRODUCT
coleenp@49813 976 void ClassLoaderData::print_on(outputStream* out) const {
coleenp@49813 977 out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
lfoltan@51268 978 p2i(this), p2i(_class_loader.ptr_raw()), loader_name_and_id());
coleenp@49813 979 if (is_anonymous()) out->print(" anonymous");
coleenp@49813 980 if (claimed()) out->print(" claimed");
coleenp@49813 981 if (is_unloading()) out->print(" unloading");
coleenp@49813 982 out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
coleenp@13728 983
coleenp@13728 984 if (_jmethod_ids != NULL) {
coleenp@13728 985 Method::print_jmethod_ids(this, out);
coleenp@13728 986 }
coleenp@49813 987 out->print(" handles count %d", _handles.count());
coleenp@49813 988 out->print(" dependencies %d", _dependency_count);
coleenp@13728 989 out->print_cr("}");
coleenp@13728 990 }
coleenp@13728 991 #endif // PRODUCT
coleenp@13728 992
coleenp@13728 993 void ClassLoaderData::verify() {
dholmes@38937 994 assert_locked_or_safepoint(_metaspace_lock);
coleenp@13728 995 oop cl = class_loader();
coleenp@13728 996
coleenp@14588 997 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
coleenp@14588 998 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
coleenp@13728 999
coleenp@13728 1000 // Verify the integrity of the allocated space.
coleenp@13728 1001 if (metaspace_or_null() != NULL) {
coleenp@13728 1002 metaspace_or_null()->verify();
coleenp@13728 1003 }
coleenp@13728 1004
coleenp@13728 1005 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@13728 1006 guarantee(k->class_loader_data() == this, "Must be the same");
coleenp@13728 1007 k->verify();
coleenp@16352 1008 assert(k != k->next_link(), "no loops!");
coleenp@13728 1009 }
coleenp@13728 1010 }
coleenp@13728 1011
coleenp@22794 1012 bool ClassLoaderData::contains_klass(Klass* klass) {
coleenp@47789 1013 // Lock-free access requires load_acquire
coleenp@47789 1014 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
coleenp@22794 1015 if (k == klass) return true;
coleenp@22794 1016 }
coleenp@22794 1017 return false;
coleenp@22794 1018 }
coleenp@22794 1019
coleenp@14588 1020
coleenp@13728 1021 // GC root of class loader data created.
coleenp@13728 1022 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
coleenp@13728 1023 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
mgronlun@25371 1024 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
coleenp@13728 1025 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
coleenp@13728 1026
coleenp@24457 1027 bool ClassLoaderDataGraph::_should_purge = false;
coleenp@27461 1028 bool ClassLoaderDataGraph::_metaspace_oom = false;
coleenp@24457 1029
coleenp@13728 1030 // Add a new class loader data node to the list. Assign the newly created
coleenp@13728 1031 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
coleenp@50319 1032 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_anonymous) {
david@35492 1033 NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
david@35492 1034 // ClassLoaderData in the graph since the CLD
coleenp@50319 1035 // contains oops in _handles that must be walked.
coleenp@13728 1036
coleenp@49813 1037 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
mgerdin@17110 1038
mgerdin@17107 1039 if (!is_anonymous) {
coleenp@14588 1040 // First, Atomically set it
eosterlund@49472 1041 ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
coleenp@14588 1042 if (old != NULL) {
coleenp@14588 1043 delete cld;
coleenp@14588 1044 // Returns the data.
coleenp@14588 1045 return old;
coleenp@14588 1046 }
coleenp@13728 1047 }
coleenp@13728 1048
coleenp@13728 1049 // We won the race, and therefore the task of adding the data to the list of
coleenp@13728 1050 // class loader data
mgerdin@17107 1051 ClassLoaderData** list_head = &_head;
mgerdin@17107 1052 ClassLoaderData* next = _head;
mgerdin@17107 1053
coleenp@13728 1054 do {
coleenp@13728 1055 cld->set_next(next);
coleenp@47789 1056 ClassLoaderData* exchanged = Atomic::cmpxchg(cld, list_head, next);
coleenp@13728 1057 if (exchanged == next) {
stuefe@46701 1058 LogTarget(Debug, class, loader, data) lt;
stuefe@46701 1059 if (lt.is_enabled()) {
coleenp@49813 1060 ResourceMark rm;
coleenp@49813 1061 LogStream ls(lt);
coleenp@49813 1062 ls.print("create ");
coleenp@49813 1063 cld->print_value_on(&ls);
coleenp@49813 1064 ls.cr();
coleenp@13728 1065 }
coleenp@13728 1066 return cld;
coleenp@13728 1067 }
coleenp@13728 1068 next = exchanged;
coleenp@13728 1069 } while (true);
mockner@35917 1070 }
coleenp@14588 1071
coleenp@50319 1072 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
coleenp@50319 1073 ClassLoaderData* loader_data = add_to_graph(loader, is_anonymous);
lfoltan@51393 1074 // Initialize _name and _name_and_id after the loader data is added to the
lfoltan@51393 1075 // CLDG because adding the Symbol for _name and _name_and_id might safepoint.
coleenp@50319 1076 if (loader.not_null()) {
lfoltan@51393 1077 loader_data->initialize_name(loader);
coleenp@50319 1078 }
coleenp@50319 1079 return loader_data;
coleenp@50319 1080 }
coleenp@50319 1081
coleenp@47735 1082 void ClassLoaderDataGraph::oops_do(OopClosure* f, bool must_claim) {
coleenp@13728 1083 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@47735 1084 cld->oops_do(f, must_claim);
coleenp@13728 1085 }
coleenp@13728 1086 }
coleenp@13728 1087
coleenp@47735 1088 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, bool must_claim) {
coleenp@14588 1089 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@14588 1090 if (cld->keep_alive()) {
coleenp@47735 1091 cld->oops_do(f, must_claim);
coleenp@14588 1092 }
coleenp@14588 1093 }
coleenp@14588 1094 }
coleenp@14588 1095
coleenp@47735 1096 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, bool must_claim) {
coleenp@13728 1097 if (ClassUnloading) {
coleenp@47735 1098 keep_alive_oops_do(f, must_claim);
coleenp@13728 1099 } else {
coleenp@47735 1100 oops_do(f, must_claim);
coleenp@13728 1101 }
coleenp@13728 1102 }
coleenp@13728 1103
sla@25051 1104 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
sla@25051 1105 for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) {
sla@25051 1106 cl->do_cld(cld);
sla@25051 1107 }
sla@25051 1108 }
sla@25051 1109
mgronlun@42639 1110 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
mgronlun@42639 1111 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
mgronlun@42639 1112 // Only walk the head until any clds not purged from prior unloading
mgronlun@42639 1113 // (CMS doesn't purge right away).
mgronlun@42639 1114 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
mgronlun@42639 1115 assert(cld->is_unloading(), "invariant");
mgronlun@42639 1116 cl->do_cld(cld);
mgronlun@42639 1117 }
mgronlun@42639 1118 }
mgronlun@42639 1119
stefank@25492 1120 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
stefank@25492 1121 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) {
stefank@25492 1122 CLDClosure* closure = cld->keep_alive() ? strong : weak;
stefank@25492 1123 if (closure != NULL) {
stefank@25492 1124 closure->do_cld(cld);
stefank@25492 1125 }
stefank@25492 1126 }
stefank@25492 1127 }
stefank@25492 1128
stefank@25492 1129 void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) {
stefank@25492 1130 roots_cld_do(cl, NULL);
stefank@25492 1131 }
stefank@25492 1132
stefank@25492 1133 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
stefank@25492 1134 if (ClassUnloading) {
stefank@25492 1135 keep_alive_cld_do(cl);
stefank@25492 1136 } else {
stefank@25492 1137 cld_do(cl);
stefank@25492 1138 }
stefank@25492 1139 }
stefank@25492 1140
coleenp@13728 1141 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
coleenp@49944 1142 Thread* thread = Thread::current();
coleenp@13728 1143 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@49944 1144 Handle holder(thread, cld->holder_phantom());
coleenp@13728 1145 cld->classes_do(klass_closure);
coleenp@13728 1146 }
coleenp@13728 1147 }
coleenp@13728 1148
sla@18025 1149 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
coleenp@49944 1150 Thread* thread = Thread::current();
sla@18025 1151 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@49944 1152 Handle holder(thread, cld->holder_phantom());
sla@18025 1153 cld->classes_do(f);
sla@18025 1154 }
sla@18025 1155 }
sla@18025 1156
roland@23526 1157 void ClassLoaderDataGraph::methods_do(void f(Method*)) {
coleenp@49944 1158 Thread* thread = Thread::current();
roland@23526 1159 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@49944 1160 Handle holder(thread, cld->holder_phantom());
roland@23526 1161 cld->methods_do(f);
roland@23526 1162 }
roland@23526 1163 }
roland@23526 1164
alanb@36508 1165 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) {
alanb@36508 1166 assert_locked_or_safepoint(Module_lock);
coleenp@49944 1167 Thread* thread = Thread::current();
alanb@36508 1168 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@49944 1169 Handle holder(thread, cld->holder_phantom());
alanb@36508 1170 cld->modules_do(f);
alanb@36508 1171 }
alanb@36508 1172 }
alanb@36508 1173
alanb@36508 1174 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) {
alanb@36508 1175 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
alanb@36508 1176 // Only walk the head until any clds not purged from prior unloading
alanb@36508 1177 // (CMS doesn't purge right away).
alanb@36508 1178 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
alanb@36508 1179 assert(cld->is_unloading(), "invariant");
alanb@36508 1180 cld->modules_do(f);
alanb@36508 1181 }
alanb@36508 1182 }
alanb@36508 1183
alanb@36508 1184 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) {
alanb@36508 1185 assert_locked_or_safepoint(Module_lock);
coleenp@49944 1186 Thread* thread = Thread::current();
alanb@36508 1187 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@49944 1188 Handle holder(thread, cld->holder_phantom());
alanb@36508 1189 cld->packages_do(f);
alanb@36508 1190 }
alanb@36508 1191 }
alanb@36508 1192
alanb@36508 1193 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) {
alanb@36508 1194 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
alanb@36508 1195 // Only walk the head until any clds not purged from prior unloading
alanb@36508 1196 // (CMS doesn't purge right away).
alanb@36508 1197 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
alanb@36508 1198 assert(cld->is_unloading(), "invariant");
alanb@36508 1199 cld->packages_do(f);
alanb@36508 1200 }
alanb@36508 1201 }
alanb@36508 1202
farvidsson@21183 1203 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
coleenp@49944 1204 Thread* thread = Thread::current();
farvidsson@21183 1205 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@49944 1206 Handle holder(thread, cld->holder_phantom());
farvidsson@21183 1207 cld->loaded_classes_do(klass_closure);
farvidsson@21183 1208 }
farvidsson@21183 1209 }
farvidsson@21183 1210
sla@18025 1211 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
sla@18025 1212 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
mgronlun@25371 1213 // Only walk the head until any clds not purged from prior unloading
mgronlun@25371 1214 // (CMS doesn't purge right away).
mgronlun@25371 1215 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
alanb@36508 1216 assert(cld->is_unloading(), "invariant");
sla@18025 1217 cld->classes_do(f);
sla@18025 1218 }
sla@18025 1219 }
sla@18025 1220
coleenp@46729 1221 #define FOR_ALL_DICTIONARY(X) for (ClassLoaderData* X = _head; X != NULL; X = X->next()) \
coleenp@46729 1222 if (X->dictionary() != NULL)
coleenp@46729 1223
coleenp@46729 1224 // Walk classes in the loaded class dictionaries in various forms.
coleenp@46729 1225 // Only walks the classes defined in this class loader.
coleenp@46729 1226 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) {
coleenp@49944 1227 Thread* thread = Thread::current();
coleenp@46729 1228 FOR_ALL_DICTIONARY(cld) {
coleenp@49944 1229 Handle holder(thread, cld->holder_phantom());
coleenp@46729 1230 cld->dictionary()->classes_do(f);
coleenp@46729 1231 }
coleenp@46729 1232 }
coleenp@46729 1233
coleenp@46729 1234 // Only walks the classes defined in this class loader.
coleenp@46729 1235 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
coleenp@49944 1236 Thread* thread = Thread::current();
coleenp@46729 1237 FOR_ALL_DICTIONARY(cld) {
coleenp@49944 1238 Handle holder(thread, cld->holder_phantom());
coleenp@46729 1239 cld->dictionary()->classes_do(f, CHECK);
coleenp@46729 1240 }
coleenp@46729 1241 }
coleenp@46729 1242
coleenp@46729 1243 // Walks all entries in the dictionary including entries initiated by this class loader.
coleenp@46729 1244 void ClassLoaderDataGraph::dictionary_all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) {
coleenp@49944 1245 Thread* thread = Thread::current();
coleenp@46729 1246 FOR_ALL_DICTIONARY(cld) {
coleenp@49944 1247 Handle holder(thread, cld->holder_phantom());
coleenp@46729 1248 cld->dictionary()->all_entries_do(f);
coleenp@46729 1249 }
coleenp@46729 1250 }
coleenp@46729 1251
coleenp@46729 1252 void ClassLoaderDataGraph::verify_dictionary() {
coleenp@46729 1253 FOR_ALL_DICTIONARY(cld) {
coleenp@46729 1254 cld->dictionary()->verify();
coleenp@46729 1255 }
coleenp@46729 1256 }
coleenp@46729 1257
coleenp@46742 1258 void ClassLoaderDataGraph::print_dictionary(outputStream* st) {
coleenp@46729 1259 FOR_ALL_DICTIONARY(cld) {
coleenp@46742 1260 st->print("Dictionary for ");
coleenp@46742 1261 cld->print_value_on(st);
coleenp@46742 1262 st->cr();
coleenp@46742 1263 cld->dictionary()->print_on(st);
coleenp@46742 1264 st->cr();
coleenp@46742 1265 }
coleenp@46742 1266 }
coleenp@46742 1267
coleenp@46742 1268 void ClassLoaderDataGraph::print_dictionary_statistics(outputStream* st) {
coleenp@46742 1269 FOR_ALL_DICTIONARY(cld) {
coleenp@46742 1270 ResourceMark rm;
coleenp@46742 1271 stringStream tempst;
lfoltan@51268 1272 tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id());
coleenp@46742 1273 cld->dictionary()->print_table_statistics(st, tempst.as_string());
coleenp@46729 1274 }
coleenp@46729 1275 }
coleenp@46729 1276
coleenp@13728 1277 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
coleenp@13728 1278 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
coleenp@13728 1279
coleenp@13728 1280 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
coleenp@13728 1281
coleenp@13728 1282 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
coleenp@13728 1283 ClassLoaderData* curr = _head;
coleenp@13728 1284 while (curr != _saved_head) {
coleenp@13728 1285 if (!curr->claimed()) {
coleenp@13728 1286 array->push(curr);
stuefe@46701 1287 LogTarget(Debug, class, loader, data) lt;
stuefe@46701 1288 if (lt.is_enabled()) {
stuefe@46701 1289 LogStream ls(lt);
stuefe@46701 1290 ls.print("found new CLD: ");
stuefe@46701 1291 curr->print_value_on(&ls);
stuefe@46701 1292 ls.cr();
coleenp@13728 1293 }
coleenp@13728 1294 }
coleenp@13728 1295
coleenp@13728 1296 curr = curr->_next;
coleenp@13728 1297 }
coleenp@13728 1298
coleenp@13728 1299 return array;
coleenp@13728 1300 }
coleenp@13728 1301
coleenp@22201 1302 #ifndef PRODUCT
coleenp@13728 1303 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
coleenp@13728 1304 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@13728 1305 if (loader_data == data) {
coleenp@13728 1306 return true;
coleenp@13728 1307 }
coleenp@13728 1308 }
coleenp@13728 1309
coleenp@13728 1310 return false;
coleenp@13728 1311 }
coleenp@13728 1312 #endif // PRODUCT
coleenp@13728 1313
egahlin@50662 1314 #if INCLUDE_JFR
egahlin@50662 1315 static Ticks class_unload_time;
egahlin@50662 1316 static void post_class_unload_event(Klass* const k) {
egahlin@50662 1317 assert(k != NULL, "invariant");
egahlin@50662 1318 EventClassUnload event(UNTIMED);
egahlin@50662 1319 event.set_endtime(class_unload_time);
egahlin@50662 1320 event.set_unloadedClass(k);
egahlin@50662 1321 event.set_definingClassLoader(k->class_loader_data());
egahlin@50662 1322 event.commit();
egahlin@50662 1323 }
egahlin@50662 1324
egahlin@50662 1325 static void post_class_unload_events() {
egahlin@50662 1326 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
egahlin@50662 1327 if (Jfr::is_enabled()) {
egahlin@50662 1328 if (EventClassUnload::is_enabled()) {
egahlin@50662 1329 class_unload_time = Ticks::now();
egahlin@50662 1330 ClassLoaderDataGraph::classes_unloading_do(&post_class_unload_event);
egahlin@50662 1331 }
egahlin@50662 1332 Jfr::on_unloading_classes();
egahlin@50662 1333 }
egahlin@50662 1334 }
egahlin@50662 1335 #endif // INCLUDE_JFR
coleenp@14588 1336
coleenp@13728 1337 // Move class loader data from main list to the unloaded list for unloading
coleenp@13728 1338 // and deallocation later.
coleenp@50313 1339 bool ClassLoaderDataGraph::do_unloading(bool clean_previous_versions) {
coleenp@29576 1340
coleenp@13728 1341 ClassLoaderData* data = _head;
coleenp@13728 1342 ClassLoaderData* prev = NULL;
coleenp@13728 1343 bool seen_dead_loader = false;
coleenp@50233 1344 uint loaders_processed = 0;
coleenp@50233 1345 uint loaders_removed = 0;
mgronlun@25371 1346
coleenp@29576 1347 // Mark metadata seen on the stack only so we can delete unneeded entries.
coleenp@29576 1348 // Only walk all metadata, including the expensive code cache walk, for Full GC
coleenp@40909 1349 // and only if class redefinition and if there's previous versions of
coleenp@29576 1350 // Klasses to delete.
coleenp@29576 1351 bool walk_all_metadata = clean_previous_versions &&
coleenp@29576 1352 JvmtiExport::has_redefined_a_class() &&
coleenp@40927 1353 InstanceKlass::has_previous_versions_and_reset();
coleenp@29576 1354 MetadataOnStackMark md_on_stack(walk_all_metadata);
coleenp@29576 1355
mgronlun@25371 1356 // Save previous _unloading pointer for CMS which may add to unloading list before
mgronlun@25371 1357 // purging and we don't want to rewalk the previously unloaded class loader data.
mgronlun@25371 1358 _saved_unloading = _unloading;
mgronlun@25371 1359
roland@25635 1360 data = _head;
coleenp@13728 1361 while (data != NULL) {
coleenp@50233 1362 if (data->is_alive()) {
coleenp@29576 1363 // clean metaspace
coleenp@29576 1364 if (walk_all_metadata) {
coleenp@29576 1365 data->classes_do(InstanceKlass::purge_previous_versions);
coleenp@29576 1366 }
coleenp@29576 1367 data->free_deallocate_list();
coleenp@13728 1368 prev = data;
coleenp@13728 1369 data = data->next();
coleenp@50233 1370 loaders_processed++;
coleenp@13728 1371 continue;
coleenp@13728 1372 }
coleenp@13728 1373 seen_dead_loader = true;
coleenp@50233 1374 loaders_removed++;
coleenp@13728 1375 ClassLoaderData* dead = data;
coleenp@14588 1376 dead->unload();
coleenp@13728 1377 data = data->next();
coleenp@13728 1378 // Remove from loader list.
jmasa@17109 1379 // This class loader data will no longer be found
jmasa@17109 1380 // in the ClassLoaderDataGraph.
coleenp@13728 1381 if (prev != NULL) {
coleenp@13728 1382 prev->set_next(data);
coleenp@13728 1383 } else {
coleenp@13728 1384 assert(dead == _head, "sanity check");
coleenp@13728 1385 _head = data;
coleenp@13728 1386 }
coleenp@13728 1387 dead->set_next(_unloading);
coleenp@13728 1388 _unloading = dead;
coleenp@13728 1389 }
sla@18025 1390
sla@18025 1391 if (seen_dead_loader) {
lfoltan@39616 1392 data = _head;
lfoltan@39616 1393 while (data != NULL) {
coleenp@46729 1394 // Remove entries in the dictionary of live class loader that have
coleenp@46729 1395 // initiated loading classes in a dead class loader.
coleenp@46729 1396 if (data->dictionary() != NULL) {
coleenp@50313 1397 data->dictionary()->do_unloading();
coleenp@46729 1398 }
coleenp@46729 1399 // Walk a ModuleEntry's reads, and a PackageEntry's exports
coleenp@46729 1400 // lists to determine if there are modules on those lists that are now
coleenp@46729 1401 // dead and should be removed. A module's life cycle is equivalent
coleenp@46729 1402 // to its defining class loader's life cycle. Since a module is
coleenp@46729 1403 // considered dead if its class loader is dead, these walks must
coleenp@46729 1404 // occur after each class loader's aliveness is determined.
hseigel@46420 1405 if (data->packages() != NULL) {
lfoltan@39616 1406 data->packages()->purge_all_package_exports();
lfoltan@39616 1407 }
lfoltan@39616 1408 if (data->modules_defined()) {
lfoltan@39616 1409 data->modules()->purge_all_module_reads();
lfoltan@39616 1410 }
lfoltan@39616 1411 data = data->next();
lfoltan@39616 1412 }
egahlin@50662 1413 JFR_ONLY(post_class_unload_events();)
sla@18025 1414 }
sla@18025 1415
coleenp@50233 1416 log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed);
coleenp@50233 1417
coleenp@13728 1418 return seen_dead_loader;
coleenp@13728 1419 }
coleenp@13728 1420
coleenp@13728 1421 void ClassLoaderDataGraph::purge() {
coleenp@24457 1422 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
coleenp@13728 1423 ClassLoaderData* list = _unloading;
coleenp@13728 1424 _unloading = NULL;
coleenp@13728 1425 ClassLoaderData* next = list;
coleenp@27461 1426 bool classes_unloaded = false;
coleenp@13728 1427 while (next != NULL) {
coleenp@13728 1428 ClassLoaderData* purge_me = next;
coleenp@13728 1429 next = purge_me->next();
coleenp@13728 1430 delete purge_me;
coleenp@27461 1431 classes_unloaded = true;
coleenp@13728 1432 }
coleenp@27461 1433 if (classes_unloaded) {
coleenp@27461 1434 Metaspace::purge();
coleenp@27461 1435 set_metaspace_oom(false);
coleenp@27461 1436 }
coleenp@13728 1437 }
coleenp@13728 1438
gziemski@47955 1439 int ClassLoaderDataGraph::resize_if_needed() {
gziemski@47955 1440 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
gziemski@47955 1441 int resized = 0;
gziemski@47955 1442 if (Dictionary::does_any_dictionary_needs_resizing()) {
gziemski@47955 1443 FOR_ALL_DICTIONARY(cld) {
gziemski@47955 1444 if (cld->dictionary()->resize_if_needed()) {
gziemski@47955 1445 resized++;
gziemski@47955 1446 }
gziemski@47955 1447 }
gziemski@47955 1448 }
gziemski@47955 1449 return resized;
gziemski@47955 1450 }
gziemski@47955 1451
stefank@25492 1452 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic()
stefank@25492 1453 : _next_klass(NULL) {
stefank@25492 1454 ClassLoaderData* cld = ClassLoaderDataGraph::_head;
stefank@25492 1455 Klass* klass = NULL;
stefank@25492 1456
stefank@25492 1457 // Find the first klass in the CLDG.
stefank@25492 1458 while (cld != NULL) {
dholmes@38937 1459 assert_locked_or_safepoint(cld->metaspace_lock());
stefank@25492 1460 klass = cld->_klasses;
stefank@25492 1461 if (klass != NULL) {
stefank@25492 1462 _next_klass = klass;
stefank@25492 1463 return;
stefank@25492 1464 }
stefank@25492 1465 cld = cld->next();
stefank@25492 1466 }
stefank@25492 1467 }
stefank@25492 1468
stefank@25492 1469 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) {
stefank@25492 1470 Klass* next = klass->next_link();
stefank@25492 1471 if (next != NULL) {
stefank@25492 1472 return next;
stefank@25492 1473 }
stefank@25492 1474
stefank@25492 1475 // No more klasses in the current CLD. Time to find a new CLD.
stefank@25492 1476 ClassLoaderData* cld = klass->class_loader_data();
dholmes@38937 1477 assert_locked_or_safepoint(cld->metaspace_lock());
stefank@25492 1478 while (next == NULL) {
stefank@25492 1479 cld = cld->next();
stefank@25492 1480 if (cld == NULL) {
stefank@25492 1481 break;
stefank@25492 1482 }
stefank@25492 1483 next = cld->_klasses;
stefank@25492 1484 }
stefank@25492 1485
stefank@25492 1486 return next;
stefank@25492 1487 }
stefank@25492 1488
stefank@25492 1489 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() {
asiebenborn@29469 1490 Klass* head = _next_klass;
stefank@25492 1491
stefank@25492 1492 while (head != NULL) {
stefank@25492 1493 Klass* next = next_klass_in_cldg(head);
stefank@25492 1494
coleenp@47789 1495 Klass* old_head = Atomic::cmpxchg(next, &_next_klass, head);
stefank@25492 1496
stefank@25492 1497 if (old_head == head) {
stefank@25492 1498 return head; // Won the CAS.
stefank@25492 1499 }
stefank@25492 1500
stefank@25492 1501 head = old_head;
stefank@25492 1502 }
stefank@25492 1503
stefank@25492 1504 // Nothing more for the iterator to hand out.
david@33105 1505 assert(head == NULL, "head is " PTR_FORMAT ", expected not null:", p2i(head));
stefank@25492 1506 return NULL;
stefank@25492 1507 }
coleenp@13728 1508
coleenp@13728 1509 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
coleenp@13728 1510 _data = ClassLoaderDataGraph::_head;
coleenp@13728 1511 }
coleenp@13728 1512
coleenp@13728 1513 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
coleenp@13728 1514
coleenp@13728 1515 #ifndef PRODUCT
coleenp@13728 1516 // callable from debugger
coleenp@13728 1517 extern "C" int print_loader_data_graph() {
coleenp@49813 1518 ResourceMark rm;
coleenp@49813 1519 ClassLoaderDataGraph::print_on(tty);
coleenp@13728 1520 return 0;
coleenp@13728 1521 }
coleenp@13728 1522
coleenp@13728 1523 void ClassLoaderDataGraph::verify() {
coleenp@13728 1524 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@13728 1525 data->verify();
coleenp@13728 1526 }
coleenp@13728 1527 }
coleenp@13728 1528
coleenp@49813 1529 void ClassLoaderDataGraph::print_on(outputStream * const out) {
coleenp@13728 1530 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@49813 1531 data->print_on(out);
coleenp@13728 1532 }
coleenp@13728 1533 }
acorn@15437 1534 #endif // PRODUCT