src/share/vm/classfile/systemDictionary.cpp

Fri, 13 Jun 2008 14:49:07 -0700

author
kvn
date
Fri, 13 Jun 2008 14:49:07 -0700
changeset 627
6d13fcb3663f
parent 567
60b728ec77c1
child 631
d1605aabd0a1
permissions
-rw-r--r--

6714404: Add UseStringCache switch to enable String caching under AggressiveOpts
Summary: Poke String.stringCacheEnabled during vm initialization
Reviewed-by: never

duke@435 1 /*
duke@435 2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_systemDictionary.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 Dictionary* SystemDictionary::_dictionary = NULL;
duke@435 30 PlaceholderTable* SystemDictionary::_placeholders = NULL;
duke@435 31 Dictionary* SystemDictionary::_shared_dictionary = NULL;
duke@435 32 LoaderConstraintTable* SystemDictionary::_loader_constraints = NULL;
duke@435 33 ResolutionErrorTable* SystemDictionary::_resolution_errors = NULL;
duke@435 34
duke@435 35
duke@435 36 int SystemDictionary::_number_of_modifications = 0;
duke@435 37
duke@435 38 oop SystemDictionary::_system_loader_lock_obj = NULL;
duke@435 39
jrose@567 40 klassOop SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
jrose@567 41 = { NULL /*, NULL...*/ };
duke@435 42
duke@435 43 klassOop SystemDictionary::_box_klasses[T_VOID+1] = { NULL /*, NULL...*/ };
duke@435 44
duke@435 45 oop SystemDictionary::_java_system_loader = NULL;
duke@435 46
duke@435 47 bool SystemDictionary::_has_loadClassInternal = false;
duke@435 48 bool SystemDictionary::_has_checkPackageAccess = false;
duke@435 49
duke@435 50 // lazily initialized klass variables
duke@435 51 volatile klassOop SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
duke@435 52
duke@435 53
duke@435 54 // ----------------------------------------------------------------------------
duke@435 55 // Java-level SystemLoader
duke@435 56
duke@435 57 oop SystemDictionary::java_system_loader() {
duke@435 58 return _java_system_loader;
duke@435 59 }
duke@435 60
duke@435 61 void SystemDictionary::compute_java_system_loader(TRAPS) {
jrose@567 62 KlassHandle system_klass(THREAD, WK_KLASS(classloader_klass));
duke@435 63 JavaValue result(T_OBJECT);
duke@435 64 JavaCalls::call_static(&result,
jrose@567 65 KlassHandle(THREAD, WK_KLASS(classloader_klass)),
duke@435 66 vmSymbolHandles::getSystemClassLoader_name(),
duke@435 67 vmSymbolHandles::void_classloader_signature(),
duke@435 68 CHECK);
duke@435 69
duke@435 70 _java_system_loader = (oop)result.get_jobject();
duke@435 71 }
duke@435 72
duke@435 73
duke@435 74 // ----------------------------------------------------------------------------
duke@435 75 // debugging
duke@435 76
duke@435 77 #ifdef ASSERT
duke@435 78
duke@435 79 // return true if class_name contains no '.' (internal format is '/')
duke@435 80 bool SystemDictionary::is_internal_format(symbolHandle class_name) {
duke@435 81 if (class_name.not_null()) {
duke@435 82 ResourceMark rm;
duke@435 83 char* name = class_name->as_C_string();
duke@435 84 return strchr(name, '.') == NULL;
duke@435 85 } else {
duke@435 86 return true;
duke@435 87 }
duke@435 88 }
duke@435 89
duke@435 90 #endif
duke@435 91
duke@435 92 // ----------------------------------------------------------------------------
duke@435 93 // Resolving of classes
duke@435 94
duke@435 95 // Forwards to resolve_or_null
duke@435 96
duke@435 97 klassOop SystemDictionary::resolve_or_fail(symbolHandle class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) {
duke@435 98 klassOop klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
duke@435 99 if (HAS_PENDING_EXCEPTION || klass == NULL) {
duke@435 100 KlassHandle k_h(THREAD, klass);
duke@435 101 // can return a null klass
duke@435 102 klass = handle_resolution_exception(class_name, class_loader, protection_domain, throw_error, k_h, THREAD);
duke@435 103 }
duke@435 104 return klass;
duke@435 105 }
duke@435 106
duke@435 107 klassOop SystemDictionary::handle_resolution_exception(symbolHandle class_name, Handle class_loader, Handle protection_domain, bool throw_error, KlassHandle klass_h, TRAPS) {
duke@435 108 if (HAS_PENDING_EXCEPTION) {
duke@435 109 // If we have a pending exception we forward it to the caller, unless throw_error is true,
duke@435 110 // in which case we have to check whether the pending exception is a ClassNotFoundException,
duke@435 111 // and if so convert it to a NoClassDefFoundError
duke@435 112 // And chain the original ClassNotFoundException
duke@435 113 if (throw_error && PENDING_EXCEPTION->is_a(SystemDictionary::classNotFoundException_klass())) {
duke@435 114 ResourceMark rm(THREAD);
duke@435 115 assert(klass_h() == NULL, "Should not have result with exception pending");
duke@435 116 Handle e(THREAD, PENDING_EXCEPTION);
duke@435 117 CLEAR_PENDING_EXCEPTION;
duke@435 118 THROW_MSG_CAUSE_0(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string(), e);
duke@435 119 } else {
duke@435 120 return NULL;
duke@435 121 }
duke@435 122 }
duke@435 123 // Class not found, throw appropriate error or exception depending on value of throw_error
duke@435 124 if (klass_h() == NULL) {
duke@435 125 ResourceMark rm(THREAD);
duke@435 126 if (throw_error) {
duke@435 127 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string());
duke@435 128 } else {
duke@435 129 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string());
duke@435 130 }
duke@435 131 }
duke@435 132 return (klassOop)klass_h();
duke@435 133 }
duke@435 134
duke@435 135
duke@435 136 klassOop SystemDictionary::resolve_or_fail(symbolHandle class_name,
duke@435 137 bool throw_error, TRAPS)
duke@435 138 {
duke@435 139 return resolve_or_fail(class_name, Handle(), Handle(), throw_error, THREAD);
duke@435 140 }
duke@435 141
duke@435 142
duke@435 143 // Forwards to resolve_instance_class_or_null
duke@435 144
duke@435 145 klassOop SystemDictionary::resolve_or_null(symbolHandle class_name, Handle class_loader, Handle protection_domain, TRAPS) {
duke@435 146 assert(!THREAD->is_Compiler_thread(), "Can not load classes with the Compiler thread");
duke@435 147 if (FieldType::is_array(class_name())) {
duke@435 148 return resolve_array_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
duke@435 149 } else {
duke@435 150 return resolve_instance_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
duke@435 151 }
duke@435 152 }
duke@435 153
duke@435 154 klassOop SystemDictionary::resolve_or_null(symbolHandle class_name, TRAPS) {
duke@435 155 return resolve_or_null(class_name, Handle(), Handle(), THREAD);
duke@435 156 }
duke@435 157
duke@435 158 // Forwards to resolve_instance_class_or_null
duke@435 159
duke@435 160 klassOop SystemDictionary::resolve_array_class_or_null(symbolHandle class_name,
duke@435 161 Handle class_loader,
duke@435 162 Handle protection_domain,
duke@435 163 TRAPS) {
duke@435 164 assert(FieldType::is_array(class_name()), "must be array");
duke@435 165 jint dimension;
duke@435 166 symbolOop object_key;
duke@435 167 klassOop k = NULL;
duke@435 168 // dimension and object_key are assigned as a side-effect of this call
duke@435 169 BasicType t = FieldType::get_array_info(class_name(),
duke@435 170 &dimension,
duke@435 171 &object_key,
duke@435 172 CHECK_NULL);
duke@435 173
duke@435 174 if (t == T_OBJECT) {
duke@435 175 symbolHandle h_key(THREAD, object_key);
duke@435 176 // naked oop "k" is OK here -- we assign back into it
duke@435 177 k = SystemDictionary::resolve_instance_class_or_null(h_key,
duke@435 178 class_loader,
duke@435 179 protection_domain,
duke@435 180 CHECK_NULL);
duke@435 181 if (k != NULL) {
duke@435 182 k = Klass::cast(k)->array_klass(dimension, CHECK_NULL);
duke@435 183 }
duke@435 184 } else {
duke@435 185 k = Universe::typeArrayKlassObj(t);
duke@435 186 k = typeArrayKlass::cast(k)->array_klass(dimension, CHECK_NULL);
duke@435 187 }
duke@435 188 return k;
duke@435 189 }
duke@435 190
duke@435 191
duke@435 192 // Must be called for any super-class or super-interface resolution
duke@435 193 // during class definition to allow class circularity checking
duke@435 194 // super-interface callers:
duke@435 195 // parse_interfaces - for defineClass & jvmtiRedefineClasses
duke@435 196 // super-class callers:
duke@435 197 // ClassFileParser - for defineClass & jvmtiRedefineClasses
duke@435 198 // load_shared_class - while loading a class from shared archive
duke@435 199 // resolve_instance_class_or_fail:
duke@435 200 // when resolving a class that has an existing placeholder with
duke@435 201 // a saved superclass [i.e. a defineClass is currently in progress]
duke@435 202 // if another thread is trying to resolve the class, it must do
duke@435 203 // super-class checks on its own thread to catch class circularity
duke@435 204 // This last call is critical in class circularity checking for cases
duke@435 205 // where classloading is delegated to different threads and the
duke@435 206 // classloader lock is released.
duke@435 207 // Take the case: Base->Super->Base
duke@435 208 // 1. If thread T1 tries to do a defineClass of class Base
duke@435 209 // resolve_super_or_fail creates placeholder: T1, Base (super Super)
duke@435 210 // 2. resolve_instance_class_or_null does not find SD or placeholder for Super
duke@435 211 // so it tries to load Super
duke@435 212 // 3. If we load the class internally, or user classloader uses same thread
duke@435 213 // loadClassFromxxx or defineClass via parseClassFile Super ...
duke@435 214 // 3.1 resolve_super_or_fail creates placeholder: T1, Super (super Base)
duke@435 215 // 3.3 resolve_instance_class_or_null Base, finds placeholder for Base
duke@435 216 // 3.4 calls resolve_super_or_fail Base
duke@435 217 // 3.5 finds T1,Base -> throws class circularity
duke@435 218 //OR 4. If T2 tries to resolve Super via defineClass Super ...
duke@435 219 // 4.1 resolve_super_or_fail creates placeholder: T2, Super (super Base)
duke@435 220 // 4.2 resolve_instance_class_or_null Base, finds placeholder for Base (super Super)
duke@435 221 // 4.3 calls resolve_super_or_fail Super in parallel on own thread T2
duke@435 222 // 4.4 finds T2, Super -> throws class circularity
duke@435 223 // Must be called, even if superclass is null, since this is
duke@435 224 // where the placeholder entry is created which claims this
duke@435 225 // thread is loading this class/classloader.
duke@435 226 klassOop SystemDictionary::resolve_super_or_fail(symbolHandle child_name,
duke@435 227 symbolHandle class_name,
duke@435 228 Handle class_loader,
duke@435 229 Handle protection_domain,
duke@435 230 bool is_superclass,
duke@435 231 TRAPS) {
duke@435 232
jrose@567 233 // Try to get one of the well-known klasses.
jrose@567 234 // They are trusted, and do not participate in circularities.
jrose@567 235 if (LinkWellKnownClasses) {
jrose@567 236 klassOop k = find_well_known_klass(class_name());
jrose@567 237 if (k != NULL) {
jrose@567 238 return k;
jrose@567 239 }
jrose@567 240 }
jrose@567 241
duke@435 242 // Double-check, if child class is already loaded, just return super-class,interface
duke@435 243 // Don't add a placedholder if already loaded, i.e. already in system dictionary
duke@435 244 // Make sure there's a placeholder for the *child* before resolving.
duke@435 245 // Used as a claim that this thread is currently loading superclass/classloader
duke@435 246 // Used here for ClassCircularity checks and also for heap verification
duke@435 247 // (every instanceKlass in the heap needs to be in the system dictionary
duke@435 248 // or have a placeholder).
duke@435 249 // Must check ClassCircularity before checking if super class is already loaded
duke@435 250 //
duke@435 251 // We might not already have a placeholder if this child_name was
duke@435 252 // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
duke@435 253 // the name of the class might not be known until the stream is actually
duke@435 254 // parsed.
duke@435 255 // Bugs 4643874, 4715493
duke@435 256 // compute_hash can have a safepoint
duke@435 257
duke@435 258 unsigned int d_hash = dictionary()->compute_hash(child_name, class_loader);
duke@435 259 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 260 unsigned int p_hash = placeholders()->compute_hash(child_name, class_loader);
duke@435 261 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 262 // can't throw error holding a lock
duke@435 263 bool child_already_loaded = false;
duke@435 264 bool throw_circularity_error = false;
duke@435 265 {
duke@435 266 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 267 klassOop childk = find_class(d_index, d_hash, child_name, class_loader);
duke@435 268 klassOop quicksuperk;
duke@435 269 // to support // loading: if child done loading, just return superclass
duke@435 270 // if class_name, & class_loader don't match:
duke@435 271 // if initial define, SD update will give LinkageError
duke@435 272 // if redefine: compare_class_versions will give HIERARCHY_CHANGED
duke@435 273 // so we don't throw an exception here.
duke@435 274 // see: nsk redefclass014 & java.lang.instrument Instrument032
duke@435 275 if ((childk != NULL ) && (is_superclass) &&
duke@435 276 ((quicksuperk = instanceKlass::cast(childk)->super()) != NULL) &&
duke@435 277
duke@435 278 ((Klass::cast(quicksuperk)->name() == class_name()) &&
duke@435 279 (Klass::cast(quicksuperk)->class_loader() == class_loader()))) {
duke@435 280 return quicksuperk;
duke@435 281 } else {
duke@435 282 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader);
duke@435 283 if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
duke@435 284 throw_circularity_error = true;
duke@435 285 }
duke@435 286
duke@435 287 // add placeholder entry even if error - callers will remove on error
duke@435 288 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, class_loader, PlaceholderTable::LOAD_SUPER, class_name, THREAD);
duke@435 289 if (throw_circularity_error) {
duke@435 290 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER);
duke@435 291 }
duke@435 292 }
duke@435 293 }
duke@435 294 if (throw_circularity_error) {
duke@435 295 ResourceMark rm(THREAD);
duke@435 296 THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string());
duke@435 297 }
duke@435 298
duke@435 299 // java.lang.Object should have been found above
duke@435 300 assert(class_name() != NULL, "null super class for resolving");
duke@435 301 // Resolve the super class or interface, check results on return
duke@435 302 klassOop superk = NULL;
duke@435 303 superk = SystemDictionary::resolve_or_null(class_name,
duke@435 304 class_loader,
duke@435 305 protection_domain,
duke@435 306 THREAD);
duke@435 307
duke@435 308 KlassHandle superk_h(THREAD, superk);
duke@435 309
duke@435 310 // Note: clean up of placeholders currently in callers of
duke@435 311 // resolve_super_or_fail - either at update_dictionary time
duke@435 312 // or on error
duke@435 313 {
duke@435 314 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 315 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader);
duke@435 316 if (probe != NULL) {
duke@435 317 probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER);
duke@435 318 }
duke@435 319 }
duke@435 320 if (HAS_PENDING_EXCEPTION || superk_h() == NULL) {
duke@435 321 // can null superk
duke@435 322 superk_h = KlassHandle(THREAD, handle_resolution_exception(class_name, class_loader, protection_domain, true, superk_h, THREAD));
duke@435 323 }
duke@435 324
duke@435 325 return superk_h();
duke@435 326 }
duke@435 327
duke@435 328
duke@435 329 void SystemDictionary::validate_protection_domain(instanceKlassHandle klass,
duke@435 330 Handle class_loader,
duke@435 331 Handle protection_domain,
duke@435 332 TRAPS) {
duke@435 333 if(!has_checkPackageAccess()) return;
duke@435 334
duke@435 335 // Now we have to call back to java to check if the initating class has access
duke@435 336 JavaValue result(T_VOID);
duke@435 337 if (TraceProtectionDomainVerification) {
duke@435 338 // Print out trace information
duke@435 339 tty->print_cr("Checking package access");
duke@435 340 tty->print(" - class loader: "); class_loader()->print_value_on(tty); tty->cr();
duke@435 341 tty->print(" - protection domain: "); protection_domain()->print_value_on(tty); tty->cr();
duke@435 342 tty->print(" - loading: "); klass()->print_value_on(tty); tty->cr();
duke@435 343 }
duke@435 344
duke@435 345 assert(class_loader() != NULL, "should not have non-null protection domain for null classloader");
duke@435 346
duke@435 347 KlassHandle system_loader(THREAD, SystemDictionary::classloader_klass());
duke@435 348 JavaCalls::call_special(&result,
duke@435 349 class_loader,
duke@435 350 system_loader,
duke@435 351 vmSymbolHandles::checkPackageAccess_name(),
duke@435 352 vmSymbolHandles::class_protectiondomain_signature(),
duke@435 353 Handle(THREAD, klass->java_mirror()),
duke@435 354 protection_domain,
duke@435 355 THREAD);
duke@435 356
duke@435 357 if (TraceProtectionDomainVerification) {
duke@435 358 if (HAS_PENDING_EXCEPTION) {
duke@435 359 tty->print_cr(" -> DENIED !!!!!!!!!!!!!!!!!!!!!");
duke@435 360 } else {
duke@435 361 tty->print_cr(" -> granted");
duke@435 362 }
duke@435 363 tty->cr();
duke@435 364 }
duke@435 365
duke@435 366 if (HAS_PENDING_EXCEPTION) return;
duke@435 367
duke@435 368 // If no exception has been thrown, we have validated the protection domain
duke@435 369 // Insert the protection domain of the initiating class into the set.
duke@435 370 {
duke@435 371 // We recalculate the entry here -- we've called out to java since
duke@435 372 // the last time it was calculated.
duke@435 373 symbolHandle kn(THREAD, klass->name());
duke@435 374 unsigned int d_hash = dictionary()->compute_hash(kn, class_loader);
duke@435 375 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 376
duke@435 377 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 378 {
duke@435 379 // Note that we have an entry, and entries can be deleted only during GC,
duke@435 380 // so we cannot allow GC to occur while we're holding this entry.
duke@435 381
duke@435 382 // We're using a No_Safepoint_Verifier to catch any place where we
duke@435 383 // might potentially do a GC at all.
duke@435 384 // SystemDictionary::do_unloading() asserts that classes are only
duke@435 385 // unloaded at a safepoint.
duke@435 386 No_Safepoint_Verifier nosafepoint;
duke@435 387 dictionary()->add_protection_domain(d_index, d_hash, klass, class_loader,
duke@435 388 protection_domain, THREAD);
duke@435 389 }
duke@435 390 }
duke@435 391 }
duke@435 392
duke@435 393 // We only get here if this thread finds that another thread
duke@435 394 // has already claimed the placeholder token for the current operation,
duke@435 395 // but that other thread either never owned or gave up the
duke@435 396 // object lock
duke@435 397 // Waits on SystemDictionary_lock to indicate placeholder table updated
duke@435 398 // On return, caller must recheck placeholder table state
duke@435 399 //
duke@435 400 // We only get here if
duke@435 401 // 1) custom classLoader, i.e. not bootstrap classloader
duke@435 402 // 2) UnsyncloadClass not set
duke@435 403 // 3) custom classLoader has broken the class loader objectLock
duke@435 404 // so another thread got here in parallel
duke@435 405 //
duke@435 406 // lockObject must be held.
duke@435 407 // Complicated dance due to lock ordering:
duke@435 408 // Must first release the classloader object lock to
duke@435 409 // allow initial definer to complete the class definition
duke@435 410 // and to avoid deadlock
duke@435 411 // Reclaim classloader lock object with same original recursion count
duke@435 412 // Must release SystemDictionary_lock after notify, since
duke@435 413 // class loader lock must be claimed before SystemDictionary_lock
duke@435 414 // to prevent deadlocks
duke@435 415 //
duke@435 416 // The notify allows applications that did an untimed wait() on
duke@435 417 // the classloader object lock to not hang.
duke@435 418 void SystemDictionary::double_lock_wait(Handle lockObject, TRAPS) {
duke@435 419 assert_lock_strong(SystemDictionary_lock);
duke@435 420
duke@435 421 bool calledholdinglock
duke@435 422 = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject);
duke@435 423 assert(calledholdinglock,"must hold lock for notify");
duke@435 424 assert(!UnsyncloadClass, "unexpected double_lock_wait");
duke@435 425 ObjectSynchronizer::notifyall(lockObject, THREAD);
duke@435 426 intptr_t recursions = ObjectSynchronizer::complete_exit(lockObject, THREAD);
duke@435 427 SystemDictionary_lock->wait();
duke@435 428 SystemDictionary_lock->unlock();
duke@435 429 ObjectSynchronizer::reenter(lockObject, recursions, THREAD);
duke@435 430 SystemDictionary_lock->lock();
duke@435 431 }
duke@435 432
duke@435 433 // If the class in is in the placeholder table, class loading is in progress
duke@435 434 // For cases where the application changes threads to load classes, it
duke@435 435 // is critical to ClassCircularity detection that we try loading
duke@435 436 // the superclass on the same thread internally, so we do parallel
duke@435 437 // super class loading here.
duke@435 438 // This also is critical in cases where the original thread gets stalled
duke@435 439 // even in non-circularity situations.
duke@435 440 // Note: only one thread can define the class, but multiple can resolve
duke@435 441 // Note: must call resolve_super_or_fail even if null super -
duke@435 442 // to force placeholder entry creation for this class
duke@435 443 // Caller must check for pending exception
duke@435 444 // Returns non-null klassOop if other thread has completed load
duke@435 445 // and we are done,
duke@435 446 // If return null klassOop and no pending exception, the caller must load the class
duke@435 447 instanceKlassHandle SystemDictionary::handle_parallel_super_load(
duke@435 448 symbolHandle name, symbolHandle superclassname, Handle class_loader,
duke@435 449 Handle protection_domain, Handle lockObject, TRAPS) {
duke@435 450
duke@435 451 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
duke@435 452 unsigned int d_hash = dictionary()->compute_hash(name, class_loader);
duke@435 453 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 454 unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
duke@435 455 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 456
duke@435 457 // superk is not used, resolve_super called for circularity check only
duke@435 458 // This code is reached in two situations. One if this thread
duke@435 459 // is loading the same class twice (e.g. ClassCircularity, or
duke@435 460 // java.lang.instrument).
duke@435 461 // The second is if another thread started the resolve_super first
duke@435 462 // and has not yet finished.
duke@435 463 // In both cases the original caller will clean up the placeholder
duke@435 464 // entry on error.
duke@435 465 klassOop superk = SystemDictionary::resolve_super_or_fail(name,
duke@435 466 superclassname,
duke@435 467 class_loader,
duke@435 468 protection_domain,
duke@435 469 true,
duke@435 470 CHECK_(nh));
duke@435 471 // We don't redefine the class, so we just need to clean up if there
duke@435 472 // was not an error (don't want to modify any system dictionary
duke@435 473 // data structures).
duke@435 474 {
duke@435 475 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 476 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
duke@435 477 SystemDictionary_lock->notify_all();
duke@435 478 }
duke@435 479
duke@435 480 // UnsyncloadClass does NOT wait for parallel superclass loads to complete
duke@435 481 // Bootstrap classloader does wait for parallel superclass loads
duke@435 482 if (UnsyncloadClass) {
duke@435 483 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 484 // Check if classloading completed while we were loading superclass or waiting
duke@435 485 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 486 if (check != NULL) {
duke@435 487 // Klass is already loaded, so just return it
duke@435 488 return(instanceKlassHandle(THREAD, check));
duke@435 489 } else {
duke@435 490 return nh;
duke@435 491 }
duke@435 492 }
duke@435 493
duke@435 494 // must loop to both handle other placeholder updates
duke@435 495 // and spurious notifications
duke@435 496 bool super_load_in_progress = true;
duke@435 497 PlaceholderEntry* placeholder;
duke@435 498 while (super_load_in_progress) {
duke@435 499 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 500 // Check if classloading completed while we were loading superclass or waiting
duke@435 501 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 502 if (check != NULL) {
duke@435 503 // Klass is already loaded, so just return it
duke@435 504 return(instanceKlassHandle(THREAD, check));
duke@435 505 } else {
duke@435 506 placeholder = placeholders()->get_entry(p_index, p_hash, name, class_loader);
duke@435 507 if (placeholder && placeholder->super_load_in_progress() ){
duke@435 508 // Before UnsyncloadClass:
duke@435 509 // We only get here if the application has released the
duke@435 510 // classloader lock when another thread was in the middle of loading a
duke@435 511 // superclass/superinterface for this class, and now
duke@435 512 // this thread is also trying to load this class.
duke@435 513 // To minimize surprises, the first thread that started to
duke@435 514 // load a class should be the one to complete the loading
duke@435 515 // with the classfile it initially expected.
duke@435 516 // This logic has the current thread wait once it has done
duke@435 517 // all the superclass/superinterface loading it can, until
duke@435 518 // the original thread completes the class loading or fails
duke@435 519 // If it completes we will use the resulting instanceKlass
duke@435 520 // which we will find below in the systemDictionary.
duke@435 521 // We also get here for parallel bootstrap classloader
duke@435 522 if (class_loader.is_null()) {
duke@435 523 SystemDictionary_lock->wait();
duke@435 524 } else {
duke@435 525 double_lock_wait(lockObject, THREAD);
duke@435 526 }
duke@435 527 } else {
duke@435 528 // If not in SD and not in PH, other thread's load must have failed
duke@435 529 super_load_in_progress = false;
duke@435 530 }
duke@435 531 }
duke@435 532 }
duke@435 533 return (nh);
duke@435 534 }
duke@435 535
duke@435 536
duke@435 537 klassOop SystemDictionary::resolve_instance_class_or_null(symbolHandle class_name, Handle class_loader, Handle protection_domain, TRAPS) {
duke@435 538 assert(class_name.not_null() && !FieldType::is_array(class_name()), "invalid class name");
duke@435 539 // First check to see if we should remove wrapping L and ;
duke@435 540 symbolHandle name;
duke@435 541 if (FieldType::is_obj(class_name())) {
duke@435 542 ResourceMark rm(THREAD);
duke@435 543 // Ignore wrapping L and ;.
duke@435 544 name = oopFactory::new_symbol_handle(class_name()->as_C_string() + 1, class_name()->utf8_length() - 2, CHECK_NULL);
duke@435 545 } else {
duke@435 546 name = class_name;
duke@435 547 }
duke@435 548
duke@435 549 // UseNewReflection
duke@435 550 // Fix for 4474172; see evaluation for more details
duke@435 551 class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
duke@435 552
duke@435 553 // Do lookup to see if class already exist and the protection domain
duke@435 554 // has the right access
duke@435 555 unsigned int d_hash = dictionary()->compute_hash(name, class_loader);
duke@435 556 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 557 klassOop probe = dictionary()->find(d_index, d_hash, name, class_loader,
duke@435 558 protection_domain, THREAD);
duke@435 559 if (probe != NULL) return probe;
duke@435 560
duke@435 561
duke@435 562 // Non-bootstrap class loaders will call out to class loader and
duke@435 563 // define via jvm/jni_DefineClass which will acquire the
duke@435 564 // class loader object lock to protect against multiple threads
duke@435 565 // defining the class in parallel by accident.
duke@435 566 // This lock must be acquired here so the waiter will find
duke@435 567 // any successful result in the SystemDictionary and not attempt
duke@435 568 // the define
duke@435 569 // Classloaders that support parallelism, e.g. bootstrap classloader,
duke@435 570 // or all classloaders with UnsyncloadClass do not acquire lock here
duke@435 571 bool DoObjectLock = true;
duke@435 572 if (UnsyncloadClass || (class_loader.is_null())) {
duke@435 573 DoObjectLock = false;
duke@435 574 }
duke@435 575
duke@435 576 unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
duke@435 577 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 578
duke@435 579 // Class is not in SystemDictionary so we have to do loading.
duke@435 580 // Make sure we are synchronized on the class loader before we proceed
duke@435 581 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
duke@435 582 check_loader_lock_contention(lockObject, THREAD);
duke@435 583 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
duke@435 584
duke@435 585 // Check again (after locking) if class already exist in SystemDictionary
duke@435 586 bool class_has_been_loaded = false;
duke@435 587 bool super_load_in_progress = false;
duke@435 588 bool havesupername = false;
duke@435 589 instanceKlassHandle k;
duke@435 590 PlaceholderEntry* placeholder;
duke@435 591 symbolHandle superclassname;
duke@435 592
duke@435 593 {
duke@435 594 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 595 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 596 if (check != NULL) {
duke@435 597 // Klass is already loaded, so just return it
duke@435 598 class_has_been_loaded = true;
duke@435 599 k = instanceKlassHandle(THREAD, check);
duke@435 600 } else {
duke@435 601 placeholder = placeholders()->get_entry(p_index, p_hash, name, class_loader);
duke@435 602 if (placeholder && placeholder->super_load_in_progress()) {
duke@435 603 super_load_in_progress = true;
duke@435 604 if (placeholder->havesupername() == true) {
duke@435 605 superclassname = symbolHandle(THREAD, placeholder->supername());
duke@435 606 havesupername = true;
duke@435 607 }
duke@435 608 }
duke@435 609 }
duke@435 610 }
duke@435 611
duke@435 612 // If the class in is in the placeholder table, class loading is in progress
duke@435 613 if (super_load_in_progress && havesupername==true) {
duke@435 614 k = SystemDictionary::handle_parallel_super_load(name, superclassname,
duke@435 615 class_loader, protection_domain, lockObject, THREAD);
duke@435 616 if (HAS_PENDING_EXCEPTION) {
duke@435 617 return NULL;
duke@435 618 }
duke@435 619 if (!k.is_null()) {
duke@435 620 class_has_been_loaded = true;
duke@435 621 }
duke@435 622 }
duke@435 623
duke@435 624 if (!class_has_been_loaded) {
duke@435 625
duke@435 626 // add placeholder entry to record loading instance class
duke@435 627 // Five cases:
duke@435 628 // All cases need to prevent modifying bootclasssearchpath
duke@435 629 // in parallel with a classload of same classname
duke@435 630 // case 1. traditional classloaders that rely on the classloader object lock
duke@435 631 // - no other need for LOAD_INSTANCE
duke@435 632 // case 2. traditional classloaders that break the classloader object lock
duke@435 633 // as a deadlock workaround. Detection of this case requires that
duke@435 634 // this check is done while holding the classloader object lock,
duke@435 635 // and that lock is still held when calling classloader's loadClass.
duke@435 636 // For these classloaders, we ensure that the first requestor
duke@435 637 // completes the load and other requestors wait for completion.
duke@435 638 // case 3. UnsyncloadClass - don't use objectLocker
duke@435 639 // With this flag, we allow parallel classloading of a
duke@435 640 // class/classloader pair
duke@435 641 // case4. Bootstrap classloader - don't own objectLocker
duke@435 642 // This classloader supports parallelism at the classloader level,
duke@435 643 // but only allows a single load of a class/classloader pair.
duke@435 644 // No performance benefit and no deadlock issues.
duke@435 645 // case 5. Future: parallel user level classloaders - without objectLocker
duke@435 646 symbolHandle nullsymbolHandle;
duke@435 647 bool throw_circularity_error = false;
duke@435 648 {
duke@435 649 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 650 if (!UnsyncloadClass) {
duke@435 651 PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
duke@435 652 if (oldprobe) {
duke@435 653 // only need check_seen_thread once, not on each loop
duke@435 654 // 6341374 java/lang/Instrument with -Xcomp
duke@435 655 if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
duke@435 656 throw_circularity_error = true;
duke@435 657 } else {
duke@435 658 // case 1: traditional: should never see load_in_progress.
duke@435 659 while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
duke@435 660
duke@435 661 // case 4: bootstrap classloader: prevent futile classloading,
duke@435 662 // wait on first requestor
duke@435 663 if (class_loader.is_null()) {
duke@435 664 SystemDictionary_lock->wait();
duke@435 665 } else {
duke@435 666 // case 2: traditional with broken classloader lock. wait on first
duke@435 667 // requestor.
duke@435 668 double_lock_wait(lockObject, THREAD);
duke@435 669 }
duke@435 670 // Check if classloading completed while we were waiting
duke@435 671 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 672 if (check != NULL) {
duke@435 673 // Klass is already loaded, so just return it
duke@435 674 k = instanceKlassHandle(THREAD, check);
duke@435 675 class_has_been_loaded = true;
duke@435 676 }
duke@435 677 // check if other thread failed to load and cleaned up
duke@435 678 oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
duke@435 679 }
duke@435 680 }
duke@435 681 }
duke@435 682 }
duke@435 683 // All cases: add LOAD_INSTANCE
duke@435 684 // case 3: UnsyncloadClass: allow competing threads to try
duke@435 685 // LOAD_INSTANCE in parallel
duke@435 686 // add placeholder entry even if error - callers will remove on error
duke@435 687 if (!class_has_been_loaded) {
duke@435 688 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, class_loader, PlaceholderTable::LOAD_INSTANCE, nullsymbolHandle, THREAD);
duke@435 689 if (throw_circularity_error) {
duke@435 690 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
duke@435 691 }
duke@435 692 // For class loaders that do not acquire the classloader object lock,
duke@435 693 // if they did not catch another thread holding LOAD_INSTANCE,
duke@435 694 // need a check analogous to the acquire ObjectLocker/find_class
duke@435 695 // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
duke@435 696 // one final check if the load has already completed
duke@435 697 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 698 if (check != NULL) {
duke@435 699 // Klass is already loaded, so just return it
duke@435 700 k = instanceKlassHandle(THREAD, check);
duke@435 701 class_has_been_loaded = true;
duke@435 702 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
duke@435 703 }
duke@435 704 }
duke@435 705 }
duke@435 706 // must throw error outside of owning lock
duke@435 707 if (throw_circularity_error) {
duke@435 708 ResourceMark rm(THREAD);
duke@435 709 THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string());
duke@435 710 }
duke@435 711
duke@435 712 if (!class_has_been_loaded) {
duke@435 713
duke@435 714 // Do actual loading
duke@435 715 k = load_instance_class(name, class_loader, THREAD);
duke@435 716
duke@435 717 // In custom class loaders, the usual findClass calls
duke@435 718 // findLoadedClass, which directly searches the SystemDictionary, then
duke@435 719 // defineClass. If these are not atomic with respect to other threads,
duke@435 720 // the findLoadedClass can fail, but the defineClass can get a
duke@435 721 // LinkageError:: duplicate class definition.
duke@435 722 // If they got a linkageError, check if a parallel class load succeeded.
duke@435 723 // If it did, then for bytecode resolution the specification requires
duke@435 724 // that we return the same result we did for the other thread, i.e. the
duke@435 725 // successfully loaded instanceKlass
duke@435 726 // Note: Class can not be unloaded as long as any classloader refs exist
duke@435 727 // Should not get here for classloaders that support parallelism
duke@435 728 // with the new cleaner mechanism, e.g. bootstrap classloader
duke@435 729 if (UnsyncloadClass || (class_loader.is_null())) {
duke@435 730 if (k.is_null() && HAS_PENDING_EXCEPTION
duke@435 731 && PENDING_EXCEPTION->is_a(SystemDictionary::linkageError_klass())) {
duke@435 732 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 733 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 734 if (check != NULL) {
duke@435 735 // Klass is already loaded, so just use it
duke@435 736 k = instanceKlassHandle(THREAD, check);
duke@435 737 CLEAR_PENDING_EXCEPTION;
duke@435 738 guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
duke@435 739 }
duke@435 740 }
duke@435 741 }
duke@435 742
duke@435 743 // clean up placeholder entries for success or error
duke@435 744 // This cleans up LOAD_INSTANCE entries
duke@435 745 // It also cleans up LOAD_SUPER entries on errors from
duke@435 746 // calling load_instance_class
duke@435 747 {
duke@435 748 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 749 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
duke@435 750 if (probe != NULL) {
duke@435 751 probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
duke@435 752 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
duke@435 753 SystemDictionary_lock->notify_all();
duke@435 754 }
duke@435 755 }
duke@435 756
duke@435 757 // If everything was OK (no exceptions, no null return value), and
duke@435 758 // class_loader is NOT the defining loader, do a little more bookkeeping.
duke@435 759 if (!HAS_PENDING_EXCEPTION && !k.is_null() &&
duke@435 760 k->class_loader() != class_loader()) {
duke@435 761
duke@435 762 check_constraints(d_index, d_hash, k, class_loader, false, THREAD);
duke@435 763
duke@435 764 // Need to check for a PENDING_EXCEPTION again; check_constraints
duke@435 765 // can throw and doesn't use the CHECK macro.
duke@435 766 if (!HAS_PENDING_EXCEPTION) {
duke@435 767 { // Grabbing the Compile_lock prevents systemDictionary updates
duke@435 768 // during compilations.
duke@435 769 MutexLocker mu(Compile_lock, THREAD);
duke@435 770 update_dictionary(d_index, d_hash, p_index, p_hash,
duke@435 771 k, class_loader, THREAD);
duke@435 772 }
duke@435 773 if (JvmtiExport::should_post_class_load()) {
duke@435 774 Thread *thread = THREAD;
duke@435 775 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
duke@435 776 JvmtiExport::post_class_load((JavaThread *) thread, k());
duke@435 777 }
duke@435 778 }
duke@435 779 }
duke@435 780 if (HAS_PENDING_EXCEPTION || k.is_null()) {
duke@435 781 // On error, clean up placeholders
duke@435 782 {
duke@435 783 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 784 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
duke@435 785 SystemDictionary_lock->notify_all();
duke@435 786 }
duke@435 787 return NULL;
duke@435 788 }
duke@435 789 }
duke@435 790 }
duke@435 791
duke@435 792 #ifdef ASSERT
duke@435 793 {
duke@435 794 Handle loader (THREAD, k->class_loader());
duke@435 795 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 796 oop kk = find_class_or_placeholder(name, loader);
duke@435 797 assert(kk == k(), "should be present in dictionary");
duke@435 798 }
duke@435 799 #endif
duke@435 800
duke@435 801 // return if the protection domain in NULL
duke@435 802 if (protection_domain() == NULL) return k();
duke@435 803
duke@435 804 // Check the protection domain has the right access
duke@435 805 {
duke@435 806 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 807 // Note that we have an entry, and entries can be deleted only during GC,
duke@435 808 // so we cannot allow GC to occur while we're holding this entry.
duke@435 809 // We're using a No_Safepoint_Verifier to catch any place where we
duke@435 810 // might potentially do a GC at all.
duke@435 811 // SystemDictionary::do_unloading() asserts that classes are only
duke@435 812 // unloaded at a safepoint.
duke@435 813 No_Safepoint_Verifier nosafepoint;
duke@435 814 if (dictionary()->is_valid_protection_domain(d_index, d_hash, name,
duke@435 815 class_loader,
duke@435 816 protection_domain)) {
duke@435 817 return k();
duke@435 818 }
duke@435 819 }
duke@435 820
duke@435 821 // Verify protection domain. If it fails an exception is thrown
duke@435 822 validate_protection_domain(k, class_loader, protection_domain, CHECK_(klassOop(NULL)));
duke@435 823
duke@435 824 return k();
duke@435 825 }
duke@435 826
duke@435 827
duke@435 828 // This routine does not lock the system dictionary.
duke@435 829 //
duke@435 830 // Since readers don't hold a lock, we must make sure that system
duke@435 831 // dictionary entries are only removed at a safepoint (when only one
duke@435 832 // thread is running), and are added to in a safe way (all links must
duke@435 833 // be updated in an MT-safe manner).
duke@435 834 //
duke@435 835 // Callers should be aware that an entry could be added just after
duke@435 836 // _dictionary->bucket(index) is read here, so the caller will not see
duke@435 837 // the new entry.
duke@435 838
duke@435 839 klassOop SystemDictionary::find(symbolHandle class_name,
duke@435 840 Handle class_loader,
duke@435 841 Handle protection_domain,
duke@435 842 TRAPS) {
duke@435 843
duke@435 844 unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
duke@435 845 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 846
duke@435 847 {
duke@435 848 // Note that we have an entry, and entries can be deleted only during GC,
duke@435 849 // so we cannot allow GC to occur while we're holding this entry.
duke@435 850 // We're using a No_Safepoint_Verifier to catch any place where we
duke@435 851 // might potentially do a GC at all.
duke@435 852 // SystemDictionary::do_unloading() asserts that classes are only
duke@435 853 // unloaded at a safepoint.
duke@435 854 No_Safepoint_Verifier nosafepoint;
duke@435 855 return dictionary()->find(d_index, d_hash, class_name, class_loader,
duke@435 856 protection_domain, THREAD);
duke@435 857 }
duke@435 858 }
duke@435 859
duke@435 860
duke@435 861 // Look for a loaded instance or array klass by name. Do not do any loading.
duke@435 862 // return NULL in case of error.
duke@435 863 klassOop SystemDictionary::find_instance_or_array_klass(symbolHandle class_name,
duke@435 864 Handle class_loader,
duke@435 865 Handle protection_domain,
duke@435 866 TRAPS) {
duke@435 867 klassOop k = NULL;
duke@435 868 assert(class_name() != NULL, "class name must be non NULL");
jrose@567 869
jrose@567 870 // Try to get one of the well-known klasses.
jrose@567 871 if (LinkWellKnownClasses) {
jrose@567 872 k = find_well_known_klass(class_name());
jrose@567 873 if (k != NULL) {
jrose@567 874 return k;
jrose@567 875 }
jrose@567 876 }
jrose@567 877
duke@435 878 if (FieldType::is_array(class_name())) {
duke@435 879 // The name refers to an array. Parse the name.
duke@435 880 jint dimension;
duke@435 881 symbolOop object_key;
duke@435 882
duke@435 883 // dimension and object_key are assigned as a side-effect of this call
duke@435 884 BasicType t = FieldType::get_array_info(class_name(), &dimension,
duke@435 885 &object_key, CHECK_(NULL));
duke@435 886 if (t != T_OBJECT) {
duke@435 887 k = Universe::typeArrayKlassObj(t);
duke@435 888 } else {
duke@435 889 symbolHandle h_key(THREAD, object_key);
duke@435 890 k = SystemDictionary::find(h_key, class_loader, protection_domain, THREAD);
duke@435 891 }
duke@435 892 if (k != NULL) {
duke@435 893 k = Klass::cast(k)->array_klass_or_null(dimension);
duke@435 894 }
duke@435 895 } else {
duke@435 896 k = find(class_name, class_loader, protection_domain, THREAD);
duke@435 897 }
duke@435 898 return k;
duke@435 899 }
duke@435 900
jrose@567 901 // Quick range check for names of well-known classes:
jrose@567 902 static symbolOop wk_klass_name_limits[2] = {NULL, NULL};
jrose@567 903
jrose@567 904 #ifndef PRODUCT
jrose@567 905 static int find_wkk_calls, find_wkk_probes, find_wkk_wins;
jrose@567 906 // counts for "hello world": 3983, 1616, 1075
jrose@567 907 // => 60% hit after limit guard, 25% total win rate
jrose@567 908 #endif
jrose@567 909
jrose@567 910 klassOop SystemDictionary::find_well_known_klass(symbolOop class_name) {
jrose@567 911 // A bounds-check on class_name will quickly get a negative result.
jrose@567 912 NOT_PRODUCT(find_wkk_calls++);
jrose@567 913 if (class_name >= wk_klass_name_limits[0] &&
jrose@567 914 class_name <= wk_klass_name_limits[1]) {
jrose@567 915 NOT_PRODUCT(find_wkk_probes++);
jrose@567 916 vmSymbols::SID sid = vmSymbols::find_sid(class_name);
jrose@567 917 if (sid != vmSymbols::NO_SID) {
jrose@567 918 klassOop k = NULL;
jrose@567 919 switch (sid) {
jrose@567 920 #define WK_KLASS_CASE(name, symbol, ignore_option) \
jrose@567 921 case vmSymbols::VM_SYMBOL_ENUM_NAME(symbol): \
jrose@567 922 k = WK_KLASS(name); break;
jrose@567 923 WK_KLASSES_DO(WK_KLASS_CASE)
jrose@567 924 #undef WK_KLASS_CASE
jrose@567 925 }
jrose@567 926 NOT_PRODUCT(if (k != NULL) find_wkk_wins++);
jrose@567 927 return k;
jrose@567 928 }
jrose@567 929 }
jrose@567 930 return NULL;
jrose@567 931 }
jrose@567 932
duke@435 933 // Note: this method is much like resolve_from_stream, but
duke@435 934 // updates no supplemental data structures.
duke@435 935 // TODO consolidate the two methods with a helper routine?
duke@435 936 klassOop SystemDictionary::parse_stream(symbolHandle class_name,
duke@435 937 Handle class_loader,
duke@435 938 Handle protection_domain,
duke@435 939 ClassFileStream* st,
duke@435 940 TRAPS) {
duke@435 941 symbolHandle parsed_name;
duke@435 942
duke@435 943 // Parse the stream. Note that we do this even though this klass might
duke@435 944 // already be present in the SystemDictionary, otherwise we would not
duke@435 945 // throw potential ClassFormatErrors.
duke@435 946 //
duke@435 947 // Note: "name" is updated.
duke@435 948 // Further note: a placeholder will be added for this class when
duke@435 949 // super classes are loaded (resolve_super_or_fail). We expect this
duke@435 950 // to be called for all classes but java.lang.Object; and we preload
duke@435 951 // java.lang.Object through resolve_or_fail, not this path.
duke@435 952
duke@435 953 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
duke@435 954 class_loader,
duke@435 955 protection_domain,
duke@435 956 parsed_name,
duke@435 957 THREAD);
duke@435 958
duke@435 959
duke@435 960 // We don't redefine the class, so we just need to clean up whether there
duke@435 961 // was an error or not (don't want to modify any system dictionary
duke@435 962 // data structures).
duke@435 963 // Parsed name could be null if we threw an error before we got far
duke@435 964 // enough along to parse it -- in that case, there is nothing to clean up.
duke@435 965 if (!parsed_name.is_null()) {
duke@435 966 unsigned int p_hash = placeholders()->compute_hash(parsed_name,
duke@435 967 class_loader);
duke@435 968 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 969 {
duke@435 970 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 971 placeholders()->find_and_remove(p_index, p_hash, parsed_name, class_loader, THREAD);
duke@435 972 SystemDictionary_lock->notify_all();
duke@435 973 }
duke@435 974 }
duke@435 975
duke@435 976 return k();
duke@435 977 }
duke@435 978
duke@435 979 // Add a klass to the system from a stream (called by jni_DefineClass and
duke@435 980 // JVM_DefineClass).
duke@435 981 // Note: class_name can be NULL. In that case we do not know the name of
duke@435 982 // the class until we have parsed the stream.
duke@435 983
duke@435 984 klassOop SystemDictionary::resolve_from_stream(symbolHandle class_name,
duke@435 985 Handle class_loader,
duke@435 986 Handle protection_domain,
duke@435 987 ClassFileStream* st,
duke@435 988 TRAPS) {
duke@435 989
duke@435 990 // Make sure we are synchronized on the class loader before we initiate
duke@435 991 // loading.
duke@435 992 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
duke@435 993 check_loader_lock_contention(lockObject, THREAD);
duke@435 994 ObjectLocker ol(lockObject, THREAD);
duke@435 995
duke@435 996 symbolHandle parsed_name;
duke@435 997
duke@435 998 // Parse the stream. Note that we do this even though this klass might
duke@435 999 // already be present in the SystemDictionary, otherwise we would not
duke@435 1000 // throw potential ClassFormatErrors.
duke@435 1001 //
duke@435 1002 // Note: "name" is updated.
duke@435 1003 // Further note: a placeholder will be added for this class when
duke@435 1004 // super classes are loaded (resolve_super_or_fail). We expect this
duke@435 1005 // to be called for all classes but java.lang.Object; and we preload
duke@435 1006 // java.lang.Object through resolve_or_fail, not this path.
duke@435 1007
duke@435 1008 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
duke@435 1009 class_loader,
duke@435 1010 protection_domain,
duke@435 1011 parsed_name,
duke@435 1012 THREAD);
duke@435 1013
duke@435 1014 const char* pkg = "java/";
duke@435 1015 if (!HAS_PENDING_EXCEPTION &&
duke@435 1016 !class_loader.is_null() &&
duke@435 1017 !parsed_name.is_null() &&
duke@435 1018 !strncmp((const char*)parsed_name->bytes(), pkg, strlen(pkg))) {
duke@435 1019 // It is illegal to define classes in the "java." package from
duke@435 1020 // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
duke@435 1021 ResourceMark rm(THREAD);
duke@435 1022 char* name = parsed_name->as_C_string();
duke@435 1023 char* index = strrchr(name, '/');
duke@435 1024 *index = '\0'; // chop to just the package name
duke@435 1025 while ((index = strchr(name, '/')) != NULL) {
duke@435 1026 *index = '.'; // replace '/' with '.' in package name
duke@435 1027 }
duke@435 1028 const char* fmt = "Prohibited package name: %s";
duke@435 1029 size_t len = strlen(fmt) + strlen(name);
duke@435 1030 char* message = NEW_RESOURCE_ARRAY(char, len);
duke@435 1031 jio_snprintf(message, len, fmt, name);
duke@435 1032 Exceptions::_throw_msg(THREAD_AND_LOCATION,
duke@435 1033 vmSymbols::java_lang_SecurityException(), message);
duke@435 1034 }
duke@435 1035
duke@435 1036 if (!HAS_PENDING_EXCEPTION) {
duke@435 1037 assert(!parsed_name.is_null(), "Sanity");
duke@435 1038 assert(class_name.is_null() || class_name() == parsed_name(),
duke@435 1039 "name mismatch");
duke@435 1040 // Verification prevents us from creating names with dots in them, this
duke@435 1041 // asserts that that's the case.
duke@435 1042 assert(is_internal_format(parsed_name),
duke@435 1043 "external class name format used internally");
duke@435 1044
duke@435 1045 // Add class just loaded
duke@435 1046 define_instance_class(k, THREAD);
duke@435 1047 }
duke@435 1048
duke@435 1049 // If parsing the class file or define_instance_class failed, we
duke@435 1050 // need to remove the placeholder added on our behalf. But we
duke@435 1051 // must make sure parsed_name is valid first (it won't be if we had
duke@435 1052 // a format error before the class was parsed far enough to
duke@435 1053 // find the name).
duke@435 1054 if (HAS_PENDING_EXCEPTION && !parsed_name.is_null()) {
duke@435 1055 unsigned int p_hash = placeholders()->compute_hash(parsed_name,
duke@435 1056 class_loader);
duke@435 1057 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 1058 {
duke@435 1059 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 1060 placeholders()->find_and_remove(p_index, p_hash, parsed_name, class_loader, THREAD);
duke@435 1061 SystemDictionary_lock->notify_all();
duke@435 1062 }
duke@435 1063 return NULL;
duke@435 1064 }
duke@435 1065
duke@435 1066 // Make sure that we didn't leave a place holder in the
duke@435 1067 // SystemDictionary; this is only done on success
duke@435 1068 debug_only( {
duke@435 1069 if (!HAS_PENDING_EXCEPTION) {
duke@435 1070 assert(!parsed_name.is_null(), "parsed_name is still null?");
duke@435 1071 symbolHandle h_name (THREAD, k->name());
duke@435 1072 Handle h_loader (THREAD, k->class_loader());
duke@435 1073
duke@435 1074 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 1075
duke@435 1076 oop check = find_class_or_placeholder(parsed_name, class_loader);
duke@435 1077 assert(check == k(), "should be present in the dictionary");
duke@435 1078
duke@435 1079 oop check2 = find_class_or_placeholder(h_name, h_loader);
duke@435 1080 assert(check == check2, "name inconsistancy in SystemDictionary");
duke@435 1081 }
duke@435 1082 } );
duke@435 1083
duke@435 1084 return k();
duke@435 1085 }
duke@435 1086
duke@435 1087
duke@435 1088 void SystemDictionary::set_shared_dictionary(HashtableBucket* t, int length,
duke@435 1089 int number_of_entries) {
duke@435 1090 assert(length == _nof_buckets * sizeof(HashtableBucket),
duke@435 1091 "bad shared dictionary size.");
duke@435 1092 _shared_dictionary = new Dictionary(_nof_buckets, t, number_of_entries);
duke@435 1093 }
duke@435 1094
duke@435 1095
duke@435 1096 // If there is a shared dictionary, then find the entry for the
duke@435 1097 // given shared system class, if any.
duke@435 1098
duke@435 1099 klassOop SystemDictionary::find_shared_class(symbolHandle class_name) {
duke@435 1100 if (shared_dictionary() != NULL) {
duke@435 1101 unsigned int d_hash = dictionary()->compute_hash(class_name, Handle());
duke@435 1102 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 1103 return shared_dictionary()->find_shared_class(d_index, d_hash, class_name);
duke@435 1104 } else {
duke@435 1105 return NULL;
duke@435 1106 }
duke@435 1107 }
duke@435 1108
duke@435 1109
duke@435 1110 // Load a class from the shared spaces (found through the shared system
duke@435 1111 // dictionary). Force the superclass and all interfaces to be loaded.
duke@435 1112 // Update the class definition to include sibling classes and no
duke@435 1113 // subclasses (yet). [Classes in the shared space are not part of the
duke@435 1114 // object hierarchy until loaded.]
duke@435 1115
duke@435 1116 instanceKlassHandle SystemDictionary::load_shared_class(
duke@435 1117 symbolHandle class_name, Handle class_loader, TRAPS) {
duke@435 1118 instanceKlassHandle ik (THREAD, find_shared_class(class_name));
duke@435 1119 return load_shared_class(ik, class_loader, THREAD);
duke@435 1120 }
duke@435 1121
duke@435 1122 // Note well! Changes to this method may affect oop access order
duke@435 1123 // in the shared archive. Please take care to not make changes that
duke@435 1124 // adversely affect cold start time by changing the oop access order
duke@435 1125 // that is specified in dump.cpp MarkAndMoveOrderedReadOnly and
duke@435 1126 // MarkAndMoveOrderedReadWrite closures.
duke@435 1127 instanceKlassHandle SystemDictionary::load_shared_class(
duke@435 1128 instanceKlassHandle ik, Handle class_loader, TRAPS) {
duke@435 1129 assert(class_loader.is_null(), "non-null classloader for shared class?");
duke@435 1130 if (ik.not_null()) {
duke@435 1131 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
duke@435 1132 symbolHandle class_name(THREAD, ik->name());
duke@435 1133
duke@435 1134 // Found the class, now load the superclass and interfaces. If they
duke@435 1135 // are shared, add them to the main system dictionary and reset
duke@435 1136 // their hierarchy references (supers, subs, and interfaces).
duke@435 1137
duke@435 1138 if (ik->super() != NULL) {
duke@435 1139 symbolHandle cn(THREAD, ik->super()->klass_part()->name());
duke@435 1140 resolve_super_or_fail(class_name, cn,
duke@435 1141 class_loader, Handle(), true, CHECK_(nh));
duke@435 1142 }
duke@435 1143
duke@435 1144 objArrayHandle interfaces (THREAD, ik->local_interfaces());
duke@435 1145 int num_interfaces = interfaces->length();
duke@435 1146 for (int index = 0; index < num_interfaces; index++) {
duke@435 1147 klassOop k = klassOop(interfaces->obj_at(index));
duke@435 1148
duke@435 1149 // Note: can not use instanceKlass::cast here because
duke@435 1150 // interfaces' instanceKlass's C++ vtbls haven't been
duke@435 1151 // reinitialized yet (they will be once the interface classes
duke@435 1152 // are loaded)
duke@435 1153 symbolHandle name (THREAD, k->klass_part()->name());
duke@435 1154 resolve_super_or_fail(class_name, name, class_loader, Handle(), false, CHECK_(nh));
duke@435 1155 }
duke@435 1156
duke@435 1157 // Adjust methods to recover missing data. They need addresses for
duke@435 1158 // interpreter entry points and their default native method address
duke@435 1159 // must be reset.
duke@435 1160
duke@435 1161 // Updating methods must be done under a lock so multiple
duke@435 1162 // threads don't update these in parallel
duke@435 1163 // Shared classes are all currently loaded by the bootstrap
duke@435 1164 // classloader, so this will never cause a deadlock on
duke@435 1165 // a custom class loader lock.
duke@435 1166
duke@435 1167 {
duke@435 1168 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
duke@435 1169 check_loader_lock_contention(lockObject, THREAD);
duke@435 1170 ObjectLocker ol(lockObject, THREAD, true);
duke@435 1171
duke@435 1172 objArrayHandle methods (THREAD, ik->methods());
duke@435 1173 int num_methods = methods->length();
duke@435 1174 for (int index2 = 0; index2 < num_methods; ++index2) {
duke@435 1175 methodHandle m(THREAD, methodOop(methods->obj_at(index2)));
duke@435 1176 m()->link_method(m, CHECK_(nh));
duke@435 1177 }
duke@435 1178 }
duke@435 1179
duke@435 1180 if (TraceClassLoading) {
duke@435 1181 ResourceMark rm;
duke@435 1182 tty->print("[Loaded %s", ik->external_name());
duke@435 1183 tty->print(" from shared objects file");
duke@435 1184 tty->print_cr("]");
duke@435 1185 }
duke@435 1186 // notify a class loaded from shared object
duke@435 1187 ClassLoadingService::notify_class_loaded(instanceKlass::cast(ik()),
duke@435 1188 true /* shared class */);
duke@435 1189 }
duke@435 1190 return ik;
duke@435 1191 }
duke@435 1192
duke@435 1193 #ifdef KERNEL
duke@435 1194 // Some classes on the bootstrap class path haven't been installed on the
duke@435 1195 // system yet. Call the DownloadManager method to make them appear in the
duke@435 1196 // bootstrap class path and try again to load the named class.
duke@435 1197 // Note that with delegation class loaders all classes in another loader will
duke@435 1198 // first try to call this so it'd better be fast!!
duke@435 1199 static instanceKlassHandle download_and_retry_class_load(
duke@435 1200 symbolHandle class_name,
duke@435 1201 TRAPS) {
duke@435 1202
duke@435 1203 klassOop dlm = SystemDictionary::sun_jkernel_DownloadManager_klass();
duke@435 1204 instanceKlassHandle nk;
duke@435 1205
duke@435 1206 // If download manager class isn't loaded just return.
duke@435 1207 if (dlm == NULL) return nk;
duke@435 1208
duke@435 1209 { HandleMark hm(THREAD);
duke@435 1210 ResourceMark rm(THREAD);
duke@435 1211 Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nk));
duke@435 1212 Handle class_string = java_lang_String::externalize_classname(s, CHECK_(nk));
duke@435 1213
duke@435 1214 // return value
duke@435 1215 JavaValue result(T_OBJECT);
duke@435 1216
duke@435 1217 // Call the DownloadManager. We assume that it has a lock because
duke@435 1218 // multiple classes could be not found and downloaded at the same time.
duke@435 1219 // class sun.misc.DownloadManager;
duke@435 1220 // public static String getBootClassPathEntryForClass(String className);
duke@435 1221 JavaCalls::call_static(&result,
duke@435 1222 KlassHandle(THREAD, dlm),
duke@435 1223 vmSymbolHandles::getBootClassPathEntryForClass_name(),
duke@435 1224 vmSymbolHandles::string_string_signature(),
duke@435 1225 class_string,
duke@435 1226 CHECK_(nk));
duke@435 1227
duke@435 1228 // Get result.string and add to bootclasspath
duke@435 1229 assert(result.get_type() == T_OBJECT, "just checking");
duke@435 1230 oop obj = (oop) result.get_jobject();
duke@435 1231 if (obj == NULL) { return nk; }
duke@435 1232
coleenp@457 1233 Handle h_obj(THREAD, obj);
coleenp@457 1234 char* new_class_name = java_lang_String::as_platform_dependent_str(h_obj,
coleenp@457 1235 CHECK_(nk));
duke@435 1236
duke@435 1237 // lock the loader
duke@435 1238 // we use this lock because JVMTI does.
duke@435 1239 Handle loader_lock(THREAD, SystemDictionary::system_loader_lock());
duke@435 1240
duke@435 1241 ObjectLocker ol(loader_lock, THREAD);
duke@435 1242 // add the file to the bootclasspath
duke@435 1243 ClassLoader::update_class_path_entry_list(new_class_name, true);
duke@435 1244 } // end HandleMark
duke@435 1245
duke@435 1246 if (TraceClassLoading) {
duke@435 1247 ClassLoader::print_bootclasspath();
duke@435 1248 }
duke@435 1249 return ClassLoader::load_classfile(class_name, CHECK_(nk));
duke@435 1250 }
duke@435 1251 #endif // KERNEL
duke@435 1252
duke@435 1253
duke@435 1254 instanceKlassHandle SystemDictionary::load_instance_class(symbolHandle class_name, Handle class_loader, TRAPS) {
duke@435 1255 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
duke@435 1256 if (class_loader.is_null()) {
duke@435 1257 // Search the shared system dictionary for classes preloaded into the
duke@435 1258 // shared spaces.
duke@435 1259 instanceKlassHandle k;
duke@435 1260 k = load_shared_class(class_name, class_loader, THREAD);
duke@435 1261
duke@435 1262 if (k.is_null()) {
duke@435 1263 // Use VM class loader
duke@435 1264 k = ClassLoader::load_classfile(class_name, CHECK_(nh));
duke@435 1265 }
duke@435 1266
duke@435 1267 #ifdef KERNEL
duke@435 1268 // If the VM class loader has failed to load the class, call the
duke@435 1269 // DownloadManager class to make it magically appear on the classpath
duke@435 1270 // and try again. This is only configured with the Kernel VM.
duke@435 1271 if (k.is_null()) {
duke@435 1272 k = download_and_retry_class_load(class_name, CHECK_(nh));
duke@435 1273 }
duke@435 1274 #endif // KERNEL
duke@435 1275
duke@435 1276 // find_or_define_instance_class may return a different k
duke@435 1277 if (!k.is_null()) {
duke@435 1278 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
duke@435 1279 }
duke@435 1280 return k;
duke@435 1281 } else {
duke@435 1282 // Use user specified class loader to load class. Call loadClass operation on class_loader.
duke@435 1283 ResourceMark rm(THREAD);
duke@435 1284
duke@435 1285 Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nh));
duke@435 1286 // Translate to external class name format, i.e., convert '/' chars to '.'
duke@435 1287 Handle string = java_lang_String::externalize_classname(s, CHECK_(nh));
duke@435 1288
duke@435 1289 JavaValue result(T_OBJECT);
duke@435 1290
duke@435 1291 KlassHandle spec_klass (THREAD, SystemDictionary::classloader_klass());
duke@435 1292
duke@435 1293 // UnsyncloadClass option means don't synchronize loadClass() calls.
duke@435 1294 // loadClassInternal() is synchronized and public loadClass(String) is not.
duke@435 1295 // This flag is for diagnostic purposes only. It is risky to call
duke@435 1296 // custom class loaders without synchronization.
duke@435 1297 // WARNING If a custom class loader does NOT synchronizer findClass, or callers of
duke@435 1298 // findClass, this flag risks unexpected timing bugs in the field.
duke@435 1299 // Do NOT assume this will be supported in future releases.
duke@435 1300 if (!UnsyncloadClass && has_loadClassInternal()) {
duke@435 1301 JavaCalls::call_special(&result,
duke@435 1302 class_loader,
duke@435 1303 spec_klass,
duke@435 1304 vmSymbolHandles::loadClassInternal_name(),
duke@435 1305 vmSymbolHandles::string_class_signature(),
duke@435 1306 string,
duke@435 1307 CHECK_(nh));
duke@435 1308 } else {
duke@435 1309 JavaCalls::call_virtual(&result,
duke@435 1310 class_loader,
duke@435 1311 spec_klass,
duke@435 1312 vmSymbolHandles::loadClass_name(),
duke@435 1313 vmSymbolHandles::string_class_signature(),
duke@435 1314 string,
duke@435 1315 CHECK_(nh));
duke@435 1316 }
duke@435 1317
duke@435 1318 assert(result.get_type() == T_OBJECT, "just checking");
duke@435 1319 oop obj = (oop) result.get_jobject();
duke@435 1320
duke@435 1321 // Primitive classes return null since forName() can not be
duke@435 1322 // used to obtain any of the Class objects representing primitives or void
duke@435 1323 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
duke@435 1324 instanceKlassHandle k =
duke@435 1325 instanceKlassHandle(THREAD, java_lang_Class::as_klassOop(obj));
duke@435 1326 // For user defined Java class loaders, check that the name returned is
duke@435 1327 // the same as that requested. This check is done for the bootstrap
duke@435 1328 // loader when parsing the class file.
duke@435 1329 if (class_name() == k->name()) {
duke@435 1330 return k;
duke@435 1331 }
duke@435 1332 }
duke@435 1333 // Class is not found or has the wrong name, return NULL
duke@435 1334 return nh;
duke@435 1335 }
duke@435 1336 }
duke@435 1337
duke@435 1338 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
duke@435 1339
duke@435 1340 Handle class_loader_h(THREAD, k->class_loader());
duke@435 1341
duke@435 1342 // for bootstrap classloader don't acquire lock
duke@435 1343 if (!class_loader_h.is_null()) {
duke@435 1344 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
duke@435 1345 compute_loader_lock_object(class_loader_h, THREAD)),
duke@435 1346 "define called without lock");
duke@435 1347 }
duke@435 1348
duke@435 1349
duke@435 1350 // Check class-loading constraints. Throw exception if violation is detected.
duke@435 1351 // Grabs and releases SystemDictionary_lock
duke@435 1352 // The check_constraints/find_class call and update_dictionary sequence
duke@435 1353 // must be "atomic" for a specific class/classloader pair so we never
duke@435 1354 // define two different instanceKlasses for that class/classloader pair.
duke@435 1355 // Existing classloaders will call define_instance_class with the
duke@435 1356 // classloader lock held
duke@435 1357 // Parallel classloaders will call find_or_define_instance_class
duke@435 1358 // which will require a token to perform the define class
duke@435 1359 symbolHandle name_h(THREAD, k->name());
duke@435 1360 unsigned int d_hash = dictionary()->compute_hash(name_h, class_loader_h);
duke@435 1361 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 1362 check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
duke@435 1363
duke@435 1364 // Register class just loaded with class loader (placed in Vector)
duke@435 1365 // Note we do this before updating the dictionary, as this can
duke@435 1366 // fail with an OutOfMemoryError (if it does, we will *not* put this
duke@435 1367 // class in the dictionary and will not update the class hierarchy).
duke@435 1368 if (k->class_loader() != NULL) {
duke@435 1369 methodHandle m(THREAD, Universe::loader_addClass_method());
duke@435 1370 JavaValue result(T_VOID);
duke@435 1371 JavaCallArguments args(class_loader_h);
duke@435 1372 args.push_oop(Handle(THREAD, k->java_mirror()));
duke@435 1373 JavaCalls::call(&result, m, &args, CHECK);
duke@435 1374 }
duke@435 1375
duke@435 1376 // Add the new class. We need recompile lock during update of CHA.
duke@435 1377 {
duke@435 1378 unsigned int p_hash = placeholders()->compute_hash(name_h, class_loader_h);
duke@435 1379 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 1380
duke@435 1381 MutexLocker mu_r(Compile_lock, THREAD);
duke@435 1382
duke@435 1383 // Add to class hierarchy, initialize vtables, and do possible
duke@435 1384 // deoptimizations.
duke@435 1385 add_to_hierarchy(k, CHECK); // No exception, but can block
duke@435 1386
duke@435 1387 // Add to systemDictionary - so other classes can see it.
duke@435 1388 // Grabs and releases SystemDictionary_lock
duke@435 1389 update_dictionary(d_index, d_hash, p_index, p_hash,
duke@435 1390 k, class_loader_h, THREAD);
duke@435 1391 }
duke@435 1392 k->eager_initialize(THREAD);
duke@435 1393
duke@435 1394 // notify jvmti
duke@435 1395 if (JvmtiExport::should_post_class_load()) {
duke@435 1396 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
duke@435 1397 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
duke@435 1398
duke@435 1399 }
duke@435 1400 }
duke@435 1401
duke@435 1402 // Support parallel classloading
duke@435 1403 // Initial implementation for bootstrap classloader
duke@435 1404 // For future:
duke@435 1405 // For custom class loaders that support parallel classloading,
duke@435 1406 // in case they do not synchronize around
duke@435 1407 // FindLoadedClass/DefineClass calls, we check for parallel
duke@435 1408 // loading for them, wait if a defineClass is in progress
duke@435 1409 // and return the initial requestor's results
duke@435 1410 // For better performance, the class loaders should synchronize
duke@435 1411 // findClass(), i.e. FindLoadedClass/DefineClass or they
duke@435 1412 // potentially waste time reading and parsing the bytestream.
duke@435 1413 // Note: VM callers should ensure consistency of k/class_name,class_loader
duke@435 1414 instanceKlassHandle SystemDictionary::find_or_define_instance_class(symbolHandle class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
duke@435 1415
duke@435 1416 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
duke@435 1417
duke@435 1418 unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
duke@435 1419 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 1420
duke@435 1421 // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
duke@435 1422 unsigned int p_hash = placeholders()->compute_hash(class_name, class_loader);
duke@435 1423 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 1424 PlaceholderEntry* probe;
duke@435 1425
duke@435 1426 {
duke@435 1427 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 1428 // First check if class already defined
duke@435 1429 klassOop check = find_class(d_index, d_hash, class_name, class_loader);
duke@435 1430 if (check != NULL) {
duke@435 1431 return(instanceKlassHandle(THREAD, check));
duke@435 1432 }
duke@435 1433
duke@435 1434 // Acquire define token for this class/classloader
duke@435 1435 symbolHandle nullsymbolHandle;
duke@435 1436 probe = placeholders()->find_and_add(p_index, p_hash, class_name, class_loader, PlaceholderTable::DEFINE_CLASS, nullsymbolHandle, THREAD);
duke@435 1437 // Check if another thread defining in parallel
duke@435 1438 if (probe->definer() == NULL) {
duke@435 1439 // Thread will define the class
duke@435 1440 probe->set_definer(THREAD);
duke@435 1441 } else {
duke@435 1442 // Wait for defining thread to finish and return results
duke@435 1443 while (probe->definer() != NULL) {
duke@435 1444 SystemDictionary_lock->wait();
duke@435 1445 }
duke@435 1446 if (probe->instanceKlass() != NULL) {
duke@435 1447 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
duke@435 1448 return(instanceKlassHandle(THREAD, probe->instanceKlass()));
duke@435 1449 } else {
duke@435 1450 // If definer had an error, try again as any new thread would
duke@435 1451 probe->set_definer(THREAD);
duke@435 1452 #ifdef ASSERT
duke@435 1453 klassOop check = find_class(d_index, d_hash, class_name, class_loader);
duke@435 1454 assert(check == NULL, "definer missed recording success");
duke@435 1455 #endif
duke@435 1456 }
duke@435 1457 }
duke@435 1458 }
duke@435 1459
duke@435 1460 define_instance_class(k, THREAD);
duke@435 1461
duke@435 1462 Handle linkage_exception = Handle(); // null handle
duke@435 1463
duke@435 1464 // definer must notify any waiting threads
duke@435 1465 {
duke@435 1466 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 1467 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, class_name, class_loader);
duke@435 1468 assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
duke@435 1469 if (probe != NULL) {
duke@435 1470 if (HAS_PENDING_EXCEPTION) {
duke@435 1471 linkage_exception = Handle(THREAD,PENDING_EXCEPTION);
duke@435 1472 CLEAR_PENDING_EXCEPTION;
duke@435 1473 } else {
duke@435 1474 probe->set_instanceKlass(k());
duke@435 1475 }
duke@435 1476 probe->set_definer(NULL);
duke@435 1477 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
duke@435 1478 SystemDictionary_lock->notify_all();
duke@435 1479 }
duke@435 1480 }
duke@435 1481
duke@435 1482 // Can't throw exception while holding lock due to rank ordering
duke@435 1483 if (linkage_exception() != NULL) {
duke@435 1484 THROW_OOP_(linkage_exception(), nh); // throws exception and returns
duke@435 1485 }
duke@435 1486
duke@435 1487 return k;
duke@435 1488 }
duke@435 1489
duke@435 1490 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) {
duke@435 1491 // If class_loader is NULL we synchronize on _system_loader_lock_obj
duke@435 1492 if (class_loader.is_null()) {
duke@435 1493 return Handle(THREAD, _system_loader_lock_obj);
duke@435 1494 } else {
duke@435 1495 return class_loader;
duke@435 1496 }
duke@435 1497 }
duke@435 1498
duke@435 1499 // This method is added to check how often we have to wait to grab loader
duke@435 1500 // lock. The results are being recorded in the performance counters defined in
duke@435 1501 // ClassLoader::_sync_systemLoaderLockContentionRate and
duke@435 1502 // ClassLoader::_sync_nonSystemLoaderLockConteionRate.
duke@435 1503 void SystemDictionary::check_loader_lock_contention(Handle loader_lock, TRAPS) {
duke@435 1504 if (!UsePerfData) {
duke@435 1505 return;
duke@435 1506 }
duke@435 1507
duke@435 1508 assert(!loader_lock.is_null(), "NULL lock object");
duke@435 1509
duke@435 1510 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader_lock)
duke@435 1511 == ObjectSynchronizer::owner_other) {
duke@435 1512 // contention will likely happen, so increment the corresponding
duke@435 1513 // contention counter.
duke@435 1514 if (loader_lock() == _system_loader_lock_obj) {
duke@435 1515 ClassLoader::sync_systemLoaderLockContentionRate()->inc();
duke@435 1516 } else {
duke@435 1517 ClassLoader::sync_nonSystemLoaderLockContentionRate()->inc();
duke@435 1518 }
duke@435 1519 }
duke@435 1520 }
duke@435 1521
duke@435 1522 // ----------------------------------------------------------------------------
duke@435 1523 // Lookup
duke@435 1524
duke@435 1525 klassOop SystemDictionary::find_class(int index, unsigned int hash,
duke@435 1526 symbolHandle class_name,
duke@435 1527 Handle class_loader) {
duke@435 1528 assert_locked_or_safepoint(SystemDictionary_lock);
duke@435 1529 assert (index == dictionary()->index_for(class_name, class_loader),
duke@435 1530 "incorrect index?");
duke@435 1531
duke@435 1532 klassOop k = dictionary()->find_class(index, hash, class_name, class_loader);
duke@435 1533 return k;
duke@435 1534 }
duke@435 1535
duke@435 1536
duke@435 1537 // Basic find on classes in the midst of being loaded
duke@435 1538 symbolOop SystemDictionary::find_placeholder(int index, unsigned int hash,
duke@435 1539 symbolHandle class_name,
duke@435 1540 Handle class_loader) {
duke@435 1541 assert_locked_or_safepoint(SystemDictionary_lock);
duke@435 1542
duke@435 1543 return placeholders()->find_entry(index, hash, class_name, class_loader);
duke@435 1544 }
duke@435 1545
duke@435 1546
duke@435 1547 // Used for assertions and verification only
duke@435 1548 oop SystemDictionary::find_class_or_placeholder(symbolHandle class_name,
duke@435 1549 Handle class_loader) {
duke@435 1550 #ifndef ASSERT
duke@435 1551 guarantee(VerifyBeforeGC ||
duke@435 1552 VerifyDuringGC ||
duke@435 1553 VerifyBeforeExit ||
duke@435 1554 VerifyAfterGC, "too expensive");
duke@435 1555 #endif
duke@435 1556 assert_locked_or_safepoint(SystemDictionary_lock);
duke@435 1557 symbolOop class_name_ = class_name();
duke@435 1558 oop class_loader_ = class_loader();
duke@435 1559
duke@435 1560 // First look in the loaded class array
duke@435 1561 unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
duke@435 1562 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 1563 oop lookup = find_class(d_index, d_hash, class_name, class_loader);
duke@435 1564
duke@435 1565 if (lookup == NULL) {
duke@435 1566 // Next try the placeholders
duke@435 1567 unsigned int p_hash = placeholders()->compute_hash(class_name,class_loader);
duke@435 1568 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 1569 lookup = find_placeholder(p_index, p_hash, class_name, class_loader);
duke@435 1570 }
duke@435 1571
duke@435 1572 return lookup;
duke@435 1573 }
duke@435 1574
duke@435 1575
duke@435 1576 // Get the next class in the diictionary.
duke@435 1577 klassOop SystemDictionary::try_get_next_class() {
duke@435 1578 return dictionary()->try_get_next_class();
duke@435 1579 }
duke@435 1580
duke@435 1581
duke@435 1582 // ----------------------------------------------------------------------------
duke@435 1583 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
duke@435 1584 // is held, to ensure that the compiler is not using the class hierachy, and that deoptimization will kick in
duke@435 1585 // before a new class is used.
duke@435 1586
duke@435 1587 void SystemDictionary::add_to_hierarchy(instanceKlassHandle k, TRAPS) {
duke@435 1588 assert(k.not_null(), "just checking");
duke@435 1589 // Link into hierachy. Make sure the vtables are initialized before linking into
duke@435 1590 k->append_to_sibling_list(); // add to superklass/sibling list
duke@435 1591 k->process_interfaces(THREAD); // handle all "implements" declarations
duke@435 1592 k->set_init_state(instanceKlass::loaded);
duke@435 1593 // Now flush all code that depended on old class hierarchy.
duke@435 1594 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
duke@435 1595 // Also, first reinitialize vtable because it may have gotten out of synch
duke@435 1596 // while the new class wasn't connected to the class hierarchy.
duke@435 1597 Universe::flush_dependents_on(k);
duke@435 1598 }
duke@435 1599
duke@435 1600
duke@435 1601 // ----------------------------------------------------------------------------
duke@435 1602 // GC support
duke@435 1603
duke@435 1604 // Following roots during mark-sweep is separated in two phases.
duke@435 1605 //
duke@435 1606 // The first phase follows preloaded classes and all other system
duke@435 1607 // classes, since these will never get unloaded anyway.
duke@435 1608 //
duke@435 1609 // The second phase removes (unloads) unreachable classes from the
duke@435 1610 // system dictionary and follows the remaining classes' contents.
duke@435 1611
duke@435 1612 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
duke@435 1613 // Follow preloaded classes/mirrors and system loader object
duke@435 1614 blk->do_oop(&_java_system_loader);
duke@435 1615 preloaded_oops_do(blk);
duke@435 1616 always_strong_classes_do(blk);
duke@435 1617 }
duke@435 1618
duke@435 1619
duke@435 1620 void SystemDictionary::always_strong_classes_do(OopClosure* blk) {
duke@435 1621 // Follow all system classes and temporary placeholders in dictionary
duke@435 1622 dictionary()->always_strong_classes_do(blk);
duke@435 1623
duke@435 1624 // Placeholders. These are *always* strong roots, as they
duke@435 1625 // represent classes we're actively loading.
duke@435 1626 placeholders_do(blk);
duke@435 1627
duke@435 1628 // Loader constraints. We must keep the symbolOop used in the name alive.
duke@435 1629 constraints()->always_strong_classes_do(blk);
duke@435 1630
duke@435 1631 // Resolution errors keep the symbolOop for the error alive
duke@435 1632 resolution_errors()->always_strong_classes_do(blk);
duke@435 1633 }
duke@435 1634
duke@435 1635
duke@435 1636 void SystemDictionary::placeholders_do(OopClosure* blk) {
duke@435 1637 placeholders()->oops_do(blk);
duke@435 1638 }
duke@435 1639
duke@435 1640
duke@435 1641 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive) {
duke@435 1642 bool result = dictionary()->do_unloading(is_alive);
duke@435 1643 constraints()->purge_loader_constraints(is_alive);
duke@435 1644 resolution_errors()->purge_resolution_errors(is_alive);
duke@435 1645 return result;
duke@435 1646 }
duke@435 1647
duke@435 1648
duke@435 1649 // The mirrors are scanned by shared_oops_do() which is
duke@435 1650 // not called by oops_do(). In order to process oops in
duke@435 1651 // a necessary order, shared_oops_do() is call by
duke@435 1652 // Universe::oops_do().
duke@435 1653 void SystemDictionary::oops_do(OopClosure* f) {
duke@435 1654 // Adjust preloaded classes and system loader object
duke@435 1655 f->do_oop(&_java_system_loader);
duke@435 1656 preloaded_oops_do(f);
duke@435 1657
duke@435 1658 lazily_loaded_oops_do(f);
duke@435 1659
duke@435 1660 // Adjust dictionary
duke@435 1661 dictionary()->oops_do(f);
duke@435 1662
duke@435 1663 // Partially loaded classes
duke@435 1664 placeholders()->oops_do(f);
duke@435 1665
duke@435 1666 // Adjust constraint table
duke@435 1667 constraints()->oops_do(f);
duke@435 1668
duke@435 1669 // Adjust resolution error table
duke@435 1670 resolution_errors()->oops_do(f);
duke@435 1671 }
duke@435 1672
duke@435 1673
duke@435 1674 void SystemDictionary::preloaded_oops_do(OopClosure* f) {
jrose@567 1675 f->do_oop((oop*) &wk_klass_name_limits[0]);
jrose@567 1676 f->do_oop((oop*) &wk_klass_name_limits[1]);
duke@435 1677
jrose@567 1678 for (int k = (int)FIRST_WKID; k < (int)WKID_LIMIT; k++) {
jrose@567 1679 f->do_oop((oop*) &_well_known_klasses[k]);
jrose@567 1680 }
duke@435 1681
duke@435 1682 {
duke@435 1683 for (int i = 0; i < T_VOID+1; i++) {
duke@435 1684 if (_box_klasses[i] != NULL) {
duke@435 1685 assert(i >= T_BOOLEAN, "checking");
duke@435 1686 f->do_oop((oop*) &_box_klasses[i]);
duke@435 1687 }
duke@435 1688 }
duke@435 1689 }
duke@435 1690
duke@435 1691 // The basic type mirrors would have already been processed in
duke@435 1692 // Universe::oops_do(), via a call to shared_oops_do(), so should
duke@435 1693 // not be processed again.
duke@435 1694
duke@435 1695 f->do_oop((oop*) &_system_loader_lock_obj);
duke@435 1696 FilteredFieldsMap::klasses_oops_do(f);
duke@435 1697 }
duke@435 1698
duke@435 1699 void SystemDictionary::lazily_loaded_oops_do(OopClosure* f) {
duke@435 1700 f->do_oop((oop*) &_abstract_ownable_synchronizer_klass);
duke@435 1701 }
duke@435 1702
duke@435 1703 // Just the classes from defining class loaders
duke@435 1704 // Don't iterate over placeholders
duke@435 1705 void SystemDictionary::classes_do(void f(klassOop)) {
duke@435 1706 dictionary()->classes_do(f);
duke@435 1707 }
duke@435 1708
duke@435 1709 // Added for initialize_itable_for_klass
duke@435 1710 // Just the classes from defining class loaders
duke@435 1711 // Don't iterate over placeholders
duke@435 1712 void SystemDictionary::classes_do(void f(klassOop, TRAPS), TRAPS) {
duke@435 1713 dictionary()->classes_do(f, CHECK);
duke@435 1714 }
duke@435 1715
duke@435 1716 // All classes, and their class loaders
duke@435 1717 // Don't iterate over placeholders
duke@435 1718 void SystemDictionary::classes_do(void f(klassOop, oop)) {
duke@435 1719 dictionary()->classes_do(f);
duke@435 1720 }
duke@435 1721
duke@435 1722 // All classes, and their class loaders
duke@435 1723 // (added for helpers that use HandleMarks and ResourceMarks)
duke@435 1724 // Don't iterate over placeholders
duke@435 1725 void SystemDictionary::classes_do(void f(klassOop, oop, TRAPS), TRAPS) {
duke@435 1726 dictionary()->classes_do(f, CHECK);
duke@435 1727 }
duke@435 1728
duke@435 1729 void SystemDictionary::placeholders_do(void f(symbolOop, oop)) {
duke@435 1730 placeholders()->entries_do(f);
duke@435 1731 }
duke@435 1732
duke@435 1733 void SystemDictionary::methods_do(void f(methodOop)) {
duke@435 1734 dictionary()->methods_do(f);
duke@435 1735 }
duke@435 1736
duke@435 1737 // ----------------------------------------------------------------------------
duke@435 1738 // Lazily load klasses
duke@435 1739
duke@435 1740 void SystemDictionary::load_abstract_ownable_synchronizer_klass(TRAPS) {
duke@435 1741 assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later");
duke@435 1742
duke@435 1743 // if multiple threads calling this function, only one thread will load
duke@435 1744 // the class. The other threads will find the loaded version once the
duke@435 1745 // class is loaded.
duke@435 1746 klassOop aos = _abstract_ownable_synchronizer_klass;
duke@435 1747 if (aos == NULL) {
duke@435 1748 klassOop k = resolve_or_fail(vmSymbolHandles::java_util_concurrent_locks_AbstractOwnableSynchronizer(), true, CHECK);
duke@435 1749 // Force a fence to prevent any read before the write completes
duke@435 1750 OrderAccess::fence();
duke@435 1751 _abstract_ownable_synchronizer_klass = k;
duke@435 1752 }
duke@435 1753 }
duke@435 1754
duke@435 1755 // ----------------------------------------------------------------------------
duke@435 1756 // Initialization
duke@435 1757
duke@435 1758 void SystemDictionary::initialize(TRAPS) {
duke@435 1759 // Allocate arrays
duke@435 1760 assert(dictionary() == NULL,
duke@435 1761 "SystemDictionary should only be initialized once");
duke@435 1762 _dictionary = new Dictionary(_nof_buckets);
duke@435 1763 _placeholders = new PlaceholderTable(_nof_buckets);
duke@435 1764 _number_of_modifications = 0;
duke@435 1765 _loader_constraints = new LoaderConstraintTable(_loader_constraint_size);
duke@435 1766 _resolution_errors = new ResolutionErrorTable(_resolution_error_size);
duke@435 1767
duke@435 1768 // Allocate private object used as system class loader lock
duke@435 1769 _system_loader_lock_obj = oopFactory::new_system_objArray(0, CHECK);
duke@435 1770 // Initialize basic classes
duke@435 1771 initialize_preloaded_classes(CHECK);
duke@435 1772 }
duke@435 1773
jrose@567 1774 // Compact table of directions on the initialization of klasses:
jrose@567 1775 static const short wk_init_info[] = {
jrose@567 1776 #define WK_KLASS_INIT_INFO(name, symbol, option) \
jrose@567 1777 ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
jrose@567 1778 << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
jrose@567 1779 | (int)SystemDictionary::option ),
jrose@567 1780 WK_KLASSES_DO(WK_KLASS_INIT_INFO)
jrose@567 1781 #undef WK_KLASS_INIT_INFO
jrose@567 1782 0
jrose@567 1783 };
jrose@567 1784
jrose@567 1785 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
jrose@567 1786 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
jrose@567 1787 int info = wk_init_info[id - FIRST_WKID];
jrose@567 1788 int sid = (info >> CEIL_LG_OPTION_LIMIT);
jrose@567 1789 symbolHandle symbol = vmSymbolHandles::symbol_handle_at((vmSymbols::SID)sid);
jrose@567 1790 klassOop* klassp = &_well_known_klasses[id];
jrose@567 1791 bool must_load = (init_opt < SystemDictionary::Opt);
jrose@567 1792 bool try_load = true;
jrose@567 1793 if (init_opt == SystemDictionary::Opt_Kernel) {
jrose@567 1794 #ifndef KERNEL
jrose@567 1795 try_load = false;
jrose@567 1796 #endif //KERNEL
jrose@567 1797 }
jrose@567 1798 if ((*klassp) == NULL && try_load) {
jrose@567 1799 if (must_load) {
jrose@567 1800 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class
jrose@567 1801 } else {
jrose@567 1802 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass
jrose@567 1803 }
jrose@567 1804 }
jrose@567 1805 return ((*klassp) != NULL);
jrose@567 1806 }
jrose@567 1807
jrose@567 1808 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) {
jrose@567 1809 assert((int)start_id <= (int)limit_id, "IDs are out of order!");
jrose@567 1810 for (int id = (int)start_id; id < (int)limit_id; id++) {
jrose@567 1811 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
jrose@567 1812 int info = wk_init_info[id - FIRST_WKID];
jrose@567 1813 int sid = (info >> CEIL_LG_OPTION_LIMIT);
jrose@567 1814 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT));
jrose@567 1815
jrose@567 1816 initialize_wk_klass((WKID)id, opt, CHECK);
jrose@567 1817
jrose@567 1818 // Update limits, so find_well_known_klass can be very fast:
jrose@567 1819 symbolOop s = vmSymbols::symbol_at((vmSymbols::SID)sid);
jrose@567 1820 if (wk_klass_name_limits[1] == NULL) {
jrose@567 1821 wk_klass_name_limits[0] = wk_klass_name_limits[1] = s;
jrose@567 1822 } else if (wk_klass_name_limits[1] < s) {
jrose@567 1823 wk_klass_name_limits[1] = s;
jrose@567 1824 } else if (wk_klass_name_limits[0] > s) {
jrose@567 1825 wk_klass_name_limits[0] = s;
jrose@567 1826 }
jrose@567 1827 }
jrose@567 1828 }
jrose@567 1829
duke@435 1830
duke@435 1831 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
jrose@567 1832 assert(WK_KLASS(object_klass) == NULL, "preloaded classes should only be initialized once");
duke@435 1833 // Preload commonly used klasses
jrose@567 1834 WKID scan = FIRST_WKID;
jrose@567 1835 // first do Object, String, Class
jrose@567 1836 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(class_klass), scan, CHECK);
jrose@567 1837
jrose@567 1838 debug_only(instanceKlass::verify_class_klass_nonstatic_oop_maps(WK_KLASS(class_klass)));
jrose@567 1839
duke@435 1840 // Fixup mirrors for classes loaded before java.lang.Class.
duke@435 1841 // These calls iterate over the objects currently in the perm gen
duke@435 1842 // so calling them at this point is matters (not before when there
duke@435 1843 // are fewer objects and not later after there are more objects
duke@435 1844 // in the perm gen.
duke@435 1845 Universe::initialize_basic_type_mirrors(CHECK);
duke@435 1846 Universe::fixup_mirrors(CHECK);
duke@435 1847
jrose@567 1848 // do a bunch more:
jrose@567 1849 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(reference_klass), scan, CHECK);
duke@435 1850
duke@435 1851 // Preload ref klasses and set reference types
jrose@567 1852 instanceKlass::cast(WK_KLASS(reference_klass))->set_reference_type(REF_OTHER);
jrose@567 1853 instanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(reference_klass));
duke@435 1854
jrose@567 1855 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(phantom_reference_klass), scan, CHECK);
jrose@567 1856 instanceKlass::cast(WK_KLASS(soft_reference_klass))->set_reference_type(REF_SOFT);
jrose@567 1857 instanceKlass::cast(WK_KLASS(weak_reference_klass))->set_reference_type(REF_WEAK);
jrose@567 1858 instanceKlass::cast(WK_KLASS(final_reference_klass))->set_reference_type(REF_FINAL);
jrose@567 1859 instanceKlass::cast(WK_KLASS(phantom_reference_klass))->set_reference_type(REF_PHANTOM);
duke@435 1860
jrose@567 1861 initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK);
duke@435 1862
jrose@567 1863 _box_klasses[T_BOOLEAN] = WK_KLASS(boolean_klass);
jrose@567 1864 _box_klasses[T_CHAR] = WK_KLASS(char_klass);
jrose@567 1865 _box_klasses[T_FLOAT] = WK_KLASS(float_klass);
jrose@567 1866 _box_klasses[T_DOUBLE] = WK_KLASS(double_klass);
jrose@567 1867 _box_klasses[T_BYTE] = WK_KLASS(byte_klass);
jrose@567 1868 _box_klasses[T_SHORT] = WK_KLASS(short_klass);
jrose@567 1869 _box_klasses[T_INT] = WK_KLASS(int_klass);
jrose@567 1870 _box_klasses[T_LONG] = WK_KLASS(long_klass);
jrose@567 1871 //_box_klasses[T_OBJECT] = WK_KLASS(object_klass);
jrose@567 1872 //_box_klasses[T_ARRAY] = WK_KLASS(object_klass);
duke@435 1873
duke@435 1874 #ifdef KERNEL
jrose@567 1875 if (sun_jkernel_DownloadManager_klass() == NULL) {
duke@435 1876 warning("Cannot find sun/jkernel/DownloadManager");
duke@435 1877 }
duke@435 1878 #endif // KERNEL
duke@435 1879 { // Compute whether we should use loadClass or loadClassInternal when loading classes.
duke@435 1880 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());
duke@435 1881 _has_loadClassInternal = (method != NULL);
duke@435 1882 }
duke@435 1883
duke@435 1884 { // Compute whether we should use checkPackageAccess or NOT
duke@435 1885 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
duke@435 1886 _has_checkPackageAccess = (method != NULL);
duke@435 1887 }
duke@435 1888 }
duke@435 1889
duke@435 1890 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
duke@435 1891 // If so, returns the basic type it holds. If not, returns T_OBJECT.
duke@435 1892 BasicType SystemDictionary::box_klass_type(klassOop k) {
duke@435 1893 assert(k != NULL, "");
duke@435 1894 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
duke@435 1895 if (_box_klasses[i] == k)
duke@435 1896 return (BasicType)i;
duke@435 1897 }
duke@435 1898 return T_OBJECT;
duke@435 1899 }
duke@435 1900
duke@435 1901 // Constraints on class loaders. The details of the algorithm can be
duke@435 1902 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
duke@435 1903 // Virtual Machine" by Sheng Liang and Gilad Bracha. The basic idea is
duke@435 1904 // that the system dictionary needs to maintain a set of contraints that
duke@435 1905 // must be satisfied by all classes in the dictionary.
duke@435 1906 // if defining is true, then LinkageError if already in systemDictionary
duke@435 1907 // if initiating loader, then ok if instanceKlass matches existing entry
duke@435 1908
duke@435 1909 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
duke@435 1910 instanceKlassHandle k,
duke@435 1911 Handle class_loader, bool defining,
duke@435 1912 TRAPS) {
duke@435 1913 const char *linkage_error = NULL;
duke@435 1914 {
duke@435 1915 symbolHandle name (THREAD, k->name());
duke@435 1916 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 1917
duke@435 1918 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 1919 if (check != (klassOop)NULL) {
duke@435 1920 // if different instanceKlass - duplicate class definition,
duke@435 1921 // else - ok, class loaded by a different thread in parallel,
duke@435 1922 // we should only have found it if it was done loading and ok to use
duke@435 1923 // system dictionary only holds instance classes, placeholders
duke@435 1924 // also holds array classes
duke@435 1925
duke@435 1926 assert(check->klass_part()->oop_is_instance(), "noninstance in systemdictionary");
duke@435 1927 if ((defining == true) || (k() != check)) {
duke@435 1928 linkage_error = "loader (instance of %s): attempted duplicate class "
duke@435 1929 "definition for name: \"%s\"";
duke@435 1930 } else {
duke@435 1931 return;
duke@435 1932 }
duke@435 1933 }
duke@435 1934
duke@435 1935 #ifdef ASSERT
duke@435 1936 unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
duke@435 1937 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 1938 symbolOop ph_check = find_placeholder(p_index, p_hash, name, class_loader);
duke@435 1939 assert(ph_check == NULL || ph_check == name(), "invalid symbol");
duke@435 1940 #endif
duke@435 1941
duke@435 1942 if (linkage_error == NULL) {
duke@435 1943 if (constraints()->check_or_update(k, class_loader, name) == false) {
duke@435 1944 linkage_error = "loader constraint violation: loader (instance of %s)"
duke@435 1945 " previously initiated loading for a different type with name \"%s\"";
duke@435 1946 }
duke@435 1947 }
duke@435 1948 }
duke@435 1949
duke@435 1950 // Throw error now if needed (cannot throw while holding
duke@435 1951 // SystemDictionary_lock because of rank ordering)
duke@435 1952
duke@435 1953 if (linkage_error) {
duke@435 1954 ResourceMark rm(THREAD);
duke@435 1955 const char* class_loader_name = loader_name(class_loader());
duke@435 1956 char* type_name = k->name()->as_C_string();
duke@435 1957 size_t buflen = strlen(linkage_error) + strlen(class_loader_name) +
duke@435 1958 strlen(type_name);
duke@435 1959 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
duke@435 1960 jio_snprintf(buf, buflen, linkage_error, class_loader_name, type_name);
duke@435 1961 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
duke@435 1962 }
duke@435 1963 }
duke@435 1964
duke@435 1965
duke@435 1966 // Update system dictionary - done after check_constraint and add_to_hierachy
duke@435 1967 // have been called.
duke@435 1968 void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash,
duke@435 1969 int p_index, unsigned int p_hash,
duke@435 1970 instanceKlassHandle k,
duke@435 1971 Handle class_loader,
duke@435 1972 TRAPS) {
duke@435 1973 // Compile_lock prevents systemDictionary updates during compilations
duke@435 1974 assert_locked_or_safepoint(Compile_lock);
duke@435 1975 symbolHandle name (THREAD, k->name());
duke@435 1976
duke@435 1977 {
duke@435 1978 MutexLocker mu1(SystemDictionary_lock, THREAD);
duke@435 1979
duke@435 1980 // See whether biased locking is enabled and if so set it for this
duke@435 1981 // klass.
duke@435 1982 // Note that this must be done past the last potential blocking
duke@435 1983 // point / safepoint. We enable biased locking lazily using a
duke@435 1984 // VM_Operation to iterate the SystemDictionary and installing the
duke@435 1985 // biasable mark word into each instanceKlass's prototype header.
duke@435 1986 // To avoid race conditions where we accidentally miss enabling the
duke@435 1987 // optimization for one class in the process of being added to the
duke@435 1988 // dictionary, we must not safepoint after the test of
duke@435 1989 // BiasedLocking::enabled().
duke@435 1990 if (UseBiasedLocking && BiasedLocking::enabled()) {
duke@435 1991 // Set biased locking bit for all loaded classes; it will be
duke@435 1992 // cleared if revocation occurs too often for this type
duke@435 1993 // NOTE that we must only do this when the class is initally
duke@435 1994 // defined, not each time it is referenced from a new class loader
duke@435 1995 if (k->class_loader() == class_loader()) {
duke@435 1996 k->set_prototype_header(markOopDesc::biased_locking_prototype());
duke@435 1997 }
duke@435 1998 }
duke@435 1999
duke@435 2000 // Check for a placeholder. If there, remove it and make a
duke@435 2001 // new system dictionary entry.
duke@435 2002 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
duke@435 2003 klassOop sd_check = find_class(d_index, d_hash, name, class_loader);
duke@435 2004 if (sd_check == NULL) {
duke@435 2005 dictionary()->add_klass(name, class_loader, k);
duke@435 2006 notice_modification();
duke@435 2007 }
duke@435 2008 #ifdef ASSERT
duke@435 2009 sd_check = find_class(d_index, d_hash, name, class_loader);
duke@435 2010 assert (sd_check != NULL, "should have entry in system dictionary");
duke@435 2011 // Changed to allow PH to remain to complete class circularity checking
duke@435 2012 // while only one thread can define a class at one time, multiple
duke@435 2013 // classes can resolve the superclass for a class at one time,
duke@435 2014 // and the placeholder is used to track that
duke@435 2015 // symbolOop ph_check = find_placeholder(p_index, p_hash, name, class_loader);
duke@435 2016 // assert (ph_check == NULL, "should not have a placeholder entry");
duke@435 2017 #endif
duke@435 2018 SystemDictionary_lock->notify_all();
duke@435 2019 }
duke@435 2020 }
duke@435 2021
duke@435 2022
duke@435 2023 klassOop SystemDictionary::find_constrained_instance_or_array_klass(
duke@435 2024 symbolHandle class_name, Handle class_loader, TRAPS) {
duke@435 2025
duke@435 2026 // First see if it has been loaded directly.
duke@435 2027 // Force the protection domain to be null. (This removes protection checks.)
duke@435 2028 Handle no_protection_domain;
duke@435 2029 klassOop klass = find_instance_or_array_klass(class_name, class_loader,
duke@435 2030 no_protection_domain, CHECK_NULL);
duke@435 2031 if (klass != NULL)
duke@435 2032 return klass;
duke@435 2033
duke@435 2034 // Now look to see if it has been loaded elsewhere, and is subject to
duke@435 2035 // a loader constraint that would require this loader to return the
duke@435 2036 // klass that is already loaded.
duke@435 2037 if (FieldType::is_array(class_name())) {
duke@435 2038 // Array classes are hard because their klassOops are not kept in the
duke@435 2039 // constraint table. The array klass may be constrained, but the elem class
duke@435 2040 // may not be.
duke@435 2041 jint dimension;
duke@435 2042 symbolOop object_key;
duke@435 2043 BasicType t = FieldType::get_array_info(class_name(), &dimension,
duke@435 2044 &object_key, CHECK_(NULL));
duke@435 2045 if (t != T_OBJECT) {
duke@435 2046 klass = Universe::typeArrayKlassObj(t);
duke@435 2047 } else {
duke@435 2048 symbolHandle elem_name(THREAD, object_key);
duke@435 2049 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 2050 klass = constraints()->find_constrained_elem_klass(class_name, elem_name, class_loader, THREAD);
duke@435 2051 }
duke@435 2052 if (klass != NULL) {
duke@435 2053 klass = Klass::cast(klass)->array_klass_or_null(dimension);
duke@435 2054 }
duke@435 2055 } else {
duke@435 2056 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 2057 // Non-array classes are easy: simply check the constraint table.
duke@435 2058 klass = constraints()->find_constrained_klass(class_name, class_loader);
duke@435 2059 }
duke@435 2060
duke@435 2061 return klass;
duke@435 2062 }
duke@435 2063
duke@435 2064
duke@435 2065 bool SystemDictionary::add_loader_constraint(symbolHandle class_name,
duke@435 2066 Handle class_loader1,
duke@435 2067 Handle class_loader2,
duke@435 2068 Thread* THREAD) {
duke@435 2069 unsigned int d_hash1 = dictionary()->compute_hash(class_name, class_loader1);
duke@435 2070 int d_index1 = dictionary()->hash_to_index(d_hash1);
duke@435 2071
duke@435 2072 unsigned int d_hash2 = dictionary()->compute_hash(class_name, class_loader2);
duke@435 2073 int d_index2 = dictionary()->hash_to_index(d_hash2);
duke@435 2074
duke@435 2075 {
duke@435 2076 MutexLocker mu_s(SystemDictionary_lock, THREAD);
duke@435 2077
duke@435 2078 // Better never do a GC while we're holding these oops
duke@435 2079 No_Safepoint_Verifier nosafepoint;
duke@435 2080
duke@435 2081 klassOop klass1 = find_class(d_index1, d_hash1, class_name, class_loader1);
duke@435 2082 klassOop klass2 = find_class(d_index2, d_hash2, class_name, class_loader2);
duke@435 2083 return constraints()->add_entry(class_name, klass1, class_loader1,
duke@435 2084 klass2, class_loader2);
duke@435 2085 }
duke@435 2086 }
duke@435 2087
duke@435 2088 // Add entry to resolution error table to record the error when the first
duke@435 2089 // attempt to resolve a reference to a class has failed.
duke@435 2090 void SystemDictionary::add_resolution_error(constantPoolHandle pool, int which, symbolHandle error) {
duke@435 2091 unsigned int hash = resolution_errors()->compute_hash(pool, which);
duke@435 2092 int index = resolution_errors()->hash_to_index(hash);
duke@435 2093 {
duke@435 2094 MutexLocker ml(SystemDictionary_lock, Thread::current());
duke@435 2095 resolution_errors()->add_entry(index, hash, pool, which, error);
duke@435 2096 }
duke@435 2097 }
duke@435 2098
duke@435 2099 // Lookup resolution error table. Returns error if found, otherwise NULL.
duke@435 2100 symbolOop SystemDictionary::find_resolution_error(constantPoolHandle pool, int which) {
duke@435 2101 unsigned int hash = resolution_errors()->compute_hash(pool, which);
duke@435 2102 int index = resolution_errors()->hash_to_index(hash);
duke@435 2103 {
duke@435 2104 MutexLocker ml(SystemDictionary_lock, Thread::current());
duke@435 2105 ResolutionErrorEntry* entry = resolution_errors()->find_entry(index, hash, pool, which);
duke@435 2106 return (entry != NULL) ? entry->error() : (symbolOop)NULL;
duke@435 2107 }
duke@435 2108 }
duke@435 2109
duke@435 2110
duke@435 2111 // Make sure all class components (including arrays) in the given
duke@435 2112 // signature will be resolved to the same class in both loaders.
duke@435 2113 // Returns the name of the type that failed a loader constraint check, or
duke@435 2114 // NULL if no constraint failed. The returned C string needs cleaning up
duke@435 2115 // with a ResourceMark in the caller
duke@435 2116 char* SystemDictionary::check_signature_loaders(symbolHandle signature,
duke@435 2117 Handle loader1, Handle loader2,
duke@435 2118 bool is_method, TRAPS) {
duke@435 2119 // Nothing to do if loaders are the same.
duke@435 2120 if (loader1() == loader2()) {
duke@435 2121 return NULL;
duke@435 2122 }
duke@435 2123
duke@435 2124 SignatureStream sig_strm(signature, is_method);
duke@435 2125 while (!sig_strm.is_done()) {
duke@435 2126 if (sig_strm.is_object()) {
duke@435 2127 symbolOop s = sig_strm.as_symbol(CHECK_NULL);
duke@435 2128 symbolHandle sig (THREAD, s);
duke@435 2129 if (!add_loader_constraint(sig, loader1, loader2, THREAD)) {
duke@435 2130 return sig()->as_C_string();
duke@435 2131 }
duke@435 2132 }
duke@435 2133 sig_strm.next();
duke@435 2134 }
duke@435 2135 return NULL;
duke@435 2136 }
duke@435 2137
duke@435 2138
duke@435 2139 // Since the identity hash code for symbols changes when the symbols are
duke@435 2140 // moved from the regular perm gen (hash in the mark word) to the shared
duke@435 2141 // spaces (hash is the address), the classes loaded into the dictionary
duke@435 2142 // may be in the wrong buckets.
duke@435 2143
duke@435 2144 void SystemDictionary::reorder_dictionary() {
duke@435 2145 dictionary()->reorder_dictionary();
duke@435 2146 }
duke@435 2147
duke@435 2148
duke@435 2149 void SystemDictionary::copy_buckets(char** top, char* end) {
duke@435 2150 dictionary()->copy_buckets(top, end);
duke@435 2151 }
duke@435 2152
duke@435 2153
duke@435 2154 void SystemDictionary::copy_table(char** top, char* end) {
duke@435 2155 dictionary()->copy_table(top, end);
duke@435 2156 }
duke@435 2157
duke@435 2158
duke@435 2159 void SystemDictionary::reverse() {
duke@435 2160 dictionary()->reverse();
duke@435 2161 }
duke@435 2162
duke@435 2163 int SystemDictionary::number_of_classes() {
duke@435 2164 return dictionary()->number_of_entries();
duke@435 2165 }
duke@435 2166
duke@435 2167
duke@435 2168 // ----------------------------------------------------------------------------
duke@435 2169 #ifndef PRODUCT
duke@435 2170
duke@435 2171 void SystemDictionary::print() {
duke@435 2172 dictionary()->print();
duke@435 2173
duke@435 2174 // Placeholders
duke@435 2175 GCMutexLocker mu(SystemDictionary_lock);
duke@435 2176 placeholders()->print();
duke@435 2177
duke@435 2178 // loader constraints - print under SD_lock
duke@435 2179 constraints()->print();
duke@435 2180 }
duke@435 2181
duke@435 2182 #endif
duke@435 2183
duke@435 2184 void SystemDictionary::verify() {
duke@435 2185 guarantee(dictionary() != NULL, "Verify of system dictionary failed");
duke@435 2186 guarantee(constraints() != NULL,
duke@435 2187 "Verify of loader constraints failed");
duke@435 2188 guarantee(dictionary()->number_of_entries() >= 0 &&
duke@435 2189 placeholders()->number_of_entries() >= 0,
duke@435 2190 "Verify of system dictionary failed");
duke@435 2191
duke@435 2192 // Verify dictionary
duke@435 2193 dictionary()->verify();
duke@435 2194
duke@435 2195 GCMutexLocker mu(SystemDictionary_lock);
duke@435 2196 placeholders()->verify();
duke@435 2197
duke@435 2198 // Verify constraint table
duke@435 2199 guarantee(constraints() != NULL, "Verify of loader constraints failed");
duke@435 2200 constraints()->verify(dictionary());
duke@435 2201 }
duke@435 2202
duke@435 2203
duke@435 2204 void SystemDictionary::verify_obj_klass_present(Handle obj,
duke@435 2205 symbolHandle class_name,
duke@435 2206 Handle class_loader) {
duke@435 2207 GCMutexLocker mu(SystemDictionary_lock);
duke@435 2208 oop probe = find_class_or_placeholder(class_name, class_loader);
duke@435 2209 if (probe == NULL) {
duke@435 2210 probe = SystemDictionary::find_shared_class(class_name);
duke@435 2211 }
duke@435 2212 guarantee(probe != NULL &&
duke@435 2213 (!probe->is_klass() || probe == obj()),
duke@435 2214 "Loaded klasses should be in SystemDictionary");
duke@435 2215 }
duke@435 2216
duke@435 2217 #ifndef PRODUCT
duke@435 2218
duke@435 2219 // statistics code
duke@435 2220 class ClassStatistics: AllStatic {
duke@435 2221 private:
duke@435 2222 static int nclasses; // number of classes
duke@435 2223 static int nmethods; // number of methods
duke@435 2224 static int nmethoddata; // number of methodData
duke@435 2225 static int class_size; // size of class objects in words
duke@435 2226 static int method_size; // size of method objects in words
duke@435 2227 static int debug_size; // size of debug info in methods
duke@435 2228 static int methoddata_size; // size of methodData objects in words
duke@435 2229
duke@435 2230 static void do_class(klassOop k) {
duke@435 2231 nclasses++;
duke@435 2232 class_size += k->size();
duke@435 2233 if (k->klass_part()->oop_is_instance()) {
duke@435 2234 instanceKlass* ik = (instanceKlass*)k->klass_part();
duke@435 2235 class_size += ik->methods()->size();
duke@435 2236 class_size += ik->constants()->size();
duke@435 2237 class_size += ik->local_interfaces()->size();
duke@435 2238 class_size += ik->transitive_interfaces()->size();
duke@435 2239 // We do not have to count implementors, since we only store one!
duke@435 2240 class_size += ik->fields()->size();
duke@435 2241 }
duke@435 2242 }
duke@435 2243
duke@435 2244 static void do_method(methodOop m) {
duke@435 2245 nmethods++;
duke@435 2246 method_size += m->size();
duke@435 2247 // class loader uses same objArray for empty vectors, so don't count these
duke@435 2248 if (m->exception_table()->length() != 0) method_size += m->exception_table()->size();
duke@435 2249 if (m->has_stackmap_table()) {
duke@435 2250 method_size += m->stackmap_data()->size();
duke@435 2251 }
duke@435 2252
duke@435 2253 methodDataOop mdo = m->method_data();
duke@435 2254 if (mdo != NULL) {
duke@435 2255 nmethoddata++;
duke@435 2256 methoddata_size += mdo->size();
duke@435 2257 }
duke@435 2258 }
duke@435 2259
duke@435 2260 public:
duke@435 2261 static void print() {
duke@435 2262 SystemDictionary::classes_do(do_class);
duke@435 2263 SystemDictionary::methods_do(do_method);
duke@435 2264 tty->print_cr("Class statistics:");
duke@435 2265 tty->print_cr("%d classes (%d bytes)", nclasses, class_size * oopSize);
duke@435 2266 tty->print_cr("%d methods (%d bytes = %d base + %d debug info)", nmethods,
duke@435 2267 (method_size + debug_size) * oopSize, method_size * oopSize, debug_size * oopSize);
duke@435 2268 tty->print_cr("%d methoddata (%d bytes)", nmethoddata, methoddata_size * oopSize);
duke@435 2269 }
duke@435 2270 };
duke@435 2271
duke@435 2272
duke@435 2273 int ClassStatistics::nclasses = 0;
duke@435 2274 int ClassStatistics::nmethods = 0;
duke@435 2275 int ClassStatistics::nmethoddata = 0;
duke@435 2276 int ClassStatistics::class_size = 0;
duke@435 2277 int ClassStatistics::method_size = 0;
duke@435 2278 int ClassStatistics::debug_size = 0;
duke@435 2279 int ClassStatistics::methoddata_size = 0;
duke@435 2280
duke@435 2281 void SystemDictionary::print_class_statistics() {
duke@435 2282 ResourceMark rm;
duke@435 2283 ClassStatistics::print();
duke@435 2284 }
duke@435 2285
duke@435 2286
duke@435 2287 class MethodStatistics: AllStatic {
duke@435 2288 public:
duke@435 2289 enum {
duke@435 2290 max_parameter_size = 10
duke@435 2291 };
duke@435 2292 private:
duke@435 2293
duke@435 2294 static int _number_of_methods;
duke@435 2295 static int _number_of_final_methods;
duke@435 2296 static int _number_of_static_methods;
duke@435 2297 static int _number_of_native_methods;
duke@435 2298 static int _number_of_synchronized_methods;
duke@435 2299 static int _number_of_profiled_methods;
duke@435 2300 static int _number_of_bytecodes;
duke@435 2301 static int _parameter_size_profile[max_parameter_size];
duke@435 2302 static int _bytecodes_profile[Bytecodes::number_of_java_codes];
duke@435 2303
duke@435 2304 static void initialize() {
duke@435 2305 _number_of_methods = 0;
duke@435 2306 _number_of_final_methods = 0;
duke@435 2307 _number_of_static_methods = 0;
duke@435 2308 _number_of_native_methods = 0;
duke@435 2309 _number_of_synchronized_methods = 0;
duke@435 2310 _number_of_profiled_methods = 0;
duke@435 2311 _number_of_bytecodes = 0;
duke@435 2312 for (int i = 0; i < max_parameter_size ; i++) _parameter_size_profile[i] = 0;
duke@435 2313 for (int j = 0; j < Bytecodes::number_of_java_codes; j++) _bytecodes_profile [j] = 0;
duke@435 2314 };
duke@435 2315
duke@435 2316 static void do_method(methodOop m) {
duke@435 2317 _number_of_methods++;
duke@435 2318 // collect flag info
duke@435 2319 if (m->is_final() ) _number_of_final_methods++;
duke@435 2320 if (m->is_static() ) _number_of_static_methods++;
duke@435 2321 if (m->is_native() ) _number_of_native_methods++;
duke@435 2322 if (m->is_synchronized()) _number_of_synchronized_methods++;
duke@435 2323 if (m->method_data() != NULL) _number_of_profiled_methods++;
duke@435 2324 // collect parameter size info (add one for receiver, if any)
duke@435 2325 _parameter_size_profile[MIN2(m->size_of_parameters() + (m->is_static() ? 0 : 1), max_parameter_size - 1)]++;
duke@435 2326 // collect bytecodes info
duke@435 2327 {
duke@435 2328 Thread *thread = Thread::current();
duke@435 2329 HandleMark hm(thread);
duke@435 2330 BytecodeStream s(methodHandle(thread, m));
duke@435 2331 Bytecodes::Code c;
duke@435 2332 while ((c = s.next()) >= 0) {
duke@435 2333 _number_of_bytecodes++;
duke@435 2334 _bytecodes_profile[c]++;
duke@435 2335 }
duke@435 2336 }
duke@435 2337 }
duke@435 2338
duke@435 2339 public:
duke@435 2340 static void print() {
duke@435 2341 initialize();
duke@435 2342 SystemDictionary::methods_do(do_method);
duke@435 2343 // generate output
duke@435 2344 tty->cr();
duke@435 2345 tty->print_cr("Method statistics (static):");
duke@435 2346 // flag distribution
duke@435 2347 tty->cr();
duke@435 2348 tty->print_cr("%6d final methods %6.1f%%", _number_of_final_methods , _number_of_final_methods * 100.0F / _number_of_methods);
duke@435 2349 tty->print_cr("%6d static methods %6.1f%%", _number_of_static_methods , _number_of_static_methods * 100.0F / _number_of_methods);
duke@435 2350 tty->print_cr("%6d native methods %6.1f%%", _number_of_native_methods , _number_of_native_methods * 100.0F / _number_of_methods);
duke@435 2351 tty->print_cr("%6d synchronized methods %6.1f%%", _number_of_synchronized_methods, _number_of_synchronized_methods * 100.0F / _number_of_methods);
duke@435 2352 tty->print_cr("%6d profiled methods %6.1f%%", _number_of_profiled_methods, _number_of_profiled_methods * 100.0F / _number_of_methods);
duke@435 2353 // parameter size profile
duke@435 2354 tty->cr();
duke@435 2355 { int tot = 0;
duke@435 2356 int avg = 0;
duke@435 2357 for (int i = 0; i < max_parameter_size; i++) {
duke@435 2358 int n = _parameter_size_profile[i];
duke@435 2359 tot += n;
duke@435 2360 avg += n*i;
duke@435 2361 tty->print_cr("parameter size = %1d: %6d methods %5.1f%%", i, n, n * 100.0F / _number_of_methods);
duke@435 2362 }
duke@435 2363 assert(tot == _number_of_methods, "should be the same");
duke@435 2364 tty->print_cr(" %6d methods 100.0%%", _number_of_methods);
duke@435 2365 tty->print_cr("(average parameter size = %3.1f including receiver, if any)", (float)avg / _number_of_methods);
duke@435 2366 }
duke@435 2367 // bytecodes profile
duke@435 2368 tty->cr();
duke@435 2369 { int tot = 0;
duke@435 2370 for (int i = 0; i < Bytecodes::number_of_java_codes; i++) {
duke@435 2371 if (Bytecodes::is_defined(i)) {
duke@435 2372 Bytecodes::Code c = Bytecodes::cast(i);
duke@435 2373 int n = _bytecodes_profile[c];
duke@435 2374 tot += n;
duke@435 2375 tty->print_cr("%9d %7.3f%% %s", n, n * 100.0F / _number_of_bytecodes, Bytecodes::name(c));
duke@435 2376 }
duke@435 2377 }
duke@435 2378 assert(tot == _number_of_bytecodes, "should be the same");
duke@435 2379 tty->print_cr("%9d 100.000%%", _number_of_bytecodes);
duke@435 2380 }
duke@435 2381 tty->cr();
duke@435 2382 }
duke@435 2383 };
duke@435 2384
duke@435 2385 int MethodStatistics::_number_of_methods;
duke@435 2386 int MethodStatistics::_number_of_final_methods;
duke@435 2387 int MethodStatistics::_number_of_static_methods;
duke@435 2388 int MethodStatistics::_number_of_native_methods;
duke@435 2389 int MethodStatistics::_number_of_synchronized_methods;
duke@435 2390 int MethodStatistics::_number_of_profiled_methods;
duke@435 2391 int MethodStatistics::_number_of_bytecodes;
duke@435 2392 int MethodStatistics::_parameter_size_profile[MethodStatistics::max_parameter_size];
duke@435 2393 int MethodStatistics::_bytecodes_profile[Bytecodes::number_of_java_codes];
duke@435 2394
duke@435 2395
duke@435 2396 void SystemDictionary::print_method_statistics() {
duke@435 2397 MethodStatistics::print();
duke@435 2398 }
duke@435 2399
duke@435 2400 #endif // PRODUCT

mercurial