1 /* 2 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "cds/archiveHeapLoader.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/heapShared.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/classLoaderData.inline.hpp" 31 #include "classfile/classLoaderDataGraph.inline.hpp" 32 #include "classfile/javaClasses.inline.hpp" 33 #include "classfile/moduleEntry.hpp" 34 #include "classfile/systemDictionary.hpp" 35 #include "classfile/systemDictionaryShared.hpp" 36 #include "classfile/vmClasses.hpp" 37 #include "classfile/vmSymbols.hpp" 38 #include "gc/shared/collectedHeap.inline.hpp" 39 #include "jvm_io.h" 40 #include "logging/log.hpp" 41 #include "memory/metadataFactory.hpp" 42 #include "memory/metaspaceClosure.hpp" 43 #include "memory/oopFactory.hpp" 44 #include "memory/resourceArea.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/compressedOops.inline.hpp" 47 #include "oops/instanceKlass.hpp" 48 #include "oops/klass.inline.hpp" 49 #include "oops/objArrayKlass.hpp" 50 #include "oops/oop.inline.hpp" 51 #include "oops/oopHandle.inline.hpp" 52 #include "prims/jvmtiExport.hpp" 53 #include "runtime/atomic.hpp" 54 #include "runtime/handles.inline.hpp" 55 #include "runtime/perfData.hpp" 56 #include "utilities/macros.hpp" 57 #include "utilities/powerOfTwo.hpp" 58 #include "utilities/rotate_bits.hpp" 59 #include "utilities/stack.inline.hpp" 60 61 void Klass::set_java_mirror(Handle m) { 62 assert(!m.is_null(), "New mirror should never be null."); 63 assert(_java_mirror.is_empty(), "should only be used to initialize mirror"); 64 _java_mirror = class_loader_data()->add_handle(m); 65 } 66 67 bool Klass::is_cloneable() const { 68 return _misc_flags.is_cloneable_fast() || 69 is_subtype_of(vmClasses::Cloneable_klass()); 70 } 71 72 void Klass::set_is_cloneable() { 73 if (name() == vmSymbols::java_lang_invoke_MemberName()) { 74 assert(is_final(), "no subclasses allowed"); 75 // MemberName cloning should not be intrinsified and always happen in JVM_Clone. 76 } else if (is_instance_klass() && InstanceKlass::cast(this)->reference_type() != REF_NONE) { 77 // Reference cloning should not be intrinsified and always happen in JVM_Clone. 78 } else { 79 _misc_flags.set_is_cloneable_fast(true); 80 } 81 } 82 83 uint8_t Klass::compute_hash_slot(Symbol* n) { 84 uint hash_code; 85 // Special cases for the two superclasses of all Array instances. 86 // Code elsewhere assumes, for all instances of ArrayKlass, that 87 // these two interfaces will be in this order. 88 89 // We ensure there are some empty slots in the hash table between 90 // these two very common interfaces because if they were adjacent 91 // (e.g. Slots 0 and 1), then any other class which hashed to 0 or 1 92 // would result in a probe length of 3. 93 if (n == vmSymbols::java_lang_Cloneable()) { 94 hash_code = 0; 95 } else if (n == vmSymbols::java_io_Serializable()) { 96 hash_code = SECONDARY_SUPERS_TABLE_SIZE / 2; 97 } else { 98 auto s = (const jbyte*) n->bytes(); 99 hash_code = java_lang_String::hash_code(s, n->utf8_length()); 100 // We use String::hash_code here (rather than e.g. 101 // Symbol::identity_hash()) in order to have a hash code that 102 // does not change from run to run. We want that because the 103 // hash value for a secondary superclass appears in generated 104 // code as a constant. 105 106 // This constant is magic: see Knuth, "Fibonacci Hashing". 107 constexpr uint multiplier 108 = 2654435769; // (uint)(((u8)1 << 32) / ((1 + sqrt(5)) / 2 )) 109 constexpr uint hash_shift = sizeof(hash_code) * 8 - 6; 110 // The leading bits of the least significant half of the product. 111 hash_code = (hash_code * multiplier) >> hash_shift; 112 113 if (StressSecondarySupers) { 114 // Generate many hash collisions in order to stress-test the 115 // linear search fallback. 116 hash_code = hash_code % 3; 117 hash_code = hash_code * (SECONDARY_SUPERS_TABLE_SIZE / 3); 118 } 119 } 120 121 return (hash_code & SECONDARY_SUPERS_TABLE_MASK); 122 } 123 124 void Klass::set_name(Symbol* n) { 125 _name = n; 126 127 if (_name != nullptr) { 128 _name->increment_refcount(); 129 } 130 131 { 132 elapsedTimer selftime; 133 selftime.start(); 134 135 _hash_slot = compute_hash_slot(n); 136 assert(_hash_slot < SECONDARY_SUPERS_TABLE_SIZE, "required"); 137 138 selftime.stop(); 139 if (UsePerfData) { 140 ClassLoader::perf_secondary_hash_time()->inc(selftime.ticks()); 141 } 142 } 143 144 if (CDSConfig::is_dumping_archive() && is_instance_klass()) { 145 SystemDictionaryShared::init_dumptime_info(InstanceKlass::cast(this)); 146 } 147 } 148 149 bool Klass::is_subclass_of(const Klass* k) const { 150 // Run up the super chain and check 151 if (this == k) return true; 152 153 Klass* t = const_cast<Klass*>(this)->super(); 154 155 while (t != nullptr) { 156 if (t == k) return true; 157 t = t->super(); 158 } 159 return false; 160 } 161 162 void Klass::release_C_heap_structures(bool release_constant_pool) { 163 if (_name != nullptr) _name->decrement_refcount(); 164 } 165 166 bool Klass::linear_search_secondary_supers(const Klass* k) const { 167 // Scan the array-of-objects for a match 168 // FIXME: We could do something smarter here, maybe a vectorized 169 // comparison or a binary search, but is that worth any added 170 // complexity? 171 int cnt = secondary_supers()->length(); 172 for (int i = 0; i < cnt; i++) { 173 if (secondary_supers()->at(i) == k) { 174 return true; 175 } 176 } 177 return false; 178 } 179 180 // Given a secondary superklass k, an initial array index, and an 181 // occupancy bitmap rotated such that Bit 1 is the next bit to test, 182 // search for k. 183 bool Klass::fallback_search_secondary_supers(const Klass* k, int index, uintx rotated_bitmap) const { 184 // Once the occupancy bitmap is almost full, it's faster to use a 185 // linear search. 186 if (secondary_supers()->length() > SECONDARY_SUPERS_TABLE_SIZE - 2) { 187 return linear_search_secondary_supers(k); 188 } 189 190 // This is conventional linear probing, but instead of terminating 191 // when a null entry is found in the table, we maintain a bitmap 192 // in which a 0 indicates missing entries. 193 194 precond((int)population_count(rotated_bitmap) == secondary_supers()->length()); 195 196 // The check for secondary_supers()->length() <= SECONDARY_SUPERS_TABLE_SIZE - 2 197 // at the start of this function guarantees there are 0s in the 198 // bitmap, so this loop eventually terminates. 199 while ((rotated_bitmap & 2) != 0) { 200 if (++index == secondary_supers()->length()) { 201 index = 0; 202 } 203 if (secondary_supers()->at(index) == k) { 204 return true; 205 } 206 rotated_bitmap = rotate_right(rotated_bitmap, 1); 207 } 208 return false; 209 } 210 211 // Return self, except for abstract classes with exactly 1 212 // implementor. Then return the 1 concrete implementation. 213 Klass *Klass::up_cast_abstract() { 214 Klass *r = this; 215 while( r->is_abstract() ) { // Receiver is abstract? 216 Klass *s = r->subklass(); // Check for exactly 1 subklass 217 if (s == nullptr || s->next_sibling() != nullptr) // Oops; wrong count; give up 218 return this; // Return 'this' as a no-progress flag 219 r = s; // Loop till find concrete class 220 } 221 return r; // Return the 1 concrete class 222 } 223 224 // Find LCA in class hierarchy 225 Klass *Klass::LCA( Klass *k2 ) { 226 Klass *k1 = this; 227 while( 1 ) { 228 if( k1->is_subtype_of(k2) ) return k2; 229 if( k2->is_subtype_of(k1) ) return k1; 230 k1 = k1->super(); 231 k2 = k2->super(); 232 } 233 } 234 235 236 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) { 237 ResourceMark rm(THREAD); 238 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 239 : vmSymbols::java_lang_InstantiationException(), external_name()); 240 } 241 242 243 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { 244 ResourceMark rm(THREAD); 245 assert(s != nullptr, "Throw NPE!"); 246 THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), 247 err_msg("arraycopy: source type %s is not an array", s->klass()->external_name())); 248 } 249 250 251 void Klass::initialize(TRAPS) { 252 ShouldNotReachHere(); 253 } 254 255 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 256 #ifdef ASSERT 257 tty->print_cr("Error: find_field called on a klass oop." 258 " Likely error: reflection method does not correctly" 259 " wrap return value in a mirror object."); 260 #endif 261 ShouldNotReachHere(); 262 return nullptr; 263 } 264 265 Method* Klass::uncached_lookup_method(const Symbol* name, const Symbol* signature, 266 OverpassLookupMode overpass_mode, 267 PrivateLookupMode private_mode) const { 268 #ifdef ASSERT 269 tty->print_cr("Error: uncached_lookup_method called on a klass oop." 270 " Likely error: reflection method does not correctly" 271 " wrap return value in a mirror object."); 272 #endif 273 ShouldNotReachHere(); 274 return nullptr; 275 } 276 277 Klass::Klass() : _kind(UnknownKlassKind) { 278 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for cds"); 279 } 280 281 // "Normal" instantiation is preceded by a MetaspaceObj allocation 282 // which zeros out memory - calloc equivalent. 283 // The constructor is also used from CppVtableCloner, 284 // which doesn't zero out the memory before calling the constructor. 285 Klass::Klass(KlassKind kind) : _kind(kind), 286 _prototype_header(markWord::prototype()), 287 _shared_class_path_index(-1) { 288 CDS_ONLY(_shared_class_flags = 0;) 289 CDS_JAVA_HEAP_ONLY(_archived_mirror_index = -1;) 290 _primary_supers[0] = this; 291 set_super_check_offset(in_bytes(primary_supers_offset())); 292 } 293 294 jint Klass::array_layout_helper(BasicType etype) { 295 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype"); 296 // Note that T_ARRAY is not allowed here. 297 int hsize = arrayOopDesc::base_offset_in_bytes(etype); 298 int esize = type2aelembytes(etype); 299 bool isobj = (etype == T_OBJECT); 300 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value; 301 int lh = array_layout_helper(tag, false, hsize, etype, exact_log2(esize)); 302 303 assert(lh < (int)_lh_neutral_value, "must look like an array layout"); 304 assert(layout_helper_is_array(lh), "correct kind"); 305 assert(layout_helper_is_objArray(lh) == isobj, "correct kind"); 306 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind"); 307 assert(layout_helper_header_size(lh) == hsize, "correct decode"); 308 assert(layout_helper_element_type(lh) == etype, "correct decode"); 309 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode"); 310 311 return lh; 312 } 313 314 bool Klass::can_be_primary_super_slow() const { 315 if (super() == nullptr) 316 return true; 317 else if (super()->super_depth() >= primary_super_limit()-1) 318 return false; 319 else 320 return true; 321 } 322 323 void Klass::set_secondary_supers(Array<Klass*>* secondaries, uintx bitmap) { 324 #ifdef ASSERT 325 if (secondaries != nullptr) { 326 uintx real_bitmap = compute_secondary_supers_bitmap(secondaries); 327 assert(bitmap == real_bitmap, "must be"); 328 assert(secondaries->length() >= (int)population_count(bitmap), "must be"); 329 } 330 #endif 331 _secondary_supers_bitmap = bitmap; 332 _secondary_supers = secondaries; 333 334 if (secondaries != nullptr) { 335 LogMessage(class, load) msg; 336 NonInterleavingLogStream log {LogLevel::Debug, msg}; 337 if (log.is_enabled()) { 338 ResourceMark rm; 339 log.print_cr("set_secondary_supers: hash_slot: %d; klass: %s", hash_slot(), external_name()); 340 print_secondary_supers_on(&log); 341 } 342 } 343 } 344 345 // Hashed secondary superclasses 346 // 347 // We use a compressed 64-entry hash table with linear probing. We 348 // start by creating a hash table in the usual way, followed by a pass 349 // that removes all the null entries. To indicate which entries would 350 // have been null we use a bitmap that contains a 1 in each position 351 // where an entry is present, 0 otherwise. This bitmap also serves as 352 // a kind of Bloom filter, which in many cases allows us quickly to 353 // eliminate the possibility that something is a member of a set of 354 // secondaries. 355 uintx Klass::hash_secondary_supers(Array<Klass*>* secondaries, bool rewrite) { 356 const int length = secondaries->length(); 357 358 if (length == 0) { 359 return SECONDARY_SUPERS_BITMAP_EMPTY; 360 } 361 362 if (length == 1) { 363 int hash_slot = secondaries->at(0)->hash_slot(); 364 return uintx(1) << hash_slot; 365 } 366 367 // Invariant: _secondary_supers.length >= population_count(_secondary_supers_bitmap) 368 369 // Don't attempt to hash a table that's completely full, because in 370 // the case of an absent interface linear probing would not 371 // terminate. 372 if (length >= SECONDARY_SUPERS_TABLE_SIZE) { 373 return SECONDARY_SUPERS_BITMAP_FULL; 374 } 375 376 { 377 PerfTraceTime ptt(ClassLoader::perf_secondary_hash_time()); 378 379 ResourceMark rm; 380 uintx bitmap = SECONDARY_SUPERS_BITMAP_EMPTY; 381 auto hashed_secondaries = new GrowableArray<Klass*>(SECONDARY_SUPERS_TABLE_SIZE, 382 SECONDARY_SUPERS_TABLE_SIZE, nullptr); 383 384 for (int j = 0; j < length; j++) { 385 Klass* k = secondaries->at(j); 386 hash_insert(k, hashed_secondaries, bitmap); 387 } 388 389 // Pack the hashed secondaries array by copying it into the 390 // secondaries array, sans nulls, if modification is allowed. 391 // Otherwise, validate the order. 392 int i = 0; 393 for (int slot = 0; slot < SECONDARY_SUPERS_TABLE_SIZE; slot++) { 394 bool has_element = ((bitmap >> slot) & 1) != 0; 395 assert(has_element == (hashed_secondaries->at(slot) != nullptr), ""); 396 if (has_element) { 397 Klass* k = hashed_secondaries->at(slot); 398 if (rewrite) { 399 secondaries->at_put(i, k); 400 } else if (secondaries->at(i) != k) { 401 assert(false, "broken secondary supers hash table"); 402 return SECONDARY_SUPERS_BITMAP_FULL; 403 } 404 i++; 405 } 406 } 407 assert(i == secondaries->length(), "mismatch"); 408 postcond((int)population_count(bitmap) == secondaries->length()); 409 410 return bitmap; 411 } 412 } 413 414 void Klass::hash_insert(Klass* klass, GrowableArray<Klass*>* secondaries, uintx& bitmap) { 415 assert(bitmap != SECONDARY_SUPERS_BITMAP_FULL, ""); 416 417 int dist = 0; 418 for (int slot = klass->hash_slot(); true; slot = (slot + 1) & SECONDARY_SUPERS_TABLE_MASK) { 419 Klass* existing = secondaries->at(slot); 420 assert(((bitmap >> slot) & 1) == (existing != nullptr), "mismatch"); 421 if (existing == nullptr) { // no conflict 422 secondaries->at_put(slot, klass); 423 bitmap |= uintx(1) << slot; 424 assert(bitmap != SECONDARY_SUPERS_BITMAP_FULL, ""); 425 return; 426 } else { 427 // Use Robin Hood hashing to minimize the worst case search. 428 // Also, every permutation of the insertion sequence produces 429 // the same final Robin Hood hash table, provided that a 430 // consistent tie breaker is used. 431 int existing_dist = (slot - existing->hash_slot()) & SECONDARY_SUPERS_TABLE_MASK; 432 if (existing_dist < dist 433 // This tie breaker ensures that the hash order is maintained. 434 || ((existing_dist == dist) 435 && (uintptr_t(existing) < uintptr_t(klass)))) { 436 Klass* tmp = secondaries->at(slot); 437 secondaries->at_put(slot, klass); 438 klass = tmp; 439 dist = existing_dist; 440 } 441 ++dist; 442 } 443 } 444 } 445 446 Array<Klass*>* Klass::pack_secondary_supers(ClassLoaderData* loader_data, 447 GrowableArray<Klass*>* primaries, 448 GrowableArray<Klass*>* secondaries, 449 uintx& bitmap, TRAPS) { 450 int new_length = primaries->length() + secondaries->length(); 451 Array<Klass*>* secondary_supers = MetadataFactory::new_array<Klass*>(loader_data, new_length, CHECK_NULL); 452 453 // Combine the two arrays into a metadata object to pack the array. 454 // The primaries are added in the reverse order, then the secondaries. 455 int fill_p = primaries->length(); 456 for (int j = 0; j < fill_p; j++) { 457 secondary_supers->at_put(j, primaries->pop()); // add primaries in reverse order. 458 } 459 for( int j = 0; j < secondaries->length(); j++ ) { 460 secondary_supers->at_put(j+fill_p, secondaries->at(j)); // add secondaries on the end. 461 } 462 #ifdef ASSERT 463 // We must not copy any null placeholders left over from bootstrap. 464 for (int j = 0; j < secondary_supers->length(); j++) { 465 assert(secondary_supers->at(j) != nullptr, "correct bootstrapping order"); 466 } 467 #endif 468 469 bitmap = hash_secondary_supers(secondary_supers, /*rewrite=*/true); // rewrites freshly allocated array 470 return secondary_supers; 471 } 472 473 uintx Klass::compute_secondary_supers_bitmap(Array<Klass*>* secondary_supers) { 474 return hash_secondary_supers(secondary_supers, /*rewrite=*/false); // no rewrites allowed 475 } 476 477 uint8_t Klass::compute_home_slot(Klass* k, uintx bitmap) { 478 uint8_t hash = k->hash_slot(); 479 if (hash > 0) { 480 return population_count(bitmap << (SECONDARY_SUPERS_TABLE_SIZE - hash)); 481 } 482 return 0; 483 } 484 485 486 void Klass::initialize_supers(Klass* k, Array<InstanceKlass*>* transitive_interfaces, TRAPS) { 487 if (k == nullptr) { 488 set_super(nullptr); 489 _primary_supers[0] = this; 490 assert(super_depth() == 0, "Object must already be initialized properly"); 491 } else if (k != super() || k == vmClasses::Object_klass()) { 492 assert(super() == nullptr || super() == vmClasses::Object_klass(), 493 "initialize this only once to a non-trivial value"); 494 set_super(k); 495 Klass* sup = k; 496 int sup_depth = sup->super_depth(); 497 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit()); 498 if (!can_be_primary_super_slow()) 499 my_depth = primary_super_limit(); 500 for (juint i = 0; i < my_depth; i++) { 501 _primary_supers[i] = sup->_primary_supers[i]; 502 } 503 Klass* *super_check_cell; 504 if (my_depth < primary_super_limit()) { 505 _primary_supers[my_depth] = this; 506 super_check_cell = &_primary_supers[my_depth]; 507 } else { 508 // Overflow of the primary_supers array forces me to be secondary. 509 super_check_cell = &_secondary_super_cache; 510 } 511 set_super_check_offset(u4((address)super_check_cell - (address) this)); 512 513 #ifdef ASSERT 514 { 515 juint j = super_depth(); 516 assert(j == my_depth, "computed accessor gets right answer"); 517 Klass* t = this; 518 while (!t->can_be_primary_super()) { 519 t = t->super(); 520 j = t->super_depth(); 521 } 522 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) { 523 assert(primary_super_of_depth(j1) == nullptr, "super list padding"); 524 } 525 while (t != nullptr) { 526 assert(primary_super_of_depth(j) == t, "super list initialization"); 527 t = t->super(); 528 --j; 529 } 530 assert(j == (juint)-1, "correct depth count"); 531 } 532 #endif 533 } 534 535 if (secondary_supers() == nullptr) { 536 537 // Now compute the list of secondary supertypes. 538 // Secondaries can occasionally be on the super chain, 539 // if the inline "_primary_supers" array overflows. 540 int extras = 0; 541 Klass* p; 542 for (p = super(); !(p == nullptr || p->can_be_primary_super()); p = p->super()) { 543 ++extras; 544 } 545 546 ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below 547 548 // Compute the "real" non-extra secondaries. 549 GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras, transitive_interfaces); 550 if (secondaries == nullptr) { 551 // secondary_supers set by compute_secondary_supers 552 return; 553 } 554 555 GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras); 556 557 for (p = super(); !(p == nullptr || p->can_be_primary_super()); p = p->super()) { 558 int i; // Scan for overflow primaries being duplicates of 2nd'arys 559 560 // This happens frequently for very deeply nested arrays: the 561 // primary superclass chain overflows into the secondary. The 562 // secondary list contains the element_klass's secondaries with 563 // an extra array dimension added. If the element_klass's 564 // secondary list already contains some primary overflows, they 565 // (with the extra level of array-ness) will collide with the 566 // normal primary superclass overflows. 567 for( i = 0; i < secondaries->length(); i++ ) { 568 if( secondaries->at(i) == p ) 569 break; 570 } 571 if( i < secondaries->length() ) 572 continue; // It's a dup, don't put it in 573 primaries->push(p); 574 } 575 // Combine the two arrays into a metadata object to pack the array. 576 uintx bitmap = 0; 577 Array<Klass*>* s2 = pack_secondary_supers(class_loader_data(), primaries, secondaries, bitmap, CHECK); 578 set_secondary_supers(s2, bitmap); 579 } 580 } 581 582 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots, 583 Array<InstanceKlass*>* transitive_interfaces) { 584 assert(num_extra_slots == 0, "override for complex klasses"); 585 assert(transitive_interfaces == nullptr, "sanity"); 586 set_secondary_supers(Universe::the_empty_klass_array(), Universe::the_empty_klass_bitmap()); 587 return nullptr; 588 } 589 590 591 // superklass links 592 InstanceKlass* Klass::superklass() const { 593 assert(super() == nullptr || super()->is_instance_klass(), "must be instance klass"); 594 return _super == nullptr ? nullptr : InstanceKlass::cast(_super); 595 } 596 597 // subklass links. Used by the compiler (and vtable initialization) 598 // May be cleaned concurrently, so must use the Compile_lock. 599 // The log parameter is for clean_weak_klass_links to report unlinked classes. 600 Klass* Klass::subklass(bool log) const { 601 // Need load_acquire on the _subklass, because it races with inserts that 602 // publishes freshly initialized data. 603 for (Klass* chain = Atomic::load_acquire(&_subklass); 604 chain != nullptr; 605 // Do not need load_acquire on _next_sibling, because inserts never 606 // create _next_sibling edges to dead data. 607 chain = Atomic::load(&chain->_next_sibling)) 608 { 609 if (chain->is_loader_alive()) { 610 return chain; 611 } else if (log) { 612 if (log_is_enabled(Trace, class, unload)) { 613 ResourceMark rm; 614 log_trace(class, unload)("unlinking class (subclass): %s", chain->external_name()); 615 } 616 } 617 } 618 return nullptr; 619 } 620 621 Klass* Klass::next_sibling(bool log) const { 622 // Do not need load_acquire on _next_sibling, because inserts never 623 // create _next_sibling edges to dead data. 624 for (Klass* chain = Atomic::load(&_next_sibling); 625 chain != nullptr; 626 chain = Atomic::load(&chain->_next_sibling)) { 627 // Only return alive klass, there may be stale klass 628 // in this chain if cleaned concurrently. 629 if (chain->is_loader_alive()) { 630 return chain; 631 } else if (log) { 632 if (log_is_enabled(Trace, class, unload)) { 633 ResourceMark rm; 634 log_trace(class, unload)("unlinking class (sibling): %s", chain->external_name()); 635 } 636 } 637 } 638 return nullptr; 639 } 640 641 void Klass::set_subklass(Klass* s) { 642 assert(s != this, "sanity check"); 643 Atomic::release_store(&_subklass, s); 644 } 645 646 void Klass::set_next_sibling(Klass* s) { 647 assert(s != this, "sanity check"); 648 // Does not need release semantics. If used by cleanup, it will link to 649 // already safely published data, and if used by inserts, will be published 650 // safely using cmpxchg. 651 Atomic::store(&_next_sibling, s); 652 } 653 654 void Klass::append_to_sibling_list() { 655 if (Universe::is_fully_initialized()) { 656 assert_locked_or_safepoint(Compile_lock); 657 } 658 debug_only(verify();) 659 // add ourselves to superklass' subklass list 660 InstanceKlass* super = superklass(); 661 if (super == nullptr) return; // special case: class Object 662 assert((!super->is_interface() // interfaces cannot be supers 663 && (super->superklass() == nullptr || !is_interface())), 664 "an interface can only be a subklass of Object"); 665 666 // Make sure there is no stale subklass head 667 super->clean_subklass(); 668 669 for (;;) { 670 Klass* prev_first_subklass = Atomic::load_acquire(&_super->_subklass); 671 if (prev_first_subklass != nullptr) { 672 // set our sibling to be the superklass' previous first subklass 673 assert(prev_first_subklass->is_loader_alive(), "May not attach not alive klasses"); 674 set_next_sibling(prev_first_subklass); 675 } 676 // Note that the prev_first_subklass is always alive, meaning no sibling_next links 677 // are ever created to not alive klasses. This is an important invariant of the lock-free 678 // cleaning protocol, that allows us to safely unlink dead klasses from the sibling list. 679 if (Atomic::cmpxchg(&super->_subklass, prev_first_subklass, this) == prev_first_subklass) { 680 return; 681 } 682 } 683 debug_only(verify();) 684 } 685 686 void Klass::clean_subklass() { 687 for (;;) { 688 // Need load_acquire, due to contending with concurrent inserts 689 Klass* subklass = Atomic::load_acquire(&_subklass); 690 if (subklass == nullptr || subklass->is_loader_alive()) { 691 return; 692 } 693 // Try to fix _subklass until it points at something not dead. 694 Atomic::cmpxchg(&_subklass, subklass, subklass->next_sibling()); 695 } 696 } 697 698 void Klass::clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses) { 699 if (!ClassUnloading || !unloading_occurred) { 700 return; 701 } 702 703 Klass* root = vmClasses::Object_klass(); 704 Stack<Klass*, mtGC> stack; 705 706 stack.push(root); 707 while (!stack.is_empty()) { 708 Klass* current = stack.pop(); 709 710 assert(current->is_loader_alive(), "just checking, this should be live"); 711 712 // Find and set the first alive subklass 713 Klass* sub = current->subklass(true); 714 current->clean_subklass(); 715 if (sub != nullptr) { 716 stack.push(sub); 717 } 718 719 // Find and set the first alive sibling 720 Klass* sibling = current->next_sibling(true); 721 current->set_next_sibling(sibling); 722 if (sibling != nullptr) { 723 stack.push(sibling); 724 } 725 726 // Clean the implementors list and method data. 727 if (clean_alive_klasses && current->is_instance_klass()) { 728 InstanceKlass* ik = InstanceKlass::cast(current); 729 ik->clean_weak_instanceklass_links(); 730 731 // JVMTI RedefineClasses creates previous versions that are not in 732 // the class hierarchy, so process them here. 733 while ((ik = ik->previous_versions()) != nullptr) { 734 ik->clean_weak_instanceklass_links(); 735 } 736 } 737 } 738 } 739 740 void Klass::metaspace_pointers_do(MetaspaceClosure* it) { 741 if (log_is_enabled(Trace, cds)) { 742 ResourceMark rm; 743 log_trace(cds)("Iter(Klass): %p (%s)", this, external_name()); 744 } 745 746 it->push(&_name); 747 it->push(&_secondary_supers); 748 for (int i = 0; i < _primary_super_limit; i++) { 749 it->push(&_primary_supers[i]); 750 } 751 it->push(&_super); 752 if (!CDSConfig::is_dumping_archive()) { 753 // If dumping archive, these may point to excluded classes. There's no need 754 // to follow these pointers anyway, as they will be set to null in 755 // remove_unshareable_info(). 756 it->push((Klass**)&_subklass); 757 it->push((Klass**)&_next_sibling); 758 it->push(&_next_link); 759 } 760 761 vtableEntry* vt = start_of_vtable(); 762 for (int i=0; i<vtable_length(); i++) { 763 it->push(vt[i].method_addr()); 764 } 765 } 766 767 #if INCLUDE_CDS 768 void Klass::remove_unshareable_info() { 769 assert(CDSConfig::is_dumping_archive(), 770 "only called during CDS dump time"); 771 JFR_ONLY(REMOVE_ID(this);) 772 if (log_is_enabled(Trace, cds, unshareable)) { 773 ResourceMark rm; 774 log_trace(cds, unshareable)("remove: %s", external_name()); 775 } 776 777 // _secondary_super_cache may be updated by an is_subtype_of() call 778 // while ArchiveBuilder is copying metaspace objects. Let's reset it to 779 // null and let it be repopulated at runtime. 780 set_secondary_super_cache(nullptr); 781 782 set_subklass(nullptr); 783 set_next_sibling(nullptr); 784 set_next_link(nullptr); 785 786 // Null out class_loader_data because we don't share that yet. 787 set_class_loader_data(nullptr); 788 set_is_shared(); 789 790 // FIXME: validation in Klass::hash_secondary_supers() may fail for shared klasses. 791 // Even though the bitmaps always match, the canonical order of elements in the table 792 // is not guaranteed to stay the same (see tie breaker during Robin Hood hashing in Klass::hash_insert). 793 //assert(compute_secondary_supers_bitmap(secondary_supers()) == _secondary_supers_bitmap, "broken table"); 794 } 795 796 void Klass::remove_java_mirror() { 797 assert(CDSConfig::is_dumping_archive(), "sanity"); 798 if (log_is_enabled(Trace, cds, unshareable)) { 799 ResourceMark rm; 800 log_trace(cds, unshareable)("remove java_mirror: %s", external_name()); 801 } 802 // Just null out the mirror. The class_loader_data() no longer exists. 803 clear_java_mirror_handle(); 804 } 805 806 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 807 assert(is_klass(), "ensure C++ vtable is restored"); 808 assert(is_shared(), "must be set"); 809 assert(secondary_supers()->length() >= (int)population_count(_secondary_supers_bitmap), "must be"); 810 JFR_ONLY(RESTORE_ID(this);) 811 if (log_is_enabled(Trace, cds, unshareable)) { 812 ResourceMark rm(THREAD); 813 oop class_loader = loader_data->class_loader(); 814 log_trace(cds, unshareable)("restore: %s with class loader: %s", external_name(), 815 class_loader != nullptr ? class_loader->klass()->external_name() : "boot"); 816 } 817 818 // If an exception happened during CDS restore, some of these fields may already be 819 // set. We leave the class on the CLD list, even if incomplete so that we don't 820 // modify the CLD list outside a safepoint. 821 if (class_loader_data() == nullptr) { 822 set_class_loader_data(loader_data); 823 824 // Add to class loader list first before creating the mirror 825 // (same order as class file parsing) 826 loader_data->add_class(this); 827 } 828 829 Handle loader(THREAD, loader_data->class_loader()); 830 ModuleEntry* module_entry = nullptr; 831 Klass* k = this; 832 if (k->is_objArray_klass()) { 833 k = ObjArrayKlass::cast(k)->bottom_klass(); 834 } 835 // Obtain klass' module. 836 if (k->is_instance_klass()) { 837 InstanceKlass* ik = (InstanceKlass*) k; 838 module_entry = ik->module(); 839 } else { 840 module_entry = ModuleEntryTable::javabase_moduleEntry(); 841 } 842 // Obtain java.lang.Module, if available 843 Handle module_handle(THREAD, ((module_entry != nullptr) ? module_entry->module() : (oop)nullptr)); 844 845 if (this->has_archived_mirror_index()) { 846 ResourceMark rm(THREAD); 847 log_debug(cds, mirror)("%s has raw archived mirror", external_name()); 848 if (ArchiveHeapLoader::is_in_use()) { 849 bool present = java_lang_Class::restore_archived_mirror(this, loader, module_handle, 850 protection_domain, 851 CHECK); 852 if (present) { 853 return; 854 } 855 } 856 857 // No archived mirror data 858 log_debug(cds, mirror)("No archived mirror data for %s", external_name()); 859 clear_java_mirror_handle(); 860 this->clear_archived_mirror_index(); 861 } 862 863 // Only recreate it if not present. A previous attempt to restore may have 864 // gotten an OOM later but keep the mirror if it was created. 865 if (java_mirror() == nullptr) { 866 ResourceMark rm(THREAD); 867 log_trace(cds, mirror)("Recreate mirror for %s", external_name()); 868 java_lang_Class::create_mirror(this, loader, module_handle, protection_domain, Handle(), CHECK); 869 } 870 } 871 #endif // INCLUDE_CDS 872 873 #if INCLUDE_CDS_JAVA_HEAP 874 oop Klass::archived_java_mirror() { 875 assert(has_archived_mirror_index(), "must have archived mirror"); 876 return HeapShared::get_root(_archived_mirror_index); 877 } 878 879 void Klass::clear_archived_mirror_index() { 880 if (_archived_mirror_index >= 0) { 881 HeapShared::clear_root(_archived_mirror_index); 882 } 883 _archived_mirror_index = -1; 884 } 885 886 // No GC barrier 887 void Klass::set_archived_java_mirror(int mirror_index) { 888 assert(CDSConfig::is_dumping_heap(), "sanity"); 889 _archived_mirror_index = mirror_index; 890 } 891 #endif // INCLUDE_CDS_JAVA_HEAP 892 893 void Klass::check_array_allocation_length(int length, int max_length, TRAPS) { 894 if (length > max_length) { 895 if (!THREAD->is_in_internal_oome_mark()) { 896 report_java_out_of_memory("Requested array size exceeds VM limit"); 897 JvmtiExport::post_array_size_exhausted(); 898 THROW_OOP(Universe::out_of_memory_error_array_size()); 899 } else { 900 THROW_OOP(Universe::out_of_memory_error_java_heap_without_backtrace()); 901 } 902 } else if (length < 0) { 903 THROW_MSG(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length)); 904 } 905 } 906 907 // Replace the last '+' char with '/'. 908 static char* convert_hidden_name_to_java(Symbol* name) { 909 size_t name_len = name->utf8_length(); 910 char* result = NEW_RESOURCE_ARRAY(char, name_len + 1); 911 name->as_klass_external_name(result, (int)name_len + 1); 912 for (int index = (int)name_len; index > 0; index--) { 913 if (result[index] == '+') { 914 result[index] = JVM_SIGNATURE_SLASH; 915 break; 916 } 917 } 918 return result; 919 } 920 921 // In product mode, this function doesn't have virtual function calls so 922 // there might be some performance advantage to handling InstanceKlass here. 923 const char* Klass::external_name() const { 924 if (is_instance_klass()) { 925 const InstanceKlass* ik = static_cast<const InstanceKlass*>(this); 926 if (ik->is_hidden()) { 927 char* result = convert_hidden_name_to_java(name()); 928 return result; 929 } 930 } else if (is_objArray_klass() && ObjArrayKlass::cast(this)->bottom_klass()->is_hidden()) { 931 char* result = convert_hidden_name_to_java(name()); 932 return result; 933 } 934 if (name() == nullptr) return "<unknown>"; 935 return name()->as_klass_external_name(); 936 } 937 938 const char* Klass::signature_name() const { 939 if (name() == nullptr) return "<unknown>"; 940 if (is_objArray_klass() && ObjArrayKlass::cast(this)->bottom_klass()->is_hidden()) { 941 size_t name_len = name()->utf8_length(); 942 char* result = NEW_RESOURCE_ARRAY(char, name_len + 1); 943 name()->as_C_string(result, (int)name_len + 1); 944 for (int index = (int)name_len; index > 0; index--) { 945 if (result[index] == '+') { 946 result[index] = JVM_SIGNATURE_DOT; 947 break; 948 } 949 } 950 return result; 951 } 952 return name()->as_C_string(); 953 } 954 955 const char* Klass::external_kind() const { 956 if (is_interface()) return "interface"; 957 if (is_abstract()) return "abstract class"; 958 return "class"; 959 } 960 961 // Unless overridden, jvmti_class_status has no flags set. 962 jint Klass::jvmti_class_status() const { 963 return 0; 964 } 965 966 967 // Printing 968 969 void Klass::print_on(outputStream* st) const { 970 ResourceMark rm; 971 // print title 972 st->print("%s", internal_name()); 973 print_address_on(st); 974 st->cr(); 975 } 976 977 #define BULLET " - " 978 979 // Caller needs ResourceMark 980 void Klass::oop_print_on(oop obj, outputStream* st) { 981 // print title 982 st->print_cr("%s ", internal_name()); 983 obj->print_address_on(st); 984 985 if (WizardMode) { 986 // print header 987 obj->mark().print_on(st); 988 st->cr(); 989 st->print(BULLET"prototype_header: " INTPTR_FORMAT, _prototype_header.value()); 990 st->cr(); 991 } 992 993 // print class 994 st->print(BULLET"klass: "); 995 obj->klass()->print_value_on(st); 996 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr(); 997 st->cr(); 998 } 999 1000 void Klass::oop_print_value_on(oop obj, outputStream* st) { 1001 // print title 1002 ResourceMark rm; // Cannot print in debug mode without this 1003 st->print("%s", internal_name()); 1004 obj->print_address_on(st); 1005 } 1006 1007 // Verification 1008 1009 void Klass::verify_on(outputStream* st) { 1010 1011 // This can be expensive, but it is worth checking that this klass is actually 1012 // in the CLD graph but not in production. 1013 assert(Metaspace::contains((address)this), "Should be"); 1014 1015 guarantee(this->is_klass(),"should be klass"); 1016 1017 if (super() != nullptr) { 1018 guarantee(super()->is_klass(), "should be klass"); 1019 } 1020 if (secondary_super_cache() != nullptr) { 1021 Klass* ko = secondary_super_cache(); 1022 guarantee(ko->is_klass(), "should be klass"); 1023 } 1024 for ( uint i = 0; i < primary_super_limit(); i++ ) { 1025 Klass* ko = _primary_supers[i]; 1026 if (ko != nullptr) { 1027 guarantee(ko->is_klass(), "should be klass"); 1028 } 1029 } 1030 1031 if (java_mirror_no_keepalive() != nullptr) { 1032 guarantee(java_lang_Class::is_instance(java_mirror_no_keepalive()), "should be instance"); 1033 } 1034 } 1035 1036 void Klass::oop_verify_on(oop obj, outputStream* st) { 1037 guarantee(oopDesc::is_oop(obj), "should be oop"); 1038 guarantee(obj->klass()->is_klass(), "klass field is not a klass"); 1039 } 1040 1041 bool Klass::is_valid(Klass* k) { 1042 if (!is_aligned(k, sizeof(MetaWord))) return false; 1043 if ((size_t)k < os::min_page_size()) return false; 1044 1045 if (!os::is_readable_range(k, k + 1)) return false; 1046 if (!Metaspace::contains(k)) return false; 1047 1048 if (!Symbol::is_valid(k->name())) return false; 1049 return ClassLoaderDataGraph::is_valid(k->class_loader_data()); 1050 } 1051 1052 Method* Klass::method_at_vtable(int index) { 1053 #ifndef PRODUCT 1054 assert(index >= 0, "valid vtable index"); 1055 if (DebugVtables) { 1056 verify_vtable_index(index); 1057 } 1058 #endif 1059 return start_of_vtable()[index].method(); 1060 } 1061 1062 1063 #ifndef PRODUCT 1064 1065 bool Klass::verify_vtable_index(int i) { 1066 int limit = vtable_length()/vtableEntry::size(); 1067 assert(i >= 0 && i < limit, "index %d out of bounds %d", i, limit); 1068 return true; 1069 } 1070 1071 #endif // PRODUCT 1072 1073 // Caller needs ResourceMark 1074 // joint_in_module_of_loader provides an optimization if 2 classes are in 1075 // the same module to succinctly print out relevant information about their 1076 // module name and class loader's name_and_id for error messages. 1077 // Format: 1078 // <fully-qualified-external-class-name1> and <fully-qualified-external-class-name2> 1079 // are in module <module-name>[@<version>] 1080 // of loader <loader-name_and_id>[, parent loader <parent-loader-name_and_id>] 1081 const char* Klass::joint_in_module_of_loader(const Klass* class2, bool include_parent_loader) const { 1082 assert(module() == class2->module(), "classes do not have the same module"); 1083 const char* class1_name = external_name(); 1084 size_t len = strlen(class1_name) + 1; 1085 1086 const char* class2_description = class2->class_in_module_of_loader(true, include_parent_loader); 1087 len += strlen(class2_description); 1088 1089 len += strlen(" and "); 1090 1091 char* joint_description = NEW_RESOURCE_ARRAY_RETURN_NULL(char, len); 1092 1093 // Just return the FQN if error when allocating string 1094 if (joint_description == nullptr) { 1095 return class1_name; 1096 } 1097 1098 jio_snprintf(joint_description, len, "%s and %s", 1099 class1_name, 1100 class2_description); 1101 1102 return joint_description; 1103 } 1104 1105 // Caller needs ResourceMark 1106 // class_in_module_of_loader provides a standard way to include 1107 // relevant information about a class, such as its module name as 1108 // well as its class loader's name_and_id, in error messages and logging. 1109 // Format: 1110 // <fully-qualified-external-class-name> is in module <module-name>[@<version>] 1111 // of loader <loader-name_and_id>[, parent loader <parent-loader-name_and_id>] 1112 const char* Klass::class_in_module_of_loader(bool use_are, bool include_parent_loader) const { 1113 // 1. fully qualified external name of class 1114 const char* klass_name = external_name(); 1115 size_t len = strlen(klass_name) + 1; 1116 1117 // 2. module name + @version 1118 const char* module_name = ""; 1119 const char* version = ""; 1120 bool has_version = false; 1121 bool module_is_named = false; 1122 const char* module_name_phrase = ""; 1123 const Klass* bottom_klass = is_objArray_klass() ? 1124 ObjArrayKlass::cast(this)->bottom_klass() : this; 1125 if (bottom_klass->is_instance_klass()) { 1126 ModuleEntry* module = InstanceKlass::cast(bottom_klass)->module(); 1127 if (module->is_named()) { 1128 module_is_named = true; 1129 module_name_phrase = "module "; 1130 module_name = module->name()->as_C_string(); 1131 len += strlen(module_name); 1132 // Use version if exists and is not a jdk module 1133 if (module->should_show_version()) { 1134 has_version = true; 1135 version = module->version()->as_C_string(); 1136 // Include stlen(version) + 1 for the "@" 1137 len += strlen(version) + 1; 1138 } 1139 } else { 1140 module_name = UNNAMED_MODULE; 1141 len += UNNAMED_MODULE_LEN; 1142 } 1143 } else { 1144 // klass is an array of primitives, module is java.base 1145 module_is_named = true; 1146 module_name_phrase = "module "; 1147 module_name = JAVA_BASE_NAME; 1148 len += JAVA_BASE_NAME_LEN; 1149 } 1150 1151 // 3. class loader's name_and_id 1152 ClassLoaderData* cld = class_loader_data(); 1153 assert(cld != nullptr, "class_loader_data should not be null"); 1154 const char* loader_name_and_id = cld->loader_name_and_id(); 1155 len += strlen(loader_name_and_id); 1156 1157 // 4. include parent loader information 1158 const char* parent_loader_phrase = ""; 1159 const char* parent_loader_name_and_id = ""; 1160 if (include_parent_loader && 1161 !cld->is_builtin_class_loader_data()) { 1162 oop parent_loader = java_lang_ClassLoader::parent(class_loader()); 1163 ClassLoaderData *parent_cld = ClassLoaderData::class_loader_data_or_null(parent_loader); 1164 // The parent loader's ClassLoaderData could be null if it is 1165 // a delegating class loader that has never defined a class. 1166 // In this case the loader's name must be obtained via the parent loader's oop. 1167 if (parent_cld == nullptr) { 1168 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(parent_loader); 1169 if (cl_name_and_id != nullptr) { 1170 parent_loader_name_and_id = java_lang_String::as_utf8_string(cl_name_and_id); 1171 } 1172 } else { 1173 parent_loader_name_and_id = parent_cld->loader_name_and_id(); 1174 } 1175 parent_loader_phrase = ", parent loader "; 1176 len += strlen(parent_loader_phrase) + strlen(parent_loader_name_and_id); 1177 } 1178 1179 // Start to construct final full class description string 1180 len += ((use_are) ? strlen(" are in ") : strlen(" is in ")); 1181 len += strlen(module_name_phrase) + strlen(" of loader "); 1182 1183 char* class_description = NEW_RESOURCE_ARRAY_RETURN_NULL(char, len); 1184 1185 // Just return the FQN if error when allocating string 1186 if (class_description == nullptr) { 1187 return klass_name; 1188 } 1189 1190 jio_snprintf(class_description, len, "%s %s in %s%s%s%s of loader %s%s%s", 1191 klass_name, 1192 (use_are) ? "are" : "is", 1193 module_name_phrase, 1194 module_name, 1195 (has_version) ? "@" : "", 1196 (has_version) ? version : "", 1197 loader_name_and_id, 1198 parent_loader_phrase, 1199 parent_loader_name_and_id); 1200 1201 return class_description; 1202 } 1203 1204 class LookupStats : StackObj { 1205 private: 1206 uint _no_of_samples; 1207 uint _worst; 1208 uint _worst_count; 1209 uint _average; 1210 uint _best; 1211 uint _best_count; 1212 public: 1213 LookupStats() : _no_of_samples(0), _worst(0), _worst_count(0), _average(0), _best(INT_MAX), _best_count(0) {} 1214 1215 ~LookupStats() { 1216 assert(_best <= _worst || _no_of_samples == 0, "sanity"); 1217 } 1218 1219 void sample(uint value) { 1220 ++_no_of_samples; 1221 _average += value; 1222 1223 if (_worst < value) { 1224 _worst = value; 1225 _worst_count = 1; 1226 } else if (_worst == value) { 1227 ++_worst_count; 1228 } 1229 1230 if (_best > value) { 1231 _best = value; 1232 _best_count = 1; 1233 } else if (_best == value) { 1234 ++_best_count; 1235 } 1236 } 1237 1238 void print_on(outputStream* st) const { 1239 st->print("best: %2d (%4.1f%%)", _best, (100.0 * _best_count) / _no_of_samples); 1240 if (_best_count < _no_of_samples) { 1241 st->print("; average: %4.1f; worst: %2d (%4.1f%%)", 1242 (1.0 * _average) / _no_of_samples, 1243 _worst, (100.0 * _worst_count) / _no_of_samples); 1244 } 1245 } 1246 }; 1247 1248 static void print_positive_lookup_stats(Array<Klass*>* secondary_supers, uintx bitmap, outputStream* st) { 1249 int num_of_supers = secondary_supers->length(); 1250 1251 LookupStats s; 1252 for (int i = 0; i < num_of_supers; i++) { 1253 Klass* secondary_super = secondary_supers->at(i); 1254 int home_slot = Klass::compute_home_slot(secondary_super, bitmap); 1255 uint score = 1 + ((i - home_slot) & Klass::SECONDARY_SUPERS_TABLE_MASK); 1256 s.sample(score); 1257 } 1258 st->print("positive_lookup: "); s.print_on(st); 1259 } 1260 1261 static uint compute_distance_to_nearest_zero(int slot, uintx bitmap) { 1262 assert(~bitmap != 0, "no zeroes"); 1263 uintx start = rotate_right(bitmap, slot); 1264 return count_trailing_zeros(~start); 1265 } 1266 1267 static void print_negative_lookup_stats(uintx bitmap, outputStream* st) { 1268 LookupStats s; 1269 for (int slot = 0; slot < Klass::SECONDARY_SUPERS_TABLE_SIZE; slot++) { 1270 uint score = compute_distance_to_nearest_zero(slot, bitmap); 1271 s.sample(score); 1272 } 1273 st->print("negative_lookup: "); s.print_on(st); 1274 } 1275 1276 void Klass::print_secondary_supers_on(outputStream* st) const { 1277 if (secondary_supers() != nullptr) { 1278 st->print(" - "); st->print("%d elements;", _secondary_supers->length()); 1279 st->print_cr(" bitmap: " UINTX_FORMAT_X_0 ";", _secondary_supers_bitmap); 1280 if (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_EMPTY && 1281 _secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL) { 1282 st->print(" - "); print_positive_lookup_stats(secondary_supers(), 1283 _secondary_supers_bitmap, st); st->cr(); 1284 st->print(" - "); print_negative_lookup_stats(_secondary_supers_bitmap, st); st->cr(); 1285 } 1286 } else { 1287 st->print("null"); 1288 } 1289 } 1290 1291 void Klass::on_secondary_supers_verification_failure(Klass* super, Klass* sub, bool linear_result, bool table_result, const char* msg) { 1292 ResourceMark rm; 1293 super->print(); 1294 sub->print(); 1295 fatal("%s: %s implements %s: linear_search: %d; table_lookup: %d", 1296 msg, sub->external_name(), super->external_name(), linear_result, table_result); 1297 }