1 /*
   2  * Copyright (c) 2023, 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 <cds/archiveBuilder.hpp>
  26 #include <classfile/systemDictionaryShared.hpp>
  27 #include "precompiled.hpp"
  28 #include "ci/ciEnv.hpp"
  29 #include "ci/ciMetadata.hpp"
  30 #include "cds/archiveBuilder.hpp"
  31 #include "cds/cdsConfig.hpp"
  32 #include "cds/metaspaceShared.hpp"
  33 #include "cds/methodDataDictionary.hpp"
  34 #include "cds/methodProfiler.hpp"
  35 #include "cds/runTimeClassInfo.hpp"
  36 #include "classfile/classLoaderData.hpp"
  37 #include "classfile/compactHashtable.hpp"
  38 #include "classfile/javaClasses.hpp"
  39 #include "classfile/symbolTable.hpp"
  40 #include "classfile/systemDictionaryShared.hpp"
  41 #include "compiler/compileTask.hpp"
  42 #include "memory/metadataFactory.hpp"
  43 #include "memory/metaspaceClosure.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "oops/fieldStreams.inline.hpp"
  46 #include "oops/method.hpp"
  47 #include "oops/methodCounters.hpp"
  48 #include "oops/trainingData.hpp"
  49 #include "runtime/arguments.hpp"
  50 #include "runtime/fieldDescriptor.inline.hpp"
  51 #include "runtime/javaThread.inline.hpp"
  52 #include "runtime/jniHandles.inline.hpp"
  53 #include "runtime/os.hpp"
  54 #include "utilities/growableArray.hpp"
  55 #include "utilities/xmlstream.hpp"
  56 
  57 TrainingData::TrainingDataSet TrainingData::_training_data_set(1024, 0x3fffffff);
  58 TrainingDataDictionary TrainingData::_archived_training_data_dictionary;
  59 TrainingDataDictionary TrainingData::_archived_training_data_dictionary_for_dumping;
  60 GrowableArrayCHeap<DumpTimeTrainingDataInfo, mtClassShared>* TrainingData::_dumptime_training_data_dictionary = nullptr;
  61 Array<MethodTrainingData*>* TrainingData::_recompilation_schedule = nullptr;
  62 Array<MethodTrainingData*>* TrainingData::_recompilation_schedule_for_dumping = nullptr;
  63 volatile bool* TrainingData::_recompilation_status = nullptr;
  64 int TrainingData::TrainingDataLocker::_lock_mode;
  65 
  66 MethodTrainingData::MethodTrainingData() {
  67   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
  68 }
  69 
  70 KlassTrainingData::KlassTrainingData() {
  71   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
  72 }
  73 
  74 CompileTrainingData::CompileTrainingData() : _level(-1), _compile_id(-1) {
  75   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
  76 }
  77 
  78 #if INCLUDE_CDS
  79 void TrainingData::restore_all_unshareable_info(TRAPS) {
  80   class RestoreUnshareableInfo {
  81     JavaThread *_thread;
  82   public:
  83     RestoreUnshareableInfo(JavaThread *thread) : _thread(thread) { }
  84     void do_value(TrainingData* td) {
  85       td->restore_unshareable_info(_thread);
  86     }
  87   };
  88 
  89   if (have_data()) {
  90     if (!archived_training_data_dictionary()->empty()) {
  91       RestoreUnshareableInfo r(THREAD);
  92       archived_training_data_dictionary()->iterate(&r);
  93     }
  94   }
  95 }
  96 #endif
  97 
  98 void TrainingData::initialize() {
  99   // this is a nop if training modes are not enabled
 100   if (have_data() || need_data()) {
 101     TrainingDataLocker::initialize();
 102   }
 103   if (have_data()) {
 104     // Initialize dependency tracking
 105     training_data_set()->iterate_all([](const Key* k, TrainingData* td) {
 106       if (td->is_MethodTrainingData()) {
 107         td->as_MethodTrainingData()->initialize_deps_tracking();
 108       }
 109     });
 110 
 111     if (_recompilation_schedule != nullptr && _recompilation_schedule->length() > 0) {
 112       const int size = _recompilation_schedule->length();
 113       _recompilation_status = NEW_C_HEAP_ARRAY(bool, size, mtCompiler);
 114       for (int i = 0; i < size; i++) {
 115         _recompilation_status[i] = false;
 116       }
 117     }
 118   }
 119 }
 120 
 121 TrainingData::Key::Key(const KlassTrainingData* klass, Symbol* method_name, Symbol* signature)
 122   : Key(method_name, signature, klass) {}
 123 
 124 TrainingData::Key::Key(const InstanceKlass* klass)
 125   : Key(klass->name(), klass->class_loader_name_and_id()) {
 126   // Often the loader is either null or the string "'app'" (w/ extra quotes).
 127   // It can also be "'platform'".
 128 }
 129 
 130 TrainingData::Key::Key(const Method* method)
 131   : Key(method->name(), method->signature(), KlassTrainingData::make(method->method_holder()))
 132 {}
 133 
 134 MethodTrainingData* MethodTrainingData::make(KlassTrainingData* klass, Symbol* name, Symbol* signature) {
 135   Key key(klass, name, signature);
 136   TrainingData* td = have_data() ? lookup_archived_training_data(&key) : nullptr;
 137   MethodTrainingData* mtd = nullptr;
 138   if (td != nullptr) {
 139     mtd = td->as_MethodTrainingData();
 140     return mtd;
 141   }
 142   TrainingDataLocker l;
 143   td = training_data_set()->find(&key);
 144   if (td != nullptr) {
 145     mtd = td->as_MethodTrainingData();
 146   } else {
 147     mtd = MethodTrainingData::allocate(klass, name, signature);
 148     td = training_data_set()->install(mtd);
 149     assert(td == mtd, "");
 150   }
 151   assert(mtd != nullptr, "");
 152   return mtd;
 153 }
 154 
 155 MethodTrainingData* MethodTrainingData::make(KlassTrainingData* klass,
 156                                              const char* name, const char* signature) {
 157   TempNewSymbol n = SymbolTable::new_symbol(name);
 158   TempNewSymbol s = SymbolTable::new_symbol(signature);
 159   return make(klass, n, s);
 160 }
 161 
 162 MethodTrainingData* MethodTrainingData::make(const methodHandle& method,
 163                                              bool null_if_not_found) {
 164   MethodTrainingData* mtd = nullptr;
 165   if (!have_data() && !need_data()) {
 166     return mtd;
 167   }
 168   // Try grabbing the cached value first.
 169   MethodCounters* mcs = method->method_counters();
 170   if (mcs != nullptr) {
 171     mtd = mcs->method_training_data();
 172     if (mtd != nullptr) {
 173       return mtd;
 174     }
 175   } else {
 176     mcs = Method::build_method_counters(Thread::current(), method());
 177   }
 178 
 179   KlassTrainingData* holder = KlassTrainingData::make(method->method_holder(), null_if_not_found);
 180   assert(holder != nullptr || null_if_not_found, "");
 181   if (holder == nullptr) {
 182     return nullptr;
 183   }
 184   Key key(method->name(), method->signature(), holder);
 185   TrainingData* td = have_data()? lookup_archived_training_data(&key) : nullptr;
 186   if (td != nullptr) {
 187     mtd = td->as_MethodTrainingData();
 188     mtd->refresh_from(method());
 189     method->init_training_data(mtd);  // Cache the pointer for next time.
 190     return mtd;
 191   } else {
 192     TrainingDataLocker l;
 193     td = training_data_set()->find(&key);
 194     if (td == nullptr && null_if_not_found) {
 195       return nullptr;
 196     }
 197     if (td != nullptr) {
 198       mtd = td->as_MethodTrainingData();
 199       mtd->refresh_from(method());
 200       method->init_training_data(mtd); // Cache the pointer for next time.
 201       return mtd;
 202     }
 203   }
 204   assert(td == nullptr && mtd == nullptr && !null_if_not_found, "Should return if have result");
 205   KlassTrainingData* ktd = KlassTrainingData::make(method->method_holder());
 206   {
 207     TrainingDataLocker l;
 208     td = training_data_set()->find(&key);
 209     if (td == nullptr) {
 210       mtd = MethodTrainingData::allocate(ktd, method());
 211       td = training_data_set()->install(mtd);
 212       assert(td == mtd, "");
 213     } else {
 214       mtd = td->as_MethodTrainingData();
 215     }
 216     mtd->refresh_from(method());
 217     method->init_training_data(mtd);
 218   }
 219   return mtd;
 220 }
 221 
 222 void MethodTrainingData::print_on(outputStream* st, bool name_only) const {
 223   _klass->print_on(st, true);
 224   st->print(".");
 225   name()->print_symbol_on(st);
 226   signature()->print_symbol_on(st);
 227   if (name_only) {
 228     return;
 229   }
 230   if (!has_holder()) {
 231     st->print("[SYM]");
 232   }
 233   if (_level_mask) {
 234     st->print(" LM%d", _level_mask);
 235   }
 236   st->print(" mc=%p mdo=%p", _final_counters, _final_profile);
 237 }
 238 
 239 void MethodTrainingData::refresh_from(const Method* method) {
 240   if (method == nullptr || method == _holder) {
 241     return;
 242   }
 243   _holder = method;
 244 }
 245 
 246 CompileTrainingData* CompileTrainingData::make(CompileTask* task) {
 247   int level = task->comp_level();
 248   int compile_id = task->compile_id();
 249   Thread* thread = Thread::current();
 250   methodHandle m(thread, task->method());
 251   MethodTrainingData* mtd = MethodTrainingData::make(m);
 252   CompileTrainingData* ctd = CompileTrainingData::make(mtd, level, compile_id);
 253   return ctd;
 254 }
 255 
 256 CompileTrainingData* CompileTrainingData::make(MethodTrainingData* mtd, int level, int compile_id) {
 257   assert(level > CompLevel_none, "not a compiled level");
 258   mtd->notice_compilation(level);
 259 
 260   CompileTrainingData* ctd = CompileTrainingData::allocate(mtd, level, compile_id);
 261   TrainingDataLocker l;
 262   ctd->_next = mtd->_compile;
 263   mtd->_compile = ctd;
 264   if (mtd->_last_toplevel_compiles[level - 1] == nullptr || mtd->_last_toplevel_compiles[level - 1]->compile_id() < compile_id) {
 265     mtd->_last_toplevel_compiles[level - 1] = ctd;
 266     mtd->_highest_top_level = MAX2(mtd->_highest_top_level, level);
 267   }
 268   return ctd;
 269 }
 270 
 271 void CompileTrainingData::dec_init_deps_left(KlassTrainingData* ktd) {
 272   LogStreamHandle(Trace, training) log;
 273   if (log.is_enabled()) {
 274     log.print("CTD "); print_on(&log); log.cr();
 275     log.print("KTD "); ktd->print_on(&log); log.cr();
 276   }
 277   assert(ktd!= nullptr && ktd->has_holder(), "");
 278   assert(_init_deps.contains(ktd), "");
 279   assert(_init_deps_left > 0, "");
 280 
 281   Atomic::sub(&_init_deps_left, 1);
 282 }
 283 
 284 void CompileTrainingData::print_on(outputStream* st, bool name_only) const {
 285   _method->print_on(st, true);
 286   st->print("#%dL%d", _compile_id, _level);
 287   if (name_only) {
 288     return;
 289   }
 290   #define MAYBE_TIME(Q, _qtime) \
 291     if (_qtime != 0) st->print(" " #Q "%.3f", _qtime)
 292   MAYBE_TIME(Q, _qtime);
 293   MAYBE_TIME(S, _stime);
 294   MAYBE_TIME(E, _etime);
 295   if (_init_deps.length() > 0) {
 296     if (_init_deps_left > 0) {
 297       st->print(" udeps=%d", _init_deps_left);
 298     }
 299     for (int i = 0, len = _init_deps.length(); i < len; i++) {
 300       st->print(" dep:");
 301       _init_deps.at(i)->print_on(st, true);
 302     }
 303   }
 304 }
 305 
 306 void CompileTrainingData::record_compilation_queued(CompileTask* task) {
 307   _qtime = tty->time_stamp().seconds();
 308 }
 309 void CompileTrainingData::record_compilation_start(CompileTask* task) {
 310   _stime = tty->time_stamp().seconds();
 311 }
 312 void CompileTrainingData::record_compilation_end(CompileTask* task) {
 313   _etime = tty->time_stamp().seconds();
 314   if (task->is_success()) {   // record something about the nmethod output
 315     _nm_total_size = task->nm_total_size();
 316   }
 317 }
 318 void CompileTrainingData::notice_inlined_method(CompileTask* task,
 319                                                 const methodHandle& method) {
 320   MethodTrainingData* mtd = MethodTrainingData::make(method);
 321   if (mtd != nullptr) {
 322     mtd->notice_compilation(task->comp_level(), true);
 323   }
 324 }
 325 
 326 void CompileTrainingData::notice_jit_observation(ciEnv* env, ciBaseObject* what) {
 327   // A JIT is starting to look at class k.
 328   // We could follow the queries that it is making, but it is
 329   // simpler to assume, conservatively, that the JIT will
 330   // eventually depend on the initialization state of k.
 331   CompileTask* task = env->task();
 332   assert(task != nullptr, "");
 333   Method* method = task->method();
 334   InstanceKlass* compiling_klass = method->method_holder();
 335   if (what->is_metadata()) {
 336     ciMetadata* md = what->as_metadata();
 337     if (md->is_loaded() && md->is_instance_klass()) {
 338       ciInstanceKlass* cik = md->as_instance_klass();
 339 
 340       if (cik->is_initialized()) {
 341         InstanceKlass* ik = md->as_instance_klass()->get_instanceKlass();
 342         KlassTrainingData* ktd = ik->training_data_or_null();
 343         if (ktd != nullptr) {
 344           // This JIT task is (probably) requesting that ik be initialized,
 345           // so add him to my _init_deps list.
 346           TrainingDataLocker l;
 347           add_init_dep(ktd);
 348         }
 349       }
 350     }
 351   }
 352 }
 353 
 354 void KlassTrainingData::prepare(Visitor& visitor) {
 355   if (visitor.is_visited(this)) {
 356     return;
 357   }
 358   visitor.visit(this);
 359   ClassLoaderData* loader_data = nullptr;
 360   if (_holder != nullptr) {
 361     loader_data = _holder->class_loader_data();
 362   } else {
 363     loader_data = java_lang_ClassLoader::loader_data(SystemDictionary::java_system_loader()); // default CLD
 364   }
 365   _init_deps.prepare(loader_data);
 366   _comp_deps.prepare(loader_data);
 367 }
 368 
 369 void MethodTrainingData::prepare(Visitor& visitor) {
 370   if (visitor.is_visited(this)) {
 371     return;
 372   }
 373   visitor.visit(this);
 374   klass()->prepare(visitor);
 375   if (has_holder()) {
 376     _final_counters = holder()->method_counters();
 377     _final_profile  = holder()->method_data();
 378     assert(_final_profile == nullptr || _final_profile->method() == holder(), "");
 379   }
 380   if (_compile != nullptr) {
 381     // Just prepare the first one, or prepare them all?  This needs
 382     // an option, because it's useful to dump them all for analysis,
 383     // but it is likely only the first one (= most recent) matters.
 384     for (CompileTrainingData* ctd = _compile; ctd != nullptr; ctd = ctd->next()) {
 385       ctd->prepare(visitor);
 386     }
 387   }
 388 }
 389 
 390 void CompileTrainingData::prepare(Visitor& visitor) {
 391   if (visitor.is_visited(this)) {
 392     return;
 393   }
 394   visitor.visit(this);
 395   method()->prepare(visitor);
 396   ClassLoaderData* loader_data = _method->klass()->class_loader_data();
 397   _init_deps.prepare(loader_data);
 398   _ci_records.prepare(loader_data);
 399 }
 400 
 401 using FieldData = KlassTrainingData::FieldData;
 402 int KlassTrainingData::_clinit_count;  //number <clinit> events in RecordTraining
 403 GrowableArrayCHeap<FieldData, mtCompiler>* KlassTrainingData::_no_static_fields;
 404 
 405 KlassTrainingData* KlassTrainingData::make(Symbol* name, Symbol* loader_name) {
 406   Key key(name, loader_name);
 407   TrainingData* td = have_data() ? lookup_archived_training_data(&key) : nullptr;
 408   KlassTrainingData* ktd = nullptr;
 409   if (td != nullptr) {
 410     ktd = td->as_KlassTrainingData();
 411     return ktd;
 412   }
 413   TrainingDataLocker l;
 414   td = training_data_set()->find(&key);
 415   if (td == nullptr) {
 416     ktd = KlassTrainingData::allocate(name, loader_name);
 417     td = training_data_set()->install(ktd);
 418     assert(ktd == td, "");
 419   } else {
 420     ktd = td->as_KlassTrainingData();
 421   }
 422   assert(ktd != nullptr, "");
 423   return ktd;
 424 }
 425 
 426 KlassTrainingData* KlassTrainingData::make(const char* name, const char* loader_name) {
 427   TempNewSymbol n = SymbolTable::new_symbol(name);
 428   if (loader_name == nullptr) {
 429     return make(n, nullptr);
 430   } else {
 431     TempNewSymbol l = SymbolTable::new_symbol(loader_name);
 432     return make(n, l);
 433   }
 434 }
 435 
 436 KlassTrainingData* KlassTrainingData::make(InstanceKlass* holder, bool null_if_not_found) {
 437   Key key(holder);
 438   TrainingData* td = have_data() ? lookup_archived_training_data(&key) : nullptr;
 439   KlassTrainingData* ktd = nullptr;
 440   if (td != nullptr) {
 441     ktd = td->as_KlassTrainingData();
 442     ktd->refresh_from(holder);
 443     holder->init_training_data(ktd);
 444     guarantee(ktd->has_holder() && ktd->holder() == holder, "");
 445     return ktd;
 446   }
 447   TrainingDataLocker l;
 448   td = training_data_set()->find(&key);
 449   if (td == nullptr) {
 450     if (null_if_not_found) {
 451       return nullptr;
 452     }
 453     ktd = KlassTrainingData::allocate(holder);
 454     td = training_data_set()->install(ktd);
 455     assert(ktd == td, "");
 456   } else {
 457     ktd = td->as_KlassTrainingData();
 458   }
 459   assert(ktd != nullptr, "");
 460   ktd->refresh_from(holder);
 461   bool ok = holder->init_training_data(ktd);
 462   assert(ok, "CAS under mutex cannot fail");
 463   guarantee(ktd->has_holder() && ktd->holder() == holder, "");
 464   return ktd;
 465 }
 466 
 467 void KlassTrainingData::print_on(outputStream* st, bool name_only) const {
 468   name()->print_symbol_on(st);
 469   if (has_holder()) {
 470     switch (holder()->init_state()) {
 471       case InstanceKlass::allocated:            st->print("[A]"); break;
 472       case InstanceKlass::loaded:               st->print("[D]"); break;
 473       case InstanceKlass::being_linked:         st->print("[l]"); break;
 474       case InstanceKlass::linked:               st->print("[L]"); break;
 475       case InstanceKlass::being_initialized:    st->print("[i]"); break;
 476       case InstanceKlass::fully_initialized:    /*st->print("");*/ break;
 477       case InstanceKlass::initialization_error: st->print("[E]"); break;
 478       default: fatal("unknown state: %d", holder()->init_state());
 479     }
 480     if (holder()->is_interface()) {
 481       st->print("I");
 482     }
 483   } else {
 484     st->print("[SYM]");
 485   }
 486   if (name_only) {
 487     return;
 488   }
 489   if (_clinit_sequence_index) {
 490     st->print("IC%d", _clinit_sequence_index);
 491   }
 492   for (int i = 0, len = _init_deps.length(); i < len; i++) {
 493     st->print(" dep:");
 494     _init_deps.at(i)->print_on(st, true);
 495   }
 496 }
 497 
 498 void KlassTrainingData::refresh_from(const InstanceKlass* klass) {
 499   if (!has_holder()) {
 500     init_holder(klass);
 501   }
 502   if (holder() == klass) {
 503     if (klass->is_initialized() && !_clinit_is_done) {
 504       _clinit_is_done = true;
 505     }
 506   }
 507 }
 508 
 509 void KlassTrainingData::init_holder(const InstanceKlass* klass) {
 510   if (holder() == klass) {
 511     return;   // no change to make
 512   }
 513 
 514   jobject hmj = _holder_mirror;
 515   if (hmj != nullptr) {   // clear out previous handle, if any
 516     _holder_mirror = nullptr;
 517     assert(JNIHandles::is_global_handle(hmj), "");
 518     JNIHandles::destroy_global(hmj);
 519   }
 520 
 521   // reset state derived from any previous klass
 522   _static_fields = nullptr;
 523   _fieldinit_count = 0;
 524   _clinit_is_done = false;
 525   _clinit_sequence_index = 0;
 526 
 527   // Keep the klass alive during the training run, unconditionally.
 528   //
 529   // FIXME: Revisit this decision; we could allow training runs to
 530   // unload classes in the normal way.  We might use make_weak_global
 531   // instead of make_global.
 532   //
 533   // The data from the training run would mention the name of the
 534   // unloaded class (and of its loader).  Is it worth the complexity
 535   // to track and then unload classes, remembering just their names?
 536 
 537   if (klass != nullptr) {
 538     Handle hm(JavaThread::current(), klass->java_mirror());
 539     hmj = JNIHandles::make_global(hm);
 540     Atomic::release_store(&_holder_mirror, hmj);
 541   }
 542 
 543   Atomic::release_store(&_holder, const_cast<InstanceKlass*>(klass));
 544   assert(holder() == klass, "");
 545 
 546   if (klass == nullptr)  return;
 547 
 548   bool is_init = klass->is_initialized();
 549   _clinit_is_done = is_init;
 550   if (is_init) {
 551     // if <clinit> is in the past, do not bother tracking fields
 552     _static_fields = no_static_fields();
 553   } else {
 554     setup_static_fields(klass);
 555   }
 556 }
 557 
 558 void KlassTrainingData::record_initialization_start() {
 559   if (!TrainingData::need_data()) {
 560     return;
 561   }
 562   //assert(_clinit_sequence_index == 0, "set this under mutex");
 563   _clinit_sequence_index = next_clinit_count();
 564   log_initialization(true);
 565 }
 566 
 567 bool KlassTrainingData::add_initialization_touch(Klass* requester) {
 568   _has_initialization_touch = true;
 569   if (requester == nullptr || !requester->is_instance_klass())
 570     return false;
 571   auto rtd = KlassTrainingData::make(InstanceKlass::cast(requester));
 572   if (rtd != nullptr && rtd != this) {
 573     // The requester is asking that I be initialized; this means
 574     // that I should be added to his _init_deps list.up
 575     TrainingDataLocker l;
 576     rtd->add_init_dep(this);
 577   }
 578   return true;
 579 }
 580 
 581 void KlassTrainingData::record_initialization_end() {
 582   _clinit_is_done = true;  // we know this now
 583   log_initialization(false);
 584 }
 585 
 586 void KlassTrainingData::notice_fully_initialized() {
 587   TrainingDataLocker l; // Not a real lock if we don't collect the data,
 588                         // that's why we need the atomic decrement below.
 589   for (int i = 0; i < comp_dep_count(); i++) {
 590     comp_dep(i)->dec_init_deps_left(this);
 591   }
 592 }
 593 
 594 GrowableArrayCHeap<FieldData, mtCompiler>*
 595 KlassTrainingData::no_static_fields() {
 596   GrowableArrayCHeap<FieldData, mtCompiler>* nsf = _no_static_fields;
 597   if (nsf != nullptr) {
 598     return nsf;
 599   }
 600   nsf = new GrowableArrayCHeap<FieldData, mtCompiler>(0);
 601   if (nsf != nullptr && !Atomic::replace_if_null(&_no_static_fields, nsf)) {
 602     delete nsf;
 603     nsf = _no_static_fields;
 604   }
 605   return nsf;
 606 }
 607 
 608 // Note:  Racers may do this more than once.
 609 // So, make no externally visible side effects.
 610 void KlassTrainingData::setup_static_fields(const InstanceKlass* holder) {
 611   auto fda = Atomic::load_acquire(&_static_fields);
 612   if (fda != nullptr)  return;
 613   fda = new GrowableArrayCHeap<FieldData, mtCompiler>();
 614   int num_statics = 0;
 615   for (JavaFieldStream fs(holder); !fs.done(); fs.next()) {
 616     if (!fs.access_flags().is_static())
 617       continue;  // only tracking static fields
 618     if (fs.access_flags().is_final() && fs.initval_index() != 0)
 619       continue;  // skip constants initialized directly by the JVM
 620     fda->append(FieldData());
 621     // set up tracking data for the field
 622     FieldData& data = fda->adr_at(num_statics)[0];
 623     data.init_from(fs.field_descriptor());
 624     if (!field_state_is_clean(&data)) {
 625       data._fieldinit_sequence_index = ++_fieldinit_count;
 626     }
 627     ++num_statics;
 628   }
 629   if (num_statics == 0) {
 630     delete fda;
 631     fda = no_static_fields();
 632   }
 633 
 634   // After the array is set up, store it; arbitrate among racers.
 635   if (!Atomic::replace_if_null(&_static_fields, fda)) {
 636     if (fda != no_static_fields()) {
 637       delete fda;
 638     }
 639   }
 640 }
 641 
 642 // Combined linear search pass to find the name, and also
 643 // note missed field updates.  It could be a fancy binary search,
 644 // except we want to do a linear walk anyway to look for updates.
 645 // It is possible we missed an initial `putstatic`, or maybe it never happened.
 646 // Work around the leaky detection by periodic checks for evidence of inits.
 647 KlassTrainingData::FieldData*
 648 KlassTrainingData::check_field_states_and_find_field(Symbol* name) {
 649   int len;
 650   if (_static_fields == nullptr || (len = _static_fields->length()) == 0)
 651     return nullptr;
 652   FieldData* result = nullptr;
 653   for (int i = 0; i < len; i++) {
 654     FieldData* fdata = _static_fields->adr_at(i);
 655     if (fdata->_name == name)  result = fdata;
 656     if (fdata->_fieldinit_sequence_index == 0 &&
 657         !field_state_is_clean(fdata)) {
 658       // Oops, a missed update.  Track it after the fact.
 659       assert(!all_field_states_done(), "");
 660       record_static_field_init(fdata, "unknown");
 661     }
 662   }
 663   return result;
 664 }
 665 
 666 bool KlassTrainingData::record_static_field_init(FieldData* fdata,
 667                                             const char* reason) {
 668   int& seq = fdata->_fieldinit_sequence_index;
 669   int PENDING = -1;
 670   int found = Atomic::cmpxchg(&seq, 0, PENDING, memory_order_conservative);
 671   if (found != 0)  return false;  // racer beat us to it
 672   Atomic::store(&seq, next_fieldinit_count());
 673   {
 674     ttyLocker ttyl;
 675     xtty->begin_elem("initialize_static_field");
 676     xtty->klass(holder());
 677     print_iclock_attr(holder(), xtty, seq);
 678     xtty->name(fdata->_name);
 679     xtty->print(" reason='%s'", reason);
 680     xtty->thread();
 681     xtty->stamp();
 682     xtty->end_elem();
 683   }
 684   return true;
 685 }
 686 
 687 void KlassTrainingData::print_klass_attrs(xmlStream* xtty,
 688                                      Klass* klass, const char* prefix) {
 689   if (!klass)  return;
 690   xtty->klass(klass, prefix);
 691   if (!klass->is_instance_klass())  return;
 692 
 693   // print a little more information in case it is useful
 694   InstanceKlass* ik = InstanceKlass::cast(klass);
 695   int ikf = ik->access_flags().as_int() & (u2)-1;
 696   ikf &= ~JVM_ACC_SUPER;  // this is strictly noise
 697   char ikf2[20];
 698   char* ikf2p = &ikf2[0];
 699   if (ik->is_sealed()) { *ikf2p++ = 's'; }
 700   *ikf2p = 0;
 701   // no need for is_hidden since the name makes it obvious
 702   xtty->print(" %skflags='%d%s'", prefix, ikf, &ikf2[0]);
 703   print_iclock_attr(ik, xtty, -1, prefix);
 704 }
 705 
 706 void KlassTrainingData::print_iclock_attr(InstanceKlass* klass,
 707                                           xmlStream* xtty,
 708                                           int fieldinit_index,
 709                                           const char* prefix) {
 710   KlassTrainingData* tdata = klass->training_data_or_null();
 711   const int all_fields_done = 9999;
 712   int clinit_index = 0;
 713   if (tdata != nullptr) {
 714     if (fieldinit_index < 0) {
 715       if (tdata->_clinit_is_done)
 716         fieldinit_index = all_fields_done;
 717       else {
 718         fieldinit_index = tdata->_fieldinit_count;
 719         if (fieldinit_index > 900) {
 720           // ... 42.899, 42.900, 42.900901, 42.900902, ... 42.930000
 721           fieldinit_index += 900000;
 722         }
 723       }
 724     }
 725     clinit_index = tdata->clinit_sequence_index_or_zero();
 726   }
 727   const char* istate = "";
 728   if (klass->is_initialized()) {
 729     if (tdata != nullptr)
 730       tdata->_clinit_is_done = true;  // notice this, just in case
 731     fieldinit_index = all_fields_done;
 732   } else if (klass->is_not_initialized()) {
 733     if (tdata == nullptr || clinit_index != 0)
 734       istate = "U";
 735   } else if (klass->is_being_initialized()) {
 736     // check for intermediate states:  R = recursive, O = other thread
 737     istate = klass->is_init_thread(JavaThread::current()) ? "R" : "O";
 738   } else {
 739     istate = "E";  // initialization error, which is very rare
 740   }
 741   if (fieldinit_index < 0)
 742     fieldinit_index = 0;
 743   if (fieldinit_index < 100000)
 744     xtty->print(" %siclock='%d.%03d%s'", prefix,
 745                 clinit_index, fieldinit_index, istate);
 746   else
 747     // avoid clock wrap for ridiculous field counts
 748     xtty->print(" %siclock='%d.%06d%s'", prefix,
 749                 clinit_index, fieldinit_index, istate);
 750 }
 751 
 752 
 753 // Decide if the field state looks clean.
 754 // Without further effort we cannot tell if someone has just stored
 755 // the default value, so this query can return false positives,
 756 // claims that a field is "clean" even if it has been subject to updates.
 757 bool KlassTrainingData::field_state_is_clean(FieldData* fdata) {
 758   oop mirror = holder()->java_mirror();
 759   int fo = fdata->_offset;
 760   switch (fdata->_type) {
 761   case T_OBJECT:
 762   case T_ARRAY:
 763     return (mirror->obj_field(fo) == nullptr);
 764   case T_BYTE:
 765     return (mirror->byte_field(fo) == 0);
 766   case T_BOOLEAN:
 767     return (mirror->bool_field(fo) == 0);
 768   case T_CHAR:
 769     return (mirror->char_field(fo) == 0);
 770   case T_SHORT:
 771     return (mirror->short_field(fo) == 0);
 772   case T_INT:
 773   case T_FLOAT:
 774     // use int field format to test for zero because of -0.0f
 775     return (mirror->int_field(fo) == 0);
 776   case T_LONG:
 777   case T_DOUBLE:
 778     // use long field format to test for zero because of -0.0d
 779     return (mirror->long_field(fo) == 0);
 780   default:
 781     break;
 782   }
 783   return true;
 784 }
 785 
 786 // called externally
 787 bool KlassTrainingData::record_static_field_init(fieldDescriptor* fd,
 788                                             const char* reason) {
 789   if (!_static_fields)  return false;  // should not happen unless OOM
 790   if (fd->field_holder() != holder())  return false;  // should not happen...
 791   FieldData* fdp = check_field_states_and_find_field(fd->name());
 792   if (fdp == nullptr)  return false;
 793   return record_static_field_init(fdp, reason);
 794 }
 795 
 796 void KlassTrainingData::record_initialization_touch(const char* reason,
 797                                                     Symbol* name,
 798                                                     Symbol* sig,
 799                                                     Klass* requesting_klass,
 800                                                     const char* context,
 801                                                     TRAPS) {
 802   Klass* init_klass = THREAD->class_being_initialized();
 803   if (!strcmp(reason, "super")) {
 804     // Extra-special touch during class initialization per JVMS Step 7.
 805     // We track this touch as if from RK.<clinit>, even if RK doesn't have one.
 806     init_klass = requesting_klass;
 807     requesting_klass = nullptr;  // ignore any real <clinit> on stack
 808   }
 809   add_initialization_touch(init_klass ? init_klass : requesting_klass);
 810 }
 811 
 812 void KlassTrainingData::log_initialization(bool is_start) {
 813   if (xtty == nullptr)  return;
 814   ttyLocker ttyl;
 815   // Note:  These XML records might not nest properly.
 816   // So we use <init/> and <init_done/>, not <init> and </init>.
 817   if (is_start) {
 818     xtty->begin_elem("initialization");
 819     print_klass_attrs(xtty, holder());
 820     xtty->thread();
 821     xtty->stamp();
 822     xtty->end_elem();
 823   } else {
 824     xtty->begin_elem("initialization_done");
 825     print_klass_attrs(xtty, holder());
 826     xtty->thread();
 827     xtty->stamp();
 828     xtty->end_elem();
 829   }
 830 }
 831 
 832 void TrainingData::init_dumptime_table(TRAPS) {
 833   if (!need_data()) {
 834     return;
 835   }
 836   _dumptime_training_data_dictionary = new GrowableArrayCHeap<DumpTimeTrainingDataInfo, mtClassShared>();
 837   if (CDSConfig::is_dumping_final_static_archive()) {
 838     class Transfer {
 839     public:
 840       void do_value(TrainingData* record) {
 841         _dumptime_training_data_dictionary->append(record);
 842       }
 843     } transfer;
 844     _archived_training_data_dictionary.iterate(&transfer);
 845   } else {
 846     ResourceMark rm;
 847     Visitor visitor(training_data_set()->size());
 848     training_data_set()->iterate_all([&](const TrainingData::Key* k, TrainingData* td) {
 849       td->prepare(visitor);
 850       if (!td->is_CompileTrainingData()) {
 851         _dumptime_training_data_dictionary->append(td);
 852       }
 853     });
 854   }
 855 
 856   prepare_recompilation_schedule(CHECK);
 857 }
 858 
 859 void TrainingData::prepare_recompilation_schedule(TRAPS) {
 860   if (!need_data()) {
 861     return;
 862   }
 863   auto nmethods = MethodProfiler::sampled_nmethods();
 864   GrowableArray<MethodTrainingData*> dyn_recompilation_schedule;
 865   for (auto it = nmethods->begin(); it != nmethods->end(); ++it) {
 866     nmethod* nm = *it;
 867     if (RecordOnlyTopCompilations && nm->method_profiling_count() == 0) {
 868       break;
 869     }
 870     if (nm->method() != nullptr) {
 871       MethodTrainingData* mtd = nm->method()->training_data_or_null();
 872       if (mtd != nullptr) {
 873         dyn_recompilation_schedule.append(mtd);
 874       }
 875     }
 876   }
 877   delete nmethods;
 878   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 879   _recompilation_schedule_for_dumping = MetadataFactory::new_array<MethodTrainingData*>(loader_data, dyn_recompilation_schedule.length(), THREAD);
 880   int i = 0;
 881   for (auto it = dyn_recompilation_schedule.begin(); it != dyn_recompilation_schedule.end(); ++it) {
 882     _recompilation_schedule_for_dumping->at_put(i++, *it);
 883   }
 884 }
 885 
 886 #if INCLUDE_CDS
 887 void TrainingData::iterate_roots(MetaspaceClosure* it) {
 888   if (!need_data()) {
 889     return;
 890   }
 891   assert(_dumptime_training_data_dictionary != nullptr, "");
 892   for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
 893     _dumptime_training_data_dictionary->at(i).metaspace_pointers_do(it);
 894   }
 895   it->push(&_recompilation_schedule_for_dumping);
 896 }
 897 
 898 void TrainingData::dump_training_data() {
 899   if (!need_data()) {
 900     return;
 901   }
 902   write_training_data_dictionary(&_archived_training_data_dictionary_for_dumping);
 903 }
 904 
 905 void TrainingData::cleanup_training_data() {
 906   if (_dumptime_training_data_dictionary != nullptr) {
 907     ResourceMark rm;
 908     Visitor visitor(_dumptime_training_data_dictionary->length());
 909     for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
 910       TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
 911       td->cleanup(visitor);
 912     }
 913   }
 914   _recompilation_status = nullptr;
 915 }
 916 
 917 void KlassTrainingData::cleanup(Visitor& visitor) {
 918   if (visitor.is_visited(this)) {
 919     return;
 920   }
 921   visitor.visit(this);
 922   if (holder() != nullptr) {
 923     bool is_excluded = !holder()->is_loaded() || SystemDictionaryShared::check_for_exclusion(holder(), nullptr);
 924     if (is_excluded) {
 925       ResourceMark rm;
 926       log_debug(cds)("Cleanup KTD %s", name()->as_klass_external_name());
 927       _holder = nullptr; // reset
 928     }
 929   }
 930   for (int i = 0; i < _comp_deps.length(); i++) {
 931     _comp_deps.at(i)->cleanup(visitor);
 932   }
 933 }
 934 
 935 void MethodTrainingData::cleanup(Visitor& visitor) {
 936   if (visitor.is_visited(this)) {
 937     return;
 938   }
 939   visitor.visit(this);
 940   if (has_holder()) {
 941     if (SystemDictionaryShared::check_for_exclusion(holder()->method_holder(), nullptr)) {
 942       log_debug(cds)("Cleanup MTD %s::%s", name()->as_klass_external_name(), signature()->as_utf8());
 943       if (_final_profile != nullptr && _final_profile->method() != _holder) {
 944         // FIXME: MDO still points at the stale method; either completely drop the MDO or zero out the link
 945         log_warning(cds)("Stale MDO for  %s::%s", name()->as_klass_external_name(), signature()->as_utf8());
 946       }
 947       _holder = nullptr;
 948     }
 949   }
 950   for (CompileTrainingData* ctd = _compile; ctd != nullptr; ctd = ctd->next()) {
 951     if (ctd->method() != this) {
 952       ctd->method()->cleanup(visitor);
 953     }
 954   }
 955 }
 956 
 957 void CompileTrainingData::cleanup(Visitor& visitor) {
 958   if (visitor.is_visited(this)) {
 959     return;
 960   }
 961   visitor.visit(this);
 962   method()->cleanup(visitor);
 963 }
 964 
 965 void TrainingData::serialize_training_data(SerializeClosure* soc) {
 966   if (soc->writing()) {
 967     _archived_training_data_dictionary_for_dumping.serialize_header(soc);
 968     soc->do_ptr(&_recompilation_schedule_for_dumping);
 969   } else {
 970     _archived_training_data_dictionary.serialize_header(soc);
 971     soc->do_ptr(&_recompilation_schedule);
 972   }
 973 }
 974 
 975 void TrainingData::adjust_training_data_dictionary() {
 976   if (!need_data()) {
 977     return;
 978   }
 979   assert(_dumptime_training_data_dictionary != nullptr, "");
 980   for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
 981     TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
 982     td = ArchiveBuilder::current()->get_buffered_addr(td);
 983     assert(MetaspaceShared::is_in_shared_metaspace(td) || ArchiveBuilder::current()->is_in_buffer_space(td), "");
 984     td->remove_unshareable_info();
 985   }
 986 }
 987 
 988 void TrainingData::print_archived_training_data_on(outputStream* st) {
 989   st->print_cr("Archived TrainingData Dictionary");
 990   TrainingDataPrinter tdp(st);
 991   TrainingDataLocker::initialize();
 992   _archived_training_data_dictionary.iterate(&tdp);
 993   if (_recompilation_schedule != nullptr && _recompilation_schedule->length() > 0) {
 994     st->print_cr("Archived TrainingData Recompilation Schedule");
 995     for (int i = 0; i < _recompilation_schedule->length(); i++) {
 996       st->print("%4d: ", i);
 997       MethodTrainingData* mtd = _recompilation_schedule->at(i);
 998       if (mtd != nullptr) {
 999         mtd->print_on(st);
1000       } else {
1001         st->print("nullptr");
1002       }
1003       st->cr();
1004     }
1005   }
1006 }
1007 
1008 void TrainingData::Key::metaspace_pointers_do(MetaspaceClosure *iter) {
1009   iter->push(&_name1);
1010   iter->push(&_name2);
1011   iter->push((TrainingData**)&_holder);
1012 }
1013 
1014 void TrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
1015   _key.metaspace_pointers_do(iter);
1016 }
1017 
1018 bool TrainingData::Key::can_compute_cds_hash(const Key* const& k) {
1019   return (k->name1() == nullptr || MetaspaceObj::is_shared(k->name1())) &&
1020          (k->name2() == nullptr || MetaspaceObj::is_shared(k->name2())) &&
1021          (k->holder() == nullptr || MetaspaceObj::is_shared(k->holder()));
1022 }
1023 
1024 uint TrainingData::Key::cds_hash(const Key* const& k) {
1025   uint hash = 0;
1026   if (k->holder() != nullptr) {
1027     hash += SystemDictionaryShared::hash_for_shared_dictionary((address)k->holder());
1028   }
1029   if (k->name1() != nullptr) {
1030     hash += SystemDictionaryShared::hash_for_shared_dictionary((address)k->name1());
1031   }
1032   if (k->name2() != nullptr) {
1033     hash += SystemDictionaryShared::hash_for_shared_dictionary((address)k->name2());
1034   }
1035   return hash;
1036 }
1037 
1038 void TrainingData::write_training_data_dictionary(TrainingDataDictionary* dictionary) {
1039   if (!need_data()) {
1040     return;
1041   }
1042   assert(_dumptime_training_data_dictionary != nullptr, "");
1043   CompactHashtableStats stats;
1044   dictionary->reset();
1045   CompactHashtableWriter writer(_dumptime_training_data_dictionary->length(), &stats);
1046   for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
1047     TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
1048 #ifdef ASSERT
1049     for (int j = i+1; j < _dumptime_training_data_dictionary->length(); j++) {
1050       TrainingData* td1 = _dumptime_training_data_dictionary->at(j).training_data();
1051       assert(!TrainingData::Key::equals(td1, td->key(), -1), "conflict");
1052     }
1053 #endif // ASSERT
1054     td = ArchiveBuilder::current()->get_buffered_addr(td);
1055     uint hash = TrainingData::Key::cds_hash(td->key());
1056     u4 delta = ArchiveBuilder::current()->buffer_to_offset_u4((address)td);
1057     writer.add(hash, delta);
1058   }
1059   writer.dump(dictionary, "training data dictionary");
1060 }
1061 
1062 size_t TrainingData::estimate_size_for_archive() {
1063   if (_dumptime_training_data_dictionary != nullptr) {
1064     return CompactHashtableWriter::estimate_size(_dumptime_training_data_dictionary->length());
1065   } else {
1066     return 0;
1067   }
1068 }
1069 
1070 TrainingData* TrainingData::lookup_archived_training_data(const Key* k) {
1071   // For this to work, all components of the key must be in shared metaspace.
1072   if (!TrainingData::Key::can_compute_cds_hash(k) || _archived_training_data_dictionary.empty()) {
1073     return nullptr;
1074   }
1075   uint hash = TrainingData::Key::cds_hash(k);
1076   TrainingData* td = _archived_training_data_dictionary.lookup(k, hash, -1 /*unused*/);
1077   if (td != nullptr) {
1078     if ((td->is_KlassTrainingData()  && td->as_KlassTrainingData()->has_holder()) ||
1079         (td->is_MethodTrainingData() && td->as_MethodTrainingData()->has_holder())) {
1080       return td;
1081     } else {
1082       // FIXME: decide what to do with symbolic TD
1083       LogStreamHandle(Info,training) log;
1084       if (log.is_enabled()) {
1085         ResourceMark rm;
1086         log.print_cr("Ignored symbolic TrainingData:");
1087         log.print_cr("  Key: %s %s",
1088                      (k->name1() != nullptr ? k->name1()->as_C_string() : "(null)"),
1089                      (k->name2() != nullptr ? k->name2()->as_C_string() : "(null)"));
1090         td->print_on(&log);
1091         log.cr();
1092       }
1093     }
1094   }
1095   return nullptr;
1096 }
1097 #endif
1098 
1099 KlassTrainingData* TrainingData::lookup_for(InstanceKlass* ik) {
1100   if (TrainingData::have_data() && ik != nullptr && ik->is_loaded()) {
1101     TrainingData::Key key(ik);
1102     TrainingData* td = TrainingData::lookup_archived_training_data(&key);
1103     if (td != nullptr && td->is_KlassTrainingData()) {
1104       return td->as_KlassTrainingData();
1105     }
1106   }
1107   return nullptr;
1108 }
1109 
1110 MethodTrainingData* TrainingData::lookup_for(Method* m) {
1111   if (TrainingData::have_data() && m != nullptr) {
1112     KlassTrainingData* holder_ktd = TrainingData::lookup_for(m->method_holder());
1113     if (holder_ktd != nullptr) {
1114       TrainingData::Key key(m->name(), m->signature(), holder_ktd);
1115       TrainingData* td = TrainingData::lookup_archived_training_data(&key);
1116       if (td != nullptr && td->is_MethodTrainingData()) {
1117         return td->as_MethodTrainingData();
1118       }
1119     }
1120   }
1121   return nullptr;
1122 }
1123 
1124 template <typename T>
1125 void TrainingData::DepList<T>::metaspace_pointers_do(MetaspaceClosure* iter) {
1126   iter->push(&_deps);
1127 }
1128 
1129 void KlassTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
1130   log_trace(cds)("Iter(KlassTrainingData): %p", this);
1131   TrainingData::metaspace_pointers_do(iter);
1132   _init_deps.metaspace_pointers_do(iter);
1133   _comp_deps.metaspace_pointers_do(iter);
1134   iter->push(&_holder);
1135 }
1136 
1137 void MethodTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
1138   log_trace(cds)("Iter(MethodTrainingData): %p", this);
1139   TrainingData::metaspace_pointers_do(iter);
1140   iter->push(&_klass);
1141   iter->push((Method**)&_holder);
1142   iter->push(&_compile);
1143   for (int i = 0; i < CompLevel_count; i++) {
1144     iter->push(&_last_toplevel_compiles[i]);
1145   }
1146   iter->push(&_final_profile);
1147   iter->push(&_final_counters);
1148 }
1149 
1150 void CompileTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
1151   log_trace(cds)("Iter(CompileTrainingData): %p", this);
1152   TrainingData::metaspace_pointers_do(iter);
1153   _init_deps.metaspace_pointers_do(iter);
1154   _ci_records.metaspace_pointers_do(iter);
1155   iter->push(&_method);
1156   iter->push(&_next);
1157 }
1158 
1159 template <typename T>
1160 void TrainingData::DepList<T>::prepare(ClassLoaderData* loader_data) {
1161   if (_deps == nullptr && _deps_dyn != nullptr) {
1162     JavaThread* THREAD = JavaThread::current();
1163     int len = _deps_dyn->length();
1164     _deps = MetadataFactory::new_array<T>(loader_data, len, THREAD);
1165     for (int i = 0; i < len; i++) {
1166       _deps->at_put(i, _deps_dyn->at(i)); // copy
1167     }
1168   }
1169 }
1170 
1171 KlassTrainingData* KlassTrainingData::allocate(InstanceKlass* holder) {
1172   assert(need_data() || have_data(), "");
1173   JavaThread* THREAD = JavaThread::current();
1174 
1175   size_t size = align_metadata_size(align_up(sizeof(KlassTrainingData), BytesPerWord) / BytesPerWord);
1176 
1177   ClassLoaderData* loader_data = holder->class_loader_data();
1178   return new (loader_data, size, MetaspaceObj::KlassTrainingDataType, THREAD)
1179       KlassTrainingData(holder);
1180 }
1181 
1182 KlassTrainingData* KlassTrainingData::allocate(Symbol* name, Symbol* loader_name) {
1183   assert(need_data() || have_data(), "");
1184   JavaThread* THREAD = JavaThread::current();
1185 
1186   size_t size = align_metadata_size(align_up(sizeof(KlassTrainingData), BytesPerWord) / BytesPerWord);
1187 
1188   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1189   return new (loader_data, size, MetaspaceObj::KlassTrainingDataType, THREAD)
1190       KlassTrainingData(name, loader_name);
1191 }
1192 
1193 MethodTrainingData* MethodTrainingData::allocate(KlassTrainingData* ktd, Symbol* name, Symbol* signature) {
1194   assert(need_data() || have_data(), "");
1195   JavaThread* THREAD = JavaThread::current();
1196 
1197   size_t size = align_metadata_size(align_up(sizeof(MethodTrainingData), BytesPerWord) / BytesPerWord);
1198 
1199   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1200   return new (loader_data, size, MetaspaceObj::KlassTrainingDataType, THREAD)
1201       MethodTrainingData(ktd, name, signature);
1202 }
1203 
1204 MethodTrainingData* MethodTrainingData::allocate(KlassTrainingData* ktd, Method* m) {
1205   assert(need_data() || have_data(), "");
1206   JavaThread* THREAD = JavaThread::current();
1207 
1208   size_t size = align_metadata_size(align_up(sizeof(MethodTrainingData), BytesPerWord) / BytesPerWord);
1209 
1210   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1211   return new (loader_data, size, MetaspaceObj::KlassTrainingDataType, THREAD)
1212       MethodTrainingData(ktd, m->name(), m->signature());
1213 }
1214 
1215 CompileTrainingData* CompileTrainingData::allocate(MethodTrainingData* mtd, int level, int compile_id) {
1216   assert(need_data() || have_data(), "");
1217   JavaThread* THREAD = JavaThread::current();
1218   size_t size = align_metadata_size(align_up(sizeof(CompileTrainingData), BytesPerWord) / BytesPerWord);
1219 
1220   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1221   return new (loader_data, size, MetaspaceObj::KlassTrainingDataType, THREAD)
1222       CompileTrainingData(mtd, level, compile_id);
1223 }
1224 
1225 static const char* tag(void* p) {
1226   if (p == nullptr) {
1227     return "   ";
1228   } else if (MetaspaceShared::is_shared_dynamic(p)) {
1229     return "<D>";
1230   } else if (MetaspaceShared::is_in_shared_metaspace(p)) {
1231     return "<S>";
1232   } else {
1233     return "???";
1234   }
1235 }
1236 
1237 void TrainingDataPrinter::do_value(const RunTimeClassInfo* record) {
1238   ResourceMark rm;
1239   KlassTrainingData* ktd = record->_klass->training_data_or_null();
1240   if (ktd != nullptr) {
1241     _st->print("%4d: KTD %s%p for %s %s", _index++, tag(ktd), ktd, record->_klass->external_name(),
1242                SystemDictionaryShared::class_loader_name_for_shared(record->_klass));
1243     ktd->print_on(_st);
1244     _st->cr();
1245     ktd->iterate_all_comp_deps([&](CompileTrainingData* ctd) {
1246       ResourceMark rm;
1247       _st->print_raw("    ");
1248       ctd->print_on(_st);
1249     });
1250   }
1251 }
1252 
1253 void TrainingDataPrinter::do_value(const RunTimeMethodDataInfo* record) {
1254   ResourceMark rm;
1255   MethodCounters* mc = record->method_counters();
1256   if (mc != nullptr) {
1257     MethodTrainingData* mtd = mc->method_training_data();
1258     if (mtd != nullptr) {
1259       _st->print("%4d: MTD %s%p ", _index++, tag(mtd), mtd);
1260       mtd->print_on(_st);
1261       _st->cr();
1262 
1263       int i = 0;
1264       for (CompileTrainingData* ctd = mtd->compile(); ctd != nullptr; ctd = ctd->next()) {
1265         _st->print("  CTD[%d]: ", i++);
1266         ctd->print_on(_st);
1267         _st->cr();
1268       }
1269     }
1270   }
1271 }
1272 
1273 void TrainingDataPrinter::do_value(TrainingData* td) {
1274 #ifdef ASSERT
1275   TrainingData::Key key(td->key()->name1(), td->key()->name2(), td->key()->holder());
1276   assert(td == TrainingData::archived_training_data_dictionary()->lookup(td->key(), TrainingData::Key::cds_hash(td->key()), -1), "");
1277   assert(td == TrainingData::archived_training_data_dictionary()->lookup(&key, TrainingData::Key::cds_hash(&key), -1), "");
1278 #endif // ASSERT
1279 
1280   const char* type = (td->is_KlassTrainingData()   ? "K" :
1281                       td->is_MethodTrainingData()  ? "M" :
1282                       td->is_CompileTrainingData() ? "C" : "?");
1283   _st->print("%4d: %p %s ", _index++, td, type);
1284   td->print_on(_st);
1285   _st->cr();
1286   if (td->is_KlassTrainingData()) {
1287     td->as_KlassTrainingData()->iterate_all_comp_deps([&](CompileTrainingData* ctd) {
1288       ResourceMark rm;
1289       _st->print_raw("  C ");
1290       ctd->print_on(_st);
1291       _st->cr();
1292     });
1293   } else if (td->is_MethodTrainingData()) {
1294     td->as_MethodTrainingData()->iterate_all_compiles([&](CompileTrainingData* ctd) {
1295       ResourceMark rm;
1296       _st->print_raw("  C ");
1297       ctd->print_on(_st);
1298       _st->cr();
1299     });
1300   } else if (td->is_CompileTrainingData()) {
1301     // ?
1302   }
1303 }
1304 
1305 
1306 #if INCLUDE_CDS
1307 void KlassTrainingData::remove_unshareable_info() {
1308   TrainingData::remove_unshareable_info();
1309   _static_fields = nullptr;
1310   _no_static_fields = nullptr;
1311   _holder_mirror = nullptr;
1312   _init_deps.remove_unshareable_info();
1313   _comp_deps.remove_unshareable_info();
1314 }
1315 
1316 void KlassTrainingData::restore_unshareable_info(TRAPS) {
1317   TrainingData::restore_unshareable_info(CHECK);
1318   _init_deps.restore_unshareable_info(CHECK);
1319   _comp_deps.restore_unshareable_info(CHECK);
1320 }
1321 
1322 void MethodTrainingData::remove_unshareable_info() {
1323   TrainingData::remove_unshareable_info();
1324   for (CompileTrainingData* ctd = _compile; ctd != nullptr; ctd = ctd->next()) {
1325     ctd->remove_unshareable_info();
1326   }
1327   if (_final_counters != nullptr) {
1328     _final_counters->remove_unshareable_info();
1329   }
1330   if (_final_profile != nullptr) {
1331     _final_profile->remove_unshareable_info();
1332   }
1333 }
1334 
1335 void MethodTrainingData::restore_unshareable_info(TRAPS) {
1336   TrainingData::restore_unshareable_info(CHECK);
1337   for (CompileTrainingData* ctd = _compile; ctd != nullptr; ctd = ctd->next()) {
1338     ctd->restore_unshareable_info(CHECK);
1339   }
1340   if (_final_counters != nullptr ) {
1341     _final_counters->restore_unshareable_info(CHECK);
1342   }
1343   if (_final_profile != nullptr) {
1344     _final_profile->restore_unshareable_info(CHECK);
1345   }
1346   initialize_deps_tracking();
1347 }
1348 
1349 void CompileTrainingData::remove_unshareable_info() {
1350   TrainingData::remove_unshareable_info();
1351   _init_deps.remove_unshareable_info();
1352   _ci_records.remove_unshareable_info();
1353 }
1354 
1355 void CompileTrainingData::restore_unshareable_info(TRAPS) {
1356   TrainingData::restore_unshareable_info(CHECK);
1357   _init_deps.restore_unshareable_info(CHECK);
1358   _ci_records.restore_unshareable_info(CHECK);
1359 }
1360 #endif // INCLUDE_CDS