src/share/vm/oops/klass.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
parent 9507
7e72702243a4
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

duke@435 1 /*
kbarrett@9099 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. 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 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
coleenp@4037 26 #include "classfile/javaClasses.hpp"
coleenp@4037 27 #include "classfile/dictionary.hpp"
stefank@2314 28 #include "classfile/systemDictionary.hpp"
stefank@2314 29 #include "classfile/vmSymbols.hpp"
coleenp@4037 30 #include "gc_implementation/shared/markSweep.inline.hpp"
stefank@2314 31 #include "gc_interface/collectedHeap.inline.hpp"
acorn@4497 32 #include "memory/heapInspection.hpp"
coleenp@4037 33 #include "memory/metadataFactory.hpp"
stefank@2314 34 #include "memory/oopFactory.hpp"
stefank@2314 35 #include "memory/resourceArea.hpp"
stefank@2314 36 #include "oops/instanceKlass.hpp"
stefank@2314 37 #include "oops/klass.inline.hpp"
stefank@2314 38 #include "oops/oop.inline2.hpp"
goetz@6911 39 #include "runtime/atomic.inline.hpp"
goetz@6911 40 #include "runtime/orderAccess.inline.hpp"
coleenp@4037 41 #include "utilities/stack.hpp"
jprovino@4542 42 #include "utilities/macros.hpp"
jprovino@4542 43 #if INCLUDE_ALL_GCS
stefank@6992 44 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
coleenp@4037 45 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
coleenp@4037 46 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
coleenp@4037 47 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
jprovino@4542 48 #endif // INCLUDE_ALL_GCS
apetushkov@9858 49 #if INCLUDE_JFR
apetushkov@9858 50 #include "jfr/support/jfrTraceIdExtension.hpp"
apetushkov@9858 51 #endif
duke@435 52
kbarrett@9099 53 bool Klass::is_cloneable() const {
kbarrett@9099 54 return _access_flags.is_cloneable() ||
kbarrett@9099 55 is_subtype_of(SystemDictionary::Cloneable_klass());
kbarrett@9099 56 }
kbarrett@9099 57
kbarrett@9099 58 void Klass::set_is_cloneable() {
kbarrett@9099 59 if (oop_is_instance() && InstanceKlass::cast(this)->reference_type() != REF_NONE) {
kbarrett@9099 60 // Reference cloning should not be intrinsified and always happen in JVM_Clone.
kbarrett@9099 61 } else {
kbarrett@9099 62 _access_flags.set_is_cloneable();
kbarrett@9099 63 }
kbarrett@9099 64 }
kbarrett@9099 65
coleenp@2497 66 void Klass::set_name(Symbol* n) {
coleenp@2497 67 _name = n;
coleenp@2497 68 if (_name != NULL) _name->increment_refcount();
coleenp@2497 69 }
duke@435 70
minqi@5097 71 bool Klass::is_subclass_of(const Klass* k) const {
duke@435 72 // Run up the super chain and check
coleenp@4037 73 if (this == k) return true;
duke@435 74
coleenp@4037 75 Klass* t = const_cast<Klass*>(this)->super();
duke@435 76
duke@435 77 while (t != NULL) {
duke@435 78 if (t == k) return true;
hseigel@4278 79 t = t->super();
duke@435 80 }
duke@435 81 return false;
duke@435 82 }
duke@435 83
coleenp@4037 84 bool Klass::search_secondary_supers(Klass* k) const {
duke@435 85 // Put some extra logic here out-of-line, before the search proper.
duke@435 86 // This cuts down the size of the inline method.
duke@435 87
duke@435 88 // This is necessary, since I am never in my own secondary_super list.
coleenp@4037 89 if (this == k)
duke@435 90 return true;
duke@435 91 // Scan the array-of-objects for a match
duke@435 92 int cnt = secondary_supers()->length();
duke@435 93 for (int i = 0; i < cnt; i++) {
coleenp@4037 94 if (secondary_supers()->at(i) == k) {
duke@435 95 ((Klass*)this)->set_secondary_super_cache(k);
duke@435 96 return true;
duke@435 97 }
duke@435 98 }
duke@435 99 return false;
duke@435 100 }
duke@435 101
duke@435 102 // Return self, except for abstract classes with exactly 1
duke@435 103 // implementor. Then return the 1 concrete implementation.
duke@435 104 Klass *Klass::up_cast_abstract() {
duke@435 105 Klass *r = this;
duke@435 106 while( r->is_abstract() ) { // Receiver is abstract?
duke@435 107 Klass *s = r->subklass(); // Check for exactly 1 subklass
duke@435 108 if( !s || s->next_sibling() ) // Oops; wrong count; give up
duke@435 109 return this; // Return 'this' as a no-progress flag
duke@435 110 r = s; // Loop till find concrete class
duke@435 111 }
duke@435 112 return r; // Return the 1 concrete class
duke@435 113 }
duke@435 114
twisti@1040 115 // Find LCA in class hierarchy
duke@435 116 Klass *Klass::LCA( Klass *k2 ) {
duke@435 117 Klass *k1 = this;
duke@435 118 while( 1 ) {
coleenp@4037 119 if( k1->is_subtype_of(k2) ) return k2;
coleenp@4037 120 if( k2->is_subtype_of(k1) ) return k1;
coleenp@4037 121 k1 = k1->super();
coleenp@4037 122 k2 = k2->super();
duke@435 123 }
duke@435 124 }
duke@435 125
duke@435 126
duke@435 127 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) {
duke@435 128 ResourceMark rm(THREAD);
duke@435 129 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
duke@435 130 : vmSymbols::java_lang_InstantiationException(), external_name());
duke@435 131 }
duke@435 132
duke@435 133
duke@435 134 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
duke@435 135 THROW(vmSymbols::java_lang_ArrayStoreException());
duke@435 136 }
duke@435 137
duke@435 138
duke@435 139 void Klass::initialize(TRAPS) {
duke@435 140 ShouldNotReachHere();
duke@435 141 }
duke@435 142
coleenp@4037 143 bool Klass::compute_is_subtype_of(Klass* k) {
duke@435 144 assert(k->is_klass(), "argument must be a class");
duke@435 145 return is_subclass_of(k);
duke@435 146 }
duke@435 147
lfoltan@6818 148 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
lfoltan@6818 149 #ifdef ASSERT
lfoltan@6818 150 tty->print_cr("Error: find_field called on a klass oop."
lfoltan@6818 151 " Likely error: reflection method does not correctly"
lfoltan@6818 152 " wrap return value in a mirror object.");
lfoltan@6818 153 #endif
lfoltan@6818 154 ShouldNotReachHere();
lfoltan@6818 155 return NULL;
lfoltan@6818 156 }
duke@435 157
dbuck@8716 158 Method* Klass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
duke@435 159 #ifdef ASSERT
duke@435 160 tty->print_cr("Error: uncached_lookup_method called on a klass oop."
duke@435 161 " Likely error: reflection method does not correctly"
duke@435 162 " wrap return value in a mirror object.");
duke@435 163 #endif
duke@435 164 ShouldNotReachHere();
duke@435 165 return NULL;
duke@435 166 }
duke@435 167
coleenp@5614 168 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
coleenp@4037 169 return Metaspace::allocate(loader_data, word_size, /*read_only*/false,
phh@9507 170 MetaspaceObj::ClassType, THREAD);
coleenp@4037 171 }
coleenp@4037 172
coleenp@4037 173 Klass::Klass() {
coleenp@4037 174 Klass* k = this;
duke@435 175
coleenp@4712 176 // Preinitialize supertype information.
coleenp@4712 177 // A later call to initialize_supers() may update these settings:
coleenp@4712 178 set_super(NULL);
coleenp@4712 179 for (juint i = 0; i < Klass::primary_super_limit(); i++) {
coleenp@4712 180 _primary_supers[i] = NULL;
duke@435 181 }
coleenp@4712 182 set_secondary_supers(NULL);
coleenp@4712 183 set_secondary_super_cache(NULL);
coleenp@4712 184 _primary_supers[0] = k;
coleenp@4712 185 set_super_check_offset(in_bytes(primary_supers_offset()));
duke@435 186
stefank@6992 187 // The constructor is used from init_self_patching_vtbl_list,
stefank@6992 188 // which doesn't zero out the memory before calling the constructor.
stefank@6992 189 // Need to set the field explicitly to not hit an assert that the field
stefank@6992 190 // should be NULL before setting it.
stefank@6992 191 _java_mirror = NULL;
stefank@6992 192
coleenp@4037 193 set_modifier_flags(0);
coleenp@4037 194 set_layout_helper(Klass::_lh_neutral_value);
coleenp@4037 195 set_name(NULL);
duke@435 196 AccessFlags af;
duke@435 197 af.set_flags(0);
coleenp@4037 198 set_access_flags(af);
coleenp@4037 199 set_subklass(NULL);
coleenp@4037 200 set_next_sibling(NULL);
coleenp@4037 201 set_next_link(NULL);
duke@435 202
coleenp@4037 203 set_prototype_header(markOopDesc::prototype());
coleenp@4037 204 set_biased_lock_revocation_count(0);
coleenp@4037 205 set_last_biased_lock_bulk_revocation_time(0);
duke@435 206
coleenp@4037 207 // The klass doesn't have any references at this point.
coleenp@4037 208 clear_modified_oops();
coleenp@4037 209 clear_accumulated_modified_oops();
iklam@7089 210 _shared_class_path_index = -1;
duke@435 211 }
duke@435 212
duke@435 213 jint Klass::array_layout_helper(BasicType etype) {
duke@435 214 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
duke@435 215 // Note that T_ARRAY is not allowed here.
duke@435 216 int hsize = arrayOopDesc::base_offset_in_bytes(etype);
kvn@464 217 int esize = type2aelembytes(etype);
duke@435 218 bool isobj = (etype == T_OBJECT);
duke@435 219 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
duke@435 220 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
duke@435 221
duke@435 222 assert(lh < (int)_lh_neutral_value, "must look like an array layout");
coleenp@4037 223 assert(layout_helper_is_array(lh), "correct kind");
duke@435 224 assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
duke@435 225 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
duke@435 226 assert(layout_helper_header_size(lh) == hsize, "correct decode");
duke@435 227 assert(layout_helper_element_type(lh) == etype, "correct decode");
duke@435 228 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
duke@435 229
duke@435 230 return lh;
duke@435 231 }
duke@435 232
duke@435 233 bool Klass::can_be_primary_super_slow() const {
duke@435 234 if (super() == NULL)
duke@435 235 return true;
coleenp@4037 236 else if (super()->super_depth() >= primary_super_limit()-1)
duke@435 237 return false;
duke@435 238 else
duke@435 239 return true;
duke@435 240 }
duke@435 241
coleenp@4037 242 void Klass::initialize_supers(Klass* k, TRAPS) {
duke@435 243 if (FastSuperclassLimit == 0) {
duke@435 244 // None of the other machinery matters.
duke@435 245 set_super(k);
duke@435 246 return;
duke@435 247 }
duke@435 248 if (k == NULL) {
duke@435 249 set_super(NULL);
coleenp@4037 250 _primary_supers[0] = this;
duke@435 251 assert(super_depth() == 0, "Object must already be initialized properly");
never@1577 252 } else if (k != super() || k == SystemDictionary::Object_klass()) {
never@1577 253 assert(super() == NULL || super() == SystemDictionary::Object_klass(),
duke@435 254 "initialize this only once to a non-trivial value");
duke@435 255 set_super(k);
coleenp@4037 256 Klass* sup = k;
duke@435 257 int sup_depth = sup->super_depth();
duke@435 258 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit());
duke@435 259 if (!can_be_primary_super_slow())
duke@435 260 my_depth = primary_super_limit();
duke@435 261 for (juint i = 0; i < my_depth; i++) {
coleenp@4037 262 _primary_supers[i] = sup->_primary_supers[i];
duke@435 263 }
coleenp@4037 264 Klass* *super_check_cell;
duke@435 265 if (my_depth < primary_super_limit()) {
coleenp@4037 266 _primary_supers[my_depth] = this;
duke@435 267 super_check_cell = &_primary_supers[my_depth];
duke@435 268 } else {
duke@435 269 // Overflow of the primary_supers array forces me to be secondary.
duke@435 270 super_check_cell = &_secondary_super_cache;
duke@435 271 }
coleenp@4037 272 set_super_check_offset((address)super_check_cell - (address) this);
duke@435 273
duke@435 274 #ifdef ASSERT
duke@435 275 {
duke@435 276 juint j = super_depth();
duke@435 277 assert(j == my_depth, "computed accessor gets right answer");
coleenp@4037 278 Klass* t = this;
hseigel@4278 279 while (!t->can_be_primary_super()) {
hseigel@4278 280 t = t->super();
hseigel@4278 281 j = t->super_depth();
duke@435 282 }
duke@435 283 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) {
duke@435 284 assert(primary_super_of_depth(j1) == NULL, "super list padding");
duke@435 285 }
duke@435 286 while (t != NULL) {
duke@435 287 assert(primary_super_of_depth(j) == t, "super list initialization");
hseigel@4278 288 t = t->super();
duke@435 289 --j;
duke@435 290 }
duke@435 291 assert(j == (juint)-1, "correct depth count");
duke@435 292 }
duke@435 293 #endif
duke@435 294 }
duke@435 295
duke@435 296 if (secondary_supers() == NULL) {
duke@435 297 KlassHandle this_kh (THREAD, this);
duke@435 298
duke@435 299 // Now compute the list of secondary supertypes.
duke@435 300 // Secondaries can occasionally be on the super chain,
duke@435 301 // if the inline "_primary_supers" array overflows.
duke@435 302 int extras = 0;
coleenp@4037 303 Klass* p;
coleenp@4037 304 for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
duke@435 305 ++extras;
duke@435 306 }
duke@435 307
coleenp@4037 308 ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below
coleenp@4037 309
duke@435 310 // Compute the "real" non-extra secondaries.
coleenp@4037 311 GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras);
coleenp@4037 312 if (secondaries == NULL) {
coleenp@4037 313 // secondary_supers set by compute_secondary_supers
coleenp@4037 314 return;
coleenp@4037 315 }
duke@435 316
coleenp@4037 317 GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras);
coleenp@4037 318
coleenp@4037 319 for (p = this_kh->super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
duke@435 320 int i; // Scan for overflow primaries being duplicates of 2nd'arys
duke@435 321
duke@435 322 // This happens frequently for very deeply nested arrays: the
duke@435 323 // primary superclass chain overflows into the secondary. The
duke@435 324 // secondary list contains the element_klass's secondaries with
duke@435 325 // an extra array dimension added. If the element_klass's
duke@435 326 // secondary list already contains some primary overflows, they
duke@435 327 // (with the extra level of array-ness) will collide with the
duke@435 328 // normal primary superclass overflows.
coleenp@4037 329 for( i = 0; i < secondaries->length(); i++ ) {
coleenp@4037 330 if( secondaries->at(i) == p )
duke@435 331 break;
coleenp@4037 332 }
duke@435 333 if( i < secondaries->length() )
duke@435 334 continue; // It's a dup, don't put it in
coleenp@4037 335 primaries->push(p);
duke@435 336 }
coleenp@4037 337 // Combine the two arrays into a metadata object to pack the array.
coleenp@4037 338 // The primaries are added in the reverse order, then the secondaries.
coleenp@4037 339 int new_length = primaries->length() + secondaries->length();
coleenp@4037 340 Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>(
coleenp@4037 341 class_loader_data(), new_length, CHECK);
coleenp@4037 342 int fill_p = primaries->length();
coleenp@4037 343 for (int j = 0; j < fill_p; j++) {
coleenp@4037 344 s2->at_put(j, primaries->pop()); // add primaries in reverse order.
coleenp@4037 345 }
coleenp@4037 346 for( int j = 0; j < secondaries->length(); j++ ) {
coleenp@4037 347 s2->at_put(j+fill_p, secondaries->at(j)); // add secondaries on the end.
duke@435 348 }
duke@435 349
duke@435 350 #ifdef ASSERT
duke@435 351 // We must not copy any NULL placeholders left over from bootstrap.
coleenp@4037 352 for (int j = 0; j < s2->length(); j++) {
coleenp@4037 353 assert(s2->at(j) != NULL, "correct bootstrapping order");
duke@435 354 }
duke@435 355 #endif
duke@435 356
coleenp@4037 357 this_kh->set_secondary_supers(s2);
duke@435 358 }
duke@435 359 }
duke@435 360
coleenp@4037 361 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) {
duke@435 362 assert(num_extra_slots == 0, "override for complex klasses");
coleenp@4037 363 set_secondary_supers(Universe::the_empty_klass_array());
coleenp@4037 364 return NULL;
duke@435 365 }
duke@435 366
duke@435 367
duke@435 368 Klass* Klass::subklass() const {
hseigel@4278 369 return _subklass == NULL ? NULL : _subklass;
duke@435 370 }
duke@435 371
coleenp@4037 372 InstanceKlass* Klass::superklass() const {
coleenp@4037 373 assert(super() == NULL || super()->oop_is_instance(), "must be instance klass");
coleenp@4037 374 return _super == NULL ? NULL : InstanceKlass::cast(_super);
duke@435 375 }
duke@435 376
duke@435 377 Klass* Klass::next_sibling() const {
hseigel@4278 378 return _next_sibling == NULL ? NULL : _next_sibling;
duke@435 379 }
duke@435 380
coleenp@4037 381 void Klass::set_subklass(Klass* s) {
coleenp@4037 382 assert(s != this, "sanity check");
coleenp@4037 383 _subklass = s;
duke@435 384 }
duke@435 385
coleenp@4037 386 void Klass::set_next_sibling(Klass* s) {
coleenp@4037 387 assert(s != this, "sanity check");
coleenp@4037 388 _next_sibling = s;
duke@435 389 }
duke@435 390
duke@435 391 void Klass::append_to_sibling_list() {
coleenp@4178 392 debug_only(verify();)
duke@435 393 // add ourselves to superklass' subklass list
coleenp@4037 394 InstanceKlass* super = superklass();
duke@435 395 if (super == NULL) return; // special case: class Object
coleenp@4178 396 assert((!super->is_interface() // interfaces cannot be supers
duke@435 397 && (super->superklass() == NULL || !is_interface())),
duke@435 398 "an interface can only be a subklass of Object");
coleenp@4037 399 Klass* prev_first_subklass = super->subklass_oop();
duke@435 400 if (prev_first_subklass != NULL) {
duke@435 401 // set our sibling to be the superklass' previous first subklass
duke@435 402 set_next_sibling(prev_first_subklass);
duke@435 403 }
duke@435 404 // make ourselves the superklass' first subklass
coleenp@4037 405 super->set_subklass(this);
coleenp@4178 406 debug_only(verify();)
duke@435 407 }
duke@435 408
coleenp@4037 409 bool Klass::is_loader_alive(BoolObjectClosure* is_alive) {
coleenp@4304 410 #ifdef ASSERT
coleenp@4037 411 // The class is alive iff the class loader is alive.
coleenp@4037 412 oop loader = class_loader();
coleenp@4304 413 bool loader_alive = (loader == NULL) || is_alive->do_object_b(loader);
coleenp@4304 414 #endif // ASSERT
coleenp@4304 415
coleenp@4304 416 // The class is alive if it's mirror is alive (which should be marked if the
coleenp@4304 417 // loader is alive) unless it's an anoymous class.
coleenp@4304 418 bool mirror_alive = is_alive->do_object_b(java_mirror());
coleenp@4304 419 assert(!mirror_alive || loader_alive, "loader must be alive if the mirror is"
coleenp@4304 420 " but not the other way around with anonymous classes");
coleenp@4304 421 return mirror_alive;
coleenp@4037 422 }
coleenp@4037 423
stefank@6992 424 void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive, bool clean_alive_klasses) {
coleenp@4037 425 if (!ClassUnloading) {
coleenp@4037 426 return;
coleenp@4037 427 }
coleenp@4037 428
coleenp@4037 429 Klass* root = SystemDictionary::Object_klass();
coleenp@4037 430 Stack<Klass*, mtGC> stack;
coleenp@4037 431
coleenp@4037 432 stack.push(root);
coleenp@4037 433 while (!stack.is_empty()) {
coleenp@4037 434 Klass* current = stack.pop();
coleenp@4037 435
coleenp@4037 436 assert(current->is_loader_alive(is_alive), "just checking, this should be live");
coleenp@4037 437
coleenp@4037 438 // Find and set the first alive subklass
coleenp@4037 439 Klass* sub = current->subklass_oop();
coleenp@4037 440 while (sub != NULL && !sub->is_loader_alive(is_alive)) {
duke@435 441 #ifndef PRODUCT
coleenp@4304 442 if (TraceClassUnloading && WizardMode) {
coleenp@4304 443 ResourceMark rm;
coleenp@4037 444 tty->print_cr("[Unlinking class (subclass) %s]", sub->external_name());
coleenp@4304 445 }
duke@435 446 #endif
coleenp@4037 447 sub = sub->next_sibling_oop();
duke@435 448 }
coleenp@4037 449 current->set_subklass(sub);
coleenp@4037 450 if (sub != NULL) {
coleenp@4037 451 stack.push(sub);
coleenp@4037 452 }
coleenp@4037 453
coleenp@4037 454 // Find and set the first alive sibling
coleenp@4037 455 Klass* sibling = current->next_sibling_oop();
coleenp@4037 456 while (sibling != NULL && !sibling->is_loader_alive(is_alive)) {
coleenp@4304 457 if (TraceClassUnloading && WizardMode) {
coleenp@4304 458 ResourceMark rm;
coleenp@4037 459 tty->print_cr("[Unlinking class (sibling) %s]", sibling->external_name());
coleenp@4304 460 }
coleenp@4037 461 sibling = sibling->next_sibling_oop();
coleenp@4304 462 }
coleenp@4037 463 current->set_next_sibling(sibling);
coleenp@4037 464 if (sibling != NULL) {
coleenp@4037 465 stack.push(sibling);
coleenp@4304 466 }
coleenp@4037 467
coleenp@4037 468 // Clean the implementors list and method data.
stefank@6992 469 if (clean_alive_klasses && current->oop_is_instance()) {
coleenp@4037 470 InstanceKlass* ik = InstanceKlass::cast(current);
stefank@8185 471 ik->clean_weak_instanceklass_links(is_alive);
kevinw@9184 472
kevinw@9184 473 // JVMTI RedefineClasses creates previous versions that are not in
kevinw@9184 474 // the class hierarchy, so process them here.
kevinw@9184 475 while ((ik = ik->previous_versions()) != NULL) {
kevinw@9184 476 ik->clean_weak_instanceklass_links(is_alive);
kevinw@9184 477 }
duke@435 478 }
duke@435 479 }
duke@435 480 }
duke@435 481
coleenp@4037 482 void Klass::klass_update_barrier_set(oop v) {
coleenp@4037 483 record_modified_oops();
coleenp@4037 484 }
coleenp@4037 485
stefank@6992 486 // This barrier is used by G1 to remember the old oop values, so
stefank@6992 487 // that we don't forget any objects that were live at the snapshot at
stefank@6992 488 // the beginning. This function is only used when we write oops into Klasses.
stefank@6992 489 void Klass::klass_update_barrier_set_pre(oop* p, oop v) {
stefank@6992 490 #if INCLUDE_ALL_GCS
stefank@6992 491 if (UseG1GC) {
stefank@6992 492 oop obj = *p;
stefank@6992 493 if (obj != NULL) {
stefank@6992 494 G1SATBCardTableModRefBS::enqueue(obj);
stefank@6992 495 }
stefank@6992 496 }
stefank@6992 497 #endif
coleenp@4037 498 }
coleenp@4037 499
coleenp@4037 500 void Klass::klass_oop_store(oop* p, oop v) {
coleenp@4037 501 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
coleenp@4037 502 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
coleenp@4037 503
coleenp@4037 504 // do the store
coleenp@4037 505 if (always_do_update_barrier) {
coleenp@4037 506 klass_oop_store((volatile oop*)p, v);
coleenp@4037 507 } else {
stefank@6992 508 klass_update_barrier_set_pre(p, v);
coleenp@4037 509 *p = v;
coleenp@4037 510 klass_update_barrier_set(v);
coleenp@4037 511 }
coleenp@4037 512 }
coleenp@4037 513
coleenp@4037 514 void Klass::klass_oop_store(volatile oop* p, oop v) {
coleenp@4037 515 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
coleenp@4037 516 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
coleenp@4037 517
stefank@6992 518 klass_update_barrier_set_pre((oop*)p, v); // Cast away volatile.
coleenp@4037 519 OrderAccess::release_store_ptr(p, v);
coleenp@4037 520 klass_update_barrier_set(v);
coleenp@4037 521 }
coleenp@4037 522
coleenp@4037 523 void Klass::oops_do(OopClosure* cl) {
coleenp@4037 524 cl->do_oop(&_java_mirror);
coleenp@4037 525 }
duke@435 526
duke@435 527 void Klass::remove_unshareable_info() {
coleenp@6626 528 assert (DumpSharedSpaces, "only called for DumpSharedSpaces");
coleenp@6626 529
apetushkov@9858 530 JFR_ONLY(REMOVE_ID(this);)
duke@435 531 set_subklass(NULL);
duke@435 532 set_next_sibling(NULL);
coleenp@4037 533 // Clear the java mirror
coleenp@4037 534 set_java_mirror(NULL);
coleenp@4037 535 set_next_link(NULL);
coleenp@4037 536
coleenp@4037 537 // Null out class_loader_data because we don't share that yet.
coleenp@4037 538 set_class_loader_data(NULL);
duke@435 539 }
duke@435 540
iklam@7089 541 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
apetushkov@9858 542 JFR_ONLY(RESTORE_ID(this);)
coleenp@6626 543 // If an exception happened during CDS restore, some of these fields may already be
coleenp@6626 544 // set. We leave the class on the CLD list, even if incomplete so that we don't
coleenp@6626 545 // modify the CLD list outside a safepoint.
coleenp@6626 546 if (class_loader_data() == NULL) {
iklam@7089 547 // Restore class_loader_data
coleenp@6626 548 set_class_loader_data(loader_data);
duke@435 549
iklam@7089 550 // Add to class loader list first before creating the mirror
coleenp@6626 551 // (same order as class file parsing)
coleenp@6626 552 loader_data->add_class(this);
coleenp@6626 553 }
coleenp@4037 554
iklam@7089 555 // Recreate the class mirror.
coleenp@6626 556 // Only recreate it if not present. A previous attempt to restore may have
coleenp@6626 557 // gotten an OOM later but keep the mirror if it was created.
coleenp@6626 558 if (java_mirror() == NULL) {
coleenp@7129 559 java_lang_Class::create_mirror(this, class_loader(), protection_domain, CHECK);
coleenp@6626 560 }
coleenp@2497 561 }
coleenp@2497 562
coleenp@4037 563 Klass* Klass::array_klass_or_null(int rank) {
duke@435 564 EXCEPTION_MARK;
duke@435 565 // No exception can be thrown by array_klass_impl when called with or_null == true.
duke@435 566 // (In anycase, the execption mark will fail if it do so)
duke@435 567 return array_klass_impl(true, rank, THREAD);
duke@435 568 }
duke@435 569
duke@435 570
coleenp@4037 571 Klass* Klass::array_klass_or_null() {
duke@435 572 EXCEPTION_MARK;
duke@435 573 // No exception can be thrown by array_klass_impl when called with or_null == true.
duke@435 574 // (In anycase, the execption mark will fail if it do so)
duke@435 575 return array_klass_impl(true, THREAD);
duke@435 576 }
duke@435 577
duke@435 578
coleenp@4037 579 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
coleenp@4142 580 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
duke@435 581 return NULL;
duke@435 582 }
duke@435 583
duke@435 584
coleenp@4037 585 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
coleenp@4142 586 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
duke@435 587 return NULL;
duke@435 588 }
duke@435 589
coleenp@4037 590 oop Klass::class_loader() const { return class_loader_data()->class_loader(); }
coleenp@4037 591
duke@435 592 const char* Klass::external_name() const {
jrose@866 593 if (oop_is_instance()) {
coleenp@4037 594 InstanceKlass* ik = (InstanceKlass*) this;
jrose@866 595 if (ik->is_anonymous()) {
twisti@2698 596 assert(EnableInvokeDynamic, "");
coleenp@4304 597 intptr_t hash = 0;
coleenp@4304 598 if (ik->java_mirror() != NULL) {
coleenp@4304 599 // java_mirror might not be created yet, return 0 as hash.
coleenp@4304 600 hash = ik->java_mirror()->identity_hash();
coleenp@4304 601 }
jrose@866 602 char hash_buf[40];
jrose@866 603 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
jrose@866 604 size_t hash_len = strlen(hash_buf);
jrose@866 605
jrose@866 606 size_t result_len = name()->utf8_length();
jrose@866 607 char* result = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1);
jrose@866 608 name()->as_klass_external_name(result, (int) result_len + 1);
jrose@866 609 assert(strlen(result) == result_len, "");
jrose@866 610 strcpy(result + result_len, hash_buf);
jrose@866 611 assert(strlen(result) == result_len + hash_len, "");
jrose@866 612 return result;
jrose@866 613 }
jrose@866 614 }
jrose@1474 615 if (name() == NULL) return "<unknown>";
duke@435 616 return name()->as_klass_external_name();
duke@435 617 }
duke@435 618
duke@435 619
jrose@1474 620 const char* Klass::signature_name() const {
jrose@1474 621 if (name() == NULL) return "<unknown>";
duke@435 622 return name()->as_C_string();
duke@435 623 }
duke@435 624
duke@435 625 // Unless overridden, modifier_flags is 0.
duke@435 626 jint Klass::compute_modifier_flags(TRAPS) const {
duke@435 627 return 0;
duke@435 628 }
duke@435 629
duke@435 630 int Klass::atomic_incr_biased_lock_revocation_count() {
duke@435 631 return (int) Atomic::add(1, &_biased_lock_revocation_count);
duke@435 632 }
duke@435 633
duke@435 634 // Unless overridden, jvmti_class_status has no flags set.
duke@435 635 jint Klass::jvmti_class_status() const {
duke@435 636 return 0;
duke@435 637 }
duke@435 638
coleenp@4037 639
duke@435 640 // Printing
duke@435 641
coleenp@4037 642 void Klass::print_on(outputStream* st) const {
coleenp@4037 643 ResourceMark rm;
coleenp@4037 644 // print title
coleenp@4037 645 st->print("%s", internal_name());
coleenp@4037 646 print_address_on(st);
coleenp@4037 647 st->cr();
coleenp@4037 648 }
coleenp@4037 649
duke@435 650 void Klass::oop_print_on(oop obj, outputStream* st) {
duke@435 651 ResourceMark rm;
duke@435 652 // print title
duke@435 653 st->print_cr("%s ", internal_name());
duke@435 654 obj->print_address_on(st);
duke@435 655
duke@435 656 if (WizardMode) {
duke@435 657 // print header
duke@435 658 obj->mark()->print_on(st);
duke@435 659 }
duke@435 660
duke@435 661 // print class
duke@435 662 st->print(" - klass: ");
duke@435 663 obj->klass()->print_value_on(st);
duke@435 664 st->cr();
duke@435 665 }
duke@435 666
duke@435 667 void Klass::oop_print_value_on(oop obj, outputStream* st) {
duke@435 668 // print title
duke@435 669 ResourceMark rm; // Cannot print in debug mode without this
duke@435 670 st->print("%s", internal_name());
duke@435 671 obj->print_address_on(st);
duke@435 672 }
duke@435 673
acorn@4497 674 #if INCLUDE_SERVICES
acorn@4497 675 // Size Statistics
acorn@4497 676 void Klass::collect_statistics(KlassSizeStats *sz) const {
acorn@4497 677 sz->_klass_bytes = sz->count(this);
acorn@4497 678 sz->_mirror_bytes = sz->count(java_mirror());
acorn@4497 679 sz->_secondary_supers_bytes = sz->count_array(secondary_supers());
acorn@4497 680
acorn@4497 681 sz->_ro_bytes += sz->_secondary_supers_bytes;
acorn@4497 682 sz->_rw_bytes += sz->_klass_bytes + sz->_mirror_bytes;
acorn@4497 683 }
acorn@4497 684 #endif // INCLUDE_SERVICES
coleenp@4037 685
duke@435 686 // Verification
duke@435 687
coleenp@6316 688 void Klass::verify_on(outputStream* st) {
coleenp@4037 689
coleenp@5307 690 // This can be expensive, but it is worth checking that this klass is actually
coleenp@5307 691 // in the CLD graph but not in production.
coleenp@6678 692 assert(Metaspace::contains((address)this), "Should be");
coleenp@4037 693
coleenp@4037 694 guarantee(this->is_klass(),"should be klass");
coleenp@4037 695
coleenp@4037 696 if (super() != NULL) {
coleenp@4037 697 guarantee(super()->is_klass(), "should be klass");
coleenp@4037 698 }
coleenp@4037 699 if (secondary_super_cache() != NULL) {
coleenp@4037 700 Klass* ko = secondary_super_cache();
coleenp@4037 701 guarantee(ko->is_klass(), "should be klass");
coleenp@4037 702 }
coleenp@4037 703 for ( uint i = 0; i < primary_super_limit(); i++ ) {
coleenp@4037 704 Klass* ko = _primary_supers[i];
coleenp@4037 705 if (ko != NULL) {
coleenp@4037 706 guarantee(ko->is_klass(), "should be klass");
coleenp@4037 707 }
coleenp@4037 708 }
coleenp@4037 709
coleenp@4037 710 if (java_mirror() != NULL) {
coleenp@4037 711 guarantee(java_mirror()->is_oop(), "should be instance");
coleenp@4037 712 }
coleenp@4037 713 }
coleenp@4037 714
duke@435 715 void Klass::oop_verify_on(oop obj, outputStream* st) {
duke@435 716 guarantee(obj->is_oop(), "should be oop");
duke@435 717 guarantee(obj->klass()->is_klass(), "klass field is not a klass");
duke@435 718 }
duke@435 719
duke@435 720 #ifndef PRODUCT
duke@435 721
drchase@5732 722 bool Klass::verify_vtable_index(int i) {
duke@435 723 if (oop_is_instance()) {
drchase@5732 724 int limit = ((InstanceKlass*)this)->vtable_length()/vtableEntry::size();
drchase@5732 725 assert(i >= 0 && i < limit, err_msg("index %d out of bounds %d", i, limit));
duke@435 726 } else {
coleenp@4037 727 assert(oop_is_array(), "Must be");
drchase@5732 728 int limit = ((ArrayKlass*)this)->vtable_length()/vtableEntry::size();
drchase@5732 729 assert(i >= 0 && i < limit, err_msg("index %d out of bounds %d", i, limit));
duke@435 730 }
drchase@5732 731 return true;
drchase@5732 732 }
drchase@5732 733
drchase@5732 734 bool Klass::verify_itable_index(int i) {
drchase@5732 735 assert(oop_is_instance(), "");
drchase@5732 736 int method_count = klassItable::method_count_for_interface(this);
drchase@5732 737 assert(i >= 0 && i < method_count, "index out of bounds");
drchase@5732 738 return true;
duke@435 739 }
duke@435 740
duke@435 741 #endif
stefank@6976 742
stefank@6976 743 /////////////// Unit tests ///////////////
stefank@6976 744
stefank@6976 745 #ifndef PRODUCT
stefank@6976 746
stefank@6976 747 class TestKlass {
stefank@6976 748 public:
stefank@6976 749 static void test_oop_is_instanceClassLoader() {
stefank@6976 750 assert(SystemDictionary::ClassLoader_klass()->oop_is_instanceClassLoader(), "assert");
stefank@6976 751 assert(!SystemDictionary::String_klass()->oop_is_instanceClassLoader(), "assert");
stefank@6976 752 }
stefank@6976 753 };
stefank@6976 754
stefank@6976 755 void TestKlass_test() {
stefank@6976 756 TestKlass::test_oop_is_instanceClassLoader();
stefank@6976 757 }
stefank@6976 758
stefank@6976 759 #endif

mercurial